comparison tests/div.ur @ 1619:15e0c935c91b

Catching integer divisions by zero
author Adam Chlipala <adam@chlipala.net>
date Sat, 03 Dec 2011 09:44:07 -0500
parents
children
comparison
equal deleted inserted replaced
1618:705cb41ac7d0 1619:15e0c935c91b
1 functor Make(M : sig
2 type t
3 val read_t : read t
4 val show_t : show t
5 val num_t : num t
6 end) = struct
7 fun calculate (n1, n2) = return <xml><body>
8 {[readError n1 / readError n2 : M.t]}<br/>
9 {[readError n1 % readError n2 : M.t]}<br/>
10 </body></xml>
11
12 fun main () =
13 s1 <- source "";
14 s2 <- source "";
15 s3 <- source "";
16 s4 <- source "";
17 return <xml><body>
18 <h1>Client-side</h1>
19
20 <ctextbox source={s1}/> / <ctextbox source={s2}/>
21 <button value="=" onclick={n1 <- get s1;
22 n2 <- get s2;
23 set s3 (show (readError n1 / readError n2 : M.t));
24 set s4 (show (readError n1 % readError n2 : M.t))}/>
25 <dyn signal={n <- signal s3; return (txt n)}/>,
26 <dyn signal={n <- signal s4; return (txt n)}/>
27
28 <h1>Server-side</h1>
29
30 <form>
31 <textbox{#1}/> / <textbox{#2}/>
32 <submit value="=" action={calculate}/>
33 </form>
34 </body></xml>
35 end
36
37 structure Int = Make(struct type t = int end)
38 structure Float = Make(struct type t = float end)
39
40 fun main () : transaction page = return <xml><body>
41 <li><a link={Int.main ()}>Int</a></li>
42 <li><a link={Float.main ()}>Float</a></li>
43 </body></xml>