123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145(************************************************************************)(* * 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) *)(************************************************************************)openUtilopenNamesopenUnivopenUVarsopenCookingmoduleNamedDecl=Context.Named.Declarationtypesection_entry=|SecDefinitionofConstant.t|SecInductiveofMutInd.ttype'at={prev:'atoption;(** Section surrounding the current one *)entries:section_entrylist;(** Global declarations introduced in the section *)context:Constr.named_context;(** Declarations local to the section, intended to be interleaved
with global declarations *)mono_universes:ContextSet.t;(** Global universes introduced in the section *)poly_universes:UContext.t;(** Universes local to the section *)all_poly_univs:Instance.t;(** All polymorphic universes, including from previous sections. *)has_poly_univs:bool;(** Are there polymorphic universes or constraints, including in previous sections. *)expand_info_map:expand_info;(** Tells how to re-instantiate global declarations when they are
generalized *)cooking_info_map:cooking_infoentry_map;custom:'a;}letrecdepthsec=1+matchsec.prevwithNone->0|Someprev->depthprevlethas_poly_univssec=sec.has_poly_univsletall_poly_univssec=sec.all_poly_univsletmap_customfsec={secwithcustom=fsec.custom}letadd_emapev(cmap,imap)=matchewith|SecDefinitioncon->(Cmap.addconvcmap,imap)|SecInductiveind->(cmap,Mindmap.addindvimap)letpush_local_universe_contextctxsec=ifUContext.is_emptyctxthensecelseletsctx=sec.poly_universesinletpoly_universes=UContext.unionsctxctxinletall_poly_univs=Instance.appendsec.all_poly_univs(UContext.instancectx)in{secwithpoly_universes;all_poly_univs;has_poly_univs=true}letis_polymorphic_univusec=let_,us=Instance.to_arraysec.all_poly_univsinArray.exists(funu'->Level.equaluu')usletpush_constraintsuctxsec=ifsec.has_poly_univs&&Constraints.exists(fun(l,_,r)->is_polymorphic_univlsec||is_polymorphic_univrsec)(snductx)thenCErrors.user_errPp.(str"Cannot add monomorphic constraints which refer to section polymorphic universes.");letuctx'=sec.mono_universesinletmono_universes=(ContextSet.unionuctxuctx')in{secwithmono_universes}letopen_section~customprev={prev;context=[];mono_universes=ContextSet.empty;poly_universes=UContext.empty;all_poly_univs=Option.cata(funsec->sec.all_poly_univs)Instance.emptyprev;has_poly_univs=Option.catahas_poly_univsfalseprev;entries=[];expand_info_map=(Cmap.empty,Mindmap.empty);cooking_info_map=(Cmap.empty,Mindmap.empty);custom=custom;}letclose_sectionsec=sec.prev,sec.entries,sec.mono_universes,sec.customletpush_localdsec={secwithcontext=d::sec.context}letextract_hypsvarsused=(* Only keep the part that is used by the declaration *)List.filter(fund->Id.Set.mem(NamedDecl.get_idd)used)varsletsegment_of_entryenveuctxsec=lethyps=matchewith|SecDefinitioncon->(Environ.lookup_constantconenv).Declarations.const_hyps|SecInductivemind->(Environ.lookup_mindmindenv).Declarations.mind_hypsinlethyps=Context.Named.to_varshypsin(* [sec.context] are the named hypotheses, [hyps] the subset that is
declared by the global *)letctx=extract_hypssec.contexthypsin(* Add recursive calls: projections are recursively referring to the
mind they belong to *)letrecursive=matchewith|SecDefinition_->None|SecInductiveind->SomeindinCooking.make_cooking_info~recursivesec.expand_info_mapctxuctxletpush_globalenv~polyesec=ifhas_poly_univssec&¬polythenCErrors.user_errPp.(str"Cannot add a universe monomorphic declaration when \
section polymorphic universes are present.")elseletcooking_info,abstr_inst_info=segment_of_entryenvesec.poly_universessecinletcooking_info_map=add_emapecooking_infosec.cooking_info_mapinletexpand_info_map=add_emapeabstr_inst_infosec.expand_info_mapin{secwithentries=e::sec.entries;expand_info_map;cooking_info_map}letsegment_of_constantconsec=Cmap.findcon(fstsec.cooking_info_map)letsegment_of_inductiveconsec=Mindmap.findcon(sndsec.cooking_info_map)letis_in_section_envgrsec=letopenGlobRefinmatchgrwith|VarRefid->letvars=sec.contextinList.exists(fundecl->Id.equalid(NamedDecl.get_iddecl))vars|ConstRefcon->Cmap.memcon(fstsec.expand_info_map)|IndRef(ind,_)|ConstructRef((ind,_),_)->Mindmap.memind(sndsec.expand_info_map)