Source file abstract_interp.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
exception Error_Top
exception Error_Bottom
exception Not_less_than
exception Can_not_subdiv
type truth = True | False | Unknown
let inv_truth = function
| Unknown -> Unknown
| True -> False
| False -> True
module Comp = struct
type t = Lt | Gt | Le | Ge | Eq | Ne
type result = truth = True | False | Unknown
let inv = function
| Gt -> Le
| Lt -> Ge
| Le -> Gt
| Ge -> Lt
| Eq -> Ne
| Ne -> Eq
let sym = function
| Gt -> Lt
| Lt -> Gt
| Le -> Ge
| Ge -> Le
| Eq -> Eq
| Ne -> Ne
let pretty_comp fmt = function
| Gt -> Format.pp_print_string fmt ">"
| Lt -> Format.pp_print_string fmt "<"
| Le -> Format.pp_print_string fmt "<="
| Ge -> Format.pp_print_string fmt ">="
| Eq -> Format.pp_print_string fmt "=="
| Ne -> Format.pp_print_string fmt "!="
let inv_result = function
| Unknown -> Unknown
| True -> False
| False -> True
end
module Int = struct
include (Integer: module type of Integer with type t = Integer.t)
module Set = Set.Make(Integer)
let ten_thousands = Integer.of_int 10000;;
let minus_ten_thousands = Integer.of_int @@ -10000;;
let pretty fmt v =
if Integer.ge v ten_thousands
|| Integer.le v minus_ten_thousands
then Format.fprintf fmt "%a" (Integer.pretty ~hexa:true) v
else Format.fprintf fmt "%s" @@ Z.to_string v
(** execute [f] on [inf], [inf + step], ... *)
let fold f ~inf ~sup ~step acc =
let nb_loop = e_div (sub sup inf) step in
let rec fold_incr ~counter ~inf acc =
if equal counter onethousand then
Codex_log.performance_warning
"enumerating %a integers" pretty nb_loop;
if le inf sup then begin
fold_incr ~counter:(succ counter) ~inf:(add step inf) (f inf acc)
end else acc
in
let rec fold_decr ~counter ~sup acc =
if equal counter onethousand then
Codex_log.performance_warning
"enumerating %a integers" pretty nb_loop;
if le inf sup then begin
fold_decr ~counter:(succ counter) ~sup:(add step sup) (f sup acc)
end else acc
in
if le zero step
then fold_incr ~counter:zero ~inf acc
else fold_decr ~counter:zero ~sup acc
end
module Rel = struct
include Int
let check ~rem ~modu =
zero <= rem && rem < modu
let add_abs = add
let sub_abs = sub
end
module Bool = struct
type t = Top | True | False | Bottom
let hash (b : t) = Hashtbl.hash b
let equal (b1 : t) (b2 : t) = b1 = b2
let compare (b1 : t) (b2 : t) = Stdlib.compare b1 b2
let pretty fmt = function
| Top -> Format.fprintf fmt "Top"
| True -> Format.fprintf fmt "True"
| False -> Format.fprintf fmt "False"
| Bottom -> Format.fprintf fmt "Bottom"
let is_included t1 t2 = match t1, t2 with
| Bottom, _
| _, Top
| True, True
| False, False -> true
| _ -> false
let bottom = Bottom
let top = Top
let join b1 b2 = match b1, b2 with
| Top, _
| _, Top
| True, False -> Top
| True, _
| _, True -> True
| False, _
| _, False -> False
| Bottom, Bottom -> Bottom
let narrow b1 b2 = match b1, b2 with
| Bottom, _
| _, Bottom
| True, False -> Bottom
| True, _
| _, True -> True
| False, _
| _, False -> False
| Top, Top -> Top
let link = join
let meet = narrow
type widen_hint = unit
let widen () = join
let cardinal_zero_or_one b = not (equal b top)
let intersects b1 b2 = match b1, b2 with
| Bottom, _ | _, Bottom -> false
| _, Top | Top, _ -> true
| False, False | True, True -> true
| False, True | True, False -> false
let diff b1 b2 = match b1, b2 with
| b1, Bottom -> b1
| _, Top -> Bottom
| Bottom, _ -> Bottom
| Top, True -> False
| Top, False -> True
| True, True -> Bottom
| True, False -> True
| False, True -> False
| False, False -> Bottom
let diff_if_one b1 b2 = match b1, b2 with
| b1, Top -> b1
| _, _ -> diff b1 b2
let fold_enum f b init =
let elements = match b with
| Top -> [True; False]
| True -> [True]
| False -> [False]
| Bottom -> []
in
List.fold_right (fun b acc -> f b acc) elements init
let cardinal = function
| Top -> 2
| True | False -> 1
| Bottom -> 0
let cardinal_less_than b i =
let c = cardinal b in
if c > i then raise Not_less_than
else c
type blt = t
end