How to merge OCaml module types (signatures) defining the same type?

谁都会走 提交于 2019-11-30 03:19:18

问题


In OCaml, I have two module types defining a type t:

module type Asig = sig
    type t
    val a : t
end

module type Bsig = sig
    type t
    val b : t
end

I want to automate the creation of a module type merging them. I want to create a module type equivalent to:

module type ABsig_manual = sig
    type t
    val a : t
    val b : t
end

I tried

module type ABsig = sig
    include Asig
    include Bsig
end

but this fails with Error: Multiple definition of the type name t. It seems impossible to add a type constraint to the include so I'm stuck.

Context: I have a module AB that does implement both signatures and I want to feed it to a functor like:

module MakeC(AB) = struct
    type t = AB.t list
    let c = [AB.a; AB.b]
end

module C = MakeC(AB)

I could use two arguments like in:

module UglyMakeC(A : Asig)(B : Bsig with type t = A.t) = struct
    type t = A.t list
    let c = [A.a; B.b]
end

module C = UglyMakeC(AB)(AB)

but this (is ugly and) doesn't scale well to more functors or more signatures to merge.

So, how can I automate merging those two module types? I can modify A and B as needed but I want to keep them separated. Also, maybe my approach is completely wrong, and in that case I'd love pointers to a better direction.

Type sharing in OCaml - typechecker error is related but merges modules, not module types.


回答1:


Here is the way to do it :

module type Asig = sig
    type t
    val a : t
end

module type Bsig = sig
    type t
    val b : t
end

module type ABsig = sig
    include Asig
    include Bsig with type t := t
end

It's called "destructive substitution".



来源:https://stackoverflow.com/questions/32911819/how-to-merge-ocaml-module-types-signatures-defining-the-same-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!