123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128(************************************************************************)(* * 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) *)(************************************************************************)openSortsopenNamesopenConstropenUnivtypeuniv_length_mismatch={actual:int;expect:int;}(* Due to an OCaml bug ocaml/ocaml#10027 inlining this record will cause
compliation with -rectypes to crash. *)exceptionUniverseLengthMismatchofuniv_length_mismatchlet()=CErrors.register_handler(function|UniverseLengthMismatch{actual;expect}->SomePp.(str"Universe instance length is "++intactual++str" but should be "++intexpect++str".")|_->None)(* Generator of levels *)letnew_univ_id=letcnt=ref0infun()->incrcnt;!cntletnew_univ_global()=lets=ifFlags.async_proofs_is_worker()then!Flags.async_proofs_worker_idelse""inUniv.UGlobal.make(Global.current_dirpath())s(new_univ_id())letfresh_level()=Univ.Level.make(new_univ_global())letnew_sort_id=letcnt=ref0infun()->incrcnt;!cntletnew_sort_global()=lets=ifFlags.async_proofs_is_worker()then!Flags.async_proofs_worker_idelse""inSorts.QVar.makes(new_sort_id())letfresh_instanceauctx=letinst=Array.init(AbstractContext.sizeauctx)(fun_->fresh_level())inletctx=Array.fold_rightLevel.Set.addinstLevel.Set.emptyinletinst=Instance.of_arrayinstininst,(ctx,AbstractContext.instantiateinstauctx)letexisting_instance?locauctxinst=let()=letactual=Array.length(Instance.to_arrayinst)andexpect=AbstractContext.sizeauctxinifnot(Int.equalactualexpect)thenLoc.raise?loc(UniverseLengthMismatch{actual;expect})else()ininst,(Level.Set.empty,AbstractContext.instantiateinstauctx)letfresh_instance_from?locctx=function|Someinst->existing_instance?locctxinst|None->fresh_instancectx(** Fresh universe polymorphic construction *)letfresh_global_instance?loc?namesenvgr=letauctx=Environ.universes_of_globalenvgrinletu,ctx=fresh_instance_from?locauctxnamesinu,ctxletfresh_constant_instanceenvc=letu,ctx=fresh_global_instanceenv(GlobRef.ConstRefc)in(c,u),ctxletfresh_inductive_instanceenvind=letu,ctx=fresh_global_instanceenv(GlobRef.IndRefind)in(ind,u),ctxletfresh_constructor_instanceenvc=letu,ctx=fresh_global_instanceenv(GlobRef.ConstructRefc)in(c,u),ctxletfresh_array_instanceenv=letauctx=CPrimitives.typ_univsCPrimitives.PT_arrayinletu,ctx=fresh_instance_fromauctxNoneinu,ctxletfresh_global_instance?loc?namesenvgr=letu,ctx=fresh_global_instance?loc?namesenvgrinmkRef(gr,u),ctxletconstr_of_monomorphic_globalenvgr=ifnot(Environ.is_polymorphicenvgr)thenfst(fresh_global_instanceenvgr)elseCErrors.user_errPp.(str"globalization of polymorphic reference "++Nametab.pr_global_envId.Set.emptygr++str" would forget universes.")letfresh_sort_in_family=function|InSProp->Sorts.sprop,ContextSet.empty|InProp->Sorts.prop,ContextSet.empty|InSet->Sorts.set,ContextSet.empty|InType|InQSort(* Treat as Type *)->letu=fresh_level()insort_of_univ(Univ.Universe.makeu),ContextSet.singletonuletnew_global_univ()=letu=fresh_level()in(Univ.Universe.makeu,ContextSet.singletonu)letfresh_universe_context_set_instancectx=ifContextSet.is_emptyctxthenLevel.Map.empty,ctxelselet(univs,cst)=ContextSet.levelsctx,ContextSet.constraintsctxinletunivs',subst=Level.Set.fold(funu(univs',subst)->letu'=fresh_level()in(Level.Set.addu'univs',Level.Map.adduu'subst))univs(Level.Set.empty,Level.Map.empty)inletcst'=subst_univs_level_constraintssubstcstinsubst,(univs',cst')