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 argument (that is, when the argument is a tuple):

which is strictly equivalent to:

%%s%%) (%%x1%%,%%...%%,%%xn%%) -> %%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 need to call itself recursively, for instance). When the two names coincide (or when you don't need an internal name), there is a lighter notation:

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 "patterns" 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.

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 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.