Ltac Inversion_typ T :=
    let INVTYP := fresh "INVTYP" in
    (generalize T; intro INVTYP; inversion INVTYP using inversion_lemma;
      unfold inv_typ in |- *; clear INVTYP; intros).

Theorem typ_sub : forall (g : env) (x : term) (d : decl), typ g x (typ_of_decl d) -> forall (e : env) (u U : term), typ e u U -> forall (f : env) (n : nat), sub_in_env g d x n e f -> wf f -> typ f (subst_rec x u n) (subst_rec x U n).