memento.xml 11.4 KB
Newer Older
1
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2
<page name="memento">
3

4
<title>Quick reference</title>
5

6
7
8
9
<left>
<p>This page briefly presents the syntax of the CDuce language.</p>
<boxes-toc/>
<p>See also:</p>
10
<local-links href="index,manual,tutorial"/>
11
</left>
12
13
14

<box title="Identifiers" link="id">
<ul>
15
16
<li> Type and Pattern identifiers: words formed by of Unicode letters and 
the underscore "_" character,  starting by an uppercase letter. </li>
17
  
18
<li> value identifiers: words formed by of Unicode letters and the underscore "
19
_" character,  starting by a lowercase letter or underscore.</li>
20
21
22
23
24
25
26
</ul>
</box>

<box title="Scalars" link="scalars">
<ul>
 <li>Large integers: 
   <ul> 
27
28
29
30
     <li>Values: <code>0,1,2,3,...</code> </li>
     <li>Types: intervals <code>-*--10, 20--30, 50--*, ...</code>,
                singletons <code>0,1,2,3,...</code> </li>
     <li>Operators: <code>+,-,/,*,div,mod, int_of</code> </li>
31
32
   </ul>
 </li>
33
34
35
36
37
38
39
40
  <li>Floats: 
   <ul> 
     <li>Values: <i>none built-in</i>. </li>
     <li>Types: only <code>Float</code>. </li>
     <li>Operators: <code>float_of</code> : String -> Float</li>
   </ul>
 </li>
<li>Unicode characters:
41
   <ul>
42
43
44
45
46
47
     <li>Values: quoted characters (<code>'a'</code>, <code>'b'</code>, 
         <code>'c'</code>, ...,<code>'あ'</code>, <code>'い'</code>, ... , 
         <code>'私'</code>, ... , <code>'⊆'</code>, ...),
         codepoint-defined characters (<code>'\x%%h%%;' '\%%d%%;' 
         </code> where <code>%%h%%</code> and
         <code>%%d%%</code> are hexadecimal and decimal integers
48
         respectively), and backslash-escaped characters 
49
50
         (<code>'\t'</code> tab, <code>'\n'</code> newline, 
         <code>'\r'</code> return, <code>'\\'</code> backslash).</li>
51
52
     <li>Types: intervals <code>'a'--'z', '0'--'9'</code>,
                singletons <code>'a','b','c',...</code> </li>
53
     <li>Operators: <code>char_of_int</code> : Int -> Char, <code>int_of_char</code> : Char -> Int</li>
54
55
56
57
   </ul>
 </li>
 <li>Symbolic atoms:
   <ul>
58
59
     <li>Values: <code>`A, `B, `a, `b, `true, `false, ...</code> </li>
     <li>Types: singletons <code>`A, `B, ...</code> </li>
60
61
     <li>Operators: <code>make_atom</code> : (String,String) -> Atom,
          <code>split_atom</code> : Atom -> (String,String) </li>
62
     <li>CDuce also supports <local href="namespaces">XML Namespaces</local></li>
63
64
65
66
67
   </ul>
 </li>
</ul>
</box>

68
<box title="Operators, built-in functions" link="op">
69
70
<ul>
   <li>Infix:
71
     <br/> <code>@</code> : concatenation of sequences
72
73
     <br/> <code>+,*,-,div,mod</code> : Integer,Integer -> Integer
     <br/> <code>=, &lt;&lt;, &lt;=, &gt;&gt;, &gt;= </code> :
74
 <i>t</i>,<i>t</i> -> Bool = <code>`true | `false</code> (any non functional type <i>t</i>)
75
76
     <br/> <code>||, &amp;&amp;</code> : Bool,Bool -> Bool
     <br/> <code>not</code>: Bool -> Bool
77
78
   </li>
   <li>Prefix:
79
        <br/><code>load_xml</code> : Latin1 -> AnyXml,
80
81
82
83
84
        <br/><code>load_html</code> : Latin1 -> [ Any* ],
        <br/><code>load_file</code> : Latin1 -> Latin1,
        <br/><code>load_file_utf8</code> : Latin1 -> String,
        <br/><code>dump_to_file</code> : Latin1 -> String -> [],
        <br/><code>dump_to_file_utf8</code> : Latin1 -> String -> [],
85
86
        <br/><code>print_xml</code> : Any -> Latin1,
        <br/><code>print_xml_utf8</code> : Any -> String,
87
        <br/><code>print</code> : Latin1 -> [],
88
        <br/><code>print_utf8</code> : String -> [],
89
90
        <br/><code>dump_xml</code> : Any -> [],
        <br/><code>dump_xml_utf8</code> : Any -> [],
91
        <br/><code>int_of</code> : String -> Int,
92
        <br/><code>float_of</code> : String -> Float,
93
        <br/><code>string_of</code> : Any -> Latin1,
94
        <br/><code>char_of_int</code> : Int -> Char,
95
96
        <br/><code>make_atom</code> : (String,String) -> Atom,
	<br/><code>split_atom</code> : Atom -> (String,String),
