-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathefficient_ancestor.ML
427 lines (318 loc) · 12.1 KB
/
efficient_ancestor.ML
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
(* Test that words can handle numbers between 0 and 31 *)
val _ = if 5 <= Word.wordSize then () else raise (Fail ("wordSize less than 5"));
structure Uint32 : sig
val set_bit : Word32.word -> IntInf.int -> bool -> Word32.word
val shiftl : Word32.word -> IntInf.int -> Word32.word
val shiftr : Word32.word -> IntInf.int -> Word32.word
val shiftr_signed : Word32.word -> IntInf.int -> Word32.word
val test_bit : Word32.word -> IntInf.int -> bool
end = struct
fun set_bit x n b =
let val mask = Word32.<< (0wx1, Word.fromLargeInt (IntInf.toLarge n))
in if b then Word32.orb (x, mask)
else Word32.andb (x, Word32.notb mask)
end
fun shiftl x n =
Word32.<< (x, Word.fromLargeInt (IntInf.toLarge n))
fun shiftr x n =
Word32.>> (x, Word.fromLargeInt (IntInf.toLarge n))
fun shiftr_signed x n =
Word32.~>> (x, Word.fromLargeInt (IntInf.toLarge n))
fun test_bit x n =
Word32.andb (x, Word32.<< (0wx1, Word.fromLargeInt (IntInf.toLarge n))) <> Word32.fromInt 0
end; (* struct Uint32 *)
structure STArray = struct
datatype 'a Cell = Invalid | Value of 'a array;
exception AccessedOldVersion;
type 'a array = 'a Cell Unsynchronized.ref;
fun fromList l = Unsynchronized.ref (Value (Array.fromList l));
fun array (size, v) = Unsynchronized.ref (Value (Array.array (size,v)));
fun tabulate (size, f) = Unsynchronized.ref (Value (Array.tabulate(size, f)));
fun sub (Unsynchronized.ref Invalid, idx) = raise AccessedOldVersion |
sub (Unsynchronized.ref (Value a), idx) = Array.sub (a,idx);
fun update (aref,idx,v) =
case aref of
(Unsynchronized.ref Invalid) => raise AccessedOldVersion |
(Unsynchronized.ref (Value a)) => (
aref := Invalid;
Array.update (a,idx,v);
Unsynchronized.ref (Value a)
);
fun length (Unsynchronized.ref Invalid) = raise AccessedOldVersion |
length (Unsynchronized.ref (Value a)) = Array.length a
fun grow (aref, i, x) = case aref of
(Unsynchronized.ref Invalid) => raise AccessedOldVersion |
(Unsynchronized.ref (Value a)) => (
let val len=Array.length a;
val na = Array.array (len+i,x)
in
aref := Invalid;
Array.copy {src=a, dst=na, di=0};
Unsynchronized.ref (Value na)
end
);
fun shrink (aref, sz) = case aref of
(Unsynchronized.ref Invalid) => raise AccessedOldVersion |
(Unsynchronized.ref (Value a)) => (
if sz > Array.length a then
raise Size
else (
aref:=Invalid;
Unsynchronized.ref (Value (Array.tabulate (sz,fn i => Array.sub (a,i))))
)
);
structure IsabelleMapping = struct
type 'a ArrayType = 'a array;
fun new_array (a:'a) (n:IntInf.int) = array (IntInf.toInt n, a);
fun array_length (a:'a ArrayType) = IntInf.fromInt (length a);
fun array_get (a:'a ArrayType) (i:IntInf.int) = sub (a, IntInf.toInt i);
fun array_set (a:'a ArrayType) (i:IntInf.int) (e:'a) = update (a, IntInf.toInt i, e);
fun array_of_list (xs:'a list) = fromList xs;
fun array_grow (a:'a ArrayType) (i:IntInf.int) (x:'a) = grow (a, IntInf.toInt i, x);
fun array_shrink (a:'a ArrayType) (sz:IntInf.int) = shrink (a,IntInf.toInt sz);
end;
end;
structure FArray = struct
datatype 'a Cell = Value of 'a Array.array | Upd of (int*'a*'a Cell Unsynchronized.ref);
type 'a array = 'a Cell Unsynchronized.ref;
fun array (size,v) = Unsynchronized.ref (Value (Array.array (size,v)));
fun tabulate (size, f) = Unsynchronized.ref (Value (Array.tabulate(size, f)));
fun fromList l = Unsynchronized.ref (Value (Array.fromList l));
fun sub (Unsynchronized.ref (Value a), idx) = Array.sub (a,idx) |
sub (Unsynchronized.ref (Upd (i,v,cr)),idx) =
if i=idx then v
else sub (cr,idx);
fun length (Unsynchronized.ref (Value a)) = Array.length a |
length (Unsynchronized.ref (Upd (i,v,cr))) = length cr;
fun realize_aux (aref, v) =
case aref of
(Unsynchronized.ref (Value a)) => (
let
val len = Array.length a;
val a' = Array.array (len,v);
in
Array.copy {src=a, dst=a', di=0};
Unsynchronized.ref (Value a')
end
) |
(Unsynchronized.ref (Upd (i,v,cr))) => (
let val res=realize_aux (cr,v) in
case res of
(Unsynchronized.ref (Value a)) => (Array.update (a,i,v); res)
end
);
fun realize aref =
case aref of
(Unsynchronized.ref (Value _)) => aref |
(Unsynchronized.ref (Upd (i,v,cr))) => realize_aux(aref,v);
fun update (aref,idx,v) =
case aref of
(Unsynchronized.ref (Value a)) => (
let val nref=Unsynchronized.ref (Value a) in
aref := Upd (idx,Array.sub(a,idx),nref);
Array.update (a,idx,v);
nref
end
) |
(Unsynchronized.ref (Upd _)) =>
let val ra = realize_aux(aref,v) in
case ra of
(Unsynchronized.ref (Value a)) => Array.update (a,idx,v);
ra
end
;
fun grow (aref, inc, x) = case aref of
(Unsynchronized.ref (Value a)) => (
let val len=Array.length a;
val na = Array.array (len+inc,x)
in
Array.copy {src=a, dst=na, di=0};
Unsynchronized.ref (Value na)
end
)
| (Unsynchronized.ref (Upd _)) => (
grow (realize aref, inc, x)
);
fun shrink (aref, sz) = case aref of
(Unsynchronized.ref (Value a)) => (
if sz > Array.length a then
raise Size
else (
Unsynchronized.ref (Value (Array.tabulate (sz,fn i => Array.sub (a,i))))
)
) |
(Unsynchronized.ref (Upd _)) => (
shrink (realize aref,sz)
);
structure IsabelleMapping = struct
type 'a ArrayType = 'a array;
fun new_array (a:'a) (n:IntInf.int) = array (IntInf.toInt n, a);
fun array_length (a:'a ArrayType) = IntInf.fromInt (length a);
fun array_get (a:'a ArrayType) (i:IntInf.int) = sub (a, IntInf.toInt i);
fun array_set (a:'a ArrayType) (i:IntInf.int) (e:'a) = update (a, IntInf.toInt i, e);
fun array_of_list (xs:'a list) = fromList xs;
fun array_grow (a:'a ArrayType) (i:IntInf.int) (x:'a) = grow (a, IntInf.toInt i, x);
fun array_shrink (a:'a ArrayType) (sz:IntInf.int) = shrink (a,IntInf.toInt sz);
fun array_get_oo (d:'a) (a:'a ArrayType) (i:IntInf.int) =
sub (a,IntInf.toInt i) handle Subscript => d
fun array_set_oo (d:(unit->'a ArrayType)) (a:'a ArrayType) (i:IntInf.int) (e:'a) =
update (a, IntInf.toInt i, e) handle Subscript => d ()
end;
end;
structure Bits_Integer : sig
val set_bit : IntInf.int -> IntInf.int -> bool -> IntInf.int
val shiftl : IntInf.int -> IntInf.int -> IntInf.int
val shiftr : IntInf.int -> IntInf.int -> IntInf.int
val test_bit : IntInf.int -> IntInf.int -> bool
end = struct
val maxWord = IntInf.pow (2, Word.wordSize);
fun set_bit x n b =
if n < maxWord then
if b then IntInf.orb (x, IntInf.<< (1, Word.fromLargeInt (IntInf.toLarge n)))
else IntInf.andb (x, IntInf.notb (IntInf.<< (1, Word.fromLargeInt (IntInf.toLarge n))))
else raise (Fail ("Bit index too large: " ^ IntInf.toString n));
fun shiftl x n =
if n < maxWord then IntInf.<< (x, Word.fromLargeInt (IntInf.toLarge n))
else raise (Fail ("Shift operand too large: " ^ IntInf.toString n));
fun shiftr x n =
if n < maxWord then IntInf.~>> (x, Word.fromLargeInt (IntInf.toLarge n))
else raise (Fail ("Shift operand too large: " ^ IntInf.toString n));
fun test_bit x n =
if n < maxWord then IntInf.andb (x, IntInf.<< (1, Word.fromLargeInt (IntInf.toLarge n))) <> 0
else raise (Fail ("Bit index too large: " ^ IntInf.toString n));
end; (*struct Bits_Integer*)
structure HOL : sig
type 'a equal
type 'a itself
val eq : 'a equal -> 'a -> 'a -> bool
end = struct
type 'a equal = {equal : 'a -> 'a -> bool};
val equal = #equal : 'a equal -> 'a -> 'a -> bool;
datatype 'a itself = Type;
fun eq A_ a b = equal A_ a b;
end; (*struct HOL*)
structure Map : sig
val map_of : 'a HOL.equal -> ('a * 'b) list -> 'a -> 'b option
end = struct
fun map_of A_ ((l, v) :: ps) k =
(if HOL.eq A_ l k then SOME v else map_of A_ ps k)
| map_of A_ [] k = NONE;
end; (*struct Map*)
structure Orderings : sig
type 'a ord
val less_eq : 'a ord -> 'a -> 'a -> bool
val less : 'a ord -> 'a -> 'a -> bool
type 'a preorder
val ord_preorder : 'a preorder -> 'a ord
type 'a order
val preorder_order : 'a order -> 'a preorder
type 'a linorder
val order_linorder : 'a linorder -> 'a order
end = struct
type 'a ord = {less_eq : 'a -> 'a -> bool, less : 'a -> 'a -> bool};
val less_eq = #less_eq : 'a ord -> 'a -> 'a -> bool;
val less = #less : 'a ord -> 'a -> 'a -> bool;
type 'a preorder = {ord_preorder : 'a ord};
val ord_preorder = #ord_preorder : 'a preorder -> 'a ord;
type 'a order = {preorder_order : 'a preorder};
val preorder_order = #preorder_order : 'a order -> 'a preorder;
type 'a linorder = {order_linorder : 'a order};
val order_linorder = #order_linorder : 'a linorder -> 'a order;
end; (*struct Orderings*)
structure RBT_Impl : sig
type color
type ('a, 'b) rbt
val rbt_lookup : 'a Orderings.ord -> ('a, 'b) rbt -> 'a -> 'b option
end = struct
datatype color = R | B;
datatype ('a, 'b) rbt = Empty |
Branch of color * ('a, 'b) rbt * 'a * 'b * ('a, 'b) rbt;
fun rbt_lookup A_ Empty k = NONE
| rbt_lookup A_ (Branch (uu, l, x, y, r)) k =
(if Orderings.less A_ k x then rbt_lookup A_ l k
else (if Orderings.less A_ x k then rbt_lookup A_ r k else SOME y));
end; (*struct RBT_Impl*)
structure RBT : sig
type ('b, 'a) rbt
val lookup : 'a Orderings.linorder -> ('a, 'b) rbt -> 'a -> 'b option
end = struct
datatype ('b, 'a) rbt = RBT of ('b, 'a) RBT_Impl.rbt;
fun impl_of B_ (RBT x) = x;
fun lookup A_ x =
RBT_Impl.rbt_lookup
((Orderings.ord_preorder o Orderings.preorder_order o
Orderings.order_linorder)
A_)
(impl_of A_ x);
end; (*struct RBT*)
structure Arith : sig
type nat
end = struct
datatype nat = Nat of IntInf.int;
end; (*struct Arith*)
structure HashCode : sig
type 'a hashable
val hashcode : 'a hashable -> 'a -> Word32.word
val def_hashmap_size : 'a hashable -> 'a HOL.itself -> Arith.nat
end = struct
type 'a hashable =
{hashcode : 'a -> Word32.word, def_hashmap_size : 'a HOL.itself -> Arith.nat};
val hashcode = #hashcode : 'a hashable -> 'a -> Word32.word;
val def_hashmap_size = #def_hashmap_size :
'a hashable -> 'a HOL.itself -> Arith.nat;
end; (*struct HashCode*)
structure Assoc_List : sig
type ('b, 'a) assoc_list
val lookup : 'a HOL.equal -> ('a, 'b) assoc_list -> 'a -> 'b option
end = struct
datatype ('b, 'a) assoc_list = Assoc_List of ('b * 'a) list;
fun impl_of (Assoc_List x) = x;
fun lookup A_ al = Map.map_of A_ (impl_of al);
end; (*struct Assoc_List*)
structure Uint32a : sig
val linorder_uint32 : Word32.word Orderings.linorder
end = struct
val ord_uint32 =
{less_eq = (fn a => fn b => Word32.<= (a, b)),
less = (fn a => fn b => Word32.< (a, b))}
: Word32.word Orderings.ord;
val preorder_uint32 = {ord_preorder = ord_uint32} :
Word32.word Orderings.preorder;
val order_uint32 = {preorder_order = preorder_uint32} :
Word32.word Orderings.order;
val linorder_uint32 = {order_linorder = order_uint32} :
Word32.word Orderings.linorder;
end; (*struct Uint32a*)
structure HashMap_Impl : sig
val lookup :
'a HOL.equal * 'a HashCode.hashable ->
'a -> (Word32.word, ('a, 'b) Assoc_List.assoc_list) RBT.rbt -> 'b option
end = struct
fun lookup (A1_, A2_) k m =
(case RBT.lookup Uint32a.linorder_uint32 m (HashCode.hashcode A2_ k)
of NONE => NONE | SOME lm => Assoc_List.lookup A1_ lm k);
end; (*struct HashMap_Impl*)
structure HashMap : sig
type ('b, 'a) hashmap
val hm_lookup :
'a HOL.equal * 'a HashCode.hashable -> 'a -> ('a, 'b) hashmap -> 'b option
end = struct
datatype ('b, 'a) hashmap =
RBT_HM of (Word32.word, ('b, 'a) Assoc_List.assoc_list) RBT.rbt;
fun impl_of_RBT_HM B_ (RBT_HM x) = x;
fun hm_lookup (A1_, A2_) k hm =
HashMap_Impl.lookup (A1_, A2_) k (impl_of_RBT_HM A2_ hm);
end; (*struct HashMap*)
structure Move : sig
val efficient_ancestor :
'a HOL.equal * 'a HashCode.hashable ->
('a, ('b * 'a)) HashMap.hashmap -> 'a -> 'a -> bool
end = struct
fun efficient_ancestor (A1_, A2_) t p c =
(case HashMap.hm_lookup (A1_, A2_) c t of NONE => false
| SOME a => let
val (_, aa) = a;
in
HOL.eq A1_ aa p orelse efficient_ancestor (A1_, A2_) t p aa
end);
end; (*struct Move*)