-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformula.ml
1160 lines (1073 loc) · 34.7 KB
/
formula.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
open Hashcons
open Format
type comp = | Less | More | Eq | Mod of int
type set = | And | Or
type var =
| Fr of int
| Bounded of string *int * int
type quantifier =
| Free
| Existential
| No_formula
module Comp =
struct
type t = var
let compare = Pervasives.compare
end
module VS = Set.Make (Comp)
type right =
| Const of int (* = i *)
| Minus of (int * var) (* +i-x *)
| Var of (int * var) (* +i+x*)
module Comp' =
struct
type t = right
let compare = Pervasives.compare
end
module RS = Set.Make (Comp')
module RM = Map.Make (Comp')
let not_comp = function
| Less -> More
| More -> Less
| Eq -> Eq
| Mod i -> Mod i
(**the strict comparison *)
let fcomps = function | Less -> (<) | More -> (>) | Eq -> (=) | Mod k -> (fun x y -> (x -y) mod k=0)
let fcomp = function | Less -> (<=) | More -> (>=) | Eq -> (=) | Mod k -> (fun x y -> (x -y) mod k=0)
type neg = | Pos | Neg
let not_neg = function | Pos -> Neg | Neg -> Pos
let printf_comp neg ppf c =
let c=
if neg= Neg
then
match c with
| Eq -> "≠"
| Less -> ">"
| More -> "<"
| Mod _ -> "≭"
else
match c with
| Eq -> "="
| Less -> "≤"
| More -> "≥"
| Mod _ -> "≡"
in
fprintf ppf "%s" c
let vid = ref (-1)
(** The type of variable, it must correspond to a position *)
let newBoundVar s=
incr vid;
Bounded (s,0, !vid)
let newBoundArray d s=
incr vid;
Array.init d (fun i -> Bounded (s^ string_of_int i,i, !vid))
let free d =
let list_0_d = Array.of_list (Math.gen_list 0 (d -1)) in
Array.map (fun i -> Fr i) list_0_d
type t = t_node Hashcons.hash_consed
and t_node =
| Imply of ( t*t)
| Set of t_node Hset.t * set
| Not of t
| True | False
| Bool of var
| Comp of (var* right * comp)
| Assert of t
(*type sigma1 = (var list * t))*)
module For_node =
struct
type t = t_node
let equal f g=
match (f,g) with
| Set (l,a), Set (m,b) -> Hset.equal l m && a==b
| Assert a , Assert b
| Not a , Not b -> a==b
| Imply (a,b) , Imply (c,d) -> a==c && b ==d
| Bool a , Bool b -> a=b
| Comp (a,r,c), Comp (b,s,d) -> a=b && r = s && c=d
| False, False | True, True -> true
| _, _ -> false
let hashSet s =
Hset.fold (fun e acc-> 2* acc + e.hkey) s 0
let hash f =
abs(match f with
| Imply (l,r) -> 2* l.hkey + 3* r.hkey
| Set (f,And) -> 3* (hashSet f)
| Set (f,Or) -> 5* (hashSet f)
| Not f -> 7 * f.hkey
| Assert f -> 11 * f.hkey
| f -> Hashtbl.hash f)
end
module Fterm = Hashcons.Make(For_node)
let ft = Fterm.create 251
(** a function to obtain a t from a t_node *)
let fc f=
Fterm.hashcons ft f
let true_ = fc True
let false_ = fc False
let zero = function | And -> false_ | Or -> true_
let unity = function | And -> true_ | Or -> false_
let lesseq (a, b) = if a=b then true_ else fc (Comp (a,Var(0,b), Less))
let lessst (a, b) = if a=b then false_ else fc (Comp (a,Var(-1,b),Less))
(** a <= i +b *)
let less (a,i, b) = if a=b then false_ else fc (Comp (a,Var(i,b),Less))
(** x <= i*)
let lessc (x,i) = fc (Comp (x,Const(i),Less))
(** x>= i*)
let morec (x,i) = fc (Comp (x,Const(i),More))
let bool a = fc (Bool a)
(** x = i*)
let const (x,i)= fc (Comp(x,Const(i),Eq))
let comp (a,b,c) =
match b, c with
| _, Mod k when k = 1 -> true_
| Const i, Mod k -> fc (Comp (a, Const (Math.mod_ i k), Mod k))
| Var (i, x), Mod k ->
if x = a then
if i mod k = 0
then true_
else false_
else
fc (Comp (a, Var (Math.mod_ i k, x), Mod k))
| Minus (i, x), Mod k ->
if a= x
then
(* x= i-x mod k; 2x = i mod k; if k and i are even; x = i/2 mod k/2; else x = i(2^-1) mod k *)
if k mod 2 = 0
then if i mod 2 = 0
then fc (Comp (a, Const (i/2), Mod (k/2)))
else false_
else
let two = Math.mul_inv 2 k in
let i = Math.mod_ (i*two) k in
fc (Comp (a, Const i, Mod k))
else
fc (Comp (a, Minus (Math.mod_ i k, x), Mod k))
(* a ~ i+a; 0 ~ i;*)
| Var (i,x), _ when a = x->
if (fcomp c) 0 i then true_ else false_
(* a ~ i-a; 2a ~ i; a~ i/2 *)
| Minus (i,x), Eq when a = x->
if i mod 2 = 0
then fc (Comp (x, Const (i/2), Eq))
else false_
| Minus (i,x), Less when a = x->
fc (Comp (x, Const (Math.half_floor i), Less))
| Minus (i,x), More when a = x->
fc (Comp (x, Const (Math.half_ceil i), More))
| _ ->fc (Comp (a,b,c))
let mod_ (x,i,k) = comp(x, Const i, Mod k)
let mode (x,i,y,k) = comp (x, Var(-i,y), Mod k)
let add (a, b, c) = comp (a, Var(-b, c), Eq)
(** x = b-y *)
let minus (x, b, y) = comp (x,Minus( b, y), Eq)
let set (s, typ) =
let s =
Hset.fold
(fun e acc ->
match
e.node, typ, acc with
| _, _, None
| False, And, _
| True, Or, _
| Assert _, Or, _ -> None
|Set (s, a), _, Some acc when a = typ -> Some (Hset.union s acc)
| True, And, _
| False, Or, _ -> acc
| _, _, Some acc -> Some (Hset.add e acc)
) s (Some Hset.empty) in
match s with
| None -> zero typ
| Some s->
if Hset.is_empty s
then unity typ
else if Hset.is_singleton s
then Hset.choose s
else fc (Set(s, typ))
let and_ s = set (s,And)
let or_ s = set (s,Or)
let set_list typ l=
let l = List.fold_left (fun set e -> Hset.add e set) Hset.empty l in
set (l, typ)
let or_list = set_list Or
let and_list = set_list And
let rec not_ f=
match f.node with
| Not f -> f
| False -> true_
| True -> false_
(* x > i-y; x>= i+1-y*)
| Comp (x,Minus(i, y), Less) -> comp (x, Minus(i+1,y), More)
| Comp (x,Var(i, y), Less) -> comp (x, Var(i+1,y), More)
(* x < i-y; x<= i-y-1*)
| Comp (x,Minus(i, y), More) -> comp (x, Minus(i-1,y), Less)
| Comp (x,Var(i, y), More) -> comp (x, Var(i-1,y), Less)
(* x>i; x>=i+1*)
| Comp (x,Const( i), Less) -> morec (x, i+1)
(* x<i; x<=i-1*)
| Comp (x,Const( i), More) -> lessc (x, i-1)
| Comp (x, r, Mod 2) ->
let r =
match r with
| Const i -> Const (i+1)
| Minus (i, y) -> Minus (i+1, y)
| Var (i,y) -> Var (i+1, y)
in comp (x, r, Mod 2)
| _ -> fc (Not f)
let imply (l,r) =
match l.node, r.node with
| False, _ -> true_
| _, True -> true_
| True, _ -> r
| _, False -> not_ l
| _, _ -> fc (Imply (l, r))
(** mod x =c+ y mod k using only mod predicate *)
let mod_lin (x, c, y, k) =
let list = Math.gen_list 0 (k-1) in
let a=
List.map
(fun i->
let l= mod_ (x, i, k)
and r = mod_ (x, i -c, k) in
imply (l,r)
) list
in and_list a
(** mod x =+c -y mod k using only mod predicate *)
let mod_lin_n (x, c, y, k) =
let list = Math.gen_list 0 (k-1) in
let a=
List.map
(fun i->
let l= mod_ (x, i, k)
(* y = c-x*)
and r = mod_ (x, c-i, k) in
imply (l,r)
) list
in and_list a
(** x+1 = y*)
let equal (x, y) = add (x,0,y)
let succ (x, y) = add (x,1,y)
let neg x = lessc (x,-1)
(** remain the value of the free variable, raise Invalid_argument if it is bounded *)
let of_free = function
| Fr n -> n
| _ -> raise (Invalid_argument "not a free variable")
let rec length f=
let lv = function
| Fr x | Bounded (_, x,_)-> Math.log x
in
let lr = function
| Const i -> Math.log i
| Var (i,x)
| Minus (i,x)->
lv x + Math.log i
in
let lc = function
| Less | More | Eq -> 1
| Mod k -> Math.log k
in
match f.node with
| Imply (l,r) -> 1+ length l+ length r
| Set (f,_) -> Hset.fold (fun x i -> (length x) + i + 1) f (-1)
| Assert f
| Not f -> 1 + (length f)
| Comp (x,r,c) -> lv x + lc c +lr r
| True | False -> 1
| Bool v -> lv v
let eq_tuple ar1 ar2 =
let ar1= Array.to_list ar1
and ar2 = Array.to_list ar2
in and_list (List.map2 (fun i j -> equal (i,j)) ar1 ar2 )
(** return a set of variable appearing once/many time*)
let mod_bound1_many f =
let add i (one, two) =
match i with
| Bounded _ ->
if VS.mem i two
then (one, two)
else
if VS.mem i one
then (VS.remove i one), (VS.add i two)
else (VS.add i one), two
| Fr _ -> (one,two) in
let add_right r bs = match r with
| Minus (_, x)
| Var (_, x) -> add x bs
| Const _ -> bs in
let add_comp k = function
| Mod k' -> Math.lcm k k'
| _ -> k in
let rec aux f ((k,bs) as acc)= match f.node with
| Imply (l,r) -> aux r (aux l acc)
| Set (l,_) -> Hset.fold aux l acc
| False | True -> acc
| Assert f -> acc
| Not f -> aux f acc
| Comp (v,r,c) -> add_comp k c, add_right r (add v bs)
| Bool v -> k, add v bs
in aux f (1,(VS.empty,VS.empty))
let printf_var ppf = function
| Fr i -> fprintf ppf "x%d" i
| Bounded (s , _, _ ) -> fprintf ppf "%s" s
let printf_right ppf r =
match r with
| Const i -> fprintf ppf "%d" i
| Minus (i,v)->
if i = 0 then
fprintf ppf "-%a" printf_var v
else
fprintf ppf "%d-%a" i printf_var v
| Var (i,v)->
if i = 0 then
fprintf ppf "%a" printf_var v
else
fprintf ppf "%d+%a" i printf_var v
let printf ppf formula =
let rec aux pff f =
match f.node with
| Imply (l, r) ->
fprintf ppf "@[(%a⇒@ %a)@]" aux l aux r
| Set (phis, And) ->
if Hset.is_empty phis
then fprintf ppf "(∧)"
else Pretty.print_set ppf "@[(" "∧@ " ")@]" phis aux
| Set (phis, Or) ->
if Hset.is_empty phis
then fprintf ppf "(∨)"
else Pretty.print_set ppf "@[(" "∨@ " ")@]" phis aux
| True -> fprintf ppf "True"
| False -> fprintf ppf "False"
| Bool x -> printf_var ppf x
| Comp (x, v, c) ->
fprintf ppf "%a%a%a" printf_var x (printf_comp Pos) c printf_right v;
(match c with | Mod k -> fprintf ppf " mod %d" k| _ -> ())
| Assert f -> fprintf ppf "[%a]" aux f
| Not phi->
match phi.node with
| Comp (x,v, c) ->
fprintf ppf "%a%a%a" printf_var x (printf_comp Neg) c printf_right v;
(match c with | Mod k -> fprintf ppf " mod %d" k| _ -> ())
| _ -> fprintf ppf "¬%a" aux phi
in
aux ppf formula
let assert_ f =
(* fprintf std_formatter "We assert %a@." printf f; *)
match f.node with
| False -> false_
(* the case true should never happen, but who knows, it wouldn't be
mathematicaly false *)
| True -> true_
| _ -> fc (Assert f)
let power bm =
let pr = function
| Const i -> Const (i*bm)
| Var (i,x) -> Var (i*bm, x)
| Minus (i,x) -> Minus (i*bm, x)
in
let pc = function
| Mod k -> Mod (k*bm)
| x -> x
in
let rec aux f= match f.node with
| Imply (l,r) -> imply (aux l, aux r)
| Set (f,a) -> set (Hset.map aux f, a)
| Not f -> not_ (aux f)
| Comp (x,r,c) -> comp (x, pr r, pc c)
| True -> true_ | False -> false_
| Bool v -> bool v
| Assert f -> assert_ (aux f)
in aux
let move i f =
let replace j = let j = of_free j in Fr (if j<i then j else j+1) in
let replace_right = function
| Var (i,y) -> Var (i, replace y)
| Minus (i,y) -> Minus (i, replace y)
| Const (i) -> Const i
in
let rec aux f = match f.node with
| Imply (l, r) ->
imply (aux l, aux r)
| Set (l, a) ->
set ((Hset.map aux l),a)
| Assert f -> assert_ (aux f)
| Not f -> not_ (aux f)
| Comp (x,r, c) ->
comp (replace x,replace_right r,c)
| Bool x -> bool (replace x)
| True -> true_
| False -> false_
in aux f
let change_free bound f =
let replace = function
| Fr x -> bound.(x)
| x -> x in
let replace_right = function
| Const i -> Const i
| Minus (i, v) -> Minus (i, replace v)
| Var (i, v) -> Var (i, replace v) in
let rec aux f = match f.node with
| Assert f -> assert_ (aux f)
| Imply (l,r) -> imply (aux l, aux r)
| Set (l,a) -> set (Hset.map aux l, a)
| True -> true_
| False -> false_
| Not f -> not_ (aux f)
| Bool x -> bool (replace x)
| Comp (x,r, c)-> comp (replace x,replace_right r, c)
in aux f
(**return a shorter equivalent formula*)
(*neg is Neg if there is an odd number of Negation before the variable.
If neg true, then more set are accepted when a predicate is replaced by "false" else by "true"
And if a variable is used only once, we can always assume it is in an atomic formula which is as we want true or false *)
let rec simplify natural formula=
(* Take a fact that we assume (or whose negation we assume) and simplify according to it *)
let rec change fact assumed_true formula =
let rec aux formula =
(* fprintf std_formatter "we replace %a in %a @." printf fact printf formula; *)
if formula == fact
then (
(* fprintf std_formatter "we have replaced %a@." printf fact; *)
if assumed_true then true_ else false_)
else match formula.node with
| Imply (l,r) -> imply (aux l,aux r)
| Set (l,a) -> set (Hset.map aux l, a)
| Not f -> not_ (aux f)
| Assert f -> let f' = aux f in
(* if f.node = False *)
(* then fprintf std_formatter "we have changed %a in False@." printf f; *)
assert_ (f')
| Comp (x,Const i, Eq) -> (*x = i *)
(match fact.node with
| Comp (y,Const j,Mod( k)) when x=y ->
(* x = j mod k;*)
if Math.xor ((j -i )mod k = 0) assumed_true
(* if we know that x = j mod k, and we state that x = i != j mod k, then false *)
(* if we know that x != j mod k, and we state that x= i = j mod k, then false *)
then (
(* fprintf std_formatter "simplification of %a with %a, j=%d, i=%d, mod=%d@." printf formula printf fact j i ((j-i) mod k); *)
false_)
else formula
| Comp (y,Const( j), Eq) when x=y && assumed_true -> (*we can assume that i<>j *)
false_
| _ -> formula)
| Comp (x,Const(i),Mod(k)) ->
(match fact.node with
| Comp (y,Const(j),Mod(k')) when k=k' && x=y ->
(*we can assume that i<>j *)
if assumed_true
then false_
else formula
| _ -> formula
)
| Comp (x,Const(c),e) -> (* e is Less or More, let say Less; x<= c *)
(match fact.node with
| Comp (y,Const(i), Eq) when x=y -> (* we know that x=i*)
if assumed_true && (fcomp e) i c
then true_
else formula
| Comp (y,Const(i), f) when x=y && (not_comp f)=e -> (* x >=i *)
if (fcomps e) c i && assumed_true (* if c<i *)
then false_
else formula
| Comp (y,Const(i), f) when x=y && f=e && assumed_true -> (* x <=i *)
let extremum = if (fcomp e) c i(* if c<i*) then c else i in
comp (x, Const extremum, f)
| _ -> formula
)
| Comp (x,Var(i,y), Eq)-> (* x = i+y *)
(match fact.node with
| Comp (z,Const( j), Eq) when x=z && assumed_true-> (* x = j*)
const (y,j-i)
| Comp (z,Const( j), Eq) when y=z && assumed_true -> (*y=j*)
const (x, j+i)
| _ -> formula)
| Comp (x,Minus(i,y), Eq)-> (* x = i-y *)
(match fact.node with
| Comp (z,Const( j), Eq) when x=z && assumed_true-> (* x = j*)
const (y,i-j)
| Comp (z,Const( j), Eq) when y=z && assumed_true -> (*y=j*)
const (x, i-j)
| _ -> formula)
| _ -> formula
in
match fact.node, assumed_true with
| Not fact, _ -> change fact (not assumed_true) formula
| Assert fact, _ -> formula
| Comp (a, b, Less), false
| Comp (a, b, More), false -> change (not_ fact) true formula
| _ -> aux formula
in
let _,(bounded_once, _)= mod_bound1_many formula in
(* the actual simplification *)
let rec aux neg formula=
let if_neg =(if neg= Neg then false_ else true_) in
match formula.node with
| True -> true_
| False -> false_
| Comp (_,_,Mod k) when k=1 -> true_
| Assert f -> let f' = (aux neg f) in
(* if f.node = False *)
(* then fprintf std_formatter "we have simplified %a in False@." printf f; *)
assert_ f'
| Comp (x,Const(_),c) ->
let b1 = VS.mem x bounded_once in
if b1
then
if natural = Math.Z
then if_neg
else
match
c, neg with
| Eq, _ -> if_neg
|Mod _, _ -> if_neg
| More, Pos -> true_
| Less, Neg -> false_
| _, _ -> formula
else formula
(* x ~ i + y *)
| Comp (x,Var( i,y), c) ->
let x_once= VS.mem x bounded_once
and y_once = VS.mem y bounded_once in
if (not x_once) && (not y_once)
then formula
else (
if natural= Math.Z
then if_neg
else
match
x_once, y_once, neg, c, i<0, i>0 with
| false, false, _, _, _, _ -> formula
| _, _, _,Mod _, _, _
| true, true, _, _, _, _ -> if_neg
| true, _, Pos, Eq,false, _
| _, true, Pos, Eq, _, false
| true, _, Pos, More, _, _
| _, true, Pos, Less, _, _ -> true_
| true, _, Neg, Less, _, _
| _, _, Neg, Eq , _, _
| _, true, Neg, More, _, _ -> false_
| _, _, _, _, _, _ -> formula)
(* x ~ i-y *)
| Comp (x,Minus( i, y), c) ->
let x_once = VS.mem x bounded_once
and y_once = VS.mem x bounded_once in
if (not x_once) && (not y_once)
then formula
else (
if natural = Math.Z
then if_neg
else
match (*if x_once is false, then y_once *)
x_once, neg, c with
| true, Pos, More
| false, Pos, Less -> true_
| true, Neg, Less
| false, Neg, More -> false_
| _, _, _ -> formula)
| Bool x ->
if VS.mem x bounded_once then if_neg else formula
| Imply (l, r) ->
let l = aux (not_neg neg) l
and r = aux neg r
in
let r= change l true r in
let l = change r false l in
imply (l, r)
| Not formula ->
let formula = aux (not_neg neg) formula in
not_ formula
| Set (s,a) ->
let s = Hset.map (aux neg) s in
(* this function takes the list of formula already seen and the
one to do *)
let rec aux2 seen todo =
if Hset.is_empty todo
then seen
else
let f = Hset.choose todo in
let todo = Hset.remove f todo in
let change_set = Hset.map (change f (a = And)) in
let seen = change_set seen in
let todo = change_set todo in
let seen = Hset.add f seen in
aux2 seen todo
in
let s = aux2 Hset.empty s in
set (s,a)
in
let formula' = aux Pos formula in
(* if formula = formula' *)
(* then *)
(* fprintf std_formatter "we can't simplify %a@.@." printf formula *)
(* else *)
(* fprintf std_formatter "we simplify %a in@. %a@.@." printf formula printf formula'; *)
(* the algorithm is a fixpoint, we repeat while things change. It
ends, because it change to become smaller *)
if formula != formula' then simplify natural formula' else formula'
let simplify_natural = simplify Math.N
let simplify_relative = simplify Math.Z
let inN n =function
| Bounded (_, x, _) | Fr x -> Subset.ins n x
(*could take inN as a parameter if needed*)
let change_n n =
let rec aux formula = match formula.node with
| Imply (l,r) -> imply (aux l, aux r)
| Assert f -> assert_ (aux f)
| Set (f,a) -> set (Hset.map aux f, a)
| Not f -> not_ (aux f)
| Comp (x,Const i,c) -> if inN n x (*-x-1 <=i; x+1>=-i*)
then comp (x, Const(-i-1), not_comp c)
else formula
| Comp (x, Minus(i, y), c) ->
(match inN n x, inN n y with
(*-x-1 <= i -(-y-1); x +1>= -i-y-1; x>=-y -i -1 *)
| true, true-> comp (x, Minus (-i -1, y), not_comp c)
| false,false-> formula
(* x <= i-(-y-1); x<=y +i +1*)
| false, true -> comp (x,Var(i+1,y),c)
(* -x-1 <= i-y; x+1>= y-i; x>= y-i-1*)
| true, false -> comp (x,Var(-1-i,y), not_comp c))
| Comp (x, Var(i, y), c) ->
(match inN n x, inN n y with
(*-x-1 <= i +(-y-1); x +1>= -i+y+1; x>=+y -i +1 *)
| true, true-> comp (x, Var (-i +1, y), not_comp c)
| false,false-> formula
(* x <= i+(-y-1); x<=-y +i -1*)
| false, true -> comp (x,Minus(i-1,y),c)
(* -x-1 <= i+y; x+1>=-y-i; x>=-y-i-1*)
| true, false -> comp (x,Var(-1-i,y), not_comp c))
| True | False | Bool _ -> formula
in aux
let rec add1 f =
match f.node with
| Imply (l,r) -> imply (add1 l, add1 r)
| Set (f,a) -> set (Hset.map add1 f,a)
| Not f -> not_ (add1 f)
| Assert f -> assert_ (add1 f)
(*x-1 ~ i; x ~i+1 *)
| Comp (x,Const( i),c) -> comp (x, Const(i+1),c)
(* x-1 ~ i-(y-1); x ~ i+2 -y*)
| Comp (x,Minus(i,y),c) -> comp (x, Minus(i+2, y),c)
| Comp (_, Var _, _)
| True | False | Bool _ -> f
(** replace Bool x by value in f*)
let rep_bool x f value =
let rec aux f =
match f.node with
| Imply (l, r) -> imply (aux l, aux r)
| Set (fs,a) -> set (Hset.map aux fs,a)
| Not f -> not_ (aux f)
| Assert f -> assert_ (aux f)
| Bool y -> if x = y
then value
else f
| True | False | Comp _ -> f
in aux f
(** "add_var i r" add the integer i to the variable r*)
let add_var i =function
| Const j -> Const (i+j)
| Var (j,y) -> Var (i+j,y)
| Minus (j,y) -> Minus (i+j,y)
(** add - to the constant part *)
let inv_var =function
| Const j -> Const (-j)
| Var (j,y) -> Var (-j,y)
| Minus (j,y) -> Minus (-j,y)
(** replace_var x val1 val2, replace the occurence of x in the "right"
val1 by val2 and return a new right argument *)
let replace_var x val1 val2 =
match val1 with
| Var (i, y) -> if x=y
then add_var i val2
else Var (i,y)
| Minus (i, y) -> if x = y
then add_var i (inv_var val2)
else Minus(i,y)
| Const i -> Const i
(** as comp, but left take a "right" instead of a variable*)
let rec comp_left (l, r, c) =
match l, r with
| Const i, Const j->
if (fcomps c) i j
then true_
else false_
(* i< j+x; x>i-j*)
| Const i, Var (j,x) ->
comp (x, Const (i-j), not_comp c)
(* i < j -x; x< j-i *)
| Const i, Minus (j,x) ->
comp (x, Const (j-i), not_comp c)
(* i-x < j-y; x-i> y-j *)
| Minus (i, x), Minus (j,y) -> comp_left (Var (-i, x), Var (-j,y), not_comp c)
| Minus _, _ -> comp_left (r,l,not_comp c)
| Var (i, x), _ -> comp (x, add_var (-i) r , c)
(** replace every occurence of the bounded variable x in f with the value "right"*)
let rep_right x f value =
let rec aux f =
match f.node with
| Imply (l, r) -> imply (aux l, aux r)
| Set (fs,a) -> set (Hset.map aux fs,a)
| Not f -> not_ (aux f)
| Assert f -> assert_ (aux f)
| True | False | Bool _ -> f
| Comp (y, r, c) ->
let r = replace_var x r value
in
if x = y
then comp_left (value, r, c)
else comp (y,r,c)
in aux f
(** return a set of value that is compared to the variable, or None if it is a boolean *)
let find_comp var f=
let add i x acc =
if x = var then RS.add i acc else acc
in
let right r x acc =match r with
| Const _ -> acc
| Minus (i, y) ->
(* x = i -y; hence y = i-x*)
add (Minus (i, y)) y acc
| Var (i, y) ->
(* x= i + y; hence y = x-i*)
add (Var (-i,x)) y acc
in
let rec aux f acc =
match acc with
| None -> None
| Some acc' ->
match f.node with
| Imply (l,r) -> aux r (aux l acc)
| Set (l,_) -> Hset.fold aux l acc
| Not f -> aux f acc
| True | False | Assert _
| Comp (_, _, Mod _) -> acc
| Comp (v, r, _) -> Some( right r v (add r v acc'))
| Bool x-> if x = var then None else acc
in aux f (Some RS.empty)
let elim natural f =
let k, (vars1, vars2) = mod_bound1_many f in
(* fprintf std_formatter "k is %d@." k; *)
let vars = VS.union vars1 vars2 in
let vars = VS.filter (function | Bounded _ -> true | Fr _ -> false) vars in
let f'=
VS.fold
(fun bounded_var f ->
let f = simplify natural f in
let vars = find_comp bounded_var f in
match vars with
| None ->
((* fprintf std_formatter "replacing %a by bools@." printf_var bounded_var; *)
or_list [rep_bool bounded_var f true_; rep_bool bounded_var f false_])
| Some vars ->
let vars =
if RS.is_empty vars (* if we don't compare it to constant, we must choose arbitrarily one *)
then RS.singleton (Const 0)
else vars
in
let all_vars = ref RS.empty in
RS.iter
(fun v->
match v with
| Const i ->
let min = if natural = Math.N
then max 0 (i-k)
else i -k
in
for j = min to i+k do
all_vars := RS.add (Const j) !all_vars
done;
| Var (i,x) ->
for j = i-k to i+k do
all_vars := RS.add (Var (j,x)) !all_vars
done;
| Minus (i,x) ->
for j = i-k to i+k do
all_vars := RS.add (Minus (j,x)) !all_vars
done;
) vars;
let vars = RS.elements !all_vars in
let fs = List.map
(fun v ->
let f'=rep_right bounded_var f v in
(* fprintf std_formatter "@.replacing %a@, by %a@, in %a@, we get %a@." printf_var bounded_var printf_right v printf f printf f'; *)
f'
) vars in
let f'= or_list fs in
f'
) vars f
in
(* fprintf std_formatter "%a@,, without quantifier is %a@." printf f printf f'; *)
simplify natural f'
let rec remove_assert f =
let rec aux f=
match f.node with
| Imply (l,r) -> imply (aux l, aux r)
| Set (f,a) -> set (Hset.map aux f, a)
| Not f -> not_ (aux f)
| Assert f -> true_
| _ -> f
in
let f' = aux f in
(* fprintf std_formatter "removing in %a@, gives %a@." printf f printf f'; *)
f'
let remove_simplify n f=
simplify n (remove_assert (simplify n f))
(** compute the residual of the formula, with the letter s in base
b*)
let residual b s f=
let of_s x = let x' = of_free x in IntAr.get s x' in
let rec aux f =
match f.node with
| Imply (l, r) -> imply (aux l, aux r)
| Set (e,a) -> set (Hset.map aux e,a)
| Not f -> not_ (aux f)
| Assert f -> assert_ (aux f)
| True | False | Bool _ -> f
| Comp (x, r, c) ->
let g = ref 0 in
let of_r = match r with
| Var (i,y) -> i+ of_s y
| Minus (i, y) -> i- of_s y
| Const i -> i
in
let num = of_r - (of_s x) in
let num = match c with
| Mod k ->
g := Math.gcd b k;
let b'= b/ !g in
let k'= k/ !g in
let inv =Math.mul_inv b' k' in
num * inv
| _ -> num
in
let num_b = match c with
| More -> Math.div_ceil num b
| Less -> Math.div_floor num b
| Eq -> num / b
| Mod k -> num / !g
in
let r = match r with
| Var (_,x) -> Var (num_b,x)
| Minus (_,x) -> Minus (num_b,x)
| Const _ -> Const num_b
in
let f = comp (x, r, c) in
match c with
| Eq ->
if num mod b = 0
then f
else false_
| Mod k ->
if num mod !g =0
then comp (x, r, Mod (k/ !g))
else false_
| Less| More -> f
in aux f
(** compute the inverse of the residual of the formula, with the letter s in base b*)
let inv_residual b s f=
let rec aux f =
match f.node with
| Imply (l, r) -> imply (aux l, aux r)
| Set (e,a) -> set (Hset.map aux e,a)
| Not f -> not_ (aux f)
| Assert f -> assert_ (aux f)
| Bool x -> bool x
| True -> true_
| False -> false_
| Comp (x, r, c) ->
let x' = of_free x in
let sx = (IntAr.get s x')in
let c = match c with
| Mod k -> Mod (k*b)
| _ -> c
in
let r =
match r with
| Const i -> Const (i*b + sx)
| Var (i,y) ->
let y' = of_free y in
let sy = (IntAr.get s y')in
Const (i*b + sx -sy)
| Minus (i,y) ->
let y' = of_free y in
let sy = (IntAr.get s y')in
Const (i*b + sx + sy)
in
comp (x, r, c)
in
let f' = aux f in
(* fprintf std_formatter "inv_residual of %a@, by %a@,, is %a@." printf f IntAr.printf s printf f'; *)
f'
let rec accept_n n f =
let inN = inN n in
let val_var x = (if inN x then -1 else 0) in
let rec aux f =
match f.node with
| Imply (l,r) -> aux r || (not (aux l))
| Set (f,a) ->
Hset.fold
(fun f ->
(match a with And -> (&&) | Or-> ( || )) (aux f)
) f (a = And)
| Not f -> not (aux f)
| Assert f -> aux f(* this shouldn't happen but who knows *)
| False -> false
| True -> true
| Bool v -> true
| Comp (x,r, c) ->
let val_right = match r with
| Const i -> i