归咎于多个OCaml的签名到一个模块
问题描述:
ocaml的结合签名归咎于多个OCaml的签名到一个模块
假设我有两个签名,有序,和现场
module type ORDERED = sig
type t
type comparison = LT | EQ | GT
val cmp : t -> t -> comparison
end
module type FIELD = sig
type t
val (+) : t -> t -> t
val (*) : t -> t -> t
val inv : t -> t
val neg : t -> t
val zero : t
val one : t
end
我想打一个仿函数,它有两个Ordered Fields
并产生另一个Ordered Field
(说操作是组件化的,我们使用字典顺序进行比较)。我如何指定“输入模块”同时满足两个签名?
这里是我想要做一些稻草人语法:
module NaivePair = functor (Left : ORDERED & FIELD) (Right : ORDERED & FIELD) ->
struct
type t = Left.t * Right.t
(* definitions *)
end
它可能的情况是,有采取签名的“联盟”(而不是匿名联合)一种优雅的方式,或创建一个围绕具体的ORDERED
和FIELD
实现的包装模块,它们恰好共享一个类型t。我很好奇OCaml习惯做什么的习惯做法。
答
定义使用include
和with type t := t
一个新的模块类型:
module type ORDERED_FILED = sig
include ORDERED
include FIELD with type t := t
end
没有with type t := t
,定义被拒绝,因为这两个ORDERED
和FIELD
声明同名的类型。 include FIELD with type t := t
是通过ORDERED.t
“替代”t
的FIELD
。
ocamlc -i -c x.ml
看到什么ORDERED_FIELD
是你想要什么:
$ ocamlc -i -c x.ml
...
...
module type ORDERED_FILED =
sig
type t
type comparison = LT | EQ | GT
val cmp : t -> t -> comparison
val (+) : t -> t -> t
val (*) : t -> t -> t
val inv : t -> t
val neg : t -> t
val zero : t
val one : t
end