Require Export fintype. Require Export header_extensible.

Section ty.
Inductive ty (nty : ) : Type :=
  | var_ty : (fin) (nty) ty (nty)
  | top : ty (nty)
  | arr : ty (nty) ty (nty)
  | all : ty ((S) nty) ty (nty)
  | recty : list (prod ( ) (ty (nty))) ty (nty).

Lemma congr_top { mty : } : top (mty) = top (mty) .
Proof. congruence. Qed.

Lemma congr_arr { mty : } { : ty (mty) } { : ty (mty) } ( : = ) : arr (mty) = arr (mty) .
Proof. congruence. Qed.

Lemma congr_all { mty : } { : ty ((S) mty) } { : ty ((S) mty) } ( : = ) : all (mty) = all (mty) .
Proof. congruence. Qed.

Lemma congr_recty { mty : } { : list (prod ( ) (ty (mty))) } { : list (prod ( ) (ty (mty))) } ( : = ) : recty (mty) = recty (mty) .
Proof. congruence. Qed.

Definition upRen_ty_ty { m : } { n : } ( : (fin) (m) (fin) (n)) : (fin) ((S) (m)) (fin) ((S) (n)) :=
  (up_ren) .

Definition upRenList_ty_ty (p : ) { m : } { n : } ( : (fin) (m) (fin) (n)) : (fin) (p+ (m)) (fin) (p+ (n)) :=
  upRen_p p .

Fixpoint ren_ty { mty : } { nty : } (xity : (fin) (mty) (fin) (nty)) (s : ty (mty)) : ty (nty) :=
    match s with
    | var_ty (_) s (var_ty (nty)) (xity s)
    | top (_) top (nty)
    | arr (_) arr (nty) ((ren_ty xity) )
    | all (_) all (nty) ((ren_ty (upRen_ty_ty xity)) )
    | recty (_) recty (nty) ((list_map (prod_map ( x x) (ren_ty xity))) )
    end.

Definition up_ty_ty { m : } { nty : } ( : (fin) (m) ty (nty)) : (fin) ((S) (m)) ty ((S) nty) :=
  (scons) ((var_ty ((S) nty)) (var_zero)) ((funcomp) (ren_ty (shift)) ).

Definition upList_ty_ty (p : ) { m : } { nty : } ( : (fin) (m) ty (nty)) : (fin) (p+ (m)) ty (p+ nty) :=
  scons_p p ((funcomp) (var_ty (p+ nty)) (zero_p p)) ((funcomp) (ren_ty (shift_p p)) ).

Fixpoint subst_ty { mty : } { nty : } (sigmaty : (fin) (mty) ty (nty)) (s : ty (mty)) : ty (nty) :=
    match s with
    | var_ty (_) s sigmaty s
    | top (_) top (nty)
    | arr (_) arr (nty) ((subst_ty sigmaty) )
    | all (_) all (nty) ((subst_ty (up_ty_ty sigmaty)) )
    | recty (_) recty (nty) ((list_map (prod_map ( x x) (subst_ty sigmaty))) )
    end.

Definition upId_ty_ty { mty : } ( : (fin) (mty) ty (mty)) (Eq : x, x = (var_ty (mty)) x) : x, (up_ty_ty ) x = (var_ty ((S) mty)) x :=
   n match n with
  | Some fin_n (ap) (ren_ty (shift)) (Eq fin_n)
  | None eq_refl
  end.

Definition upIdList_ty_ty { p : } { mty : } ( : (fin) (mty) ty (mty)) (Eq : x, x = (var_ty (mty)) x) : x, (upList_ty_ty p ) x = (var_ty (p+ mty)) x :=
   n scons_p_eta (var_ty (p+ mty)) ( n (ap) (ren_ty (shift_p p)) (Eq n)) ( n eq_refl).

Fixpoint idSubst_ty { mty : } (sigmaty : (fin) (mty) ty (mty)) (Eqty : x, sigmaty x = (var_ty (mty)) x) (s : ty (mty)) : subst_ty sigmaty s = s :=
    match s with
    | var_ty (_) s Eqty s
    | top (_) congr_top
    | arr (_) congr_arr ((idSubst_ty sigmaty Eqty) )
    | all (_) congr_all ((idSubst_ty (up_ty_ty sigmaty) (upId_ty_ty (_) Eqty)) )
    | recty (_) congr_recty ((list_id (prod_id ( x (eq_refl) x) (idSubst_ty sigmaty Eqty))) )
    end.

