concurrent_stack3.v 6.34 KB
Newer Older
1 2 3 4 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 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 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
From iris.program_logic Require Export weakestpre hoare.
From iris.heap_lang Require Export lang proofmode notation.
From iris.algebra Require Import excl.
Set Default Proof Using "Type".

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

Section stack_works.
  Context `{!heapG Σ}.
  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.

  Theorem stack_works P Q Q' Q'' Φ :
    ( (f f : val) ι,
        ((( v vs, P (v :: vs) ={∖↑ι}= Q v  P vs) -
            (P [] ={∖↑ι}= Q'  P []) -
            WP f #() {{ v, ( (v' : val), v  SOMEV v'  Q v')  (v  NONEV  Q')}}))
         - ( (v : val),
                (( vs, P vs ={∖↑ι}= P (v :: vs)  Q'') -
                  WP f v {{ v, Q'' }}))
         - Φ (f, f)%V)%I
    - P []
    - WP mk_stack #()  {{ Φ }}.
  Proof.
    iIntros "HΦ HP".
    pose proof (nroot .@ "N") as N.
    wp_let.
    wp_alloc l as "Hl".
    iMod (inv_alloc N _ (stack_inv P #l) with "[Hl HP]") as "#Istack".
    { iNext; iExists l, (InjLV #()), []; iSplit; iFrame; auto. }
    wp_let.
    iApply "HΦ".
    - iIntros "!# Hsucc Hfail".
      iLöb as "IH".
      wp_rec.
      wp_bind (! _)%E.
      iInv N as "Hstack" "Hclose".
      iDestruct "Hstack" as (l' v' xs) "[>% [Hl' [Hstack HP]]]".
      injection H; 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.
        iMod ("Hfail" with "HP") as "[HQ' HP]".
        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 as "Hstack" "Hclose".
        iDestruct "Hstack" as (l'' v' ys) "[>% [Hl'' [Hstack HP]]]".
        injection H2; 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 H3; intros; subst.
        iDestruct ("Hsucc" with "[HP]") as "> [HQ HP]"; auto.
        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.
        iApply ("IH" with "Hsucc Hfail").
    - iIntros (v) "!# Hpush".
      iLöb as "IH".
      wp_rec.
      wp_bind (! _)%E.
      iInv N as "Hstack" "Hclose".
      iDestruct "Hstack" as (l' v' ys) "[>% [Hl' [Hstack HP]]]".
      injection H; intros; subst.
      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 as "Hstack" "Hclose".
      iDestruct "Hstack" as (l'' v'' xs) "[>% [Hl'' [Hstack HP]]]".
      injection H0; intros; subst.
      assert (Decision (v' = v''%V)) as Heq by apply val_eq_dec.
      destruct Heq.
      + wp_cas_suc.
        iDestruct ("Hpush" with "[HP]") as "> [HP HQ]"; auto.
        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").
  Qed.
End stack_works.