Require Import VST.veric.Clight_base.
Require Import VST.msl.msl_standard.
Require Import VST.veric.compcert_rmaps.
Require Import VST.veric.Clight_lemmas.
Require Import VST.veric.mpred.
Require Import VST.veric.tycontext.
Require Import VST.veric.expr2.
Require Export VST.veric.environ_lemmas.
Require Import VST.veric.binop_lemmas2.
Require Import VST.veric.binop_lemmas3.
Require Import VST.veric.binop_lemmas.
Require Import VST.veric.binop_lemmas4.
Require Import VST.veric.expr_lemmas2.
Require Export VST.veric.expr_lemmas3.
Require Export VST.veric.expr_lemmas4.
Require Import VST.veric.juicy_mem.
Import Cop.
Import Cop2.
Import Clight_Cop2.

(*
Lemma typecheck_environ_update_te:
  forall rho c Delta, typecheck_temp_environ (te_of rho) (temp_types (update_tycon Delta c))
     ->
typecheck_temp_environ  (te_of rho) (temp_types Delta)

with typecheck_ls_update_te : forall Delta ty b id l,
(temp_types Delta) ! id = Some (ty, b) ->
exists b2, (temp_types (join_tycon_labeled l Delta)) ! id = Some (ty, b2).
Proof.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
intros.
unfold typecheck_temp_environ in H. unfold typecheck_temp_environ.
destruct c; intros; simpl in *; try solve[eapply H; apply H0].
*
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
destruct (eq_dec id i). subst.
destruct (H i true ty). unfold initialized. rewrite H0.
(* Goal: @eq (option type) (@PTree.get type id (@PTree.set type i0 t1 (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p))) (@Some type ty) *)
unfold temp_types. simpl. rewrite PTree.gsspec. rewrite peq_true.
auto. destruct H1. destruct H2. inv H2. exists x. auto.
(* Goal: not (@eq positive x y) *)
(* Goal: forall _ : @list_disjoint positive (@map (prod positive type) positive (@fst positive type) p) (@cons positive i0 (@map (prod positive type) positive (@fst positive type) t)), @eq (option (prod type bool)) (@PTree.get (prod type bool) id (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun param : prod ident type => @PTree.set (prod type bool) (@fst ident type param) (@pair type bool (@snd ident type param) true)) (@PTree.set (prod type bool) i0 (@pair type bool t1 false) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t (prod type bool)) => let (id, ty) := temp in @PTree.set (prod type bool) id (@pair type bool ty false) tenv) (PTree.empty (prod type bool)) t)) p)) (@PTree.get (prod type bool) id (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun param : prod ident type => @PTree.set (prod type bool) (@fst ident type param) (@pair type bool (@snd ident type param) true)) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t (prod type bool)) => let (id, ty) := temp in @PTree.set (prod type bool) id (@pair type bool ty false) tenv) (PTree.empty (prod type bool)) t) p)) *)
apply H.
unfold initialized.
remember ((temp_types Delta) ! i). destruct o. destruct p.
(* Goal: @eq (option type) (@PTree.get type id (@PTree.set type i0 t1 (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p))) (@Some type ty) *)
unfold temp_types. simpl. rewrite PTree.gsspec.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
rewrite peq_false by auto. apply H0. auto.
*
destruct o.
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
destruct (eq_dec id i). subst. destruct (H i true ty).
unfold initialized.
remember ((temp_types Delta) ! i). destruct o. destruct p.
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
unfold temp_types. simpl. inv H0.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
rewrite PTree.gsspec. rewrite peq_true. eauto. congruence.
destruct H1. destruct H2. inv H2. eauto.
(* Goal: not (@eq positive x y) *)
(* Goal: forall _ : @list_disjoint positive (@map (prod positive type) positive (@fst positive type) p) (@cons positive i0 (@map (prod positive type) positive (@fst positive type) t)), @eq (option (prod type bool)) (@PTree.get (prod type bool) id (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun param : prod ident type => @PTree.set (prod type bool) (@fst ident type param) (@pair type bool (@snd ident type param) true)) (@PTree.set (prod type bool) i0 (@pair type bool t1 false) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t (prod type bool)) => let (id, ty) := temp in @PTree.set (prod type bool) id (@pair type bool ty false) tenv) (PTree.empty (prod type bool)) t)) p)) (@PTree.get (prod type bool) id (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun param : prod ident type => @PTree.set (prod type bool) (@fst ident type param) (@pair type bool (@snd ident type param) true)) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t (prod type bool)) => let (id, ty) := temp in @PTree.set (prod type bool) id (@pair type bool ty false) tenv) (PTree.empty (prod type bool)) t) p)) *)
eapply H. unfold initialized.
remember ((temp_types Delta) ! i). destruct o. destruct p.
(* Goal: @eq (option type) (@PTree.get type id (@PTree.set type i0 t1 (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p))) (@Some type ty) *)
unfold temp_types. simpl. rewrite PTree.gsspec.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
rewrite peq_false by auto. apply H0. auto. eauto.
*
destruct (update_tycon_te_same c1 _ _ _ _ H0).
destruct (update_tycon_te_same c2 _ _ _ _ H1).
destruct (H _ _ _ H2) as [v [? ?]]. exists v.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
split; auto. destruct H4; auto. left. destruct b; simpl; auto.
*
destruct (update_tycon_te_same c1 _ _ _ _ H0).
destruct (update_tycon_te_same c2 _ _ _ _ H0).
specialize (H id ((b || x) && (b || x0))%bool ty ).
spec H.
 unfold join_tycon. remember (update_tycon Delta c1).
destruct t. remember (update_tycon Delta c2).
destruct t. unfold temp_types in *.
unfold update_tycon. simpl in *.
apply join_te_eqv; eauto.    destruct b; auto. simpl in *.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
destruct H. exists x1. split. destruct H. auto. left. auto.
*
 edestruct (update_labeled_te_same l Delta id).  apply H0.
(* Goal: or (@eq (prod positive type) (@pair ident type i t) (@pair positive type id ty)) (@In (prod positive type) (@pair positive type id ty) p) *)
 edestruct H. apply H1.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
 destruct H2. exists x0. split; auto. destruct b; simpl; auto.
* destruct (update_tycon_te_same c _ _ _ _ H0) as [bb HH].
  simpl in HH.
  destruct (H _ _ _ HH) as [v [AA BB]]. exists v; split; trivial.
  destruct BB. 2: right; trivial.
  destruct b; simpl in *. contradiction. left; trivial.
