one_shot.v 4 KB
Newer Older
1
From iris.algebra Require Import dec_agree csum.
2
From iris.program_logic Require Import hoare.
3
From iris.heap_lang Require Import assert proofmode notation.
Robbert Krebbers's avatar
Robbert Krebbers committed
4
From iris.proofmode Require Import invariants ghost_ownership.
5 6 7
Import uPred.

Definition one_shot_example : val := λ: <>,
8
  let: "x" := ref NONE in (
9
  (* tryset *) (λ: "n",
10
    CAS "x" NONE (SOME "n")),
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"
19 20
       end
    end)).
21
Global Opaque one_shot_example.
22

23 24 25 26 27 28
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).

Class one_shotG Σ := one_shot_inG :> inG heap_lang Σ one_shotR.
Definition one_shotGF : gFunctorList := [GFunctor (constRF one_shotR)].
29
Instance inGF_one_shotG Σ : inGFs heap_lang Σ one_shotGF  one_shotG Σ.
30 31
Proof. intros [? _]; apply: inGF_inG. Qed.

32
Section proof.
33
Context `{!heapG Σ, !one_shotG Σ} (N : namespace) (HN : heapN  N).
34 35 36
Local Notation iProp := (iPropG heap_lang Σ).

Definition one_shot_inv (γ : gname) (l : loc) : iProp :=
37
  (l  NONEV  own γ Pending   n : Z, l  SOMEV #n  own γ (Shot n))%I.
38 39

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

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