-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.ml
377 lines (313 loc) · 10.4 KB
/
util.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
let fst (a,b) = a
let snd (a,b) = b
let min : int list -> int
= fun lst ->
let fst = List.hd lst in
List.fold_left (fun min n -> if min>n then n else min) fst lst
let max : int list -> int
= fun lst ->
let fst = List.hd lst in
List.fold_left (fun max n -> if max<n then n else max) fst lst
let rec fix f x =
let x' = f x in
if x' = x then x'
else fix f x'
let rec decreasing : int list -> bool
= fun lst ->
match lst with
| [] -> true
| h::[] -> true
| h1::h2::t -> if h1>=h2 then decreasing (h2::t) else false
let (<<<) f g = fun x -> f (g x)
let (>>>) f g = fun x -> g (f x)
let id x = x
let flip f = fun y x -> f x y
let domof m = BatMap.foldi (fun k _ set -> BatSet.add k set) m BatSet.empty
(** This applies [List.fold_left], but the argument type is the same with
[PSet.fold]. *)
let list_fold : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
= fun f list init ->
List.fold_left (flip f) init list
let list_map = List.map
let list_filter = List.filter
let list_split = List.split
let list_combine = List.combine
let list_hd = List.hd
let list_tl = List.tl
let list_fold2 : ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
= fun f list1 list2 init ->
let f' acc a b = f a b acc in
List.fold_left2 f' init list1 list2
let list_rev : 'a list -> 'a list
= fun l ->
let rec list_rev_rec l1 l2 =
match l1 with
| [] -> l2
| a :: b -> list_rev_rec b (a :: l2) in
list_rev_rec l []
let find_opt : 'a -> ('a, 'b) BatMap.t -> 'b option
= fun k m ->
try Some (BatMap.find k m) with
| Not_found -> None
let find_def : 'a -> ('a, 'b) BatMap.t -> 'b -> 'b
= fun k m default ->
BatOption.default default (find_opt k m)
let list2set l = list_fold BatSet.add l BatSet.empty
let set2list s = BatSet.fold (fun x l -> x::l) s []
let set_union_small_big small big = BatSet.fold BatSet.add small big
(* fixpoint operator for set *)
let rec fix_set : ('a BatSet.t -> 'a BatSet.t) -> 'a BatSet.t -> 'a BatSet.t
= fun f init ->
let next = f init in
if BatSet.subset next init then init
else fix_set f next
let rec lookup (x:'a) (l:('a * 'b) list) : 'b option =
match l with
| [] -> None
| (y, v)::l -> if x = y then Some v else lookup x l
let rec update (m:('a * ('b list)) list) ((k, v):'a * 'b) : ('a * ('b list)) list =
match m with
| [] -> [(k, [v])]
| (k', vs)::m -> if k = k' then (k', v::vs)::m else (k', vs)::update m (k, v)
let rec remove_first (f:'a -> bool) (l:'a list) : 'a list =
match l with
| [] -> []
| x::l -> if f x then l else x::(remove_first f l)
(* Returns [0; ...; n-1] *)
let rec range (n:int) : int list =
let rec f acc n =
if n < 0 then acc else f (n :: acc) (n-1)
in
f [] (n-1)
(* Returns [1; ...; n] *)
let rec range1 (n:int) : int list =
let rec f acc n =
if n <= 0 then acc else f (n :: acc) (n-1)
in
f [] n
(* Returns [n; ...; m] *)
let rec rangen (n:int) (m:int) : int list =
let rec f acc n =
if n > m then acc else f (n :: acc) (n+1)
in
f [] n
let index_of (x:'a) (l:'a list) : int option =
let rec helper n x l =
match l with
| [] -> None
| y::l -> if x = y then Some n else helper (n+1) x l in
helper 0 x l
let rec nub (l:'a list) : 'a list =
match l with
| [] -> []
| x::l -> if List.mem x l then nub l else x :: nub l
let rec find (f:'a -> bool) (l:'a list) : 'a option =
match l with
| [] -> None
| x::l -> if f x then Some x else find f l
let rec try_first (x:'a) (l:('a -> 'b option) list) : 'b option =
match l with
| [] -> None
| f::l ->
begin match f x with
| Some y -> Some y
| None -> try_first x l
end
let rec try_first_lazy (l:'a option Lazy.t list) : 'a option =
match l with
| [] -> None
| x::l ->
begin match Lazy.force x with
| Some v -> Some v
| None -> try_first_lazy l
end
let const (v:'a) : 'a -> 'a = fun _ -> v
let cons (x:'a) (l:'a list) : 'a list = x :: l
let rec replicate (n:int) (x:'a) : 'a list =
if n <= 0 then [] else x::replicate (n-1) x
let rec find_first (f:'a -> bool) (l:'a list) : 'a option =
match l with
| [] -> None
| x :: l -> if f x then Some x else find_first f l
let rec partitions (n:int) (k:int) : int list list =
if n <= 0 || k <= 0 then
[]
else if k == 1 then
[[n]]
else
List.fold_left (fun res i ->
List.append res @@ List.map (cons i) (partitions (n-i) (k-1)))
[] (List.map ((+) 1) (range (n-k+1)))
type choice = MayNot | Must | May
let partitions_rel (k:int) : choice list list =
let rec mark_part acc n c =
if n >= k then acc else
let ch =
if c > 0 then May else if c = 0 then Must else MayNot
in
mark_part (ch :: acc) (n+1) (c-1)
in
List.map (mark_part [] 0) (range k |> List.rev)
let rec combinations (l:'a list list) : 'a list list =
match l with
| [] -> []
| [x] -> List.map (fun n -> [n]) x
| x :: l ->
List.fold_left
(fun res n -> List.append res (List.map (cons n) (combinations l)))
[] x
let rec disjoint (l1:'a list) (l2:'a list) : bool =
match l1 with
| [] -> true
| x::l1 -> if List.mem x l2 then false else disjoint l1 l2
let rec is_some : 'a option -> bool =
function | Some v -> true | None -> false
type ('a, 'b) either = Left of 'a | Right of 'b
let rec all_eq (p:'a -> 'a -> bool) (l:'a list) : bool =
match l with
| [] -> true
| [x] -> true
| x::y::l -> if not (p x y) then false else all_eq p (y::l)
let rec partition (n:int) (l:'a list) : 'a list * 'a * 'a list =
let rec search pre n post =
if n = 0 then
(pre, List.hd post, List.tl post)
else
search (List.hd post :: pre) (n-1) (List.tl post)
in
if n < List.length l then
search [] n l
else
raise @@ Invalid_argument "(partition) index out of range"
let rec separate ~f:(f:'a -> bool) (l:'a list) : 'a list * 'a list =
let rec sep acc l =
match l with
| [] -> acc
| x :: l ->
let (lacc, racc) = acc in
if f x then sep (lacc @ [x], racc) l else sep (lacc, racc @ [x]) l
in
sep ([], []) l
let rec time_action ~f:(f: unit -> 'a) : float * 'a =
let t1 = Unix.gettimeofday () in
let res = f () in
let t2 = Unix.gettimeofday () in
(t2 -. t1, res)
let rec list_remove1 : 'a -> 'a list -> 'a list
= fun e lst ->
match lst with
| [] -> raise (Failure "List.remove1 : not found")
| hd::tl -> if hd = e then tl else hd::(list_remove1 e tl)
let rec list_remove : 'a -> 'a list -> 'a list
= fun e lst ->
match lst with
| [] -> []
| hd::tl -> if hd = e then list_remove e tl else hd::(list_remove e tl)
let rec list_uniq : 'a list -> 'a list
= fun lst ->
match lst with
| [] -> []
| hd::tl -> hd::(list_remove hd (list_uniq tl))
let rec list_append_uniq : 'a list -> 'a list -> 'a list
= fun lst1 lst2 -> list_uniq (lst1@lst2)
(* Refactoring required... *)
let rec list_drop_first : 'a list -> 'a -> 'a list
= fun lst e ->
match lst with
| [] -> []
| hd::tl -> if hd = e then tl else hd::(list_drop_first tl e)
let rec list_sub : 'a list -> 'a list -> 'a list
= fun lst1 lst2 ->
match lst2 with
| [] -> lst1
| hd::tl -> list_sub (list_drop_first lst1 hd) tl
let rec list_combination : 'a list -> int -> ('a list) list
= fun lst k ->
if k = 0 then [[]] else
match lst with
| [] -> raise (Failure "List.comb : empty list ")
| hd::tl ->
if k = List.length lst then [lst]
else if k = 1 then List.map (fun e -> [e]) lst
else (List.map (fun prev -> hd::prev) (list_combination tl (k-1))) @ (list_combination tl k)
let rec insert_all_pos : 'a list -> 'a -> ('a list) list
= fun lst e ->
let rec insert_kth_pos : 'a list -> 'a -> int -> 'a list
= fun lst e k ->
match lst with
| [] -> if k = 0 then [e] else raise (Failure "insert_kth_pos : invalid k")
| hd::tl -> if k = 0 then e::lst else hd::(insert_kth_pos tl e (k-1))
in
let rec iter : 'a list -> 'a -> int -> ('a list) list
= fun lst e n ->
match n with
| 0 -> [insert_kth_pos lst e 0]
| n -> if n > 0 then (iter lst e (n-1)) @ [(insert_kth_pos lst e n)] else raise (Failure "insert_all_pos : Invalid")
in
iter lst e (List.length lst)
let rec list_permutation : 'a list -> ('a list) list
= fun lst ->
match lst with
| [] -> []
| [hd] -> [[hd]]
| hd::tl -> List.fold_left (fun acc perm -> insert_all_pos perm hd @ acc) [] (list_permutation tl)
let rec list_permutationk : 'a list -> int -> ('a list) list
= fun lst k ->
List.fold_left (fun acc comb ->
acc @ list_permutation comb
) [] (list_combination lst k)
let rec list_match : ('a -> 'a -> bool) -> 'a list -> 'a list -> ('a * 'a) list option
= fun pred lst1 lst2 ->
if List.length lst1 <> List.length lst2 then
None (* If the number of cfgs of two programs is different fail to match *)
else
let matching_candidates = list_permutation lst2 in
try
let matched_cand = List.find (fun cand -> List.for_all2 pred lst1 cand) matching_candidates in
Some (List.combine lst1 matched_cand)
with Not_found -> None
(* Set operation *)
let rec find_first_set : ('a -> bool) -> 'a BatSet.t -> 'a
= fun pred set ->
if BatSet.is_empty set then
raise (Failure "Not found : Set.find_first")
else
let (candidate, remains) = BatSet.pop set in
if pred candidate then candidate else find_first_set pred remains
let rec compare_set : 'a BatSet.t -> 'a BatSet.t -> ('a -> 'a -> bool) -> bool
= fun set1 set2 comp ->
if BatSet.is_empty (BatSet.union set1 set2) then true
else
let (elem1, set1) = BatSet.pop set1 in
let matched = BatSet.filter (fun elem2 -> comp elem1 elem2) set2 in
if BatSet.cardinal matched = 1 then compare_set set1 (BatSet.diff set2 matched) comp
else false
(* Map operation *)
let keys : ('a, 'b) BatMap.t -> 'a BatSet.t
= fun map -> BatMap.foldi (fun a b set -> BatSet.add a set) map BatSet.empty
let rec join_tuple : 'a BatSet.t -> 'b BatSet.t -> ('a * 'b) BatSet.t
= fun s1 s2 ->
BatSet.fold (fun e1 acc ->
BatSet.fold (fun e2 acc ->
BatSet.add (e1, e2) acc
) s2 acc
) s1 BatSet.empty
let rec join_triple : 'a BatSet.t -> 'b BatSet.t -> 'c BatSet.t -> ('a * 'b * 'c) BatSet.t
= fun s1 s2 s3 ->
BatSet.fold (fun e1 acc ->
BatSet.fold (fun e2 acc ->
BatSet.fold (fun e3 acc ->
BatSet.add (e1, e2, e3) acc
) s3 acc
) s2 acc
) s1 BatSet.empty
let rec join_list : ('a BatSet.t) list -> ('a list) BatSet.t
= fun sets ->
match sets with
| [] -> BatSet.singleton []
| hd::tl ->
BatSet.fold (fun lst acc ->
BatSet.fold (fun e acc ->
BatSet.add (e::lst) acc
) hd acc
) (join_list tl) BatSet.empty