diff --git a/CHANGELOG.md b/CHANGELOG.md index f5625ad66a..7a9235e18d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,7 @@ - Make .cmt and .cmti files smaller by no longer embedding a copy of the .cmi. https://github.com/rescript-lang/rescript/pull/8774 - Use the OCaml standard library's hash tables in the compiler. https://github.com/rescript-lang/rescript/pull/8786 - Use the OCaml standard library's hash tables for the compiler's hash sets, and always order the imports of a module used both with and without `default` the same way. https://github.com/rescript-lang/rescript/pull/8787 +- Use the OCaml standard library's sets in the compiler. https://github.com/rescript-lang/rescript/pull/8789 - Represent explicit expression braces as `Pexp_braces` in parsetree v1 and format `else` branches consistently with `if` branches. https://github.com/rescript-lang/rescript/pull/8678 - Omit redundant braces around multi-statement switch case bodies when formatting. https://github.com/rescript-lang/rescript/pull/8677 - Avoid running `rescript-schema-ppx` and `sury-ppx` on source files without an `@schema` annotation. https://github.com/rescript-lang/rescript/pull/8662 diff --git a/compiler/core/js_analyzer.ml b/compiler/core/js_analyzer.ml index 23236ec7f4..96bd8645ee 100644 --- a/compiler/core/js_analyzer.ml +++ b/compiler/core/js_analyzer.ml @@ -28,7 +28,7 @@ type idents_stats = { } let add_defined_idents (x : idents_stats) ident = - x.defined_idents <- Set_ident.add x.defined_idents ident + x.defined_idents <- Set_ident.add ident x.defined_idents let add_record_rest_field_idents stats fields = List.iter @@ -61,8 +61,8 @@ let free_variables (stats : idents_stats) = | Some v -> self.expression self v); ident = (fun _ id -> - if not (Set_ident.mem stats.defined_idents id) then - stats.used_idents <- Set_ident.add stats.used_idents id); + if not (Set_ident.mem id stats.defined_idents) then + stats.used_idents <- Set_ident.add id stats.used_idents); expression = (fun self exp -> match exp.expression_desc with diff --git a/compiler/core/js_dump.ml b/compiler/core/js_dump.ml index ede0a7670c..2abd820819 100644 --- a/compiler/core/js_dump.ml +++ b/compiler/core/js_dump.ml @@ -411,7 +411,7 @@ let rec pp_function ~return_unit ~async ~is_method ?directive cxt (f : P.t) match fn_state with | Is_return | No_name _ -> Js_fun_env.get_unbounded env | Name_top id | Name_non_top id -> - Set_ident.add (Js_fun_env.get_unbounded env) id + Set_ident.add id (Js_fun_env.get_unbounded env) in (* the context will be continued after this function *) let outer_cxt = Ext_pp_scope.merge cxt set_env in diff --git a/compiler/core/js_pass_flatten_and_mark_dead.ml b/compiler/core/js_pass_flatten_and_mark_dead.ml index 6ec8fc67e7..fcd7a0a698 100644 --- a/compiler/core/js_pass_flatten_and_mark_dead.ml +++ b/compiler/core/js_pass_flatten_and_mark_dead.ml @@ -69,7 +69,7 @@ let mark_dead_code (js : J.program) : J.program = Js_analyzer.no_side_effect_expression x in let () = - if Set_ident.mem js.export_set ident then + if Set_ident.mem ident js.export_set then Js_op_util.update_used_stats ident_info Exported in let () = diff --git a/compiler/core/js_pass_get_used.ml b/compiler/core/js_pass_get_used.ml index 628c275f15..2f058b9bb9 100644 --- a/compiler/core/js_pass_get_used.ml +++ b/compiler/core/js_pass_get_used.ml @@ -31,7 +31,7 @@ let post_process_stats my_export_set (defined_idents : J.variable_declaration Hash_ident.t) stats = Hash_ident.iter (fun ident (v : J.variable_declaration) -> - if Set_ident.mem my_export_set ident then + if Set_ident.mem ident my_export_set then Js_op_util.update_used_stats v.ident_info Exported else let pure = diff --git a/compiler/core/js_pass_scope.ml b/compiler/core/js_pass_scope.ml index 23a40465c7..24810599e1 100644 --- a/compiler/core/js_pass_scope.ml +++ b/compiler/core/js_pass_scope.ml @@ -116,18 +116,18 @@ let with_in_loop (st : state) b = let add_loop_mutable_variable (st : state) id = { st with - loop_mutable_values = Set_ident.add st.loop_mutable_values id; - mutable_values = Set_ident.add st.mutable_values id; + loop_mutable_values = Set_ident.add id st.loop_mutable_values; + mutable_values = Set_ident.add id st.mutable_values; } let add_mutable_variable (st : state) id = - {st with mutable_values = Set_ident.add st.mutable_values id} + {st with mutable_values = Set_ident.add id st.mutable_values} let add_defined_ident (st : state) id = - {st with defined_idents = Set_ident.add st.defined_idents id} + {st with defined_idents = Set_ident.add id st.defined_idents} let add_used_ident (st : state) id = - {st with used_idents = Set_ident.add st.used_idents id} + {st with used_idents = Set_ident.add id st.used_idents} let add_defined_idents st ids = List.fold_left add_defined_ident st ids @@ -160,7 +160,7 @@ let record_scope_pass = (* mark which param is used *) params |> List.iteri (fun i v -> - if not (Set_ident.mem used_idents' v) then + if not (Set_ident.mem v used_idents') then Js_fun_env.mark_unused env i); let closured_idents' = (* pass param_set down *) @@ -305,19 +305,19 @@ let record_scope_pass = *) { state with - used_idents = Set_ident.add state.used_idents x; - defined_idents = Set_ident.add state.defined_idents x; + used_idents = Set_ident.add x state.used_idents; + defined_idents = Set_ident.add x state.defined_idents; }); for_ident = (fun _ state x -> { state with - loop_mutable_values = Set_ident.add state.loop_mutable_values x; + loop_mutable_values = Set_ident.add x state.loop_mutable_values; }); ident = (fun _ state x -> - if Set_ident.mem state.defined_idents x then state - else {state with used_idents = Set_ident.add state.used_idents x}); + if Set_ident.mem x state.defined_idents then state + else {state with used_idents = Set_ident.add x state.used_idents}); } let program js = diff --git a/compiler/core/js_pass_tailcall_inline.ml b/compiler/core/js_pass_tailcall_inline.ml index 5a92b05cac..f858e4832e 100644 --- a/compiler/core/js_pass_tailcall_inline.ml +++ b/compiler/core/js_pass_tailcall_inline.ml @@ -137,7 +137,7 @@ let subst (export_set : Set_ident.t) stats = comment = _; } as st) :: rest -> ( - let is_export = Set_ident.mem export_set vd.ident in + let is_export = Set_ident.mem vd.ident export_set in if is_export then self.statement self st :: self.block self rest else match Hash_ident.find_opt stats vd.ident with diff --git a/compiler/core/js_shake.ml b/compiler/core/js_shake.ml index 48d6901bf1..e633ed9a87 100644 --- a/compiler/core/js_shake.ml +++ b/compiler/core/js_shake.ml @@ -32,8 +32,8 @@ let live_idents (export_set : Set_ident.t) (block : J.block) : Set_ident.t = let live = ref Set_ident.empty in let worklist = ref [] in let mark id = - if not (Set_ident.mem !live id) then ( - live := Set_ident.add !live id; + if not (Set_ident.mem id !live) then ( + live := Set_ident.add id !live; worklist := id :: !worklist) in Ext_list.iter block (fun (st : J.statement) -> @@ -50,15 +50,15 @@ let live_idents (export_set : Set_ident.t) (block : J.block) : Set_ident.t = | Variable {value = None; _} -> () | _ -> if not (Js_analyzer.no_side_effect_statement st) then - Set_ident.iter (Js_analyzer.free_variables_of_statement st) mark); - Set_ident.iter export_set mark; + Set_ident.iter mark (Js_analyzer.free_variables_of_statement st)); + Set_ident.iter mark export_set; let rec drain () = match !worklist with | [] -> () | id :: rest -> worklist := rest; (match Hash_ident.find_opt deps id with - | Some fv -> Set_ident.iter fv mark + | Some fv -> Set_ident.iter mark fv | None -> ()); drain () in @@ -72,7 +72,7 @@ let shake_program (program : J.program) = Ext_list.fold_right block [] (fun (st : J.statement) acc -> match st.statement_desc with | Variable {ident; value; _} -> ( - if Set_ident.mem really_set ident then st :: acc + if Set_ident.mem ident really_set then st :: acc else match value with | None -> acc diff --git a/compiler/core/lam_check.ml b/compiler/core/lam_check.ml index c78daddbb1..490fc6854d 100644 --- a/compiler/core/lam_check.ml +++ b/compiler/core/lam_check.ml @@ -84,11 +84,11 @@ let check ~file ~pass lam = check_list_snd cases cxt; Option.iter (fun x -> check_staticfails x cxt) default | Lstaticraise (i, args) -> - if Set_int.mem cxt i then check_list args cxt + if Set_int.mem i cxt then check_list args cxt else failwith (Printf.sprintf "exit %d unbound after %s in %s" i pass file) | Lstaticcatch (e1, (j, _vars), e2) -> - check_staticfails e1 (Set_int.add cxt j); + check_staticfails e1 (Set_int.add j cxt); check_staticfails e2 cxt | Ltrywith (e1, _exn, e2) -> check_staticfails e1 cxt; diff --git a/compiler/core/lam_closure.ml b/compiler/core/lam_closure.ml index de79452799..7b6fb19e84 100644 --- a/compiler/core/lam_closure.ml +++ b/compiler/core/lam_closure.ml @@ -55,14 +55,14 @@ let free_variables (export_idents : Set_ident.t) (params : stats Map_ident.t) (lam : Lambda.t) : stats Map_ident.t = let fv = ref params in let local_set = ref export_idents in - let local_add k = local_set := Set_ident.add !local_set k in + let local_add k = local_set := Set_ident.add k !local_set in let local_add_list ks = - local_set := Ext_list.fold_left ks !local_set Set_ident.add - in - (* base don the envrionmet, recoring the use cases of arguments + local_set := List.fold_left (fun acc k -> Set_ident.add k acc) !local_set ks + (* base don the envrionmet, recoring the use cases of arguments relies on [identifier] uniquely bound *) + in let used (cur_pos : position) (v : Ident.t) = - if not (Set_ident.mem !local_set v) then fv := adjust !fv cur_pos v + if not (Set_ident.mem v !local_set) then fv := adjust !fv cur_pos v in let rec iter (top : position) (lam : Lambda.t) = @@ -87,7 +87,7 @@ let free_variables (export_idents : Set_ident.t) (params : stats Map_ident.t) | Lletrec (decl, body) -> local_set := Ext_list.fold_left decl !local_set (fun acc (id, _) -> - Set_ident.add acc id); + Set_ident.add id acc); Ext_list.iter decl (fun (_, exp) -> iter sink_pos exp); iter sink_pos body | Lswitch diff --git a/compiler/core/lam_coercion.ml b/compiler/core/lam_coercion.ml index f56a23f83d..348bab065b 100644 --- a/compiler/core/lam_coercion.ml +++ b/compiler/core/lam_coercion.ml @@ -106,9 +106,8 @@ let handle_exports (meta : Lam_stats.t) (lambda_exports : Lambda.t list) export_set = (if id.stamp = original_export_id.stamp then acc.export_set else - Set_ident.add - (Set_ident.remove acc.export_set original_export_id) - id); + Set_ident.add id + (Set_ident.remove original_export_id acc.export_set)); } else let newid = Ident.rename original_export_id in @@ -162,7 +161,7 @@ let handle_exports (meta : Lam_stats.t) (lambda_exports : Lambda.t list) Ext_list.fold_left reverse_input (result.export_map, result.groups) (fun (export_map, acc) x -> ( (match x with - | Single (_, id, lam) when Set_ident.mem export_set id -> + | Single (_, id, lam) when Set_ident.mem id export_set -> Map_ident.add export_map id lam (* relies on the Invariant that [eoid] can not be bound before FIX: such invariant may not hold diff --git a/compiler/core/lam_compile.ml b/compiler/core/lam_compile.ml index 9a5c9312cc..f770309cdb 100644 --- a/compiler/core/lam_compile.ml +++ b/compiler/core/lam_compile.ml @@ -1272,7 +1272,7 @@ let compile output_prefix = (lambda_cxt : Lam_compile_context.t) = let new_cxt = {lambda_cxt with continuation = NeedValue Not_tail} in let emitted_id = - if Set_ident.mem (Lambda_traverse.free_variables body) id then id + if Set_ident.mem id (Lambda_traverse.free_variables body) then id else Ext_ident.create_tmp ~name:"_for_of" () in let block = @@ -1295,7 +1295,7 @@ let compile output_prefix = (body : Lambda.t) (lambda_cxt : Lam_compile_context.t) = let new_cxt = {lambda_cxt with continuation = NeedValue Not_tail} in let emitted_id = - if Set_ident.mem (Lambda_traverse.free_variables body) id then id + if Set_ident.mem id (Lambda_traverse.free_variables body) then id else Ext_ident.create_tmp ~name:"_for_await_of" () in let block = diff --git a/compiler/core/lam_compile_main.ml b/compiler/core/lam_compile_main.ml index 663e4b639d..93f0e1f0da 100644 --- a/compiler/core/lam_compile_main.ml +++ b/compiler/core/lam_compile_main.ml @@ -73,12 +73,12 @@ let declare_undeclared_exports (exports : Ident.t list) (block : J.block) : let declared = Ext_list.fold_left block Set_ident.empty (fun acc (stmt : J.statement) -> match stmt.statement_desc with - | Variable {ident} -> Set_ident.add acc ident + | Variable {ident} -> Set_ident.add ident acc | _ -> acc) in block @ Ext_list.filter_map exports (fun id -> - if Set_ident.mem declared id then None + if Set_ident.mem id declared then None else Some (Js_stmt_make.declare_variable ~kind:Strict id)) (** Also need analyze its depenency is pure or not *) @@ -129,13 +129,13 @@ let js_hoisted_aliases (export_ids : Ident.t list) in let rec resolve_binding seen = function | Lambda.Lvar id as lam -> ( - if Set_ident.mem seen id then (lam, Some id) + if Set_ident.mem id seen then (lam, Some id) else match Map_ident.find_opt group_map id with | Some ((Lambda.Lvar _ | Lambda.Lprim {primitive = Lambda.Pfield _; _}) as alias) -> - resolve_binding (Set_ident.add seen id) alias + resolve_binding (Set_ident.add id seen) alias | Some resolved -> (resolved, Some id) | None -> (lam, Some id)) | Lambda.Lprim {primitive = Lambda.Pfield (pos, _); args = [base]} as lam @@ -178,10 +178,10 @@ let js_hoisted_aliases (export_ids : Ident.t list) Ext_list.fold_left groups Set_string.empty (fun occupied group -> match group with | Single (_, id, _) -> - Set_string.add occupied (Ext_ident.convert id.Ident.name) + Set_string.add (Ext_ident.convert id.Ident.name) occupied | Recursive bindings -> Ext_list.fold_left bindings occupied (fun occupied (id, _) -> - Set_string.add occupied (Ext_ident.convert id.Ident.name)) + Set_string.add (Ext_ident.convert id.Ident.name) occupied) | Nop _ -> occupied) in fst @@ -208,7 +208,7 @@ let js_hoisted_aliases (export_ids : Ident.t list) |> String.concat "$" in let js_name = Ext_ident.convert name in - if Set_string.mem occupied_names js_name then + if Set_string.mem js_name occupied_names then let error_loc = match target with | Lambda.Lfunction {loc} -> loc @@ -227,7 +227,7 @@ let js_hoisted_aliases (export_ids : Ident.t list) path, name ) :: aliases, - Set_string.add occupied_names js_name ) + Set_string.add js_name occupied_names ) | Some _ | None -> missing_path ()) | None -> missing_path ()) | None -> missing_path ()) @@ -362,7 +362,7 @@ let compile (output_prefix : string) export_idents hoisted (lam : Lambda.t) = exports = meta.exports @ List.rev hoisted_exports; export_idents = Ext_list.fold_left hoisted_exports meta.export_idents (fun acc id -> - Set_ident.add acc id); + Set_ident.add id acc); } in let export_map = diff --git a/compiler/core/lam_dce.ml b/compiler/core/lam_dce.ml index d5f36bb9dc..2f930c192d 100644 --- a/compiler/core/lam_dce.ml +++ b/compiler/core/lam_dce.ml @@ -32,7 +32,7 @@ let transitive_closure (initial_idents : Ident.t list) | None -> Ext_fmt.failwithf ~loc:__LOC__ "%s/%d not found" (Ident.name id) id.stamp - | Some e -> Set_ident.iter e dfs) + | Some e -> Set_ident.iter dfs e) in Ext_list.iter initial_idents dfs; visited @@ -61,8 +61,10 @@ let remove export_idents (rest : Lam_group.t list) : Lam_group.t list = if Lam_analysis.no_side_effects lam then acc else (* its free varaibles here will be defined above *) - Set_ident.fold (Lambda_traverse.free_variables lam) acc - (fun x acc -> x :: acc)) + Set_ident.fold + (fun x acc -> x :: acc) + (Lambda_traverse.free_variables lam) + acc) in let visited = transitive_closure initial_idents ident_free_vars in Ext_list.fold_left rest [] (fun acc x -> diff --git a/compiler/core/lam_hit.ml b/compiler/core/lam_hit.ml index dcd0dc9700..fdc610d1ac 100644 --- a/compiler/core/lam_hit.ml +++ b/compiler/core/lam_hit.ml @@ -29,7 +29,7 @@ let hit_variables (fv : Set_ident.t) (l : t) : bool = match x with | None -> false | Some a -> hit a - and hit_var (id : Ident.t) = Set_ident.mem fv id + and hit_var (id : Ident.t) = Set_ident.mem id fv and hit_list_snd : 'a. ('a * t) list -> bool = fun x -> Ext_list.exists_snd x hit and hit_list xs = Ext_list.exists xs hit diff --git a/compiler/core/lam_pass_collapse_var_aliases.ml b/compiler/core/lam_pass_collapse_var_aliases.ml index 4dbb766e82..7992564e44 100644 --- a/compiler/core/lam_pass_collapse_var_aliases.ml +++ b/compiler/core/lam_pass_collapse_var_aliases.ml @@ -22,7 +22,7 @@ let collapse ~exports (lam : Lambda.t) : Lambda.t = Hash_ident.add tbl id u; (* The binding is dropped unless the name is exported, in which case it has to survive under its own name. *) - if Set_ident.mem exports id then + if Set_ident.mem id exports then Lambda.let_ Alias id (Lambda.var u) (go body) else go body | _ -> Lambda_traverse.shallow_map_sharing go lam diff --git a/compiler/core/lam_pass_collect.ml b/compiler/core/lam_pass_collect.ml index 83605b0080..f4cf298593 100644 --- a/compiler/core/lam_pass_collect.ml +++ b/compiler/core/lam_pass_collect.ml @@ -78,7 +78,7 @@ let collect_info (meta : Lam_stats.t) (lam : Lambda.t) = collect body | x -> collect x; - if Set_ident.mem meta.export_idents ident then + if Set_ident.mem ident meta.export_idents then annotate meta rec_flag ident (Lam_arity_analysis.get_arity meta x) lam and collect (lam : Lambda.t) = match lam with diff --git a/compiler/core/lam_pass_deep_flatten.ml b/compiler/core/lam_pass_deep_flatten.ml index a061800717..9addd28873 100644 --- a/compiler/core/lam_pass_deep_flatten.ml +++ b/compiler/core/lam_pass_deep_flatten.ml @@ -236,7 +236,7 @@ let deep_flatten (lam : Lambda.t) : Lambda.t = let groups = Ext_list.map_snd_sharing bind_args aux in let collections = Ext_list.fold_left groups Set_ident.empty (fun set (id, _) -> - Set_ident.add set id) + Set_ident.add id set) in (* Try to extract some value definitions from recursive values as [wrap], it will stop whenever it find it could not move forward diff --git a/compiler/core/lam_pass_remove_alias.ml b/compiler/core/lam_pass_remove_alias.ml index 8d0b07e60f..83cbc15f06 100644 --- a/compiler/core/lam_pass_remove_alias.ml +++ b/compiler/core/lam_pass_remove_alias.ml @@ -176,7 +176,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = }) when Lam_analysis.lfunction_can_be_inlined m -> if Ext_list.same_length ap_args params then - if is_a_functor (* && (Set_ident.mem v meta.export_idents) && false *) + if is_a_functor (* && (Set_ident.mem meta.export_idents v) && false *) then (* TODO: check l1 if it is exported, if so, maybe not since in that case, @@ -190,7 +190,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = let param_map = Lam_closure.is_closed_with_map meta.export_idents params body in - let is_export_id = Set_ident.mem meta.export_idents v in + let is_export_id = Set_ident.mem v meta.export_idents in match (is_export_id, param_map) with | false, (_, param_map) | true, (true, param_map) -> ( match rec_flag with diff --git a/compiler/ext/ext_pp_scope.ml b/compiler/ext/ext_pp_scope.ml index 4781d30422..2968ec39c2 100644 --- a/compiler/ext/ext_pp_scope.ml +++ b/compiler/ext/ext_pp_scope.ml @@ -93,18 +93,22 @@ let ident (cxt : t) f (id : Ident.t) : t = cxt let merge (cxt : t) (set : Set_ident.t) = - Set_ident.fold set cxt (fun ident acc -> + Set_ident.fold + (fun ident acc -> snd (add_ident ~mangled:(Ext_ident.convert ident.name) ident.stamp acc)) + set cxt (* Assume that all idents are already in [scope] so both [param/0] and [param/1] are in idents, we don't need update twice, once is enough *) let sub_scope (scope : t) (idents : Set_ident.t) : t = - Set_ident.fold idents empty (fun {name} acc -> + Set_ident.fold + (fun {name} acc -> let mangled = Ext_ident.convert name in match Map_string.find_exn scope mangled with | exception Not_found -> assert false | stamps -> if Map_string.mem acc mangled then acc else Map_string.add acc mangled stamps) + idents empty diff --git a/compiler/ext/ext_set.ml b/compiler/ext/ext_set.ml deleted file mode 100644 index 04a811cb44..0000000000 --- a/compiler/ext/ext_set.ml +++ /dev/null @@ -1,197 +0,0 @@ -(* Copyright (C) 2015-2016 Bloomberg Finance L.P. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * In addition to the permissions granted to you by the LGPL, you may combine - * or link a "work that uses the Library" with a publicly distributed version - * of this file to produce a combined library or application, then distribute - * that combined work under the terms of your choosing, with no requirement - * to comply with the obligations normally placed on you by section 4 of the - * LGPL version 3 (or the corresponding section of a later version of the LGPL - * should you choose to use a later version). - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) - -module type OrderedType = sig - type t - - val compare : t -> t -> int - val equal : t -> t -> bool -end - -module Make (Elt : OrderedType) = struct - type elt = Elt.t - - let compare_elt = Elt.compare - let eq_elt = Elt.equal - - type 'a t0 = 'a Set_gen.t - - type t = elt t0 - - let empty = Set_gen.empty - let is_empty = Set_gen.is_empty - let iter = Set_gen.iter - let fold = Set_gen.fold - let for_all = Set_gen.for_all - let exists = Set_gen.exists - let singleton = Set_gen.singleton - let cardinal = Set_gen.cardinal - let elements = Set_gen.elements - let choose = Set_gen.choose - - let of_sorted_array = Set_gen.of_sorted_array - - let rec mem (tree : t) (x : elt) = - match tree with - | Empty -> false - | Leaf v -> eq_elt x v - | Node {l; v; r} -> - let c = compare_elt x v in - c = 0 || mem (if c < 0 then l else r) x - - type split = Yes of {l: t; r: t} | No of {l: t; r: t} - - let[@inline] split_l (x : split) = - match x with - | Yes {l} | No {l} -> l - - let[@inline] split_r (x : split) = - match x with - | Yes {r} | No {r} -> r - - let[@inline] split_pres (x : split) = - match x with - | Yes _ -> true - | No _ -> false - - let rec split (tree : t) x : split = - match tree with - | Empty -> No {l = empty; r = empty} - | Leaf v -> - let c = compare_elt x v in - if c = 0 then Yes {l = empty; r = empty} - else if c < 0 then No {l = empty; r = tree} - else No {l = tree; r = empty} - | Node {l; v; r} -> ( - let c = compare_elt x v in - if c = 0 then Yes {l; r} - else if c < 0 then - match split l x with - | Yes result -> Yes {result with r = Set_gen.internal_join result.r v r} - | No result -> No {result with r = Set_gen.internal_join result.r v r} - else - match split r x with - | Yes result -> Yes {result with l = Set_gen.internal_join l v result.l} - | No result -> No {result with l = Set_gen.internal_join l v result.l}) - - let rec add (tree : t) x : t = - match tree with - | Empty -> singleton x - | Leaf v -> - let c = compare_elt x v in - if c = 0 then tree - else if c < 0 then Set_gen.unsafe_two_elements x v - else Set_gen.unsafe_two_elements v x - | Node {l; v; r} as t -> - let c = compare_elt x v in - if c = 0 then t - else if c < 0 then Set_gen.bal (add l x) v r - else Set_gen.bal l v (add r x) - - let rec union (s1 : t) (s2 : t) : t = - match (s1, s2) with - | Empty, t | t, Empty -> t - | Node _, Leaf v2 -> add s1 v2 - | Leaf v1, Node _ -> add s2 v1 - | Leaf x, Leaf v -> - let c = compare_elt x v in - if c = 0 then s1 - else if c < 0 then Set_gen.unsafe_two_elements x v - else Set_gen.unsafe_two_elements v x - | ( Node {l = l1; v = v1; r = r1; h = h1}, - Node {l = l2; v = v2; r = r2; h = h2} ) -> - if h1 >= h2 then - let split_result = split s2 v1 in - Set_gen.internal_join - (union l1 (split_l split_result)) - v1 - (union r1 (split_r split_result)) - else - let split_result = split s1 v2 in - Set_gen.internal_join - (union (split_l split_result) l2) - v2 - (union (split_r split_result) r2) - - let rec inter (s1 : t) (s2 : t) : t = - match (s1, s2) with - | Empty, _ | _, Empty -> empty - | Leaf v, _ -> if mem s2 v then s1 else empty - | Node ({v} as s1), _ -> - let result = split s2 v in - if split_pres result then - Set_gen.internal_join - (inter s1.l (split_l result)) - v - (inter s1.r (split_r result)) - else - Set_gen.internal_concat - (inter s1.l (split_l result)) - (inter s1.r (split_r result)) - - let rec diff (s1 : t) (s2 : t) : t = - match (s1, s2) with - | Empty, _ -> empty - | t1, Empty -> t1 - | Leaf v, _ -> if mem s2 v then empty else s1 - | Node ({v} as s1), _ -> - let result = split s2 v in - if split_pres result then - Set_gen.internal_concat - (diff s1.l (split_l result)) - (diff s1.r (split_r result)) - else - Set_gen.internal_join - (diff s1.l (split_l result)) - v - (diff s1.r (split_r result)) - - let rec remove (tree : t) (x : elt) : t = - match tree with - | Empty -> empty (* This case actually would be never reached *) - | Leaf v -> if eq_elt x v then empty else tree - | Node {l; v; r} -> - let c = compare_elt x v in - if c = 0 then Set_gen.internal_merge l r - else if c < 0 then Set_gen.bal (remove l x) v r - else Set_gen.bal l v (remove r x) - - let of_list l = - match l with - | [] -> empty - | [x0] -> singleton x0 - | [x0; x1] -> add (singleton x0) x1 - | [x0; x1; x2] -> add (add (singleton x0) x1) x2 - | [x0; x1; x2; x3] -> add (add (add (singleton x0) x1) x2) x3 - | [x0; x1; x2; x3; x4] -> add (add (add (add (singleton x0) x1) x2) x3) x4 - | _ -> - let arrs = Array.of_list l in - Array.sort compare_elt arrs; - of_sorted_array arrs - - (* also check order *) - let invariant t = - Set_gen.check t; - Set_gen.is_ordered ~cmp:compare_elt t -end diff --git a/compiler/ext/ext_set.mli b/compiler/ext/ext_set.mli deleted file mode 100644 index fcb00c6d4f..0000000000 --- a/compiler/ext/ext_set.mli +++ /dev/null @@ -1,32 +0,0 @@ -(* Copyright (C) 2015-2016 Bloomberg Finance L.P. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * In addition to the permissions granted to you by the LGPL, you may combine - * or link a "work that uses the Library" with a publicly distributed version - * of this file to produce a combined library or application, then distribute - * that combined work under the terms of your choosing, with no requirement - * to comply with the obligations normally placed on you by section 4 of the - * LGPL version 3 (or the corresponding section of a later version of the LGPL - * should you choose to use a later version). - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) - -module type OrderedType = sig - type t - - val compare : t -> t -> int - val equal : t -> t -> bool -end - -module Make (Elt : OrderedType) : Set_gen.S with type elt = Elt.t diff --git a/compiler/ext/set_gen.ml b/compiler/ext/set_gen.ml deleted file mode 100644 index 74892686b6..0000000000 --- a/compiler/ext/set_gen.ml +++ /dev/null @@ -1,342 +0,0 @@ -(***********************************************************************) -(* *) -(* OCaml *) -(* *) -(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *) -(* *) -(* Copyright 1996 Institut National de Recherche en Informatique et *) -(* en Automatique. All rights reserved. This file is distributed *) -(* under the terms of the GNU Library General Public License, with *) -(* the special exception on linking described in file ../LICENSE. *) -(* *) -(***********************************************************************) -[@@@warnerror "+55"] - -(* balanced tree based on stdlib distribution *) - -type 'a t0 = Empty | Leaf of 'a | Node of {l: 'a t0; v: 'a; r: 'a t0; h: int} - -type 'a partial_node = {l: 'a t0; v: 'a; r: 'a t0; h: int} - -external ( ~! ) : 'a t0 -> 'a partial_node = "%identity" - -let empty = Empty - -let[@inline] height = function - | Empty -> 0 - | Leaf _ -> 1 - | Node {h} -> h - -let[@inline] calc_height a b = (if a >= b then a else b) + 1 - -(* - Invariants: - 1. {[ l < v < r]} - 2. l and r balanced - 3. [height l] - [height r] <= 2 -*) -let[@inline] unsafe_node v l r h = Node {l; v; r; h} - -let[@inline] unsafe_node_maybe_leaf v l r h = - if h = 1 then Leaf v else Node {l; v; r; h} - -let[@inline] singleton x = Leaf x - -let[@inline] unsafe_two_elements x v = unsafe_node v (singleton x) empty 2 - -type 'a t = 'a t0 = private - | Empty - | Leaf of 'a - | Node of {l: 'a t0; v: 'a; r: 'a t0; h: int} - -(* Smallest and greatest element of a set *) - -let rec min_exn = function - | Empty -> raise Not_found - | Leaf v -> v - | Node {l; v} -> ( - match l with - | Empty -> v - | Leaf _ | Node _ -> min_exn l) - -let[@inline] is_empty = function - | Empty -> true - | _ -> false - -let rec cardinal_aux acc = function - | Empty -> acc - | Leaf _ -> acc + 1 - | Node {l; r} -> cardinal_aux (cardinal_aux (acc + 1) r) l - -let cardinal s = cardinal_aux 0 s - -let rec elements_aux accu = function - | Empty -> accu - | Leaf v -> v :: accu - | Node {l; v; r} -> elements_aux (v :: elements_aux accu r) l - -let elements s = elements_aux [] s - -let choose = min_exn - -let rec iter x f = - match x with - | Empty -> () - | Leaf v -> f v - | Node {l; v; r} -> - iter l f; - f v; - iter r f - -let rec fold s accu f = - match s with - | Empty -> accu - | Leaf v -> f v accu - | Node {l; v; r} -> fold r (f v (fold l accu f)) f - -let rec for_all x p = - match x with - | Empty -> true - | Leaf v -> p v - | Node {l; v; r} -> p v && for_all l p && for_all r p - -let rec exists x p = - match x with - | Empty -> false - | Leaf v -> p v - | Node {l; v; r} -> p v || exists l p || exists r p - -exception Height_invariant_broken - -exception Height_diff_borken - -let rec check_height_and_diff = function - | Empty -> 0 - | Leaf _ -> 1 - | Node {l; r; h} -> - let hl = check_height_and_diff l in - let hr = check_height_and_diff r in - if h <> calc_height hl hr then raise Height_invariant_broken - else - let diff = abs (hl - hr) in - if diff > 2 then raise Height_diff_borken else h - -let check tree = ignore (check_height_and_diff tree) - -(* Same as create, but performs one step of rebalancing if necessary. - Invariants: - 1. {[ l < v < r ]} - 2. l and r balanced - 3. | height l - height r | <= 3. - - Proof by indunction - - Lemma: the height of [bal l v r] will bounded by [max l r] + 1 -*) -let bal l v r : _ t = - let hl = height l in - let hr = height r in - if hl > hr + 2 then - let {l = ll; r = lr; v = lv; h = _} = ~!l in - let hll = height ll in - let hlr = height lr in - if hll >= hlr then - let hnode = calc_height hlr hr in - unsafe_node lv ll - (unsafe_node_maybe_leaf v lr r hnode) - (calc_height hll hnode) - else - let {l = lrl; r = lrr; v = lrv} = ~!lr in - let hlrl = height lrl in - let hlrr = height lrr in - let hlnode = calc_height hll hlrl in - let hrnode = calc_height hlrr hr in - unsafe_node lrv - (unsafe_node_maybe_leaf lv ll lrl hlnode) - (unsafe_node_maybe_leaf v lrr r hrnode) - (calc_height hlnode hrnode) - else if hr > hl + 2 then - let {l = rl; r = rr; v = rv} = ~!r in - let hrr = height rr in - let hrl = height rl in - if hrr >= hrl then - let hnode = calc_height hl hrl in - unsafe_node rv - (unsafe_node_maybe_leaf v l rl hnode) - rr (calc_height hnode hrr) - else - let {l = rll; r = rlr; v = rlv} = ~!rl in - let hrll = height rll in - let hrlr = height rlr in - let hlnode = calc_height hl hrll in - let hrnode = calc_height hrlr hrr in - unsafe_node rlv - (unsafe_node_maybe_leaf v l rll hlnode) - (unsafe_node_maybe_leaf rv rlr rr hrnode) - (calc_height hlnode hrnode) - else unsafe_node_maybe_leaf v l r (calc_height hl hr) - -let rec remove_min_elt = function - | Empty -> invalid_arg "Set.remove_min_elt" - | Leaf _ -> empty - | Node {l = Empty; r} -> r - | Node {l; v; r} -> bal (remove_min_elt l) v r - -(* - All elements of l must precede the elements of r. - Assume | height l - height r | <= 2. - weak form of [concat] -*) - -let internal_merge l r = - match (l, r) with - | Empty, t -> t - | t, Empty -> t - | _, _ -> bal l (min_exn r) (remove_min_elt r) - -(* Beware: those two functions assume that the added v is *strictly* - smaller (or bigger) than all the present elements in the tree; it - does not test for equality with the current min (or max) element. - Indeed, they are only used during the "join" operation which - respects this precondition. -*) - -let rec add_min v = function - | Empty -> singleton v - | Leaf x -> unsafe_two_elements v x - | Node n -> bal (add_min v n.l) n.v n.r - -let rec add_max v = function - | Empty -> singleton v - | Leaf x -> unsafe_two_elements x v - | Node n -> bal n.l n.v (add_max v n.r) - -(** - Invariants: - 1. l < v < r - 2. l and r are balanced - - Proof by induction - The height of output will be ~~ (max (height l) (height r) + 2) - Also use the lemma from [bal] -*) -let rec internal_join l v r = - match (l, r) with - | Empty, _ -> add_min v r - | _, Empty -> add_max v l - | Leaf lv, Node {h = rh} -> - if rh > 3 then add_min lv (add_min v r) (* FIXME: could inlined *) - else unsafe_node v l r (rh + 1) - | Leaf _, Leaf _ -> unsafe_node v l r 2 - | Node {h = lh}, Leaf rv -> - if lh > 3 then add_max rv (add_max v l) else unsafe_node v l r (lh + 1) - | Node {l = ll; v = lv; r = lr; h = lh}, Node {l = rl; v = rv; r = rr; h = rh} - -> - if lh > rh + 2 then - (* proof by induction: - now [height of ll] is [lh - 1] - *) - bal ll lv (internal_join lr v r) - else if rh > lh + 2 then bal (internal_join l v rl) rv rr - else unsafe_node v l r (calc_height lh rh) - -(* - Required Invariants: - [t1] < [t2] -*) -let internal_concat t1 t2 = - match (t1, t2) with - | Empty, t -> t - | t, Empty -> t - | _, _ -> internal_join t1 (min_exn t2) (remove_min_elt t2) - -let of_sorted_array l = - let rec sub start n l = - if n = 0 then empty - else if n = 1 then - let x0 = Array.unsafe_get l start in - singleton x0 - else if n = 2 then - let x0 = Array.unsafe_get l start in - let x1 = Array.unsafe_get l (start + 1) in - unsafe_node x1 (singleton x0) empty 2 - else if n = 3 then - let x0 = Array.unsafe_get l start in - let x1 = Array.unsafe_get l (start + 1) in - let x2 = Array.unsafe_get l (start + 2) in - unsafe_node x1 (singleton x0) (singleton x2) 2 - else - let nl = n / 2 in - let left = sub start nl l in - let mid = start + nl in - let v = Array.unsafe_get l mid in - let right = sub (mid + 1) (n - nl - 1) l in - unsafe_node v left right (calc_height (height left) (height right)) - in - sub 0 (Array.length l) l - -let is_ordered ~cmp tree = - let rec is_ordered_min_max tree = - match tree with - | Empty -> `Empty - | Leaf v -> `V (v, v) - | Node {l; v; r} -> ( - match is_ordered_min_max l with - | `No -> `No - | `Empty -> ( - match is_ordered_min_max r with - | `No -> `No - | `Empty -> `V (v, v) - | `V (l, r) -> if cmp v l < 0 then `V (v, r) else `No) - | `V (min_v, max_v) -> ( - match is_ordered_min_max r with - | `No -> `No - | `Empty -> if cmp max_v v < 0 then `V (min_v, v) else `No - | `V (min_v_r, max_v_r) -> - if cmp max_v min_v_r < 0 then `V (min_v, max_v_r) else `No)) - in - is_ordered_min_max tree <> `No - -module type S = sig - type elt - - type t - - val empty : t - - val is_empty : t -> bool - - val iter : t -> (elt -> unit) -> unit - - val fold : t -> 'a -> (elt -> 'a -> 'a) -> 'a - - val for_all : t -> (elt -> bool) -> bool - - val exists : t -> (elt -> bool) -> bool - - val singleton : elt -> t - - val cardinal : t -> int - - val elements : t -> elt list - - val choose : t -> elt - - val mem : t -> elt -> bool - - val add : t -> elt -> t - - val remove : t -> elt -> t - - val union : t -> t -> t - - val inter : t -> t -> t - - val diff : t -> t -> t - - val of_list : elt list -> t - - val of_sorted_array : elt array -> t - - val invariant : t -> bool -end diff --git a/compiler/ext/set_gen.mli b/compiler/ext/set_gen.mli deleted file mode 100644 index cdd9247541..0000000000 --- a/compiler/ext/set_gen.mli +++ /dev/null @@ -1,84 +0,0 @@ -type 'a t = private - | Empty - | Leaf of 'a - | Node of {l: 'a t; v: 'a; r: 'a t; h: int} - -val empty : 'a t - -val is_empty : 'a t -> bool - -val unsafe_two_elements : 'a -> 'a -> 'a t - -val cardinal : 'a t -> int - -val elements : 'a t -> 'a list - -val choose : 'a t -> 'a - -val iter : 'a t -> ('a -> unit) -> unit - -val fold : 'a t -> 'c -> ('a -> 'c -> 'c) -> 'c - -val for_all : 'a t -> ('a -> bool) -> bool - -val exists : 'a t -> ('a -> bool) -> bool - -val check : 'a t -> unit - -val bal : 'a t -> 'a -> 'a t -> 'a t - -val singleton : 'a -> 'a t - -val internal_merge : 'a t -> 'a t -> 'a t - -val internal_join : 'a t -> 'a -> 'a t -> 'a t - -val internal_concat : 'a t -> 'a t -> 'a t - -val of_sorted_array : 'a array -> 'a t - -val is_ordered : cmp:('a -> 'a -> int) -> 'a t -> bool - -module type S = sig - type elt - - type t - - val empty : t - - val is_empty : t -> bool - - val iter : t -> (elt -> unit) -> unit - - val fold : t -> 'a -> (elt -> 'a -> 'a) -> 'a - - val for_all : t -> (elt -> bool) -> bool - - val exists : t -> (elt -> bool) -> bool - - val singleton : elt -> t - - val cardinal : t -> int - - val elements : t -> elt list - - val choose : t -> elt - - val mem : t -> elt -> bool - - val add : t -> elt -> t - - val remove : t -> elt -> t - - val union : t -> t -> t - - val inter : t -> t -> t - - val diff : t -> t -> t - - val of_list : elt list -> t - - val of_sorted_array : elt array -> t - - val invariant : t -> bool -end diff --git a/compiler/ext/set_ident.ml b/compiler/ext/set_ident.ml index 483e0ab6bc..86657fba76 100644 --- a/compiler/ext/set_ident.ml +++ b/compiler/ext/set_ident.ml @@ -22,7 +22,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -include Ext_set.Make (struct +include Set.Make (struct type t = Ident.t let compare (x : t) (y : t) = @@ -31,6 +31,4 @@ include Ext_set.Make (struct else let name = Stdlib.compare x.name y.name in if name <> 0 then name else Stdlib.compare x.flags y.flags - - let equal = Ident.same end) diff --git a/compiler/ext/set_ident.mli b/compiler/ext/set_ident.mli index 49638c9f46..b2271582ac 100644 --- a/compiler/ext/set_ident.mli +++ b/compiler/ext/set_ident.mli @@ -22,4 +22,4 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -include Set_gen.S with type elt = Ident.t +include Set.S with type elt = Ident.t diff --git a/compiler/ext/set_int.ml b/compiler/ext/set_int.ml index 91505b5c32..170b951cf7 100644 --- a/compiler/ext/set_int.ml +++ b/compiler/ext/set_int.ml @@ -22,9 +22,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -include Ext_set.Make (struct +include Set.Make (struct type t = int let compare = Ext_int.compare - let equal = Ext_int.equal end) diff --git a/compiler/ext/set_int.mli b/compiler/ext/set_int.mli index 80b3b6c16e..db239d5c0a 100644 --- a/compiler/ext/set_int.mli +++ b/compiler/ext/set_int.mli @@ -1 +1 @@ -include Set_gen.S with type elt = int +include Set.S with type elt = int diff --git a/compiler/ext/set_string.ml b/compiler/ext/set_string.ml index ef7f66fcbf..e5765b7cdf 100644 --- a/compiler/ext/set_string.ml +++ b/compiler/ext/set_string.ml @@ -22,9 +22,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -include Ext_set.Make (struct +include Set.Make (struct type t = string let compare = Ext_string.compare - let equal = Ext_string.equal end) diff --git a/compiler/ext/set_string.mli b/compiler/ext/set_string.mli index d734859735..fbb89c9095 100644 --- a/compiler/ext/set_string.mli +++ b/compiler/ext/set_string.mli @@ -22,4 +22,4 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -include Set_gen.S with type elt = string +include Set.S with type elt = string diff --git a/compiler/ml/lambda_traverse.ml b/compiler/ml/lambda_traverse.ml index 5883126c6e..d4964dea77 100644 --- a/compiler/ml/lambda_traverse.ml +++ b/compiler/ml/lambda_traverse.ml @@ -210,20 +210,20 @@ let free_ids get l = let fv = ref Set_ident.empty in let rec free l = iter free l; - fv := List.fold_left Set_ident.add !fv (get l); + fv := List.fold_left (fun acc id -> Set_ident.add id acc) !fv (get l); match l with | Lfunction {params} -> - List.iter (fun param -> fv := Set_ident.remove !fv param) params - | Llet (_str, id, _arg, _body) -> fv := Set_ident.remove !fv id + List.iter (fun param -> fv := Set_ident.remove param !fv) params + | Llet (_str, id, _arg, _body) -> fv := Set_ident.remove id !fv | Lletrec (decl, _body) -> - List.iter (fun (id, _exp) -> fv := Set_ident.remove !fv id) decl + List.iter (fun (id, _exp) -> fv := Set_ident.remove id !fv) decl | Lstaticcatch (_e1, (_, vars), _e2) -> - List.iter (fun id -> fv := Set_ident.remove !fv id) vars - | Ltrywith (_e1, exn, _e2) -> fv := Set_ident.remove !fv exn - | Lfor (v, _e1, _e2, _dir, _e3) -> fv := Set_ident.remove !fv v + List.iter (fun id -> fv := Set_ident.remove id !fv) vars + | Ltrywith (_e1, exn, _e2) -> fv := Set_ident.remove exn !fv + | Lfor (v, _e1, _e2, _dir, _e3) -> fv := Set_ident.remove v !fv | Lfor_of (v, _e1, _e2) | Lfor_await_of (v, _e1, _e2) -> - fv := Set_ident.remove !fv v - | Lassign (id, _e) -> fv := Set_ident.add !fv id + fv := Set_ident.remove v !fv + | Lassign (id, _e) -> fv := Set_ident.add id !fv | Lvar _ | Lglobal_module _ | Lconst _ | Lapply _ | Lprim _ | Lswitch _ | Lstringswitch _ | Lstaticraise _ | Lifthenelse _ | Lsequence _ | Lbreak | Lcontinue | Lwhile _ -> diff --git a/compiler/ml/matching.ml b/compiler/ml/matching.ml index f76f6ccb62..dfabf676e2 100644 --- a/compiler/ml/matching.ml +++ b/compiler/ml/matching.ml @@ -395,7 +395,7 @@ let action_free_variables {binds; guard; body} = (fun (_, id, e) acc -> Set_ident.union (Lambda_traverse.free_variables e) - (Set_ident.remove acc id)) + (Set_ident.remove id acc)) binds inner type pattern_matching = { @@ -647,14 +647,14 @@ let default_compat p def = (* Or-pattern expansion, variables are a complication w.r.t. the article *) let rec extract_vars r p = match p.pat_desc with - | Tpat_var (id, _) -> Set_ident.add r id - | Tpat_alias (p, id, _) -> extract_vars (Set_ident.add r id) p + | Tpat_var (id, _) -> Set_ident.add id r + | Tpat_alias (p, id, _) -> extract_vars (Set_ident.add id r) p | Tpat_tuple pats -> List.fold_left extract_vars r pats | Tpat_record (lpats, _, rest) -> ( let r = List.fold_left (fun r (_, _, p, _) -> extract_vars r p) r lpats in match rest with | None -> r - | Some rest -> Set_ident.add r rest.rest_ident) + | Some rest -> Set_ident.add rest.rest_ident r) | Tpat_dict entries -> List.fold_left (fun r {tdp_pattern} -> extract_vars r tdp_pattern) r entries | Tpat_construct (_, _, pats) -> List.fold_left extract_vars r pats diff --git a/compiler/ml/mtype.ml b/compiler/ml/mtype.ml index dddeb68223..e3c61ab789 100644 --- a/compiler/ml/mtype.ml +++ b/compiler/ml/mtype.ml @@ -294,7 +294,7 @@ let rec collect_ids subst bindings p = try collect_ids subst bindings (Ident.find_same id bindings) with Not_found -> Set_ident.empty in - Set_ident.add ids id + Set_ident.add id ids | _ -> Set_ident.empty let collect_arg_paths mty = @@ -343,7 +343,7 @@ and remove_aliases_sig env excl sg = | Sig_module (id, md, rs) :: rem -> let mty = match md.md_type with - | Mty_alias _ when Set_ident.mem excl id -> md.md_type + | Mty_alias _ when Set_ident.mem id excl -> md.md_type | mty -> remove_aliases env excl mty in Sig_module (id, {md with md_type = mty}, rs) diff --git a/compiler/ml/parmatch.ml b/compiler/ml/parmatch.ml index 311a682b5e..a92bcdceae 100644 --- a/compiler/ml/parmatch.ml +++ b/compiler/ml/parmatch.ml @@ -2302,9 +2302,9 @@ type amb_row = {unseen: pattern list; seen: Set_ident.t list} let rec do_push r p ps seen k = match p.pat_desc with - | Tpat_alias (p, x, _) -> do_push (Set_ident.add r x) p ps seen k + | Tpat_alias (p, x, _) -> do_push (Set_ident.add x r) p ps seen k | Tpat_var (x, _) -> - (omega, {unseen = ps; seen = Set_ident.add r x :: seen}) :: k + (omega, {unseen = ps; seen = Set_ident.add x r :: seen}) :: k | Tpat_or (p1, p2, _) -> do_push r p1 ps seen (do_push r p2 ps seen k) | _ -> (p, {unseen = ps; seen = r :: seen}) :: k @@ -2464,7 +2464,7 @@ let all_rhs_idents exp = let enter_expression exp = match exp.exp_desc with | Texp_ident (path, _lid, _descr) -> - List.iter (fun id -> ids := Set_ident.add !ids id) (Path.heads path) + List.iter (fun id -> ids := Set_ident.add id !ids) (Path.heads path) | _ -> () (* Very hackish, detect unpack pattern compilation @@ -2484,9 +2484,9 @@ let all_rhs_idents exp = ({exp_desc = Texp_ident (Path.Pident id_exp, _, _)}, _); }, _ ) -> - assert (Set_ident.mem !ids id_exp); - if not (Set_ident.mem !ids id_mod) then - ids := Set_ident.remove !ids id_exp + assert (Set_ident.mem id_exp !ids); + if not (Set_ident.mem id_mod !ids) then + ids := Set_ident.remove id_exp !ids | _ -> assert false end) in Iterator.iter_expression exp; diff --git a/compiler/ml/record_runtime.ml b/compiler/ml/record_runtime.ml index a2103af8ea..848c65b755 100644 --- a/compiler/ml/record_runtime.ml +++ b/compiler/ml/record_runtime.ml @@ -29,9 +29,9 @@ let rec check_duplicated_labels_aux (lbls : Parsetree.label_declaration list) match lbls with | [] -> None | ({pld_name = {txt}} as lbl) :: rest -> ( - if Set_string.mem coll txt && txt <> "..." then Some lbl.pld_name + if Set_string.mem txt coll && txt <> "..." then Some lbl.pld_name else - let coll_with_lbl = Set_string.add coll txt in + let coll_with_lbl = Set_string.add txt coll in match lbl.pld_runtime_name with | None -> check_duplicated_labels_aux rest coll_with_lbl | Some {txt; loc} -> @@ -39,9 +39,9 @@ let rec check_duplicated_labels_aux (lbls : Parsetree.label_declaration list) (* Checked against the fields seen before this one rather than against [coll_with_lbl], so that [@as("x") x] renames a field to the name it already has. *) - if Set_string.mem coll name then Some {Asttypes.txt = name; loc} + if Set_string.mem name coll then Some {Asttypes.txt = name; loc} else - check_duplicated_labels_aux rest (Set_string.add coll_with_lbl name)) + check_duplicated_labels_aux rest (Set_string.add name coll_with_lbl)) (* A field has one runtime name, so only the first [@as] naming it is taken out; a second one is left behind and reported here. *) diff --git a/compiler/ml/transl_recmodule.ml b/compiler/ml/transl_recmodule.ml index e3ea5b4547..5cb8007aca 100644 --- a/compiler/ml/transl_recmodule.ml +++ b/compiler/ml/transl_recmodule.ml @@ -99,7 +99,7 @@ let reorder_rec_bindings bindings = if init.(i) = None then ( status.(i) <- Inprogress; for j = 0 to num_bindings - 1 do - if Set_ident.mem fv.(i) id.(j) then emit_binding j + if Set_ident.mem id.(j) fv.(i) then emit_binding j done); res := (id.(i), init.(i), rhs.(i)) :: !res; status.(i) <- Defined @@ -173,25 +173,25 @@ let rec is_function_or_const_block (lam : Lambda.t) acc = | Lprim {primitive = Pmakeblock _; args; loc = _} -> Ext_list.for_all args (fun x -> match x with - | Lvar id -> Set_ident.mem acc id + | Lvar id -> Set_ident.mem id acc | Lfunction _ | Lconst _ -> true | _ -> false) | Llet (_, id, Lfunction _, cont) -> - is_function_or_const_block cont (Set_ident.add acc id) + is_function_or_const_block cont (Set_ident.add id acc) | Lletrec (bindings, cont) -> ( let rec aux_bindings bindings acc = match bindings with | [] -> Some acc | (id, Lambda.Lfunction _) :: rest -> - aux_bindings rest (Set_ident.add acc id) + aux_bindings rest (Set_ident.add id acc) | (_, _) :: _ -> None in match aux_bindings bindings acc with | None -> false | Some acc -> is_function_or_const_block cont acc) | Llet (_, _, Lconst _, cont) -> is_function_or_const_block cont acc - | Llet (_, id1, Lvar id2, cont) when Set_ident.mem acc id2 -> - is_function_or_const_block cont (Set_ident.add acc id1) + | Llet (_, id1, Lvar id2, cont) when Set_ident.mem id2 acc -> + is_function_or_const_block cont (Set_ident.add id1 acc) | _ -> false let is_strict_or_all_functions (xs : binding list) = diff --git a/compiler/ml/translmod.ml b/compiler/ml/translmod.ml index 5876dcd507..a1eb7bb8df 100644 --- a/compiler/ml/translmod.ml +++ b/compiler/ml/translmod.ml @@ -132,7 +132,7 @@ and wrap_id_pos_list loc id_pos_list get_field lam = let lam, s = List.fold_left (fun (lam, s) (id', pos, c) -> - if Set_ident.mem fv id' then + if Set_ident.mem id' fv then let id'' = Ident.create (Ident.name id') in ( Lambda.let_ Alias id'' (apply_coercion loc Alias c (get_field (Ident.name id') pos)) @@ -327,7 +327,7 @@ and transl_structure loc fields cc rootpath final_env = function assert (List.length runtime_fields = List.length pos_cc_list); let v = reverse_of_list fields in let get_field pos = Lambda.var v.(pos) - and ids = List.fold_left Set_ident.add Set_ident.empty fields in + and ids = Set_ident.of_list fields in let get_field_name _name = get_field in let result = List.fold_right @@ -354,7 +354,7 @@ and transl_structure loc fields cc rootpath final_env = function ~args:result loc and id_pos_list = Ext_list.filter id_pos_list (fun (id, _, _) -> - not (Set_ident.mem ids id)) + not (Set_ident.mem id ids)) in ( wrap_id_pos_list loc id_pos_list get_field_name lam, List.length pos_cc_list ) diff --git a/tests/ounit_tests/ounit_bal_tree_tests.ml b/tests/ounit_tests/ounit_bal_tree_tests.ml deleted file mode 100644 index 08ff8c9d4e..0000000000 --- a/tests/ounit_tests/ounit_bal_tree_tests.ml +++ /dev/null @@ -1,61 +0,0 @@ -let ( >:: ), ( >::: ) = OUnit.(( >:: ), ( >::: )) - -let ( =~ ) = OUnit.assert_equal - -module Set_poly = struct - include Set_int - let of_sorted_list xs = Array.of_list xs |> of_sorted_array - let of_array l = Array.fold_left add empty l -end -let suites = - __FILE__ - >::: [ - ( __LOC__ >:: fun _ -> - OUnit.assert_bool __LOC__ - (Set_poly.invariant - (Set_poly.of_array (Array.init 1000 (fun n -> n)))) ); - ( __LOC__ >:: fun _ -> - OUnit.assert_bool __LOC__ - (Set_poly.invariant - (Set_poly.of_array (Array.init 1000 (fun n -> 1000 - n)))) ); - ( __LOC__ >:: fun _ -> - OUnit.assert_bool __LOC__ - (Set_poly.invariant - (Set_poly.of_array (Array.init 1000 (fun _ -> Random.int 1000)))) - ); - ( __LOC__ >:: fun _ -> - OUnit.assert_bool __LOC__ - (Set_poly.invariant - (Set_poly.of_sorted_list - (Array.to_list (Array.init 1000 (fun n -> n))))) ); - ( __LOC__ >:: fun _ -> - let arr = Array.init 1000 (fun n -> n) in - let set = Set_poly.of_sorted_array arr in - OUnit.assert_bool __LOC__ (Set_poly.invariant set); - OUnit.assert_equal 1000 (Set_poly.cardinal set) ); - ( __LOC__ >:: fun _ -> - for i = 0 to 200 do - let arr = Array.init i (fun n -> n) in - let set = Set_poly.of_sorted_array arr in - OUnit.assert_bool __LOC__ (Set_poly.invariant set); - OUnit.assert_equal i (Set_poly.cardinal set) - done ); - ( __LOC__ >:: fun _ -> - let arr_size = 200 in - let arr_sets = Array.make 200 Set_poly.empty in - for i = 0 to arr_size - 1 do - let size = Random.int 1000 in - let arr = Array.init size (fun n -> n) in - arr_sets.(i) <- Set_poly.of_sorted_array arr - done; - let large = Array.fold_left Set_poly.union Set_poly.empty arr_sets in - OUnit.assert_bool __LOC__ (Set_poly.invariant large) ); - ( __LOC__ >:: fun _ -> - let arr_size = 1_00_000 in - let v = ref Set_int.empty in - for _ = 0 to arr_size - 1 do - let size = Random.int 0x3FFFFFFF in - v := Set_int.add !v size - done; - OUnit.assert_bool __LOC__ (Set_int.invariant !v) ); - ] diff --git a/tests/ounit_tests/ounit_js_analyzer_tests.ml b/tests/ounit_tests/ounit_js_analyzer_tests.ml index aab68618ad..c714e53e54 100644 --- a/tests/ounit_tests/ounit_js_analyzer_tests.ml +++ b/tests/ounit_tests/ounit_js_analyzer_tests.ml @@ -128,9 +128,9 @@ let suites = Js_analyzer.free_variables_of_statement (record_rest_statement ~source ~field ~rest) in - OUnit.assert_bool __LOC__ (Set_ident.mem free source); - OUnit.assert_bool __LOC__ (not (Set_ident.mem free field)); - OUnit.assert_bool __LOC__ (not (Set_ident.mem free rest)) ); + OUnit.assert_bool __LOC__ (Set_ident.mem source free); + OUnit.assert_bool __LOC__ (not (Set_ident.mem field free)); + OUnit.assert_bool __LOC__ (not (Set_ident.mem rest free)) ); ( __LOC__ >:: fun _ -> let param = Ident.create "param" in let transformed = diff --git a/tests/ounit_tests/ounit_tests_main.ml b/tests/ounit_tests/ounit_tests_main.ml index c8881d80d3..20e04f8533 100644 --- a/tests/ounit_tests/ounit_tests_main.ml +++ b/tests/ounit_tests/ounit_tests_main.ml @@ -5,7 +5,6 @@ let suites = Ounit_json_tests.suites; Ounit_scc_tests.suites; Ounit_list_test.suites; - Ounit_bal_tree_tests.suites; Ounit_hash_stubs_test.suites; Ounit_map_tests.suites; Ounit_hashtbl_tests.suites; diff --git a/tests/ounit_tests/ounit_vec_test.ml b/tests/ounit_tests/ounit_vec_test.ml index 23f4ab41c2..4252964f75 100644 --- a/tests/ounit_tests/ounit_vec_test.ml +++ b/tests/ounit_tests/ounit_vec_test.ml @@ -56,7 +56,7 @@ let suites = let v = Vec_int.inplace_filter_with (fun x -> x mod 2 = 0) - ~cb_no:(fun a b -> Set_int.add b a) + ~cb_no:(fun a b -> Set_int.add a b) Set_int.empty u in let even, odd =