[sledge] Minor simplifications using Set and Map iterators

Reviewed By: jvillard

Differential Revision: D24313893

fbshipit-source-id: 8275b3127
master
Josh Berdine 4 years ago committed by Facebook GitHub Bot
parent 920c553902
commit 2083e3ee86

@ -95,8 +95,8 @@ let used_globals pgm preanalyze : Domain_used_globals.r =
(Llair.Reg.Map.map summary_table ~f:Llair.Reg.Set.union_list) (Llair.Reg.Map.map summary_table ~f:Llair.Reg.Set.union_list)
else else
Declared Declared
(IArray.fold pgm.globals Llair.Reg.Set.empty ~f:(fun g -> (Llair.Reg.Set.of_iter
Llair.Reg.Set.add g.reg )) (Iter.map ~f:(fun g -> g.reg) (IArray.to_iter pgm.globals)))
let analyze = let analyze =
let%map_open bound = let%map_open bound =

@ -12,6 +12,7 @@ module Import = struct
end end
let mem elt seq ~eq = mem ~eq ~x:elt seq let mem elt seq ~eq = mem ~eq ~x:elt seq
let map seq ~f = map ~f seq
let sort seq ~cmp = sort ~cmp seq let sort seq ~cmp = sort ~cmp seq
let sort_uniq seq ~cmp = sort_uniq ~cmp seq let sort_uniq seq ~cmp = sort_uniq ~cmp seq
let sorted seq ~cmp = sorted ~cmp seq let sorted seq ~cmp = sorted ~cmp seq

@ -12,6 +12,7 @@ module Import : sig
end end
val mem : 'a -> 'a t -> eq:('a -> 'a -> bool) -> bool val mem : 'a -> 'a t -> eq:('a -> 'a -> bool) -> bool
val map : 'a t -> f:('a -> 'b) -> 'b t
val sort : 'a t -> cmp:('a -> 'a -> int) -> 'a t val sort : 'a t -> cmp:('a -> 'a -> int) -> 'a t
val sort_uniq : 'a t -> cmp:('a -> 'a -> int) -> 'a t val sort_uniq : 'a t -> cmp:('a -> 'a -> int) -> 'a t
val sorted : 'a t -> cmp:('a -> 'a -> int) -> bool val sorted : 'a t -> cmp:('a -> 'a -> int) -> bool

@ -162,6 +162,9 @@ end) : S with type key = Key.t = struct
let keys = M.keys let keys = M.keys
let values = M.values let values = M.values
let to_iter = M.to_iter let to_iter = M.to_iter
let to_list = M.to_list
let of_iter = M.of_iter
let of_list = M.of_list
let to_iter2 l r = let to_iter2 l r =
let seq = ref Iter.empty in let seq = ref Iter.empty in

