Previous Up Next
6.8 Type and exception definitions

6.8.1 Type definitions

Type definitions bind type constructors to data types: either variant types, record types, type abbreviations, or abstract data types. They also bind the value constructors and record fields associated with the definition.


type-definition ::= type typedef  { and typedef }
typedef ::= [type-params]  typeconstr-name  [type-information]
type-information ::= [type-equation]  [type-representation]  { type-constraint }
type-equation ::= = typexpr
type-representation ::= = constr-decl  { | constr-decl }
  | = { field-decl  { ; field-decl } }
type-params ::= type-param
  | ( type-param  { , type-param } )
type-param ::= ' ident
  | + ' ident
  | - ' ident
constr-decl ::= constr-name
  | constr-name of  typexpr
field-decl ::= field-name :  poly-typexpr
  | mutable field-name :  poly-typexpr
type-constraint ::= constraint ' ident =  typexpr

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 a lowercase identifier, possibly preceded by one or several type parameters, and followed by an optional type equation, then an optional type representation, and then a constraint clause. The identifier is the name of the type constructor being defined.

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. Each type parameter may be prefixed by a variance constraint + (resp. -) indicating that the parameter is covariant (resp. contravariant). These type parameters can appear in the type expressions of the right-hand side of the definition, restricted eventually by a variance constraint ; i.e. a covariant parameter may only appear on the right side of a functional arrow (more precisely, follow the left branch of an even number of arrows), and a convariant parameter only the left side (left branch of an odd number of arrows).

The optional type equation = typexpr makes the defined type equivalent to the type expression typexpr on the right of the = sign: one can be substituted for the other during typing. If no type equation is given, a new type is generated: the defined type is incompatible with any other type.

The optional type representation describes the data structure representing the defined type, by giving the list of associated constructors (if it is a variant type) or associated fields (if it is a record type). If no type representation is given, nothing is assumed on the structure of the type besides what is stated in the optional type equation.

The type representation = constr-decl  { | constr-decl } describes a variant type. The constructor declarations constr-decl1, ...,  constr-decln describe the constructors associated to this variant type. The constructor declaration constr-name of  typexpr declares the name constr-name as a non-constant constructor, whose argument has type typexpr. The constructor declaration constr-name declares the name constr-name as a constant constructor. Constructor names must be capitalized.

The type representation = { field-decl  { ; field-decl } } describes a record type. The field declarations field-decl1, ...,  field-decln describe the fields associated to this record type. The field declaration field-name :  poly-typexpr declares field-name as a field whose argument has type poly-typexpr. The field declaration mutable field-name :  poly-typexpr behaves similarly; in addition, it allows physical modification over the argument to this field. Immutable fields are covariant, but mutable fields are neither covariant nor contravariant. Both mutable and immutable field may have an explicitly polymorphic type. The polymorphism of the contents is statically checked whenever a record value is created or modified. Extracted values may have their types instanciated.

The two components of a type definition, the optional equation and the optional representation, can be combined independently, giving rise to four typical situations:
Abstract type: no equation, no representation.
 
When appearing in a module signature, this definition specifies nothing on the type constructor, besides its number of parameters: its representation is hidden and it is assumed incompatible with any other type.

Type abbreviation: an equation, no representation.
 
This defines the type constructor as an abbreviation for the type expression on the right of the = sign.

New variant type or record type: no equation, a representation.
 
This generates a new type constructor and defines associated constructors or fields, through which values of that type can be directly built or inspected.

Re-exported variant type or record type: an equation, a representation.
 
In this case, the type constructor is defined as an abbreviation for the type expression given in the equation, but in addition the constructors or fields given in the representation remain attached to the defined type constructor. The type expression in the equation part must agree with the representation: it must be of the same kind (record or variant) and have exactly the same constructors or fields, in the same order, with the same arguments.
The type variables appearing as type parameters can optionally be prefixed by + or - to indicate that the type constructor is covariant or contravariant with respect to this parameter. This variance information is used to decide subtyping relations when checking the validity of >: coercions (see section 6.7.5).

For instance, type +'a t declares t as an abstract type that is covariant in its parameter; this means that if the type t is a subtype of the type s, then t t is a subtype of s t. Similarly, type -'a t declares that the abstract type t is contravariant in its parameter: if t is subtype of s, then s t is subtype of t t. If no + or - variance annotation is given, the type constructor is assumed invariant in the corresponding parameter. For instance, the abstract type declaration type 'a t means that t t is neither a subtype nor a supertype of s t if t is subtype of s.

The variance indicated by the + and - annotations on parameters are required only for abstract types. For abbreviations, variant types or record types, the variance properties of the type constructor are inferred from its definition, and the variance annotations are only checked for conformance with the definition.

The construct constraint ' ident =  typexpr allows to specify type parameters. Any actual type argument corresponding to the type parameter ident has to be an instance of typexpr (more precisely, ident and typexpr are unified). Type variables of typexpr can appear in the type equation and the type declaration.

6.8.2 Exception definitions

exception-definition ::= exception constr-name  [of typexpr]
  | exception constr-name =  constr

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.

The form exception constr-name  [of typexpr] generates a new exception, distinct from all other exceptions in the system. The form exception constr-name =  constr gives an alternate name to an existing exception.


Previous Up Next