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

N_A*_*row 9 ocaml functor

我正在尝试编写一个模块,在另一个模块上实现算法,可以通过各种方式实现.所以我的想法是把第一个模块写成

module type Base = sig
  type t
  val f : t -> t
end
Run Code Online (Sandbox Code Playgroud)

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

module type BasedOnBase = functor (B : Base) -> sig
  type b
  val g : B.t -> b
end
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试编写一个模块,该模块通过兼容的模块进行参数化BasedOnBase,这就是我遇到的问题.我的天真方法不起作用,我试过了

(* won't compile *)

module type Alg = functor (BoB : BasedOnBase) -> sig
  val h : BoB.b -> bool
end
Run Code Online (Sandbox Code Playgroud)

以及

(* won't compile *)

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

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

[...]
Error: Unbound type constructor BoB.b
Run Code Online (Sandbox Code Playgroud)

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

jro*_*uie 8

你可以这样写:

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

有了这个,你需要B:Base在实例化类型的模块时传递一个模块Alg,这在你的问题中并非如此.

编辑:甚至这个:

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

  • 这适用于你的情况:模块Alg = functor(BoB:BasedOnBase) - > functor(B:Base) - > struct module X = BoB(B);; 让hx = Xg x end ;; (3认同)