-
Notifications
You must be signed in to change notification settings - Fork 6
/
ext_tla.ml
1634 lines (1513 loc) · 60.6 KB
/
ext_tla.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
(* Copyright 2008 INRIA *)
Version.add "$Id$";;
(* Extension for TLA+ : set theory. *)
open Printf;;
open Expr;;
open Misc;;
open Mlproof;;
open Node;;
open Phrase;;
let add_formula e = ();;
let remove_formula e = ();;
let tla_set_constructors = [
(* "TLA.BOOLEAN"; is an abbrev in Isabelle *)
"TLA.emptyset";
"TLA.upair";
"TLA.addElt";
"TLA.set";
"TLA.infinity";
"TLA.SUBSET";
"TLA.UNION";
"TLA.INTER";
"TLA.cup";
"TLA.cap";
"TLA.setminus";
"TLA.subsetOf";
"TLA.setOfAll";
"TLA.FuncSet";
"TLA.DOMAIN";
"TLA.Product";
"arith.N";
"arith.Z";
"arith.R";
"arith.natrange";
"arith.intrange";
"TLA.Seq";
"TLA.recordset";
];;
let tla_fcn_constructors = [
(* Note: $string is not here, even though strings are functions
Same for TLA.tuple
*)
"TLA.Fcn";
"TLA.except";
"TLA.oneArg";
"TLA.extend";
];;
let tla_other_symbols = [
"TLA.in";
"TLA.isAFcn";
"TLA.cond";
"TLA.CASE";
"TLA.tuple";
"TLA.record";
"TLA.fapply";
"TLA.box";
];;
let is_set_expr e =
match e with
| Evar (v, _) -> List.mem v tla_set_constructors
| Eapp (Evar(f,_), _, _) -> List.mem f tla_set_constructors
| _ -> false
;;
let is_fcn_expr e =
match e with
| Evar (v, _) -> List.mem v tla_fcn_constructors
| Eapp (Evar(f,_), _, _) -> List.mem f tla_fcn_constructors
| _ -> false
;;
let is_notequiv e =
match e with
| Eapp (Evar("$notequiv",_), _, _) -> true
| _ -> false
;;
let mkbranches hs = Array.of_list (List.map (fun x -> [x]) hs);;
let rec decompose_add e =
match e with
| Eapp (Evar("TLA.addElt",_), [e1; e2], _) ->
let (l, rest) = decompose_add e2 in (e1 :: l, rest)
| _ -> ([], e)
;;
let rec get_values_set e =
match e with
| Evar ("TLA.emptyset", _) -> ([], false)
| Eapp (Evar("TLA.upair",_), [e1; e2], _) -> ([e1; e2], false)
| Eapp (Evar("TLA.set",_), l, _) -> (l, false)
| Eapp (Evar("TLA.addElt",_), [e1; e2], _) ->
let (vs, rest) = get_values_set e2 in
(e1 :: vs, rest)
| Eapp (Evar("TLA.union",_), [e1; e2], _) ->
let (vs1, rest1) = get_values_set e1 in
let (vs2, rest2) = get_values_set e2 in
(vs1 @ vs2, rest1 || rest2)
(*| Eapp ("TLA.FuncSet", ...) -> cross-product TODO? *)
| _ -> ([], true)
;;
let is_var e = match e with Evar _ -> true | _ -> false;;
let rec succ_nat n accu =
if n <= 0
then accu
else succ_nat (n-1) (eapp (tvar_none "TLA.fapply", [tvar_none "TLA.Succ"; accu]))
;;
let mk_nat n = succ_nat n (tvar_none "0");;
let is_string e = match e with Eapp (Evar("$string",_), _, _) -> true | _ -> false;;
let trivially_notin e1 e2 =
match e1, e2 with
| Eapp (Evar("$string",_), _, _), Eapp (Evar("TLA.set",_), elements, _) ->
let f x = is_string x && not (Expr.equal x e1) in
List.for_all f elements
| _ -> false
;;
let rec mk_pairs l =
match l with
| [] -> []
| a :: b :: t -> (a, b) :: (mk_pairs t)
| _ -> Error.warn "record or record set with odd number of fields"; []
;;
let rec check_record_labels l =
match l with
| (Eapp (Evar("$string",_), [Evar (l1, _)], _), _)
:: (Eapp (Evar("$string",_), [Evar (l2, _)], _), _) :: _
when l1 = l2 ->
Error.warn (sprintf "duplicate record field %s" l1);
raise (Invalid_argument "check_record_labels")
| (l1, _) :: (l2, _) :: t when Expr.equal l1 l2 ->
Error.warn "duplicate record field (non-string)";
raise (Invalid_argument "check_record_labels")
| _ :: t -> check_record_labels t
| [] -> ()
;;
let get_record_labels l =
list_sort_unique Expr.compare (List.map fst (mk_pairs l))
;;
let field_trivially_notin rtype (lbl, e) =
try trivially_notin e (List.assq lbl rtype)
with Not_found -> true
;;
let newnodes_prop e g =
let mknode prio name args branches =
[ Node {
nconc = [e];
nrule = Ext ("tla", name, args);
nprio = prio;
ngoal = g;
nbranches = branches;
} ]
in
let mknode_cut prio name args branches =
[ Node {
nconc = [];
nrule = Ext ("tla", name, args);
nprio = prio;
ngoal = g;
nbranches = branches;
} ]
in
let mknode_inst prio m term =
match m with
| Eall (v, p, _) ->
let n = Expr.substitute [(v, term)] p in
[ Node {
nconc = [m];
nrule = All (m, term);
nprio = prio;
ngoal = g;
nbranches = [| [n] |];
} ]
| Eex (v, p, _) ->
let n = Expr.substitute [(v, term)] (enot p) in
let nm = enot (m) in
[ Node {
nconc = [nm];
nrule = NotEx (nm, term);
nprio = prio;
ngoal = g;
nbranches = [| [n] |];
} ]
| _ -> assert false
in
match e with
| Eapp (Evar("=",_), [e1; Etrue], _) ->
mknode Prop "eq_x_true" [e; e1; e1] [| [e1] |]
| Eapp (Evar("=",_), [Etrue; e1], _) ->
mknode Prop "eq_true_x" [e; e1; e1] [| [e1] |]
| Enot (Eapp (Evar("=",_), [e1; Etrue], _), _) ->
let h1 = enot (e1) in
mknode Prop "noteq_x_true" [e; h1; e1] [| [h1] |]
| Enot (Eapp (Evar("=",_), [Etrue; e1], _), _) ->
let h1 = enot (e1) in
mknode Prop "noteq_true_x" [e; h1; e1] [| [h1] |]
| Eapp (Evar("=",_), [e1; Efalse], _) ->
let h = enot (e1) in
mknode Prop "eq_x_false" [e; h; e1] [| [h] |]
| Eapp (Evar("=",_), [Efalse; e1], _) ->
let h = enot (e1) in
mknode Prop "eq_false_x" [e; h; e1] [| [h] |]
| Emeta (e1, _) ->
mknode_inst Arity e1 efalse
| Enot (Emeta (e1, _), _) ->
mknode_inst Arity e1 etrue
| Eapp (Evar("TLA.in",_), [e1; Evar ("TLA.emptyset", _)], _) ->
mknode Prop "in_emptyset" [e; e1] [| |]
| Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.upair",_), [e2; e3], _)], _) ->
let h1 = eeq e1 e2 in
let h2 = eeq e1 e3 in
mknode Prop "in_upair" [e; h1; h2; e1; e2; e3] [| [h1]; [h2] |]
| Enot (Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.upair",_), [e2; e3], _)], _), _) ->
let h1 = enot (eeq e1 e2) in
let h2 = enot (eeq e1 e3) in
mknode Prop "notin_upair" [e; h1; h2; e1; e2; e3] [| [h1; h2] |]
| Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.addElt",_), [e2; e3], _) as s], _) ->
let (elems, rest) = decompose_add s in
let helems = List.map (fun x -> eeq e1 x) elems in
let hs =
if Expr.equal rest (tvar_none "TLA.emptyset") then helems
else helems @ [eapp (tvar_none "TLA.in", [e1; rest])]
in
mknode Prop "in_addElt" [e; e1; s] (mkbranches hs)
| Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.set",_), elems, _) as s], _) ->
let helems = List.map (fun x -> eeq e1 x) elems in
mknode Prop "in_set" [e; e1; s] (mkbranches helems)
| Enot (Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.addElt",_), [e2; e3], _) as s], _), _) ->
let (elems, rest) = decompose_add s in
let helems = List.map (fun x -> enot (eeq e1 x)) elems in
let hs =
if Expr.equal rest (tvar_none "TLA.emptyset") then helems
else enot (eapp (tvar_none "TLA.in", [e1; rest])) :: helems
in
mknode Prop "notin_addElt" [e; e1; s] [| hs |]
| Enot (Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.set",_), elems, _) as s], _), _) ->
let helems = List.map (fun x -> enot (eeq e1 x)) elems in
mknode Prop "notin_set" [e; e1; s] [| helems |]
| Enot (Eapp (Evar("TLA.in",_), [e1; Emeta (m, _)], _), _) ->
mknode_inst (Inst m) m (eapp (tvar_none "TLA.set", [e1]))
(* infinity -- needed ? *)
| Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.SUBSET",_), [s], _)], _) ->
let h1 = eapp (tvar_none "TLA.subseteq", [e1; s]) in
mknode Prop "in_SUBSET" [e; h1; e1; s] [| [h1] |]
| Enot (Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.SUBSET",_), [s], _)], _), _) ->
let h1 = enot (eapp (tvar_none "TLA.subseteq", [e1; s])) in
mknode Prop "notin_SUBSET" [e; h1; e1; s] [| [h1] |]
| Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.UNION",_), [s], _)], _) ->
let b = Expr.newtvar type_none in
let h1 = eex (b, eand (eapp (tvar_none "TLA.in", [b; s]),
eapp (tvar_none "TLA.in", [e1; b]))) in
mknode Prop "in_UNION" [e; h1; e1; s] [| [h1] |]
| Enot (Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.UNION",_), [s], _)], _), _) ->
let b = Expr.newtvar type_none in
let h1 = enot (eex (b, eand (eapp (tvar_none "TLA.in", [b; s]),
eapp (tvar_none "TLA.in", [e1; b])))) in
mknode Arity "notin_UNION" [e; h1; e1; s] [| [h1] |]
(* INTER -- needed ? *)
| Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.cup",_), [e2; e3], _)], _) ->
let h1 = eapp (tvar_none "TLA.in", [e1; e2]) in
let h2 = eapp (tvar_none "TLA.in", [e1; e3]) in
mknode Prop "in_cup" [e; h1; h2; e1; e2; e3] [| [h1]; [h2] |]
| Enot (Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.cup",_), [e2; e3], _)], _), _) ->
let h1 = enot (eapp (tvar_none "TLA.in", [e1; e2])) in
let h2 = enot (eapp (tvar_none "TLA.in", [e1; e3])) in
mknode Prop "notin_cup" [e; h1; h2; e1; e2; e3] [| [h1; h2] |]
| Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.cap",_), [e2; e3], _)], _) ->
let h1 = eapp (tvar_none "TLA.in", [e1; e2]) in
let h2 = eapp (tvar_none "TLA.in", [e1; e3]) in
mknode Prop "in_cap" [e; h1; h2; e1; e2; e3] [| [h1; h2] |]
| Enot (Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.cap",_), [e2; e3], _)], _), _) ->
let h1 = enot (eapp (tvar_none "TLA.in", [e1; e2])) in
let h2 = enot (eapp (tvar_none "TLA.in", [e1; e3])) in
mknode Prop "notin_cap" [e; h1; h2; e1; e2; e3] [| [h1]; [h2] |]
| Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.setminus",_), [e2; e3], _)], _) ->
let h1 = eapp (tvar_none "TLA.in", [e1; e2]) in
let h2 = enot (eapp (tvar_none "TLA.in", [e1; e3])) in
mknode Prop "in_setminus" [e; h1; h2; e1; e2; e3] [| [h1; h2] |]
| Enot (Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.setminus",_), [e2; e3], _)], _), _) ->
let h1 = enot (eapp (tvar_none "TLA.in", [e1; e2])) in
let h2 = eapp (tvar_none "TLA.in", [e1; e3]) in
mknode Prop "notin_setminus" [e; h1; h2; e1; e2; e3] [| [h1]; [h2] |]
| Eapp (Evar("TLA.in",_),
[e1; Eapp (Evar("TLA.subsetOf",_), [s; Elam (v, p, _) as pred], _)],
_) ->
let h1 = eapp (tvar_none "TLA.in", [e1; s]) in
let h2 = substitute [(v, e1)] p in
mknode Prop "in_subsetof" [e; h1; h2; e1; s; pred] [| [h1; h2] |]
| Enot (Eapp (Evar("TLA.in",_),
[e1; Eapp (Evar("TLA.subsetOf",_), [s; Elam (v, p, _) as pred], _)],
_), _) ->
let h1 = enot (eapp (tvar_none "TLA.in", [e1; s])) in
let h2 = enot (substitute [(v, e1)] p) in
mknode Prop "notin_subsetof" [e; h1; h2; e1; s; pred] [| [h1]; [h2] |]
| Eapp (Evar("TLA.in",_),
[e1; Eapp (Evar("TLA.setOfAll",_), [s; Elam (v, p, _) as pred], _)],
_) ->
let x = Expr.newtvar type_none in
let h1 = eex (x, eand (eapp (tvar_none "TLA.in", [x; s]),
eeq e1 (substitute [(v, x)] p)))
in
mknode (Inst h1) "in_setofall" [e; h1; e1; s; pred] [| [h1] |]
| Enot (Eapp (Evar("TLA.in",_),
[e1; Eapp (Evar("TLA.setOfAll",_), [s; Elam (v, p, _) as pred], _)],
_), _) ->
let x = Expr.newtvar type_none in
let h1 = enot (eex (x, eand (eapp (tvar_none "TLA.in", [x; s]),
eeq e1 (substitute [(v, x)] p))))
in
mknode (Inst h1) "notin_setofall" [e; h1; e1; s; pred] [| [h1] |]
| Eapp (Evar("TLA.in",_), [f; Eapp (Evar("TLA.FuncSet",_), [a; b], _)], _) ->
let h1 = eapp (tvar_none "TLA.isAFcn", [f]) in
let h2 = eeq (eapp (tvar_none "TLA.DOMAIN", [f])) a in
let x = Expr.newtvar type_none in
let h3 = eall (x,
eimply (eapp (tvar_none "TLA.in", [x; a]),
eapp (tvar_none "TLA.in", [eapp (tvar_none "TLA.fapply", [f; x]); b])))
in
mknode Arity "in_funcset" [e; h1; h2; h3; f; a; b] [| [h1; h2] |] @
mknode (Inst h3) "in_funcset" [e; h1; h2; h3; f; a; b] [| [h1; h2; h3] |]
| Enot (Eapp (Evar("TLA.in",_), [f; Eapp (Evar("TLA.FuncSet",_), [a; b], _)], _), _) ->
let h1 = enot (eapp (tvar_none "TLA.isAFcn", [f])) in
let h2 = enot (eeq (eapp (tvar_none "TLA.DOMAIN", [f])) a) in
let x = Expr.newtvar type_none in
let h3 = enot (
eall (x,
eimply (eapp (tvar_none "TLA.in", [x; a]),
eapp (tvar_none "TLA.in", [eapp (tvar_none "TLA.fapply", [f; x]); b]))))
in
let prio =
match f with
| Eapp (Evar(s,_), _, _) when List.mem s tla_fcn_constructors -> Arity
| _ -> Inst h3
in
let shortcut =
match f with
| Eapp (Evar("TLA.except",_), [f1; v; e1], _) ->
let h1 = enot (eapp (tvar_none "TLA.in", [f1; eapp (tvar_none "TLA.FuncSet", [a; b])])) in
let h2 = enot (eapp (tvar_none "TLA.in", [e1; b])) in
mknode Arity "except_notin_funcset" [e; h1; h2; f1; v; e1; a; b]
[| [h1]; [h2] |]
| _ -> []
in
shortcut @ mknode prio "notin_funcset" [e; h1; h2; h3; f; a; b]
[| [h1]; [h2]; [h3] |]
| Eapp (Evar("=",_), [e1; Evar ("TLA.emptyset", _)], _) ->
let x = Expr.newtvar type_none in
let h = eall (x, enot (eapp (tvar_none "TLA.in", [x; e1]))) in
mknode Arity "setequalempty" [e; h; e1] [| [h] |]
| Eapp (Evar("=",_), [Evar ("TLA.emptyset", _); e1], _) ->
let x = Expr.newtvar type_none in
let h = eall (x, enot (eapp (tvar_none "TLA.in", [x; e1]))) in
mknode Arity "setemptyequal" [e; h; e1] [| [h] |]
(* redundant ?
| Eapp ("=", [e1; e2], _) when is_set_expr e1 || is_set_expr e2 ->
let x = Expr.newtvar type_none in
let h = eall (x, "", eequiv (eapp ("TLA.in", [x; e1]),
eapp ("TLA.in", [x; e2])))
in
mknode (Inst h) "setequal" [e; h; e1; e2] [| [h] |]
*)
| Eapp (Evar("=",_), [e1; e2], _) when is_set_expr e1 || is_set_expr e2 ->
let x = Expr.newtvar type_none in
let h = eall (x, eequiv (eapp (tvar_none "TLA.in", [x; e1]),
eapp (tvar_none "TLA.in", [x; e2])))
in
mknode (Inst h) "setequal" [e; h; e1; e2] [| [h] |]
| Enot (Eapp (Evar("=",_), [e1; e2], _), _) when is_set_expr e1 || is_set_expr e2 ->
let x = Expr.newtvar type_none in
let h = enot (eall (x, eequiv (eapp (tvar_none "TLA.in", [x; e1]),
eapp (tvar_none "TLA.in", [x; e2]))))
in
mknode (Inst h) "notsetequal" [e; h; e1; e2] [| [h] |]
| Enot (Eapp (Evar("TLA.isAFcn",_), [Eapp (Evar("TLA.Fcn",_), [s; l], _)], _), _) ->
mknode Prop "notisafcn_fcn" [e; s; l] [| |]
| Enot (Eapp (Evar("TLA.isAFcn",_), [Eapp (Evar("TLA.except",_), [f; v; e1], _)], _), _) ->
mknode Prop "notisafcn_except" [e; f; v; e1] [| |]
| Enot (Eapp (Evar("TLA.isAFcn",_), [Eapp (Evar("TLA.oneArg",_), [e1; e2], _)], _), _) ->
mknode Prop "notisafcn_onearg" [e; e1; e2] [| |]
| Enot (Eapp (Evar("TLA.isAFcn",_), [Eapp (Evar("TLA.extend",_), [f; g], _)], _), _) ->
mknode Prop "notisafcn_extend" [e; f; g] [| |]
| Eapp (Evar("=",_), [e1; e2], _)
when (is_fcn_expr e1 || is_fcn_expr e2)
(* && not (is_var e1) && not (is_var e2) *) ->
let x = Expr.newtvar type_none in
let h1 = eequiv (eapp (tvar_none "TLA.isAFcn", [e1]), eapp (tvar_none "TLA.isAFcn", [e2])) in
let h2 = eeq (eapp (tvar_none "TLA.DOMAIN", [e1])) (eapp (tvar_none "TLA.DOMAIN", [e2]))
in
let h3 = eall (x, eeq (eapp (tvar_none "TLA.fapply", [e1; x])) (eapp (tvar_none "TLA.fapply", [e2; x])))
in
let h = eand (eand (h1, h2), h3) in
mknode (Inst h3) "funequal" [e; h; e1; e2] [| [h] |]
| Enot (Eapp (Evar("=",_), [e1; e2], _), _) when is_fcn_expr e1 || is_fcn_expr e2 ->
let x = Expr.newtvar type_none in
let h0 = eapp (tvar_none "TLA.isAFcn", [e1]) in
let h1 = eapp (tvar_none "TLA.isAFcn", [e2]) in
let h2 = eeq (eapp (tvar_none "TLA.DOMAIN", [e1])) (eapp (tvar_none "TLA.DOMAIN", [e2]))
in
let h3 = eall (x, eimply (eapp (tvar_none "TLA.in", [x; eapp(tvar_none "TLA.DOMAIN",[e2])]),
eeq (eapp (tvar_none "TLA.fapply", [e1; x]))
(eapp (tvar_none "TLA.fapply", [e2; x]))))
in
let h = enot (eand (eand (eand (h0, h1), h2), h3)) in
mknode (Inst h3) "notfunequal" [e; h; e1; e2] [| [h] |]
| Enot (Eapp (Evar("=",_), [Etau (v1, p1, _); Etau (v2, p2, _)], _), _)
when not (is_notequiv p1) && not (is_notequiv p2) ->
let x = Expr.newtvar type_none in
let p1x = substitute [(v1, x)] p1 in
let p2x = substitute [(v2, x)] p2 in
let h = eex (x, eapp (tvar_none "$notequiv", [p1x; p2x])) in
mknode (Inst h) "choose_diff_choose"
[e; h; elam (v1, p1); elam (v2, p2)] [| [h] |]
| Eapp (Evar("$notequiv",_), [e1; e2], _) ->
let h = enot (eequiv (e1, e2)) in
mknode Arity "notequivdef" [] [| [h] |]
| Enot (Eapp (Evar("=",_), [e1; Etau (v2, p2, _)], _), _)
| Enot (Eapp (Evar("=",_), [Etau (v2, p2, _); e1], _), _)
->
let h1 = eex (v2, p2) in
let h2a = enot (eex (v2, p2)) in
let h2b = enot (substitute [(v2, e1)] p2) in
mknode_cut (Inst h2a) "notequalchoose"
[h1; h2a; h2b; elam (v2, p2); e1]
[| [h1]; [h2a; h2b] |]
| Eall (v, Eimply (Eapp (Evar("TLA.in",_),
[vv; ( Eapp (Evar(("TLA.addElt" | "TLA.upair"
| "TLA.set" | "TLA.union"),_), _, _)
| Evar ("TLA.emptyset", _)
) as s], _), _, _), _) ->
let (elems, rest) = get_values_set s in
let nodes = List.map (fun elem -> mknode_inst Arity e elem) elems in
List.flatten nodes @ (if rest then [] else [Stop])
| Enot ((Eex (v, Eand (Eapp (Evar("TLA.in",_),
[vv; ( Eapp (Evar(("TLA.addElt" | "TLA.upair"
| "TLA.set" | "TLA.union"),_), _, _)
| Evar ("TLA.emptyset", _)
) as s], _), _, _), _) as ex), _) ->
let (elems, rest) = get_values_set s in
let nodes = List.map (fun elem -> mknode_inst Arity ex elem) elems in
List.flatten nodes @ (if rest then [] else [Stop])
| Enot (Eapp (Evar("TLA.in",_), [Evar ("0", _); Evar ("arith.N", _)], _), _) ->
mknode Prop "in_nat_0" [e] [| |]
| Enot (Eapp (Evar("TLA.in",_), [Eapp (Evar("TLA.fapply",_), [Evar ("TLA.Succ", _); e1], _);
Evar ("arith.N", _)], _), _) ->
let h = enot (eapp (tvar_none "TLA.in", [e1; tvar_none "arith.N"])) in
mknode Prop "in_nat_succ" [e; h; e1] [| [h] |]
| Enot (Eapp (Evar("TLA.in",_), [Emeta (m, _); Evar ("arith.N", _)], _), _) ->
mknode_inst (Inst m) m (tvar_none "0")
| Eapp (Evar("TLA.in",_), [Emeta _; s], _)
| Enot (Eapp (Evar("TLA.in",_), [Emeta _; s], _), _) ->
let x = Expr.newtvar type_none in
let f eneq =
match eneq with
| Enot (Eapp (Evar("=",_), [s1; s2], _), _) ->
let h = enot (eall (x, eequiv (eapp (tvar_none "TLA.in", [x; s1]),
eapp (tvar_none "TLA.in", [x; s2]))))
in
mknode (Inst h) "notsetequal" [eneq; h; s1; s2] [| [h] |]
| _ -> assert false
in List.flatten (List.map f (Index.find_neq s))
| Eapp (Evar("TLA.box",_), [e1], _) ->
mknode Prop "box_p" [e; e1] [| [e1] |]
(* tuples *)
| Eapp (Evar("=",_), [Eapp (Evar("TLA.tuple",_), args1, _); Eapp (Evar("TLA.tuple",_), args2, _)], _)
when List.length args1 = List.length args2 ->
let hs = List.map2 (fun a1 a2 -> eeq a1 a2) args1 args2 in
mknode Prop "tuple_eq_match" (e :: hs) [| hs |]
| Eapp (Evar("=",_), [Eapp (Evar("TLA.tuple",_), args1,_); Eapp (Evar("TLA.tuple",_), args2,_)],_) ->
mknode Prop "tuple_eq_mismatch" [e; e] [| |]
| Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.Product",_),
[Eapp (Evar("TLA.tuple",_), args, _) as e2], _)], _) ->
let mk_h arg i =
eapp (tvar_none "TLA.in", [eapp (tvar_none "TLA.fapply", [e1; mk_nat i]); arg])
in
let hs = list_mapi mk_h args 1 in
let n = mk_nat (List.length args) in
let h0 = eapp (tvar_none "TLA.isASeq", [e1]) in
let h1 = eeq (eapp (tvar_none "TLA.Len", [e1])) n in
mknode Prop "in_product" (e :: e1 :: e2 :: h0 :: h1 :: hs)
[| h0 :: h1 :: hs |]
| Enot (Eapp (Evar("TLA.in",_), [Eapp (Evar("TLA.tuple",_), a1, _);
Eapp (Evar("TLA.Product",_),
[Eapp (Evar("TLA.tuple",_), a2, _)], _)],_),_)
when List.length a1 <> List.length a2
|| List.exists2 trivially_notin a1 a2 ->
[]
| Enot (Eapp (Evar("TLA.in",_),
[e1; Eapp (Evar("TLA.Product",_),
[Eapp (Evar("TLA.tuple",_), args, _) as e2], _)],_),_) ->
let mk_h arg i =
enot (eapp (tvar_none "TLA.in", [eapp (tvar_none "TLA.fapply", [e1; mk_nat i]); arg]))
in
let hs = list_mapi mk_h args 1 in
let n = mk_nat (List.length args) in
let h0 = enot (eapp (tvar_none "TLA.isASeq", [e1])) in
let h1 = enot (eeq (eapp (tvar_none "TLA.Len", [e1])) n) in
let hh = h0 :: h1 :: hs in
let branches = List.map (fun x -> [x]) hh in
mknode Prop "notin_product" (e :: e1 :: e2 :: hh) (Array.of_list branches)
| Enot (Eapp (Evar("TLA.isASeq",_), [Eapp (Evar("TLA.tuple",_), _, _)], _), _) ->
mknode Prop "tuple_notisaseq" [e] [| |]
(* records *)
| Eapp (Evar("=",_), [Eapp (Evar("TLA.record",_), args1, _); Eapp (Evar("TLA.record",_), args2, _)], _)
-> begin try
let cmp (a1, a2) (b1, b2) = Expr.compare a1 b1 in
let args1 = List.sort cmp (mk_pairs args1) in
let args2 = List.sort cmp (mk_pairs args2) in
check_record_labels args1;
check_record_labels args2;
let rec mk_hyps as1 as2 =
match as1, as2 with
| [], [] -> []
| (l1, a1) :: t1, (l2, a2) :: t2 when Expr.equal l1 l2 ->
eeq a1 a2 :: mk_hyps t1 t2
| _ -> raise Exit
in
let hs = mk_hyps args1 args2 in
mknode Prop "record_eq_match" (e :: hs) [| hs |]
with
| Invalid_argument x when x = "check_record_labels" -> []
| Exit -> mknode Prop "record_eq_mismatch" [e; e] [| |]
end
| Enot (Eapp (Evar("=",_), [Eapp (Evar("TLA.record",_), args1, _);
Eapp (Evar("TLA.record",_), args2, _)], _), _) ->
begin try
let cmp (a1, a2) (b1, b2) = Expr.compare a1 b1 in
let args1 = List.sort cmp (mk_pairs args1) in
let args2 = List.sort cmp (mk_pairs args2) in
check_record_labels args1;
check_record_labels args2;
let rec mk_hyps as1 as2 =
match as1, as2 with
| [], [] -> []
| (l1, a1) :: t1, (l2, a2) :: t2 when Expr.equal l1 l2 ->
enot (eeq a1 a2) :: mk_hyps t1 t2
| _ -> raise Exit
in
let hs = mk_hyps args1 args2 in
let branches = Array.of_list (List.map (fun x -> [x]) hs) in
mknode Prop "record_neq_match" (e :: hs) branches
with
| Invalid_argument x when x = "check_record_labels" -> []
| Exit -> []
end
| Enot (Eapp (Evar("=",_), [e1; Eapp (Evar("TLA.record",_), args, _) as e2], _), _) ->
let l_args = mk_pairs args in
let mk_h (l, arg) =
enot (eeq (eapp (tvar_none "TLA.fapply", [e1; l])) arg)
in
let hs = List.map mk_h l_args in
let lbls = eapp (tvar_none "TLA.set", List.map fst l_args) in
let h0 = enot (eapp (tvar_none "TLA.isAFcn", [e1])) in
let h1 = enot (eeq (eapp (tvar_none "TLA.DOMAIN", [e1])) lbls) in
let hh = h0 :: h1 :: hs in
let branches = Array.of_list (List.map (fun x -> [x]) hh) in
mknode Prop "neq_record" (e :: e1 :: e2 :: hh) branches
| Enot (Eapp (Evar("=",_), [Eapp (Evar("TLA.record",_), args, _) as e2; e1], _), _) ->
let l_args = mk_pairs args in
let mk_h (l, arg) =
enot (eeq (eapp (tvar_none "TLA.fapply", [e1; l])) arg)
in
let hs = List.map mk_h l_args in
let lbls = eapp (tvar_none "TLA.set", List.map fst l_args) in
let h0 = enot (eapp (tvar_none "TLA.isAFcn", [e1])) in
let h1 = enot (eeq (eapp (tvar_none "TLA.DOMAIN", [e1])) lbls) in
let hh = h0 :: h1 :: hs in
let branches = Array.of_list (List.map (fun x -> [x]) hh) in
mknode Prop "record_neq" (e :: e1 :: e2 :: hh) branches
| Eapp (Evar("TLA.in",_), [e1; (Eapp (Evar("TLA.recordset",_), args, _) as e2)], _) ->
let l_args = mk_pairs args in
let mk_h (l, arg) = eapp (tvar_none "TLA.in", [eapp (tvar_none "TLA.fapply", [e1; l]); arg]) in
let hs = List.map mk_h l_args in
let lbls = eapp (tvar_none "TLA.set", List.map fst l_args) in
let h0 = eapp (tvar_none "TLA.isAFcn", [e1]) in
let h1 = eeq (eapp (tvar_none "TLA.DOMAIN", [e1])) lbls in
mknode Prop "in_recordset" (e :: e1 :: e2 :: h0 :: h1 :: hs)
[| h0 :: h1 :: hs |]
| Enot (Eapp (Evar("TLA.in",_), [Eapp (Evar("TLA.record",_), a1, _);
Eapp (Evar("TLA.recordset",_), a2, _)], _), _)
when get_record_labels a1 <> get_record_labels a2
|| List.exists (field_trivially_notin (mk_pairs a2)) (mk_pairs a1) ->
[]
| Enot (Eapp (Evar("TLA.in",_), [e1; Eapp (Evar("TLA.recordset",_), args, _) as e2], _), _) ->
let l_args = mk_pairs args in
let mk_h (l, arg) =
enot (eapp (tvar_none "TLA.in", [eapp (tvar_none "TLA.fapply", [e1; l]); arg]))
in
let hs = List.map mk_h l_args in
let lbls = eapp (tvar_none "TLA.set", List.map fst (mk_pairs args)) in
let h0 = enot (eapp (tvar_none "TLA.isAFcn", [e1])) in
let h1 = enot (eeq (eapp (tvar_none "TLA.DOMAIN", [e1])) lbls) in
let hh = h0 :: h1 :: hs in
let branches = List.map (fun x -> [x]) hh in
mknode Prop "notin_recordset" (e :: e1 :: e2 :: hh)
(Array.of_list branches)
| Enot (Eapp (Evar("TLA.isAFcn",_), [Eapp (Evar("TLA.record",_), _, _)], _), _) ->
mknode Prop "record_notisafcn" [e] [| |]
(* shortcuts for subseteq *)
| Eapp (Evar("TLA.subseteq",_), [Eapp (Evar("TLA.cup",_), [e1; e2], _); e3], _) ->
let h1 = eapp (tvar_none "TLA.subseteq", [e1; e3]) in
let h2 = eapp (tvar_none "TLA.subseteq", [e2; e3]) in
mknode Arity "cup_subseteq" [e; h1; h2] [| [h1; h2] |]
| Enot (Eapp (Evar("TLA.subseteq",_), [Eapp (Evar("TLA.cup",_), [e1; e2], _); e3], _), _) ->
let h1 = enot (eapp (tvar_none "TLA.subseteq", [e1; e3])) in
let h2 = enot (eapp (tvar_none "TLA.subseteq", [e2; e3])) in
mknode Arity "not_cup_subseteq" [e; h1; h2] [| [h1]; [h2] |]
| Eapp (Evar("TLA.subseteq",_), [e1; Eapp (Evar("TLA.cap",_), [e2; e3], _)], _) ->
let h1 = eapp (tvar_none "TLA.subseteq", [e1; e3]) in
let h2 = eapp (tvar_none "TLA.subseteq", [e1; e3]) in
mknode Arity "subseteq_cap" [e; h1; h2] [| [h1; h2] |]
| Enot (Eapp (Evar("TLA.subseteq",_), [e1; Eapp (Evar("TLA.cap",_), [e2; e3], _)], _), _) ->
let h1 = enot (eapp (tvar_none "TLA.subseteq", [e1; e3])) in
let h2 = enot (eapp (tvar_none "TLA.subseteq", [e1; e3])) in
mknode Arity "not_subseteq_cap" [e; h1; h2] [| [h1]; [h2] |]
| _ -> []
;;
let newnodes_inst_bounded e g =
let prio = Inst e in (* FIXME change to Arity ? *)
match e with
| Eapp (Evar("TLA.in",_), [a; s], _) when not (has_metas a) ->
let get_p (e1, _) =
match e1 with
| Eapp (Evar("TLA.bAll",_), [s1; p], _) when Expr.equal s1 s -> [(e1, p)]
| Enot (Eapp (Evar("TLA.bEx",_), [s1; p], _), _) when Expr.equal s1 s ->
[(e1, p)]
| _ -> []
in
let univ = List.flatten (List.map get_p (Index.find_pos "TLA.bAll")) in
let exist = List.flatten (List.map get_p (Index.find_neg "TLA.bEx")) in
let mk_inst_all (f, p) =
let h = apply p a in
Node {
nconc = [e; f];
nrule = Ext ("tla", "all_in", [f; e; h; s; p]);
nprio = prio;
ngoal = g;
nbranches = [| [h] |];
}
in
let mk_inst_notex (f, p) =
let h = enot (apply p a) in
Node {
nconc = [e; f];
nrule = Ext ("tla", "notex_in", [f; e; h; s; p]);
nprio = prio;
ngoal = g;
nbranches = [| [h] |];
}
in
List.map mk_inst_all univ @@ List.map mk_inst_notex exist
| Eapp (Evar("TLA.bAll",_), [s; p], _) ->
let get_value (f, _) =
match f with
| Eapp (Evar("TLA.in",_), [a; s1], _) when not (has_metas a) && Expr.equal s s1 ->
[(f, a)]
| _ -> []
in
let values = List.flatten (List.map get_value (Index.find_pos "TLA.in")) in
let mk_inst (f, v) =
let h = apply p v in
Node {
nconc = [f; e];
nrule = Ext ("tla", "all_in", [e; f; h; s; p]);
nprio = prio;
ngoal = g;
nbranches = [| [h] |];
}
in
List.map mk_inst values
| Enot (Eapp (Evar("TLA.bEx",_), [s; p], _), _) ->
let get_value (f, _) =
match f with
| Eapp (Evar("TLA.in",_), [a; s1], _) when not (has_metas a) && Expr.equal s s1 ->
[(f, a)]
| _ -> []
in
let values = List.flatten (List.map get_value (Index.find_pos "TLA.in")) in
let mk_inst (f, v) =
let h = enot (apply p v) in
Node {
nconc = [f; e];
nrule = Ext ("tla", "notex_in", [e; f; h; s; p]);
nprio = prio;
ngoal = g;
nbranches = [| [h] |];
}
in
List.map mk_inst values
| _ -> []
;;
let strip_neg e = match e with Enot (ne, _) -> ne | _ -> assert false;;
let mknode_pos_l (e, e1, e2, g) =
let eq = eeq e1 e2 in
let h1 = enot (eeq e e1) in
Node {
nconc = [e; eq];
nrule = Ext ("tla", "p_eq_l", [e; eq; h1; e2; e; e1; e2]);
nprio = Arity_eq;
ngoal = g;
nbranches = [| [h1]; [e2] |];
}
;;
let mknode_pos_r (e, e1, e2, g) =
let eq = eeq e1 e2 in
let h1 = enot (eeq e e2) in
Node {
nconc = [e; eq];
nrule = Ext ("tla", "p_eq_r", [e; eq; h1; e1; e; e1; e2]);
nprio = Arity_eq;
ngoal = g;
nbranches = [| [h1]; [e1] |];
}
;;
let mknode_neg_l (e, e1, e2, g) =
let ne = strip_neg e in
let eq = eeq e1 e2 in
let h1 = enot (eeq ne e1) in
let h2 = enot (e2) in
Node {
nconc = [e; eq];
nrule = Ext ("tla", "np_eq_l", [e; eq; h1; h2; ne; e1; e2]);
nprio = Arity_eq;
ngoal = g;
nbranches = [| [h1]; [h2] |];
}
;;
let mknode_neg_r (e, e1, e2, g) =
let ne = strip_neg e in
let eq = eeq e1 e2 in
let h1 = enot (eeq ne e2) in
let h2 = enot (e1) in
Node {
nconc = [e; eq];
nrule = Ext ("tla", "np_eq_r", [e; eq; h1; h2; ne; e1; e2]);
nprio = Arity_eq;
ngoal = g;
nbranches = [| [h1]; [h2] |];
}
;;
let newnodes_prop_eq e g =
let seq = tvar_none "=" in
let decompose eq =
match eq with
| Eapp (eeq, [e1; e2], _) -> (e, e1, e2, g)
| Enot (Eapp (eeq, [e1; e2], _), _) -> (e, e1, e2, g)
| _ -> assert false
in
match e with
| Eapp (Evar(s,_), args, _) ->
let matches_l = Index.find_trans_left seq (Index.Sym s) in
let matches_r = Index.find_trans_right seq (Index.Sym s) in
List.map (fun (eq, _) -> mknode_pos_l (decompose eq)) matches_l
@ List.map (fun (eq, _) -> mknode_pos_r (decompose eq)) matches_r
| Enot (Eapp (Evar(s,_), args, _), _) ->
let matches_l = Index.find_trans_left seq (Index.Sym s) in
let matches_r = Index.find_trans_right seq (Index.Sym s) in
List.map (fun (eq, _) -> mknode_neg_l (decompose eq)) matches_l
@ List.map (fun (eq, _) -> mknode_neg_r (decompose eq)) matches_r
| _ -> []
;;
let newnodes_eq_prop_l e g =
match e with
| Eapp (Evar("=",_), [Eapp (Evar(s,_), _, _) as e1; e2], _) ->
let matches_p = Index.find_pos s in
let matches_n = Index.find_neg s in
List.map (fun (e, gg) -> mknode_pos_l (e, e1, e2, gg)) matches_p
@ List.map (fun (e, gg) -> mknode_neg_l (e, e1, e2, gg)) matches_n
| _ -> []
;;
let newnodes_eq_prop_r e g =
match e with
| Eapp (Evar("=",_), [e1; Eapp (Evar(s,_), _, _) as e2], _) ->
let matches_p = Index.find_pos s in
let matches_n = Index.find_neg s in
List.map (fun (e, gg) -> mknode_pos_r (e, e1, e2, gg)) matches_p
@ List.map (fun (e, gg) -> mknode_neg_r (e, e1, e2, gg)) matches_n
| _ -> []
;;
let has_subst e = Index.find_eq_lr e <> [] || Index.find_eq_rl e <> [];;
let do_substitutions v p g =
let rhs = Index.find_eq_lr v in
let lhs = Index.find_eq_rl v in
let f lr e =
let eqn = if lr then eeq v e else eeq e v in
Node {
nconc = [apply p v; eqn];
nrule = if lr then CongruenceLR (p, v, e) else CongruenceRL (p, v, e);
nprio = Arity;
ngoal = g;
nbranches = [| [apply p e] |];
}
in
List.map (f true) rhs @@ List.map (f false) lhs
;;
let rec newnodes_subst x ctx e g =
let appctx e = substitute [(x, e)] ctx in
match e with
| Evar _ -> []
| Emeta _ -> []
| Enot (e1, _) -> newnodes_subst x (appctx (enot x)) e1 g
| Eapp (Evar("TLA.in",_), [Evar _ as e1; e2], _) when has_subst e1 ->
let nctx = appctx (eapp (tvar_none "TLA.in", [x; e2])) in
do_substitutions e1 (elam (x, nctx)) g
| Eapp (Evar("TLA.in",_), [e1; Evar _ as e2], _) when has_subst e2 ->
let nctx = appctx (eapp (tvar_none "TLA.in", [e1; x])) in
do_substitutions e2 (elam (x, nctx)) g
(* TODO:
| Eapp ("TLA.fapply", [Eapp ("$string", [Evar (s, _)], _);
<some nat constant in range>], _) ->
get the nth char and convert it to Isabelle notation
*)
| Eapp (Evar("TLA.fapply",_), [Evar _ as e1; e2], _) when has_subst e1 ->
let nctx = appctx (eapp (tvar_none "TLA.fapply", [x; e2])) in
do_substitutions e1 (elam (x, nctx)) g
| Eapp (Evar("TLA.isAFcn",_), [Evar _ as e1], _) when has_subst e1 ->
let nctx = appctx (eapp (tvar_none "TLA.isAFcn", [x])) in
do_substitutions e1 (elam (x, nctx)) g
| Eapp (Evar("TLA.isASeq",_), [Evar _ as e1], _) when has_subst e1 ->
let nctx = appctx (eapp (tvar_none "TLA.isASeq", [x])) in
do_substitutions e1 (elam (x, nctx)) g
| Eapp (Evar("TLA.DOMAIN",_), [Evar _ as e1], _) when has_subst e1 ->
let nctx = appctx (eapp (tvar_none "TLA.DOMAIN", [x])) in
do_substitutions e1 (elam (x, nctx)) g
| Eapp (Evar(("$notequiv" | "TLA.cond" | "TLA.CASE"),_), _, _) -> []
| Eapp (Evar(f,_) as f', args, _) ->
let rec loop leftarg rightarg =
match rightarg with
| [] -> []
| h::t ->
let e1 = eapp (f', List.rev_append leftarg (x :: t)) in
let newctx = appctx e1 in
let rw = newnodes_subst x newctx h g in
if rw <> [] then rw
else loop (h::leftarg) t
in
loop [] args
| _ -> []
;;
let newnodes_subst e g =
let x = Expr.newtvar type_none in
newnodes_subst x x e g
;;
let rec mk_case_branches ctx l cond =
match l with
| [] -> [ [cond] ]
| [e] -> [ [cond; ctx e] ]
| c :: e :: t -> [c; ctx e] :: mk_case_branches ctx t (eand (enot (c), cond))
;;
let apply f e =
match f with
| Elam (v, b, _) -> Expr.substitute [(v, e)] b
| _ -> assert false
;;
let has_ex e =
match e with
| Etau (v, (Enot (p, _) as np), _) ->
Index.member (eex (v, np)) || Index.member (enot (eall (v, p)))
| Etau (v, p, _) -> Index.member (eex (v, p))
| _ -> assert false
;;
let rec get_nat_const e accu =
match e with
| Evar (s, _) ->
begin try accu + int_of_string s
with Failure _ -> raise (Invalid_argument "get_nat_const")
end
| Eapp (Evar("TLA.fapply",_), [Evar ("TLA.Succ", _); e1], _) ->
get_nat_const e1 (accu + 1)
| _ -> raise (Invalid_argument "get_nat_const")
;;
let is_in_1_to e max =
try
let n = get_nat_const e 0 in
0 < n && n <= max
with Invalid_argument _ -> false
;;
let rewrites in_expr x ctx e mknode =
let lamctx = elam (x, ctx) in
let appctx e = substitute [(x, e)] ctx in
let mk_boolcase_1 name e1 =
let h1a = eeq e etrue in
let h1b = appctx (etrue) in
let h2a = eeq e efalse in
let h2b = appctx (efalse) in
mknode ("boolcase_" ^ name) [appctx e; h1a; h1b; h2a; h2b; lamctx; e1]
[] [| [h1a; h1b]; [h2a; h2b] |]
in
let mk_boolcase_2 name e1 e2 =
let h1a = eeq e etrue in
let h1b = appctx (etrue) in
let h2a = eeq e efalse in
let h2b = appctx (efalse) in
mknode ("boolcase_" ^ name) [appctx e; h1a; h1b; h2a; h2b; lamctx; e1; e2]
[] [| [h1a; h1b]; [h2a; h2b] |]
in
let mk_eq_nodes lamctx heads e =
let appctx x = apply lamctx x in
let good_head x =
match x with
| Eapp (Evar(hd,_), _, _) -> heads = [] || List.mem hd heads
| Evar (hd, _) -> heads = [] || List.mem hd heads
| _ -> false
in
let lr = List.filter good_head (Index.find_eq_lr e) in
let rl = List.filter good_head (Index.find_eq_rl e) in
let rew_lr e2 =
let h = appctx e2 in
let c2 = eeq e e2 in
mknode ("rewrite_lr") [lamctx; e; e2] [c2] [| [h] |]
in
let rew_rl e2 =
let h = appctx e2 in
let c2 = eeq e2 e in
mknode ("rewrite_rl") [lamctx; e; e2] [c2] [| [h] |]