rules_binary.v 17.8 KB
Newer Older
1
From iris.program_logic Require Export language ectx_language ectxi_language.
Amin Timany's avatar
Amin Timany committed
2
From iris.program_logic Require Import lifting.
Hai Dang's avatar
Hai Dang committed
3
From iris.algebra Require Import excl auth frac agree gmap list.
Amin Timany's avatar
Amin Timany committed
4
From iris_examples.logrel.F_mu_ref_conc Require Export rules.
Amin Timany's avatar
Amin Timany committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
From iris.proofmode Require Import tactics.
Import uPred.

Definition specN := nroot .@ "spec".

(** The CMRA for the heap of the specification. *)
Definition tpoolUR : ucmraT := gmapUR nat (exclR exprC).
Definition cfgUR := prodUR tpoolUR (gen_heapUR loc val).

Fixpoint to_tpool_go (i : nat) (tp : list expr) : tpoolUR :=
  match tp with
  | [] => 
  | e :: tp => <[i:=Excl e]>(to_tpool_go (S i) tp)
  end.
Definition to_tpool : list expr  tpoolUR := to_tpool_go 0.

(** The CMRA for the thread pool. *)
Class cfgSG Σ := CFGSG { cfg_inG :> inG Σ (authR cfgUR); cfg_name : gname }.

