-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.ml
1526 lines (1305 loc) · 44.7 KB
/
ast.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 © 2009 The Regents of the University of California. All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
* FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION
* TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
*)
(**
* This module implements a DAG representation for expressions and
* predicates: each sub-predicate or sub-expression is paired with
* a unique int ID, which enables constant time hashing.
* However, one must take care when using DAGS:
* (1) they can only be constructed using the appropriate functions
* (2) when destructed via pattern-matching, one must discard the ID
*)
(* random touch *)
module F = Format
module SM = Misc.StringMap
open Misc.Ops
let mydebug = false
module Sort =
struct
type loc =
| Loc of string
| Lvar of int
| LFun
type t =
| Int
| Bool
| Obj
| Var of int (* type-var *)
| Ptr of loc (* c-pointer *)
| Func of int * t list (* type-var-arity, in-types @ [out-type] *)
| Num (* kind, for numeric tyvars -- ptr(loc(s)) -- *)
type sub = { locs: (int * string) list;
vars: (int * t) list; }
(*
let is_loc_string s =
let re = Str.regexp "[a-zA-Z]+[0-9]+" in
Str.string_match re s 0
let loc_of_string = fun s -> let _ = asserts (is_loc_string s) in Loc s
let loc_of_index = fun i -> Lvar i
*)
let t_num = Num
let t_obj = Obj
let t_bool = Bool
let t_int = Int
let t_generic = fun i -> let _ = asserts (0 <= i) "t_generic: %d" i in Var i
let t_ptr = fun l -> Ptr l
let t_func = fun i ts -> Func (i, ts)
let loc_to_string = function
| Loc s -> s
| Lvar i -> string_of_int i
| LFun -> "<fun>"
let rec to_string = function
| Var i -> Printf.sprintf "@(%d)" i
| Int -> "int"
| Bool -> "bool"
| Obj -> "obj"
| Num -> "num"
| Ptr l -> Printf.sprintf "ptr(%s)" (loc_to_string l)
| Func (n, ts) -> ts |> List.map to_string
|> String.concat " ; "
|> Printf.sprintf "func(%d, [%s])" n
let to_string_short = function
| Func _ -> "func"
(* | Ptr _ -> "ptr" *)
| t -> to_string t
let print fmt t =
t |> to_string
|> Format.fprintf fmt "%s"
let sub_to_string {locs = ls; vars = vs} =
let lts = fun (i, s) -> Printf.sprintf "(%d := %s)" i s in
let vts = fun (i, t) -> Printf.sprintf "(%d := %s)" i (to_string t) in
Printf.sprintf "locs := %s, vars := %s \n"
(String.concat "" (List.map lts ls))
(String.concat "" (List.map vts vs))
let rec map f = function
| Func (n, ts) -> Func (n, List.map (map f) ts)
| t -> f t
let rec fold f b = function
| Func (n, ts) as t -> List.fold_left (fold f) (f b t) ts
| t -> f b t
let subs_tvar ts =
map begin function
| Var i -> Misc.do_catchf "ERROR: subs_tvar" (List.nth ts) i
| t -> t
end
let is_bool = function
| Bool -> true
| _ -> false
let is_int = function
| Int -> true
| _ -> false
let is_func = function
| Func _ -> true
| _ -> false
let func_of_t = function
| Func (_, ts) -> Some (ts |> Misc.list_snoc |> Misc.swap)
| _ -> None
let ptr_of_t = function
| Ptr l -> Some l
| _ -> None
let compat t1 t2 = match t1, t2 with
| Int, (Ptr _) -> true
| (Ptr _), Int -> true
| _ -> t1 = t2
(*
let concretize ts = function
| Func (n, ats) when n = List.length ts ->
Func (n, List.map (subs_tvar ts) ats)
| _ ->
assertf "ERROR: bad application"
let is_monotype t =
fold (fun b t -> b && (match t with Var _ -> false | _ -> true)) true t
*)
let lookup_var = fun s i -> try Some (List.assoc i s.vars) with Not_found -> None
let lookup_loc = fun s j -> try Some (List.assoc j s.locs) with Not_found -> None
let unifyt s = function
| Num,_ | _, Num -> None
| (Var i), ct
when ct != Bool ->
begin match lookup_var s i with
| Some ct' when ct = ct' -> Some s
| Some _ -> None
| None -> Some {s with vars = (i,ct) :: s.vars}
end
| Ptr LFun, Ptr _
| Ptr _, Ptr LFun -> Some s
| Ptr (Loc cl), Ptr (Lvar j)
| Ptr (Lvar j), Ptr (Loc cl) ->
begin match lookup_loc s j with
| Some cl' when cl' = cl -> Some s
| Some _ -> None
| None -> Some {s with locs = (j,cl) :: s.locs}
end
| (t1, t2) when t1 = t2 -> Some s
(*
| Int, Int | Bool, Bool | Obj, Obj ->
Some s
*)
| _ -> None
let unify ats cts =
let _ = asserts (List.length ats = List.length cts) "ERROR: unify sorts" in
List.combine ats cts
|> Misc.maybe_fold unifyt {vars = []; locs = []}
(* >> (fun so -> Printf.printf "unify: [%s] ~ [%s] = %s \n"
(String.concat "; " (List.map to_string ats))
(String.concat "; " (List.map to_string cts))
(match so with None -> "NONE" | Some s -> sub_to_string s))
*)
let apply s =
map begin fun t -> match t with
| Var i -> (match lookup_var s i with Some t' -> t' | _ -> t)
| Ptr (Lvar j) -> (match lookup_loc s j with Some l -> Ptr (Loc l) | _ -> t)
| _ -> t
end
end
module Symbol =
struct
type t = string
let mk_wild =
let t,_ = Misc.mk_int_factory () in
t <+> string_of_int <+> (^) "~A"
let is_wild_fresh s = s = "_"
let is_wild_any s = s.[0] = '~'
let is_wild_pre s = s.[0] = '@'
let is_wild s = is_wild_fresh s || is_wild_any s || is_wild_pre s
let is_safe s =
let re = Str.regexp "[A-Za-z '~' '_' '\'' '@' ][0-9 a-z A-Z '_' '@' '\'' '.' '#']*$" in
Str.string_match re s 0
let of_string, to_string =
let of_t = Hashtbl.create 117 in
let to_t = Hashtbl.create 117 in
let bind = fun s sy -> Hashtbl.replace of_t s sy; Hashtbl.replace to_t sy s in
let f,_ = Misc.mk_string_factory "FIXPOINTSYMBOL_" in
((fun s ->
if is_wild_fresh s then mk_wild () else
if is_safe s then s else
try Hashtbl.find of_t s with Not_found ->
let sy = f () in
let _ = bind s sy in sy),
(fun sy -> try Hashtbl.find to_t sy with Not_found -> sy))
let to_string = fun s -> s (* if is_safe s then s else "'" ^ s ^ "'" *)
let suffix = fun s suff -> of_string ((to_string s) ^ suff)
let print fmt s =
to_string s |> Format.fprintf fmt "%s"
let vvprefix = "VV_"
let vvsuffix = function
| Sort.Ptr l -> Sort.loc_to_string l
| t -> Sort.to_string_short t
let is_value_variable = Misc.is_prefix vvprefix
let value_variable t = vvprefix ^ (vvsuffix t)
(* DEBUG *)
let vvprefix = "VV"
let is_value_variable = (=) vvprefix
let value_variable _ = vvprefix
module SMap = Misc.EMap (struct type t = string
let compare i1 i2 = compare i1 i2
let print = print end)
module SSet = Misc.ESet (struct type t = string
let compare i1 i2 = compare i1 i2 end)
(* let sm_length m =
SMap.fold (fun _ _ i -> i+1) m 0
let sm_filter f sm =
SMap.fold begin fun x y sm ->
if f x y then SMap.add x y sm else sm
end sm SMap.empty
let sm_to_list sm =
SMap.fold (fun x y acc -> (x,y)::acc) sm []
let sm_of_list xs =
List.fold_left (fun sm (k,v) -> SMap.add k v sm) SMap.empty xs
*)
end
module Constant =
struct
type t = Int of int
let to_string = function
| Int i -> string_of_int i
let print fmt s =
to_string s |> Format.fprintf fmt "%s"
end
type tag = int
type brel = Eq | Ne | Gt | Ge | Lt | Le
type bop = Plus | Minus | Times | Div | Mod (* NOTE: For "Mod" 2nd expr should be a constant or a var *)
type expr = expr_int * tag
and expr_int =
| Con of Constant.t
| Var of Symbol.t
| App of Symbol.t * expr list
| Bin of expr * bop * expr
| Ite of pred * expr * expr
| Fld of Symbol.t * expr (* NOTE: Fld (s, e) == App ("field"^s,[e]) *)
| Cst of expr * Sort.t
| Bot
| MExp of expr list
| MBin of expr * bop list * expr
and pred = pred_int * tag
and pred_int =
| True
| False
| And of pred list
| Or of pred list
| Not of pred
| Imp of pred * pred
| Iff of pred * pred
| Bexp of expr
| Atom of expr * brel * expr
| MAtom of expr * brel list * expr
| Forall of ((Symbol.t * Sort.t) list) * pred
let list_hash b xs =
List.fold_left (fun v (_,id) -> 2*v + id) b xs
module Hashcons (X : sig type t
val sub_equal : t -> t -> bool
val hash : t -> int end) = struct
module HashStruct = struct
type t = X.t * int
let equal (x,_) (y,_) = X.sub_equal x y
let hash (x,_) = X.hash x
end
module Hash = Weak.Make(HashStruct)
let wrap =
let tab = Hash.create 251 in
let ctr = ref 0 in
fun e ->
let res = Hash.merge tab (e, !ctr) in
let _ = if snd res = !ctr then incr ctr in
res
let unwrap (e,_) = e
end
module ExprHashconsStruct = struct
type t = expr_int
let sub_equal e1 e2 =
match e1, e2 with
| Con c1, Con c2 ->
c1 = c2
| MExp es1, MExp es2 ->
es1 = es2
| Var x1, Var x2 ->
x1 = x2
| App (s1, e1s), App (s2, e2s) ->
(s1 = s2) &&
(try List.for_all2 (==) e1s e2s with _ -> false)
| Bin (e1, op1, e1'), Bin (e2, op2, e2') ->
op1 = op2 && e1 == e2 && e1' == e2'
| MBin (e1, ops1, e1'), MBin (e2, ops2, e2') ->
ops1 = ops2 && e1 == e2 && e1' == e2'
| Ite (ip1,te1,ee1), Ite (ip2,te2,ee2) ->
ip1 == ip2 && te1 == te2 && ee1 == ee2
| Fld (s1, e1), Fld (s2, e2) ->
s1 = s2 && e1 == e2
| _ ->
false
let hash = function
| Con (Constant.Int x) ->
x
| MExp es ->
list_hash 6 es
| Var x ->
Hashtbl.hash x
| App (s, es) ->
list_hash ((Hashtbl.hash s) + 1) es
| Bin ((_,id1), op, (_,id2)) ->
(Hashtbl.hash op) + 1 + (2 * id1) + id2
| MBin ((_,id1), op::_ , (_,id2)) ->
(Hashtbl.hash op) + 1 + (2 * id1) + id2
| Ite ((_,id1), (_,id2), (_,id3)) ->
32 + (4 * id1) + (2 * id2) + id3
| Fld (s, (_,id)) ->
(Hashtbl.hash s) + 12 + id
| Cst ((_, id), t) ->
id + Hashtbl.hash (Sort.to_string t)
| Bot ->
0
| _ -> assertf "pattern error in A.pred hash"
end
module ExprHashcons = Hashcons(ExprHashconsStruct)
module PredHashconsStruct = struct
type t = pred_int
let sub_equal p1 p2 =
match p1, p2 with
| True, True | False, False ->
true
| And p1s, And p2s | Or p1s, Or p2s ->
(try List.for_all2 (==) p1s p2s with _ -> false)
| Not p1, Not p2 ->
p1 == p2
| Imp (p1, p1'), Imp (p2, p2') ->
p1 == p2 && p1' == p2'
| Iff (p1,p1'), Iff (p2,p2') ->
p1 == p2 && p1' == p2'
| Bexp e1, Bexp e2 ->
e1 == e2
| Atom (e1, r1, e1'), Atom (e2, r2, e2') ->
r1 = r2 && e1 == e2 && e1' == e2'
| MAtom (e1, r1, e1'), MAtom (e2, r2, e2') ->
r1 = r2 && e1 == e2 && e1' == e2'
| Forall(q1s,p1), Forall(q2s,p2) ->
q1s = q2s && p1 == p2
| _ ->
false
let hash = function
| True ->
0
| False ->
1
| And ps ->
list_hash 2 ps
| Or ps ->
list_hash 3 ps
| Not (_,id) ->
8 + id
| Imp ((_,id1), (_,id2)) ->
20 + (2 * id1) + id2
| Iff ((_,id1), (_,id2)) ->
28 + (2 * id1) + id2
| Bexp (_, id) ->
32 + id
| Atom ((_,id1), r, (_,id2)) ->
36 + (Hashtbl.hash r) + (2 * id1) + id2
| MAtom ((_,id1), r, (_,id2)) ->
42 + (Hashtbl.hash r) + (2 * id1) + id2
| Forall(qs,(_,id)) ->
50 + (2 * (Hashtbl.hash qs)) + id
end
module PredHashcons = Hashcons(PredHashconsStruct)
let ewr = ExprHashcons.wrap
let euw = ExprHashcons.unwrap
let pwr = PredHashcons.wrap
let puw = PredHashcons.unwrap
(* Constructors: Expressions *)
let eCon = fun c -> ewr (Con c)
let eMExp = fun es -> ewr (MExp es)
let eInt = fun i -> eCon (Constant.Int i)
let zero = eInt 0
let one = eInt 1
let bot = ewr Bot
let eMod = fun (e, m) -> ewr (Bin (e, Mod, eInt m))
let eModExp = fun (e, m) -> ewr (Bin (e, Mod, m))
let eVar = fun s -> ewr (Var s)
let eApp = fun (s, es) -> ewr (App (s, es))
let eBin = fun (e1, op, e2) -> ewr (Bin (e1, op, e2))
let eMBin = fun (e1, ops, e2) -> ewr (MBin (e1, ops, e2))
let eIte = fun (ip,te,ee) -> ewr (Ite(ip,te,ee))
let eFld = fun (s,e) -> ewr (Fld (s,e))
let eCst = fun (e,t) -> ewr (Cst (e, t))
let eTim = function
| (Con (Constant.Int n1), _), (Con (Constant.Int n2), _) ->
ewr (Con (Constant.Int (n1 * n2)))
| (Con (Constant.Int 1), _), e2 ->
e2
| (Con (Constant.Int (-1)), _), e2 ->
eBin (zero, Minus, e2)
| (e1, e2) -> eBin (e1, Times, e2)
let rec conjuncts = function
| And ps, _ -> Misc.flap conjuncts ps
| True, _ -> []
| p -> [p]
(* Constructors: Predicates *)
let pTrue = pwr True
let pFalse = pwr False
let pAtom = fun (e1, r, e2) -> pwr (Atom (e1, r, e2))
let pMAtom = fun (e1, r, e2) -> pwr (MAtom (e1, r, e2))
let pOr = fun ps -> pwr (Or ps)
let pNot = fun p -> pwr (Not p)
let pBexp = fun e -> pwr (Bexp e)
let pImp = fun (p1,p2) -> pwr (Imp (p1,p2))
let pIff = fun (p1,p2) -> pwr (Iff (p1,p2))
let pForall= fun (qs, p) -> pwr (Forall (qs, p))
let pEqual = fun (e1,e2) -> pAtom (e1, Eq, e2)
let pAnd = fun ps -> match Misc.flap conjuncts ps with
| [] -> pTrue
| [p] -> p
| ps -> pwr (And (Misc.flap conjuncts ps))
module ExprHash = Hashtbl.Make(struct
type t = expr
let equal (_,x) (_,y) = (x = y)
let hash (_,x) = x
end)
module PredHash = Hashtbl.Make(struct
type t = pred
let equal (_,x) (_,y) = (x = y)
let hash (_,x) = x
end)
let bop_to_string = function
| Plus -> "+"
| Minus -> "-"
| Times -> "*"
| Div -> "/"
| Mod -> "mod"
let brel_to_string = function
| Eq -> "="
| Ne -> "!="
| Gt -> ">"
| Ge -> ">="
| Lt -> "<"
| Le -> "<="
let print_brel ppf r =
F.fprintf ppf "%s" (brel_to_string r)
let print_binding ppf (s,t) =
F.fprintf ppf "%a:%a" Symbol.print s Sort.print t
let bind_to_string (s,t) =
Printf.sprintf "%s:%s" (Symbol.to_string s) (Sort.to_string t)
let rec print_expr ppf e = match euw e with
| Con c ->
F.fprintf ppf "%a" Constant.print c
| MExp es ->
F.fprintf ppf "[%a]" (Misc.pprint_many false " ; " print_expr) es
| Var s ->
F.fprintf ppf "%a" Symbol.print s
| App (s, es) ->
F.fprintf ppf "%a([%a])"
Symbol.print s
(Misc.pprint_many false "; " print_expr) es
| Bin (e1, op, e2) ->
F.fprintf ppf "(%a %s %a)"
print_expr e1
(bop_to_string op)
print_expr e2
| MBin (e1, ops, e2) ->
F.fprintf ppf "(%a [%s] %a)"
print_expr e1
(ops |>: bop_to_string |> String.concat " ; ")
print_expr e2
| Ite(ip,te,ee) ->
F.fprintf ppf "(%a ? %a : %a)"
print_pred ip
print_expr te
print_expr ee
| Fld(s, e) ->
F.fprintf ppf "%a.%s" print_expr e s
| Cst(e,t) ->
F.fprintf ppf "(%a : %a)"
print_expr e
Sort.print t
| Bot ->
F.fprintf ppf "_|_"
and print_pred ppf p = match puw p with
| True ->
F.fprintf ppf "true"
| False ->
F.fprintf ppf "false"
| Bexp (App (s, es), _) ->
F.fprintf ppf "%a(%a)" Symbol.print s (Misc.pprint_many false ", " print_expr) es
| Bexp e ->
F.fprintf ppf "(Bexp %a)" print_expr e
| Not p ->
F.fprintf ppf "(~ (%a))" print_pred p
| Imp (p1, p2) ->
F.fprintf ppf "(%a => %a)" print_pred p1 print_pred p2
| Iff (p1, p2) ->
F.fprintf ppf "(%a <=> %a)" print_pred p1 print_pred p2
| And ps -> begin match ps with [] -> F.fprintf ppf "true" | _ ->
F.fprintf ppf "&& %a" (Misc.pprint_many_brackets true print_pred) ps
end
| Or ps -> begin match ps with [] -> F.fprintf ppf "false" | _ ->
F.fprintf ppf "|| %a" (Misc.pprint_many_brackets true print_pred) ps
end
| Atom (e1, r, e2) ->
(* F.fprintf ppf "@[(%a %s %a)@]" *)
F.fprintf ppf "(%a %s %a)"
print_expr e1
(brel_to_string r)
print_expr e2
| MAtom (e1, rs, e2) ->
F.fprintf ppf "(%a [%a] %a)"
(* F.fprintf ppf "@[(%a [%a] %a)@]" *)
print_expr e1
(Misc.pprint_many false " ; " print_brel) rs
print_expr e2
| Forall (qs, p) ->
F.fprintf ppf "forall [%a] . %a"
(Misc.pprint_many false "; " print_binding) qs
print_pred p
let rec expr_to_string e =
match euw e with
| Con c ->
Constant.to_string c
| MExp es ->
Printf.sprintf "[%s]" (es |>: expr_to_string |> String.concat " ; ")
| Var s ->
Symbol.to_string s
| App (s, es) ->
Printf.sprintf "%s([%s])"
(Symbol.to_string s)
(es |> List.map expr_to_string |> String.concat "; ")
| Bin (e1, op, e2) ->
Printf.sprintf "(%s %s %s)"
(expr_to_string e1) (bop_to_string op) (expr_to_string e2)
| MBin (e1, ops, e2) ->
Printf.sprintf "(%s [%s] %s)"
(expr_to_string e1)
(ops |> List.map bop_to_string |> String.concat "; ")
(expr_to_string e2)
| Ite(ip,te,ee) ->
Printf.sprintf "(%s ? %s : %s)"
(pred_to_string ip) (expr_to_string te) (expr_to_string ee)
| Fld(s,e) ->
Printf.sprintf "%s.%s" (expr_to_string e) s
| Cst(e,t) ->
Printf.sprintf "(%s : %s)" (expr_to_string e) (Sort.to_string t)
| Bot ->
Printf.sprintf "_|_"
and pred_to_string p =
match puw p with
| True ->
"true"
| False ->
"false"
| Bexp e ->
Printf.sprintf "(Bexp %s)" (expr_to_string e)
| Not p ->
Printf.sprintf "(~ (%s))" (pred_to_string p)
| Imp (p1, p2) ->
Printf.sprintf "(%s => %s)" (pred_to_string p1) (pred_to_string p2)
| Iff (p1, p2) ->
Printf.sprintf "(%s <=> %s)" (pred_to_string p1) (pred_to_string p2)
| And ps ->
Printf.sprintf "&& [%s]" (List.map pred_to_string ps |> String.concat " ; ")
| Or ps ->
Printf.sprintf "|| [%s]" (List.map pred_to_string ps |> String.concat ";")
| Atom (e1, r, e2) ->
Printf.sprintf "(%s %s %s)"
(expr_to_string e1) (brel_to_string r) (expr_to_string e2)
| MAtom (e1, rs, e2) ->
Printf.sprintf "(%s [%s] %s)"
(expr_to_string e1)
(List.map brel_to_string rs |> String.concat " ; ")
(expr_to_string e2)
| Forall (qs,p) ->
Printf.sprintf "forall [%s] . %s"
(List.map bind_to_string qs |> String.concat "; ") (pred_to_string p)
let rec pred_map hp he fp fe p =
let rec pm p =
try PredHash.find hp p with Not_found -> begin
let p' =
match puw p with
| True | False as p1 ->
p1
| And ps ->
And (List.map pm ps)
| Or ps ->
Or (List.map pm ps)
| Not p ->
Not (pm p)
| Imp (p1, p2) ->
Imp (pm p1, pm p2)
| Iff (p1, p2) ->
Iff (pm p1, pm p2)
| Bexp e ->
Bexp (expr_map hp he fp fe e)
| Atom (e1, r, e2) ->
Atom (expr_map hp he fp fe e1, r, expr_map hp he fp fe e2)
| MAtom (e1, rs, e2) ->
MAtom (expr_map hp he fp fe e1, rs, expr_map hp he fp fe e2)
| Forall (qs, p) ->
Forall (qs, pm p) in
let rv = fp (pwr p') in
let _ = PredHash.add hp p rv in
rv
end in pm p
and expr_map hp he fp fe e =
let rec em e =
try ExprHash.find he e with Not_found -> begin
let e' =
match euw e with
| Con _ | Var _ | Bot as e1 ->
e1
| MExp es ->
MExp (List.map em es)
| App (f, es) ->
App (f, List.map em es)
| Bin (e1, op, e2) ->
Bin (em e1, op, em e2)
| MBin (e1, ops, e2) ->
MBin (em e1, ops, em e2)
| Ite (ip, te, ee) ->
Ite (pred_map hp he fp fe ip, em te, em ee)
| Fld (s, e1) ->
Fld (s, em e1)
| Cst (e1, t) ->
Cst (em e1, t)
in
let rv = fe (ewr e') in
let _ = ExprHash.add he e rv in
rv
end in em e
let rec pred_iter fp fe pw =
begin match puw pw with
| True | False -> ()
| Bexp e -> expr_iter fp fe e
| Not p -> pred_iter fp fe p
| Imp (p1, p2) -> pred_iter fp fe p1; pred_iter fp fe p2
| Iff (p1, p2) -> pred_iter fp fe p1; pred_iter fp fe p2
| And ps | Or ps -> List.iter (pred_iter fp fe) ps
| Atom (e1, _, e2) -> expr_iter fp fe e1; expr_iter fp fe e2
| MAtom (e1, _, e2) -> expr_iter fp fe e1; expr_iter fp fe e2
| Forall (_, p) -> pred_iter fp fe p (* pmr: looks wrong, but so does pred_map *)
end;
fp pw
and expr_iter fp fe ew =
begin match puw ew with
| Con _ | Var _ | Bot ->
()
| MExp es ->
List.iter (expr_iter fp fe) es
| App (_, es) ->
List.iter (expr_iter fp fe) es
| Bin (e1, _, e2) ->
expr_iter fp fe e1; expr_iter fp fe e2
| MBin (e1, _, e2) ->
expr_iter fp fe e1; expr_iter fp fe e2
| Ite (ip, te, ee) ->
pred_iter fp fe ip; expr_iter fp fe te; expr_iter fp fe ee
| Fld (_, e1) | Cst (e1, _) ->
expr_iter fp fe e1
end;
fe ew
let esub x e = function
| (Var y), _ when x = y -> e
| _ as e1 -> e1
let expr_subst hp he e x e' =
expr_map hp he id (esub x e') e
let pred_subst hp he p x e' =
pred_map hp he id (esub x e') p
module Expression =
struct
module Hash = ExprHash
let to_string = expr_to_string
(* let print = fun fmt e -> Format.pp_print_string fmt (to_string e)
*)
let print = print_expr
let show = print Format.std_formatter
let map fp fe e =
let hp = PredHash.create 251 in
let he = ExprHash.create 251 in
expr_map hp he fp fe e
let iter fp fe e =
expr_iter fp fe e
let subst e x e' =
map id (esub x e') e
let substs e xes =
map id (fun e -> List.fold_left (esub |> Misc.uncurry |> Misc.flip) e xes) e
let support e =
let xs = ref Symbol.SSet.empty in
iter un begin function
| (Var x), _
| (App (x,_)),_ -> xs := Symbol.SSet.add x !xs
| _ -> ()
end e;
Symbol.SSet.elements !xs |> List.sort compare
let unwrap = euw
let has_bot p =
let r = ref false in
iter un begin function
| Bot, _ -> r := true
| _ -> ()
end p;
!r
end
module Predicate = struct
module Hash = PredHash
let to_string = pred_to_string
let print = print_pred
let show = print Format.std_formatter
let map fp fe p =
let hp = PredHash.create 251 in
let he = ExprHash.create 251 in
pred_map hp he fp fe p
let iter fp fe p =
pred_iter fp fe p
let subst p x e' =
map id (esub x e') p
let substs p xes =
map id (fun e -> List.fold_left (esub |> Misc.uncurry |> Misc.flip) e xes) p
let support p =
let xs = ref Symbol.SSet.empty in
iter un begin function
| (Var x), _
| (App (x,_)),_ -> xs := Symbol.SSet.add x !xs;
| _ -> ()
end p;
Symbol.SSet.elements !xs |> List.sort compare
(*
let size p =
let c = ref 0 in
let f = fun _ -> incr c in
let _ = iter f f p in
!c
let size p =
let c = ref 0 in
let _ = iter (fun _ -> incr c) p in
!c
*)
let unwrap = puw
let is_contra =
let t = PredHash.create 17 in
let _ = [pFalse; pNot pTrue; pAtom (zero, Eq, one); pAtom (one, Eq, zero)]
|> List.iter (fun p-> PredHash.replace t p ()) in
fun p -> PredHash.mem t p
let rec is_tauto = function
| Atom(e1, Eq, e2), _ -> snd e1 == snd e2
| Imp (p1, p2), _ -> snd p1 == snd p2
| And ps, _ -> List.for_all is_tauto ps
| Or ps, _ -> List.exists is_tauto ps
| True, _ -> true
| _ -> false
let has_bot p =
let r = ref false in
iter un begin function
| Bot, _ -> r := true
| _ -> ()
end p;
!r
end
let print_stats _ =
Printf.printf "Ast Stats. [none] \n"
(********************************************************************************)
(************************** Rationalizing Division ******************************)
(********************************************************************************)
let expr_isdiv = function
| Bin (_, Div, _), _ -> true
| _ -> false
let pull_divisor = function
| Bin (_, Div, (Con (Constant.Int i),_)), _ -> i
| _ -> 1
let calc_cm e1 e2 =
pull_divisor e1 * pull_divisor e2
let rec apply_mult m = function
| Bin (e, Div, (Con (Constant.Int d),_)), _ ->
let _ = assert ((m/d) * d = m) in
eTim ((eCon (Constant.Int (m/d))), e)
| Bin (e1, op, e2), _ ->
eBin (apply_mult m e1, op, apply_mult m e2)
| Con (Constant.Int i), _ ->
eCon (Constant.Int (i*m))
| e ->
eTim (eCon (Constant.Int m), e)
let rec pred_isdiv = function
| True,_ | False,_ ->
false
| And ps,_ | Or ps,_ ->
List.exists pred_isdiv ps
| Not p, _ | Forall (_, p), _ ->
pred_isdiv p
| Imp (p1, p2), _ ->
pred_isdiv p1 || pred_isdiv p2
| Iff (p1, p2), _ ->
pred_isdiv p1 || pred_isdiv p2
| Bexp e, _ ->
expr_isdiv e
| Atom (e1, _, e2), _ ->
expr_isdiv e1 || expr_isdiv e2
| _ -> failwith "Unexpected: pred_isdiv"
let bound m e e1 e2 =
pAnd [pAtom (apply_mult m e, Gt, apply_mult m e2);
pAtom(apply_mult m e, Le, apply_mult m e1)]
let rec fixdiv = function
| p when not (pred_isdiv p) ->
p
| Atom ((Var _,_) as e, Eq, e1), _ | Atom ((Con _, _) as e, Eq, e1), _ ->
bound (calc_cm e e1) e e1 (eBin (e1, Minus, one))
| And ps, _ ->
pAnd (List.map fixdiv ps)
| Or ps, _ ->
pOr (List.map fixdiv ps)
| Imp (p1, p2), _ ->
pImp (fixdiv p1, fixdiv p2)
| Iff (p1, p2), _ ->
pIff (fixdiv p1, fixdiv p2)
| Not p, _ ->
pNot (fixdiv p)
| p -> p
(***************************************************************************)
(************* Type Checking Expressions and Predicates ********************)
(***************************************************************************)
let sortcheck_sym f s = f s
(* try Some (f s) with _ -> None *)
let sortcheck_loc f = function
| Sort.Loc s -> sortcheck_sym f (Symbol.of_string s)
| Sort.Lvar _ -> None
| Sort.LFun -> None
let rec sortcheck_expr f e =
match euw e with
| Bot ->
None
| Con _ ->
Some Sort.Int
| Var s ->