Require Import VST.floyd.base2.
Require Import VST.floyd.client_lemmas.
Require Import VST.floyd.closed_lemmas.
Require Import VST.floyd.local2ptree_denote.

Local Open Scope logic.

Definition eval_vardesc (id: ident) (ty: type) (Delta: tycontext) (T2: PTree.t (type * val)) (GV: option globals) : option val :=
  match (var_types Delta) ! id with
  | Some _ => match T2 ! id with
              | Some (ty', v) =>
                      if eqb_type ty ty'
                      then Some v
                      else None
              | None => None
              end
  | None => match GV with
            | Some gv => Some (gv id)
            | None => None
            end
  end.

Definition eval_lvardesc (id: ident) (ty: type) (Delta: tycontext) (T2: PTree.t (type * val)) : option val :=
  match (var_types Delta) ! id with
  | Some _ => match T2 ! id with
              | Some (ty', v) =>
                      if eqb_type ty ty'
                      then Some v
                      else None
              | None => None
              end
  | None => None
  end.

(* Goal: @In (forall _ : environ, Prop) (locald_denote (temp i v)) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) *)
(* Goal: @eq val (eval_id i rho) v *)
Fixpoint msubst_eval_expr {cs: compspecs} (Delta: tycontext) (T1: PTree.t val) (T2: PTree.t (type * val)) (GV: option globals) (e: Clight.expr) : option val :=
  match e with
  | Econst_int i ty => Some (Vint i)
  | Econst_long i ty => Some (Vlong i)
  | Econst_float f ty => Some (Vfloat f)
  | Econst_single f ty => Some (Vsingle f)
  | Etempvar id ty => PTree.get id T1
  | Eaddrof a ty => msubst_eval_lvalue Delta T1 T2 GV a
  | Eunop op a ty => option_map (eval_unop op (typeof a))
                                        (msubst_eval_expr Delta T1 T2 GV a)
  | Ebinop op a1 a2 ty =>
      match (msubst_eval_expr Delta T1 T2 GV a1), (msubst_eval_expr Delta T1 T2 GV a2) with
      | Some v1, Some v2 => Some (eval_binop op (typeof a1) (typeof a2) v1 v2)
      | _, _ => None
      end
  | Ecast a ty => option_map (eval_cast (typeof a) ty) (msubst_eval_expr Delta T1 T2 GV a)
  | Evar id ty => eval_vardesc id ty Delta T2 GV

  | Ederef a ty => msubst_eval_expr Delta T1 T2 GV a
  | Efield a i ty => option_map (eval_field (typeof a) i) (msubst_eval_lvalue Delta T1 T2 GV a)
  | Esizeof t _ => Some (Vptrofs (Ptrofs.repr (sizeof t)))
  | Ealignof t _ => Some (Vptrofs (Ptrofs.repr (alignof t)))
  end
(* Goal: @In (forall _ : environ, Prop) (locald_denote (temp i v)) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) *)
(* Goal: @eq val (eval_id i rho) v *)
  with msubst_eval_lvalue {cs: compspecs} (Delta: tycontext) (T1: PTree.t val) (T2: PTree.t (type * val)) (GV: option globals) (e: Clight.expr) : option val :=
  match e with
  | Evar id ty => eval_vardesc id ty Delta T2 GV
  | Ederef a ty => msubst_eval_expr Delta T1 T2 GV a
  | Efield a i ty => option_map (eval_field (typeof a) i)
                              (msubst_eval_lvalue Delta T1 T2 GV a)
  | _  => Some Vundef
  end.

(* Goal: @In (forall _ : environ, Prop) (locald_denote (temp i v)) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) *)
(* Goal: @eq val (eval_id i rho) v *)
Definition msubst_eval_LR {cs: compspecs} Delta T1 T2 GV e (lr: LLRR) :=
  match lr with
  | LLLL => msubst_eval_lvalue Delta T1 T2 GV e
  | RRRR => msubst_eval_expr Delta T1 T2 GV e
  end.

(* Goal: @In (forall _ : environ, Prop) (locald_denote (temp i v)) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) *)
(* Goal: @eq val (eval_id i rho) v *)
Definition msubst_eval_lvar {cs: compspecs} Delta T2 i t :=
  eval_lvardesc i t Delta T2.

Lemma msubst_eval_expr_eq_aux:
(* Goal: @In (forall _ : environ, Prop) (locald_denote (temp i v)) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) *)
(* Goal: @eq val (eval_id i rho) v *)
  forall {cs: compspecs} (Delta: tycontext) (T1: PTree.t val) (T2: PTree.t (type * val)) (GV: option globals) e rho v,
    (forall i v, T1 ! i = Some v -> eval_id i rho = v) ->
    (forall i t v,
     eval_vardesc i t Delta T2 GV  = Some v -> eval_var i t rho = v) ->
    msubst_eval_expr Delta T1 T2 GV e = Some v ->
    eval_expr e rho = v
