refl_mem_step.v 68 KB
Newer Older
1 2 3 4 5 6 7
From iris.algebra Require Import local_updates.

From stbor.lang Require Import steps_progress steps_inversion steps_retag.
From stbor.sim Require Export instance body.

Set Default Proof Using "Type".

Ralf Jung's avatar
Ralf Jung committed
8 9
Section mem.
Implicit Types Φ: resUR  nat  result  state  result  state  Prop.
10 11 12

(** MEM STEP -----------------------------------------------------------------*)

Hai Dang's avatar
Hai Dang committed
13 14 15 16 17 18 19 20 21 22 23
Lemma wsat_tmap_nxtp r σs σt :
  wsat r σs σt  r.(rtm) !! σt.(snp) = None.
Proof.
  intros (WFS & WFT & VALID & PINV & CINV & SREL & LINV).
  destruct (r.(rtm) !! σt.(snp)) as [[k h]|] eqn:Eqr; [|done].
  exfalso.
  move : (proj1 (proj1 VALID) σt.(snp)). rewrite Eqr.
  intros [[k' Eqk']%tagKindR_valid VALh]. simpl in Eqk', VALh. subst k.
  destruct (PINV σt.(snp) k' h) as [Lt _]; [by rewrite Eqr|]. lia.
Qed.

24 25 26 27 28 29 30 31 32
Lemma sim_body_alloc_local fs ft r n T σs σt Φ :
  let l := (fresh_block σt.(shp), 0) in
  let t := (Tagged σt.(snp)) in
  let σs' := mkState (init_mem l (tsize T) σs.(shp))
                     (init_stacks σs.(sst) l (tsize T) t) σs.(scs)
                     (S σs.(snp)) σs.(snc) in
  let σt' := mkState (init_mem l (tsize T) σt.(shp))
                     (init_stacks σt.(sst) l (tsize T) t) σt.(scs)
                     (S σt.(snp)) σt.(snc) in
Hai Dang's avatar
Hai Dang committed
33 34
  let rt : resUR := res_tag σt.(snp) tkUnique  in
  let r' : resUR := res_mapsto l (tsize T)  σt.(snp) in
Ralf Jung's avatar
Ralf Jung committed
35
  Φ (r  rt  r') n (PlaceR l t T) σs' (PlaceR l t T) σt' 
36 37
  r {n,fs,ft} (Alloc T, σs)  (Alloc T, σt) : Φ.
Proof.
Hai Dang's avatar
Hai Dang committed
38
  intros l t σs' σt' rt r' POST.
39 40 41
  pfold. intros NT. intros.
  have EqDOM := wsat_heap_dom _ _ _ WSAT.
  have EqFRESH := fresh_block_equiv _ _ EqDOM.
Hai Dang's avatar
Hai Dang committed
42
  have HNP := wsat_tmap_nxtp _ _ _ WSAT.
43 44 45 46 47 48 49 50 51 52 53
  destruct WSAT as (WFS & WFT & VALID & PINV & CINV & SREL & LINV).
  destruct SREL as (Eqst&Eqnp&Eqcs&Eqnc&REL).
  have Eqlst: l = (fresh_block σs.(shp), 0). { by rewrite /l EqFRESH. }
  split; [|done|].
  { right. do 2 eexists. by eapply (head_step_fill_tstep _ []), alloc_head_step. }
  constructor 1. intros ? σt1 STEPT.
  destruct (tstep_alloc_inv _ _ _ _ _ STEPT) as [? Eqσt'].
  rewrite -/σt' in Eqσt'. subst et' σt1.
  have STEPS: (Alloc T, σs) ~{fs}~> (Place l t T, σs').
  { subst l σs' t. rewrite Eqlst -Eqnp.
    eapply (head_step_fill_tstep _ []),  alloc_head_step. }
Hai Dang's avatar
Hai Dang committed
54
  eexists _, σs', (r  rt  r'), n. split; last split.
55
  { left. by apply tc_once. }
Hai Dang's avatar
Hai Dang committed
56 57 58 59 60 61 62
  { have Eqrcm: (r_f  r  rt  r').(rcm)  (r_f  r).(rcm)
      by rewrite /rcm /= 2!right_id.
    have Eqrlm: (r_f  r  rt).(rlm)  (r_f  r).(rlm)
      by rewrite /rlm /= right_id.
    have Eqrtm: (r_f  r  rt  r').(rtm)  (r_f  r  rt).(rtm)
      by rewrite /rtm /= right_id.
    have HLF :  i, (i < tsize T)%nat  (r_f  r  rt).(rlm) !! (l + i) = None.
63
    { intros i Lti.
Hai Dang's avatar
Hai Dang committed
64
      rewrite lookup_op lookup_empty right_id_L.
65 66 67 68
      destruct ((r_f  r).(rlm) !! (l + i)) as [ls|] eqn:Eql; [|done].
      exfalso.
      destruct (LINV _ (elem_of_dom_2 _ _ _ Eql)) as [EqD _].
      by apply (is_fresh_block σt.(shp) i). }
Hai Dang's avatar
Hai Dang committed
69 70 71 72
    have VALIDt:  (r_f  r  rt).
    { apply (local_update_discrete_valid_frame _ ε rt); [by rewrite right_id|].
      rewrite right_id. by apply res_tag_alloc_local_update. }
    have VALID':  (r_f  r  rt  r').
73
    { apply (local_update_discrete_valid_frame _ ε r'); [by rewrite right_id|].
Hai Dang's avatar
Hai Dang committed
74 75 76
      rewrite /= right_id -cmra_assoc cmra_assoc.
      rewrite /r'. by apply res_mapsto_local_alloc_local_update. }
    rewrite 2!cmra_assoc.
77 78 79 80 81 82 83 84
    destruct (init_mem_lookup l (tsize T) σt.(shp)) as [HLmt1 HLmt2].
    destruct (init_mem_lookup l (tsize T) σs.(shp)) as [HLms1 HLms2].
    destruct (init_stacks_lookup σt.(sst) l (tsize T) t) as [HLst1 HLst2].
    destruct (init_stacks_lookup σs.(sst) l (tsize T) t) as [HLss1 HLss2].
    split; last split; last split; last split; last split; last split.
    - by apply (tstep_wf _ _ _ STEPS WFS).
    - by apply (tstep_wf _ _ _ STEPT WFT).
    - done.
Hai Dang's avatar
Hai Dang committed
85 86 87 88 89 90 91 92 93
    - intros t1 k1 h1. rewrite Eqrtm.
      case (decide (t1 = σt.(snp))) => ?; [subst t1|].
      { intros Eqh1. split; [simpl; lia|].
        move : Eqh1. rewrite lookup_op lookup_insert.
        intros [Eqh1 ?]%tagKindR_exclusive_heaplet'. subst k1.
        intros l1 s1. rewrite -Eqh1 fmap_empty lookup_empty. by inversion 1. }
      intros Eqh'.
      have Eqh1: (r_f  r).(rtm) !! t1  Some (to_tagKindR k1, h1).
      { move : Eqh'. rewrite lookup_op lookup_insert_ne // lookup_empty right_id //. }
94 95
      specialize (PINV _ _ _ Eqh1) as [? PINV]. split; [simpl; lia|].
      intros l1 s1 Eqs1. specialize (PINV _ _ Eqs1) as [Eql PINV].
Hai Dang's avatar
Hai Dang committed
96 97
      split. { apply lmap_lookup_op_l; [apply VALID'|].
               apply lmap_lookup_op_l; [apply VALIDt|done]. }
98 99 100 101 102 103 104 105 106 107 108 109 110 111
      have InD : l1  dom (gset loc) (r_f  r).(rlm).
      { rewrite elem_of_dom. move : Eql. clear.
        destruct ((r_f  r).(rlm) !! l1) eqn:Eql1; rewrite Eql1; [by eexists|].
        by inversion 1. }
      specialize (LINV _ InD) as [EqH _].
      intros stk. subst σt'. simpl.
      destruct (init_stacks_lookup σt.(sst) l (tsize T) t) as [_ HLt1].
      have ?:  i, (i < tsize T)%nat  l1  l + i.
      { intros i Lti Eq. rewrite Eq in EqH. by apply (is_fresh_block σt.(shp) i). }
      rewrite HLt1 // HLmt2 //. apply PINV.
    - intros c cs. subst σt'. rewrite Eqrcm /=. intros Eqc.
      specialize (CINV _ _ Eqc). destruct cs as [[Tc|]| |]; [|done..].
      destruct CINV as [IN CINV]. split; [done|].
      intros t1 [Ltc Ht]%CINV. split; [lia|].
Hai Dang's avatar
Hai Dang committed
112 113 114 115 116 117 118 119 120 121 122
      intros k h. rewrite Eqrtm.
      case (decide (t1 = σt.(snp))) => ?; [subst t1|].
      { intros Eqh1. move : (proj1 (proj1 VALIDt) σt.(snp)). rewrite Eqh1.
        intros [[k' Eqk']%tagKindR_valid Eq2]. simpl in Eqk', Eq2. subst k.
        move : Eqh1. rewrite lookup_op lookup_insert.
        intros [Eqh1 ?]%tagKindR_exclusive_heaplet'.
        intros l1. rewrite -Eqh1 fmap_empty dom_empty. by inversion 1. }
      intros Eqt1'.
      have Eqt1: (r_f  r).(rtm) !! t1  Some (k, h).
      { move : Eqt1'. rewrite lookup_op lookup_insert_ne // lookup_empty right_id //. }
      intros l1 Inl1.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
      specialize (Ht _ _ Eqt1 _ Inl1).
      rewrite HLst2 //. intros i Lti Eqi.
      move : (proj1 (proj1 VALID) t1). rewrite Eqt1.
      intros [[k' Eqk']%tagKindR_valid VLh]. simpl in Eqk', VLh. subst k.
      specialize (PINV _ _ _ Eqt1) as [? PINV].
      move : Inl1. rewrite elem_of_dom. intros [ss Eqss].
      specialize (VLh l1). rewrite Eqss in VLh.
      apply to_agree_uninj in VLh as [s Eqs'].
      have Eqs : h !! l1  Some $ to_agree s by rewrite Eqss Eqs'.
      specialize (PINV _ _ Eqs) as [InP _].
      have InD : l1  dom (gset loc) (r_f  r).(rlm).
      { rewrite elem_of_dom.
        destruct ((r_f  r).(rlm) !! l1) eqn:Eql;
          rewrite Eql; [by eexists|by inversion InP]. }
      specialize (LINV _ InD) as [InD' _]. rewrite Eqi in InD'.
      by apply (is_fresh_block σt.(shp) i).
    - rewrite /srel.
      repeat split; [|simpl;auto..|].
      { subst σs' σt' l t. by rewrite Eqst EqFRESH. }
      intros l1 InD1 HL1.
      destruct (res_mapsto_lookup_shared _ _ _ _ _ _ HL1) as [HL1' NEQ].
      specialize (HLmt2 _ NEQ).
      have InD1': l1  dom (gset loc) σt.(shp)
        by rewrite elem_of_dom -HLmt2 -(elem_of_dom (D:=gset loc)).
Hai Dang's avatar
Hai Dang committed
147
      rewrite -> Eqrlm in HL1'.
148 149 150 151
      specialize (REL _ InD1' HL1') as [PB|[t' PV]].
      + left. subst σt'. rewrite /pub_loc /=. rewrite HLmt2 HLms2 //.
        intros st Eqs. specialize (PB _ Eqs) as [ss [Eqss AREL]].
        exists ss. split; [done|]. move : AREL.
Hai Dang's avatar
Hai Dang committed
152 153 154 155 156 157 158
        apply arel_mono; [done|].
        etrans. apply cmra_included_l. apply cmra_included_l.
      + right. exists t'. destruct PV as (c' & T' & h' & Eqc' & InT & Eqt' & Inl).
        exists c', T', h'. rewrite Eqrcm Eqrtm. do 2 (split; [done|]).
        split; [|done]. move : (proj1 (proj1 VALIDt) t').
        rewrite lookup_op Eqt'.
        intros EqN%exclusive_Some_l; [|apply _]. by rewrite EqN right_id.
159
    - intros l1. rewrite dom_op elem_of_union. intros [InO|InN].
Hai Dang's avatar
Hai Dang committed
160 161
      + move : InO. rewrite {1}/rlm in Eqrlm. rewrite -> Eqrlm. intros InO.
        specialize (LINV _ InO) as [InD LINV].
162 163 164 165 166 167
        have NEQ:  i, (i < tsize T)%nat  l1  l + i.
        { intros i Lt Eqi. rewrite Eqi in InD.
          by apply (is_fresh_block σt.(shp) i). }
        split.
        { subst σt'. by rewrite /= elem_of_dom (HLmt2 _ NEQ)
                                -(elem_of_dom (D:=gset loc)). }
Hai Dang's avatar
Hai Dang committed
168 169 170
        intros s t1.
        destruct (res_mapsto_lookup l (tsize T)  σt.(snp)) as [EQ1 EQ2].
        rewrite lookup_op (EQ2 _ NEQ) right_id Eqrlm.
171
        intros Eql1. specialize (LINV _ _ Eql1). subst σt'.
Hai Dang's avatar
Hai Dang committed
172 173 174 175 176 177
        rewrite /= (HLmt2 _ NEQ) (HLms2 _ NEQ) (HLst2 _ NEQ) (HLss2 _ NEQ).
        destruct LINV as (?&?&?&?& h1 & Eqt1).
        do 4 (split; [done|]). exists h1. rewrite Eqrtm.
        move : (proj1 (proj1 VALIDt) t1).
        rewrite lookup_op Eqt1.
        intros EqN%exclusive_Some_l; [|apply _]. by rewrite EqN right_id.
178 179 180 181 182 183 184 185 186 187 188
      + move: InN. rewrite elem_of_dom. intros [ls Eqls].
        have Eqls' : r'.(rlm) !! l1  Some ls by rewrite Eqls.
        destruct (res_mapsto_lookup_case l _ _ _ _ _ Eqls') as [Eq1 IN].
        destruct IN as [i [[? Lti] Eql1]].
        have Lti': (Z.to_nat i < tsize T)%nat by rewrite Nat2Z.inj_lt Z2Nat.id.
        have Eq2 := HLmt1 _ Lti'. rewrite Z2Nat.id // -Eql1 in Eq2.
        have Eq3 := HLms1 _ Lti'. rewrite Z2Nat.id // -Eql1 in Eq3.
        have Eq4 := HLst1 _ Lti'. rewrite Z2Nat.id // -Eql1 in Eq4.
        have Eq5 := HLss1 _ Lti'. rewrite Z2Nat.id // -Eql1 in Eq5.
        split.
        { subst σt'. rewrite /= elem_of_dom Eq2. by eexists. }
Hai Dang's avatar
Hai Dang committed
189
        intros s t1 Eql. rewrite /= Eq2 Eq3 Eq4 Eq5.
190 191
        specialize (HLF _ Lti'). rewrite Z2Nat.id // in HLF.
        move : Eql. rewrite Eql1 lookup_op HLF left_id /=.
Hai Dang's avatar
Hai Dang committed
192
        intros [Eq _]%res_mapsto_lookup_case. simpl in Eq.
Hai Dang's avatar
Hai Dang committed
193 194
        clear -Eq Eqrtm HNP. simplify_eq. do 4 (split; [done|]).
        exists . rewrite Eqrtm lookup_op HNP left_id lookup_insert fmap_empty //. }
195
  left.
Ralf Jung's avatar
Ralf Jung committed
196
  apply: sim_body_result. intros.
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  apply POST; eauto.
Qed.

(** Alloc *)
(* 
Lemma sim_body_alloc_shared fs ft r n T σs σt Φ :
  let ls := (fresh_block σs.(shp), 0) in
  let lt := (fresh_block σt.(shp), 0) in
  let ts := (Tagged σs.(snp)) in
  let tgt := (Tagged σt.(snp)) in
  let σs' := mkState (init_mem ls (tsize T) σs.(shp))
                     (init_stacks σs.(sst) ls (tsize T) ts) σs.(scs)
                     (S σs.(snp)) σs.(snc) in
  let σt' := mkState (init_mem lt (tsize T) σt.(shp))
                     (init_stacks σt.(sst) lt (tsize T) tgt) σt.(scs)
                     (S σt.(snp)) σt.(snc) in
  let r' : resUR :=
    (({[σt.(snp) := (to_tagKindR tkUnique, to_heapletR $ init_mem lt (tsize T) )]},
        ε), ε) in
  (ls = lt  ts = tgt 
    Φ (r  r') n (PlaceR ls ts T) σs' (PlaceR lt tgt T) σt' : Prop) 
  r {n,fs,ft} (Alloc T, σs)  (Alloc T, σt) : Φ.
Proof.
  intros ls lt ts tgt σs' σt' r' POST.
  pfold. intros NT. intros.
  have EqDOM := wsat_heap_dom _ _ _ WSAT.
  have EqFRESH := fresh_block_equiv _ _ EqDOM.
  destruct WSAT as (WFS & WFT & VALID & PINV & CINV & SREL & LINV).
  have Eqnp : σs.(snp) = σt.(snp). { by destruct SREL as (?&?&?&?). }
  have Eqlst: ls = lt. { by rewrite /ls /lt EqFRESH. }
  split; [|done|].
  { right. do 2 eexists. by eapply (head_step_fill_tstep _ []), alloc_head_step. }
  constructor 1. intros ? σt1 STEPT.
  destruct (tstep_alloc_inv _ _ _ _ _ STEPT) as [? Eqσt'].
  rewrite -/σt' in Eqσt'. subst et' σt1.
  have STEPS: (Alloc T, σs) ~{fs}~>
              (Place (fresh_block σs.(shp), 0) (Tagged σs.(snp)) T, σs').
  { subst ls σs' ts. eapply (head_step_fill_tstep _ []), alloc_head_step. }
  eexists _, σs', (r  r'), (S n). split; last split.
  { left. by apply tc_once. }
  { have HLF : (r_f  r).(rtm) !! σt.(snp) = None.
    { destruct ((r_f  r).(rtm) !! σt.(snp)) as [[k h]|] eqn:Eqkh; [|done]. exfalso.
      destruct (tagKindR_valid k) as [k' Eqk'].
      { apply (Some_valid (k,h)). rewrite -Some_valid -Eqkh. apply VALID. }
      destruct (PINV σt.(snp) k' h) as [Lt _]; [by rewrite Eqkh Eqk'|lia]. }
    have VALID':  (r_f  r  r').
    { apply (local_update_discrete_valid_frame _ ε r'); [by rewrite right_id|].
      do 2 apply prod_local_update_1. rewrite /= right_id.
      rewrite -(cmra_comm _ (r_f.(rtm)  r.(rtm))) -insert_singleton_op //.
      apply alloc_singleton_local_update; [done|]. split; [done|].
      by apply to_heapletR_valid. }
    have INCL: r_f  r  r_f  r  r' by apply cmra_included_l.
    rewrite cmra_assoc.
    destruct (init_mem_lookup ls (tsize T) σs.(shp)) as [HLs1 HLs2].
    destruct (init_mem_lookup lt (tsize T) σt.(shp)) as [HLt1 HLt2].
    split; last split; last split; last split; last split; last split.
    - by apply (tstep_wf _ _ _ STEPS WFS).
    - by apply (tstep_wf _ _ _ STEPT WFT).
    - done.
    - intros t k h. rewrite lookup_op.
      case (decide (t = σt.(snp))) => ?; [subst t|].
      + rewrite /= lookup_insert HLF left_id.
        intros [Eq1 Eq2]%Some_equiv_inj. simpl in Eq1, Eq2. split; [lia|].
        intros l s. rewrite -Eq2. intros Eqs stk Eqstk pm opro Instk NDIS.
        (* l is new memory *)
        apply to_heapletR_lookup in Eqs.
        destruct (init_mem_lookup_empty _ _ _ _ Eqs) as [i [[? Lti] Eql]].
        have Eqi: Z.of_nat (Z.to_nat i) = i by rewrite Z2Nat.id.
        have Lti': (Z.to_nat i < tsize T)%nat by rewrite Nat2Z.inj_lt Eqi.
        have ?: s = ScPoison.
        { rewrite Eql -Eqi in Eqs.
          rewrite (proj1 (init_mem_lookup lt (tsize T) )) // in Eqs.
          by inversion Eqs. } subst s.
        have Eqs2 := HLt1 _ Lti'. rewrite Eqi -Eql in Eqs2. split; [done|].
        destruct k; [|by inversion Eq1].
        have Eqstk2 := proj1 (init_stacks_lookup σt.(sst) lt (tsize T) tgt) _ Lti'.
        rewrite Eqi -Eql Eqstk in Eqstk2.
        exists []. move : Instk. inversion Eqstk2.
        rewrite elem_of_list_singleton. by inversion 1.
      + rewrite lookup_insert_ne // right_id. intros Eqkh.
        specialize (PINV t k h Eqkh) as [Lt PINV].
        split. { etrans; [exact Lt|simpl; lia]. }
        intros l s Eqs stk Eqstk pm opro Instk NDIS.
        specialize (PINV l s Eqs).
        destruct (init_stacks_lookup_case _ _ _ _ _ _ Eqstk)
          as [[EqstkO Lti]|[i [[? Lti] Eql]]].
        * specialize (PINV _ EqstkO _ _ Instk NDIS) as [Eqss PINV].
          rewrite /= HLt2 //.
        * exfalso. move : Eqstk. simpl.
          destruct (init_stacks_lookup σt.(sst) lt (tsize T) tgt) as [EQ _].
          have Lti': (Z.to_nat i < tsize T)%nat by rewrite Nat2Z.inj_lt Z2Nat.id //.
          specialize (EQ _ Lti'). rewrite Z2Nat.id // in EQ. rewrite Eql EQ.
          intros. inversion Eqstk. clear Eqstk. subst stk.
          move : Instk. rewrite elem_of_list_singleton. by inversion 1.
    - intros c cs. rewrite /rcm /= right_id => /CINV.
      destruct cs as [[T0|]| |]; [|done..]. intros [InT Eqh].
      split; [done|]. intros t2 InT2. specialize (Eqh t2 InT2) as [Lt2 Eqh].
      split; [lia|]. intros k2 h2. rewrite lookup_op.
      case (decide (t2 = σt.(snp))) => ?; [subst t2|]; [exfalso; by lia|].
      rewrite lookup_insert_ne // right_id.
      intros Eqh2 l Inl.
      specialize (Eqh _ _ Eqh2 l Inl) as (stk & pm & Eqsk & Instk).
      destruct (init_stacks_lookup_case_2 _ lt (tsize T) tgt _ _ Eqsk)
        as [[EqO NIn]|[i [[? Lti] [Eqi EqN]]]].
      + exists stk, pm. by rewrite EqO.
      + exfalso. apply (is_fresh_block σt.(shp) i).
        rewrite (state_wf_dom _ WFT). apply elem_of_dom. exists stk.
        rewrite (_: (fresh_block (shp σt), i) = lt + i) //.
        by rewrite -Eqi.
    - destruct SREL as (Eqst&_&Eqcs&Eqnc&VREL).
      repeat split; simpl; [|auto..|].
      { subst σs' σt' ls lt ts tgt. by rewrite Eqst EqFRESH Eqnp. }
      intros l st HL.
      destruct (init_mem_lookup_case _ _ _ _ _ HL) as [[EqO NIn]|[i [[? Lti] Eqi]]].
      + rewrite -Eqlst in NIn. rewrite (HLs2 _ NIn).
        specialize (VREL _ _ EqO) as [[ss [? AREL]]|[t PV]].
        * left. exists ss. split; [done|]. move : AREL. by apply arel_mono.
        * right. exists t. move : PV. by apply priv_loc_mono.
      + left. exists %S.
        have Lti': (Z.to_nat i < tsize T)%nat by rewrite Nat2Z.inj_lt Z2Nat.id //.
        specialize (HLt1 _ Lti').
        rewrite Z2Nat.id // -Eqi HL in HLt1.
        specialize (HLs1 _ Lti'). rewrite -Eqlst in Eqi.
        rewrite Z2Nat.id // -Eqi in HLs1. split; [done|by inversion HLt1].
    - intros ???. rewrite /lmap_inv /= right_id. intros IN.
      specialize (LINV _ _ _ IN) as [Eq1 Eq2].
      have ?:  i : nat, (i < tsize T)%nat  l  lt + i.
      { intros i Lt Eq. apply (is_fresh_block σt.(shp) i), elem_of_dom.
        exists s. rewrite (_ : (fresh_block σt.(shp), Z.of_nat i) = l) //. }
      rewrite HLt2 // HLs2 // Eqlst //. }
  left.
Ralf Jung's avatar
Ralf Jung committed
328
  apply: sim_body_result. intros.
329 330 331 332
  apply POST; eauto. by rewrite /ts Eqnp.
Qed. *)

(** Copy *)
Hai Dang's avatar
Hai Dang committed
333 334 335 336 337 338 339 340 341 342 343 344 345

Lemma public_access_not_local r_f r l t n kind σs σt:
  let access :=
    for_each σt.(sst) l n false (λ stk, nstk'  access1 stk kind (Tagged t) σt.(scs); Some nstk'.2) in
  wsat (r_f  r) σs σt 
  is_Some access 
  ( (h: heapletR), r.(rtm) !! t  Some (to_tagKindR tkPub, h)) 
   i, (i < n)%nat  r.(rlm) !! (l + i)  Some $ to_locStateR lsShared.
Proof.
  intros access WSAT [α' IS] [h Eqt] i Lt.

Abort.

346 347 348 349 350 351 352 353 354 355
Lemma sim_body_copy_public fs ft r n l t Ts Tt σs σt Φ
  (EQS: tsize Ts = tsize Tt)
  (PUBLIC:  (h: heapletR), r.(rtm) !! t  Some (to_tagKindR tkPub, h))
  (SHR:  i, (i < tsize Tt)%nat  r.(rlm) !! (l + i)  Some $ to_locStateR lsShared) :
  ( vs vt r',
    read_mem l (tsize Ts) σs.(shp) = Some vs 
    read_mem l (tsize Tt) σt.(shp) = Some vt 
     α', memory_read σt.(sst) σt.(scs) l (Tagged t) (tsize Tt) = Some α' 
      let σs' := mkState σs.(shp) α' σs.(scs) σs.(snp) σs.(snc) in
      let σt' := mkState σt.(shp) α' σt.(scs) σt.(snp) σt.(snc) in
Ralf Jung's avatar
Ralf Jung committed
356
      vrel (r  r') vs vt  Φ (r  r') n (ValR vs) σs' (ValR vt) σt') 
357 358 359 360 361 362 363 364 365
  r {n,fs,ft} (Copy (Place l (Tagged t) Ts), σs)  (Copy (Place l (Tagged t) Tt), σt) : Φ.
Proof.
  intros POST. pfold.
  intros NT. intros.
  destruct WSAT as (WFS & WFT & VALID & PINV & CINV & SREL & LINV).
  split; [|done|].
  { right.
    destruct (NT (Copy (Place l (Tagged t) Ts)) σs) as [[]|[es' [σs' STEPS]]];
      [done..|].
Hai Dang's avatar
Hai Dang committed
366 367
    destruct (tstep_copy_inv _ (PlaceR l (Tagged t) Ts) _ _ _ STEPS)
      as (?&?&?& vs & α' & EqH & ? & Eqvs & Eqα' & ?). symmetry in EqH. simplify_eq.
368 369 370 371 372 373 374 375
    destruct (read_mem_is_Some l (tsize Tt) σt.(shp)) as [vt Eqvt].
    { intros m. rewrite (srel_heap_dom _ _ _ WFS WFT SREL) -EQS.
      apply (read_mem_is_Some' l (tsize Ts) σs.(shp)). by eexists. }
    have Eqα'2: memory_read σt.(sst) σt.(scs) l (Tagged t)  (tsize Tt) = Some α'.
    { destruct SREL as (Eqst&?&Eqcs&?). by rewrite -Eqst -Eqcs -EQS. }
    exists (#vt)%E, (mkState σt.(shp) α' σt.(scs) σt.(snp) σt.(snc)).
    by eapply (head_step_fill_tstep _ []), copy_head_step'. }
  constructor 1. intros.
Hai Dang's avatar
Hai Dang committed
376 377
  destruct (tstep_copy_inv _ (PlaceR l (Tagged t) Tt) _ _ _ STEPT)
    as (?&?&?& vt & α' & EqH & ? & Eqvt & Eqα' & ?). symmetry in EqH. simplify_eq.
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
  destruct (read_mem_is_Some l (tsize Ts) σs.(shp)) as [vs Eqvs].
  { intros m. rewrite -(srel_heap_dom _ _ _ WFS WFT SREL) EQS.
    apply (read_mem_is_Some' l (tsize Tt) σt.(shp)). by eexists. }
  have Eqα'2: memory_read σs.(sst) σs.(scs) l (Tagged t)  (tsize Ts) = Some α'.
  { destruct SREL as (Eqst&?&Eqcs&?). by rewrite Eqst Eqcs EQS. }
  set σs' := mkState σs.(shp) α' σs.(scs) σs.(snp) σs.(snc).
  have STEPS: (Copy (Place l (Tagged t)  Ts), σs) ~{fs}~> ((#vs)%E, σs').
  { by eapply (head_step_fill_tstep _ []), copy_head_step'. }
  have CORE : (r_f  r)  r_f  r  core (r_f  r) by rewrite cmra_core_r.
  assert (VREL': vrel (core (r_f  r)) vs vt).
  { destruct PUBLIC as [h PUB].
    destruct (tmap_lookup_op_r_equiv_pub r_f.(rtm) r.(rtm) t h) as [h0 Eqh0];
      [apply VALID|done|].
    destruct (PINV _ _ _ Eqh0) as [Lt PB].
    destruct SREL as (Eqst & Eqnp & Eqcs & Eqnc & PRIV).
    destruct (read_mem_values _ _ _ _ Eqvs) as [Eqls HLs].
    destruct (read_mem_values _ _ _ _ Eqvt) as [Eqlt HLt].
    apply Forall2_same_length_lookup. split; [by rewrite Eqls Eqlt|].
    intros i ss st Hss Hst.
    have HLLs := lookup_lt_Some _ _ _ Hss. have HLLt := lookup_lt_Some _ _ _ Hst.
    rewrite -Eqls in HLs. specialize (HLs _ HLLs). rewrite Hss in HLs.
    rewrite -Eqlt in HLt. specialize (HLt _ HLLt). rewrite Hst in HLt.
    rewrite -Eqlt in SHR.
    have SHR' := lmap_lookup_op_r _ _ (proj2 VALID) _ _ (SHR _ HLLt).
    specialize (PRIV _ (elem_of_dom_2 _ _ _ HLt) SHR')
      as [PUBl|[t' PV]].
    { destruct (PUBl _ HLt) as [ss' [Eqss' AREL]].
      rewrite HLs in Eqss'. symmetry in Eqss'. simplify_eq. move: AREL.
      destruct ss as [| |l1 [t1|]|], st as [| |l2 [t2|]|]; auto; simpl; [|by intros [?[]]].
      intros [? [? [h' Eqh']]]. simplify_eq. do 2 (split; [done|]).
      exists h'. by apply tmap_lookup_core_pub. }
    destruct PV as (c' & T' & h' & Eqci & InT' & Eqh' & Inl).
     (* impossible: t' is protected by c' which is still active.
      So t  t' and protected(t',c') is on top of (l + i), so access with t is UB *)
    exfalso.
    have NEQ: t'  t.
    { intros ?. subst t'.
      apply (tmap_lookup_op_r_equiv_pub r_f.(rtm)) in PUB as [? PUB];
        [|by apply VALID].
      rewrite -> PUB in Eqh'. apply Some_equiv_inj in Eqh' as [Eqk' ?].
      inversion Eqk'. }
    specialize (CINV _ _ Eqci) as [Inc' CINV].
    specialize (CINV _ InT') as [Ltt' CINV].
    specialize (CINV _ _ Eqh' _ Inl) as (stk & pm & Eqstk & Instk & NDIS).
    specialize (PINV _ _ _ Eqh') as [_ PINV].
    have VALh' :  h'.
    { apply (Some_valid (to_tagKindR tkUnique,h')).
      rewrite -Some_valid -Eqh'. apply VALID. }
    destruct (heapletR_elem_of_dom _ _ VALh' Inl) as [s Eqs].
    specialize (PINV _ _ Eqs) as [_ PINV].
    specialize (PINV _ Eqstk _ _ Instk NDIS) as (Eqss & HD).
    destruct (for_each_lookup _ _ _ _ _ Eqα') as [EQ1 _].
    rewrite Eqlt in HLLt.
    specialize (EQ1 _ _ HLLt Eqstk) as (stk' & Eqstk' & EQ2).
    move : EQ2. case access1 as [[n1 stk1]|] eqn:EQ3; [|done].
    simpl. inversion 1. subst stk1.
    have ND := proj2 (state_wf_stack_item _ WFT _ _ Eqstk).
    admit.
  }
  exists (Val vs), σs', (r  (core (r_f  r))), n. split; last split.
  { left. by constructor 1. }
  { rewrite cmra_assoc -CORE.
    split; last split; last split; last split; last split; last split.
    - by apply (tstep_wf _ _ _ STEPS WFS).
    - by apply (tstep_wf _ _ _ STEPT WFT).
    - done.
Hai Dang's avatar
Hai Dang committed
444
    - intros t1 k h Eqt. specialize (PINV t1 k h Eqt) as [Lt PI]. simpl.
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
      split; [done|]. intros l' s' Eqk'.
      specialize (PI _ _ Eqk') as [? PI]. split; [done|].
      intros stk' Eqstk'.
      destruct (for_each_access1 _ _ _ _ _ _ _ Eqα' _ _ Eqstk')
        as (stk & Eqstk & SUB & ?).
      intros pm opro In' NDIS.
      destruct (SUB _ In') as (it2 & In2 & Eqt2 & Eqp2 & NDIS2).
      specialize (PI _ Eqstk it2.(perm) opro) as [Eql' HTOP].
      { simpl in *. rewrite /= Eqt2 Eqp2. by destruct it2. }
      { simpl in *. by rewrite (NDIS2 NDIS). }
      split; [done|].
      destruct (for_each_lookup_case _ _ _ _ _ Eqα' _ _ _ Eqstk Eqstk')
        as [?|[MR _]]; [by subst|]. clear -In' MR HTOP Eqstk WFT NDIS.
      destruct (access1 stk AccessRead (Tagged t) σt.(scs)) as [[n stk1]|] eqn:Eqstk';
        [|done]. simpl in MR. simplify_eq.
      destruct (state_wf_stack_item _ WFT _ _ Eqstk). move : Eqstk' HTOP.
      destruct k.
      + eapply access1_head_preserving; eauto.
      + eapply access1_active_SRO_preserving; eauto.
Hai Dang's avatar
Hai Dang committed
464
    - intros c cs Eqc. specialize (CINV _ _ Eqc). simpl.
465 466 467 468 469 470 471 472
      clear -Eqα' CINV. destruct cs as [[T|]| |]; [|done..].
      destruct CINV as [IN CINV]. split; [done|].
      intros t1 InT. specialize (CINV _ InT) as [? CINV]. split; [done|].
      intros k h Eqt l' Inh.
      destruct (CINV _ _ Eqt _ Inh) as (stk' & pm' & Eqstk' & Instk' & NDIS).
      destruct (for_each_access1_active_preserving _ _ _ _ _ _ _ Eqα' _ _ Eqstk')
        as [stk [Eqstk AS]].
      exists stk, pm'. split; last split; [done| |done]. by apply AS.
Hai Dang's avatar
Hai Dang committed
473 474
    - rewrite /srel /=. by destruct SREL as (?&?&?&?&?).
    - intros l1. simpl. intros Inl1.
475 476 477 478 479 480 481
      specialize (LINV _ Inl1) as [InD1 LINV]. split; [done|].
      intros s stk Eqs.
      have HLF :  i, (i < tsize Tt)%nat  l1  (l + i).
      { intros i Lti Eq. subst l1.
        move : Eqs. rewrite lookup_op (SHR _ Lti). apply lmap_exclusive_r. }
      destruct (for_each_lookup _ _ _ _ _ Eqα') as [_ [_ EQt]].
      rewrite (EQt _ HLF).
Hai Dang's avatar
Hai Dang committed
482
      by specialize (LINV _ _ Eqs) as (?&?&Eqa1&Eqas&?). }
483
  left.
Ralf Jung's avatar
Ralf Jung committed
484
  apply: sim_body_result. intros.
485 486
  have VREL2: vrel (r  (core (r_f  r))) vs vt.
  { eapply vrel_mono; [done| |exact VREL']. apply cmra_included_r. }
Hai Dang's avatar
Hai Dang committed
487
  apply POST; eauto.
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
Admitted.

(** Write *)
Fixpoint write_heaplet (l: loc) (v: value) (h: gmapR loc (agreeR scalarC)) :=
  match v with
  | [] => h
  | s :: v =>
      write_heaplet (l + 1) v (if h !! l then <[l := to_agree s]> h else h)
  end.

(* TODO: move *)
Instance insert_gmap_proper `{Countable K} {A : cmraT} (i: K) :
  Proper (() ==> () ==> ()) (insert (M:=gmap K A) i).
Proof.
  intros m1 m2 Eq a1 a2 Eqa i'. case (decide (i = i')) => [->|?].
  - by rewrite 2!lookup_insert Eq.
  - do 2 (rewrite lookup_insert_ne //).
Qed.

Instance write_heaplet_proper (l: loc) (v: value) :
  Proper (() ==> ()) (write_heaplet l v).
Proof.
  intros h1 h2 Eq. revert l h1 h2 Eq.
  induction v as [|s v IH]; intros l h1 h2 Eq; simpl; [done|].
  apply IH. move : (Eq l).
  destruct (h1 !! l) as [s1|] eqn:Eq1, (h2 !! l) as [s2|] eqn:Eq2; [..|done];
    rewrite Eq1 Eq2; intros Eql; [by rewrite Eq|by inversion Eql..].
Qed.

Lemma write_heaplet_dom (l: loc) (v: value) h :
  dom (gset loc) (write_heaplet l v h)  dom (gset loc) h.
Proof.
  revert l h.
  induction v as [|s v IH]; intros l h; simpl; [done|].
  rewrite IH. destruct (h !! l) eqn:Eq; [|done].
  rewrite dom_map_insert_is_Some //. by eexists.
Qed.

Lemma write_heaplet_empty l v : write_heaplet l v   .
Proof. revert l. induction v as [|?? IH]; [done|]; intros l. apply IH. Qed.

Lemma write_heaplet_valid l v h:
   h   (write_heaplet l v h).
Proof.
  revert l h. induction v as [|s v IH]; intros l h VALID; simpl; [done|].
  apply IH. destruct (h !! l) eqn:Eql; [|done]. by apply insert_valid.
Qed.

Lemma write_heaplet_lookup (l: loc) (vl: value) (h: heapletR) :
  ( (i: nat) s, (i < length vl)%nat 
    write_heaplet l vl h !! (l + i)  Some s 
    Some s  to_agree <$> vl !! i) 
  ( (l': loc), ( (i: nat), (i < length vl)%nat  l'  l + i) 
    write_heaplet l vl h !! l'  h !! l').
Proof.
  revert l h. induction vl as [|v vl IH]; move => l h; simpl;
    [split; [intros; by lia|done]|].
  destruct (IH (l + 1) (if h !! l then <[l:=to_agree v]> h else h)) as [IH1 IH2].
  split.
  - intros i s Lt. destruct i as [|i].
    + rewrite shift_loc_0_nat /=. rewrite IH2; [|].
      * destruct (h !! l) eqn:Eql; [by rewrite lookup_insert|].
        rewrite Eql; by inversion 1.
      * move => ? _.
        rewrite shift_loc_assoc -{1}(shift_loc_0 l) => /shift_loc_inj ?. by lia.
    + intros Eq.  rewrite /= -IH1; [eauto|lia|].
      by rewrite shift_loc_assoc -(Nat2Z.inj_add 1).
  - intros l' Lt. rewrite IH2.
    + destruct (h !! l) eqn:Eql; [|done].
      rewrite lookup_insert_ne; [done|].
      move => ?. subst l'. apply (Lt O); [lia|by rewrite shift_loc_0_nat].
    + move => i Lti. rewrite shift_loc_assoc -(Nat2Z.inj_add 1).
      apply Lt. by lia.
Qed.

Hai Dang's avatar
Hai Dang committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
Lemma res_mapsto_1_insert_local_update (r: resUR) l v1 v2 t
  (NONE: r.(rlm) !! l = None):
  (r  res_mapsto l 1 v1 t, res_mapsto l 1 v1 t) ~l~>
  (r  res_mapsto l 1 v2 t, res_mapsto l 1 v2 t).
Proof.
  intros. destruct r as [[tm cm] lm]. rewrite 4!pair_op 2!right_id.
  apply prod_local_update_2.
  rewrite cmra_comm (cmra_comm _ (init_local_res l 1 v2 t )).
  rewrite /= /init_local_res /= 2!fmap_insert /= fmap_empty.
  do 2 rewrite -insert_singleton_op //.
  rewrite -(insert_insert lm l (Cinl (Excl (v2, t))) (Cinl (Excl (v1, t)))).
  eapply (singleton_local_update (<[l:=Cinl (Excl (v1, t))]> lm : lmapUR)).
  { by rewrite lookup_insert. }
Abort.

578 579
Lemma sim_body_write_local_1 fs ft r r' n l tg T v v' σs σt Φ :
  tsize T = 1%nat 
Hai Dang's avatar
Hai Dang committed
580
  r  r'  res_mapsto l 1 v' tg 
581 582 583
  ( s, v = [s] 
    let σs' := mkState (<[l := s]> σs.(shp)) σs.(sst) σs.(scs) σs.(snp) σs.(snc) in
    let σt' := mkState (<[l := s]> σt.(shp)) σt.(sst) σt.(scs) σt.(snp) σt.(snc) in
Hai Dang's avatar
Hai Dang committed
584
    Φ (r'  res_mapsto l 1 s tg) n
Ralf Jung's avatar
Ralf Jung committed
585
      (ValR [%S]) σs' (ValR [%S]) σt') 
586 587 588 589 590 591 592 593 594
  r {n,fs,ft}
    (Place l (Tagged tg) T <- #v, σs)  (Place l (Tagged tg) T <- #v, σt) : Φ.
Proof.
  intros LenT Eqr POST. pfold.
  intros NT. intros.
  destruct WSAT as (WFS & WFT & VALID & PINV & CINV & SREL & LINV).
  split; [|done|].
  { right.
    edestruct NT as [[]|[es' [σs' STEPS]]]; [constructor 1|done|].
Hai Dang's avatar
Hai Dang committed
595 596 597 598
    destruct (tstep_write_inv _ (PlaceR _ _ _) (ValR _) _ _ _ STEPS)
      as (?&?&?&?& α' & EqH & EqH' & ? & Eqα' & EqD & IN & EQL & ?).
    symmetry in EqH, EqH'. simplify_eq.
    setoid_rewrite <-(srel_heap_dom _ _ _ WFS WFT SREL) in EqD.
599 600 601 602 603 604
    destruct SREL as (Eqst&Eqnp&Eqcs&Eqnc&AREL).
    rewrite Eqst Eqcs in Eqα'. rewrite Eqnp in IN. rewrite EQL in EqD.
    exists (#[])%V,
           (mkState (write_mem l v σt.(shp)) α' σt.(scs) σt.(snp) σt.(snc)).
    eapply (head_step_fill_tstep _ []), write_head_step'; eauto. }
  constructor 1. intros.
Hai Dang's avatar
Hai Dang committed
605 606 607
  destruct (tstep_write_inv _ (PlaceR _ _ _) (ValR _) _ _ _ STEPT)
      as (?&?&?&?& α' & EqH & EqH' & ? & Eqα' & EqD & IN & EQL & ?).
  symmetry in EqH, EqH'. simplify_eq.
608 609 610 611 612
  assert ( s, v = [s]) as [s ?].
  { rewrite LenT in EQL. destruct v as [|s v]; [simpl in EQL; done|].
    exists s. destruct v; [done|simpl in EQL; lia]. } subst v.

  have VALIDr:= cmra_valid_op_r _ _ VALID.
Hai Dang's avatar
Hai Dang committed
613
  have HLlr: r.(rlm) !! l  Some (to_locStateR (lsLocal v' tg)).
614 615 616 617 618 619
  { rewrite Eqr. apply lmap_lookup_op_r.
    - rewrite ->Eqr in VALIDr. apply VALIDr.
    - by rewrite /= /init_local_res lookup_fmap /= lookup_insert /=. }
  destruct (LINV l) as [Inl Eql].
  { rewrite dom_op elem_of_union. right. rewrite elem_of_dom.
    destruct (r.(rlm) !! l) eqn:Eql2; rewrite Eql2; [by eexists|inversion HLlr]. }
Hai Dang's avatar
Hai Dang committed
620
  destruct (Eql v' tg) as (Eql1 & Eql2 & Eqsl1 & Eqsl2 & LocUnique).
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
  { apply lmap_lookup_op_r; [apply VALID|done]. } clear Eql.
  have ?: α' = σt.(sst).
  { move : Eqα'. rewrite LenT /= /memory_written /= shift_loc_0_nat.
    rewrite Eqsl2 /=.
    destruct (tag_unique_head_access σt.(scs) (init_stack (Tagged tg))
                tg None AccessWrite) as [ns Eqss]; [by exists []|].
    rewrite Eqss /= insert_id //. by inversion 1. } subst α'.

  set σs' := mkState (<[l := s]> σs.(shp)) σs.(sst) σs.(scs) σs.(snp) σs.(snc).
  have STEPS: ((Place l (Tagged tg) T <- #[s])%E, σs) ~{fs}~> ((#[])%V, σs').
  { setoid_rewrite (srel_heap_dom _ _ _ WFS WFT SREL) in EqD.
    destruct SREL as (Eqst&Eqnp&Eqcs&Eqnc&AREL).
    rewrite -Eqst -Eqcs in Eqα'.
    rewrite EQL in EqD. rewrite -Eqnp in IN.
    eapply (head_step_fill_tstep _ []), write_head_step'; eauto. }

Hai Dang's avatar
Hai Dang committed
637
  exists (#[])%V, σs', (r'  res_mapsto l 1 s tg), n.
638 639
  split; last split.
  { left. by constructor 1. }
Hai Dang's avatar
Hai Dang committed
640
  { have HLlrf: (r_f  r) .(rlm) !! l  Some (to_locStateR (lsLocal v' tg)).
641 642 643 644 645 646 647
    { apply lmap_lookup_op_r; [apply VALID|done]. }
    have HLN: (r_f  r').(rlm) !! l = None.
    { destruct ((r_f  r').(rlm) !! l) as [ls|] eqn:Eqls; [|done].
      exfalso. move : HLlrf.
      rewrite Eqr cmra_assoc lookup_op Eqls /=
              /init_local_res lookup_fmap /= lookup_insert.
      apply lmap_exclusive_2. }
Hai Dang's avatar
Hai Dang committed
648 649
    have HTEq: (r_f  r'  res_mapsto l 1 v' tg).(rtm) 
               (r_f  r'  res_mapsto l 1 s tg).(rtm).
650
    { rewrite /rtm /= right_id //. }
Hai Dang's avatar
Hai Dang committed
651 652
    have HCEq: (r_f  r'  res_mapsto l 1 v' tg).(rcm) 
               (r_f  r'  res_mapsto l 1 s tg).(rcm).
653 654 655 656 657
    { rewrite /rcm /= right_id //. }
    rewrite cmra_assoc.
    split; last split; last split; last split; last split; last split.
    - by apply (tstep_wf _ _ _ STEPS WFS).
    - by apply (tstep_wf _ _ _ STEPT WFT).
Hai Dang's avatar
Hai Dang committed
658
    - move : VALID. rewrite Eqr cmra_assoc => VALID.
659
      apply (local_update_discrete_valid_frame _ _ _ VALID).
Hai Dang's avatar
Hai Dang committed
660
      by eapply res_mapsto_1_insert_local_update.
Hai Dang's avatar
Hai Dang committed
661
    - intros t k h HL. destruct (PINV t k h) as [? PI].
662 663 664 665 666 667 668 669 670 671
      { rewrite Eqr. move: HL. by rewrite 4!lookup_op /= 2!right_id. }
      split; [done|]. simpl.
      intros l1 s1 Eqs1. specialize (PI l1 s1 Eqs1) as [HLs1 PI].
      have NEql1: l1  l.
      { intros ?. subst l1. move : HLs1. rewrite HLlrf.
        intros Eq%Some_equiv_inj. inversion Eq. } split.
      { move : HLs1. rewrite Eqr cmra_assoc /=.
        rewrite lookup_op (lookup_op (r_f.2  r'.2)) /init_local_res /= 2!lookup_fmap.
        do 2 rewrite lookup_insert_ne //. }
      by setoid_rewrite lookup_insert_ne.
Hai Dang's avatar
Hai Dang committed
672
    - intros c cs. simpl. rewrite -HCEq. intros Eqcm.
673 674 675
      move : CINV. rewrite Eqr cmra_assoc => CINV.
      specialize (CINV _ _ Eqcm). destruct cs as [[]| |]; [|done..].
      destruct CINV as [? CINV]. split; [done|]. by setoid_rewrite <- HTEq.
Hai Dang's avatar
Hai Dang committed
676
    - destruct SREL as (?&?&?&?& REL). do 4 (split; [done|]).
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
      simpl. intros l1 Inl1 Eq1.
      have NEql1: l1  l.
      { intros ?. subst l1. move : Eq1. rewrite lookup_op HLN left_id.
        rewrite /init_local_res lookup_fmap /= lookup_insert.
        intros Eq%Some_equiv_inj. inversion Eq. }
      move : Inl1. rewrite dom_insert elem_of_union elem_of_singleton.
      intros [?|Inl1]; [done|].
      have Eq1' : (r_f  r).(rlm) !! l1  Some (Cinr ()).
      { rewrite Eqr cmra_assoc -Eq1 lookup_op (lookup_op (r_f.2  r'.2)).
        rewrite /init_local_res /= 2!lookup_fmap lookup_insert_ne // lookup_insert_ne //. }
      specialize (REL _ Inl1 Eq1') as [REL|REL].
      + left. move : REL. rewrite /pub_loc /=.
        do 2 rewrite lookup_insert_ne //. intros REL st Eqst.
        specialize (REL st Eqst) as [ss [Eqss AREL]].
        exists ss. split; [done|]. move : AREL. rewrite /arel /=.
        destruct ss as [| |? [] |], st as [| |? []|]; try done; [|naive_solver].
        setoid_rewrite Eqr. setoid_rewrite cmra_assoc. by setoid_rewrite <- HTEq.
      + right. move : REL. setoid_rewrite Eqr. setoid_rewrite cmra_assoc.
        rewrite /priv_loc. by setoid_rewrite <- HTEq.
Hai Dang's avatar
Hai Dang committed
696
    - move : LINV. rewrite Eqr cmra_assoc.
697 698
      (* TODO: general property of lmap_inv w.r.t to separable resource *)
      intros LINV l1 Inl1.
Hai Dang's avatar
Hai Dang committed
699 700
      have EqD': dom (gset loc) (r_f  r'  res_mapsto l 1 s tg).(rlm)
           dom (gset loc) (r_f  r'  res_mapsto l 1 v' tg).(rlm).
701
      { rewrite dom_op /= (dom_op (r_f.2  r'.2)).
Hai Dang's avatar
Hai Dang committed
702 703
        rewrite (_: dom (gset loc) (init_local_res l 1 s tg )
                   dom (gset loc) (init_local_res l 1 v' tg )) //.
704 705 706 707
        rewrite /init_local_res /= 2!fmap_insert /= fmap_empty 2!insert_empty.
        rewrite 2!dom_singleton //. }
      rewrite -> EqD' in Inl1. specialize (LINV _ Inl1) as [Inh LINV].
      split. { by apply dom_insert_subseteq. }
Hai Dang's avatar
Hai Dang committed
708
      intros s1 t1. destruct ((r_f  r').(rlm) !! l1) as [ls1|] eqn:Eqls1.
709 710
      + have NEQ: l1  l. { intros ?. subst l1. by rewrite Eqls1 in HLN. }
        intros EQl1.
Hai Dang's avatar
Hai Dang committed
711 712
        have EQl1' : (r_f  r'  res_mapsto l 1 v' tg).(rlm) !! l1
             Some (to_locStateR (lsLocal s1 t1)).
713 714 715 716 717 718 719
        { move : EQl1. rewrite lookup_op Eqls1 lookup_op Eqls1.
          rewrite /= /init_local_res 2!lookup_fmap /= lookup_insert_ne // lookup_insert_ne //. }
        specialize (LINV _ _ EQl1').
        rewrite /= lookup_insert_ne // lookup_insert_ne //.
      + rewrite /= lookup_op Eqls1 left_id /init_local_res /= lookup_fmap.
        intros Eq. case (decide (l1 = l)) => ?; [subst l1|].
        * move : Eq. rewrite 3!lookup_insert /=.
Hai Dang's avatar
Hai Dang committed
720 721 722
          intros Eq%Some_equiv_inj. simplify_eq. do 4 (split; [done|]).
          destruct LocUnique as [h1 Eqh1]. exists h1.
          by rewrite -HTEq -cmra_assoc -Eqr Eqh1.
723 724 725
        * exfalso. move : Eq. rewrite lookup_insert_ne // lookup_empty /=.
          by inversion 1. }
  left.
Ralf Jung's avatar
Ralf Jung committed
726
  apply: sim_body_result.
Hai Dang's avatar
Hai Dang committed
727
  intros. simpl. by apply POST.
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
Qed.

Lemma sim_body_write_related_values
  fs ft (r: resUR) k0 h0 n l tg Ts Tt v σs σt Φ
  (EQS: tsize Ts = tsize Tt)
  (Eqtg: r.(rtm) !! tg = Some (to_tagKindR k0, h0))
  (SHR:  i, (i < tsize Tt)%nat  r.(rlm) !! (l + i)  Some $ to_locStateR lsShared)
  (* assuming to-write values are related *)
  (PUBWRITE:  s, s  v  arel r s s) :
  let r' := if k0 then
            ((<[tg := (to_tagKindR tkUnique,  write_heaplet l v h0)]> r.(rtm),
              r.(rcm)), r.(rlm))
            else r in
  ( α', memory_written σt.(sst) σt.(scs) l (Tagged tg) (tsize Tt) = Some α' 
    let σs' := mkState (write_mem l v σs.(shp)) α' σs.(scs) σs.(snp) σs.(snc) in
    let σt' := mkState (write_mem l v σt.(shp)) α' σt.(scs) σt.(snp) σt.(snc) in
Ralf Jung's avatar
Ralf Jung committed
744
    Φ r' n (ValR []%S) σs' (ValR []%S) σt' : Prop) 
745 746 747 748 749 750 751 752 753
  r {n,fs,ft}
    (Place l (Tagged tg) Ts <- #v, σs)  (Place l (Tagged tg) Tt <- #v, σt) : Φ.
Proof.
  intros r' POST. pfold.
  intros NT. intros.
  destruct WSAT as (WFS & WFT & VALID & PINV & CINV & SREL & LINV).
  split; [|done|].
  { right.
    edestruct NT as [[]|[es' [σs' STEPS]]]; [constructor 1|done|].
Hai Dang's avatar
Hai Dang committed
754 755 756 757
    destruct (tstep_write_inv _ (PlaceR _ _ _) (ValR _) _ _ _ STEPS)
      as (?&?&?&?& α' & EqH & EqH' & ? & Eqα' & EqD & IN & EQL & ?).
    symmetry in EqH, EqH'. simplify_eq.
    setoid_rewrite <-(srel_heap_dom _ _ _ WFS WFT SREL) in EqD.
758 759 760 761 762 763 764
    destruct SREL as (Eqst&Eqnp&Eqcs&Eqnc&AREL).
    rewrite Eqst Eqcs EQS in Eqα'. rewrite -EQL in EQS.
    rewrite EQS in EqD. rewrite Eqnp in IN.
    exists (#[])%V,
           (mkState (write_mem l v σt.(shp)) α' σt.(scs) σt.(snp) σt.(snc)).
    by eapply (head_step_fill_tstep _ []), write_head_step'. }
  constructor 1. intros.
Hai Dang's avatar
Hai Dang committed
765 766 767
  destruct (tstep_write_inv _ (PlaceR _ _ _) (ValR _) _ _ _ STEPT)
      as (?&?&?&?& α' & EqH & EqH' & ? & Eqα' & EqD & IN & EQL & ?).
  symmetry in EqH, EqH'. simplify_eq.
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
  set σs' := mkState (write_mem l v σs.(shp)) α' σs.(scs) σs.(snp) σs.(snc).
  have STEPS: ((Place l (Tagged tg) Ts <- v)%E, σs) ~{fs}~> ((#[])%V, σs').
  { setoid_rewrite (srel_heap_dom _ _ _ WFS WFT SREL) in EqD.
    destruct SREL as (Eqst&Eqnp&Eqcs&Eqnc&AREL).
    rewrite -Eqst -Eqcs -EQS in Eqα'. rewrite -EQS in EQL.
    rewrite EQL in EqD. rewrite -Eqnp in IN.
    eapply (head_step_fill_tstep _ []), write_head_step'; eauto. }
  have HL: if k0 then  kh, r_f.(rtm)  <[tg:=kh]> r.(rtm) = <[tg:=kh]> (r_f.(rtm)  r.(rtm)) else True.
  { destruct k0; [|done]. intros.
    rewrite (tmap_insert_op_r r_f.(rtm) r.(rtm) tg h0) //. apply VALID. }
  have HL2: if k0 then  (r_f.(rtm)  r.(rtm)) !! tg = Some (to_tagKindR tkUnique, h0) else True.
  { destruct k0; [|done].
    by apply (tmap_lookup_op_r _ _ _ _ (proj1 (proj1 VALID)) Eqtg). }
  exists (#[])%V, σs', r', n. split; last split.
  { left. by constructor 1. }
  { have Eqrlm: (r_f  r').(rlm)  (r_f  r).(rlm) by destruct k0.
    destruct (for_each_lookup _ _ _ _ _ Eqα') as [EQ1 EQ2].
    split; last split; last split; last split; last split; last split.
    - by apply (tstep_wf _ _ _ STEPS WFS).
    - by apply (tstep_wf _ _ _ STEPT WFT).
    - (* valid *)
      rewrite /r'. destruct k0; [|done]. do 2 (split; [|apply VALID]).
      eapply tmap_valid; eauto; [|apply VALID]. split; [done|].
      apply write_heaplet_valid.
      have VL := (proj1 (proj1 (cmra_valid_op_r _ _ VALID)) tg).
      rewrite Eqtg in VL. apply VL.
    - (* tmap_inv *)
      intros t k h Eqt.
      have Eqttg: t = tg  k0 = tkUnique  k = k0  h  write_heaplet l v h0.
      { intros. subst t k0. move  : Eqt. rewrite /rtm /= HL lookup_insert.
        intros [Eq1 Eq2]%Some_equiv_inj.
        simpl in Eq1, Eq2. rewrite Eq2. repeat split; [|done].
        destruct k; [done|inversion Eq1]. }
      have CASEt : (t = tg  k0 = tkUnique  k = k0  h  write_heaplet l v h0 
                    (r_f  r).(rtm) !! t  Some (to_tagKindR k, h) 
                      (k = tkUnique  t  tg)).
      { move : Eqt. rewrite /r'.
        destruct k0; simpl.
        - rewrite /rtm /= HL.
          case (decide (t = tg)) => ?; [subst t|rewrite lookup_insert_ne //].
          + left. by destruct Eqttg.
          + right. naive_solver.
        - case (decide (t = tg)) => ?; [subst t|].
          + intros Eqt. right. split; [done|]. intros ?. subst k.
            move : Eqt. rewrite lookup_op Eqtg. by move => /tagKindR_exclusive_2.
          + right. naive_solver. }
      split.
Hai Dang's avatar
Hai Dang committed
815
      { simpl. destruct CASEt as [(?&?&?&?Eqh)|[Eqh NEQ]].
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
        - subst t k k0. apply (PINV tg tkUnique h0). by rewrite HL2.
        - move : Eqh. apply PINV. }
      intros l' s' Eqk'. split.
      { destruct CASEt as [(?&?&?&?Eqh)|[Eqh NEQ]].
        - subst t k k0. destruct (PINV tg tkUnique h0) as [? PI]; [by rewrite HL2|].
          have InD': l'  dom (gset loc) h.
          { rewrite elem_of_dom.
            destruct (h !! l') eqn:Eql'; rewrite Eql'; [by eexists|by inversion Eqk']. }
          move : InD'. rewrite Eqh write_heaplet_dom elem_of_dom.
          intros [s0 Eqs0].
          have VALS := proj1 (proj1 (cmra_valid_op_r _ _ VALID)) tg.
          rewrite Eqtg in VALS.
          have VALs0:  s0. { change ( (Some s0)). rewrite -Eqs0. apply VALS. }
          apply to_agree_uninj in VALs0 as [ss0 Eqss0].
          destruct (PI l' ss0) as [? _]; [|done]. by rewrite Eqs0 Eqss0.
        - specialize (PINV _ _ _ Eqh) as [? PINV].
          specialize (PINV _ _ Eqk') as [EQ _]. rewrite /r' /=. by destruct k0. }
Hai Dang's avatar
Hai Dang committed
833
      intros stk'. simpl.
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
      destruct (write_mem_lookup_case l v σt.(shp) l')
          as [[i [Lti [Eqi Eqmi]]]|[NEql Eql]]; last first.
      { (* l' is NOT written to *)
        destruct (for_each_lookup _ _ _ _ _ Eqα') as [_ [_ EQ]].
        rewrite EQL in NEql. rewrite (EQ _ NEql) Eql.
        destruct CASEt as [(?&?&?&?Eqh)|[Eqh ?]]; [|by apply (PINV t k h Eqh)].
        subst t k k0. apply (PINV tg tkUnique h0).
        - by rewrite HL2.
        - move : Eqk'. rewrite Eqh. rewrite -EQL in NEql.
          by rewrite (proj2 (write_heaplet_lookup l v h0) _ NEql). }
      (* l' is written to *)
      intros Eqstk' pm opro In' NDIS. subst l'.
      destruct (for_each_access1 _ _ _ _ _ _ _ Eqα' _ _ Eqstk')
        as (stk & Eqstk & SUB & ?).
      destruct (SUB _ In') as (it2 & In2 & Eqt2 & Eqp2 & NDIS2). simpl in *.
      destruct CASEt as [(?&?&?&?Eqh)|[Eqh NEQ]].
      + (* t = tg *)
        subst t k k0. rewrite -> Eqh in Eqk'. split.
        * have Eqs' := proj1 (write_heaplet_lookup l v h0) _ _ Lti Eqk'.
          rewrite (proj1 (write_mem_lookup l v σt.(shp)) _ Lti).
          destruct (v !! i) as [s''|] eqn: Eq''; [rewrite Eq''|by inversion Eqs'].
          apply Some_equiv_inj, to_agree_inj in Eqs'. by inversion Eqs'.
        * assert ( s0: scalar, h0 !! (l + i)  Some (to_agree s0)) as [s0 Eq0].
          { assert (is_Some (h0 !! (l + i))) as [s0 Eqs0].
            { rewrite -(elem_of_dom (D:=gset loc)) -(write_heaplet_dom l v h0).
              move : Eqk'.
              destruct (write_heaplet l v h0 !! (l + i)) eqn: Eq'';
                rewrite Eq''; [intros _|by inversion 1].
              apply (elem_of_dom_2 _ _ _ Eq''). }
            rewrite Eqs0.
            destruct (to_agree_uninj s0) as [s1 Eq1]; [|by exists s1; rewrite -Eq1].
            apply (lookup_valid_Some h0 (l + i)); [|by rewrite Eqs0].
            apply (lookup_valid_Some (r_f.(rtm)  r.(rtm)) tg (to_tagKindR tkUnique, h0));
              [by apply (proj1 VALID)|by rewrite HL2]. }
          specialize (PINV tg tkUnique h0) as [Lt PI]; [by rewrite HL2|].
          specialize (PI _ _ Eq0) as [? PI].
          specialize (PI _ Eqstk it2.(perm) opro) as [Eql' HTOP].
          { rewrite /= Eqt2 Eqp2. by destruct it2. } { by rewrite (NDIS2 NDIS). }
          destruct (for_each_lookup_case _ _ _ _ _ Eqα' _ _ _ Eqstk Eqstk')
            as [?|[MR _]]; [by subst|]. clear -In' MR HTOP Eqstk WFT NDIS.
          destruct (access1 stk AccessWrite (Tagged tg) σt.(scs))
            as [[n stk1]|] eqn:Eqstk'; [|done]. simpl in MR. simplify_eq.
          destruct (state_wf_stack_item _ WFT _ _ Eqstk). move : Eqstk' HTOP.
          eapply access1_head_preserving; eauto.
      + (* invoke PINV for t *)
        exfalso. destruct (PINV t k h Eqh) as [Lt PI].
        specialize (PI _ _ Eqk') as [? PI].
        specialize (PI _ Eqstk it2.(perm) opro) as [Eql' HTOP].
        { rewrite /= Eqt2 Eqp2. by destruct it2. } { by rewrite (NDIS2 NDIS). }
        destruct k.
        * (* if k is Unique  t  tg, writing with tg must have popped t
            from top, contradicting In'. *)
          rewrite EQL in Lti. destruct (EQ1 _ _ Lti Eqstk) as [ss' [Eq' EQ3]].
          have ?: ss' = stk'. { rewrite Eqstk' in Eq'. by inversion Eq'. }
          subst ss'. clear Eq'. move : EQ3.
          case access1 as [[n1 stk1]|] eqn:EQ4; [|done].
          simpl. intros ?. simplify_eq.
          specialize (NEQ eq_refl).
          have ND := proj2 (state_wf_stack_item _ WFT _ _ Eqstk).
          move : In'.
          eapply (access1_write_remove_incompatible_head _ tg t _ _ _ ND);
            [by eexists|done..].
        * (* if k is Public => t is for SRO, and must also have been popped,
             contradicting In'. *)
          rewrite EQL in Lti. destruct (EQ1 _ _ Lti Eqstk) as [ss' [Eq' EQ3]].
          have ?: ss' = stk'. { rewrite Eqstk' in Eq'. by inversion Eq'. }
          subst ss'. clear Eq'. move : EQ3.
          case access1 as [[n1 stk1]|] eqn:EQ4; [|done].
          simpl. intros ?. simplify_eq.
          have ND := proj2 (state_wf_stack_item _ WFT _ _ Eqstk).
          move : In'.
          eapply (access1_write_remove_incompatible_active_SRO _ tg t _ _ _ ND); eauto.
    - (* cmap_inv : make sure tags in the new resource are still on top *)
      intros c cs Eqc'.
      have Eqc: (r_f  r).(rcm) !! c  Some cs.
      { move  : Eqc'. rewrite /r'. by destruct k0. }
Hai Dang's avatar
Hai Dang committed
910
      specialize (CINV _ _ Eqc). simpl.
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
      clear -Eqα' CINV Eqtg VALID HL HL2. destruct cs as [[T|]| |]; [|done..].
      destruct CINV as [IN CINV]. split; [done|].
      intros t InT. specialize (CINV _ InT) as [? CINV]. split; [done|].
      intros k h.
      (* TODO: duplicated proofs *)
      rewrite /r'. destruct k0.
      + (* if tg was unique *)
        rewrite /rtm /= HL.
        case (decide (t = tg)) => ?.
        { subst tg. rewrite lookup_insert.
          intros [Eqk Eqh]%Some_equiv_inj. simpl in Eqk, Eqh.
          have Eqt : (r_f  r).(rtm) !! t  Some (k, h0) by rewrite HL2 -Eqk.
          intros l'. rewrite -Eqh write_heaplet_dom. intros Inh.
          destruct (CINV _ _ Eqt _ Inh) as (stk' & pm' & Eqstk' & Instk' & NDIS).
          destruct (for_each_access1_active_preserving _ _ _ _ _ _ _ Eqα' _ _ Eqstk')
            as [stk [Eqstk AS]].
          exists stk, pm'. split; last split; [done|by apply AS|done]. }
        { rewrite lookup_insert_ne //.
          intros Eqt l' Inh.
          destruct (CINV _ _ Eqt _ Inh) as (stk' & pm' & Eqstk' & Instk' & NDIS).
          destruct (for_each_access1_active_preserving _ _ _ _ _ _ _ Eqα' _ _ Eqstk')
            as [stk [Eqstk AS]].
          exists stk, pm'. split; last split; [done|by apply AS|done]. }
      + (* if tg was public *)
        intros Eqt l' Inh.
        destruct (CINV _ _ Eqt _ Inh) as (stk' & pm' & Eqstk' & Instk' & NDIS).
        destruct (for_each_access1_active_preserving _ _ _ _ _ _ _ Eqα' _ _ Eqstk')
          as [stk [Eqstk AS]].
        exists stk, pm'. split; last split; [done|by apply AS|done].
    - (* srel *)
Hai Dang's avatar
Hai Dang committed
941
      rewrite /srel /=. destruct SREL as (?&?&?&?&Eq).
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
      repeat split; [done..|].
      intros l1 InD1 Eq1.
      destruct (write_mem_lookup l v σs.(shp)) as [EqN EqO]. rewrite /r'.
      destruct (write_mem_lookup_case l v σt.(shp) l1)
        as [[i [Lti [Eqi Eqmi]]]|[NEql Eql]].
      + subst l1. specialize (EqN _ Lti). (* rewrite EqN. *)
        have InD := (EqD _ Lti).
        rewrite (_: r_f.2  r'.2 = (r_f  r').(rlm)) // in Eq1.
        rewrite -> Eqrlm in Eq1. specialize (Eq _ InD Eq1).
        destruct (lookup_lt_is_Some_2 _ _ Lti) as [s Eqs].
        destruct k0.
        * (* tg was unique, and (l + i) was written to *)
          destruct Eq as [PB|[t PV]].
          { left. intros st. simpl. intros Eqst.
            have ?: st = s. { rewrite Eqmi Eqs in Eqst. by inversion Eqst. }
            subst st. exists s. rewrite EqN. split; [done|].
            move : (PUBWRITE _ (elem_of_list_lookup_2 _ _ _ Eqs)).
            rewrite /arel /=. destruct s as [| |l0 t0|]; [done..| |done].
            intros [? [? Eqt0]]. repeat split; [done..|].
            destruct t0 as [t0|]; [|done].
            repeat split. destruct Eqt0 as [ht0 Eqt0].
            destruct (tmap_lookup_op_r_equiv_pub r_f.(rtm) r.(rtm)
                        _ _ (proj1 (proj1 VALID)) Eqt0) as [h' Eq'].
            exists (h'  ht0). rewrite /rtm /= HL lookup_insert_ne //.
            intros ?; subst t0. rewrite Eqtg in Eqt0.
            apply Some_equiv_inj in Eqt0 as [Eqt0 _]. inversion Eqt0. }
          { destruct PV as (c & T & h & Eqc & InT & Eqt & Inh).
            right. exists t, c, T.
            case (decide (t = tg)) => ?; [subst t|].
            - exists (write_heaplet l v h0). do 2 (split; [done|]). split.
              by rewrite /rtm /= HL lookup_insert.
              rewrite write_heaplet_dom.
              rewrite HL2 in Eqt. apply Some_equiv_inj in Eqt as [? Eqt].
              simpl in Eqt. by rewrite Eqt.
            - exists h. rewrite /rtm /= HL. do 2 (split; [done|]).
              rewrite lookup_insert_ne //. }
        * (* tg was public, and (l + i) was written to *)
          left. intros st. simpl. intros Eqst.
          have ?: st = s. { rewrite Eqmi Eqs in Eqst. by inversion Eqst. }
          subst st. exists s. rewrite EqN. split; [done|].
          (* we know that the values written must be publicly related *)
          apply (arel_mono r _ VALID).
          { apply cmra_included_r. }
          { apply PUBWRITE, (elem_of_list_lookup_2 _ _ _ Eqs). }
      + specialize (EqO _ NEql).
        have InD1': l1  dom (gset loc) σt.(shp)
          by rewrite elem_of_dom -Eql -(elem_of_dom (D:=gset loc)).
        have Eq1' : (r_f  r).(rlm) !! l1  Some $ to_locStateR lsShared.
        { move : Eq1. by destruct k0. }
        specialize (Eq _ InD1' Eq1'). rewrite /pub_loc EqO Eql.
        destruct k0; [|done].
        destruct Eq as [PB|[t PV]].
        * (* tg was unique, and l1 was NOT written to *)
          left. intros st Eqst. destruct (PB _ Eqst) as [ss [Eqss AREL]].
          exists ss. split; [done|]. move : AREL. rewrite /arel.
          destruct ss as [| |l0 t0|], st as [| |l3 t3|]; try done.
          intros [? [? Eqt]]. subst l3 t3. repeat split.
          destruct t0 as [t0|]; [|done].
          destruct Eqt as [h Eqt]. exists h.
          rewrite /rtm /= HL (lookup_insert_ne _ tg) //.
          intros ?. subst t0. move : Eqt. rewrite lookup_op Eqtg.
          by apply tagKindR_exclusive.
        * (* tg was public, and l1 was NOT written to *)
          right. destruct PV as (c & T & h & Eqc & InT & Eqt & Inl).
          exists t, c, T. simpl.
          case (decide (t = tg)) => ?; [subst t|].
          { eexists (write_heaplet l v h0).
            rewrite /rtm /= HL lookup_insert. repeat split; [done..|].
            rewrite lookup_op Eqtg in Eqt.
            by rewrite write_heaplet_dom (tagKindR_exclusive_heaplet _ _ _ Eqt). }
          { exists h. rewrite /rtm /= HL lookup_insert_ne //. }
    - intros l'. rewrite -> Eqrlm. setoid_rewrite Eqrlm.
      intros InD. specialize (LINV _ InD) as [? LINV].
Hai Dang's avatar
Hai Dang committed
1015
      split. { rewrite /= write_mem_dom //. }
Hai Dang's avatar
Hai Dang committed
1016 1017
      intros s t1 Eq. rewrite /=.
      specialize (LINV _ _ Eq) as (?&?&?&?& h & Eqh).
1018 1019 1020 1021 1022 1023 1024
      destruct (write_mem_lookup l v σs.(shp)) as [_ HLs].
      destruct (write_mem_lookup l v σt.(shp)) as [_ HLt].
      have NEQ:  i, (i < length v)%nat  l'  l + i.
      { intros i Lti EQ. rewrite EQL in Lti. specialize (SHR _ Lti).
        subst l'. apply (lmap_lookup_op_r r_f.(rlm)) in SHR; [|apply VALID].
        move : Eq. rewrite SHR. intros Eqv%Some_equiv_inj. inversion Eqv. }
      destruct EQ2 as [_ EQ2].
Hai Dang's avatar
Hai Dang committed
1025 1026 1027 1028 1029 1030
      rewrite HLs // HLt // EQ2 //; [|rewrite -EQL //].
      do 4 (split; [done|]). rewrite /r'. destruct k0; simpl; [|by exists h].
      setoid_rewrite HL.
      case (decide (t1 = tg)) => ?; [subst t1|].
      rewrite lookup_insert. by eexists.
      rewrite lookup_insert_ne //. by eexists.
1031 1032
  }
  left.
Ralf Jung's avatar
Ralf Jung committed
1033
  apply: sim_body_result.
Hai Dang's avatar
Hai Dang committed
1034
  intros. simpl. by apply POST.
1035 1036
Qed.

Ralf Jung's avatar
Ralf Jung committed
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
(** can probably be derived from [write_related_values]? *)
Lemma sim_body_write_owned
  fs ft (r r' r'' rs: resUR) h n l tg T s σs σt Φ:
  tsize T = 1%nat 
  r  r'  res_tag tg tkUnique h 
  arel rs s s  (* assuming to-write values are related *)
  r'  r''  rs 
  ( α', memory_written σt.(sst) σt.(scs) l (Tagged tg) (tsize T) = Some α' 
    let σs' := mkState (write_mem l [s] σs.(shp)) α' σs.(scs) σs.(snp) σs.(snc) in
    let σt' := mkState (write_mem l [s] σt.(shp)) α' σt.(scs) σt.(snp) σt.(snc) in
    tag_on_top σt l tg 
    Φ (r'  res_tag tg tkUnique (<[l:=s]> h)) n (ValR []%S) σs' (ValR []%S) σt') 
  r {n,fs,ft}
    (Place l (Tagged tg) T <- #[s], σs)  (Place l (Tagged tg) T <- #[s], σt) : Φ.
Proof.
Admitted.

1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
(** Retag *)

Lemma retag_ref_change_1 h α cids c nxtp x rk mut T h' α' nxtp'
  (N2: rk  TwoPhase) (TS: (O < tsize T)%nat) (FRZ: is_freeze T) :
  retag h α nxtp cids c x rk (Reference (RefPtr mut) T) = Some (h', α', nxtp') 
   l otag, h !! x = Some (ScPtr l otag) 
   rk' new,
    h' = <[x := ScPtr l new]>h 
    retag_ref h α cids nxtp l otag T rk' (adding_protector rk c) =
      Some (new, α', nxtp') 
    rk' = if mut then UniqueRef (is_two_phase rk) else SharedRef.
Proof.
  rewrite retag_equation_2 /=.
  destruct (h !! x) as [[| |l t|]|]; simpl; [done..| |done|done].
  destruct mut; (case retag_ref as [[[t1 α1] n1]|] eqn:Eq => [/=|//]);
    intros; simplify_eq; exists l, t; (split; [done|]);
    eexists; exists t1; done.
Qed.

Lemma retag_ref_change_2
  h α cids c nxtp l otag rk (mut: mutability) T new α' nxtp'
  (TS: (O < tsize T)%nat) (FRZ: is_freeze T) :
  let rk' := if mut then UniqueRef false else SharedRef in
  let opro := (adding_protector rk c) in
  retag_ref h α cids nxtp l otag T rk' opro = Some (new, α', nxtp') 
  nxtp' = S nxtp  new = Tagged nxtp 
  reborrowN α cids l (tsize T) otag (Tagged nxtp)
            (if mut then Unique else SharedReadOnly) opro = Some α'.
Proof.
  intros rk' opro. rewrite /retag_ref. destruct (tsize T) as [|n] eqn:EqT; [lia|].
  destruct mut; simpl; [|rewrite visit_freeze_sensitive_is_freeze //];
    case reborrowN as [α1|] eqn:Eq1 => [/=|//]; intros; simplify_eq; by rewrite -EqT.
Qed.

Lemma retag_ref_change h α cids c nxtp x rk mut T h' α' nxtp'
  (N2: rk  TwoPhase) (TS: (O < tsize T)%nat) (FRZ: is_freeze T) :
  retag h α nxtp cids c x rk (Reference (RefPtr mut) T) = Some (h', α', nxtp') 
   l otag, h !! x = Some (ScPtr l otag) 
    h' = <[x := ScPtr l (Tagged nxtp)]>h 
    nxtp' = S nxtp 
    reborrowN α cids l (tsize T) otag (Tagged nxtp)
            (if mut then Unique else SharedReadOnly) (adding_protector rk c) = Some α'.
Proof.
  intros RT.
  apply retag_ref_change_1 in RT
    as (l & otag & EqL & rk' & new & Eqh & RT &?); [|done..].
  subst. exists l, otag. split; [done|].
  rewrite (_: is_two_phase rk = false) in RT; [|by destruct rk].
  apply retag_ref_change_2 in RT as (?&?&?); [|done..]. by subst new.
Qed.

Lemma retag_ref_reborrowN
  (h: mem) α t cids c x l otg T rk (mut: mutability) α'
  (N2: rk  TwoPhase) (TS: (O < tsize T)%nat) (FRZ: is_freeze T) :
  h !! x = Some (ScPtr l otg) 
  reborrowN α cids l (tsize T) otg (Tagged t)
     (if mut then Unique else SharedReadOnly) (adding_protector rk c) =
     Some α' 
  retag h α t cids c x rk (Reference (RefPtr mut) T) = Some (<[x:=ScPtr l (Tagged t)]> h, α', S t).
Proof.
  intros Eqx RB. rewrite retag_equation_2 Eqx /= /retag_ref.
  destruct (tsize T) eqn:EqT; [lia|].
  rewrite (_: is_two_phase rk = false); [|by destruct rk].
  destruct mut; simpl; [|rewrite visit_freeze_sensitive_is_freeze //]; rewrite EqT RB /= //.
Qed.

Lemma sim_body_retag_default fs ft r n x xtag mut T σs σt Φ
  (TS: (O < tsize T)%nat) (FRZ: is_freeze T) (Eqx: σs.(shp) = σt.(shp)) :
  let Tr := (Reference (RefPtr mut) T) in
  ( c cids hs' αs' nps' ht' αt' npt' (STACK: σt.(scs) = c :: cids),
    retag σt.(shp) σt.(sst) σt.(snp) σt.(scs) c x Default Tr
      = Some (ht', αt', npt') 
    retag σs.(shp) σs.(sst) σs.(snp) σs.(scs) c x Default Tr
      = Some (hs', αs', nps') 
    let σs' := mkState hs' αs' σs.(scs) nps' σs.(snc) in
    let σt' := mkState ht' αt' σt.(scs) npt' σt.(snc) in
Ralf Jung's avatar
Ralf Jung committed
1130
      Φ r n (ValR []%S) σs' (ValR []%S) σt' : Prop) 
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
  r {n,fs,ft}
    (Retag (Place x xtag Tr) Default, σs) 
    (Retag (Place x xtag Tr) Default, σt) : Φ.
Proof.
  intros Tr POST. pfold. intros NT. intros.
  destruct WSAT as (WFS & WFT & VALID & PINV & CINV & SREL & LINV).
  split; [|done|].
  { right.
    edestruct NT as [[]|[es' [σs' STEPS]]]; [constructor 1|done|].
    (* inversion retag of src *)
    destruct (tstep_retag_inv _ _ _ _ _ _ _ _ _ STEPS)
      as (c & cids & h' & α' & nxtp' & Eqs & EqT & ? & ?). subst es'.
    apply retag_ref_change in EqT as (l & otg & Eqx' & Eqh' & Eqp' & RB); [|done..].
    subst h' nxtp'. destruct SREL as (Eqst & Eqnp & Eqcs & Eqnc &?).
    rewrite Eqx in Eqx'. rewrite Eqst Eqcs Eqnp in RB. rewrite Eqcs in Eqs.
    (* retag of tgt *)
    exists (#[])%V,
      (mkState (<[x:=ScPtr l (Tagged σt.(snp))]> σt.(shp)) α' σt.(scs) (S σt.(snp)) σt.(snc)).
    eapply (head_step_fill_tstep _ []), retag1_head_step'; [eauto|].
    eapply retag_ref_reborrowN; eauto. }
  constructor 1.
  intros.
  (* inversion retag of tgt *)
  destruct (tstep_retag_inv _ _ _ _ _ _ _ _ _ STEPT)
      as (c & cids & h' & α' & nxtp' & Eqs & EqT & ? & ?). subst et'.
  apply retag_ref_change in EqT as (l & otg & Eqx' & Eqh' & Eqp' & RB); [|done..].
  subst h' nxtp'.
  exists (#[])%V,
      (mkState (<[x:=ScPtr l (Tagged σs.(snp))]> σs.(shp)) α' σs.(scs) (S σs.(snp)) σs.(s