Debian Codegraph @

kernel/modops.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* Created by Jacek Chrzaszcz, Aug 2002 as part of the implementation of
12 the Coq module system *)
13 (* Inlining and more liberal use of modules and module types by Claudio
14 Sacerdoti, Nov 2004 *)
15 (* New structure-based model of modules and miscellaneous bug fixes by
16 Élie Soubiran, from Feb 2008 *)
17
18 (* This file provides with various operations on modules and module types *)
19
20 open Util
21 open Names
22 open Constr
23 open Declarations
24 open Mod_declarations
25 open Declareops
26 open Environ
27 open Mod_subst
28
29 (** {6 Errors } *)
30
31 type signature_mismatch_error =
32 | InductiveFieldExpected of mutual_inductive_body
33 | DefinitionFieldExpected
34 | ModuleFieldExpected
35 | ModuleTypeFieldExpected
36 | NotConvertibleInductiveField of Id.t * (env * types * types) option
37 | NotConvertibleConstructorField of Id.t * (env * types * types) option
38 | NotConvertibleBodyField of (env * constr * constr) option
39 | NotConvertibleTypeField of env * types * types
40 | CumulativeStatusExpected of bool
41 | PolymorphicStatusExpected of bool
42 | NotSameConstructorNamesField of Id.t array * Id.t array
43 | NotSameInductiveNameInBlockField of Id.t * Id.t
44 | FiniteInductiveFieldExpected of bool
45 | InductiveNumbersFieldExpected of { got : int; expected : int }
46 | InductiveParamsNumberField of { got : int; expected : int }
47 | RecordFieldExpected of bool
48 | RecordProjectionsExpected of { expected : Name.t list; got : Name.t list }
49 | NotEqualInductiveAliases of MutInd.t * MutInd.t
50 | IncompatibleUniverses of { err : UGraph.univ_inconsistency; env : env; t1 : types; t2 : types }
51 | IncompatibleQualities of { err : QGraph.elimination_error; env : env; t1 : types; t2 : types }
52 | IncompatiblePolymorphism of env * types * types
53 | IncompatibleUnivConstraints of { got : UVars.AbstractContext.t; expect : UVars.AbstractContext.t }
54 | IncompatibleVariance
55 | NoRewriteRulesSubtyping
56
57 type with_constraint_error =
58 | WithSignatureMismatch of signature_mismatch_error
59 | WithCannotConstrainPrimitive
60 | WithCannotConstrainSymbol
61
62 type subtyping_trace_elt =
63 | Submodule of Id.t
64 | FunctorArgument of int
65
66 type module_typing_error =
67 | SignatureMismatch of subtyping_trace_elt list * Id.t * signature_mismatch_error
68 | LabelAlreadyDeclared of Id.t
69 | NotAFunctor
70 | IsAFunctor of ModPath.t
71 | IncompatibleModuleTypes of module_type_body * module_type_body
72 | NotEqualModulePaths of ModPath.t * ModPath.t
73 | NoSuchLabel of Id.t * ModPath.t
74 | NotAModuleLabel of Id.t
75 | NotAConstant of Id.t
76 | IncorrectWithConstraint of Id.t * with_constraint_error
77 | GenerativeModuleExpected of Id.t
78 | LabelMissing of Id.t * string
79 | IncludeRestrictedFunctor of ModPath.t
80
81 exception ModuleTypingError of module_typing_error
82
83 let error_existing_label l =
84 raise (ModuleTypingError (LabelAlreadyDeclared l))
85
86 let error_not_a_functor () =
87 raise (ModuleTypingError NotAFunctor)
88
89 let error_is_a_functor mp =
90 raise (ModuleTypingError (IsAFunctor mp))
91
92 let error_incompatible_modtypes mexpr1 mexpr2 =
93 raise (ModuleTypingError (IncompatibleModuleTypes (mexpr1,mexpr2)))
94
95 let error_not_equal_modpaths mp1 mp2 =
96 raise (ModuleTypingError (NotEqualModulePaths (mp1,mp2)))
97
98 let error_signature_mismatch trace l why =
99 raise (ModuleTypingError (SignatureMismatch (trace,l,why)))
100
101 let error_no_such_label l mp =
102 raise (ModuleTypingError (NoSuchLabel (l,mp)))
103
104 let error_not_a_module_label s =
105 raise (ModuleTypingError (NotAModuleLabel s))
106
107 let error_not_a_constant l =
108 raise (ModuleTypingError (NotAConstant l))
109
110 let error_incorrect_with_constraint l err =
111 raise (ModuleTypingError (IncorrectWithConstraint (l, err)))
112
113 let error_generative_module_expected l =
114 raise (ModuleTypingError (GenerativeModuleExpected l))
115
116 let error_no_such_label_sub l l1 =
117 raise (ModuleTypingError (LabelMissing (l,l1)))
118
119 let error_include_restricted_functor mp =
120 raise (ModuleTypingError (IncludeRestrictedFunctor mp))
121
122 (** {6 Operations on functors } *)
123
124 let is_functor = function
125 | NoFunctor _ -> false
126 | MoreFunctor _ -> true
127
128 let destr_functor = function
129 | NoFunctor _ -> error_not_a_functor ()
130 | MoreFunctor (mbid,ty,x) -> (mbid,ty,x)
131
132 let destr_nofunctor mp = function
133 | NoFunctor a -> a
134 | MoreFunctor _ -> error_is_a_functor mp
135
136 let get_global_delta mb = match mod_global_delta mb with
137 | None -> assert false
138 | Some delta -> delta
139
140 (** {6 Misc operations } *)
141
142 let check_modpath_equiv env mp1 mp2 =
143 if ModPath.equal mp1 mp2 then ()
144 else
145 let mp1' = mp_of_delta (mod_delta @@ lookup_module mp1 env) mp1 in
146 let mp2' = mp_of_delta (mod_delta @@ lookup_module mp2 env) mp2 in
147 if ModPath.equal mp1' mp2' then ()
148 else error_not_equal_modpaths mp1 mp2
149
150 let rec annotate_module_expression me mty = match me, mty with
151 | MENoFunctor me, (NoFunctor _ | MoreFunctor _) -> NoFunctor me
152 | MEMoreFunctor me, MoreFunctor (mbid, arg, mty) ->
153 let me = annotate_module_expression me mty in
154 MoreFunctor (mbid, arg, me)
155 | MEMoreFunctor _, NoFunctor _ -> assert false
156
157 let rec annotate_struct_body body sign = match sign with
158 | NoFunctor _ -> NoFunctor body
159 | MoreFunctor (mbid, mty, sign) ->
160 MoreFunctor (mbid, mty, annotate_struct_body body sign)
161
162 (** {6 Substitutions of modular structures } *)
163
164 let subst_signature subst = subst_signature subst_codom subst
165 let subst_structure subst = subst_structure subst_codom subst
166
167 (** {6 Adding a module in the environment } *)
168
169 let add_retroknowledge l env =
170 List.fold_left Primred.add_retroknowledge env l
171
172 let rec add_structure mp sign resolver linkinfo env =
173 let add_field env (l,elem) = match elem with
174 | SFBconst cb ->
175 let c = constant_of_delta_kn resolver (KerName.make mp l) in
176 Environ.add_constant_key c cb linkinfo env
177 | SFBmind mib ->
178 let mind = mind_of_delta_kn resolver (KerName.make mp l) in
179 let mib =
180 if mib.mind_private != None then
181 { mib with mind_private = Some true }
182 else mib
183 in
184 Environ.add_mind_key mind mib linkinfo env
185 | SFBmodule mb -> add_module (MPdot (mp, l)) mb linkinfo env (* adds components as well *)
186 | SFBmodtype mtb -> Environ.add_modtype (MPdot (mp, l)) mtb env
187 | SFBrules r -> Environ.add_rewrite_rules r.rewrules_rules env
188 in
189 List.fold_left add_field env sign
190
191 and add_module mp mb linkinfo env =
192 let env = Environ.shallow_add_module mp mb env in
193 match mod_type mb with
194 | NoFunctor struc ->
195 let delta = get_global_delta mb in
196 add_retroknowledge (mod_retroknowledge mb)
197 (add_structure mp struc delta linkinfo env)
198 | MoreFunctor _ -> env
199
200 let add_linked_module mp mb linkinfo env =
201 add_module mp mb linkinfo env
202
203 let add_structure mp sign resolver env =
204 add_structure mp sign resolver no_link_info env
205
206 let add_module mp mb env =
207 add_module mp mb no_link_info env
208
209 let add_module_parameter mbid mtb env =
210 add_module (MPbound mbid) (module_body_of_type mtb) env
211
212 (** {6 Strengthening a signature for subtyping } *)
213
214 let strengthen_const mp_from l cb resolver =
215 match cb.const_body with
216 | Def _ -> cb
217 | _ ->
218 let kn = KerName.make mp_from l in
219 let con = constant_of_delta_kn resolver kn in
220 let u = UVars.make_abstract_instance (Declareops.constant_polymorphic_context cb) in
221 { cb with
222 const_body = Def (mkConstU (con,u));
223 const_body_code = Some (Vmbytegen.compile_alias con) }
224
225 let rec strengthen_module mp mb = match mod_type mb with
226 | NoFunctor struc ->
227 let delta_mb = get_global_delta mb in
228 if mp_in_delta mp delta_mb then mb
229 else
230 let reso, struc' = strengthen_signature mp struc delta_mb in
231 let reso = add_mp_delta_resolver mp mp (add_delta_resolver delta_mb reso) in
232 strengthen_module_body ~src:mp (NoFunctor struc') reso mb
233 | MoreFunctor _ -> mb
234
235 and strengthen_signature mp struc reso0 =
236 let strengthen_field reso item = match item with
237 | (l, SFBconst cb) ->
238 reso, (l, SFBconst (strengthen_const mp l cb reso0))
239 | (l, SFBmodule mb) ->
240 let mp' = MPdot (mp, l) in
241 let mb' = strengthen_module mp' mb in
242 let reso = match mod_global_delta mb with
243 | None ->
244 (* See {!strengthen_and_subst_module} *)
245 add_mp_delta_resolver mp' mp' reso
246 | Some delta ->
247 add_delta_resolver delta reso
248 in
249 reso, (l, SFBmodule mb')
250 | (_, (SFBmind _ | SFBrules _ | SFBmodtype _)) ->
251 reso, item
252 in
253 List.fold_left_map strengthen_field (empty_delta_resolver mp) struc
254
255 let strengthen mtb mp = match mod_type mtb with
256 | NoFunctor struc ->
257 let delta_mtb = get_global_delta mtb in
258 (* Has mtb already been strengthened ? *)
259 if mp_in_delta mp delta_mtb then mtb
260 else
261 let reso', struc' = strengthen_signature mp struc delta_mtb in
262 let reso' = add_delta_resolver delta_mtb (add_mp_delta_resolver mp mp reso') in
263 strengthen_module_type struc' reso' mtb
264 | MoreFunctor _ -> mtb
265
266 (** {6 Strengthening a module for [Module M := M'] or [Include M] } *)
267
268 let rec strengthen_and_subst_module mb subst mp_from mp_to =
269 match mod_type mb with
270 | NoFunctor struc ->
271 let delta_mb = get_global_delta mb in
272 let mb_is_an_alias = mp_in_delta mp_from delta_mb in
273 if mb_is_an_alias then
274 subst_module subst_dom_codom subst mp_from mb
275 else
276 let reso',struc' =
277 strengthen_and_subst_struct struc subst
278 mp_from mp_to false false delta_mb
279 in
280 (* Don't forget to add the original resolver up to substitution *)
281 let reso' = add_delta_resolver (subst_dom_delta_resolver subst delta_mb) (add_mp_delta_resolver mp_to mp_from reso') in
282 strengthen_module_body ~src:mp_from (NoFunctor struc') reso' mb
283 | MoreFunctor _ ->
284 let subst = add_mp mp_from mp_to (empty_delta_resolver mp_to) subst in
285 subst_module subst_dom_codom subst mp_from mb
286
287 and strengthen_and_subst_struct struc subst mp_from mp_to alias incl reso =
288 let strengthen_and_subst_field reso' item = match item with
289 | (l,SFBconst cb) ->
290 let cb' = subst_const_body subst cb in
291 let cb' =
292 if alias then (* optimization *) cb'
293 else strengthen_const mp_from l cb' reso
294 in
295 let item' = if cb' == cb then item else (l, SFBconst cb') in
296 if incl then
297 (* If we are performing an inclusion we need to add
298 the fact that the constant mp_to.l is \Delta-equivalent
299 to reso(mp_from.l) *)
300 let kn_from = KerName.make mp_from l in
301 let kn_to = KerName.make mp_to l in
302 let kn_canonical = kn_of_delta reso kn_from in
303 add_kn_delta_resolver kn_to kn_canonical reso', item'
304 else
305 (* In this case the fact that the constant mp_to.l is
306 \Delta-equivalent to resolver(mp_from.l) is already known
307 because reso' contains mp_to maps to reso(mp_from) *)
308 reso', item'
309 | (l,SFBmind mib) ->
310 let mib' = subst_mind_body subst mib in
311 let item' = if mib' == mib then item else (l, SFBmind mib') in
312 (* Same as constant *)
313 if incl then
314 let kn_from = KerName.make mp_from l in
315 let kn_to = KerName.make mp_to l in
316 let kn_canonical = kn_of_delta reso kn_from in
317 add_kn_delta_resolver kn_to kn_canonical reso', item'
318 else
319 reso', item'
320 | (l, SFBrules rrb) ->
321 let rrb' = subst_rewrite_rules subst rrb in
322 let item' = if rrb' == rrb then item else (l, SFBrules rrb') in
323 (* Same as constant *)
324 if incl then
325 let kn_from = KerName.make mp_from l in
326 let kn_to = KerName.make mp_to l in
327 let kn_canonical = kn_of_delta reso kn_from in
328 add_kn_delta_resolver kn_to kn_canonical reso', item'
329 else
330 reso', item'
331 | (l,SFBmodule mb) ->
332 let mp_from' = MPdot (mp_from,l) in
333 let mp_to' = MPdot (mp_to,l) in
334 let mb' = if alias then
335 subst_module subst_dom_codom subst mp_from' mb
336 else
337 strengthen_and_subst_module mb subst mp_from' mp_to'
338 in
339 let item' = if mb' == mb then item else (l, SFBmodule mb') in
340 (* if mb is a functor we should not derive new equivalences
341 on names, hence we add the fact that the functor can only
342 be equivalent to itself. If we adopt an applicative
343 semantic for functor this should be changed.*)
344 begin match mod_global_delta mb' with
345 | None -> (* functor case *)
346 add_mp_delta_resolver mp_to' mp_to' reso', item'
347 | Some delta ->
348 add_delta_resolver delta reso', item'
349 end
350 | (l,SFBmodtype mty) ->
351 let mp_from' = MPdot (mp_from,l) in
352 let mp_to' = MPdot(mp_to,l) in
353 let subst' = add_mp mp_from' mp_to' (empty_delta_resolver mp_to') subst in
354 let mty' = subst_modtype (subst_shallow_dom_codom subst') subst' mp_from' mty in
355 let item' = if mty' == mty then item else (l, SFBmodtype mty') in
356 add_mp_delta_resolver mp_to' mp_to' reso', item'
357 in
358 List.Smart.fold_left_map strengthen_and_subst_field (empty_delta_resolver mp_to) struc
359
360 (** Let P be a module path when we write:
361 "Module M:=P." or "Module M. Include P. End M."
362 We need to perform two operations to compute the body of M.
363 - The first one is applying the substitution {P <- M} on the type of P, i.e.
364 to replace any expression in P referring to P itself by the same
365 expression referring instead to M
366 - The second one is strengthening, i.e. associating to each
367 abstract/opaque field t in P a defined field t := Q.t where Q is the
368 Delta-normal form of P (possibly P itself):
369 - in the alias case "Module M:=P." where "P" is already an alias
370 with canonical form "Q": add the module Delta-equivalence "M := Q"
371 - in the alias case where P is not itself an alias:
372 add the module Delta-equivalence "M := P"
373 - in the "Include" case: add a Delta-equivalence "t := t'" where
374 "t'" is the canonical form of "P.t" on each field *)
375
376 let strengthen_and_subst_module_body mp_from mb mp include_b = match mod_type mb with
377 | NoFunctor struc ->
378 let delta_mb = get_global_delta mb in
379 let mb_is_an_alias = mp_in_delta mp_from delta_mb in
380 (* if mb.mod_mp is an alias then the strengthening is useless
381 (i.e. it is already done)*)
382 let mp_alias = mp_of_delta delta_mb mp_from in
383 let subst_resolver = map_mp mp_from mp (empty_delta_resolver mp) in
384 let new_resolver =
385 add_mp_delta_resolver mp mp_alias
386 (subst_dom_delta_resolver subst_resolver delta_mb)
387 in
388 let subst = map_mp mp_from mp new_resolver in
389 let reso',struc' =
390 strengthen_and_subst_struct struc subst
391 mp_from mp mb_is_an_alias include_b delta_mb
392 in
393 let reso' = if include_b then reso' else add_delta_resolver new_resolver reso' in
394 strengthen_module_body ~src:mp_from (NoFunctor struc') reso' mb
395 | MoreFunctor _ ->
396 let subst = map_mp mp_from mp (empty_delta_resolver mp) in
397 Mod_declarations.subst_module subst_dom_codom subst mp_from mb
398
399 (* [mp_from] is the ambient modpath of [sign] *)
400 let subst_modtype_signature_and_resolver mp_from mp_to sign reso =
401 let subst = map_mp mp_from mp_to (empty_delta_resolver mp_to) in
402 Mod_declarations.subst_signature subst_dom_codom subst mp_from sign, subst_dom_codom_delta_resolver subst reso
403
404 let rec collect_mbid l sign = match sign with
405 | MoreFunctor (mbid,ty,m) ->
406 let m' = collect_mbid (MBId.Set.add mbid l) m in
407 if m==m' then sign else MoreFunctor (mbid,ty,m')
408 | NoFunctor struc ->
409 let struc' = clean_structure l struc in
410 if struc==struc' then sign else NoFunctor struc'
411
412 let clean_bounded_mod_expr sign =
413 if is_functor sign then collect_mbid MBId.Set.empty sign else sign
414
415 (** {6 Building map of constants to inline } *)
416
417 let inline_delta_resolver env inl mp mbid mtb delta =
418 let constants = inline_of_delta inl (mod_delta mtb) in
419 let rec make_inline delta = function
420 | [] -> delta
421 | (lev,kn)::r ->
422 let kn = replace_mp_in_kn (MPbound mbid) mp kn in
423 let con = constant_of_delta_kn delta kn in
424 if not (Environ.mem_constant con env) then
425 error_no_such_label_sub (Constant.label con)
426 (ModPath.to_string (Constant.modpath con))
427 else
428 let constant = lookup_constant con env in
429 let l = make_inline delta r in
430 match constant.const_body with
431 | Undef _ | OpaqueDef _ | Primitive _ | Symbol _ -> l
432 | Def constr ->
433 let ctx = Declareops.constant_polymorphic_context constant in
434 let constr = {UVars.univ_abstracted_value=constr; univ_abstracted_binder=ctx} in
435 add_inline_delta_resolver kn (lev, Some constr) l
436 in
437 make_inline delta constants