Ltac rename_tm_Typing x0 K2 :=
let M1 := fresh in
let K1 := fresh in
match goal with
[ H0 : AnnTyping ([(?x, Tm ?A)] ++ ?G) (open_tm_wrt_tm ?B (a_Var_f ?x)) ?C |- _]
=> assert (AC : AnnCtx ([(x, Tm A)] ++ G)); eauto with ctx_wff;
inversion AC; subst;
assert (TV : AnnTyping ([(x0,Tm A)] ++ G) (a_Var_f x0) A); eauto;
assert (CTX : AnnCtx ([(x,Tm A)] ++ [(x0, Tm A)] ++ G));
[ match goal with
[ H7 : AnnTyping G A a_Star |- _] =>
econstructor; auto;
pose M1 := (AnnTyping_weakening H7 [(x0,Tm A)] nil G);
simpl_env in M1; eapply M1; eauto end |
pose K1 := AnnTyping_weakening H0 [(x0,Tm A)] [(x, Tm A)] G eq_refl CTX;
clearbody K1;
pose K2 := (first ann_tm_substitution_mutual) _ _ _ K1 _ _ _ TV nil x eq_refl;
clearbody K2; simpl_env in K2;
repeat rewrite tm_subst_tm_tm_open_tm_wrt_tm in K2; auto;
rewrite tm_subst_tm_tm_var in K2;
rewrite tm_subst_tm_tm_fresh_eq in K2]
end.

Lemma AnnDefEq_tm_swap : forall x1 x G A1 D g2 b1 b2,
x1 `notin` fv_tm_tm_co g2 \u fv_tm_tm_tm b1 \u fv_tm_tm_tm b2
-> x `notin` dom G \u {{ x1 }}
-> AnnDefEq ([(x1, Tm A1)] ++ G) D  (open_co_wrt_tm g2 (a_Var_f x1))
(open_tm_wrt_tm b1 (a_Var_f x1)) (open_tm_wrt_tm b2 (a_Var_f x1))
-> AnnDefEq ([(x, Tm A1)] ++ G) D  (open_co_wrt_tm g2 (a_Var_f x))
(open_tm_wrt_tm b1 (a_Var_f x)) (open_tm_wrt_tm b2 (a_Var_f x)).