Inductive type_error : Set :=
| Under : term -> type_error -> type_error
| Expected_type : term -> term -> term -> type_error
| Topsort : sort -> type_error
| Db_error : nat -> type_error
| Lambda_topsort : term -> sort -> type_error
| Not_a_type : term -> term -> type_error
| Not_a_fun : term -> term -> type_error
| Apply_err : term -> term -> term -> term -> type_error.
(* Meaning of errors *)
Inductive expln : env -> type_error -> Prop :=
| Exp_under :
forall (e : env) (t : term) (err : type_error),
expln (Ax t :: e) err -> expln e (Under t err)
| Exp_exp_type :
forall (e : env) (m at_ et : term),
typ_ppal e m at_ ->
~ typ e m et ->
free_db (length e) et -> expln e (Expected_type m at_ et)
| Exp_topsort :
forall (e : env) (s : sort),
wf e -> (forall t : term, ~ typ e (Srt s) t) -> expln e (Topsort s)
| Exp_db :
forall (e : env) (n : nat),
wf e -> length e <= n -> expln e (Db_error n)
| Exp_lam_topsort :
forall (e : env) (m t : term) (s : sort),
typ_ppal (Ax t :: e) m (Srt s) ->
(forall u : term, ~ typ e (Srt s) u) ->
expln e (Lambda_topsort (Abs t m) s)
| Exp_type :
forall (e : env) (m t : term),
typ e m t ->
(forall s : sort, ~ typ e m (Srt s)) -> expln e (Not_a_type m t)
| Exp_fun :
forall (e : env) (m t : term),
typ e m t ->
(forall a b : term, ~ typ e m (Prod a b)) -> expln e (Not_a_fun m t)
| Exp_appl_err :
forall (e : env) (u v a b tv : term),
typ e u (Prod a b) ->
(forall c d : term,
typ e u (Prod c d) -> le_type e (Prod a b) (Prod c d)) ->
typ_ppal e v tv ->
~ typ e v a -> expln e (Apply_err u (Prod a b) v tv).
Hint Resolve Exp_under Exp_exp_type Exp_topsort Exp_db Exp_lam_topsort
Exp_type Exp_fun Exp_appl_err: pts.
simple induction 1; intros; auto with pts.
inversion_clear H1.
apply typ_wf with (1 := H2).
apply typ_wf with (1 := pp_ok H0).
cut (wf (Ax t :: e0)); intros.
apply inv_wf with (1 := H2).
apply typ_wf with (1 := pp_ok H0).
apply typ_wf with (1 := H0).
apply typ_wf with (1 := H0).
apply typ_wf with (1 := H0).
Qed.
Inductive inf_error : term -> type_error -> Prop :=
| Infe_subt :
forall (m n : term) (err : type_error),
subt_nobind m n -> inf_error m err -> inf_error n err
| Infe_under :
forall (m n T : term) (err : type_error),
subt_bind T m n -> inf_error m err -> inf_error n (Under T err)
| Infe_topsort : forall s : sort, inf_error (Srt s) (Topsort s)
| Infe_db : forall n : nat, inf_error (Ref n) (Db_error n)
| Infe_lam_topsort :
forall (M T : term) (s : sort),
inf_error (Abs T M) (Lambda_topsort (Abs T M) s)
| Infe_type_abs :
forall m n t : term, inf_error (Abs m n) (Not_a_type m t)
| Infe_fun : forall m n t : term, inf_error (App m n) (Not_a_fun m t)
| Infe_appl_err :
forall m n tf ta : term, inf_error (App m n) (Apply_err m tf n ta)
| Infe_type_prod_l :
forall m n t : term, inf_error (Prod m n) (Not_a_type m t)
| Infe_type_prod_r :
forall m n t : term, inf_error (Prod m n) (Under m (Not_a_type n t)).
Hint Resolve Infe_topsort Infe_db Infe_lam_topsort Infe_type_abs Infe_fun
Infe_appl_err Infe_type_prod_l Infe_type_prod_r: pts.

Lemma inf_no_exp :
forall a b c d : term, ~ inf_error a (Expected_type b c d).