Require Export SfLib.

Require Export Arith.EqNat.
Require Export Arith.Le.

(*
type safety for minidot-like calculus:
- branding / undbranding example
- static / dynamic stp relation
- self types
*)

(*
TODO

- object creation without let
    (but we can already encode it)
- other static stp rules
    and, fun, mem, bot, top etc
- subsumption for has_type rules

- extend dyn stp rules with
  static tenv for self refs
- pack/unpack in subtyping

- multiple type members
- lower and upper bounds

*)

(* ############################################################ *)
(* Syntax *)
(* ############################################################ *)

Module DOT.

Definition id := nat.

Inductive ty : Type :=
  | TNoF   : ty (* marker for empty method list *)

  | TBool  : ty
  | TAnd   : ty -> ty -> ty
  | TFun   : id -> ty -> ty -> ty
  | TMem   : (option ty) -> ty
  | TSel   : id -> ty

  | TSelB  : id -> ty
  | TBind  : ty -> ty
.

Inductive tm : Type :=
  | ttrue : tm
  | tfalse : tm
  | tvar : id -> tm
  | tapp : id -> id -> tm -> tm (* a.f(x) *)
  | tabs : id -> ty -> list (id * dc) -> tm -> tm (* let f:T = x => y in z *)
  | tlet : id -> ty -> tm -> tm -> tm (* let x:T = y *)
with dc: Type :=
  | dfun : ty -> ty -> id -> tm -> dc (* def m:T = x => y *)
.

Fixpoint dc_type_and (dcs: list(nat*dc)) :=
  match dcs with
    | nil => TNoF
    | (n, dfun T1 T2 _ _)::dcs =>
      TAnd (TFun (length dcs) T1 T2)  (dc_type_and dcs)
  end.

Definition TObj p dcs := TAnd (TMem p) (dc_type_and dcs).
Definition TArrow p x y := TAnd (TMem p) (TAnd (TFun 0 x y) TNoF).

Inductive vl : Type :=
| vbool : bool -> vl
| vabs  : list (id*vl) -> id -> ty -> list (id * dc) -> vl. (* clos env f:T = x => y *)

Definition env := list (nat*vl).
Definition tenv := list (nat*ty).

Fixpoint index {X : Type} (n : nat)
               (l : list (nat * X)) : option X :=
  match l with
    | [] => None
    (* for now, ignore binding value n' !!! *)
    | (n',a) :: l'  => if beq_nat n (length l') then Some a else index n l'
  end.

(* LOCALLY NAMELESS *)

Inductive closed_rec: nat -> ty -> Prop :=
| cl_top: forall k,
    closed_rec k TNoF
| cl_bool: forall k,
    closed_rec k TBool
| cl_mem_n: forall k,
    closed_rec k (TMem None)
| cl_mem_s: forall k T1,
    closed_rec k T1 ->
    closed_rec k (TMem (Some T1))
| cl_fun: forall k m T1 T2,
    closed_rec k T1 ->
    closed_rec k T2 ->
    closed_rec k (TFun m T1 T2)
| cl_and: forall k T1 T2,
    closed_rec k T1 ->
    closed_rec k T2 ->
    closed_rec k (TAnd T1 T2)
| cl_bind: forall k T1,
    closed_rec (S k) T1 ->
    closed_rec k (TBind T1)
| cl_sel: forall k x,
    closed_rec k (TSel x)
| cl_selb: forall k i,
    k > i ->
    closed_rec k (TSelB i)
.

Hint Constructors closed_rec.

Definition closed j T := closed_rec j T.

Fixpoint open_rec (k: nat) (u: id) (T: ty) { struct T }: ty :=
  match T with
    | TSel x      => TSel x (* free var remains free. functional, so we can't check for conflict *)
    | TSelB i     => if beq_nat k i then TSel u else TSelB i
    | TBind T1    => TBind (open_rec (S k) u T1)
    | TNoF        => TNoF
    | TBool       => TBool
    | TAnd T1 T2  => TAnd (open_rec k u T1) (open_rec k u T2)
    | TMem (Some T1) => TMem (Some (open_rec k u T1))
    | TMem None   => TMem None
    | TFun m T1 T2  => TFun m (open_rec k u T1) (open_rec k u T2)
  end.

Definition open u T := open_rec 0 u T.

(* sanity check *)
Example open_ex1: open 9 (TBind (TAnd (TMem None) (TFun 0 (TSelB 1) (TSelB 0)))) =
                      (TBind (TAnd (TMem None) (TFun 0 (TSel  9) (TSelB 0)))).
Proof. compute. eauto. Qed.

Lemma closed_no_open: forall T x j,
  closed_rec j T ->
  T = open_rec j x T.
Proof.
  intros. induction H; intros; eauto;
  try solve [compute; compute in IHclosed_rec; rewrite <-IHclosed_rec; auto];
  try solve [compute; compute in IHclosed_rec1; compute in IHclosed_rec2; rewrite <-IHclosed_rec1; rewrite <-IHclosed_rec2; auto].

  Case "TSelB".
    unfold open_rec. assert (k <> i). omega.
    apply beq_nat_false_iff in H0.
    rewrite H0. auto.
Qed.

Lemma closed_upgrade: forall i j T,
 closed_rec i T ->
 j >= i ->
 closed_rec j T.
Proof.
 intros. generalize dependent j. induction H; intros; eauto.
 Case "TBind". econstructor. eapply IHclosed_rec. omega.
 Case "TSelB". econstructor. omega.
Qed.

Hint Unfold open.
Hint Unfold closed.

(* ############################################################ *)
(* Static properties: type assignment, subtyping, ... *)
(* ############################################################ *)

(* static type expansion.
   needs to imply dynamic subtyping / value typing. *)
Inductive tresolve: id -> ty -> ty -> Prop :=
  | tr_self: forall x T,
             tresolve x T T
  | tr_and1: forall x T1 T2 T,
             tresolve x T1 T ->
             tresolve x (TAnd T1 T2) T
  | tr_and2: forall x T1 T2 T,
             tresolve x T2 T ->
             tresolve x (TAnd T1 T2) T
  | tr_unpack: forall x T2 T3 T,
             open x T2 = T3 ->
             tresolve x T3 T ->
             tresolve x (TBind T2) T
.

Tactic Notation "tresolve_cases" tactic(first) ident(c) :=
  first;
  [ Case_aux c "Self" |
    Case_aux c "And1" |
    Case_aux c "And2" |
    Case_aux c "Bind" ].

(* static type well-formedness.
   needs to imply dynamic subtyping. *)
Inductive wf_type : tenv -> ty -> Prop :=
| wf_top: forall env,
    wf_type env TNoF
| wf_bool: forall env,
    wf_type env TBool
| wf_and: forall env T1 T2,
             wf_type env T1 ->
             wf_type env T2 ->
             wf_type env (TAnd T1 T2)
| wf_mema: forall env,
             wf_type env (TMem None)
| wf_mem: forall env TA,
             wf_type env TA ->
             wf_type env (TMem (Some TA))
| wf_fun: forall env f T1 T2,
             wf_type env T1 ->
             wf_type env T2 ->
             wf_type env (TFun f T1 T2)

| wf_sel: forall envz x TE TA,
            index x envz = Some (TE) ->
            tresolve x TE (TMem TA) ->
            wf_type envz (TSel x)

| wf_selb: forall envz x, (* note: disregarding bind-scope *)
             wf_type envz (TSelB x)
| wf_bind: forall envz T,
             wf_type envz T ->
             wf_type envz (TBind T)

.

Tactic Notation "wf_cases" tactic(first) ident(c) :=
  first;
  [ Case_aux c "Top" |
    Case_aux c "Bool" |
    Case_aux c "And" |
    Case_aux c "MemA" |
    Case_aux c "Mem" |
    Case_aux c "Fun" |
    Case_aux c "Sel" |
    Case_aux x "SelB" |
    Case_aux c "Bind" ].

(* static subtyping: during type checking/assignment.
   needs to imply dynamic subtyping *)
Inductive atp: tenv -> ty -> ty -> Prop :=
| atp_sel2: forall x env T TF,
    index x env = Some TF ->
    tresolve x TF (TMem (Some T)) ->
    atp env T (TSel x)
| atp_sel1: forall x env T TF,
    index x env = Some TF ->
    tresolve x TF (TMem (Some T)) ->
    atp env (TSel x) T
.

Tactic Notation "atp_cases" tactic(first) ident(c) :=
  first;
  [ Case_aux c "? < Sel" | Case_aux c "Sel < ?" ].

Inductive has_type : list (nat*ty) -> tm -> ty -> Prop :=
| t_true: forall env,
           has_type env ttrue TBool
| t_false: forall env,
           has_type env tfalse TBool
| t_var: forall n (env:list (nat*ty)) t1,
           index n env = Some t1 ->
           wf_type env t1 ->
           has_type env (tvar n) t1
| t_vara: forall n env T T2,
           index n env = Some T ->
           atp env T T2 ->
           wf_type env T2 ->
           has_type env (tvar n) T2

| t_var_pack: forall n env T T2,
           index n env = Some T ->
           open n T2 = T ->
           wf_type env T ->
           wf_type env (TBind T2) ->
           has_type env (tvar n) (TBind T2)

| t_var_unpack: forall n env T T2,
           index n env = Some (TBind T2) ->
           open n T2 = T ->
           wf_type env T ->
           wf_type env (TBind T2) ->
           has_type env (tvar n) T

| t_app: forall env f m x TF T1 T2,
           index f env = Some TF ->
           tresolve f TF (TFun m T1 T2) ->
           wf_type env T1 ->
           wf_type env T2 ->
           has_type env x T1 ->
           has_type env (tapp f m x) T2
| t_abs: forall env TF TFN f z dcs TA T3,
           TF  = (TObj (Some TA) dcs) ->
           TFN = (TObj None      dcs) ->
           closed 0 TA -> (* should be implied by WF below *)
           dc_has_type ((f,TF)::env) dcs ->

           has_type ((f,TFN)::env) z T3 ->

           wf_type ((f,TF)::env) TF ->
           wf_type env T3 ->
           has_type env (tabs f TA dcs z) T3
| t_let: forall env x y z T1 T3,
           has_type env y T1 ->
           has_type ((x,T1)::env) z T3 ->
           wf_type env T3 ->
           has_type env (tlet x T1 y z) T3

 with dc_has_type: list(nat * ty) -> list (nat*dc) -> Prop :=
      | dt_fun: forall env x y m T1 T2 dcs,
          has_type ((x,T1)::env) y T2 ->
          dc_has_type env dcs ->
          m = length dcs ->
          dc_has_type env ((m, dfun T1 T2 x y)::dcs)
      | dt_nil: forall env,
           dc_has_type env nil
.

(* ############################################################ *)
(* Dynamic properties: value typing, evaluation, ... *)
(* ############################################################ *)

(* dynamic subtyping: during execution *)
Inductive stp : nat -> env -> ty -> env -> ty -> Prop :=
| stp_top: forall n1 G1 G2,
    stp n1 G1 TNoF G2 TNoF (* don't want to deal with it now *)

| stp_bool: forall n1 G1 G2,
    stp n1 G1 TBool G2 TBool

| stp_fun: forall n1 n2 m G1 G2 T11 T12 T21 T22,
    stp n1 G2 T21 G1 T11 ->
    stp n2 G1 T12 G2 T22 ->
    stp (S (n1+n2)) G1 (TFun m T11 T12) G2 (TFun m T21 T22)

| stp_mem_ss: forall n1 n2 G1 G2 TA1 TA2,
    stp n1 G1 TA1 G2 TA2 ->
    stp n2 G2 TA2 G1 TA1 ->
    stp (S (n1+n2)) G1 (TMem (Some TA1)) G2 (TMem (Some TA2))
| stp_mema_sn: forall n1 G1 G2 TA,
    stp n1 G1 TA G1 TA -> (* regularity *)
    stp (S (n1+n1)) G1 (TMem (Some TA)) G2 (TMem None)
| stp_mema_nn: forall n1 G1 G2,
    stp n1 G1 (TMem None) G2 (TMem None)

| stp_bind: forall n1 G1 G2 TA1 TA2,
    stp n1 G1 TA1 G2 TA2 ->
    stp (S n1+n1) G1 (TBind TA1) G2 (TBind TA2) (* may relax to diff types *)

| stp_and11: forall n1 n2 G1 G2 T1 T2 T,
    stp n1 G1 T1 G2 T ->
    stp n2 G1 T2 G1 T2 -> (* regularity *)
    stp (S (n1+n2)) G1 (TAnd T1 T2) G2 T
| stp_and12: forall n1 n2 G1 G2 T1 T2 T,
    stp n1 G1 T2 G2 T ->
    stp n2 G1 T1 G1 T1 -> (* regularity *)
    stp (S (n1+n2)) G1 (TAnd T1 T2) G2 T
| stp_and2: forall n1 n2 G1 G2 T1 T2 T,
    stp n1 G1 T G2 T1 ->
    stp n2 G1 T G2 T2 ->
    stp (S (n1+n2)) G1 T G2 (TAnd T1 T2)

| stp_sel2: forall n2 f x dcs T1 TA G1 G2 GC,
    index x G2 = Some (vabs GC f TA dcs) ->
    closed 0 TA ->
    stp n2 G1 T1 ((f,vabs GC f TA dcs)::GC) TA ->
    stp (S n2) G1 T1 G2 (TSel x)
| stp_sel1: forall n2 f x dcs TA T2 G1 G2 GC,
    index x G1 = Some (vabs GC f TA dcs) ->
    closed 0 TA ->
    stp n2 ((f,vabs GC f TA dcs)::GC) TA G2 T2 ->
    stp (S n2) G1 (TSel x) G2 T2
| stp_selx: forall n1 x1 x2 v G1 G2,
    (*    resolve G1 x = Some (GC,TC) -> *)
    (* don't need TC? but shouldn't we know it's a closure? *)
    index x1 G1 = Some v ->
    index x2 G2 = Some v ->
    stp n1 G1 (TSel x1) G2 (TSel x2)

| stp_selbx: forall n1 x G1 G2,
    stp n1 G1 (TSelB x) G2 (TSelB x)

.

Tactic Notation "stp_cases" tactic(first) ident(c) :=
  first;
  [ Case_aux c "Top < Top" |
    Case_aux c "Bool < Bool" |
    Case_aux c "Fun < Fun" |
    Case_aux c "Mem Some < Mem Some" |
    Case_aux c "Mem Some < Mem None" |
    Case_aux c "Mem None < Mem None" |
    Case_aux c "Bind < Bind" |
    Case_aux c "T & ? < T" |
    Case_aux c "? & T < T" |
    Case_aux c "? < ? & ?" |
    Case_aux c "? < Sel" |
    Case_aux c "Sel < ?" |
    Case_aux c "Sel < Sel" |
    Case_aux c "SelB < SelB" ].

Definition stpd G1 T1 G2 T2 := exists n, stp n G1 T1 G2 T2.

Inductive wf_env : list (nat*vl) -> list (nat*ty) -> Prop :=
| wfe_nil : wf_env nil nil
| wfe_cons : forall n v t vs ts,
     val_type ((n,v)::vs) v t -> wf_env vs ts -> wf_env (cons (n,v) vs) (cons (n,t) ts)

with val_type : env -> vl -> ty -> Prop :=
| v_bool: forall venv b T,
    stpd nil TBool venv T ->
    val_type venv (vbool b) T
| v_abs: forall env venv tenv TW TF f dcs TA,
    TF  = (TObj (Some TA) dcs) ->
    closed 0 TA -> (* should be implied by WF below *)
    dc_has_type ((f,TF)::tenv) dcs ->

    wf_env env tenv ->
    wf_type ((f,TF)::tenv) TF ->
    stpd ((f,(vabs env f TA dcs))::env) TF venv TW ->
    val_type venv (vabs env f TA dcs) TW

| v_pack: forall venv venv3 x v T T2 T3,
    index x venv = Some v ->
    val_type venv v T ->
    open x T2 = T ->
    stpd venv (TBind T2) venv3 T3 ->
    val_type venv3 v T3
.

(* evaluation. could use do-notation to clean up syntax *)
Fixpoint teval(n: nat)(env: env)(t: tm){struct n}: option (option vl) :=
  match n with
    | 0 => None
    | S n =>
      match t with
        | ttrue  => Some (Some (vbool true))
        | tfalse => Some (Some (vbool false))
        | tvar x => Some (index x env)
        | tabs f T dcs z =>
          teval n ((f,vabs env f T dcs)::env) z
        | tapp x m ex =>
                  match teval n env ex with
                    | None => None
                    | Some None => Some None
                    | Some (Some vx) =>
          match index x env with
            | None => Some(None)
            | Some (vbool _) => Some(None)
            | Some (vabs env2 f T dcs) =>
              match index m dcs with
                | None => Some(None)
                | Some (dfun T1 T2 x ey) =>
                      teval n ((x,vx)::(f,vabs env2 f T dcs)::env2) ey
                  end
              end
          end
        | tlet x T1 y z =>
          match teval n env y with
            | None => None
            | Some None => Some None
            | Some (Some vx) =>
              teval n ((x,vx)::env) z
          end
      end
  end.

(* not used, just for completeness *)
Inductive eval : env -> tm -> option vl -> Prop :=
| e_true: forall env,
    eval env ttrue (Some (vbool true))
| e_false: forall env,
    eval env tfalse (Some (vbool false))
| e_var: forall n (env:list (nat*vl)) v1,
    index n env = Some v1 ->
    eval env (tvar n) (Some v1)
| e_app: forall env env2 T T1 T2 n m f x ey ex vx rvy dcs,
    index n env = Some (vabs env2 f T dcs) ->
    index m dcs = Some (dfun T1 T2 x ey) ->
    eval env ex (Some vx) ->
    eval ((x,vx)::(f,vabs env2 f T dcs)::env2) ey rvy ->
    eval env (tapp n m ex) rvy
| e_abs: forall env f T dcs z rvz,
    eval ((f,vabs env f T dcs)::env) z rvz ->
    eval env (tabs f T dcs z) rvz.

(* ############################################################ *)
(* Examples *)
(* ############################################################ *)

Hint Constructors ty.
Hint Constructors tm.
Hint Constructors vl.

Hint Constructors eval.
Hint Constructors has_type.
Hint Constructors val_type.
Hint Constructors wf_env.

Hint Constructors wf_type.
Hint Constructors stp.

Hint Constructors atp.
Hint Constructors dc_has_type.

Hint Unfold stpd.

Hint Constructors option.
Hint Constructors list.

Hint Unfold index.
Hint Unfold length.

Hint Constructors tresolve.

Hint Resolve ex_intro.

Require Import LibTactics.

Require Import Coq.Program.Equality.
Require Import Coq.Classes.Equivalence.
Require Import Coq.Classes.EquivDec.
Require Import Coq.Logic.Decidable.

(* examples *)

Definition TNat := TBool.

Definition f := 0. (*(Id 10).*)
Hint Unfold f.

Definition x := 1. (*(Id 0).*)
Definition y := 2. (*(Id 1).*)
Definition z := 3. (*(Id 2).*)
Hint Unfold x.
Hint Unfold y.
Hint Unfold z.

Definition t01 := (TArrow (Some TNat) TNat TNat).
Definition t11 := (TArrow None TNat TNat).
Definition t02 := (TArrow (Some TNat) (TSel f) (TSel f)).
Definition t12 := (TArrow None (TSel f) (TSel f)).

Hint Unfold t01.
Hint Unfold t11.
Hint Unfold t02.
Hint Unfold t12.

Definition idx (i:nat) a b := (i, dfun a b x (tvar x)).

Fixpoint tnew i t d z := tabs i t d z.

Example xx1 : eval nil ttrue (Some (vbool true)) .
Proof. eauto. Qed.

Example ev2 : eval nil
   (tnew f TNat [idx 0 TNat TNat] (tvar f))
   (Some (vabs nil f TNat [idx 0 TNat TNat])).
Proof.
  repeat (econstructor; eauto).
Qed.

Example tp2 : has_type nil
   (tabs f TNat [idx 0 TNat TNat] (tvar f))
   t11. (* want t11 here! *)
Proof.
  repeat (econstructor; compute; eauto).
Qed.

(*
let f: { A = Nat; Nat => Nat } = x => x
let x: { A; Nat => Nat } = f
let y: Nat = x(7)
true
*)
Example tp3 : has_type nil
   (tabs f TNat [idx 0 TNat TNat]
     (tlet x t11 (tvar f) (* abstract type mem *)
        ttrue))
   TBool.
Proof.
  repeat (econstructor; eauto).
Qed.

(* Hint Extern 1 (_ = _) => abstract compute. *)

Hint Constructors has_type.
Hint Constructors dc_has_type.

Hint Unfold idx.
Hint Unfold dc_type_and.

(*
match goal with
        | |- has_type _ (tvar _) _ =>
          try solve [apply t_vara;
                      repeat (econstructor; eauto)]
          | _ => idtac
      end;
*)

Ltac crush_has_tp :=
  try solve [econstructor; compute; eauto; crush_has_tp];
  try solve [eapply t_vara; compute; eauto; crush_has_tp];
  try solve [eapply t_var_unpack; compute; eauto; crush_has_tp];
  try solve [eapply t_var_pack; compute; eauto; crush_has_tp];
  try solve [eapply tr_unpack; crush_has_tp].

(*
let f: { A = Nat; Nat => f.A } = x => x
true
*)
Example tp4 : has_type nil
   (tabs f TNat [idx 0 TNat (TSel f)]
        ttrue)
   TBool.
Proof.
  crush_has_tp.
Qed.

(*
let f: { A = Nat; Nat => f.A } = x => x
let x: { A; Nat => f.A } = f
true
*)
Example tp5 : has_type nil
   (tabs f TNat [idx 0 TNat (TSel f)]
     (tlet x (TArrow None TNat (TSel f)) (tvar f) (* abstract type mem *)
        ttrue))
   TBool.
Proof.
  crush_has_tp.
Qed.

(*
branding/unbranding. roughly this:

val a = new {
  type A = Nat
  def intro(x:Nat): a.A = x
  def elim(x:a.A): Nat = x
} // type A abstract outside

val x: a.A = a.intro(7)
val y: Nat = a.elim(x)

val z: a.A = 7 // fail
val u: Nat = x // fail
*)

(*
BRANDING
let f: { A = Nat; Nat => f.A } = x => x
let x: { A; Nat => f.A } = f
let f.A: Nat = x(7)
true
*)
Example tp6 : has_type nil
   (tabs f TNat [idx 0 TNat (TSel f)]
     (tlet x (TArrow None TNat (TSel f)) (tvar f) (* abstract type mem *)
        (tlet y (TSel f) (tapp x 0 ttrue)
           ttrue)))
   TBool.
Proof.
  crush_has_tp.
Qed.

(*
UNBRANDING
let f: { A = Nat; Nat => f.A ; f.A => Nat } = x => x ; x => x
let x: Nat = 7
let y: f.A = f.0(x) // intro
let z: Nat = f.1(y) // elim
z
*)
Example tp7 : has_type nil
   (tabs f TNat [idx 1 (TSel f) TNat; idx 0 TNat (TSel f)]
     (tlet x (TBool) (ttrue)
       (tlet y (TSel f) (tapp f 0 (tvar x)) (* call intro *)
         (tlet z TNat (tapp f 1 (tvar y))   (* call elim *)
           (tvar z)))))
   TBool.
Proof.
  crush_has_tp.
Qed.

(*
SELF TYPES: PACK
let f: { A = Nat; Nat => f.A } = x => x
f
*)
Example tp8 : has_type nil
   (tabs f TNat [idx 0 TNat (TSel f)]
     (tvar f))
   (TBind (TAnd (TMem None) (TAnd (TFun 0 TNat (TSelB 0)) TNoF))).
Proof.
  crush_has_tp.
Qed.

(*
SELF TYPES: return from function
let f: { A = Nat; Nat => f.A } = x => x
f
 *)

(*
Expand bind so that the (TSel f) becomes well-formed.
*)

Example tp91 : has_type
    [(f,(TBind (TAnd (TMem None) (TAnd (TFun 0 TNat (TSelB 0)) TNoF))))]
    (tvar f)
    (TAnd (TMem None) (TAnd (TFun 0 TNat (TSel f)) TNoF)).
Proof.
  crush_has_tp.
Qed.

Example tp92 : has_type
    [(f,(TBind (TAnd (TMem None) (TAnd (TFun 0 TNat (TSelB 0)) TNoF))))]
    (tapp f 0 ttrue)
    (TSel f).
Proof.
  econstructor; crush_has_tp.
Qed.

Example tp93 : has_type nil
  (tlet f (TBind (TAnd (TMem None) (TAnd (TFun 0 TNat (TSelB 0)) TNoF)))
    (tabs f TNat [idx 0 TNat (TSel f)]
      (tvar f))
    (tlet x (TAnd (TMem None) (TAnd (TFun 0 TNat (TSel f)) TNoF)) (tvar f)
      ttrue))  (* note that x.fun has type Nat -> f.A *)
  TBool.       (* if we want Nat -> x.A we need to assign a Bind to x *)
Proof.
  crush_has_tp.
Qed.

Example tp94 : has_type nil
  (tlet f (TBind (TAnd (TMem None) (TAnd (TFun 1 (TSelB 0) TNat) (TAnd (TFun 0 TNat (TSelB 0)) TNoF))))
    (tabs f TNat [idx 1 (TSel f) TNat; idx 0 TNat (TSel f)]
          (tvar f))
    (* x binding is not used. just a sanity check that we derive the correct type *)
    (tlet x (TAnd (TMem None) (TAnd (TFun 1 (TSel f) TNat) (TAnd (TFun 0 TNat (TSel f)) TNoF))) (tvar f)
      (tlet y (TSel f) (tapp f 0 (ttrue)) (* call intro *)
         (tlet z TNat (tapp f 1 (tvar y))   (* call elim *)
           (tvar z)))))
  TBool.
Proof.
  econstructor; crush_has_tp.
  econstructor; crush_has_tp.
  econstructor; crush_has_tp.
  econstructor; crush_has_tp.
  econstructor; crush_has_tp.
  econstructor; crush_has_tp.
Qed.

(* ############################################################ *)
(* Proofs *)
(* ############################################################ *)

Lemma hastp_wf: forall G e T, has_type G e T -> wf_type G T.
  Proof. intros. induction H; eauto.
Qed.

Lemma index_max : forall X vs n (T: X),
                       index n vs = Some T ->
                       n < length vs.
Proof.
  intros X vs. induction vs.
  Case "nil". intros. inversion H.
  Case "cons".
  intros. inversion H. destruct a.
  case_eq (beq_nat n (length vs)); intros E.
  SCase "hit".
  rewrite E in H1. inversion H1. subst.
  eapply beq_nat_true in E.
  unfold length. unfold length in E. rewrite E. eauto.
  SCase "miss".
  rewrite E in H1.
  assert (n < length vs). eapply IHvs. apply H1.
  compute. eauto.
Qed.

Lemma index_extend : forall X vs n a (T: X),
                       index n vs = Some T ->
                       index n (a::vs) = Some T.

Proof.
  intros.
  assert (n < length vs). eapply index_max. eauto.
  assert (n <> length vs). omega.
  assert (beq_nat n (length vs) = false) as E. eapply beq_nat_false_iff; eauto.
  unfold index. unfold index in H. rewrite H. rewrite E. destruct a. reflexivity.
Qed.

Hint Resolve index_extend.

Lemma wft_extend : forall vs x v1 T,
                       wf_type vs T ->
                       wf_type ((x,v1)::vs) T.
Proof. intros. induction H; eauto.  Qed.

Hint Resolve wft_extend.

(* impossible subtyping cases, uses for contradictions *)
Inductive nostp: ty -> ty -> Prop :=
| nostp_top_fun: forall m T1 T2,
   nostp TNoF (TFun m T1 T2)
| nostp_top_mem: forall TA,
   nostp TNoF (TMem TA)
| nostp_fun: forall T1 T2 T3 T4 n1 n2,
   not (n1 = n2) ->
   nostp (TFun n1 T1 T2) (TFun n2 T3 T4)
| nostp_fun_mem: forall m TA T1 T2,
   nostp (TMem TA) (TFun m T1 T2)
| nostp_mem_fun: forall m TA T1 T2,
   nostp (TFun m T1 T2) (TMem TA)
| nostp_and: forall T1 T2 T,
    nostp T1 T ->
    nostp T2 T ->
    nostp (TAnd T1 T2) T
.

Hint Constructors nostp.

(* INVERSION CASES *)

Lemma stp_mem_invA: forall G1 G2 TA1 TA2,
    stpd G1 (TMem (Some TA1)) G2 (TMem (Some TA2)) ->
    stpd G1 TA1 G2 TA2.
Proof. intros. destruct H. inversion H. eexists. eauto. Qed.

Lemma stp_mem_invB: forall G1 G2 TA1 TA2,
    stpd G1 (TMem (Some TA1)) G2 (TMem (Some TA2)) ->
    stpd G2 TA2 G1 TA1.
Proof. intros. destruct H. inversion H. eexists. eauto. Qed.

Lemma stp_funA: forall m G1 G2 T11 T12 T21 T22,
    stpd G1 (TFun m T11 T12) G2 (TFun m T21 T22) ->
    stpd G2 T21 G1 T11.
Proof. intros. destruct H. inversion H. eexists. eauto. Qed.
Lemma stp_funB: forall m G1 G2 T11 T12 T21 T22,
    stpd G1 (TFun m T11 T12) G2 (TFun m T21 T22) ->
    stpd G1 T12 G2 T22.
Proof. intros. destruct H. inversion H. eexists. eauto. Qed.

(* invert `and` if one branch is impossible *)
Lemma nostp_no_rhs_and: forall T1 T2 T,
      nostp T (TAnd T1 T2) ->
      False.
Proof. intros. remember (TAnd T1 T2). induction H; inversion Heqt.
       eauto.
Qed.
Lemma nostp_no_rhs_sel: forall T x,
      nostp T (TSel x) ->
      False.
Proof. intros. remember (TSel x0). induction H; inversion Heqt.
       eauto.
Qed.

Hint Resolve ex_intro.

Lemma stp_contra: forall T1 T2 G1 G2,
      nostp T1 T2 ->
      stpd G1 T1 G2 T2 ->
      False.
Proof. intros. induction H; destruct H0 as [n H0]; inversion H0; subst; eauto.

(*
       eapply IHnostp1. eexists. eauto.
       eapply IHnostp2. eexists. eauto.
*)
       eapply nostp_no_rhs_and. eauto.
       eapply nostp_no_rhs_sel. eauto.

Qed.

Lemma stp_andA: forall G1 G2 T1 T2 T,
    stpd G1 (TAnd T1 T2) G2 T ->
    nostp T2 T ->
    stpd G1 T1 G2 T.
Proof. intros. destruct H. inversion H.
       subst. eexists. eauto.
       eapply stp_contra in H0. contradiction. exists n1. eauto.
       subst. eapply nostp_no_rhs_and in H0. contradiction.
       subst. eapply nostp_no_rhs_sel in H0. contradiction.
Qed.
Lemma stp_andB: forall G1 G2 T1 T2 T,
    stpd G1 (TAnd T1 T2) G2 T ->
    nostp T1 T ->
    stpd G1 T2 G2 T.
Proof. intros. destruct H. inversion H.
       eapply stp_contra in H0. contradiction. exists n1. eauto.
       subst. eexists. eauto.
       subst. eapply nostp_no_rhs_and in H0. contradiction.
       subst. eapply nostp_no_rhs_sel in H0. contradiction.
Qed.

Lemma stp_and2A: forall G1 G2 T1 T2 T,
    stpd G1 T G2 (TAnd T1 T2) ->
    stpd G1 T G2 T1.
Proof. intros. remember (TAnd T1 T2). destruct H. induction H; inversion Heqt.
       eapply IHstp1 in H1. destruct H1.
       eexists. eapply stp_and11. eauto. eauto.
       eapply IHstp1 in H1. destruct H1.
       eexists. eapply stp_and12. eauto. eauto.
       subst. eexists. eauto.
       eapply IHstp in H2. destruct H2.
       subst. eexists. eapply stp_sel1. eauto. eauto. eauto.
Qed.

Lemma stp_and2B: forall G1 G2 T1 T2 T,
    stpd G1 T G2 (TAnd T1 T2) ->
    stpd G1 T G2 T2.
Proof. intros. remember (TAnd T1 T2). destruct H. induction H; inversion Heqt.
       eapply IHstp1 in H1. destruct H1.
       eexists. eapply stp_and11. eauto. eauto.
       eapply IHstp1 in H1. destruct H1.
       eexists. eapply stp_and12. eauto. eauto.
       subst. eexists. eauto.
       eapply IHstp in H2. destruct H2.
       subst. eexists. eapply stp_sel1. eauto. eauto. eauto.
Qed.

(* EXTENSION *)

Hint Constructors stp.

Lemma stp_extend : forall SF G1 G2 T1 T2 x v,
    stp SF G1 T1 G2 T2 ->
    stp SF ((x,v)::G1) T1 G2 T2 /\
    stp SF G1 T1 ((x,v)::G2) T2 /\
    stp SF ((x,v)::G1) T1 ((x,v)::G2) T2.
Proof. intros. stp_cases (induction H) Case;
         try inversion IHstp as [IH_1 [IH_2 IH_12]];
         try inversion IHstp1 as [IH1_1 [IH1_2 IH1_12]];
         try inversion IHstp2 as [IH2_1 [IH2_2 IH2_12]];
         split; try solve [eauto; constructor; eauto].
Qed.

Lemma stp_extend1 : forall SF G1 G2 T1 T2 x v,
    stp SF G1 T1 G2 T2 ->
    stp SF ((x,v)::G1) T1 G2 T2.
Proof. intros. eapply stp_extend. eauto. Qed.

Lemma stp_extend2 : forall SF G1 G2 T1 T2 x v,
    stp SF G1 T1 G2 T2 ->
    stp SF G1 T1 ((x,v)::G2) T2.
Proof. intros. eapply stp_extend. eauto. Qed.

Lemma stpd_extend1 : forall G1 G2 T1 T2 x v,
                       stpd G1 T1 G2 T2 ->
                       stpd ((x,v)::G1) T1 G2 T2.
Proof. intros. destruct H. eexists. eapply stp_extend1. apply H. Qed.

Hint Resolve stp_extend1.

Lemma stpd_extend2 : forall G1 G2 T1 T2 x v,
                       stpd G1 T1 G2 T2 ->
                       stpd G1 T1 ((x,v)::G2) T2.
Proof. intros. destruct H. eexists. eapply stp_extend2. apply H. Qed.

Hint Resolve stp_extend2.

(* REGULARITY *)

Lemma stp_reg : forall G1 G2 T1 T2,
    stpd G1 T1 G2 T2 ->
    stpd G1 T1 G1 T1 /\ stpd G2 T2 G2 T2.
Proof. intros. destruct H. stp_cases (induction H) Case;
         try inversion IHstp as [[IH_n1 IH_1] [IH_n2 IH_2]];
         try inversion IHstp1 as [[IH_n1 IH_1] [IH_n2 IH_2]];
         try inversion IHstp2 as [[IH_n3 IH_3] [IH_n4 IH_4]];
         split;
         try solve [exists 0; eauto];
         try solve [eexists; eauto].
Qed.

Lemma stp_reg1 : forall G1 G2 T1 T2,
    stpd G1 T1 G2 T2 ->
    stpd G1 T1 G1 T1.
Proof. intros. eapply stp_reg in H. inversion H. eauto. Qed.

Lemma stp_reg2 : forall G1 G2 T1 T2,
    stpd G1 T1 G2 T2 ->
    stpd G2 T2 G2 T2.
Proof. intros. eapply stp_reg in H. inversion H. eauto. Qed.

(* HELPERS *)

Lemma stpd_sel2: forall G1 T1 G2 GC TA f x dcs,
                      index x G2 = Some(vabs GC f TA dcs) ->
                      closed 0 TA ->
                      stpd G1 T1 ((f,vabs GC f TA dcs)::GC) TA ->
                      stpd G1 T1 G2 (TSel x)
.
Proof. intros. destruct H1. eexists. eapply stp_sel2; eauto. Qed.

Lemma stpd_sel1: forall G1 GC TA T2 G2 f x dcs,
                      index x G1 = Some(vabs GC f TA dcs) ->
                      closed 0 TA ->
                      stpd ((f,vabs GC f TA dcs)::GC) TA G2 T2 ->
                      stpd G1 (TSel x) G2 T2
.
Proof. intros. destruct H1. eexists. eapply stp_sel1; eauto. Qed.

Lemma stpd_and11: forall G1 G2 T1 T2 T,
                      stpd G1 T1 G2 T ->
                      stpd G1 T2 G1 T2 ->
                      stpd G1 (TAnd T1 T2) G2 T
.
Proof. intros. destruct H. destruct H0. eexists. eapply stp_and11; eauto. Qed.

Lemma stpd_and12: forall G1 G2 T1 T2 T,
                      stpd G1 T2 G2 T ->
                      stpd G1 T1 G1 T1 ->
                      stpd G1 (TAnd T1 T2) G2 T
.
Proof. intros. destruct H. destruct H0. eexists. eapply stp_and12; eauto. Qed.

Lemma stpd_and2: forall G1 G2 T1 T2 T,
                      stpd G1 T G2 T1 ->
                      stpd G1 T G2 T2 ->
                      stpd G1 T G2 (TAnd T1 T2)
.
Proof. intros. destruct H. destruct H0. eexists. eapply stp_and2; eauto. Qed.

Lemma stpd_fun:  forall m G1 G2 T11 T12 T21 T22,
    stpd G2 T21 G1 T11 ->
    stpd G1 T12 G2 T22 ->
    stpd G1 (TFun m T11 T12) G2 (TFun m T21 T22)
.
Proof. intros. destruct H. destruct H0. eexists. eapply stp_fun; eauto. Qed.

Lemma stpd_mem_ss: forall G1 G2 TA1 TA2,
    stpd G1 TA1 G2 TA2 ->
    stpd G2 TA2 G1 TA1 ->
    stpd G1 (TMem (Some TA1)) G2 (TMem (Some TA2)).
Proof. intros. destruct H. destruct H0. eexists. eapply stp_mem_ss; eauto. Qed.

Lemma stpd_mema_sn: forall G1 G2 TA,
    stpd G1 TA G1 TA -> (* regularity *)
    stpd G1 (TMem (Some TA)) G2 (TMem None).
Proof. intros. destruct H. eexists. eapply stp_mema_sn; eauto. Qed.

Lemma stpd_mema_nn: forall G1 G2,
    stpd G1 (TMem None) G2 (TMem None).
Proof. intros. exists 0. eapply stp_mema_nn; eauto. Qed.

Lemma stpd_bind: forall G1 G2 TA1 TA2,
    stpd G1 TA1 G2 TA2 ->
    stpd G1 (TBind TA1) G2 (TBind TA2).
Proof. intros. destruct H. eexists. eapply stp_bind; eauto. Qed.

(* TRANSITIVITY *)

Definition trans_on n12 n23 :=
                      forall  T1 T2 T3 G1 G2 G3,
                      stp n12 G1 T1 G2 T2 ->
                      stp n23 G2 T2 G3 T3 ->
                      stpd G1 T1 G3 T3.
Hint Unfold trans_on.

Definition trans_up n := forall n12 n23, n12 + n23 <= n ->
                      trans_on n12 n23.
Hint Unfold trans_up.

Lemma trans_le: forall n n1 n2,
                      trans_up n ->
                      n1 + n2 <= n ->
                      trans_on n1 n2
.
Proof. intros. unfold trans_up in H. eapply H. eauto. Qed.

Lemma stp_trans: forall n, trans_up n.
Proof. intros n.
       induction n.
       Case "z".
       unfold trans_up. unfold trans_on.
       intros.
       assert (n12 = 0). omega. assert (n23 = 0). omega. subst.
       inversion H0; inversion H1; subst;
       try solve [inversion H0];
       try solve [inversion H1];
       try solve [exists 0; eauto].

       SCase "Sel < Sel".
       inversion H13. subst. rewrite H3 in H9. inversion H9. subst.
       exists 0. eapply stp_selx. eauto. eauto.

       SCase "SelB < SelB".
       inversion H9. subst.
       exists 0. eapply stp_selbx.

       Case "S n".
       unfold trans_up. intros n12 n23 NE   T1 T2 T3 G1 G2 G3    S12 S23.

       (* case analysis takes a long time! >= 144 cases to start with *)
       stp_cases(inversion S12) SCase;  stp_cases(inversion S23) SSCase;  subst;

       try solve [SSCase "? < Sel";
         eapply stpd_sel2; [eauto | eauto | eapply trans_le in IHn; [ eapply IHn; eauto | omega ]]];

       try solve [SCase "Sel < ?";
         eapply stpd_sel1; [eauto | eauto | eapply trans_le in IHn; [ eapply IHn; eauto | omega ]]];

       try solve [SSCase "? < ? & ?";
         eapply stpd_and2; [ eapply trans_le in IHn; [ eapply IHn; eauto | omega] |
                             eapply trans_le in IHn; [ eapply IHn; eauto | omega]]];

       try solve [SCase "T & ? < T";
         eapply stpd_and11; [ eapply trans_le in IHn; [ eapply IHn; eauto | omega] | eexists; eauto]];

       try solve [SCase "? & T < T";
         eapply stpd_and12; [ eapply trans_le in IHn; [ eapply IHn; eauto | omega] | eexists; eauto]];

       try solve [exists 0; eauto];
       try solve by inversion;
       idtac.

(*
       SCase "Bool < Bool". SSCase "Bool < Bool".
       eapply ex_intro with 0. eapply stp_bool.
*)
       idtac.

       SCase "Fun < Fun". SSCase "Fun < Fun". inversion H10. subst.
       eapply stpd_fun. eapply trans_le in IHn. eapply IHn. eauto. eauto. omega.
                        eapply trans_le in IHn. eapply IHn. eauto. eauto. omega.

       SCase "Mem Some < Mem Some". SSCase "Mem Some < Mem Some". inversion H10. subst.
       eapply stpd_mem_ss. eapply trans_le in IHn. eapply IHn. eauto. eauto. omega.
                           eapply trans_le in IHn. eapply IHn. eauto. eauto. omega.

       SCase "Mem Some < Mem Some". SSCase "Mem Some < Mem None". inversion H9. subst.
       eapply stpd_mema_sn. eapply trans_le in IHn. eapply IHn. eauto. eauto. omega.

       SCase "Mem Some < Mem None". SSCase "Mem None < Mem None".
       eapply stpd_mema_sn. eapply trans_le in IHn. eapply IHn. eauto. eauto. omega.

       SCase "Bind < Bind". SSCase "Bind < Bind". inversion H8. subst.
       eapply stpd_bind. eapply trans_le in IHn. eapply IHn. eauto. eauto. omega.

       SCase "? < ? & ?". SSCase "T & ? < T". inversion H10. subst.
       eapply trans_le in IHn. eapply IHn. apply H. apply H6. omega.

       SCase "? < ? & ?". SSCase "? & T < T". inversion H10. subst.
       eapply trans_le in IHn. eapply IHn. apply H0. apply H6. omega.

       SCase "? < Sel". SSCase "Sel < ?". (* proper mid *)
       assert (trans_on n2 n0) as IHX. eapply trans_le; [ eauto | omega ].
       (* invert TSel x = TSel x0 *)
       inversion H12. subst x0. rewrite H in H7. inversion H7. subst.
       eapply IHX. apply H1. apply H9.

       SCase "? < Sel". SSCase "Sel < Sel".
       inversion H11. subst x1. rewrite H in H7. inversion H7. subst.
       eapply stpd_sel2. eauto. eauto. eexists. eapply H1.

       SCase "Sel < Sel". SSCase "Sel < ?".
       inversion H11. subst x2. rewrite H0 in H6. inversion H6. subst.
       eapply stpd_sel1. eauto. eauto. eexists. eapply H8.

       SCase "Sel < Sel". SSCase "Sel < Sel".
       exists 0. eapply stp_selx. eauto. eauto.  inversion H10. subst.
         rewrite H0 in H6. inversion H6. subst. eapply H7.

       SCase "SelB < SelB". SSCase "SelB < SelB".
       inversion H6. subst.
       exists 0. eapply stp_selbx.
Qed.

Lemma stpd_trans: forall G1 G2 G3 T1 T2 T3,
    stpd G1 T1 G2 T2 ->
    stpd G2 T2 G3 T3 ->
    stpd G1 T1 G3 T3.
Proof. intros.
    destruct H. destruct H0. eapply (stp_trans (x0+x1) x0 x1). eauto. eapply H. eapply H0.
Qed.

(* HELPERS SPECIFIC TO OBJ TYPES *)

(* used in abs preservation case *)
Lemma stpd_obj_reg_mem : forall G1 TA dcs,
                      stpd G1 (TObj (Some TA) dcs) G1 (TObj (Some TA) dcs)  ->
                      stpd G1 TA G1 TA.
Proof.
  intros.
  assert (stpd G1 (TObj (Some TA) dcs) G1 (TMem (Some TA))).
  eapply stp_and2A. eauto.
  eapply stp_mem_invA. eapply stp_reg2. eauto.
Qed.

Lemma stpd_obj_reg_dcs : forall G1 TA dcs,
                      stpd G1 (TObj (Some TA) dcs) G1 (TObj (Some TA) dcs)  ->
                      stpd G1 (dc_type_and dcs) G1 (dc_type_and dcs).
Proof.
  intros.
  assert (stpd G1 (TObj (Some TA) dcs) G1 (dc_type_and dcs)).
  eapply stp_and2B. eauto.
  eapply stp_reg2. eauto.
Qed.

Lemma stpd_mem_abs : forall G1 TA dcs,
                      stpd G1 (TObj (Some TA) dcs) G1 (TObj (Some TA) dcs)  ->
                      stpd G1 (TObj (Some TA) dcs) G1 (TObj None dcs).
Proof.

  intros. unfold TObj. unfold TObj in H.

  eapply stpd_and2. eapply stpd_and11. eapply stpd_mema_sn.
    eapply stpd_obj_reg_mem. eauto.
    eapply stpd_obj_reg_dcs. eauto.
    eapply stp_and2B. apply H.
Qed.

Lemma nostp_inv_dcs_mem: forall dcs TA, (* not needed? *)
  nostp (dc_type_and dcs) (TMem TA).
Proof.
  intros.
  induction dcs.
  Case "nil". eauto.
  Case "cons".
    unfold dc_type_and. destruct a. destruct d.
    eapply nostp_and.
      eapply nostp_mem_fun.
      eapply IHdcs.
Qed.

Lemma stp_inv_obj_ex_mem: forall env0 env1 dcs TA TA2,
  stpd env0 (TObj (Some TA) dcs) env1 (TMem (Some TA2)) ->
      stpd env0 TA env1 TA2 /\ stpd env1 TA2 env0 TA.
Proof. intros.
       unfold TObj in H.
       assert (nostp (dc_type_and dcs) (TMem (Some TA2))). eapply nostp_inv_dcs_mem.
       eapply stp_andA in H. destruct H. inversion H.
       split; eexists; eauto.
       eauto. (* nostp *)
Qed.

Lemma stp_inv_obj_ex_mem0: forall env0 env1 dcs TA TA2,
  stpd env0 (TObj (Some TA) dcs) env1 (TMem (Some TA2)) ->
      stpd env0 (TObj (Some TA) dcs) env0 (TMem (Some TA)).
Proof. intros.
       assert (stpd env0 (dc_type_and dcs) env0 (dc_type_and dcs)).
         eapply stpd_obj_reg_dcs. eapply stp_reg1. eauto.
       unfold TObj in H.
       assert (nostp (dc_type_and dcs) (TMem (Some TA2))). eapply nostp_inv_dcs_mem.
       eapply stp_andA in H. destruct H. inversion H.
       subst.
       eapply stpd_and11. eapply stpd_mem_ss.
         eapply stpd_trans. eexists; eauto. eexists; eauto.
         eapply stpd_trans. eexists; eauto. eexists; eauto.
       eauto. (* dcs regularity *)
       eauto. (* nostp *)
Qed.

(* could use nostp *)
Lemma stp_inv_obj_bind_contra: forall env0 env1 dcs TA TB,
  stpd env0 (TObj (Some TA) dcs) env1 (TBind TB) ->
  False.
Proof.
  intros. destruct H. inversion H. inversion H4. subst. clear H.
  clear H7. generalize dependent n1.
  induction dcs; intros. inversion H4.
  simpl in H4. destruct a; destruct d.
  inversion H4; subst; inversion H3; eapply IHdcs; eapply H3.
Qed.

Lemma nostp_inv_dcs: forall m dcs T3 T4,
  length dcs <= m ->
  nostp (dc_type_and dcs) (TFun m T3 T4).
Proof.
  intros.
  induction dcs.
  Case "nil". eauto.
  Case "cons".
    assert (S (length dcs) = length (a::dcs)) as L. eauto.
    unfold dc_type_and. destruct a. destruct d.
    eapply nostp_and.
      eapply nostp_fun. omega.
      eapply IHdcs. omega.
Qed.

(* SOUNDNESS HELPERS / CONNECT STATIC <-> DYNAMIC *)

Hint Resolve stp_extend1.
Hint Resolve stp_extend2.
Hint Resolve stp_reg1.

Hint Resolve wft_extend.

Lemma valtp_extend : forall vs x v v1 T,
                       val_type vs v T ->
                       val_type ((x,v1)::vs) v T.
Proof. intros. induction H; eauto using stpd_extend2. Qed.

Lemma valtp_widen: forall G1 G2 T1 T2 v,
                     val_type G1 v T1 ->
                     stpd G1 T1 G2 T2 ->
                     val_type G2 v T2.
Proof.
  intros. induction H.
    Case "Bool". eapply v_bool. eapply stpd_trans; eauto.
    Case "Abs". eapply v_abs. eauto. eauto. eauto. eauto. eauto. eapply stpd_trans; eauto.
    Case "Pack". eapply v_pack. eauto. eauto. eauto. eapply stpd_trans; eauto.
Qed.

Lemma wf_length : forall vs ts,
                    wf_env vs ts ->
                    (length vs = length ts).
Proof. intros. induction H. auto.
assert ((length ((n,v)::vs)) = 1 + length vs). constructor.
assert ((length ((n,t)::ts)) = 1 + length ts). constructor.
rewrite IHwf_env in H1. auto. Qed.

Hint Resolve wf_length.

Lemma index_safe_ex: forall H1 G1 TF i,
             wf_env H1 G1 ->
             index i G1 = Some TF ->
             exists v, index i H1 = Some v /\ val_type H1 v TF.
Proof. intros. induction H.
       Case "nil". inversion H0.
       Case "cons". inversion H0.
         case_eq (beq_nat i (length ts)).
           SCase "hit".
             intros E.
             rewrite E in H3. inversion H3. subst t.
             assert (beq_nat i (length vs) = true). eauto.
             assert (index i ((n, v) :: vs) = Some v).  eauto. unfold index. rewrite H2. eauto.
             eauto.
           SCase "miss".
             intros E.
             assert (beq_nat i (length vs) = false). eauto.
             rewrite E in H3.
             assert (exists v0, index i vs = Some v0 /\ val_type vs v0 TF) as HI. eapply IHwf_env. eauto.
           inversion HI as [v0 HI1]. inversion HI1.
           eexists. econstructor. eapply index_extend; eauto. eapply valtp_extend; eauto.
Qed.

Lemma index_safe_ex2: forall H1 G1 f0 dc TA i TF,
             wf_env H1 G1 ->
             index i ((f0, TObj (Some TA) dc):: G1) = Some TF ->
             exists v, index i ((f0, vabs H1 f0 TA dc):: H1) = Some v.
Proof. intros.
       inversion H0.
       case_eq (beq_nat i (length G1)).
         Case "hit".
           intros. rewrite H2 in H3. inversion H3. eexists.
           assert (beq_nat i (length H1) = true). eauto. unfold index.
           rewrite H4. reflexivity.
         Case "miss".
           intros. assert (beq_nat i (length H1) = false). eauto.
           rewrite H2 in H3.
           assert (exists v, index i H1 = Some v /\ val_type H1 v TF) as HI. eapply index_safe_ex; eauto.
         inversion HI as [v0 HI1]. inversion HI1.
         eexists. eapply index_extend. eauto.
 Qed.

(* structural helpers -- these could be tactified directly *)
Lemma clos_extract_fun: forall x1 (venv1:env) v j m T1 T2,
  (index x1 venv1 = Some v) \/ (closed j (TFun m T1 T2)) ->
  ((index x1 venv1 = Some v) \/ (closed j T1)) /\
  ((index x1 venv1 = Some v) \/ (closed j T2)).
Proof.
  intros. destruct H.
  split. left. eauto. left. eauto.
  inversion H.
  split. right. eauto. right. eauto.
Qed.

Lemma clos_extract_and: forall x1 (venv1:env) v j T1 T2,
  (index x1 venv1 = Some v) \/ (closed j (TAnd T1 T2)) ->
  ((index x1 venv1 = Some v) \/ (closed j T1)) /\
  ((index x1 venv1 = Some v) \/ (closed j T2)).
Proof.
  intros. destruct H.
  split. left. eauto. left. eauto.
  inversion H.
  split. right. eauto. right. eauto.
Qed.

Lemma clos_extract_mem: forall x1 (venv1:env) v j T1,
  (index x1 venv1 = Some v) \/ (closed j (TMem (Some T1))) ->
  (index x1 venv1 = Some v) \/ (closed j T1).
Proof.
  intros. inversion H.
  left. eauto.
  right. inversion H0. eauto.
Qed.

Lemma clos_extract_bind: forall x1 (venv1:env) v j T1,
  (index x1 venv1 = Some v) \/ (closed j (TBind T1)) ->
  (index x1 venv1 = Some v) \/ (closed (S j) T1).
Proof.
  intros. inversion H.
  left. eauto.
  right. inversion H0. eauto.
Qed.

(* self type/bind packing / unpacking *)
Lemma open2stp: forall venv0 venv1 x0 x1 j v n1 T1 T2,
  (index x0 venv0 = Some v) \/ (closed j T1) ->
  (index x1 venv1 = Some v) \/ (closed j T2) ->
  stp n1 venv0 T1 venv1 T2 ->
  stpd venv0 (open_rec j x0 T1) venv1 (open_rec j x1 T2).
Proof.
  intros.
  remember (n1) as n. rewrite Heqn in H1.
  assert (n1 <= n). omega. clear Heqn.
  gen n1 venv0 venv1 x0 x1 j T1 T2.
  induction n.
  (*0*) intros. assert (n1 = 0). omega. subst.
  inversion H1; subst;
  try solve [inversion H1]; try solve [inversion H2];
  try solve [exists 0; eauto].

  SCase "SelB < SelB". inversion H1. subst. eauto.
    unfold open. unfold open_rec.
    case_eq (beq_nat j x2); intros E. eapply beq_nat_true_iff in E.
    exists 0. eapply stp_selx.
      inversion H. eauto. inversion H3. omega.
      inversion H0. eauto. inversion H3. omega.
      eauto.

  (* S n *)
  intros. (* sloooow *)

  inversion H1; subst; eauto; try solve by inversion.

  Ltac vc H := try (eapply clos_extract_fun in H; inversion H; clear H);
               try (eapply clos_extract_mem in H);
               try (eapply clos_extract_and in H; inversion H; clear H);
               try (eapply clos_extract_bind in H).

  Case "Fun < Fun". vc H. vc H0. eapply stpd_fun.
    eapply IHn with n0. omega. eauto. eauto. eauto.
    eapply IHn with n2. omega. eauto. eauto. eauto.

  Case "Mem < Mem". vc H. vc H0.  eapply stpd_mem_ss.
    eapply IHn with n0. omega. eauto. eauto. eauto.
    eapply IHn with n2. omega. eauto. eauto. eauto.

  Case "Mem < Mem None". vc H. eapply stpd_mema_sn.
    eapply IHn with n0. omega. eauto. eauto. eauto.

  Case "Bind < Bind". vc H. vc H0.  eapply stpd_bind.
    eapply IHn with n0. omega. eauto. eauto. eauto.

  Case "And1 < ?". vc H. vc H0.  eapply stpd_and11.
    eapply IHn with n0. omega. eauto. eauto. eauto.
    eapply IHn with n2. omega. eauto. eauto. eauto.

  Case "And2 < ?". vc H. vc H0.  eapply stpd_and12.
    eapply IHn with n0. omega. eauto. eauto. eauto.
    eapply IHn with n2. omega. eauto. eauto. eauto.

  Case "? < And2". vc H. vc H0.  eapply stpd_and2.
    eapply IHn with n0. omega. eauto. eauto. eauto.
    eapply IHn with n2. omega. eauto. eauto. eauto.

  (* for SEL, we rely on TA being closed *)
  Case "? < Sel". eapply stpd_sel2.
    eauto. eauto.
    assert (closed j TA) as CLOS. eapply closed_upgrade; eauto. omega.
    assert (TA = open_rec j x TA) as OPEN_ID.
      eapply closed_no_open; eauto.

    remember ((f0, vabs GC f0 TA dcs) :: GC) as GC0.
    rewrite OPEN_ID.
    eapply IHn with n2. omega. eauto. right. apply CLOS. eauto.

  Case "Sel < ?". eapply stpd_sel1.
    eauto. eauto.
    assert (closed j TA) as CLOS. eapply closed_upgrade; eauto. omega.
    assert (TA = open_rec j x TA) as OPEN_ID.
      eapply closed_no_open; eauto.

    remember ((f0, vabs GC f0 TA dcs) :: GC) as GC0.
    rewrite OPEN_ID.
    eapply IHn with n2. omega. right. apply CLOS. eauto. eauto.

Qed.

(* STATIC WF IMPLIES DYNAMIC STP *)

Lemma stp_wf_refl: forall G1 H1 T1,
                     wf_env H1 G1 ->
                     wf_type G1 T1 ->
                     stpd H1 T1 H1 T1.
Proof.
  intros. wf_cases (induction H0) Case; try solve [exists 0; auto]; try solve [auto 4]. (* eauto would use trans *)
  Case "And".
    eapply stpd_and2.
      eapply stpd_and11. eapply IHwf_type1. eauto. eapply IHwf_type2. eauto.
      eapply stpd_and12. eapply IHwf_type2. eauto. eapply IHwf_type1. eauto.
  Case "Mem".
    eapply stpd_mem_ss; eapply IHwf_type; eauto.
  Case "Fun".
    eapply stpd_fun. eapply IHwf_type1; eauto. eapply IHwf_type2; eauto.
  Case "Sel".
    assert (exists v, index x0 H1 = Some(v) /\ val_type H1 v TE) as IE.
    SCase "IE". eapply index_safe_ex. eauto. eauto.
    inversion IE as [v IE']. inversion IE' as [IE1 IE2].
    exists 0. eapply stp_selx. eapply IE1. eapply IE1.
  Case "Bind".
    eapply stpd_bind; eapply IHwf_type; eauto.
Qed.

Lemma stp_cf_refl: forall G1 H1 T1 TA f dc,
                     wf_env H1 G1 ->
                     wf_type ((f,TObj (Some TA) dc)::G1) T1 ->
                     stpd ((f,vabs H1 f TA dc)::H1) T1 ((f,vabs H1 f TA dc)::H1) T1.
Proof.
  intros.
  remember ((f0, TObj (Some TA) dc0) :: G1) as G2.
  remember ((f0, vabs H1 f0 TA dc0) :: H1) as H2.
  wf_cases (induction H0) Case; try solve [exists 0; auto]; try solve [auto 4].
  Case "And".
    eapply stpd_and2.
      eapply stpd_and11. eapply IHwf_type1. eauto. eapply IHwf_type2. eauto.
      eapply stpd_and12. eapply IHwf_type2. eauto. eapply IHwf_type1. eauto.
  Case "Mem".
    eapply stpd_mem_ss; eapply IHwf_type; eauto.
  Case "Fun".
    eapply stpd_fun. eapply IHwf_type1; eauto. eapply IHwf_type2; eauto.
  Case "Sel".
    assert (exists v, index x0 H2 = Some v) as IE.
    SCase "IE". subst. eapply index_safe_ex2. eauto. subst. eauto.
    inversion IE as [v IE1].
    exists 0. eapply stp_selx. eapply IE1. eapply IE1.
  Case "Bind".
    eapply stpd_bind; eapply IHwf_type; eauto.
Qed.

(*
Note:

We need to consider wf_type and stp with extended environment relative
to wf_env, because this is part of the construction of a val_type that
will become part of the extended wf_env!
*)

(* STATIC STP IMPLIES DYNAMIC STP *)
(*
Lemma tresolve2stp: forall x H1 H2 T1 T2 T3,
                 stpd H1 T1 H2 T2 ->
                 tresolve x T2 T3 ->
                 stpd H1 T1 H2 T3.
Proof.
  intros.
  tresolve_cases (induction H0) Case.
  Case "Self". eauto.
  Case "And1". eapply IHtresolve. eapply stp_and2A. eauto.
  Case "And2". eapply IHtresolve. eapply stp_and2B. eauto.
  Case "Bind". eapply IHtresolve. inversion H. inversion H4. subst.
Qed.
*)
Lemma valtp_widen_tresolve: forall G1 T1 T2 x v,
                     index x G1 = Some v ->
                     val_type G1 v T1 ->
                     tresolve x T1 T2 ->
                     val_type G1 v T2.
Proof.
  intros.
  tresolve_cases (induction H1) Case.
  Case "Self". eauto.
  Case "And1".
    eapply IHtresolve. eauto. inversion H0; subst; econstructor; repeat eauto.
    (*b*) eapply stp_and2A; eauto.
    (*a*) eapply stp_and2A; eauto.
    (*p*) inversion H5. inversion H4. eauto.
  Case "And2".
    eapply IHtresolve. eauto. inversion H0; subst; econstructor; repeat eauto.
    (*b*) eapply stp_and2B; eauto.
    (*a*) eapply stp_and2B; eauto.
    (*p*) inversion H5. inversion H4. eauto.
  Case "Bind".
    (* bool *) inversion H0. subst. destruct H3. inversion H1.
    (* abs  *) assert False. eapply stp_inv_obj_bind_contra. subst. eauto. contradiction.
    (* pack *) subst. inversion H6. inversion H1.
    eapply IHtresolve. eauto. eapply valtp_widen. apply H4. eapply open2stp; eauto.
(*
  induction H; econstructor.
    Case "Bool". eapply tresolve2stp; eauto.
    Case "Abs". eauto. eauto. eauto. eauto. eauto. eapply tresolve2stp;  eauto.
    Case "Pack". eauto. eauto. eauto. eapply tresolve2stp; eauto. *)
Qed.

Lemma atp2stp: forall G1 H1 T1 T2,
                 wf_env H1 G1 ->
                 atp G1 T1 T2 ->
                 stpd H1 T1 H1 T2.
Proof.
  intros.
  atp_cases (induction H0) Case.
  Case "? < Sel".
    assert (exists v, index x0 H1 = Some v /\ val_type H1 v TF) as IE.
    SCase "IE". eapply index_safe_ex; eauto.
      inversion IE as [v [IE1 IE2]].

    assert (val_type H1 v (TMem (Some T))) as JE.
    SCase "JE". eapply valtp_widen_tresolve; eauto.

    destruct v.
    SCase "bool". inversion JE.
      subst. inversion H5. inversion H3. (* not bool / bool *)
      subst. inversion H6. inversion H5. (* not bool / pack *)

    SCase "abs". inversion JE.
      SSCase "vabs".
        assert (stpd ((i, vabs l i t l0) :: l) t H1 T /\
                stpd H1 T ((i, vabs l i t l0) :: l) t) as [HXL HXR].
          eapply stp_inv_obj_ex_mem. subst. apply H14. (* stp HC TC H1 T *)

        eapply stpd_sel2. apply IE1. eauto. apply HXR.
      SSCase "vpack".
        destruct H6. inversion H6.

  Case "Sel < ?".

    assert (exists v, index x0 H1 = Some v /\ val_type H1 v TF) as IE.
    SCase "IE". eapply index_safe_ex; eauto.
      inversion IE as [v IE']. inversion IE' as [IE1 IE2].

    assert (val_type H1 v (TMem (Some T))) as JE.
    SCase "JE". eapply valtp_widen_tresolve; eauto.

    destruct v.
    SCase "bool". inversion JE.
      subst. inversion H5. inversion H3. (* not bool / bool *)
      subst. inversion H6. inversion H5. (* not bool / pack *)

    SCase "abs". inversion JE.
      SSCase "vabs".
        assert (stpd ((i, vabs l i t l0) :: l) t H1 T /\
                stpd H1 T ((i, vabs l i t l0) :: l) t) as [HXL HXR].
          eapply stp_inv_obj_ex_mem. subst. apply H14. (* stp HC TC H1 T *)

        eapply stpd_sel1. apply IE1. eauto. apply HXL.
      SSCase "vpack".
        destruct H6. inversion H6.
Qed.

Lemma valtp_widen_atp: forall G H T1 T2 v,
                     val_type H v T1 ->
                     wf_env H G ->
                     atp G T1 T2 ->
                     val_type H v T2.
Proof.
  intros. eapply valtp_widen. eauto. eauto. eapply atp2stp. eauto. eauto.
Qed.

(* VALUE TYPING INVERSION LEMMAS *)

Hint Resolve stp_extend1.
Hint Resolve stp_extend2.
Hint Resolve stp_reg1.
Hint Resolve stp_wf_refl.

Hint Resolve wft_extend.
Hint Resolve valtp_widen.

Lemma stp_inv_obj_ex: forall m dcs env0 env1 T3 T4 TA,
  stpd env0 (TObj (Some TA) dcs) env1 (TFun m T3 T4) ->
  exists T1 T2 x ey,
    index m dcs = Some (dfun T1 T2 x ey) /\
    stpd env0 (TFun m T1 T2) env1 (TFun m T3 T4).
Proof.
  intros.

  unfold TObj in H.
  eapply stp_andB in H. (* it's not the TMem *)

  induction dcs.
  Case "nil". destruct H. inversion H. (* can't happen *)
  Case "cons".
    case_eq (beq_nat m (length dcs)); intros E.
    SCase "hit".
      unfold dc_type_and in H. destruct a. destruct d. subst.
      assert (m = length dcs) as L. eapply beq_nat_true; eauto.

      eexists. eexists. eexists. eexists. eexists.
      unfold index. rewrite E. reflexivity.
      eapply stp_andA. subst m. eapply H. eapply nostp_inv_dcs. omega.
    SCase "miss".
      unfold dc_type_and in H. destruct a. destruct d.
      assert (not (m = length dcs)) as L. eapply beq_nat_false; eauto.
      assert (exists T1 T2 x ey,
  index m dcs = Some (dfun T1 T2 x ey) /\
  stpd env0 (TFun m T1 T2) env1 (TFun m T3 T4)) as HI. eapply IHdcs.
        eapply stp_andB. eapply H. eapply nostp_fun. eauto.

  destruct HI as [T1 [T2 [x0 [ey [IX ST]]]]].
  eexists. eexists. eexists. eexists. eexists.
  eapply index_extend; eauto. eauto.

  eapply nostp_fun_mem.
Qed.

Lemma dc_inv_has_type: forall m x ey dcs tenv0 T1 T2,
  index m dcs = Some (dfun T1 T2 x ey) ->
  dc_has_type tenv0 dcs ->
  has_type ((x,T1) :: tenv0) ey T2.
Proof.
  intros.
  induction dcs.
  Case "nil". inversion H.
  Case "cons". inversion H. destruct a.
    case_eq (beq_nat m (length dcs)); intros E; rewrite E in H2; inversion H2; subst.
    SCase "hit". inversion H0. eauto.
    SCase "miss". inversion H0. eapply IHdcs; eauto.
Qed.

Lemma invert_abs: forall venv vf vx m T1 T2,
  val_type venv vf (TFun m T1 T2) ->
  exists env tenv f x y dcs T3 T4 TA TF,
    TF = TObj (Some TA) dcs /\
    vf = (vabs env f TA dcs) /\
    wf_env env tenv /\
    wf_type ((f,TF)::tenv) TF /\
    closed 0 TA /\
    dc_has_type ((f, TF) :: tenv) dcs /\
    index m dcs = Some (dfun T3 T4 x y) /\
    has_type ((x,T3)::(f,TF)::tenv) y T4 /\
    stpd venv T1 ((x,vx)::(f,vf)::env) T3 /\
    stpd ((x,vx)::(f,vf)::env) T4 venv T2.
Proof.
  (*  intros. inversion H. repeat eexists; repeat eauto. *)

  intros. inversion H. destruct H0. inversion H0. (* bool case *)

  assert (exists T3 T4 x y, index m dcs = Some (dfun T3 T4 x y) /\
         stpd ((f0, vabs env0 f0 TA dcs) :: env0) (TFun m T3 T4) venv (TFun m T1 T2)). eapply stp_inv_obj_ex. subst TF. eapply H5.

  destruct H9 as [T3 [T4 [x0 [y0 [IX ST]]]]].

  subst TF.
  destruct ST as [nx ST]. inversion ST.

  repeat eexists.
  eauto. eauto. eauto. eauto. eauto. eauto. eapply dc_inv_has_type; eauto.
  eauto. eauto.

  (*pack*) inversion H3. inversion H7.
Qed.

(* FINAL THEOREM *)

Inductive res_type: env -> option vl -> ty -> Prop :=
| not_stuck: forall v T venv,
      val_type venv v T ->
      res_type venv (Some v) T.

Hint Constructors res_type.
Hint Resolve not_stuck.

(* if not timed out, then result is not stuck, and well-typed *)
Theorem full_safety : forall n e tenv venv res T,
  teval n venv e = Some res -> has_type tenv e T -> wf_env venv tenv ->
  res_type venv res T.

Proof.
  intros n. induction n.
  (* 0 *)   intros. inversion H.
  (* S n *) intros. destruct e; inversion H; inversion H0.

  Case "True".  eapply not_stuck. eapply v_bool. exists 0. eauto.
  Case "False". eapply not_stuck. eapply v_bool. exists 0. eauto.

  Case "Var".
    SCase "TVar".
      destruct (index_safe_ex venv tenv0 T i) as [v [I V]]; eauto.
      rewrite I. eapply not_stuck. eapply V.
    SCase "TVara".
      destruct (index_safe_ex venv tenv0 T0 i) as [v [I V]]; eauto.
      rewrite I. eapply not_stuck. eapply valtp_widen_atp. eapply V. eauto. eauto.
    SCase "TVar pack".
      destruct (index_safe_ex venv tenv0 T0 i) as [v [I V]]; eauto.
      rewrite I. eapply not_stuck. eapply v_pack. apply I. apply V. eauto. eauto.
    SCase "TVar unpack".
      destruct (index_safe_ex venv tenv0 (TBind T2) i) as [v [I V]]; eauto.
      inversion V.
        (* bool *) destruct H10. inversion H10.
        (* abs  *) assert False. eapply stp_inv_obj_bind_contra. subst. apply H15. contradiction.
        (* pack *) subst. inversion H13. inversion H2. subst.

      rewrite I. eapply not_stuck. eapply valtp_widen. apply H11.
      eapply open2stp; eauto.

  Case "App".
    (*remember (teval n venv e1) as tf.*)
    remember (teval n venv e) as tx.
    subst T.

    destruct tx as [rx|]; try solve by inversion.
    assert (res_type venv rx T1) as HRX. SCase "HRX". subst. eapply IHn; eauto.
    inversion HRX as [vx].

    (*
    destruct tf as [rf|]; subst rx; try solve by inversion.
    assert (res_type venv rf (TFun T1 T2)) as HRF. SCase "HRF". subst. eapply IHn; eauto.
    inversion HRF as [vf].
     *)

    destruct (index_safe_ex venv tenv0 TF i) as [vf [I V]]; eauto.

    eapply valtp_widen_tresolve in V; eauto.
    subst i0.
    destruct (invert_abs venv vf vx m T1 T2) as
        [env1 [tenv [f1 [x1 [y1 [dcs [T3 [T4 [TA [TF1
        [ETF [EVF [WFE [WFT [ CLS [HDCS [HDC [HTY [STX STY
        ]]]]]]]]]]]]]]]]]]]. eapply V.
    (* now we know it's a closure, and we have has_type evidence,
    so we can check the body *)

    rewrite I in H3.
    assert (res_type ((x1,vx)::(f1,vf)::env1) res T4) as HRY.
      SCase "HRY".
        subst. rewrite HDC in H3. eapply IHn. eauto. eauto.
        (* wf_env f x *) econstructor. eapply valtp_widen; eauto.
        (* wf_env f   *) econstructor. eapply v_abs; eauto.  eapply stp_cf_refl; eauto.
        eauto.

    inversion HRY as [vy].

    eapply not_stuck. eapply valtp_widen; eauto.

  Case "Abs".
    remember (teval n ((i, vabs venv i t l) :: venv) e) as tx.
    destruct tx as [rx|]; subst; try solve by inversion.

     remember i as f0.
     remember l as dcs.
     remember venv as env0.
     remember (TObj (Some t) dcs) as TF.
     remember (TObj None     dcs) as TFA.
     remember ((f0, vabs env0 f0 t dcs) :: env0) as venvf.

     assert (stpd venvf TF venvf TF) as ST0. SCase "ST0".
       subst. eapply stp_cf_refl; eauto.
     assert (stpd venvf TF venvf TFA) as STA. SCase "STA".
       subst. eapply stpd_mem_abs. eauto.
     assert (res_type venvf res T) as HI. SCase "HI".
       subst. eapply IHn; eauto.
     inversion HI.

     subst. eapply not_stuck. eapply valtp_widen. eauto.
       eapply stpd_extend1. eapply stp_reg1. eapply stp_wf_refl; eauto.

   Case "Let".
     remember (teval n venv e1) as tx.
     destruct tx as [rx|]; subst; try solve by inversion.
     assert (res_type venv rx t) as HRX. SCase "HRX". subst. eapply IHn; eauto.
     inversion HRX as [vx].

     subst.

     assert (res_type ((i, vx) :: venv) res T) as HI. SCase "HI".
       subst. eapply IHn; eauto. constructor. eapply valtp_widen. eauto.
       eapply stpd_extend2. eapply stp_wf_refl. eauto. eapply hastp_wf. eauto. eauto.
     inversion HI.

     subst. eapply not_stuck. eapply valtp_widen. eauto.
       eapply stpd_extend1. eapply stp_reg1. eapply stp_wf_refl; eauto.

Qed.

End DOT.


Require Import MirrorCore.Lambda.Expr.
Require Import MirrorCore.Lambda.ExprUnify_simul.
Require Import MirrorCore.RTac.RTac.
Require McExamples.Simple.Simple.
Require Import MirrorCore.syms.SymEnv.
Require Import MirrorCore.syms.SymSum.

Inductive Even : nat -> Prop :=
| E0 : Even 0
| EO : forall n, Odd n -> Even (S n)
with Odd : nat -> Prop :=
| OE : forall n, Even n -> Odd (S n).

Let typ := Simple.typ.
Let func := (SymEnv.func + nat)%type.
Let tyNat := Simple.tyNat.
Let tyProp := Simple.tyProp.
Let tyArr := Simple.tyArr.

Definition args : functions _ :=
  from_list ({| ftype := tyNat ; fdenote := 0 |} ::
             {| ftype := tyArr tyNat tyNat ; fdenote := S |} ::
             {| ftype := tyArr tyNat tyProp ; fdenote := Even |} ::
             {| ftype := tyArr tyNat tyProp ; fdenote := Odd |} :: nil).

Let Zero_syn : expr typ func := ExprCore.Inj (inl 1%positive).
Let S_syn : expr typ func := ExprCore.Inj (inl 2%positive).
Let Even_syn : expr typ func := ExprCore.Inj (inl 3%positive).
Let Odd_syn : expr typ func := ExprCore.Inj (inl 4%positive).

Definition E0_syn : Lemma.lemma typ (expr typ func) (expr typ func) :=
{| Lemma.vars := nil
 ; Lemma.premises := nil
 ; Lemma.concl := App Even_syn Zero_syn
 |}.

Definition EO_syn : Lemma.lemma typ (expr typ func) (expr typ func) :=
{| Lemma.vars := tyNat :: nil
 ; Lemma.premises := App Odd_syn (ExprCore.Var 0) :: nil
 ; Lemma.concl := App Even_syn (App S_syn (ExprCore.Var 0))
 |}.

Definition OE_syn : Lemma.lemma typ (expr typ func) (expr typ func) :=
{| Lemma.vars := tyNat :: nil
 ; Lemma.premises := App Even_syn (ExprCore.Var 0) :: nil
 ; Lemma.concl := App Odd_syn (App S_syn (ExprCore.Var 0))
 |}.

Local Existing Instance Simple.RType_typ.
Definition RSym_nat : @RSym typ _ nat :=
{| typeof_sym := fun _ => Some tyNat
 ; symD := fun x => x
 ; sym_eqb := fun x y => Some (EqNat.beq_nat x y)
 |}.

Local Instance RSym_func : RSym func := RSym_sum (@RSym_func _ _ args) RSym_nat.
Local Instance RSymOk_func : RSymOk RSym_func := @RSymOk_sum _ _ _ _ _ _ _ _.
constructor. simpl.
intros. case PeanoNat.Nat.eqb_spec; auto.
Defined.

Local Instance Expr_expr : Expr _ (expr typ func) := @Expr_expr typ func _ _ _.
Local Instance ExprOk_expr : @ExprOk _ _ (expr typ func) _ := @ExprOk_expr typ func _ _ _ _ _ _.

Instance ReifiedLemma_E0_syn : ReifiedLemma E0_syn := { ReifiedLemma_proof := E0 }.
Instance ReifiedLemma_EO_syn : ReifiedLemma EO_syn := { ReifiedLemma_proof := EO }.
Instance ReifiedLemma_OE_syn : ReifiedLemma OE_syn := { ReifiedLemma_proof := OE }.

Let APPLY_no_minify (l : Lemma.lemma typ (expr typ func) (expr typ func)) : rtac typ (expr typ func) :=
  APPLY (fun subst _ _ => @exprUnify subst typ _ Simple.RType_typ _ _ _ _ 10) l.

Let APPLY (l : Lemma.lemma typ (expr typ func) (expr typ func)) : rtac typ (expr typ func) :=
  THEN (APPLY_no_minify l) MINIFY.

Theorem APPLY_sound
: forall l,
    ReifiedLemma l ->
    rtac_sound (APPLY l).
Proof.
  intros. unfold APPLY.
  eapply THEN_sound; eauto using MINIFY_sound with typeclass_instances.
  eapply APPLY_sound; eauto using exprUnify_sound, APPLY_sound, MINIFY_sound with typeclass_instances.
Qed.

Definition even_odd_tac : rtac typ (expr typ func) :=
  REPEAT 2000 (FIRST [ APPLY E0_syn
                     | APPLY EO_syn
                     | APPLY OE_syn ]).

Definition build_n (n : nat) : expr typ func := Inj (inr n).

Definition runRTac_empty_goal (tac : rtac typ (expr typ func))
           (goal : expr typ func)  :=
  (@runOnGoals _ _ _ _ tac)
        nil nil 0 0 _ (@TopSubst _ _ nil nil)
        (@GGoal typ (expr typ func) goal).

Theorem even_odd_tac_sound
: rtac_sound even_odd_tac.
Proof.
  eapply REPEAT_sound.
  eapply FIRST_sound.
  repeat first [ econstructor; [ eapply APPLY_sound; eauto with typeclass_instances | ]
               | solve [ constructor ] ].
Qed.

(*Time Eval vm_compute in runRTac_empty_goal even_odd_tac (App Even_syn (build_n 1024)).*)

Definition GoalD (us vs : env) (g : Goal typ (expr typ func)) : Prop :=
  let (tus, us0) := split_env us in
  let (tvs, vs0) := split_env vs in
  match @goalD typ (expr typ func) _ _ _ tus tvs g with
  | Some P => P us0 vs0
  | None => True
  end.

Definition full_solved : @Result typ (expr typ func) (CTop nil nil) :=
  Solved (TopSubst (expr typ func) nil nil).

Theorem generic_soundness
: forall tac,
    rtac_sound tac ->
    forall e,
    runRTac_empty_goal tac e = full_solved ->
    GoalD nil nil (GGoal e).
Proof.
  intros. unfold rtac_sound, runRTac_empty_goal in *.
  eapply H in H0.
  simpl in H0.
  destruct H0.
  - constructor.
  - constructor.
  - red. simpl.
    destruct (Ctx.propD nil nil e).
    + destruct H1; auto.
    + auto.
Qed.

Theorem runGoal_sound
: forall e,
    runRTac_empty_goal even_odd_tac e = full_solved ->
    GoalD nil nil (GGoal e).
Proof.
  intros; eapply generic_soundness; eauto using even_odd_tac_sound.
Qed.

Let Const (n : nat) : expr typ func := ExprCore.Inj (inr n).

Definition pE0_syn : Lemma.lemma typ (expr typ func) (expr typ func) :=
{| Lemma.vars := nil
 ; Lemma.premises := nil
 ; Lemma.concl := App Even_syn (Const 0)
 |}.

Definition pEO_syn (n : nat) : Lemma.lemma typ (expr typ func) (expr typ func) :=
{| Lemma.vars := nil
 ; Lemma.premises := App Odd_syn (Const n) :: nil
 ; Lemma.concl := App Even_syn (Const (S n))
 |}.

Definition pOE_syn (n : nat) : Lemma.lemma typ (expr typ func) (expr typ func) :=
{| Lemma.vars := nil
 ; Lemma.premises := App Even_syn (Const n) :: nil
 ; Lemma.concl := App Odd_syn (Const (S n))
 |}.

Instance ReifiedLemma_pE0_syn : ReifiedLemma pE0_syn := { ReifiedLemma_proof := E0 }.
Instance ReifiedLemma_pEO_syn n : ReifiedLemma (pEO_syn n) := { ReifiedLemma_proof := EO n }.
Instance ReifiedLemma_pOE_syn n : ReifiedLemma (pOE_syn n) := { ReifiedLemma_proof := OE n }.

Definition even_odd_tac_nrec (fuel : nat) : rtac typ (expr typ func) :=
  REC fuel
      (fun tac =>
         AT_GOAL (fun _ _ e =>
                    match e with
                    | App (ExprCore.Inj (inl 3%positive))
                          (ExprCore.Inj (inr 0)) =>
                      APPLY pE0_syn
                    | App (ExprCore.Inj (inl 3%positive))
                          (ExprCore.Inj (inr (S n))) =>
                      APPLY (pEO_syn n) ;; runOnGoals tac
                    | App (ExprCore.Inj (inl 4%positive))
                          (ExprCore.Inj (inr (S n))) =>
                      APPLY (pOE_syn n) ;; runOnGoals tac
                    | _ => FAIL
                    end))%rtac
       FAIL.

Fixpoint even_odd_tac_mf (n : nat) : rtac typ (expr typ func) :=
  match n with
  | 0 => APPLY pE0_syn
  | S n => FIRST [ APPLY (pEO_syn n) | APPLY (pOE_syn n) ] ;;
                 (fun x => runOnGoals (even_odd_tac_mf n) x)
  end%rtac.

Theorem runGoal_sound_mf
: forall n,
    runRTac_empty_goal (even_odd_tac_mf n) (App Even_syn (Const n)) = full_solved ->
    GoalD nil nil (GGoal (App Even_syn (Const n))).
Proof.
  intros; eapply generic_soundness; eauto.
  clear. induction n.
  { simpl. eapply APPLY_sound; eauto with typeclass_instances. }
  { Opaque FIRST. simpl.
    rtac_derive_soundness_default; eapply APPLY_sound; eauto with typeclass_instances. }
Qed.

Theorem runGoal_sound_nonrec
: forall n,
    runRTac_empty_goal (even_odd_tac_nrec n) (App Even_syn (Const n)) = full_solved ->
    GoalD nil nil (GGoal (App Even_syn (Const n))).
Proof.
  intros; eapply generic_soundness; eauto.
  unfold even_odd_tac_nrec.
  rtac_derive_soundness_default;
  eapply APPLY_sound; eauto with typeclass_instances.
Qed.

(*
Time Eval vm_compute in let n := 2000 in runRTac_empty_goal (even_odd_tac_mf n) (App Even_syn (Const n)).

Time Eval vm_compute in let n := 2000 in runRTac_empty_goal (even_odd_tac_nrec n) (App Even_syn (Const n)).
*)

Goal Even 512.
  Time match goal with
    | |- Even ?X =>
      pose (GoalD nil nil (GGoal (App Even_syn (Const X)))) ;
      apply (runGoal_sound_nonrec X); vm_compute; reflexivity
  end.
Time Qed.


From Test Require Import tactic.

Section FOFProblem.

Variable Universe : Set.
Variable UniverseElement : Universe.

Variable par_ : Universe -> Universe -> Universe -> Universe -> Prop.
Variable oS_ : Universe -> Universe -> Universe -> Universe -> Prop.
Variable congA_ : Universe -> Universe -> Universe -> Universe -> Universe -> Universe -> Prop.
Variable cong_ : Universe -> Universe -> Universe -> Universe -> Prop.
Variable col_ : Universe -> Universe -> Universe -> Prop.
Variable betS_ : Universe -> Universe -> Universe -> Prop.

Variable defsameside_1 : (forall P Q A B : Universe, (exists X U V : Universe, (oS_ P Q A B -> (col_ A B U /\ (col_ A B V /\ (betS_ P U X /\ (betS_ Q V X /\ (~(col_ A B P) /\ ~(col_ A B Q))))))))).
Variable defsameside2_2 : (forall P Q A B X U V : Universe, ((col_ A B U /\ (col_ A B V /\ (betS_ P U X /\ (betS_ Q V X /\ (~(col_ A B P) /\ ~(col_ A B Q)))))) -> oS_ P Q A B)).
Variable lemma_NCdistinct_3 : (forall A B C : Universe, (~(col_ A B C) -> (A <> B /\ (B <> C /\ (A <> C /\ (B <> A /\ (C <> B /\ C <> A))))))).
Variable lemma_inequalitysymmetric_4 : (forall A B : Universe, (A <> B -> B <> A)).
Variable lemma_extension_5 : (forall A B P Q : Universe, (exists X : Universe, ((A <> B /\ P <> Q) -> (betS_ A B X /\ cong_ B X P Q)))).
Variable axiom_betweennesssymmetry_6 : (forall A B C : Universe, (betS_ A B C -> betS_ C B A)).
Variable proposition_28A_7 : (forall A B C D E G H : Universe, ((betS_ A G B /\ (betS_ C H D /\ (betS_ E G H /\ (congA_ E G B G H D /\ oS_ B D G H)))) -> par_ A B C D)).
Variable defcollinear_8 : (forall A B C : Universe, (col_ A B C -> (A = B \/ (A = C \/ (B = C \/ (betS_ B A C \/ (betS_ A B C \/ betS_ A C B))))))).
Variable defcollinear2a_9 : (forall A B C : Universe, (A = B -> col_ A B C)).
Variable defcollinear2b_10 : (forall A B C : Universe, (A = C -> col_ A B C)).
Variable defcollinear2c_11 : (forall A B C : Universe, (B = C -> col_ A B C)).
Variable defcollinear2d_12 : (forall A B C : Universe, (betS_ B A C -> col_ A B C)).
Variable defcollinear2e_13 : (forall A B C : Universe, (betS_ A B C -> col_ A B C)).
Variable defcollinear2f_14 : (forall A B C : Universe, (betS_ A C B -> col_ A B C)).
Variable lemma_collinearorder_15 : (forall A B C : Universe, (col_ A B C -> (col_ B A C /\ (col_ B C A /\ (col_ C A B /\ (col_ A C B /\ col_ C B A)))))).
Variable lemma_collinearparallel_16 : (forall A B C Xc Xd : Universe, ((par_ A B Xc Xd /\ (col_ Xc Xd C /\ C <> Xd)) -> par_ A B C Xd)).
Variable lemma_parallelsymmetric_17 : (forall A B C D : Universe, (par_ A B C D -> par_ C D A B)).

Theorem proposition_28D_18 : (forall B D E G H : Universe, ((betS_ E G H /\ (congA_ E G B G H D /\ oS_ B D G H)) -> par_ G B H D)).
Proof.
  time tac.
Qed.

End FOFProblem.


Require Export P04.

Lemma optimize_0plus_com_sound:
  ctrans_sound optimize_0plus_com.
Proof.
  unfold ctrans_sound. intros. unfold cequiv.
  com_cases (induction c) Case; intros.
  (* SKIP *)
  reflexivity.
  (* Ass *)
  simpl. split; intros.
  SCase "->". inversion H; subst. apply E_Ass. symmetry. apply optimize_0plus_aexp_sound.
  SCase "<-". inversion H; subst. rewrite<-optimize_0plus_aexp_sound. apply E_Ass. reflexivity.
  (* ;; *)
  simpl.
  split; intros; inversion H; subst;
  assert (c1 / st || st'0 <-> optimize_0plus_com c1 / st || st'0); try apply IHc1;
  assert (c2 / st'0 || st' <-> optimize_0plus_com c2 / st'0 || st'); try apply IHc2;
  destruct H0 as [Hc1 Hoc1]; destruct H1 as [Hc2 Hoc2].
  SCase "->";
  apply E_Seq with st'0; try apply Hc1; try apply Hc2; assumption.
  SCase "<-";
  apply E_Seq with st'0; try apply Hoc1; try apply Hoc2; assumption.
  (* IFB *)
  destruct (beval st b) eqn:Hb.
  SCase "True". split; intros; apply E_IfTrue.
    (* -> *)rewrite<-optimize_0plus_bexp_sound; assumption. inversion H; subst. apply IHc1; assumption. rewrite H5 in Hb. inversion Hb. assumption.
    (* <- *) inversion H; subst. apply IHc1; assumption. rewrite<-optimize_0plus_bexp_sound in H5. rewrite H5 in Hb. inversion Hb.
  SCase "False". split; intros.
    SSCase "->".
    inversion H; subst. rewrite H5 in Hb; inversion Hb.
    apply E_IfFalse. rewrite<-optimize_0plus_bexp_sound; assumption. apply IHc2; assumption.
    SSCase "<-".
    inversion H; subst. rewrite<-optimize_0plus_bexp_sound in H5. rewrite H5 in Hb; inversion Hb.
    apply E_IfFalse; try assumption. apply IHc2; try assumption.
  (* WHILE *)
  split; intros.
  SCase "->".  inversion H; subst.
  SSCase "E_WhileEnd".
    apply E_WhileEnd. rewrite<-optimize_0plus_bexp_sound; assumption.
  SSCase "E_WhileLoop".
simpl.
Print cequiv.
  assert (HWhile : cequiv (WHILE b DO c END) (WHILE optimize_0plus_bexp b DO optimize_0plus_com c END)).
  apply CWhile_congruence.
  apply optimize_0plus_bexp_sound.
  unfold cequiv. apply IHc.
  unfold cequiv in HWhile. apply HWhile. assumption.

  SCase "<-". inversion H; subst.
  SSCase "E_WhileEnd".
    apply E_WhileEnd. rewrite<-optimize_0plus_bexp_sound in H4; assumption.
  assert (HWhile : cequiv (WHILE b DO c END) (WHILE optimize_0plus_bexp b DO optimize_0plus_com c END)).
 apply CWhile_congruence.
  apply optimize_0plus_bexp_sound.
  unfold cequiv. apply IHc.
  unfold cequiv in HWhile. apply HWhile. assumption.
Qed.


Require Import Verse.Monoid.

(** * Abstract Types and code.

There are three important concepts of interest for us when dealing
with an abstract language --- the types of the language, the variable
universe, and finally the code. Since our languages are all embedded
in Coq, for every language L of interest, we have a Coq type [τ] that
represent the types of this language. Having fixed the types, the
variable universe for this type is just the type family [τ -> Type].
What is left is the code. In our setting, code is just a variable
parametrised monoid. We capture programs associated with a give type
[τ] by the following class.

*)

Module Code.
  Class class (type : Type) := { t : (type -> Type) -> Type;
                                 monoid_instance : forall v : type -> Type, Monoid (t v)
                               }.

End Code.

Instance code_monoid (type : Type)`{Code.class type}(v : type -> Type) : Monoid (Code.t v)
  := Code.monoid_instance v.

Definition sequence {type : Type}`{Code.class type}{v} (ps : list (Code.t v)) :  Code.t v :=
  mconcat ps.

(**

A more concrete instance of a program is when we have statements and
consider the monoid of list of statements (straight line
programs). Ideally, fixing the types should fix the underlying program
and hence we do not declare the definition below as an instance. When
program instances are being defined, we might want to use this
definition to declare an appropriate instances.

*)

Definition straight_line_program (type : Type)(stmt : (type -> Type) -> Type) : Code.class type
  :=  {| Code.t := fun v => list (stmt v);
         Code.monoid_instance := _
      |}.

(** * Translations

The next important consideration for us is translation between
programs written in different languages. We begin with a translation
between their types.

 *)

Module Translate.

  Class class (stype dtype : Type) := tr : stype -> dtype.

  Instance variable_translate stype dtype `{Translate.class stype dtype} : Translate.class (dtype -> Type)(stype -> Type)
    := fun v => fun st => v (Translate.tr st).

End Translate.

Module Type LanguageTranslate.

  Parameter stype dtype : Type.

  Declare Instance scode : Code.class stype.
  Declare Instance dcode : Code.class dtype.

  Declare Instance type_translate : Translate.class stype dtype.
  Declare Instance code_translation {v : dtype -> Type} : Translate.class (Code.t (Translate.tr v)) (Code.t v).

End LanguageTranslate.

Require Import Verse.Utils.hlist.

(** * Code with inlined functions

Although we are interested in straight line programs, breaking up such
programs into function calls that are eventually inlined is useful to
simplfy proofs. The types below captures the functional variant of the
call.

*)

Module FunCode.

  Inductive t {type : Type}(code : (type -> Type) -> Type)(v : type -> Type) : Type :=
  | callFunc : forall lts : list type, (forall w, hlist w lts -> code w)
                                  -> hlist v lts -> t code v.

  Arguments callFunc [type code v lts].

  Definition inline {type}{v : type -> Type}{code : (type -> Type) -> Type}(p : t code v) : code v :=
    match p with
    | callFunc func args => func v args
    end.

  Definition inlineS {type}`{Code.class type}{v : type -> Type}(prog : list (t Code.t v)) : Code.t v :=
    sequence (List.map inline prog).

  Definition call {type}{lts : list type}{code : (type -> Type) ->  Type}
             (func : forall w, curried w (code w) lts) {v} : hlist v lts -> t code v :=
    callFunc (fun w => uncurry (func w)).

End FunCode.


Require Import fastReals.interface.
Require Import Coq.ZArith.BinInt.

(*Coq.Numbers.BinNums.*)
Global Instance MinClassZ : MinClass Z := Zmin.
Global Instance MaxClassZ : MaxClass Z := Zmax.

Require Import MathClasses.interfaces.canonical_names.
Require Import MCInstances.

Require Import ROSCOQ.core.
Require Import CoRN.reals.fast.CRtrans.

Global Instance MinClassIR : MinClass IR := Min.
Global Instance MaxClassIR : MaxClass IR := Max.
Global Instance SinClassIR : SinClass IR := Sin.
Global Instance CosClassIR : CosClass IR := Cos.

Global Instance ApartTIR : ApartT IR := @cs_ap IR.

Global Instance RecipTIR : @ReciprocalT IR _ _ := @f_rcpcl IR.

Require Import MathClasses.interfaces.abstract_algebra.

Global Instance srmInjQ : SemiRing_Morphism (cast Q IR).
Proof using.
repeat (split; try apply _).
- intros. apply inj_Q_plus.
- intros. apply inj_Q_Zero.
- intros. apply inj_Q_mult.
- intros. apply inj_Q_One.
Qed.

Require Import MathClasses.theory.rings.
(*typeclass resolution happens right here, when declaring the database,
and not while rewriting. leaving out arguments here can confuse
the resolution later*)
Hint Rewrite
(@preserves_2 Q IR _ _ _ _ _ _ _ _ _ _ (@cast Q IR _) _)
(@preserves_1 Q IR _ _ _ _ _ _ _ _ _ _ (@cast Q IR _) _)
(@preserves_0 Q IR _ _ _ _ _ _ _ _ _ _ (@cast Q IR _) _)
(@preserves_negate Q _ _ _ _ _ _ _ IR _ _ _ _ _ _ _  (@cast Q IR _) _)
(@preserves_plus Q IR _ _ _ _ _ _ _ _ _ _ (@cast Q IR _) _)
(@preserves_mult Q IR  _ _ _ _ _ _ _ _ _ _ (@cast Q IR _) _)
 : Q2RMC.

Require Import MathClasses.interfaces.orders.

Global Instance OrderPreservingQR : OrderPreserving (cast Q ℝ).
Proof using.
  split;[split; try apply _|].
  intros ? ? H. apply inj_Q_leEq. exact H.
Qed.

Hint Unfold cos CosClassIR sin SinClassIR min MinClassIR max MaxClassIR One_instance_IR: IRMC.

(*
Require Import QArith.

Instance ApartTQ : ApartT Q := fun a b => (~ a==b).
*)


Require Export Db.Lemmas.
Require Export StlcIso.SpecSyntax.
Require Export StlcIso.SpecEvaluation.
Require Export Coq.Program.Tactics.
Require Import Common.Common.
Require Import Common.Relations.

Require Import Lia.

Local Ltac crush :=
  intros; cbn in * |-;
  repeat
    (cbn;
     repeat crushStlcSyntaxMatchH;
     repeat crushDbSyntaxMatchH;
     subst*);
  try discriminate;
  eauto with eval.

(* ** Evaluation contexts *)
Lemma ectx_cat (C₁ C₂: PCtx) :
  ECtx C₁ → ECtx C₂ → ECtx (pctx_cat C₁ C₂).
Proof.
  induction C₂; simpl; intros; destruct_conjs; auto.
Qed.

Lemma pctx_cat_app (t : Tm) (C₁ C₂ : PCtx) :
  pctx_app t (pctx_cat C₁ C₂) = pctx_app (pctx_app t C₁) C₂.
Proof.
  induction C₂; crush.
Qed.

Lemma eval_ctx C t t' (eC : ECtx C) :
  t --> t' -> pctx_app t C --> pctx_app t' C.
Proof.
  induction 1.
  rewrite <- ?pctx_cat_app in *.
  eauto using ectx_cat with eval.
Qed.

Lemma evalstar_ctx {t t'} C (eC: ECtx C) :
  t -->* t' → pctx_app t C -->* pctx_app t' C.
Proof.
  induction 1; eauto using eval_ctx with eval.
Qed.

Lemma evalplus_ctx C t t' (eC: ECtx C) :
  t -->+ t' → pctx_app t C -->+ pctx_app t' C.
Proof.
  induction 1; eauto using eval_ctx with eval.
Qed.

Lemma evaln_ctx {C t t' n} :
  ECtx C → evaln t t' n -> evaln (pctx_app t C) (pctx_app t' C) n.
Proof.
  intros ec.
  induction 1; unfold evaln; eauto using eval_ctx with eval.
Qed.

Lemma termination_closed_under_antireduction {t t'} :
  t --> t' → t'⇓ → t⇓.
Proof.
  destruct 2 as [v ?].
  exists v; crush.
Qed.

Lemma terminationN_closed_under_antireduction {t t' n} :
  t --> t' → TerminatingN t' n → TerminatingN t (S n).
Proof.
  destruct 2 as (v & m & ? & ? & ?).
  exists v, (S m); repeat split.
  - crush.
  - lia.
  - eapply stepRel_step; crush.
Qed.

Lemma termination_closed_under_antireductionStar {t t'} :
  t -->* t' → t'⇓ → t⇓.
Proof.
  intros e m. induction e;
  eauto using termination_closed_under_antireduction.
Qed.

Lemma value_ectx_inv C t (ec : ECtx C) :
  Value (pctx_app t C) → Value t.
Proof.
  intros v; induction C; crush.
Qed.

Lemma eval₀_ectx_inv C t (ec : ECtx C) {t' t''} :
  t'' -->₀ t' → t'' = pctx_app t C →
  Value t ∨ C = phole.
Proof.
  induction 1;
  destruct C; crush;
  try match goal with
      [ H : _ = pctx_app t C |- _ ] =>
      (assert (Value (pctx_app t C)) by (rewrite <- H; crush))
  end; eauto using value_ectx_inv.
Qed.

Lemma pctx_cat_phole_leftzero {C} :
  pctx_cat phole C = C.
Proof.
  induction C; crush; f_equal; crush.
Qed.

Lemma pctx_cat_assoc {C C' C''} :
  pctx_cat C (pctx_cat C' C'') = pctx_cat (pctx_cat C C') C''.
Proof.
  induction C''; crush; f_equal; crush.
Qed.

Lemma values_terminate {v : Tm} : Value v → v ⇓.
Proof.
  intros vv. exists v; crush.
Qed.

Definition normal' (t : Tm) := ∀ t', ¬ (t --> t').
Lemma values_are_normal' {t : Tm} : Value t -> normal' t.
Proof. induction 2 using eval_ind'; crush. Qed.

Lemma values_are_normal {t : Tm} : Value t -> normal t.
Proof.
  generalize @values_are_normal'.
  unfold normal, normal', not.
  intros ? ? t'; destruct t'; eauto.
Qed.

Lemma normal_evalStar {v t} : normal v -> v -->* t -> v = t.
Proof.
  intros nv [|t' ev ets]; [reflexivity|].
  exfalso; eapply nv; eauto.
Qed.

Lemma value_evalStar {v t} : Value v -> v -->* t -> v = t.
Proof.
  eauto using normal_evalStar, values_are_normal.
Qed.

Lemma values_terminateN {t n} : Value t → t ⇓_ n.
Proof.
  intros v. exists t, 0; repeat split.
  - crush.
  - lia.
  - eapply stepRel_zero.
Qed.

Section InvertECtxEq.

  Inductive EctxAppEq : ∀ (t : Tm) (C : PCtx) (t' : Tm) (C' : PCtx),  Prop :=
    | EctxAppEqLeft : ∀ t C C', ECtx C → EctxAppEq (pctx_app t C) C' t (pctx_cat C C')
    | EctxAppEqRight : ∀ t C C', ECtx C → EctxAppEq t (pctx_cat C C') (pctx_app t C) C'
    | ECtxValueLeft : ∀ t t' C C', Value t → EctxAppEq t C t' C'
    | ECtxValueRight : ∀ t t' C C', Value t' → EctxAppEq t C t' C'
  .

  Lemma ectxAppEqExtend {t C t' C'} C'' :
    EctxAppEq t C t' C' →
    EctxAppEq t (pctx_cat C C'') t' (pctx_cat C' C'').
  Proof.
    induction 1; rewrite <- ?pctx_cat_assoc; constructor; assumption.
  Qed.

  Lemma ectxAppEqExtendPAbs {t C t' C' τ} :
    EctxAppEq t C t' C' →
    EctxAppEq t (pabs τ C) t' (pabs τ C').
  Proof.
    eapply (ectxAppEqExtend (pabs τ phole)).
  Qed.

  Lemma ectxAppEqExtendPInl {t C t' C'} :
    EctxAppEq t C t' C' →
    EctxAppEq t (pinl C) t' (pinl C').
  Proof.
    eapply (ectxAppEqExtend (pinl phole)).
  Qed.

  Lemma ectxAppEqExtendPInr {t C t' C'} :
    EctxAppEq t C t' C' →
    EctxAppEq t (pinr C) t' (pinr C').
  Proof.
    eapply (ectxAppEqExtend (pinr phole)).
  Qed.

  Lemma ectxAppEqExtendPProj₁ {t C t' C'} :
    EctxAppEq t C t' C' →
    EctxAppEq t (pproj₁ C) t' (pproj₁ C').
  Proof.
    eapply (ectxAppEqExtend (pproj₁ phole)).
  Qed.

  Lemma ectxAppEqExtendPProj₂ {t C t' C'} :
    EctxAppEq t C t' C' →
    EctxAppEq t (pproj₂ C) t' (pproj₂ C').
  Proof.
    eapply (ectxAppEqExtend (pproj₂ phole)).
  Qed.

  Lemma ectxAppEqExtendPPair₁ {t C t' C' t₂} :
    EctxAppEq t C t' C' →
    EctxAppEq t (ppair₁ C t₂) t' (ppair₁ C' t₂).
  Proof.
    eapply (ectxAppEqExtend (ppair₁ phole t₂)).
  Qed.

  Lemma ectxAppEqExtendPPair₂ {t C t' C' t₁} :
    EctxAppEq t C t' C' →
    EctxAppEq t (ppair₂ t₁ C) t' (ppair₂ t₁ C').
  Proof.
    eapply (ectxAppEqExtend (ppair₂ t₁ phole)).
  Qed.

  Lemma ectxAppEqExtendPApp₁ {t C t' C' t₂} :
    EctxAppEq t C t' C' →
    EctxAppEq t (papp₁ C t₂) t' (papp₁ C' t₂).
  Proof.
    eapply (ectxAppEqExtend (papp₁ phole t₂)).
  Qed.

  Lemma ectxAppEqExtendPApp₂ {t C t' C' t₁} :
    EctxAppEq t C t' C' →
    EctxAppEq t (papp₂ t₁ C) t' (papp₂ t₁ C').
  Proof.
    eapply (ectxAppEqExtend (papp₂ t₁ phole)).
  Qed.

  Lemma ectxAppEqExtendPSeq₁ {t C t' C' t₂} :
    EctxAppEq t C t' C' →
    EctxAppEq t (pseq₁ C t₂) t' (pseq₁ C' t₂).
  Proof.
    eapply (ectxAppEqExtend (pseq₁ phole t₂)).
  Qed.

  Lemma ectxAppEqExtendPIte₁ {t C t' C' t₂ t₃} :
    EctxAppEq t C t' C' →
    EctxAppEq t (pite₁ C t₂ t₃) t' (pite₁ C' t₂ t₃).
  Proof.
    eapply (ectxAppEqExtend (pite₁ phole t₂ t₃)).
  Qed.

  Lemma ectxAppEqExtendPCaseof₁ {t C t' C' t₂ t₃} :
    EctxAppEq t C t' C' →
    EctxAppEq t (pcaseof₁ C t₂ t₃) t' (pcaseof₁ C' t₂ t₃).
  Proof.
    eapply (ectxAppEqExtend (pcaseof₁ phole t₂ t₃)).
  Qed.

  Lemma ectxAppEqExtendPFold {t C t' C'} :
    EctxAppEq t C t' C' →
    EctxAppEq t (pfold C) t' (pfold C').
  Proof.
    eapply (ectxAppEqExtend (pfold phole)).
  Qed.

  Lemma ectxAppEqExtendPUnfold {t C t' C'} :
    EctxAppEq t C t' C' →
    EctxAppEq t (punfold C) t' (punfold C').
  Proof.
    eapply (ectxAppEqExtend (punfold phole)).
  Qed.

  Lemma ectxAppEqPHoleRight {t C} : ECtx C → EctxAppEq t C (pctx_app t C) phole.
  Proof.
    change C with (pctx_cat C phole); eauto using EctxAppEq.
  Qed.

  Lemma ectxAppEqPHoleLeft {t C} : ECtx C → EctxAppEq (pctx_app t C) phole t C.
  Proof.
    change C with (pctx_cat C phole); eauto using EctxAppEq.
  Qed.

  Lemma pctx_app_eq t C t' C' :
    ECtx C → ECtx C' →
    pctx_app t C = pctx_app t' C' →
    EctxAppEq t C t' C'.
  Proof.
    revert t' C';
    induction C; intros t' C' eC eC' eq';
    [simpl in *; subst;
     eauto using ectxAppEqPHoleLeft, ectxAppEqPHoleRight, EctxAppEq
    |idtac..];
    (destruct C';
      [change (pctx_app t' phole) with t' in *; subst;
       eauto using ectxAppEqPHoleLeft, ectxAppEqPHoleRight, EctxAppEq
      |idtac..]);
    crush;
    eauto using ectxAppEqExtendPAbs, ectxAppEqExtendPProj₂, ectxAppEqExtendPProj₁, ectxAppEqExtendPInr, ectxAppEqExtendPInl, ectxAppEqExtendPApp₁, ectxAppEqExtendPApp₂, ectxAppEqExtendPPair₁, ectxAppEqExtendPPair₂, ectxAppEqExtendPIte₁, ectxAppEqExtendPCaseof₁, ectxAppEqExtendPFold, ectxAppEqExtendPUnfold, ectxAppEqExtendPSeq₁;
    eauto using value_ectx_inv, ECtxValueRight, ECtxValueLeft.
  Qed.

End InvertECtxEq.

Lemma eval_ectx_inv C t (ec : ECtx C) {t' t''} :
  t'' --> t' → pctx_app t C = t'' →
  Value t ∨ exists t'', t' = pctx_app t'' C ∧ t --> t''.
Proof.
  induction 1.
  intros eq.
  pose proof (pctx_app_eq _ _ _ _ ec H0 eq) as inv.
  induction inv.
  - right.
    exists (pctx_app t' C).
    eauto using pctx_cat_app, eval_ctx₀.
  - destruct (eval₀_ectx_inv C t H1 H eq_refl); crush.
    right; exists t'.
    rewrite pctx_cat_phole_leftzero;
    eauto using (eval_ctx₀ phole).
  - left; crush.
  - exfalso.
    eapply (values_are_normal H1 t').
    eapply (eval_ctx₀ phole); crush.
Qed.

Lemma evalStar_ectx_inv C t (ec : ECtx C) v :
  pctx_app t C -->* v → Value v →
  exists v', Value v' ∧ t -->* v' ∧ pctx_app v' C -->* v.
Proof.
  intros es vv; depind es.
  - exists t; eauto using value_ectx_inv with eval.
  - destruct (eval_ectx_inv C t ec H eq_refl) as [vt|[t'' [eq e]]].
    + exists t; crush.
    + subst.
      destruct (IHes t'' C ec eq_refl vv) as (v' & vv' & es1' & es2').
      exists v'; crush.
Qed.

Lemma evaln_ectx_inv C t (ec : ECtx C) v n :
  evaln (pctx_app t C) v n → Value v →
  exists n' v', n' <= n /\ Value v' ∧ t -->* v' ∧ evaln (pctx_app v' C) v n'.
Proof.
  intros es vv; depind es.
  - exists 0, t; repeat split; eauto using value_ectx_inv, stepRel_zero with eval.
    eapply stepRel_zero.
  - destruct (eval_ectx_inv C t ec H eq_refl) as [vt|[t3 [eq e]]].
    + exists (S n), t; repeat split; eauto with eval.
      eapply stepRel_step; eauto.
    + subst.
      destruct (IHes t3 C ec eq_refl vv) as (n' & v' & nplen & vv' & es1' & es2').
      exists n', v'; repeat split; eauto with eval.
Qed.

Lemma inversion_termination_evalcontext C t (ec: ECtx C) :
  Terminating (pctx_app t C) → Terminating t.
Proof.
  destruct 1 as (v & vv & es).
  destruct (evalStar_ectx_inv C t ec v es) as (v' & vv' & es1 & es2); subst; crush.
  exists v'; crush.
Qed.

Corollary divergence_closed_under_evalcontext t :
  t⇑ → ∀ p, ECtx p → (pctx_app t p)⇑.
Proof. eauto using inversion_termination_evalcontext. Qed.

Corollary divergence_closed_under_evalcontext' {t C t'} :
  t⇑ → t' = pctx_app t C → ECtx C → t'⇑.
Proof. intros; subst; eauto using divergence_closed_under_evalcontext. Qed.

Ltac crushImpossibleEvals :=
  match goal with
          [ H : abs _ _ --> _ |- _ ] => exfalso; refine (values_are_normal' _ _ H); crush
        | [ H : true --> _ |- _ ] => exfalso; refine (values_are_normal' _ _ H); crush
        | [ H : false --> _ |- _ ] => exfalso; refine (values_are_normal' _ _ H); crush
        | [ H : unit --> _ |- _ ] => exfalso; refine (values_are_normal' _ _ H); crush
        | [ H : Value ?x, H' : ?x --> _ |- _ ] => exfalso; refine (values_are_normal' H _ H'); crush
        | [ Hx : Value ?x, Hy : Value ?y,  H' : pair ?x ?y --> _ |- _ ] => exfalso; refine (values_are_normal' _ _ H'); crush
        | [ Hx : Value ?x,  H' : inl ?x --> _ |- _ ] => exfalso; refine (values_are_normal' _ _ H'); crush
        | [ Hx : Value ?x,  H' : inr ?x --> _ |- _ ] => exfalso; refine (values_are_normal' _ _ H'); crush
        | [ Hx : Value ?x,  H' : fold_ ?x --> _ |- _ ] => exfalso; refine (values_are_normal' _ _ H'); crush
  end.

Lemma TerminatingN_Terminating {t : Tm} {n} : t ⇓_ n -> t ⇓.
Proof.
  destruct 1 as (v & m & vv & ineq & es).
  exists v; eauto using stepRel_to_evalStar.
Qed.

Lemma evalStar_to_evaln {t t'} : t -->* t' → exists n, evaln t t' n.
Proof.
  induction 1.
  - exists 0; apply stepRel_zero.
  - destruct (IHclos_refl_trans_1n) as (n & en).
    exists (S n); eapply stepRel_step; crush.
Qed.

Lemma evaln_to_evalStar {t t' n} :
  evaln t t' n → t -->* t'.
Proof.
  induction 1; crush.
Qed.

(* Lemma ctxevaln_to_ctxevalStar {t t' n} : *)
(*   ctxevaln t t' n → ctxevalStar t t'. *)
(* Proof. *)
(*   induction 1; crush. *)
(* Qed. *)

(* (* The following implication is actually an equivalence, but we don't need that. *) *)
(* Lemma ctxeval_eval_ctx {t t'} : ctxeval t t' → forall Cu, ECtx Cu → pctx_app t Cu --> pctx_app t' Cu. *)
(* Proof. *)
(*   destruct 1; intros; rewrite <- ?pctx_cat_app; eauto using ectx_cat with eval. *)
(* Qed. *)

(* Lemma ctxevaln_ctx {t t' n} : *)
(*   ctxevaln t t' n -> forall C, ECtx C → evaln (pctx_app t C) (pctx_app t' C) n. *)
(* Proof. *)
(*   intros ec C eC; unfold evaln. *)
(*   induction ec; eauto using ctxeval_eval_ctx with eval. *)
(* Qed. *)

(* Lemma ctxevaln_evaln_ctx {t t' n} : ctxevaln t t' n → forall Cu, ECtx Cu → evaln (pctx_app t Cu) (pctx_app t' Cu) n. *)
(* Proof. *)
(*   induction 1; unfold evaln in *; *)
(*   econstructor; eauto using ctxeval_eval_ctx with eval. *)
(* Qed. *)

(* Lemma extend_ctxeval tu tu' Cu : ECtx Cu → ctxeval tu tu' → ctxeval (pctx_app tu Cu) (pctx_app tu' Cu). *)
(* Proof. *)
(*   intros eCu ce.  *)
(*   induction ce. *)
(*   rewrite <- ?pctx_cat_app. *)
(*   eauto using ctxeval, ectx_cat. *)
(* Qed. *)

(* Lemma extend_ctxevalStar {tu tu'} Cu : ECtx Cu → ctxevalStar tu tu' → ctxevalStar (pctx_app tu Cu) (pctx_app tu' Cu). *)
(* Proof. *)
(*   intros eCu ce.  *)
(*   unfold ctxevalStar. *)
(*   induction ce; *)
(*   eauto using extend_ctxeval with eval. *)
(* Qed. *)

(* Lemma ctxevalStar_evalStar {tu tu'} : ctxevalStar tu tu' → tu -->* tu'. *)
(* Proof. *)
(*   unfold ctxevalStar. *)
(*   induction 1; *)
(*   eauto using ctxeval_eval with eval. *)
(* Qed. *)

(* Lemma evalStar_ctxevalStar {tu tu'} : tu -->* tu' -> ctxevalStar tu tu'. *)
(* Proof. *)
(*   unfold ctxevalStar. *)
(*   induction 1; *)
(*   eauto using eval_ctxeval with eval. *)
(* Qed. *)

(* Lemma ctxevalStar_normal {tu tu'} : normal tu -> ctxevalStar tu tu' -> tu = tu'. *)
(* Proof. *)
(*   intros ntu eu. *)
(*   destruct eu as [|tu'' tu' eu]; try reflexivity. *)
(*   eapply ctxeval_eval in eu. *)
(*   exfalso. exact (ntu _ eu). *)
(* Qed. *)

(* This should hold, but doesn't? *)
Lemma Terminating_TerminatingN {t : Tm} : t ⇓ -> ∃ n, t ⇓_ n.
Proof.
  destruct 1 as (v & vv & es).
  destruct (evalStar_to_evaln es) as [n en].
  exists n; econstructor; crush.
Qed.

Lemma determinacy' {t t' t'' t'''} : t --> t' → t'' --> t''' → t = t'' → t' = t'''.
Proof.
  intros e₁.
  revert t'' t'''.
  induction e₁ using eval_ind';
  induction 1 using eval_ind'; crush; try crushImpossibleEvals.
Qed.

Lemma determinacy {t t' t''} : t --> t' → t --> t'' → t' = t''.
Proof.
  eauto using determinacy'.
Qed.

Lemma determinacyStar1 {t1 t2 t3} : t1 --> t2 -> t1 -->* t3 -> normal t3 -> t2 -->* t3.
Proof.
  intros e12 es13 n3.
  destruct es13 as [|t4 t3 e14 es43].
  - exfalso. refine (n3 _ e12).
  - destruct (determinacy e12 e14); eauto.
Qed.

Lemma determinacyStar' {t1 t2 t3 t4} : t1 -->* t2 -> t3 -->* t4 -> t1 = t3 -> normal t4 -> t2 -->* t4.
Proof.
  intros e12. revert t3.
  induction e12 as [|x t5 t2 e15 e52]; intros t3 e34 -> n4; eauto.
  destruct e34 as [|t6 t4 e26 e64].
  - exfalso. eapply (n4 _ e15).
  - refine (IHe52 t6 e64 _ n4).
    eapply determinacy; eauto.
Qed.

Lemma determinacyStar {t1 t2 t4} : t1 -->* t2 -> t1 -->* t4 -> normal t4 -> t2 -->* t4.
Proof.
  eauto using determinacyStar'.
Qed.

(* Lemma determinacyStar_ctxeval {t1 t2 t4} : ctxevalStar t1 t2 -> ctxevalStar t1 t4 -> normal t4 -> ctxevalStar t2 t4. *)
(* Proof. *)
(*   eauto using evalStar_ctxevalStar, determinacyStar, ctxevalStar_evalStar. *)
(* Qed. *)

Lemma TerminatingD (t: Tm) (m: t⇓) :
  ∀ t', t --> t' → Terminating t'.
Proof.
  destruct m as (v & vv & es).
  destruct es;
  intros t' e; try crushImpossibleEvals.
  destruct (determinacy H e).
  exists z; crush.
Qed.

Lemma TerminatingDN (t: Tm) n (term: t ⇓_ (S n)) :
  ∀ t', t --> t' → TerminatingN t' n.
Proof.
  destruct term as (v & m & vv & ineq & esn).
  intros t' e.
  destruct esn; try crushImpossibleEvals.
  destruct (determinacy H e).
  exists t'', n0; repeat split.
  - crush.
  - lia.
  - crush.
Qed.

Lemma termination_closed_under_evalplus {t t'} :
  t -->+ t' → t⇓ → t'⇓.
Proof. intros e m; induction e; eauto using TerminatingD. Qed.

Lemma termination_closed_under_evalstar {t t'} :
  t -->* t' → t⇓ → t'⇓.
Proof. intros e m; induction e; eauto using TerminatingD. Qed.

Corollary divergence_closed_under_eval {t t'} :
  t --> t' → t'⇑ → t⇑.
Proof. eauto using TerminatingD with eval. Qed.

Corollary divergence_closed_under_evalplus {t t'} :
  t -->+ t' → t'⇑ → t⇑.
Proof. eauto using termination_closed_under_evalplus. Qed.

Corollary divergence_closed_under_evalstar {t t'} :
  t -->* t' → t'⇑ → t⇑.
Proof. eauto using termination_closed_under_evalstar. Qed.

Lemma cycles_dont_terminate {t} :
  t -->+ t → t⇑.
Proof.
  intros cycle.
  destruct 1 as (v & vv & ?).
  enough (forall t', t' -->+ t → t' -->* v → False) by eauto with eval.
  intros ? cycle'.
  induction 1 as [?|? ? ? e' ? IHes''];
    destruct (inversion_evalPlus cycle') as (? & cycleStart & ?).
  - refine (values_are_normal vv _ cycleStart).
  - rewrite <- ?(determinacy e' cycleStart) in *.
    eauto using IHes'', evalStarPlusToPlus.
Qed.

Lemma TerminatingN_lt {t n n'} :
  TerminatingN t n → n ≤ n' → TerminatingN t n'.
Proof.
  destruct 1 as (v & m & vv & ineq & esm).
  intros ineq'.
  exists v, m; repeat split; crush.
  lia.
Qed.

(* Lemma TerminatingN_eval1 {t t' n} : *)
(*   t --> t' → TerminatingN t' n → TerminatingN t (S n). *)
(* Proof. *)
(*   intros e term. *)
(*   destruct term as [v m]. *)
(*   apply (TerminatingIN t (S n) v (S m)); crush; try omega. *)
(*   eapply stepRel_step; crush. *)
(* Qed. *)

Lemma TerminatingN_eval {t t' n} :
  t --> t' → TerminatingN t' n ↔ TerminatingN t (S n).
Proof.
  intros term.
  split; intros;
  [eapply terminationN_closed_under_antireduction | eapply TerminatingDN];
  crush.
Qed.

Lemma TerminatingN_evaln {t t' n } n' :
  evaln t t' n → TerminatingN t' n' ↔ TerminatingN t (n + n').
Proof.
  induction 1; constructor; auto; intro term;
  change (S n + n') with (S (n + n')) in *;
  [rewrite <- (TerminatingN_eval H) | rewrite <- (TerminatingN_eval H) in term];
  apply IHstepRel; auto.
Qed.

Lemma evaln_split1 {t t' n} :
  evaln t t' (S n) → exists t'', eval t t'' ∧ evaln t'' t' n.
Proof.
  remember (S n) as n'.
  destruct 1; inversion Heqn'.
  subst.
  now exists t'.
Qed.

Lemma evaln_split {t t' } n n':
  evaln t t' (n + n') → exists t'', evaln t t'' n ∧ evaln t'' t' n'.
Proof.
  revert t; induction n.
  - intros; exists t; simpl in *; split; [apply stepRel_zero|assumption].
  - intros t esn.
    destruct (evaln_split1 esn) as (t'' & es & esn').
    destruct (IHn t'' esn') as (t3 & es1 & es2).
    exists t3; split; try assumption.
    eapply stepRel_step; crush.
Qed.

Lemma TerminatingN_xor_evaln {t t' n} :
  TerminatingN t n → evaln t t' (S n) → False.
Proof.
  intros term esn.
  replace (S n) with (n + 1) in esn by lia.
  destruct (evaln_split n 1 esn) as (t'' & en & e1).
  replace n with (n + 0) in term by lia.
  rewrite <- (TerminatingN_evaln 0 en) in term.
  destruct term as (v & m & vv & ineq & esm).
  assert (m = 0) by lia; subst.
  inversion esm; subst.
  inversion e1.
  crushImpossibleEvals.
Qed.

Section EvalInContext.
  Lemma eval₀_to_eval {t t'} :
    t -->₀ t' →
    t --> t'.
  Proof.
    intros e₀.
    change (t --> t') with (pctx_app t phole --> pctx_app t' phole).
    eapply (eval_ctx₀ phole); crush.
  Qed.

  Lemma eval_from_eval₀ {t t' t₀ t₀' C} :
    t₀ -->₀ t₀' →
    t = pctx_app t₀ C →
    t' = pctx_app t₀' C →
    ECtx C →
    t --> t'.
  Proof.
    intros; subst; eauto using eval_ctx₀.
  Qed.

  Lemma evalstar_ctx' {t t' t₀ t₀' C} :
    t₀ -->* t₀' →
    t = pctx_app t₀ C →
    t' = pctx_app t₀' C →
    ECtx C →
    t -->* t'.
  Proof.
    intros; subst; eauto using evalstar_ctx.
  Qed.

End EvalInContext.

Ltac inferContext :=
  cbn; try reflexivity;
  let rec inferC acc t t₀ :=
      match t with
        | t₀ => instantiate (1 := acc)
        | app ?t1 ?t2 => inferC (pctx_cat (papp₁ phole t2) acc) t1 t₀ + inferC (pctx_cat (papp₂ t1 phole) acc) t2 t₀
        | pair ?t1 ?t2 => inferC (pctx_cat (ppair₁ phole t2) acc) t1 t₀ + inferC (pctx_cat (ppair₂ t1 phole) acc) t2 t₀
        | seq ?t1 ?t2 => inferC (pctx_cat (pseq₁ phole t2) acc) t1 t₀
        | inl ?t1 => inferC (pctx_cat (pinl phole) acc) t1 t₀
        | inr ?t1 => inferC (pctx_cat (pinr phole) acc) t1 t₀
        | ite ?t1 ?t2 ?t3 => inferC (pctx_cat (pite₁ phole t2 t3) acc) t1 t₀
        | caseof ?t1 ?t2 ?t3 => inferC (pctx_cat (pcaseof₁ phole t2 t3) acc) t1 t₀
        | proj₁ ?t1 => inferC (pctx_cat (pproj₁ phole) acc) t1 t₀
        | proj₂ ?t1 => inferC (pctx_cat (pproj₂ phole) acc) t1 t₀
        | fold_ ?t1 => inferC (pctx_cat (pfold phole) acc) t1 t₀
        | unfold_ ?t1 => inferC (pctx_cat (punfold phole) acc) t1 t₀
        | pctx_app t₀ ?C => instantiate (1 := pctx_cat C acc)
        | pctx_app ?t1 (pctx_cat ?C1 ?C2) => inferC (pctx_app (pctx_app t1 C1) C2) t₀
        | pctx_app ?t1 ?C => inferC (pctx_cat C acc) t1 t₀
      end
  in repeat match goal with
              | [ |- ?t = pctx_app ?t₀ (pctx_cat ?C1 ?C2) ] => (rewrite -> ?pctx_cat_app)
              | [ |- pctx_app ?t0 ?C = pctx_app ?t' ?C ] => f_equal
              | [ |- ?t = pctx_app ?t₀ ?C ] => (inferC phole t t₀; rewrite -> ?pctx_cat_app; cbn; rewrite -> ?pctx_cat_app in *; reflexivity)
            end.

Lemma test_inferContext (t : Tm) (C' : PCtx): True.
Proof.
  assert (pctx_app (fold_ (inl t)) C' = pctx_app t (pctx_cat (pfold (pinl phole)) C')).
  inferContext.
  trivial.
Qed.

Ltac crushStlcEval :=
  match goal with
      [ |- ECtx (pctx_cat _ _) ] => eapply ectx_cat
    | [ |- ECtx (papp₁ _ _) ] => unfold ECtx
    | [ |- ECtx (papp₂ _ _) ] => unfold ECtx
    | [ |- ECtx (pinl _) ] => unfold ECtx
    | [ |- ECtx (pinr _) ] => unfold ECtx
    | [ |- ECtx (pfold _) ] => unfold ECtx
    | [ |- ECtx (punfold _) ] => unfold ECtx
    | [ |- ECtx phole ] => unfold ECtx
    | [ |- ECtx (pcaseof₁ _ _ _) ] => unfold ECtx
  end.