OCaml中具有多个参数的函子 [英] Functors with multiple arguments in OCaml
问题描述
我有以下情况:
module type M = sig type s = ... end
module Make(P: Something) : (M with type s = P.t) = struct
type s = P.t
...
end
可以很好地生成M
类型的模块,该模块在其实现内使用类型为Something
的模块的特定实现.
that works fine to generate modules of M
type that use specific implementation of modules of type Something
inside their implementation.
现在假设我将另一个模块定义为
Now suppose I have another module defined as
module type AU = sig
val feed : float -> unitv
val nth : int -> (float -> float)
val reset : unit -> unit
end
具有多种实现方式
module SUAlg : AU = struct ... end
module MLAlg : AU = struct ... end
module ACEAlg : AU = struct ... end
问题的关键是,现在应该在两件事上对M
模块进行参数化设置:Something
模块和AU
模块,以使其类似于
The point of the question is that the M
module should be parametrized over two things now: a Something
module and a AU
module so that it's something like
module Make(P: Something) : (M with type s = P.t) = struct
type s = P.t
module Alg = MLAlg (* just an example *)
...
end
但我想有一个给定Something
和给定AU
的通用仿函数,它会生成一个模块,两者都具体化.有没有办法轻松获得它?
but I would like to have a generic functor that given a Something
and given an AU
it produces a module with both things concretized. Is there a way to obtain that easily?
由于functor语法很奇怪,而且我仍然很陌生,所以我不知道我要问的内容是否可以通过简单的方式解决.
Since functor syntax is quite strange and I'm still new to it I don't know if what I'm asking can be solved in a simple way or not.
预先感谢
推荐答案
是的,函子可以具有多个参数.语法如下:
Yes, a functor can have several arguments. The syntax is like this:
module Make_LOffset
(V:Lattice_With_Isotropy.S)
(LOffset : Offsetmap.S with type y = V.t and type widen_hint = V.widen_hint) =
struct
…
end
然后可以将Make_LOffset(V)(LOffset)
应用于函子.
The functor can then be applied with Make_LOffset(V)(LOffset)
.
在此示例中,为确保语法正确而从现有代码中提取了Make_LOffset
的内容,并分别使用了两个签名Lattice_With_Isotropy.S
和Offsetmap.S
的模块V
和LOffset
对其进行了参数化.在with type … and type …
部分这两个签名之间还有其他类型约束.
In this example, taken from existing code to ensure it is syntactically correct, Make_LOffset
is parameterized by two modules V
and LOffset
, of respective signatures Lattice_With_Isotropy.S
and Offsetmap.S
. There are additional type constraints between the two signatures, the with type … and type …
part.
这篇关于OCaml中具有多个参数的函子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!