gpt4 book ai didi

ocaml - 在参数化模块上创建参数化模块

转载 作者:行者123 更新时间:2023-12-02 02:12:48 24 4
gpt4 key购买 nike

我正在尝试编写一个模块,该模块在另一个可以以各种方式实现的模块上实现算法。所以我的想法是将第一个模块编写为

module type Base = sig
type t
val f : t -> t
end

然后我编写了第二个模块,该模块通过与 Base 兼容的模块进行参数化:

module type BasedOnBase = functor (B : Base) -> sig
type b
val g : B.t -> b
end

现在我正在尝试编写一个在与 BasedOnBase 兼容的模块上进行参数化的模块,这就是我陷入困境的地方。我的天真的方法行不通,我已经尝试过了

(* won't compile *)

module type Alg = functor (BoB : BasedOnBase) -> sig
val h : BoB.b -> bool
end

以及

(* won't compile *)

module type Alg = functor (BoB : functor (B : Base) -> BasedOnBase) -> sig
val h : BoB.b -> bool
end

但这两次尝试都会导致此错误:

[...]
Error: Unbound type constructor BoB.b

所以我显然在这里遗漏了一些东西,但我似乎无法解决这个问题。我将如何实现我想要的目标,可能以完全不同的方式?

最佳答案

你可以这样写:

module type Alg = functor (BoB : BasedOnBase) -> functor (B:Base) -> sig
type t
val h : t -> bool
end with type t = BoB(B).b

这样,您需要在实例化 Alg 类型的模块时传递模块 B:Base,但您的问题中并非如此。

编辑:甚至是这样:

module type Alg =
functor (BoB : BasedOnBase) ->
functor (B : Base) -> sig
val h : BoB(B).b -> bool
end

关于ocaml - 在参数化模块上创建参数化模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12075225/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com