helping.v 7.31 KB
Newer Older
Dan Frumin's avatar
Dan Frumin committed
1 2 3 4 5 6 7
(* Stack with helping *)
From iris.proofmode Require Import tactics.
From iris_logrel Require Export logrel examples.stack.mailbox.

Definition LIST τ :=
  TRec (TSum TUnit (TProd τ.[ren (+1)] (TVar 0))).

Dan Frumin's avatar
Dan Frumin committed
8 9
Notation Conse h t := (Fold (SOME (Pair h t))).
Notation Nile := (Fold NONE).
Dan Frumin's avatar
Dan Frumin committed
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

Definition pop_st : val := λ: "r" "get", rec: "pop" <> :=
  match: "get" #() with
    NONE =>
    (match: Unfold !"r" with
       NONE => NONE
     | SOME "hd" =>
       if: CAS "r" (Fold (SOME "hd")) (Snd "hd")
       then SOME (Fst "hd")
       else "pop" #()
     end)
  | SOME "x" => SOME "x"
  end.

Definition push_st : val := λ: "r" "put", rec: "push" "n" :=
  match: "put" "n" with
    NONE => #()
  | SOME "n" =>
    let: "r'" := !"r" in
    let: "r''" := Fold (SOME ("n", "r'")) in
    if: CAS "r" "r'" "r''"
    then #()
    else "push" "n"
  end.

Definition mk_stack : val :=  λ: "_",
  Unpack mailbox $ λ: "M",
  let: "new_mb" := Fst (Fst "M") in
  let: "put" := Snd (Fst "M") in
  let: "get" := Snd "M" in
  let: "mb" := "new_mb" #() in
  let: "r" := ref (Fold NONE) in
  (pop_st "r" (λ: <>, "get" "mb"),
   push_st "r" (λ: "n", "put" "mb" "n")).