Definition upExtRen_ty_ty { m : } { n : } ( : (fin) (m) (fin) (n)) ( : (fin) (m) (fin) (n)) (Eq : x, x = x) : x, (upRen_ty_ty ) x = (upRen_ty_ty ) x :=
   n match n with
  | Some fin_n (ap) (shift) (Eq fin_n)
  | None eq_refl
  end.

Definition upExtRen_list_ty_ty { p : } { m : } { n : } ( : (fin) (m) (fin) (n)) ( : (fin) (m) (fin) (n)) (Eq : x, x = x) : x, (upRenList_ty_ty p ) x = (upRenList_ty_ty p ) x :=
   n scons_p_congr ( n eq_refl) ( n (ap) (shift_p p) (Eq n)).

Fixpoint extRen_ty { mty : } { nty : } (xity : (fin) (mty) (fin) (nty)) (zetaty : (fin) (mty) (fin) (nty)) (Eqty : x, xity x = zetaty x) (s : ty (mty)) : ren_ty xity s = ren_ty zetaty s :=
    match s with
    | var_ty (_) s (ap) (var_ty (nty)) (Eqty s)
    | top (_) congr_top
    | arr (_) congr_arr ((extRen_ty xity zetaty Eqty) )
    | all (_) congr_all ((extRen_ty (upRen_ty_ty xity) (upRen_ty_ty zetaty) (upExtRen_ty_ty (_) (_) Eqty)) )
    | recty (_) congr_recty ((list_ext (prod_ext ( x (eq_refl) x) (extRen_ty xity zetaty Eqty))) )
    end.

Definition upExt_ty_ty { m : } { nty : } ( : (fin) (m) ty (nty)) ( : (fin) (m) ty (nty)) (Eq : x, x = x) : x, (up_ty_ty ) x = (up_ty_ty ) x :=
   n match n with
  | Some fin_n (ap) (ren_ty (shift)) (Eq fin_n)
  | None eq_refl
  end.

Definition upExt_list_ty_ty { p : } { m : } { nty : } ( : (fin) (m) ty (nty)) ( : (fin) (m) ty (nty)) (Eq : x, x = x) : x, (upList_ty_ty p ) x = (upList_ty_ty p ) x :=
   n scons_p_congr ( n eq_refl) ( n (ap) (ren_ty (shift_p p)) (Eq n)).

Fixpoint ext_ty { mty : } { nty : } (sigmaty : (fin) (mty) ty (nty)) (tauty : (fin) (mty) ty (nty)) (Eqty : x, sigmaty x = tauty x) (s : ty (mty)) : subst_ty sigmaty s = subst_ty tauty s :=
    match s with
    | var_ty (_) s Eqty s
    | top (_) congr_top
    | arr (_) congr_arr ((ext_ty sigmaty tauty Eqty) )
    | all (_) congr_all ((ext_ty (up_ty_ty sigmaty) (up_ty_ty tauty) (upExt_ty_ty (_) (_) Eqty)) )
    | recty (_) congr_recty ((list_ext (prod_ext ( x (eq_refl) x) (ext_ty sigmaty tauty Eqty))) )
    end.

Definition up_ren_ren_ty_ty { k : } { l : } { m : } ( : (fin) (k) (fin) (l)) ( : (fin) (l) (fin) (m)) ( : (fin) (k) (fin) (m)) (Eq : x, ((funcomp) ) x = x) : x, ((funcomp) (upRen_ty_ty ) (upRen_ty_ty )) x = (upRen_ty_ty ) x :=
  up_ren_ren Eq.

Definition up_ren_ren_list_ty_ty { p : } { k : } { l : } { m : } ( : (fin) (k) (fin) (l)) ( : (fin) (l) (fin) (m)) ( : (fin) (k) (fin) (m)) (Eq : x, ((funcomp) ) x = x) : x, ((funcomp) (upRenList_ty_ty p ) (upRenList_ty_ty p )) x = (upRenList_ty_ty p ) x :=
  up_ren_ren_p Eq.