97
        <br/><code>system</code> : Latin1 -> { stdout = Latin1; stderr = Latin1; 
98
             status = (`exited,Int) | (`stopped,Int) | (`signaled,Int)
99
        },
100
101
102
103
        <br/><code>exit</code> : 0--255 -> Empty,
        <br/><code>getenv</code> : Latin1 -> Latin1,
        <br/><code>argv</code> : [] -> [ String* ],
        <br/><code>raise</code> : Any -> Empty
104
105
106
107
108
109
   </li>
 </ul>
</box>

<box title="Pairs" link="pair">
<ul>
110
111
 <li>Expressions: <code>(e1,e2)</code> </li>
 <li>Types and patterns: <code>(t1,t2)</code> </li>
112
 <li>Note: tuples are right-associative pairs; e.g.: 
113
           <code>(1,2,3)=(1,(2,3))</code> </li>
114
115
 <li>When a capture variable appears on both side of a pair pattern,
     the two captured values are paired
116
117
     together (e.g. <code>match (1,2,3) with (x,(_,x)) -> x ==>
(1,3)</code>). </li>
118
119
120
121
122
</ul>
</box>

<box title="Sequences" link="seq">
<ul>
123
124
125
126
127
<li>Expressions: <code>[ 1 2 3 ]</code>, 
     which is syntactic sugar for <code>(1,(2,(3,`nil)))</code> </li>
<li>A sub-sequence can be escaped by !: <code>[ 1 2 ![ 3 4 ] 5
]</code> is then equal to <code>[ 1 2 3 4  5 ]</code> . </li>
<li>Types and patterns : <code>[ R ]</code> where <code>R</code> is
128
129
130
131
    a regular expression built on types and patterns:
   <ul>
     <li>A type or a pattern is a regexp by itself, matching a single
         element of the sequence </li>
132
133
134
     <li>Postfix repetition operators: <code>*,+,?</code>
         and the ungreedy variants (for patterns) <code>*?, +?
         ,??</code></li>
135
     <li>Concatenation of regexps</li>
136
     <li>For patterns, sequence capture variable <code>x::R</code> </li>
137
138
139
   </ul>
 </li>
<li>It is possible to specify a tail, for expressions, types, and patterns;
140
141
    e.g.: <code>[ x::Int*; q ]</code>   </li>
<li>Map: <code>map e with p1 -> e1 | ... | pn -> en</code>. 
142
    Each element of e must be matched. </li>
143
<li>Transform: <code>transform e with p1 -> e1 | ... | pn -> en</code>. 
144
145
    Unmatched elements are discarded; each branch returns a sequence
    and all the resulting sequences are concatenated together. </li>
146
147
148
149
150
<li>Selection: : <code>select %%e%% from %%p1%% in  %%e1%%  ...  %%pn%%
    in %%en%% where %%e'%%</code>. SQL-like selection with the possibility 
    of using CDuce patterns instead of variables. <code>%%e1%%  ...
    %%en%%</code> must be sequences and <code>%%e'%%</code> a boolean
     expression.</li>
151
152
<li>Operators: concatenation <code>e1 @ e2 = [ !e1 !e2 ]</code>,
              flattening <code>flatten e = transform e with x -> x</code>.
153
154
155
156
157
158
</li>
</ul>
</box>

<box title="Record" link="record">
<ul>
159
<li>Records literal <code>{ l1 = e1; ...; ln = en }</code></li>
160
<li>Types: <code>{ l1 = t1; ...; ln = tn }</code> (closed, no more
161
fields allowed), <code>{ l1 = t1; ...; ln = tn; .. }</code> (open,
162
any other field allowed). Optional fields: <code>li =? ti</code>
163
instead of <code>li = ti</code>. Semi-colons are optional.</li>
164
<li>Record concatenation:  <code>e1 + e2</code>
165
 (priority to the fields from the right argument) </li>
166
167
<li>Field removal: <code>e1 \ l</code> (does nothing if the
field <code>l</code> is not present)</li>
168
<li>Field access: <code>e1.l</code></li>
169
<li>Labels are in fact Qualified Names (see <local href="namespaces"/>)</li>
170
171
172
173
174
175
</ul>
</box>

<box title="Strings" link="string">
<ul>
<li>Strings are actually sequences of characters.</li>
176
177
178
<li>Expressions: <code>"abc", [ 'abc' ], [ 'a' 'b' 'c' ]</code>. </li>
<li>Operators: <code>string_of, print, dump_to_file</code></li>
<li><code>PCDATA</code> means <code>Char*</code> inside regular expressions</li>
179
180
181
182
183
</ul>
</box>

