<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <META name="GENERATOR" content="hevea 1.06-7 of 2001-11-14"> <TITLE> Type and exception definitions </TITLE> </HEAD> <BODY TEXT=black BGCOLOR=white> <A HREF="manual015.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A> <A HREF="manual008.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A> <A HREF="manual017.html"><IMG SRC ="next_motif.gif" ALT="Next"></A> <HR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc71"><B><FONT SIZE=5>6.8</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Type and exception definitions</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <BR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#7fff7f"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc72"><B><FONT SIZE=4>6.8.1</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=4>Type definitions</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="s:type-defs"></A><BR> 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.<BR> <BR> <A NAME="@manual.kwd52"></A><BR> <DIV ALIGN=center><TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD ALIGN=right NOWRAP> <TT><I><FONT COLOR=maroon>type-definition</FONT></I></TT></TD> <TD ALIGN=right NOWRAP>::=</TD> <TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>type</FONT></TT> <TT><I><FONT COLOR=maroon>typedef</FONT></I></TT> { <TT><FONT COLOR=blue>and</FONT></TT> <TT><I><FONT COLOR=maroon>typedef</FONT></I></TT> }</TD> </TR> <TR><TD ALIGN=right NOWRAP> <TT><I><FONT COLOR=maroon>typedef</FONT></I></TT></TD> <TD ALIGN=right NOWRAP>::=</TD> <TD ALIGN=left NOWRAP> [<TT><I><FONT COLOR=maroon>type-params</FONT></I></TT>] <TT><I><FONT COLOR=maroon>typeconstr-name</FONT></I></TT> [<TT><I><FONT COLOR=maroon>type-information</FONT></I></TT>]</TD> </TR> <TR><TD ALIGN=right NOWRAP> <TT><I><FONT COLOR=maroon>type-information</FONT></I></TT></TD> <TD ALIGN=right NOWRAP>::=</TD> <TD ALIGN=left NOWRAP> [<TT><I><FONT COLOR=maroon>type-equation</FONT></I></TT>] [<TT><I><FONT COLOR=maroon>type-representation</FONT></I></TT>] { <TT><I><FONT COLOR=maroon>type-constraint</FONT></I></TT> }</TD> </TR> <TR><TD ALIGN=right NOWRAP> <TT><I><FONT COLOR=maroon>type-equation</FONT></I></TT></TD> <TD ALIGN=right NOWRAP>::=</TD> <TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT></TD> </TR> <TR><TD ALIGN=right NOWRAP> <TT><I><FONT COLOR=maroon>type-representation</FONT></I></TT></TD> <TD ALIGN=right NOWRAP>::=</TD> <TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>constr-decl</FONT></I></TT> { <TT><FONT COLOR=blue>|</FONT></TT> <TT><I><FONT COLOR=maroon>constr-decl</FONT></I></TT> }</TD> </TR> <TR><TD ALIGN=right NOWRAP> </TD> <TD ALIGN=right NOWRAP>|</TD> <TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>=</FONT></TT> <TT><FONT COLOR=blue>{</FONT></TT> <TT><I><FONT COLOR=maroon>field-decl</FONT></I></TT> { <TT><FONT COLOR=blue>;</FONT></TT> <TT><I><FONT COLOR=maroon>field-decl</FONT></I></TT> } <TT><FONT COLOR=blue>}</FONT></TT></TD> </TR> <TR><TD ALIGN=right NOWRAP> <TT><I><FONT COLOR=maroon>type-params</FONT></I></TT></TD> <TD ALIGN=right NOWRAP>::=</TD> <TD ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>type-param</FONT></I></TT></TD> </TR> <TR><TD ALIGN=right NOWRAP> </TD> <TD ALIGN=right NOWRAP>|</TD> <TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>type-param</FONT></I></TT> { <TT><FONT COLOR=blue>,</FONT></TT> <TT><I><FONT COLOR=maroon>type-param</FONT></I></TT> } <TT><FONT COLOR=blue>)</FONT></TT></TD> </TR> <TR><TD ALIGN=right NOWRAP> <TT><I><FONT COLOR=maroon>type-param</FONT></I></TT></TD> <TD ALIGN=right NOWRAP>::=</TD> <TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>ident</FONT></I></TT></TD> </TR> <TR><TD ALIGN=right NOWRAP> </TD> <TD ALIGN=right NOWRAP>|</TD> <TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>+</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>ident</FONT></I></TT></TD> </TR> <TR><TD ALIGN=right NOWRAP> </TD> <TD ALIGN=right NOWRAP>|</TD> <TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>-</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>ident</FONT></I></TT></TD> </TR> <TR><TD ALIGN=right NOWRAP> <TT><I><FONT COLOR=maroon>constr-decl</FONT></I></TT></TD> <TD ALIGN=right NOWRAP>::=</TD> <TD ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>cconstr-name</FONT></I></TT></TD> </TR> <TR><TD ALIGN=right NOWRAP> </TD> <TD ALIGN=right NOWRAP>|</TD> <TD ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>ncconstr-name</FONT></I></TT> <TT><FONT COLOR=blue>of</FONT></TT> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT></TD> </TR> <TR><TD ALIGN=right NOWRAP> <TT><I><FONT COLOR=maroon>field-decl</FONT></I></TT></TD> <TD ALIGN=right NOWRAP>::=</TD> <TD ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>field-name</FONT></I></TT> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>poly-typexpr</FONT></I></TT></TD> </TR> <TR><TD ALIGN=right NOWRAP> </TD> <TD ALIGN=right NOWRAP>|</TD> <TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>mutable</FONT></TT> <TT><I><FONT COLOR=maroon>field-name</FONT></I></TT> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>poly-typexpr</FONT></I></TT></TD> </TR> <TR><TD ALIGN=right NOWRAP> <TT><I><FONT COLOR=maroon>type-constraint</FONT></I></TT></TD> <TD ALIGN=right NOWRAP>::=</TD> <TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>constraint</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>ident</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT></TD> </TR></TABLE></DIV> <A NAME="@manual.kwd53"></A> <A NAME="@manual.kwd54"></A><BR> Type definitions are introduced by the <TT>type</TT> keyword, and consist in one or several simple definitions, possibly mutually recursive, separated by the <TT>and</TT> keyword. Each simple definition defines one type constructor.<BR> <BR> 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.<BR> <BR> The optional type parameters are either one type variable <TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>ident</FONT></I></TT>, for type constructors with one parameter, or a list of type variables (<TT><FONT COLOR=blue>'</FONT></TT><TT><I><FONT COLOR=maroon>ident</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB>,...,<TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>ident</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB>), for type constructors with several parameters. Each type parameter may be prefixed by a variance constraint <TT><FONT COLOR=blue>+</FONT></TT> (resp. <TT><FONT COLOR=blue>-</FONT></TT>) 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 ; <EM>i.e.</EM> 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).<BR> <BR> The optional type equation <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> makes the defined type equivalent to the type expression <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> on the right of the <TT><FONT COLOR=blue>=</FONT></TT> 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.<BR> <BR> 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.<BR> <BR> The type representation <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>constr-decl</FONT></I></TT> { <TT><FONT COLOR=blue>|</FONT></TT> <TT><I><FONT COLOR=maroon>constr-decl</FONT></I></TT> } describes a variant type. The constructor declarations <TT><I><FONT COLOR=maroon>constr-decl</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB>, ..., <TT><I><FONT COLOR=maroon>constr-decl</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB> describe the constructors associated to this variant type. The constructor declaration <TT><I><FONT COLOR=maroon>ncconstr-name</FONT></I></TT> <TT><FONT COLOR=blue>of</FONT></TT> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> declares the name <TT><I><FONT COLOR=maroon>ncconstr-name</FONT></I></TT> as a non-constant constructor, whose argument has type <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT>. The constructor declaration <TT><I><FONT COLOR=maroon>cconstr-name</FONT></I></TT> declares the name <TT><I><FONT COLOR=maroon>cconstr-name</FONT></I></TT> as a constant constructor. Constructor names must be capitalized.<BR> <BR> The type representation <TT><FONT COLOR=blue>=</FONT></TT> <TT><FONT COLOR=blue>{</FONT></TT> <TT><I><FONT COLOR=maroon>field-decl</FONT></I></TT> { <TT><FONT COLOR=blue>;</FONT></TT> <TT><I><FONT COLOR=maroon>field-decl</FONT></I></TT> } <TT><FONT COLOR=blue>}</FONT></TT> describes a record type. The field declarations <TT><I><FONT COLOR=maroon>field-decl</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB>, ..., <TT><I><FONT COLOR=maroon>field-decl</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB> describe the fields associated to this record type. The field declaration <TT><I><FONT COLOR=maroon>field-name</FONT></I></TT> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>poly-typexpr</FONT></I></TT> declares <TT><I><FONT COLOR=maroon>field-name</FONT></I></TT> as a field whose argument has type <TT><I><FONT COLOR=maroon>poly-typexpr</FONT></I></TT>. The field declaration <TT><FONT COLOR=blue>mutable</FONT></TT> <TT><I><FONT COLOR=maroon>field-name</FONT></I></TT> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>poly-typexpr</FONT></I></TT> <A NAME="@manual.kwd55"></A> 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.<BR> <BR> The two components of a type definition, the optional equation and the optional representation, can be combined independently, giving rise to four typical situations: <DL COMPACT=compact><DT> <B>Abstract type: no equation, no representation.</B><DD> <BR> 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.<BR> <BR> <DT><B>Type abbreviation: an equation, no representation.</B><DD> <BR> This defines the type constructor as an abbreviation for the type expression on the right of the <TT><FONT COLOR=blue>=</FONT></TT> sign. <BR> <BR> <DT><B>New variant type or record type: no equation, a representation.</B><DD> <BR> This generates a new type constructor and defines associated constructors or fields, through which values of that type can be directly built or inspected.<BR> <BR> <DT><B>Re-exported variant type or record type: an equation, a representation.</B><DD> <BR> 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. </DL> The type variables appearing as type parameters can optionally be prefixed by <TT>+</TT> or <TT>-</TT> 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 <TT><FONT COLOR=blue>>:</FONT></TT> coercions (see section <A HREF="manual015.html#s:objects">6.7.5</A>).<BR> <BR> For instance, <TT>type +'a t</TT> declares <TT>t</TT> as an abstract type that is covariant in its parameter; this means that if the type <FONT FACE=symbol>t</FONT> is a subtype of the type <FONT FACE=symbol>s</FONT>, then <FONT FACE=symbol>t</FONT> <TT> <I>t</I></TT> is a subtype of <FONT FACE=symbol>s</FONT> <TT> <I>t</I></TT>. Similarly, <TT>type -'a t</TT> declares that the abstract type <TT>t</TT> is contravariant in its parameter: if <FONT FACE=symbol>t</FONT> is subtype of <FONT FACE=symbol>s</FONT>, then <FONT FACE=symbol>s</FONT> <TT> <I>t</I></TT> is subtype of <FONT FACE=symbol>t</FONT> <TT> <I>t</I></TT>. If no <TT>+</TT> or <TT>-</TT> variance annotation is given, the type constructor is assumed invariant in the corresponding parameter. For instance, the abstract type declaration <TT>type 'a t</TT> means that <FONT FACE=symbol>t</FONT> <TT> <I>t</I></TT> is neither a subtype nor a supertype of <FONT FACE=symbol>s</FONT> <TT> <I>t</I></TT> if <FONT FACE=symbol>t</FONT> is subtype of <FONT FACE=symbol>s</FONT>.<BR> <BR> The variance indicated by the <TT>+</TT> and <TT>-</TT> 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.<BR> <BR> <A NAME="@manual.kwd56"></A> The construct <TT><FONT COLOR=blue>constraint</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>ident</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> allows to specify type parameters. Any actual type argument corresponding to the type parameter <TT><I><FONT COLOR=maroon>ident</FONT></I></TT> has to be an instance of <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> (more precisely, <TT><I><FONT COLOR=maroon>ident</FONT></I></TT> and <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> are unified). Type variables of <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> can appear in the type equation and the type declaration.<BR> <BR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#7fff7f"><DIV ALIGN=center><TABLE> <TR><TD><A NAME="htoc73"><B><FONT SIZE=4>6.8.2</FONT></B></A></TD> <TD WIDTH="100%" ALIGN=center><B><FONT SIZE=4>Exception definitions</FONT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="s:excdef"></A> <A NAME="@manual.kwd57"></A><BR> <DIV ALIGN=center><TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD ALIGN=right NOWRAP> <TT><I><FONT COLOR=maroon>exception-definition</FONT></I></TT></TD> <TD ALIGN=right NOWRAP>::=</TD> <TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>exception</FONT></TT> <TT><I><FONT COLOR=maroon>constr-decl</FONT></I></TT></TD> </TR> <TR><TD ALIGN=right NOWRAP> </TD> <TD ALIGN=right NOWRAP>|</TD> <TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>exception</FONT></TT> <TT><I><FONT COLOR=maroon>cconstr-name</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>cconstr</FONT></I></TT></TD> </TR> <TR><TD ALIGN=right NOWRAP> </TD> <TD ALIGN=right NOWRAP>|</TD> <TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>exception</FONT></TT> <TT><I><FONT COLOR=maroon>ncconstr-name</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>ncconstr</FONT></I></TT></TD> </TR></TABLE></DIV><BR> Exception definitions add new constructors to the built-in variant type <CODE>exn</CODE> of exception values. The constructors are declared as for a definition of a variant type.<BR> <BR> The form <TT><FONT COLOR=blue>exception</FONT></TT> <TT><I><FONT COLOR=maroon>constr-decl</FONT></I></TT> generates a new exception, distinct from all other exceptions in the system. The form <TT><FONT COLOR=blue>exception</FONT></TT> <TT><I><FONT COLOR=maroon>name</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>constr</FONT></I></TT> gives an alternate name to an existing exception. <BR> <BR> <HR> <A HREF="manual015.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A> <A HREF="manual008.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A> <A HREF="manual017.html"><IMG SRC ="next_motif.gif" ALT="Next"></A> </BODY> </HTML>