with msubst_eval_lvalue_eq_aux:
(* Goal: @In (forall _ : environ, Prop) (locald_denote (temp i v)) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) *)
(* Goal: @eq val (eval_id i rho) v *)
  forall {cs: compspecs} (Delta: tycontext) (T1: PTree.t val) (T2: PTree.t (type * val)) (GV: option globals) e rho v,
    (forall i v, T1 ! i = Some v -> eval_id i rho = v) ->
    (forall i t v,
     eval_vardesc i t Delta T2 GV = Some v -> eval_var i t rho = v) ->
    msubst_eval_lvalue Delta T1 T2 GV e = Some v ->
    eval_lvalue e rho = v.
Proof.
  + clear msubst_eval_expr_eq_aux.
    induction e; intros; simpl in H1 |- *; try solve [inversion H1; auto].
    - erewrite msubst_eval_lvalue_eq_aux; eauto.
    - unfold_lift; simpl.
      destruct (msubst_eval_expr Delta T1 T2 GV e) eqn:?; [| inversion H1].
      inversion H1.
      rewrite IHe with (v := v0) by auto.
      reflexivity.
    - unfold_lift; simpl.
      destruct (msubst_eval_expr Delta T1 T2 GV e1) eqn:?; [| inversion H1].
      destruct (msubst_eval_expr Delta T1 T2 GV e2) eqn:?; [| inversion H1].
      inversion H1.
      rewrite IHe1 with (v := v0) by auto.
      rewrite IHe2 with (v := v1) by auto.
      reflexivity.
    - unfold_lift; simpl.
      destruct (msubst_eval_expr Delta T1 T2 GV e) eqn:?; [| inversion H1].
      inversion H1.
      rewrite IHe with (v := v0) by auto.
      reflexivity.
    - unfold_lift; simpl.
      destruct (msubst_eval_lvalue Delta T1 T2 GV e) eqn:?; [| inversion H1].
      inversion H1.
      erewrite msubst_eval_lvalue_eq_aux by eauto.
      reflexivity.
  + clear msubst_eval_lvalue_eq_aux.
    induction e; intros; simpl in H1 |- *; try solve [inversion H1; auto].
    - unfold_lift; simpl.
      destruct (msubst_eval_expr Delta T1 T2 GV e) eqn:?; [| inversion H1].
      inversion H1.
      erewrite msubst_eval_expr_eq_aux by eauto;
(* Goal: @eq (list val) (@cons val (force_val (sem_cast (typeof a) t v0)) vl) (@cons val (force_val (sem_cast (typeof a) t v0)) vl) *)
      auto.
    - unfold_lift; simpl.
      destruct (msubst_eval_lvalue Delta T1 T2 GV e) eqn:?; [| inversion H1].
      inversion H1.
      rewrite IHe with (v := v0) by auto.
      reflexivity.
Qed.

Require Import VST.veric.expr_lemmas2.


(* Goal: @In (forall _ : environ, Prop) (locald_denote (temp i v)) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) *)
(* Goal: @eq val (eval_id i rho) v *)
Lemma msubst_eval_eq_aux {cs: compspecs}: forall Delta T1 T2 GV rho,
  tc_environ Delta rho ->
  fold_right `(and) `(True) (map locald_denote (LocalD T1 T2 GV)) rho ->
  (forall i v, T1 ! i = Some v -> eval_id i rho = v) /\
  (forall i t v, eval_vardesc i t Delta T2 GV = Some v ->
      eval_var i t rho = v).
Proof.
(* Goal: forall (Delta : tycontext) (T1 : PTree.t val) (T2 : PTree.t (prod type val)) (GV : option globals) (rho : environ) (_ : tc_environ Delta rho) (_ : @fold_right (forall _ : lift_S (LiftEnviron Prop), Prop) (forall _ : lift_S (Tarrow Prop (LiftEnviron Prop)), Prop) (@liftx (Tarrow Prop (Tarrow Prop (LiftEnviron Prop))) (and : forall (_ : Prop) (_ : Prop), Prop)) (@liftx (LiftEnviron Prop) (True : Prop)) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) rho), and (forall (i : positive) (v : val) (_ : @eq (option val) (@PTree.get val i T1) (@Some val v)), @eq val (eval_id i rho) v) (forall (i : ident) (t : type) (v : val) (_ : @eq (option val) (eval_vardesc i t Delta T2 GV) (@Some val v)), @eq val (eval_var i t rho) v) *)
  intros; split; intros.
  + intros.
(* Goal: @eq val (eval_id i rho) v *)
    assert (In (locald_denote (temp i v)) (map locald_denote (LocalD T1 T2 GV))).
(* Goal: @In (forall _ : environ, Prop) (locald_denote (temp i v)) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) *)
(* Goal: @eq val (eval_id i rho) v *)
    {
      apply  in_map.
(* Goal: @In localdef (temp i v) (LocalD T1 T2 GV) *)
      apply LocalD_sound.
(* Goal: or (@ex positive (fun i0 : positive => @ex val (fun v0 : val => and (@eq (option val) (@PTree.get val i0 T1) (@Some val v0)) (@eq localdef (temp i v) (temp i0 v0))))) (or (@ex positive (fun i0 : positive => @ex type (fun t : type => @ex val (fun v0 : val => and (@eq (option (prod type val)) (@PTree.get (prod type val) i0 T2) (@Some (prod type val) (@pair type val t v0))) (@eq localdef (temp i v) (lvar i0 t v0)))))) (@ex globals (fun gv : globals => and (@eq (option globals) GV (@Some globals gv)) (@eq localdef (temp i v) (gvars gv))))) *)
      left.
(* Goal: @ex positive (fun i0 : positive => @ex val (fun v0 : val => and (@eq (option val) (@PTree.get val i0 T1) (@Some val v0)) (@eq localdef (temp i v) (temp i0 v0)))) *)
      eauto.
    }
