-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp.ml
executable file
·1334 lines (1152 loc) · 42 KB
/
exp.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* Poling: Proof Of Linearizability Generator
* Poling is built on top of CAVE and shares the same license with CAVE
* See LICENSE.txt for license.
* Contact: He Zhu, Department of Computer Science, Purdue University
* Email: zhu103@purdue.edu
*)
(******************************************************************************)
(* __ ___ CAVE: Concurrent Algorithm VErifier *)
(* / /\ \ / | Copyright (c) 2010, Viktor Vafeiadis *)
(* | /--\ \ / |--- *)
(* \__ / \ \/ |___ See LICENSE.txt for license. *)
(* *)
(******************************************************************************)
open Misc
(* -------------------------------------------------------------------------- *)
(** {2 Data types} *)
(* -------------------------------------------------------------------------- *)
type sfn1 =
| Sfn_NOT
| Sfn_item
| Sfn_sorted
| Sfn_hd
| Sfn_tl
| Sfn_set_of_list
| Sfn_can_return
type sfn2 =
| Sfn_list_lt
| Sfn_subset
| Sfn_set_minus
type sfn =
| Sfn_list
| Sfn_set
| Sfn_AND
| Sfn_OR
| Sfn_XOR
| Sfn_undef
type sfn_grp =
| Sfn_plus
| Sfn_pos
(** parameters for identifiers, in priority order *)
type id_kind = EXIST_RENAMED | EXIST_ORIG | NORMAL_RENAMED | NORMAL_ORIG
type id_internal1 = id_kind
type id_internal2 = int
type id_internal3 = string
(** NB: Expressions are kept in a canonical form.
Do not create them directly: use the constructors instead.
*)
type exp =
| Enum of int
| Eident of id_internal1 * id_internal2 * id_internal3
| Eeq of exp * exp
| Efun1 of sfn1 * exp
| Efun2 of sfn2 * exp * exp
| Efun of sfn * exp list
| Egrp of sfn_grp * int * ie_list (** Abelian group *)
and ie_list =
| IEnil
| IEcons of exp * int * ie_list
(* -------------------------------------------------------------------------- *)
(** {2 Built-in functions} *)
(* -------------------------------------------------------------------------- *)
let string_of_sfn1 = function
| Sfn_NOT -> "!"
| Sfn_item -> "@item"
| Sfn_sorted -> "@sorted"
| Sfn_hd -> "@hd"
| Sfn_tl -> "@tl"
| Sfn_set_of_list -> "@set_of_list"
| Sfn_can_return -> "@can_return"
let string_of_sfn2 = function
| Sfn_list_lt -> "@lt"
| Sfn_subset -> "@subset"
| Sfn_set_minus -> "@minus"
let string_of_sfn = function
| Sfn_list -> "@list"
| Sfn_set -> "@set"
| Sfn_AND -> "@and"
| Sfn_OR -> "@or"
| Sfn_XOR -> "@xor"
| Sfn_undef -> "@undef"
let string_of_sfn_grp = function
| Sfn_plus -> "@plus"
| Sfn_pos -> "@pos"
(* -------------------------------------------------------------------------- *)
(** {2 Equality and comparison } *)
(* -------------------------------------------------------------------------- *)
let rec equal_exp e1 e2 =
e1==e2 || begin match e1 with
| Enum n1 -> (match e2 with Enum n2 -> n1 = n2 | _ -> false)
| Eident _ -> false
| Eeq (e1,f1) ->
(match e2 with Eeq (e2,f2) ->
equal_exp e1 e2 && equal_exp f1 f2
| _ -> false)
| Efun1 (s1,e1) ->
(match e2 with Efun1 (s2,e2) -> s1==s2 && equal_exp e1 e2
| _ -> false)
| Efun2 (s1,e1,f1) ->
(match e2 with Efun2 (s2,e2,f2) ->
s1==s2 && equal_exp e1 e2 && equal_exp f1 f2
| _ -> false)
| Efun (s1,el1) ->
(match e2 with Efun (s2,el2) ->
s1==s2 && equal_exp_list el1 el2
| _ -> false)
| Egrp (s1,n1,nel1) ->
(match e2 with Egrp (s2,n2,nel2) ->
Pervasives.(=) s1 s2 && n1=n2 && equal_ie_list nel1 nel2
| _ -> false)
end
and equal_exp_list el1 el2 = match el1,el2 with
| [],[] -> true
| [],_ -> false
| _,[] -> false
| e1::el1',e2::el2' -> equal_exp e1 e2 && equal_exp_list el1' el2'
and equal_ie_list el1 el2 = match el1,el2 with
| IEnil,IEnil -> true
| IEnil,_ -> false
| _,IEnil -> false
| IEcons(e1,i1,el1'),IEcons (e2,i2,el2') ->
i1==i2 && equal_exp e1 e2 && equal_ie_list el1' el2'
let compare_int (i1: int) (i2: int) =
Pervasives.compare i1 i2
let compare_small_int x y =
Obj.magic x - (Obj.magic y : int)
let rec compare_exp e1 e2 =
if e1==e2 then 0 else
let n = compare_small_int (Obj.tag (Obj.repr e1)) (Obj.tag (Obj.repr e2)) in
if n <> 0 then n else match e1,e2 with
| Enum n1, Enum n2 -> compare_int n1 n2
| Eident (kind1,id1,_), Eident (kind2,id2,_) ->
let n = compare_small_int kind1 kind2 in if n<>0 then n else
id2 - id1
| Eeq (e1,f1), Eeq (e2,f2) ->
let n = compare_exp e1 e2 in if n<>0 then n else
compare_exp f1 f2
| Efun1 (s1,e1), Efun1 (s2,e2) ->
let n = compare_small_int s1 s2 in if n<>0 then n else
compare_exp e1 e2
| Efun2 (s1,e1,f1), Efun2 (s2,e2,f2) ->
let n = compare_small_int s1 s2 in if n<>0 then n else
let n = compare_exp e1 e2 in if n<>0 then n else
compare_exp f1 f2
| Efun (s1,el1), Efun (s2,el2) ->
let n = compare_small_int s1 s2 in if n<>0 then n else
compare_exp_list el1 el2
| Egrp (s1,n1,nel1), Egrp (s2,n2,nel2) ->
let n = compare_small_int s1 s2 in if n<>0 then n else
let n = compare_int n1 n2 in if n<>0 then n else
compare_ie_list nel1 nel2
| _ -> assert false
and compare_exp_list el1 el2 = match el1,el2 with
| [],[] -> 0
| [],_ -> -1
| _,[] -> 1
| e1::el1',e2::el2' ->
let n = compare_exp e1 e2 in if n<>0 then n else
compare_exp_list el1' el2'
and compare_ie_list nel1 nel2 = match nel1,nel2 with
| IEnil,IEnil -> 0
| IEnil,_ -> -1
| _,IEnil -> 1
| IEcons(e1,n1,el1'),IEcons(e2,n2,el2') ->
let n = compare_exp e1 e2 in if n<>0 then n else
let n = compare_int n1 n2 in if n<>0 then n else
compare_ie_list el1' el2'
let rec abs_compare_exp e1 e2 =
if e1==e2 then 0 else
let n = compare_small_int (Obj.tag (Obj.repr e1)) (Obj.tag (Obj.repr e2)) in
if n <> 0 then n else match e1,e2 with
| Enum n1, Enum n2 -> compare_int n1 n2
| Eident (kind1, id1, _), Eident (kind2, id2, _) ->
let n = compare_small_int kind1 kind2 in if n<>0 then n else
if kind1==EXIST_RENAMED || kind1==EXIST_ORIG then 0 else
id2 - id1
| Eeq (e1,f1), Eeq (e2,f2) ->
let n = abs_compare_exp e1 e2 in if n<>0 then n else
abs_compare_exp f1 f2
| Efun1 (s1,e1), Efun1 (s2,e2) ->
let n = compare_small_int s1 s2 in if n<>0 then n else
abs_compare_exp e1 e2
| Efun2 (s1,e1,f1), Efun2 (s2,e2,f2) ->
let n = compare_small_int s1 s2 in if n<>0 then n else
let n = abs_compare_exp e1 e2 in if n<>0 then n else
abs_compare_exp f1 f2
| Efun (s1,el1), Efun (s2,el2) ->
let n = compare_small_int s1 s2 in if n<>0 then n else
abs_compare_exp_list el1 el2
| Egrp (s1,n1,nel1), Egrp (s2,n2,nel2) ->
let n = compare_small_int s1 s2 in if n<>0 then n else
let n = compare_int n1 n2 in if n<>0 then n else
abs_compare_ie_list nel1 nel2
| _ -> assert false
and abs_compare_exp_list el1 el2 = match el1,el2 with
| [],[] -> 0
| [],_ -> -1
| _,[] -> 1
| e1::el1',e2::el2' ->
let n = abs_compare_exp e1 e2 in if n<>0 then n else
abs_compare_exp_list el1' el2'
and abs_compare_ie_list nel1 nel2 = match nel1,nel2 with
| IEnil,IEnil -> 0
| IEnil,_ -> -1
| _,IEnil -> 1
| IEcons(e1,n1,el1'),IEcons(e2,n2,el2') ->
let n = abs_compare_exp e1 e2 in if n<>0 then n else
let n = compare_int n1 n2 in if n<>0 then n else
abs_compare_ie_list el1' el2'
(* -------------------------------------------------------------------------- *)
(** {2 Identifiers } *)
(* -------------------------------------------------------------------------- *)
module Id = struct
(** Always of the form [Eident ...] *)
type t = exp
let of_exp e =
assert (match e with Eident _ -> true | _ -> false);
e
let unique_id_count = ref 0
let create_ident_ht = Hashtbl.create 251
let get_unique_id () =
let _ = incr unique_id_count in
!unique_id_count
let get_id_kind (x: t) = (Obj.obj (Obj.field (Obj.repr x) 0) : id_kind)
let get_id_uid (x: t) = (Obj.obj (Obj.field (Obj.repr x) 1) : int)
let get_id_name (x: t) = (Obj.obj (Obj.field (Obj.repr x) 2) : string)
(* generate fresh identifiers *)
let gensym_str s =
assert (String.length s <> 0);
let count = get_unique_id () in
Eident (NORMAL_RENAMED, count, s)
let gensym_str_ex s =
assert (String.length s <> 0);
let count = get_unique_id () in
Eident (EXIST_RENAMED, count, s)
let gensym_norm id = gensym_str (get_id_name id)
let gensym_garb id = gensym_str_ex (get_id_name id)
let tempid () = gensym_str "temporary"
let existential_name s =
assert (String.length s <> 0);
String.length s <> 0 && String.get s 0 = '_'
let un_EX s =
try String.sub s 1 (String.length s - 1)
with Not_found -> assert false
let is_ex_kind = function
| EXIST_ORIG | EXIST_RENAMED -> true
| NORMAL_ORIG | NORMAL_RENAMED -> false
let create s =
assert (String.length s <> 0);
try
Hashtbl.find create_ident_ht s
with Not_found ->
let unique_id = get_unique_id () in
let res =
if existential_name s
then Eident (EXIST_ORIG, unique_id, un_EX s)
else Eident (NORMAL_ORIG, unique_id, s) in
Hashtbl.add create_ident_ht s res;
res
let tid = create "TID"
let mytid = create "myTID"
let result = create "Result"
let junk = create "JUNK"
let string_of_id_internal kind uid name = match kind with
| EXIST_ORIG -> "_" ^ name
| EXIST_RENAMED -> "_" ^ name ^ "_" ^ string_of_int uid
| NORMAL_ORIG -> name
| NORMAL_RENAMED -> name ^ "_" ^ string_of_int uid
let to_string = function
| Eident (kind, uid, name) -> string_of_id_internal kind uid name
| _ -> assert false
let is_existential id =
is_ex_kind (get_id_kind id)
let to_unex_string id =
if (is_existential id) then un_EX (to_string id)
else to_string id
let is_no_junk_var i =
match (get_id_name i) with
| "" -> true
| "split" | "VAL" | "valsplit" -> not (is_existential i)
| s -> String.get s 0 != '.'
let compare i1 i2 =
let n = compare_small_int (get_id_kind i1) (get_id_kind i2) in
if n<>0 then n else
get_id_uid i2 - get_id_uid i1
end
module IdSet = Set.Make(Id)
module IdMap = Map.Make(Id)
(* -------------------------------------------------------------------------- *)
(** {2 Expression misc operations} *)
(* -------------------------------------------------------------------------- *)
let rec ie_fold f l acc = match l with
| IEnil -> acc
| IEcons(e,n,l) -> ie_fold f l (f e n acc)
let rec ie_fold_exp f l acc = match l with
| IEnil -> acc
| IEcons(e,_n,l) -> ie_fold_exp f l (f e acc)
let rec ie_forall_exp f l = match l with
| IEnil -> true
| IEcons(e,_n,l) -> f e && ie_forall_exp f l
let rec exp_fold f e acc = match e with
| Enum _
| Eident _ -> f e acc
| Eeq (e1,e2) -> exp_fold f e2 (exp_fold f e1 (f e acc))
| Efun1(_,e1) -> exp_fold f e1 (f e acc)
| Efun2(_,e1,e2) -> exp_fold f e2 (exp_fold f e1 (f e acc))
| Efun(_,el) -> List.fold (exp_fold f) el (f e acc)
| Egrp(_,_,nel) -> ie_fold_exp (exp_fold f) nel (f e acc)
let rec mem_exp y l = (* List.exists (equal_exp y) l *)
match l with
| [] -> false
| x :: l -> equal_exp y x || mem_exp y l
let remdup_exp l = match l with | [] | [_] -> l | _ ->
let l2 = List.stable_sort compare_exp l in
let rec remf = function
| x::(y::_ as xs) -> if equal_exp x y then remf xs else x::(remf xs)
| xs -> xs
in remf l2
let remdup_and_zero l = match l with
| [] -> l
| [Enum 0] -> []
| [_] -> l
| _ ->
let l2 = List.stable_sort compare_exp l in
let rec remf = function
| Enum 0 :: xs -> remf xs
| x::(y::_ as xs) -> if equal_exp x y then remf xs else x::(remf xs)
| xs -> xs in
remf l2
(** return [true] if expression is an existentially quantified identifier *)
let existential_id = function
| Eident (kind, _, _) -> Id.is_ex_kind kind
| _ -> false
let rec ie_negate nel = match nel with
| IEnil -> IEnil
| IEcons (e,n,nel) -> IEcons (e,-n,ie_negate nel)
let rec ie_mult_app m acc nel = match nel with
| IEnil -> acc
| IEcons (e,n,nel) -> ie_mult_app m ((e,m * n)::acc) nel
(* -------------------------------------------------------------------------- *)
(** {2 Expression constants} *)
(* -------------------------------------------------------------------------- *)
module E = struct
let zero = Enum 0
let one = Enum 1
let tid = Id.tid
let empty_list = Efun (Sfn_list, [])
let empty_set = Efun (Sfn_set, [])
let undef = Efun (Sfn_undef, [])
(* -------------------------------------------------------------------------- *)
(** {2 Expression constructors} *)
(* -------------------------------------------------------------------------- *)
(** NB: The constructors perform elaborate simplifications. *)
(** Constructor for [Enum n]. *)
let num n = match n with
| 0 -> zero
| 1 -> one
| n -> Enum n
(** Constructor for identifiers (identity function). *)
let id (i: Id.t) = (i: exp)
(** {3 Abelian groups} *)
(* -------------------------------------------------------------------------- *)
(** Constructor for [Egrp(Sfn_plus,n,nel)]. *)
let grp_plus n nel = match n,nel with
n, IEnil -> num n
| 0, IEcons(e,1,IEnil) -> e
| n, nel ->
let rec go n res = function (* combine nested functions *)
| IEnil -> (n, res)
| IEcons (Enum n',m,nel) -> go (n + (m * n')) res nel
| IEcons (Egrp(Sfn_plus,n',nel'),m,nel) ->
go (n + m * n') (ie_mult_app m res nel') nel
| IEcons (e,n',nel) -> go n ((e,n')::res) nel in
let rec go2 res = function (* put same terms together *)
| (e,0)::nel -> go2 res nel
| (e,n)::(e',m)::nel when equal_exp e e' ->
if n+m=0 then go2 res nel else go2 res ((e,n+m)::nel)
| (e,n)::nel -> go2 (IEcons(e,n,res)) nel
| [] -> res in
let (n,nel) = go n [] nel in
let nel = List.sort (fun (x,_) (y,_) -> -compare_exp x y) nel in
let nel = go2 IEnil nel in
match n,nel with
| n,IEnil -> num n
| 0,IEcons(e,1,IEnil) -> e
| _ -> Egrp(Sfn_plus,n,nel)
(** Constructor for [Egrp(Sfn_pos,n,nel)]. *)
let grp_pos n nel = match n,nel with
n, IEnil -> if n >= 0 then one else zero
| n, nel ->
let rec go n res = function (* combine nested functions *)
| IEnil -> (n, res)
| IEcons (Enum n',m,nel) -> go (n + (m * n')) res nel
| IEcons (Egrp(Sfn_plus,n',nel'),m,nel) ->
go (n + m * n') (ie_mult_app m res nel') nel
| IEcons (e,n',nel) -> go n ((e,n')::res) nel in
let rec go2 res = function (* put same terms together *)
| (e,0)::nel -> go2 res nel
| (e,n)::(e',m)::nel when equal_exp e e' ->
if n+m=0 then go2 res nel else go2 res ((e,n+m)::nel)
| (e,n)::nel -> go2 (IEcons(e,n,res)) nel
| [] -> res in
let (n,nel) = go n [] nel in
let nel = List.sort (fun (x,_) (y,_) -> -compare_exp x y) nel in
let nel = go2 IEnil nel in
match n,nel with
| n,IEnil -> if n >= 0 then one else zero
| _ -> Egrp(Sfn_pos,n,nel)
(** Constructor for [Egrp(i,n,nel)]. *)
let grp i n nel = match i with
| Sfn_plus -> grp_plus n nel
| Sfn_pos -> grp_pos n nel
let add e1 e2 = grp_plus 0 (IEcons(e1,1,IEcons(e2,1,IEnil)))
let sub e1 e2 = grp_plus 0 (IEcons(e1,1,IEcons(e2,-1,IEnil)))
(** {3 Boolean operations} *)
(* -------------------------------------------------------------------------- *)
let band el =
let rec concat_AND e res = match e with
| Enum n -> if n = 0 then [zero] else res
| Efun(Sfn_AND,el') -> List.fold concat_AND el' res
| e -> e::res in
match List.reduce concat_AND el with
| [] -> one
| [e] -> e
| el ->
if List.exists (fun x -> x==zero) el then zero
else Efun(Sfn_AND, remdup_exp el)
let lifted_AND e efn =
match e with
| Enum 0 -> e
| Enum _ -> efn ()
| _ -> band [e; efn ()]
let bor el =
let rec concat_OR e res = match e with
| Enum n -> if n = 0 then res else [one]
| Efun(Sfn_OR,el') -> List.fold concat_OR el' res
| e -> e::res in
match List.reduce concat_OR el with
| [] -> zero
| [e] -> e
| el ->
if List.exists (fun x -> x==one) el then one
else Efun(Sfn_OR, remdup_exp el)
let rec bnot e = match e with
| Enum n -> if n = 0 then one else zero
| Efun1 (Sfn_NOT,x) -> x
| Efun (Sfn_AND,x) -> bor (List.map bnot x)
| Efun (Sfn_OR,x) -> band (List.map bnot x)
(* *) | Eeq (Enum 0,
(( Efun2 (Sfn_subset, _, _)
| Efun (Sfn_AND, _)
| Efun (Sfn_OR, _)
| Efun1 (Sfn_NOT, _)
) as x)) -> x
(* *)
| Egrp (Sfn_pos,n,nel) -> Egrp (Sfn_pos,-n-1,ie_negate nel)
| _ -> Efun1 (Sfn_NOT, e)
(* bitwise *)
let xor el =
let rec concat_XOR n acc l = match l with
| [] -> (n,acc)
| Enum n' :: l -> concat_XOR (n lxor n') acc l
| Efun(Sfn_XOR,Enum n' :: el') :: l ->
concat_XOR (n lxor n') (List.rev_append el' acc) l
| Efun(Sfn_XOR,el') :: l ->
concat_XOR n (List.rev_append el' acc) l
| e :: l -> concat_XOR n (e::acc) l in
let rec remove_XOR l = match l with
| e :: e' :: l when equal_exp e e' -> remove_XOR l
| e :: l -> e :: remove_XOR l
| [] -> [] in
let (n,el) = concat_XOR 0 [] el in
let el = List.sort compare_exp el in
let el = remove_XOR el in
match n, el with
| n, [] -> num n
| 0, [e] -> e
| 0, el -> Efun(Sfn_XOR, el)
| n, el -> Efun(Sfn_XOR, num n :: el)
(** {3 Sequences and multisets} *)
(* -------------------------------------------------------------------------- *)
let item e = Efun1 (Sfn_item, e)
let list el =
let rec concat e res = match e with
| Efun(Sfn_list,el') -> List.fold concat el' res
| e -> e::res in
match List.reduce concat el with
| [] -> empty_list
| [e] -> e
| el -> Efun(Sfn_list, List.rev el)
let set el =
let rec concat e res = match e with
| Efun(Sfn_set,el') -> List.fold concat el' res
| e -> e::res in
match List.reduce concat el with
| [] -> empty_set
| [e] -> e
| el -> Efun(Sfn_set, List.sort compare_exp el)
(** {3 Equality} *)
(* -------------------------------------------------------------------------- *)
let eq_simp e1 e2 =
let n = compare_exp e1 e2 in
if n < 0 then Eeq(e1,e2)
else if n = 0 then one
else Eeq(e2,e1)
(** Constructor for equalities *)
let rec eq e1 e2 =
if e1==e2 then one else
match (e1,e2) with
| Enum n, Enum m -> if n=m then one else zero
| Efun1(Sfn_item,e1), Efun1(Sfn_item,e2) -> eq e1 e2
| Efun1(Sfn_item, _), Efun(Sfn_list,el2) -> list_eq [e1] el2
| Efun1(Sfn_item, _), Efun(Sfn_set,el2) -> set_eq [e1] el2
| Efun(Sfn_list,el1), Efun1(Sfn_item, _) -> list_eq el1 [e2]
| Efun(Sfn_list,el1), Efun(Sfn_list,el2) -> list_eq el1 el2
| Efun(Sfn_set, el1), Efun1(Sfn_item, _) -> set_eq el1 [e2]
| Efun(Sfn_set, el1), Efun(Sfn_set, el2) -> set_eq el1 el2
| Egrp _, _
| _, Egrp _ -> grp_eq e1 e2
| _ -> eq_simp e1 e2
and grp_eq e1 e2 =
match sub e1 e2 with
| Enum 0 -> one
| Enum _ -> zero
| (Eident _ | Efun _) as e -> eq_simp zero e
| Egrp(Sfn_plus,n,IEcons((Eident _ as e),1,nel)) ->
eq_simp e (grp_plus (-n) (ie_negate nel))
| Egrp(Sfn_plus,n,(IEcons((Eident _ as e),-1,nel))) ->
eq_simp e (grp_plus n nel)
| _ ->
match xor [e1;e2] with
| Enum 0 -> one
| Enum n -> zero
| Efun(Sfn_XOR,e1::el) -> eq_simp e1 (xor el)
| e -> eq_simp zero e
and list_eq el1 el2 =
let has_item =
List.exists (function Efun1(Sfn_item,_) -> true | _ -> false) in
let rec i_eq_l x el2 =
match el2 with
| [] -> one
| Efun1(Sfn_item,e2)::el2 ->
lifted_AND (eq x e2) (fun () -> list_eq [] el2)
| e2::el2 ->
lifted_AND (eq e2 empty_list) (fun () -> i_eq_l x el2) in
match (el1,el2) with
(* One or both lists are empty *)
| [], [] -> one
| [], el2 ->
if has_item el2 then zero
else band (List.map (eq_simp empty_list) el2)
| el1, [] ->
if has_item el1 then zero
else band (List.map (eq_simp empty_list) el1)
(* One or both lists start with @item *)
| (Efun1(Sfn_item,e1)::el1, Efun1(Sfn_item,e2)::el2) ->
lifted_AND (eq e1 e2) (fun () -> list_eq el1 el2)
| ([Efun1(Sfn_item,e1)], el2) when has_item el2 -> i_eq_l e1 el2
| (el1, [Efun1(Sfn_item,e2)]) when has_item el1 -> i_eq_l e2 el1
(* Simple cases *)
| ([e1], [e2]) -> eq e1 e2
| (e1::el1, e2::el2) when equal_exp e1 e2 -> list_eq el1 el2
| (el1, el2) ->
begin match (List.rev el1, List.rev el2) with
| (e1::el1, e2::el2) when equal_exp e1 e2 ->
list_eq (List.rev el1) (List.rev el2)
| (Efun1(Sfn_item,e1)::el1, Efun1(Sfn_item,e2)::el2) ->
lifted_AND (eq e1 e2)
(fun () -> list_eq (List.rev el1) (List.rev el2))
| _ ->
eq_simp (list el1) (list el2)
end
and set_eq el1 el2 =
let has_item =
List.exists (function Efun1(Sfn_item,_) -> true | _ -> false) in
let rec go el1a el2a el1 el2 =
match (el1, el2) with
| [], _ -> (el1a, List.rev_append el2a el2)
| _, [] -> (List.rev_append el1a el1, el2a)
| e1::el1b, e2::el2b ->
let n = compare_exp e1 e2 in
if n = 0 then go el1a el2a el1b el2b
else if n < 0 then go (e1::el1a) el2a el1b el2
else go el1a (e2::el2a) el1 el2b in
let (el1,el2) = go [] [] el1 el2 in
match (el1,el2) with
| ([],[]) -> one
| ([], xl) ->
if has_item xl then zero
else band (List.map (eq_simp empty_set) xl)
| (xl, []) ->
if has_item xl then zero
else band (List.map (eq_simp empty_set) xl)
| ([e1], [e2]) -> eq e1 e2
| (el1, el2) -> eq_simp (set el1) (set el2)
(** Constructor for [e1 != e2]. *)
let neq e1 e2 = bnot (eq e1 e2)
(** {3 Operations on sequences and multisets} *)
(* -------------------------------------------------------------------------- *)
let hd = function
| Efun1 (Sfn_item, e) -> e
| Efun (Sfn_list, Efun1 (Sfn_item, e)::_) -> e
| e -> Efun1 (Sfn_hd, e)
let tl = function
| Efun1 (Sfn_item, e) -> empty_list
| Efun (Sfn_list, Efun1 (Sfn_item,_)::l) -> list l
| Efun (Sfn_list, []) -> empty_list
| e -> Efun1 (Sfn_tl, e)
let set_of_list x = match x with
| Efun1 (Sfn_item, _) -> x
| Efun (Sfn_list, el) ->
let f y = match y with
| Efun1 (Sfn_item,_) -> y
| _ -> Efun1 (Sfn_set_of_list, y)
in
set (List.map f el)
| _ -> Efun1 (Sfn_set_of_list, x)
let list_lt e1 e2 =
let go_item e1 e2 r =
if equal_exp e1 e2 then
bor [eq e1 empty_list; eq e2 empty_list] :: r
else
match e1, e2 with
| (Efun1 (Sfn_item, Enum x), Efun1 (Sfn_item, Enum y)) ->
if x < y then r else [zero]
| _ -> Efun2 (Sfn_list_lt, e1, e2)::r
in
let l1 = match e1 with Efun(Sfn_list, xl) -> xl | x -> [x] in
let l2 = match e2 with Efun(Sfn_list, xl) -> xl | x -> [x] in
band (List.fold (fun x -> List.fold (go_item x) l2) l1 [])
let ltn e1 e2 =
(* grp_pos (-1) (IEcons (e1, -1, IEcons (e2, 1, IEnil))) *)
list_lt (Efun1 (Sfn_item, e1)) (Efun1 (Sfn_item, e2))
let leq e1 e2 = bnot (ltn e2 e1)
let rec sorted e = match e with
| Efun1 (Sfn_item, x) -> one
| Efun (Sfn_list, x) ->
begin match x with
| [] -> one
| x::xl ->
let x = list [x] in
let y = list xl in
band [sorted x; sorted y; list_lt x y]
end
| _ -> Efun1 (Sfn_sorted, e)
let set_minus x y =
let x = match x with Efun (Sfn_set,el) -> el | _ -> [x] in
let y = match y with Efun (Sfn_set,el) -> el | _ -> [y] in
let rec go_minus acc_x acc_y xl yl = match xl, yl with
| x::xl1, y::yl1 ->
let n = compare_exp x y in
if n < 0 then go_minus (x::acc_x) acc_y xl1 yl
else if n = 0 then go_minus acc_x acc_y xl1 yl1
else go_minus acc_x (y::acc_y) xl yl1
| [], _::_ -> Efun2 (Sfn_set_minus, set (List.rev acc_x),
set (List.rev_append acc_y yl))
| _, [] ->
begin match acc_y with
| [] -> set (List.rev_append acc_x xl)
| _ -> Efun2 (Sfn_set_minus, set (List.rev_append acc_x xl),
set (List.rev acc_y))
end in
go_minus [] [] x y
(* TODO -- unsound for multisets *)
let subset x y =
let x = match x with Efun (Sfn_set,el) -> el | _ -> [x] in
let y = match y with Efun (Sfn_set,el) -> el | _ -> [y] in
let x = List.filter (fun e -> not (List.exists (equal_exp e) y)) x in
begin match (x, y) with
| ([], _) -> one
| (_::_, []) -> zero
| ([Efun1 (Sfn_item, x)], [Efun1 (Sfn_item, y)]) -> eq x y
| ([Efun1 (Sfn_item, x) as v], _) ->
bor (List.map (function
| Efun1 (Sfn_item, y) -> eq x y
| y -> Efun2 (Sfn_subset, v, y)) y)
| _ -> Efun2 (Sfn_subset, set x, set y)
end
(** {3 Summary} *)
(* -------------------------------------------------------------------------- *)
(** Constructor for [Efun(s,el)]. *)
let funn s el = match s with
| Sfn_list -> list el
| Sfn_set -> set el
| Sfn_AND -> band el
| Sfn_OR -> bor el
| Sfn_XOR -> xor el
| Sfn_undef -> undef
let fun1 s e = match s with
| Sfn_NOT -> bnot e
| Sfn_hd -> hd e
| Sfn_tl -> tl e
| Sfn_set_of_list -> set_of_list e
| Sfn_sorted -> sorted e
| Sfn_item
| Sfn_can_return -> Efun1 (s, e)
let fun2 s e1 e2 = match s with
| Sfn_list_lt -> list_lt e1 e2
| Sfn_subset -> subset e1 e2
| Sfn_set_minus -> set_minus e1 e2
(* -------------------------------------------------------------------------- *)
(** {2 Free variables} *)
(* -------------------------------------------------------------------------- *)
let rec fv e acc = match e with
| Enum _ -> acc
| Eident _ -> IdSet.add e acc
| Eeq(e1,e2) -> fv e1 (fv e2 acc)
| Efun1(_,e1) -> fv e1 acc
| Efun2(_,e1,e2) -> fv e1 (fv e2 acc)
| Efun(_,el) -> List.fold fv el acc
| Egrp(_,_,nel) -> ie_fold_exp fv nel acc
let rec exfv e acc = match e with
| Enum _ -> acc
| Eident (kind,_,_) -> if Id.is_ex_kind kind then IdSet.add e acc else acc
| Eeq(e1,e2) -> exfv e1 (exfv e2 acc)
| Efun1(_,e1) -> exfv e1 acc
| Efun2(_,e1,e2) -> exfv e1 (exfv e2 acc)
| Efun(_,el) -> List.fold exfv el acc
| Egrp(_,_,nel) -> ie_fold_exp exfv nel acc
let rec fhd e acc = match e with
| Enum _ | Eident _ -> acc
| Eeq (e1,e2) -> fhd e1 (fhd e2 acc)
| Efun1 (Sfn_hd, (Eident _ as e')) -> IdSet.add e' acc
| Efun1(_,e1) -> fhd e1 acc
| Efun2(_,e1,e2) -> fhd e1 (fhd e2 acc)
| Efun (_,el) -> List.fold fhd el acc
| Egrp(_,_,nel) -> ie_fold_exp fhd nel acc
let rec forall_fv (f: Id.t -> bool) e = match e with
| Enum _ -> true
| Eident _ -> f e
| Eeq(e1,e2) -> forall_fv f e1 && forall_fv f e2
| Efun1(_,e1) -> forall_fv f e1
| Efun2(_,e1,e2) -> forall_fv f e1 && forall_fv f e2
| Efun(_,el) -> List.for_all (forall_fv f) el
| Egrp(_,_,nel) -> ie_forall_exp (forall_fv f) nel
let gensym_str s = (Id.gensym_str s : exp)
let gensym_str_ex s = (Id.gensym_str_ex s : exp)
end
(* -------------------------------------------------------------------------- *)
(** {2 Substitutions} *)
(* -------------------------------------------------------------------------- *)
type subst = exp -> exp
let rec map_sub f l = match l with
| [] -> l
| x::xl ->
let y = f x in
let yl = map_sub f xl in
if xl==yl && x==y then l else y::yl
(** FIXME: Do not implement Egrp subsitution *)
let rec map_id_exp_sub fsub e = match e with
| Enum _
| Eident _ -> (match fsub e with Some e -> e | None -> e)
| Eeq (e1,e2) -> Eeq (map_id_exp_sub fsub e1, map_id_exp_sub fsub e2)
| Efun1(x,e1) -> Efun1 (x, map_id_exp_sub fsub e1)
| Efun2(x,e1,e2) -> Efun2 (x, map_id_exp_sub fsub e1, map_id_exp_sub fsub e2)
| Efun(x,el) -> Efun (x, List.map (map_id_exp_sub fsub) el)
| Egrp(x,y,nel) -> e
let rec map_n_sub (f: subst) l = match l with
| IEnil -> l
| IEcons (x,n,xl) ->
let y = f x in
let yl = map_n_sub f xl in
if xl==yl && x==y then l else IEcons(y,n,yl)
let mk_fun_subst subf =
let rec ff_e e =
let e' = subf e in
if e'!=e then e'
else match e with
| Enum _ | Eident _ -> e
| Eeq (e1,e2) ->
let e1' = ff_e e1 in
let e2' = ff_e e2 in
if e1'==e1 && e2'==e2 then e else E.eq e1' e2'
| Efun1 (i,e1) ->
let e1' = ff_e e1 in
if e1'==e1 then e else E.fun1 i e1'
| Efun2 (i,e1,e2) ->
let e1' = ff_e e1 in
let e2' = ff_e e2 in
if e1'==e1 && e2'==e2 then e else E.fun2 i e1' e2'
| Efun (i,el) ->
let el' = map_sub ff_e el in
if el' == el then e else E.funn i el'
| Egrp(op,n,nel) ->
let nel' = map_n_sub ff_e nel in
if nel' == nel then e else E.grp op n nel'
in ff_e
let mk_single_subst i' e' =
let rec sf_e e = match e with
| Enum _ -> e
| Eident _ -> if e == i' then e' else e
| Eeq (e1,e2) ->
let e1' = sf_e e1 in
let e2' = sf_e e2 in
if e1'==e1 && e2'==e2 then e else E.eq e1' e2'
| Efun1 (i,e1) ->
let e1' = sf_e e1 in
if e1'==e1 then e else E.fun1 i e1'
| Efun2 (i,e1,e2) ->
let e1' = sf_e e1 in
let e2' = sf_e e2 in
if e1'==e1 && e2'==e2 then e else E.fun2 i e1' e2'
| Efun (i,el) ->
let el' = map_sub sf_e el in
if el' == el then e else E.funn i el'
| Egrp(op,n,nel) ->
let nel' = map_n_sub sf_e nel in
if nel' == nel then e else E.grp op n nel'
in sf_e
let mk_id_subst (f : Id.t -> exp) =
let rec f_e e = match e with
| Enum _ -> e
| Eident _ -> f e
| Eeq (e1,e2) ->
let e1' = f_e e1 in
let e2' = f_e e2 in
if e1'==e1 && e2'==e2 then e else E.eq e1' e2'
| Efun1 (i,e1) ->
let e1' = f_e e1 in
if e1'==e1 then e else E.fun1 i e1'
| Efun2 (i,e1,e2) ->
let e1' = f_e e1 in
let e2' = f_e e2 in
if e1'==e1 && e2'==e2 then e else E.fun2 i e1' e2'
| Efun (i,el) ->
let el' = map_sub f_e el in
if el' == el then e else E.funn i el'
| Egrp(op,n,nel) ->
let nel' = map_n_sub f_e nel in
if nel' == nel then e else E.grp op n nel'
in f_e
let rec opt_findq l x = match l with
| PCons (k, v, ln) ->
if k == x then v else opt_findq ln x
| PNil -> E.id x
let rec mk_idl_subst l e = match e with
| Enum _ -> e
| Eident _ -> opt_findq l e
| Eeq (e1,e2) ->
let e1' = mk_idl_subst l e1 in
let e2' = mk_idl_subst l e2 in
if e1'==e1 && e2'==e2 then e else E.eq e1' e2'
| Efun1 (i,e1) ->
let e1' = mk_idl_subst l e1 in
if e1'==e1 then e else E.fun1 i e1'
| Efun2 (i,e1,e2) ->
let e1' = mk_idl_subst l e1 in
let e2' = mk_idl_subst l e2 in
if e1'==e1 && e2'==e2 then e else E.fun2 i e1' e2'
| Efun (i,el) ->
let el' = map_sub (fun x -> mk_idl_subst l x) el in
if el' == el then e else E.funn i el'
| Egrp(op,n,nel) ->
let nel' = map_n_sub (fun x -> mk_idl_subst l x) nel in
if nel' == nel then e else E.grp op n nel'
let rec mk_subst (l: (Id.t, exp) plist) = match l with
| PNil -> identity
| PCons(i', e', PNil) -> mk_single_subst i' e'
| _ -> mk_idl_subst l
let mk_gensym_garb_subst i =
mk_single_subst i (Id.gensym_garb i)
let mk_gensym_garb_subst_idset l =
let l' = IdSet.fold (fun i r -> PCons(i, Id.gensym_garb i, r)) l PNil in
mk_subst l'
let mk_gensym_garb_subst_idset2 l =
let (l1, l2) =
IdSet.fold
(fun i (r1,r2) ->
let j = Id.gensym_garb i in