examples.ml 2.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
let examples = [ "ovfun","
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 fun sort (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 -> sort x in
       let d = map fc with x -> sort x in
       <(tag) name=n>[ <sons>s  <daughters>d ] 
;; 

let base : Person = 
<person gender=\"M\">[ 
  <name>\"Claude\"
  <children>[ 
    <person gender=\"F\">[
      <name>\"Vronique\"
      <children>[
        <person gender=\"F\">[
          <name>\"Ilaria\"
          <children>[] 
        ] 
      ] 
    <tel> \"314-1592654\"
    ]
  ] 
  <tel kind=\"home\"> \"271-828182\"
]
;;

sort base;;
";"note","
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 fun format (<doc>s : Doc) : Result = 
  let (body,notes) = text (s,1) in
  <doc>[ <body>body <notes>notes ];;

let fun 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.' ];;

format src;;
"; ];;
let present = "<ul
><li
><a href=\"/cgi-bin/cduce2?example=ovfun\"
>Overloaded functions.</a
>This examples demonstrates the use of overloaded functions.</li
><li
><a href=\"/cgi-bin/cduce2?example=note\"
>Footnotes.</a
> This example shows how to bind an XML element with surrounding text.</li
></ul
>";;