Sophie

Sophie

distrib > * > 2010.0 > * > by-pkgid > 50e6cd590109ca4ca0931fda4b384942 > files > 25

cduce-0.5.3-2mdv2010.0.x86_64.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>CDuce: Types and patterns</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/><link href="cduce.css" type="text/css" rel="stylesheet"/></head><body style="margin: 0; padding : 0;"><table width="100&#37;" border="0" cellspacing="10" cellpadding="0"><tr><td valign="top" style="width:20&#37;;" align="left"><div class="leftbar" id="leftbar"><div class="smallbox"><ul><li><a href="#gen">Types and patterns</a></li><li><a href="#capture">Capture variables and default patterns</a></li><li><a href="#bool">Boolean connectives</a></li><li><a href="#recurs">Recursive types and patterns</a></li><li><a href="#basic">Scalar types</a></li><li><a href="#pairs">Pairs</a></li><li><a href="#seq">Sequences</a></li><li><a href="#string">Strings</a></li><li><a href="#record">Records</a></li><li><a href="#xml">XML elements</a></li><li><a href="#fun">Functions</a></li><li><a href="#ref">References</a></li><li><a href="#abstr">OCaml abstract types</a></li><li><a href="#syntax">Complete syntax</a></li></ul></div></div></td><td><h1>Types and patterns</h1><div class="mainpanel"><div class="smallbox"><p><a href="index.html">CDuce: documentation</a>: <a href="manual.html">User's manual</a>: Types and patterns</p><p><a href="manual_interpreter.html"><img class="icon" width="16" alt="Previous page:" src="img/left.gif" height="16"/> Compiler/interpreter/toplevel</a> <a href="manual_expressions.html"><img class="icon" width="16" alt="Next page:" src="img/right.gif" height="16"/> Expressions</a></p></div><div><h2><a name="gen">Types and patterns</a></h2><p>
In CDuce, a type denotes a set of values, and a pattern
extracts sub-values from a value. Syntactically, types and patterns
are very close. Indeed, any type can be seen as a pattern
(which accepts any value and extracts nothing), and a pattern
without any capture variable is nothing but a type.
</p><p>
Moreover, values
also share a common syntax with types and patterns. This is motivated
by the fact that basic and constructed values (that is, any values without
functional values inside) are themselves singleton types.
For instance <b><tt>(1,2)</tt></b> is both a value, a type and a pattern.
As a type, it can be interpreted as a singleton type, 
or as a pair type made of two singleton types.
As a pattern, it can be interpreted as a type constraint,
or as a pair pattern of two type constraints.
</p><p>
In this page, we present all the types and patterns that CDuce recognizes.
It is also the occasion to present the CDuce values themselves, the
corresponding expression constructions, and fundamental operations on them.
</p></div><div><h2><a name="capture">Capture variables and default patterns</a></h2><p>
A value identifier inside a pattern behaves as a capture variable:
it accepts and bind any value.
</p><p>
Another form of capture variable is the default value pattern
<b><tt>( <i>x</i> := <i>c</i> )</tt></b> where <b><tt><i>x</i></tt></b>
is a capture variable (that is, an identifier),
and <b><tt><i>c</i></tt></b> is a scalar constant.
The semantics of this pattern is to bind the capture variable
to the constant, disregarding the matched value (and accepting
any value).
</p><p>
Such a pattern is useful in conjunction with the first match policy
(see below) to define &quot;default cases&quot;. For instance, the pattern
<b><tt>((x &amp; Int) | (x := 0), (y &amp; Int) | (y := 0))</tt></b>
accepts any pair and bind <b><tt>x</tt></b> to the left component
if it is an integer (and <b><tt>0</tt></b> otherwise), and similarly
for <b><tt>y</tt></b> with the right component of the pair.
</p></div><div><h2><a name="bool">Boolean connectives</a></h2><p>
CDuce recognize the full set of boolean connectives, whose
interpretation is purely set-theoretic.
</p><ul><li><b><tt>Empty</tt></b> denotes the empty type (no value).</li><li><b><tt>Any</tt></b> and <b><tt>_</tt></b> denote the universal type (all the values); the preferred notation is <b><tt>Any</tt></b> for types
and <b><tt>_</tt></b> for patterns, but they are strictly equivalent.
 </li><li><b><tt>&amp;</tt></b> is the conjunction boolean connective.
The type <b><tt><i>t1</i> &amp; <i>t2</i></tt></b> has all the values
that belongs to <b><tt><i>t1</i></tt></b> and to <b><tt><i>t2</i></tt></b>.
Similarly, the pattern <b><tt><i>p1</i> &amp; <i>p2</i></tt></b> accepts
all the values accepted by both sub-patterns; a capture variable
cannot appear on both side of this pattern. 
</li><li><b><tt>|</tt></b> is the disjunction boolean connective.
The type <b><tt><i>t1</i> | <i>t2</i></tt></b> has all the values
that belongs either to <b><tt><i>t1</i></tt></b> or to <b><tt><i>t2</i></tt></b>.
Similarly, the pattern <b><tt><i>p1</i> | <i>p2</i></tt></b> accepts
all the values accepted by any of the two sub-patterns;
if both match, the first match policy applies, and <b><tt><i>p1</i></tt></b>
dictates how to capture sub-values. The two sub-patterns
must have the same set of capture variables.</li><li><b><tt>\</tt></b> is the difference boolean connective.
The left hand-side can be a type or a pattern, but the right-hand side
is necessarily a type (no capture variable).</li></ul></div><div><h2><a name="recurs">Recursive types and patterns</a></h2><p>
A set of mutually recursive types can be defined
by toplevel type declarations, as in:
</p><div class="code"><pre>
type T1 = &lt;a&gt;[ T2* ]
type T2 = &lt;b&gt;[ T1 T1 ]
</pre></div><p>
It is also possible to use the syntax
<b><tt><i>T</i> where <i>T1</i> = <i>t1</i> and ... and <i>Tn</i> = <i>tn</i></tt></b>
where <b><tt><i>T</i></tt></b> and the <b><tt><i>Ti</i></tt></b> are type identifiers
and the <b><tt><i>ti</i></tt></b> are type expressions. The same notation
works for recursive patterns (for which there is no toplevel declarations).
</p><p>
There is an important restriction concerning recursive types:
any cycle must cross a <em>type constructor</em> (pairs, records, XML
elements, arrows). Boolean connectives do <em>not</em> count as type
constructors!  The code sample above is a correct definition.
The one below is invalid, because there is an unguarded cycle
between <b><tt>T</tt></b> and <b><tt>S</tt></b>.
</p><div class="code"><pre>
type T = S | (S,S)  (* INVALID! *)
type S = T          (* INVALID! *)
</pre></div></div><div><h2><a name="basic">Scalar types</a></h2><p>
CDuce has three kind of atomic (scalar) values: 
integers, characters, and atoms. To each kind corresponds a family of types.
</p><ul><li><b>Integers</b>. 
 <br/>CDuce integers are arbitrarily large. An integer
  literal is a sequence of decimal digits, plus an optional leading unary
  minus (<b><tt>-</tt></b>) character.
   <ul><li><b><tt>Int</tt></b>: all the integers.</li><li><b><tt><i>i</i>--<i>j</i></tt></b> (where <b><tt><i>i</i></tt></b> and
     <b><tt><i>j</i></tt></b> are integer literals, or <b><tt>*</tt></b>
     for infinity): integer interval. E.g.: <b><tt>100--*</tt></b>,
     <b><tt>*--0</tt></b><a name="bnote1"/><a href="#note1">[1]</a>
 (note that <b><tt>*</tt></b> stands both
     for plus and minus infinity). </li><li><b><tt><i>i</i></tt></b> (where <b><tt><i>i</i></tt></b> is an integer
     literal): integer singleton type.</li></ul></li><li><b>Floats</b>.