*
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
intros. destruct l; simpl in *.
exists b; assumption.
 destruct (update_tycon_te_same s _ _ _ _ H).
(* Goal: not (@eq positive x y) *)
(* Goal: forall _ : @list_disjoint positive (@map (prod positive type) positive (@fst positive type) p) (@cons positive i0 (@map (prod positive type) positive (@fst positive type) t)), @eq (option (prod type bool)) (@PTree.get (prod type bool) id (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun param : prod ident type => @PTree.set (prod type bool) (@fst ident type param) (@pair type bool (@snd ident type param) true)) (@PTree.set (prod type bool) i0 (@pair type bool t1 false) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t (prod type bool)) => let (id, ty) := temp in @PTree.set (prod type bool) id (@pair type bool ty false) tenv) (PTree.empty (prod type bool)) t)) p)) (@PTree.get (prod type bool) id (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun param : prod ident type => @PTree.set (prod type bool) (@fst ident type param) (@pair type bool (@snd ident type param) true)) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t (prod type bool)) => let (id, ty) := temp in @PTree.set (prod type bool) id (@pair type bool ty false) tenv) (PTree.empty (prod type bool)) t) p)) *)
edestruct typecheck_ls_update_te. apply H.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
rewrite temp_types_update_dist. erewrite join_te_eqv; eauto.
Qed.

Lemma typecheck_environ_update_ve : forall (rho : environ) (c : statement) (Delta : tycontext),
typecheck_var_environ (ve_of rho) (var_types (update_tycon Delta c)) ->
typecheck_var_environ (ve_of rho) (var_types Delta).
Proof.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
intros.
intros id t; specialize (H id t).
induction c; simpl in *; try apply H;
try destruct o; try rewrite set_temp_ve in *;
(* Goal: not (@eq positive x y) *)
(* Goal: forall _ : @list_disjoint positive (@map (prod positive type) positive (@fst positive type) p) (@cons positive i0 (@map (prod positive type) positive (@fst positive type) t)), @eq (option (prod type bool)) (@PTree.get (prod type bool) id (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun param : prod ident type => @PTree.set (prod type bool) (@fst ident type param) (@pair type bool (@snd ident type param) true)) (@PTree.set (prod type bool) i0 (@pair type bool t1 false) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t (prod type bool)) => let (id, ty) := temp in @PTree.set (prod type bool) id (@pair type bool ty false) tenv) (PTree.empty (prod type bool)) t)) p)) (@PTree.get (prod type bool) id (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun param : prod ident type => @PTree.set (prod type bool) (@fst ident type param) (@pair type bool (@snd ident type param) true)) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t (prod type bool)) => let (id, ty) := temp in @PTree.set (prod type bool) id (@pair type bool ty false) tenv) (PTree.empty (prod type bool)) t) p)) *)
 try apply H.
repeat rewrite update_tycon_same_ve in *; auto.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
rewrite var_types_update_dist, update_tycon_same_ve in H; auto.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
rewrite update_le_same_ve in H; auto.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
auto.
Qed.


Lemma typecheck_environ_update_ge : forall (rho : environ) (c : statement) (Delta : tycontext),
typecheck_glob_environ (ge_of rho) (glob_types (update_tycon Delta c)) ->
typecheck_glob_environ (ge_of rho) (glob_types Delta).
Proof.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
intros. destruct c; simpl in *; try apply H;
try destruct o; try rewrite set_temp_ge in *; try apply H;
unfold typecheck_glob_environ in *; intros; eapply H; try rewrite glob_types_update_dist;
try apply join_ge_eqv;
repeat rewrite update_tycon_same_ge in *; try rewrite update_le_same_ge;
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
eauto.
Qed.

Lemma typecheck_environ_update:
  forall rho c Delta, typecheck_environ (update_tycon Delta c) rho ->
       typecheck_environ Delta rho.
Proof.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
intros. unfold typecheck_environ in *. intuition.
clear - H0. unfold typecheck_temp_environ in *.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
eapply typecheck_environ_update_te; eauto.

clear -H. eapply typecheck_environ_update_ve; eauto.

eapply typecheck_environ_update_ge.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
eauto.
Qed.
*)
Lemma tc_bool_val:
  forall v t,
       tc_val t v ->
       bool_type t = true ->
      exists b, strict_bool_val v t = Some b.
Proof.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
intros.
(* Goal: @ex bool (fun b : bool => @eq (option bool) (strict_bool_val v t) (@Some bool b)) *)
unfold bool_type in H0.
destruct (eqb_type t int_or_ptr_type) eqn:J;
destruct t; try destruct f; inv H0;
destruct v; try solve [inv H]; simpl; eauto;
exists false;
unfold tc_val in H; try rewrite J in H;
try (inv H; try reflexivity).
Qed.

Lemma bool_val_strict: forall t v b, tc_val t v -> bool_type t = true -> bool_val t v = Some b ->
  strict_bool_val v t = Some b.
Proof.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
  intros.
(* Goal: @eq (option bool) (strict_bool_val v t) (@Some bool b) *)
  assert (eqb_type t int_or_ptr_type = false) as Hf.
(* Goal: @eq bool (eqb_type t int_or_ptr_type) false *)
(* Goal: @eq (option bool) (strict_bool_val v t) (@Some bool b) *)
  { destruct t; auto.
(* Goal: @eq bool (eqb_type (Tpointer t a) int_or_ptr_type) false *)
    apply negb_true; auto. }
  destruct t, v; auto; try solve [destruct f; auto]; simpl in *; unfold bool_val in *;
    simpl in *; rewrite ?Hf in *; auto; try discriminate; simpl in *; try contradiction.
(* Goal: @eq (option bool) (if Archi.ptr64 then @None bool else if Int.eq i Int.zero then @Some bool false else @None bool) (@Some bool b) *)
  destruct Archi.ptr64; inv H1.
(* Goal: @eq (option bool) (if Int.eq Int.zero Int.zero then @Some bool false else @None bool) (@Some bool (negb (Int.eq Int.zero Int.zero))) *)
  rewrite ?Int.eq_true, ?Int64.eq_true; auto.
Qed.

Lemma bool_val_Cop: forall t v m b b', bool_val t v = Some b -> Cop.bool_val v t m = Some b' ->
  b = b'.
