module type ModuleTypeName = sig
specifications...
end
If you give a module a type:
module Mod : Sig = struct ... end
The type checker ensures
Sig must be defined in Mod with correct typesSig can be accessed outside Mod. The module is sealed.Sig must be defined in ModMod must be the same as in Sig, or more general, for example:module type IntFun = sig
val f : int -> int
end
module type IntFun = sig val f : int -> int end
We can implement this module with a function of type int -> int.
module SuccFun : IntFun = struct
let f x = x + 1
end
module SuccFun : IntFun
We can also implement this module with a more general type:
module IdFun : IntFun = struct
let f x = x
end
module IdFun : IntFun
Here, the function f has type 'a -> 'a. You will never get a type error for a more general type.
IdFun.f "hello"
File "[4]", line 1, characters 8-15:
1 | IdFun.f "hello"
^^^^^^^
Error: This expression has type string but an expression was expected of type
int
Note that we can't use any other type here, as the module is sealed.