Commit | Line | Data |
---|---|---|
7f918cf1 CE |
1 | fun 'a printSize (name: string, value: 'a): unit= |
2 | (print "The size of " | |
3 | ; print name | |
4 | ; print " is = " | |
5 | ; print (Int.toString (MLton.size value)) | |
6 | ; print " bytes.\n" | |
7 | ; MLton.GC.collect ()) | |
8 | ||
9 | datatype day = Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | |
10 | ||
11 | val l = [1, 2, 3, 4] | |
12 | ||
13 | val ul = [(),(),(),()] | |
14 | val bl = [true, false, true, false] | |
15 | val dl = [Monday, Tuesday, Thursday, Friday] | |
16 | ||
17 | val rul = ref ul | |
18 | val rbl = ref bl | |
19 | val rdl = ref dl | |
20 | val r0 = ref () | |
21 | val r8 = ref (Word8.fromInt 0) | |
22 | val r16 = ref (Word16.fromInt 0) | |
23 | val r32 = ref (Word32.fromInt 0) | |
24 | val r64 = ref (Word64.fromInt 0) | |
25 | ||
26 | val _ = | |
27 | (() | |
28 | ; printSize ("unit", ()) | |
29 | ; printSize ("unit * unit", ((),())) | |
30 | ; printSize ("bool", true) | |
31 | ; printSize ("bool * bool", (true, false)) | |
32 | ; printSize ("day", Monday) | |
33 | ; printSize ("day * day", (Monday, Tuesday)) | |
34 | ; printSize ("a char", #"c") | |
35 | ; printSize ("a char * char", (#"c", #"d")) | |
36 | ; printSize ("a word8", Word8.fromInt 0) | |
37 | ; printSize ("a word8 * word8", (Word8.fromInt 0, Word8.fromInt 1)) | |
38 | ; printSize ("a word16", Word16.fromInt 0) | |
39 | ; printSize ("a word16 * word16", (Word16.fromInt 0, Word16.fromInt 1)) | |
40 | ; printSize ("a word32", Word32.fromInt 0) | |
41 | ; printSize ("a word32 * word32", (Word32.fromInt 0, Word32.fromInt 1)) | |
42 | ; printSize ("a word64", Word64.fromInt 0) | |
43 | ; printSize ("a word64 * word64", (Word64.fromInt 90, Word64.fromInt 91)) | |
44 | ; printSize ("a word64 * word64 * word64", (Word64.fromInt 80, Word64.fromInt 81, Word64.fromInt 82)) | |
45 | ; printSize ("a word64 * word64 * word64 * word64", (Word64.fromInt 70, Word64.fromInt 71, Word64.fromInt 72, Word64.fromInt 73)) | |
46 | ; printSize ("a unit list of length 4", ul) | |
47 | ; printSize ("a bool list of length 4", bl) | |
48 | ; printSize ("a day list of length 4", dl) | |
49 | ; printSize ("an int list of length 4", l) | |
50 | ; printSize ("a string of length 10", "0123456789") | |
51 | ; List.app (fn i => printSize ("a word64 array of length " ^ Int.toString i, Array.tabulate (i, fn _ => Word64.fromInt 0))) | |
52 | (List.tabulate (13, fn i => i)) | |
53 | ; List.app (fn i => printSize ("a word32 array of length " ^ Int.toString i, Array.tabulate (i, fn _ => Word32.fromInt 0))) | |
54 | (List.tabulate (13, fn i => i)) | |
55 | ; List.app (fn i => printSize ("a word16 array of length " ^ Int.toString i, Array.tabulate (i, fn _ => Word16.fromInt 0))) | |
56 | (List.tabulate (13, fn i => i)) | |
57 | ; List.app (fn i => printSize ("a word8 array of length " ^ Int.toString i, Array.tabulate (i, fn _ => Word8.fromInt 0))) | |
58 | (List.tabulate (13, fn i => i)) | |
59 | ; List.app (fn i => printSize ("a unit array of length " ^ Int.toString i, Array.tabulate (i, fn _ => ()))) | |
60 | (List.tabulate (13, fn i => i)) | |
61 | ; printSize ("a word64 ref", r64) | |
62 | ; printSize ("a word32 ref", r32) | |
63 | ; printSize ("a word16 ref", r16) | |
64 | ; printSize ("a word8 ref", r8) | |
65 | ; printSize ("a unit ref", r0) | |
66 | ; printSize ("a double array of length 10", | |
67 | Array.tabulate (10, fn _ => 0.0)) | |
68 | ; printSize ("a (word32 * double) array of length 10", | |
69 | Array.tabulate (10, fn i => (Word32.fromInt i, 0.0))) | |
70 | ; printSize ("a (word32 * word32 * double) array of length 10", | |
71 | Array.tabulate (10, fn i => (Word32.fromInt (i + 1), | |
72 | Word32.fromInt i, 0.0))) | |
73 | ; printSize ("a (word64 * double) array of length 10", | |
74 | Array.tabulate (10, fn i => (Word64.fromInt (i + 1), 0.0))) | |
75 | ; printSize ("a (word16 * double) array of length 10", | |
76 | Array.tabulate (10, fn i => (Word16.fromInt (i + 1), 0.0))) | |
77 | ; printSize ("a word64 array of length 10", | |
78 | Array.tabulate (10, fn i => Word64.fromInt i)) | |
79 | ; printSize ("a (word32 * word64) array of length 10", | |
80 | Array.tabulate (10, fn i => (Word32.fromInt i, | |
81 | Word64.fromInt i))) | |
82 | ; printSize ("a (word32 * word32 * word64) array of length 10", | |
83 | Array.tabulate (10, fn i => (Word32.fromInt i, | |
84 | Word32.fromInt (i + 1), | |
85 | Word64.fromInt i))) | |
86 | ; printSize ("a (word64 * word64) array of length 10", | |
87 | Array.tabulate (10, fn i => (Word64.fromInt (i + 1), | |
88 | Word64.fromInt i))) | |
89 | ; printSize ("a (word16 * word64) array of length 10", | |
90 | Array.tabulate (10, fn i => (Word16.fromInt (i + 1), | |
91 | Word64.fromInt i))) | |
92 | ; printSize ("an array of length 10 of 2-ples of ints", | |
93 | Array.tabulate (10, fn i => (i, i + 1))) | |
94 | ; printSize ("an array of length 10 of 2-ples of (shared) ints", | |
95 | let val t = (0, 1) in | |
96 | Array.tabulate (10, fn _ => t) | |
97 | end) | |
98 | ; printSize ("an array of length 10 of arrays of length 20 of ints", | |
99 | Array.tabulate (10, fn i => Array.tabulate (20, fn j => i + j))) | |
100 | ; printSize ("an array of length 10 of (shared) arrays of length 20 of ints", | |
101 | let val a = Array.tabulate (20, fn j => j) | |
102 | in Array.tabulate (10, fn i => a) | |
103 | end) | |
104 | ; printSize ("an array of length 10 of tuples of word16 * (arrays of length 20 of ints)", | |
105 | Array.tabulate (10, fn i => (Word16.fromInt i, Array.tabulate (20, fn j => i + j)))) | |
106 | ; printSize ("an array of length 10 of tuples of word32 * (arrays of length 20 of ints)", | |
107 | Array.tabulate (10, fn i => (Word32.fromInt i, Array.tabulate (20, fn j => i + j)))) | |
108 | ; printSize ("an array of length 10 of tuples of word64 * (arrays of length 20 of ints)", | |
109 | Array.tabulate (10, fn i => (Word64.fromInt i, Array.tabulate (20, fn j => i + j)))) | |
110 | ; printSize ("an array of length 10 of tuples of real32 * (arrays of length 20 of ints)", | |
111 | Array.tabulate (10, fn i => (Real32.fromInt i, Array.tabulate (20, fn j => i + j)))) | |
112 | ; printSize ("an array of length 10 of tuples of real64 * (arrays of length 20 of ints)", | |
113 | Array.tabulate (10, fn i => (Real64.fromInt i, Array.tabulate (20, fn j => i + j)))) | |
114 | ; printSize ("a useless function", fn _ => 13) | |
115 | ; printSize ("an empty string", "") | |
116 | ; () | |
117 | ) handle _ => (r0 := (); r8 := 0wx1; r16 := 0wx1; r32 := 0wx1; r64 := 0wx1; rul := []; rbl := []; rdl := []) | |
118 | ||
119 | (* This is here so that the list is "useful". | |
120 | * If it were removed, then the optimizer (remove-unused-constructors) | |
121 | * would remove l entirely. | |
122 | *) | |
123 | val _ = if 10 = foldl (op +) 0 l | |
124 | then () | |
125 | else raise Fail "bug" | |
126 | ||
127 | val _ = if ! r0 = () andalso | |
128 | ! r8 = 0wx0 andalso | |
129 | ! r16 = 0wx0 andalso | |
130 | ! r32 = 0wx0 andalso | |
131 | ! r64 = 0wx0 andalso | |
132 | ! rul = ul andalso | |
133 | ! rbl = bl andalso | |
134 | ! rdl = dl andalso | |
135 | true | |
136 | then () | |
137 | else raise Fail "bug" |