| 1 |
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
|
| 2 |
<page name="ocaml">
|
| 3 |
|
| 4 |
<title>OCamlDuce</title>
|
| 5 |
|
| 6 |
<left>
|
| 7 |
<local-links href="index,documentation"/>
|
| 8 |
<p>On this page:</p>
|
| 9 |
<boxes-toc/>
|
| 10 |
</left>
|
| 11 |
|
| 12 |
<box>
|
| 13 |
|
| 14 |
<p>
|
| 15 |
OCamlDuce is a merger between <a
|
| 16 |
href="http://caml.inria.fr/">OCaml</a> and
|
| 17 |
<local href="index">CDuce</local>. It comes as a modified
|
| 18 |
version of OCaml which integrates CDuce features: expressions, types,
|
| 19 |
patterns.
|
| 20 |
</p>
|
| 21 |
|
| 22 |
<p>
|
| 23 |
See the <a
|
| 24 |
href="http://pauillac.inria.fr/~frisch/ocamlcduce/doc/README.cduce">README.cduce</a>
|
| 25 |
file for more information, The sub-directory <tt>tests/</tt> has some
|
| 26 |
simple examples. There is also an ocamldoc-generated documentation
|
| 27 |
for the <a href="http://pauillac.inria.fr/~frisch/ocamlcduce/doc/">support library</a>.
|
| 28 |
</p>
|
| 29 |
|
| 30 |
</box>
|
| 31 |
|
| 32 |
<box title="Download and installation" link="install">
|
| 33 |
|
| 34 |
<p>
|
| 35 |
The build procedure for OCamlDuce is exactly the same as for OCaml:
|
| 36 |
<tt>configure, make world, make install</tt>. The names of the tools
|
| 37 |
are unchanged: <tt>ocaml,ocamlc,ocamlopt</tt>. Currently, OCamlDuce
|
| 38 |
is based on CVS snapshots of OCaml (between 3.08.3 and the current
|
| 39 |
<tt>release308</tt> branch) and CDuce (between 0.3.91 and the head).
|
| 40 |
</p>
|
| 41 |
|
| 42 |
<ul>
|
| 43 |
<li><a
|
| 44 |
href="http://pauillac.inria.fr/~frisch/ocamlcduce/download/cduce-ocaml-0.0.5.tar.gz">Compiler,
|
| 45 |
version 0.0.5</a></li>
|
| 46 |
<!--<li><a
|
| 47 |
href="http://pauillac.inria.fr/~frisch/ocamlcduce/download/xml-support-0.0.4.tar.gz">Support
|
| 48 |
library, version 0.0.4</a></li>-->
|
| 49 |
</ul>
|
| 50 |
|
| 51 |
<p>
|
| 52 |
GODI users can upgrade an existing installation by adding this
|
| 53 |
line to their <tt>etc/godi.conf</tt> file:
|
| 54 |
</p>
|
| 55 |
<sample>
|
| 56 |
GODI_BUILD_SITES += http://pauillac.inria.fr/~frisch/ocamlcduce/godi
|
| 57 |
</sample>
|
| 58 |
<p>
|
| 59 |
and by forcing a recompilation of the <tt>godi-ocaml-src</tt>
|
| 60 |
and <tt>godi-ocaml</tt> packages. <!--They should also build
|
| 61 |
the <tt>godi-xml-support</tt> library.-->
|
| 62 |
</p>
|
| 63 |
|
| 64 |
<!--
|
| 65 |
<p>
|
| 66 |
Some simple examples can be found <a -->
|
| 67 |
<!--href="http://pauillac.inria.fr/~frisch/ocamlcduce/tests/">here</a>.</p>
|
| 68 |
-->
|
| 69 |
|
| 70 |
</box>
|
| 71 |
|
| 72 |
<box title="Overview" link="overview">
|
| 73 |
|
| 74 |
<p>
|
| 75 |
In a nutshell, OCamlDuce extends OCaml with new kind of values
|
| 76 |
(<em>x-values</em>) which represent XML documents, fragments, tags, Unicode
|
| 77 |
strings. To describe these values, it also extends the type algebra
|
| 78 |
with so-called <em>x-types</em>. The philosophy behind these types is that they
|
| 79 |
represent <em>set of x-values</em>. They can be very precise: indeed,
|
| 80 |
each value can be seen as a singleton type (a set with a single
|
| 81 |
value), and it is possible to form Boolean combinations of x-types
|
| 82 |
(intersection, union, difference).
|
| 83 |
</p>
|
| 84 |
|
| 85 |
<p>
|
| 86 |
OCamlDuce's type system can be understood as a refinement of OCaml.
|
| 87 |
For each sub-expression which is inferred to be of the x-kind (using
|
| 88 |
OCaml unification based type-system), OCamlDuce will try to infer to
|
| 89 |
best possible sound x-type. Here, best means smallest for the natural
|
| 90 |
subtyping relation (set inclusion). The inference algorithm is
|
| 91 |
actually a data-flow analysis: the x-type will collect all the values
|
| 92 |
that can be produced by the expression, considering all the possible
|
| 93 |
data-flow in the program. It it sometimes necessary to provide
|
| 94 |
explicit type annotations to help the type checker infer this type, in
|
| 95 |
particular when you define recursive functions or when you use
|
| 96 |
iterators.
|
| 97 |
</p>
|
| 98 |
|
| 99 |
<p>
|
| 100 |
Subtyping is implicit for x-types: if an expression is inferred to be
|
| 101 |
of x-type <code>t</code>, which is a subtype of <code>s</code>, then
|
| 102 |
it is possible to use this expression in any context which expects a
|
| 103 |
value of type <code>s</code>.
|
| 104 |
</p>
|
| 105 |
|
| 106 |
</box>
|
| 107 |
|
| 108 |
<box title="Getting started" link="start">
|
| 109 |
|
| 110 |
<p>
|
| 111 |
Most of the new language features are enclosed within double curly braces
|
| 112 |
<code>{{ON}}{{...}}</code>. For instance, the following code sample
|
| 113 |
defines a value <code>x</code> as an XML element (with tag
|
| 114 |
<code>a</code>, an attribute <code>href</code>, and a simple
|
| 115 |
string as content):
|
| 116 |
</p>
|
| 117 |
|
| 118 |
<sample><![CDATA[{{ON}}
|
| 119 |
# let x = {{ <a href="http://www.cduce.org">['CDuce'] }};;
|
| 120 |
val x : {{<a href=[ 'http://www.cduce.org' ]>[ 'CDuce' ]}} =
|
| 121 |
{{<a href="http://www.cduce.org">[ 'CDuce' ]}}
|
| 122 |
]]></sample>
|
| 123 |
|
| 124 |
<p>
|
| 125 |
What appears between the curly braces is called an x-expression.
|
| 126 |
Similarly, there are x-types (as seen above), and also x-patterns.
|
| 127 |
The delimiters <code>{{ON}}{{...}}</code> are only used
|
| 128 |
for syntactical reasons, to avoid clashed between OCaml and CDuce
|
| 129 |
syntaxes and lexical conventions. As a matter of fact,
|
| 130 |
an OCaml expression need not be a syntactical x-expression
|
| 131 |
(delimited by double curly braces) to evaluate to an x-value.
|
| 132 |
For instance, once <code>x</code> has been declared as above,
|
| 133 |
the expression <code>x</code> evaluates to an x-value.
|
| 134 |
</p>
|
| 135 |
|
| 136 |
|
| 137 |
<p>
|
| 138 |
It is possible to use an arbitrary
|
| 139 |
OCaml expression as part of an x-expression: it must simply be
|
| 140 |
protected by a new pair of double curly braces. For instance, there is
|
| 141 |
no <code>if-then-else</code> construction for x-expressions, but you
|
| 142 |
can write:
|
| 143 |
</p>
|
| 144 |
|
| 145 |
<sample><![CDATA[{{ON}}
|
| 146 |
# {{ <a href={{if true then {{"a"}} else {{"z"}}}}>[] }};;
|
| 147 |
- : {{<a href=[ 'a' | 'z' ]>[ ]}} = {{<a href="a">[ ]}}
|
| 148 |
]]></sample>
|
| 149 |
|
| 150 |
<p>
|
| 151 |
Only the highlighted parts are parsed as x-expressions. The
|
| 152 |
<code>if-then-else</code> sub-expression is parsed as an OCaml
|
| 153 |
expression, but its type is an x-type (namely <code>{{ON}}{{[ 'a' |
|
| 154 |
'z' ]}}</code>).
|
| 155 |
</p>
|
| 156 |
|
| 157 |
</box>
|
| 158 |
|
| 159 |
<box title="X-values" link="values">
|
| 160 |
|
| 161 |
<p>
|
| 162 |
X-values are intended to represent XML documents and fragments
|
| 163 |
thereof: elements, tags, text, sequences. In this section, we
|
| 164 |
present the x-value algebra, the syntax of the corresponding
|
| 165 |
x-expression constructors and the associated x-types.
|
| 166 |
</p>
|
| 167 |
|
| 168 |
<p>
|
| 169 |
There are three kinds of atomic kind of x-values:
|
| 170 |
</p>
|
| 171 |
<ul>
|
| 172 |
<li>Unicode characters;</li>
|
| 173 |
<li>qualified names;</li>
|
| 174 |
<li>arbitrarily large integers.</li>
|
| 175 |
</ul>
|
| 176 |
|
| 177 |
<section title="Characters">
|
| 178 |
|
| 179 |
<p>
|
| 180 |
X-characters are different from OCaml characters. They can represent
|
| 181 |
the range of Unicode codepoints defined in the XML specification.
|
| 182 |
Character literals are delimited by single quotes. The escape
|
| 183 |
sequences \n, \r, \t, \b, \', \", \\ are recognized as usual. The
|
| 184 |
numerical escape sequence are written <code>\n;</code> where n is an integer
|
| 185 |
literal (note the extra semi-colon). The source code is interpreted as
|
| 186 |
being encoded in iso-8859-1. As a consequence, Unicode characters which are not
|
| 187 |
part of the Latin1 character set must be introduced with this
|
| 188 |
numerical escape mechanism. The x-types for x-characters are:
|
| 189 |
</p>
|
| 190 |
<ul>
|
| 191 |
<li>singletons;</li>
|
| 192 |
<li>intervals, written <code>c -- d</code>, where <code>c</code> and
|
| 193 |
<code>d</code> are literals (example: <code>{{ON}}type t = {{ 'a'--'z'
|
| 194 |
}}</code>);</li>
|
| 195 |
<li>the type of all x-characters, written <code>Char</code>;</li>
|
| 196 |
<li>the type of all Latin1 characters, written <code>Latin1Char</code>
|
| 197 |
(defined as <code>\0; -- \255;</code>).</li>
|
| 198 |
</ul>
|
| 199 |
|
| 200 |
</section>
|
| 201 |
|
| 202 |
<section title="Integers">
|
| 203 |
|
| 204 |
<p>
|
| 205 |
X-integers are arbitrarily large. Literals must be written in decimal.
|
| 206 |
Negative literals must be in parenthesis. E.g.: <code>(-3)</code>.
|
| 207 |
The x-types for x-integers are:
|
| 208 |
</p>
|
| 209 |
<ul>
|
| 210 |
<li>singletons;</li>
|
| 211 |
<li>intervals, written <code>i -- j</code>, where <code>i</code> and
|
| 212 |
<code>j</code> are literals (example: <code>{{ON}}type t = {{ 10--20
|
| 213 |
}}</code>); it is possible to replace <code>i</code> or <code>j</code>
|
| 214 |
with <code>**</code> to define open-ended intervals, e.g.
|
| 215 |
<code>{{ON}}type pos = {{ 1 -- ** }}</code>;
|
| 216 |
</li>
|
| 217 |
<li>the type of all x-integers, written <code>Int</code>;</li>
|
| 218 |
<li>the type of all the integers which can be represented by a
|
| 219 |
signed 32 (resp. 64) bit machine word, written <code>Int32</code> (resp.
|
| 220 |
<code>Int64</code>).</li>
|
| 221 |
</ul>
|
| 222 |
|
| 223 |
</section>
|
| 224 |
|
| 225 |
<section title="Qualified names">
|
| 226 |
|
| 227 |
<p>
|
| 228 |
Qualified names are intended to represent XML tag names. Conceptually,
|
| 229 |
they are made of a namespace URI and a local name. Since URIs tends
|
| 230 |
to be long, literals are of the form <code>`prefix:local</code>
|
| 231 |
where <code>local</code> is the local name and <code>prefix</code>
|
| 232 |
is an <em>namespace prefix</em> bound to some URI (in the scope of the
|
| 233 |
literal). The local name follows the definitions from
|
| 234 |
the XML Namespaces specification; a dot character must be protected
|
| 235 |
by a backslash and non-Latin1 characters are written as character
|
| 236 |
literals <code>\n;</code>. <a href="#ns">See below</a> for a
|
| 237 |
explanation on how to bind prefixes to URIs. To refer
|
| 238 |
to the default namespace (or the absence of namespace if not default
|
| 239 |
has been defined), the syntax is simply <code>`local</code>.
|
| 240 |
The x-types for qualified names are:
|
| 241 |
</p>
|
| 242 |
<ul>
|
| 243 |
<li>singletons;</li>
|
| 244 |
<li>the type of all qualified names, written <code>Atom</code>;</li>
|
| 245 |
<li>the type of all qualified names from a specified namespace,
|
| 246 |
written <code>`ns:*</code>.</li>
|
| 247 |
</ul>
|
| 248 |
</section>
|
| 249 |
|
| 250 |
<section title="Records">
|
| 251 |
|
| 252 |
<p>
|
| 253 |
X-records are mainly used to represent the set of attributes of an XML
|
| 254 |
element. An x-record is a binding from a finite set of <em>labels</em>
|
| 255 |
to x-values. Labels follows the same syntax as for qualified names
|
| 256 |
without the leading backquote. However, if the namespace prefix is not
|
| 257 |
given, the default namespace does not apply (the namespace URI is
|
| 258 |
empty). The syntax for record x-expressions is <code> { l1=e1
|
| 259 |
... ln=en }</code> where the <code>li</code> are labels and the
|
| 260 |
<code>ei</code> are x-expressions. Fields can also be separated with a
|
| 261 |
semi-colon. It is legal to omit the expression for a field; the label is then
|
| 262 |
taken as the content of the field (a value with this name must be
|
| 263 |
defined in the current scope), e.g.: <code>{{ON}}let x = ... and y = ...
|
| 264 |
in {{ {x y z=3} }}</code> is equivalent to <code>{{ON}}let x = ... and
|
| 265 |
y = ... in {{ {x=x y=y z=3} }}</code>. The types for x-records specify
|
| 266 |
which labels are authorized/mandatory, and what the types of the
|
| 267 |
corresponding fields are. There are two kind of record x-types:
|
| 268 |
</p>
|
| 269 |
|
| 270 |
<ul>
|
| 271 |
<li>
|
| 272 |
Closed record types, which only allow a finite number of fields:
|
| 273 |
<code>{ l1=t1 ... ln=tn }</code>;
|
| 274 |
</li>
|
| 275 |
<li>
|
| 276 |
Open record types, which allow additional fields (with arbitrary
|
| 277 |
type):
|
| 278 |
<code>{ l1=t1 ... ln=tn .. }</code> (the final two colons are
|
| 279 |
in the syntax).
|
| 280 |
</li>
|
| 281 |
</ul>
|
| 282 |
|
| 283 |
<p>
|
| 284 |
In both cases, it is possible to make one of
|
| 285 |
the fields optional by changing = to =?.
|
| 286 |
</p>
|
| 287 |
|
| 288 |
<p>
|
| 289 |
The x-type of all x-record is thus <code>{ .. }</code>,
|
| 290 |
and the x-type of x-records with maybe a field <code>l</code>
|
| 291 |
of type <code>Int</code> and maybe arbitrary other fields is
|
| 292 |
<code>{ l=?Int .. }</code>.
|
| 293 |
</p>
|
| 294 |
|
| 295 |
</section>
|
| 296 |
|
| 297 |
<section title="Sequences">
|
| 298 |
|
| 299 |
<p>
|
| 300 |
X-sequences are finite and ordered collections of x-values.
|
| 301 |
The syntax for a sequence x-expression in
|
| 302 |
<code>[ e1 ... en ]</code> (note that elements are <em>not</em> separated
|
| 303 |
by semi-colons as in OCaml list). Each item <code>ei</code>
|
| 304 |
can either be:
|
| 305 |
</p>
|
| 306 |
<ul>
|
| 307 |
<li>an x-expression;</li>
|
| 308 |
<li><code>!e</code> where <code>e</code> is an x-expression which
|
| 309 |
evaluates to a sequence (whose content is inserted in the sequence
|
| 310 |
which is currently defined); e.g.
|
| 311 |
<code>let x = [ 2 3 ] in [ 1 !x 4 ]</code> is equivalent to
|
| 312 |
<code>[ 1 2 3 4 ]</code>;</li>
|
| 313 |
<li>a string literal delimited by simple quotes; e.g.
|
| 314 |
<code>[ 'abc' ]</code> is equivalent to <code>[ 'a' 'b' 'c' ]</code>.</li>
|
| 315 |
</ul>
|
| 316 |
|
| 317 |
<p>
|
| 318 |
X-types for sequences are of the form <code>[R]</code>
|
| 319 |
where <code>R</code> is a regular expression over x-types which
|
| 320 |
describe the possible contents of the sequences. The possible
|
| 321 |
forms of regular expressions are:
|
| 322 |
</p>
|
| 323 |
|
| 324 |
<ul>
|
| 325 |
<li><code>t</code> (one single element of x-type <code>t</code>)</li>
|
| 326 |
<li><code>R*</code> (zero or more repetitions)</li>
|
| 327 |
<li><code>R+</code> (one or more repetitions)</li>
|
| 328 |
<li><code>R?</code> (zero or one repetition)</li>
|
| 329 |
<li><code>R1 R2</code> (sequence)</li>
|
| 330 |
<li><code>R1|R2</code> (alternation)</li>
|
| 331 |
<li><code>(R)</code></li>
|
| 332 |
<li><code>/t</code> (guard: the tail of the sequence must comply with
|
| 333 |
<code>t</code>).</li>
|
| 334 |
<li><code>PCDATA</code> (equivalent to Char*).</li>
|
| 335 |
</ul>
|
| 336 |
|
| 337 |
<note>sequence are actually encoded with embedded pairs and a
|
| 338 |
terminator, and sequences types are encoded with product types and
|
| 339 |
recursive types. The encoding is available to the programmer
|
| 340 |
but not described in this manual.
|
| 341 |
</note>
|
| 342 |
|
| 343 |
</section>
|
| 344 |
|
| 345 |
<section title="Strings">
|
| 346 |
|
| 347 |
<p>
|
| 348 |
Strings are nothing but sequences of characters. There are two
|
| 349 |
predefined types <code>String</code> and <code>Latin1</code>
|
| 350 |
(defined as <code>[ Char* ]</code> and <code>[ Latin1Char* ]</code>).
|
| 351 |
</p>
|
| 352 |
|
| 353 |
<p>
|
| 354 |
A string literal <code>[ '...' ]</code> can also be written
|
| 355 |
<code>"..." </code> (without the square brackets). Note that simple
|
| 356 |
(resp. double) quotes need to be escaped only when the string is
|
| 357 |
delimited with double (resp. simple) quotes.
|
| 358 |
</p>
|
| 359 |
|
| 360 |
</section>
|
| 361 |
|
| 362 |
<section title="XML elements">
|
| 363 |
|
| 364 |
<p>
|
| 365 |
An XML element is a triple of x-values. The syntax for
|
| 366 |
the corresponding x-expression constructor is
|
| 367 |
<code><![CDATA[<(e1) (e2)>e3]]></code>. When <code>e1</code> is a
|
| 368 |
qualified name literal, it is possible to omit the leading
|
| 369 |
backquote and the surrounding parentheses. Similarly,
|
| 370 |
when <code>e2</code> is an x-record literal, it is possible
|
| 371 |
to omit the curly braces and the parentheses. For instance,
|
| 372 |
one can simply write <code><![CDATA[<a href="abc">['def']]]></code>
|
| 373 |
instead of <code><![CDATA[<(`a) ({href="abc"})>['def']]]></code>.
|
| 374 |
</p>
|
| 375 |
|
| 376 |
<p>
|
| 377 |
XML element x-type are written <code><![CDATA[<(t1) (t2)>t3]]></code>,
|
| 378 |
and the same simplifications applies. For instance, if
|
| 379 |
the namespace prefix <code>ns</code> has been defined,
|
| 380 |
the following is a legal x-type <code><![CDATA[<ns:* ..>[]]]></code>;
|
| 381 |
it describes XML elements whose tag is in the namespace bound to
|
| 382 |
<code>ns</code>, with an empty content, and with an arbitrary set of
|
| 383 |
attributes. An underscore in place of <code>(t1)</code> is
|
| 384 |
equivalent to <code>(Atom)</code> (any tag).
|
| 385 |
</p>
|
| 386 |
|
| 387 |
</section>
|
| 388 |
|
| 389 |
</box>
|
| 390 |
|
| 391 |
<box title="X-expressions" link="expr">
|
| 392 |
|
| 393 |
<p>
|
| 394 |
In the previous section, we have seen the syntax for x-values
|
| 395 |
constructors (constant literals, sequence, record, element constructors).
|
| 396 |
In this section, we describe the other kinds of x-expressions.
|
| 397 |
</p>
|
| 398 |
|
| 399 |
<section title="Binary infix operators">
|
| 400 |
|
| 401 |
<p>
|
| 402 |
The arithmetic operators on integers follow the usual precedence.
|
| 403 |
They are written <code>+,*,-,div,mod</code> (they are all infix).
|
| 404 |
</p>
|
| 405 |
|
| 406 |
<p>
|
| 407 |
Record concatenation: <code>e1 ++ e2</code>. The x-expressions
|
| 408 |
<code>e1</code> and <code>e2</code> must evaluate to x-records.
|
| 409 |
The result is obtained by concatening them. If a field with the same
|
| 410 |
label is present in both records, the right-most one is selected.
|
| 411 |
</p>
|
| 412 |
|
| 413 |
<p>
|
| 414 |
Sequence concatenation: <code>e1 @ e2</code>, equivalent
|
| 415 |
to <code>[!e1 !e2]</code>.
|
| 416 |
</p>
|
| 417 |
|
| 418 |
</section>
|
| 419 |
|
| 420 |
<section title="Projections, filtering">
|
| 421 |
|
| 422 |
<p>
|
| 423 |
If the x-expression <code>e</code> evaluates to a record or an XML
|
| 424 |
element, the construction <code>e.l</code> will extract the value of
|
| 425 |
field or attribute <code>l</code>. Similarly, the construction
|
| 426 |
<code>e.?l</code> will extract the value of field or attribute
|
| 427 |
<code>l</code> if present, and return the empty sequence
|
| 428 |
<code>[]</code> otherwise.
|
| 429 |
</p>
|
| 430 |
|
| 431 |
<p>
|
| 432 |
If the x-expression <code>e</code> evaluates to a record,
|
| 433 |
the construction <code>e -. l</code> will produce a new record
|
| 434 |
where the field <code>l</code> has been removed (if present).
|
| 435 |
</p>
|
| 436 |
|
| 437 |
<p>
|
| 438 |
If the x-expression <code>e</code> evaluates to an x-sequence,
|
| 439 |
the construction <code>e/</code> will result in a new x-sequence
|
| 440 |
obtained by taking in order all the children of the XML elements
|
| 441 |
from the sequence <code>e</code>. For instance, the x-expression
|
| 442 |
<code><![CDATA[[<a>[ 1 2 3 ] 4 5 <b>[ 6 7 8 ] ]/]]></code>
|
| 443 |
evaluates to the x-value <code>[ 1 2 3 6 7 8 ]</code>.
|
| 444 |
</p>
|
| 445 |
|
| 446 |
<p>
|
| 447 |
If the x-expression <code>e</code> evaluates to an x-sequence,
|
| 448 |
the construction <code>e.(t)</code> (where <code>t</code> is an
|
| 449 |
x-type) will result in a new x-sequence
|
| 450 |
obtained by filtering <code>e</code> to keep only the elements
|
| 451 |
of type <code>t</code>. For instance, the x-expression
|
| 452 |
<code><![CDATA[[<a>[ 1 2 3 ] 4 5 <b>[ 6 7 8 ] ].(Int)]]></code>
|
| 453 |
evaluates to the x-value <code>[ 4 5 ]</code>.
|
| 454 |
</p>
|
| 455 |
</section>
|
| 456 |
|
| 457 |
<section title="Dynamic type checking">
|
| 458 |
|
| 459 |
<p>
|
| 460 |
If <code>e</code> is an x-expression and <code>t</code> is an x-type,
|
| 461 |
the construction <code>(e :? t)</code> returns the same
|
| 462 |
result as <code>e</code> if it has type <code>t</code>, and otherwise
|
| 463 |
raises a <code>Failure</code> exception whose argument explains
|
| 464 |
why this is not the case.
|
| 465 |
</p>
|
| 466 |
|
| 467 |
<sample><![CDATA[{{ON}}
|
| 468 |
# let f (x : {{ Any }}) = {{ (x :? <a>[ Int* ] ) }} in
|
| 469 |
f {{ <a>[ 1 2 '3' ] }};;
|
| 470 |
Exception:
|
| 471 |
Failure
|
| 472 |
"Value <a>[ 1 2 '3' ] does not match type <a>[ Int* ]\nValue '3' does not match type Int\n".
|
| 473 |
]]></sample>
|
| 474 |
</section>
|
| 475 |
|
| 476 |
<section title="Pattern matching">
|
| 477 |
|
| 478 |
<p>
|
| 479 |
OCamlDuce comes with a powerful pattern matching operation.
|
| 480 |
X-patterns are described <a href="#patterns">below</a>.
|
| 481 |
The syntax for the pattern matching operation is:
|
| 482 |
<code>match e with p1 -> e1 | ... | pn -> en</code>.
|
| 483 |
The type-system ensures exhaustivivity for the pattern matching
|
| 484 |
and infers precise types for the capture variables.
|
| 485 |
It is also possile to use x-pattern matching as a regular
|
| 486 |
OCaml expression; x-patterns must be surrounded by {{..}}, e.g.:
|
| 487 |
match e with {{p1}} -> e1 | ... | {{pn}} -> en
|
| 488 |
function {{p1}} -> e1 | ... | {{pn}} -> en
|
| 489 |
</p>
|
| 490 |
|
| 491 |
<note>
|
| 492 |
currently it is impossible to mix normal OCaml patterns and x-patterns
|
| 493 |
in a single pattern matching.
|
| 494 |
</note>
|
| 495 |
|
| 496 |
</section>
|
| 497 |
|
| 498 |
<section title="Local binding">
|
| 499 |
|
| 500 |
<p>
|
| 501 |
The x-expression <code>let p=e1 in e2</code> is equivalent to
|
| 502 |
<code>match e1 with p -> e2</code>. There is also an local binding
|
| 503 |
with an x-pattern in OCaml expressions: <code>let {{p}}=e1 in
|
| 504 |
e2</code>.
|
| 505 |
</p>
|
| 506 |
|
| 507 |
</section>
|
| 508 |
|
| 509 |
|
| 510 |
<section title="Iterators">
|
| 511 |
|
| 512 |
<p>
|
| 513 |
OCamlDuce comes with a sequence iterator
|
| 514 |
<code>map e with p1 -> e1 | ... | pn -> en</code> and
|
| 515 |
a tree iterator
|
| 516 |
<code>map* e with p1 -> e1 | ... | pn -> en</code>.
|
| 517 |
</p>
|
| 518 |
|
| 519 |
<p>
|
| 520 |
For both constructions, the argument must evaluate to a sequence.
|
| 521 |
The <code>map</code> iterator applies the patterns to each element
|
| 522 |
of this sequence in turns and produces a new sequence by concatenating
|
| 523 |
all the results (all the right-hand sides must thus produce a
|
| 524 |
sequence). The set of patterns must be exhaustive for all the possible
|
| 525 |
elements of the input sequence.
|
| 526 |
</p>
|
| 527 |
|
| 528 |
<p>
|
| 529 |
The tree iterator is similar except that the patterns need not be
|
| 530 |
exhaustive. If some element of the input sequence is not matched,
|
| 531 |
it is simply copied into the result unless it is an XML element. In
|
| 532 |
this case, the transformation is applied recursively to its content.
|
| 533 |
</p>
|
| 534 |
|
| 535 |
</section>
|
| 536 |
|
| 537 |
<section title="OCaml constructions">
|
| 538 |
|
| 539 |
<p>
|
| 540 |
As a convenience, some of the OCaml expression constructors
|
| 541 |
are allowed as x-expressions (without a need to go back to OCaml
|
| 542 |
with double curly braces): (unqualified) value identifiers and
|
| 543 |
function calls.
|
| 544 |
</p>
|
| 545 |
|
| 546 |
</section>
|
| 547 |
|
| 548 |
</box>
|
| 549 |
|
| 550 |
<box title="More on x-types" link="types">
|
| 551 |
|
| 552 |
<p>
|
| 553 |
We have seen how to write simple x-types. We can then combine
|
| 554 |
them with Boolean connectives:
|
| 555 |
</p>
|
| 556 |
|
| 557 |
<ul>
|
| 558 |
<li><code>t1 & t2</code>: intersection;</li>
|
| 559 |
<li><code>t1 | t2</code>: union;</li>
|
| 560 |
<li><code>t1 - t2</code>: difference.</li>
|
| 561 |
</ul>
|
| 562 |
|
| 563 |
<p>
|
| 564 |
The empty x-type is written <code>Empty</code> (it contains no value),
|
| 565 |
and the universal x-type is written <code>Any</code> (it contains
|
| 566 |
all the x-values) or <code>_</code>.
|
| 567 |
</p>
|
| 568 |
|
| 569 |
<p>
|
| 570 |
When an x-type has been bound to some OCaml identifier
|
| 571 |
(<code>{{ON}}type t = {{...}}</code>), it is possible to use
|
| 572 |
this identifier in another x-type. Recursive definitions
|
| 573 |
are allowed:
|
| 574 |
</p>
|
| 575 |
|
| 576 |
<sample><![CDATA[{{ON}}
|
| 577 |
type t1 = {{ <a>[ t2* ] }}
|
| 578 |
and t2 = {{ <b>[ t1* ] }}
|
| 579 |
]]></sample>
|
| 580 |
|
| 581 |
<p>
|
| 582 |
Note that x-values are always finite and acyclic. The type checker
|
| 583 |
detects type definition which would yield empty types:
|
| 584 |
</p>
|
| 585 |
|
| 586 |
<sample><![CDATA[{{ON}}
|
| 587 |
# type t = {{ <a>[ t+ ] }};;
|
| 588 |
This definition yields an empty type
|
| 589 |
]]></sample>
|
| 590 |
|
| 591 |
<p>
|
| 592 |
If <code>t1</code> and <code>t2</code> are record x-types,
|
| 593 |
we can combine them with the infix <code>++</code> operator, which
|
| 594 |
mimics the corresponding operator on expressions (record
|
| 595 |
concatenation). Similarly, we can use the infix <code>@</code>
|
| 596 |
concatenation operator on sequence x-types.
|
| 597 |
</p>
|
| 598 |
|
| 599 |
</box>
|
| 600 |
|
| 601 |
<box title="X-patterns" link="patterns">
|
| 602 |
|
| 603 |
<p>
|
| 604 |
X-patterns follow the same syntax as X-types. In particular,
|
| 605 |
any X-type is a valid X-pattern. In addition to X-types constructors,
|
| 606 |
X-patterns can have:
|
| 607 |
</p>
|
| 608 |
|
| 609 |
<ul>
|
| 610 |
<li>capture variables (lowercase OCaml identifiers);</li>
|
| 611 |
<li>constant bindings <code>(x := c)</code> where x is a capture
|
| 612 |
variable and c is
|
| 613 |
a literal x-constant (this pattern always succeeds and returns the
|
| 614 |
binding x->c).</li>
|
| 615 |
</ul>
|
| 616 |
|
| 617 |
<p>
|
| 618 |
In record x-patterns, it is possible to omit the <code>=p</code> part of a field.
|
| 619 |
The content is then replaced with the label name considered as
|
| 620 |
a capture variable. E.g. <code>{ x y=p }</code> is equivalent to
|
| 621 |
<code>{ x=x y=p }</code>.</p>
|
| 622 |
|
| 623 |
<p>It is also possible to add an "else" clause:
|
| 624 |
<code>{ x = (a,_)|(a:=3) }</code>
|
| 625 |
will accept any record with atmost the field <code>x</code>. If the content
|
| 626 |
is a pair, the capture variable a will be bound to its component;
|
| 627 |
otherwise, it is set to <code>3</code>.</p>
|
| 628 |
|
| 629 |
<p>
|
| 630 |
In regular expressions, it is possible to extract whole subsequences
|
| 631 |
with the notation <code>x::R</code>, e.g.: <code>[ _* x::Int+ _* ]</code>
|
| 632 |
</p>
|
| 633 |
|
| 634 |
<p>
|
| 635 |
If the same sequence capture variable appears several times (or below a
|
| 636 |
repetition) in a regexp, it is bound to the concatenation of all
|
| 637 |
matched subsequences. E.g.: <code>[ (x::Int | _)* ]</code> will
|
| 638 |
collect in <code>x</code> all the elements of type <code>Int</code> from
|
| 639 |
a sequence.</p>
|
| 640 |
|
| 641 |
<p>
|
| 642 |
The regexp operators +,*,? are greedy by default (they match as long
|
| 643 |
as possible). They admit non-greedy variants +?,*?,??.
|
| 644 |
</p>
|
| 645 |
</box>
|
| 646 |
|
| 647 |
<box title="Namespace bindings" link="ns">
|
| 648 |
|
| 649 |
<p>
|
| 650 |
The binding of namespace prefixes to URIs
|
| 651 |
can be done either by toplevel phrases (structure items) or
|
| 652 |
by local declarations:
|
| 653 |
</p>
|
| 654 |
|
| 655 |
<sample>{{ON}}
|
| 656 |
# {{ namespace ns = "http://..." }};;
|
| 657 |
# let x = {{ `ns: x }};;
|
| 658 |
val x : {{`ns:x}} = {{`ns:x}}
|
| 659 |
# let x = {{ let namespace ns = "http://..." in `ns:x }};;
|
| 660 |
val x : {{`ns:x}} = {{`ns:x}}
|
| 661 |
</sample>
|
| 662 |
|
| 663 |
<p>The toplevel definitions can also appear in module interfaces
|
| 664 |
(signatures). A toplevel prefix binding is not exported by a module: its scope
|
| 665 |
is limited to the current structure or signature. It is possible
|
| 666 |
to specify a default namespace, and to reset it:
|
| 667 |
</p>
|
| 668 |
|
| 669 |
<sample>{{ON}}
|
| 670 |
# {{ namespace "http://..." }};;
|
| 671 |
# {{ `x }};;
|
| 672 |
- : {{`ns1:x}} = {{`ns1:x}}
|
| 673 |
# {{ namespace "" }};;
|
| 674 |
# {{ `x }};;
|
| 675 |
- : {{`x}} = {{`x}}
|
| 676 |
</sample>
|
| 677 |
|
| 678 |
<p>
|
| 679 |
Note that the value pretty-printer invented some prefix
|
| 680 |
for the namespace URI. The default prefix declaration also have a
|
| 681 |
local form <code> let namespace "..." in ... </code>.
|
| 682 |
</p>
|
| 683 |
|
| 684 |
</box>
|
| 685 |
|
| 686 |
<box title="A few words about performances" link="perf">
|
| 687 |
|
| 688 |
<p>
|
| 689 |
TODO: efficient repr of strings, concatenation. Pattern matching.
|
| 690 |
</p>
|
| 691 |
|
| 692 |
</box>
|
| 693 |
|
| 694 |
</page>
|