one_shot.v 3.94 KB
Newer Older
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
1
From iris.algebra Require Import dec_agree csum.
Ralf Jung's avatar
Ralf Jung committed
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.
Ralf Jung's avatar
Ralf Jung committed
5 6 7
Import uPred.

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

Class one_shotG Σ :=
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
24
  one_shot_inG :> inG heap_lang Σ (csumR (exclR unitC)(dec_agreeR Z)).
Ralf Jung's avatar
Ralf Jung committed
25
Definition one_shotGF : gFunctorList :=
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
26
  [GFunctor (constRF (csumR (exclR unitC)(dec_agreeR Z)))].
Ralf Jung's avatar
Ralf Jung committed
27
Instance inGF_one_shotG Σ : inGFs heap_lang Σ one_shotGF  one_shotG Σ.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
28 29 30
Proof. intros [? _]; apply: inGF_inG. Qed.

Notation Pending := (Cinl (Excl ())).
Ralf Jung's avatar
Ralf Jung committed
31 32

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

Definition one_shot_inv (γ : gname) (l : loc) : iProp :=
37 38
  (l  NONEV  own γ Pending 
   n : Z, l  SOMEV #n  own γ (Cinr (DecAgree n)))%I.
Ralf Jung's avatar
Ralf Jung committed
39 40

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

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