Commit 00a7791a authored by Pietro Abate's avatar Pietro Abate

[r2005-03-06 16:13:08 by afrisch] New syntax for record types/patterns

Original author: afrisch
Date: 2005-03-06 16:13:08+00:00
parent c05df5e0
......@@ -25,6 +25,7 @@ Since 0.2.2
closed record: { l1=t1 l2=t2 }
- the ";" between fields is optional even for records
(used to be optional only for attributes)
* Keywords are now allowed as type names
- Tools:
* A new tool cduce_mktop produces customized CDuce toplevels with embedded
......
......@@ -121,8 +121,8 @@ test:
# Web site
driver/examples.ml: cduce web/examples/build.cd web/examples/examples.xml
(cd web/examples; ../../cduce build.cd --arg examples.xml)
driver/examples.ml: cduce web/xhtml.cdo web/examples/build.cd web/examples/examples.xml
(cd web/examples; ../../cduce build.cd -I .. --arg examples.xml)
webpages: cduce web/site.cdo
(cd web; ../cduce --run site.cdo --arg site.xml)
......
......@@ -453,4 +453,4 @@ Mutable values.
</li><li><a href=\"/cgi-bin/cduce?example=pm_compil\">Compilation of pattern matching.</a>
This example demonstrates the efficient compilation of pattern
matching.
</li></ul>"
\ No newline at end of file
</li></ul>"
......@@ -62,16 +62,16 @@ in XML transformations. Here, functions are stored within documents.
<box title="Types, pattern matching" link="typpm">
<demo><![CDATA[
type Bib = [ Book* ]
type Book = <book>[ Title Subtitle? Author+ ]
type Book = <book ..>[ Title Subtitle? Author+ ]
type Title = <title>[ PCDATA ]
type Subtitle = <subtitle>[ PCDATA ]
type Author = <author>[ PCDATA ]
let title(Book -> String)
<book>[ <title>x _* ] -> x
<book ..>[ <title>x _* ] -> x
let authors(Book -> [Author+])
<book>[ (x::Author|_)* ] -> x
<book ..>[ (x::Author|_)* ] -> x
]]></demo></box>
<box title="Sample values" link="samp"><demo prefix="last"><![CDATA[
......@@ -93,7 +93,7 @@ type ABook = <book print=FBook>[ Title Subtitle? Author+ ]
type ABib = [ ABook* ]
(* Remark: ABook <= Book, ABib <= Bib *)
let set(<book>c : Book)(f : FBook) : ABook =
let set(<book ..>c : Book)(f : FBook) : ABook =
<book print=f>c
let prepare(b : Bib) : ABib =
......@@ -123,7 +123,7 @@ let change(p : Book -> Bool)
type HasSub = <_>[ _* Subtitle _* ]
let subtitle(Book & HasSub -> String)
<book>[ _* <subtitle>x _* ] -> x
<book ..>[ _* <subtitle>x _* ] -> x
let change_sub =
change
......
(* This CDuce script generates the file examples.ml *)
using H="xhtml"
(*
include "../xhtml-strict.cd" (* XHTML 1 Strict DTD *)
include "../xhtml-categ.cd" (* Categories (Inline, ...) from this DTD *)
*)
(* Input documents: CDuce examples *)
......@@ -10,32 +13,32 @@ type Example = <example code=Latin1 title=Latin1>Latin1
(** Command line **)
let input =
let fname =
match argv [] with
| [ s ] -> s
| _ -> raise "Please specify an input file on the command line"
let examples =
match load_xml input with
match load_xml fname with
| Examples & <_>e -> e
| _ -> raise "Invalid input document"
let script (code : Latin1) : Latin1 =
"/cgi-bin/cduce?example=" @ code
let script (c : Latin1) : Latin1 =
"/cgi-bin/cduce?example=" @ c
let do_example (Example -> Xli)
<_ code=c title=t>a ->
<li>[ <a href = (script c) >[ !t '.' ] ' ' !a ]
let do_example (Example -> H.li)
<_ code=c title=t>ex ->
<li>[ <a href = (script c) >[ !t '.' ] ' ' !ex ]
let protect_quote (s : Latin1) : Latin1 =
transform s with '"' -> [ '\\"' ] | c -> [c]
let to_ml (e : [Example*]) : Latin1 =
transform e with
<_ code=c>_ ->
let code = load_file (c @ ".cd") in
[ '"' !c '","' !(protect_quote code) '";']
<_ code=c ..>_ ->
let cod = load_file (c @ ".cd") in
[ '"' !c '","' !(protect_quote cod) '";']
;;
......
......@@ -393,7 +393,7 @@ actually Qualified Names (see <local href="namespaces"/>).
<p>
The syntax of a record expression is
<code>{ %%l1%% = %%e1%%; %%...%%; %%ln%% = %%en%% }</code>
<code>{ %%l1%%=%%e1%%; %%...%%; %%ln%%=%%en%% }</code>
where the <code>%%li%%</code> are label names (same lexical
conventions as for identifiers), and the <code>%%vi%%</code>
are expressions. When an expression <code>%%ei%%</code>
......@@ -401,41 +401,33 @@ is simply a variable whose name match the field label
<code>%%li%%</code>, it is possible to omit it.
E.g.: <code>{ x; y = 10; z }</code>
is equivalent to <code>{ x = x; y = 10; z = z }</code>.
The semi-colons between fields are optional.
</p>
<p>
They are two kinds of record types. Open record types
are written <code>{ %%l1%% = %%t1%%; %%...%%; %%ln%% = %%tn%%
are written <code>{ %%l1%%=%%t1%%; %%...%%; %%ln%%=%%tn%%; ..
}</code>, and closed record types are written
<code>{| %%l1%% = %%t1%%; %%...%%; %%ln%% = %%tn%%
|}</code>.
<code>{ %%l1%% = %%t1%%; %%...%%; %%ln%% = %%tn%% }</code>.
Both denote all the record values where
the labels <code>%%li%%</code> are present and the associated values
are in the corresponding type. The distinction is that that open
type allow extra fields, whereas the closed type gives a strict
enumeration of the possible fields.
enumeration of the possible fields. The semi-colon between fields is optional.
</p>
<p>
Additionally, both for open and close record types,
it is possible to specify optional fields by using <code>=?</code>
instead of <code>=</code> between a label and a type.
For instance, <code>{| x = Int; y =? Bool |}</code>
For instance, <code>{ x = Int; y =? Bool }</code>
represents records with an <code>x</code> field of type
<code>Int</code>, an optional field <code>y</code> (when it is
present, it has type <code>Bool</code>), and no other field.
</p>
<p>
Note that the value <code>{ x = 1; y = 2 }</code>
has actually the type <code>{| x = 1; y = 2 |}</code>
which is more precise than <code>{ x = 1; y = 2 }</code>. This is
the only situation where the singleton type corresponding to a constructed
value is not syntactically equal to this value.
</p>
<p>
The syntax is the same for patterns. Note that capture variables
cannot appear in an optional field. A common idiom is to bind
......@@ -491,10 +483,10 @@ The following sample shows several way to write XML types.
</p>
<sample><![CDATA[
type A = <a x=String y=String>[ A* ]
type B = <(`x | `y)>[ ]
type C = <c {| x = String; y = String |}>[ ]
type U = { x = String; y =? String }
type A = <a x=String y=String ..>[ A* ]
type B = <(`x | `y) ..>[ ]
type C = <c x = String; y = String>[ ]
type U = { x = String y =? String ..}
type V = [ W* ]
type W = <v (U)>V
]]></sample>
......@@ -556,7 +548,7 @@ reference type. Instead, references are implemented
in an object-oriented way. The type <code>ref %%T%%</code>
denotes references of values of type <code>%%T%%</code>. It
is only syntactic sugar for the type
<code>{| get = [] -> T ; set = T -> [] |}</code>.
<code>{ get = [] -> T ; set = T -> [] }</code>.
</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