@@ -716,14 +716,106 @@ Definition fold_termM {M} `{Monad M} {Acc} (f : Acc -> term -> M Acc) (acc : Acc
716716
717717
718718(** * Traversal functions with a context *)
719+
720+
721+
722+ Definition fix_decls (l : mfixpoint term) :=
723+ let fix aux acc ds :=
724+ match ds with
725+ | nil => acc
726+ | d :: ds => aux (vass d.(dname) d.(dtype) :: acc) ds
727+ end
728+ in aux [] l.
729+
730+ Section Lookups.
731+ Context (Σ : global_env).
732+
733+ Definition polymorphic_constraints u :=
734+ match u with
735+ | Monomorphic_ctx => ConstraintSet.empty
736+ | Polymorphic_ctx ctx => (AUContext.repr ctx).2.2
737+ end .
738+
739+ Definition lookup_constant_type cst u :=
740+ match lookup_env Σ cst with
741+ | Some (ConstantDecl {| cst_type := ty; cst_universes := uctx |}) =>
742+ Some (subst_instance u ty)
743+ | _ => None
744+ end .
745+
746+ Definition lookup_constant_type_cstrs cst u :=
747+ match lookup_env Σ cst with
748+ | Some (ConstantDecl {| cst_type := ty; cst_universes := uctx |}) =>
749+ let cstrs := polymorphic_constraints uctx in
750+ Some (subst_instance u ty, subst_instance_cstrs u cstrs)
751+ | _ => None
752+ end .
753+
754+ Definition lookup_ind_decl ind i :=
755+ match lookup_env Σ ind with
756+ | Some (InductiveDecl mdecl) =>
757+ match nth_error mdecl.(ind_bodies) i with
758+ | Some body => Some (mdecl, body)
759+ | None => None
760+ end
761+ | _ => None
762+ end .
763+
764+ Definition lookup_ind_type ind i (u : list Level.t) :=
765+ match lookup_ind_decl ind i with
766+ |None => None
767+ |Some res =>
768+ Some (subst_instance u (snd res).(ind_type))
769+ end .
770+
771+ Definition lookup_ind_type_cstrs ind i (u : list Level.t) :=
772+ match lookup_ind_decl ind i with
773+ |None => None
774+ |Some res =>
775+ let '(mib, body) := res in
776+ let uctx := mib.(ind_universes) in
777+ let cstrs := polymorphic_constraints uctx in
778+ Some (subst_instance u body.(ind_type), subst_instance_cstrs u cstrs)
779+ end .
780+
781+ Definition lookup_constructor_decl ind i k :=
782+ match lookup_ind_decl ind i with
783+ |None => None
784+ |Some res =>
785+ let '(mib, body) := res in
786+ match nth_error body.(ind_ctors) k with
787+ | Some cdecl => Some (mib, cdecl)
788+ | None => None
789+ end
790+ end .
791+
792+ Definition lookup_constructor_type ind i k u :=
793+ match lookup_constructor_decl ind i k with
794+ |None => None
795+ |Some res =>
796+ let '(mib, cdecl) := res in
797+ Some (subst0 (inds ind u mib.(ind_bodies)) (subst_instance u cdecl.(cstr_type)))
798+ end .
799+
800+ Definition lookup_constructor_type_cstrs ind i k u :=
801+ match lookup_constructor_decl ind i k with
802+ |None => None
803+ |Some res =>
804+ let '(mib, cdecl) := res in
805+ let cstrs := polymorphic_constraints mib.(ind_universes) in
806+ Some (subst0 (inds ind u mib.(ind_bodies)) (subst_instance u cdecl.(cstr_type)),
807+ subst_instance_cstrs u cstrs)
808+ end .
809+ End Lookups.
810+
719811Definition rebuild_case_predicate_ctx_with_context (Σ : global_env) ind (p : predicate term) : context :=
720812 match lookup_ind_decl Σ (inductive_mind ind) (inductive_ind ind) with
721- | TypeError _ => []
722- | Checked (mib, oib) => case_predicate_context ind mib oib p
813+ | None => []
814+ | Some (mib, oib) => case_predicate_context ind mib oib p
723815 end .
724816
725817Definition map_context_with_context (f : context -> term -> term) (c : context) Γ : context :=
726- fold_left (fun acc decl => map_decl (f (Γ ,,, acc)) decl :: acc) (List.rev c) [].
818+ fold_left (fun acc decl => map_decl (f (Γ ,,, acc)) decl :: acc) (List.rev c) [].
727819
728820Definition map_predicate_with_context (Σ : global_env) (f : context -> term -> term) Γ ind (p : predicate term) :=
729821 let pctx := rebuild_case_predicate_ctx_with_context Σ ind p in
@@ -735,8 +827,8 @@ Definition map_predicate_with_context (Σ : global_env) (f : context -> term ->
735827
736828Definition rebuild_case_branch_ctx_with_context Σ ind i p br :=
737829 match lookup_constructor_decl Σ (inductive_mind ind) (inductive_ind ind) i with
738- | TypeError _ => []
739- | Checked (mib, cdecl) => case_branch_context ind mib cdecl p br
830+ | None => []
831+ | Some (mib, cdecl) => case_branch_context ind mib cdecl p br
740832 end .
741833
742834Definition map_case_branch_with_context Σ ind i (f : context -> term -> term) Γ p br :=
0 commit comments