(* Goal: @eq val (eval_id i rho) v *)
    pose proof local_ext _ _ _ H2 H0.
(* Goal: @eq val (eval_id i rho) v *)
    destruct H3; auto.
  + intros.
(* Goal: @eq val (eval_var i t rho) v *)
    unfold eval_vardesc in H1.
(* Goal: @eq val (eval_var i t rho) v *)
    unfold eval_var.
(* Goal: @eq val match @Map.get (prod block type) (ve_of rho) i with | Some (pair b ty' as p) => if eqb_type t ty' then Vptr b Ptrofs.zero else Vundef | None => match @Map.get block (ge_of rho) i with | Some b => Vptr b Ptrofs.zero | None => Vundef end end v *)
    destruct_var_types i; rewrite ?Heqo, ?Heqo0 in *.
    - destruct (T2 ! i) as [[? ?]|] eqn:?; [| inv H1].
(* Goal: @eq val (if eqb_type t t0 then Vptr b Ptrofs.zero else Vundef) v *)
      destruct (eqb_type t t1) eqn:?; inv H1.
(* Goal: @eq val (if eqb_type t t0 then Vptr b Ptrofs.zero else Vundef) v *)
      apply eqb_type_true in Heqb0. subst t1.
      assert (In (locald_denote (lvar i t v))  (map locald_denote (LocalD T1 T2 GV)))
        by (apply  in_map; apply LocalD_sound; eauto 50).
      assert (H3 := local_ext _ _ _ H1 H0). clear - H3 Heqo Heqo0.
(* Goal: @eq val (if eqb_type t t0 then Vptr b Ptrofs.zero else Vundef) v *)
      hnf in H3.
(* Goal: @eq val (if eqb_type t t0 then Vptr b Ptrofs.zero else Vundef) v *)
      rewrite Heqo0 in H3.
(* Goal: @eq val (if eqb_type t t0 then Vptr b Ptrofs.zero else Vundef) v *)
      destruct H3; subst. rewrite eqb_type_refl. auto.
    - destruct GV as [?gv |]; inv H1.
      assert (In (locald_denote (gvars gv)) (map locald_denote (LocalD T1 T2 (Some gv))))
        by (apply  in_map; apply LocalD_sound; eauto 50).
      assert (H3 := local_ext _ _ _ H1 H0). clear - H3.
(* Goal: @eq val (if eqb_type t t0 then Vptr b Ptrofs.zero else Vundef) v *)
      unfold eval_var in *. hnf in H3.
(* Goal: @eq val match @Map.get block (ge_of rho) i with | Some b => Vptr b Ptrofs.zero | None => Vundef end (gv i) *)
      subst.
(* Goal: @eq (list val) (@cons val (force_val (sem_cast (typeof a) t v0)) vl) (@cons val (force_val (sem_cast (typeof a) t v0)) vl) *)
      auto.
Qed.

(* Goal: @In (forall _ : environ, Prop) (locald_denote (temp i v)) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) *)
(* Goal: @eq val (eval_id i rho) v *)
Lemma msubst_eval_lvar_eq_aux {cs: compspecs}: forall Delta T1 T2 GV rho,
  tc_environ Delta rho ->
  fold_right `(and) `(True) (map locald_denote (LocalD T1 T2 GV)) rho ->
  (forall i t v, eval_lvardesc i t Delta T2 = Some v ->
      eval_lvar i t rho = v).
Proof.
(* Goal: forall (cs : compspecs) (Delta : tycontext) (P : list Prop) (T1 : PTree.t val) (T2 : PTree.t (prod type val)) (GV : option globals) (R : list mpred) (i : ident) (t : type) (v : val) (_ : @eq (option val) (@msubst_eval_lvar cs Delta T2 i t) (@Some val v)), @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v : forall _ : val, Prop) (eval_lvar i t))) *)
  intros.
(* Goal: @eq val (eval_lvar i t rho) v *)
  unfold eval_lvar.
