使用OCaml模块的Diamond继承

使用OCaml模块的Diamond继承,ocaml,Ocaml,我有四个模块 环,是环的子模的域,是环的子模的系数,是环的子模的可除系数和系数 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

我有四个模块

环,是环的子模的域,是环的子模的系数,是环的子模的可除系数和系数

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说:

文件“CoefficientDivisible.ml”,第7行,字符1-20: 错误:类型名t的多个定义。 名称在给定的结构或签名中必须是唯一的


你有解决办法吗?

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

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 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
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