Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file tacticals.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595(************************************************************************)(* * 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) *)(************************************************************************)openPpopenCErrorsopenUtilopenNamesopenConstropenEConstropenDeclarationsopenTactypesmoduleRelDecl=Context.Rel.DeclarationmoduleNamedDecl=Context.Named.Declaration(*********************)(* Tacticals *)(*********************)exceptionFailErrorofint*Pp.tLazy.tletcatch_failerror(e,info)=matchewith|FailError(lvl,s)whenlvl>0->Exninfo.iraise(FailError(lvl-1,s),info)|e->Control.check_for_interrupt()(************************************************************************)(* Elimination Tacticals *)(************************************************************************)(* The following tacticals allow to apply a tactic to the
branches generated by the application of an elimination
tactic.
Two auxiliary types --branch_args and branch_assumptions-- are
used to keep track of some information about the ``branches'' of
the elimination. *)letfix_empty_or_and_patternnvl=(* 1- The syntax does not distinguish between "[ ]" for one clause with no
names and "[ ]" for no clause at all *)(* 2- More generally, we admit "[ ]" for any disjunctive pattern of
arbitrary length *)matchlwith|IntroOrPattern[[]]->IntroOrPattern(List.makenv[])|_->lletcheck_or_and_pattern_size?loccheck_andnamesbranchsigns=letn=Array.lengthbranchsignsinletmsgp1p2=strbrk"a conjunctive pattern made of "++intp1++(ifp1==p2thenmt()elsestr" or "++intp2)++str" patterns"inleterr1p1p2=user_err?loc(str"Expects "++msgp1p2++str".")inleterrnn=user_err?loc(str"Expects a disjunctive pattern with "++intn++str" branches.")inleterr1'p1p2=user_err?loc(strbrk"Expects a disjunctive pattern with 1 branch or "++msgp1p2++str".")inleterrforthcoming?loc=user_err?loc(strbrk"Unexpected non atomic pattern.")inmatchnameswith|IntroAndPatternl->ifnot(Int.equaln1)thenerrnn;letl'=List.filterCAst.(function{v=IntroForthcoming_}->true|{v=IntroNaming_}|{v=IntroAction_}->false)linifl'!=[]thenerrforthcoming?loc:(List.hdl').CAst.loc;ifcheck_andthenletp1=List.count(funx->x)branchsigns.(0)inletp2=List.lengthbranchsigns.(0)inletp=List.lengthlinifnot(Int.equalpp1||Int.equalpp2)thenerr1p1p2;ifInt.equalpp1thenIntroAndPattern(List.extendbranchsigns.(0)(CAst.make@@IntroNamingNamegen.IntroAnonymous)l)elsenameselsenames|IntroOrPatternll->ifnot(Int.equaln(List.lengthll))thenifInt.equaln1thenletp1=List.count(funx->x)branchsigns.(0)inletp2=List.lengthbranchsigns.(0)inerr1'p1p2elseerrnn;namesletget_and_check_or_and_pattern_gen?loccheck_andnamesbranchsigns=letnames=check_or_and_pattern_size?loccheck_andnamesbranchsignsinmatchnameswith|IntroAndPatternl->[|l|]|IntroOrPatternl->Array.of_listlletget_and_check_or_and_pattern?loc=get_and_check_or_and_pattern_gen?loctrueletcompute_induction_namescheck_andbranchletsigns=function|None->Array.make(Array.lengthbranchletsigns)[]|Some{CAst.loc;v=names}->letnames=fix_empty_or_and_pattern(Array.lengthbranchletsigns)namesinget_and_check_or_and_pattern_gencheck_and?locnamesbranchletsigns(* Compute the let-in signature of case analysis or standard induction scheme *)letcompute_constructor_signaturesenv~rec_flag((_,kasity),u)=letrecanalreccrecargs=matchc,recargswith|RelDecl.LocalAssum_::c,recarg::rest->letrest=analreccrestinbeginmatchDeclareops.dest_recargrecargwith|Norec|Nested_->true::rest|Mrec(_,j)->ifrec_flag&&Int.equaljkthentrue::true::restelsetrue::restend|RelDecl.LocalDef_::c,rest->false::analreccrest|[],[]->[]|_->anomaly(Pp.str"compute_constructor_signatures.")inlet(mib,mip)=Inductive.lookup_mind_specifenvityinletmap(ctx,_)=List.skipn(Context.Rel.lengthmib.mind_params_ctxt)(List.revctx)inletlc=Array.mapmapmip.mind_nf_lcinletlrecargs=Declareops.dest_subtermsmip.mind_recargsinArray.map2analreclclrecargsopenProofviewopenProofview.NotationsopenTacmachlettclIDTAC=tclUNIT()lettclTHENt1t2=t1<*>t2lettclFAILn?infolvlmsg=letinfo=matchinfowith(* If the backtrace points here it means the caller didn't save
the backtrace correctly *)|None->Exninfo.reify()|Someinfo->infointclZERO~info(FailError(lvl,lazymsg))lettclFAIL?infomsg=tclFAILn?info0msglettclZEROMSG?info?locmsg=letinfo=matchinfowith(* If the backtrace points here it means the caller didn't save
the backtrace correctly *)|None->Exninfo.reify()|Someinfo->infoinletinfo=matchlocwith|None->info|Someloc->Loc.add_locinfolocinleterr=UserErrormsgintclZERO~infoerrletcatch_failerrore=trycatch_failerrore;tclUNIT()withewhenCErrors.noncriticale->let_,info=Exninfo.captureeintclZERO~infoe(* spiwack: I chose to give the Ltac + the same semantics as
[Proofview.tclOR], however, for consistency with the or-else
tactical, we may consider wrapping the first argument with
[tclPROGRESS]. It strikes me as a bad idea, but consistency can be
considered valuable. *)lettclORt1t2=tclINDEPENDENTbeginProofview.tclORt1beginfune->catch_failerrore<*>t2endendlettclORDt1t2=tclINDEPENDENTbeginProofview.tclORt1beginfune->catch_failerrore<*>t2()endendlettclONCE=Proofview.tclONCElettclEXACTLY_ONCEt=Proofview.tclEXACTLY_ONCE(FailError(0,lazy(assertfalse)))tlettclIFCATCHtttte=tclINDEPENDENTbeginProofview.tclIFCATCHttt(fune->catch_failerrore<*>te())endlettclORELSE0t1t2=tclINDEPENDENTbegintclORELSEt1beginfune->catch_failerrore<*>t2endendlettclORELSE0Lt1t2=tclINDEPENDENTLbegintclORELSEt1beginfune->catch_failerrore<*>t2endendlettclORELSEt1t2=tclORELSE0(tclPROGRESSt1)t2lettclTHENS3PARTSt1l1repeatl2=tclINDEPENDENTbegint1<*>Proofview.tclORELSE(* converts the [SizeMismatch] error into an ltac error *)begintclEXTEND(Array.to_listl1)repeat(Array.to_listl2)endbeginfunction(e,info)->matchewith|SizeMismatch(i,_)->leterrmsg=str"Incorrect number of goals"++spc()++str"(expected "++inti++str(String.plurali" tactic")++str")"intclFAILerrmsg|reraise->tclZERO~inforeraiseendendlettclTHENSFIRSTnt1lrepeat=tclTHENS3PARTSt1lrepeat[||]lettclTHENFIRSTnt1l=tclTHENSFIRSTnt1l(tclUNIT())lettclTHENFIRSTt1t2=tclTHENFIRSTnt1[|t2|]lettclBINDFIRSTt1t2=t1>>=funans->Proofview.Unsafe.tclGETGOALS>>=fungls->matchglswith|[]->tclFAIL(str"Expect at least one goal.")|hd::tl->Proofview.Unsafe.tclSETGOALS[hd]<*>t2ans>>=funans->Proofview.Unsafe.tclNEWGOALStl<*>Proofview.tclUNITanslettclTHENSLASTnt1repeatl=tclTHENS3PARTSt1[||]repeatllettclTHENLASTnt1l=tclTHENS3PARTSt1[||](tclUNIT())llettclTHENLASTt1t2=tclTHENLASTnt1[|t2|]letoption_of_failurefx=trySome(fx)withFailure_->NonelettclBINDLASTt1t2=t1>>=funans->Proofview.Unsafe.tclGETGOALS>>=fungls->matchoption_of_failureList.sep_lastglswith|None->tclFAIL(str"Expect at least one goal.")|Some(last,firstn)->Proofview.Unsafe.tclSETGOALS[last]<*>t2ans>>=funans->Proofview.Unsafe.tclGETGOALS>>=funnewgls->tclEVARMAP>>=funsigma->letfirstn=Proofview.Unsafe.undefinedsigmafirstninProofview.Unsafe.tclSETGOALS(firstn@newgls)<*>Proofview.tclUNITanslettclTHENStl=tclINDEPENDENTbegint<*>Proofview.tclORELSE(* converts the [SizeMismatch] error into an ltac error *)begintclDISPATCHlendbeginfunction(e,info)->matchewith|SizeMismatch(i,_)->leterrmsg=str"Incorrect number of goals"++spc()++str"(expected "++inti++str(String.plurali" tactic")++str")"intclFAILerrmsg|reraise->tclZERO~inforeraiseendendlettclTHENLISTl=List.fold_lefttclTHEN(tclUNIT())l(* [tclMAP f [x1..xn]] builds [(f x1);(f x2);...(f xn)] *)lettclMAPtacfunl=List.fold_right(funx->(tclTHEN(tacfunx)))l(tclUNIT())lettclTRYt=tclORELSE0t(tclUNIT())lettclTRYbt=tclORELSE0L(t<*>tclUNITtrue)(tclUNITfalse)lettclIFTHENELSEt1t2t3=tclINDEPENDENTbeginProofview.tclIFCATCHt1(fun()->t2)(fun(e,info)->Proofview.tclORELSEt3(fune'->tclZERO~infoe))endlettclIFTHENSVELSEt1at3=Proofview.tclIFCATCHt1(fun()->tclDISPATCH(Array.to_lista))(fun_->t3)lettclIFTHENFIRSTELSEt1t2t3=Proofview.tclIFCATCHt1(fun()->tclEXTEND[t2](tclUNIT())[])(fun_->t3)lettclIFTHENTRYELSEMUSTt1t2=tclIFTHENELSEt1(tclTRYt2)t2lettclIFTHENFIRSTTRYELSEMUSTt1t2=tclIFTHENFIRSTELSEt1(tclTRYt2)t2(* Try the first tactic that does not fail in a list of tactics *)letrectclFIRST=function|[]->letinfo=Exninfo.reify()intclZEROMSG~info(str"No applicable tactic.")|t::rest->tclORELSE0t(tclFIRSTrest)letrectclFIRST_PROGRESS_ONtac=function|[]->tclFAIL(str"No applicable tactic")|[a]->taca(* so that returned failure is the one from last item *)|a::tl->tclORELSE(taca)(tclFIRST_PROGRESS_ONtactl)letrectclDOnt=ifn<0thenletinfo=Exninfo.reify()intclZEROMSG~info(str"Wrong argument : Do needs a positive integer.")elseifn=0thentclUNIT()elseifn=1thentelse(* Thunk to avoid stack overflow with large n *)tclTHENt(tclUNIT()>>=fun()->(tclDO(n-1)t))letrectclREPEAT0t=tclINDEPENDENTbeginProofview.tclIFCATCHt(fun()->tclCHECKINTERRUPT<*>tclREPEAT0t)(fune->catch_failerrore<*>tclUNIT())endlettclREPEATt=tclREPEAT0(tclPROGRESSt)letrectclREPEAT_MAIN0t=Proofview.tclIFCATCHt(fun()->tclTRYFOCUS11(tclREPEAT_MAIN0t))(fune->catch_failerrore<*>tclUNIT())lettclREPEAT_MAINt=tclREPEAT_MAIN0(tclPROGRESSt)lettclCOMPLETEt=t>>=funres->(tclINDEPENDENT(letinfo=Exninfo.reify()intclZEROMSG~info(str"Proof is not complete.")))<*>tclUNITres(* Try the first that solves the current goal *)lettclSOLVEtacl=tclFIRST(List.maptclCOMPLETEtacl)lettclPROGRESSt=Proofview.tclINDEPENDENT(Proofview.tclPROGRESSt)(* Check that holes in arguments have been resolved *)letcheck_evarsenvsigmaextsigmaorigsigma=(* origsigma ⊆ extsigma ⊆ sigma *)letreachable=lazy(Evarutil.reachable_from_evarssigma(Evar.Map.domain(Evd.undefined_maporigsigma)))inletrecis_undefined_up_to_restrictionsigmaevk=ifEvd.memorigsigmaevkthenNoneelseletEvarInfoevi=Evd.findsigmaevkinmatchEvd.evar_bodyeviwith|Evd.Evar_empty->Some(evk,Evd.EvarInfoevi)|Evd.Evar_definedc->matchConstr.kind(EConstr.Unsafe.to_constrc)with|Evar(evk,l)->is_undefined_up_to_restrictionsigmaevk|_->(* We make the assumption that there is no way to refine an
evar remaining after typing from the initial term given to
apply/elim and co tactics, is it correct? *)NoneinifEvd.undefined_mapextsigma==Evd.undefined_maporigsigmathen()elseletrest=Evd.fold_undefined(funevkeviacc->matchis_undefined_up_to_restrictionsigmaevkwith|Some(evk',evi)->(* If [evk'] descends from [evk] which descends itself from
an originally undefined evar in [origsigma], it is a not
a fresh undefined hole from [sigma]. *)ifEvar.Set.memevk(Lazy.forcereachable)thenaccelse(evk',evi)::acc|_->acc)extsigma[]inmatchrestwith|[]->()|(evk,EvarInfoevi)::_->let(loc,_)=Evd.evar_sourceeviinPretype_errors.error_unsolvable_implicit?locenvsigmaevkNonelettclMAPDELAYEDWITHHOLESaccept_unresolved_holesltac=letrecaux=function|[]->tclUNIT()|x::l->Proofview.Goal.enterbeginfungl->letenv=Proofview.Goal.envglinletsigma_initial=Proofview.Goal.sigmaglinlet(sigma,x)=xenvsigma_initialinProofview.Unsafe.tclEVARSsigma<*>tacx>>=fun()->auxl>>=fun()->ifaccept_unresolved_holesthentclUNIT()elsetclEVARMAP>>=funsigma_final->trylet()=check_evarsenvsigma_finalsigmasigma_initialintclUNIT()withewhenCErrors.noncriticale->lete,info=Exninfo.captureeintclZERO~infoeendinauxl(* The following is basically
tclMAPDELAYEDWITHHOLES accept_unresolved_holes [fun _ _ -> (sigma,())] (fun () -> tac)
but with value not necessarily in unit *)lettclWITHHOLESaccept_unresolved_holestacsigma=tclEVARMAP>>=funsigma_initial->ifsigma==sigma_initialthentacelseletcheck_evars_ifx=ifnotaccept_unresolved_holesthentclEVARMAP>>=funsigma_final->tclENV>>=funenv->trylet()=check_evarsenvsigma_finalsigmasigma_initialintclUNITxwithewhenCErrors.noncriticale->lete,info=Exninfo.captureeintclZERO~infoeelsetclUNITxinProofview.Unsafe.tclEVARSsigma<*>tac>>=check_evars_iflettclDELAYEDWITHHOLEScheckxtac=Proofview.Goal.enterbeginfungl->letenv=Proofview.Goal.envglinletsigma=Proofview.Goal.sigmaglinlet(sigma,x)=xenvsigmaintclWITHHOLEScheck(tacx)sigmaendlettclTIMEOUTnt=Proofview.tclOR(Proofview.tclTIMEOUTnt)beginfunction(e,info)->matchewith|Logic_monad.Tac_Timeoutase->letinfo=Exninfo.reify()inProofview.tclZERO~info(FailError(0,lazy(CErrors.printe)))|e->Proofview.tclZERO~infoeendlettclTIMEst=Proofview.tclTIMEstletnthDeclmgl=lethyps=Proofview.Goal.hypsglintryList.nthhyps(m-1)withFailure_->CErrors.user_errPp.(str"No such assumption.")letnLastDeclsgln=tryList.firstnn(Proofview.Goal.hypsgl)withFailure_->CErrors.user_errPp.(str"Not enough hypotheses in the goal.")letnthHypIdmgl=(* We only use [id] *)nthDeclmgl|>NamedDecl.get_idletnthHypmgl=mkVar(nthHypIdmgl)letonNthHypIdmtac=Proofview.Goal.enterbeginfungl->tac(nthHypIdmgl)endletonNthHypmtac=Proofview.Goal.enterbeginfungl->tac(nthHypmgl)endletonLastHypId=onNthHypId1letonLastHyp=onNthHyp1letonNthDeclmtac=Proofview.Goal.enterbeginfungl->Proofview.tclUNIT(nthDeclmgl)>>=tacendletonLastDecl=onNthDecl1letnLastHypsIdgln=List.map(NamedDecl.get_id)(nLastDeclsgln)letnLastHypsgln=List.mapmkVar(nLastHypsIdgln)letifOnHyppredtac1tac2id=Proofview.Goal.enterbeginfungl->lettyp=Tacmach.pf_get_hyp_typidglinifpf_applypredgl(id,typ)thentac1idelsetac2idendletonHypsfindtac=Proofview.Goal.enterbeginfungl->tac(findgl)endletonNLastDeclsntac=onHyps(fungl->nLastDeclsgln)tacletonNLastHypsIdntac=onHyps(fungl->nLastHypsIdgln)tacletonNLastHypsntac=onHyps(fungl->nLastHypsgln)tacletafterHypidtac=Proofview.Goal.enterbeginfungl->lethyps=Proofview.Goal.hypsglinletrem,_=List.split_when(NamedDecl.get_id%>Id.equalid)hypsintacremendletfullGoalgl=lethyps=Tacmach.pf_ids_of_hypsglinNone::List.mapOption.makehypslettryAllHypstac=Proofview.Goal.enterbeginfungl->lethyps=Tacmach.pf_ids_of_hypsglintclFIRST_PROGRESS_ONtachypsendlettryAllHypsAndConcltac=Proofview.Goal.enterbeginfungl->tclFIRST_PROGRESS_ONtac(fullGoalgl)endletonClausetaccl=Proofview.Goal.enterbeginfungl->lethyps=Tacmach.pf_ids_of_hypsglintclMAPtac(Locusops.simple_clause_of(fun()->hyps)cl)endletfullGoalgl=None::List.mapOption.make(Tacmach.pf_ids_of_hypsgl)letonAllHypstac=Proofview.Goal.enterbeginfungl->tclMAPtac(Tacmach.pf_ids_of_hypsgl)endletonAllHypsAndConcltac=Proofview.Goal.enterbeginfungl->tclMAPtac(fullGoalgl)endletelimination_sort_of_goalgl=(* Retyping will expand evars anyway. *)letc=Proofview.Goal.conclglinpf_applyRetyping.get_sort_family_ofglcletelimination_sort_of_hypidgl=(* Retyping will expand evars anyway. *)letc=pf_get_hyp_typidglinpf_applyRetyping.get_sort_family_ofglcletelimination_sort_of_clauseidgl=matchidwith|None->elimination_sort_of_goalgl|Someid->elimination_sort_of_hypidglletpf_constr_of_globalref=Proofview.tclEVARMAP>>=funsigma->Proofview.tclENV>>=funenv->let(sigma,c)=Evd.fresh_globalenvsigmarefinProofview.Unsafe.tclEVARSsigma<*>Proofview.tclUNITclettclTYPEOFTHEN?refreshctac=Proofview.Goal.enter(fungl->letenv=Proofview.Goal.envglinletsigma=Proofview.Goal.sigmaglinlet(sigma,t)=Typing.type_of?refreshenvsigmacinProofview.Unsafe.tclEVARSsigma<*>tacsigmat)lettclSELECT=Goal_select.tclSELECT