代码之家  ›  专栏  ›  技术社区  ›  Pierre-olivier Gendraud

带有ocaml模块的菱形继承[重复]

  •  2
  • Pierre-olivier Gendraud  · 技术社区  · 8 年前

    我有四个模块。

    环,是环的一个子模的场,系数是环的一个子模,系数可除,系数可除,系数可除,系数可除,系数可除,系数可除。

    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的多个定义。 在给定的结构或签名中,名称必须是唯一的。

    你有解决办法吗?

    1 回复  |  直到 8 年前
        1
  •  4
  •   octachron    8 年前

    破坏性替换通常是多重定义困境的答案:

    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
    

    模块类型 Ring , Field CoefficientDivisible 有严格的定义:

    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
    
    推荐文章