问题 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 在他们的实施中。

现在假设我有另一个模块定义为

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 模块现在应该在两个方面进行参数化:a Something 模块和 AU 模块,这样就像

module Make(P: Something) : (M with type s = P.t) = struct
   type s = P.t
   module Alg = MLAlg (* just an example *)
   ...
end

但我希望有一个通用仿函数给出一个 Something 给了一个 AU 它生成一个模块,两个东西都具体化。有没有办法轻松获得?

仿函数语法很奇怪,我还是新手,我不知道我问的问题是否可以用简单的方法解决。

提前致谢


9000
2017-08-21 13:57


起源



答案:


是的,一个仿函数可以有几个参数。语法是这样的:

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)

在此示例中,从现有代码中获取以确保它在语法上是正确的, Make_LOffset 由两个模块参数化 V 和 LOffset各签名 Lattice_With_Isotropy.S 和 Offsetmap.S。两个签名之间还有其他类型约束 with type … and type … 部分。


15
2017-08-21 14:11



将参数传递给仿函数有不同的语法吗? Make_LOffset(arg1 arg2)? - stumped
@stumped好吧,参数将是模块,所以它们看起来像 Arg1 和 Arg2。语法是 Make_LOffset(Arg1)(Arg2)。我已经更新了答案。 - Pascal Cuoq


答案:


是的,一个仿函数可以有几个参数。语法是这样的:

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)

在此示例中,从现有代码中获取以确保它在语法上是正确的, Make_LOffset 由两个模块参数化 V 和 LOffset各签名 Lattice_With_Isotropy.S 和 Offsetmap.S。两个签名之间还有其他类型约束 with type … and type … 部分。


15
2017-08-21 14:11



将参数传递给仿函数有不同的语法吗? Make_LOffset(arg1 arg2)? - stumped
@stumped好吧,参数将是模块,所以它们看起来像 Arg1 和 Arg2。语法是 Make_LOffset(Arg1)(Arg2)。我已经更新了答案。 - Pascal Cuoq