Skip to content

Commit 2cda2b3

Browse files
committed
Extract GobList.cartesian_map
1 parent c630eae commit 2cda2b3

File tree

5 files changed

+23
-21
lines changed

5 files changed

+23
-21
lines changed

src/arg/myARG.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ struct
278278
let next_opt _ = None
279279
end
280280

281-
let cartesian_concat_paths (ps : cfg_path list) (qs : cfg_path list) : cfg_path list = List.concat_map (fun p -> List.map (fun q -> p @ q) qs) ps
281+
let cartesian_append: cfg_path list -> cfg_path list -> cfg_path list = GobList.cartesian_map (@)
282282

283283
let partition_if_next if_next =
284284
let (if_next_trues, if_next_falses) = List.partition (function
@@ -318,8 +318,8 @@ struct
318318
if Node.equal if_false_next_n if_true_next_false_next_n then
319319
let exp = BinOp (LAnd, e, e2, intType) in
320320
Some [
321-
(Test (exp, true), if_true_next_true_next_n, cartesian_concat_paths if_true_next_ps if_true_next_true_next_ps);
322-
(Test (exp, false), if_true_next_false_next_n, if_false_next_ps @ cartesian_concat_paths if_true_next_ps if_true_next_false_next_ps) (* concat two different path families to same false node *)
321+
(Test (exp, true), if_true_next_true_next_n, cartesian_append if_true_next_ps if_true_next_true_next_ps);
322+
(Test (exp, false), if_true_next_false_next_n, if_false_next_ps @ cartesian_append if_true_next_ps if_true_next_false_next_ps) (* concat two different path families to same false node *)
323323
]
324324
else
325325
None
@@ -330,8 +330,8 @@ struct
330330
if Node.equal if_true_next_n if_false_next_true_next_n then
331331
let exp = BinOp (LOr, e, e2, intType) in
332332
Some [
333-
(Test (exp, true), if_false_next_true_next_n, if_true_next_ps @ cartesian_concat_paths if_false_next_ps if_false_next_true_next_ps); (* concat two different path families to same true node *)
334-
(Test (exp, false), if_false_next_false_next_n, cartesian_concat_paths if_false_next_ps if_false_next_false_next_ps)
333+
(Test (exp, true), if_false_next_true_next_n, if_true_next_ps @ cartesian_append if_false_next_ps if_false_next_true_next_ps); (* concat two different path families to same true node *)
334+
(Test (exp, false), if_false_next_false_next_n, cartesian_append if_false_next_ps if_false_next_false_next_ps)
335335
]
336336
else
337337
None
@@ -366,8 +366,8 @@ struct
366366
| [(Assign (v_true, e_true), if_true_next_next_n, if_true_next_next_ps)], [(Assign (v_false, e_false), if_false_next_next_n, if_false_next_next_ps)] when v_true = v_false && Node.equal if_true_next_next_n if_false_next_next_n ->
367367
let exp = ternary e_cond e_true e_false in
368368
Some [
369-
(Assign (v_true, exp), if_true_next_next_n, cartesian_concat_paths if_true_next_ps if_true_next_next_ps);
370-
(Assign (v_false, exp), if_false_next_next_n, cartesian_concat_paths if_false_next_ps if_false_next_next_ps)
369+
(Assign (v_true, exp), if_true_next_next_n, cartesian_append if_true_next_ps if_true_next_next_ps);
370+
(Assign (v_false, exp), if_false_next_next_n, cartesian_append if_false_next_ps if_false_next_next_ps)
371371
]
372372
| _, _ -> None
373373
else

src/cdomain/value/cdomains/structDomain.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ struct
177177
in
178178
let x_list = HS.elements x in
179179
let y_list = HS.elements y in
180-
List.concat_map (fun xss -> List.map (fun yss -> (xss, yss)) y_list) x_list
180+
(* TODO: GobList.cartesian_filter_map? or just Seq? *)
181+
BatList.cartesian_product x_list y_list
181182
|> List.filter (fun (ssx, ssy) -> maps_overlap ssx ssy)
182183
|> List.map (fun (ssx, ssy) -> f ssx ssy)
183184
|> HS.of_list
@@ -195,7 +196,7 @@ struct
195196
let widen_with_fct f =
196197
let product_widen op a b = (* assumes b to be bigger than a *) (* from HS.product_widen *)
197198
let xs,ys = HS.elements a, HS.elements b in
198-
List.concat_map (fun x -> List.map (fun y -> op x y) ys) xs |> fun x -> HS.of_list (List.append x ys)
199+
GobList.cartesian_map op xs ys |> fun x -> HS.of_list (List.append x ys)
199200
in
200201
product_widen (fun x y -> if SS.leq x y then (SS.widen_with_fct f) x y else SS.bot ())
201202

@@ -367,7 +368,7 @@ struct
367368
let ((sx, kx), (sy, ky)) = (x, y) in
368369
let x_list = HS.elements sx in
369370
let y_list = HS.elements sy in
370-
let s = List.concat_map (fun xss -> List.map (fun yss -> (xss, yss)) y_list) x_list
371+
let s = BatList.cartesian_product x_list y_list (* TODO: GobList.cartesian_filter_map? or just Seq? *)
371372
|> List.filter (fun (ssx, ssy) -> maps_overlap ssx ssy)
372373
|> List.map (fun (ssx, ssy) -> f ssx ssy)
373374
|> HS.of_list
@@ -395,7 +396,7 @@ struct
395396
let widen_with_fct f (x, kx) (y, ky) =
396397
let product_widen op a b = (* assumes b to be bigger than a *) (* from HS.product_widen *)
397398
let xs,ys = HS.elements a, HS.elements b in
398-
List.concat_map (fun x -> List.map (op x) ys) xs |> fun x -> HS.of_list (List.append x ys)
399+
GobList.cartesian_map op xs ys |> fun x -> HS.of_list (List.append x ys)
399400
in
400401
let s = product_widen (fun x y -> if SS.leq x y then (SS.widen_with_fct f) x y else SS.bot ()) x y
401402
in reduce_key (s, take_some_key kx ky s)

src/cdomains/congruenceClosure.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,12 @@ module Disequalities = struct
209209
let arg = List.fold_left do_bindings TMap.empty clist in
210210
(uf, cmap, arg)
211211

212+
(* TODO: GobList.cartesian_fold_left? *)
212213
let fold_left2 f acc l1 l2 =
213214
List.fold_left (
214215
fun acc x -> List.fold_left (
215216
fun acc y -> f acc x y) acc l2) acc l1
216217

217-
let map2 f l1 l2 =
218-
let map_f x = List.map (f x) l2 in
219-
List.concat_map map_f l1
220-
221218
let map_find_opt (v,r) map =
222219
let inner_map = TMap.find_opt v map in
223220
BatOption.map_default (ZMap.find_opt r) None inner_map

src/domain/hoareDomain.ml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ struct
199199
let reduce s = filter (fun x -> not (exists (le x) s)) s
200200
let product_bot op a b =
201201
let a,b = elements a, elements b in
202-
List.concat_map (fun x -> List.map (fun y -> op x y) b) a |> fun x -> reduce (of_list x)
202+
GobList.cartesian_map op a b |> fun x -> reduce (of_list x)
203203
let product_widen op a b = (* assumes b to be bigger than a *)
204204
let xs,ys = elements a, elements b in
205-
List.concat_map (fun x -> List.map (fun y -> op x y) ys) xs |> fun x -> reduce (union b (of_list x))
205+
GobList.cartesian_map op xs ys |> fun x -> reduce (union b (of_list x))
206206
let widen = product_widen (fun x y -> if B.leq x y then B.widen x y else B.bot ())
207207
let narrow = product_bot (fun x y -> if B.leq y x then B.narrow x y else x)
208208

@@ -302,18 +302,18 @@ struct
302302
maximals
303303
let product_bot op op2 a b =
304304
let a,b = elements a, elements b in
305-
List.concat_map (fun (x,xr) -> List.map (fun (y,yr) -> (op x y, op2 xr yr)) b) a |> fun x -> reduce (of_list x)
305+
GobList.cartesian_map (fun (x,xr) (y,yr) -> (op x y, op2 xr yr)) a b |> fun x -> reduce (of_list x)
306306
let product_bot2 op2 a b =
307307
let a,b = elements a, elements b in
308-
List.concat_map (fun (x,xr) -> List.map (fun (y,yr) -> op2 (x, xr) (y, yr)) b) a |> fun x -> reduce (of_list x)
308+
GobList.cartesian_map op2 a b |> fun x -> reduce (of_list x)
309309
(* why are type annotations needed for product_widen? *)
310310
(* TODO: unused now *)
311311
let product_widen op op2 (a:t) (b:t): t = (* assumes b to be bigger than a *)
312312
let xs,ys = elements a, elements b in
313-
List.concat_map (fun (x,xr) -> List.map (fun (y,yr) -> (op x y, op2 xr yr)) ys) xs |> fun x -> reduce (join b (of_list x)) (* join instead of union because R is HoareDomain.Set for witness generation *)
313+
GobList.cartesian_map (fun (x,xr) (y,yr) -> (op x y, op2 xr yr)) xs ys |> fun x -> reduce (join b (of_list x)) (* join instead of union because R is HoareDomain.Set for witness generation *)
314314
let product_widen2 op2 (a:t) (b:t): t = (* assumes b to be bigger than a *)
315315
let xs,ys = elements a, elements b in
316-
List.concat_map (fun (x,xr) -> List.map (fun (y,yr) -> op2 (x, xr) (y, yr)) ys) xs |> fun x -> reduce (join b (of_list x)) (* join instead of union because R is HoareDomain.Set for witness generation *)
316+
GobList.cartesian_map op2 xs ys |> fun x -> reduce (join b (of_list x)) (* join instead of union because R is HoareDomain.Set for witness generation *)
317317
let join a b = join a b |> reduce
318318
let meet = product_bot SpecD.meet R.inter
319319
(* let narrow = product_bot (fun x y -> if SpecD.leq y x then SpecD.narrow x y else x) R.narrow *)
@@ -368,6 +368,7 @@ struct
368368
(* TODO: move to Set above? *)
369369
let product_widen (op: elt -> elt -> elt option) a b = (* assumes b to be bigger than a *)
370370
let xs,ys = elements a, elements b in
371+
(* TODO: GobList.cartesian_filter_map? *)
371372
List.concat_map (fun x -> List.filter_map (fun y -> op x y) ys) xs |> fun x -> join b (of_list x)
372373
let widen = product_widen (fun x y -> if E.leq x y then Some (E.widen x y) else None)
373374

src/util/std/gobList.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ let until_last_with (pred: 'a -> bool) (xs: 'a list) =
5555
in
5656
until_last_helper [] [] xs
5757

58+
let cartesian_map f l1 l2 =
59+
List.concat_map (fun x -> List.map (f x) l2) l1
60+
5861

5962
(** Open this to use applicative functor/monad syntax for {!list}. *)
6063
module Syntax =

0 commit comments

Comments
 (0)