Commit f439f418 authored by Pietro Abate's avatar Pietro Abate
Browse files

[r2004-10-30 01:56:24 by beppe] Empty log message

Original author: beppe
Date: 2004-10-30 01:56:24+00:00
parent 5dd13efc
......@@ -51,7 +51,7 @@ when <code>%%expr1%%</code> is an atom, and to omit the surrounding <code>{{{ }
</p>
<p> While this abbreviation results quite handy it requires some care when using
record patterns. As we sais the general form of a record pattern is:
record patterns. As we said the general form of a record pattern is:
</p>
<sample><![CDATA[
<(%%p1%%) (%%p2%%)>%%p3%%
......@@ -59,18 +59,77 @@ record patterns. As we sais the general form of a record pattern is:
<p>
and the same abbreviations as for expressions apply. In particular this means
that, say, the pattern <code>&lt;t (a)>_</code> stands for <code>&lt;(`t)
(a)>_</code>. Therefore while <code>&lt;t (a)>_</code> matches all the elements of
tag <code>t</code> (and captures in the varible <code>a</code> the attributes),
the pattern <code>&lt;(t) (a)>_</code> matches all XML elements (whatever their
tag is) and captures their tag in the variable <code>t</code> (and their
attributes in <code>a</code>). Another point to notice is that <code>&lt;t>_</code>
stands for <code>&lt;t ({})>_</code> (more precisely, for <code>&lt;(`t) ({})>_</code>). Since
<code>{}</code> is the open empty record type, then it matches all the records. Therefore
<code>&lt;t>_</code> matches all elements of tag <code>t</code> whatever they have whatever they have attributes or not. In the following we enumerate some simple examples to show what we explained:
(a)>_</code>. Therefore while <code>&lt;t (a)>_</code> matches all the elements
of tag <code>t</code> (and captures in the variable <code>a</code> the
attributes), the pattern <code>&lt;(t) (a)>_</code> matches all XML elements
(whatever their tag is) and captures their tag in the variable <code>t</code>
(and their attributes in <code>a</code>). Another point to notice is that
<code>&lt;t>_</code> stands for <code>&lt;t ({})>_</code> (more precisely, for
<code>&lt;(`t) ({})>_</code>). Since <code>{}</code> is the <i>open</i> empty
record type, then it matches all the records. Therefore <code>&lt;t>_</code>
matches all elements of tag <code>t</code> whatever they have whatever they have
attributes or not. In the following we enumerate some simple examples to show
what we explained. For that consider the following definitions for bibliographic data:
</p>
<sample><![CDATA[
type Biblio = [(Paper|Book)*]
type Paper = <paper isbn=?String year=String>[ Author+ Title Conference Url? ]
type Book = <book isbn=String> [ Author+ Title Url? ]
type Author = <author>[ PCDATA ]
type Title = <title>[ PCDATA ]
type Conference = <conference>[ PCDATA ]
type Url = <url>[ PCDATA ]
]]></sample>
<p>
Let <code>bib</code> be of type <code>Biblio</code> then
</p>
<sample><![CDATA[
transform bib with
<book (a)> [ (x::(Any\Url)|_)* ] -> [ <book (a)> x ]
]]></sample>
<p>
returns the list of all books without their Url element.
</p>
<sample><![CDATA[
transform bib with
<(book) (a)> [ (x::(Any\Url)|_)* ] -> [ <(book) (a)> x ]
]]></sample>
<p>
returns the bibliography in which all entries (either books or paper) no longer
have their Url elements (<code>book</code> is now a capture variable). Equivalently we could have
pushed the differences on tags:
</p>
<sample><![CDATA[
transform bib with
<(book) (a)> [ (x::<(Any\`url)>_|_)* ] -> [ <(book) (a)> x ]
]]></sample>
<p>
We can furthermore perform many kinds of manipulations on the attributes by
using the <a href="manual_expressions.html#record">operators for records</a>,
namely <code>%%r%%\%%l%%</code> which deletes the field <code>%%l%%</code>
in the record <code>%%r%%</code> whenever it is present, and <code>%%r1%% +
%%r2%%</code> which merges the records <code>%%r1%%</code> and
<code>%%r2%%</code> by giving the priority to the fields in the latter. For
instance
</p>
<sample><![CDATA[
transform bib with
<(t) (a)> x -> [ <(x) (a\isbn)> x ]
]]></sample>
<p>
strips all the ISBN attributes.
</p>
<sample><![CDATA[
transform bib with
<_ (a)> [(x::(Author|Title|Url)|_)*] -> [ <book ({isbn="fake"}+a\year)> x ]
]]></sample>
<p>
returns the bibliography in which all the Paper elements are transformed into
books, by forgetting the Conference elements removing the year attribute and
possibly adding a fake isbn attribute. Note that since record concatenation the
right-hand side record has the priority then if the record captured by
<code>a</code> already contains an isbn attribute, then this is preserved.
</p>
<b style="color:#FF0080">TO BE DONE</b>
<p>
As an example to summarize what we said above, consider the the elements
......@@ -90,16 +149,17 @@ type Tr = <tr {| bgcolor=?String; style=?String |}>[ Td+]
type Td = <td {| bgcolor=?String; style=?String |}>[ Table* | PCDATA ]
let strict ([Table*]->[Table*]; [Tr+]->[Tr+]; [Td+]->[Td+]; [PCDATA]->[PCDATA])
x -> map x with
<(t) (a& {| bgcolor=c; style=s |})> l
-> <(t) (a \ bgcolor+{style=(s@"; bgcolor:"@c)})>(strict l)
| <(t) (a& {| bgcolor=c |})> l
-> <(t) (a \ bgcolor+{style=("bgcolor:"@c)})>(strict l)
| <(t)> l -> <(t)>(strict l)
| c -> c
x ->
map x with
<(t) (a& {| bgcolor=c; style=s |})> l
-> <(t) (a\bgcolor+{style=(s@"; bgcolor:"@c)})>(strict l)
| <(t) (a& {| bgcolor=c |})> l
-> <(t) (a\bgcolor+{style=("bgcolor:"@c)})>(strict l)
| <(t)> l -> <(t)>(strict l)
| c -> c
]]></sample>
<p>
As an exercise the reader can try to rewrite the function strict so that the first three branches of the map are condensated into a unique branch.
As an exercise the reader can try to rewrite the function strict so that the first three branches of the map are condensed into a unique branch.
</p>
</box>
......@@ -108,7 +168,7 @@ As an exercise the reader can try to rewrite the function strict so that the fir
<p>
The blend of type type constructors and boolean combinator can be used to
reduce verbosity in writing pattern matching. As an example we show how to handle
tags with several optional attibutes.
tags with several optional attributes.
</p>
<p>
Consider the following fragment of code from site.cd from the CDuce
......@@ -161,7 +221,7 @@ let content (Sample -> String)
The intersection pattern <code>&amp;</code> makes both patterns to be matched
against the record of attributes: each pattern check the presence of an specific
attributes, if it present it captures the attribute value in a given variables
while if the attribute is absent the default subpattern to assign to the
while if the attribute is absent the default sub-pattern to assign to the
variable a default value.
</p>
</box>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment