1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
<!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.08">
<LINK rel="stylesheet" type="text/css" href="manual.css">
<TITLE>
Type and exception definitions
</TITLE>
</HEAD>
<BODY >
<A HREF="manual015.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual008.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="manual017.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>
<H2 CLASS="section"><A NAME="htoc72">6.8</A> Type and exception definitions</H2>
<H3 CLASS="subsection"><A NAME="htoc73">6.8.1</A> Type definitions</H3>
<A NAME="s:type-defs"></A>
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.kwd53"></A><BR>
<BR>
<DIV CLASS="center"><TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD ALIGN=right NOWRAP>
<A NAME="type-definition"></A>
<FONT COLOR=maroon><I><TT>type-definition</TT></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
<FONT COLOR=blue><TT>type</TT></FONT> <FONT COLOR=maroon><I><a href="#typedef"><font color=maroon><TT>typedef</TT></font></a></I></FONT> { <FONT COLOR=blue><TT>and</TT></FONT> <FONT COLOR=maroon><I><a href="#typedef"><font color=maroon><TT>typedef</TT></font></a></I></FONT> }</TD>
</TR>
<TR><TD ALIGN=right NOWRAP>
<A NAME="typedef"></A>
<FONT COLOR=maroon><I><TT>typedef</TT></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
[<FONT COLOR=maroon><I><a href="#type-params"><font color=maroon><TT>type-params</TT></font></a></I></FONT>] <FONT COLOR=maroon><I><a href="manual011.html#typeconstr-name"><font color=maroon><TT>typeconstr-name</TT></font></a></I></FONT> [<FONT COLOR=maroon><I><a href="#type-information"><font color=maroon><TT>type-information</TT></font></a></I></FONT>]</TD>
</TR>
<TR><TD ALIGN=right NOWRAP>
<A NAME="type-information"></A>
<FONT COLOR=maroon><I><TT>type-information</TT></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
[<FONT COLOR=maroon><I><a href="#type-equation"><font color=maroon><TT>type-equation</TT></font></a></I></FONT>] [<FONT COLOR=maroon><I><a href="#type-representation"><font color=maroon><TT>type-representation</TT></font></a></I></FONT>] { <FONT COLOR=maroon><I><a href="#type-constraint"><font color=maroon><TT>type-constraint</TT></font></a></I></FONT> }</TD>
</TR>
<TR><TD ALIGN=right NOWRAP>
<A NAME="type-equation"></A>
<FONT COLOR=maroon><I><TT>type-equation</TT></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
<FONT COLOR=blue><TT>=</TT></FONT> <FONT COLOR=maroon><I><a href="manual012.html#typexpr"><font color=maroon><TT>typexpr</TT></font></a></I></FONT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP>
<A NAME="type-representation"></A>
<FONT COLOR=maroon><I><TT>type-representation</TT></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
<FONT COLOR=blue><TT>=</TT></FONT> <FONT COLOR=maroon><I><a href="#constr-decl"><font color=maroon><TT>constr-decl</TT></font></a></I></FONT> { <FONT COLOR=blue><TT>|</TT></FONT> <FONT COLOR=maroon><I><a href="#constr-decl"><font color=maroon><TT>constr-decl</TT></font></a></I></FONT> }</TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>∣</TD>
<TD ALIGN=left NOWRAP> <FONT COLOR=blue><TT>=</TT> <TT>{</TT></FONT> <FONT COLOR=maroon><I><a href="#field-decl"><font color=maroon><TT>field-decl</TT></font></a></I></FONT> { <FONT COLOR=blue><TT>;</TT></FONT> <FONT COLOR=maroon><I><a href="#field-decl"><font color=maroon><TT>field-decl</TT></font></a></I></FONT> } <FONT COLOR=blue><TT>}</TT></FONT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP>
<A NAME="type-params"></A>
<FONT COLOR=maroon><I><TT>type-params</TT></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
<FONT COLOR=maroon><I><a href="#type-param"><font color=maroon><TT>type-param</TT></font></a></I></FONT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>∣</TD>
<TD ALIGN=left NOWRAP> <FONT COLOR=blue><TT>(</TT></FONT> <FONT COLOR=maroon><I><a href="#type-param"><font color=maroon><TT>type-param</TT></font></a></I></FONT> { <FONT COLOR=blue><TT>,</TT></FONT> <FONT COLOR=maroon><I><a href="#type-param"><font color=maroon><TT>type-param</TT></font></a></I></FONT> } <FONT COLOR=blue><TT>)</TT></FONT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP>
<A NAME="type-param"></A>
<FONT COLOR=maroon><I><TT>type-param</TT></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
<FONT COLOR=blue><TT>'</TT></FONT> <FONT COLOR=maroon><I><a href="manual009.html#ident"><font color=maroon><TT>ident</TT></font></a></I></FONT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>∣</TD>
<TD ALIGN=left NOWRAP> <FONT COLOR=blue><TT>+</TT> <TT>'</TT></FONT> <FONT COLOR=maroon><I><a href="manual009.html#ident"><font color=maroon><TT>ident</TT></font></a></I></FONT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>∣</TD>
<TD ALIGN=left NOWRAP> <FONT COLOR=blue><TT>-</TT> <TT>'</TT></FONT> <FONT COLOR=maroon><I><a href="manual009.html#ident"><font color=maroon><TT>ident</TT></font></a></I></FONT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP>
<A NAME="constr-decl"></A>
<FONT COLOR=maroon><I><TT>constr-decl</TT></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
<FONT COLOR=maroon><I><a href="manual011.html#constr-name"><font color=maroon><TT>constr-name</TT></font></a></I></FONT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>∣</TD>
<TD ALIGN=left NOWRAP> <FONT COLOR=maroon><TT><a href="manual011.html#constr-name"><font color=maroon><I>constr-name</I></font></a></TT> <FONT COLOR=blue><TT>of</TT></FONT> <TT><a href="manual012.html#typexpr"><font color=maroon><I>typexpr</I></font></a></TT></FONT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP>
<A NAME="field-decl"></A>
<FONT COLOR=maroon><I><TT>field-decl</TT></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
<FONT COLOR=maroon><TT><a href="manual011.html#field-name"><font color=maroon><I>field-name</I></font></a></TT> <FONT COLOR=blue><TT>:</TT></FONT> <TT><a href="manual012.html#poly-typexpr"><font color=maroon><I>poly-typexpr</I></font></a></TT></FONT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>∣</TD>
<TD ALIGN=left NOWRAP> <FONT COLOR=blue><TT>mutable</TT></FONT> <FONT COLOR=maroon><TT><a href="manual011.html#field-name"><font color=maroon><I>field-name</I></font></a></TT> <FONT COLOR=blue><TT>:</TT></FONT> <TT><a href="manual012.html#poly-typexpr"><font color=maroon><I>poly-typexpr</I></font></a></TT></FONT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP>
<A NAME="type-constraint"></A>
<FONT COLOR=maroon><I><TT>type-constraint</TT></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
<FONT COLOR=blue><TT>constraint</TT></FONT> <FONT COLOR=blue><TT>'</TT></FONT> <FONT COLOR=maroon><TT><a href="manual009.html#ident"><font color=maroon><I>ident</I></font></a></TT> <FONT COLOR=blue><TT>=</TT></FONT> <TT><a href="manual012.html#typexpr"><font color=maroon><I>typexpr</I></font></a></TT></FONT></TD>
</TR></TABLE></DIV>
<A NAME="@manual.kwd54"></A>
<A NAME="@manual.kwd55"></A><BR>
<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 <FONT COLOR=blue><TT>'</TT></FONT> <FONT COLOR=maroon><I><TT>ident</TT></I></FONT>,
for type constructors with one parameter, or a list of type variables
(<TT><FONT COLOR=blue>'</FONT><FONT COLOR=maroon><I>ident</I></FONT></TT><SUB>1</SUB>,…,<FONT COLOR=blue><TT>'</TT></FONT> <I><FONT COLOR=maroon><TT>ident</TT></FONT><SUB>n</SUB></I>), for type constructors with several
parameters. Each type parameter may be prefixed by a variance
constraint <FONT COLOR=blue><TT>+</TT></FONT> (resp. <FONT COLOR=blue><TT>-</TT></FONT>) 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 contravariant parameter only the left side (left branch of
an odd number of arrows).
<div style="background-color:yellow; color:red; border-style:none; border-width:0.5pt">
If the type has either a representation or
an equation, and the parameter is free (<EM>i.e.</EM> not bound via a
type constraint to a constructed type), its variance constraint is
checked but subtyping <EM>etc.</EM> will use the inferred variance of the
parameter, which may be better; otherwise (<EM>i.e.</EM> for abstract
types or non-free parameters), the variance must be given explicitly,
and the parameter is invariant if no variance was given.<BR>
</div>
<BR>
The optional type equation <FONT COLOR=blue><TT>=</TT></FONT> <FONT COLOR=maroon><I><TT>typexpr</TT></I></FONT> makes the defined type
equivalent to the type expression <FONT COLOR=maroon><I><TT>typexpr</TT></I></FONT> on the right of the <FONT COLOR=blue><TT>=</TT></FONT>
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 <FONT COLOR=blue><TT>=</TT></FONT> <FONT COLOR=maroon><I><TT>constr-decl</TT></I></FONT> { <FONT COLOR=blue><TT>|</TT></FONT> <FONT COLOR=maroon><I><TT>constr-decl</TT></I></FONT> }
describes a variant type. The constructor declarations
<FONT COLOR=maroon><I><TT>constr-decl</TT></I></FONT><SUB>1</SUB>, …, <I><FONT COLOR=maroon><TT>constr-decl</TT></FONT><SUB>n</SUB></I> describe the constructors
associated to this variant type. The constructor
declaration <FONT COLOR=maroon><TT><I>constr-name</I></TT> <FONT COLOR=blue><TT>of</TT></FONT> <TT><I>typexpr</I></TT></FONT> declares the name
<FONT COLOR=maroon><I><TT>constr-name</TT></I></FONT> as a non-constant constructor, whose
argument has type <FONT COLOR=maroon><I><TT>typexpr</TT></I></FONT>. The constructor declaration <FONT COLOR=maroon><I><TT>constr-name</TT></I></FONT>
declares the name <FONT COLOR=maroon><I><TT>constr-name</TT></I></FONT> as a constant
constructor. Constructor names must be capitalized.<BR>
<BR>
The type representation <FONT COLOR=blue><TT>=</TT> <TT>{</TT></FONT> <FONT COLOR=maroon><I><TT>field-decl</TT></I></FONT> { <FONT COLOR=blue><TT>;</TT></FONT> <FONT COLOR=maroon><I><TT>field-decl</TT></I></FONT> } <FONT COLOR=blue><TT>}</TT></FONT>
describes a record type. The field declarations <FONT COLOR=maroon><I><TT>field-decl</TT></I></FONT><SUB>1</SUB>, …,
<I><FONT COLOR=maroon><TT>field-decl</TT></FONT><SUB>n</SUB></I> describe the fields associated to this record type.
The field declaration <FONT COLOR=maroon><TT><I>field-name</I></TT> <FONT COLOR=blue><TT>:</TT></FONT> <TT><I>poly-typexpr</I></TT></FONT> declares
<FONT COLOR=maroon><I><TT>field-name</TT></I></FONT> as a field whose argument has type <FONT COLOR=maroon><I><TT>poly-typexpr</TT></I></FONT>.
The field declaration <FONT COLOR=blue><TT>mutable</TT></FONT> <FONT COLOR=maroon><TT><I>field-name</I></TT> <FONT COLOR=blue><TT>:</TT></FONT> <TT><I>poly-typexpr</I></TT></FONT>
<A NAME="@manual.kwd56"></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 CLASS="description" COMPACT=compact><DT CLASS="dt-description">
<B>Abstract type: no equation, no representation.</B><DD CLASS="dd-description"> <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 CLASS="dt-description"><B>Type abbreviation: an equation, no representation.</B><DD CLASS="dd-description"> <BR>
This defines the type constructor as an abbreviation for the type
expression on the right of the <FONT COLOR=blue><TT>=</TT></FONT> sign. <BR>
<BR>
<DT CLASS="dt-description"><B>New variant type or record type: no equation, a representation.</B><DD CLASS="dd-description"> <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 CLASS="dt-description"><B>Re-exported variant type or record type: an equation,
a representation.</B><DD CLASS="dd-description"> <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 <FONT COLOR=blue><TT>:></TT></FONT> 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 τ is a
subtype of the type σ, then τ <I><TT> t</TT></I> is a subtype of σ
<I><TT> t</TT></I>. Similarly, <TT>type -'a t</TT> declares that the abstract type <TT>t</TT> is
contravariant in its parameter: if τ is subtype of σ, then
σ <I><TT> t</TT></I> is subtype of τ <I><TT> t</TT></I>. 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 τ <I><TT> t</TT></I> is neither a subtype nor a
supertype of σ <I><TT> t</TT></I> if τ is subtype of σ.<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.kwd57"></A>
The construct <FONT COLOR=blue><TT>constraint</TT></FONT> <FONT COLOR=blue><TT>'</TT></FONT> <FONT COLOR=maroon><TT><I>ident</I></TT> <FONT COLOR=blue><TT>=</TT></FONT> <TT><I>typexpr</I></TT></FONT> allows to specify
type parameters. Any actual type argument corresponding to the type
parameter <FONT COLOR=maroon><I><TT>ident</TT></I></FONT> has to be an instance of <FONT COLOR=maroon><I><TT>typexpr</TT></I></FONT> (more precisely,
<FONT COLOR=maroon><I><TT>ident</TT></I></FONT> and <FONT COLOR=maroon><I><TT>typexpr</TT></I></FONT> are unified). Type variables of <FONT COLOR=maroon><I><TT>typexpr</TT></I></FONT> can
appear in the type equation and the type declaration.<BR>
<BR>
<H3 CLASS="subsection"><A NAME="htoc74">6.8.2</A> Exception definitions</H3> <A NAME="s:excdef"></A>
<A NAME="@manual.kwd58"></A><BR>
<BR>
<DIV CLASS="center"><TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD ALIGN=right NOWRAP>
<A NAME="exception-definition"></A>
<FONT COLOR=maroon><I><TT>exception-definition</TT></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
<FONT COLOR=blue><TT>exception</TT></FONT> <FONT COLOR=maroon><I><a href="manual011.html#constr-name"><font color=maroon><TT>constr-name</TT></font></a></I></FONT> [<FONT COLOR=blue><TT>of</TT></FONT> <FONT COLOR=maroon><I><a href="manual012.html#typexpr"><font color=maroon><TT>typexpr</TT></font></a></I></FONT>]</TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>∣</TD>
<TD ALIGN=left NOWRAP> <FONT COLOR=blue><TT>exception</TT></FONT> <FONT COLOR=maroon><TT><a href="manual011.html#constr-name"><font color=maroon><I>constr-name</I></font></a></TT> <FONT COLOR=blue><TT>=</TT></FONT> <TT><a href="manual011.html#constr"><font color=maroon><I>constr</I></font></a></TT></FONT></TD>
</TR></TABLE></DIV><BR>
<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 <FONT COLOR=blue><TT>exception</TT></FONT> <FONT COLOR=maroon><I><TT>constr-name</TT></I></FONT> [<FONT COLOR=blue><TT>of</TT></FONT> <FONT COLOR=maroon><I><TT>typexpr</TT></I></FONT>]
generates a new exception, distinct from all other exceptions in the system.
The form <FONT COLOR=blue><TT>exception</TT></FONT> <FONT COLOR=maroon><TT><I>constr-name</I></TT> <FONT COLOR=blue><TT>=</TT></FONT> <TT><I>constr</I></TT></FONT>
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="Up"></A>
<A HREF="manual017.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
|