(* Goal: @eq val match @Map.get (prod block type) (ve_of rho) i with | Some (pair b ty' as p) => if eqb_type t ty' then Vptr b Ptrofs.zero else Vundef | None => Vundef end v *)
  unfold eval_lvardesc in H1.
(* Goal: @eq val match @Map.get (prod block type) (ve_of rho) i with | Some (pair b ty' as p) => if eqb_type t ty' then Vptr b Ptrofs.zero else Vundef | None => Vundef end v *)
  destruct_var_types i; rewrite ?Heqo, ?Heqo0 in *; [| inv H1].
(* Goal: @eq val (if eqb_type t t0 then Vptr b Ptrofs.zero else Vundef) v *)
  destruct (T2 ! i) as [[? ?]|] eqn:?; [| inv H1].
(* Goal: @eq val (if eqb_type t t0 then Vptr b Ptrofs.zero else Vundef) v *)
  destruct (eqb_type t t1) eqn:?; inv H1.
(* Goal: @eq val (if eqb_type t t0 then Vptr b Ptrofs.zero else Vundef) v *)
  apply eqb_type_true in Heqb0; subst t1.
  assert (In (locald_denote (lvar i t v))  (map locald_denote (LocalD T1 T2 GV)))
    by ( apply  in_map; apply LocalD_sound; eauto 50).
  assert (H3 := local_ext _ _ _ H1 H0). clear - H3 Heqo Heqo0.
(* Goal: @eq val (if eqb_type t t0 then Vptr b Ptrofs.zero else Vundef) v *)
  hnf in H3.
(* Goal: @eq val (if eqb_type t t0 then Vptr b Ptrofs.zero else Vundef) v *)
  rewrite Heqo0 in H3.
(* Goal: @eq val (if eqb_type t t0 then Vptr b Ptrofs.zero else Vundef) v *)
  destruct H3; subst. rewrite eqb_type_refl. auto.
Qed.

(* Goal: @In (forall _ : environ, Prop) (locald_denote (temp i v)) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) *)
(* Goal: @eq val (eval_id i rho) v *)
Lemma msubst_eval_expr_eq: forall {cs: compspecs} Delta P T1 T2 GV R e v,
  msubst_eval_expr Delta T1 T2 GV e = Some v ->
  ENTAIL Delta, PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)) |--
    local (`(eq v) (eval_expr e)).
Proof.
(* Goal: forall (cs : compspecs) (Delta : tycontext) (P : list Prop) (T1 : PTree.t val) (T2 : PTree.t (prod type val)) (GV : option globals) (R : list mpred) (i : ident) (t : type) (v : val) (_ : @eq (option val) (@msubst_eval_lvar cs Delta T2 i t) (@Some val v)), @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v : forall _ : val, Prop) (eval_lvar i t))) *)
  intros.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v : forall _ : val, Prop) (eval_lvar i t))) *)
  unfold PROPx, LOCALx.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@prop (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@fold_right Prop Prop and True P)) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (@fold_right (forall _ : lift_S (LiftEnviron Prop), Prop) (forall _ : lift_S (Tarrow Prop (LiftEnviron Prop)), Prop) (@liftx (Tarrow Prop (Tarrow Prop (LiftEnviron Prop))) and) (@liftx (LiftEnviron Prop) True) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)))) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v) (eval_lvar i t))) *)
  apply derives_trans with (local (tc_environ Delta) && local (fold_right (` and) (` True) (map locald_denote (LocalD T1 T2 GV)))); [solve_andp |].
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (local (@fold_right (forall _ : lift_S (LiftEnviron Prop), Prop) (forall _ : lift_S (Tarrow Prop (LiftEnviron Prop)), Prop) (@liftx (Tarrow Prop (Tarrow Prop (LiftEnviron Prop))) and) (@liftx (LiftEnviron Prop) True) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV))))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v) (eval_lvar i t))) *)
  unfold local, lift, lift1.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (fun rho : environ => @prop mpred Nveric (tc_environ Delta rho)) (fun rho : environ => @prop mpred Nveric (@fold_right (forall _ : lift_S (LiftEnviron Prop), Prop) (forall _ : lift_S (Tarrow Prop (LiftEnviron Prop)), Prop) (@liftx (Tarrow Prop (Tarrow Prop (LiftEnviron Prop))) and) (@liftx (LiftEnviron Prop) True) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) rho))) (fun rho : environ => @prop mpred Nveric (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v) (eval_lvar i t) rho)) *)
  simpl; intro rho.
(* Goal: forall (cs : compspecs) (Delta : tycontext) (P : list Prop) (T1 : PTree.t val) (T2 : PTree.t (prod type val)) (GV : option globals) (R : list mpred) (i : ident) (t : type) (v : val) (_ : @eq (option val) (@msubst_eval_lvar cs Delta T2 i t) (@Some val v)), @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v : forall _ : val, Prop) (eval_lvar i t))) *)
  normalize; intros.
(* Goal: @liftx (Tarrow val (LiftEnviron Prop)) (@eq val v) (eval_lvar i t) rho *)
      autorewrite with subst norm1 norm2; normalize.
(* Goal: @eq val v (@eval_lvalue cs e rho) *)
  destruct (msubst_eval_eq_aux _ _ _ _ _ H0 H1).
(* Goal: @eq (list val) (@cons val (force_val (sem_cast (typeof a) t v0)) vl) (@cons val (force_val (sem_cast (typeof a) t v0)) vl) *)
  apply eq_sym, (msubst_eval_expr_eq_aux Delta T1 T2 GV); auto.
Qed.

(* Goal: @In (forall _ : environ, Prop) (locald_denote (temp i v)) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) *)
(* Goal: @eq val (eval_id i rho) v *)
Lemma msubst_eval_lvalue_eq: forall {cs: compspecs} Delta P T1 T2 GV R e v,
  msubst_eval_lvalue Delta T1 T2 GV e = Some v ->
  ENTAIL Delta, PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)) |--
    local (`(eq v) (eval_lvalue e)).