Proof.
  destruct t, v; try discriminate; unfold bool_val; try destruct f; try discriminate;
    simpl; intros ???; try simple_if_tac; intro; inv H; unfold Cop.bool_val; simpl; intro X;
    inv X; auto;
  try solve [destruct i; inv H0; auto];
  try solve [revert H0; repeat simple_if_tac; intros; congruence].
Qed.

Lemma map_ptree_rel : forall id v te, Map.set id v (make_tenv te) = make_tenv (PTree.set id v te).
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
intros. unfold Map.set. unfold make_tenv. extensionality. rewrite PTree.gsspec; auto.
Qed.

(* Goal: @eq bool (eqb_type t int_or_ptr_type) false *)
(* Goal: @eq (option bool) (strict_bool_val v t) (@Some bool b) *)
Lemma cast_exists : forall {CS: compspecs} Delta e2 t rho phi
(TC: typecheck_environ Delta rho),
denote_tc_assert (typecheck_expr Delta e2) rho phi ->
denote_tc_assert (isCastResultType (typeof e2) t e2)
  rho phi ->
sem_cast (typeof e2) t (eval_expr e2 rho)  =
Some (force_val (sem_cast (typeof e2) t (eval_expr e2 rho))).
Proof.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
intros.
(* Goal: @eq bool (eqb_type t int_or_ptr_type) false *)
(* Goal: @eq (option bool) (strict_bool_val v t) (@Some bool b) *)
assert (exists v, sem_cast (typeof e2) t (eval_expr e2 rho) = Some v). {
apply typecheck_expr_sound in H; [ | auto ].
rewrite isCastR in H0.
unfold sem_cast.
rename t into t0.
remember (typeof e2); remember (eval_expr e2 rho).
unfold sem_cast.
destruct Archi.ptr64 eqn:Hp.
*
destruct (eqb_type t int_or_ptr_type) eqn:J.
 +
  apply eqb_type_true in J. rewrite J in *.
  exists v.
  hnf in H; rewrite eqb_type_refl in H; unfold is_pointer_or_integer in H;
  rewrite Hp in H.
  destruct v; try contradiction;
  unfold classify_cast in *;
  destruct t0 as [ | [ | | | ] [ | ] ? | i2 ? | [ | ] ? | | | | | ];
  rewrite ?Hp in *; try contradiction;
  simpl in H0 |- *; auto.
+
 unfold sem_cast_pointer, classify_cast in *. rewrite Hp, J in *.
 destruct (eqb_type t0 int_or_ptr_type) eqn:J0.
 -
  apply eqb_type_true in J0. subst t0.
  unfold int_or_ptr_type at 1 in H0. unfold int_or_ptr_type at 1.
  destruct (is_int_type t) eqn:?HH.
  ** destruct t; try inv HH.
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
     inv H0.
  ** 
    destruct t as [ | [ | | | ] [ | ] a | i a | [ | ] a | | | | | ]; destruct v; try contradiction; try inv HH;
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
    eauto.
 -
 destruct t0 as [ | [ | | | ] [ | ] ? | ? ? | [ | ] ? | | | | | ]; try contradiction; rewrite ?J0; eauto;
  destruct t as [ | [ | | | ] [ | ] ? | ? ? | [ | ] ? | | | | | ]; try contradiction; 
    destruct v; try contradiction; 
(*
 try (rewrite denote_tc_assert_andp in H0; simpl in H0);
 unfold is_pointer_type in H0; rewrite ?J in H0; rewrite ?J0 in H0;
*) simpl in *; rewrite ?J in *; rewrite ?J0 in *;
  try solve [eexists; simpl; eauto];
 try contradiction;
 try solve [
  unfold_lift in H0; simpl in H0; rewrite <- Heqv in H0; simpl in H0; 
  match type of H0 with (app_pred match ?ZZ with Some _ => _ | None => _ end _ /\ _) =>
    destruct ZZ eqn:H5
 end;
 destruct H0 as [H0 H0']; do 3 red in H0, H0';
 try contradiction;
 simpl;
 first [rewrite (float_to_int_ok _ _ H5)
        | rewrite (float_to_intu_ok _ _ H5)
        | rewrite (single_to_int_ok _ _ H5)
        | rewrite (single_to_intu_ok _ _ H5)
        ] ;
    [ eexists; reflexivity
    | split; omega ]].
 all: try (unfold is_pointer_or_null in H; rewrite Hp in H; contradiction).
all:  try (rewrite Hp; eexists; reflexivity).
*
destruct (eqb_type t int_or_ptr_type) eqn:J.
 +
  apply eqb_type_true in J. rewrite J in *.
  exists v.
  hnf in H; rewrite eqb_type_refl in H; unfold is_pointer_or_integer in H;
  rewrite Hp in H.
  destruct v; try contradiction;
  unfold classify_cast in *;
  destruct t0 as [ | [ | | | ] [ | ] ? | i2 ? | [ | ] ? | | | | | ];
  rewrite ?Hp in *; try contradiction;
  simpl in H0 |- *; auto.
+
 unfold sem_cast_pointer, classify_cast in *. rewrite Hp, J in *.
 destruct (eqb_type t0 int_or_ptr_type) eqn:J0.
 -
  apply eqb_type_true in J0. subst t0.
  unfold int_or_ptr_type at 1 in H0. unfold int_or_ptr_type at 1.
  destruct t as [ | [ | | | ] [ | ] a | i a | [ | ] a | | | | | ]; destruct v; try contradiction; eauto.
 -
 simpl in *.
 destruct t0 as [ | [ | | | ] [ | ] ? | ? ? | [ | ] ? | | | | | ]; try contradiction; rewrite ?J0; eauto;
  destruct t as [ | [ | | | ] [ | ] ? | ? ? | [ | ] ? | | | | | ]; try contradiction; simpl in *;
    destruct v; try contradiction; 
  try solve [eexists; simpl; rewrite ?Hp; eauto];
 try (rewrite J in H); 
 try contradiction;
 try solve [
  unfold_lift in H0; simpl in H0; rewrite <- Heqv in H0; simpl in H0; 
  match type of H0 with (app_pred match ?ZZ with Some _ => _ | None => _ end _ /\ _) =>
    destruct ZZ eqn:H5
 end;
 destruct H0 as [H0 H0']; do 3 red in H0,H0';
 try contradiction;
 simpl;

 first [rewrite (float_to_int_ok _ _ H5)
        | rewrite (float_to_intu_ok _ _ H5)
        | rewrite (single_to_int_ok _ _ H5)
        | rewrite (single_to_intu_ok _ _ H5)
        ] ;
    [ eexists; reflexivity | omega];
  simpl; rewrite Hp; eauto];
  (hnf in H; rewrite Hp in H; contradiction H).
}
Opaque liftx.
destruct H1. rewrite H1. auto.
Qed.

Definition func_tycontext_t_denote :=
forall p t id ty ,  list_norepet (map fst p ++ map fst t ) ->
((make_tycontext_t p t) ! id = Some ty <-> (In (id,ty) p \/ In (id,ty) t)).

Definition func_tycontext_v_denote :=
forall v id ty, list_norepet (map fst v) ->
((make_tycontext_v v) ! id = Some ty <-> In (id,ty) v).

Lemma func_tycontext_v_sound : func_tycontext_v_denote.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
unfold func_tycontext_v_denote. intros.
split; intros; induction v. simpl in *.
rewrite PTree.gempty in *. congruence.

simpl in *. destruct a. inv H. rewrite PTree.gsspec in *. if_tac in H0.
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
inv H0. auto. intuition.

(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
inv H0.

simpl in *. destruct a. simpl in *. rewrite PTree.gsspec. destruct H0.
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
inv H0. if_tac. auto. intuition. inv H. if_tac. subst.
clear - H0 H3. rewrite in_map_iff in *. destruct H3. exists (i,ty). auto.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
apply IHv; auto.
Qed.


Lemma set_inside : forall i0 t1 t p id,
list_disjoint (map fst p) (i0 :: map fst t) ->
(fold_right
          (fun param : ident * type =>
           PTree.set (fst param) (snd param, true))
          (PTree.set i0 (t1, false)
             (fold_right
                (fun (temp : ident * type) (tenv : PTree.t (type * bool)) =>
                 let (id, ty) := temp in PTree.set id (ty, false) tenv)
                (PTree.empty (type * bool)) t)) p) ! id =
(PTree.set i0 (t1, false) (
(fold_right
          (fun param : ident * type =>
           PTree.set (fst param) (snd param, true))
                (fold_right
                (fun (temp : ident * type) (tenv : PTree.t (type * bool)) =>
                 let (id, ty) := temp in PTree.set id (ty, false) tenv)
                (PTree.empty (type * bool)) t)) p)) ! id
.
Proof.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
intros.
(* Goal: @eq (option (prod type bool)) (@PTree.get (prod type bool) id (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun param : prod ident type => @PTree.set (prod type bool) (@fst ident type param) (@pair type bool (@snd ident type param) true)) (@PTree.set (prod type bool) i0 (@pair type bool t1 false) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t (prod type bool)) => let (id, ty) := temp in @PTree.set (prod type bool) id (@pair type bool ty false) tenv) (PTree.empty (prod type bool)) t)) p)) (@PTree.get (prod type bool) id (@PTree.set (prod type bool) i0 (@pair type bool t1 false) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun param : prod ident type => @PTree.set (prod type bool) (@fst ident type param) (@pair type bool (@snd ident type param) true)) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t (prod type bool)) => let (id, ty) := temp in @PTree.set (prod type bool) id (@pair type bool ty false) tenv) (PTree.empty (prod type bool)) t) p))) *)
induction t.
  simpl in *. rewrite PTree.gsspec.
