The difference between modules and existential

This is the knowledge that OCaml modules are "just" existential types. What is there some kind of parity between

module X = struct type t val x : t end

and

data 'a spec = { x : 'a }
data x = X : 'a spec

and this is not entirely true.

But, as I just showed, OCaml has both modules and existential types. My question is:

  • How do they differ?
  • Is there anything that can be implemented in one and not the other?
  • When do you use one on top of the other (in particular, comparing first-class modules with existential types)?
+4
source share
2 answers

Completion of gsg's answer to the third point.

There are two ways to use modules:

  • , . . F , , .

  • . .

: GADT . ( , ).

3 . , . , GADT. GADT , :

type _ ty =
  | List : ty -> ty list
  | Int : int list

type exist = E : 'a ty * 'a -> exist

, , , - GADT.

module type Exist = sig
  type t
  val t : t ty
end
module Int_list : Exist = struct
  type t = int list 
  let t = List Int
end
let int_list = (module Int_list:Exist)

, . , , , .

+2

, . , .

, : , , include module type of, ..

, () arity, , OCaml :

module type M = sig
  type 'a t
  val x : 'a t
end

, . , , , .

+3

All Articles