Commit 779524a3 authored by Mattias's avatar Mattias
Browse files

Merge branch 'transfer_website' into 'dev'

Deleted the website from the cduce repo

See merge request !19
parents 55e536b6 b7db21ee
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<site>
<title>CDuce</title>
<footer>
<meta>
<p>
<local href="sitemap">Site map</local>
</p>
</meta>
</footer>
<page name="index">
<title>CDuce: documentation</title>
<include file="tutorial.xml"/>
<include file="manual.xml"/>
<include file="memento.xml"/>
<left>
<p>
This is the main entry point to CDuce documentation.
</p>
<pages-toc/>
</left>
<box title="Available documents" link="src">
<p>
We mantain three kinds of on-line documentation: a <a
href="manual.html">User's guide</a> where all CDuce constructions are
grouped by categories and explained; a <a href="tutorial.html">Tutorial</a>
where we give a gentle introduction to programming in CDuce; and a
<a href="memento.html">Quick reference card</a> where we group and briefly explain in a single page CDuce syntax.
</p>
</box>
<page name="sitemap">
<title>Sitemap</title>
<left>
<p>This page lists all the pages from the CDuce documentation.</p>
</left>
<box title="Sitemap" link="sitemap">
<site-toc/>
</box>
</page>
</page>
</site>
(rule
(alias website)
(deps
%{bin:cduce}
site.cdo
(glob_files *.xml)
(glob_files manual/*.xml)
(glob_files tutorial/*.xml))
(targets)
(action
(progn
(system "mkdir doc")
(run %{bin:cduce} --run site.cdo --arg doc.xml -o doc))))
(rule
(deps site.cd siteTypes.cdo xhtml.cdo)
(target site.cdo)
(action
(run %{bin:cduce} --compile site.cd)))
(rule
(deps siteTypes.cd xhtml.cdo)
(target siteTypes.cdo)
(action
(run %{bin:cduce} --compile siteTypes.cd)))
(rule
(deps
(glob_files xhtml*.cd))
(target xhtml.cdo)
(action
(run %{bin:cduce} --compile xhtml.cd)))
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<page name="examples">
<title>Advanced examples</title>
<left>
<p>This page presents some sample CDuce programs.</p>
<boxes-toc/>
<p>See also:</p>
<local-links href="index,proto,memento"/>
</left>
<box title="Advanced examples" link="start">
This page presents some advanced programming examples in CDuce. <b
style="color:#FF0080">If you never saw CDuce programs before, this is the wrong
page</b> to start with. Rather follow our <local href="tutorial"/>, or test the
simple examples in our <a href="http://reglisse.ens.fr/cgi-bin/cduce">on line demo</a>.
</box>
<box title="Our canonical example" link="sort">
<p>
The example below is the one we use to demonstrate
how overloaded functions can avoid duplicating code.
Without overloaded functions, we would need to define
two mutually recursive functions in order to type-check
the transformation. Here, two constraints
in the (highlighted) function interface
can express precisely the behaviour of the function.
A detailed explanation of the code can be found <a href="tutorial_overloading.html#canonical">here</a>.
</p>
<sample>
<include-verbatim file="overloading.cd"/>
</sample>
</box>
<box title="Datatypes + first-class functions" link="data">
<p>
The program below shows how to simulate ML data types in CDuce.
It implements a (naive backtracking) regular expression recognizer.
The examples also demonstrate the use of first-class functions
(used as continuations).
</p>
<p>
Exercise for the reader: show that the algorithm may not terminate for
some special regular expressions.
</p>
<sample highlight="false">
<include-verbatim file="regexp.cd"/>
</sample>
</box>
<box title="First-class functions and XML together" link="funxml">
<p>
The program below illustrates the use of first-class functions stored
in (pseudo) XML documents.
</p>
<sample highlight="false">
<include-verbatim file="funxml.cd"/>
</sample>
</box>
<box title="CDuce quine" link="quine">
<p>
A quine (a.k.a. self-rep) is a program that produces its own source
code. Here is an example of a quine written in CDuce.
</p>
<sample highlight="false">
<include-verbatim file="quine.cd"/>
</sample>
</box>
<box title="The script that generates this site" link="site">
<p>
The script below is one of the longest CDuce application ever written
;-) It is used to produce all the pages of this web site (except
the <local href="proto">web prototype</local> which is
a CGI script written in OCaml). CDuce type system ensures
that produced pages are valid w.r.t XHTML 1.0 Strict.
</p>
<p>
This program features both XML and text-content manipulation.
It also demonstrates the use of non-XML internal data structures.
Here, a tree represents internally the site
structure, and a list represents the path from the
root to the current page (in order to display the "You're here" line).
</p>
<sample highlight="false">
<include-verbatim file="site.cd"/>
</sample>
</box>
</page>
type Biblio = <bibliography>[Heading Paper*]
type Heading = <heading>[ PCDATA ]
type Paper = <paper>[ Author+ Title Conference File ]
type Author = <author>[ PCDATA ]
type Title = <title>[ PCDATA ]
type Conference = <conference>[ PCDATA ]
type File = <file>[ PCDATA ]
(* Simplified HTML *)
type Html = <html>[ <head>[ <title>[ PCDATA ] ] <body>Mix ]
type Mix = [ ( <h1>Mix | <a href=String>Mix | <p>Mix | <em>Mix
| <ul>[ <li>Mix +] | Char )* ]
let do_authors ([Author+] -> Mix)
| [ <author>a ] -> a
| [ <author>a <author>b ] -> a @ " and, " @ b
| [ <author>a; x] -> a @ ", " @ (do_authors x)
let do_paper (Paper -> <li>Mix)
<paper>[ x::_* <title>t <conference>c <file>f ] ->
<li>[ <a href=f>t !(do_authors x) '; in ' <em>c '.' ]
let do_biblio (Biblio -> Html)
<bibliography>[ <heading>h; p ] ->
let body = match p with
| [] -> "Empty bibliography"
| l -> [ <h1>h <ul>(map l with x -> do_paper x) ]
in
<html>[ <head>[ <title>h ] <body>body ]
let bib : Biblio =
<bibliography>[
<heading>"Alain Frisch's bibliography"
<paper>[
<author>"Alain Frisch"
<author>"Giuseppe Castagna"
<author>"Vronique Benzaken"
<title>"Semantic subtyping"
<conference>"LICS 02"
<file>"semsub.ps.gz"
]
<paper>[
<author>"Mariangiola Dezani-Ciancaglini"
<author>"Alain Frisch"
<author>"Elio Giovannetti"
<author>"Yoko Motohama"
<title>"The Relevance of Semantic Subtyping"
<conference>"ITRS'02"
<file>"itrs02.ps.gz"
]
<paper>[
<author>"Vronique Benzaken"
<author>"Giuseppe Castagna"
<author>"Alain Frisch"
<title>"CDuce: a white-paper"
<conference>"PLANX-02"
<file>"planx.ps.gz"
]
]
in
do_biblio bib
(* 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 *)
type Examples = <examples>[Example+]
type Example = <example code=Latin1 title=Latin1>Latin1
(** Command line **)
let fname =
match argv [] with
| [ s ] -> s
| _ -> raise "Please specify an input file on the command line"
let examples =
match load_xml fname with
| Examples & <_>e -> e
| _ -> raise "Invalid input document"
let script (c : Latin1) : Latin1 =
"/cgi-bin/cduce?example=" @ c
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 cod = load_file (c @ ".cd") in
[ '"' !c '","' !(protect_quote cod) '";']
;;
dump_to_file "../../driver/examples.ml"
[ '
let examples = [ ' !(to_ml examples) ' ]
let present = "'
!(protect_quote (print_xml
<ul>(map examples with e -> do_example e))
) '"' ]
<?xml version="1.0" encoding="iso-8859-1"?>
<examples>
<example code="q1" title="Q1">
Q1 Query
</example>
<example code="q2" title="Q2">
Q2 Query
</example>
<example code="q3" title="Q3">
Q3 Query
</example>
<example code="q4" title="Q4">
Q4 Query
</example>
<example code="q5" title="Q5">
Q5 Query
</example>
<example code="q6" title="Q6">
Q6 Query
</example>
<example code="q8" title="Q8">
Q8 Query
</example>
<example code="q9" title="Q9">
Q9 Query
</example>
<example code="q11" title="Q11">
Q11 Query
</example>
<example code="q12" title="Q12">
Q12 Query
</example>
<example code="qatt1" title="Attributs et éléments">
Partie 1
</example>
<example code="qatt2" title="Attributs et éléments">
Partie 2
</example>
<example code="qatt3" title="Attributs et éléments">
Partie 3
</example>
<example code="xml" title="XML elements">
XML elements.
</example>
<example code="functions" title="Functions">
Several syntaxes to define functions.
</example>
<example code="mutrec" title="Mutual recursion">
Mutual toplevel definition for types and functions.
</example>
<example code="sequence" title="Sequence literals">
How to write sequences.
</example>
<example code="seqtypes" title="Sequence types">
Types for sequences.
</example>
<example code="integers" title="The factorial function">
What about computing 10000! ?
</example>
<example code="sumtype" title="Sum types">
How to simulate ML sum types.
</example>
<example code="ovfun" title="Overloaded functions">
This examples demonstrates the use of overloaded functions.
</example>
<example code="note" title="Footnotes">
This example shows how to bind an XML element with surrounding text.
</example>
<example code="biblio" title="Bibliography">
The good old XML bibliography example.
</example>
<example code="projection" title="Projection">
Syntactic sugar for projection.
</example>
<example code="xtransform" title="Tree transformations">
How to perform XSLT-like transformations.
</example>
<example code="reference" title="References">
Mutable values.
</example>
<example code="pm_compil" title="Compilation of pattern matching">
This example demonstrates the efficient compilation of pattern
matching.
</example>
</examples>
(* Simple functions can be defined this way: *)
let f1 (x : Int) : Int = x + 3
;;
f1 5
(* With several arguments: *)
let f2 (x : Int, y : Int) : Int = x + y
;;
f2 (10,20)
(* Currified form *)
let add (x : Int) (y : Int) : Int = x + y
;;
add 10 20
(* You may directly deconstruct the arguments: *)
type A = <a href=String>String
let f3 (<a href=url>txt : A) : String = url @ "=>" @ txt
;;
f3 <a href="http://www.cduce.org">"CDuce homepage";;
(* In general, if you want to specify several arrow types, or
use several pattern matching branches, you have the general
form: *)
let f4 (A -> String; ['0'--'9'+] -> Int)
| x & A -> f3 x
| x -> int_of x
;;
f4 "123"
(* Yes, CDuce can handle large integers! *)
let facto (Int -> Int)
| 0 | 1 -> 1
| n -> n * (facto (n - 1))
in
facto 300
(* The tail-recursive way *)
let facto ((Int,Int) -> Int)
| (x, 0 | 1) -> x
| (x, n) -> facto (x * n, n - 1)
in
facto (1,10000)
(* Adjacent type declarations are mutually recursive *)
type T = <t>S
type S = [ (Char | T)* ]
let x : S = [ 'abc' <t>['def'] 'ghi' ]
(* Similarly for toplevel function definitions *)
let f (x : Int) : Int = g x
let g (x : Int) : Int = 3
let a = 2
let h (x : Int) : Int = f x
(* f and g are mutually recursive, but they cannot use h *)
type Doc = <doc>Text
type Text = [ (Char | (Letter+ ' '* Note))* ]
type Letter = 'a'--'z' | 'A'--'Z'
type Note = <note>[ PCDATA ]
type Flow = [ (Char | <ref no=Int>[ PCDATA ])* ]
type Notes = [ <note no=Int>[ PCDATA ]* ]
type Result = <doc>[ <body>Flow <notes>Notes ]
let format (<doc>s : Doc) : Result =
let (body,notes) = text (s,1) in
<doc>[ <body>body <notes>notes ]
let text ( (Text,Int) -> (Flow,Notes) )
| ([ pre::Char*? (word::Letter+ ' '* <note>n); rem ], count) ->
let (body,notes) = text (rem, count + 1) in
(pre @ [<ref no=count>word] @ body,
[<note no=count>n] @ notes)
| (body,_) -> (body, [])
let src : Doc = <doc>[ 'CDuce ' <note>"Frisch, Castagna, Benzaken"
' is an XML ' <note>"a W3C standard"
'-friendly programming language.' ]
in
format src
type Person = FPerson | MPerson
type FPerson = <person gender = "F" >[ Name Children (Tel | Email)?]
type MPerson = <person gender="M">[ Name Children (Tel | Email)?]
type Children = <children>[Person*]
type Name = <name>[ PCDATA ]
type Tel = <tel kind=?"home"|"work">['0'--'9'+ '-' '0'--'9'+]
type Email = <email>[PCDATA '@' PCDATA]
type Man = <man name=String>[ Sons Daughters ]
type Woman = <woman name=String>[ Sons Daughters ]
type Sons = <sons>[ Man* ]
type Daughters = <daughters>[ Woman* ]
let split (MPerson -> Man ; FPerson -> Woman)
<person gender=g>[ <name>n <children>[(mc::MPerson | fc::FPerson)*]; _] ->
let tag = match g with "F" -> `woman | "M" -> `man in
let s = map mc with x -> split x in
let d = map fc with x -> split x in
<(tag) name=n>[ <sons>s <daughters>d ]
let base : Person =
<person gender="F">[
<name>"Themis"
<children>[
<person gender="M">[
<name>"Prometheus"
<children>[
<person gender="M">[
<name>"Deucalion"
<children>[]
]
]
<email>"focifero@olympus.com"
]
<person gender="M">[
<name>"Epimetheus"
<children>[]
<tel> "314-1592654"
]
]
<tel kind="home"> "271-828182"
]
in
split base
(* This example demonstrates the efficient compilation of pattern
matching. *)
type A = <a>[ Int* ]
type B = <b>[ Char* ]
(* Imagine we want to compile the following function:
fun ([A+|B+] -> Bool) [A+] -> 0 | [B+] -> 1
For an arbitrary value, it is expensive to check whether it has
type [A+] or not. But if we know statically that it has type [A+|B+],
we just have to check the tag of the first element !
This is demonstrated by the following internal debugging feature.
The syntax is:
debug compile T P1 ... Pn
where T is the input type (static information about the matched
value) and P1,...,Pn are the patterns to compile (simultaneously).
The "debug compile" instruction displays an human-readable
representation of the automaton corresponding to the pattern
matching. Note: in actual evaluation, this automaton is build
lazily (= on-the-fly, = JIT).
*)
debug compile [A+|B+] [A+] [B+]
(* You can see on the output that the pattern matching is actually
compiled as:
fun ([A+|B+] -> Int) [ <a>_ ; _ ] -> 0 | _ -> 1
*)
(* The projection e/t is translated to:
transform e with [ (x::t|_)* ] -> x *)
type Biblio = <bibliography>[Heading Paper*]
type Heading = <heading>[ PCDATA ]
type Paper = <paper>[ Author+ Title Conference File ]
type Author = <author>[ PCDATA ]
type Title = <title>[ PCDATA ]
type Conference = <conference>[ PCDATA ]
type File = <file>[ PCDATA ]
let bib : Biblio =
<bibliography>[
<heading>"Alain Frisch's bibliography"
<paper>[
<author>"Alain Frisch"
<author>"Giuseppe Castagna"
<author>"Vronique Benzaken"
<title>"Semantic subtyping"
<conference>"LICS 02"
<file>"semsub.ps.gz"
]
<paper>[
<author>"Mariangiola Dezani-Ciancaglini"
<author>"Alain Frisch"
<author>"Elio Giovannetti"
<author>"Yoko Motohama"
<title>"The Relevance of Semantic Subtyping"
<conference>"ITRS'02"
<file>"itrs02.ps.gz"
]
<paper>[
<author>"Vronique Benzaken"
<author>"Giuseppe Castagna"
<author>"Alain Frisch"
<title>"CDuce: a white-paper"
<conference>"PLANX-02"
<file>"planx.ps.gz"
]
]
let titles = [bib]/<paper>_/<title>_
let authors = [bib]/<paper>_/<author>_
let titles_concat = [bib]/<paper>_/<title>_/Char
type Bib = <bib>[Book*]
type Book = <book year=String>[Title (Author+ | Editor+ ) Publisher Price ]
type Author = <author>[Last First ]
type Editor = <editor>[Last First Affiliation ]
type Title = <title>[PCDATA ]
type Last = <last>[PCDATA]
type First = <first>[PCDATA]
type Affiliation = <affiliation>[PCDATA]
type Publisher = <publisher>[PCDATA]
type Price = <price>[PCDATA];;
let doc : Bib = <bib>[
<book year="1994">[
<title>['TCP/IP Illustrated']
<author>[<last>['Stevens']