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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
<!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>
Language extensions
</TITLE>
</HEAD>
<BODY >
<A HREF="manual008.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="manual022.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>
<H1 CLASS="chapter"><A NAME="htoc91">Chapter 7</A> Language extensions</H1> <A NAME="c:extensions"></A>
This chapter describes language extensions and convenience features
that are implemented in Objective Caml, but not described in the
Objective Caml reference manual.<BR>
<BR>
<H2 CLASS="section"><A NAME="htoc92">7.1</A> Integer literals for types <TT>int32</TT>, <TT>int64</TT> and <TT>nativeint</TT></H2><BR>
<BR>
<DIV CLASS="center"><TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD ALIGN=right NOWRAP>
<A NAME="int32-literal"></A>
<FONT COLOR=maroon><I><TT>int32-literal</TT></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP> <FONT COLOR=maroon><I><a href="manual009.html#integer-literal"><font color=maroon><TT>integer-literal</TT></font></a></I></FONT> <FONT COLOR=blue><TT>l</TT></FONT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP>
<A NAME="int64-literal"></A>
<FONT COLOR=maroon><I><TT>int64-literal</TT></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP> <FONT COLOR=maroon><I><a href="manual009.html#integer-literal"><font color=maroon><TT>integer-literal</TT></font></a></I></FONT> <FONT COLOR=blue><TT>L</TT></FONT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP>
<A NAME="nativeint-literal"></A>
<FONT COLOR=maroon><I><TT>nativeint-literal</TT></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP> <FONT COLOR=maroon><I><a href="manual009.html#integer-literal"><font color=maroon><TT>integer-literal</TT></font></a></I></FONT> <FONT COLOR=blue><TT>n</TT></FONT></TD>
</TR></TABLE></DIV>
An integer literal can be followed by one of the letters <TT>l</TT>, <TT>L</TT> or <TT>n</TT>
to indicate that this integer has type <TT>int32</TT>, <TT>int64</TT> or <TT>nativeint</TT>
respectively, instead of the default type <TT>int</TT> for integer literals.
<A NAME="@manual3"></A>
<A NAME="@manual4"></A>
<A NAME="@manual5"></A>
The library modules <TT>Int32</TT>[<TT><A HREF="libref/Int32.html">Int32</A></TT>],
<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>]
provide operations on these integer types.<BR>
<BR>
<H2 CLASS="section"><A NAME="htoc93">7.2</A> Streams and stream parsers</H2>
<A NAME="s:streams"></A>
The syntax for streams and stream parsers is no longer part of the
Objective Caml language, but available through a Camlp4 syntax
extension. See the Camlp4 reference manual for more information.
<div style="background-color:yellow; color:red; border-style:none; border-width:0.5pt">
Support for basic operations on streams is still available through the
<TT>Stream</TT>[<TT><A HREF="libref/Stream.html">Stream</A></TT>]
module of the standard library.
</div>
Objective Caml programs
that use the stream parser syntax should be compiled with the
<TT>-pp camlp4o</TT> option to <TT>ocamlc</TT> and <TT>ocamlopt</TT>. For interactive use,
run <TT>ocaml</TT> and issue the <CODE>#load "camlp4o.cma";;</CODE> command.<BR>
<BR>
<H2 CLASS="section"><A NAME="htoc94">7.3</A> Recursive definitions of values</H2> <A NAME="s:letrecvalues"></A>
As mentioned in section <A HREF="manual015.html#s:localdef">6.7.1</A>, the <FONT COLOR=blue><TT>let rec</TT></FONT> binding
construct, in addition to the definition of recursive functions,
also supports a certain class of recursive definitions of
non-functional values, such as
<DIV CLASS="center">
<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>
<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>
<FONT COLOR=blue><TT>in</TT></FONT> <FONT COLOR=maroon><I><TT>expr</TT></I></FONT>
</DIV>
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
<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
definitions where the defined names occur only inside function
bodies or as argument to a data constructor.<BR>
<BR>
More precisely, consider the expression:
<DIV CLASS="center">
<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>
</DIV>
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
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
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>
<BR>
An expression <FONT COLOR=maroon><I><TT>e</TT></I></FONT> is said to be <EM>statically constructive
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
one of the following conditions is true:
<UL CLASS="itemize"><LI CLASS="li-itemize">
<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>
<LI CLASS="li-itemize"><FONT COLOR=maroon><I><TT>e</TT></I></FONT> is a variable
<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> …
<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> …
<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>
<LI CLASS="li-itemize"><FONT COLOR=maroon><I><TT>e</TT></I></FONT> has one of the following forms, where each one of
<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
<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
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>:
<UL CLASS="itemize"><LI CLASS="li-itemize">
<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> …
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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
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>
<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>
<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>
</UL>
</UL>
An expression <FONT COLOR=maroon><I><TT>e</TT></I></FONT> is said to be <EM>immediately linked to</EM> the variable
<FONT COLOR=maroon><I><TT>name</TT></I></FONT> in the following cases:
<UL CLASS="itemize"><LI CLASS="li-itemize">
<FONT COLOR=maroon><I><TT>e</TT></I></FONT> is <FONT COLOR=maroon><I><TT>name</TT></I></FONT>
<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>
is immediately linked to <FONT COLOR=maroon><I><TT>name</TT></I></FONT>
<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> …
<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
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>
is immediately linked to <FONT COLOR=maroon><I><TT>name</TT></I></FONT>.
</UL>
<H2 CLASS="section"><A NAME="htoc95">7.4</A> Range patterns</H2>
In patterns, Objective Caml recognizes the form
<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>
(two character literals separated by <TT>..</TT>) as shorthand for the pattern
<DIV CLASS="center">
<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> …
<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>
</DIV>
where <I>c</I><SUB>1</SUB>, <I>c</I><SUB>2</SUB>, ..., <I>c<SUB>n</SUB></I> are the characters
that occur between <I>c</I> and <I>d</I> in the ASCII character set. For
instance, the pattern <TT>'0'..'9'</TT> matches all characters that are digits.<BR>
<BR>
<H2 CLASS="section"><A NAME="htoc96">7.5</A> Assertion checking</H2>
<A NAME="@manual.kwd169"></A>
Objective Caml supports the <TT>assert</TT> construct to check debugging assertions.
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
returns <TT>()</TT> if <FONT COLOR=maroon><I><TT>expr</TT></I></FONT> evaluates to <TT>true</TT>. Otherwise, the exception
<TT>Assert_failure</TT> is raised with the source file name and the
location of <FONT COLOR=maroon><I><TT>expr</TT></I></FONT> as arguments. Assertion
checking can be turned off with the <TT>-noassert</TT> compiler option.<BR>
<BR>
As a special case, <TT>assert false</TT> is reduced to
<TT>raise (Assert_failure ...)</TT>, which is polymorphic (and
is not turned off by the <TT>-noassert</TT> option).
<A NAME="@manual6"></A><BR>
<BR>
<H2 CLASS="section"><A NAME="htoc97">7.6</A> Lazy evaluation</H2>
<A NAME="@manual.kwd170"></A>
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
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
evaluated at this point in the program. Instead, its evaluation will
be performed the first time <TT>Lazy.force</TT> is applied to the value
<I>v</I>, returning the actual value of <FONT COLOR=maroon><I><TT>expr</TT></I></FONT>. Subsequent applications
of <TT>Lazy.force</TT> to <I>v</I> do not evaluate <FONT COLOR=maroon><I><TT>expr</TT></I></FONT> again.
For more information, see the description of module <TT>Lazy</TT> in the
standard library (see
<A HREF="libref/Lazy.html">Module <TT>Lazy</TT></A>).
<A NAME="@manual7"></A><A NAME="@manual8"></A><BR>
<BR>
<H2 CLASS="section"><A NAME="htoc98">7.7</A> Local modules</H2>
<A NAME="@manual.kwd171"></A>
<A NAME="@manual.kwd172"></A>
The expression
<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>
locally binds the module expression <FONT COLOR=maroon><I><TT>module-expr</TT></I></FONT> to the identifier
<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>.
It then returns the value of <FONT COLOR=maroon><I><TT>expr</TT></I></FONT>. For example:
<PRE CLASS="verbatim">
let remove_duplicates comparison_fun string_list =
let module StringSet =
Set.Make(struct type t = string
let compare = comparison_fun end) in
StringSet.elements
(List.fold_right StringSet.add string_list StringSet.empty)
</PRE>
<H2 CLASS="section"><A NAME="htoc99">7.8</A> Private types</H2>
<A NAME="@manual.kwd173"></A><BR>
<BR>
<DIV CLASS="center"><TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD ALIGN=right NOWRAP>
<FONT COLOR=maroon><I><a href="manual016.html#type-representation"><font color=maroon><TT>type-representation</TT></font></a></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
...</TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>∣</TD>
<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>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>∣</TD>
<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>
</TR></TABLE></DIV>
Private types are variant or record types. Values of
these types can be de-structured normally in pattern-matching or via
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
these types cannot be constructed directly by constructor application
or record construction. Moreover, assignment on a mutable field of a
private record type is not allowed.<BR>
<BR>
The typical use of private types is in the export signature of a
module, to ensure that construction of values of the private type always
go through the functions provided by the module, while still allowing
pattern-matching outside the defining module. For example:
<PRE CLASS="verbatim">
module M : sig
type t = private A | B of int
val a : t
val b : int -> t
end
= struct
type t = A | B of int
let a = A
let b n = assert (n > 0); B n
end
</PRE>Here, the <TT>private</TT> declaration ensures that in any value of type
<TT>M.t</TT>, the argument to the <TT>B</TT> constructor is always a positive integer.<BR>
<BR>
<div style="background-color:yellow; color:red; border-style:none; border-width:0.5pt">
With respect to the variance of their parameters, private types are
handled like abstract types. That is, if a private type has
parameters, their variance is the one explicitly given by prefixing
the parameter by a `<TT>+</TT>' or a `<TT>-</TT>', it is invariant otherwise.<BR>
</div>
<BR>
<H2 CLASS="section"><A NAME="htoc100">7.9</A> Recursive modules</H2> <A NAME="s-recursive-modules"></A>
<A NAME="@manual.kwd174"></A>
<A NAME="@manual.kwd175"></A><BR>
<BR>
<DIV CLASS="center"><TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD ALIGN=right NOWRAP>
<FONT COLOR=maroon><I><a href="manual019.html#definition"><font color=maroon><TT>definition</TT></font></a></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
...</TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>∣</TD>
<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>
{ <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>
</TR>
<TR><TD ALIGN=right NOWRAP>
<FONT COLOR=maroon><I><a href="manual018.html#specification"><font color=maroon><TT>specification</TT></font></a></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
...</TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>∣</TD>
<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>
{ <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>
</TR></TABLE></DIV><BR>
<BR>
Recursive module definitions, introduced by the 'module rec' ...'and' ... construction, generalize regular module definitions
<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
<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
<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
identifiers being defined. A typical example of a recursive module
definition is:
<PRE CLASS="verbatim">
module rec A : sig
type t = Leaf of string | Node of ASet.t
val compare: t -> t -> int
end
= struct
type t = Leaf of string | Node of ASet.t
let compare t1 t2 =
match (t1, t2) with
(Leaf s1, Leaf s2) -> Pervasives.compare s1 s2
| (Leaf _, Node _) -> 1
| (Node _, Leaf _) -> -1
| (Node n1, Node n2) -> ASet.compare n1 n2
end
and ASet : Set.S with type elt = A.t
= Set.Make(A)
</PRE>It can be given the following specification:
<PRE CLASS="verbatim">
module rec A : sig
type t = Leaf of string | Node of ASet.t
val compare: t -> t -> int
end
and ASet : Set.S with type elt = A.t
</PRE>
This is an experimental extension of Objective Caml: the class of
recursive definitions accepted, as well as its dynamic semantics are
not final and subject to change in future releases.<BR>
<BR>
Currently, the compiler requires that all dependency cycles between
the recursively-defined module identifiers go through at least one
“safe” module. A module is “safe” if all value definitions that
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
recursive module definition proceeds by building initial values for
the safe modules involved, binding all (functional) values to
<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
module expressions are then evaluated, and the initial values
for the safe modules are replaced by the values thus computed. If a
function component of a safe module is applied during this computation
(which corresponds to an ill-founded recursive definition), the
<TT>Undefined_recursive_module</TT> exception is raised.<BR>
<BR>
<div style="background-color:yellow; color:red; border-style:none; border-width:0.5pt">
<H2 CLASS="section"><A NAME="htoc101">7.10</A> Private row types</H2> <A NAME="s-private-rows"></A>
<A NAME="@manual.kwd176"></A><BR>
<BR>
<DIV CLASS="center"><TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD ALIGN=right NOWRAP>
<FONT COLOR=maroon><I><a href="manual016.html#type-equation"><font color=maroon><TT>type-equation</TT></font></a></I></FONT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
...</TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>∣</TD>
<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>
</TR></TABLE></DIV>
Private row types are type abbreviations where part of the
structure of the type is left abstract. Concretely <FONT COLOR=maroon><I><TT>typexpr</TT></I></FONT> in the
above should denote either an object type or a polymorphic variant
type, with some possibility of refinement left. If the private
declaration is used in an interface, the corresponding implementation
may either provide a ground instance, or a refined private type.
<PRE CLASS="verbatim">
module M : sig type c = private < x : int; .. > val o : c end =
struct
class c = object method x = 3 method y = 2 end
let o = new c
end
</PRE>This declaration does more than hiding the <TT>y</TT> method, it also makes
the type <TT>c</TT> incompatible with any other closed object type, meaning
that only <TT>o</TT> will be of type <TT>c</TT>. In that respect it behaves
similarly to private record types. But private row types are
more flexible with respect to incremental refinement. This feature can
be used in combination with functors.
<PRE CLASS="verbatim">
module F(X : sig type c = private < x : int; .. > end) =
struct
let get_x (o : X.c) = o#x
end
module G(X : sig type c = private < x : int; y : int; .. > end) =
struct
include F(X)
let get_y (o : X.c) = o#y
end
</PRE>
Polymorphic variant types can be refined in two ways, either to allow
the addition of new constructors, or to allow the disparition of
declared constructors. The second case corresponds to private variant
types (one cannot create a value of the private type), while the first
case requires default cases in pattern-matching to handle addition.
<PRE CLASS="verbatim">
type t = [ `A of int | `B of bool ]
type u = private [< t > `A ]
type v = private [> t ]
</PRE>With type <TT>u</TT>, it is possible to create values of the form <TT>(`A n)</TT>,
but not <TT>(`B b)</TT>. With type <TT>v</TT>, construction is not restricted but
pattern-matching must have a default case.<BR>
<BR>
Like for abstract and private types, the variance of type parameters
is not infered, and must be given explicitly.
<BR>
</div>
<BR>
<HR>
<A HREF="manual008.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="manual022.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
|