concurrent_stack4.v 23.2 KB
Newer Older
1 2 3 4
From iris.program_logic Require Export weakestpre hoare.
From iris.heap_lang Require Export lang proofmode notation.
From iris.algebra Require Import excl.

5 6
From iris_examples.concurrent_stacks Require Import spec.

Ralf Jung's avatar
Ralf Jung committed
7
(** Stack 3: Helping, view-shift spec. *)
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
Definition mk_offer : val :=
  λ: "v", ("v", ref #0).
Definition revoke_offer : val :=
  λ: "v", if: CAS (Snd "v") #0 #2 then SOME (Fst "v") else NONE.
Definition take_offer : val :=
  λ: "v", if: CAS (Snd "v") #0 #1 then SOME (Fst "v") else NONE.

Definition mailbox : val :=
  λ: "_",
  let: "r" := ref NONEV in
  (rec: "put" "v" :=
     let: "off" := mk_offer "v" in
     "r" <- SOME "off";;
     revoke_offer "off",
   rec: "get" "n" :=
     let: "offopt" := !"r" in
     match: "offopt" with
       NONE => NONE
     | SOME "x" => if: CAS (Snd "x") #0 #1 then SOME (Fst "x") else NONE
     end
  ).

Definition mk_stack : val :=
  λ: "_",
  let: "mailbox" := mailbox #() in
  let: "put" := Fst "mailbox" in
  let: "get" := Snd "mailbox" in
  let: "r" := ref NONEV in
  (rec: "pop" "n" :=
     match: "get" #() with
       NONE =>
       (match: !"r" with
          NONE => NONE
        | SOME "hd" =>
          if: CAS "r" (SOME "hd") (Snd "hd")
          then SOME (Fst "hd")
          else "pop" "n"
        end)
     | SOME "x" => SOME "x"
     end,
    rec: "push" "n" :=
      match: "put" "n" with
        NONE => #()
      | SOME "n" =>
        let: "r'" := !"r" in
        let: "r''" := SOME ("n", "r'") in
        if: CAS "r" "r'" "r''"
        then #()
        else "push" "n"
      end).

Definition channelR := exclR unitR.
Class channelG Σ := { channel_inG :> inG Σ channelR }.
Definition channelΣ : gFunctors := #[GFunctor channelR].
Instance subG_channelΣ {Σ} : subG channelΣ Σ  channelG Σ.
Proof. solve_inG. Qed.

Section side_channel.
  Context `{!heapG Σ, !channelG Σ} (N : namespace).

  Implicit Types l : loc.

  Definition stages γ (P : list val  iProp Σ) (Q : iProp Σ) l (v : val):=
Ralf Jung's avatar
Ralf Jung committed
72
    ((l  #0  ( vs, P vs ={  N}= P (v :: vs)  Q))
73 74 75 76 77 78 79 80 81 82 83
      (l  #1  Q)
      (l  #1  own γ (Excl ()))
      (l  #2  own γ (Excl ())))%I.

  Definition is_offer γ (P : list val  iProp Σ) Q (v : val) : iProp Σ :=
    ( v' l, v = (v', #l)%V  inv (N .@ "offer_inv")(stages γ P Q l v'))%I.

  Definition mailbox_inv (P : list val  iProp Σ) Q (v : val) : iProp Σ :=
    ( l, v = #l  (l  NONEV  ( v' γ, l  SOMEV v'  is_offer γ P Q v')))%I.
End side_channel.
Section stack_works.
Ralf Jung's avatar
Ralf Jung committed
84
  Context `{!heapG Σ}.
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 139 140 141 142 143 144 145

  Implicit Types l : loc.

  Fixpoint is_stack xs v : iProp Σ :=
    (match xs with
     | [] => v = NONEV
     | x :: xs =>  (t : val), v = SOMEV (x, t)%V  is_stack xs t
     end)%I.

  Definition stack_inv P v :=
    ( l v' xs, v = #l  l  v'  is_stack xs v'  P xs)%I.

  Lemma is_stack_disj xs v :
      is_stack xs v - is_stack xs v  (v = NONEV   (h t : val), v = SOMEV (h, t)%V).
  Proof.
    iIntros "Hstack".
    destruct xs.
    - iSplit; try iLeft; auto.
    - iSplit; auto; iRight; iDestruct "Hstack" as (t) "[% Hstack]";
      iExists v0, t; auto.
  Qed.

  Lemma is_stack_uniq :  xs ys v,
      is_stack xs v  is_stack ys v - xs = ys.
  Proof.
    induction xs, ys; iIntros (v') "[Hstack1 Hstack2]"; auto.
    - iDestruct "Hstack1" as "%".
      iDestruct "Hstack2" as (t) "[% Hstack2]".
      subst.
      discriminate.
    - iDestruct "Hstack2" as "%".
      iDestruct "Hstack1" as (t) "[% Hstack1]".
      subst.
      discriminate.
    - iDestruct "Hstack1" as (t) "[% Hstack1]".
      iDestruct "Hstack2" as (t') "[% Hstack2]".
      subst; injection H0; intros; subst.
      iDestruct (IHxs with "[Hstack1 Hstack2]") as "%".
        by iSplitL "Hstack1".
      subst; auto.
  Qed.

  Lemma is_stack_empty :  xs,
      is_stack xs (InjLV #()) - xs = [].
  Proof.
    iIntros (xs) "Hstack".
    destruct xs; auto.
    iDestruct "Hstack" as (t) "[% rest]".
    discriminate.
  Qed.
  Lemma is_stack_cons :  xs h t,
      is_stack xs (InjRV (h, t)%V) -
               is_stack xs (InjRV (h, t)%V)   ys, xs = h :: ys.
  Proof.
    destruct xs; iIntros (h t) "Hstack".
    - iDestruct "Hstack" as "%"; discriminate.
    - iSplit; [auto | iExists xs].
      iDestruct "Hstack" as (t') "[% Hstack]".
      injection H; intros; subst; auto.
  Qed.

Ralf Jung's avatar
Ralf Jung committed
146 147
  (* Whole-stack invariant (P). *)
  Theorem stack_works {channelG0 : channelG Σ} N P Q Q' Q'' Φ :
148
     ( (f f : val),
Ralf Jung's avatar
Ralf Jung committed
149 150
        (((( v vs, P (v :: vs) ={   N}= Q v  P vs)
             (P [] ={   N}= Q'  P []) -
151
            WP f #() {{ v, ( (v' : val), v  SOMEV v'  Q v')  (v  NONEV  Q')}}))
Ralf Jung's avatar
Ralf Jung committed
152 153
          ( (v : val),
                (( vs, P vs ={   N}= P (v :: vs)  Q'') - WP f v {{ v, Q'' }})))
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
         - Φ (f, f)%V)%I
    - P []
    - WP mk_stack #()  {{ Φ }}.
  Proof.
    iIntros "HΦ HP".
    wp_let.
    wp_let.
    wp_alloc m as "Hm".
    iMod (inv_alloc (N .@ "mailbox_inv") _ (mailbox_inv N P Q'' #m) with "[Hm]") as "#Imailbox".
    iExists m; iSplit; auto.
    wp_let.
    wp_let.
    wp_proj.
    wp_let.
    wp_proj.
    wp_let.
    wp_alloc l as "Hl".
    iMod (inv_alloc (N .@ "stack_inv") _ (stack_inv P #l) with "[Hl HP]") as "#Istack".
    { iNext; iExists l, (InjLV #()), []; iSplit; iFrame; auto. }
    wp_let.
Ralf Jung's avatar
Ralf Jung committed
174 175
    iApply "HΦ"; iSplit.
    - iIntros "!# Hcont".
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
      iLöb as "IH".
      wp_rec.
      wp_rec.
      wp_bind (! _)%E.
      iInv (N .@ "mailbox_inv") as "Hmail" "Hclose".
      iDestruct "Hmail" as (m') "[>% H]".
      injection H; intros; subst.
      iDestruct "H" as "[Hm' | H]".
      * wp_load.
        iMod ("Hclose" with "[Hm']").
        { iExists m'; iSplit; auto. }
        iModIntro.
        wp_let.
        wp_match.
        wp_match.
        wp_bind (! _)%E.
        iInv (N .@ "stack_inv") as "Hstack" "Hclose".
        iDestruct "Hstack" as (l' v' xs) "[>% [Hl' [Hstack HP]]]".
        injection H0; intros; subst.
        wp_load.
        iDestruct (is_stack_disj with "[Hstack]") as "[Hstack H]"; auto.
        iDestruct "H" as "[% | H]".
        + subst.
          iDestruct (is_stack_empty with "Hstack") as "%".
          subst.
Ralf Jung's avatar
Ralf Jung committed
201 202 203 204
          iDestruct "Hcont" as "[_ Hfail]".
          iDestruct ("Hfail" with "HP") as "Hfail".
          iDestruct (fupd_mask_mono with "Hfail") as "Hfail";
          last iMod "Hfail" as "[HQ' HP]"; first by solve_ndisj.
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
          iMod ("Hclose" with "[Hl' Hstack HP]").
          { iExists l', (InjLV #()), []; iSplit; iFrame; auto. }
          iModIntro.
          wp_match.
          iRight; auto.
        + iDestruct "H" as (h t) "%".
          subst.
          iMod ("Hclose" with "[Hl' Hstack HP]").
          { iExists l', (InjRV (h, t)), xs; iSplit; iFrame; auto. }
          iModIntro.

          assert (to_val (h, t)%V = Some (h, t)%V) by apply to_of_val.
          assert (is_Some (to_val (h, t)%V)) by (exists (h, t)%V; auto).
          wp_match.
          unfold subst; simpl; fold of_val.

          wp_proj.
          wp_bind (CAS _ _ _).
          iInv (N .@ "stack_inv") as "Hstack" "Hclose".
          iDestruct "Hstack" as (l'' v' ys) "[>% [Hl'' [Hstack HP]]]".
          injection H3; intros; subst.
          assert (Decision (v' = InjRV (h, t)%V)) as Heq by apply val_eq_dec.
          destruct Heq.
          ++ wp_cas_suc.
             subst.
             iDestruct (is_stack_cons with "Hstack") as "[Hstack H]".
             iDestruct "H" as (ys') "%"; subst.
             iDestruct "Hstack" as (t') "[% Hstack]".
             injection H4; intros; subst.
Ralf Jung's avatar
Ralf Jung committed
234 235 236 237
             iDestruct "Hcont" as "[Hsucc _]".
             iDestruct ("Hsucc" with "HP") as "Hsucc".
             iDestruct (fupd_mask_mono with "Hsucc") as "Hsucc";
               last iMod "Hsucc" as "[HQ HP]"; first by solve_ndisj.
238 239 240 241 242 243 244 245 246 247 248
             iMod ("Hclose" with "[Hl'' Hstack HP]").
             { iExists l'', t', ys'; iSplit; iFrame; auto. }
             iModIntro.
             wp_if.
             wp_proj.
             iLeft; iExists h; auto.
          ++ wp_cas_fail.
             iMod ("Hclose" with "[Hl'' Hstack HP]").
             { iExists l'', v', ys; iSplit; iFrame; auto. }
             iModIntro.
             wp_if.
Ralf Jung's avatar
Ralf Jung committed
249
             iApply ("IH" with "Hcont").
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
      * iDestruct "H" as (v' γ') "[Hm' Hstages]".
        wp_load.
        iDestruct "Hstages" as (v'' l') "[% #Hstages]".
        iMod ("Hclose" with "[Hm' Hstages]").
        { iExists m'. iSplit; auto; iRight.
          iExists v', γ'; iFrame. iExists v'', l'; iSplit; auto. }
        iModIntro.
        wp_let.
        wp_match.
        unfold take_offer.
        subst.
        wp_proj.
        wp_bind (CAS _ _ _).
        iInv (N .@ "offer_inv") as "Hstage" "Hclose".
        iDestruct "Hstage" as "[H | [H | [H | H]]]".
        + iDestruct "H" as "[Hl' Hmove]".
          iInv (N .@ "stack_inv") as "Hstack" "Hclose2".
          wp_cas_suc.
          iDestruct "Hstack" as (l'' v' xs) "[% [Hl'' [Hstack HP]]]".
Ralf Jung's avatar
Ralf Jung committed
269 270 271 272 273 274 275 276 277
          iDestruct ("Hmove" with "HP") as "Hmove".
          iDestruct (fupd_mask_mono with "Hmove") as "Hmove";
            last iMod "Hmove" as "[HP HQ'']".
          { apply ndisj_subseteq_difference; try solve_ndisj. }
          iDestruct "Hcont" as "[Hsucc _]".
          iDestruct ("Hsucc" with "HP") as "Hsucc".
          iDestruct (fupd_mask_mono with "Hsucc") as "Hsucc";
            last (iMod "Hsucc" as "[HQ HP]").
          { apply ndisj_subseteq_difference; try solve_ndisj. }
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
          iMod ("Hclose2" with "[Hl'' Hstack HP]").
          { iExists l'', v', xs; iSplit; iFrame; auto. }
          iModIntro.
          iMod ("Hclose" with "[Hl' HQ'']").
          { iRight; iLeft; iFrame. }
          iModIntro.
          wp_if.
          wp_proj.

          assert (to_val v'' = Some v'') by apply to_of_val.
          assert (is_Some (to_val v'')) by (exists v''; auto).
          assert (to_val (InjRV v'') = Some (InjRV v'')) by apply to_of_val.
          assert (is_Some (to_val (InjRV v''))) by (exists (InjRV v''); auto).
          wp_match.
          iLeft; iExists v''; iFrame; auto.
        + iDestruct "H" as "[Hl' HQ'']".
          wp_cas_fail.
          iMod ("Hclose" with "[Hl' HQ'']").
          { iRight; iLeft; iFrame. }
          iModIntro.
          wp_if.
          wp_match.
          wp_bind (! _)%E.
          iInv (N .@ "stack_inv") as "Hstack" "Hclose".
          iDestruct "Hstack" as (l'' v''' xs) "[>% [Hl' [Hstack HP]]]"; simplify_eq.
          wp_load.
          iDestruct (is_stack_disj with "[Hstack]") as "[Hstack H]"; auto.
          iDestruct "H" as "[% | H]".
          ** subst.
             iDestruct (is_stack_empty with "Hstack") as "%".
             subst.
Ralf Jung's avatar
Ralf Jung committed
309 310 311 312 313
             iDestruct "Hcont" as "[_ Hfail]".
             iDestruct ("Hfail" with "HP") as "Hfail".
             iDestruct (fupd_mask_mono with "Hfail") as "Hfail";
               last iMod "Hfail" as "[HQ' HP]"; first by solve_ndisj.

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
             iMod ("Hclose" with "[Hl' Hstack HP]").
             { iExists l'', (InjLV #()), []; iSplit; iFrame; auto. }
             iModIntro.
             wp_match.
             iRight; auto.
          ** iDestruct "H" as (h t) "%".
             subst.
             iMod ("Hclose" with "[Hl' Hstack HP]").
             { iExists l'', (InjRV (h, t)), xs; iSplit; iFrame; auto. }
             iModIntro.

             assert (to_val (h, t)%V = Some (h, t)%V) by apply to_of_val.
             assert (is_Some (to_val (h, t)%V)) by (exists (h, t)%V; auto).
             wp_match.
             unfold subst; simpl; fold of_val.

             wp_proj.
             wp_bind (CAS _ _ _).
             iInv (N .@ "stack_inv") as "Hstack" "Hclose".
             iDestruct "Hstack" as (l''' v''' ys) "[>% [Hl''' [Hstack HP]]]"; simplify_eq.
             destruct (decide (v''' = InjRV (h, t)%V)).
             ++ wp_cas_suc.
                subst.
                iDestruct (is_stack_cons with "Hstack") as "[Hstack H]".
                iDestruct "H" as (ys') "%"; subst.
                iDestruct "Hstack" as (t') "[% Hstack]"; simplify_eq.
Ralf Jung's avatar
Ralf Jung committed
340 341 342 343
                iDestruct "Hcont" as "[Hsucc _]".
                iDestruct ("Hsucc" with "HP") as "Hsucc".
                iDestruct (fupd_mask_mono with "Hsucc") as "Hsucc";
                  last iMod "Hsucc" as "[HQ HP]"; first by solve_ndisj.
344 345 346 347 348 349 350 351 352 353 354
                iMod ("Hclose" with "[Hl''' Hstack HP]").
                { iExists l''', t', ys'; iSplit; iFrame; auto. }
                iModIntro.
                wp_if.
                wp_proj.
                iLeft; iExists h; auto.
             ++ wp_cas_fail.
                iMod ("Hclose" with "[Hl''' Hstack HP]").
                { iExists l''', v''', ys; iSplit; iFrame; auto. }
                iModIntro.
                wp_if.
Ralf Jung's avatar
Ralf Jung committed
355
                iApply ("IH" with "Hcont").
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
        + iDestruct "H" as "[Hl' Hγ']".
          wp_cas_fail.
          iMod ("Hclose" with "[Hl' Hγ']").
          { iRight; iRight; iLeft; iFrame. }
          iModIntro.
          wp_if.
          wp_match.
          wp_bind (! _)%E.
          iInv (N .@ "stack_inv") as "Hstack" "Hclose".
          iDestruct "Hstack" as (l'' v''' xs) "[>% [Hl' [Hstack HP]]]"; simplify_eq.
          wp_load.
          iDestruct (is_stack_disj with "[Hstack]") as "[Hstack H]"; auto.
          iDestruct "H" as "[% | H]".
          ** subst.
             iDestruct (is_stack_empty with "Hstack") as "%".
             subst.
Ralf Jung's avatar
Ralf Jung committed
372 373 374 375
             iDestruct "Hcont" as "[_ Hfail]".
             iDestruct ("Hfail" with "HP") as "Hfail".
             iDestruct (fupd_mask_mono with "Hfail") as "Hfail";
               last iMod "Hfail" as "[HQ HP]"; first by solve_ndisj.
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
             iMod ("Hclose" with "[Hl' Hstack HP]").
             { iExists l'', (InjLV #()), []; iSplit; iFrame; auto. }
             iModIntro.
             wp_match.
             iRight; auto.
          ** iDestruct "H" as (h t) "%".
             subst.
             iMod ("Hclose" with "[Hl' Hstack HP]").
             { iExists l'', (InjRV (h, t)), xs; iSplit; iFrame; auto. }
             iModIntro.

             assert (to_val (h, t)%V = Some (h, t)%V) by apply to_of_val.
             assert (is_Some (to_val (h, t)%V)) by (exists (h, t)%V; auto).
             wp_match.
             unfold subst; simpl; fold of_val.

             wp_proj.
             wp_bind (CAS _ _ _).
             iInv (N .@ "stack_inv") as "Hstack" "Hclose".
             iDestruct "Hstack" as (l''' v''' ys) "[>% [Hl''' [Hstack HP]]]"; simplify_eq.
             destruct (decide (v''' = InjRV (h, t)%V)).
             ++ wp_cas_suc.
                subst.
                iDestruct (is_stack_cons with "Hstack") as "[Hstack H]".
                iDestruct "H" as (ys') "%"; subst.
                iDestruct "Hstack" as (t') "[% Hstack]"; simplify_eq.
Ralf Jung's avatar
Ralf Jung committed
402 403 404 405
                iDestruct "Hcont" as "[Hsucc _]".
                iDestruct ("Hsucc" with "HP") as "Hsucc".
                iDestruct (fupd_mask_mono with "Hsucc") as "Hsucc";
                  last iMod "Hsucc" as "[HQ HP]"; first by solve_ndisj.
406 407 408 409 410 411 412 413 414 415 416
                iMod ("Hclose" with "[Hl''' Hstack HP]").
                { iExists l''', t', ys'; iSplit; iFrame; auto. }
                iModIntro.
                wp_if.
                wp_proj.
                iLeft; iExists h; auto.
             ++ wp_cas_fail.
                iMod ("Hclose" with "[Hl''' Hstack HP]").
                { iExists l''', v''', ys; iSplit; iFrame; auto. }
                iModIntro.
                wp_if.
Ralf Jung's avatar
Ralf Jung committed
417
                iApply ("IH" with "Hcont").
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434

        + iDestruct "H" as "[Hl' Hγ']".
          wp_cas_fail.
          iMod ("Hclose" with "[Hl' Hγ']").
          { iRight; iRight; iRight; iFrame. }
          iModIntro.
          wp_if.
          wp_match.
          wp_bind (! _)%E.
          iInv (N .@ "stack_inv") as "Hstack" "Hclose".
          iDestruct "Hstack" as (l'' v''' xs) "[>% [Hl' [Hstack HP]]]"; simplify_eq.
          wp_load.
          iDestruct (is_stack_disj with "[Hstack]") as "[Hstack H]"; auto.
          iDestruct "H" as "[% | H]".
          ** subst.
             iDestruct (is_stack_empty with "Hstack") as "%".
             subst.
Ralf Jung's avatar
Ralf Jung committed
435 436 437 438
             iDestruct "Hcont" as "[_ Hfail]".
             iDestruct ("Hfail" with "HP") as "Hfail".
             iDestruct (fupd_mask_mono with "Hfail") as "Hfail";
               last iMod "Hfail" as "[HQ HP]"; first by solve_ndisj.
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
             iMod ("Hclose" with "[Hl' Hstack HP]").
             { iExists l'', (InjLV #()), []; iSplit; iFrame; auto. }
             iModIntro.
             wp_match.
             iRight; auto.
          ** iDestruct "H" as (h t) "%".
             subst.
             iMod ("Hclose" with "[Hl' Hstack HP]").
             { iExists l'', (InjRV (h, t)), xs; iSplit; iFrame; auto. }
             iModIntro.

             assert (to_val (h, t)%V = Some (h, t)%V) by apply to_of_val.
             assert (is_Some (to_val (h, t)%V)) by (exists (h, t)%V; auto).
             wp_match.
             unfold subst; simpl; fold of_val.

             wp_proj.
             wp_bind (CAS _ _ _).
             iInv (N .@ "stack_inv") as "Hstack" "Hclose".
             iDestruct "Hstack" as (l''' v''' ys) "[>% [Hl''' [Hstack HP]]]"; simplify_eq.
             destruct (decide (v''' = InjRV (h, t)%V)).
             ++ wp_cas_suc.
                subst.
                iDestruct (is_stack_cons with "Hstack") as "[Hstack H]".
                iDestruct "H" as (ys') "%"; subst.
                iDestruct "Hstack" as (t') "[% Hstack]"; simplify_eq.
Ralf Jung's avatar
Ralf Jung committed
465 466 467 468
                iDestruct "Hcont" as "[Hsucc _]".
                iDestruct ("Hsucc" with "HP") as "Hsucc".
                iDestruct (fupd_mask_mono with "Hsucc") as "Hsucc";
                  last iMod "Hsucc" as "[HQ HP]"; first by solve_ndisj.
469 470 471 472 473 474 475 476 477 478 479
                iMod ("Hclose" with "[Hl''' Hstack HP]").
                { iExists l''', t', ys'; iSplit; iFrame; auto. }
                iModIntro.
                wp_if.
                wp_proj.
                iLeft; iExists h; auto.
             ++ wp_cas_fail.
                iMod ("Hclose" with "[Hl''' Hstack HP]").
                { iExists l''', v''', ys; iSplit; iFrame; auto. }
                iModIntro.
                wp_if.
Ralf Jung's avatar
Ralf Jung committed
480
                iApply ("IH" with "Hcont").
481 482 483 484 485 486 487 488 489 490 491
    - iIntros (v) "!# Hpush".
      iLöb as "IH".
      wp_rec.
      wp_rec.
      wp_lam.
      wp_alloc s as "Hs".
      wp_let.
      wp_bind (_ <- _)%E.
      iInv (N .@ "mailbox_inv") as "Hmail" "Hclose".
      iDestruct "Hmail" as (m') "[>% Hmail]"; simplify_eq.
      iMod (own_alloc (Excl ())) as (γ) "Hγ". done.
Ralf Jung's avatar
Ralf Jung committed
492
      iMod (inv_alloc (N .@ "offer_inv") _ (stages N γ P Q'' s v) with "[Hs Hpush]") as "#Istages".
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
      { iLeft; iFrame; auto. }
      iDestruct "Hmail" as "[H | H]".
      * wp_store.
        iMod ("Hclose" with "[H]").
        { iExists m'. iSplit; auto; iRight; iExists (v, #s)%V, γ.
          iFrame. iExists v, s; iSplit; auto. }
        iModIntro.
        wp_let.
        wp_lam.
        wp_proj.
        wp_bind (CAS _ _ _).
        iInv (N .@ "offer_inv") as "Hstages" "Hclose".
        iDestruct "Hstages" as "[H | [H | [H | H]]]".
        + iDestruct "H" as "[Hs Hpush]".
          wp_cas_suc.
          iMod ("Hclose" with "[Hs Hγ]").
          { iRight; iRight; iRight; iFrame. }
          iModIntro.
          wp_if.
          wp_proj.
          wp_match.

          wp_bind (! _)%E.
          iInv (N .@ "stack_inv") as "Hstack" "Hclose".
          iDestruct "Hstack" as (l' v' ys) "[>% [Hl' [Hstack HP]]]"; simplify_eq.
          wp_load.
          iMod ("Hclose" with "[Hl' Hstack HP]").
          { iExists l', v', ys; iSplit; iFrame; auto. }
          iModIntro.
          wp_let.
          wp_let.
          wp_bind (CAS _ _ _).
          iInv (N .@ "stack_inv") as "Hstack" "Hclose".
          iDestruct "Hstack" as (l'' v'' xs) "[>% [Hl'' [Hstack HP]]]"; simplify_eq.
          destruct (decide (v' = v''%V)).
          ++ wp_cas_suc.
Ralf Jung's avatar
Ralf Jung committed
529 530 531
             iDestruct ("Hpush" with "HP") as "Hpush".
             iDestruct (fupd_mask_mono with "Hpush") as "Hpush";
               last iMod "Hpush" as "[HP HQ]"; first by solve_ndisj.
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 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
             iMod ("Hclose" with "[Hl'' Hstack HP]").
             { iExists l'', (InjRV (v, v')), (v :: xs); iSplit; iFrame; auto.
               iExists v'; iSplit; subst; auto. }
             iModIntro.
             wp_if.
             done.
          ++ wp_cas_fail.
             iMod ("Hclose" with "[Hl'' Hstack HP]").
             { iExists l'', v'', xs; iSplit; iFrame; auto. }
             iModIntro.
             wp_if.
             iApply ("IH" with "Hpush").
        + iDestruct "H" as "[Hs HQ'']".
          wp_cas_fail.
          iMod ("Hclose" with "[Hs Hγ]").
          { iRight; iRight; iLeft. iFrame. }
          iModIntro.
          wp_if.
          wp_match.
          done.
        + iDestruct "H" as "[Hs Hγ']".
          wp_cas_fail.
          by iDestruct (own_valid_2 with "Hγ Hγ'") as %?.
        + iDestruct "H" as "[Hs Hγ']".
          wp_cas_fail.
          by iDestruct (own_valid_2 with "Hγ Hγ'") as %?.
    * iDestruct "H" as (v' γ') "[Hm Hoffer]".
      wp_store.
      iMod ("Hclose" with "[Hm]").
      { iExists m'. iSplit; auto; iRight; iExists (v, #s)%V, γ.
        iFrame. iExists v, s; iSplit; auto. }
      iModIntro.
      wp_let.
      wp_lam.
      wp_proj.
      wp_bind (CAS _ _ _).
      iInv (N .@ "offer_inv") as "Hstages" "Hclose".
      iDestruct "Hstages" as "[H | [H | [H | H]]]".
      + iDestruct "H" as "[Hs Hpush]".
        wp_cas_suc.
        iMod ("Hclose" with "[Hs Hγ]").
        { iRight; iRight; iRight; iFrame. }
        iModIntro.
        wp_if.
        wp_proj.

        wp_match.
        wp_bind (! _)%E.
        iInv (N .@ "stack_inv") as "Hstack" "Hclose".
        iDestruct "Hstack" as (l' v'' ys) "[>% [Hl' [Hstack HP]]]"; simplify_eq.
        wp_load.
        iMod ("Hclose" with "[Hl' Hstack HP]").
        { iExists l', v'', ys; iSplit; iFrame; auto. }
        iModIntro.
        wp_let.
        wp_let.
        wp_bind (CAS _ _ _).
        iInv (N .@ "stack_inv") as "Hstack" "Hclose".
        iDestruct "Hstack" as (l'' v''' xs) "[>% [Hl'' [Hstack HP]]]"; simplify_eq.
        destruct (decide (v'' = v'''%V)).
        ++ wp_cas_suc.
Ralf Jung's avatar
Ralf Jung committed
593 594 595
           iDestruct ("Hpush" with "HP") as "Hpush".
           iDestruct (fupd_mask_mono with "Hpush") as "Hpush";
             last iMod "Hpush" as "[HP HQ]"; first by solve_ndisj.
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
           iMod ("Hclose" with "[Hl'' Hstack HP]").
           { iExists l'', (InjRV (v, v'')), (v :: xs); iSplit; iFrame; auto.
             iExists v''; iSplit; subst; auto. }
           iModIntro.
           wp_if.
           done.
        ++ wp_cas_fail.
           iMod ("Hclose" with "[Hl'' Hstack HP]").
           { iExists l'', v''', xs; iSplit; iFrame; auto. }
           iModIntro.
           wp_if.
           iApply ("IH" with "Hpush").
      + iDestruct "H" as "[Hs HQ'']".
        wp_cas_fail.
        iMod ("Hclose" with "[Hs Hγ]").
        { iRight; iRight; iLeft. iFrame. }
        iModIntro.
        wp_if.
        wp_match.
        done.
      + iDestruct "H" as "[Hs Hγ']".
        wp_cas_fail.
        by iDestruct (own_valid_2 with "Hγ Hγ'") as %?.
      + iDestruct "H" as "[Hs Hγ']".
        wp_cas_fail.
        by iDestruct (own_valid_2 with "Hγ Hγ'") as %?.
  Qed.
623 624 625 626 627

  Program Definition is_concurrent_stack `{!channelG Σ} : concurrent_stack Σ :=
    {| spec.mk_stack := mk_stack |}.
  Next Obligation.
    iIntros (?????? Φ) "HP HΦ". iApply (stack_works with "[HΦ] HP").
Ralf Jung's avatar
Ralf Jung committed
628
    iNext. iIntros (f f) "#[Hpop Hpush]". iApply "HΦ". iFrame "#".
629
  Qed.
630
End stack_works.