File: manual010.html

package info (click to toggle)
ocaml-doc 2.04-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,820 kB
  • ctags: 997
  • sloc: makefile: 38; sh: 12
file content (190 lines) | stat: -rw-r--r-- 12,305 bytes parent folder | download
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
<!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">
<TITLE>
 Type expressions
</TITLE>
</HEAD>
<BODY >
<A HREF="manual009.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual011.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="manual006.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<HR>

<H2>5.4&nbsp;&nbsp; Type expressions</H2>
<A NAME="@manual.kwd2"></A><DIV ALIGN=center>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD  ALIGN=right NOWRAP>
<TT><I><FONT COLOR=maroon>typexpr</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>&nbsp;</TD>
<TD  ALIGN=right NOWRAP>|</TD>
<TD  ALIGN=left NOWRAP> <TT><FONT COLOR=blue>_</FONT></TT></TD>
</TR>
<TR><TD  ALIGN=right NOWRAP>&nbsp;</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> <TT><FONT COLOR=blue>)</FONT></TT></TD>
</TR>
<TR><TD  ALIGN=right NOWRAP>&nbsp;</TD>
<TD  ALIGN=right NOWRAP>|</TD>
<TD  ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> <TT><FONT COLOR=blue>-&gt;</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT></TD>
</TR>
<TR><TD  ALIGN=right NOWRAP>&nbsp;</TD>
<TD  ALIGN=right NOWRAP>|</TD>
<TD  ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> &nbsp;{ <TT><FONT COLOR=blue>*</FONT></TT> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> }<SUP><FONT SIZE=2>+</FONT></SUP></TD>
</TR>
<TR><TD  ALIGN=right NOWRAP>&nbsp;</TD>
<TD  ALIGN=right NOWRAP>|</TD>
<TD  ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>typeconstr</FONT></I></TT></TD>
</TR>
<TR><TD  ALIGN=right NOWRAP>&nbsp;</TD>
<TD  ALIGN=right NOWRAP>|</TD>
<TD  ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> &nbsp;<TT><I><FONT COLOR=maroon>typeconstr</FONT></I></TT></TD>
</TR>
<TR><TD  ALIGN=right NOWRAP>&nbsp;</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> &nbsp;{ <TT><FONT COLOR=blue>,</FONT></TT> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> } <TT><FONT COLOR=blue>)</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>typeconstr</FONT></I></TT></TD>
</TR>
<TR><TD  ALIGN=right NOWRAP>&nbsp;</TD>
<TD  ALIGN=right NOWRAP>|</TD>
<TD  ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> <TT><FONT COLOR=blue>as</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>ident</FONT></I></TT></TD>
</TR>
<TR><TD  ALIGN=right NOWRAP>&nbsp;</TD>
<TD  ALIGN=right NOWRAP>|</TD>
<TD  ALIGN=left NOWRAP> <TT><FONT COLOR=blue>&lt;</FONT></TT> [<TT><FONT COLOR=blue>..</FONT></TT>] <TT><FONT COLOR=blue>&gt;</FONT></TT></TD>
</TR>
<TR><TD  ALIGN=right NOWRAP>&nbsp;</TD>
<TD  ALIGN=right NOWRAP>|</TD>
<TD  ALIGN=left NOWRAP> <TT><FONT COLOR=blue>&lt;</FONT></TT> <TT><I><FONT COLOR=maroon>method-type</FONT></I></TT> &nbsp;{ <TT><FONT COLOR=blue>;</FONT></TT> <TT><I><FONT COLOR=maroon>method-type</FONT></I></TT> } &nbsp;[<TT><FONT COLOR=blue>;</FONT></TT> <TT><FONT COLOR=blue>..</FONT></TT>] <TT><FONT COLOR=blue>&gt;</FONT></TT></TD>
</TR>
<TR><TD  ALIGN=right NOWRAP>&nbsp;</TD>
<TD  ALIGN=right NOWRAP>|</TD>
<TD  ALIGN=left NOWRAP> <TT><FONT COLOR=blue>#</FONT></TT> <TT><I><FONT COLOR=maroon>class-path</FONT></I></TT></TD>
</TR>
<TR><TD  ALIGN=right NOWRAP>&nbsp;</TD>
<TD  ALIGN=right NOWRAP>|</TD>
<TD  ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> <TT><FONT COLOR=blue>#</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>class-path</FONT></I></TT></TD>
</TR>
<TR><TD  ALIGN=right NOWRAP>&nbsp;</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> &nbsp;{ <TT><FONT COLOR=blue>,</FONT></TT> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> } ) <TT><FONT COLOR=blue>#</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>class-path</FONT></I></TT></TD>
</TR>
<TR><TD  ALIGN=right NOWRAP>
<TT><I><FONT COLOR=maroon>method-type</FONT></I></TT></TD>
<TD  ALIGN=right NOWRAP>::=</TD>
<TD  ALIGN=left NOWRAP>
 <TT><I><FONT COLOR=maroon>method-name</FONT></I></TT> <TT><FONT COLOR=blue>:</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT></TD>
