Skip to content

Commit a4bbe1e

Browse files
committed
Statically separate the three kinds of δ-resolvers.
Except for the broken upper layers, there were actually three distinct kinds of δ-resolvers. 1. Resolvers from module bodies without any inlining information, as it makes no sense there. 2. Resolvers from module types, which may carry inlining parameter information, i.e. an inlining level for some constants. 3. Resolvers from module substitutions, which may carry actual inlining payload as terms attached to some constant to replace them. The upper layers used to gladly break these invariants to carry both the aliasing and inlining information through the same channel, but this was fixed in the last commit. This commit annotates the type of δ-resolvers to statically separate the various kinds in the code.
1 parent dd25f81 commit a4bbe1e

23 files changed

Lines changed: 381 additions & 193 deletions

checker/mod_checking.ml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ let rec check_mexpr env mse mp_mse res = match mse with
267267
| NoFunctor _ -> mod_delta mb
268268
| MoreFunctor _ -> Mod_subst.empty_delta_resolver mp
269269
in
270-
let subst = Mod_subst.map_mbid farg_id mp mp_delta in
270+
let subst =
271+
Mod_subst.map_mbid farg_id mp (Mod_subst.of_body_delta_resolver mp_delta)
272+
in
271273
Modops.subst_signature subst mp_mse fbody_b, Mod_subst.subst_codom_delta_resolver subst delta
272274
| MEwith _ -> CErrors.user_err Pp.(str "Unsupported 'with' constraint in module implementation")
273275

@@ -279,7 +281,7 @@ let rec check_mexpression env sign mbtyp mp_mse res = match sign with
279281
MoreFunctor(arg_id,mtb,body), delta
280282
| MENoFunctor me -> check_mexpr env me mp_mse res
281283

282-
let rec check_module env opac mp mb opacify =
284+
let rec check_module env opac mp (mb : Mod_declarations.module_body) opacify =
283285
Flags.if_verbose Feedback.msg_notice (str " checking module: " ++ str (ModPath.to_string mp));
284286
let delta_mb = mod_delta mb in
285287
let opac =
@@ -298,22 +300,23 @@ let rec check_module env opac mp mb opacify =
298300
let () = match optsign with
299301
| None -> ()
300302
| Some (sign,delta) ->
301-
let mtb1 = mk_mtb sign delta
302-
and mtb2 = mk_mtb (mod_type mb) delta_mb in
303+
let mtb1 = mk_mtb sign (Mod_subst.of_body_delta_resolver delta)
304+
and mtb2 = mk_mtb (mod_type mb) (Mod_subst.of_body_delta_resolver delta_mb) in
303305
let state = (Environ.universes env, Conversion.checked_universes) in
304306
let env = Modops.add_module mp (module_body_of_type mtb1) env in
305307
let _ : UGraph.t = Subtyping.check_subtypes state env mp mp mtb2 in
306308
()
307309
in
308310
opac
309311

310-
and check_module_type env mp mty =
312+
and check_module_type env mp (mty : Mod_declarations.module_type_body) =
311313
Flags.if_verbose Feedback.msg_notice (str " checking module type: " ++ str (ModPath.to_string @@ mp));
312314
let _ : check_state =
313315
check_signature env empty_state (mod_type mty) mp (mod_delta mty) empty_cset in
314316
()
315317

316-
and check_structure_field env opac mp lab res opacify = function
318+
and check_structure_field : type a. _ -> _ -> _ -> _ -> a Mod_subst.delta_resolver -> _ -> _ -> _ =
319+
fun env opac mp lab res opacify -> function
317320
| SFBconst cb ->
318321
let kn = KerName.make mp lab in
319322
let kn = Mod_subst.constant_of_delta_kn res kn in
@@ -342,7 +345,8 @@ and check_structure_field env opac mp lab res opacify = function
342345
check_rewrite_rules_body env lab rrb;
343346
Environ.add_rewrite_rules rrb.rewrules_rules env, opac
344347

345-
and check_signature env opac sign mp_mse res opacify = match sign with
348+
and check_signature : type a. _ -> _ -> _ -> _ -> a Mod_subst.delta_resolver -> _ -> _ =
349+
fun env opac sign mp_mse res opacify -> match sign with
346350
| MoreFunctor (arg_id, mtb, body) ->
347351
let () = check_module_type env (MPbound arg_id) mtb in
348352
let env' = Modops.add_module_parameter arg_id mtb env in

checker/values.ml

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,35 @@ let v_section_ctxt = v_enum "emptylist" 1
297297

298298
let v_univ_abstracted v = v_tuple "univ_abstracted" [|v;v_abs_context|]
299299

300-
let v_delta_hint =
301-
v_sum "delta_hint" 0 [|[|v_int; v_opt (v_univ_abstracted v_constr)|];[|v_kn|]|]
300+
let v_modbody_delta_hint =
301+
v_sum "delta_hint" 0
302+
[|[|v_kn|]; [|v_fail "inline"|]; [|v_fail "inline"|]|]
302303

