13 Module Type Syntax and Semantics

Syntax

module type ModuleTypeName = sig
    specifications...
end

Semantics

If you give a module a type:

module Mod : Sig = struct ... end

The type checker ensures

  1. Signature matching: everything specified in Sig must be defined in Mod with correct types
  2. Encapsulation: only what is specified in Sig can be accessed outside Mod. The module is sealed.

Signature matching

We can implement this module with a function of type int -> int.

We can also implement this module with a more general type:

Here, the function f has type 'a -> 'a. You will never get a type error for a more general type.

Note that we can't use any other type here, as the module is sealed.