Forget type constraint in function returning first-class module

Hi folks,

I am a bit stuck.
If I declare these toy modules and functors:

module type Empty = sig end
module type Type = sig type t end
module F(E:Empty): Type with type t = int = struct type t = int end

let f (module E:Empty) = (module F(E): Type with type t = int)
let empty: (module Empty) = (module struct end)

I would like to define a first-class module of type Type from the function f without the type constraint. That is, something of this kind :

let forget: (module Type) = f empty

But this does not compile : the compiler complains that :

This expression has type (module Type with type t = int)
but an expression was expected of type (module Type)

For the record, this is perfectly fine:

let forget: (module Type with type t = int) = f empty

I am surprised of this behaviour, for the standard modules forgetting some type information is generally not a problem (that’s the basis for type abstraction, right ?). Why does it do that and how can I achieve what I want to do ?

Thanks all in advance

You just need to be explicit with the coercion:

let forget: (module Type) = (f empty :> (module Type))
1 Like

Thanks for your solution.

That does indeed solve this toy example but apparently this not sufficient for my real use-case (I still get a type error)… I have to think about it a little before I can further reduce the problem (or hopefully find a solution by myself).

Thanks again