| 1 |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
| 2 |
<page name="memento">
|
| 3 |
|
| 4 |
<title>Quick reference</title>
|
| 5 |
|
| 6 |
<left>
|
| 7 |
<p>This page briefly presents the syntax of the CDuce language.</p>
|
| 8 |
<boxes-toc/>
|
| 9 |
<p>See also:</p>
|
| 10 |
<local-links href="index,manual,tutorial"/>
|
| 11 |
</left>
|
| 12 |
|
| 13 |
<box title="Identifiers" link="id">
|
| 14 |
<ul>
|
| 15 |
<li> Type and Pattern identifiers: words formed by of Unicode letters and and
|
| 16 |
the underscore "_" character, starting by a capitalized letter. </li>
|
| 17 |
|
| 18 |
<li> value identifiers: words formed by of Unicode letters and the underscore "
|
| 19 |
_" character, starting by a capitalized letter or underscore.</li>
|
| 20 |
</ul>
|
| 21 |
</box>
|
| 22 |
|
| 23 |
<box title="Scalars" link="scalars">
|
| 24 |
<ul>
|
| 25 |
<li>Large integers:
|
| 26 |
<ul>
|
| 27 |
<li>Values: <code>0,1,2,3,...</code> </li>
|
| 28 |
<li>Types: intervals <code>-*--10, 20--30, 50--*, ...</code>,
|
| 29 |
singletons <code>0,1,2,3,...</code> </li>
|
| 30 |
<li>Operators: <code>+,-,/,*,div,mod, int_of</code> </li>
|
| 31 |
</ul>
|
| 32 |
</li>
|
| 33 |
<li>Floats:
|
| 34 |
<ul>
|
| 35 |
<li>Values: <i>none built-in</i>. </li>
|
| 36 |
<li>Types: only <code>Float</code>. </li>
|
| 37 |
<li>Operators: <code>float_of</code> : String -> Float</li>
|
| 38 |
</ul>
|
| 39 |
</li>
|
| 40 |
<li>Unicode characters:
|
| 41 |
<ul>
|
| 42 |
<li>Values: quoted characters (<code>'a'</code>, <code>'b'</code>,
|
| 43 |
<code>'c'</code>, ...,<code>'あ'</code>, <code>'い'</code>, ... ,
|
| 44 |
<code>'私'</code>, ... , <code>'⊆'</code>, ...),
|
| 45 |
codepoint-defined characters (<code>'\x%%h%%;' '\%%d%%;'
|
| 46 |
</code> where <code>%%h%%</code> and
|
| 47 |
<code>%%d%%</code> are hexadecimal and decimal integers
|
| 48 |
respectively), and backslash-escaped characters
|
| 49 |
(<code>'\t'</code> tab, <code>'\n'</code> newline,
|
| 50 |
<code>'\r'</code> return, <code>'\\'</code> backslash).</li>
|
| 51 |
<li>Types: intervals <code>'a'--'z', '0'--'9'</code>,
|
| 52 |
singletons <code>'a','b','c',...</code> </li>
|
| 53 |
<li>Operators: <code>char_of_int</code> : Int -> Char</li>
|
| 54 |
</ul>
|
| 55 |
</li>
|
| 56 |
<li>Symbolic atoms:
|
| 57 |
<ul>
|
| 58 |
<li>Values: <code>`A, `B, `a, `b, `true, `false, ...</code> </li>
|
| 59 |
<li>Types: singletons <code>`A, `B, ...</code> </li>
|
| 60 |
<li>Operators: <code>make_atom</code> : (String,String) -> Atom,
|
| 61 |
<code>split_atom</code> : Atom -> (String,String) </li>
|
| 62 |
<li>CDuce also supports <local href="namespaces">XML Namespaces</local></li>
|
| 63 |
</ul>
|
| 64 |
</li>
|
| 65 |
</ul>
|
| 66 |
</box>
|
| 67 |
|
| 68 |
<box title="Operators, built-in functions" link="op">
|
| 69 |
<ul>
|
| 70 |
<li>Infix:
|
| 71 |
<br/> <code>@</code> : concatenation of sequences
|
| 72 |
<br/> <code>+,*,-,div,mod</code> : Integer,Integer -> Integer
|
| 73 |
<br/> <code>=, <<, <=, >>, >= </code> :
|
| 74 |
<i>t</i>,<i>t</i> -> Bool = <code>`true | `false</code> (any non functional type <i>t</i>)
|
| 75 |
<br/> <code>||, &&</code> : Bool,Bool -> Bool
|
| 76 |
<br/> <code>not</code>: Bool -> Bool
|
| 77 |
</li>
|
| 78 |
<li>Prefix:
|
| 79 |
<br/><code>load_xml</code> : Latin1 -> AnyXml,
|
| 80 |
<br/><code>load_html</code> : Latin1 -> [ Any* ],
|
| 81 |
<br/><code>load_file</code> : Latin1 -> Latin1,
|
| 82 |
<br/><code>load_file_utf8</code> : Latin1 -> String,
|
| 83 |
<br/><code>dump_to_file</code> : Latin1 -> String -> [],
|
| 84 |
<br/><code>dump_to_file_utf8</code> : Latin1 -> String -> [],
|
| 85 |
<br/><code>print_xml</code> : Any -> Latin1,
|
| 86 |
<br/><code>print_xml_utf8</code> : Any -> String,
|
| 87 |
<br/><code>print</code> : Latin1 -> [],
|
| 88 |
<br/><code>print_utf8</code> : String -> [],
|
| 89 |
<br/><code>int_of</code> : String -> Int,
|
| 90 |
<br/><code>float_of</code> : String -> Float,
|
| 91 |
<br/><code>string_of</code> : Any -> Latin1,
|
| 92 |
<br/><code>char_of_int</code> : Int -> Char,
|
| 93 |
<br/><code>atom_of</code> : String -> Atom,
|
| 94 |
<br/><code>system</code> : Latin1 -> { stdout = Latin1; stderr = Latin1;
|
| 95 |
status = (`exited,Int) | (`stopped,Int) | (`signaled,Int)
|
| 96 |
},
|
| 97 |
<br/><code>exit</code> : 0--255 -> Empty,
|
| 98 |
<br/><code>getenv</code> : Latin1 -> Latin1,
|
| 99 |
<br/><code>argv</code> : [] -> [ String* ],
|
| 100 |
<br/><code>raise</code> : Any -> Empty
|
| 101 |
</li>
|
| 102 |
</ul>
|
| 103 |
</box>
|
| 104 |
|
| 105 |
<box title="Pairs" link="pair">
|
| 106 |
<ul>
|
| 107 |
<li>Expressions: <code>(e1,e2)</code> </li>
|
| 108 |
<li>Types and patterns: <code>(t1,t2)</code> </li>
|
| 109 |
<li>Note: tuples are right-associative pairs; e.g.:
|
| 110 |
<code>(1,2,3)=(1,(2,3))</code> </li>
|
| 111 |
<li>When a capture variable appears on both side of a pair pattern,
|
| 112 |
the two captured values are paired
|
| 113 |
together (e.g. <code>match (1,2,3) with (x,(_,x)) -> x ==>
|
| 114 |
(1,3)</code>). </li>
|
| 115 |
</ul>
|
| 116 |
</box>
|
| 117 |
|
| 118 |
<box title="Sequences" link="seq">
|
| 119 |
<ul>
|
| 120 |
<li>Expressions: <code>[ 1 2 3 ]</code>,
|
| 121 |
which is syntactic sugar for <code>(1,(2,(3,`nil)))</code> </li>
|
| 122 |
<li>A sub-sequence can be escaped by !: <code>[ 1 2 ![ 3 4 ] 5
|
| 123 |
]</code> is then equal to <code>[ 1 2 3 4 5 ]</code> . </li>
|
| 124 |
<li>Types and patterns : <code>[ R ]</code> where <code>R</code> is
|
| 125 |
a regular expression built on types and patterns:
|
| 126 |
<ul>
|
| 127 |
<li>A type or a pattern is a regexp by itself, matching a single
|
| 128 |
element of the sequence </li>
|
| 129 |
<li>Postfix repetition operators: <code>*,+,?</code>
|
| 130 |
and the ungreedy variants (for patterns) <code>*?, +?
|
| 131 |
,??</code></li>
|
| 132 |
<li>Concatenation of regexps</li>
|
| 133 |
<li>For patterns, sequence capture variable <code>x::R</code> </li>
|
| 134 |
</ul>
|
| 135 |
</li>
|
| 136 |
<li>It is possible to specify a tail, for expressions, types, and patterns;
|
| 137 |
e.g.: <code>[ x::Int*; q ]</code> </li>
|
| 138 |
<li>Map: <code>map e with p1 -> e1 | ... | pn -> en</code>.
|
| 139 |
Each element of e must be matched. </li>
|
| 140 |
<li>Transform: <code>transform e with p1 -> e1 | ... | pn -> en</code>.
|
| 141 |
Unmatched elements are discarded; each branch returns a sequence
|
| 142 |
and all the resulting sequences are concatenated together. </li>
|
| 143 |
<li>Selection: : <code>select %%e%% from %%p1%% in %%e1%% ... %%pn%%
|
| 144 |
in %%en%% where %%e'%%</code>. SQL-like selection with the possibility
|
| 145 |
of using CDuce patterns instead of variables. <code>%%e1%% ...
|
| 146 |
%%en%%</code> must be sequences and <code>%%e'%%</code> a boolean
|
| 147 |
expression.</li>
|
| 148 |
<li>Operators: concatenation <code>e1 @ e2 = [ !e1 !e2 ]</code>,
|
| 149 |
flattening <code>flatten e = transform e with x -> x</code>.
|
| 150 |
</li>
|
| 151 |
</ul>
|
| 152 |
</box>
|
| 153 |
|
| 154 |
<box title="Record" link="record">
|
| 155 |
<ul>
|
| 156 |
<li>Records literal <code>{ l1 = e1; ...; ln = en }</code></li>
|
| 157 |
<li>Types: <code>{ l1 = t1; ...; ln = tn }</code> (closed, no more
|
| 158 |
fields allowed), <code>{ l1 = t1; ...; ln = tn; .. }</code> (open,
|
| 159 |
any other field allowed). Optional fields: <code>li =? ti</code>
|
| 160 |
instead of <code>li = ti</code>. Semi-colons are optional.</li>
|
| 161 |
<li>Record concatenation: <code>e1 + e2</code>
|
| 162 |
(priority to the fields from the right argument) </li>
|
| 163 |
<li>Field removal: <code>e1 \ l</code> (does nothing if the
|
| 164 |
field <code>l</code> is not present)</li>
|
| 165 |
<li>Field access: <code>e1.l</code></li>
|
| 166 |
<li>Labels are in fact Qualified Names (see <local href="namespaces"/>)</li>
|
| 167 |
</ul>
|
| 168 |
</box>
|
| 169 |
|
| 170 |
<box title="Strings" link="string">
|
| 171 |
<ul>
|
| 172 |
<li>Strings are actually sequences of characters.</li>
|
| 173 |
<li>Expressions: <code>"abc", [ 'abc' ], [ 'a' 'b' 'c' ]</code>. </li>
|
| 174 |
<li>Operators: <code>string_of, print, dump_to_file</code></li>
|
| 175 |
<li><code>PCDATA</code> means <code>Char*</code> inside regular expressions</li>
|
| 176 |
</ul>
|
| 177 |
</box>
|
| 178 |
|
| 179 |
<box title="XML elements" link="xml">
|
| 180 |
<ul>
|
| 181 |
<li>Expressions: <code> <(tag) (attr)>content</code> </li>
|
| 182 |
<li>If the tag is an atom <code>`X</code>, it can be written
|
| 183 |
<code>X</code> (without the <code>(..)</code>).
|
| 184 |
Similarly, parenthesis and curly braces may be omitted
|
| 185 |
when attr is a record <code>l1=e1;...;ln=en</code>
|
| 186 |
(semicolon can also be omitted in this case).
|
| 187 |
E.g: <code><a href="abc">[ 'abc' ]</code>.</li>
|
| 188 |
<li>Types and patterns: same notations.</li>
|
| 189 |
<li>XPath like projection: <code>%%e%%/%%t%%</code>. For every
|
| 190 |
XML tree in <code>%%e%%</code> it returns the sequence of children
|
| 191 |
of type <code>%%t%%</code></li>
|
| 192 |
<li>Tree transformation: <code>xtransform e with p1 -> e1 | ... | pn -> en</code>.
|
| 193 |
Applies to sequences of XML trees. Unmatched elements are left unchanged and the
|
| 194 |
transformation is recursively applied to the sequence of children of the unmatched
|
| 195 |
element; as for transform, each branch returns a sequence
|
| 196 |
and all the resulting sequences are concatenated together. </li>
|
| 197 |
<li>Operators: <code>load_xml : Latin1 -> AnyXml; print_xml : Any -> Latin1</code>
|
| 198 |
</li>
|
| 199 |
</ul>
|
| 200 |
</box>
|
| 201 |
|
| 202 |
<box title="Functions" link="fun">
|
| 203 |
<ul>
|
| 204 |
<li>Expressions:
|
| 205 |
<ul>
|
| 206 |
<li>General form: <code>fun f (t1->s1;...;tn->sn)
|
| 207 |
p1 -> e1 | ... | pm -> em</code> (<code>f</code> is optional) </li>
|
| 208 |
<li>Simple function: <code>fun f (p : t) : s = e</code>,
|
| 209 |
equivalent to <code>fun f (t -> s) p -> e</code> </li>
|
| 210 |
<li>Multiple arguments: <code>fun f (p1 : t1, p2 : t2,...) : s =
|
| 211 |
e</code>, equivalent to <code>fun f ((p1,p2,...):(t1,t2,...)) : s
|
| 212 |
= e</code> (note the blank spaces around colons to avoid ambiguity
|
| 213 |
with namespaces) </li>
|
| 214 |
<li>Currified function: <code>fun f (p1 : t1) (p2 : t2) ... : s =
|
| 215 |
e</code> (can be combined with the multiple arguments syntax).</li>
|
| 216 |
</ul>
|
| 217 |
</li>
|
| 218 |
<li>Types: <code>t -> s</code> </li>
|
| 219 |
</ul>
|
| 220 |
</box>
|
| 221 |
|
| 222 |
<box title="Pattern matching, exceptions, ..." link="match">
|
| 223 |
<ul>
|
| 224 |
<li>Type restriction: <code>(e : t)</code> (forgets any more precise
|
| 225 |
type for <code>e</code>; note the blank spaces around colons to avoid ambiguity with namespaces) </li>
|
| 226 |
<li>Pattern matching: <code>match e with p1 -> e1 | ... | pn ->
|
| 227 |
en</code></li>
|
| 228 |
<li>Local binding: <code>let p = e1 in e2</code>, equivalent to
|
| 229 |
<code>match e1 with p -> e2</code>;
|
| 230 |
<code>let p : t = e1 in e2</code> equivalent to
|
| 231 |
<code>let p = (e1 : t) in e2</code> </li>
|
| 232 |
<li>If-then-else: <code>if e1 then e2 else e3</code>, equivalent to
|
| 233 |
<code>match e1 with `true -> e2 | `false -> e3</code></li>
|
| 234 |
<li>Exceptions: <ul>
|
| 235 |
<li>Raise exception: <code>raise e</code></li>
|
| 236 |
<li>Handle exception: <code>try e with p1 -> e1 | ... | pn ->
|
| 237 |
en</code></li>
|
| 238 |
</ul> </li>
|
| 239 |
</ul>
|
| 240 |
</box>
|
| 241 |
|
| 242 |
<box title="More about types and patterns" link="type">
|
| 243 |
<ul>
|
| 244 |
<li>Boolean connectives: <code>&,|,\</code> (<code>|</code> is
|
| 245 |
first-match). </li>
|
| 246 |
<li>Empty and universal types: <code>Empty,Any</code> or
|
| 247 |
<code>_</code>.</li>
|
| 248 |
<li>Recursive types and patterns: <code>t where T1 = t2 and ... and
|
| 249 |
Tn = tn</code>.</li>
|
| 250 |
<li>Capture variable: <code>x</code>. </li>
|
| 251 |
<li>Default values: <code>(x := c)</code>. </li>
|
| 252 |
</ul>
|
| 253 |
</box>
|
| 254 |
|
| 255 |
<box title="References" link="ref">
|
| 256 |
|
| 257 |
<ul>
|
| 258 |
<li>Type: <code>ref %%T%%</code>.</li>
|
| 259 |
<li>Construction: <code>ref %%T%% %%e%%</code>.</li>
|
| 260 |
<li>Dereferencing: <code>!%%e1%%</code>.</li>
|
| 261 |
<li>Assignment: <code>%%e1%% := %%e2%%</code>.</li>
|
| 262 |
</ul>
|
| 263 |
|
| 264 |
</box>
|
| 265 |
|
| 266 |
<box title="Toplevel statements" link="toplevel">
|
| 267 |
<ul>
|
| 268 |
<li>Global expression to evaluate.</li>
|
| 269 |
<li>Global let-binding.</li>
|
| 270 |
<li>Global function declaration.</li>
|
| 271 |
<li>Type declarations: <code>type T = t</code>.</li>
|
| 272 |
<li>Global <local href="namespaces">namespace</local>:
|
| 273 |
<code>namespace p = "..."</code>,
|
| 274 |
<code>namespace "..."</code>.</li>
|
| 275 |
<li>Source inclusion: <code>include %%filename_string%%</code>.</li>
|
| 276 |
<li>Debug directives: <code>debug %%directive argument%%</code> <br/>
|
| 277 |
where <code>%%directive%%</code> is one of the following: <code>accept</code>,
|
| 278 |
<code>subtype</code>, <code>compile</code>, <code>sample</code>, <code>filter</code>.
|
| 279 |
</li>
|
| 280 |
<li>Toplevel directives: <code>#env</code>, <code>#quit</code>,
|
| 281 |
<code>#reinit_ns</code>.</li>
|
| 282 |
</ul>
|
| 283 |
</box>
|
| 284 |
|
| 285 |
</page>
|