Debian Codegraph @

benchmarks.larceny/src/puzzle.sml

raw
1 (******************************************************************************
2 * File: puzzle.sch
3 * Description: PUZZLE benchmark
4 * Author: Richard Gabriel, after Forrest Baskett
5 * Created: 12-Apr-85
6 * Modified: 12-Apr-85 14:20:23 (Bob Shaw)
7 * 11-Aug-87 (Will Clinger)
8 * 22-Jan-88 (Will Clinger)
9 * 30-Mar-92 (Will Clinger -- empty list counts as true)
10 * 5-May-94 (Will Clinger -- translated into SML)
11 * Language: Standard ML
12 * Status: Public Domain
13 ******************************************************************************)
14
15 val size = 511
16 val classmax = 3
17 val typemax = 12
18
19 val iii = ref 0
20 val kount = ref 0
21 val d = 8
22
23 fun start () =
24 let open Array
25 val piececount = tabulate (classmax + 1, fn i => 0)
26 val class = tabulate (typemax + 1, fn i => 0)
27 val piecemax = tabulate (typemax + 1, fn i => 0)
28 val puzzle = tabulate (size + 1, fn i => true)
29 val p = tabulate (typemax + 1,
30 fn i => tabulate (size + 1,
31 fn i => false))
32
33 fun fit (i, j) =
34 let val endd = sub (piecemax, i)
35 fun loop k =
36 if (k > endd)
37 orelse ((sub (sub (p, i), k))
38 andalso sub (puzzle, j + k))
39 then (k > endd)
40 else loop (k + 1)
41 in loop 0
42 end
43
44 fun place (i, j) =
45 let val endd = sub (piecemax, i)
46 fun loop1 k =
47 if k > endd
48 then ()
49 else (if sub (sub (p, i), k)
50 then update (puzzle, j + k, true)
51 else ();
52 loop1 (k + 1))
53 fun loop2 k =
54 if (k > size) orelse not (sub (puzzle, k))
55 then ((* print "\nPuzzle filled"; *)
56 if k > size then 0 else k)
57 else loop2 (k + 1)
58 in (loop1 0;
59 update (piececount,
60 sub (class, i),
61 sub (piececount, sub (class, i)) - 1);
62 loop2 j)
63 end
64
65 fun puzzle_remove (i, j) =
66 let val endd = sub (piecemax, i)
67 fun loop k =
68 if k > endd
69 then ()
70 else (if sub (sub (p, i), k)
71 then update (puzzle, j + k, false)
72 else ();
73 loop (k + 1))
74 in (loop 0;
75 update (piececount,
76 sub (class, i),
77 sub (piececount, sub (class, i)) + 1))
78 end
79
80 (*
81 * fun trial_output (x:int, y:int) =
82 * (print "\nPiece ";
83 * print (Int.toString x);
84 * print " at ";
85 * print(Int.toString y);
86 * print ".")
87 *)
88
89 fun trial j =
90 let val k = ref 0
91 fun loop i =
92 if i > typemax
93 then (kount := !kount + 1;
94 false)
95 else if not (0 = sub (piececount, sub (class, i)))
96 then if fit (i, j)
97 then (k := place (i, j);
98 if (trial (!k)) orelse (!k = 0)
99 then ( (* trial_output (i+1, !k+1);
100 *)
101 kount := !kount + 1;
102 true)
103 else (puzzle_remove (i, j);
104 loop (i + 1)))
105 else loop (i + 1)
106 else loop (i + 1)
107 in loop 0
108 end
109
110 fun definePiece (iclass, ii, jj, kk) =
111 let val index = ref 0
112 fun loopi i =
113 if i > ii
114 then ()
115 else (let fun loopj j =
116 if j > jj
117 then ()
118 else (let fun loopk k =
119 if k > kk
120 then ()
121 else (index := i + d * (j + (d * k));
122 update (sub (p, !iii),
123 !index,
124 true);
125 loopk (k + 1))
126 in loopk 0
127 end;
128 loopj (j + 1))
129 in loopj 0
130 end;
131 loopi (i + 1))
132 in (loopi 0;
133 update (class, !iii, iclass);
134 update (piecemax, !iii, !index);
135 if not (!iii = typemax)
136 then iii := !iii + 1
137 else ())
138 end
139
140 fun start () =
141 let fun loop1 m =
142 if m > size
143 then ()
144 else (update (puzzle, m, true);
145 loop1 (m + 1))
146 fun loop2 i =
147 if i > 5
148 then ()
149 else (let fun loopj j =
150 if j > 5
151 then ()
152 else (let fun loopk k =
153 if k > 5
154 then ()
155 else (update (puzzle,
156 i +
157 (d * (j + (d * k))),
158 false);
159 loopk (k + 1))
160 in loopk 1
161 end;
162 loopj (j + 1))
163 in loopj 1
164 end;
165 loop2 (i + 1))
166 fun loop3 i =
167 if i > typemax
168 then ()
169 else (let fun loopm m =
170 if m > size
171 then ()
172 else (update (sub (p, i), m, false);
173 loopm (m + 1))
174 in loopm 0
175 end;
176 loop3 (i + 1))
177 in (kount := 0;
178
179 loop1 0;
180 loop2 1;
181 loop3 0;
182 iii := 0;
183
184 definePiece (0, 3, 1, 0);
185 definePiece (0, 1, 0, 3);
186 definePiece (0, 0, 3, 1);
187 definePiece (0, 1, 3, 0);
188 definePiece (0, 3, 0, 1);
189 definePiece (0, 0, 1, 3);
190
191 definePiece (1, 2, 0, 0);
192 definePiece (1, 0, 2, 0);
193 definePiece (1, 0, 0, 2);
194
195 definePiece (2, 1, 1, 0);
196 definePiece (2, 1, 0, 1);
197 definePiece (2, 0, 1, 1);
198
199 definePiece (3, 1, 1, 1);
200
201 update (piececount, 0, 13);
202 update (piececount, 1, 3);
203 update (piececount, 2, 1);
204 update (piececount, 3, 1);
205
206 let val m = (d * (d + 1)) + 1
207 val n = ref 0
208 in (if fit (0, m)
209 then n := place (0, m)
210 else print "\nError.";
211 if trial (!n)
212 then ((*
213 print "\nSuccess in ";
214 print (Int.toString (!kount));
215 print " trials."
216 *))
217 else print "\nFailure.")
218 end)
219 end
220 in start()
221 end
222
223 fun puzzle_benchmark (n) =
224 run_benchmark ("puzzle", n, fn () => start(), fn (x) => !kount = 2005)
225
226 fun main () = puzzle_benchmark (puzzle_iters)
227