ocaml: what is an expression that has the keyword val in parenthesis?

Viewed 54

I ran into this piece of code here and I don't understand the syntax:

module Proof = (val p)

I know val can be used in a module type or a signature, but I haven't seen it being used this way.

1 Answers

This syntax is used for unpacking first class modules.

module type IntWrapper = sig
  val wrappedInt : int
end

module MyIntWrapper : IntWrapper = struct
  let wrappedInt = 2
end

(* pack a module into a value *)
let packedModule = (module MyIntWrapper : IntWrapper)

(* unpack module *)
module UnpackedModule = (val packedModule : IntWrapper)

(* or *)
module UnpackedModule' : IntWrapper = (val packedModule)

Reference: Real world ocaml book

Related