Source file setDomain.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
(** Set domains. *)
module Pretty = GoblintCil.Pretty
open Pretty
exception Unsupported of string
let unsupported s = raise (Unsupported s)
(** A set domain must support all the standard library set operations.
They have been copied instead of included since our [empty] has a different signature. *)
module type S =
sig
include Lattice.S
type elt
val empty: unit -> t
val is_empty: t -> bool
val mem: elt -> t -> bool
val add: elt -> t -> t
val singleton: elt -> t
val remove: elt -> t -> t
(** See {!Set.S.remove}.
{b NB!} On set abstractions this is a {e strong} removal,
i.e. all subsumed elements are also removed.
@see <https://github.com/goblint/analyzer/pull/809#discussion_r936336198> *)
val union: t -> t -> t
val inter: t -> t -> t
val diff: t -> t -> t
(** See {!Set.S.diff}.
{b NB!} On set abstractions this is a {e strong} removal,
i.e. all subsumed elements are also removed.
@see <https://github.com/goblint/analyzer/pull/809#discussion_r936336198> *)
val subset: t -> t -> bool
val disjoint: t -> t -> bool
val iter: (elt -> unit) -> t -> unit
(** See {!Set.S.iter}.
On set abstractions this iterates only over canonical elements,
not all subsumed elements. *)
val map: (elt -> elt) -> t -> t
(** See {!Set.S.map}.
On set abstractions this maps only canonical elements,
not all subsumed elements. *)
val fold: (elt -> 'a -> 'a) -> t -> 'a -> 'a
(** See {!Set.S.fold}.
On set abstractions this folds only over canonical elements,
not all subsumed elements. *)
val for_all: (elt -> bool) -> t -> bool
(** See {!Set.S.for_all}.
On set abstractions this checks only canonical elements,
not all subsumed elements. *)
val exists: (elt -> bool) -> t -> bool
(** See {!Set.S.exists}.
On set abstractions this checks only canonical elements,
not all subsumed elements. *)
val filter: (elt -> bool) -> t -> t
(** See {!Set.S.filter}.
On set abstractions this filters only canonical elements,
not all subsumed elements. *)
val partition: (elt -> bool) -> t -> t * t
(** See {!Set.S.partition}.
On set abstractions this partitions only canonical elements,
not all subsumed elements. *)
val cardinal: t -> int
(** See {!Set.S.cardinal}.
On set abstractions this counts only canonical elements,
not all subsumed elements. *)
val elements: t -> elt list
(** See {!Set.S.elements}.
On set abstractions this lists only canonical elements,
not all subsumed elements. *)
val of_list: elt list -> t
val min_elt: t -> elt
(** See {!Set.S.min_elt}.
On set abstractions this chooses only a canonical element,
not any subsumed element. *)
val max_elt: t -> elt
(** See {!Set.S.max_elt}.
On set abstractions this chooses only a canonical element,
not any subsumed element. *)
val choose: t -> elt
(** See {!Set.S.choose}.
On set abstractions this chooses only a canonical element,
not any subsumed element. *)
end
(** Subsignature of {!S}, which is sufficient for {!Print}. *)
module type Elements =
sig
type t
type elt
val elements: t -> elt list
val iter: (elt -> unit) -> t -> unit
end
(** Reusable output definitions for sets. *)
module Print (E: Printable.S) (S: Elements with type elt = E.t) =
struct
let pretty () x =
let elts = S.elements x in
let content = List.map (E.pretty ()) elts in
let rec separate x =
match x with
| [] -> []
| [x] -> [x]
| (x::xs) -> x ++ (text "," ++ break) :: separate xs
in
let separated = separate content in
let content = List.fold_left (++) nil separated in
(text "{" ++ align) ++ content ++ (unalign ++ text "}")
(** Short summary for sets. *)
let show x : string =
let all_elems : string list = List.map E.show (S.elements x) in
Printable.get_short_list "{" "}" all_elems
let to_yojson x = [%to_yojson: E.t list] (S.elements x)
let printXml f xs =
BatPrintf.fprintf f "<value>\n<set>\n";
S.iter (E.printXml f) xs;
BatPrintf.fprintf f "</set>\n</value>\n"
end
(** A functor for creating a simple set domain, there is no top element, and
* calling [top ()] will raise an exception *)
module Make (Base: Printable.S): S with
type elt = Base.t and
type t = BatSet.Make (Base).t =
struct
include Printable.Std
include BatSet.Make(Base)
let name () = "Set (" ^ Base.name () ^ ")"
let empty _ = empty
let leq = subset
let join = union
let widen = join
let meet = inter
let narrow = meet
let bot = empty
let is_bot = is_empty
let top () = unsupported "Make.top"
let is_top _ = false
include Print (Base) (
struct
type nonrec t = t
type nonrec elt = elt
let elements = elements
let iter = iter
end
)
let hash x = fold (fun x y -> 13 * y + Base.hash x) x 0
let relift x = map Base.relift x
let pretty_diff () ((x:t),(y:t)): Pretty.doc =
if leq x y then dprintf "%s: These are fine!" (name ()) else
if is_bot y then dprintf "%s: %a instead of bot" (name ()) pretty x else begin
let evil = choose (diff x y) in
Pretty.dprintf "%s: %a not leq %a\n @[because %a@]" (name ()) pretty x pretty y Base.pretty evil
end
let arbitrary () = QCheck.map ~rev:elements of_list @@ QCheck.small_list (Base.arbitrary ())
end
(** A functor for creating a path sensitive set domain, that joins the base
* analysis whenever the user elements coincide. Just as above there is no top
* element, and calling [top ()] will raise an exception *)
module SensitiveConf (C: Printable.ProdConfiguration) (Base: Lattice.S) (User: Printable.S) =
struct
module Elt = Printable.ProdConf (C) (Base) (User)
include Make(Elt)
let name () = "Sensitive " ^ name ()
let leq s1 s2 =
let p (b1,u1) = exists (fun (b2,u2) -> User.equal u1 u2 && Base.leq b1 b2) s2 in
for_all p s1
let pretty_diff () ((x:t),(y:t)): Pretty.doc =
Pretty.dprintf "%s: %a not leq %a" (name ()) pretty x pretty y
let join s1 s2 =
let f (b2,u2) (s1,res) =
let (s1_match, s1_rest) = partition (fun (b1,u1) -> User.equal u1 u2) s1 in
let el =
try let (b1,u1) = choose s1_match in (Base.join b1 b2, u2)
with Not_found -> (b2,u2)
in
(s1_rest, add el res)
in
let (s1', res) = fold f s2 (s1, empty ()) in
union s1' res
let add e s = join (singleton e) s
let meet s1 s2 =
let f (b2,u2) (s1,res) =
let (s1_match, s1_rest) = partition (fun (b1,u1) -> User.equal u1 u2) s1 in
let res =
try
let (b1,u1) = choose s1_match in
add (Base.meet b1 b2, u2) res
with Not_found -> res
in
(s1_rest, res)
in
snd (fold f s2 (s1, empty ()))
end
[@@deprecated]
(** Auxiliary signature for naming the top element *)
module type ToppedSetNames =
sig
val topname: string
end
module LiftTop (S: S) (N: ToppedSetNames): S with
type elt = S.elt and
type t = [`Top | `Lifted of S.t] =
struct
include Printable.Std
include Lattice.LiftTop (S)
type elt = S.elt
let empty () = `Lifted (S.empty ())
let is_empty x =
match x with
| `Top -> false
| `Lifted x -> S.is_empty x
let mem x s =
match s with
| `Top -> true
| `Lifted s -> S.mem x s
let add x s =
match s with
| `Top -> `Top
| `Lifted s -> `Lifted (S.add x s)
let singleton x = `Lifted (S.singleton x)
let remove x s =
match s with
| `Top -> `Top
| `Lifted s -> `Lifted (S.remove x s)
let union x y =
match x, y with
| `Top, _ -> `Top
| _, `Top -> `Top
| `Lifted x, `Lifted y -> `Lifted (S.union x y)
let inter x y =
match x, y with
| `Top, y -> y
| x, `Top -> x
| `Lifted x, `Lifted y -> `Lifted (S.inter x y)
let diff x y =
match x, y with
| x, `Top -> empty ()
| `Top, y -> `Top
| `Lifted x, `Lifted y -> `Lifted (S.diff x y)
let subset x y =
match x, y with
| _, `Top -> true
| `Top, _ -> false
| `Lifted x, `Lifted y -> S.subset x y
let disjoint x y =
match x, y with
| `Top, `Top -> false
| `Lifted x, `Top
| `Top, `Lifted x -> S.is_empty x
| `Lifted x, `Lifted y -> S.disjoint x y
let schema normal abnormal x =
match x with
| `Top -> unsupported abnormal
| `Lifted t -> normal t
let schema_default v f = function
| `Top -> v
| `Lifted x -> f x
let map f x =
match x with
| `Top -> `Top
| `Lifted t -> `Lifted (S.map f t)
let iter f = schema (S.iter f) "iter on `Top"
let fold f x e = schema (fun t -> S.fold f t e) "fold on `Top" x
let for_all f = schema_default false (S.for_all f)
let exists f = schema_default true (S.exists f)
let filter f = schema (fun t -> `Lifted (S.filter f t)) "filter on `Top"
let elements = schema S.elements "elements on `Top"
let of_list xs = `Lifted (S.of_list xs)
let cardinal = schema S.cardinal "cardinal on `Top"
let min_elt = schema S.min_elt "min_elt on `Top"
let max_elt = schema S.max_elt "max_elt on `Top"
let choose = schema S.choose "choose on `Top"
let partition f = schema (fun t -> match S.partition f t
with (a,b) -> (`Lifted a, `Lifted b)) "filter on `Top"
let pretty () x =
match x with
| `Top -> text N.topname
| `Lifted t -> S.pretty () t
let show x : string =
match x with
| `Top -> N.topname
| `Lifted t -> S.show t
let bot () = `Lifted (S.bot ())
let is_bot x =
match x with
| `Top -> false
| `Lifted x -> S.is_bot x
let leq x y =
match x, y with
| _, `Top -> true
| `Top, _ -> false
| `Lifted x, `Lifted y -> S.leq x y
let join x y =
match x, y with
| `Top, _ -> `Top
| _, `Top -> `Top
| `Lifted x, `Lifted y -> `Lifted (S.join x y)
let widen x y =
match x, y with
| `Top, _
| _, `Top -> `Top
| `Lifted x, `Lifted y -> `Lifted (S.widen x y)
let meet x y =
match x, y with
| `Top, y -> y
| x, `Top -> x
| `Lifted x, `Lifted y -> `Lifted (S.meet x y)
let narrow x y =
match x, y with
| `Top, y -> y
| x, `Top -> x
| `Lifted x, `Lifted y -> `Lifted (S.narrow x y)
let arbitrary () = QCheck.set_print show (arbitrary ())
end
(** Functor for creating artificially topped set domains. *)
module ToppedSet (Base: Printable.S) (N: ToppedSetNames): S with
type elt = Base.t and
type t = [`Top | `Lifted of Make (Base).t] =
struct
module S = Make (Base)
include LiftTop (S) (N)
end
module HeadlessSet (Base: Printable.S) =
struct
include Make(Base)
let name () = "Headless " ^ name ()
let pretty () x =
let elts = elements x in
let content = List.map (Base.pretty ()) elts in
let rec separate x =
match x with
| [] -> []
| [x] -> [x]
| (x::xs) -> x ++ (text ", ") ++ line :: separate xs
in
let separated = separate content in
let content = List.fold_left (++) nil separated in
content
let pretty_diff () ((x:t),(y:t)): Pretty.doc =
Pretty.dprintf "%s: %a not leq %a" (name ()) pretty x pretty y
let printXml f xs =
iter (Base.printXml f) xs
end
(** Reverses lattice order of a set domain while keeping the set operations same. *)
module Reverse (Base: S) =
struct
include Base
include Lattice.Reverse (Base)
end
module type FiniteSetElem =
sig
include Printable.S
val elems: t list
(** List of all possible elements. *)
end
module FiniteSet (E: FiniteSetElem) =
struct
module E =
struct
include E
let arbitrary () = QCheck.oneofl E.elems
end
include Make (E)
let top () = of_list E.elems
let is_top x = equal x (top ())
end
(** Set abstracted by a single (joined) element.
Element-wise {!S} operations only observe the single element. *)
module Joined (E: Lattice.S): S with type elt = E.t =
struct
type elt = E.t
include E
let singleton e = e
let of_list es = List.fold_left E.join (E.bot ()) es
let exists p e = p e
let for_all p e = p e
let mem e e' = E.leq e e'
let choose e = e
let elements e = [e]
let remove e e' =
if E.leq e' e then
E.bot ()
else
e'
let map f e = f e
let fold f e a = f e a
let empty () = E.bot ()
let add e e' = E.join e e'
let is_empty e = E.is_bot e
let union e e' = E.join e e'
let diff e e' = remove e' e
let iter f e = f e
let cardinal e =
if is_empty e then
0
else
1
let inter e e' = E.meet e e'
let subset e e' = E.leq e e'
let filter p e = unsupported "Joined.filter"
let partition p e = unsupported "Joined.partition"
let min_elt e = unsupported "Joined.min_elt"
let max_elt e = unsupported "Joined.max_elt"
let disjoint e e' = is_empty (inter e e')
end