Proof.
(* Goal: forall (cs : compspecs) (Delta : tycontext) (P : list Prop) (T1 : PTree.t val) (T2 : PTree.t (prod type val)) (GV : option globals) (R : list mpred) (i : ident) (t : type) (v : val) (_ : @eq (option val) (@msubst_eval_lvar cs Delta T2 i t) (@Some val v)), @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v : forall _ : val, Prop) (eval_lvar i t))) *)
  intros.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v : forall _ : val, Prop) (eval_lvar i t))) *)
  unfold PROPx, LOCALx.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@prop (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@fold_right Prop Prop and True P)) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (@fold_right (forall _ : lift_S (LiftEnviron Prop), Prop) (forall _ : lift_S (Tarrow Prop (LiftEnviron Prop)), Prop) (@liftx (Tarrow Prop (Tarrow Prop (LiftEnviron Prop))) and) (@liftx (LiftEnviron Prop) True) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)))) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v) (eval_lvar i t))) *)
  apply derives_trans with (local (tc_environ Delta) && local (fold_right (` and) (` True) (map locald_denote (LocalD T1 T2 GV)))); [solve_andp |].
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (local (@fold_right (forall _ : lift_S (LiftEnviron Prop), Prop) (forall _ : lift_S (Tarrow Prop (LiftEnviron Prop)), Prop) (@liftx (Tarrow Prop (Tarrow Prop (LiftEnviron Prop))) and) (@liftx (LiftEnviron Prop) True) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV))))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v) (eval_lvar i t))) *)
  unfold local, lift, lift1.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (fun rho : environ => @prop mpred Nveric (tc_environ Delta rho)) (fun rho : environ => @prop mpred Nveric (@fold_right (forall _ : lift_S (LiftEnviron Prop), Prop) (forall _ : lift_S (Tarrow Prop (LiftEnviron Prop)), Prop) (@liftx (Tarrow Prop (Tarrow Prop (LiftEnviron Prop))) and) (@liftx (LiftEnviron Prop) True) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) rho))) (fun rho : environ => @prop mpred Nveric (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v) (eval_lvar i t) rho)) *)
  simpl; intro rho.
(* Goal: forall (cs : compspecs) (Delta : tycontext) (P : list Prop) (T1 : PTree.t val) (T2 : PTree.t (prod type val)) (GV : option globals) (R : list mpred) (i : ident) (t : type) (v : val) (_ : @eq (option val) (@msubst_eval_lvar cs Delta T2 i t) (@Some val v)), @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v : forall _ : val, Prop) (eval_lvar i t))) *)
  normalize; intros.
(* Goal: @liftx (Tarrow val (LiftEnviron Prop)) (@eq val v) (eval_lvar i t) rho *)
      autorewrite with subst norm1 norm2; normalize.
(* Goal: @eq val v (@eval_lvalue cs e rho) *)
  destruct (msubst_eval_eq_aux _ _ _ _ _ H0 H1).
(* Goal: @eq (list val) (@cons val (force_val (sem_cast (typeof a) t v0)) vl) (@cons val (force_val (sem_cast (typeof a) t v0)) vl) *)
  apply eq_sym, (msubst_eval_lvalue_eq_aux Delta T1 T2 GV); auto.
Qed.

(* Goal: @In (forall _ : environ, Prop) (locald_denote (temp i v)) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) *)
(* Goal: @eq val (eval_id i rho) v *)
Lemma msubst_eval_LR_eq: forall {cs: compspecs} Delta P T1 T2 GV R e v lr,
  msubst_eval_LR Delta T1 T2 GV e lr = Some v ->
  ENTAIL Delta, PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)) |--
    local (`(eq v) (eval_LR e lr)).
Proof.
(* Goal: forall (cs : compspecs) (Delta : tycontext) (P : list Prop) (T1 : PTree.t val) (T2 : PTree.t (prod type val)) (GV : option globals) (R : list mpred) (i : ident) (t : type) (v : val) (_ : @eq (option val) (@msubst_eval_lvar cs Delta T2 i t) (@Some val v)), @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v : forall _ : val, Prop) (eval_lvar i t))) *)
  intros.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v : forall _ : val, Prop) (@eval_LR cs e lr))) *)
  destruct lr.
  + apply msubst_eval_lvalue_eq; auto.
  + apply msubst_eval_expr_eq; auto.
Qed.

Lemma msubst_eval_exprlist_eq:
(* Goal: @In (forall _ : environ, Prop) (locald_denote (temp i v)) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) *)
(* Goal: @eq val (eval_id i rho) v *)
  forall {cs: compspecs} Delta P T1 T2 GV R tys el vl,
  force_list
           (map (msubst_eval_expr Delta T1 T2 GV)
              (explicit_cast_exprlist tys el)) = Some vl ->
 ENTAIL Delta, PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)) |--
   local (`(eq vl) (eval_exprlist tys el)).
