Commit 3a69bccd by Pietro Abate

### [r2003-07-08 09:23:43 by cvscast] Empty log message

```Original author: cvscast
Date: 2003-07-08 09:23:44+00:00```
parent e02861b7
 ... ... @@ -7,6 +7,7 @@

... ...

 Patterns for dummies Patterns for dummies TO BE DONE

Recursive patterns use the same syntax as recursive types: %%P%% where %%P1%%=%%p1%% and ... and %%Pn%%=%%pn%% with P, P1,..., Pn being variables ranging over pattern identifiers (i.e., identifiers starting by a capital letter). Recursive patterns allow one to express complex extraction of information from the matched value. For instance, consider the pattern P where P = (x & Int, _) | (_, P); it extracts from a sequence the first element of type Int (recall that sequences are encoded with pairs). The order is important, because the pattern P where P = (_, P) | (x & Int, _) extracts the last element of type Int.

A pattern may also extract and reconstruct a subsequence, using the convention described before that when a capture variable appears on both sides of a pair pattern, the two values bound to this variable are paired together. For instance, P where P = (x & Int, P) | (_, P) | (x := `nil) extracts all the elements of type Int from a sequence (x is bound to the sequence containing them) and the pattern P where P = (x & Int, (x & Int, _)) | (_, P) extracts the first pair of consecutive integers.

CDuce provides syntactic sugar for defining patterns working on sequences with regular expressions built from patterns, usual regular expression operators, and sequence capture variables of the form x::%%R%% (where R is a pattern regular expression).

Regular expression operators *, +, ? are greedy in the sense that they try to match as many times as possible. Ungreedy versions *?, +? and ?? are also provided; the difference in the compilation scheme is just a matter of order in alternative patterns. For instance, [_* (x & Int) _*] is compiled to P where P = (_,P) | (x & Int, _) while [_*? (x & Int) _*] is compiled to P where P = (x & Int, _) | (_,P).

Let us detail the compilation of an example with a sequence capture variable:

The first step is to propagate the variable down to simple patterns:

which is then compiled to the recursive pattern:

The (d & `nil) pattern above has a double purpose: it checks that the end of the matched sequence has been reached, and it binds d to `nil, to create the end of the new sequence.

Note the difference between [ x&Int ] and [ x::Int ]. Both patterns accept sequences formed of a single integer {{i}}, but the first one binds {{i}} to x, whereas the second one binds to x the sequence [{{i}}].

A mix of greedy and ungreedy operators with the first match policy of alternate patterns allows the definition of powerful extractions. For instance, one can define a function that for a given person returns the first work phone number if any, otherwise the last e-mail, if any, otherwise any telephone number, or the string "no contact":

String) <_>[ _ _ ( _*? x) | (_* x) | x ] -> x | _ -> "no contact" ]]>

(note that <tel>x does not need to be preceded by any wildcard pattern as it is the only possible remaining case).

\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!