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

[r2003-07-08 11:48:40 by cvscast] Empty log message

Original author: cvscast
Date: 2003-07-08 11:52:10+00:00
parent ceaa152e
......@@ -7,7 +7,7 @@
<p>
CDuce is a strongly-typed functional programming language adapted
to the manipulation of XML documents. Its syntax is reminiscient
to the manipulation of XML documents. Its syntax is reminiscent
of the ML family, but CDuce has a completely different type system.
</p>
......@@ -45,14 +45,14 @@ to extract from an input value some sub-values, which can then be
used in the rest of the program. For instance, the pattern
<code>&lt;a href=x>[]</code> extracts the value of the
<code>href</code> attribute and binds it to the <em>value
identifier</em> <code>x</code>.
identifier</em> <code> x</code>.
</li>
</ul>
<section title="A first example">
<sample><![CDATA[
let x = "Hello, " in
let y = "world !" in
let y = "world!" in
x @ y
]]></sample>
......@@ -71,6 +71,29 @@ let %%p%% = %%e%% in %%e'%%
where <code>%%p%%</code> is a pattern and <code>%%e%%</code>,
<code>%%e'%%</code> are expressions.
</p>
<p>
Patterns are much more than simple variables. They can be used to decompose
values. For instance, if the words <tt>Hello</tt> and <tt>world</tt> are in the two elements of a pair, we can capture each of them and concatenate them as follows:
</p>
<sample><![CDATA[
let (x,y) = ("Hello, " , "world!") in x @ y
]]></sample>
<p>
Patterns can also check types. So for instance
</p>
<sample><![CDATA[
let (x & String, y) = %%e%% in x
]]></sample>
<p>
would return a (static) type error if the first projection of <code>%%e%%</code> has not the static type <code>String</code>.
</p>
<p>
The form <code>let x&amp;%%t%% = %%e%% in %%e'%%</code> is used so often that we introduced a special syntax for it:
</p>
<sample><![CDATA[
let x : %%t%% = %%e%% in %%e'%%
]]></sample>
</box>
<box title="XML documents" link="xml">
......@@ -187,13 +210,40 @@ characters.</p>
by binding the variable <code>parents</code> to the XML document. It
also specifies that parents has the type <a
href="#type_decl"><code>ParentBook</code></a>: this is optional but it
usually allows earlier detection of type errors. If the file XML on
the left hand-side is stored in a file, say, <tt>parents.xml</tt> then
the same binding can be obtained by loading the file as follows </p>
<sample><![CDATA[ let parents : ParentBook = {{load_xml}}
"parents.xml" ]]></sample> <p> as <code>load_xml</code> converts and
XML document stored i a file into the CDuce expression representing
it.</p>
usually allows earlier detection of type errors.
</p>
<p>
If the file XML on the left hand-side is stored in a file, say,
<tt>parents.xml</tt> then it can be loaded from the file by <code>{{load_xml}}
"parents.xml"</code> as the builtin function <code>load_xml</code> converts and
XML document stored in a file into the CDuce expression representing it. However
<code>load_xml</code> has type <code>String->Any</code>, where
<code>Any</code> is the type of all values. Therefore if we try to reproduce the
same binding as the above by writing the following declaration
</p>
<sample><![CDATA[
let parents : ParentBook = {{load_xml}} "parents.xml"
]]></sample>
<p>
we would obtain a type error as we were trying to use an expression of type
<code>Any</code> where an expression of type <code>ParentBook</code> is expected.
The right way to reproduce the binding above is:
</p>
<sample><![CDATA[
let parents : ParentBook =
match load_xml "parents.xml" with
x & ParentBook -> x
| _ -> raise "parents.xml is not a document of type ParentBook"
]]></sample>
<p>
what this expression does is that before assigning the result of the load_xml expression to the
variable <code>parents</code> it matches it against the type
<code>ParentBook</code>. If it succeeds (i.e., if the XML file in the document has
type <code>ParentBook</code>) then it performs the assignment (the variable
<code>x</code> is bound to the result of the load_xml expression by the pattern
<code>x&amp;ParentBook</code>) otherwise it raises an exception.
</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