Expressions

The page presents the different kind of values: scalar constant (integers, characters, atoms), structured values (pairs, records, sequences, XML elements), and functional values (abstractions). Value themselves are expressions, and the value constructors for structured values operate also on expressions.

This page presents the other kinds of expressions in the language.

A fundamental operation in CDuce is pattern matching:

%%e1%% %%...%% | %%pn%% -> %%en%% ]]>

The first vertical bar | can be omitted. The semantics is to try to match the result of the evaluation of %%e%% successively with each pattern %%pi%%. The first matching pattern triggers the corresponding expression in the right hand side, which can use the variables bound by the pattern. Note that a first match policy, as for the disjunction patterns.

The static type system ensures that the pattern matching is exhaustive: the type computed for %%e%% must be a subtype of the union of the types accepted by all the patterns.

Local definition is a lighter notation for a pattern matching with a single branch:

is equivalent to:

%%e2%% ]]>

Note that the pattern %%p%% need not be a simple capture variable.

The general form for a function expression is:

%%s1%%; %%...%%; %%tn%% -> %%sn%%) | %%p1%% -> %%e1%% %%...%% | %%pn%% -> %%en%% ]]>

The first line is the interface of the function, and the remaining is the body, which is a form of pattern matching (the first vertical bar | can thus be omitted).

The identifier %%f%% is optional; it is useful to define a recursive function (the body of the function can use this identifier to refer to the function itself).

The interface of the function specifies some constraints on the behavior of the function. Namely, when the function receive an argument of type, say %%ti%%, the result (if any) must be of type %%si%%. The type system ensures this property by type-checking the body once for each constraint.

The function operate by pattern-matching the argument (which is a value) exactly as for standard pattern matching. Actually, it is always possible to add a line x -> match x with between the interface and the body without changing the semantics.

When there is a single constraint in the interface, there is an alternative notation, which is lighter for several arguments (that is, when the argument is a tuple):

(note the blank spaces around the colons which are mandatory when the pattern is a variable The reason why the blank spaces are mandatory with variables is that the XML recommendation allows colons to occur in variables ("names" in XML terminology: see section on namespaces), so the blanks disambiguate the variables. Actually only the blank on the right handside is necessary: CDuce accepts fun %%f%% (%%x1%% :%%t1%%, %%...%%, %%xn :tn%%):%%s%% = %%e%%, as well (see also this paragraph on let declarations in the tutorial).) which is strictly equivalent to:

%%s%%) (%%p1%%,%%...%%,%%pn%%) -> %%e%% ]]>

It is also possible to define currified functions with this syntax:

which is strictly equivalent to:

(%%s1%%,%%...%%,%%sm%%) -> %%...%% -> %%s%%) (%%p1%%,%%...%%,%%pn%%) -> fun ((%%s1%%,%%...%%,%%sm%%) -> %%...%% -> %%s%%) (%%q1%%,%%...%%,%%qm%%) -> %%...%% %%e%% ]]>

The standard notation for local binding a function is:

Here, %%f%% is the "external" name for the function, and %%g%% is the "internal" name (used when the function needs to call itself recursively, for instance). When the two names coincide (or when you don't need an internal name), there are lighter notations:

The only way to use a function is ultimately to apply it to an argument. The notation is simply a juxtaposition of the function and its argument. E.g.:

evaluates to 11. The static type system ensures that applications cannot fail.

Note that even if there is no functional "pattern" in CDuce, it is possible to use in a pattern a type constraint with a functional type, as in:

Int) | f & (Int -> Int) -> f 5 | x & Int -> x | _ -> 0 ]]>

The concatenation operator is written @. There is also a flatten operator which takes a sequence of sequences and returns their concatenation.

There are two built-in constructions to iterate over a sequence. Both have a very precise typing which takes into account the position of elements in the input sequence as given by its static type. The map construction is:

%%e1%% %%...%% | %%pn%% -> %%en%% ]]>

Note the syntactic similarity with pattern matching. Actually, map is a pattern matching form, where the branches are applied in turn to each element of the input sequence (the result of the evaluation of %%e%%). The semantics is to return a sequence of the same length, where each element in the input sequence is replaced by the result of the matching branch.

Contrary to map, the transform construction can return a sequence of a different length. This is achieved by letting each branch return a sequence instead of a single element. The syntax is:

%%e1%% %%...%% | %%pn%% -> %%en%% ]]>

There is always an implicit default branch _ -> [] at then end of transform, which means that unmatched elements of the input sequence are simply discarded.

Note that map can be simulated by transform by replacing each expression %%ei%% with [ %%ei%% ].

Conversely, transform can be simulated by map by using the flatten operator. Indeed, we can rewrite transform %%e%% with %%...%% as flatten (map %%e%% with %%...%% | _ -> []).

The following construction raises an exception:

The result of the evaluation of %%e%% is the argument of the exception.

It is possible to catch an exception with an exception handler:

%%e1%% %%...%% | %%pn%% -> %%en%% ]]>

Whenever the evaluation of %%e%% raises an exception, the handler tries to match the argument of the exception with the patterns (following a first-match policy). If no pattern matches, the exception is propagated.