Proof.
(* Goal: forall (cs : compspecs) (Delta : tycontext) (P : list Prop) (T1 : PTree.t val) (T2 : PTree.t (prod type val)) (GV : option globals) (R : list mpred) (i : ident) (t : type) (v : val) (_ : @eq (option val) (@msubst_eval_lvar cs Delta T2 i t) (@Some val v)), @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v : forall _ : val, Prop) (eval_lvar i t))) *)
intros.
revert tys vl H; induction el; destruct tys, vl; intros;
  try solve [inv H];
  try solve [go_lowerx;  apply prop_right; reflexivity].
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@nil val)) (@eval_exprlist cs (@cons type t tys) (@cons expr a el)))) *)
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@cons val v vl)) (@eval_exprlist cs (@cons type t tys) (@cons expr a el)))) *)
 simpl map in H.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@nil val)) (@eval_exprlist cs (@cons type t tys) (@cons expr a el)))) *)
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@cons val v vl)) (@eval_exprlist cs (@cons type t tys) (@cons expr a el)))) *)
 unfold force_list in H; fold (@force_list val) in H.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@nil val)) (@eval_exprlist cs (@cons type t tys) (@cons expr a el)))) *)
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@cons val v vl)) (@eval_exprlist cs (@cons type t tys) (@cons expr a el)))) *)
 destruct (msubst_eval_expr Delta T1 T2 GV a) eqn:?.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@cons val v vl)) (@eval_exprlist cs (@cons type t tys) (@cons expr a el)))) *)
 simpl in H.
 destruct (force_list
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@nil val)) (@eval_exprlist cs (@cons type t tys) (@cons expr a el)))) *)
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@cons val v vl)) (@eval_exprlist cs (@cons type t tys) (@cons expr a el)))) *)
        (map (msubst_eval_expr Delta T1 T2 GV) (explicit_cast_exprlist tys el))); inv H.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@cons val v vl)) (@eval_exprlist cs (@cons type t tys) (@cons expr a el)))) *)
 simpl in H. inv H.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@cons val v vl)) (@eval_exprlist cs (@cons type t tys) (@cons expr a el)))) *)
 simpl in H.
 destruct (option_map (force_val1 (sem_cast (typeof a) t))
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@nil val)) (@eval_exprlist cs (@cons type t tys) (@cons expr a el)))) *)
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@cons val v vl)) (@eval_exprlist cs (@cons type t tys) (@cons expr a el)))) *)
        (msubst_eval_expr Delta T1 T2 GV a)) eqn:?; inv H.
  destruct ( force_list
         (map (msubst_eval_expr Delta T1 T2 GV) (explicit_cast_exprlist tys el))) eqn:?; inv H1.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@cons val v vl)) (@eval_exprlist cs (@cons type t tys) (@cons expr a el)))) *)
  specialize (IHel _ _ Heqo0).
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@cons val v vl)) (@eval_exprlist cs (@cons type t tys) (@cons expr a el)))) *)
  simpl eval_exprlist.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@cons val v vl)) (@liftx (Tarrow val (Tarrow (list val) (LiftEnviron (list val)))) (@cons val) (@liftx (Tarrow (option val) (LiftEnviron val)) force_val (@liftx (Tarrow val (LiftEnviron (option val))) (sem_cast (typeof a) t) (@eval_expr cs a))) (@eval_exprlist cs tys el)))) *)
  destruct (msubst_eval_expr Delta T1 T2 GV a) eqn:?; inv Heqo.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@cons val (force_val (sem_cast (typeof a) t v0)) vl)) (@liftx (Tarrow val (Tarrow (list val) (LiftEnviron (list val)))) (@cons val) (@liftx (Tarrow (option val) (LiftEnviron val)) force_val (@liftx (Tarrow val (LiftEnviron (option val))) (sem_cast (typeof a) t) (@eval_expr cs a))) (@eval_exprlist cs tys el)))) *)
  apply msubst_eval_expr_eq with (P0:=P)(GV0:=GV)(R0:=R) in Heqo1.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow (list val) (LiftEnviron Prop)) (@eq (list val) (@cons val (force_val (sem_cast (typeof a) t v0)) vl)) (@liftx (Tarrow val (Tarrow (list val) (LiftEnviron (list val)))) (@cons val) (@liftx (Tarrow (option val) (LiftEnviron val)) force_val (@liftx (Tarrow val (LiftEnviron (option val))) (sem_cast (typeof a) t) (@eval_expr cs a))) (@eval_exprlist cs tys el)))) *)
  apply derives_trans with (local (`(eq v0) (eval_expr a)) && local (`(eq vl) (eval_exprlist tys el))).
(* Goal: @eq (list val) (@cons val (force_val (sem_cast (typeof a) t v0)) vl) (@cons val (force_val (sem_cast (typeof a) t v0)) vl) *)
  apply andp_right; auto.
(* Goal: forall (cs : compspecs) (Delta : tycontext) (P : list Prop) (T1 : PTree.t val) (T2 : PTree.t (prod type val)) (GV : option globals) (R : list mpred) (i : ident) (t : type) (v : val) (_ : @eq (option val) (@msubst_eval_lvar cs Delta T2 i t) (@Some val v)), @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v : forall _ : val, Prop) (eval_lvar i t))) *)
  go_lowerx. unfold_lift. intros. apply prop_right.
  rewrite <- H. rewrite <- H0.