(* Goal: @eq (option type) (if peq id i0 then @Some type t1 else @PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
  if_tac.
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
    subst.
(* Goal: @eq (option (prod type bool)) (@PTree.get (prod type bool) id (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun param : prod ident type => @PTree.set (prod type bool) (@fst ident type param) (@pair type bool (@snd ident type param) true)) (@PTree.set (prod type bool) i0 (@pair type bool t1 false) (@PTree.set (prod type bool) p0 (@pair type bool t0 false) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t (prod type bool)) => let (id, ty) := temp in @PTree.set (prod type bool) id (@pair type bool ty false) tenv) (PTree.empty (prod type bool)) t))) p)) (@PTree.get (prod type bool) id (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun param : prod ident type => @PTree.set (prod type bool) (@fst ident type param) (@pair type bool (@snd ident type param) true)) (@PTree.set (prod type bool) p0 (@pair type bool t0 false) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t (prod type bool)) => let (id, ty) := temp in @PTree.set (prod type bool) id (@pair type bool ty false) tenv) (PTree.empty (prod type bool)) t)) p)) *)
    induction p.
      simpl in *. rewrite PTree.gsspec. rewrite peq_true. auto.

      simpl in *. rewrite PTree.gsspec. if_tac. subst.
      clear - H. unfold list_disjoint in *. specialize (H (fst a) (fst a)).
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
      intuition. apply IHp. unfold list_disjoint in *. intros.
      apply H; simpl in *; auto.

(* Goal: @eq (option (prod type bool)) (@PTree.get (prod type bool) id (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun param : prod ident type => @PTree.set (prod type bool) (@fst ident type param) (@pair type bool (@snd ident type param) true)) (@PTree.set (prod type bool) i0 (@pair type bool t1 false) (@PTree.set (prod type bool) p0 (@pair type bool t0 false) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t (prod type bool)) => let (id, ty) := temp in @PTree.set (prod type bool) id (@pair type bool ty false) tenv) (PTree.empty (prod type bool)) t))) p)) (@PTree.get (prod type bool) id (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun param : prod ident type => @PTree.set (prod type bool) (@fst ident type param) (@pair type bool (@snd ident type param) true)) (@PTree.set (prod type bool) p0 (@pair type bool t0 false) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t (prod type bool)) => let (id, ty) := temp in @PTree.set (prod type bool) id (@pair type bool ty false) tenv) (PTree.empty (prod type bool)) t)) p)) *)
    induction p.
       simpl in *. rewrite PTree.gsspec. if_tac. intuition.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
       auto.

       simpl in *.  repeat rewrite PTree.gsspec in *. destruct a.
       simpl in *. if_tac. auto. rewrite IHp.  auto. unfold list_disjoint in *.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
       intros. apply H; simpl in *; auto.

  simpl in *. rewrite PTree.gsspec in *. if_tac.
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
    subst.
