Require Import VST.sepcomp.semantics.

Require Import VST.veric.Clight_base.
Require Import VST.veric.Clight_new.
Require Import VST.veric.Clight_lemmas.
Require Import VST.sepcomp.step_lemmas.
Require Import VST.sepcomp.event_semantics.
Require Import VST.veric.SeparationLogic.
Require Import VST.veric.juicy_extspec.
Require Import VST.veric.juicy_mem.
Require VST.veric.NullExtension.
Require Import VST.veric.Clight_sim.
Require Import VST.veric.SeparationLogicSoundness.
Require Import VST.sepcomp.extspec.
Require Import VST.msl.msl_standard.

Import VericSound.
Import VericMinimumSeparationLogic.
Import VericMinimumSeparationLogic.CSHL_Def.
Import VericMinimumSeparationLogic.CSHL_Defs.

Definition ignores_juice Z (J: external_specification juicy_mem external_function Z) : Prop :=
  (forall e t b tl vl x jm jm',
     m_dry jm = m_dry jm' ->
    ext_spec_pre J e t b tl vl x jm ->
    ext_spec_pre J e t b tl vl x jm') /\
 (forall ef t b ot v x jm jm',
     m_dry jm = m_dry jm' -> 
    ext_spec_post J ef t b ot v x jm ->
    ext_spec_post J ef t b ot v x jm') /\
 (forall v x jm jm',
     m_dry jm = m_dry jm' -> 
     ext_spec_exit J v x jm ->
     ext_spec_exit J v x jm').

Import VST.veric.compcert_rmaps.R.

Definition mem_evolve (m m': mem) : Prop :=
   (* dry version of resource_decay *)
 forall loc,
 match access_at m loc Cur, access_at m' loc Cur with
 | None, None => True
 | None, Some Freeable => True
 | Some Freeable, None => True
 | Some Writable, Some p' => p' = Writable
 | Some p, Some p' => p=p' /\ access_at m loc Max = access_at m' loc Max
 | _, _ => False
 end.
   
Definition ext_spec_mem_evolve (Z: Type) 
  (D: external_specification mem external_function Z) :=
 forall ef w b tl vl ot v z m z' m',
    ext_spec_pre D ef w b tl vl z m ->
    ext_spec_post D ef w b ot v z' m' ->
    mem_evolve m m'.

Definition juicy_dry_ext_spec (Z: Type)
   (J: external_specification juicy_mem external_function Z)
   (D: external_specification mem external_function Z)
   (dessicate: forall ef jm, ext_spec_type J ef -> ext_spec_type D ef) :=
  (forall e t t' b tl vl x jm,
    dessicate e jm t = t' ->
    (ext_spec_pre J e t b tl vl x jm ->
    ext_spec_pre D e t' b tl vl x (m_dry jm))) /\
 (forall ef t t' b ot v x jm0 jm,
    (exists tl vl x0, dessicate ef jm0 t = t' /\ ext_spec_pre J ef t b tl vl x0 jm0) ->
    (level jm <= level jm0)%nat ->
    resource_at (m_phi jm) = resource_fmap (approx (level jm)) (approx (level jm)) oo juicy_mem_lemmas.rebuild_juicy_mem_fmap jm0 (m_dry jm) ->
    ghost_of (m_phi jm) = Some (ghost_PCM.ext_ghost x, compcert_rmaps.RML.R.NoneP) :: ghost_fmap (approx (level jm)) (approx (level jm)) (tl (ghost_of (m_phi jm0))) ->
    (ext_spec_post D ef t' b ot v x (m_dry jm) ->
     ext_spec_post J ef t b ot v x jm)) /\
 (forall v x jm,
     ext_spec_exit J v x jm <->
     ext_spec_exit D v x (m_dry jm)).

Definition juicy_dry_ext_spec_make (Z: Type) 
   (J: external_specification juicy_mem external_function Z) :
   external_specification mem external_function Z.
(* Goal: external_specification Mem.mem external_function Z *)
destruct J.
(* Goal: external_specification Mem.mem external_function Z *)
apply Build_external_specification with ext_spec_type.
(* Goal: forall (e : external_function) (_ : ext_spec_type e) (_ : injective_PTree block) (_ : list typ) (_ : list val) (_ : Z) (_ : Mem.mem), Prop *)
(* Goal: forall (e : external_function) (_ : ext_spec_type e) (_ : injective_PTree block) (_ : option typ) (_ : option val) (_ : Z) (_ : Mem.mem), Prop *)
(* Goal: forall (_ : option val) (_ : Z) (_ : Mem.mem), Prop *)
intros e t b tl vl x m.
apply (forall jm, m_dry jm = m -> (* external ghost matches x -> *) ext_spec_pre e t b tl vl x jm).
(* Goal: forall (e : external_function) (_ : ext_spec_type e) (_ : injective_PTree block) (_ : option typ) (_ : option val) (_ : Z) (_ : Mem.mem), Prop *)
(* Goal: forall (_ : option val) (_ : Z) (_ : Mem.mem), Prop *)
intros e t b ot v x m.
(* Goal: Prop *)
(* Goal: forall (_ : option val) (_ : Z) (_ : Mem.mem), Prop *)
apply (forall jm, m_dry jm = m -> ext_spec_post e t b ot v x jm).
(* Goal: forall (_ : option val) (_ : Z) (_ : Mem.mem), Prop *)
intros v x m.
(* Goal: Prop *)
apply (forall jm, m_dry jm = m -> ext_spec_exit v x jm).
Defined.


Definition dessicate_id Z 
   (J: external_specification juicy_mem external_function Z) :
   forall ef (jm : juicy_mem), ext_spec_type J ef -> 
       ext_spec_type (juicy_dry_ext_spec_make Z J) ef.
(* Goal: forall (jm : juicy_mem) (m' : Mem.mem) (phi' : compcert_rmaps.RML.R.rmap) (_ : mem_evolve (m_dry jm) m') (_ : @eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi') (juicy_mem_lemmas.rebuild_juicy_mem_fmap jm m')), mem_rmap_cohere m' phi' *)
intros.
(* Goal: @ext_spec_type Mem.mem external_function Z (juicy_dry_ext_spec_make Z J) ef *)
destruct J; simpl in *. apply X.
Defined.

Lemma jdes_make_lemma:
  forall Z J, ignores_juice Z J ->
    juicy_dry_ext_spec Z J (juicy_dry_ext_spec_make Z J)
     (dessicate_id Z J).
Proof.
(* Goal: forall (jm : juicy_mem) (m' : Mem.mem) (phi' : compcert_rmaps.RML.R.rmap) (_ : mem_evolve (m_dry jm) m') (_ : @eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi') (juicy_mem_lemmas.rebuild_juicy_mem_fmap jm m')), mem_rmap_cohere m' phi' *)
intros.
destruct H as [? [? ?]], J; split; [ | split3]; simpl in *; intros; auto.
-
subst t'.
eapply H. symmetry; eassumption.  auto.
-
destruct H2 as (? & ? & ? & ? & ?).
subst t'.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
eapply H0; auto.
-
eapply H1. symmetry; eassumption. auto.
Qed.

Definition mem_rmap_cohere m phi :=
  contents_cohere m phi /\
  access_cohere m phi /\
  max_access_cohere m phi /\ alloc_cohere m phi.

Lemma age_to_cohere:
 forall m phi n,
    mem_rmap_cohere m phi -> mem_rmap_cohere m (age_to.age_to n phi).
Proof.
(* Goal: forall (jm : juicy_mem) (m' : Mem.mem) (phi' : compcert_rmaps.RML.R.rmap) (_ : mem_evolve (m_dry jm) m') (_ : @eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi') (juicy_mem_lemmas.rebuild_juicy_mem_fmap jm m')), mem_rmap_cohere m' phi' *)
intros.
(* Goal: mem_rmap_cohere m (@proj1_sig compcert_rmaps.RML.R.rmap (fun phi0 : compcert_rmaps.RML.R.rmap => and (@eq nat (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi0) (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi)) (and (@eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi0) (compcert_rmaps.RML.R.resource_at phi)) (@eq compcert_rmaps.RML.R.ghost (compcert_rmaps.RML.R.ghost_of phi0) g))) (compcert_rmaps.RML.make_rmap (compcert_rmaps.RML.R.resource_at phi) g (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi) (initial_world.set_ghost_obligation_1 phi) Hg)) *)
destruct H as [? [? [? ?]]].
(* Goal: forall (jm : juicy_mem) (m' : Mem.mem) (phi' : compcert_rmaps.RML.R.rmap) (_ : mem_evolve (m_dry jm) m') (_ : @eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi') (juicy_mem_lemmas.rebuild_juicy_mem_fmap jm m')), mem_rmap_cohere m' phi' *)
split; [ | split3]; hnf; intros.
-
(* Goal: and (@eq memval (contents_at m loc) v) (@eq compcert_rmaps.RML.R.preds pp compcert_rmaps.RML.R.NoneP) *)
hnf in H.
(* Goal: and (@eq memval (contents_at m loc) v) (@eq compcert_rmaps.RML.R.preds pp compcert_rmaps.RML.R.NoneP) *)
rewrite age_to_resource_at.age_to_resource_at in H3.
(* Goal: and (@eq memval (contents_at m loc) v) (@eq compcert_rmaps.RML.R.preds pp compcert_rmaps.RML.R.NoneP) *)
destruct (phi @ loc) eqn:?H; inv H3.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (H _ _ _ _ _ H4); split; subst; auto.
-
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.resource_at (@age_to.age_to n compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi) loc) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
rewrite age_to_resource_at.age_to_resource_at .
(* Goal: @eq (option permission) (access_at m loc Cur) (perm_of_res (compcert_rmaps.RML.R.resource_at phi loc)) *)
specialize (H0 loc).
(* Goal: @eq (option permission) (access_at m loc Cur) (perm_of_res (compcert_rmaps.RML.R.resource_at phi loc)) *)
rewrite H0.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (phi @ loc); simpl; auto.
-
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.resource_at (@age_to.age_to n compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi) loc) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
rewrite age_to_resource_at.age_to_resource_at .
(* Goal: Mem.perm_order'' (max_access_at m loc) (perm_of_res' (compcert_rmaps.RML.R.resource_at phi loc)) *)
specialize (H1 loc).
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (phi @ loc); simpl; auto.
-
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.resource_at (@age_to.age_to n compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi) loc) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
rewrite age_to_resource_at.age_to_resource_at .
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.resource_at phi loc) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
specialize (H2 loc H3).
(* Goal: Mem.perm_order'' (access_at m' loc Cur) (@Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
rewrite H2.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
reflexivity.
Qed.

Lemma set_ghost_cohere:
 forall m phi g H,
    mem_rmap_cohere m phi -> 
   mem_rmap_cohere m (initial_world.set_ghost phi g H).
Proof.
(* Goal: forall (jm : juicy_mem) (m' : Mem.mem) (phi' : compcert_rmaps.RML.R.rmap) (_ : mem_evolve (m_dry jm) m') (_ : @eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi') (juicy_mem_lemmas.rebuild_juicy_mem_fmap jm m')), mem_rmap_cohere m' phi' *)
intros.
(* Goal: mem_rmap_cohere m (initial_world.set_ghost phi g H) *)
unfold initial_world.set_ghost.
(* Goal: mem_rmap_cohere m (@proj1_sig compcert_rmaps.RML.R.rmap (fun phi0 : compcert_rmaps.RML.R.rmap => and (@eq nat (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi0) (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi)) (and (@eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi0) (compcert_rmaps.RML.R.resource_at phi)) (@eq compcert_rmaps.RML.R.ghost (compcert_rmaps.RML.R.ghost_of phi0) g))) (compcert_rmaps.RML.make_rmap (compcert_rmaps.RML.R.resource_at phi) g (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi) (initial_world.set_ghost_obligation_1 phi) H)) *)
rename H into Hg. rename H0 into H.
(* Goal: mem_rmap_cohere m (@proj1_sig compcert_rmaps.RML.R.rmap (fun phi0 : compcert_rmaps.RML.R.rmap => and (@eq nat (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi0) (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi)) (and (@eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi0) (compcert_rmaps.RML.R.resource_at phi)) (@eq compcert_rmaps.RML.R.ghost (compcert_rmaps.RML.R.ghost_of phi0) g))) (compcert_rmaps.RML.make_rmap (compcert_rmaps.RML.R.resource_at phi) g (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi) (initial_world.set_ghost_obligation_1 phi) Hg)) *)
destruct H as [? [? [? ?]]].
(* Goal: forall (jm : juicy_mem) (m' : Mem.mem) (phi' : compcert_rmaps.RML.R.rmap) (_ : mem_evolve (m_dry jm) m') (_ : @eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi') (juicy_mem_lemmas.rebuild_juicy_mem_fmap jm m')), mem_rmap_cohere m' phi' *)
split; [ | split3]; hnf; intros.
-
(* Goal: and (@eq memval (contents_at m loc) v) (@eq compcert_rmaps.RML.R.preds pp compcert_rmaps.RML.R.NoneP) *)
hnf in H.
(* Goal: and (@eq memval (contents_at m loc) v) (@eq compcert_rmaps.RML.R.preds pp compcert_rmaps.RML.R.NoneP) *)
rewrite resource_at_make_rmap in H3.
(* Goal: and (@eq memval (contents_at m loc) v) (@eq compcert_rmaps.RML.R.preds pp compcert_rmaps.RML.R.NoneP) *)
destruct (phi @ loc) eqn:?H; inv H3.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (H _ _ _ _ _ H4); split; subst; auto.
-
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.resource_at (@proj1_sig compcert_rmaps.RML.R.rmap (fun phi0 : compcert_rmaps.RML.R.rmap => and (@eq nat (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi0) (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi)) (and (@eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi0) (compcert_rmaps.RML.R.resource_at phi)) (@eq compcert_rmaps.RML.R.ghost (compcert_rmaps.RML.R.ghost_of phi0) g))) (compcert_rmaps.RML.make_rmap (compcert_rmaps.RML.R.resource_at phi) g (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi) (initial_world.set_ghost_obligation_1 phi) Hg)) loc) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
rewrite resource_at_make_rmap.
(* Goal: @eq (option permission) (access_at m loc Cur) (perm_of_res (compcert_rmaps.RML.R.resource_at phi loc)) *)
specialize (H0 loc).
(* Goal: @eq (option permission) (access_at m loc Cur) (perm_of_res (compcert_rmaps.RML.R.resource_at phi loc)) *)
rewrite H0.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (phi @ loc); simpl; auto.
-
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.resource_at (@proj1_sig compcert_rmaps.RML.R.rmap (fun phi0 : compcert_rmaps.RML.R.rmap => and (@eq nat (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi0) (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi)) (and (@eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi0) (compcert_rmaps.RML.R.resource_at phi)) (@eq compcert_rmaps.RML.R.ghost (compcert_rmaps.RML.R.ghost_of phi0) g))) (compcert_rmaps.RML.make_rmap (compcert_rmaps.RML.R.resource_at phi) g (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi) (initial_world.set_ghost_obligation_1 phi) Hg)) loc) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
rewrite resource_at_make_rmap.
(* Goal: Mem.perm_order'' (max_access_at m loc) (perm_of_res' (compcert_rmaps.RML.R.resource_at phi loc)) *)
specialize (H1 loc).
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (phi @ loc); simpl; auto.
-
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.resource_at (@proj1_sig compcert_rmaps.RML.R.rmap (fun phi0 : compcert_rmaps.RML.R.rmap => and (@eq nat (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi0) (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi)) (and (@eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi0) (compcert_rmaps.RML.R.resource_at phi)) (@eq compcert_rmaps.RML.R.ghost (compcert_rmaps.RML.R.ghost_of phi0) g))) (compcert_rmaps.RML.make_rmap (compcert_rmaps.RML.R.resource_at phi) g (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi) (initial_world.set_ghost_obligation_1 phi) Hg)) loc) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
rewrite resource_at_make_rmap.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.resource_at phi loc) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
specialize (H2 loc H3).
(* Goal: Mem.perm_order'' (access_at m' loc Cur) (@Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
rewrite H2.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
reflexivity.
Qed.

Lemma mem_evolve_cohere:
  forall jm m' phi',
   mem_evolve (m_dry jm) m' ->
   compcert_rmaps.RML.R.resource_at phi' =
     juicy_mem_lemmas.rebuild_juicy_mem_fmap jm m' ->
   mem_rmap_cohere m' phi'.
Proof.
(* Goal: forall (jm : juicy_mem) (m' : Mem.mem) (phi' : compcert_rmaps.RML.R.rmap) (_ : mem_evolve (m_dry jm) m') (_ : @eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi') (juicy_mem_lemmas.rebuild_juicy_mem_fmap jm m')), mem_rmap_cohere m' phi' *)
intros.
(* Goal: mem_rmap_cohere m' phi' *)
destruct jm.
(* Goal: mem_rmap_cohere m' phi' *)
simpl in *.
unfold  juicy_mem_lemmas.rebuild_juicy_mem_fmap in H0.
(* Goal: mem_rmap_cohere m' phi' *)
simpl in H0.
(* Goal: mem_rmap_cohere m' phi' *)
split; [ | split3].
-
(* Goal: alloc_cohere m' phi' *)
hnf; intros; specialize (H loc).
(* Goal: @eq compcert_rmaps.RML.R.resource match compcert_rmaps.RML.R.resource_at phi loc with | compcert_rmaps.RML.R.NO sh rsh => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh rsh | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL m as k) p => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.LK n i as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.FUN f c as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.PURE k pp => compcert_rmaps.RML.R.PURE k pp end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
rewrite (JMaccess loc) in *.
(* Goal: mem_rmap_cohere m' phi' *)
rewrite H0 in *; clear H0; simpl in *.
(* Goal: @eq compcert_rmaps.RML.R.resource match compcert_rmaps.RML.R.resource_at phi loc with | compcert_rmaps.RML.R.NO sh rsh => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh rsh | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL m as k) p => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.LK n i as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.FUN f c as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.PURE k pp => compcert_rmaps.RML.R.PURE k pp end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct (phi @ loc) eqn:?H.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
simpl in H. if_tac in H.
(* Goal: False *)
(* Goal: False *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
if_tac in H1.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
inv H1; auto.
(* Goal: @eq (option permission) (@None permission) (perm_of_res (if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable)) *)
(* Goal: @eq (option permission) (access_at m' loc Cur) (@Some permission Nonempty) *)
(* Goal: @eq (option permission) (access_at m' loc Cur) (@Some permission Nonempty) *)
(* Goal: @eq (option permission) (access_at m' loc Cur) (perm_of_res (compcert_rmaps.RML.R.PURE k p)) *)
inv H1.
(* Goal: False *)
(* Goal: False *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
if_tac in H1.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
inv H1; auto.
(* Goal: @eq (option permission) (@None permission) (perm_of_res (if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable)) *)
(* Goal: @eq (option permission) (access_at m' loc Cur) (@Some permission Nonempty) *)
(* Goal: @eq (option permission) (access_at m' loc Cur) (@Some permission Nonempty) *)
(* Goal: @eq (option permission) (access_at m' loc Cur) (perm_of_res (compcert_rmaps.RML.R.PURE k p)) *)
inv H1.
(* Goal: mem_rmap_cohere m' phi' *)
destruct k; simpl in *.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (perm_of_sh sh0) as [[ | | | ] | ] eqn:?H; try contradiction ;auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (access_at m' loc Cur) as [[ | | | ] | ]  eqn:?H; try contradiction; try discriminate; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
if_tac in H1; inv H1; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
if_tac in H1; inv H1; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
if_tac in H1; inv H1; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
if_tac in H1; inv H1; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
if_tac in H1; inv H1; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
if_tac in H1; inv H1; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
if_tac in H1; inv H1; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
inv H1; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
inv H1; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
inv H1; auto.
-
(* Goal: alloc_cohere m' phi' *)
hnf; intros; specialize (H loc).
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.resource_at phi' loc) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
rewrite H0; clear H0.
(* Goal: @eq compcert_rmaps.RML.R.resource match compcert_rmaps.RML.R.resource_at phi loc with | compcert_rmaps.RML.R.NO sh rsh => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh rsh | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL m as k) p => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.LK n i as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.FUN f c as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.PURE k pp => compcert_rmaps.RML.R.PURE k pp end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
rewrite (JMaccess loc) in *.
(* Goal: @eq compcert_rmaps.RML.R.resource match compcert_rmaps.RML.R.resource_at phi loc with | compcert_rmaps.RML.R.NO sh rsh => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh rsh | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL m as k) p => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.LK n i as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.FUN f c as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.PURE k pp => compcert_rmaps.RML.R.PURE k pp end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct (phi @ loc) eqn:?H.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
simpl in H. if_tac in H.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (access_at m' loc Cur) as [[ | | | ] | ] eqn:?H; try contradiction; try discriminate; simpl; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
unfold perm_of_sh. rewrite if_true by auto. rewrite if_true by auto. auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
subst. rewrite if_true by auto; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (access_at m' loc Cur) as [[ | | | ] | ] eqn:?H; try contradiction; try discriminate; simpl; auto.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
rewrite if_false by auto; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct k; simpl in *; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (perm_of_sh sh) as [[ | | | ] | ] eqn:?H; try contradiction ;auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (access_at m' loc Cur) as [[ | | | ] | ]  eqn:?H; try solve [contradiction]; try discriminate; auto.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
simpl. rewrite if_true; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (access_at m' loc Cur) as [[ | | | ] | ]  eqn:?H; try solve [contradiction]; try discriminate; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (access_at m' loc Cur) as [[ | | | ] | ]  eqn:?H; try solve [contradiction]; try discriminate; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (access_at m' loc Cur) as [[ | | | ] | ]  eqn:?H; try solve [contradiction]; try discriminate; auto.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
elimtype False; clear - r H1.
(* Goal: False *)
(* Goal: False *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
unfold perm_of_sh in H1. if_tac in H1. if_tac in H1; inv H1.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
rewrite if_true in H1 by auto. inv H1.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (access_at m' loc Cur) as [[ | | | ] | ]  eqn:?H; try solve [contradiction]; try discriminate; auto.
(* Goal: False *)
(* Goal: False *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
unfold perm_of_sh in H1. if_tac in H1. if_tac in H1; inv H1.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
rewrite if_true in H1 by auto. inv H1.
(* Goal: False *)
(* Goal: False *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
unfold perm_of_sh in H1. if_tac in H1. if_tac in H1; inv H1.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
rewrite if_true in H1 by auto.
(* Goal: @eq (option permission) (@None permission) (perm_of_res (if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable)) *)
(* Goal: @eq (option permission) (access_at m' loc Cur) (@Some permission Nonempty) *)
(* Goal: @eq (option permission) (access_at m' loc Cur) (@Some permission Nonempty) *)
(* Goal: @eq (option permission) (access_at m' loc Cur) (perm_of_res (compcert_rmaps.RML.R.PURE k p)) *)
inv H1.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (access_at m' loc Cur) as [[ | | | ] | ]  eqn:?H; try solve [contradiction]; try discriminate; auto.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (access_at m' loc Cur) as [[ | | | ] | ]  eqn:?H; try solve [contradiction]; try discriminate; auto.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (access_at m' loc Cur) as [[ | | | ] | ]  eqn:?H; try solve [contradiction]; try discriminate; auto.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
simpl in H; destruct H; discriminate.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
simpl in H; destruct H; discriminate.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
simpl in H; destruct H; discriminate.
-
(* Goal: alloc_cohere m' phi' *)
hnf; intros; specialize (H loc).
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.resource_at phi' loc) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
rewrite H0; clear H0.
(* Goal: @eq compcert_rmaps.RML.R.resource match compcert_rmaps.RML.R.resource_at phi loc with | compcert_rmaps.RML.R.NO sh rsh => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh rsh | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL m as k) p => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.LK n i as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.FUN f c as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.PURE k pp => compcert_rmaps.RML.R.PURE k pp end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
rewrite (JMaccess loc) in *.
(* Goal: @eq compcert_rmaps.RML.R.resource match compcert_rmaps.RML.R.resource_at phi loc with | compcert_rmaps.RML.R.NO sh rsh => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh rsh | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL m as k) p => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.LK n i as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.FUN f c as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.PURE k pp => compcert_rmaps.RML.R.PURE k pp end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct (phi @ loc) eqn:?H.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
simpl in H. if_tac in H.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (access_at m' loc Cur) as [[ | | | ] | ] eqn:?H; try contradiction; try discriminate; simpl; auto.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable)) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
eapply perm_order''_trans; [apply access_cur_max | ].
(* Goal: Mem.perm_order'' (access_at m' loc Cur) (@Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
rewrite H2.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
unfold perm_of_sh. rewrite if_true by auto. rewrite if_true by auto. constructor.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
subst sh. rewrite if_true by auto.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (@None permission) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n)) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
apply po_None.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (access_at m' loc Cur) as [[ | | | ] | ] eqn:?H; try contradiction; try discriminate; simpl; auto.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh Tsh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (if @eq_dec Share.t Share.EqDec_share sh Share.bot then @None permission else @Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; discriminate.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
rewrite if_false by auto.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable)) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
eapply perm_order''_trans; [apply access_cur_max | ].
(* Goal: Mem.perm_order'' (access_at m' loc Cur) (@Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
rewrite H2. constructor.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct k; simpl in *; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
destruct (perm_of_sh sh) as [[ | | | ] | ] eqn:?H; try contradiction ;auto.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable)) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
eapply perm_order''_trans; [apply access_cur_max | ].
(* Goal: @eq compcert_rmaps.RML.R.resource (if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct (access_at m' loc Cur). destruct H; subst.
match goal with |- Mem.perm_order'' _ ?A =>
  destruct A; try constructor
end.
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
simpl.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
rewrite if_true by auto. auto.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable)) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
eapply perm_order''_trans; [apply access_cur_max | ].
(* Goal: @eq compcert_rmaps.RML.R.resource (if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct (access_at m' loc Cur). destruct H; subst.
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
rewrite if_true. simpl. rewrite H1. apply perm_refl.
clear - r H1.
(* Goal: False *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
unfold perm_of_sh in H1.
(* Goal: False *)
(* Goal: False *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
if_tac in H1. if_tac in H1. inv H1; constructor.
(* Goal: Mem.perm_order'' (@Some permission Nonempty) (@Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
inv H1; constructor.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
rewrite if_true in H1 by auto. inv H1; constructor.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
contradiction.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable)) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
eapply perm_order''_trans; [apply access_cur_max | ].
(* Goal: @eq compcert_rmaps.RML.R.resource (if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct (access_at m' loc Cur). destruct H; subst.
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
rewrite if_true. simpl. rewrite H1. apply perm_refl.
clear - r H1.
(* Goal: False *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
unfold perm_of_sh in H1.
(* Goal: False *)
(* Goal: False *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
if_tac in H1. if_tac in H1. inv H1; constructor.
(* Goal: Mem.perm_order'' (@Some permission Nonempty) (@Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
inv H1; constructor.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
rewrite if_true in H1 by auto. inv H1; constructor.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
contradiction.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable)) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
eapply perm_order''_trans; [apply access_cur_max | ].
(* Goal: @eq compcert_rmaps.RML.R.resource (if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct (access_at m' loc Cur). destruct H; subst.
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
rewrite if_true. simpl. rewrite H1. apply perm_refl.
clear - r H1.
(* Goal: False *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
unfold perm_of_sh in H1.
(* Goal: False *)
(* Goal: False *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
if_tac in H1. if_tac in H1. inv H1; constructor.
(* Goal: Mem.perm_order'' (@Some permission Nonempty) (@Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
inv H1; constructor.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
rewrite if_true in H1 by auto. inv H1; constructor.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
contradiction.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable)) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
eapply perm_order''_trans; [apply access_cur_max | ].
(* Goal: Mem.perm_order'' (access_at m' loc Cur) (perm_of_res' (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable)) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct (access_at m' loc Cur). destruct p0; try contradiction.
match goal with |- Mem.perm_order'' _ ?A =>
  destruct A; try constructor
end.
(* Goal: Mem.perm_order'' (@None permission) (perm_of_res' (if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable)) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
elimtype False.
clear - H1 r.
(* Goal: False *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
unfold perm_of_sh in H1.
(* Goal: False *)
(* Goal: False *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
if_tac in H1. if_tac in H1. inv H1; constructor.
(* Goal: Mem.perm_order'' (@Some permission Nonempty) (@Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
inv H1; constructor.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
rewrite if_true in H1 by auto. inv H1; constructor.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct (access_at m' loc Cur); try contradiction.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; subst p0.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (@Some permission Nonempty) *)
specialize (JMmax_access loc).
(* Goal: Mem.perm_order'' (max_access_at m' loc) (@Some permission Nonempty) *)
rewrite H0 in JMmax_access.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (@Some permission Nonempty) *)
simpl in JMmax_access.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (@Some permission Nonempty) *)
unfold max_access_at in *.
rewrite <- H1. auto.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct (access_at m' loc Cur); try contradiction.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_sh sh) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
destruct H; subst p0.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (@Some permission Nonempty) *)
specialize (JMmax_access loc).
(* Goal: Mem.perm_order'' (max_access_at m' loc) (@Some permission Nonempty) *)
rewrite H0 in JMmax_access.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (@Some permission Nonempty) *)
simpl in JMmax_access.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (@Some permission Nonempty) *)
unfold max_access_at in *.
rewrite <- H1. auto.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
simpl in H.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct (access_at m' loc Cur); try contradiction.
(* Goal: @eq compcert_rmaps.RML.R.resource (if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct H; subst.
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
simpl.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (@Some permission Nonempty) *)
specialize (JMmax_access loc).
(* Goal: Mem.perm_order'' (max_access_at m' loc) (@Some permission Nonempty) *)
rewrite H0 in JMmax_access.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (@Some permission Nonempty) *)
simpl in JMmax_access.
(* Goal: Mem.perm_order'' (max_access_at m' loc) (@Some permission Nonempty) *)
unfold max_access_at in *.
rewrite <- H1. auto.
-
(* Goal: alloc_cohere m' phi' *)
hnf; intros; specialize (H loc).
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.resource_at phi' loc) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
rewrite H0; clear H0.
(* Goal: @eq compcert_rmaps.RML.R.resource match compcert_rmaps.RML.R.resource_at phi loc with | compcert_rmaps.RML.R.NO sh rsh => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh rsh | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL m as k) p => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.LK n i as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.FUN f c as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.PURE k pp => compcert_rmaps.RML.R.PURE k pp end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
specialize (JMalloc loc).
(* Goal: @eq compcert_rmaps.RML.R.resource match compcert_rmaps.RML.R.resource_at phi loc with | compcert_rmaps.RML.R.NO sh rsh => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh rsh | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL m as k) p => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.LK n i as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.FUN f c as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.PURE k pp => compcert_rmaps.RML.R.PURE k pp end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
rewrite (JMaccess loc) in *.
(* Goal: @eq compcert_rmaps.RML.R.resource match compcert_rmaps.RML.R.resource_at phi loc with | compcert_rmaps.RML.R.NO sh rsh => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh rsh | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL m as k) p => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.LK n i as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.YES sh rsh (compcert_rmaps.FUN f c as k) p => compcert_rmaps.RML.R.resource_at phi loc | compcert_rmaps.RML.R.PURE k pp => compcert_rmaps.RML.R.PURE k pp end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct (phi @ loc) eqn:?H.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
simpl in H. if_tac in H.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct loc as [b z]. 
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
rewrite nextblock_access_empty in *by auto.
(* Goal: @eq compcert_rmaps.RML.R.resource (if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
subst.
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
simpl.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.NO Share.bot n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
f_equal. apply proof_irr.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct loc as [b z]. 
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
rewrite nextblock_access_empty in * by auto.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
contradiction.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct loc as [b z]. 
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
rewrite nextblock_access_empty in * by auto.
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
simpl.
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct k; auto; try contradiction H.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
simpl in H.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
destruct loc as [b z]. 
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
rewrite nextblock_access_empty in * by auto.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
contradiction.
Qed.

 Lemma whole_program_sequential_safety:
   forall {CS: compspecs} {Espec: OracleKind} (initial_oracle: OK_ty) 
     (dryspec: ext_spec OK_ty)
     (dessicate : forall (ef : external_function) jm,
               ext_spec_type OK_spec ef ->
               ext_spec_type dryspec ef)
     (JDE: juicy_dry_ext_spec _ (@JE_spec OK_ty OK_spec) dryspec dessicate)
     (DME: ext_spec_mem_evolve _ dryspec)
     prog V G m,
     @semax_prog Espec (*NullExtension.Espec*) CS prog V G ->
     Genv.init_mem prog = Some m ->
     exists b, exists q, exists m',
       Genv.find_symbol (Genv.globalenv prog) (prog_main prog) = Some b /\
       initial_core  (cl_core_sem (globalenv prog))
           0 m q m' (Vptr b Ptrofs.zero) nil /\
       forall n,
        @dry_safeN _ _ _ OK_ty (genv_symb_injective)
            (coresem_extract_cenv  (cl_core_sem (globalenv prog))
                (prog_comp_env prog)) 
            (*(dryspec  OK_ty)*) dryspec
            (Build_genv (Genv.globalenv prog) (prog_comp_env prog)) 
             n initial_oracle q m'.
Proof.
(* Goal: forall (jm : juicy_mem) (m' : Mem.mem) (phi' : compcert_rmaps.RML.R.rmap) (_ : mem_evolve (m_dry jm) m') (_ : @eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi') (juicy_mem_lemmas.rebuild_juicy_mem_fmap jm m')), mem_rmap_cohere m' phi' *)
 intros.
 destruct (@semax_prog_rule' (*NullExtension.*)Espec CS _ _ _ _ 
     0 (*additional temporary argument - TODO (Santiago): FIXME*)
     H H0) as [b [q [[H1 H2] H3]]].
 destruct (H3 O initial_oracle) as [jmx [H4x [H5x [H6x [H7x _]]]]].
 destruct (H2 jmx H4x) as [jmx' [H8x H8y]].
 exists b, q, (m_dry jmx').
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
 split3; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
 rewrite H4x in H8y. auto.
(* Goal: @eq compcert_rmaps.RML.R.resource (if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
 subst. simpl. clear H5x H6x H7x H8y.
 forget (m_dry jmx) as m. clear jmx.
 intro n.
 specialize (H3 n initial_oracle).
 destruct H3 as [jm [? [? [? [? _]]]]].
 unfold semax.jsafeN in H6.
 subst m.
 assert (joins (compcert_rmaps.RML.R.ghost_of (m_phi jm))
   (Some (ghost_PCM.ext_ref initial_oracle, compcert_rmaps.RML.R.NoneP) :: nil)) as J.
 { destruct (compcert_rmaps.RML.R.ghost_of (m_phi jm)); inv H5.
(* Goal: Mem.perm_order'' (@Some permission Nonempty) (@Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
   eexists; constructor; constructor.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
   instantiate (1 := (_, _)); constructor; simpl; constructor; auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
   instantiate (1 := (Some _, _)); repeat constructor; simpl; auto. }
 clear - JDE DME H4 J H6.
  rewrite <- H4 in H6|-*.
 assert (level jm <= n)%nat by omega.
 clear H4; rename H into H4.
 forget initial_oracle as ora.
(* Goal: forall (jm : juicy_mem) (m' : Mem.mem) (phi' : compcert_rmaps.RML.R.rmap) (_ : mem_evolve (m_dry jm) m') (_ : @eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi') (juicy_mem_lemmas.rebuild_juicy_mem_fmap jm m')), mem_rmap_cohere m' phi' *)
 revert ora jm q H4 J H6; induction n; simpl; intros.
(* Goal: Mem.perm_order'' (@Some permission Nonempty) (@Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
 assert (level (m_phi jm) = 0%nat) by omega. rewrite H; constructor.
 inv H6.
 - constructor.
 -
   rewrite <- level_juice_level_phi in H4.
   destruct H0 as (?&?&?&Hg).
   eapply safeN_step.
   + red. red. fold (globalenv prog). eassumption.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
   + destruct (H1 (Some (ghost_PCM.ext_ref ora, compcert_rmaps.RML.R.NoneP) :: nil)) as (m'' & J'' & (? & ? & ?) & ?); auto.
     { eexists; apply join_comm, core_unit. }
     { rewrite Hg.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
       destruct J; eexists; apply compcert_rmaps.RML.ghost_fmap_join; eauto. }
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
     replace (m_dry m') with (m_dry m'') by auto.
     change (level (m_phi jm)) with (level jm) in *.
     replace n0 with (level m'') by omega.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
     apply IHn; auto. omega.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
     replace (level m'') with n0 by omega. auto.
 -
(*
   assert (JDE1': ext_spec_type dryspec = ext_spec_type OK_spec)
      by apply JDE.
*)
(*   destruct JDE as [JDE1 [JDE2 [JDE3 JDE4]]]. *)
(* Goal: mem_rmap_cohere m' phi' *)
   destruct dryspec as [ty pre post exit]. simpl in *. (* subst ty. *)
(* Goal: mem_rmap_cohere m' phi' *)
   destruct JE_spec as [ty' pre' post' exit']. simpl in *.
   change (level (m_phi jm)) with (level jm) in *.
   destruct JDE as [JDE1 [JDE2 JDE3]].
   specialize (JDE1 e x (dessicate e jm x)); simpl in JDE1.
   eapply safeN_external.
     eassumption.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
     apply JDE1. reflexivity. assumption.
(* Goal: forall (jm : juicy_mem) (m' : Mem.mem) (phi' : compcert_rmaps.RML.R.rmap) (_ : mem_evolve (m_dry jm) m') (_ : @eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi') (juicy_mem_lemmas.rebuild_juicy_mem_fmap jm m')), mem_rmap_cohere m' phi' *)
     simpl. intros.
     assert (H20: exists jm', m_dry jm' = m' 
                      /\ (level jm' = n')%nat
                      /\ juicy_safety.pures_eq (m_phi jm) (m_phi jm')
                      /\ resource_at (m_phi jm') = resource_fmap (approx (level jm')) (approx (level jm')) oo juicy_mem_lemmas.rebuild_juicy_mem_fmap jm (m_dry jm')
                      /\ compcert_rmaps.RML.R.ghost_of (m_phi jm') = Some (ghost_PCM.ext_ghost z', compcert_rmaps.RML.R.NoneP) :: ghost_fmap (approx (level jm')) (approx (level jm')) (tl (ghost_of (m_phi jm)))). {
     destruct (juicy_mem_lemmas.rebuild_juicy_mem_rmap jm m') 
            as [phi [? [? ?]]].
     assert (own.ghost_approx phi (Some (ghost_PCM.ext_ghost z', NoneP) :: tl (compcert_rmaps.RML.R.ghost_of phi)) =
        Some (ghost_PCM.ext_ghost z', NoneP) :: tl (compcert_rmaps.RML.R.ghost_of phi)) as Happrox.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.NO Share.bot n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES Tsh (@writable_readable Tsh writable_share_top) (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO sh n) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
     { simpl; f_equal.
        rewrite <- compcert_rmaps.RML.ghost_of_approx at 2.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
        destruct (compcert_rmaps.RML.R.ghost_of phi); auto. }
     set (phi1 := initial_world.set_ghost _ _ Happrox).
     assert (level phi1 = level phi /\ resource_at phi1 = resource_at phi) as [Hl1 Hr1].
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
     { subst phi1; unfold initial_world.set_ghost; rewrite level_make_rmap, resource_at_make_rmap; auto. }
     pose (phi' := age_to.age_to n' phi1).
     assert (mem_rmap_cohere m' phi'). {
       clear - H1 Hr1 Hl1 H8 H7 H6 H3 DME JDE1.
       apply JDE1 in H1; [ | reflexivity].
       specialize (DME e _ _ _ _ _ _ _ _ _ _ H1 H6).
     subst phi'.
     apply age_to_cohere.
     subst phi1.
     apply set_ghost_cohere.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
     eapply mem_evolve_cohere; eauto.
   }
    destruct H10 as [H10 [H11 [H12 H13]]].
     pose (jm' := mkJuicyMem _ _ H10 H11 H12 H13).
     exists jm'.
(* Goal: mem_rmap_cohere m' phi' *)
     split; [ | split3].
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
     subst jm'; simpl; auto.
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
     subst jm' phi'; simpl. apply age_to.level_age_to. omega.
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
     hnf. split. intro loc. subst jm' phi'. simpl.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.resource_at (@age_to.age_to n compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi) loc) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
     rewrite age_to_resource_at.age_to_resource_at.
     rewrite Hr1, H8. unfold juicy_mem_lemmas.rebuild_juicy_mem_fmap.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
     destruct (m_phi jm @ loc); auto. rewrite age_to.level_age_to by omega.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
      reflexivity.
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
     intro loc. subst jm' phi'. simpl.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.resource_at (@age_to.age_to n compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi) loc) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
     rewrite age_to_resource_at.age_to_resource_at.
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
     rewrite Hr1, H8. unfold juicy_mem_lemmas.rebuild_juicy_mem_fmap; simpl.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
     destruct (m_phi jm @ loc); auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
     if_tac; simpl; auto. destruct k; simpl; auto. if_tac; simpl; eauto. simpl; eauto.
     subst jm' phi'. simpl m_phi.
     rewrite age_to_resource_at.age_to_ghost_of.
     subst phi1.
     split.
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
     extensionality; unfold compose; simpl.
     rewrite age_to_resource_at.age_to_resource_at, age_to.level_age_to by omega.
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.resource_at (@proj1_sig compcert_rmaps.RML.R.rmap (fun phi0 : compcert_rmaps.RML.R.rmap => and (@eq nat (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi0) (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi)) (and (@eq (forall _ : compcert_rmaps.RML.R.AV.address, compcert_rmaps.RML.R.resource) (compcert_rmaps.RML.R.resource_at phi0) (compcert_rmaps.RML.R.resource_at phi)) (@eq compcert_rmaps.RML.R.ghost (compcert_rmaps.RML.R.ghost_of phi0) g))) (compcert_rmaps.RML.make_rmap (compcert_rmaps.RML.R.resource_at phi) g (@level compcert_rmaps.RML.R.rmap compcert_rmaps.RML.R.ag_rmap phi) (initial_world.set_ghost_obligation_1 phi) Hg)) loc) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
     unfold initial_world.set_ghost; rewrite resource_at_make_rmap.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
     rewrite H8; auto.
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
     unfold initial_world.set_ghost; rewrite ghost_of_make_rmap; simpl.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
     rewrite age_to.level_age_to, H9 by (rewrite level_make_rmap; omega); simpl; auto.
   }
   destruct H20 as [jm'  [H26 [H27 [H28 [H29 Hg']]]]].
   specialize (H2 ret jm' z' n' Hargsty Hretty).
   spec H2. omega.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
    spec H2. hnf; split3; auto. omega.
  spec H2.
  eapply JDE2; eauto 6. omega. subst m'. apply H6.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
  destruct H2 as [c' [H2a H2b]]; exists c'; split; auto.
  hnf in H2b.
  specialize (H2b (Some (ghost_PCM.ext_ref z', compcert_rmaps.RML.R.NoneP) :: nil)).
  spec H2b. apply join_sub_refl.
  spec H2b.
  { rewrite Hg'.
    eexists (Some (ghost_PCM.ext_both z', compcert_rmaps.RML.R.NoneP) :: _);
(* Goal: Mem.perm_order'' (@Some permission Nonempty) (@Some permission Nonempty) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (access_at m' loc Cur) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' loc)) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end) *)
(* Goal: Mem.perm_order'' (max_access_at m' loc) (perm_of_res' (compcert_rmaps.RML.R.PURE k p)) *)
      repeat constructor.  }
  destruct H2b as [jm'' [? [? ?]]].
  destruct H7 as [? [? ?]].
  subst m'. rewrite <- H7.
  specialize (IHn  z' jm'' c').
  subst n'. rewrite <- H9.
  change (level (m_phi jm'')) with (level  jm'') in IHn.
  apply IHn. omega.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
  auto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
  rewrite H9; auto.
 - eapply safeN_halted; eauto.
(* Goal: Mem.perm_order'' (access_at m loc Max) (@Some permission Nonempty) *)
    apply JDE. auto.
(* Goal: @eq compcert_rmaps.RML.R.resource match k with | compcert_rmaps.VAL m => if juicy_mem_lemmas.readable_perm (@None permission) then compcert_rmaps.RML.R.YES sh r (compcert_rmaps.VAL (contents_at m' (@pair positive Z b z))) compcert_rmaps.RML.R.NoneP else compcert_rmaps.RML.R.NO Share.bot bot_unreadable | compcert_rmaps.LK n i => YES sh r k p | compcert_rmaps.FUN f c => YES sh r k p end (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
(* Goal: @eq compcert_rmaps.RML.R.resource (compcert_rmaps.RML.R.PURE k p) (compcert_rmaps.RML.R.NO Share.bot bot_unreadable) *)
 Unshelve. simpl. split; [apply Share.nontrivial | hnf]. exists None; constructor.
all: fail.
Qed.

Require Import VST.veric.juicy_safety.

Definition fun_id (ext_link: Strings.String.string -> ident) (ef: external_function) : option ident :=
  match ef with EF_external id sig => Some (ext_link id) | _ => None end.

Print genv.

Lemma module_sequential_safety : (*TODO*)
   forall {CS: compspecs} (prog: program) (V: varspecs) (G: funspecs) ora m f f_id f_b f_body args,
     let ge := Genv.globalenv prog in
     let ext_link := ext_link_prog prog in
     let spec := add_funspecs NullExtension.Espec ext_link G in
     let tys := sig_args (ef_sig f) in
     let rty := sig_res (ef_sig f) in
     let sem := juicy_core_sem (cl_core_sem (Build_genv ge (prog_comp_env prog))) in
     @semax_prog spec CS prog V G ->
     fun_id ext_link f = Some f_id ->
     Genv.find_symbol ge f_id = Some f_b ->
     Genv.find_funct  ge (Vptr f_b Ptrofs.zero) = Some f_body ->
     forall x : ext_spec_type (@OK_spec spec) f,
     ext_spec_pre (@OK_spec spec) f x (genv_symb_injective ge) tys args ora m ->
     exists q,
       initial_core sem 
         0 (*additional temporary argument - TODO (Santiago): FIXME*)
             m q m
              (Vptr f_b Ptrofs.zero) args /\
       forall n, safeN_(genv_symb := @genv_symb_injective _ _)(Hrel := juicy_extspec.Hrel) (coresem_extract_cenv sem (prog_comp_env prog))
(upd_exit (@OK_spec spec) x (genv_symb_injective ge)) ge n ora q m.
Abort.