Section definitionsS.
  Context `{cfgSG Σ, invG Σ}.

  Definition heapS_mapsto (l : loc) (q : Qp) (v: val) : iProp Σ :=
    own cfg_name ( (ε, {[ l := (q, to_agree v) ]})).

  Definition tpool_mapsto (j : nat) (e: expr) : iProp Σ :=
    own cfg_name ( ({[ j := Excl e ]}, )).

  Definition spec_inv (ρ : cfg F_mu_ref_conc_lang) : iProp Σ :=
    ( tp σ, own cfg_name ( (to_tpool tp, to_gen_heap σ))
Ralf Jung's avatar
Ralf Jung committed
35
                  rtc erased_step ρ (tp,σ))%I.
36 37
  Definition spec_ctx : iProp Σ :=
    ( ρ, inv specN (spec_inv ρ))%I.
Amin Timany's avatar
Amin Timany committed
38 39 40

  Global Instance heapS_mapsto_timeless l q v : Timeless (heapS_mapsto l q v).
  Proof. apply _. Qed.
41
  Global Instance spec_ctx_persistent : Persistent spec_ctx.
Amin Timany's avatar
Amin Timany committed
42 43 44 45 46
  Proof. apply _. Qed.
End definitionsS.
Typeclasses Opaque heapS_mapsto tpool_mapsto.

Notation "l ↦ₛ{ q } v" := (heapS_mapsto l q v)
Ralf Jung's avatar
Ralf Jung committed
47 48 49
  (at level 20, q at level 50, format "l  ↦ₛ{ q }  v") : bi_scope.
Notation "l ↦ₛ v" := (heapS_mapsto l 1 v) (at level 20) : bi_scope.
Notation "j ⤇ e" := (tpool_mapsto j e) (at level 20) : bi_scope.
Amin Timany's avatar
Amin Timany committed
50

Robbert Krebbers's avatar
Robbert Krebbers committed
51 52 53 54 55 56 57 58 59 60 61 62
Ltac iAsimpl :=
  repeat match goal with
  | |- context [ (_  ?e)%I ] => progress (
    let e' := fresh in evar (e':expr);
    assert (e = e') as ->; [asimpl; unfold e'; reflexivity|];
    unfold e'; clear e')
  | |- context [ WP ?e @ _ {{ _ }}%I ] => progress (
    let e' := fresh in evar (e':expr);
    assert (e = e') as ->; [asimpl; unfold e'; reflexivity|];
    unfold e'; clear e')
  end.

Amin Timany's avatar
Amin Timany committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
Section conversions.
  Context `{cfgSG Σ}.

  (** Conversion to tpools and back *)
  Lemma to_tpool_valid es :  to_tpool es.
  Proof.
    rewrite /to_tpool. move: 0.
    induction es as [|e es]=> n //. by apply insert_valid.
  Qed.

  Lemma tpool_lookup tp j : to_tpool tp !! j = Excl <$> tp !! j.
  Proof.
    cut ( i, to_tpool_go i tp !! (i + j) = Excl <$> tp !! j).
    { intros help. apply (help 0). }
    revert j. induction tp as [|e tp IH]=> //= -[|j] i /=.
    - by rewrite Nat.add_0_r lookup_insert.
    - by rewrite -Nat.add_succ_comm lookup_insert_ne; last lia.
  Qed.
  Lemma tpool_lookup_Some tp j e : to_tpool tp !! j = Excl' e  tp !! j = Some e.
  Proof. rewrite tpool_lookup fmap_Some. naive_solver. Qed.
  Hint Resolve tpool_lookup_Some.

  Lemma to_tpool_insert tp j e :
    j < length tp 
    to_tpool (<[j:=e]> tp) = <[j:=Excl e]> (to_tpool tp).
  Proof.
    intros. apply: map_eq=> i. destruct (decide (i = j)) as [->|].
    - by rewrite tpool_lookup lookup_insert list_lookup_insert.
    - rewrite tpool_lookup lookup_insert_ne // list_lookup_insert_ne //.
      by rewrite tpool_lookup.
  Qed.
  Lemma to_tpool_insert' tp j e :
    is_Some (to_tpool tp !! j) 
    to_tpool (<[j:=e]> tp) = <[j:=Excl e]> (to_tpool tp).
  Proof.
    rewrite tpool_lookup fmap_is_Some lookup_lt_is_Some. apply to_tpool_insert.
  Qed.

  Lemma to_tpool_snoc tp e :
    to_tpool (tp ++ [e]) = <[length tp:=Excl e]>(to_tpool tp).
  Proof.
    intros. apply: map_eq=> i.
    destruct (lt_eq_lt_dec i (length tp)) as [[?| ->]|?].
    - rewrite lookup_insert_ne; last lia. by rewrite !tpool_lookup lookup_app_l.
    - by rewrite lookup_insert tpool_lookup lookup_app_r // Nat.sub_diag.
    - rewrite lookup_insert_ne; last lia.
      rewrite !tpool_lookup ?lookup_ge_None_2 ?app_length //=;
         change (ofe_car exprC) with expr; lia.
  Qed.

  Lemma tpool_singleton_included tp j e :
    {[j := Excl e]}  to_tpool tp  tp !! j = Some e.
  Proof.
    move=> /singleton_included [ex [/leibniz_equiv_iff]].
    rewrite tpool_lookup fmap_Some=> [[e' [-> ->]] /Excl_included ?]. by f_equal.
  Qed.
  Lemma tpool_singleton_included' tp j e :
    {[j := Excl e]}  to_tpool tp  to_tpool tp !! j = Excl' e.
  Proof. rewrite tpool_lookup. by move=> /tpool_singleton_included=> ->. Qed.

End conversions.

Section cfg.
  Context `{heapIG Σ, cfgSG Σ}.
  Implicit Types P Q : iProp Σ.
  Implicit Types Φ : val  iProp Σ.
  Implicit Types σ : state.
  Implicit Types e : expr.
  Implicit Types v : val.

  Local Hint Resolve tpool_lookup.
  Local Hint Resolve tpool_lookup_Some.
  Local Hint Resolve to_tpool_insert.
  Local Hint Resolve to_tpool_insert'.
  Local Hint Resolve tpool_singleton_included.

Ralf Jung's avatar
Ralf Jung committed
139 140 141
  Lemma step_insert K tp j e σ κ e' σ' efs :
    tp !! j = Some (fill K e)  head_step e σ κ e' σ' efs 
    erased_step (tp, σ) (<[j:=fill K e']> tp ++ efs, σ').
Amin Timany's avatar
Amin Timany committed
142 143 144 145
  Proof.
    intros. rewrite -(take_drop_middle tp j (fill K e)) //.
    rewrite insert_app_r_alt take_length_le ?Nat.sub_diag /=;
      eauto using lookup_lt_Some, Nat.lt_le_incl.
Ralf Jung's avatar
Ralf Jung committed
146
    rewrite -(assoc_L (++)) /=. eexists.
Amin Timany's avatar
Amin Timany committed
147 148 149
    eapply step_atomic; eauto. by apply: Ectx_step'.
  Qed.

Ralf Jung's avatar
Ralf Jung committed
150 151 152
  Lemma step_insert_no_fork K tp j e σ κ e' σ' :
    tp !! j = Some (fill K e)  head_step e σ κ e' σ' [] 
    erased_step (tp, σ) (<[j:=fill K e']> tp, σ').
Amin Timany's avatar
Amin Timany committed
153 154
  Proof. rewrite -(right_id_L [] (++) (<[_:=_]>_)). by apply step_insert. Qed.

155 156 157 158 159 160 161 162 163 164 165 166
  Lemma nsteps_inv_r {A} n (R : A  A  Prop) x y :
    nsteps R (S n) x y   z, nsteps R n x z  R z y.
  Proof.
    revert x y; induction n; intros x y.
    - inversion 1; subst.
      match goal with H : nsteps _ 0 _ _ |- _ => inversion H end; subst.
      eexists; repeat econstructor; eauto.
    - inversion 1; subst.
      edestruct IHn as [z [? ?]]; eauto.
      exists z; split; eauto using nsteps_l.
  Qed.

167
  Lemma step_pure' E j K e e' (P : Prop) n :
168 169
    P 
    PureExec P n e e' 
Amin Timany's avatar
Amin Timany committed
170
    nclose specN  E 
171
    spec_ctx  j  fill K e ={E}= j  fill K e'.
Amin Timany's avatar
Amin Timany committed
172
  Proof.
173 174
    iIntros (HP Hex ?) "[#Hinv Hj]". iDestruct "Hinv" as (ρ) "Hspec".
    rewrite /spec_ctx /tpool_mapsto.
175 176
    iInv specN as (tp σ) ">[Hown Hrtc]" "Hclose".
    iDestruct "Hrtc" as %Hrtc.
Amin Timany's avatar
Amin Timany committed
177
    iDestruct (own_valid_2 with "Hown Hj")
Hai Dang's avatar
Hai Dang committed
178
      as %[[Htpj%tpool_singleton_included' _]%prod_included ?]%auth_both_valid.
Amin Timany's avatar
Amin Timany committed
179 180 181 182 183
    iMod (own_update_2 with "Hown Hj") as "[Hown Hj]".
    { by eapply auth_update, prod_local_update_1,
        singleton_local_update, (exclusive_local_update _ (Excl (fill K e'))). }
    iFrame "Hj". iApply "Hclose". iNext. iExists (<[j:=fill K e']> tp), σ.
    rewrite to_tpool_insert'; last eauto.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    iFrame. iPureIntro.
    apply rtc_nsteps in Hrtc; destruct Hrtc as [m Hrtc].
    specialize (Hex HP). apply (nsteps_rtc (m + n)).
    eapply nsteps_trans; eauto.
    revert e e' Htpj Hex.
    induction n => e e' Htpj Hex.
    - inversion Hex; subst.
      rewrite list_insert_id; eauto. econstructor.
    - apply nsteps_inv_r in Hex.
      destruct Hex as [z [Hex1 Hex2]].
      specialize (IHn _ _ Htpj Hex1).
      eapply nsteps_r; eauto.
      replace (<[j:=fill K e']> tp) with
          (<[j:=fill K e']> (<[j:=fill K z]> tp)); last first.
      { clear. revert tp; induction j; intros tp.
        - destruct tp; trivial.
        - destruct tp; simpl; auto. by rewrite IHj. }
      destruct Hex2 as [Hexs Hexd].
      specialize (Hexs σ). destruct Hexs as [e'' [σ' [efs Hexs]]].
      specialize (Hexd σ [] e'' σ' efs Hexs); destruct Hexd as [? [? [? ?]]];
        subst.
      inversion Hexs; simpl in *; subst.
      rewrite -!fill_app.
      eapply step_insert_no_fork; eauto.
      { apply list_lookup_insert. apply lookup_lt_is_Some; eauto. }
Amin Timany's avatar
Amin Timany committed
209 210
  Qed.

211

212
  Lemma do_step_pure E j K e e' `{!PureExec True 1 e e'}:
213
    nclose specN  E 
214
    spec_ctx  j  fill K e ={E}= j  fill K e'.
215 216
  Proof. by eapply step_pure'; last eauto. Qed.

217
  Lemma step_alloc E j K e v:
Amin Timany's avatar
Amin Timany committed
218
    to_val e = Some v  nclose specN  E 
219
    spec_ctx  j  fill K (Alloc e) ={E}=  l, j  fill K (Loc l)  l ↦ₛ v.
Amin Timany's avatar
Amin Timany committed
220
  Proof.
221 222
    iIntros (??) "[#Hinv Hj]". iDestruct "Hinv" as (ρ) "Hinv".
    rewrite /spec_ctx /tpool_mapsto.
Amin Timany's avatar
Amin Timany committed
223 224 225
    iInv specN as (tp σ) ">[Hown %]" "Hclose".
    destruct (exist_fresh (dom (gset positive) σ)) as [l Hl%not_elem_of_dom].
    iDestruct (own_valid_2 with "Hown Hj")
Hai Dang's avatar
Hai Dang committed
226
      as %[[?%tpool_singleton_included' _]%prod_included ?]%auth_both_valid.
Amin Timany's avatar
Amin Timany committed
227 228 229 230 231 232 233 234 235 236 237 238 239
    iMod (own_update_2 with "Hown Hj") as "[Hown Hj]".
    { by eapply auth_update, prod_local_update_1,
        singleton_local_update, (exclusive_local_update _ (Excl (fill K (Loc l)))). }
    iMod (own_update with "Hown") as "[Hown Hl]".
    { eapply auth_update_alloc, prod_local_update_2,
        (alloc_singleton_local_update _ l (1%Qp,to_agree v)); last done.
      by apply lookup_to_gen_heap_None. }
    iExists l. rewrite /heapS_mapsto. iFrame "Hj Hl". iApply "Hclose". iNext.
    iExists (<[j:=fill K (Loc l)]> tp), (<[l:=v]>σ).
    rewrite to_gen_heap_insert to_tpool_insert'; last eauto. iFrame. iPureIntro.
    eapply rtc_r, step_insert_no_fork; eauto. econstructor; eauto.
  Qed.

240
  Lemma step_load E j K l q v:
Amin Timany's avatar
Amin Timany committed
241
    nclose specN  E 
242
    spec_ctx  j  fill K (Load (Loc l))  l ↦ₛ{q} v
Amin Timany's avatar
Amin Timany committed
243 244
    ={E}= j  fill K (of_val v)  l ↦ₛ{q} v.
  Proof.
245
    iIntros (?) "(#Hinv & Hj & Hl)". iDestruct "Hinv" as (ρ) "Hinv".
Amin Timany's avatar
Amin Timany committed
246 247 248
    rewrite /spec_ctx /tpool_mapsto /heapS_mapsto.
    iInv specN as (tp σ) ">[Hown %]" "Hclose".
    iDestruct (own_valid_2 with "Hown Hj")
Hai Dang's avatar
Hai Dang committed
249
      as %[[?%tpool_singleton_included' _]%prod_included ?]%auth_both_valid.
Amin Timany's avatar
Amin Timany committed
250
    iDestruct (own_valid_2 with "Hown Hl") 
Hai Dang's avatar
Hai Dang committed
251 252
      as %[[? ?%gen_heap_singleton_included]%prod_included ?]%auth_both_valid.
    iMod (own_update_2 with "Hown Hj") as "[Hown Hj]".
Amin Timany's avatar
Amin Timany committed
253 254 255 256 257 258 259 260
    { by eapply auth_update, prod_local_update_1, singleton_local_update,
        (exclusive_local_update _ (Excl (fill K (of_val v)))). }
    iFrame "Hj Hl". iApply "Hclose". iNext.
    iExists (<[j:=fill K (of_val v)]> tp), σ.
    rewrite to_tpool_insert'; last eauto. iFrame. iPureIntro.
    eapply rtc_r, step_insert_no_fork; eauto. econstructor; eauto.
  Qed.

261
  Lemma step_store E j K l v' e v:
Amin Timany's avatar
Amin Timany committed
262
    to_val e = Some v  nclose specN  E 
263
    spec_ctx  j  fill K (Store (Loc l) e)  l ↦ₛ v'
Amin Timany's avatar
Amin Timany committed
264 265
    ={E}= j  fill K Unit  l ↦ₛ v.
  Proof.
266
    iIntros (??) "(#Hinv & Hj & Hl)". iDestruct "Hinv" as (ρ) "Hinv".
Amin Timany's avatar
Amin Timany committed
267 268 269
    rewrite /spec_ctx /tpool_mapsto /heapS_mapsto.
    iInv specN as (tp σ) ">[Hown %]" "Hclose".
    iDestruct (own_valid_2 with "Hown Hj")
Hai Dang's avatar
Hai Dang committed
270
      as %[[?%tpool_singleton_included' _]%prod_included _]%auth_both_valid.
Amin Timany's avatar
Amin Timany committed
271
    iDestruct (own_valid_2 with "Hown Hl")
Hai Dang's avatar
Hai Dang committed
272
      as %[[_ Hl%gen_heap_singleton_included]%prod_included _]%auth_both_valid.
Amin Timany's avatar
Amin Timany committed
273 274 275 276 277 278 279 280 281 282 283 284 285
    iMod (own_update_2 with "Hown Hj") as "[Hown Hj]".
    { by eapply auth_update, prod_local_update_1, singleton_local_update,
        (exclusive_local_update _ (Excl (fill K Unit))). }
    iMod (own_update_2 with "Hown Hl") as "[Hown Hl]".
    { eapply auth_update, prod_local_update_2, singleton_local_update,
        (exclusive_local_update _ (1%Qp, to_agree v)); last done.
      by rewrite /to_gen_heap lookup_fmap Hl. }
    iFrame "Hj Hl". iApply "Hclose". iNext.
    iExists (<[j:=fill K Unit]> tp), (<[l:=v]>σ).
    rewrite to_gen_heap_insert to_tpool_insert'; last eauto. iFrame. iPureIntro.
    eapply rtc_r, step_insert_no_fork; eauto. econstructor; eauto.
  Qed.

286
  Lemma step_cas_fail E j K l q v' e1 v1 e2 v2:
Amin Timany's avatar
Amin Timany committed
287
    to_val e1 = Some v1  to_val e2 = Some v2  nclose specN  E  v'  v1 
288
    spec_ctx  j  fill K (CAS (Loc l) e1 e2)  l ↦ₛ{q} v'
Amin Timany's avatar
Amin Timany committed
289 290
    ={E}= j  fill K (# false)  l ↦ₛ{q} v'.
  Proof.
291
    iIntros (????) "(#Hinv & Hj & Hl)". iDestruct "Hinv" as (ρ) "Hinv".
Amin Timany's avatar
Amin Timany committed
292 293 294
    rewrite /spec_ctx /tpool_mapsto /heapS_mapsto.
    iInv specN as (tp σ) ">[Hown %]" "Hclose".
    iDestruct (own_valid_2 with "Hown Hj")
Hai Dang's avatar
Hai Dang committed
295
      as %[[?%tpool_singleton_included' _]%prod_included ?]%auth_both_valid.
Amin Timany's avatar
Amin Timany committed
296
    iDestruct (own_valid_2 with "Hown Hl")
Hai Dang's avatar
Hai Dang committed
297
      as %[[_ ?%gen_heap_singleton_included]%prod_included _]%auth_both_valid.
Amin Timany's avatar
Amin Timany committed
298 299 300 301 302 303 304 305 306
    iMod (own_update_2 with "Hown Hj") as "[Hown Hj]".
    { by eapply auth_update, prod_local_update_1, singleton_local_update,
        (exclusive_local_update _ (Excl (fill K (# false)))). }
    iFrame "Hj Hl". iApply "Hclose". iNext.
    iExists (<[j:=fill K (# false)]> tp), σ.
    rewrite to_tpool_insert'; last eauto. iFrame. iPureIntro.
    eapply rtc_r, step_insert_no_fork; eauto. econstructor; eauto.
  Qed.

307
  Lemma step_cas_suc E j K l e1 v1 v1' e2 v2:
Amin Timany's avatar
Amin Timany committed
308
    to_val e1 = Some v1  to_val e2 = Some v2  nclose specN  E  v1 = v1' 
309
    spec_ctx  j  fill K (CAS (Loc l) e1 e2)  l ↦ₛ v1'
Amin Timany's avatar
Amin Timany committed
310 311
    ={E}= j  fill K (# true)  l ↦ₛ v2.
  Proof.
312
    iIntros (????) "(#Hinv & Hj & Hl)"; subst. iDestruct "Hinv" as (ρ) "Hinv".
Amin Timany's avatar
Amin Timany committed
313 314 315
    rewrite /spec_ctx /tpool_mapsto /heapS_mapsto.
    iInv specN as (tp σ) ">[Hown %]" "Hclose".
    iDestruct (own_valid_2 with "Hown Hj")
Hai Dang's avatar
Hai Dang committed
316
      as %[[?%tpool_singleton_included' _]%prod_included _]%auth_both_valid.
Amin Timany's avatar
Amin Timany committed
317
    iDestruct (own_valid_2 with "Hown Hl")
Hai Dang's avatar
Hai Dang committed
318
      as %[[_ Hl%gen_heap_singleton_included]%prod_included _]%auth_both_valid.
Amin Timany's avatar
Amin Timany committed
319 320 321 322 323 324 325 326 327 328 329 330 331
    iMod (own_update_2 with "Hown Hj") as "[Hown Hj]".
    { by eapply auth_update, prod_local_update_1, singleton_local_update,
        (exclusive_local_update _ (Excl (fill K (# true)))). }
    iMod (own_update_2 with "Hown Hl") as "[Hown Hl]".
    { eapply auth_update, prod_local_update_2, singleton_local_update,
        (exclusive_local_update _ (1%Qp, to_agree v2)); last done.
      by rewrite /to_gen_heap lookup_fmap Hl. }
    iFrame "Hj Hl". iApply "Hclose". iNext.
    iExists (<[j:=fill K (# true)]> tp), (<[l:=v2]>σ).
    rewrite to_gen_heap_insert to_tpool_insert'; last eauto. iFrame. iPureIntro.
    eapply rtc_r, step_insert_no_fork; eauto. econstructor; eauto.
  Qed.

332
  Lemma step_rec E j K e1 e2 v :
Amin Timany's avatar
Amin Timany committed
333
    to_val e2 = Some v  nclose specN  E 
334
    spec_ctx  j  fill K (App (Rec e1) e2)
Amin Timany's avatar
Amin Timany committed
335
    ={E}= j  fill K (e1.[Rec e1,e2/]).
336 337
  Proof. by intros ?; apply: do_step_pure. Qed.

338
  Lemma step_lam E j K e1 e2 v :
339
    to_val e2 = Some v  nclose specN  E 
340
    spec_ctx  j  fill K (App (Lam e1) e2)
341 342 343
    ={E}= j  fill K (e1.[e2/]).
  Proof. by intros ?; apply: do_step_pure. Qed.

344
  Lemma step_letin E j K e1 e2 v :
345
    to_val e1 = Some v  nclose specN  E 
346
    spec_ctx  j  fill K (LetIn e1 e2)
347 348 349
    ={E}= j  fill K (e2.[e1/]).
  Proof. by intros ?; apply: do_step_pure. Qed.

350
  Lemma step_seq E j K e1 e2 v :
351
    to_val e1 = Some v  nclose specN  E 
352
    spec_ctx  j  fill K (Seq e1 e2)
353 354
    ={E}= j  fill K e2.
  Proof. by intros ?; apply: do_step_pure. Qed.
Amin Timany's avatar
Amin Timany committed
355

356
  Lemma step_tlam E j K e :
Amin Timany's avatar
Amin Timany committed
357
    nclose specN  E 
358
    spec_ctx  j  fill K (TApp (TLam e)) ={E}= j  fill K e.
359
  Proof. by intros ?; apply: do_step_pure. Qed.
Amin Timany's avatar
Amin Timany committed
360

361
  Lemma step_Fold E j K e v :
Amin Timany's avatar
Amin Timany committed
362
    to_val e = Some v  nclose specN  E 
363
    spec_ctx  j  fill K (Unfold (Fold e)) ={E}= j  fill K e.
364
  Proof. by intros ?; apply: do_step_pure. Qed.
Amin Timany's avatar
Amin Timany committed
365

366
  Lemma step_fst E j K e1 v1 e2 v2 :
Amin Timany's avatar
Amin Timany committed
367
    to_val e1 = Some v1  to_val e2 = Some v2  nclose specN  E 
368
    spec_ctx  j  fill K (Fst (Pair e1 e2)) ={E}= j  fill K e1.
369
  Proof. by intros; apply: do_step_pure. Qed.
Amin Timany's avatar
Amin Timany committed
370

371
  Lemma step_snd E j K e1 v1 e2 v2 :
Amin Timany's avatar
Amin Timany committed
372
    to_val e1 = Some v1  to_val e2 = Some v2  nclose specN  E 
373
    spec_ctx  j  fill K (Snd (Pair e1 e2)) ={E}= j  fill K e2.
374
  Proof. by intros; apply: do_step_pure. Qed.
Amin Timany's avatar
Amin Timany committed
375

376
  Lemma step_case_inl E j K e0 v0 e1 e2 :
Amin Timany's avatar
Amin Timany committed
377
    to_val e0 = Some v0  nclose specN  E 
378
    spec_ctx  j  fill K (Case (InjL e0) e1 e2)
Amin Timany's avatar
Amin Timany committed
379
      ={E}= j  fill K (e1.[e0/]).
380
  Proof. by intros; apply: do_step_pure. Qed.
Amin Timany's avatar
Amin Timany committed
381

382
  Lemma step_case_inr E j K e0 v0 e1 e2 :
Amin Timany's avatar
Amin Timany committed
383
    to_val e0 = Some v0  nclose specN  E 
384
    spec_ctx  j  fill K (Case (InjR e0) e1 e2)
Amin Timany's avatar
Amin Timany committed
385
      ={E}= j  fill K (e2.[e0/]).
386
  Proof. by intros; apply: do_step_pure. Qed.
Amin Timany's avatar
Amin Timany committed
387

388
  Lemma step_if_false E j K e1 e2 :
Amin Timany's avatar
Amin Timany committed
389
    nclose specN  E 
390
    spec_ctx  j  fill K (If (# false) e1 e2) ={E}= j  fill K e2.
391
  Proof. by intros; apply: do_step_pure. Qed.
Amin Timany's avatar
Amin Timany committed
392

393
  Lemma step_if_true E j K e1 e2 :
Amin Timany's avatar
Amin Timany committed
394
    nclose specN  E 
395
    spec_ctx  j  fill K (If (# true) e1 e2) ={E}= j  fill K e1.
396
  Proof. by intros; apply: do_step_pure. Qed.
Amin Timany's avatar
Amin Timany committed
397

398
  Lemma step_nat_binop E j K op a b :
Amin Timany's avatar
Amin Timany committed
399
    nclose specN  E 
400
    spec_ctx  j  fill K (BinOp op (#n a) (#n b))
Amin Timany's avatar
Amin Timany committed
401
      ={E}= j  fill K (of_val (binop_eval op a b)).
402
  Proof. by intros; apply: do_step_pure. Qed.
Amin Timany's avatar
Amin Timany committed
403

404
  Lemma step_fork E j K e :
Amin Timany's avatar
Amin Timany committed
405
    nclose specN  E 
406
    spec_ctx  j  fill K (Fork e) ={E}=  j', j  fill K Unit  j'  e.
Amin Timany's avatar
Amin Timany committed
407
  Proof.
408 409
    iIntros (?) "[#Hinv Hj]". iDestruct "Hinv" as (ρ) "Hinv".
    rewrite /spec_ctx /tpool_mapsto.
Amin Timany's avatar
Amin Timany committed
410 411
    iInv specN as (tp σ) ">[Hown %]" "Hclose".
    iDestruct (own_valid_2 with "Hown Hj")
Hai Dang's avatar
Hai Dang committed
412
      as %[[?%tpool_singleton_included' _]%prod_included ?]%auth_both_valid.
Amin Timany's avatar
Amin Timany committed
413 414 415 416 417 418 419
    assert (j < length tp) by eauto using lookup_lt_Some.
    iMod (own_update_2 with "Hown Hj") as "[Hown Hj]".
    { by eapply auth_update, prod_local_update_1,
        singleton_local_update, (exclusive_local_update _ (Excl (fill K Unit))). }
    iMod (own_update with "Hown") as "[Hown Hfork]".
    { eapply auth_update_alloc, prod_local_update_1,
        (alloc_singleton_local_update _ (length tp) (Excl e)); last done.
Robbert Krebbers's avatar
Robbert Krebbers committed
420
      rewrite lookup_insert_ne ?tpool_lookup; last lia.
Amin Timany's avatar
Amin Timany committed
421 422 423 424 425 426 427
      by rewrite lookup_ge_None_2. }
    iExists (length tp). iFrame "Hj Hfork". iApply "Hclose". iNext.
    iExists (<[j:=fill K Unit]> tp ++ [e]), σ.
    rewrite to_tpool_snoc insert_length to_tpool_insert //. iFrame. iPureIntro.
    eapply rtc_r, step_insert; eauto. econstructor; eauto.
  Qed.
End cfg.