Debian Codegraph @

tactics/eqschemes.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 (* File created by Hugo Herbelin, Nov 2009 *)
12
13 (* This file builds schemes related to equality inductive types,
14 especially for dependent rewrite, rewriting on arbitrary equality
15 types and congruence on arbitrary equality types *)
16
17 (* However, the choices made lack uniformity, as we have to make a
18 compromise between several constraints and ideal requirements:
19
20 - Having the extended schemes working conservatively over the
21 existing non-dependent schemes eq_rect and eq_rect_r. There is in
22 particular a problem with the dependent rewriting schemes in
23 hypotheses for which the inductive types cannot be in last
24 position of the scheme as it is the general rule in Rocq. This has
25 an effect on the order of generated goals (side-conditions of the
26 lemma after or before the main goal). The non-dependent case can be
27 fixed but to the price of a lost of uniformity wrt side-conditions
28 in the dependent and non-dependent cases.
29
30 - Having schemes general enough to support non-symmetric equality
31 type like eq_true.
32
33 - Having schemes that avoid introducing beta-expansions blocked by
34 "match" so as to please the guard condition, but this introduces
35 some tricky things involving involutivity of symmetry that I
36 don't how to avoid. The result below is a compromise with
37 dependent left-to-right rewriting in conclusion (l2r_dep) using
38 the tricky involutivity of symmetry and dependent left-to-right
39 rewriting in hypotheses (r2l_forward_dep), that one wants to be
40 used for non-symmetric equality and that introduces blocked
41 beta-expansions.
42
43 One may wonder whether these extensions are worth to be done
44 regarding the price we have to pay and regarding the rare
45 situations where they are needed. However, I believe it meets a
46 natural expectation of the user.
47 *)
48
49 open CErrors
50 open Util
51 open Names
52 open Term
53 open Constr
54 open Context
55 open Declarations
56 open Environ
57 open Inductive
58 open Termops
59 open Vars
60 open Namegen
61 open Inductiveops
62 open Ind_tables
63 open Indrec
64 open Context.Rel.Declaration
65
66 module RelDecl = Context.Rel.Declaration
67
68 let hid = Id.of_string "H"
69 let xid = Id.of_string "X"
70
71 let default_id_of_sort = let open Sorts.Quality in function
72 | QConstant QSProp | QConstant QProp -> hid
73 | QConstant QType | QVar _ -> xid
74
75 let default_id_of_ind ind mip =
76 default_id_of_sort (Elimschemes.pseudo_sort_quality_for_elim ind mip)
77
78 let fresh env id avoid =
79 let freshid = next_global_ident_away (Global.safe_env ()) id avoid in
80 freshid, Id.Set.add freshid avoid
81 let with_context_set ctx (b, ctx') =
82 (b, UnivGen.sort_context_union ctx ctx')
83
84 let of_context_set env ctx =
85 UState.merge_sort_context ~sideff:false ~src:UState.Internal UnivRigid (UState.from_env env) ctx
86
87 let build_dependent_inductive ind (mib,mip) =
88 let realargs,_ = List.chop mip.mind_nrealdecls mip.mind_arity_ctxt in
89 applist
90 (mkIndU ind,
91 Context.Rel.instance_list mkRel mip.mind_nrealdecls mib.mind_params_ctxt
92 @ Context.Rel.instance_list mkRel 0 realargs)
93
94 let named_hd env t na = named_hd env (Evd.from_env env) (EConstr.of_constr t) na
95 let name_assumption env = function
96 | LocalAssum (na,t) -> LocalAssum (map_annot (named_hd env t) na, t)
97 | LocalDef (na,c,t) -> LocalDef (map_annot (named_hd env c) na, c, t)
98
99 let name_context env hyps =
100 snd
101 (List.fold_left
102 (fun (env,hyps) d ->
103 let d' = name_assumption env d in (push_rel d' env, d' :: hyps))
104 (env,[]) (List.rev hyps))
105
106 let my_it_mkLambda_or_LetIn s c = Term.it_mkLambda_or_LetIn c s
107 let my_it_mkProd_or_LetIn s c = Term.it_mkProd_or_LetIn c s
108 let my_it_mkLambda_or_LetIn_name env s c =
109 let mkLambda_or_LetIn_name d b = mkLambda_or_LetIn (name_assumption env d) b in
110 List.fold_left (fun c d -> mkLambda_or_LetIn_name d c) c s
111
112 let get_rocq_eq env ctx =
113 let eq = Globnames.destIndRef (Rocqlib.lib_ref "core.eq.type") in
114 let eq, ctx = with_context_set ctx
115 (UnivGen.fresh_inductive_instance env eq) in
116 mkIndU eq, mkConstructUi (eq,1), ctx
117
118 let univ_of_eq env eq =
119 let open EConstr in
120 let eq = of_constr eq in
121 let sigma = Evd.from_env env in
122 match kind sigma (Retyping.get_type_of env sigma eq) with
123 | Prod (_,t,_) -> (match kind sigma t with
124 Sort k ->
125 (match ESorts.kind sigma k with Type u -> u | _ -> assert false)
126 | _ -> assert false)
127 | _ -> assert false
128
129 (**********************************************************************)
130 (* Check if an inductive type [ind] has the form *)
131 (* *)
132 (* I q1..qm,p1..pn a1..an with one constructor *)
133 (* C : I q1..qm,p1..pn p1..pn *)
134 (* *)
135 (* in which case, a symmetry lemma is definable *)
136 (**********************************************************************)
137
138 let error msg = user_err Pp.(str msg)
139
140 let get_sym_eq_data env (ind,u) =
141 let (mib,mip as specif) = lookup_mind_specif env ind in
142 if not (Int.equal (Array.length mib.mind_packets) 1) ||
143 not (Int.equal (Array.length mip.mind_nf_lc) 1) then
144 error "Not an inductive type with a single constructor.";
145 let arityctxt = Vars.subst_instance_context u mip.mind_arity_ctxt in
146 let realsign,_ = List.chop mip.mind_nrealdecls arityctxt in
147 if List.exists is_local_def realsign then
148 error "Inductive equalities with local definitions in arity not supported.";
149 let constrsign,ccl = mip.mind_nf_lc.(0) in
150 let _,constrargs = decompose_app_list ccl in
151 if not (Int.equal (Context.Rel.length constrsign) (Context.Rel.length mib.mind_params_ctxt)) then
152 error "Constructor must have no arguments"; (* This can be relaxed... *)
153 let params,constrargs = List.chop mib.mind_nparams constrargs in
154 if mip.mind_nrealargs > mib.mind_nparams then
155 error "Constructors arguments must repeat the parameters.";
156 let _,params2 = List.chop (mib.mind_nparams-mip.mind_nrealargs) params in
157 let paramsctxt = Vars.subst_instance_context u mib.mind_params_ctxt in
158 let paramsctxt1,_ =
159 List.chop (mib.mind_nparams-mip.mind_nrealargs) paramsctxt in
160 if not (List.equal Constr.equal params2 constrargs) then
161 error "Constructors arguments must repeat the parameters.";
162 (* nrealargs_ctxt and nrealargs are the same here *)
163 (specif,mip.mind_nrealargs,realsign,paramsctxt,paramsctxt1)
164
165 (**********************************************************************)
166 (* Check if an inductive type [ind] has the form *)
167 (* *)
168 (* I q1..qm a1..an with one constructor *)
169 (* C : I q1..qm b1..bn *)
170 (* *)
171 (* in which case it expresses the equalities ai=bi, but not in a way *)
172 (* such that symmetry is a priori definable *)
173 (**********************************************************************)
174
175 let get_non_sym_eq_data env (ind,u) =
176 let (mib,mip as specif) = lookup_mind_specif env ind in
177 if not (Int.equal (Array.length mib.mind_packets) 1) ||
178 not (Int.equal (Array.length mip.mind_nf_lc) 1) then
179 error "Not an inductive type with a single constructor.";
180 let arityctxt = Vars.subst_instance_context u mip.mind_arity_ctxt in
181 let realsign,_ = List.chop mip.mind_nrealdecls arityctxt in
182 if List.exists is_local_def realsign then
183 error "Inductive equalities with local definitions in arity not supported";
184 let constrsign,ccl = mip.mind_nf_lc.(0) in
185 let _,constrargs = decompose_app_list ccl in
186 if not (Int.equal (Context.Rel.length constrsign) (Context.Rel.length mib.mind_params_ctxt)) then
187 error "Constructor must have no arguments";
188 let _,constrargs = List.chop mib.mind_nparams constrargs in
189 let constrargs = List.map (Vars.subst_instance_constr u) constrargs in
190 let paramsctxt = Vars.subst_instance_context u mib.mind_params_ctxt in
191 (specif,constrargs,realsign,paramsctxt,mip.mind_nrealargs)
192
193 (**********************************************************************)
194 (* Build the symmetry lemma associated to an inductive type *)
195 (* I q1..qm,p1..pn a1..an with one constructor *)
196 (* C : I q1..qm,p1..pn p1..pn *)
197 (* *)
198 (* sym := fun q1..qn p1..pn a1..an (H:I q1..qm p1..pn a1..an) => *)
199 (* match H in I _.._ a1..an return I q1..qm a1..an p1..pn with *)
200 (* C => C *)
201 (* end *)
202 (* : forall q1..qm p1..pn a1..an I q1..qm p1..pn a1..an -> *)
203 (* I q1..qm a1..an p1..pn *)
204 (* *)
205 (**********************************************************************)
206
207 let build_sym_scheme env _handle ind =
208 let (ind,u as indu), ctx = UnivGen.fresh_inductive_instance env ind in
209 let (mib,mip as specif),nrealargs,realsign,paramsctxt,paramsctxt1 =
210 get_sym_eq_data env indu in
211 let cstr n =
212 mkApp (mkConstructUi(indu,1),Context.Rel.instance mkRel n mib.mind_params_ctxt) in
213 let varH,_ = fresh env (default_id_of_ind ind mip) Id.Set.empty in
214 let applied_ind = build_dependent_inductive indu specif in
215 let indr = UVars.subst_instance_relevance u mip.mind_relevance in
216 let realsign_ind =
217 name_context env ((LocalAssum (make_annot (Name varH) indr,applied_ind))::realsign) in
218 let rci = Sorts.Relevant in (* TODO relevance *)
219 let ci = make_case_info env ind RegularStyle in
220 let p =
221 my_it_mkLambda_or_LetIn_name env
222 (lift_rel_context (nrealargs+1) realsign_ind)
223 (mkApp (mkIndU indu,
224 Array.concat
225 [Context.Rel.instance mkRel (3*nrealargs+2) paramsctxt1;
226 rel_vect 1 nrealargs;
227 rel_vect (2*nrealargs+2) nrealargs]))
228 in
229 let c =
230 (my_it_mkLambda_or_LetIn paramsctxt
231 (my_it_mkLambda_or_LetIn_name env realsign_ind
232 (mkCase
233 (Inductive.contract_case env
234 (ci,
235 (p,rci),
236 NoInvert,
237 mkRel 1 (* varH *),
238 [|cstr (nrealargs+1)|])))))
239 in
240 c, of_context_set env ctx
241
242 let sym_scheme_kind =
243 declare_individual_scheme_object "sym"
244 build_sym_scheme
245
246 (**********************************************************************)
247 (* Build the involutivity of symmetry for an inductive type *)
248 (* I q1..qm,p1..pn a1..an with one constructor *)
249 (* C : I q1..qm,p1..pn p1..pn *)
250 (* *)
251 (* inv := fun q1..qn p1..pn a1..an (H:I q1..qm p1..pn a1..an) => *)
252 (* match H in I _.._ a1..an return *)
253 (* sym q1..qm p1..pn a1..an (sym q1..qm a1..an p1..pn H) = H *)
254 (* with *)
255 (* C => refl_equal C *)
256 (* end *)
257 (* : forall q1..qm p1..pn a1..an (H:I q1..qm a1..an p1..pn), *)
258 (* sym q1..qm p1..pn a1..an (sym q1..qm a1..an p1..pn H) = H *)
259 (* *)
260 (**********************************************************************)
261
262 let const_of_scheme kind env handle ind ctx =
263 let sym_scheme = match local_lookup_scheme handle kind ind with Some cst -> cst | None -> assert false in
264 with_context_set ctx (UnivGen.fresh_global_instance env sym_scheme)
265
266 let build_sym_involutive_scheme env handle ind =
267 let (ind,u as indu), ctx = UnivGen.fresh_inductive_instance env ind in
268 let (mib,mip as specif),nrealargs,realsign,paramsctxt,paramsctxt1 =
269 get_sym_eq_data env indu in
270 let eq,eqrefl,ctx = get_rocq_eq env ctx in
271 let sym, ctx = const_of_scheme sym_scheme_kind env handle ind ctx in
272 let cstr n = mkApp (mkConstructUi (indu,1),Context.Rel.instance mkRel n paramsctxt) in
273 let indr = UVars.subst_instance_relevance u mip.mind_relevance in
274 let varH,_ = fresh env (default_id_of_ind ind mip) Id.Set.empty in
275 let applied_ind = build_dependent_inductive indu specif in
276 let applied_ind_C =
277 mkApp
278 (mkIndU indu, Array.append
279 (Context.Rel.instance mkRel (nrealargs+1) mib.mind_params_ctxt)
280 (rel_vect (nrealargs+1) nrealargs)) in
281 let realsign_ind =
282 name_context env ((LocalAssum (make_annot (Name varH) indr,applied_ind))::realsign) in
283 let rci = Sorts.Relevant in (* TODO relevance *)
284 let ci = make_case_info env ind RegularStyle in
285 let c =
286 (my_it_mkLambda_or_LetIn paramsctxt
287 (my_it_mkLambda_or_LetIn_name env realsign_ind
288 (mkCase (Inductive.contract_case env (ci,
289 (my_it_mkLambda_or_LetIn_name env
290 (lift_rel_context (nrealargs+1) realsign_ind)
291 (mkApp (eq,[|
292 mkApp
293 (mkIndU indu, Array.concat
294 [Context.Rel.instance mkRel (3*nrealargs+2) paramsctxt1;
295 rel_vect (2*nrealargs+2) nrealargs;
296 rel_vect 1 nrealargs]);
297 mkApp (sym,Array.concat
298 [Context.Rel.instance mkRel (3*nrealargs+2) paramsctxt1;
299 rel_vect 1 nrealargs;
300 rel_vect (2*nrealargs+2) nrealargs;
301 [|mkApp (sym,Array.concat
302 [Context.Rel.instance mkRel (3*nrealargs+2) paramsctxt1;
303 rel_vect (2*nrealargs+2) nrealargs;
304 rel_vect 1 nrealargs;
305 [|mkRel 1|]])|]]);
306 mkRel 1|])), rci),
307 NoInvert,
308 mkRel 1 (* varH *),
309 [|mkApp(eqrefl,[|applied_ind_C;cstr (nrealargs+1)|])|])))))
310 in (c, of_context_set env ctx)
311
312 let sym_involutive_scheme_kind =
313 declare_individual_scheme_object "sym_involutive"
314 ~deps:(fun _ ind -> [SchemeIndividualDep (ind, sym_scheme_kind)])
315 build_sym_involutive_scheme
316
317 (**********************************************************************)
318 (* Build the left-to-right rewriting lemma for conclusion associated *)
319 (* to an inductive type I q1..qm,p1..pn a1..an with one constructor *)
320 (* C : I q1..qm,p1..pn p1..pn *)
321 (* (symmetric equality in non-dependent and dependent cases) *)
322 (* *)
323 (* We could have defined the scheme in one match over a generalized *)
324 (* type but this behaves badly wrt the guard condition, so we use *)
325 (* symmetry instead; with commutative-cuts-aware guard condition a *)
326 (* proof in the style of l2r_forward is also possible (see below) *)
327 (* *)
328 (* rew := fun q1..qm p1..pn a1..an *)
329 (* (P:forall p1..pn, I q1..qm p1..pn a1..an -> kind) *)
330 (* (HC:P a1..an C) *)
331 (* (H:I q1..qm p1..pn a1..an) => *)
332 (* match sym_involutive q1..qm p1..pn a1..an H as Heq *)
333 (* in _ = H return P p1..pn H *)
334 (* with *)
335 (* refl => *)
336 (* match sym q1..qm p1..pn a1..an H as H *)
337 (* in I _.._ p1..pn *)
338 (* return P p1..pn (sym q1..qm a1..an p1..pn H) *)
339 (* with *)
340 (* C => HC *)
341 (* end *)
342 (* end *)
343 (* : forall q1..qn p1..pn a1..an *)
344 (* (P:forall p1..pn, I q1..qm p1..pn a1..an -> kind), *)
345 (* P a1..an C -> *)
346 (* forall (H:I q1..qm p1..pn a1..an), P p1..pn H *)
347 (* *)
348 (* where A1..An are the common types of p1..pn and a1..an *)
349 (* *)
350 (* Note: the symmetry is needed in the dependent case since the *)
351 (* dependency is on the inner arguments (the indices in C) and these *)
352 (* inner arguments need to be visible as parameters to be able to *)
353 (* abstract over them in P. *)
354 (**********************************************************************)
355
356 (**********************************************************************)
357 (* For information, the alternative proof of dependent l2r_rew scheme *)
358 (* that would use commutative cuts is the following *)
359 (* *)
360 (* rew := fun q1..qm p1..pn a1..an *)
361 (* (P:forall p1..pn, I q1..qm p1..pn a1..an -> kind) *)
362 (* (HC:P a1..an C) *)
363 (* (H:I q1..qm p1..pn a1..an) => *)
364 (* match H in I .._.. a1..an return *)
365 (* forall p1..pn, I q1..qm p1..pn a1..an -> kind), *)
366 (* P a1..an C -> P p1..pn H *)
367 (* with *)
368 (* C => fun P HC => HC *)
369 (* end P HC *)
370 (* : forall q1..qn p1..pn a1..an *)
371 (* (P:forall p1..pn, I q1..qm p1..pn a1..an -> kind), *)
372 (* P a1..an C -> *)
373 (* forall (H:I q1..qm p1..pn a1..an), P p1..pn H *)
374 (* *)
375 (**********************************************************************)
376
377 let build_l2r_rew_scheme dep env handle ind kind =
378 let (ind,u as indu), ctx = UnivGen.fresh_inductive_instance env ind in
379 let (mib,mip as specif),nrealargs,realsign,paramsctxt,paramsctxt1 =
380 get_sym_eq_data env indu in
381 let sym, ctx = const_of_scheme sym_scheme_kind env handle ind ctx in
382 let sym_involutive, ctx = const_of_scheme sym_involutive_scheme_kind env handle ind ctx in
383 let eq,eqrefl,ctx = get_rocq_eq env ctx in
384 let cstr n p =
385 mkApp (mkConstructUi(indu,1),
386 Array.concat [Context.Rel.instance mkRel n paramsctxt1;
387 rel_vect p nrealargs]) in
388 let indr = UVars.subst_instance_relevance u mip.mind_relevance in
389 let varH,avoid = fresh env (default_id_of_ind ind mip) Id.Set.empty in
390 let varHC,avoid = fresh env (Id.of_string "HC") avoid in
391 let varP,_ = fresh env (Id.of_string "P") avoid in
392 let applied_ind = build_dependent_inductive indu specif in
393 let applied_ind_P =
394 mkApp (mkIndU indu, Array.concat
395 [Context.Rel.instance mkRel (3*nrealargs) paramsctxt1;
396 rel_vect 0 nrealargs;
397 rel_vect nrealargs nrealargs]) in
398 let applied_ind_G =
399 mkApp (mkIndU indu, Array.concat
400 [Context.Rel.instance mkRel (3*nrealargs+3) paramsctxt1;
401 rel_vect (nrealargs+3) nrealargs;
402 rel_vect 0 nrealargs]) in
403 let realsign_P = lift_rel_context nrealargs realsign in
404 let realsign_ind_P =
405 name_context env ((LocalAssum (make_annot (Name varH) indr,applied_ind_P))::realsign_P) in
406 let realsign_ind_G =
407 name_context env ((LocalAssum (make_annot (Name varH) indr,applied_ind_G))::
408 lift_rel_context (nrealargs+3) realsign) in
409 let applied_sym_C n =
410 mkApp(sym,
411 Array.append (Context.Rel.instance mkRel n mip.mind_arity_ctxt) [|mkVar varH|]) in
412 let applied_sym_G =
413 mkApp(sym,
414 Array.concat [Context.Rel.instance mkRel (nrealargs*3+4) paramsctxt1;
415 rel_vect (nrealargs+4) nrealargs;
416 rel_vect 1 nrealargs;
417 [|mkRel 1|]]) in
418 let s, ctx' = UnivGen.fresh_sort_in_quality kind in
419 let ctx = UnivGen.sort_context_union ctx ctx' in
420 let s = mkSort s in
421 let rci = Sorts.Relevant in (* TODO relevance *)
422 let ci = make_case_info env ind RegularStyle in
423 let cieq = make_case_info env (fst (destInd eq)) RegularStyle in
424 let applied_PC =
425 mkApp (mkVar varP,Array.append (Context.Rel.instance mkRel 1 realsign)
426 (if dep then [|cstr (2*nrealargs+1) 1|] else [||])) in
427 let applied_PG =
428 mkApp (mkVar varP,Array.append (rel_vect 1 nrealargs)
429 (if dep then [|applied_sym_G|] else [||])) in
430 let applied_PR =
431 mkApp (mkVar varP,Array.append (rel_vect (nrealargs+5) nrealargs)
432 (if dep then [|mkRel 2|] else [||])) in
433 let applied_sym_sym =
434 mkApp (sym,Array.concat
435 [Context.Rel.instance mkRel (2*nrealargs+4) paramsctxt1;
436 rel_vect 4 nrealargs;
437 rel_vect (nrealargs+4) nrealargs;
438 [|mkApp (sym,Array.concat
439 [Context.Rel.instance mkRel (2*nrealargs+4) paramsctxt1;
440 rel_vect (nrealargs+4) nrealargs;
441 rel_vect 4 nrealargs;
442 [|mkRel 2|]])|]]) in
443 let main_body =
444 mkCase (Inductive.contract_case env (ci,
445 (my_it_mkLambda_or_LetIn_name env realsign_ind_G applied_PG, rci),
446 NoInvert,
447 applied_sym_C 3,
448 [|mkVar varHC|]))
449 in
450 let c =
451 (my_it_mkLambda_or_LetIn paramsctxt
452 (my_it_mkLambda_or_LetIn_name env realsign
453 (mkNamedLambda (make_annot varP indr)
454 (my_it_mkProd_or_LetIn (if dep then realsign_ind_P else realsign_P) s)
455 (mkNamedLambda (make_annot varHC indr) applied_PC
456 (mkNamedLambda (make_annot varH indr) (lift 2 applied_ind)
457 (if dep then (* we need a coercion *)
458 mkCase (Inductive.contract_case env (cieq,
459 (mkLambda (make_annot (Name varH) indr,lift 3 applied_ind,
460 mkLambda (make_annot Anonymous indr,
461 mkApp (eq,[|lift 4 applied_ind;applied_sym_sym;mkRel 1|]),
462 applied_PR)), rci),
463 NoInvert,
464 mkApp (sym_involutive,
465 Array.append (Context.Rel.instance mkRel 3 mip.mind_arity_ctxt) [|mkVar varH|]),
466 [|main_body|]))
467 else
468 main_body))))))
469 in (c, of_context_set env ctx)
470
471 (**********************************************************************)
472 (* Build the left-to-right rewriting lemma for hypotheses associated *)
473 (* to an inductive type I q1..qm,p1..pn a1..an with one constructor *)
474 (* C : I q1..qm,p1..pn p1..pn *)
475 (* (symmetric equality in non dependent and dependent cases) *)
476 (* *)
477 (* rew := fun q1..qm p1..pn a1..an (H:I q1..qm p1..pn a1..an) *)
478 (* match H in I _.._ a1..an *)
479 (* return forall *)
480 (* (P:forall p1..pn, I q1..qm p1..pn a1..an -> kind) *)
481 (* (HC:P p1..pn H) => *)
482 (* P a1..an C *)
483 (* with *)
484 (* C => fun P HC => HC *)
485 (* end *)
486 (* : forall q1..qm p1..pn a1..an *)
487 (* (H:I q1..qm p1..pn a1..an) *)
488 (* (P:forall p1..pn, I q1..qm p1..pn a1..an ->kind), *)
489 (* P p1..pn H -> P a1..an C *)
490 (* *)
491 (* Note: the symmetry is needed in the dependent case since the *)
492 (* dependency is on the inner arguments (the indices in C) and these *)
493 (* inner arguments need to be visible as parameters to be able to *)
494 (* abstract over them in P. *)
495 (**********************************************************************)
496
497 let build_l2r_forward_rew_scheme dep env ind kind =
498 let (ind,u as indu), ctx = UnivGen.fresh_inductive_instance env ind in
499 let (mib,mip as specif),nrealargs,realsign,paramsctxt,paramsctxt1 =
500 get_sym_eq_data env indu in
501 let cstr n p =
502 mkApp (mkConstructUi(indu,1),
503 Array.concat [Context.Rel.instance mkRel n paramsctxt1;
504 rel_vect p nrealargs]) in
505 let indr = UVars.subst_instance_relevance u mip.mind_relevance in
506 let varH,avoid = fresh env (default_id_of_ind ind mip) Id.Set.empty in
507 let varHC,avoid = fresh env (Id.of_string "HC") avoid in
508 let varP,_ = fresh env (Id.of_string "P") avoid in
509 let applied_ind = build_dependent_inductive indu specif in
510 let applied_ind_P =
511 mkApp (mkIndU indu, Array.concat
512 [Context.Rel.instance mkRel (4*nrealargs+2) paramsctxt1;
513 rel_vect 0 nrealargs;
514 rel_vect (nrealargs+1) nrealargs]) in
515 let applied_ind_P' =
516 mkApp (mkIndU indu, Array.concat
517 [Context.Rel.instance mkRel (3*nrealargs+1) paramsctxt1;
518 rel_vect 0 nrealargs;
519 rel_vect (2*nrealargs+1) nrealargs]) in
520 let realsign_P n = lift_rel_context (nrealargs*n+n) realsign in
521 let realsign_ind =
522 name_context env ((LocalAssum (make_annot (Name varH) indr,applied_ind))::realsign) in
523 let realsign_ind_P n aP =
524 name_context env ((LocalAssum (make_annot (Name varH) indr,aP))::realsign_P n) in
525 let s, ctx' = UnivGen.fresh_sort_in_quality kind in
526 let sr = Sorts.relevance_of_sort s in
527 let ctx = UnivGen.sort_context_union ctx ctx' in
528 let s = mkSort s in
529 let rci = Sorts.Relevant in
530 let ci = make_case_info env ind RegularStyle in
531 let applied_PC =
532 mkApp (mkVar varP,Array.append
533 (rel_vect (nrealargs*2+3) nrealargs)
534 (if dep then [|mkRel 2|] else [||])) in
535 let applied_PC' =
536 mkApp (mkVar varP,Array.append
537 (rel_vect (nrealargs+2) nrealargs)
538 (if dep then [|cstr (2*nrealargs+2) (nrealargs+2)|]
539 else [||])) in
540 let applied_PG =
541 mkApp (mkVar varP,Array.append (rel_vect 3 nrealargs)
542 (if dep then [|cstr (3*nrealargs+4) 3|] else [||])) in
543 let c =
544 (my_it_mkLambda_or_LetIn paramsctxt
545 (my_it_mkLambda_or_LetIn_name env realsign
546 (mkNamedLambda (make_annot varH indr) applied_ind
547 (mkCase (Inductive.contract_case env (ci,
548 (my_it_mkLambda_or_LetIn_name env
549 (lift_rel_context (nrealargs+1) realsign_ind)
550 (mkNamedProd (make_annot varP Sorts.Relevant)
551 (my_it_mkProd_or_LetIn
552 (if dep then realsign_ind_P 2 applied_ind_P else realsign_P 2) s)
553 (mkNamedProd (make_annot varHC sr) applied_PC applied_PG)), rci),
554 NoInvert,
555 (mkVar varH),
556 [|mkNamedLambda (make_annot varP Sorts.Relevant)
557 (my_it_mkProd_or_LetIn
558 (if dep then realsign_ind_P 1 applied_ind_P' else realsign_P 2) s)
559 (mkNamedLambda (make_annot varHC sr) applied_PC'
560 (mkVar varHC))|]))))))
561 in c, of_context_set env ctx
562
563 (**********************************************************************)
564 (* Build the right-to-left rewriting lemma for hypotheses associated *)
565 (* to an inductive type I q1..qm a1..an with one constructor *)
566 (* C : I q1..qm b1..bn *)
567 (* (arbitrary equality in non-dependent and dependent cases) *)
568 (* *)
569 (* rew := fun q1..qm a1..an (H:I q1..qm a1..an) *)
570 (* (P:forall a1..an, I q1..qm a1..an -> kind) *)
571 (* (HC:P a1..an H) => *)
572 (* match H in I _.._ a1..an return P a1..an H -> P b1..bn C *)
573 (* with *)
574 (* C => fun x => x *)
575 (* end HC *)
576 (* : forall q1..pm a1..an (H:I q1..qm a1..an) *)
577 (* (P:forall a1..an, I q1..qm a1..an -> kind), *)
578 (* P a1..an H -> P b1..bn C *)
579 (* *)
580 (* Note that the dependent elimination here is not a dependency *)
581 (* in the conclusion of the scheme but a dependency in the premise of *)
582 (* the scheme. This is unfortunately incompatible with the standard *)
583 (* pattern for schemes in Rocq which expects that the eliminated *)
584 (* object is the last premise of the scheme. We then have no choice *)
585 (* than following the more liberal pattern of having the eliminated *)
586 (* object coming before the premises. *)
587 (* *)
588 (* Note that in the non-dependent case, this scheme (up to the order *)
589 (* of premises) generalizes the (backward) l2r scheme above: same *)
590 (* statement but no need for symmetry of the equality. *)
591 (**********************************************************************)
592
593 let build_r2l_forward_rew_scheme dep env ind kind =
594 let (ind,u as indu), ctx = UnivGen.fresh_inductive_instance env ind in
595 let ((mib,mip as specif),constrargs,realsign,paramsctxt,nrealargs) =
596 get_non_sym_eq_data env indu in
597 let cstr n =
598 mkApp (mkConstructUi(indu,1),Context.Rel.instance mkRel n mib.mind_params_ctxt) in
599 let constrargs_cstr = constrargs@[cstr 0] in
600 let indr = Inductive.relevance_of_ind_body mip u in
601 let varH,avoid = fresh env (default_id_of_ind ind mip) Id.Set.empty in
602 let varHC,avoid = fresh env (Id.of_string "HC") avoid in
603 let varP,_ = fresh env (Id.of_string "P") avoid in
604 let applied_ind = build_dependent_inductive indu specif in
605 let realsign_ind =
606 name_context env ((LocalAssum (make_annot (Name varH) indr,applied_ind))::realsign) in
607 let s, ctx' = UnivGen.fresh_sort_in_quality kind in
608 let sr = Sorts.relevance_of_sort s in
609 let ctx = UnivGen.sort_context_union ctx ctx' in
610 let s = mkSort s in
611 let ci = make_case_info env ind RegularStyle in
612 let iv =
613 (* XXX is Evd.from_env correct? *)
614 if Inductiveops.Internal.should_invert_case env (Evd.from_env env) sr ci
615 then
616 let _, args = decompose_app_list applied_ind in
617 let indices = List.skipn mib.mind_nparams args in
618 let indices = CArray.map_of_list (lift 3) indices in
619 CaseInvert { indices }
620 else NoInvert
621 in
622 let applied_PC =
623 applist (mkVar varP,if dep then constrargs_cstr else constrargs) in
624 let applied_PG =
625 mkApp (mkVar varP,
626 if dep then Context.Rel.instance mkRel 0 realsign_ind
627 else Context.Rel.instance mkRel 1 realsign) in
628 let c =
629 (my_it_mkLambda_or_LetIn paramsctxt
630 (my_it_mkLambda_or_LetIn_name env realsign_ind
631 (mkNamedLambda (make_annot varP Sorts.Relevant)
632 (my_it_mkProd_or_LetIn (lift_rel_context (nrealargs+1)
633 (if dep then realsign_ind else realsign)) s)
634 (mkNamedLambda (make_annot varHC sr) (lift 1 applied_PG)
635 (mkApp
636 (mkCase (Inductive.contract_case env (ci,
637 (my_it_mkLambda_or_LetIn_name env
638 (lift_rel_context (nrealargs+3) realsign_ind)
639 (mkArrow applied_PG sr (lift (2*nrealargs+5) applied_PC)), sr),
640 iv,
641 mkRel 3 (* varH *),
642 [|mkLambda
643 (make_annot (Name varHC) sr,
644 lift (nrealargs+3) applied_PC,
645 mkRel 1)|])),
646 [|mkVar varHC|]))))))
647 in c, of_context_set env ctx
648
649 (**********************************************************************)
650 (* This function "repairs" the non-dependent r2l forward rewriting *)
651 (* scheme by making it comply with the standard pattern of schemes *)
652 (* in Rocq. Otherwise said, it turns a scheme of type *)
653 (* *)
654 (* forall q1..pm a1..an, I q1..qm a1..an -> *)
655 (* forall (P: forall a1..an, kind), *)
656 (* P a1..an -> P b1..bn *)
657 (* *)
658 (* into a scheme of type *)
659 (* *)
660 (* forall q1..pm (P:forall a1..an, kind), *)
661 (* P a1..an -> forall a1..an, I q1..qm a1..an -> P b1..bn *)
662 (* *)
663 (**********************************************************************)
664
665 let fix_r2l_forward_rew_scheme env (c, ctx') =
666 let sigma = Evd.from_env env in
667 let t = Retyping.get_type_of env sigma (EConstr.of_constr c) in
668 let t = EConstr.Unsafe.to_constr t in
669 let ctx,_ = decompose_prod_decls t in
670 match ctx with
671 | hp :: p :: ind :: indargs ->
672 let c' =
673 my_it_mkLambda_or_LetIn indargs
674 (mkLambda_or_LetIn (RelDecl.map_constr (liftn (-1) 1) p)
675 (mkLambda_or_LetIn (RelDecl.map_constr (liftn (-1) 2) hp)
676 (mkLambda_or_LetIn (RelDecl.map_constr (lift 2) ind)
677 (EConstr.Unsafe.to_constr (Reductionops.whd_beta env sigma
678 (EConstr.of_constr (applist (c,
679 Context.Rel.instance_list mkRel 3 indargs @ [mkRel 1;mkRel 3;mkRel 2]))))))))
680 in c', ctx'
681 | _ -> anomaly (Pp.str "Ill-formed non-dependent left-to-right rewriting scheme.")
682
683 (**********************************************************************)
684 (* Build the right-to-left rewriting lemma for conclusion associated *)
685 (* to an inductive type I q1..qm a1..an with one constructor *)
686 (* C : I q1..qm b1..bn *)
687 (* (arbitrary equality in non-dependent and dependent case) *)
688 (* *)
689 (* This is actually the standard case analysis scheme *)
690 (* *)
691 (* rew := fun q1..qm a1..an *)
692 (* (P:forall a1..an, I q1..qm a1..an -> kind) *)
693 (* (H:I q1..qm a1..an) *)
694 (* (HC:P b1..bn C) => *)
695 (* match H in I _.._ a1..an return P a1..an H with *)
696 (* C => HC *)
697 (* end *)
698 (* : forall q1..pm a1..an *)
699 (* (P:forall a1..an, I q1..qm a1..an -> kind) *)
700 (* (H:I q1..qm a1..an), *)
701 (* P b1..bn C -> P a1..an H *)
702 (**********************************************************************)
703
704 let build_r2l_rew_scheme dep env ind k =
705 let sigma = Evd.from_env env in
706 let (sigma, indu) = Evd.fresh_inductive_instance env sigma ind in
707 let sigma, k = Evd.fresh_sort_in_quality ~rigid:UnivRigid sigma k in
708 let (sigma, c, _) = build_case_analysis_scheme env sigma indu dep k in
709 EConstr.Unsafe.to_constr c, Evd.ustate sigma
710
711 (**********************************************************************)
712 (* Register the rewriting schemes *)
713 (**********************************************************************)
714
715 (**********************************************************************)
716 (* Dependent rewrite from left-to-right in conclusion *)
717 (* (symmetrical equality type only) *)
718 (* Gamma |- P p1..pn H ==> Gamma |- P a1..an C *)
719 (* with H:I p1..pn a1..an in Gamma *)
720 (**********************************************************************)
721 let rew_l2r_dep_scheme_kind =
722 declare_individual_scheme_object "rew_r_dep"
723 ~deps:(fun _ ind -> [
724 SchemeIndividualDep (ind, sym_scheme_kind);
725 SchemeIndividualDep (ind, sym_involutive_scheme_kind);
726 ])
727 (fun env handle ind ->
728 build_l2r_rew_scheme true env handle ind UnivGen.QualityOrSet.qtype)
729
730 (**********************************************************************)
731 (* Dependent rewrite from right-to-left in conclusion *)
732 (* Gamma |- P a1..an H ==> Gamma |- P b1..bn C *)
733 (* with H:I a1..an in Gamma (non symmetric case) *)
734 (* or H:I b1..bn a1..an in Gamma (symmetric case) *)
735 (**********************************************************************)
736 let rew_r2l_dep_scheme_kind =
737 declare_individual_scheme_object "rew_dep"
738 (fun env _ ind -> build_r2l_rew_scheme true env ind UnivGen.QualityOrSet.qtype)
739
740 (**********************************************************************)
741 (* Dependent rewrite from right-to-left in hypotheses *)
742 (* Gamma, P a1..an H |- D ==> Gamma, P b1..bn C |- D *)
743 (* with H:I a1..an in Gamma (non symmetric case) *)
744 (* or H:I b1..bn a1..an in Gamma (symmetric case) *)
745 (**********************************************************************)
746 let rew_r2l_forward_dep_scheme_kind =
747 declare_individual_scheme_object "rew_fwd_dep"
748 (fun env _ ind -> build_r2l_forward_rew_scheme true env ind UnivGen.QualityOrSet.qtype)
749
750 (**********************************************************************)
751 (* Dependent rewrite from left-to-right in hypotheses *)
752 (* (symmetrical equality type only) *)
753 (* Gamma, P p1..pn H |- D ==> Gamma, P a1..an C |- D *)
754 (* with H:I p1..pn a1..an in Gamma *)
755 (**********************************************************************)
756 let rew_l2r_forward_dep_scheme_kind =
757 declare_individual_scheme_object "rew_fwd_r_dep"
758 (fun env _ ind -> build_l2r_forward_rew_scheme true env ind UnivGen.QualityOrSet.qtype)
759
760 (**********************************************************************)
761 (* Non-dependent rewrite from either left-to-right in conclusion or *)
762 (* right-to-left in hypotheses: both l2r_rew and r2l_forward_rew are *)
763 (* potential candidates. Since l2r_rew needs a symmetrical equality, *)
764 (* we adopt r2l_forward_rew (this one introduces a blocked beta- *)
765 (* expansion but since the guard condition supports commutative cuts *)
766 (* this is not a problem; we need though a fix to adjust it to the *)
767 (* standard form of schemes in Rocq) *)
768 (**********************************************************************)
769 let rew_l2r_scheme_kind =
770 declare_individual_scheme_object "rew_r"
771 (fun env _ ind -> fix_r2l_forward_rew_scheme env
772 (build_r2l_forward_rew_scheme false env ind UnivGen.QualityOrSet.qtype))
773
774 (**********************************************************************)
775 (* Non-dependent rewrite from either right-to-left in conclusion or *)
776 (* left-to-right in hypotheses: both r2l_rew and l2r_forward_rew but *)
777 (* since r2l_rew works in the non-symmetric case as well as without *)
778 (* introducing commutative cuts, we adopt it *)
779 (**********************************************************************)
780 let rew_r2l_scheme_kind =
781 declare_individual_scheme_object "rew"
782 (fun env _ ind -> build_r2l_rew_scheme false env ind UnivGen.QualityOrSet.qtype)
783
784 (* End of rewriting schemes *)
785
786 (**********************************************************************)
787 (* Build the congruence lemma associated to an inductive type *)
788 (* I p1..pn a with one constructor C : I q1..qn b *)
789 (* *)
790 (* congr := fun p1..pn (B:Type) (f:A->B) a (H:I p1..pn a) => *)
791 (* match H in I _.._ a' return f b = f a' with *)
792 (* C => eq_refl (f b) *)
793 (* end *)
794 (* : forall p1..pn (B:Type) (f:A->B) a, I p1..pn a -> f b = f a *)
795 (* *)
796 (* where A is the common type of a and b *)
797 (**********************************************************************)
798
799 (* TODO: extend it to types with more than one index *)
800
801 let build_congr env (eq,refl,ctx) ind =
802 let (ind,u as indu), ctx = with_context_set ctx
803 (UnivGen.fresh_inductive_instance env ind) in
804 let (mib,mip) = lookup_mind_specif env ind in
805 if not (Int.equal (Array.length mib.mind_packets) 1) || not (Int.equal (Array.length mip.mind_nf_lc) 1) then
806 error "Not an inductive type with a single constructor.";
807 if not (Int.equal mip.mind_nrealargs 1) then
808 error "Expect an inductive type with one predicate parameter.";
809 let i = 1 in
810 let arityctxt = Vars.subst_instance_context u mip.mind_arity_ctxt in
811 let paramsctxt = Vars.subst_instance_context u mib.mind_params_ctxt in
812 let realsign,_ = List.chop mip.mind_nrealdecls arityctxt in
813 if List.exists is_local_def realsign then
814 error "Inductive equalities with local definitions in arity not supported.";
815 let env_with_arity = push_rel_context arityctxt env in
816 let ty, tyr =
817 let decl = lookup_rel (mip.mind_nrealargs - i + 1) env_with_arity in
818 RelDecl.get_type decl, RelDecl.get_relevance decl
819 in
820 let constrsign,ccl = mip.mind_nf_lc.(0) in
821 let _,constrargs = decompose_app_list ccl in
822 if not (Int.equal (Context.Rel.length constrsign) (Context.Rel.length mib.mind_params_ctxt)) then
823 error "Constructor must have no arguments";
824 let b = List.nth constrargs (i + mib.mind_nparams - 1) in
825 let varB,avoid = fresh env (Id.of_string "B") Id.Set.empty in
826 let varH,avoid = fresh env (Id.of_string "H") avoid in
827 let varf,avoid = fresh env (Id.of_string "f") avoid in
828 let rci = Sorts.Relevant in (* TODO relevance *)
829 let ci = make_case_info env ind RegularStyle in
830 let lvl = UnivGen.fresh_level () in
831 let uni = Univ.Universe.make lvl in
832 let ctx =
833 let (qs, us), (qcst, ucst) = ctx in
834 let us = Univ.Level.Set.add lvl us in
835 ((qs, us), (qcst, UnivSubst.enforce_leq uni (univ_of_eq env eq) ucst)) in
836 let c =
837 my_it_mkLambda_or_LetIn paramsctxt
838 (mkNamedLambda (make_annot varB Sorts.Relevant) (mkType uni)
839 (mkNamedLambda (make_annot varf Sorts.Relevant) (mkArrow (lift 1 ty) tyr (mkVar varB))
840 (my_it_mkLambda_or_LetIn_name env (lift_rel_context 2 realsign)
841 (mkNamedLambda (make_annot varH Sorts.Relevant)
842 (applist
843 (mkIndU indu,
844 Context.Rel.instance_list mkRel (mip.mind_nrealargs+2) paramsctxt @
845 Context.Rel.instance_list mkRel 0 realsign))
846 (mkCase (Inductive.contract_case env (ci,
847 (my_it_mkLambda_or_LetIn_name env
848 (lift_rel_context (mip.mind_nrealargs+3) realsign)
849 (mkLambda
850 (make_annot Anonymous Sorts.Relevant,
851 applist
852 (mkIndU indu,
853 Context.Rel.instance_list mkRel (2*mip.mind_nrealdecls+3)
854 paramsctxt
855 @ Context.Rel.instance_list mkRel 0 realsign),
856 mkApp (eq,
857 [|mkVar varB;
858 mkApp (mkVar varf, [|lift (2*mip.mind_nrealdecls+4) b|]);
859 mkApp (mkVar varf, [|mkRel (mip.mind_nrealargs - i + 2)|])|]))), rci),
860 NoInvert,
861 mkVar varH,
862 [|mkApp (refl,
863 [|mkVar varB;
864 mkApp (mkVar varf, [|lift (mip.mind_nrealargs+3) b|])|])|])))))))
865 in c, of_context_set env ctx
866
867 let congr_scheme_kind = declare_individual_scheme_object "congr"
868 (fun env _ ind ->
869 (* May fail if equality is not defined *)
870 build_congr env (get_rocq_eq env UnivGen.empty_sort_context) ind)