Note that contrary to ML, there is no exception name: the only information carried by the exception is its argument. Consequently, it is the responsibility of the programmer to put enough information in the argument to recognize the correct exceptions. Note also that a branch (`A,x) -> %%e%% in an exception handler gives no static information about the capture variable x (its type is Any). Note: it is possible that the support for exceptions will change in the future to match ML-like named exceptions.

There are three kinds of operators on records:

Binary arithmetic operators on integers: +,-,*,div,mod. Note that / is used for projection and not for division.

Binary comparison operators (returns booleans): >,>=]]>. Note that < is used for XML elements and is this not available for comparison.

The semantics of the comparison is not specified when the values contain functions. Otherwise, the comparison gives a total ordering on CDuce values. The result type for all the comparison operators is Bool, except for equality when the arguments are known statically to be different (their types are disjoint); in this case, the result type is the singleton `false.

The if-then-else construction is standard:

and is equivalent to:

%%e2%% | `false -> %%e3%% ]]>

Note that the else-clause is mandatory.

The infix operators || and && denote respectively the logical or and the logical and. The prefix operator not denotes the logical negation.

It is possible to "forget" that an expression has a precise type, and give it a super-type:

The type of this expression if %%t%%, and %%e%% must provably have this type (it can have a subtype). This "upward coercion" can be combined with the local let binding:

which is equivalent to:

Note that the upward coercion allows earlier detection of type errors, better localization in the program, and more informative messages.

The load_xml operator parse an XML document on the local file system; the argument gives the filename:

Two operators can be used to produce a string from an XML document:

They fail if the argument is not an XML document (this isn't checked statically). The first operator print_xml prepares the document to be dumped to a ISO-8859-1 encoded XML file: Unicode characters outside Latin1 are escaped accordingly, and the operator fails if the document contains tag or attribute names which cannot be represented in ISO-8859-1. The second operator print_xml_utf8 always succeed but produces a string suitable for being dumped in an UTF-8 encoded file. See the variants of the dump_to_file operator in the section on Input/output.

In both cases, the resulting string does not contain the XML prefix "<?xml ...>".

The projection takes a sequence of XML elements and returns the concatenation of all their children with a given type. The syntax is:

which is equivalent to:

[ (x::%%t%% | _)* ] -> x ]]>

For instance, the expression [ "A" "B" ] [ "C" "D"] ] / _ ]]> evaluates to "A" "D" ] ]]>.

Another XML-specific construction is xtransform which is a generalization of transform to XML trees:

%%e1%% %%...%% | %%pn%% -> %%en%% ]]>

Here, when an XML elements in the input sequence is not matched by a pattern, the element is copied except that the transformation is applied recursively to its content. Elements in the input sequence which are not matched and are not XML elements are copied verbatim.

The operator string_of converts any value to a string, using the same pretty-printing function as the CDuce interpreter itself. The result has type Latin1.

The operator atom_of converts a string to an atom. E.g.: atom_of "x" evaluates to `x

The operator int_of converts a string to an integer. It fails if the string is not a decimal representation of an integer. There is a type-checking warning when the argument cannot be proved to be of type [ '-'? '0'--'9'+ ].

To print a string to standard output, you can use the construction:

The string will be printed assuming the terminal accepts ISO-8859-1 encoded characters (or standard output is an ISO-8859-1 stream). The operator fails if the string cannot be encoded in ISO-8859-1. Otherwise, it returns `nil. A warning is issued if the argument is not provably of type Latin1.

There are two operators available to load a file into a CDuce string:

The first one loads an ISO-8859-1 encoded file (resulting type: Latin1), whereas the second one loads a UTF-8 encoded file (resulting type: String).

There are two operators available to dump a CDuce string to a file:

The first one creates an ISO-8859-1 encoded file (it fails when the CDuce string contains non Latin1 characters), whereas the second one creates a UTF-8 encoded file. In both cases, the first argument is the filename and the second one is the string to dump.

It is possible in expression position to define a local prefix-namespace binding or to set a local default namespace.

See for more details.

The construction ref %%T%% %%e%% is used to build a reference initialized with the result of the expression %%e%%; later, the reference can receive any value of type %%T%%. The reference is actually a value of type {| get = [] -> T ; set = T -> [] |}.

Three syntactic sugar constructions are provided to facilitate the use of references:

!%%e%% === %%e%%.get [] {{ Dereferencing }} %%e1%% := %%e2%% === %%e1%%.set %%e2%% {{ Assignment }} %%e1%% ; %%e2%% === let [] = %%e1%% in %%e2%% {{Sequencing}}

CDuce is endowed with a select_from_where syntax to perform some SQL-like queries. The general form of select expressions is

where %%e%% is an expression %%b%% a boolean expression, the %%pi%%'s are patterns, and the %%ei%%'s are sequence expressions.

A select expression works like a set of nested transform expressions. The advantage of using select rather than transform is that queries are automatically optimized by applying classical logic SQL optimization techniques (this automatic optimization can be disabled).