(* Goal: @eq (option (prod type bool)) (@PTree.get (prod type bool) id (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun param : prod ident type => @PTree.set (prod type bool) (@fst ident type param) (@pair type bool (@snd ident type param) true)) (@PTree.set (prod type bool) i0 (@pair type bool t1 false) (@PTree.set (prod type bool) p0 (@pair type bool t0 false) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t (prod type bool)) => let (id, ty) := temp in @PTree.set (prod type bool) id (@pair type bool ty false) tenv) (PTree.empty (prod type bool)) t))) p)) (@PTree.get (prod type bool) id (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun param : prod ident type => @PTree.set (prod type bool) (@fst ident type param) (@pair type bool (@snd ident type param) true)) (@PTree.set (prod type bool) p0 (@pair type bool t0 false) (@fold_right (PTree.t (prod type bool)) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t (prod type bool)) => let (id, ty) := temp in @PTree.set (prod type bool) id (@pair type bool ty false) tenv) (PTree.empty (prod type bool)) t)) p)) *)
    induction p.
      simpl in *. rewrite PTree.gsspec in *. rewrite peq_true in *.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
      auto.

      simpl in *. rewrite PTree.gsspec in *. destruct a0 as (i,t0). simpl in *.
      if_tac. subst. clear - H. specialize (H i i). intuition.  apply IHp.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
      unfold list_disjoint in *. intros. apply H; simpl in *; auto.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
      intros. apply IHt. unfold list_disjoint in *. intros; simpl in *; apply H;      auto.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
      auto. auto. intuition.

    destruct a. simpl in *. induction p.
      simpl in *. rewrite PTree.gsspec. if_tac; subst. intuition.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
      repeat rewrite PTree.gsspec. auto.

      simpl in *. destruct a. simpl in *.
      spec IHt. unfold list_disjoint in *. intros; apply H; simpl in *; auto.
(* Goal: forall (_ : @list_norepet ident (@map (prod ident type) ident (@fst ident type) t)) (_ : @list_disjoint ident (@map (prod ident type) ident (@fst ident type) p) (@map (prod ident type) ident (@fst ident type) t)) (_ : or (@In (prod positive type) (@pair positive type id ty) p) (@In (prod positive type) (@pair positive type id ty) t)), @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t) p)) (@Some type ty) *)
      intuition.
      repeat rewrite PTree.gsspec in *. if_tac.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
        subst.  auto.

(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
        apply IHp. unfold list_disjoint in *.   intros. apply H. simpl in *.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
        auto.  auto. intros. auto.

Qed.

Lemma func_tycontext_t_sound : func_tycontext_t_denote.
Proof.
(* Goal: func_tycontext_t_denote *)
  unfold func_tycontext_t_denote.
  split; intros;
  unfold make_tycontext_t in *;
  apply list_norepet_app in H; destruct H as [? [? ?]].
  + induction t; induction p; simpl in *.
    - rewrite PTree.gempty in *; congruence.
    - left.
      destruct a; simpl in *. rewrite PTree.gsspec in *. if_tac in H0.
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
      inv H0. auto.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
      inv H.  destruct IHp; auto. unfold list_disjoint.  intros. inv H4.
(* Goal: or (@eq (prod positive type) (@pair ident type i t) (@pair positive type id ty)) (@In (prod positive type) (@pair positive type id ty) p) *)
      destruct H.
    - right.
      destruct a. simpl in *. rewrite PTree.gsspec in *.
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
      if_tac in H0. subst. inv H0. auto. destruct IHt. inv H1; auto.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
      unfold list_disjoint in *. intros. inv H4. auto. intuition. intuition.
    - simpl in *.
      rewrite PTree.gsspec in *.
(* Goal: or (or (@eq (prod positive type) a0 (@pair positive type id ty)) (@In (prod positive type) (@pair positive type id ty) p)) (or (@eq (prod positive type) a (@pair positive type id ty)) (@In (prod positive type) (@pair positive type id ty) t)) *)
      if_tac in H0.
      * destruct a0. simpl in *.
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
        subst. inv H0. intuition.
      * destruct a0. simpl in *.  destruct a. simpl in *.
(* Goal: or (or (@eq (prod positive type) (@pair ident type i t0) (@pair positive type id ty)) (@In (prod positive type) (@pair positive type id ty) p)) (or (@eq (prod positive type) (@pair ident type i0 t1) (@pair positive type id ty)) (@In (prod positive type) (@pair positive type id ty) t)) *)
        destruct IHp.
        ++ inv H; auto.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
        ++ intro; intros. apply H2; simpl in *; auto.
        ++ auto.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
        ++ intros. destruct IHt.
          -- inv H1; auto.
          -- intro; intros; apply H2; simpl in *; auto.
          -- auto.
          -- destruct H7.
            ** inv H7; intuition.
            ** auto.
          -- auto.
        ++ left.
(* Goal: or False (@In (prod positive type) (@pair positive type id ty) t) *)
           right. apply H4.
        ++ right. auto.
  + induction t; induction p; simpl in *.
    - intuition.
    - rewrite PTree.gsspec. if_tac.
      * subst. destruct a. simpl in *.
(* Goal: @eq (option type) (@Some type t) (@Some type ty) *)
        destruct H0; [destruct H0 |].
       ++ inv H0. auto.
       ++ subst.
          clear - H H0. inv H. rewrite in_map_iff in *. destruct H3.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
          exists (i,ty). auto.
       ++ inv H0.
      * destruct H0.
       ++ destruct a. destruct H0.
         -- subst. inv H0. intuition.
         -- simpl in *. apply IHp.
           ** inv H; auto.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
           ** intro. intros. inv H5.
           ** left. subst. auto.
       ++ inv H0.
    - destruct H0; [| destruct H0].
      * inv H0.
      * destruct a. simpl in *. inv H0; subst.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
        rewrite PTree.gsspec. rewrite peq_true. auto.
      * destruct a. simpl in *. rewrite PTree.gsspec.
(* Goal: @eq (option type) (if peq id i0 then @Some type t1 else @PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
        if_tac.
       ++ subst. clear -H0 H1. inv H1. rewrite in_map_iff in *.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
          destruct H3. exists (i,ty); auto.
       ++ apply IHt. inv H1; auto.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
          intro; auto. right. auto.
    - spec IHt; [inv H1; auto |].
      spec IHt; [intro; intros; apply H2; simpl in *; auto |].
(* Goal: @eq (option type) (@PTree.get type id (@PTree.set type (@fst ident type a0) (@snd ident type a0) (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (let (id, ty) := a in @PTree.set type id ty (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id0, ty0) := temp in @PTree.set type id0 ty0 tenv) (PTree.empty type) t)) p))) (@Some type ty) *)
      spec IHp; [inv H; auto |].
      spec IHp; [intro; intros; apply H2; simpl in *; auto |].
(* Goal: @eq (option type) (@PTree.get type id (@PTree.set type (@fst ident type a0) (@snd ident type a0) (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (let (id, ty) := a in @PTree.set type id ty (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id0, ty0) := temp in @PTree.set type id0 ty0 tenv) (PTree.empty type) t)) p))) (@Some type ty) *)
      destruct a. destruct a0. destruct H0.
      * simpl in *.
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
        destruct H0.
       ++ inv H0.
          rewrite PTree.gsspec in *. rewrite peq_true. auto.
       ++ subst.
          rewrite PTree.gsspec in *. if_tac.
         -- subst. inv H. rewrite in_map_iff in H5.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
            destruct H5. exists (i0,ty); auto.
         -- spec IHp. auto. spec IHp; auto.
      * simpl in *. rewrite PTree.gsspec. if_tac.
       ++ subst. destruct H0.
         -- inv H0. specialize (H2 i0 i0). destruct H2; simpl; auto.
         -- subst.
