Global definitions

This section describes the constructs that bind global identifiers (value variables, value constructors, type constructors, record labels).

Type definitions

type-definition:
      type typedef {and typedef}

typedef:
      type-params ident = constr-decl {| constr-decl}
   |  type-params ident = { label-decl {; label-decl} }
   |  type-params ident == typexpr
   |  type-params ident

type-params:
      nothing
   |  ' ident
   |  ( ' ident {, ' ident} )

constr-decl:
      ident
   |  ident of typexpr

label-decl:
      ident : typexpr
   |  mutable ident : typexpr

Type definitions bind type constructors to data types: either variant types, record types, type abbreviations, or abstract data types.

Type definitions are introduced by the type keyword, and consist in one or several simple definitions, possibly mutually recursive, separated by the and keyword. Each simple definition defines one type constructor.

A simple definition consists in an identifier, possibly preceded by one or several type parameters, and followed by a data type description. The identifier is the local name of the type constructor being defined. (The module name for this type constructor is the name of the module being compiled.) The optional type parameters are either one type variable ' ident, for type constructors with one parameter, or a list of type variables (' ident1,...,' identn), for type constructors with several parameters. These type parameters can appear in the type expressions of the right-hand side of the definition.

Variant types

The type definition typeparams ident = constr-decl1 |...| constr-decln defines a variant type. The constructor declarations constr-decl1,...,constr-decln describe the constructors associated to this variant type. The constructor declaration ident of typexpr declares the local name ident (in the module being compiled) as a non-constant constructor, whose argument has type typexpr. The constructor declaration ident declares the local name ident (in the module being compiled) as a constant constructor.

Record types

The type definition typeparams ident = { label-decl1 ;...; label-decln } defines a record type. The label declarations label-decl1,...,label-decln describe the labels associated to this record type. The label declaration ident : typexpr declares the local name ident in the module being compiled as a label, whose argument has type typexpr. The label declaration mutable ident : typexpr behaves similarly; in addition, it allows physical modification over the argument to this label.

Type abbreviations

The type definition typeparams ident == typexpr defines the type constructor ident as an abbreviation for the type expression typexpr.

Abstract types

The type definition typeparams ident defines ident as an abstract type. When appearing in a module interface, this definition allows exporting a type constructor while hiding how it is represented in the module implementation.

typeparams ident mutable behaves similarly, but makes it apparent that the type ident is implemented by a data type accepting physical

Exception definitions

exception-definition:
      exception constr-decl {and constr-decl}

Exception definitions add new constructors to the built-in variant type exn of exception values. The constructors are declared as for a definition of a variant type.