How to combine the types of OCaml modules (signatures) that define the same type?

In OCaml, I have two types of modules defining 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 that combines 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 is not with Error: Multiple definition of the type name t . It seems impossible to add a type constraint to include , so I'm stuck.

Context: I have an AB module that implements both signatures, and I want to pass it to a functor, for example:

 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, for example:

 module UglyMakeC(A : Asig)(B : Bsig with type t = At) = struct type t = At list let c = [Aa; Bb] end module C = UglyMakeC(AB)(AB) 

but this (is ugly and) does not scale well enough for more functors or more signatures for merging.

So how can I automate the merging of these two types of modules? I can change A and B as needed, but I want them to be separated. Also, maybe my approach is completely wrong, in which case I would like the pointers to be in a better direction.

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

+5
source share
1 answer

Here's how 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 is called "destructive substitution."

+10
source

Source: https://habr.com/ru/post/1232818/


All Articles