(* Goal: @eq (list val) (@cons val (force_val (sem_cast (typeof a) t v0)) vl) (@cons val (force_val (sem_cast (typeof a) t v0)) vl) *)
 auto.
Qed.

(* Goal: @In (forall _ : environ, Prop) (locald_denote (temp i v)) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) *)
(* Goal: @eq val (eval_id i rho) v *)
Lemma msubst_eval_lvar_eq: forall {cs: compspecs} Delta P T1 T2 GV R i t v,
  msubst_eval_lvar Delta T2 i t = Some v ->
  ENTAIL Delta, PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)) |--
    local (`(eq v) (eval_lvar i t)).
Proof.
(* Goal: forall (cs : compspecs) (Delta : tycontext) (P : list Prop) (T1 : PTree.t val) (T2 : PTree.t (prod type val)) (GV : option globals) (R : list mpred) (i : ident) (t : type) (v : val) (_ : @eq (option val) (@msubst_eval_lvar cs Delta T2 i t) (@Some val v)), @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v : forall _ : val, Prop) (eval_lvar i t))) *)
  intros.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v : forall _ : val, Prop) (eval_lvar i t))) *)
  unfold PROPx, LOCALx.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@prop (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@fold_right Prop Prop and True P)) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (@fold_right (forall _ : lift_S (LiftEnviron Prop), Prop) (forall _ : lift_S (Tarrow Prop (LiftEnviron Prop)), Prop) (@liftx (Tarrow Prop (Tarrow Prop (LiftEnviron Prop))) and) (@liftx (LiftEnviron Prop) True) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)))) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v) (eval_lvar i t))) *)
  apply derives_trans with (local (tc_environ Delta) && local (fold_right (` and) (` True) (map locald_denote (LocalD T1 T2 GV)))); [solve_andp |].
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (local (@fold_right (forall _ : lift_S (LiftEnviron Prop), Prop) (forall _ : lift_S (Tarrow Prop (LiftEnviron Prop)), Prop) (@liftx (Tarrow Prop (Tarrow Prop (LiftEnviron Prop))) and) (@liftx (LiftEnviron Prop) True) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV))))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v) (eval_lvar i t))) *)
  unfold local, lift, lift1.
(* Goal: @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (fun rho : environ => @prop mpred Nveric (tc_environ Delta rho)) (fun rho : environ => @prop mpred Nveric (@fold_right (forall _ : lift_S (LiftEnviron Prop), Prop) (forall _ : lift_S (Tarrow Prop (LiftEnviron Prop)), Prop) (@liftx (Tarrow Prop (Tarrow Prop (LiftEnviron Prop))) and) (@liftx (LiftEnviron Prop) True) (@map localdef (forall _ : environ, Prop) locald_denote (LocalD T1 T2 GV)) rho))) (fun rho : environ => @prop mpred Nveric (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v) (eval_lvar i t) rho)) *)
  simpl; intro rho.
(* Goal: forall (cs : compspecs) (Delta : tycontext) (P : list Prop) (T1 : PTree.t val) (T2 : PTree.t (prod type val)) (GV : option globals) (R : list mpred) (i : ident) (t : type) (v : val) (_ : @eq (option val) (@msubst_eval_lvar cs Delta T2 i t) (@Some val v)), @derives (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (@andp (forall _ : environ, mpred) (@LiftNatDed' mpred Nveric) (local (tc_environ Delta)) (PROPx P (LOCALx (LocalD T1 T2 GV) (SEPx R)))) (local (@liftx (Tarrow val (LiftEnviron Prop)) (@eq val v : forall _ : val, Prop) (eval_lvar i t))) *)
  normalize; intros.
(* Goal: @liftx (Tarrow val (LiftEnviron Prop)) (@eq val v) (eval_lvar i t) rho *)
      autorewrite with subst norm1 norm2; normalize.
(* Goal: @eq val v (eval_lvar i t rho) *)
  pose proof (msubst_eval_lvar_eq_aux _ _ _ _ _ H0 H1).
(* Goal: @eq val v (eval_lvar i t rho) *)
  apply eq_sym.
(* Goal: @eq (list val) (@cons val (force_val (sem_cast (typeof a) t v0)) vl) (@cons val (force_val (sem_cast (typeof a) t v0)) vl) *)
  apply H2; auto.
Qed.

Ltac solve_msubst_eval_lvalue :=
  simpl;
  cbv beta iota zeta delta [force_val2 force_val1];
  rewrite ?isptr_force_ptr, <- ?offset_val_force_ptr by auto;
  reflexivity.

Ltac solve_msubst_eval_expr :=
  simpl;
  cbv beta iota zeta delta [force_val2 force_val1];
  rewrite ?isptr_force_ptr, <- ?offset_val_force_ptr by auto;
  reflexivity.

Ltac solve_msubst_eval_LR :=
  unfold msubst_eval_LR;
  simpl;
  cbv beta iota zeta delta [force_val2 force_val1];
  rewrite ?isptr_force_ptr, <- ?offset_val_force_ptr by auto;
  reflexivity.

Ltac solve_msubst_eval_lvar :=
  unfold msubst_eval_lvar; reflexivity.

(**********************************************************)
(* Continuation *)
(*
Require Import VST.veric.xexpr_rel.


Inductive l_cont : Type :=
  | LC_deref : r_cont -> l_cont
  | LC_field : l_cont -> type -> ident -> l_cont
with r_cont : Type :=
  | RC_addrof : l_cont -> r_cont
  | RC_unop : unary_operation -> r_cont -> type -> r_cont
  | RC_binop_l : binary_operation -> r_cont -> type -> r_value -> type -> r_cont
  | RC_binop_r : binary_operation -> val -> type -> r_cont -> type -> r_cont
  | RC_cast : r_cont -> type -> type -> r_cont
  | RC_byref: l_cont -> r_cont
  | RC_load: r_cont.


Definition sum_map {A B C D : Type} (f : A -> B) (g: C -> D) (x : A + C) :=
match x with
| inl y => inl (f y)
| inr z => inr (g z)
end.

Definition prod_left_map {A B C} (f: A -> B) (x: A * C) : B * C :=
  match x with
  | (x1, x2) => (f x1, x2)
  end.

Definition compute_cont_map {A B} (f: val -> val) (g: A -> B) : option (val + (A * (l_value * type))) -> option (val + (B * (l_value * type))) := option_map (sum_map f (prod_left_map g)).

Fixpoint compute_r_cont {cs: compspecs} (T1: PTree.t val) (T2: PTree.t vardesc) (e: r_value) : option (val + (r_cont * (l_value * type))) :=
  match e with
  | R_const v => Some (inl v)
  | R_tempvar id => option_map inl (PTree.get id T1)
  | R_addrof a => compute_cont_map id RC_addrof (compute_l_cont T1 T2 a)
  | R_unop op a ta => compute_cont_map (eval_unop op ta) (fun c => RC_unop op c ta) (compute_r_cont T1 T2 a)
  | R_binop op a1 ta1 a2 ta2 =>
      match compute_r_cont T1 T2 a1 with
      | Some (inl v1) => compute_cont_map (eval_binop op ta1 ta2 v1) (fun c => RC_binop_r op v1 ta1 c ta2) (compute_r_cont T1 T2 a2)
      | Some (inr (c, e_cont)) => Some (inr (RC_binop_l op c ta1 a2 ta2, e_cont))
      | None => None
      end
  | R_cast a ta ty => compute_cont_map (eval_cast ta ty) (fun c => RC_cast c ta ty) (compute_r_cont T1 T2 a)
  | R_byref a => compute_cont_map id RC_byref (compute_l_cont T1 T2 a)
  | R_load a ty => Some (inr (RC_load, (a, ty)))
  | R_ilegal _ => None
  end
with compute_l_cont {cs: compspecs} (T1: PTree.t val) (T2: PTree.t vardesc) (e: l_value) : option (val + (l_cont * (l_value * type))) :=
  match e with
  | L_var id ty => option_map inl (eval_vardesc ty (PTree.get id T2))
  | L_deref a => compute_cont_map force_ptr LC_deref (compute_r_cont T1 T2 a)
  | L_field a ta i => compute_cont_map (eval_field ta i) (fun c => LC_field c ta i) (compute_l_cont T1 T2 a)
  | L_ilegal _ => None
  end.

Fixpoint fill_r_cont (e: r_cont) (v: val): r_value :=
  match e with
  | RC_addrof a => R_addrof (fill_l_cont a v)
  | RC_unop op a ta => R_unop op (fill_r_cont a v) ta
  | RC_binop_l op a1 ta1 a2 ta2 => R_binop op (fill_r_cont a1 v) ta1 a2 ta2
  | RC_binop_r op v1 ta1 a2 ta2 => R_binop op (R_const v1) ta1 (fill_r_cont a2 v) ta2
  | RC_cast a ta ty => R_cast (fill_r_cont a v) ta ty
  | RC_byref a => R_byref (fill_l_cont a v)
  | RC_load => R_const v
  end
with fill_l_cont (e: l_cont) (v: val): l_value :=
  match e with
  | LC_deref a => L_deref (fill_r_cont a v)
  | LC_field a ta i => L_field (fill_l_cont a v) ta i
  end.


Lemma compute_LR_cont_sound: forall (cs: compspecs) (T1: PTree.t val) (T2: PTree.t vardesc) P Q R,
  (forall e v,
    compute_r_cont T1 T2 e = Some (inl v) ->
    PROPx P (LOCALx (LocalD T1 T2 Q) (SEPx R)) |-- rel_r_value e v) /\
  (forall e v c e0 sh t p v0,
    compute_r_cont T1 T2 e = Some (inr (c, (e0, t))) ->
    PROPx P (LOCALx (LocalD T1 T2 Q) (SEPx R)) |-- rel_l_value e0 p ->
    PROPx P (LOCALx (LocalD T1 T2 Q) (SEPx R)) |-- `(mapsto sh t p v0) ->
    PROPx P (LOCALx (LocalD T1 T2 Q) (SEPx R)) |-- rel_r_value (fill_r_cont c v0) v ->
    PROPx P (LOCALx (LocalD T1 T2 Q) (SEPx R)) |-- rel_r_value e v). /\
*)
