Inductive wf_type (e : env) : term -> Prop :=
    | wft_typed :
        forall (T : term) (s : sort), typ e T (Srt s) -> wf_type e T
    | wft_top : forall s : sort, wf_type e (Srt s).Inductive wf_type (e : env) : term -> Prop :=
    | wft_typed :
        forall (T : term) (s : sort), typ e T (Srt s) -> wf_type e T
    | wft_top : forall s : sort, wf_type e (Srt s).Inductive wf_type (e : env) : term -> Prop :=
    | wft_typed :
        forall (T : term) (s : sort), typ e T (Srt s) -> wf_type e T
    | wft_top : forall s : sort, wf_type e (Srt s).Definition le_type_wf : red_rule :=
    fun e => clos_refl_trans _ (fun x y => le_type e x y /\ wf_type e y).(*
  Lemma le_type_wf_le: (e:env)(x,y:term)(le_type_wf e x y)->(le_type e x y).
*)

  Definition inv_typ (e : env) (t T : term) (P : Prop) : Prop :=
    match t with
    | Srt s1 => forall s2 : sort, axiom s1 s2 -> le_type e (Srt s2) T -> P
    | Ref n =>
        forall x : decl,
        item x e n -> le_type e (lift (S n) (typ_of_decl x)) T -> P
    | Abs A M =>
        forall (s : sort) (U : term),
        typ (Ax A :: e) M U ->
        typ e (Prod A U) (Srt s) -> le_type e (Prod A U) T -> P
    | App u v =>
        forall Ur V : term,
        typ e v V -> typ e u (Prod V Ur) -> le_type e (subst v Ur) T -> P
    | Prod A B =>
        forall s1 s2 s3 : sort,
        rule s1 s2 s3 ->
        typ e A (Srt s1) ->
        typ (Ax A :: e) B (Srt s2) -> le_type e (Srt s3) T -> P
    end.

Theorem inversion_lemma : forall (e : env) (t T : term) (P : Prop), inv_typ e t T P -> typ e t T -> P.