Ocaml 在参数化模块上创建参数化模块
我试图编写一个模块,在另一个模块上实现算法,该模块可以以各种方式实现。所以我的想法是把第一个模块写成Ocaml 在参数化模块上创建参数化模块,ocaml,functor,Ocaml,Functor,我试图编写一个模块,在另一个模块上实现算法,该模块可以以各种方式实现。所以我的想法是把第一个模块写成 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 现在我正试图编写一个模块,该模块在一个
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
太棒了,谢谢你。我现在有一个(可能很愚蠢)的后续问题:假设我实现了这个模块,并希望使用来自
BoB
的函数,如:module Alg=functor(BoB:BasedOnBase)->functor(B:Base)->struct[…]let hx=BoB(B).gx end
。这给了我一个语法错误,(BoB(B.g)x
给了我未绑定的构造函数BoB
。你介意再帮我一次吗?这在你的例子中是可行的:模块Alg=functor(BoB:BasedOnBase)->functor(B:Base)->结构模块X=BoB(B);;设hx=x.gx端;;干杯,亚历克斯,这正是我最后要做的。