Skip to content

Commit 289eda3

Browse files
author
Roman Leshchinskiy
committed
Refactor shallow_modtypes
1 parent 4ae66b2 commit 289eda3

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

ocaml/typing/includemod.ml

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,33 @@ module Sign_diff = struct
446446
}
447447
end
448448

449+
(* Quickly compare module types without expanding them *)
450+
let rec shallow_modtypes env subst mty1 mty2 =
451+
let open Subst.Lazy in
452+
match mty1, mty2 with
453+
| Mty_alias p1, Mty_alias p2 ->
454+
not (Env.is_functor_arg p2 env) && equal_module_paths env p1 subst p2
455+
| Mty_ident p1, Mty_ident p2 ->
456+
equal_modtype_paths env p1 subst p2
457+
| Mty_strengthen (mty1,p1,_), Mty_strengthen (mty2,p2,_)
458+
when shallow_modtypes env subst mty1 mty2
459+
&& shallow_module_paths env subst p1 mty2 p2 ->
460+
true
461+
| Mty_strengthen (mty1,_,_), mty2 ->
462+
(* S with M <= S *)
463+
shallow_modtypes env subst mty1 mty2
464+
| _ -> false
465+
466+
and shallow_module_paths env subst p1 mty2 p2 =
467+
equal_module_paths env p1 subst p2 ||
468+
(* This shortcut is a significant win in some cases. Note we don't apply it
469+
recursively as doing seems to be a net loss. *)
470+
match (Env.find_module_lazy p1 env).md_type with
471+
| Mty_strengthen (mty1,p1,_) ->
472+
shallow_modtypes env subst mty1 mty2
473+
&& equal_module_paths env p1 subst p2
474+
| _ | exception Not_found -> false
475+
449476
(**
450477
In the group of mutual functions below, the [~in_eq] argument is [true] when
451478
we are in fact checking equality of module types.
@@ -462,32 +489,6 @@ end
462489
described above.
463490
*)
464491

465-
(* Quickly compare module types without expanding them *)
466-
let shallow_modtypes env subst mty1 mty2 =
467-
let open Subst.Lazy in
468-
let rec cmp_modtypes mty1 mty2 =
469-
match mty1, mty2 with
470-
| Mty_alias p1, Mty_alias p2 ->
471-
not (Env.is_functor_arg p2 env) && equal_module_paths env p1 subst p2
472-
| Mty_ident p1, Mty_ident p2 ->
473-
equal_modtype_paths env p1 subst p2
474-
| Mty_strengthen (mty1,p1,_), Mty_strengthen (mty2,p2,_)
475-
when cmp_modtypes mty1 mty2 && cmp_strengthen_paths p1 mty2 p2 ->
476-
true
477-
| Mty_strengthen (mty1,_,_), mty2 ->
478-
(* S/M <= S *)
479-
cmp_modtypes mty1 mty2
480-
| _ -> false
481-
and cmp_strengthen_paths p1 mty2 p2 =
482-
equal_module_paths env p1 subst p2 ||
483-
match (Env.find_module_lazy p1 env).md_type with
484-
| Mty_strengthen (mty1,p1,_) ->
485-
(* M : S/N ==> S/M < S/N *)
486-
cmp_modtypes mty1 mty2 && equal_module_paths env p1 subst p2
487-
| _ | exception Not_found -> false
488-
in
489-
cmp_modtypes mty1 mty2
490-
491492
let rec modtypes ~in_eq ~loc env ~mark subst mty1 mty2 shape =
492493
match try_modtypes ~in_eq ~loc env ~mark subst mty1 mty2 shape with
493494
| Ok _ as ok -> ok

ocaml/typing/mtype.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ and strengthen_lazy_sig' ~aliasable sg p =
100100
match decl.mtd_type with
101101
| Some _ when not aliasable ->
102102
(* [not alisable] condition needed because of recursive modules.
103-
See [Typemod.check_recmodule_inclusion]. *)
103+
See [Typemod.check_recmodule_inclusion]. *)
104104
decl
105105
| _ ->
106106
{decl with mtd_type = Some(Mty_ident(Pdot(p,Ident.name id)))}

0 commit comments

Comments
 (0)