@ -112,6 +112,9 @@ module type S = sig
val keys : 'a t -> key iter val keys : 'a t -> key iter
val values : 'a t -> 'a iter val values : 'a t -> 'a iter
val to_iter : 'a t -> (key * 'a) iter val to_iter : 'a t -> (key * 'a) iter
val to_list : 'a t -> (key * 'a) list
val of_iter : (key * 'a) iter -> 'a t
val of_list : (key * 'a) list -> 'a t
val to_iter2 : val to_iter2 :
'a t 'a t

@ -33,15 +33,13 @@ struct
let sexp_of_t s = let sexp_of_t s =
List.sexp_of_t List.sexp_of_t
(Sexplib.Conv.sexp_of_pair Elt.sexp_of_t Mul.sexp_of_t) (Sexplib.Conv.sexp_of_pair Elt.sexp_of_t Mul.sexp_of_t)
(Iter.to_list (M.to_iter s)) (M.to_list s)
let t_of_sexp elt_of_sexp sexp = let t_of_sexp elt_of_sexp sexp =
List.fold_left M.of_list
~f:(fun m (key, data) -> M.add_exn ~key ~data m)
(List.t_of_sexp (List.t_of_sexp
(Sexplib.Conv.pair_of_sexp elt_of_sexp Mul.t_of_sexp) (Sexplib.Conv.pair_of_sexp elt_of_sexp Mul.t_of_sexp)
sexp) sexp)
M.empty
let pp sep pp_elt fs s = let pp sep pp_elt fs s =
List.pp sep pp_elt fs (Iter.to_list (M.to_iter s)) List.pp sep pp_elt fs (Iter.to_list (M.to_iter s))

@ -80,6 +80,7 @@ end) : S with type elt = Elt.t = struct
let for_all s ~f = S.for_all f s let for_all s ~f = S.for_all f s
let fold s z ~f = S.fold f s z let fold s z ~f = S.fold f s z
let to_iter = S.to_iter let to_iter = S.to_iter
let of_iter = S.of_iter
let pp ?pre ?suf ?(sep = (",@ " : (unit, unit) fmt)) pp_elt fs x = let pp ?pre ?suf ?(sep = (",@ " : (unit, unit) fmt)) pp_elt fs x =
List.pp ?pre ?suf sep pp_elt fs (S.elements x) List.pp ?pre ?suf sep pp_elt fs (S.elements x)

@ -60,6 +60,7 @@ module type S = sig
(** {1 Convert} *) (** {1 Convert} *)
val to_iter : t -> elt iter val to_iter : t -> elt iter
val of_iter : elt iter -> t
(** {1 Pretty-print} *) (** {1 Pretty-print} *)

@ -853,9 +853,7 @@ let v_to_ses : var -> Ses.Var.t =
fun v -> Ses.Var.identified ~id:(Var.id v) ~name:(Var.name v) fun v -> Ses.Var.identified ~id:(Var.id v) ~name:(Var.name v)
let vs_to_ses : Var.Set.t -> Ses.Var.Set.t = let vs_to_ses : Var.Set.t -> Ses.Var.Set.t =
fun vs -> fun vs -> Ses.Var.Set.of_iter (Iter.map ~f:v_to_ses (Var.Set.to_iter vs))
Var.Set.fold vs Ses.Var.Set.empty ~f:(fun v ->
Ses.Var.Set.add (v_to_ses v) )
let rec arith_to_ses poly = let rec arith_to_ses poly =
Arith.fold_monomials poly Ses.Term.zero ~f:(fun mono coeff e -> Arith.fold_monomials poly Ses.Term.zero ~f:(fun mono coeff e ->
@ -936,8 +934,7 @@ let v_of_ses : Ses.Var.t -> var =
fun v -> Var.identified ~id:(Ses.Var.id v) ~name:(Ses.Var.name v) fun v -> Var.identified ~id:(Ses.Var.id v) ~name:(Ses.Var.name v)
let vs_of_ses : Ses.Var.Set.t -> Var.Set.t = let vs_of_ses : Ses.Var.Set.t -> Var.Set.t =
fun vs -> fun vs -> Var.Set.of_iter (Iter.map ~f:v_of_ses (Ses.Var.Set.to_iter vs))
Ses.Var.Set.fold ~f:(fun v -> Var.Set.add (v_of_ses v)) vs Var.Set.empty
let uap1 f = ap1t (fun x -> _Apply f [|x|]) let uap1 f = ap1t (fun x -> _Apply f [|x|])
let uap2 f = ap2t (fun x y -> _Apply f [|x; y|]) let uap2 f = ap2t (fun x y -> _Apply f [|x; y|])

@ -16,9 +16,7 @@ let reg r =
let global = Llair.Reg.is_global r in let global = Llair.Reg.is_global r in
Var.program ~name ~global Var.program ~name ~global
let regs rs = let regs rs = Var.Set.of_iter (Iter.map ~f:reg (Llair.Reg.Set.to_iter rs))
Llair.Reg.Set.fold ~f:(fun r -> Var.Set.add (reg r)) rs Var.Set.empty
let uap0 f = T.apply f [||] let uap0 f = T.apply f [||]
let uap1 f a = T.apply f [|a|] let uap1 f a = T.apply f [|a|]
let uap2 f a b = T.apply f [|a; b|] let uap2 f a b = T.apply f [|a; b|]

@ -94,12 +94,8 @@ module Make (T : REPR) = struct
|> check (fun ({sub; _}, _) -> invariant sub) |> check (fun ({sub; _}, _) -> invariant sub)
let fold sub z ~f = Map.fold ~f:(fun ~key ~data -> f key data) sub z let fold sub z ~f = Map.fold ~f:(fun ~key ~data -> f key data) sub z
let domain sub = Set.of_iter (Map.keys sub)
let domain sub = let range sub = Set.of_iter (Map.values sub)
Map.fold ~f:(fun ~key ~data:_ -> Set.add key) sub Set.empty
let range sub =
Map.fold ~f:(fun ~key:_ ~data -> Set.add data) sub Set.empty
let invert sub = let invert sub =
Map.fold sub empty ~f:(fun ~key ~data sub' -> Map.fold sub empty ~f:(fun ~key ~data sub' ->

Loading…
Cancel
Save