(* Goal: forall (_ : @list_norepet ident (@map (prod ident type) ident (@fst ident type) t)) (_ : @list_disjoint ident (@map (prod ident type) ident (@fst ident type) p) (@map (prod ident type) ident (@fst ident type) t)) (_ : or (@In (prod positive type) (@pair positive type id ty) p) (@In (prod positive type) (@pair positive type id ty) t)), @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t) p)) (@Some type ty) *)
            spec IHt; [auto |].
            rewrite PTree.gsspec in *. rewrite peq_true in *. auto.
       ++ destruct H0.
         -- inv H0.
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
            spec IHp; [auto |].
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
            spec IHp; [| auto].
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
            intros; auto. destruct H5.
           ** clear - H5 H2.
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t) p)) (@Some type ty) *)
              apply list_disjoint_cons_left, list_disjoint_sym in H2.
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t) p)) (@Some type ty) *)
              eapply list_disjoint_notin in H2; [| left; eauto].
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
              destruct H2. apply in_map_iff. exists (id, ty). auto.
           ** clear - H5 H1. inv H1. destruct H2. apply in_map_iff. exists (id, ty). auto.
         -- subst.
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
            spec IHp; [auto |].
(* Goal: @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@PTree.set type i t0 (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t)) p)) (@Some type ty) *)
            spec IHp; [| auto].
(* Goal: forall (_ : @list_norepet ident (@map (prod ident type) ident (@fst ident type) t)) (_ : @list_disjoint ident (@map (prod ident type) ident (@fst ident type) p) (@map (prod ident type) ident (@fst ident type) t)) (_ : or (@In (prod positive type) (@pair positive type id ty) p) (@In (prod positive type) (@pair positive type id ty) t)), @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t) p)) (@Some type ty) *)
            spec IHt; [auto |].
            rewrite PTree.gsspec in *.
(* Goal: forall (_ : @list_norepet ident (@map (prod ident type) ident (@fst ident type) t)) (_ : @list_disjoint ident (@map (prod ident type) ident (@fst ident type) p) (@map (prod ident type) ident (@fst ident type) t)) (_ : or (@In (prod positive type) (@pair positive type id ty) p) (@In (prod positive type) (@pair positive type id ty) t)), @eq (option type) (@PTree.get type id (@fold_right (PTree.t type) (prod ident type) (fun param : prod ident type => @PTree.set type (@fst ident type param) (@snd ident type param)) (@fold_right (PTree.t type) (prod ident type) (fun (temp : prod ident type) (tenv : PTree.t type) => let (id, ty) := temp in @PTree.set type id ty tenv) (PTree.empty type) t) p)) (@Some type ty) *)
            if_tac in IHt.
           ** intuition.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
           ** intros. auto.
Qed.

Definition cast_no_val_change (from: type)(to:type) : bool :=
match from, to with
| Tint _ _ _, Tint I32 _ _ => true
| Tpointer _ _, Tpointer _ _ => 
    eqb_type from to || 
    negb (eqb_type from int_or_ptr_type) && 
    negb (eqb_type to int_or_ptr_type)
| Tfloat F64 _ , Tfloat F64 _ => true
| Tfloat F32 _ , Tfloat F32 _ => true
| _, _ => false
end.

Lemma cast_no_change : forall v from to m,
tc_val from v ->
is_true (cast_no_val_change from to) ->
Cop.sem_cast v from to m = Some v.
Proof.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
  intros. apply is_true_e in H0.
(* Goal: @eq (option val) (Cop.sem_cast v from to m) (@Some val v) *)
  unfold cast_no_val_change in H0.
(* Goal: @eq (option val) (Cop.sem_cast v from to m) (@Some val v) *)
  unfold tc_val in H.
  destruct (eqb_type from int_or_ptr_type) eqn:J;
  destruct v; destruct from as [ | [ | | | ] [ | ] | [ | ] | [ | ] | | | | | ];
  simpl in H; try tauto;
  destruct to as [ | [ | | | ] [ | ] | [ | ] | [ | ] | | | | | ];
  simpl in *; try congruence; auto.
Qed.

(* Goal: @eq bool (eqb_type t int_or_ptr_type) false *)
(* Goal: @eq (option bool) (strict_bool_val v t) (@Some bool b) *)
Lemma tc_exprlist_length : forall {CS: compspecs} Delta tl el rho phi,
denote_tc_assert (typecheck_exprlist Delta tl el) rho phi ->
length tl = length el.
Proof.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
intros. generalize dependent el. induction tl; intros. simpl in *. destruct el. inv H. auto.
inv H. simpl in H. destruct el; try solve [inv H]. simpl in *.
(* Goal: @eq nat (S (@Datatypes.length type tl)) (S (@Datatypes.length expr el)) *)
rewrite !denote_tc_assert_andp in H.
(* Goal: @eq nat (S (@Datatypes.length type tl)) (S (@Datatypes.length expr el)) *)
f_equal; apply IHtl.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
destruct H; auto.
Qed.

