forked from cubicle-model-checker/cubicle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.ml
613 lines (496 loc) · 16.5 KB
/
types.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
(**************************************************************************)
(* *)
(* Cubicle *)
(* *)
(* Copyright (C) 2011-2014 *)
(* *)
(* Sylvain Conchon and Alain Mebsout *)
(* Universite Paris-Sud 11 *)
(* *)
(* *)
(* This file is distributed under the terms of the Apache Software *)
(* License version 2.0 *)
(* *)
(**************************************************************************)
open Format
open Options
open Util
module HSet = Hstring.HSet
type op_comp = Eq | Lt | Le | Neq
type sort = Glob | Constr | Var
type const =
ConstInt of Num.num | ConstReal of Num.num | ConstName of Hstring.t
let compare_const c1 c2 = match c1, c2 with
| (ConstInt n1 | ConstReal n1), (ConstInt n2 | ConstReal n2) ->
Num.compare_num n1 n2
| (ConstInt _ | ConstReal _), _ -> -1
| _, (ConstInt _ | ConstReal _) -> 1
| ConstName h1, ConstName h2 -> Hstring.compare h1 h2
module MConst = struct
module M = Map.Make (struct type t = const let compare = compare_const end)
include M
exception Choose of const * int
let choose m =
try
M.iter (fun c i -> raise (Choose (c, i))) m;
raise Not_found
with Choose (c, i) -> c, i
let is_num m =
if M.cardinal m = 1 then
match choose m with
| (ConstInt n | ConstReal n), i -> Some (Num.mult_num (Num.Int i) n)
| _ -> None
else None
end
module Var = struct
type t =
| V of Hstring.t * sort
| T of Hstring.t * Variable.t list
let compare x y =
match x, y with
| V(a1,s1), V(a2, s2) ->
let c = Stdlib.compare s1 s2 in
if c <> 0 then c
else Hstring.compare a1 a2
| T(t1,l1), T(t2,l2) ->
let c = Hstring.compare t1 t2 in
if c<>0 then c
else Variable.compare_list l1 l2
| V(_,_), T(_,_) -> -1
| T(_,_), V(_,_) -> 1
end
module VMap = Map.Make(Var)
type cst = CInt of Num.num | CReal of Num.num | CName of Hstring.t
type poly = cst VMap.t * cst
type term =
| Const of int MConst.t
| Elem of Hstring.t * sort
| Access of Hstring.t * Variable.t list
| Arith of term * int MConst.t
(* | NArith of cst VMap.t * cst*)
let is_int_const = function
| ConstInt _ -> true
| ConstReal _ -> false
| ConstName n ->
Hstring.equal (snd (Smt.Symbol.type_of n)) Smt.Type.type_int
let compare_constants = MConst.compare Stdlib.compare
let num_of_const = function
| ConstInt n | ConstReal n -> n
| _ -> assert false
let add_constnum c i num =
match c, num with
| ConstInt n, ConstInt m ->
ConstInt (Num.add_num (Num.mult_num (Num.Int i) n) m)
| (ConstInt n | ConstReal n), (ConstInt m | ConstReal m) ->
ConstReal (Num.add_num (Num.mult_num (Num.Int i) n) m)
| _ -> assert false
let split_num_consts cs =
MConst.fold
(fun c i (cs, num) ->
match c, num with
| ConstName _, _ -> MConst.add c i cs, num
| _ -> cs, add_constnum c i num)
cs (MConst.empty, ConstInt (Num.Int 0))
let add_constant c i cs =
match c with
| ConstInt _ | ConstReal _ ->
let cs, num = split_num_consts cs in
let num = add_constnum c i num in
if Num.compare_num (num_of_const num) (Num.Int 0) = 0 then cs
else MConst.add num 1 cs
| _ ->
let i' = try MConst.find c cs with Not_found -> 0 in
let i = i + i' in
if i = 0 then MConst.remove c cs
else MConst.add c i cs
let add_constants cs1 cs2 =
let m = MConst.fold add_constant cs2 cs1 in
if MConst.is_empty m then
let c0 =
if is_int_const (fst (MConst.choose cs1)) then
ConstInt (Num.Int 0)
else ConstReal (Num.Int 0)
in
MConst.add c0 1 m
else m
let mult_const a =
MConst.map (fun i -> i * a)
let const_sign c =
try
let n = ref (Num.Int 0) in
MConst.iter (fun c i ->
if i <> 0 then
match c with
| ConstName _ -> raise Exit
| ConstInt a | ConstReal a ->
n := Num.add_num (Num.mult_num (Num.Int i) a) !n) c;
Some (Num.compare_num !n (Num.Int 0))
with Exit -> None
let const_nul c = const_sign c = Some 0
module Term = struct
type t = term
let rec compare t1 t2 =
match t1, t2 with
| Const c1, Const c2 -> compare_constants c1 c2
| Const _, _ -> -1 | _, Const _ -> 1
| Elem (_, (Constr | Var)), Elem (_, Glob) -> -1
| Elem (_, Glob), Elem (_, (Constr | Var)) -> 1
| Elem (s1, _), Elem (s2, _) -> Hstring.compare s1 s2
| Elem _, _ -> -1 | _, Elem _ -> 1
| Access (a1, l1), Access (a2, l2) ->
let c = Hstring.compare a1 a2 in
if c<>0 then c else Hstring.compare_list l1 l2
| Access _, _ -> -1 | _, Access _ -> 1
| Arith (t1, cs1), Arith (t2, cs2) ->
let c = compare t1 t2 in
if c<>0 then c else compare_constants cs1 cs2
let hash = Hashtbl.hash_param 50 50
let equal t1 t2 = compare t1 t2 = 0
let htrue = Hstring.make "True"
let hfalse = Hstring.make "False"
module STerm = Set.Make (struct
type t = term
let compare = compare
end)
module Set = STerm
let rec subst sigma t =
match t with
| Elem (x, s) ->
let nx = Variable.subst sigma x in
if x == nx then t
else Elem (nx, s)
| Access (a, lz) ->
Access (a, List.map
(fun z ->
try Variable.subst sigma z with Not_found -> z) lz)
| Arith (x, c) -> Arith (subst sigma x, c)
| _ -> t
let rec variables = function
| Elem (x, Var) -> Variable.Set.singleton x
| Access (_, lx) ->
List.fold_left (fun acc x -> Variable.Set.add x acc)
Variable.Set.empty lx
| Arith (t, _) -> variables t
| _ -> Variable.Set.empty
let variables_proc t = Variable.Set.filter Variable.is_proc (variables t)
let rec type_of = function
| Const cs ->
if is_int_const (fst (MConst.choose cs)) then
Smt.Type.type_int
else Smt.Type.type_real
| Elem (x, Var) -> Smt.Type.type_proc
| Elem (x, _) | Access (x, _) -> snd (Smt.Symbol.type_of x)
| Arith(t, _) -> type_of t
let rec print_strings fmt = function
| [] -> ()
| [s] -> fprintf fmt "%s" s
| s :: l -> fprintf fmt "%s %a" s print_strings l
let print_const fmt = function
| ConstInt n | ConstReal n -> fprintf fmt "%s" (Num.string_of_num n)
| ConstName n -> fprintf fmt "%a" Hstring.print n
let print_cs alone fmt cs =
let first = ref true in
MConst.iter
(fun c i ->
if !first && alone && i >= 0 then
if i = 1 then print_const fmt c
else fprintf fmt "%s %a" (string_of_int (abs i)) print_const c
else
fprintf fmt " %s %a"
(if i = 1 then "+" else if i = -1 then "-"
else if i < 0 then "- "^(string_of_int (abs i))
else "+ "^(string_of_int (abs i)))
print_const c;
first := false;
) cs
let rec print fmt = function
| Const cs -> print_cs true fmt cs
| Elem (s, _) -> fprintf fmt "%a" Hstring.print s
| Access (a, li) ->
fprintf fmt "%a[%a]" Hstring.print a (Hstring.print_list ", ") li
| Arith (x, cs) ->
fprintf fmt "@[%a%a@]" print x (print_cs false) cs
end
module rec Atom : sig
type t =
| True
| False
| Comp of Term.t * op_comp * Term.t
| Ite of SAtom.t * t * t
val compare : t -> t -> int
val trivial_is_implied : t -> t -> int
val neg : t -> t
val hash : t -> int
val equal : t -> t -> bool
val subst : Variable.subst -> t -> t
val has_var : Variable.t -> t -> bool
val has_vars : Variable.t list -> t -> bool
val variables : t -> Variable.Set.t
val variables_proc : t -> Variable.Set.t
val print : Format.formatter -> t -> unit
val print_atoms : bool -> string -> Format.formatter -> t list -> unit
end = struct
type t =
| True
| False
| Comp of Term.t * op_comp * Term.t
| Ite of SAtom.t * t * t
let rec compare a1 a2 =
match a1, a2 with
| True, True -> 0
| True, _ -> -1 | _, True -> 1
| False, False -> 0
| False, _ -> -1 | _, False -> 1
| Comp (x1, op1, y1), Comp (x2, op2, y2) ->
let c1 = Term.compare x1 x2 in
if c1 <> 0 then c1
else
let c0 = Stdlib.compare op1 op2 in
if c0 <> 0 then c0
else
let c2 = Term.compare y1 y2 in c2
| Comp _, _ -> -1 | _, Comp _ -> 1
| Ite (sa1, a1, b1), Ite (sa2, a2, b2) ->
let c = SAtom.compare sa1 sa2 in
if c<>0 then c else
let c = compare a1 a2 in
if c<>0 then c else compare b1 b2
let trivial_is_implied a1 a2 =
match a1, a2 with
| Comp (x1, Neq, Elem (v1, (Constr|Var))),
Comp (x2, Eq, Elem (v2, (Constr|Var)))
when not (Hstring.equal v1 v2) && Term.compare x1 x2 = 0 -> 0
| _ -> compare a1 a2
let neg = function
| True -> False
| False -> True
| Comp (c, Eq, (Elem (x, Constr))) when Hstring.equal x Term.hfalse ->
Comp (c, Eq, (Elem (Term.htrue, Constr)))
| Comp (c, Eq, (Elem (x, Constr))) when Hstring.equal x Term.htrue ->
Comp (c, Eq, (Elem (Term.hfalse, Constr)))
| Comp (x, Eq, y) -> Comp (x, Neq, y)
| Comp (x, Lt, y) -> Comp (y, Le, x)
| Comp (x, Le, y) -> Comp (y, Lt, x)
| Comp (x, Neq, y) -> Comp (x, Eq, y)
| _ -> assert false
let hash (sa: Atom.t) = Hashtbl.hash_param 50 100 sa
let equal x y = compare x y = 0
let rec subst sigma a =
match a with
| Ite (sa, a1, a2) ->
Ite(SAtom.subst sigma sa,
subst sigma a1,
subst sigma a2)
| Comp (x, op, y) ->
let sx = Term.subst sigma x in
let sy = Term.subst sigma y in
if x == sx && y == sy then a
else Comp(sx, op, sy)
| _ -> a
let rec has_vars_term vs = function
| Elem (x, Var) -> Hstring.list_mem x vs
| Access (_, lx) -> List.exists (fun z -> Hstring.list_mem z lx) vs
| Arith (x, _) -> has_vars_term vs x
| _ -> false
let rec has_vars vs = function
| True | False -> false
| Comp (x, _, y) -> has_vars_term vs x || has_vars_term vs y
| Ite (sa, a1, a2) ->
SAtom.exists (has_vars vs) sa || has_vars vs a1 || has_vars vs a2
let has_var v = has_vars [v]
let rec variables = function
| True | False -> Variable.Set.empty
| Comp (t1, _, t2) ->
Variable.Set.union (Term.variables t1) (Term.variables t2)
| Ite (sa, a1, a2) ->
let acc = Variable.Set.union (variables a1) (variables a2) in
Variable.Set.union acc (SAtom.variables sa)
let variables_proc a = Variable.Set.filter Variable.is_proc (variables a)
let str_op_comp = function Eq -> "=" | Lt -> "<" | Le -> "<=" | Neq -> "<>"
let rec print fmt = function
| True -> fprintf fmt "true"
| False -> fprintf fmt "false"
| Comp (x, op, y) ->
fprintf fmt "%a %s %a" Term.print x (str_op_comp op) Term.print y
| Ite (la, a1, a2) ->
fprintf fmt "@[ite(%a,@ %a,@ %a)@]"
(print_atoms false "&&") (SAtom.elements la) print a1 print a2
and print_atoms inline sep fmt = function
| [] -> ()
| [a] -> print fmt a
| a::l ->
if inline then
fprintf fmt "%a %s@ %a" print a sep (print_atoms inline sep) l
else
fprintf fmt "%a %s@\n%a" print a sep (print_atoms inline sep) l
end
and SAtom : sig
include Set.S with type elt = Atom.t
val equal : t -> t -> bool
val hash : t -> int
val subst : Variable.subst -> t -> t
val variables : t -> Variable.Set.t
val variables_proc : t -> Variable.Set.t
val print : Format.formatter -> t -> unit
val print_inline : Format.formatter -> t -> unit
end = struct
include Set.Make(Atom)
let add a s =
match a with
| Atom.True -> s
| Atom.False -> singleton Atom.False
| _ -> if mem Atom.False s then s else add a s
let equal sa1 sa2 = compare sa1 sa2 = 0
let hash (sa:t) = Hashtbl.hash_param 100 500 sa
let subst sigma sa =
if Variable.is_subst_identity sigma then sa
else
fold (fun a -> add (Atom.subst sigma a)) sa empty
let subst sigma sa =
TimerApply.start ();
let sa = subst sigma sa in
TimerApply.pause ();
sa
let variables sa =
fold (fun a -> Variable.Set.union (Atom.variables a)) sa Variable.Set.empty
let variables_proc sa = Variable.Set.filter Variable.is_proc (variables sa)
let print fmt sa =
fprintf fmt "@[<hov>%a@]" (Atom.print_atoms false "&&") (elements sa)
let print_inline fmt sa =
fprintf fmt "@[%a@]" (Atom.print_atoms true "&&") (elements sa)
end
module ArrayAtom = struct
type t = Atom.t array
let equal a1 a2 =
let n = Array.length a1 in
let n2 = Array.length a2 in
if n <> n2 then false
else
let res = ref true in
let i = ref 0 in
while !res && !i < n do
res := (Atom.compare a1.(!i) a2.(!i) = 0);
incr i
done;
!res
let hash = Hashtbl.hash_param 100 500
let subset a1 a2 =
TimerSubset.start ();
let n1 = Array.length a1 in
let n2 = Array.length a2 in
let s =
if n1 > n2 then false
else
let i1 = ref 0 in
let i2 = ref 0 in
while !i1 < n1 && !i2 < n2 do
let c = Atom.compare a1.(!i1) a2.(!i2) in
if c = 0 then (incr i1; incr i2)
else if c < 0 then i2 := n2
else incr i2
done;
!i1 = n1
in
TimerSubset.pause ();
s
let trivial_is_implied a1 a2 =
TimerSubset.start ();
let n1 = Array.length a1 in
let n2 = Array.length a2 in
let s =
if n1 > n2 then false
else
let i1 = ref 0 in
let i2 = ref 0 in
while !i1 < n1 && !i2 < n2 do
let c = Atom.trivial_is_implied a1.(!i1) a2.(!i2) in
if c = 0 then (incr i1; incr i2)
else if c < 0 then i2 := n2
else incr i2
done;
!i1 = n1
in
TimerSubset.pause ();
s
let subset = trivial_is_implied
let of_satom s =
Array.of_list (SAtom.elements s)
let to_satom =
Array.fold_left (fun s a -> SAtom.add a s) SAtom.empty
let union a b = Array.append a b
(* let cpt = ref 0 in fun a b -> incr cpt; eprintf "%d@." !cpt; *)
(* Array.append a b *)
(* let a = Array.append a1 a2 in *)
(* Array.fast_sort Atom.compare a; a *)
let apply_subst sigma a =
TimerApply.start ();
if Variable.is_subst_identity sigma then (TimerApply.pause (); a)
else
let a' = Array.init (Array.length a) (fun i -> Atom.subst sigma a.(i)) in
Array.fast_sort Atom.compare a';
TimerApply.pause ();
a'
let alpha atoms args =
let subst = Variable.build_subst args Variable.alphas in
List.map snd subst, apply_subst subst atoms
let nb_diff a1 a2 =
TimerSubset.start ();
let cpt = ref 0 in
let n1 = Array.length a1 in
let n2 = Array.length a2 in
let i1 = ref 0 in
let i2 = ref 0 in
while !i1 < n1 && !i2 < n2 do
let c = Atom.compare a1.(!i1) a2.(!i2) in
if c = 0 then (incr i1; incr i2)
else if c < 0 then (incr cpt; incr i1)
else incr i2
done;
TimerSubset.pause ();
!cpt + (n1 - !i1)
let compare_nb_diff a p1 p2 =
Stdlib.compare (nb_diff p1 a) (nb_diff p2 a)
let nb_common a1 a2 =
TimerSubset.start ();
let cpt = ref 0 in
let n1 = Array.length a1 in
let n2 = Array.length a2 in
let i1 = ref 0 in
let i2 = ref 0 in
while !i1 < n1 && !i2 < n2 do
let c = Atom.compare a1.(!i1) a2.(!i2) in
if c = 0 then (incr cpt; incr i1; incr i2)
else if c < 0 then incr i1
else incr i2
done;
TimerSubset.pause ();
(float_of_int !cpt) /. (float_of_int n1)
let compare_nb_common a p1 p2 =
Stdlib.compare (nb_common p2 a) (nb_common p1 a)
let diff a1 a2 =
let n1 = Array.length a1 in
let n2 = Array.length a2 in
let i1 = ref 0 in
let i2 = ref 0 in
let cpt = ref 0 in
let d = Array.copy a1 in
while !i1 < n1 && !i2 < n2 do
let ai1 = a1.(!i1) in
let c = Atom.compare ai1 a2.(!i2) in
if c = 0 then (incr i1; incr i2)
else if c < 0 then begin
d.(!cpt) <- ai1;
incr cpt;
incr i1
end
else incr i2
done;
while !i1 < n1 do
d.(!cpt) <- a1.(!i1);
incr cpt;
incr i1
done;
Array.sub d 0 !cpt
let print fmt a =
fprintf fmt "@[<hov>%a@]" (Atom.print_atoms false "&&") (Array.to_list a)
end