54
36
<A NAME="@manual5"></A>
55
37
The library modules <TT>Int32</TT>[<TT><A HREF="libref/Int32.html">Int32</A></TT>],
56
38
<TT>Int64</TT>[<TT><A HREF="libref/Int64.html">Int64</A></TT>] and <TT>Nativeint</TT>[<TT><A HREF="libref/Nativeint.html">Nativeint</A></TT>]
57
provide operations on these integer types.<BR>
60
<H2 CLASS="section"><A NAME="htoc93">7.2</A> Streams and stream parsers</H2>
61
<A NAME="s:streams"></A>
62
The syntax for streams and stream parsers is no longer part of the
39
provide operations on these integer types.</P><H2 CLASS="section"><A NAME="htoc93">7.2</A>��Streams and stream parsers</H2><P>
40
<A NAME="s:streams"></A></P><P>The syntax for streams and stream parsers is no longer part of the
63
41
Objective Caml language, but available through a Camlp4 syntax
64
42
extension. See the Camlp4 reference manual for more information.
65
<div style="background-color:yellow; color:red; border-style:none; border-width:0.5pt">
66
43
Support for basic operations on streams is still available through the
67
<TT>Stream</TT>[<TT><A HREF="libref/Stream.html">Stream</A></TT>]
68
module of the standard library.
44
<TT>Stream</TT>[<TT><A HREF="libref/Stream.html">Stream</A></TT>] module of the standard library.
70
45
Objective Caml programs
71
46
that use the stream parser syntax should be compiled with the
72
47
<TT>-pp camlp4o</TT> option to <TT>ocamlc</TT> and <TT>ocamlopt</TT>. For interactive use,
73
run <TT>ocaml</TT> and issue the <CODE>#load "camlp4o.cma";;</CODE> command.<BR>
76
<H2 CLASS="section"><A NAME="htoc94">7.3</A> Recursive definitions of values</H2> <A NAME="s:letrecvalues"></A>
77
As mentioned in section <A HREF="manual015.html#s:localdef">6.7.1</A>, the <FONT COLOR=blue><TT>let rec</TT></FONT> binding
48
run <TT>ocaml</TT> and issue the <CODE>#load "camlp4o.cma";;</CODE> command.</P><H2 CLASS="section"><A NAME="htoc94">7.3</A>��Recursive definitions of values</H2><P> <A NAME="s:letrecvalues"></A></P><P>As mentioned in section�<A HREF="expr.html#s:localdef">6.7.1</A>, the <FONT COLOR=blue><TT>let rec</TT></FONT> binding
78
49
construct, in addition to the definition of recursive functions,
79
50
also supports a certain class of recursive definitions of
80
51
non-functional values, such as
82
<FONT COLOR=blue><TT>let</TT> <TT>rec</TT></FONT> <FONT COLOR=maroon><I><TT>name</TT></I></FONT><SUB>1</SUB> <FONT COLOR=blue><TT>=</TT> <TT>1</TT> <TT>::</TT></FONT> <FONT COLOR=maroon><I><TT>name</TT></I></FONT><SUB>2</SUB>
83
<FONT COLOR=blue><TT>and</TT></FONT> <FONT COLOR=maroon><I><TT>name</TT></I></FONT><SUB>2</SUB> <FONT COLOR=blue><TT>=</TT> <TT>2</TT> <TT>::</TT></FONT> <FONT COLOR=maroon><I><TT>name</TT></I></FONT><SUB>1</SUB>
84
<FONT COLOR=blue><TT>in</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT>
86
which binds <FONT COLOR=maroon><I><TT>name</TT></I></FONT><SUB>1</SUB> to the cyclic list <TT>1::2::1::2::</TT>..., and
87
<FONT COLOR=maroon><I><TT>name</TT></I></FONT><SUB>2</SUB> to the cyclic list <TT>2::1::2::1::</TT>...Informally, the class of accepted definitions consists of those
52
</P><DIV CLASS="center">
53
<FONT COLOR=blue><TT>let</TT> <TT>rec</TT></FONT> <FONT COLOR=maroon><I>name</I></FONT><SUB>1</SUB> <FONT COLOR=blue><TT>=</TT> <TT>1</TT> <TT>::</TT></FONT> �<FONT COLOR=maroon><I>name</I></FONT><SUB>2</SUB>
54
<FONT COLOR=blue><TT>and</TT></FONT> �<FONT COLOR=maroon><I>name</I></FONT><SUB>2</SUB> <FONT COLOR=blue><TT>=</TT> <TT>2</TT> <TT>::</TT></FONT> �<FONT COLOR=maroon><I>name</I></FONT><SUB>1</SUB>
55
<FONT COLOR=blue><TT>in</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I>
57
which binds <FONT COLOR=maroon><I>name</I></FONT><SUB>1</SUB> to the cyclic list <TT>1::2::1::2::</TT>…, and
58
<FONT COLOR=maroon><I>name</I></FONT><SUB>2</SUB> to the cyclic list <TT>2::1::2::1::</TT>…Informally, the class of accepted definitions consists of those
88
59
definitions where the defined names occur only inside function
89
bodies or as argument to a data constructor.<BR>
91
More precisely, consider the expression:
93
<FONT COLOR=blue><TT>let</TT> <TT>rec</TT></FONT> <FONT COLOR=maroon><I><TT>name</TT></I></FONT><SUB>1</SUB> <FONT COLOR=blue><TT>=</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>1</SUB> <FONT COLOR=blue><TT>and</TT></FONT> … <FONT COLOR=blue><TT>and</TT></FONT> <I><FONT COLOR=maroon><TT>name</TT></FONT><SUB>n</SUB></I> <FONT COLOR=blue><TT>=</TT></FONT> <I><FONT COLOR=maroon><TT>expr</TT></FONT><SUB>n</SUB></I> <FONT COLOR=blue><TT>in</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT>
95
It will be accepted if each one of <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>1</SUB> … <I><FONT COLOR=maroon><TT>expr</TT></FONT><SUB>n</SUB></I> is
96
statically constructive with respect to <FONT COLOR=maroon><I><TT>name</TT></I></FONT><SUB>1</SUB> … <I><FONT COLOR=maroon><TT>name</TT></FONT><SUB>n</SUB></I> and
97
not immediately linked to any of <FONT COLOR=maroon><I><TT>name</TT></I></FONT><SUB>1</SUB> … <I><FONT COLOR=maroon><TT>name</TT></FONT><SUB>n</SUB></I><BR>
99
An expression <FONT COLOR=maroon><I><TT>e</TT></I></FONT> is said to be <EM>statically constructive
100
with respect to</EM> the variables <FONT COLOR=maroon><I><TT>name</TT></I></FONT><SUB>1</SUB> … <I><FONT COLOR=maroon><TT>name</TT></FONT><SUB>n</SUB></I> if at least
60
bodies or as argument to a data constructor.</P><P>More precisely, consider the expression:
61
</P><DIV CLASS="center">
62
<FONT COLOR=blue><TT>let</TT> <TT>rec</TT></FONT> <FONT COLOR=maroon><I>name</I></FONT><SUB>1</SUB> <FONT COLOR=blue><TT>=</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>and</TT></FONT> … <FONT COLOR=blue><TT>and</TT></FONT> �<I><FONT COLOR=maroon>name</FONT><SUB>n</SUB></I> <FONT COLOR=blue><TT>=</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A><SUB>n</SUB></I> <FONT COLOR=blue><TT>in</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I>
64
It will be accepted if each one of <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>1</SUB> … �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A><SUB>n</SUB></I> is
65
statically constructive with respect to <FONT COLOR=maroon><I>name</I></FONT><SUB>1</SUB> … �<I><FONT COLOR=maroon>name</FONT><SUB>n</SUB></I> and
66
not immediately linked to any of <FONT COLOR=maroon><I>name</I></FONT><SUB>1</SUB> … �<I><FONT COLOR=maroon>name</FONT><SUB>n</SUB></I></P><P>An expression <FONT COLOR=maroon><I>e</I></FONT> is said to be <EM>statically constructive
67
with respect to</EM> the variables <FONT COLOR=maroon><I>name</I></FONT><SUB>1</SUB> … �<I><FONT COLOR=maroon>name</FONT><SUB>n</SUB></I> if at least
101
68
one of the following conditions is true:
102
<UL CLASS="itemize"><LI CLASS="li-itemize">
103
<FONT COLOR=maroon><I><TT>e</TT></I></FONT> has no free occurrence of any of <FONT COLOR=maroon><I><TT>name</TT></I></FONT><SUB>1</SUB> … <I><FONT COLOR=maroon><TT>name</TT></FONT><SUB>n</SUB></I>
104
<LI CLASS="li-itemize"><FONT COLOR=maroon><I><TT>e</TT></I></FONT> is a variable
105
<LI CLASS="li-itemize"><FONT COLOR=maroon><I><TT>e</TT></I></FONT> has the form <FONT COLOR=blue><TT>fun</TT></FONT> … <FONT COLOR=blue><TT>-></TT></FONT> …
106
<LI CLASS="li-itemize"><FONT COLOR=maroon><I><TT>e</TT></I></FONT> has the form <FONT COLOR=blue><TT>function</TT></FONT> … <FONT COLOR=blue><TT>-></TT></FONT> …
107
<LI CLASS="li-itemize"><FONT COLOR=maroon><I><TT>e</TT></I></FONT> has the form <FONT COLOR=blue><TT>lazy</TT> <TT>(</TT></FONT> … <FONT COLOR=blue><TT>)</TT></FONT>
108
<LI CLASS="li-itemize"><FONT COLOR=maroon><I><TT>e</TT></I></FONT> has one of the following forms, where each one of
109
<FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>1</SUB> … <I><FONT COLOR=maroon><TT>expr</TT></FONT><SUB>m</SUB></I> is statically constructive with respect to
110
<FONT COLOR=maroon><I><TT>name</TT></I></FONT><SUB>1</SUB> … <I><FONT COLOR=maroon><TT>name</TT></FONT><SUB>n</SUB></I>, and <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>0</SUB> is statically constructive with
111
respect to <FONT COLOR=maroon><I><TT>name</TT></I></FONT><SUB>1</SUB> … <I><FONT COLOR=maroon><TT>name</TT></FONT><SUB>n</SUB></I>, <FONT COLOR=maroon><I><TT>xname</TT></I></FONT><SUB>1</SUB> … <I><FONT COLOR=maroon><TT>xname</TT></FONT><SUB>m</SUB></I>:
112
<UL CLASS="itemize"><LI CLASS="li-itemize">
113
<FONT COLOR=blue><TT>let</TT></FONT> [<FONT COLOR=blue><TT>rec</TT></FONT>] <FONT COLOR=maroon><I><TT>xname</TT></I></FONT><SUB>1</SUB> <FONT COLOR=blue><TT>=</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>1</SUB> <FONT COLOR=blue><TT>and</TT></FONT> …
114
<FONT COLOR=blue><TT>and</TT></FONT> <I><FONT COLOR=maroon><TT>xname</TT></FONT><SUB>m</SUB></I> <FONT COLOR=blue><TT>=</TT></FONT> <I><FONT COLOR=maroon><TT>expr</TT></FONT><SUB>m</SUB></I> <FONT COLOR=blue><TT>in</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>0</SUB>
115
<LI CLASS="li-itemize"><FONT COLOR=blue><TT>let</TT> <TT>module</TT></FONT> … <FONT COLOR=blue><TT>in</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>1</SUB>
116
<LI CLASS="li-itemize"><FONT COLOR=maroon><TT><I>constr</I></TT> <FONT COLOR=blue><TT>(</TT></FONT> <TT><I>expr</I></TT></FONT><SUB>1</SUB><FONT COLOR=blue><TT>,</TT></FONT> … <FONT COLOR=blue><TT>,</TT></FONT> <I><FONT COLOR=maroon><TT>expr</TT></FONT><SUB>m</SUB></I><FONT COLOR=blue><TT>)</TT></FONT>
117
<LI CLASS="li-itemize"><FONT COLOR=blue><TT>`<FONT COLOR=maroon><I>tag-name</I></FONT></TT> <TT>(</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>1</SUB><FONT COLOR=blue><TT>,</TT></FONT> … <FONT COLOR=blue><TT>,</TT></FONT> <I><FONT COLOR=maroon><TT>expr</TT></FONT><SUB>m</SUB></I><FONT COLOR=blue><TT>)</TT></FONT>
118
<LI CLASS="li-itemize"><FONT COLOR=blue><TT>[|</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>1</SUB><FONT COLOR=blue><TT>;</TT></FONT> … <FONT COLOR=blue><TT>;</TT></FONT> <I><FONT COLOR=maroon><TT>expr</TT></FONT><SUB>m</SUB></I> <FONT COLOR=blue><TT>|]</TT></FONT>
119
<LI CLASS="li-itemize"><FONT COLOR=blue><TT>{</TT></FONT> <FONT COLOR=maroon><I><TT>field</TT></I></FONT><SUB>1</SUB> <FONT COLOR=blue><TT>=</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>1</SUB><FONT COLOR=blue><TT>;</TT></FONT> … <FONT COLOR=blue><TT>;</TT></FONT> <I><FONT COLOR=maroon><TT>field</TT></FONT><SUB>m</SUB></I> = <I><FONT COLOR=maroon><TT>expr</TT></FONT><SUB>m</SUB></I> <FONT COLOR=blue><TT>}</TT></FONT>
120
<LI CLASS="li-itemize"><FONT COLOR=blue><TT>{</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>1</SUB> <FONT COLOR=blue><TT>with</TT></FONT> <FONT COLOR=maroon><I><TT>field</TT></I></FONT><SUB>2</SUB> <FONT COLOR=blue><TT>=</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>2</SUB><FONT COLOR=blue><TT>;</TT></FONT> … <FONT COLOR=blue><TT>;</TT></FONT>
121
<I><FONT COLOR=maroon><TT>field</TT></FONT><SUB>m</SUB></I> = <I><FONT COLOR=maroon><TT>expr</TT></FONT><SUB>m</SUB></I> <FONT COLOR=blue><TT>}</TT></FONT> where <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>1</SUB> is not immediately
122
linked to <FONT COLOR=maroon><I><TT>name</TT></I></FONT><SUB>1</SUB> … <I><FONT COLOR=maroon><TT>name</TT></FONT><SUB>n</SUB></I>
123
<LI CLASS="li-itemize"><FONT COLOR=blue><TT>(</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>1</SUB><FONT COLOR=blue><TT>,</TT></FONT> … <FONT COLOR=blue><TT>,</TT></FONT> <I><FONT COLOR=maroon><TT>expr</TT></FONT><SUB>m</SUB></I> <FONT COLOR=blue><TT>)</TT></FONT>
124
<LI CLASS="li-itemize"><FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>1</SUB><FONT COLOR=blue><TT>;</TT></FONT> … <FONT COLOR=blue><TT>;</TT></FONT> <I><FONT COLOR=maroon><TT>expr</TT></FONT><SUB>m</SUB></I>
127
An expression <FONT COLOR=maroon><I><TT>e</TT></I></FONT> is said to be <EM>immediately linked to</EM> the variable
128
<FONT COLOR=maroon><I><TT>name</TT></I></FONT> in the following cases:
129
<UL CLASS="itemize"><LI CLASS="li-itemize">
130
<FONT COLOR=maroon><I><TT>e</TT></I></FONT> is <FONT COLOR=maroon><I><TT>name</TT></I></FONT>
131
<LI CLASS="li-itemize"><FONT COLOR=maroon><I><TT>e</TT></I></FONT> has the form <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>1</SUB><FONT COLOR=blue><TT>;</TT></FONT> … <FONT COLOR=blue><TT>;</TT></FONT> <I><FONT COLOR=maroon><TT>expr</TT></FONT><SUB>m</SUB></I> where <I><FONT COLOR=maroon><TT>expr</TT></FONT><SUB>m</SUB></I>
132
is immediately linked to <FONT COLOR=maroon><I><TT>name</TT></I></FONT>
133
<LI CLASS="li-itemize"><FONT COLOR=maroon><I><TT>e</TT></I></FONT> has the form <FONT COLOR=blue><TT>let</TT></FONT> [<FONT COLOR=blue><TT>rec</TT></FONT>] <FONT COLOR=maroon><I><TT>xname</TT></I></FONT><SUB>1</SUB> <FONT COLOR=blue><TT>=</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>1</SUB> <FONT COLOR=blue><TT>and</TT></FONT> …
134
<FONT COLOR=blue><TT>and</TT></FONT> <I><FONT COLOR=maroon><TT>xname</TT></FONT><SUB>m</SUB></I> <FONT COLOR=blue><TT>=</TT></FONT> <I><FONT COLOR=maroon><TT>expr</TT></FONT><SUB>m</SUB></I> <FONT COLOR=blue><TT>in</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>0</SUB> where <FONT COLOR=maroon><I><TT>expr</TT></I></FONT><SUB>0</SUB> is immediately
135
linked to <FONT COLOR=maroon><I><TT>name</TT></I></FONT> or to one of the <I><FONT COLOR=maroon><TT>xname</TT></FONT><SUB>i</SUB></I> such that <I><FONT COLOR=maroon><TT>expr</TT></FONT><SUB>i</SUB></I>
136
is immediately linked to <FONT COLOR=maroon><I><TT>name</TT></I></FONT>.
139
<H2 CLASS="section"><A NAME="htoc95">7.4</A> Range patterns</H2>
140
In patterns, Objective Caml recognizes the form
141
<FONT COLOR=blue><TT>'</TT> <FONT COLOR=maroon><TT><I>c</I></TT></FONT> <TT>'</TT> <TT>..</TT> <TT>'</TT> <FONT COLOR=maroon><TT><I>d</I></TT></FONT> <TT>'</TT></FONT>
142
(two character literals separated by <TT>..</TT>) as shorthand for the pattern
144
<FONT COLOR=blue><TT>'</TT></FONT> <FONT COLOR=maroon><TT><I>c</I></TT> <FONT COLOR=blue><TT>'</TT> <TT>|</TT> <TT>'</TT></FONT> <TT><I>c</I></TT></FONT><SUB>1</SUB> <FONT COLOR=blue><TT>'</TT> <TT>|</TT> <TT>'</TT></FONT> <FONT COLOR=maroon><I><TT>c</TT></I></FONT><SUB>2</SUB> <FONT COLOR=blue><TT>'</TT> <TT>|</TT></FONT> …
145
<FONT COLOR=blue><TT>|</TT> <TT>'</TT></FONT> <I><FONT COLOR=maroon><TT>c</TT></FONT><SUB>n</SUB></I> <FONT COLOR=blue><TT>'</TT> <TT>|</TT> <TT>'</TT> <FONT COLOR=maroon><TT><I>d</I></TT></FONT> <TT>'</TT></FONT>
147
where <I>c</I><SUB>1</SUB>, <I>c</I><SUB>2</SUB>, ..., <I>c<SUB>n</SUB></I> are the characters
69
</P><UL CLASS="itemize"><LI CLASS="li-itemize">
70
<FONT COLOR=maroon><I>e</I></FONT> has no free occurrence of any of <FONT COLOR=maroon><I>name</I></FONT><SUB>1</SUB> … �<I><FONT COLOR=maroon>name</FONT><SUB>n</SUB></I>
71
</LI><LI CLASS="li-itemize"><FONT COLOR=maroon><I>e</I></FONT> is a variable
72
</LI><LI CLASS="li-itemize"><FONT COLOR=maroon><I>e</I></FONT> has the form <FONT COLOR=blue><TT>fun</TT></FONT> … <FONT COLOR=blue><TT>-></TT></FONT> …
73
</LI><LI CLASS="li-itemize"><FONT COLOR=maroon><I>e</I></FONT> has the form <FONT COLOR=blue><TT>function</TT></FONT> … <FONT COLOR=blue><TT>-></TT></FONT> …
74
</LI><LI CLASS="li-itemize"><FONT COLOR=maroon><I>e</I></FONT> has the form <FONT COLOR=blue><TT>lazy</TT> <TT>(</TT></FONT> … <FONT COLOR=blue><TT>)</TT></FONT>
75
</LI><LI CLASS="li-itemize"><FONT COLOR=maroon><I>e</I></FONT> has one of the following forms, where each one of
76
<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>1</SUB> … �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A><SUB>m</SUB></I> is statically constructive with respect to
77
<FONT COLOR=maroon><I>name</I></FONT><SUB>1</SUB> … �<I><FONT COLOR=maroon>name</FONT><SUB>n</SUB></I>, and <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>0</SUB> is statically constructive with
78
respect to <FONT COLOR=maroon><I>name</I></FONT><SUB>1</SUB> … �<I><FONT COLOR=maroon>name</FONT><SUB>n</SUB></I>, �<FONT COLOR=maroon><I>xname</I></FONT><SUB>1</SUB> … �<I><FONT COLOR=maroon>xname</FONT><SUB>m</SUB></I>:
79
<UL CLASS="itemize"><LI CLASS="li-itemize">
80
<FONT COLOR=blue><TT>let</TT></FONT> [<FONT COLOR=blue><TT>rec</TT></FONT>] <FONT COLOR=maroon><I>xname</I></FONT><SUB>1</SUB> <FONT COLOR=blue><TT>=</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>and</TT></FONT> …
81
<FONT COLOR=blue><TT>and</TT></FONT> �<I><FONT COLOR=maroon>xname</FONT><SUB>m</SUB></I> <FONT COLOR=blue><TT>=</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A><SUB>m</SUB></I> <FONT COLOR=blue><TT>in</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>0</SUB>
82
</LI><LI CLASS="li-itemize"><FONT COLOR=blue><TT>let</TT> <TT>module</TT></FONT> … <FONT COLOR=blue><TT>in</TT></FONT> <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>1</SUB>
83
</LI><LI CLASS="li-itemize"><I><A HREF="manual011.html#constr"><FONT COLOR=maroon>constr</FONT></A></I> <FONT COLOR=blue><TT>(</TT></FONT>�<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>1</SUB><FONT COLOR=blue><TT>,</TT></FONT> … <FONT COLOR=blue><TT>,</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A><SUB>m</SUB></I><FONT COLOR=blue><TT>)</TT></FONT>
84
</LI><LI CLASS="li-itemize"><FONT COLOR=blue><TT>`</TT></FONT><I><A HREF="manual011.html#tag-name"><FONT COLOR=maroon>tag-name</FONT></A></I> <FONT COLOR=blue><TT>(</TT></FONT>�<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>1</SUB><FONT COLOR=blue><TT>,</TT></FONT> … <FONT COLOR=blue><TT>,</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A><SUB>m</SUB></I><FONT COLOR=blue><TT>)</TT></FONT>
85
</LI><LI CLASS="li-itemize"><FONT COLOR=blue><TT>[|</TT></FONT> <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>1</SUB><FONT COLOR=blue><TT>;</TT></FONT> … <FONT COLOR=blue><TT>;</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A><SUB>m</SUB></I> <FONT COLOR=blue><TT>|]</TT></FONT>
86
</LI><LI CLASS="li-itemize"><FONT COLOR=blue><TT>{</TT></FONT> <I><A HREF="manual011.html#field"><FONT COLOR=maroon>field</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>=</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>1</SUB><FONT COLOR=blue><TT>;</TT></FONT> … <FONT COLOR=blue><TT>;</TT></FONT> �<I><A HREF="manual011.html#field"><FONT COLOR=maroon>field</FONT></A><SUB>m</SUB></I> = �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A><SUB>m</SUB></I> <FONT COLOR=blue><TT>}</TT></FONT>
87
</LI><LI CLASS="li-itemize"><FONT COLOR=blue><TT>{</TT></FONT> <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>with</TT></FONT> �<I><A HREF="manual011.html#field"><FONT COLOR=maroon>field</FONT></A></I><SUB>2</SUB> <FONT COLOR=blue><TT>=</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>2</SUB><FONT COLOR=blue><TT>;</TT></FONT> … <FONT COLOR=blue><TT>;</TT></FONT>
88
�<I><A HREF="manual011.html#field"><FONT COLOR=maroon>field</FONT></A><SUB>m</SUB></I> = �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A><SUB>m</SUB></I> <FONT COLOR=blue><TT>}</TT></FONT> where <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>1</SUB> is not immediately
89
linked to <FONT COLOR=maroon><I>name</I></FONT><SUB>1</SUB> … �<I><FONT COLOR=maroon>name</FONT><SUB>n</SUB></I>
90
</LI><LI CLASS="li-itemize"><FONT COLOR=blue><TT>(</TT></FONT> <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>1</SUB><FONT COLOR=blue><TT>,</TT></FONT> … <FONT COLOR=blue><TT>,</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A><SUB>m</SUB></I> <FONT COLOR=blue><TT>)</TT></FONT>
91
</LI><LI CLASS="li-itemize"><I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>1</SUB><FONT COLOR=blue><TT>;</TT></FONT> … <FONT COLOR=blue><TT>;</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A><SUB>m</SUB></I>
93
</LI></UL><P>An expression <FONT COLOR=maroon><I>e</I></FONT> is said to be <EM>immediately linked to</EM> the variable
94
<FONT COLOR=maroon><I>name</I></FONT> in the following cases:
95
</P><UL CLASS="itemize"><LI CLASS="li-itemize">
96
<FONT COLOR=maroon><I>e</I></FONT> is <FONT COLOR=maroon><I>name</I></FONT>
97
</LI><LI CLASS="li-itemize"><FONT COLOR=maroon><I>e</I></FONT> has the form <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>1</SUB><FONT COLOR=blue><TT>;</TT></FONT> … <FONT COLOR=blue><TT>;</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A><SUB>m</SUB></I> where <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A><SUB>m</SUB></I>
98
is immediately linked to <FONT COLOR=maroon><I>name</I></FONT>
99
</LI><LI CLASS="li-itemize"><FONT COLOR=maroon><I>e</I></FONT> has the form <FONT COLOR=blue><TT>let</TT></FONT> [<FONT COLOR=blue><TT>rec</TT></FONT>] <FONT COLOR=maroon><I>xname</I></FONT><SUB>1</SUB> <FONT COLOR=blue><TT>=</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>and</TT></FONT> …
100
<FONT COLOR=blue><TT>and</TT></FONT> �<I><FONT COLOR=maroon>xname</FONT><SUB>m</SUB></I> <FONT COLOR=blue><TT>=</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A><SUB>m</SUB></I> <FONT COLOR=blue><TT>in</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>0</SUB> where <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I><SUB>0</SUB> is immediately
101
linked to <FONT COLOR=maroon><I>name</I></FONT> or to one of the <I><FONT COLOR=maroon>xname</FONT><SUB>i</SUB></I> such that <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A><SUB>i</SUB></I>
102
is immediately linked to <FONT COLOR=maroon><I>name</I></FONT>.
103
</LI></UL><H2 CLASS="section"><A NAME="htoc95">7.4</A>��Range patterns</H2><P>In patterns, Objective Caml recognizes the form
104
<FONT COLOR=blue><TT>'</TT> <FONT COLOR=maroon><I>c</I></FONT> <TT>'</TT> <TT>..</TT> <TT>'</TT> <FONT COLOR=maroon><I>d</I></FONT> <TT>'</TT></FONT>
105
(two character literals separated by�<TT>..</TT>) as shorthand for the pattern
106
</P><DIV CLASS="center">
107
<FONT COLOR=blue><TT>'</TT></FONT> <FONT COLOR=maroon><I>c</I> <FONT COLOR=blue><TT>'</TT> <TT>|</TT> <TT>'</TT></FONT> <I>c</I></FONT><SUB>1</SUB> <FONT COLOR=blue><TT>'</TT> <TT>|</TT> <TT>'</TT></FONT> <FONT COLOR=maroon><I>c</I></FONT><SUB>2</SUB> <FONT COLOR=blue><TT>'</TT> <TT>|</TT></FONT> …
108
<FONT COLOR=blue><TT>|</TT> <TT>'</TT></FONT> <I><FONT COLOR=maroon>c</FONT><SUB>n</SUB></I> <FONT COLOR=blue><TT>'</TT> <TT>|</TT> <TT>'</TT> <FONT COLOR=maroon><I>d</I></FONT> <TT>'</TT></FONT>
110
where <I>c</I><SUB>1</SUB>, <I>c</I><SUB>2</SUB>, …, <I>c<SUB>n</SUB></I> are the characters
148
111
that occur between <I>c</I> and <I>d</I> in the ASCII character set. For
149
instance, the pattern <TT>'0'..'9'</TT> matches all characters that are digits.<BR>
152
<H2 CLASS="section"><A NAME="htoc96">7.5</A> Assertion checking</H2>
153
<A NAME="@manual.kwd169"></A>
154
Objective Caml supports the <TT>assert</TT> construct to check debugging assertions.
155
The expression <FONT COLOR=blue><TT>assert</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT> evaluates the expression <FONT COLOR=maroon><I><TT>expr</TT></I></FONT> and
156
returns <TT>()</TT> if <FONT COLOR=maroon><I><TT>expr</TT></I></FONT> evaluates to <TT>true</TT>. Otherwise, the exception
112
instance, the pattern <TT>'0'..'9'</TT> matches all characters that are digits.</P><H2 CLASS="section"><A NAME="htoc96">7.5</A>��Assertion checking</H2><P>
113
<A NAME="@manual.kwd173"></A></P><P>Objective Caml supports the <TT>assert</TT> construct to check debugging assertions.
114
The expression <FONT COLOR=blue><TT>assert</TT></FONT> <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I> evaluates the expression <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I> and
115
returns <TT>()</TT> if <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I> evaluates to <TT>true</TT>. Otherwise, the exception
157
116
<TT>Assert_failure</TT> is raised with the source file name and the
158
location of <FONT COLOR=maroon><I><TT>expr</TT></I></FONT> as arguments. Assertion
159
checking can be turned off with the <TT>-noassert</TT> compiler option.<BR>
161
As a special case, <TT>assert false</TT> is reduced to
117
location of <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I> as arguments. Assertion
118
checking can be turned off with the <TT>-noassert</TT> compiler option.</P><P>As a special case, <TT>assert false</TT> is reduced to
162
119
<TT>raise (Assert_failure ...)</TT>, which is polymorphic (and
163
120
is not turned off by the <TT>-noassert</TT> option).
164
<A NAME="@manual6"></A><BR>
167
<H2 CLASS="section"><A NAME="htoc97">7.6</A> Lazy evaluation</H2>
168
<A NAME="@manual.kwd170"></A>
169
The expression <FONT COLOR=blue><TT>lazy</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT> returns a value <I>v</I> of type <TT>Lazy.t</TT> that
170
encapsulates the computation of <FONT COLOR=maroon><I><TT>expr</TT></I></FONT>. The argument <FONT COLOR=maroon><I><TT>expr</TT></I></FONT> is not
121
<A NAME="@manual6"></A></P><H2 CLASS="section"><A NAME="htoc97">7.6</A>��Lazy evaluation</H2><P>
122
<A NAME="@manual.kwd174"></A></P><P>The expression <FONT COLOR=blue><TT>lazy</TT></FONT> <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I> returns a value <I>v</I> of type <TT>Lazy.t</TT> that
123
encapsulates the computation of <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I>. The argument <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I> is not
171
124
evaluated at this point in the program. Instead, its evaluation will
172
125
be performed the first time <TT>Lazy.force</TT> is applied to the value
173
<I>v</I>, returning the actual value of <FONT COLOR=maroon><I><TT>expr</TT></I></FONT>. Subsequent applications
174
of <TT>Lazy.force</TT> to <I>v</I> do not evaluate <FONT COLOR=maroon><I><TT>expr</TT></I></FONT> again.
126
<I>v</I>, returning the actual value of <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I>. Subsequent applications
127
of <TT>Lazy.force</TT> to <I>v</I> do not evaluate <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I> again.
175
128
For more information, see the description of module <TT>Lazy</TT> in the
176
129
standard library (see
177
130
<A HREF="libref/Lazy.html">Module <TT>Lazy</TT></A>).
178
<A NAME="@manual7"></A><A NAME="@manual8"></A><BR>
181
<H2 CLASS="section"><A NAME="htoc98">7.7</A> Local modules</H2>
182
<A NAME="@manual.kwd171"></A>
183
<A NAME="@manual.kwd172"></A>
185
<FONT COLOR=blue><TT>let</TT></FONT> <FONT COLOR=blue><TT>module</TT></FONT> <FONT COLOR=maroon><TT><I>module-name</I></TT> <FONT COLOR=blue><TT>=</TT></FONT> <TT><I>module-expr</I></TT> <FONT COLOR=blue><TT>in</TT></FONT> <TT><I>expr</I></TT></FONT>
186
locally binds the module expression <FONT COLOR=maroon><I><TT>module-expr</TT></I></FONT> to the identifier
187
<FONT COLOR=maroon><I><TT>module-name</TT></I></FONT> during the evaluation of the expression <FONT COLOR=maroon><I><TT>expr</TT></I></FONT>.
188
It then returns the value of <FONT COLOR=maroon><I><TT>expr</TT></I></FONT>. For example:
189
<PRE CLASS="verbatim">
190
let remove_duplicates comparison_fun string_list =
131
<A NAME="@manual7"></A><A NAME="@manual8"></A></P><H2 CLASS="section"><A NAME="htoc98">7.7</A>��Local modules</H2><P>
132
<A NAME="@manual.kwd175"></A>
133
<A NAME="@manual.kwd176"></A></P><P>The expression
134
<FONT COLOR=blue><TT>let</TT> <TT>module</TT></FONT> <I><A HREF="manual011.html#module-name"><FONT COLOR=maroon>module-name</FONT></A></I> <FONT COLOR=blue><TT>=</TT></FONT> �<I><A HREF="manual019.html#module-expr"><FONT COLOR=maroon>module-expr</FONT></A></I> <FONT COLOR=blue><TT>in</TT></FONT> �<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I>
135
locally binds the module expression <I><A HREF="manual019.html#module-expr"><FONT COLOR=maroon>module-expr</FONT></A></I> to the identifier
136
<I><A HREF="manual011.html#module-name"><FONT COLOR=maroon>module-name</FONT></A></I> during the evaluation of the expression <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I>.
137
It then returns the value of <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I>. For example:
138
</P><PRE CLASS="verbatim"> let remove_duplicates comparison_fun string_list =
191
139
let module StringSet =
192
140
Set.Make(struct type t = string
193
141
let compare = comparison_fun end) in
194
142
StringSet.elements
195
143
(List.fold_right StringSet.add string_list StringSet.empty)
198
<H2 CLASS="section"><A NAME="htoc99">7.8</A> Private types</H2>
199
<A NAME="@manual.kwd173"></A><BR>
201
<DIV CLASS="center"><TABLE CELLSPACING=2 CELLPADDING=0>
202
<TR><TD ALIGN=right NOWRAP>
203
<FONT COLOR=maroon><I><a href="manual016.html#type-representation"><font color=maroon><TT>type-representation</TT></font></a></I></FONT></TD>
204
<TD ALIGN=right NOWRAP>::=</TD>
205
<TD ALIGN=left NOWRAP>
208
<TR><TD ALIGN=right NOWRAP> </TD>
209
<TD ALIGN=right NOWRAP>∣</TD>
210
<TD ALIGN=left NOWRAP> <FONT COLOR=blue><TT>=</TT> <TT>private</TT></FONT> <FONT COLOR=maroon><I><a href="manual016.html#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="manual016.html#constr-decl"><font color=maroon><TT>constr-decl</TT></font></a></I></FONT> }</TD>
212
<TR><TD ALIGN=right NOWRAP> </TD>
213
<TD ALIGN=right NOWRAP>∣</TD>
214
<TD ALIGN=left NOWRAP> <FONT COLOR=blue><TT>=</TT> <TT>private</TT> <TT>{</TT></FONT> <FONT COLOR=maroon><I><a href="manual016.html#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="manual016.html#field-decl"><font color=maroon><TT>field-decl</TT></font></a></I></FONT> } <FONT COLOR=blue><TT>}</TT></FONT></TD>
144
</PRE><H2 CLASS="section"><A NAME="htoc99">7.8</A>��Private types</H2><P>
145
<A NAME="@manual.kwd177"></A></P><TABLE CLASS="display dcenter"><TR VALIGN="middle"><TD CLASS="dcell"><TABLE CELLSPACING=6 CELLPADDING=0><TR><TD ALIGN=right NOWRAP>
146
<I><A HREF="manual016.html#type-representation"><FONT COLOR=maroon>type-representation</FONT></A></I></TD><TD ALIGN=center NOWRAP>::=</TD><TD ALIGN=left NOWRAP>
149
<TR><TD ALIGN=right NOWRAP> </TD><TD ALIGN=center NOWRAP>∣</TD><TD ALIGN=left NOWRAP>�<FONT COLOR=blue><TT>=</TT></FONT>�<FONT COLOR=blue><TT>private</TT></FONT>�<I><A HREF="manual016.html#constr-decl"><FONT COLOR=maroon>constr-decl</FONT></A></I>��{�<FONT COLOR=blue><TT>|</TT></FONT>�<I><A HREF="manual016.html#constr-decl"><FONT COLOR=maroon>constr-decl</FONT></A></I>�}
151
<TR><TD ALIGN=right NOWRAP> </TD><TD ALIGN=center NOWRAP>∣</TD><TD ALIGN=left NOWRAP>�<FONT COLOR=blue><TT>=</TT></FONT>�<FONT COLOR=blue><TT>private</TT></FONT>�<FONT COLOR=blue><TT>{</TT></FONT>�<I><A HREF="manual016.html#field-decl"><FONT COLOR=maroon>field-decl</FONT></A></I>��{�<FONT COLOR=blue><TT>;</TT></FONT>�<I><A HREF="manual016.html#field-decl"><FONT COLOR=maroon>field-decl</FONT></A></I>�}�<FONT COLOR=blue><TT>}</TT></FONT></TD></TR>
216
154
Private types are variant or record types. Values of
217
155
these types can be de-structured normally in pattern-matching or via
218
the <FONT COLOR=maroon><TT><I>expr</I></TT> <FONT COLOR=blue><TT>.</TT></FONT> <TT><I>field</I></TT></FONT> notation for record accesses. However, values of
156
the <I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I> <FONT COLOR=blue><TT>.</TT></FONT> �<I><A HREF="manual011.html#field"><FONT COLOR=maroon>field</FONT></A></I> notation for record accesses. However, values of
219
157
these types cannot be constructed directly by constructor application
220
158
or record construction. Moreover, assignment on a mutable field of a
221
private record type is not allowed.<BR>
223
The typical use of private types is in the export signature of a
159
private record type is not allowed.</P><P>The typical use of private types is in the export signature of a
224
160
module, to ensure that construction of values of the private type always
225
161
go through the functions provided by the module, while still allowing
226
162
pattern-matching outside the defining module. For example:
227
<PRE CLASS="verbatim">
163
</P><PRE CLASS="verbatim"> module M : sig
229
164
type t = private A | B of int
231
166
val b : int -> t
236
171
let b n = assert (n > 0); B n
238
</PRE>Here, the <TT>private</TT> declaration ensures that in any value of type
239
<TT>M.t</TT>, the argument to the <TT>B</TT> constructor is always a positive integer.<BR>
241
<div style="background-color:yellow; color:red; border-style:none; border-width:0.5pt">
242
With respect to the variance of their parameters, private types are
173
</PRE><P>Here, the <TT>private</TT> declaration ensures that in any value of type
174
<TT>M.t</TT>, the argument to the <TT>B</TT> constructor is always a positive integer.</P><P>With respect to the variance of their parameters, private types are
243
175
handled like abstract types. That is, if a private type has
244
176
parameters, their variance is the one explicitly given by prefixing
245
the parameter by a `<TT>+</TT>' or a `<TT>-</TT>', it is invariant otherwise.<BR>
249
<H2 CLASS="section"><A NAME="htoc100">7.9</A> Recursive modules</H2> <A NAME="s-recursive-modules"></A>
250
<A NAME="@manual.kwd174"></A>
251
<A NAME="@manual.kwd175"></A><BR>
253
<DIV CLASS="center"><TABLE CELLSPACING=2 CELLPADDING=0>
254
<TR><TD ALIGN=right NOWRAP>
255
<FONT COLOR=maroon><I><a href="manual019.html#definition"><font color=maroon><TT>definition</TT></font></a></I></FONT></TD>
256
<TD ALIGN=right NOWRAP>::=</TD>
257
<TD ALIGN=left NOWRAP>
260
<TR><TD ALIGN=right NOWRAP> </TD>
261
<TD ALIGN=right NOWRAP>∣</TD>
262
<TD ALIGN=left NOWRAP> <FONT COLOR=blue><TT>module</TT></FONT> <FONT COLOR=blue><TT>rec</TT></FONT> <FONT COLOR=maroon><TT><a href="manual011.html#module-name"><font color=maroon><I>module-name</I></font></a></TT> <FONT COLOR=blue><TT>:</TT></FONT> <TT><a href="manual018.html#module-type"><font color=maroon><I>module-type</I></font></a></TT> <FONT COLOR=blue><TT>=</TT></FONT> <TT><a href="manual019.html#module-expr"><font color=maroon><I>module-expr</I></font></a></TT></FONT>
263
{ <FONT COLOR=blue><TT>and</TT></FONT> <FONT COLOR=maroon><TT><a href="manual011.html#module-name"><font color=maroon><I>module-name</I></font></a><FONT COLOR=blue>:</FONT></TT> <TT><a href="manual018.html#module-type"><font color=maroon><I>module-type</I></font></a></TT> <FONT COLOR=blue><TT>=</TT></FONT> <TT><a href="manual019.html#module-expr"><font color=maroon><I>module-expr</I></font></a></TT></FONT> }</TD>
265
<TR><TD ALIGN=right NOWRAP>
266
<FONT COLOR=maroon><I><a href="manual018.html#specification"><font color=maroon><TT>specification</TT></font></a></I></FONT></TD>
267
<TD ALIGN=right NOWRAP>::=</TD>
268
<TD ALIGN=left NOWRAP>
271
<TR><TD ALIGN=right NOWRAP> </TD>
272
<TD ALIGN=right NOWRAP>∣</TD>
273
<TD ALIGN=left NOWRAP> <FONT COLOR=blue><TT>module</TT></FONT> <FONT COLOR=blue><TT>rec</TT></FONT> <FONT COLOR=maroon><TT><a href="manual011.html#module-name"><font color=maroon><I>module-name</I></font></a></TT> <FONT COLOR=blue><TT>:</TT></FONT> <TT><a href="manual018.html#module-type"><font color=maroon><I>module-type</I></font></a></TT></FONT>
274
{ <FONT COLOR=blue><TT>and</TT></FONT> <FONT COLOR=maroon><TT><a href="manual011.html#module-name"><font color=maroon><I>module-name</I></font></a><FONT COLOR=blue>:</FONT></TT> <TT><a href="manual018.html#module-type"><font color=maroon><I>module-type</I></font></a></TT></FONT> }</TD>
275
</TR></TABLE></DIV><BR>
277
Recursive module definitions, introduced by the 'module rec' ...'and' ... construction, generalize regular module definitions
278
<FONT COLOR=blue><TT>module</TT></FONT> <FONT COLOR=maroon><TT><I>module-name</I></TT> <FONT COLOR=blue><TT>=</TT></FONT> <TT><I>module-expr</I></TT></FONT> and module specifications
279
<FONT COLOR=blue><TT>module</TT></FONT> <FONT COLOR=maroon><TT><I>module-name</I></TT> <FONT COLOR=blue><TT>:</TT></FONT> <TT><I>module-type</I></TT></FONT> by allowing the defining
280
<FONT COLOR=maroon><I><TT>module-expr</TT></I></FONT> and the <FONT COLOR=maroon><I><TT>module-type</TT></I></FONT> to refer recursively to the module
177
the parameter by a `<TT>+</TT>' or a `<TT>-</TT>', it is invariant otherwise.</P><H2 CLASS="section"><A NAME="htoc100">7.9</A>��Recursive modules</H2><P> <A NAME="s-recursive-modules"></A>
178
<A NAME="@manual.kwd178"></A>
179
<A NAME="@manual.kwd179"></A></P><TABLE CLASS="display dcenter"><TR VALIGN="middle"><TD CLASS="dcell"><TABLE CELLSPACING=6 CELLPADDING=0><TR><TD ALIGN=right NOWRAP>
180
<I><A HREF="manual019.html#definition"><FONT COLOR=maroon>definition</FONT></A></I></TD><TD ALIGN=center NOWRAP>::=</TD><TD ALIGN=left NOWRAP>
183
<TR><TD ALIGN=right NOWRAP> </TD><TD ALIGN=center NOWRAP>∣</TD><TD ALIGN=left NOWRAP>�<FONT COLOR=blue><TT>module</TT></FONT>�<FONT COLOR=blue><TT>rec</TT></FONT>�<I><A HREF="manual011.html#module-name"><FONT COLOR=maroon>module-name</FONT></A></I>�<FONT COLOR=blue><TT>:</TT></FONT>��<I><A HREF="manual018.html#module-type"><FONT COLOR=maroon>module-type</FONT></A></I>�<FONT COLOR=blue><TT>=</TT></FONT>��<I><A HREF="manual019.html#module-expr"><FONT COLOR=maroon>module-expr</FONT></A></I>�
184
�{�<FONT COLOR=blue><TT>and</TT></FONT>�<I><A HREF="manual011.html#module-name"><FONT COLOR=maroon>module-name</FONT></A></I><FONT COLOR=blue><TT>:</TT></FONT>��<I><A HREF="manual018.html#module-type"><FONT COLOR=maroon>module-type</FONT></A></I>�<FONT COLOR=blue><TT>=</TT></FONT>��<I><A HREF="manual019.html#module-expr"><FONT COLOR=maroon>module-expr</FONT></A></I>�}
186
<TR><TD ALIGN=right NOWRAP> </TD></TR>
187
<TR><TD ALIGN=right NOWRAP>
188
<I><A HREF="manual018.html#specification"><FONT COLOR=maroon>specification</FONT></A></I></TD><TD ALIGN=center NOWRAP>::=</TD><TD ALIGN=left NOWRAP>
191
<TR><TD ALIGN=right NOWRAP> </TD><TD ALIGN=center NOWRAP>∣</TD><TD ALIGN=left NOWRAP>�<FONT COLOR=blue><TT>module</TT></FONT>�<FONT COLOR=blue><TT>rec</TT></FONT>�<I><A HREF="manual011.html#module-name"><FONT COLOR=maroon>module-name</FONT></A></I>�<FONT COLOR=blue><TT>:</TT></FONT>��<I><A HREF="manual018.html#module-type"><FONT COLOR=maroon>module-type</FONT></A></I>
192
�{�<FONT COLOR=blue><TT>and</TT></FONT>�<I><A HREF="manual011.html#module-name"><FONT COLOR=maroon>module-name</FONT></A></I><FONT COLOR=blue><TT>:</TT></FONT>��<I><A HREF="manual018.html#module-type"><FONT COLOR=maroon>module-type</FONT></A></I>�}</TD></TR>
194
</TABLE><P>Recursive module definitions, introduced by the 'module rec' …'and' … construction, generalize regular module definitions
195
<FONT COLOR=blue><TT>module</TT></FONT> <I><A HREF="manual011.html#module-name"><FONT COLOR=maroon>module-name</FONT></A></I> <FONT COLOR=blue><TT>=</TT></FONT> �<I><A HREF="manual019.html#module-expr"><FONT COLOR=maroon>module-expr</FONT></A></I> and module specifications
196
<FONT COLOR=blue><TT>module</TT></FONT> <I><A HREF="manual011.html#module-name"><FONT COLOR=maroon>module-name</FONT></A></I> <FONT COLOR=blue><TT>:</TT></FONT> �<I><A HREF="manual018.html#module-type"><FONT COLOR=maroon>module-type</FONT></A></I> by allowing the defining
197
<I><A HREF="manual019.html#module-expr"><FONT COLOR=maroon>module-expr</FONT></A></I> and the <I><A HREF="manual018.html#module-type"><FONT COLOR=maroon>module-type</FONT></A></I> to refer recursively to the module
281
198
identifiers being defined. A typical example of a recursive module
283
<PRE CLASS="verbatim">
200
</P><PRE CLASS="verbatim"> module rec A : sig
285
201
type t = Leaf of string | Node of ASet.t
286
202
val compare: t -> t -> int
297
213
and ASet : Set.S with type elt = A.t
299
</PRE>It can be given the following specification:
300
<PRE CLASS="verbatim">
215
</PRE><P>It can be given the following specification:
216
</P><PRE CLASS="verbatim"> module rec A : sig
302
217
type t = Leaf of string | Node of ASet.t
303
218
val compare: t -> t -> int
305
220
and ASet : Set.S with type elt = A.t
307
This is an experimental extension of Objective Caml: the class of
221
</PRE><P>This is an experimental extension of Objective Caml: the class of
308
222
recursive definitions accepted, as well as its dynamic semantics are
309
not final and subject to change in future releases.<BR>
311
Currently, the compiler requires that all dependency cycles between
223
not final and subject to change in future releases.</P><P>Currently, the compiler requires that all dependency cycles between
312
224
the recursively-defined module identifiers go through at least one
313
“safe” module. A module is “safe” if all value definitions that
314
it contains have function types <FONT COLOR=maroon><I><TT>ty</TT></I></FONT><SUB>1</SUB> <FONT COLOR=blue><TT>-></TT></FONT> <FONT COLOR=maroon><I><TT>ty</TT></I></FONT><SUB>2</SUB>. Evaluation of a
225
“safe” module. A module is “safe” if all value definitions that
226
it contains have function types <I><A HREF="manual012.html#typexpr"><FONT COLOR=maroon>typexpr</FONT></A></I><SUB>1</SUB> <FONT COLOR=blue><TT>-></TT></FONT> �<I><A HREF="manual012.html#typexpr"><FONT COLOR=maroon>typexpr</FONT></A></I><SUB>2</SUB>. Evaluation of a
315
227
recursive module definition proceeds by building initial values for
316
228
the safe modules involved, binding all (functional) values to
317
<FONT COLOR=blue><TT>fun</TT> <FONT COLOR=maroon><TT><I>x</I></TT></FONT> <TT>-></TT> <TT>raise</TT> <TT>Undefined_recursive_module</TT></FONT>. The defining
229
<FONT COLOR=blue><TT>fun</TT> <TT>_</TT> <TT>-></TT> <TT>raise</TT> <TT>Undefined_recursive_module</TT></FONT>. The defining
318
230
module expressions are then evaluated, and the initial values
319
231
for the safe modules are replaced by the values thus computed. If a
320
232
function component of a safe module is applied during this computation
321
233
(which corresponds to an ill-founded recursive definition), the
322
<TT>Undefined_recursive_module</TT> exception is raised.<BR>
325
<div style="background-color:yellow; color:red; border-style:none; border-width:0.5pt">
326
<H2 CLASS="section"><A NAME="htoc101">7.10</A> Private row types</H2> <A NAME="s-private-rows"></A>
327
<A NAME="@manual.kwd176"></A><BR>
329
<DIV CLASS="center"><TABLE CELLSPACING=2 CELLPADDING=0>
330
<TR><TD ALIGN=right NOWRAP>
331
<FONT COLOR=maroon><I><a href="manual016.html#type-equation"><font color=maroon><TT>type-equation</TT></font></a></I></FONT></TD>
332
<TD ALIGN=right NOWRAP>::=</TD>
333
<TD ALIGN=left NOWRAP>
336
<TR><TD ALIGN=right NOWRAP> </TD>
337
<TD ALIGN=right NOWRAP>∣</TD>
338
<TD ALIGN=left NOWRAP> <FONT COLOR=blue><TT>=</TT> <TT>private</TT></FONT> <FONT COLOR=maroon><I><a href="manual012.html#typexpr"><font color=maroon><TT>typexpr</TT></font></a></I></FONT></TD>
234
<TT>Undefined_recursive_module</TT> exception is raised.</P><H2 CLASS="section"><A NAME="htoc101">7.10</A>��Private row types</H2><P> <A NAME="s-private-rows"></A>
235
<A NAME="@manual.kwd180"></A></P><TABLE CLASS="display dcenter"><TR VALIGN="middle"><TD CLASS="dcell"><TABLE CELLSPACING=6 CELLPADDING=0><TR><TD ALIGN=right NOWRAP>
236
<I><A HREF="manual016.html#type-equation"><FONT COLOR=maroon>type-equation</FONT></A></I></TD><TD ALIGN=center NOWRAP>::=</TD><TD ALIGN=left NOWRAP>
239
<TR><TD ALIGN=right NOWRAP> </TD><TD ALIGN=center NOWRAP>∣</TD><TD ALIGN=left NOWRAP>�<FONT COLOR=blue><TT>=</TT></FONT>�<FONT COLOR=blue><TT>private</TT></FONT>�<I><A HREF="manual012.html#typexpr"><FONT COLOR=maroon>typexpr</FONT></A></I></TD></TR>
340
242
Private row types are type abbreviations where part of the
341
structure of the type is left abstract. Concretely <FONT COLOR=maroon><I><TT>typexpr</TT></I></FONT> in the
243
structure of the type is left abstract. Concretely <I><A HREF="manual012.html#typexpr"><FONT COLOR=maroon>typexpr</FONT></A></I> in the
342
244
above should denote either an object type or a polymorphic variant
343
245
type, with some possibility of refinement left. If the private
344
246
declaration is used in an interface, the corresponding implementation
345
247
may either provide a ground instance, or a refined private type.
346
<PRE CLASS="verbatim">
347
module M : sig type c = private < x : int; .. > val o : c end =
248
</P><PRE CLASS="verbatim"> module M : sig type c = private < x : int; .. > val o : c end =
349
250
class c = object method x = 3 method y = 2 end
352
</PRE>This declaration does more than hiding the <TT>y</TT> method, it also makes
253
</PRE><P>This declaration does more than hiding the <TT>y</TT> method, it also makes
353
254
the type <TT>c</TT> incompatible with any other closed object type, meaning
354
255
that only <TT>o</TT> will be of type <TT>c</TT>. In that respect it behaves
355
256
similarly to private record types. But private row types are
356
257
more flexible with respect to incremental refinement. This feature can
357
258
be used in combination with functors.
358
<PRE CLASS="verbatim">
359
module F(X : sig type c = private < x : int; .. > end) =
259
</P><PRE CLASS="verbatim"> module F(X : sig type c = private < x : int; .. > end) =
361
261
let get_x (o : X.c) = o#x