<br/>CDuce provider minimal features for floats. The only way to
  construct a value of type <b><tt>Float</tt></b> is by the function
  <b><tt>float_of : String -&gt; Float</tt></b></li><li><b>Characters</b>. 
 <br/>CDuce manipulates Unicode characters. A character
  literal is enclosed in single quotes, e.g. <b><tt>'a', 'b', 'c'</tt></b>.
  The single quote and the backslash character must be escaped
  by a backslash: <b><tt>'\''</tt></b>, <b><tt>'\\'</tt></b>. The double
  quote can also be escaped, but this is not mandatory.
  The usual <b><tt>'\n', '\t', '\r'</tt></b> are recognized.
  Arbitrary Unicode codepoints can be written in decimal
  <b><tt>'\<i>i</i>;'</tt></b> (<b><tt><i>i</i></tt></b> is an decimal integer; note that the code is ended by a semicolon) or
  in hexadecimal <b><tt>'\x<i>i</i>;'</tt></b>. Any other occurrence of 
  a backslash character is prohibited.

   <ul><li><b><tt>Char</tt></b>: all the Unicode character set.</li><li><b><tt><i>c</i>--<i>d</i></tt></b> (where <b><tt><i>d</i></tt></b> and
     <b><tt><i>d</i></tt></b> are character literals): 
	interval of Unicode character set. E.g.: <b><tt>'a'--'z'</tt></b>. </li><li><b><tt><i>c</i></tt></b> (where <b><tt><i>c</i></tt></b> is an integer
     literal): character singleton type.</li><li><b><tt>Byte</tt></b>: all the Latin1 character set 
