Debian Codegraph @

doc/examples/size/size.sml

raw
1 fun 'a printSize (name: string, value: 'a, use: 'a -> unit): unit=
2 (print "The size of "
3 ; print name
4 ; print " is "
5 ; print (IntInf.toString (MLton.size value))
6 ; print " bytes.\n"
7 ; use value)
8
9 fun chk (x, y) =
10 if x = y
11 then ()
12 else raise Fail "bug"
13
14 val l = [1, 2, 3, 4]
15
16 val _ =
17 (printSize ("a char", #"c", fn _ => ())
18 ; printSize ("an int list of length 4",
19 List.tabulate (4, fn i => i + 1), fn l =>
20 chk (foldl (op +) 0 l, 10))
21 ; printSize ("a string of length 10",
22 CharVector.tabulate (10, fn i => chr (ord #"0" + i)), fn s =>
23 chk (CharVector.foldl (fn (c,s) => ord c + s) 0 s, 525))
24 ; printSize ("an int array of length 10",
25 Array.tabulate (10, fn i => i), fn a =>
26 chk (Array.foldl (op +) 0 a, 45))
27 ; printSize ("a double array of length 10",
28 Array.tabulate (10, real), fn a =>
29 chk (Real.floor (Array.foldl (op +) 0.0 a), 45))
30 ; printSize ("an array of length 10 of 2-ples of ints",
31 Array.tabulate (10, fn i => (i, i + 1)), fn a =>
32 chk (Array.foldl (fn ((a,b),s) => a + b + s) 0 a, 100))
33 ; printSize ("a useless function",
34 fn _ => 13, fn f => ())
35 )
36
37 local
38 open MLton.Cont
39 in
40 val rc: int option t option ref = ref NONE
41 val _ =
42 case callcc (fn k: int option t => (rc := SOME k; throw (k, NONE))) of
43 NONE => ()
44 | SOME i => print (concat [Int.toString i, "\n"])
45 end
46
47 val _ = printSize ("a continuation option ref",
48 rc, fn rc =>
49 case !rc of
50 NONE => ()
51 | SOME k => (rc := NONE; MLton.Cont.throw (k, SOME 13)))