</TR></TABLE></DIV><BR>
The table below shows the relative precedences and associativity of
operators and non-closed type constructions. The constructions with
higher precedences come first.
<A NAME="@manual.kwd3"></A>
<BR>
<BR>
<DIV ALIGN=center>
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1 WIDTH="80%">
<TR><TD  ALIGN=center NOWRAP><B>Operator</B></TD>
<TD  ALIGN=center NOWRAP><B>Associativity</B></TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>
Type constructor application</TD>
<TD  ALIGN=left NOWRAP>--</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP><TT>*</TT></TD>
<TD  ALIGN=left NOWRAP>--</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP><TT>-&gt;</TT></TD>
<TD  ALIGN=left NOWRAP>right</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP><TT>as</TT></TD>
<TD  ALIGN=left NOWRAP>--</TD>
</TR></TABLE></DIV><BR>
Type expressions denote types in definitions of data types as well as
in type constraints over patterns and expressions.<BR>
<BR>

<H4>Type variables</H4>The type expression <TT><FONT COLOR=blue>'</FONT></TT> <TT><I><FONT COLOR=maroon>ident</FONT></I></TT> stands for the type variable named
<TT><I><FONT COLOR=maroon>ident</FONT></I></TT>. The type expression <TT><FONT COLOR=blue>_</FONT></TT> stands for an anonymous type variable.
In data type definitions, type variables are names for the
data type parameters. In type constraints, they represent unspecified
types that can be instantiated by any type to satisfy the type
constraint.<BR>
<BR>

<H4>Parenthesized types</H4>The type expression <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> <TT><FONT COLOR=blue>)</FONT></TT> denotes the same type as
<TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT>.<BR>
<BR>

<H4>Function types</H4>The type expression <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>-&gt;</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT><SUB><FONT SIZE=2>2</FONT></SUB> denotes the type of
functions mapping arguments of type <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> to results of type
<TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT><SUB><FONT SIZE=2>2</FONT></SUB>.<BR>
<BR>

<H4>Tuple types</H4>The type expression <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>*</FONT></TT> ... <TT><FONT COLOR=blue>*</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB>
denotes the type of tuples whose elements belong to types <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB>,
... &nbsp;<TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB> respectively.<BR>
<BR>

<H4>Constructed types</H4>Type constructors with no parameter, as in <TT><I><FONT COLOR=maroon>typeconstr</FONT></I></TT>, are type
expressions.<BR>
<BR>
The type expression <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> &nbsp;<TT><I><FONT COLOR=maroon>typeconstr</FONT></I></TT>, where <TT><I><FONT COLOR=maroon>typeconstr</FONT></I></TT> is a type
constructor with one parameter, denotes the application of the unary type
constructor <TT><I><FONT COLOR=maroon>typeconstr</FONT></I></TT> to the type <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT>.<BR>
<BR>
The type expression (<TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB>,...,&nbsp;<TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB>) &nbsp;<TT><I><FONT COLOR=maroon>typeconstr</FONT></I></TT>, where
<TT><I><FONT COLOR=maroon>typeconstr</FONT></I></TT> is a type constructor with <I>n</I> parameters, denotes the
application of the <I>n</I>-ary type constructor <TT><I><FONT COLOR=maroon>typeconstr</FONT></I></TT> to the types
<TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> through <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB>.<BR>
<BR>

