annotate tests/stypes.ur @ 823:669ac5e9a69e

Demo compiles with pattern-matching-fu
author Adam Chlipala <adamc@hcoop.net>
date Thu, 28 May 2009 10:35:25 -0400
parents 102e81d975e3
children
rev   line source
adamc@588 1 datatype color = Red | White | Blue
adamc@588 2
adamc@588 3 fun c2s c =
adamc@588 4 case c of
adamc@588 5 Red => "Red"
adamc@588 6 | White => "White"
adamc@588 7 | Blue => "Blue"
adamc@588 8
adamc@588 9 val show_color = mkShow c2s
adamc@588 10
adamc@588 11 datatype list a = Nil | Cons of a * list a
adamc@588 12
adamc@588 13 fun isNil (t ::: Type) (ls : list t) =
adamc@588 14 case ls of
adamc@588 15 Nil => True
adamc@588 16 | _ => False
adamc@588 17
adamc@588 18 fun delist (ls : list string) : xml body [] [] =
adamc@588 19 case ls of
adamc@588 20 Nil => <xml>Nil</xml>
adamc@588 21 | Cons (h, t) => <xml>{[h]} :: {delist t}</xml>
adamc@588 22
adamc@582 23 fun main () : transaction page =
adamc@582 24 sInt <- source 0;
adamc@583 25 sFloat <- source 1.23;
adamc@583 26 sBoth <- source (7, 42.1);
adamc@583 27
adamc@583 28 sOpt <- source None;
adamc@586 29 sBool <- source True;
adamc@583 30
adamc@588 31 sColor <- source White;
adamc@588 32 sList <- source Nil;
adamc@588 33
adamc@582 34 return <xml><body>
adamc@583 35 <dyn signal={n <- signal sInt; return <xml>{[n + 3]}</xml>}/> <a onclick={set sInt 1}>Change</a><br/>
adamc@583 36
adamc@583 37 <dyn signal={n <- signal sFloat; return <xml>{[n + 1.0]}</xml>}/> <a onclick={set sFloat 4.56}>Change</a><br/>
adamc@583 38
adamc@585 39 <dyn signal={p <- signal sBoth; return <xml>{[p.1]}, {[p.2]}</xml>}/>;
adamc@585 40 <dyn signal={p <- signal sBoth; case p of
adamc@585 41 (7, _) => return <xml>Initial</xml>
adamc@585 42 | (fst, snd) => return <xml>{[fst]}, {[snd]}</xml>}/>
adamc@585 43 <a onclick={set sBoth (8, 100.001)}>Change</a><br/>
adamc@584 44
adamc@584 45 <dyn signal={o <- signal sOpt; case o of
adamc@584 46 None => return <xml>None</xml>
adamc@584 47 | Some n => return <xml>{[n]}</xml>}/>
adamc@584 48 <a onclick={set sOpt (Some 7)}>Change</a><br/>
adamc@586 49
adamc@587 50 <dyn signal={b <- signal sBool; return <xml>{[b]}</xml>}/>
adamc@587 51 <dyn signal={b <- signal sBool; if b then return <xml>Yes</xml> else return <xml>No</xml>}/>
adamc@587 52 <a onclick={set sBool False}>Change</a><br/>
adamc@588 53
adamc@588 54 <dyn signal={c <- signal sColor; return <xml>{[c]}</xml>}/>
adamc@588 55 <a onclick={set sColor Red}>Red</a>
adamc@588 56 <a onclick={set sColor White}>White</a>
adamc@588 57 <a onclick={set sColor Blue}>Blue</a><br/>
adamc@588 58
adamc@589 59 <dyn signal={ls <- signal sList; return <xml>{[isNil ls]}</xml>}/>;
adamc@589 60 <dyn signal={ls <- signal sList; return <xml>{delist ls}</xml>}/>
adamc@588 61 <a onclick={set sList (Cons ("A", Cons ("B", Nil)))}>Change</a><br/>
adamc@582 62 </body></xml>