<box title="XML elements" link="xml">
<ul>
184
185
186
 <li>Expressions: <code> &lt;(tag) (attr)>content</code> </li>
 <li>If the tag is an atom <code>`X</code>, it can be written
      <code>X</code> (without the <code>(..)</code>).
187
      Similarly, parenthesis and curly braces may be omitted
188
189
      when attr is a record <code>l1=e1;...;ln=en</code>
      (semicolon can also be omitted in this case).
190
      E.g: <code>&lt;a href="abc">[ 'abc' ]</code>.</li>
191
 <li>Types and patterns: same notations.</li>
192
193
194
 <li>XPath like projection: <code>%%e%%/%%t%%</code>. For every
     XML tree in  <code>%%e%%</code> it returns the sequence of children
     of type  <code>%%t%%</code></li>
195
196
<li>Tree transformation: <code>xtransform e with p1 -> e1 | ... | pn -> en</code>. 
    Applies to sequences of XML trees. Unmatched elements are left unchanged and the
197
    transformation is recursively applied to the sequence of children of the unmatched
198
199
    element; as for transform, each branch returns a sequence
    and all the resulting sequences are concatenated together. </li>
200
201
 <li>Operators: <code>load_xml : Latin1 -> AnyXml; print_xml : Any ->
 Latin1; dump_xml : Any -> []</code>
202
203
204
205
206
207
208
209
</li>
</ul>
</box>

<box title="Functions" link="fun">
<ul>
<li>Expressions: 
   <ul>
210
     <li>General form: <code>fun f (t1->s1;...;tn->sn)
211
     p1 -> e1 | ... | pm -> em</code> (<code>f</code> is optional) </li>
212
213
214
215
     <li>Simple function: <code>fun f (p : t) : s = e</code>,
     equivalent to <code>fun f (t -> s) p -> e</code> </li>
     <li>Multiple arguments: <code>fun f (p1 : t1, p2 : t2,...) : s =
e</code>, equivalent to <code>fun f ((p1,p2,...):(t1,t2,...)) : s
216
217
218
219
= e</code> (note the blank spaces around colons to avoid ambiguity
   with namespaces) </li>
     <li>Currified function: <code>fun f (p1 : t1) (p2 : t2) ... : s =
   e</code> (can be combined with the multiple arguments syntax).</li>
220
221
  </ul>
</li>
222
<li>Types: <code>t -> s</code> </li>
223
224
225
226
227
</ul>
</box>

<box title="Pattern matching, exceptions, ..." link="match">
<ul>
228
 <li>Type restriction: <code>(e : t)</code> (forgets any more precise
229
     type for <code>e</code>; note the blank spaces around colons to avoid ambiguity with namespaces) </li>
230
231
232
233
234
235
236
237
 <li>Pattern matching: <code>match e with p1 -> e1 | ... | pn ->
en</code></li>
 <li>Local binding: <code>let p = e1 in e2</code>, equivalent to
 <code>match e1 with p -> e2</code>; 
   <code>let p : t = e1 in e2</code> equivalent to
  <code>let p = (e1 : t) in e2</code> </li>
 <li>If-then-else: <code>if e1 then e2 else e3</code>, equivalent to
 <code>match e1 with `true -> e2 | `false -> e3</code></li>
238
 <li>Exceptions: <ul>
239
240
241
   <li>Raise exception: <code>raise e</code></li>
   <li>Handle exception: <code>try e with p1 -> e1 | ... | pn ->
    en</code></li>
242
243
244
245
246
247
  </ul> </li>
</ul>
</box>

<box title="More about types and patterns" link="type">
<ul>
248
<li>Boolean connectives: <code>&amp;,|,\</code> (<code>|</code> is
249
first-match). </li>
250
251
252
253
254
255
<li>Empty and universal types: <code>Empty,Any</code> or
<code>_</code>.</li>
<li>Recursive types and patterns: <code>t where T1 = t2 and ... and
Tn = tn</code>.</li>
<li>Capture variable: <code>x</code>. </li>
<li>Default values: <code>(x := c)</code>. </li>
256
257
</ul>
</box>
258

259
260
261
262
263
264
265
266
267
268
269
<box title="References" link="ref">

<ul>
<li>Type: <code>ref %%T%%</code>.</li>
<li>Construction: <code>ref %%T%% %%e%%</code>.</li>
<li>Dereferencing: <code>!%%e1%%</code>.</li>
<li>Assignment: <code>%%e1%% := %%e2%%</code>.</li>
</ul>

</box>

270
<box title="Toplevel statements" link="toplevel">
271
<ul>
272
273
274
<li>Global expression to evaluate.</li>
<li>Global let-binding.</li>
<li>Global function declaration.</li>
275
<li>Type declarations: <code>type T = t</code>.</li>
276
277
278
279
280
281
282
<li>Global <local href="namespaces">namespace</local>: 
 <code>namespace p = "..."</code>,
 <code>namespace "..."</code>.</li>
<li>Source inclusion: <code>include %%filename_string%%</code>.</li>
<li>Debug directives: <code>debug %%directive argument%%</code> <br/>
    where <code>%%directive%%</code> is one of the following: <code>accept</code>, 
<code>subtype</code>, <code>compile</code>, <code>sample</code>, <code>filter</code>.
283
    </li>
284
285
<li>Toplevel directives: <code>#env</code>, <code>#quit</code>,
<code>#reinit_ns</code>.</li>
286
287
288
</ul>
</box>

289
</page>