123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847(************************************************************************)(* * The Coq Proof Assistant / The Coq Development Team *)(* v * Copyright INRIA, CNRS and contributors *)(* <O___,, * (see version control and CREDITS file for authors & dates) *)(* \VV/ **************************************************************)(* // * This file is distributed under the terms of the *)(* * GNU Lesser General Public License Version 2.1 *)(* * (see LICENSE file for the text of the license) *)(************************************************************************)(* File created by Hugo Herbelin, Nov 2009 *)(* This file builds schemes related to equality inductive types,
especially for dependent rewrite, rewriting on arbitrary equality
types and congruence on arbitrary equality types *)(* However, the choices made lack uniformity, as we have to make a
compromise between several constraints and ideal requirements:
- Having the extended schemes working conservatively over the
existing non-dependent schemes eq_rect and eq_rect_r. There is in
particular a problem with the dependent rewriting schemes in
hypotheses for which the inductive types cannot be in last
position of the scheme as it is the general rule in Coq. This has
an effect on the order of generated goals (side-conditions of the
lemma after or before the main goal). The non-dependent case can be
fixed but to the price of a lost of uniformity wrt side-conditions
in the dependent and non-dependent cases.
- Having schemes general enough to support non-symmetric equality
type like eq_true.
- Having schemes that avoid introducing beta-expansions blocked by
"match" so as to please the guard condition, but this introduces
some tricky things involving involutivity of symmetry that I
don't how to avoid. The result below is a compromise with
dependent left-to-right rewriting in conclusion (l2r_dep) using
the tricky involutivity of symmetry and dependent left-to-right
rewriting in hypotheses (r2l_forward_dep), that one wants to be
used for non-symmetric equality and that introduces blocked
beta-expansions.
One may wonder whether these extensions are worth to be done
regarding the price we have to pay and regarding the rare
situations where they are needed. However, I believe it meets a
natural expectation of the user.
*)openCErrorsopenUtilopenNamesopenTermopenConstropenContextopenVarsopenDeclarationsopenEnvironopenInductiveopenTermopsopenNamegenopenInductiveopsopenInd_tablesopenIndrecopenContext.Rel.DeclarationmoduleRelDecl=Context.Rel.Declarationlethid=Id.of_string"H"letxid=Id.of_string"X"letdefault_id_of_sort=functionInSProp|InProp|InSet->hid|InType->xidletfreshenvidavoid=letfreshid=next_global_ident_awayidavoidinfreshid,Id.Set.addfreshidavoidletwith_context_setctx(b,ctx')=(b,Univ.ContextSet.unionctxctx')letbuild_dependent_inductiveind(mib,mip)=letrealargs,_=List.chopmip.mind_nrealdeclsmip.mind_arity_ctxtinapplist(mkIndUind,Context.Rel.to_extended_listmkRelmip.mind_nrealdeclsmib.mind_params_ctxt@Context.Rel.to_extended_listmkRel0realargs)letnamed_hdenvtna=named_hdenv(Evd.from_envenv)(EConstr.of_constrt)naletname_assumptionenv=function|LocalAssum(na,t)->LocalAssum(map_annot(named_hdenvt)na,t)|LocalDef(na,c,t)->LocalDef(map_annot(named_hdenvc)na,c,t)letname_contextenvhyps=snd(List.fold_left(fun(env,hyps)d->letd'=name_assumptionenvdin(push_reld'env,d'::hyps))(env,[])(List.revhyps))letmy_it_mkLambda_or_LetInsc=it_mkLambda_or_LetIncsletmy_it_mkProd_or_LetInsc=Term.it_mkProd_or_LetIncsletmy_it_mkLambda_or_LetIn_nameenvsc=letmkLambda_or_LetIn_namedb=mkLambda_or_LetIn(name_assumptionenvd)binList.fold_left(funcd->mkLambda_or_LetIn_namedc)csletget_coq_eqenvctx=tryleteq=Globnames.destIndRef(Coqlib.lib_ref"core.eq.type")in(* Do not force the lazy if they are not defined *)leteq,ctx=with_context_setctx(UnivGen.fresh_inductive_instanceenveq)inmkIndUeq,mkConstructUi(eq,1),ctxwithNot_found->user_errPp.(str"eq not found.")letuniv_of_eqenveq=letopenEConstrinleteq=of_constreqinletsigma=Evd.from_envenvinmatchkindsigma(Retyping.get_type_ofenvsigmaeq)with|Prod(_,t,_)->(matchkindsigmatwithSortk->(matchESorts.kindsigmakwithTypeu->u|_->assertfalse)|_->assertfalse)|_->assertfalse(**********************************************************************)(* Check if an inductive type [ind] has the form *)(* *)(* I q1..qm,p1..pn a1..an with one constructor *)(* C : I q1..qm,p1..pn p1..pn *)(* *)(* in which case, a symmetry lemma is definable *)(**********************************************************************)leterrormsg=user_errPp.(strmsg)letget_sym_eq_dataenv(ind,u)=let(mib,mipasspecif)=lookup_mind_specifenvindinifnot(Int.equal(Array.lengthmib.mind_packets)1)||not(Int.equal(Array.lengthmip.mind_nf_lc)1)thenerror"Not an inductive type with a single constructor.";letarityctxt=Vars.subst_instance_contextumip.mind_arity_ctxtinletrealsign,_=List.chopmip.mind_nrealdeclsarityctxtinifList.existsis_local_defrealsignthenerror"Inductive equalities with local definitions in arity not supported.";letconstrsign,ccl=mip.mind_nf_lc.(0)inlet_,constrargs=decompose_appcclinifnot(Int.equal(Context.Rel.lengthconstrsign)(Context.Rel.lengthmib.mind_params_ctxt))thenerror"Constructor must have no arguments";(* This can be relaxed... *)letparams,constrargs=List.chopmib.mind_nparamsconstrargsinifmip.mind_nrealargs>mib.mind_nparamsthenerror"Constructors arguments must repeat the parameters.";let_,params2=List.chop(mib.mind_nparams-mip.mind_nrealargs)paramsinletparamsctxt=Vars.subst_instance_contextumib.mind_params_ctxtinletparamsctxt1,_=List.chop(mib.mind_nparams-mip.mind_nrealargs)paramsctxtinifnot(List.equalConstr.equalparams2constrargs)thenerror"Constructors arguments must repeat the parameters.";(* nrealargs_ctxt and nrealargs are the same here *)(specif,mip.mind_nrealargs,realsign,paramsctxt,paramsctxt1)(**********************************************************************)(* Check if an inductive type [ind] has the form *)(* *)(* I q1..qm a1..an with one constructor *)(* C : I q1..qm b1..bn *)(* *)(* in which case it expresses the equalities ai=bi, but not in a way *)(* such that symmetry is a priori definable *)(**********************************************************************)letget_non_sym_eq_dataenv(ind,u)=let(mib,mipasspecif)=lookup_mind_specifenvindinifnot(Int.equal(Array.lengthmib.mind_packets)1)||not(Int.equal(Array.lengthmip.mind_nf_lc)1)thenerror"Not an inductive type with a single constructor.";letarityctxt=Vars.subst_instance_contextumip.mind_arity_ctxtinletrealsign,_=List.chopmip.mind_nrealdeclsarityctxtinifList.existsis_local_defrealsignthenerror"Inductive equalities with local definitions in arity not supported";letconstrsign,ccl=mip.mind_nf_lc.(0)inlet_,constrargs=decompose_appcclinifnot(Int.equal(Context.Rel.lengthconstrsign)(Context.Rel.lengthmib.mind_params_ctxt))thenerror"Constructor must have no arguments";let_,constrargs=List.chopmib.mind_nparamsconstrargsinletconstrargs=List.map(Vars.subst_instance_constru)constrargsinletparamsctxt=Vars.subst_instance_contextumib.mind_params_ctxtin(specif,constrargs,realsign,paramsctxt,mip.mind_nrealargs)(**********************************************************************)(* Build the symmetry lemma associated to an inductive type *)(* I q1..qm,p1..pn a1..an with one constructor *)(* C : I q1..qm,p1..pn p1..pn *)(* *)(* sym := fun q1..qn p1..pn a1..an (H:I q1..qm p1..pn a1..an) => *)(* match H in I _.._ a1..an return I q1..qm a1..an p1..pn with *)(* C => C *)(* end *)(* : forall q1..qm p1..pn a1..an I q1..qm p1..pn a1..an -> *)(* I q1..qm a1..an p1..pn *)(* *)(**********************************************************************)letbuild_sym_schemeenv_handleind=let(ind,uasindu),ctx=UnivGen.fresh_inductive_instanceenvindinlet(mib,mipasspecif),nrealargs,realsign,paramsctxt,paramsctxt1=get_sym_eq_dataenvinduinletcstrn=mkApp(mkConstructUi(indu,1),Context.Rel.to_extended_vectmkRelnmib.mind_params_ctxt)inletinds=snd(mind_aritymip)inletvarH,_=freshenv(default_id_of_sortinds)Id.Set.emptyinletapplied_ind=build_dependent_inductiveinduspecifinletindr=Sorts.relevance_of_sort_familyindsinletrealsign_ind=name_contextenv((LocalAssum(make_annot(NamevarH)indr,applied_ind))::realsign)inletrci=Sorts.Relevantin(* TODO relevance *)letci=make_case_infoenvindrciRegularStyleinletc=(my_it_mkLambda_or_LetInparamsctxt(my_it_mkLambda_or_LetIn_nameenvrealsign_ind(mkCase(Inductive.contract_caseenv(ci,my_it_mkLambda_or_LetIn_nameenv(lift_rel_context(nrealargs+1)realsign_ind)(mkApp(mkIndUindu,Array.concat[Context.Rel.to_extended_vectmkRel(3*nrealargs+2)paramsctxt1;rel_vect1nrealargs;rel_vect(2*nrealargs+2)nrealargs])),NoInvert,mkRel1(* varH *),[|cstr(nrealargs+1)|])))))inc,UState.of_context_setctxletsym_scheme_kind=declare_individual_scheme_object"_sym_internal"build_sym_scheme(**********************************************************************)(* Build the involutivity of symmetry for an inductive type *)(* I q1..qm,p1..pn a1..an with one constructor *)(* C : I q1..qm,p1..pn p1..pn *)(* *)(* inv := fun q1..qn p1..pn a1..an (H:I q1..qm p1..pn a1..an) => *)(* match H in I _.._ a1..an return *)(* sym q1..qm p1..pn a1..an (sym q1..qm a1..an p1..pn H) = H *)(* with *)(* C => refl_equal C *)(* end *)(* : forall q1..qm p1..pn a1..an (H:I q1..qm a1..an p1..pn), *)(* sym q1..qm p1..pn a1..an (sym q1..qm a1..an p1..pn H) = H *)(* *)(**********************************************************************)letconst_of_schemekindenvhandleindctx=letsym_scheme=matchlocal_lookup_schemehandlekindindwithSomecst->cst|None->assertfalseinletsym,ctx=with_context_setctx(UnivGen.fresh_constant_instanceenvsym_scheme)inmkConstUsym,ctxletbuild_sym_involutive_schemeenvhandleind=let(ind,uasindu),ctx=UnivGen.fresh_inductive_instanceenvindinlet(mib,mipasspecif),nrealargs,realsign,paramsctxt,paramsctxt1=get_sym_eq_dataenvinduinleteq,eqrefl,ctx=get_coq_eqenvctxinletsym,ctx=const_of_schemesym_scheme_kindenvhandleindctxinletcstrn=mkApp(mkConstructUi(indu,1),Context.Rel.to_extended_vectmkRelnparamsctxt)inletinds=snd(mind_aritymip)inletindr=Sorts.relevance_of_sort_familyindsinletvarH,_=freshenv(default_id_of_sortinds)Id.Set.emptyinletapplied_ind=build_dependent_inductiveinduspecifinletapplied_ind_C=mkApp(mkIndUindu,Array.append(Context.Rel.to_extended_vectmkRel(nrealargs+1)mib.mind_params_ctxt)(rel_vect(nrealargs+1)nrealargs))inletrealsign_ind=name_contextenv((LocalAssum(make_annot(NamevarH)indr,applied_ind))::realsign)inletrci=Sorts.Relevantin(* TODO relevance *)letci=make_case_infoenvindrciRegularStyleinletc=(my_it_mkLambda_or_LetInparamsctxt(my_it_mkLambda_or_LetIn_nameenvrealsign_ind(mkCase(Inductive.contract_caseenv(ci,my_it_mkLambda_or_LetIn_nameenv(lift_rel_context(nrealargs+1)realsign_ind)(mkApp(eq,[|mkApp(mkIndUindu,Array.concat[Context.Rel.to_extended_vectmkRel(3*nrealargs+2)paramsctxt1;rel_vect(2*nrealargs+2)nrealargs;rel_vect1nrealargs]);mkApp(sym,Array.concat[Context.Rel.to_extended_vectmkRel(3*nrealargs+2)paramsctxt1;rel_vect1nrealargs;rel_vect(2*nrealargs+2)nrealargs;[|mkApp(sym,Array.concat[Context.Rel.to_extended_vectmkRel(3*nrealargs+2)paramsctxt1;rel_vect(2*nrealargs+2)nrealargs;rel_vect1nrealargs;[|mkRel1|]])|]]);mkRel1|])),NoInvert,mkRel1(* varH *),[|mkApp(eqrefl,[|applied_ind_C;cstr(nrealargs+1)|])|])))))in(c,UState.of_context_setctx)letsym_involutive_scheme_kind=declare_individual_scheme_object"_sym_involutive"~deps:(fun_ind->[SchemeIndividualDep(ind,sym_scheme_kind)])build_sym_involutive_scheme(**********************************************************************)(* Build the left-to-right rewriting lemma for conclusion associated *)(* to an inductive type I q1..qm,p1..pn a1..an with one constructor *)(* C : I q1..qm,p1..pn p1..pn *)(* (symmetric equality in non-dependent and dependent cases) *)(* *)(* We could have defined the scheme in one match over a generalized *)(* type but this behaves badly wrt the guard condition, so we use *)(* symmetry instead; with commutative-cuts-aware guard condition a *)(* proof in the style of l2r_forward is also possible (see below) *)(* *)(* rew := fun q1..qm p1..pn a1..an *)(* (P:forall p1..pn, I q1..qm p1..pn a1..an -> kind) *)(* (HC:P a1..an C) *)(* (H:I q1..qm p1..pn a1..an) => *)(* match sym_involutive q1..qm p1..pn a1..an H as Heq *)(* in _ = H return P p1..pn H *)(* with *)(* refl => *)(* match sym q1..qm p1..pn a1..an H as H *)(* in I _.._ p1..pn *)(* return P p1..pn (sym q1..qm a1..an p1..pn H) *)(* with *)(* C => HC *)(* end *)(* end *)(* : forall q1..qn p1..pn a1..an *)(* (P:forall p1..pn, I q1..qm p1..pn a1..an -> kind), *)(* P a1..an C -> *)(* forall (H:I q1..qm p1..pn a1..an), P p1..pn H *)(* *)(* where A1..An are the common types of p1..pn and a1..an *)(* *)(* Note: the symmetry is needed in the dependent case since the *)(* dependency is on the inner arguments (the indices in C) and these *)(* inner arguments need to be visible as parameters to be able to *)(* abstract over them in P. *)(**********************************************************************)(**********************************************************************)(* For information, the alternative proof of dependent l2r_rew scheme *)(* that would use commutative cuts is the following *)(* *)(* rew := fun q1..qm p1..pn a1..an *)(* (P:forall p1..pn, I q1..qm p1..pn a1..an -> kind) *)(* (HC:P a1..an C) *)(* (H:I q1..qm p1..pn a1..an) => *)(* match H in I .._.. a1..an return *)(* forall p1..pn, I q1..qm p1..pn a1..an -> kind), *)(* P a1..an C -> P p1..pn H *)(* with *)(* C => fun P HC => HC *)(* end P HC *)(* : forall q1..qn p1..pn a1..an *)(* (P:forall p1..pn, I q1..qm p1..pn a1..an -> kind), *)(* P a1..an C -> *)(* forall (H:I q1..qm p1..pn a1..an), P p1..pn H *)(* *)(**********************************************************************)letbuild_l2r_rew_schemedepenvhandleindkind=let(ind,uasindu),ctx=UnivGen.fresh_inductive_instanceenvindinlet(mib,mipasspecif),nrealargs,realsign,paramsctxt,paramsctxt1=get_sym_eq_dataenvinduinletsym,ctx=const_of_schemesym_scheme_kindenvhandleindctxinletsym_involutive,ctx=const_of_schemesym_involutive_scheme_kindenvhandleindctxinleteq,eqrefl,ctx=get_coq_eqenvctxinletcstrnp=mkApp(mkConstructUi(indu,1),Array.concat[Context.Rel.to_extended_vectmkRelnparamsctxt1;rel_vectpnrealargs])inletinds=snd(mind_aritymip)inletindr=Sorts.relevance_of_sort_familyindsinletvarH,avoid=freshenv(default_id_of_sortinds)Id.Set.emptyinletvarHC,avoid=freshenv(Id.of_string"HC")avoidinletvarP,_=freshenv(Id.of_string"P")avoidinletapplied_ind=build_dependent_inductiveinduspecifinletapplied_ind_P=mkApp(mkIndUindu,Array.concat[Context.Rel.to_extended_vectmkRel(3*nrealargs)paramsctxt1;rel_vect0nrealargs;rel_vectnrealargsnrealargs])inletapplied_ind_G=mkApp(mkIndUindu,Array.concat[Context.Rel.to_extended_vectmkRel(3*nrealargs+3)paramsctxt1;rel_vect(nrealargs+3)nrealargs;rel_vect0nrealargs])inletrealsign_P=lift_rel_contextnrealargsrealsigninletrealsign_ind_P=name_contextenv((LocalAssum(make_annot(NamevarH)indr,applied_ind_P))::realsign_P)inletrealsign_ind_G=name_contextenv((LocalAssum(make_annot(NamevarH)indr,applied_ind_G))::lift_rel_context(nrealargs+3)realsign)inletapplied_sym_Cn=mkApp(sym,Array.append(Context.Rel.to_extended_vectmkRelnmip.mind_arity_ctxt)[|mkVarvarH|])inletapplied_sym_G=mkApp(sym,Array.concat[Context.Rel.to_extended_vectmkRel(nrealargs*3+4)paramsctxt1;rel_vect(nrealargs+4)nrealargs;rel_vect1nrealargs;[|mkRel1|]])inlets,ctx'=UnivGen.fresh_sort_in_familykindinletctx=Univ.ContextSet.unionctxctx'inlets=mkSortsinletrci=Sorts.Relevantin(* TODO relevance *)letci=make_case_infoenvindrciRegularStyleinletcieq=make_case_infoenv(fst(destIndeq))rciRegularStyleinletapplied_PC=mkApp(mkVarvarP,Array.append(Context.Rel.to_extended_vectmkRel1realsign)(ifdepthen[|cstr(2*nrealargs+1)1|]else[||]))inletapplied_PG=mkApp(mkVarvarP,Array.append(rel_vect1nrealargs)(ifdepthen[|applied_sym_G|]else[||]))inletapplied_PR=mkApp(mkVarvarP,Array.append(rel_vect(nrealargs+5)nrealargs)(ifdepthen[|mkRel2|]else[||]))inletapplied_sym_sym=mkApp(sym,Array.concat[Context.Rel.to_extended_vectmkRel(2*nrealargs+4)paramsctxt1;rel_vect4nrealargs;rel_vect(nrealargs+4)nrealargs;[|mkApp(sym,Array.concat[Context.Rel.to_extended_vectmkRel(2*nrealargs+4)paramsctxt1;rel_vect(nrealargs+4)nrealargs;rel_vect4nrealargs;[|mkRel2|]])|]])inletmain_body=mkCase(Inductive.contract_caseenv(ci,my_it_mkLambda_or_LetIn_nameenvrealsign_ind_Gapplied_PG,NoInvert,applied_sym_C3,[|mkVarvarHC|]))inletc=(my_it_mkLambda_or_LetInparamsctxt(my_it_mkLambda_or_LetIn_nameenvrealsign(mkNamedLambda(make_annotvarPindr)(my_it_mkProd_or_LetIn(ifdepthenrealsign_ind_Pelserealsign_P)s)(mkNamedLambda(make_annotvarHCindr)applied_PC(mkNamedLambda(make_annotvarHindr)(lift2applied_ind)(ifdepthen(* we need a coercion *)mkCase(Inductive.contract_caseenv(cieq,mkLambda(make_annot(NamevarH)indr,lift3applied_ind,mkLambda(make_annotAnonymousindr,mkApp(eq,[|lift4applied_ind;applied_sym_sym;mkRel1|]),applied_PR)),NoInvert,mkApp(sym_involutive,Array.append(Context.Rel.to_extended_vectmkRel3mip.mind_arity_ctxt)[|mkVarvarH|]),[|main_body|]))elsemain_body))))))in(c,UState.of_context_setctx)(**********************************************************************)(* Build the left-to-right rewriting lemma for hypotheses associated *)(* to an inductive type I q1..qm,p1..pn a1..an with one constructor *)(* C : I q1..qm,p1..pn p1..pn *)(* (symmetric equality in non dependent and dependent cases) *)(* *)(* rew := fun q1..qm p1..pn a1..an (H:I q1..qm p1..pn a1..an) *)(* match H in I _.._ a1..an *)(* return forall *)(* (P:forall p1..pn, I q1..qm p1..pn a1..an -> kind) *)(* (HC:P p1..pn H) => *)(* P a1..an C *)(* with *)(* C => fun P HC => HC *)(* end *)(* : forall q1..qm p1..pn a1..an *)(* (H:I q1..qm p1..pn a1..an) *)(* (P:forall p1..pn, I q1..qm p1..pn a1..an ->kind), *)(* P p1..pn H -> P a1..an C *)(* *)(* Note: the symmetry is needed in the dependent case since the *)(* dependency is on the inner arguments (the indices in C) and these *)(* inner arguments need to be visible as parameters to be able to *)(* abstract over them in P. *)(**********************************************************************)letbuild_l2r_forward_rew_schemedepenvindkind=let(ind,uasindu),ctx=UnivGen.fresh_inductive_instanceenvindinlet(mib,mipasspecif),nrealargs,realsign,paramsctxt,paramsctxt1=get_sym_eq_dataenvinduinletcstrnp=mkApp(mkConstructUi(indu,1),Array.concat[Context.Rel.to_extended_vectmkRelnparamsctxt1;rel_vectpnrealargs])inletinds=snd(mind_aritymip)inletindr=Sorts.relevance_of_sort_familyindsinletvarH,avoid=freshenv(default_id_of_sortinds)Id.Set.emptyinletvarHC,avoid=freshenv(Id.of_string"HC")avoidinletvarP,_=freshenv(Id.of_string"P")avoidinletapplied_ind=build_dependent_inductiveinduspecifinletapplied_ind_P=mkApp(mkIndUindu,Array.concat[Context.Rel.to_extended_vectmkRel(4*nrealargs+2)paramsctxt1;rel_vect0nrealargs;rel_vect(nrealargs+1)nrealargs])inletapplied_ind_P'=mkApp(mkIndUindu,Array.concat[Context.Rel.to_extended_vectmkRel(3*nrealargs+1)paramsctxt1;rel_vect0nrealargs;rel_vect(2*nrealargs+1)nrealargs])inletrealsign_Pn=lift_rel_context(nrealargs*n+n)realsigninletrealsign_ind=name_contextenv((LocalAssum(make_annot(NamevarH)indr,applied_ind))::realsign)inletrealsign_ind_PnaP=name_contextenv((LocalAssum(make_annot(NamevarH)indr,aP))::realsign_Pn)inlets,ctx'=UnivGen.fresh_sort_in_familykindinletctx=Univ.ContextSet.unionctxctx'inlets=mkSortsinletrci=Sorts.Relevantinletci=make_case_infoenvindrciRegularStyleinletapplied_PC=mkApp(mkVarvarP,Array.append(rel_vect(nrealargs*2+3)nrealargs)(ifdepthen[|mkRel2|]else[||]))inletapplied_PC'=mkApp(mkVarvarP,Array.append(rel_vect(nrealargs+2)nrealargs)(ifdepthen[|cstr(2*nrealargs+2)(nrealargs+2)|]else[||]))inletapplied_PG=mkApp(mkVarvarP,Array.append(rel_vect3nrealargs)(ifdepthen[|cstr(3*nrealargs+4)3|]else[||]))inletc=(my_it_mkLambda_or_LetInparamsctxt(my_it_mkLambda_or_LetIn_nameenvrealsign(mkNamedLambda(make_annotvarHindr)applied_ind(mkCase(Inductive.contract_caseenv(ci,my_it_mkLambda_or_LetIn_nameenv(lift_rel_context(nrealargs+1)realsign_ind)(mkNamedProd(make_annotvarPindr)(my_it_mkProd_or_LetIn(ifdepthenrealsign_ind_P2applied_ind_Pelserealsign_P2)s)(mkNamedProd(make_annotvarHCindr)applied_PCapplied_PG)),NoInvert,(mkVarvarH),[|mkNamedLambda(make_annotvarPindr)(my_it_mkProd_or_LetIn(ifdepthenrealsign_ind_P1applied_ind_P'elserealsign_P2)s)(mkNamedLambda(make_annotvarHCindr)applied_PC'(mkVarvarHC))|]))))))inc,UState.of_context_setctx(**********************************************************************)(* Build the right-to-left rewriting lemma for hypotheses associated *)(* to an inductive type I q1..qm a1..an with one constructor *)(* C : I q1..qm b1..bn *)(* (arbitrary equality in non-dependent and dependent cases) *)(* *)(* rew := fun q1..qm a1..an (H:I q1..qm a1..an) *)(* (P:forall a1..an, I q1..qm a1..an -> kind) *)(* (HC:P a1..an H) => *)(* match H in I _.._ a1..an return P a1..an H -> P b1..bn C *)(* with *)(* C => fun x => x *)(* end HC *)(* : forall q1..pm a1..an (H:I q1..qm a1..an) *)(* (P:forall a1..an, I q1..qm a1..an -> kind), *)(* P a1..an H -> P b1..bn C *)(* *)(* Note that the dependent elimination here is not a dependency *)(* in the conclusion of the scheme but a dependency in the premise of *)(* the scheme. This is unfortunately incompatible with the standard *)(* pattern for schemes in Coq which expects that the eliminated *)(* object is the last premise of the scheme. We then have no choice *)(* than following the more liberal pattern of having the eliminated *)(* object coming before the premises. *)(* *)(* Note that in the non-dependent case, this scheme (up to the order *)(* of premises) generalizes the (backward) l2r scheme above: same *)(* statement but no need for symmetry of the equality. *)(**********************************************************************)letbuild_r2l_forward_rew_schemedepenvindkind=let(ind,uasindu),ctx=UnivGen.fresh_inductive_instanceenvindinlet((mib,mipasspecif),constrargs,realsign,paramsctxt,nrealargs)=get_non_sym_eq_dataenvinduinletcstrn=mkApp(mkConstructUi(indu,1),Context.Rel.to_extended_vectmkRelnmib.mind_params_ctxt)inletconstrargs_cstr=constrargs@[cstr0]inletinds=snd(mind_aritymip)inletindr=Sorts.relevance_of_sort_familyindsinletvarH,avoid=freshenv(default_id_of_sortinds)Id.Set.emptyinletvarHC,avoid=freshenv(Id.of_string"HC")avoidinletvarP,_=freshenv(Id.of_string"P")avoidinletapplied_ind=build_dependent_inductiveinduspecifinletrealsign_ind=name_contextenv((LocalAssum(make_annot(NamevarH)indr,applied_ind))::realsign)inlets,ctx'=UnivGen.fresh_sort_in_familykindinletctx=Univ.ContextSet.unionctxctx'inlets=mkSortsinletrci=Sorts.Relevantin(* TODO relevance *)letci=make_case_infoenvindrciRegularStyleinletapplied_PC=applist(mkVarvarP,ifdepthenconstrargs_cstrelseconstrargs)inletapplied_PG=mkApp(mkVarvarP,ifdepthenContext.Rel.to_extended_vectmkRel0realsign_indelseContext.Rel.to_extended_vectmkRel1realsign)inletc=(my_it_mkLambda_or_LetInparamsctxt(my_it_mkLambda_or_LetIn_nameenvrealsign_ind(mkNamedLambda(make_annotvarPindr)(my_it_mkProd_or_LetIn(lift_rel_context(nrealargs+1)(ifdepthenrealsign_indelserealsign))s)(mkNamedLambda(make_annotvarHCindr)(lift1applied_PG)(mkApp(mkCase(Inductive.contract_caseenv(ci,my_it_mkLambda_or_LetIn_nameenv(lift_rel_context(nrealargs+3)realsign_ind)(mkArrowapplied_PGindr(lift(2*nrealargs+5)applied_PC)),NoInvert,mkRel3(* varH *),[|mkLambda(make_annot(NamevarHC)indr,lift(nrealargs+3)applied_PC,mkRel1)|])),[|mkVarvarHC|]))))))inc,UState.of_context_setctx(**********************************************************************)(* This function "repairs" the non-dependent r2l forward rewriting *)(* scheme by making it comply with the standard pattern of schemes *)(* in Coq. Otherwise said, it turns a scheme of type *)(* *)(* forall q1..pm a1..an, I q1..qm a1..an -> *)(* forall (P: forall a1..an, kind), *)(* P a1..an -> P b1..bn *)(* *)(* into a scheme of type *)(* *)(* forall q1..pm (P:forall a1..an, kind), *)(* P a1..an -> forall a1..an, I q1..qm a1..an -> P b1..bn *)(* *)(**********************************************************************)letfix_r2l_forward_rew_schemeenv(c,ctx')=letsigma=Evd.from_envenvinlett=Retyping.get_type_ofenvsigma(EConstr.of_constrc)inlett=EConstr.Unsafe.to_constrtinletctx,_=decompose_prod_assumtinmatchctxwith|hp::p::ind::indargs->letc'=my_it_mkLambda_or_LetInindargs(mkLambda_or_LetIn(RelDecl.map_constr(liftn(-1)1)p)(mkLambda_or_LetIn(RelDecl.map_constr(liftn(-1)2)hp)(mkLambda_or_LetIn(RelDecl.map_constr(lift2)ind)(EConstr.Unsafe.to_constr(Reductionops.whd_betaenvsigma(EConstr.of_constr(applist(c,Context.Rel.to_extended_listmkRel3indargs@[mkRel1;mkRel3;mkRel2]))))))))inc',ctx'|_->anomaly(Pp.str"Ill-formed non-dependent left-to-right rewriting scheme.")(**********************************************************************)(* Build the right-to-left rewriting lemma for conclusion associated *)(* to an inductive type I q1..qm a1..an with one constructor *)(* C : I q1..qm b1..bn *)(* (arbitrary equality in non-dependent and dependent case) *)(* *)(* This is actually the standard case analysis scheme *)(* *)(* rew := fun q1..qm a1..an *)(* (P:forall a1..an, I q1..qm a1..an -> kind) *)(* (H:I q1..qm a1..an) *)(* (HC:P b1..bn C) => *)(* match H in I _.._ a1..an return P a1..an H with *)(* C => HC *)(* end *)(* : forall q1..pm a1..an *)(* (P:forall a1..an, I q1..qm a1..an -> kind) *)(* (H:I q1..qm a1..an), *)(* P b1..bn C -> P a1..an H *)(**********************************************************************)letbuild_r2l_rew_schemedepenvindk=letsigma=Evd.from_envenvinlet(sigma,indu)=Evd.fresh_inductive_instanceenvsigmaindinlet(sigma,c)=build_case_analysis_schemeenvsigmaindudepkinc,Evd.evar_universe_contextsigma(**********************************************************************)(* Register the rewriting schemes *)(**********************************************************************)(**********************************************************************)(* Dependent rewrite from left-to-right in conclusion *)(* (symmetrical equality type only) *)(* Gamma |- P p1..pn H ==> Gamma |- P a1..an C *)(* with H:I p1..pn a1..an in Gamma *)(**********************************************************************)letrew_l2r_dep_scheme_kind=declare_individual_scheme_object"_rew_r_dep"~deps:(fun_ind->[SchemeIndividualDep(ind,sym_scheme_kind);SchemeIndividualDep(ind,sym_involutive_scheme_kind);])(funenvhandleind->build_l2r_rew_schemetrueenvhandleindInType)(**********************************************************************)(* Dependent rewrite from right-to-left in conclusion *)(* Gamma |- P a1..an H ==> Gamma |- P b1..bn C *)(* with H:I a1..an in Gamma (non symmetric case) *)(* or H:I b1..bn a1..an in Gamma (symmetric case) *)(**********************************************************************)letrew_r2l_dep_scheme_kind=declare_individual_scheme_object"_rew_dep"(funenv_ind->build_r2l_rew_schemetrueenvindInType)(**********************************************************************)(* Dependent rewrite from right-to-left in hypotheses *)(* Gamma, P a1..an H |- D ==> Gamma, P b1..bn C |- D *)(* with H:I a1..an in Gamma (non symmetric case) *)(* or H:I b1..bn a1..an in Gamma (symmetric case) *)(**********************************************************************)letrew_r2l_forward_dep_scheme_kind=declare_individual_scheme_object"_rew_fwd_dep"(funenv_ind->build_r2l_forward_rew_schemetrueenvindInType)(**********************************************************************)(* Dependent rewrite from left-to-right in hypotheses *)(* (symmetrical equality type only) *)(* Gamma, P p1..pn H |- D ==> Gamma, P a1..an C |- D *)(* with H:I p1..pn a1..an in Gamma *)(**********************************************************************)letrew_l2r_forward_dep_scheme_kind=declare_individual_scheme_object"_rew_fwd_r_dep"(funenv_ind->build_l2r_forward_rew_schemetrueenvindInType)(**********************************************************************)(* Non-dependent rewrite from either left-to-right in conclusion or *)(* right-to-left in hypotheses: both l2r_rew and r2l_forward_rew are *)(* potential candidates. Since l2r_rew needs a symmetrical equality, *)(* we adopt r2l_forward_rew (this one introduces a blocked beta- *)(* expansion but since the guard condition supports commutative cuts *)(* this is not a problem; we need though a fix to adjust it to the *)(* standard form of schemes in Coq) *)(**********************************************************************)letrew_l2r_scheme_kind=declare_individual_scheme_object"_rew_r"(funenv_ind->fix_r2l_forward_rew_schemeenv(build_r2l_forward_rew_schemefalseenvindInType))(**********************************************************************)(* Non-dependent rewrite from either right-to-left in conclusion or *)(* left-to-right in hypotheses: both r2l_rew and l2r_forward_rew but *)(* since r2l_rew works in the non-symmetric case as well as without *)(* introducing commutative cuts, we adopt it *)(**********************************************************************)letrew_r2l_scheme_kind=declare_individual_scheme_object"_rew"(funenv_ind->build_r2l_rew_schemefalseenvindInType)(* End of rewriting schemes *)(**********************************************************************)(* Build the congruence lemma associated to an inductive type *)(* I p1..pn a with one constructor C : I q1..qn b *)(* *)(* congr := fun p1..pn (B:Type) (f:A->B) a (H:I p1..pn a) => *)(* match H in I _.._ a' return f b = f a' with *)(* C => eq_refl (f b) *)(* end *)(* : forall p1..pn (B:Type) (f:A->B) a, I p1..pn a -> f b = f a *)(* *)(* where A is the common type of a and b *)(**********************************************************************)(* TODO: extend it to types with more than one index *)letbuild_congrenv(eq,refl,ctx)ind=let(ind,uasindu),ctx=with_context_setctx(UnivGen.fresh_inductive_instanceenvind)inlet(mib,mip)=lookup_mind_specifenvindinifnot(Int.equal(Array.lengthmib.mind_packets)1)||not(Int.equal(Array.lengthmip.mind_nf_lc)1)thenerror"Not an inductive type with a single constructor.";ifnot(Int.equalmip.mind_nrealargs1)thenerror"Expect an inductive type with one predicate parameter.";leti=1inletarityctxt=Vars.subst_instance_contextumip.mind_arity_ctxtinletparamsctxt=Vars.subst_instance_contextumib.mind_params_ctxtinletrealsign,_=List.chopmip.mind_nrealdeclsarityctxtinifList.existsis_local_defrealsignthenerror"Inductive equalities with local definitions in arity not supported.";letenv_with_arity=push_rel_contextarityctxtenvinletty,tyr=letdecl=lookup_rel(mip.mind_nrealargs-i+1)env_with_arityinRelDecl.get_typedecl,RelDecl.get_relevancedeclinletconstrsign,ccl=mip.mind_nf_lc.(0)inlet_,constrargs=decompose_appcclinifnot(Int.equal(Context.Rel.lengthconstrsign)(Context.Rel.lengthmib.mind_params_ctxt))thenerror"Constructor must have no arguments";letb=List.nthconstrargs(i+mib.mind_nparams-1)inletvarB,avoid=freshenv(Id.of_string"B")Id.Set.emptyinletvarH,avoid=freshenv(Id.of_string"H")avoidinletvarf,avoid=freshenv(Id.of_string"f")avoidinletrci=Sorts.Relevantin(* TODO relevance *)letci=make_case_infoenvindrciRegularStyleinletuni,ctx=Univ.extend_in_context_set(UnivGen.new_global_univ())ctxinletctx=(fstctx,Univ.enforce_lequni(univ_of_eqenveq)(sndctx))inletc=my_it_mkLambda_or_LetInparamsctxt(mkNamedLambda(make_annotvarBSorts.Relevant)(mkTypeuni)(mkNamedLambda(make_annotvarfSorts.Relevant)(mkArrow(lift1ty)tyr(mkVarvarB))(my_it_mkLambda_or_LetIn_nameenv(lift_rel_context2realsign)(mkNamedLambda(make_annotvarHSorts.Relevant)(applist(mkIndUindu,Context.Rel.to_extended_listmkRel(mip.mind_nrealargs+2)paramsctxt@Context.Rel.to_extended_listmkRel0realsign))(mkCase(Inductive.contract_caseenv(ci,my_it_mkLambda_or_LetIn_nameenv(lift_rel_context(mip.mind_nrealargs+3)realsign)(mkLambda(make_annotAnonymousSorts.Relevant,applist(mkIndUindu,Context.Rel.to_extended_listmkRel(2*mip.mind_nrealdecls+3)paramsctxt@Context.Rel.to_extended_listmkRel0realsign),mkApp(eq,[|mkVarvarB;mkApp(mkVarvarf,[|lift(2*mip.mind_nrealdecls+4)b|]);mkApp(mkVarvarf,[|mkRel(mip.mind_nrealargs-i+2)|])|]))),NoInvert,mkVarvarH,[|mkApp(refl,[|mkVarvarB;mkApp(mkVarvarf,[|lift(mip.mind_nrealargs+3)b|])|])|])))))))inc,UState.of_context_setctxletcongr_scheme_kind=declare_individual_scheme_object"_congr"(funenv_ind->(* May fail if equality is not defined *)build_congrenv(get_coq_eqenvUniv.ContextSet.empty)ind)