Section stack_works.
  Context `{!heapG Σ}.
  Implicit Types l : loc.

  Definition is_stack_pre (P : val  iProp Σ) (F : val -c> iProp Σ) :
     val -c> iProp Σ := λ v,
    (v  FoldV NONEV   (h t : val), v  FoldV (SOMEV (h, t))%V  P h   F t)%I.

  Local Instance is_stack_contr (P : val  iProp Σ): Contractive (is_stack_pre P).
  Proof.
    rewrite /is_stack_pre  => n f f' Hf v.
    repeat (f_contractive || f_equiv).
    apply Hf.
  Qed.

  Definition is_stack_def (P : val -> iProp Σ) := fixpoint (is_stack_pre P).
  Definition is_stack_aux P : seal (@is_stack_def P). by eexists. Qed.
  Definition is_stack P := unseal (is_stack_aux P).
  Definition is_stack_eq P : @is_stack P = @is_stack_def P := seal_eq (is_stack_aux P).

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

  Lemma is_stack_unfold (P : val  iProp Σ) v :
      is_stack P v ⊣⊢ is_stack_pre P (is_stack P) v.
  Proof.
    rewrite is_stack_eq. apply (fixpoint_unfold (is_stack_pre P)).
  Qed.

  Lemma is_stack_disj (P : val  iProp Σ) v :
      is_stack P v - is_stack P v  (v  FoldV NONEV   (h t : val), v  FoldV (SOMEV (h, t))%V).
  Proof.
    iIntros "Hstack".
    iDestruct (is_stack_unfold with "Hstack") as "[#Hstack|Hstack]".
    - iSplit; try iApply is_stack_unfold; iLeft; auto.
    - iDestruct "Hstack" as (h t) "[#Heq rest]".
      iSplitL; try iApply is_stack_unfold; iRight; auto.
  Qed.

  Theorem stack_works {channelG0 : channelG Σ} P Φ :
    ( (f f : val),
            ( WP f #() {{ v, ( (v' : val), v  SOMEV v'  P v')   v  NONEV }})
         - ( (v : val),  (P v - WP f v {{ v, True }}))
         - Φ (f, f)%V)%I
    - WP mk_stack #()  {{ Φ }}.
  Proof.
    iIntros "HΦ".
    unlock mk_stack.
    wp_rec.
    wp_pack.
    wp_let.
    repeat wp_proj; wp_let.
    repeat wp_proj; wp_let.
    repeat wp_proj; wp_let.
    wp_bind (new_mb _).
    iApply (new_mb_works P).
    iIntros (mb Nmb) "#Hinv".
    wp_let.
    wp_alloc r as "Hr".
    wp_let.
    pose proof (nroot .@ "N") as N.
    iMod (inv_alloc N _ (stack_inv P #r) with "[Hr]") as "#Hisstack".
    { iExists r, (FoldV NONEV); iFrame; iSplit; auto. iApply is_stack_unfold; iLeft; done. }
    unlock pop_st. do 2 wp_rec.
    unlock push_st. do 2 wp_rec.
    iApply wp_value.
    (* TODO: solve_to_val. either doesn't work or too slow *)
    { simpl. rewrite ?decide_left.
      simpl. done. }
    iApply "HΦ".
    (* The verification of pop *)
    - iIntros "!#".
      iLöb as "IH".
      wp_rec.
      wp_rec.
      wp_bind (get_mail _).
      iApply (get_mail_works P with "Hinv").
      iIntros (v) "Hv".
      iDestruct "Hv" as "[H | H]".
      + iDestruct "H" as (v') "[% HP]".
        subst.
        simpl. wp_case_inr. (* TODO: we require a simpl here *)
        wp_let.
        wp_value.
        iLeft; iExists v'; auto.
      + iDestruct "H" as "%"; subst.
        wp_case.
        wp_seq.
        wp_bind (! #r)%E.
        iInv N as "Hstack" "Hclose".
        iDestruct "Hstack" as (l'' v'') "[>% [Hl' Hstack]]". simplify_eq/=.
        wp_load.
        iDestruct (is_stack_disj with "Hstack") as "[Hstack #Heq]".
        iMod ("Hclose" with "[Hl' Hstack]").
        { iExists l'', v''; iFrame; auto. }
        iModIntro.
        iDestruct "Heq" as "[H | H]".
        * iRewrite "H".
          wp_unfold.
          wp_case. wp_seq. wp_value.          
          iRight; auto.
        * iDestruct "H" as (h t) "H"; iRewrite "H".
          simpl. wp_unfold.
          simpl. wp_case_inr. (* TODO: same comment *)
          wp_let.
          wp_proj.
          wp_bind (CAS _ _ _).
          iInv N as "Hstack" "Hclose".
          iDestruct "Hstack" as (l''' v''') "[>% [Hl'' Hstack]]". simplify_eq/=.
          destruct (decide (v''' = FoldV (InjRV (h, t))%V)) as [Heq | Heq]; subst.
          ++ (* If nothing has changed, the cas succeeds *)
            wp_cas_suc.
            iDestruct (is_stack_unfold with "Hstack") as "[Hstack | Hstack]".
            { iDestruct "Hstack" as "%"; discriminate. }
            iDestruct "Hstack" as (h' t') "[% [HP Hstack]]". simplify_eq/=.
            iMod ("Hclose" with "[Hl'' Hstack]").
            { iExists l''', t'; iFrame; auto. }
            iModIntro.
            wp_if. 
            wp_proj. 
            wp_value. iLeft; auto.
          ++ (* The case in which we fail *)
            wp_cas_fail.
            iMod ("Hclose" with "[Hl'' Hstack]").
            iExists l''', v'''; iFrame; auto.
            iModIntro.
            wp_if.
            (* Now we use our IH to loop *)
            iApply "IH".
    (* The verification of push. This is actually markedly simpler. *)
    - iIntros (v) "!# HP /=".
      (* We grab an IH to be used in the case that we loop *)
      iLöb as "IH" forall (v).
      wp_rec.
      wp_rec.
      wp_bind (put_mail _ _).
      iApply (put_mail_works P with "Hinv HP").
      iIntros (v') "Hv'".
      iDestruct "Hv'" as "[H | H]".
      * iDestruct "H" as (v'') "[% HP]"; subst.
        simpl. wp_case. (* TODO: same comment here *)
        wp_let.
        wp_bind (! _)%E.
        iInv N as "Hstack" "Hclose".
        iDestruct "Hstack" as (l'' v') "[>% [Hl' Hstack]]". simplify_eq/=.
        wp_load.
        iMod ("Hclose" with "[Hl' Hstack]").
        { by (iExists l'', v'; iFrame). }
        iModIntro.
        do 2 wp_let.
        wp_bind (CAS _ _ _).
        iInv N as "Hstack" "Hclose".
        iDestruct "Hstack" as (l''' v''') "[>% [Hl'' Hstack]]". simplify_eq/=.
        destruct (decide (v''' = v'%V)) as [Heq|Heq]; subst.
        + wp_cas_suc.
          iMod ("Hclose" with "[Hl'' HP Hstack]").
          { iExists l''', (FoldV (InjRV (v'', v')%V)).
            iSplit; iFrame; auto.
            iApply is_stack_unfold; iRight.
            iExists v'', v'; iFrame. eauto. }
          iModIntro.
          wp_if.
          by wp_value.
        + wp_cas_fail.
          iMod ("Hclose" with "[Hl'' Hstack]").
          iExists l''', v'''; iFrame; auto.
          iModIntro.
          wp_if.
          iApply ("IH" with "HP").
      * (* This is the case that our sidechannel offer succeeded *)
        iDestruct "H" as "%"; subst.
        wp_case.
        wp_seq.
        by wp_value.
  Qed.
End stack_works.