Skip to content

Commit 329a655

Browse files
committed
Canonicalize hashtables after running them through htab_map. Closes #77.
1 parent bd059a3 commit 329a655

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/boot/me/semant.ml

+3-11
Original file line numberDiff line numberDiff line change
@@ -817,14 +817,13 @@ let rebuild_ty_under_params
817817
end
818818
params
819819
in
820-
let substituted = ref false in
821820
let rec rebuild_ty t =
822821
let base = ty_fold_rebuild (fun t -> t) in
823822
let ty_fold_param (i, mut) =
824823
let param = Ast.TY_param (i, mut) in
825824
match htab_search pmap param with
826825
None -> param
827-
| Some arg -> (substituted := true; arg)
826+
| Some arg -> arg
828827
in
829828
let ty_fold_named n =
830829
let rec rebuild_name n =
@@ -863,7 +862,7 @@ let rebuild_ty_under_params
863862
begin
864863
match htab_search nmap id with
865864
None -> Ast.TY_named n
866-
| Some arg -> (substituted := true; arg)
865+
| Some arg -> arg
867866
end
868867
| _ -> Ast.TY_named n
869868
in
@@ -873,14 +872,7 @@ let rebuild_ty_under_params
873872
ty_fold_named = ty_fold_named;
874873
}
875874
in
876-
let t' = fold_ty fold t in
877-
(* FIXME (issue #77): "substituted" and "ty'" here are only required
878-
* because the current type-equality-comparison code in Type uses <>
879-
* and will judge some cases, such as rebuilt tags, as unequal simply
880-
* due to the different hashtable order in the fold. *)
881-
if !substituted
882-
then t'
883-
else t
875+
fold_ty fold t
884876
in
885877
rebuild_ty ty
886878
;;

src/boot/util/common.ml

+16-2
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,21 @@ let htab_put (htab:('a,'b) Hashtbl.t) (a:'a) (b:'b) : unit =
220220
Hashtbl.add htab a b
221221
;;
222222

223+
(* This is completely ridiculous, but it turns out that ocaml hashtables are
224+
* order-of-element-addition sensitive when it comes to the built-in
225+
* polymorphic comparison operator. So you have to canonicalize them after
226+
* you've stopped adding things to them if you ever want to use them in a
227+
* term that requires structural comparison to work. Sigh.
228+
*)
229+
230+
let htab_canonicalize (htab:('a,'b) Hashtbl.t) : ('a,'b) Hashtbl.t =
231+
let n = Hashtbl.create (Hashtbl.length htab) in
232+
Array.iter
233+
(fun k -> Hashtbl.add n k (Hashtbl.find htab k))
234+
(sorted_htab_keys htab);
235+
n
236+
;;
237+
223238
let htab_map
224239
(htab:('a,'b) Hashtbl.t)
225240
(f:'a -> 'b -> ('c * 'd))
@@ -230,10 +245,9 @@ let htab_map
230245
htab_put ntab c d
231246
in
232247
Hashtbl.iter g htab;
233-
ntab
248+
htab_canonicalize (ntab)
234249
;;
235250

236-
237251
let htab_fold
238252
(fn:'a -> 'b -> 'c -> 'c)
239253
(init:'c)

0 commit comments

Comments
 (0)