Fixpoint compRenRen_ty { kty : } { lty : } { mty : } (xity : (fin) (mty) (fin) (kty)) (zetaty : (fin) (kty) (fin) (lty)) (rhoty : (fin) (mty) (fin) (lty)) (Eqty : x, ((funcomp) zetaty xity) x = rhoty x) (s : ty (mty)) : ren_ty zetaty (ren_ty xity s) = ren_ty rhoty s :=
    match s with
    | var_ty (_) s (ap) (var_ty (lty)) (Eqty s)
    | top (_) congr_top
    | arr (_) congr_arr ((compRenRen_ty xity zetaty rhoty Eqty) )
    | all (_) congr_all ((compRenRen_ty (upRen_ty_ty xity) (upRen_ty_ty zetaty) (upRen_ty_ty rhoty) (up_ren_ren (_) (_) (_) Eqty)) )
    | recty (_) congr_recty ((list_comp (prod_comp ( x (eq_refl) x) (compRenRen_ty xity zetaty rhoty Eqty))) )
    end.

Definition up_ren_subst_ty_ty { k : } { l : } { mty : } ( : (fin) (k) (fin) (l)) ( : (fin) (l) ty (mty)) ( : (fin) (k) ty (mty)) (Eq : x, ((funcomp) ) x = x) : x, ((funcomp) (up_ty_ty ) (upRen_ty_ty )) x = (up_ty_ty ) x :=
   n match n with
  | Some fin_n (ap) (ren_ty (shift)) (Eq fin_n)
  | None eq_refl
  end.