(* Goal: @eq bool (eqb_type t int_or_ptr_type) false *)
(* Goal: @eq (option bool) (strict_bool_val v t) (@Some bool b) *)
Lemma neutral_cast_tc_val : forall {CS: compspecs} e t rho phi Delta,
true = is_neutral_cast (implicit_deref (typeof e)) t ->
denote_tc_assert (isCastResultType (implicit_deref (typeof e)) t  e) rho phi ->
denote_tc_assert (typecheck_expr Delta e) rho phi ->
typecheck_environ Delta rho ->
tc_val t (eval_expr e rho).
Proof.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
intros.
rewrite isCastR in H0.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
apply typecheck_expr_sound in H1; auto.
pose (AA := typeof e).
pose (BB := t).
Transparent Int.repr.
unfold classify_cast in *.
unfold tc_val, is_neutral_cast, implicit_deref, is_pointer_type, is_int_type in *.
destruct (typeof e)  as [ | [ | | | ] [ | ] | [ | ] | [ | ] | | | | | ] ;
destruct t as [ | [ | | | ] [ | ] | [ | ] | [ | ] | | | | | ]; 
try congruence;
repeat match goal with |- context [eqb_type ?A ?B] =>
  let J := fresh "J" in destruct (eqb_type A B) eqn:J;
  try rewrite J in *
end;
(*
repeat match goal with H: context [eqb_type ?A ?B] |- _ =>
  let J := fresh "J" in destruct (eqb_type A B) eqn:J;
   try rewrite J in *
end;
*)
try solve [
 simpl in H; simpl in H0;
try congruence; 
remember (eval_expr e rho); destruct v;
simpl in H0; try congruence; auto;
simpl in *; try congruence; super_unfold_lift;
try rewrite <- Heqv in *;  try unfold denote_tc_iszero in *;
try change Byte.min_signed with (-128) in *;
try change Byte.max_signed with 127 in *;
try change (Z.neg (shift_pos 15 1)) with (-32768);
try change Byte.max_unsigned with 255 in *;
try omega;
try apply H0;
try solve [destruct H1; subst; try split; compute; congruence]
].
change (negb true) with false in H.
rewrite andb_false_r in H. rewrite orb_false_r in H.
symmetry in H.
rewrite H in *.
apply eqb_type_true in H. rewrite H in *.
rewrite J in *.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
simpl in H0. auto.
change (negb false) with true in H.
rewrite andb_true_r in H.
symmetry in H.
apply orb_true_iff in H.
(* Goal: or (@eq (prod positive type) (@pair ident type i t) (@pair positive type id ty)) (@In (prod positive type) (@pair positive type id ty) p) *)
destruct H.
apply eqb_type_true in H. rewrite H in *.
rewrite J in *.
rewrite eqb_type_refl in *.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
auto.
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
destruct (eqb_type (Tpointer t0 a) int_or_ptr_type) eqn:?J; inv H.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
auto.
Qed.

Opaque Int.repr.

Definition typecheck_tid_ptr_compare
Delta id :=
match (temp_types Delta) ! id with
| Some t => is_int_type t
| None => false
end.

Lemma typecheck_tid_ptr_compare_sub:
   forall Delta Delta',
    tycontext_sub Delta Delta' ->
    forall id, typecheck_tid_ptr_compare Delta id = true ->
                typecheck_tid_ptr_compare Delta' id = true.
