one_shot.v 4.15 KB
Newer Older
1 2
From iris.program_logic Require Export weakestpre hoare.
From iris.heap_lang Require Export lang.
3
From iris.algebra Require Import excl dec_agree csum.
4
From iris.heap_lang Require Import assert proofmode notation.
5
From iris.proofmode Require Import tactics.
Ralf Jung's avatar
Ralf Jung committed
6 7

Definition one_shot_example : val := λ: <>,
8
  let: "x" := ref NONE in (
Ralf Jung's avatar
Ralf Jung committed
9
  (* tryset *) (λ: "n",
10
    CAS "x" NONE (SOME "n")),
Ralf Jung's avatar
Ralf Jung committed
11
  (* check  *) (λ: <>,
12 13
    let: "y" := !"x" in λ: <>,
    match: "y" with
14 15
      NONE => #()
    | SOME "n" =>
16
       match: !"x" with
17 18
         NONE => assert: #false
       | SOME "m" => assert: "n" = "m"
Ralf Jung's avatar
Ralf Jung committed
19 20
       end
    end)).
21
Global Opaque one_shot_example.
Ralf Jung's avatar
Ralf Jung committed
22

23 24 25 26
Definition one_shotR := csumR (exclR unitC) (dec_agreeR Z).
Definition Pending : one_shotR := (Cinl (Excl ()) : one_shotR).
Definition Shot (n : Z) : one_shotR := (Cinr (DecAgree n) : one_shotR).

27 28 29 30
Class one_shotG Σ := { one_shot_inG :> inG Σ one_shotR }.
Definition one_shotΣ : gFunctors := #[GFunctor (constRF one_shotR)].
Instance subG_one_shotΣ {Σ} : subG one_shotΣ Σ  one_shotG Σ.
Proof. intros [?%subG_inG _]%subG_inv. split; apply _. Qed.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
31

Ralf Jung's avatar
Ralf Jung committed
32
Section proof.
33
Context `{!heapG Σ, !one_shotG Σ} (N : namespace) (HN : heapN  N).
Ralf Jung's avatar
Ralf Jung committed
34

35
Definition one_shot_inv (γ : gname) (l : loc) : iProp Σ :=
36
  (l  NONEV  own γ Pending   n : Z, l  SOMEV #n  own γ (Shot n))%I.
Ralf Jung's avatar
Ralf Jung committed
37

38
Lemma wp_one_shot (Φ : val  iProp Σ) :
39 40 41
  heap_ctx  ( f1 f2 : val,
    ( n : Z,  WP f1 #n {{ w, w = #true  w = #false }}) 
     WP f2 #() {{ g,  WP g #() {{ _, True }} }} - Φ (f1,f2)%V)
Ralf Jung's avatar
Ralf Jung committed
42 43
   WP one_shot_example #() {{ Φ }}.
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
44
  iIntros "[#? Hf] /=".
45
  rewrite -wp_fupd /one_shot_example /=. wp_seq. wp_alloc l as "Hl". wp_let.
46 47
  iMod (own_alloc Pending) as (γ) "Hγ"; first done.
  iMod (inv_alloc N _ (one_shot_inv γ l) with "[Hl Hγ]") as "#HN".
Robbert Krebbers's avatar
Robbert Krebbers committed
48
  { iNext. iLeft. by iSplitL "Hl". }
49
  iModIntro. iApply "Hf"; iSplit.
Robbert Krebbers's avatar
Robbert Krebbers committed
50 51
  - iIntros (n) "!#". wp_let.
    iInv N as ">[[Hl Hγ]|H]" "Hclose"; last iDestruct "H" as (m) "[Hl Hγ]".
52
    + wp_cas_suc. iMod (own_update with "Hγ") as "Hγ".
53
      { by apply cmra_update_exclusive with (y:=Shot n). }
54
      iMod ("Hclose" with "[-]"); last eauto.
55
      iNext; iRight; iExists n; by iFrame.
56
    + wp_cas_fail. iMod ("Hclose" with "[-]"); last eauto.
57
      rewrite /one_shot_inv; eauto 10.
58
  - iIntros "!#". wp_seq. wp_bind (! _)%E.
Robbert Krebbers's avatar
Robbert Krebbers committed
59
    iInv N as ">Hγ" "Hclose".
60 61
    iAssert ( v, l  v  ((v = NONEV  own γ Pending) 
        n : Z, v = SOMEV #n  own γ (Shot n)))%I with "[Hγ]" as "Hv".
62
    { iDestruct "Hγ" as "[[Hl Hγ]|Hl]"; last iDestruct "Hl" as (m) "[Hl Hγ]".
63 64
      + iExists NONEV. iFrame. eauto.
      + iExists (SOMEV #m). iFrame. eauto. }
65
    iDestruct "Hv" as (v) "[Hl Hv]". wp_load.
66 67
    iAssert (one_shot_inv γ l  (v = NONEV   n : Z,
      v = SOMEV #n  own γ (Shot n)))%I with "[Hl Hv]" as "[Hinv #Hv]".
68
    { iDestruct "Hv" as "[[% ?]|Hv]"; last iDestruct "Hv" as (m) "[% ?]"; subst.
69 70
      + iSplit. iLeft; by iSplitL "Hl". eauto.
      + iSplit. iRight; iExists m; by iSplitL "Hl". eauto. }
71
    iMod ("Hclose" with "[Hinv]") as "_"; eauto; iModIntro.
72
    wp_let. iIntros "!#". wp_seq.
73
    iDestruct "Hv" as "[%|Hv]"; last iDestruct "Hv" as (m) "[% Hγ']"; subst.
74
    { by wp_match. }
75
    wp_match. wp_bind (! _)%E.
Robbert Krebbers's avatar
Robbert Krebbers committed
76
    iInv N as ">[[Hl Hγ]|H]" "Hclose"; last iDestruct "H" as (m') "[Hl Hγ]".
77
    { iCombine "Hγ" "Hγ'" as "Hγ". by iDestruct (own_valid with "Hγ") as %?. }
78
    wp_load.
Robbert Krebbers's avatar
Robbert Krebbers committed
79
    iCombine "Hγ" "Hγ'" as "Hγ".
80
    iDestruct (own_valid with "Hγ") as %[=->]%dec_agree_op_inv.
81
    iMod ("Hclose" with "[Hl]") as "_".
82
    { iNext; iRight; by eauto. }
83
    iModIntro. wp_match. iApply wp_assert. wp_op=>?; simplify_eq/=; eauto.
Ralf Jung's avatar
Ralf Jung committed
84 85
Qed.

86
Lemma ht_one_shot (Φ : val  iProp Σ) :
87
  heap_ctx  {{ True }} one_shot_example #()
88
    {{ ff,
89
      ( n : Z, {{ True }} Fst ff #n {{ w, w = #true  w = #false }}) 
90
      {{ True }} Snd ff #() {{ g, {{ True }} g #() {{ _, True }} }}
Ralf Jung's avatar
Ralf Jung committed
91 92
    }}.
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
93
  iIntros "#? !# _". iApply wp_one_shot. iSplit; first done.
94
  iIntros (f1 f2) "[#Hf1 #Hf2]"; iSplit.
Robbert Krebbers's avatar
Robbert Krebbers committed
95 96
  - iIntros (n) "!# _". wp_proj. iApply "Hf1".
  - iIntros "!# _". wp_proj.
97
    iApply (wp_wand_r with "[- $Hf2]"). by iIntros (v) "#? !# _".
Ralf Jung's avatar
Ralf Jung committed
98 99
Qed.
End proof.