ocaml - OCaml 模块的菱形继承(钻石问题)

标签 ocaml

我有四个模块。

Ring,Field是Ring的子模块,Coefficient是Ring的子模块,Coefficientdivisible是Ring和Coefficicent的子模块。

module type Ring = 
sig
  type t
  val add : t -> t -> t
  multiply : t -> t -> t
  val zero : t
  val one : t   
  val opposite : t                      (* a+ opposite a =0*)
end

module type Field =
sig
  include Ring
  val division : t -> t-> t
end

module type Coefficient = 
sig
  include Ring
  val stringDeCoef : t -> string
end

module type Coefficientvisible = 
sig
  include Field
  include Coefficient
end

当我尝试编译前三个模块时,它不会产生问题,但第四个模块返回错误消息,ocamlc 说:

File "CoefficientDivisible.ml", line 7, characters 1-20: Error: Multiple definition of the type name t. Names must be unique in a given structure or signature.

你有解决办法吗?

最佳答案

破坏性替代通常是解决多重定义问题的答案:

module type CoefficientDivisible = sig
   include Field
   include Coefficient with type t := t
end

另一种选择是使用更小的扩展模块类型并将它们组合起来 显式生成基本模块类型的扩展版本。例如, 具有以下扩展模块类型:

module type RingToField = sig
   type t
   val division: t -> t -> t
end

module type RingToCoefficient = 
sig
    type t
    val stringOfCoef : t -> string
end

模块类型RingFieldCoefficientDivisible具有直接的定义:

module type Field = sig
  include Ring
  include RingToField with type t := t
end 

module type Coefficient = sig
  include Ring
  include RingToCoefficient with type t := t
end

module type CoefficientDivisible = sig
  include Field
  include RingToCoefficient with type t := t
end

关于ocaml - OCaml 模块的菱形继承(钻石问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50507847/

相关文章:

string - 使用 OCaml 在字符串中嵌套引号?

ocaml - 为什么这种基于Lwt的看似并发的代码如此不一致

ocaml - js_of_ocaml 中的 Marshal 和 magic_copy

algorithm - 二叉搜索树中序遍历的无环函数算法

list - 列表中的最后一个元素使用 ocaml List.fold_left

ocaml - 如何在另一个 .ml 文件中访问一个 .ml 文件中定义的类型

ocaml - CAMLprim、CAMLexport、CAMLextern

ocaml - 用于检查 OCaml 代码是否编译的 OCaml 程序

c# - 在 C# 中表示参数化枚举的最佳方式?

OCaml中具有特定长度的列表的列表