Proof.
unfold typecheck_tid_ptr_compare;
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
intros.
(* Goal: @eq bool match @PTree.get type id (temp_types Delta') with | Some t => is_int_type t | None => false end true *)
destruct H as [? _].
(* Goal: @eq bool match @PTree.get type id (temp_types Delta') with | Some t => is_int_type t | None => false end true *)
specialize (H id).
(* Goal: @eq bool match @PTree.get type id (temp_types Delta') with | Some t => is_int_type t | None => false end true *)
destruct ((temp_types Delta) ! id) as [? |]; try discriminate.
(* Goal: @eq bool match @PTree.get type id (temp_types Delta') with | Some t => is_int_type t | None => false end true *)
destruct ((temp_types Delta') ! id) as [? |]; try contradiction.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
 destruct H; subst; auto.
Qed.

Lemma int64_eq_e:
 forall i j, Int64.eq i j = true -> i=j.
Proof.
(* Goal: forall (i j : Int64.int) (_ : @eq bool (Int64.eq i j) true), @eq Int64.int i j *)
intros.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
pose proof (Int64.eq_spec i j). rewrite H in H0; auto.
Qed.

Lemma tc_val_sem_cast:
(* Goal: @eq bool (eqb_type t int_or_ptr_type) false *)
(* Goal: @eq (option bool) (strict_bool_val v t) (@Some bool b) *)
  forall {CS: compspecs} t2 e2 rho phi Delta,
      typecheck_environ Delta rho ->
      denote_tc_assert (typecheck_expr Delta e2) rho phi ->
      denote_tc_assert (isCastResultType (typeof e2) t2  e2) rho phi ->
      tc_val t2 (force_val (sem_cast (typeof e2) t2 (eval_expr e2 rho))).
Proof.
intros ? ? ? ? ? ? H2 H5 H6.
assert (H7 := cast_exists _ _ _ _ phi H2 H5 H6).
assert (H8 := typecheck_expr_sound _ _ _ _ H2 H5).
clear - H7 H6 H8.
Transparent liftx.
revert H7; case_eq (sem_cast (typeof e2) t2 (eval_expr e2 rho) ); intros; inv H7.
simpl.
rewrite isCastR in H6.
unfold tc_val, sem_cast, classify_cast in *.
destruct (eqb_type t2 int_or_ptr_type) eqn:J.
(* Goal: @eq bool (eqb_type t int_or_ptr_type) false *)
(* Goal: @eq (option bool) (strict_bool_val v t) (@Some bool b) *)
{
apply eqb_type_true in J; subst t2.
destruct (eqb_type (typeof e2) int_or_ptr_type) eqn:J0;
  [| destruct Archi.ptr64 eqn:Hp;
     [ try solve [inv Hp]; destruct (is_long_type (typeof e2)) eqn:?HH
     | try solve [inv Hp]; destruct (is_int_type (typeof e2)) eqn:?HH];
[| destruct (is_pointer_type (typeof e2)) eqn:?HH] ].
(* Goal: @eq bool (eqb_type t int_or_ptr_type) false *)
(* Goal: @eq (option bool) (strict_bool_val v t) (@Some bool b) *)
{
apply eqb_type_true in J0; rewrite J0 in *.
simpl in *.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
destruct (eval_expr e2 rho); inv H; auto.
}
(* Goal: @eq bool (eqb_type t int_or_ptr_type) false *)
(* Goal: @eq (option bool) (strict_bool_val v t) (@Some bool b) *)
{
destruct (typeof e2); try solve [inv HH].
simpl in H6.
rewrite N.eqb_refl in H6.
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
try inv H.
simpl in H6.
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
destruct (eval_expr e2 rho); auto.
}
(* Goal: @eq bool (eqb_type t int_or_ptr_type) false *)
(* Goal: @eq (option bool) (strict_bool_val v t) (@Some bool b) *)
{
unfold is_pointer_type in *.
rewrite J0 in *.
rewrite eqb_type_refl in H6.
simpl in *.
destruct (typeof e2); try solve [inv HH0];
try inv H;
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
destruct (eval_expr e2 rho); auto.
}

(* Goal: @eq bool (eqb_type t int_or_ptr_type) false *)
(* Goal: @eq (option bool) (strict_bool_val v t) (@Some bool b) *)
{
unfold is_pointer_type in *.
rewrite J0 in *.
rewrite eqb_type_refl in H6.
simpl in *.
destruct (typeof e2) as [ | [ | | | ] [ | ] | [ | ] | [ | ] | | | | | ];
(* Goal: @eq nat O (@Datatypes.length expr (@nil expr)) *)
(* Goal: @eq nat O (@Datatypes.length expr (@cons expr e el)) *)
(* Goal: @eq nat (@Datatypes.length type (@cons type a tl)) (@Datatypes.length expr el) *)
inv HH; inv HH0; try inv H6; try inv H8; try inv H2; auto.
}

}
destruct (eqb_type (typeof e2) int_or_ptr_type) eqn:J0.
(* Goal: @eq bool (eqb_type t int_or_ptr_type) false *)
(* Goal: @eq (option bool) (strict_bool_val v t) (@Some bool b) *)
{
unfold is_pointer_type in *.
rewrite J0 in *.
apply eqb_type_true in J0; rewrite J0, ?J in *.
rewrite (eqb_type_sym int_or_ptr_type t2), J in *.
simpl in *.
destruct t2 as [ | [ | | | ] [ | ] | [ | ] | [ | ] | | | | | ]; try contradiction;
destruct Archi.ptr64; simpl in *; inv H; try inv H6; destruct (eval_expr e2 rho); inv H6; auto.
}
unfold sem_cast_pointer in *;
destruct t2 as [ | [ | | | ] [ | ] | [ | ] | [ | ] | | | | | ] eqn:T2;
destruct Archi.ptr64 eqn:Hp;
try rewrite denote_tc_assert_andp in H6;
try contradiction;
destruct (typeof e2) as [ | [ | | | ] [ | ] | [ | ] | [ | ] | | | | | ] eqn:Te2;
auto; try contradiction;  repeat rewrite if_true in * by auto;
    repeat match goal with
    | H: app_pred (denote_tc_assert (tc_bool _ _) _) _ |- _ => 
                      apply tc_bool_e in H
   end;
 try solve [
destruct (eval_expr e2 rho); simpl in H6,H8,H|-*;
 try inv H8; try inv H;
 try contradiction;
 try match goal with
    H: match ?A with Some _ => _ | None => _ end = _ |- _ => 
          destruct A eqn:?; inv H
    end;
 try apply I;
 try match goal with
  | |- context [if ?A then _ else _] =>
     destruct A; simpl; auto; try apply I
  | |- context [Int.sign_ext ?n ?i] =>
  apply (sign_ext_range' n i);  compute; split; congruence
  | |- context [Int.zero_ext ?n ?i] =>
  apply (zero_ext_range' n i);  compute; split; congruence
  end].

all: try solve [clear H6; simpl in H; destruct (eval_expr e2 rho); inv H; assumption].

all: try (
unfold is_pointer_type in H6; rewrite ?J,?J0 in H6; simpl in H6;
simpl in H6; rewrite denote_tc_assert_iszero' in H6; simpl in H6;
unfold denote_tc_iszero in H6; unfold_lift in H6;
destruct (eval_expr e2 rho); try contradiction; inv H; apply I).


all:
try (
unfold is_pointer_type in H6; rewrite ?J,?J0 in H6; simpl in H6;
simpl in H6; rewrite denote_tc_assert_iszero' in H6; simpl in H6; 
unfold denote_tc_iszero in H6; unfold_lift in H6;
destruct (eval_expr e2 rho); try contradiction; inv H;
apply is_true_e in H6; first [apply int_eq_e in H6 | apply int64_eq_e in H6; rewrite Int64.repr_unsigned in H6]; subst;
hnf; rewrite Hp; solve [auto]).

all:
try (
unfold is_pointer_type in H6; rewrite ?J,?J0 in H6; simpl in H6;
simpl in H6; rewrite denote_tc_assert_iszero' in H6; simpl in H6;
unfold denote_tc_iszero in H6; unfold_lift in H6;
destruct (eval_expr e2 rho); try contradiction; inv H;
simpl in H8; rewrite Hp in H8; try contradiction H8;
apply is_true_e in H6; first [apply int_eq_e in H6 | apply int64_eq_e in H6; rewrite Int64.repr_unsigned in H6]; subst;
inv H8).

all:
try (unfold is_pointer_type in H6; rewrite ?J,?J0 in H6; simpl in H6;
simpl in H6; rewrite denote_tc_assert_iszero' in H6; simpl in H6; 
unfold denote_tc_iszero in H6; unfold_lift in H6;
destruct (eval_expr e2 rho); try contradiction; inv H;
apply is_true_e in H6; first [apply int_eq_e in H6 | apply int64_eq_e in H6; rewrite Int64.repr_unsigned in H6]; subst;
simpl in H8; rewrite Hp in H8; inv H8).

all: 
try (simpl eqb_type in H6; cbv iota in H6;
unfold is_pointer_type in H6; rewrite J in H6; simpl in H6;
rewrite denote_tc_assert_iszero' in H6; simpl in H6;
unfold denote_tc_iszero in H6; unfold_lift in H6;
inv H; destruct (eval_expr e2 rho); try contradiction;
do 3 red in H6;
apply is_true_e in H6; apply int64_eq_e in H6; subst; hnf; rewrite Hp; auto).

all: try (inv H1; reflexivity).
Qed.