Definition up_ren_subst_list_ty_ty { p : } { k : } { l : } { mty : } ( : (fin) (k) (fin) (l)) ( : (fin) (l) ty (mty)) ( : (fin) (k) ty (mty)) (Eq : x, ((funcomp) ) x = x) : x, ((funcomp) (upList_ty_ty p ) (upRenList_ty_ty p )) x = (upList_ty_ty p ) x :=
   n (eq_trans) (scons_p_comp' (_) (_) (_) n) (scons_p_congr ( z scons_p_head' (_) (_) z) ( z (eq_trans) (scons_p_tail' (_) (_) ( z)) ((ap) (ren_ty (shift_p p)) (Eq z)))).

Fixpoint compRenSubst_ty { kty : } { lty : } { mty : } (xity : (fin) (mty) (fin) (kty)) (tauty : (fin) (kty) ty (lty)) (thetaty : (fin) (mty) ty (lty)) (Eqty : x, ((funcomp) tauty xity) x = thetaty x) (s : ty (mty)) : subst_ty tauty (ren_ty xity s) = subst_ty thetaty s :=
    match s with
    | var_ty (_) s Eqty s
    | top (_) congr_top
    | arr (_) congr_arr ((compRenSubst_ty xity tauty thetaty Eqty) )
    | all (_) congr_all ((compRenSubst_ty (upRen_ty_ty xity) (up_ty_ty tauty) (up_ty_ty thetaty) (up_ren_subst_ty_ty (_) (_) (_) Eqty)) )
    | recty (_) congr_recty ((list_comp (prod_comp ( x (eq_refl) x) (compRenSubst_ty xity tauty thetaty Eqty))) )
    end.

Definition up_subst_ren_ty_ty { k : } { lty : } { mty : } ( : (fin) (k) ty (lty)) (zetaty : (fin) (lty) (fin) (mty)) ( : (fin) (k) ty (mty)) (Eq : x, ((funcomp) (ren_ty zetaty) ) x = x) : x, ((funcomp) (ren_ty (upRen_ty_ty zetaty)) (up_ty_ty )) x = (up_ty_ty ) x :=
   n match n with
  | Some fin_n (eq_trans) (compRenRen_ty (shift) (upRen_ty_ty zetaty) ((funcomp) (shift) zetaty) ( x eq_refl) ( fin_n)) ((eq_trans) ((eq_sym) (compRenRen_ty zetaty (shift) ((funcomp) (shift) zetaty) ( x eq_refl) ( fin_n))) ((ap) (ren_ty (shift)) (Eq fin_n)))
  | None eq_refl
  end.

Definition up_subst_ren_list_ty_ty { p : } { k : } { lty : } { mty : } ( : (fin) (k) ty (lty)) (zetaty : (fin) (lty) (fin) (mty)) ( : (fin) (k) ty (mty)) (Eq : x, ((funcomp) (ren_ty zetaty) ) x = x) : x, ((funcomp) (ren_ty (upRenList_ty_ty p zetaty)) (upList_ty_ty p )) x = (upList_ty_ty p ) x :=
   n (eq_trans) (scons_p_comp' (_) (_) (_) n) (scons_p_congr ( x (ap) (var_ty (p+ mty)) (scons_p_head' (_) (_) x)) ( n (eq_trans) (compRenRen_ty (shift_p p) (upRenList_ty_ty p zetaty) ((funcomp) (shift_p p) zetaty) ( x scons_p_tail' (_) (_) x) ( n)) ((eq_trans) ((eq_sym) (compRenRen_ty zetaty (shift_p p) ((funcomp) (shift_p p) zetaty) ( x eq_refl) ( n))) ((ap) (ren_ty (shift_p p)) (Eq n))))).

Fixpoint compSubstRen_ty { kty : } { lty : } { mty : } (sigmaty : (fin) (mty) ty (kty)) (zetaty : (fin) (kty) (fin) (lty)) (thetaty : (fin) (mty) ty (lty)) (Eqty : x, ((funcomp) (ren_ty zetaty) sigmaty) x = thetaty x) (s : ty (mty)) : ren_ty zetaty (subst_ty sigmaty s) = subst_ty thetaty s :=
    match s with
    | var_ty (_) s Eqty s
    | top (_) congr_top
    | arr (_) congr_arr ((compSubstRen_ty sigmaty zetaty thetaty Eqty) )
    | all (_) congr_all ((compSubstRen_ty (up_ty_ty sigmaty) (upRen_ty_ty zetaty) (up_ty_ty thetaty) (up_subst_ren_ty_ty (_) (_) (_) Eqty)) )
    | recty (_) congr_recty ((list_comp (prod_comp ( x (eq_refl) x) (compSubstRen_ty sigmaty zetaty thetaty Eqty))) )
    end.

Definition up_subst_subst_ty_ty { k : } { lty : } { mty : } ( : (fin) (k) ty (lty)) (tauty : (fin) (lty) ty (mty)) ( : (fin) (k) ty (mty)) (Eq : x, ((funcomp) (subst_ty tauty) ) x = x) : x, ((funcomp) (subst_ty (up_ty_ty tauty)) (up_ty_ty )) x = (up_ty_ty ) x :=
   n match n with
  | Some fin_n (eq_trans) (compRenSubst_ty (shift) (up_ty_ty tauty) ((funcomp) (up_ty_ty tauty) (shift)) ( x eq_refl) ( fin_n)) ((eq_trans) ((eq_sym) (compSubstRen_ty tauty (shift) ((funcomp) (ren_ty (shift)) tauty) ( x eq_refl) ( fin_n))) ((ap) (ren_ty (shift)) (Eq fin_n)))
  | None eq_refl
  end.

Definition up_subst_subst_list_ty_ty { p : } { k : } { lty : } { mty : } ( : (fin) (k) ty (lty)) (tauty : (fin) (lty) ty (mty)) ( : (fin) (k) ty (mty)) (Eq : x, ((funcomp) (subst_ty tauty) ) x = x) : x, ((funcomp) (subst_ty (upList_ty_ty p tauty)) (upList_ty_ty p )) x = (upList_ty_ty p ) x :=
   n (eq_trans) (scons_p_comp' ((funcomp) (var_ty (p+ lty)) (zero_p p)) (_) (_) n) (scons_p_congr ( x scons_p_head' (_) ( z ren_ty (shift_p p) (_)) x) ( n (eq_trans) (compRenSubst_ty (shift_p p) (upList_ty_ty p tauty) ((funcomp) (upList_ty_ty p tauty) (shift_p p)) ( x eq_refl) ( n)) ((eq_trans) ((eq_sym) (compSubstRen_ty tauty (shift_p p) (_) ( x (eq_sym) (scons_p_tail' (_) (_) x)) ( n))) ((ap) (ren_ty (shift_p p)) (Eq n))))).

Fixpoint compSubstSubst_ty { kty : } { lty : } { mty : } (sigmaty : (fin) (mty) ty (kty)) (tauty : (fin) (kty) ty (lty)) (thetaty : (fin) (mty) ty (lty)) (Eqty : x, ((funcomp) (subst_ty tauty) sigmaty) x = thetaty x) (s : ty (mty)) : subst_ty tauty (subst_ty sigmaty s) = subst_ty thetaty s :=
    match s with
    | var_ty (_) s Eqty s
    | top (_) congr_top
    | arr (_) congr_arr ((compSubstSubst_ty sigmaty tauty thetaty Eqty) )
    | all (_) congr_all ((compSubstSubst_ty (up_ty_ty sigmaty) (up_ty_ty tauty) (up_ty_ty thetaty) (up_subst_subst_ty_ty (_) (_) (_) Eqty)) )
    | recty (_) congr_recty ((list_comp (prod_comp ( x (eq_refl) x) (compSubstSubst_ty sigmaty tauty thetaty Eqty))) )
    end.

Definition rinstInst_up_ty_ty { m : } { nty : } ( : (fin) (m) (fin) (nty)) ( : (fin) (m) ty (nty)) (Eq : x, ((funcomp) (var_ty (nty)) ) x = x) : x, ((funcomp) (var_ty ((S) nty)) (upRen_ty_ty )) x = (up_ty_ty ) x :=
   n match n with
  | Some fin_n (ap) (ren_ty (shift)) (Eq fin_n)
  | None eq_refl
  end.

Definition rinstInst_up_list_ty_ty { p : } { m : } { nty : } ( : (fin) (m) (fin) (nty)) ( : (fin) (m) ty (nty)) (Eq : x, ((funcomp) (var_ty (nty)) ) x = x) : x, ((funcomp) (var_ty (p+ nty)) (upRenList_ty_ty p )) x = (upList_ty_ty p ) x :=
   n (eq_trans) (scons_p_comp' (_) (_) (var_ty (p+ nty)) n) (scons_p_congr ( z eq_refl) ( n (ap) (ren_ty (shift_p p)) (Eq n))).

Fixpoint rinst_inst_ty { mty : } { nty : } (xity : (fin) (mty) (fin) (nty)) (sigmaty : (fin) (mty) ty (nty)) (Eqty : x, ((funcomp) (var_ty (nty)) xity) x = sigmaty x) (s : ty (mty)) : ren_ty xity s = subst_ty sigmaty s :=
    match s with
    | var_ty (_) s Eqty s
    | top (_) congr_top
    | arr (_) congr_arr ((rinst_inst_ty xity sigmaty Eqty) )
    | all (_) congr_all ((rinst_inst_ty (upRen_ty_ty xity) (up_ty_ty sigmaty) (rinstInst_up_ty_ty (_) (_) Eqty)) )
    | recty (_) congr_recty ((list_ext (prod_ext ( x (eq_refl) x) (rinst_inst_ty xity sigmaty Eqty))) )
    end.

Lemma rinstInst_ty { mty : } { nty : } (xity : (fin) (mty) (fin) (nty)) : ren_ty xity = subst_ty ((funcomp) (var_ty (nty)) xity) .
Proof. exact ((FunctionalExtensionality.functional_extensionality _ _ ) ( x rinst_inst_ty xity (_) ( n eq_refl) x)). Qed.

Lemma instId_ty { mty : } : subst_ty (var_ty (mty)) = id .
Proof. exact ((FunctionalExtensionality.functional_extensionality _ _ ) ( x idSubst_ty (var_ty (mty)) ( n eq_refl) ((id) x))). Qed.

Lemma rinstId_ty { mty : } : @ren_ty (mty) (mty) (id) = id .
Proof. exact ((eq_trans) (rinstInst_ty ((id) (_))) instId_ty). Qed.

Lemma varL_ty { mty : } { nty : } (sigmaty : (fin) (mty) ty (nty)) : (funcomp) (subst_ty sigmaty) (var_ty (mty)) = sigmaty .
Proof. exact ((FunctionalExtensionality.functional_extensionality _ _ ) ( x eq_refl)). Qed.

Lemma varLRen_ty { mty : } { nty : } (xity : (fin) (mty) (fin) (nty)) : (funcomp) (ren_ty xity) (var_ty (mty)) = (funcomp) (var_ty (nty)) xity .
Proof. exact ((FunctionalExtensionality.functional_extensionality _ _ ) ( x eq_refl)). Qed.

Lemma compComp_ty { kty : } { lty : } { mty : } (sigmaty : (fin) (mty) ty (kty)) (tauty : (fin) (kty) ty (lty)) (s : ty (mty)) : subst_ty tauty (subst_ty sigmaty s) = subst_ty ((funcomp) (subst_ty tauty) sigmaty) s .
Proof. exact (compSubstSubst_ty sigmaty tauty (_) ( n eq_refl) s). Qed.

Lemma compComp'_ty { kty : } { lty : } { mty : } (sigmaty : (fin) (mty) ty (kty)) (tauty : (fin) (kty) ty (lty)) : (funcomp) (subst_ty tauty) (subst_ty sigmaty) = subst_ty ((funcomp) (subst_ty tauty) sigmaty) .
Proof. exact ((FunctionalExtensionality.functional_extensionality _ _ ) ( n compComp_ty sigmaty tauty n)). Qed.

Lemma compRen_ty { kty : } { lty : } { mty : } (sigmaty : (fin) (mty) ty (kty)) (zetaty : (fin) (kty) (fin) (lty)) (s : ty (mty)) : ren_ty zetaty (subst_ty sigmaty s) = subst_ty ((funcomp) (ren_ty zetaty) sigmaty) s .
Proof. exact (compSubstRen_ty sigmaty zetaty (_) ( n eq_refl) s). Qed.

Lemma compRen'_ty { kty : } { lty : } { mty : } (sigmaty : (fin) (mty) ty (kty)) (zetaty : (fin) (kty) (fin) (lty)) : (funcomp) (ren_ty zetaty) (subst_ty sigmaty) = subst_ty ((funcomp) (ren_ty zetaty) sigmaty) .
Proof. exact ((FunctionalExtensionality.functional_extensionality _ _ ) ( n compRen_ty sigmaty zetaty n)). Qed.

Lemma renComp_ty { kty : } { lty : } { mty : } (xity : (fin) (mty) (fin) (kty)) (tauty : (fin) (kty) ty (lty)) (s : ty (mty)) : subst_ty tauty (ren_ty xity s) = subst_ty ((funcomp) tauty xity) s .
Proof. exact (compRenSubst_ty xity tauty (_) ( n eq_refl) s). Qed.

Lemma renComp'_ty { kty : } { lty : } { mty : } (xity : (fin) (mty) (fin) (kty)) (tauty : (fin) (kty) ty (lty)) : (funcomp) (subst_ty tauty) (ren_ty xity) = subst_ty ((funcomp) tauty xity) .
Proof. exact ((FunctionalExtensionality.functional_extensionality _ _ ) ( n renComp_ty xity tauty n)). Qed.

Lemma renRen_ty { kty : } { lty : } { mty : } (xity : (fin) (mty) (fin) (kty)) (zetaty : (fin) (kty) (fin) (lty)) (s : ty (mty)) : ren_ty zetaty (ren_ty xity s) = ren_ty ((funcomp) zetaty xity) s .
Proof. exact (compRenRen_ty xity zetaty (_) ( n eq_refl) s). Qed.

Lemma renRen'_ty { kty : } { lty : } { mty : } (xity : (fin) (mty) (fin) (kty)) (zetaty : (fin) (kty) (fin) (lty)) : (funcomp) (ren_ty zetaty) (ren_ty xity) = ren_ty ((funcomp) zetaty xity) .
Proof. exact ((FunctionalExtensionality.functional_extensionality _ _ ) ( n renRen_ty xity zetaty n)). Qed.

End ty.

Arguments var_ty {nty}.

Arguments top {nty}.

Arguments arr {nty}.

Arguments all {nty}.

Arguments recty {nty}.

Global Instance Subst_ty { mty : } { nty : } : Subst1 ((fin) (mty) ty (nty)) (ty (mty)) (ty (nty)) := @subst_ty (mty) (nty) .

Global Instance Ren_ty { mty : } { nty : } : Ren1 ((fin) (mty) (fin) (nty)) (ty (mty)) (ty (nty)) := @ren_ty (mty) (nty) .

Global Instance VarInstance_ty { mty : } : Var ((fin) (mty)) (ty (mty)) := @var_ty (mty) .

Notation "x '__ty'" := (var_ty x) (at level 5, format "x __ty") : subst_scope.

Notation "x '__ty'" := (@ids (_) (_) VarInstance_ty x) (at level 5, only printing, format "x __ty") : subst_scope.

Notation "'var'" := (var_ty) (only printing, at level 1) : subst_scope.

Class Up_ty X Y := up_ty : X Y.

Notation "↑__ty" := (up_ty) (only printing) : subst_scope.

Notation "↑__ty" := (up_ty_ty) (only printing) : subst_scope.

Global Instance Up_ty_ty { m : } { nty : } : Up_ty (_) (_) := @up_ty_ty (m) (nty) .

Notation "s [ sigmaty ]" := (subst_ty sigmaty s) (at level 7, left associativity, only printing) : subst_scope.

Notation "[ sigmaty ]" := (subst_ty sigmaty) (at level 1, left associativity, only printing) : fscope.

Notation "s ⟨ xity ⟩" := (ren_ty xity s) (at level 7, left associativity, only printing) : subst_scope.

Notation "⟨ xity ⟩" := (ren_ty xity) (at level 1, left associativity, only printing) : fscope.

Ltac auto_unfold := repeat unfold , , Subst1, Subst2, ids, , , Ren1, Ren2, Subst_ty, Ren_ty, VarInstance_ty.

Tactic Notation "auto_unfold" "in" "*" := repeat unfold , , Subst1, Subst2, ids, , , Ren1, Ren2, Subst_ty, Ren_ty, VarInstance_ty in *.

Ltac asimpl' := repeat first [progress rewrite ?instId_ty| progress rewrite ?compComp_ty| progress rewrite ?compComp'_ty| progress rewrite ?rinstId_ty| progress rewrite ?compRen_ty| progress rewrite ?compRen'_ty| progress rewrite ?renComp_ty| progress rewrite ?renComp'_ty| progress rewrite ?renRen_ty| progress rewrite ?renRen'_ty| progress rewrite ?varL_ty| progress rewrite ?varLRen_ty| progress (unfold up_ren, upRen_ty_ty, upRenList_ty_ty, up_ty_ty, upList_ty_ty)| progress (cbn [subst_ty ren_ty])| fsimpl].

Ltac asimpl := repeat try unfold_funcomp; auto_unfold in *; asimpl'; repeat try unfold_funcomp.

Tactic Notation "asimpl" "in" hyp(J) := revert J; asimpl; intros J.

Tactic Notation "auto_case" := auto_case (asimpl; cbn; eauto).

Tactic Notation "asimpl" "in" "*" := auto_unfold in *; repeat first [progress rewrite ?instId_ty in *| progress rewrite ?compComp_ty in *| progress rewrite ?compComp'_ty in *| progress rewrite ?rinstId_ty in *| progress rewrite ?compRen_ty in *| progress rewrite ?compRen'_ty in *| progress rewrite ?renComp_ty in *| progress rewrite ?renComp'_ty in *| progress rewrite ?renRen_ty in *| progress rewrite ?renRen'_ty in *| progress rewrite ?varL_ty in *| progress rewrite ?varLRen_ty in *| progress (unfold up_ren, upRen_ty_ty, upRenList_ty_ty, up_ty_ty, upList_ty_ty in *)| progress (cbn [subst_ty ren_ty] in *)| fsimpl in *].

Ltac substify := auto_unfold; try repeat (erewrite rinstInst_ty).

Ltac renamify := auto_unfold; try repeat (erewrite rinstInst_ty).