<H4>Recursive types</H4><A NAME="@manual.kwd4"></A>The type expression <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> <TT><FONT COLOR=blue>as</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>ident</FONT></I></TT> denotes the same type as
<TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT>, and also binds the type variable <TT><I><FONT COLOR=maroon>ident</FONT></I></TT> to type <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> both
in <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> and in the remaining part of the type. If the type variable
<TT><I><FONT COLOR=maroon>ident</FONT></I></TT> actually occurs in <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT>, a recursive type is created. Recursive
types for which there exists a recursive path that does not contain
an object type constructor are rejected. <BR>
<BR>

<H4>Object types</H4>An object type
<TT><FONT COLOR=blue>&lt;</FONT></TT> <TT><I><FONT COLOR=maroon>method-type</FONT></I></TT> &nbsp;{ <TT><FONT COLOR=blue>;</FONT></TT> <TT><I><FONT COLOR=maroon>method-type</FONT></I></TT> } <TT><FONT COLOR=blue>&gt;</FONT></TT>
is a record of method types.<BR>
<BR>
The type <TT><FONT COLOR=blue>&lt;</FONT></TT> <TT><I><FONT COLOR=maroon>method-type</FONT></I></TT> &nbsp;{ <TT><FONT COLOR=blue>;</FONT></TT> <TT><I><FONT COLOR=maroon>method-type</FONT></I></TT> } <TT><FONT COLOR=blue>;</FONT></TT> <TT><FONT COLOR=blue>..</FONT></TT> <TT><FONT COLOR=blue>&gt;</FONT></TT> is the
type of an object with methods and their associated types are described by
<TT><I><FONT COLOR=maroon>method-type</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB>, ..., &nbsp;<TT><I><FONT COLOR=maroon>method-type</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB>, and possibly some other
methods represented by the ellipsis. This ellipsis actually is
a special kind of type variable (also called <EM>row variable</EM> in the
literature) that stands for any number of extra method types.<BR>
<BR>

<H4>#-types</H4>
<A NAME="s:sharp-types"></A>The type <TT><FONT COLOR=blue>#</FONT></TT> <TT><I><FONT COLOR=maroon>class-path</FONT></I></TT> is a special kind of abbreviation. This
abbreviation unifies with the type of any object belonging to a subclass
of class <TT><I><FONT COLOR=maroon>class-path</FONT></I></TT>.
It is handled in a special way as it usually hides a type variable (an
ellipsis, representing the methods that may be added in a subclass).
In particular, it vanishes when the ellipsis gets instantiated.
Each type expression <TT><FONT COLOR=blue>#</FONT></TT> <TT><I><FONT COLOR=maroon>class-path</FONT></I></TT> defines a new type variable, so
type <TT><FONT COLOR=blue>#</FONT></TT> <TT><I><FONT COLOR=maroon>class-path</FONT></I></TT> <TT><FONT COLOR=blue>-&gt;</FONT></TT> <TT><FONT COLOR=blue>#</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>class-path</FONT></I></TT> is usually not the same as
type <TT><FONT COLOR=blue>#</FONT></TT> <TT><I><FONT COLOR=maroon>class-path</FONT></I></TT> <TT><FONT COLOR=blue>as</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>ident</FONT></I></TT> <TT><FONT COLOR=blue>-&gt;</FONT></TT> <TT><FONT COLOR=blue>'</FONT></TT> &nbsp;<TT><I><FONT COLOR=maroon>ident</FONT></I></TT>.
<BR>
<BR>

<H4>Variant and record types</H4>There are no type expressions describing variant types nor record types,
since those are always named, i.e. defined before use and referred to
by name. Type definitions are described in section&nbsp;<A HREF="manual014.html#s:type-defs">5.8.1</A>.

<BR>
<BR>
<HR>
<A HREF="manual009.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual011.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="manual006.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
</BODY>
</HTML>