模块和接口的设计

Sof*_*mur 6 ocaml

我想定义一个接口PROPERTY,至少有2个模块TypeFormula匹配它:

module type PROPERTY =
sig
  type t
  val top : t
  val bot : t
  val to_string: t -> string
  val union: t -> t -> t
  val intersection: t -> t -> t
end

module Type = (struct
  type t =
   | Tbot
   | Tint
   | Tbool
   | Ttop
  ...      
end: PROPERTY)

module Formula = (struct
  type t =
   | Fbot
   | Ftop
   | Fplus of int * Type.t
   ...
  let union = 
   ... Type.union ...
  ...      
end: PROPERTY)
Run Code Online (Sandbox Code Playgroud)

有两个要求:

1)我想在Type外面调用构造函数(如有必要,所有程序)

2)Formula包含值的某些值的一部分Types,例如Fplus (5, Type.Tint)类型Formula; 还有一些函数Formula需要调用一些函数Type,例如Formula.union需要调用Type.union

谁能告诉我如何修改上面的声明来满足我的要求?如有必要,可以添加额外的模块......

gas*_*che 6

不要将: PROPERTY密封模具应用于模块声明.这会隐藏返回模块的额外信息.你应该使用:

 module Type = struct .. end
 module Formula = struct .. end
Run Code Online (Sandbox Code Playgroud)

如果您仍想检查TypeFormula确实满足PROPERTY界面,可以单独执行此操作:

 let () =
   ignore (module Type : PROPERTY);
   ignore (module Formula : PROPERTY);
   ()
Run Code Online (Sandbox Code Playgroud)

  • 您不需要在"Type"和"Formula"声明的位置强制抽象接口.应用`ZFUN`仿函数时,将进行界面满意度检查. (3认同)