@@ -712,4 +712,157 @@ Definition fold_term {Acc} (f : Acc -> term -> Acc) (acc : Acc) (t : term) : Acc
712712
713713(** Monadic variant of [fold_term]. *)
714714Definition fold_termM {M} `{Monad M} {Acc} (f : Acc -> term -> M Acc) (acc : Acc) (t : term) : M Acc :=
715- @fold_term_with_bindersM M _ Acc unit tt (fun _ _ => ret tt) (fun _ => f) acc t.
715+ @fold_term_with_bindersM M _ Acc unit tt (fun _ _ => ret tt) (fun _ => f) acc t.
716+
717+
718+ (** * 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+
811+ Definition rebuild_case_predicate_ctx_with_context (Σ : global_env) ind (p : predicate term) : context :=
812+ match lookup_ind_decl Σ (inductive_mind ind) (inductive_ind ind) with
813+ | None => []
814+ | Some (mib, oib) => case_predicate_context ind mib oib p
815+ end .
816+
817+ Definition map_context_with_context (f : context -> term -> term) (c : context) Γ : context :=
818+ fold_left (fun acc decl => map_decl (f (Γ ,,, acc)) decl :: acc) (List.rev c) [].
819+
820+ Definition map_predicate_with_context (Σ : global_env) (f : context -> term -> term) Γ ind (p : predicate term) :=
821+ let pctx := rebuild_case_predicate_ctx_with_context Σ ind p in
822+ let Γparams := map_context_with_context f pctx Γ in
823+ {| pparams := map (f Γ) p.(pparams);
824+ puinst := p.(puinst);
825+ pcontext := p.(pcontext);
826+ preturn := f (Γparams ++ Γ) (preturn p) |}.
827+
828+ Definition rebuild_case_branch_ctx_with_context Σ ind i p br :=
829+ match lookup_constructor_decl Σ (inductive_mind ind) (inductive_ind ind) i with
830+ | None => []
831+ | Some (mib, cdecl) => case_branch_context ind mib cdecl p br
832+ end .
833+
834+ Definition map_case_branch_with_context Σ ind i (f : context -> term -> term) Γ p br :=
835+ let ctx := rebuild_case_branch_ctx_with_context Σ ind i p br in
836+ map_branch (f (Γ ,,, ctx)) br.
837+
838+ Definition map_term_with_context Σ (f : context -> term -> term) Γ (t : term) : term :=
839+ match t with
840+ | tRel i => t
841+ | tEvar ev args => tEvar ev (List.map (f Γ) args)
842+ | tLambda na T M =>
843+ let T' := f Γ T in
844+ tLambda na T' (f (Γ,, vass na T') M)
845+ | tApp u v => tApp (f Γ u) (List.map (f Γ) v)
846+ | tProd na A B =>
847+ let A' := f Γ A in
848+ tProd na A' (f (Γ ,, vass na A') B)
849+ | tCast c kind t => tCast (f Γ c) kind (f Γ t)
850+ | tLetIn na b t c =>
851+ let b' := f Γ b in
852+ let t' := f Γ t in
853+ tLetIn na b' t' (f (Γ ,, vdef na b' t') c)
854+ | tCase ci p c brs =>
855+ let p' := map_predicate_with_context Σ f Γ ci.(ci_ind) p in
856+ let brs' := mapi (fun i x => map_case_branch_with_context Σ ci.(ci_ind) i f Γ p' x) brs in
857+ tCase ci p' (f Γ c) brs'
858+ | tProj p c => tProj p (f Γ c)
859+ | tFix mfix idx =>
860+ let Γ' := fix_decls mfix ++ Γ in
861+ let mfix' := List.map (map_def (f Γ) (f Γ')) mfix in
862+ tFix mfix' idx
863+ | tCoFix mfix k =>
864+ let Γ' := fix_decls mfix ++ Γ in
865+ let mfix' := List.map (map_def (f Γ) (f Γ')) mfix in
866+ tCoFix mfix' k
867+ | x => x
868+ end .
0 commit comments