我可以在一个模块中使用2个sig吗?

时间:2018-04-25 20:51:46

标签: ocaml

我想在OCaml中创建数据库和表数据类型。我可以在1个模块中使用2个sig个关键字吗?您能举例说明如何编写签名以及如何实现签名吗?

1 个答案:

答案 0 :(得分:1)

如果你指的是一个满足2种模块类型的模块,是的,你可以。

module type ADDITIVE = sig
    type t
    val add : t -> t -> t
end

module type MULTIPLICATIVE = sig
    type t
    val multiply : t -> t -> t
end

module Number : sig
    include ADDITIVE
    include MULTIPLICATIVE with type t := t
end = struct
    type t = int
    let add x y = x + y
    let multiply x y = x + y
end

我们说模块Number的签名包含模块类型ADDITIVE(在该上下文中打开t)和MULTIPLICATIVE在{{t中具有相同类型来自t的{​​{1}}的1}},因此我们可以根据签名实现模块。

相关问题