(equivalent to <b><tt>'\0;'--'\255;'</tt></b>).</li></ul></li><li><b>Atoms</b>. 
 <br/>Atoms are symbolic elements. They are used in particular
  to denote XML tag names, and also to simulate ML sum type
  constructors and exceptions names.
  An atomic is written <b><tt>`<i>xxx</i></tt></b> where
  <b><tt><i>xxx</i></tt></b> follows the rules for CDuce identifiers.
  E.g.: <b><tt>`yes, `No, `my-name</tt></b>. The atom <b><tt>`nil</tt></b>
  is used to denote empty sequences.
   <ul><li><b><tt>Atom</tt></b>: all the atoms.</li><li><b><tt><i>a</i></tt></b> (where <b><tt><i>a</i></tt></b> is an atom
     literal): atom singleton type.</li><li><b><tt>Bool</tt></b>: the two atoms <b><tt>`true</tt></b> and
    <b><tt>`false</tt></b>.</li><li>See also: <a href="namespaces.html">XML Namespaces</a>.</li></ul></li></ul></div><div><h2><a name="pairs">Pairs</a></h2><p>
Pairs is a fundamental notion in CDuce, as they constitute a building
block for sequence. Even if syntactic sugar somewhat hides
pairs when you use sequences, it is good to know the existence of pairs.
</p><p>
A pair expression is written <b><tt>(<i>e1</i>,<i>e2</i>)</tt></b>
where <b><tt><i>e1</i></tt></b> and <b><tt><i>e2</i></tt></b> are expressions.
</p><p>
Similarly, pair types and patterns are written
<b><tt>(<i>t1</i>,<i>t2</i>)</tt></b> where <b><tt><i>t1</i></tt></b> and 
<b><tt><i>t2</i></tt></b> are types or patterns. E.g.: <b><tt>(Int,Char)</tt></b>.
</p><p>
When a capture variable <b><tt><i>x</i></tt></b> appears on both
side of a pair pattern <b><tt><i>p</i> = (<i>p1</i>,<i>p2</i>)</tt></b>, the semantics
is the following one: when a value match <b><tt><i>p</i></tt></b>,
if  <b><tt><i>x</i></tt></b> is bound to  <b><tt><i>v1</i></tt></b> by
<b><tt><i>p1</i></tt></b> and to  <b><tt><i>v2</i></tt></b> by
<b><tt><i>p2</i></tt></b>, 
then  <b><tt><i>x</i></tt></b> is bound to the pair <b><tt><i>(v1,v2)</i></tt></b> by
<b><tt><i>p</i></tt></b>.
</p><p>
Tuples are syntactic sugar for pairs. For instance,
<b><tt>(1,2,3,4)</tt></b> denotes <b><tt>(1,(2,(3,4)))</tt></b>.
</p></div><div><h2><a name="seq">Sequences</a></h2><h3>Values and expressions</h3><p>
Sequences are fundamental in CDuce. They represents
the content of XML elements, and also character strings.
Actually, they are only syntactic sugar over pairs.
</p><p>
Sequences expressions are written inside square brackets; element
are simply separated by whitespaces:
<b><tt>[ <i>e1</i> <i>e2</i> <i>...</i> <i>en</i> ]</tt></b>.
Such an expression is syntactic sugar for:
<b><tt>(<i>e1</i>,(<i>e2</i>, <i>...</i> (<i>en</i>,`nil) <i>...</i>))</tt></b>.
E.g.: <b><tt>[ 1 2 3 4 ]</tt></b>.
</p><p>
The binary operator <b><tt>@</tt></b> denotes sequence concatenation.
E.g.: <b><tt>[ 1 2 3 ] @ [ 4 5 6 ]</tt></b> evaluates to
<b><tt>[ 1 2 3 4 5 6 ]</tt></b>.
</p><p>
It is possible to specify a terminator different from <b><tt>`nil</tt></b>;
for instance 
<b><tt>[ 1 2 3 4 ; <i>q</i> ]</tt></b> denotes <b><tt>(1,(2,(3,(4,<i>q</i>))))</tt></b>, 
and is equivalent to 
<b><tt>[ 1 2 3 4 ] @ <i>q</i></tt></b>.
</p><p>
Inside the square brackets of a sequence expression, it is possible
to have elements of the form <b><tt>! <i>e</i></tt></b> (which is not
an expression by itself), where <b><tt><i>e</i></tt></b> is an expression
which should evaluate to a sequence. The semantics is
to &quot;open&quot; <b><tt><i>e</i></tt></b>. For instance:
<b><tt>[ 1 2 ![ 3 4 ] 5 ]</tt></b>
evaluates to
<b><tt>[ 1 2 3 4 5 ]</tt></b>.
Consequently, the concatenation of two sequences <b><tt><i>e1</i> @ <i>e2</i></tt></b>
can also be written <b><tt>[ !<i>e1</i> !<i>e2</i> ]</tt></b>
or  <b><tt>[ !<i>e1</i> ; <i>e2</i> ]</tt></b>.
</p><h3>Types and patterns</h3><p>
In CDuce, a sequence can be heterogeneous: the element can all have
different types. Types and patterns for sequences are specified
by regular expressions over types or patterns. The syntax is
<b><tt>[ <i>R</i> ]</tt></b> where <b><tt><i>R</i></tt></b> is a regular expression, which 
can be:
</p><ul><li>A type or a pattern, which correspond to a single element in the 
sequence (in particular, <b><tt>[ _ ]</tt></b> represents
sequences of length 1, <em>not</em> arbitrary sequences).</li><li>A juxtaposition of regular expressions <b><tt><i>R1</i> <i>R2</i></tt></b>
which represents concatenation.
 </li><li>A union of regular expressions <b><tt><i>R1</i>|<i>R2</i></tt></b>.</li><li>A postfix repetition operator; the greedy operators are
<b><tt><i>R</i>?</tt></b>,
<b><tt><i>R</i>+</tt></b>,
<b><tt><i>R</i>*</tt></b>, and the ungreedy operators are:
<b><tt><i>R</i>??</tt></b>,
<b><tt><i>R</i>+?</tt></b>,
<b><tt><i>R</i>*?</tt></b>. For types, there is no distinction in semantics between
greedy and ungreedy. </li><li>A sequence capture variable <b><tt><i>x</i>::<i>R</i></tt></b>
(only for patterns, of course).
The semantics is to capture in <b><tt><i>x</i></tt></b>  the subsequence
matched by <b><tt><i>R</i></tt></b>. The same sequence capture variable
can appear several times inside a regular expression, including
under repetition operators; in that case, all the corresponding
subsequences are concatenated together. Two instances of the
same sequence capture variable cannot be nested, as in
<b><tt>[x :: (1 x :: Int)]</tt></b>.
<br/>
Note the difference between <b><tt>[ x::Int ]</tt></b> and
<b><tt>[ (x &amp; Int) ]</tt></b>. Both accept sequences made of a single
integer, but the first one binds <b><tt>x</tt></b> to a sequence
(of a single integer), whereas the second one binds it to
the integer itself.</li><li>
Grouping <b><tt>(<i>R</i>)</tt></b>. E.g.: <b><tt>[ x::(Int Int) y ]</tt></b>.
</li><li>
Tail predicate <b><tt>/<i>p</i></tt></b>. The type/pattern <b><tt><i>p</i></tt></b> 
applies to the current tail of the sequence (the subsequence
starting at the current position). E.g.: 
<b><tt>[ (Int /(x:=1) | /(x:=2)) _* ]</tt></b> will bind
<b><tt>x</tt></b> to <b><tt>1</tt></b> if the sequence starts
with an integer and <b><tt>2</tt></b> otherwise.
</li><li>
Repetition <b><tt><i>R</i> ** <i>n</i></tt></b> where <b><tt><i>n</i></tt></b>
is a positive integer constant, which is just a shorthand
for the concatenation of <b><tt><i>n</i></tt></b> copies of <b><tt><i>R</i></tt></b>.
</li></ul><p>
Sequence types and patterns also accepts the <b><tt>[ <i>...</i>; <i>...</i> ]</tt></b>
notation. This is a convenient way to discard the tail of a sequence
in a pattern, e.g.: <b><tt>[ x::Int* ; _ ]</tt></b>, which
is equivalent to <b><tt>[ x::Int* _* ]</tt></b>.
</p><p>
It is possible to use the <b><tt>@</tt></b>
operator (sequence concatenation) on types, including in recursive 
definitions. E.g.:
</p><div class="code"><pre>
type t = [ &lt;a&gt;(t @ t) ? ]    (* [s?] where s=&lt;a&gt;[ s? s? ] *)

type x = [ Int* ]
type y = x @ [ Char* ]       (* [ Int* Char* ] *)

type t = ([Int] @ t) | []    (* [ Int* ] *)
</pre></div><p>
however when used in recursive definitions <b><tt>@</tt></b> but must be right linear so for instance the following definition are not allowed:
</p><div class="code"><pre>
type t = (t @ [Int]) | []      (* ERROR: Ill-formed concatenation loop *)
type t = t @ t               (* ERROR: Ill-formed concatenation loop *)
</pre></div></div><div><h2><a name="string">Strings</a></h2><p>
In CDuce, character strings are nothing but sequences of characters.
The type <b><tt>String</tt></b> is pre-defined as <b><tt>[ Char* ]</tt></b>.
This allows to use the full power of regular expression
pattern matching with strings.
</p><p>
Inside a regular expression type or pattern, it is possible
to use <b><tt>PCDATA</tt></b> instead of <b><tt>Char*</tt></b>
(note that both are not types on their own, they only make sense
inside square brackets, contrary to <b><tt>String</tt></b>).
</p><p>
The type <b><tt>Latin1</tt></b> is the subtype of <b><tt>String</tt></b>
defined as <b><tt>[ Byte* ]</tt></b>; it denotes strings that can
be represented in the ISO-8859-1 encoding, that is, strings made only
of characters from the Latin1 character set.
</p><p>
Several consecutive characters literal in a sequence can be
merged together between two single quotes:
<b><tt>[ 'abc' ]</tt></b> instead of <b><tt>[ 'a' 'b' 'c' ]</tt></b>.
Also it is possible to avoid square brackets by using
double quotes: <b><tt>&quot;abc&quot;</tt></b>. The same escaping rules applies
inside double quotes, except that single quotes may be escaped (but
must not), and double quotes must be.
</p></div><div><h2><a name="record">Records</a></h2><p>
Records are set of finite (name,value) bindings. They are used
in particular to represent XML attribute sets. Names are
actually Qualified Names (see <a href="namespaces.html">XML Namespaces</a>).
</p><p>
The syntax of a record expression is
<b><tt>{ <i>l1</i>=<i>e1</i>; <i>...</i>;  <i>ln</i>=<i>en</i> }</tt></b>
where the <b><tt><i>li</i></tt></b> are label names (same lexical
conventions as for identifiers), and the <b><tt><i>vi</i></tt></b>
are expressions. When an expression <b><tt><i>ei</i></tt></b>
is simply a variable whose name match the field label
<b><tt><i>li</i></tt></b>, it is possible to omit it.
E.g.: <b><tt>{ x; y = 10; z }</tt></b>
is equivalent to <b><tt>{ x = x; y = 10; z = z }</tt></b>.
The semi-colons between fields are optional.
</p><p>
They are two kinds of record types. Open record types
are written <b><tt>{ <i>l1</i>=<i>t1</i>; <i>...</i>;  <i>ln</i>=<i>tn</i>; ..
}</tt></b>, and closed record types are written
<b><tt>{ <i>l1</i> = <i>t1</i>; <i>...</i>;  <i>ln</i> = <i>tn</i> }</tt></b>.
Both denote all the record values where
the labels <b><tt><i>li</i></tt></b> are present and the associated values
are in the corresponding type. The distinction is that that open
type allow extra fields, whereas the closed type gives a strict
enumeration of the possible fields. The semi-colon between fields is optional.
</p><p>
Additionally, both for open and close record types,
it is possible to specify optional fields by using <b><tt>=?</tt></b>
instead of <b><tt>=</tt></b> between a label and a type.
For instance, <b><tt>{ x =? Int; y = Bool }</tt></b>
represents records with a <b><tt>y</tt></b> field of type
<b><tt>Bool</tt></b>, and an optional field <b><tt>y</tt></b> (that when it is
present,  has type <b><tt>Int</tt></b>), and no other field.
</p><p>
The syntax is the same for patterns. Note that capture variables
cannot appear in an optional field. A common idiom is to bind
default values to replace missing optinal fields: <b><tt>
({ x = a } | (a := 1)) &amp; { y = b }</tt></b>. A special syntax
makes this idiom more convenient:
<b><tt>{ x = a else (a:=1); y = b }</tt></b>.
</p><p>
As for record expressions, when the pattern
is simply a capture variable whose name match the field label,
it is possible to omit it. E.g.: <b><tt>{ x; y = b; z }</tt></b>
is equivalent to <b><tt>{ x = x; y = b; z = z }</tt></b>.
</p><p>
The <b><tt>+</tt></b> operator (record concatenation, with priority given
to the right argument in case of overlapping) is available on record
types and patterns. This operator can be used to make a close
record type/pattern open, or to add fields:
</p><div class="code"><pre>
type t = { a=Int b=Char }
type s = t + {..}               (* { a=Int b=Char .. }
type u = s + { c=Float }        (* { a=Int b=Char c=Float .. } *)
type v = t + { c=Float }        (* { a=Int b=Char c=Float } *)
</pre></div></div><div><h2><a name="xml">XML elements</a></h2><p>
In CDuce, the general of an XML element is
<b><tt>&lt;(<i>tag</i>) (<i>attr</i>)&gt;<i>content</i></tt></b> where
<b><tt><i>tag</i></tt></b>, 
<b><tt><i>attr</i></tt></b> and
<b><tt><i>content</i></tt></b> are three expressions.
Usually, <b><tt><i>tag</i></tt></b> is a tag literal <b><tt>`<i>xxx</i></tt></b>, and
in this case, instead of writing <b><tt>&lt;(`<i>tag</i>)&gt;</tt></b>,
you can write: <b><tt>&lt;<i>tag</i>&gt;</tt></b>.
Similarly, when <b><tt><i>attr</i></tt></b> is a record literal, you can
omit the surrounding <b><tt>({...})</tt></b>, and also the semicolon
between attributes,
E.g: <b><tt>&lt;a href=&quot;http://...&quot; dir=&quot;ltr&quot;&gt;[]</tt></b>.
</p><p>
The syntax for XML elements types and patterns follows closely
the syntax for expressions:
<b><tt>&lt;(<i>tag</i>) (<i>attr</i>)&gt;<i>content</i></tt></b>
where
<b><tt><i>tag</i></tt></b>, 
<b><tt><i>attr</i></tt></b> and
<b><tt><i>content</i></tt></b> are three types or patterns.
As for expressions, it is possible to simplify the notations
for tags and attributes. For instance,
<b><tt>&lt;(`a) ({ href=String })&gt;[]</tt></b>
can be written:
<b><tt>&lt;a href=String&gt;[]</tt></b>.
</p><p>
The following sample shows several way to write XML types.
</p><div class="code"><pre>
type A = &lt;a x=String y=String ..&gt;[ A* ]
type B = &lt;(`x | `y) ..&gt;[ ]
type C = &lt;c x = String; y = String&gt;[ ]
type U = { x = String y =? String ..}
type V = [ W* ]
type W = &lt;v (U)&gt;V
</pre></div></div><div><h2><a name="fun">Functions</a></h2><p>
CDuce is an higher-order functional languages: functions are
first-class citizen values, and can be passed as argument or returned
as result, stored in data structure, etc...
</p><p>
A functional type has the form <b><tt><i>t</i> -&gt; <i>s</i></tt></b>
where <b><tt><i>t</i></tt></b> and <b><tt><i>s</i></tt></b> are types.
Intuitively, this type corresponds to functions that accept
(at least) any argument of type <b><tt><i>t</i></tt></b>, and for
such an argument, returns a value of type <b><tt><i>s</i></tt></b>.
For instance, the type <b><tt>((Int,Int) -&gt; Int) &amp; ((Char,Char) -&gt; Char)</tt></b>
denotes functions that maps any pair of integer to an integer,
and any pair of characters to a character.
</p><p>
The explanation above gives the intuition behind the interpretation
of functional types. It is sufficient to understand which
subtyping relations and equivalences hold between (boolean
combination) of functional types. For instance,
<b><tt>(Int -&gt; Int) &amp; (Char -&gt; Char)</tt></b> is a subtype
of <b><tt>(Int|Char) -&gt; (Int|Char)</tt></b> because
with the intuition above, a function of the first type,
when given a value of type <b><tt>Int|Char</tt></b> returns
a value of type <b><tt>Int</tt></b> or of type <b><tt>Char</tt></b>
(depending on the argument).
</p><p>
Formally, the type <b><tt><i>t</i> -&gt; <i>s</i></tt></b> denotes
CDuce abstractions 
<b><tt>fun (<i>t1</i> -&gt; <i>s1</i>; <i>...</i>; <i>tn</i> -&gt; <i>sn</i>)...</tt></b>
such that <b><tt>(<i>t1</i> -&gt; <i>s1</i>) &amp; <i>...</i> &amp; (<i>tn</i> -&gt;
<i>sn</i>)</tt></b> is a subtype of <b><tt><i>t</i> -&gt; <i>s</i></tt></b>.
</p><p>
Functional types have no counterpart in patterns.
</p></div><div><h2><a name="ref">References</a></h2><p>
References are mutable memory cells. CDuce has no built-in 
reference type. Instead, references are implemented
in an object-oriented way. The type <b><tt>ref <i>T</i></tt></b>
denotes references of values of type  <b><tt><i>T</i></tt></b>. It
is only syntactic sugar for the type
<b><tt>{ get = [] -&gt; T ; set = T -&gt; [] }</tt></b>.
</p></div><div><h2><a name="abstr">OCaml abstract types</a></h2><p>
The notation <b><tt>!t</tt></b> is used by the
<a href="manual_interfacewithocaml.html">CDuce/OCaml interface</a>
to denote the OCaml abstract type <b><tt>t</tt></b>.
</p></div><div><h2><a name="syntax">Complete syntax</a></h2><p>
Below we give the complete syntax of type and pattern, the former
being patterns without capture variables
</p><p><b style="color:#FF0080">TO BE DONE</b></p></div><div class="meta"><p><a href="sitemap.html">Site map</a></p></div><div class="meta"><p><a name="note1"/><a href="#bnote1">[1]</a> 
       You should be careful when putting parenthesis around
       a type of the form <b><tt>*--<i>i</i></tt></b>. Indeed,
       <b><tt>(*--<i>i</i>)</tt></b> would be parsed as a comment.
       You have to put a whitespace after the left parenthesis.
     </p></div><div class="smallbox"><p><a href="index.html">CDuce: documentation</a>: <a href="manual.html">User's manual</a>: Types and patterns</p><p><a href="manual_interpreter.html"><img class="icon" width="16" alt="Previous page:" src="img/left.gif" height="16"/> Compiler/interpreter/toplevel</a> <a href="manual_expressions.html"><img class="icon" width="16" alt="Next page:" src="img/right.gif" height="16"/> Expressions</a></p></div></div></td></tr></table></body></html>