303-
let v_resolver =
304+
let v_modtype_delta_hint =
305+
v_sum "delta_hint" 0
306+
[|[|v_kn|]; [|v_int|]; [|v_fail "inline"|]|]
307+
308+
let v_modsubs_delta_hint =
309+
v_sum "delta_hint" 0
310+
[|[|v_kn|]; [|v_fail "inline"|]; [|v_univ_abstracted v_constr|]|]
311+
312+
let v_modbody_resolver =
313+
v_tuple "delta_resolver"
314+
[|v_mp; v_map v_mp v_mp;
315+
v_hmap v_kn v_modbody_delta_hint|]
316+
317+
let v_modtype_resolver =
318+
v_tuple "delta_resolver"
319+
[|v_mp; v_map v_mp v_mp;
320+
v_hmap v_kn v_modtype_delta_hint|]
321+
322+
let v_modsubs_resolver =
304323
v_tuple "delta_resolver"
305324
[|v_mp; v_map v_mp v_mp;
306-
v_hmap v_kn v_delta_hint|]
325+
v_hmap v_kn v_modsubs_delta_hint|]
307326

308327
let v_subst =
309-
v_annot_c ("substitution", v_map v_mp v_resolver)
328+
v_annot_c ("substitution", v_map v_mp v_modsubs_resolver)
310329

311330
(** kernel/lazyconstr *)
312331

@@ -586,13 +605,13 @@ let [_v_sfb;_v_struc;_v_sign;_v_mexpr;_v_impl;v_module;_v_modtype] : _ Vector.t
586605
and v_impl =
587606
v_sum_c ("module_impl",2, (* Abstract, FullStruct *)
588607
[|[|v_mexpr|]; (* Algebraic *)
589-
[|v_resolver; v_struc|]|]) (* Struct *)
608+
[|v_modbody_resolver; v_struc|]|]) (* Struct *)
590609
and v_module =
591610
v_tuple_c ("module_body",
592-
[|v_sum_c ("when_mod_body", 0, [|[|v_impl|]|]);v_sign;v_opt v_mexpr;v_resolver|])
611+
[|v_sum_c ("when_mod_body", 0, [|[|v_impl|]|]);v_sign;v_opt v_mexpr;v_modbody_resolver|])
593612
and v_modtype =
594613
v_tuple_c ("module_type_body",
595-
[|v_noimpl;v_sign;v_opt v_mexpr;v_resolver|])
614+
[|v_noimpl;v_sign;v_opt v_mexpr;v_modtype_resolver|])
596615
in
597616
[v_sfb;v_struc;v_sign;v_mexpr;v_impl;v_module;v_modtype])
598617

