Debian Codegraph @

kernel/declareops.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 open Declarations
12 open Mod_subst
13 open Util
14
15 module RelDecl = Context.Rel.Declaration
16
17 let noh hcons x = snd (hcons x)
18
19 (** Operations concernings types in [Declarations] :
20 [constant_body], [mutual_inductive_body], [module_body] ... *)
21
22 let safe_flags oracle = {
23 check_guarded = true;
24 check_positive = true;
25 check_universes = true;
26 conv_oracle = oracle;
27 share_reduction = true;
28 enable_VM = true;
29 enable_native_compiler = true;
30 indices_matter = true;
31 impredicative_set = false;
32 sprop_allowed = true;
33 allow_uip = false;
34 }
35
36 (** {6 Arities } *)
37
38 let hcons_template_universe ar =
39 { template_param_arguments = List.Smart.map (Option.Smart.map (noh Sorts.hcons)) ar.template_param_arguments;
40 template_concl = noh Sorts.hcons ar.template_concl;
41 template_context = noh UVars.AbstractContext.hcons ar.template_context;
42 template_defaults = noh UVars.Instance.hcons ar.template_defaults;
43 }
44
45 let universes_context = function
46 | Monomorphic -> UVars.AbstractContext.empty
47 | Polymorphic ctx -> ctx
48
49 let abstract_universes = function
50 | Entries.Monomorphic_entry ->
51 UVars.empty_sort_subst, Monomorphic
52 | Entries.Polymorphic_entry uctx ->
53 let (inst, auctx) = UVars.abstract_universes uctx in
54 let inst = UVars.make_instance_subst inst in
55 (inst, Polymorphic auctx)
56
57 (** {6 Constants } *)
58
59 let constant_is_polymorphic cb =
60 match cb.const_universes with
61 | Monomorphic -> false
62 | Polymorphic _ -> true
63
64
65 let constant_has_body cb = match cb.const_body with
66 | Undef _ | Primitive _ | Symbol _ -> false
67 | Def _ | OpaqueDef _ -> true
68
69 let constant_polymorphic_context cb =
70 universes_context cb.const_universes
71
72 let is_opaque cb = match cb.const_body with
73 | OpaqueDef _ -> true
74 | Undef _ | Def _ | Primitive _ | Symbol _ -> false
75
76 (** {7 Constant substitutions } *)
77
78 let subst_rel_declaration subst =
79 RelDecl.map_constr (subst_mps subst)
80
81 let subst_rel_context subst = List.Smart.map (subst_rel_declaration subst)
82
83 let subst_const_type subst arity =
84 if is_empty_subst subst then arity
85 else subst_mps subst arity
86
87 (** No need here to check for physical equality after substitution,
88 at least for Def due to the delayed substitution [subst_constr_subst]. *)
89 let subst_const_def subst def = match def with
90 | Undef _ | Primitive _ | Symbol _ -> def
91 | Def c -> Def (subst_mps subst c)
92 | OpaqueDef o -> OpaqueDef (Opaqueproof.subst_opaque subst o)
93
94 let subst_const_body subst cb =
95 (* we're outside sections *)
96 assert (List.is_empty cb.const_hyps && UVars.Instance.is_empty cb.const_univ_hyps);
97 if is_empty_subst subst then cb
98 else
99 let body' = subst_const_def subst cb.const_body in
100 let type' = subst_const_type subst cb.const_type in
101 if body' == cb.const_body && type' == cb.const_type
102 then cb
103 else
104 { const_hyps = [];
105 const_univ_hyps = UVars.Instance.empty;
106 const_body = body';
107 const_type = type';
108 const_body_code =
109 Option.map (Vmemitcodes.subst_body_code subst) cb.const_body_code;
110 const_universes = cb.const_universes;
111 const_relevance = cb.const_relevance;
112 const_inline_code = cb.const_inline_code;
113 const_typing_flags = cb.const_typing_flags }
114
115 (** {7 Hash-consing of constants } *)
116
117 (** This hash-consing is currently quite partial : we only
118 share internal fields (e.g. constr), and not the records
119 themselves. But would it really bring substantial gains ? *)
120
121 let hcons_rel_decl =
122 RelDecl.map_name (noh Names.Name.hcons) %> RelDecl.map_value (noh Constr.hcons) %> RelDecl.map_type (noh Constr.hcons)
123
124 let hcons_rel_context l = List.Smart.map hcons_rel_decl l
125
126 let hcons_const_def ?(hbody=noh Constr.hcons) = function
127 | Undef inl -> Undef inl
128 | Primitive p -> Primitive p
129 | Symbol r -> Symbol r
130 | Def l_constr ->
131 Def (hbody l_constr)
132 | OpaqueDef _ as x -> x (* hashconsed when turned indirect *)
133
134 let hcons_universes cbu =
135 match cbu with
136 | Monomorphic -> Monomorphic
137 | Polymorphic ctx ->
138 Polymorphic (noh UVars.AbstractContext.hcons ctx)
139
140 let hcons_const_body ?hbody cb =
141 { cb with
142 const_body = hcons_const_def ?hbody cb.const_body;
143 const_type = noh Constr.hcons cb.const_type;
144 const_universes = hcons_universes cb.const_universes;
145 }
146
147 (** {6 Inductive types } *)
148
149 let eq_recarg_type t1 t2 = match t1, t2 with
150 | RecArgInd ind1, RecArgInd ind2 -> Names.Ind.CanOrd.equal ind1 ind2
151 | RecArgPrim c1, RecArgPrim c2 -> Names.Constant.CanOrd.equal c1 c2
152 | (RecArgInd _ | RecArgPrim _), _ -> false
153
154 let eq_recarg r1 r2 = match r1, r2 with
155 | Norec, Norec -> true
156 | Norec, _ -> false
157 | Mrec t1, Mrec t2 -> eq_recarg_type t1 t2
158 | Mrec _, _ -> false
159
160 let pr_recarg_type = let open Pp in function
161 | RecArgInd (mind,i) ->
162 str "Mrec[" ++ Names.MutInd.print mind ++ pr_comma () ++ int i ++ str "]"
163 | RecArgPrim c ->
164 str "Prim[" ++ Names.Constant.print c ++ str "]"
165
166 let pr_recarg = let open Pp in function
167 | Declarations.Norec -> str "Norec"
168 | Declarations.Mrec t -> pr_recarg_type t
169
170 let pr_wf_paths x = Rtree.pr_tree pr_recarg x
171
172 let subst_recarg_type subst ty = match ty with
173 | RecArgInd (kn,i) ->
174 let kn' = subst_mind subst kn in
175 if kn==kn' then ty else RecArgInd (kn',i)
176 | RecArgPrim c ->
177 let c',_ = subst_con subst c in
178 if c==c' then ty else RecArgPrim c'
179
180 let subst_recarg subst r = match r with
181 | Norec -> r
182 | Mrec ty ->
183 let ty' = subst_recarg_type subst ty in
184 if ty==ty' then r else Mrec ty'
185
186 let mk_norec = Rtree.mk_node Norec [||]
187
188 let mk_paths r recargs =
189 Rtree.mk_node r
190 (Array.map Array.of_list recargs)
191
192 let dest_recarg p = Rtree.dest_head p
193
194 (* dest_subterms returns the sizes of each argument of each constructor of
195 an inductive object of size [p]. This should never be done for Norec,
196 because the number of sons does not correspond to the number of
197 constructors.
198 *)
199 let dest_subterms p =
200 let (ra,cstrs) = Rtree.dest_node p in
201 assert (match ra with Norec -> false | _ -> true);
202 Array.map Array.to_list cstrs
203
204 let recarg_length p j =
205 let (_,cstrs) = Rtree.dest_node p in
206 Array.length cstrs.(j-1)
207
208 let subst_wf_paths subst p = Rtree.Smart.map (subst_recarg subst) p
209
210 (** {7 Substitution of inductive declarations } *)
211
212 let subst_mind_record subst r = match r with
213 | NotRecord -> NotRecord
214 | FakeRecord -> FakeRecord
215 | PrimRecord ({ tys; _ } as infos) ->
216 let tys' = Array.Smart.map (subst_mps subst) tys in
217 if tys' == tys then r
218 else PrimRecord { infos with tys = tys' }
219
220 let subst_mind_packet subst mbp =
221 { mind_consnames = mbp.mind_consnames;
222 mind_consnrealdecls = mbp.mind_consnrealdecls;
223 mind_consnrealargs = mbp.mind_consnrealargs;
224 mind_typename = mbp.mind_typename;
225 mind_nf_lc = Array.Smart.map (fun (ctx, c) -> Context.Rel.map (subst_mps subst) ctx, subst_mps subst c) mbp.mind_nf_lc;
226 mind_arity_ctxt = subst_rel_context subst mbp.mind_arity_ctxt;
227 mind_user_arity = subst_mps subst mbp.mind_user_arity;
228 mind_sort = mbp.mind_sort;
229 mind_record = subst_mind_record subst mbp.mind_record;
230 mind_user_lc = Array.Smart.map (subst_mps subst) mbp.mind_user_lc;
231 mind_nrealargs = mbp.mind_nrealargs;
232 mind_nrealdecls = mbp.mind_nrealdecls;
233 mind_squashed = mbp.mind_squashed;
234 mind_recargs = subst_wf_paths subst mbp.mind_recargs (*wf_paths*);
235 mind_relevance = mbp.mind_relevance;
236 mind_nb_constant = mbp.mind_nb_constant;
237 mind_nb_args = mbp.mind_nb_args;
238 mind_reloc_tbl = mbp.mind_reloc_tbl }
239
240 let subst_mind_body subst mib =
241 (* we're outside sections *)
242 assert (List.is_empty mib.mind_hyps && UVars.Instance.is_empty mib.mind_univ_hyps);
243 { mind_finite = mib.mind_finite ;
244 mind_hyps = [];
245 mind_univ_hyps = UVars.Instance.empty;
246 mind_nparams = mib.mind_nparams;
247 mind_nparams_rec = mib.mind_nparams_rec;
248 mind_params_ctxt =
249 Context.Rel.map (subst_mps subst) mib.mind_params_ctxt;
250 mind_packets = Array.Smart.map (subst_mind_packet subst) mib.mind_packets ;
251 mind_universes = mib.mind_universes;
252 mind_template = mib.mind_template;
253 mind_variance = mib.mind_variance;
254 mind_sec_variance = mib.mind_sec_variance;
255 mind_private = mib.mind_private;
256 mind_typing_flags = mib.mind_typing_flags;
257 }
258
259 let mind_ntypes mib = Array.length mib.mind_packets
260
261 let inductive_polymorphic_context mib =
262 universes_context mib.mind_universes
263
264 let inductive_is_polymorphic mib =
265 match mib.mind_universes with
266 | Monomorphic -> false
267 | Polymorphic _ctx -> true
268
269 let inductive_is_cumulative mib =
270 Option.has_some mib.mind_variance
271
272 let inductive_make_projection ind mib ~proj_arg =
273 match mib.mind_packets.(snd ind).mind_record with
274 | NotRecord | FakeRecord ->
275 CErrors.anomaly Pp.(str "inductive_make_projection: not a primitive record.")
276 | PrimRecord { projections; relevances; _ } ->
277 if proj_arg < 0 || Array.length projections <= proj_arg
278 then CErrors.anomaly Pp.(str "inductive_make_projection: invalid proj_arg.");
279 let p = Names.Projection.Repr.make ind
280 ~proj_npars:mib.mind_nparams
281 ~proj_arg
282 projections.(proj_arg)
283 in
284 p, relevances.(proj_arg)
285
286 let inductive_make_projections ind mib =
287 match mib.mind_packets.(snd ind).mind_record with
288 | NotRecord | FakeRecord -> None
289 | PrimRecord { projections; relevances; has_eta; _ } ->
290 let projs = Array.map2_i (fun proj_arg lab r ->
291 Names.Projection.Repr.make ind ~proj_npars:mib.mind_nparams ~proj_arg lab, r)
292 projections relevances
293 in
294 Some (projs, has_eta)
295
296 (** {6 Hash-consing of inductive declarations } *)
297
298 (** Just as for constants, this hash-consing is quite partial *)
299
300 let hcons_mind_packet oib =
301 let user = Array.Smart.map (noh Constr.hcons) oib.mind_user_lc in
302 let map (ctx, c) = Context.Rel.map (noh Constr.hcons) ctx, noh Constr.hcons c in
303 let nf = Array.Smart.map map oib.mind_nf_lc in
304 { oib with
305 mind_typename = noh Names.Id.hcons oib.mind_typename;
306 mind_arity_ctxt = hcons_rel_context oib.mind_arity_ctxt;
307 mind_user_arity = noh Constr.hcons oib.mind_user_arity;
308 mind_sort = noh Sorts.hcons oib.mind_sort;
309 mind_consnames = Array.Smart.map (noh Names.Id.hcons) oib.mind_consnames;
310 mind_user_lc = user;
311 mind_nf_lc = nf }
312
313 let hcons_mind mib =
314 { mib with
315 mind_packets = Array.Smart.map hcons_mind_packet mib.mind_packets;
316 mind_params_ctxt = hcons_rel_context mib.mind_params_ctxt;
317 mind_template = Option.Smart.map hcons_template_universe mib.mind_template;
318 mind_universes = hcons_universes mib.mind_universes }
319
320 let subst_rewrite_rules subst ({ rewrules_rules } as rules) =
321 let body' = List.Smart.map (fun (name, ({ rhs; _ } as rule) as orig) ->
322 let rhs' = subst_mps subst rhs in
323 if rhs == rhs' then orig else name, { rule with rhs = rhs' })
324 rewrules_rules
325 in
326 if rewrules_rules == body' then rules else
327 { rewrules_rules = body' }