-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgol_sim_naiveLib.sml
333 lines (288 loc) · 11.7 KB
/
gol_sim_naiveLib.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
local open HolKernel gol_simLib in
structure gol_sim_naiveLib = struct
fun check_mask1 [] [] = true
| check_mask1 (a::xs) [] = false
| check_mask1 [] (b::bs) = false
| check_mask1 (a::xs) (b::bs) = (b orelse a = False) andalso check_mask1 xs bs
fun check_mask [] [] = true
| check_mask (a::xs) [] = false
| check_mask [] (b::bs) = false
| check_mask (a::xs) (b::bs) = check_mask1 a b andalso check_mask xs bs
fun b2n true = 1
| b2n false = 0
fun ALOOKUP [] a = NONE
| ALOOKUP ((x,y)::xs) a = if a = x then SOME y else ALOOKUP xs a
fun eval_bexp env True = true
| eval_bexp env False = false
| eval_bexp env (Not x) = not (eval_bexp env x)
| eval_bexp env (And (x, y)) = (eval_bexp env x andalso eval_bexp env y)
| eval_bexp env (Or (x, y)) = (eval_bexp env x orelse eval_bexp env y)
| eval_bexp env (Var (v, n)) = case ALOOKUP env (v,n) of SOME b => b | NONE => false
fun eval_bexp8 env (y1,y2,y3,y4,y5,y6,y7,y8) =
b2n (eval_bexp env y1) +
b2n (eval_bexp env y2) +
b2n (eval_bexp env y3) +
b2n (eval_bexp env y4) +
b2n (eval_bexp env y5) +
b2n (eval_bexp env y6) +
b2n (eval_bexp env y7) +
b2n (eval_bexp env y8)
fun count_falses x (y1,y2,y3,y4,y5,y6,y7,y8) =
b2n (x = False) +
b2n (y1 = False) +
b2n (y2 = False) +
b2n (y3 = False) +
b2n (y4 = False) +
b2n (y5 = False) +
b2n (y6 = False) +
b2n (y7 = False) +
b2n (y8 = False)
fun bvar_lt (n1,g1) (n2,g2) =
if n1 = n2 then g1 < g2 else n1 = 0
fun add_to_sorted [] v = [v]
| add_to_sorted (x::xs) v =
if bvar_lt x v then x :: add_to_sorted xs v else
if x = v then x :: xs else v :: x :: xs
fun get_bvars False acc = acc
| get_bvars True acc = acc
| get_bvars (Not x) acc = get_bvars x acc
| get_bvars (Or (x, y)) acc = get_bvars x (get_bvars y acc)
| get_bvars (And (x, y)) acc = get_bvars x (get_bvars y acc)
| get_bvars (Var (n, g)) acc = add_to_sorted acc (n,g)
fun get_bvars8 (y1,y2,y3,y4,y5,y6,y7,y8) =
(get_bvars y1 $ get_bvars y2 $ get_bvars y3 $ get_bvars y4 $
get_bvars y5 $ get_bvars y6 $ get_bvars y7 $ get_bvars y8 [])
fun build_Not x =
case x of
True => False
| False => True
| Not y => y
| _ => Not x
fun build_If x y z =
if y = z then y else
if y = True andalso z = False then x else
if y = False andalso z = True then build_Not x else
if z = False then And (x, y) else
if y = True then Or (x, z) else
if z = True then Or (y, build_Not x) else
if y = False then And (z, build_Not x) else
Or (And (x, y), And (build_Not x, z))
fun to_bexp true = True
| to_bexp false = False
(* val stat = ref (Array.fromList ([]:int list)) *)
fun gol_eval vars env x ys =
case vars of
((n,g)::vs) =>
build_If (Var (n, g))
(gol_eval vs (((n,g),true)::env) x ys)
(gol_eval vs (((n,g),false)::env) x ys)
| [] => let
val k = eval_bexp8 env ys
val mid = eval_bexp env x
in to_bexp (if mid then (k = 2 orelse k = 3) else (k = 3)) end
val count = ref Time.zeroTime
fun gol_cell x ys =
if count_falses x ys >= 7 then False else let
val vars = get_bvars x (get_bvars8 ys)
(* val timer = Timer.startCPUTimer ()
val _ = if length vars > 15 then
(PolyML.print vars; ())
else ()
val _ = Array.update (!stat, length vars, Array.sub (!stat, length vars) + 1) *)
val res = gol_eval vars [] x ys
(* val {usr, ...} = Timer.checkCPUTimer timer *)
(* val _ = count := !count + usr *)
in res end
fun hd_or_false [] = False
| hd_or_false (x::xs) = x
fun gol_row x0 (x1::xs)
y0 (y1::ys)
z0 (z1::zs) =
gol_cell y1 (x0, x1, hd_or_false xs,
y0, hd_or_false ys,
z0, z1, hd_or_false zs) ::
gol_row x1 xs
y1 ys
z1 zs
| gol_row _ _ _ _ _ _ = []
fun REPLICATE n v = List.tabulate (n, fn _ => v)
fun gol_rows prev (row :: rest) =
gol_row False prev
False row
False (case rest of
[] => REPLICATE (length row) False
| r :: _ => r)
:: gol_rows row rest
| gol_rows prev [] = []
fun gol_step_rows [] = []
| gol_step_rows (x::xs) = let
(* val _ = (count := Time.zeroTime; stat := Array.array (30, 0)) *)
val res = gol_rows (REPLICATE (length x) False) (x::xs)
(* val _ = say ("gol_step_rows: " ^ time_to_string (!count) ^ "\n")
val _ = PolyML.print (!stat) *)
in res end
fun gol_checked_steps 0 rows mask = SOME rows
| gol_checked_steps n rows mask =
if n = 0 then SOME rows else (
(* print "start\n"; *)
if check_mask rows mask then let
(* val _ = print "checked\n" *)
val rows' = time gol_step_rows rows
(* val _ = print "step\n" *)
in gol_checked_steps (n - 1) rows' mask end
else NONE)
fun or xss yss =
map2 (fn xs => fn ys => map2 (fn x => fn y => x orelse y) xs ys) xss yss
fun add_margin fill n xss = let
val ys = REPLICATE n fill
val yss = map (fn xs => ys @ xs @ ys) xss
val l = (case yss of (row::_) => length row | _ => n+n)
val ts = REPLICATE n (REPLICATE l fill)
in ts @ yss @ ts end
fun make_base_area w h = let
val trues = REPLICATE (h * 150) (REPLICATE (w * 150) true)
in add_margin false 10 trues end
fun or_box_row x w [] = []
| or_box_row x w (r::rs) =
if x = 0 then if w = 0 then r :: rs else true :: or_box_row x (w-1) rs
else r :: or_box_row (x-1) w rs
fun or_box x y w h [] = []
| or_box x y w h (r::rs) =
if y = 0 then
if h = 0 then r :: rs else
or_box_row x w r :: or_box x y w (h-1) rs
else
r :: or_box x (y-1) w h rs
fun or_io_areas [] t = t
| or_io_areas (((x,y),_,_)::rest) t =
or_box (85 + 75 * x - 6) (85 + 75 * y - 6) 12 12
(or_io_areas rest t)
fun diff (xss : bool list list) (yss : bool list list) =
map2 (fn xs => fn ys => map2 (fn x => fn y => if y then false else x) xs ys) xss yss
fun masks w h ins outs = let
val base_area_bools = make_base_area w h
val bools = REPLICATE (150 * h + 20) (REPLICATE (150 * w + 20) false)
val sets1 = or_io_areas (filter is_ns ins @ filter is_ew outs) bools
val sets2 = or_io_areas (filter is_ew ins @ filter is_ns outs) bools
in
(or sets2 (diff base_area_bools sets1),
or sets1 (diff base_area_bools sets2))
end
fun shrink_row (x1::x2::x3::xs)
(y1::y2::y3::ys)
(z1::z2::z3::zs) =
(x1 andalso x2 andalso x3 andalso
y1 andalso y2 andalso y3 andalso
z1 andalso z2 andalso z3) :: shrink_row (x2::x3::xs) (y2::y3::ys) (z2::z3::zs)
| shrink_row _ _ _ = []
fun shrink_all (r1::r2::r3::rest) =
shrink_row r1 r2 r3 :: shrink_all (r2::r3::rest)
| shrink_all _ = []
fun shrink xs = add_margin false 1 (shrink_all xs)
fun fromVector v = Vector.foldr (fn (a, r) => a :: r) [] v
fun simulation_test1 w h (ins:io_port list) (outs:io_port list) rows n = let
val rows = map fromVector (fromVector rows)
val (m1,m2) = masks w h ins outs
val res = Option.valOf $ gol_checked_steps n rows (shrink m1)
in Vector.fromList (map Vector.fromList res) end
fun diff_rows (rows : bexp list list) (bools: bool list list) =
map2 (map2 (fn r => fn b => if b then False else r)) rows bools
fun inter_rows (rows : bexp list list) (bools: bool list list) =
map2 (map2 (fn r => fn b => if b then r else False)) rows bools
fun build_Or x y =
if x = True then True else
if y = True then True else
if x = False then y else
if y = False then x else
Or (x, y)
fun or_row x p [] = []
| or_row x [] row = row
| or_row x (p::pat) (r::row) =
if x = 0 then build_Or p r :: or_row x pat row else
r :: or_row (x-1) (p::pat) row
fun or_at x y pat [] = []
| or_at x y [] (row::rows) = row::rows
| or_at x y (p::pat) (row::rows) =
if y = 0 then or_row x p row :: or_at x y pat rows else
row :: or_at x (y-1) (p::pat) rows
fun io_gate E = [
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,true ,false,false,true ,false],
[false,false,false,false,false,false,false,false,false,true ],
[false,false,false,false,false,true ,false,false,false,true ],
[false,false,false,false,false,false,true ,true ,true ,true ],
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false]]
| io_gate W = [
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false],
[true ,true ,true ,true ,false,false,false,false,false,false],
[true ,false,false,false,true ,false,false,false,false,false],
[true ,false,false,false,false,false,false,false,false,false],
[false,true ,false,false,true ,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false]]
| io_gate N = [
[false,false,true ,true ,true ,false,false,false,false,false],
[false,true ,false,false,true ,false,false,false,false,false],
[false,false,false,false,true ,false,false,false,false,false],
[false,false,false,false,true ,false,false,false,false,false],
[false,true ,false,true ,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false]]
| io_gate S = [
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,false,false,false,false],
[false,false,false,false,false,false,true ,false,true ,false],
[false,false,false,false,false,true ,false,false,false,false],
[false,false,false,false,false,true ,false,false,false,false],
[false,false,false,false,false,true ,false,false,true ,false],
[false,false,false,false,false,true ,true ,true ,false,false]]
fun or_lwss rows [] = SOME rows
| or_lwss rows (((x,y),d,v)::rest) =
case or_lwss rows rest of
NONE => NONE
| SOME rows1 =>
SOME (or_at (x * 75 - 5 + 85) (y * 75 - 5 + 85)
(map (map (fn b => if b then v else False)) (io_gate d)) rows1)
fun inc_vars rows = map (map inc) rows
fun simulation_ok w h (ins:io_port list) (outs:io_port list) rows = let
val bools = REPLICATE (150 * h + 20) (REPLICATE (150 * w + 20) false)
val (m1,m2) = masks w h ins outs
val del1 = or_io_areas (filter is_ns outs) bools
val del2 = or_io_areas (filter is_ew outs) bools
val ins1 = filter is_ns ins
val ins2 = filter is_ew ins
val outs1 = filter is_ns outs
val outs2 = filter is_ew outs
val empty = REPLICATE (150 * h + 20) (REPLICATE (150 * w + 20) False)
in
case gol_checked_steps 30 rows (shrink m1) of
NONE => false
| SOME rows1 =>
if or_lwss empty outs1 <> SOME (inter_rows rows1 del1) then false else
case or_lwss (diff_rows rows1 del1) ins1 of
NONE => false
| SOME rowsA =>
case gol_checked_steps 30 rowsA (shrink m2) of
NONE => false
| SOME rows2 =>
if or_lwss empty outs2 <> SOME (inter_rows rows2 del2) then false else
case or_lwss (diff_rows rows2 del2) ins2 of
NONE => false
| SOME rowsB =>
inc_vars rows = rowsB
end
end
end