dev/top_printers.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ val genppj : ('a -> Pp.t * Pp.t) -> 'a -> Pp.t
119119
val ppj : EConstr.unsafe_judgment -> unit
120120

121121
val ppsubst : Mod_subst.substitution -> unit
122-
val ppdelta : Mod_subst.delta_resolver -> unit
122+
val ppdelta : 'a Mod_subst.delta_resolver -> unit
123123

124124
val pp_idpred : Names.Id.Pred.t -> unit
125125
val pp_cpred : Names.Cpred.t -> unit

kernel/environ.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,8 @@ module Internal = struct
13461346
let new_mods = ModPath.Map.add mp mb env.env_modules in
13471347
{ env with env_modules = new_mods }
13481348

1349-
let rec overwrite_structure mp sign resolver env =
1349+
let rec overwrite_structure : type a. _ -> _ -> a Mod_subst.delta_resolver -> _ -> _ =
1350+
fun mp sign resolver env ->
13501351
let add_field env (l,elem) = match elem with
13511352
| SFBconst cb ->
13521353
let c = Mod_subst.constant_of_delta_kn resolver (KerName.make mp l) in

kernel/environ.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ module Internal : sig
556556
val overwrite_module_parameter : MBId.t -> module_type_body -> env -> env
557557
(** Overwriting variant of Modops.add_module_parameter, see above. *)
558558

559-
val overwrite_structure : ModPath.t -> structure_body -> Mod_subst.delta_resolver -> env -> env
559+
val overwrite_structure : ModPath.t -> structure_body -> 'a Mod_subst.delta_resolver -> env -> env
560560
(** Overwriting variant of Modops.add_structure, see above. *)
561561

562562
end

kernel/mod_declarations.ml

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ and module_signature = (module_type_body,structure_body) functorize
3131
and module_implementation =
3232
| Abstract (** no accessible implementation *)
3333
| Algebraic of module_expression (** non-interactive algebraic expression *)
34-
| Struct of delta_resolver * structure_body (** interactive body living in the parameter context of [mod_type] *)
34+
| Struct of mod_body delta_resolver * structure_body (** interactive body living in the parameter context of [mod_type] *)
3535
| FullStruct (** special case of [Struct] : the body is exactly [mod_type] *)
3636

3737
and 'a generic_module_body =
3838
{ mod_expr : ('a, module_implementation) when_mod_body; (** implementation *)
3939
mod_type : module_signature; (** expanded type *)
4040
mod_type_alg : module_expression option; (** algebraic type *)
41-
mod_delta : Mod_subst.delta_resolver; (**
41+
mod_delta : 'a Mod_subst.delta_resolver; (**
4242
quotiented set of equivalent constants and inductive names *) }
4343

4444
(** For a module, there are five possible situations:
@@ -104,11 +104,22 @@ let replace_module_body struc delta mb =
104104
mod_type_alg = None;
105105
mod_delta = delta }
106106

107-
let module_type_of_module mb =
108-
{ mb with mod_expr = ModTypeNul; mod_type_alg = None; }
107+
let module_type_of_module mb = {
108+
mod_expr = ModTypeNul;
109+
mod_type = mb.mod_type;
110+
mod_type_alg = None;
111+
mod_delta = of_body_delta_resolver mb.mod_delta;
112+
}
109113

110-
let module_body_of_type mtb =
111-
{ mtb with mod_expr = ModBodyVal Abstract; }
114+
let module_body_of_type mtb = {
115+
mod_expr = ModBodyVal Abstract;
116+
mod_type = mtb.mod_type;
117+
mod_type_alg = mtb.mod_type_alg;
118+
(* The inlining declarations of [mtb] are not inherited: they are a property
119+
of the module type, and a module implementing it does not declare
120+
anything. *)
121+
mod_delta = forget_inline_delta_resolver mtb.mod_delta;
122+
}
112123

113124
(** Setters *)
114125

@@ -118,7 +129,7 @@ let set_implementation e mb =
118129
let set_algebraic_type mb alg =
119130
{ mb with mod_type_alg = Some alg }
120131

121-
let set_delta : type a. delta_resolver -> a generic_module_body -> a generic_module_body =
132+
let set_delta : type a. a delta_resolver -> a generic_module_body -> a generic_module_body =
122133
fun delta mb -> { mb with mod_delta = delta }
123134

124135
(** Accessors *)

kernel/mod_declarations.mli

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type module_signature = (module_type_body,structure_body) functorize
4242
type module_implementation =
4343
| Abstract (** no accessible implementation *)
4444
| Algebraic of module_expression (** non-interactive algebraic expression *)
45-
| Struct of delta_resolver * structure_body (** interactive body living in the parameter context of [mod_type] *)
45+
| Struct of mod_body delta_resolver * structure_body (** interactive body living in the parameter context of [mod_type] *)
4646
| FullStruct (** special case of [Struct] : the body is exactly [mod_type] *)
4747

4848
(** Extra invariants :
@@ -60,23 +60,23 @@ type module_implementation =
6060
val mod_expr : module_body -> module_implementation
6161
val mod_type : 'a generic_module_body -> module_signature
6262
val mod_type_alg : 'a generic_module_body -> module_expression option
63-
val mod_delta : 'a generic_module_body -> delta_resolver
63+
val mod_delta : 'a generic_module_body -> 'a delta_resolver
6464

65-
val mod_global_delta : 'a generic_module_body -> delta_resolver option
65+
val mod_global_delta : 'a generic_module_body -> 'a delta_resolver option
6666
(** [None] if the argument is a functor, [mod_delta] otherwise *)
6767

6868
(** {6 Builders} *)
6969

70-
val make_module_body : module_signature -> Mod_subst.delta_resolver -> module_body
71-
val make_module_type : module_signature -> Mod_subst.delta_resolver -> module_type_body
70+
val make_module_body : module_signature -> Mod_subst.mod_body Mod_subst.delta_resolver -> module_body
71+
val make_module_type : module_signature -> Mod_subst.mod_type Mod_subst.delta_resolver -> module_type_body
7272

7373
val strengthen_module_body : src:ModPath.t ->
74-
module_signature -> delta_resolver -> module_body -> module_body
74+
module_signature -> mod_body delta_resolver -> module_body -> module_body
7575

7676
val strengthen_module_type :
77-
structure_body -> delta_resolver -> module_type_body -> module_type_body
77+
structure_body -> mod_type delta_resolver -> module_type_body -> module_type_body
7878

79-
val replace_module_body : structure_body -> delta_resolver -> module_body -> module_body
79+
val replace_module_body : structure_body -> mod_body delta_resolver -> module_body -> module_body
8080

8181
val module_type_of_module : module_body -> module_type_body
8282
val module_body_of_type : module_type_body -> module_body
@@ -89,7 +89,7 @@ val functorize_module : (Names.MBId.t * module_type_body) list -> module_body ->
8989

9090
val set_implementation : module_implementation -> module_body -> module_body
9191
val set_algebraic_type : module_type_body -> module_expression -> module_type_body
92-
val set_delta : Mod_subst.delta_resolver -> 'a generic_module_body -> 'a generic_module_body
92+
val set_delta : 'a Mod_subst.delta_resolver -> 'a generic_module_body -> 'a generic_module_body
9393

9494
(** {6 Substitution} *)
9595

0 commit comments

Comments
 (0)