proof.v 8.87 KB
Newer Older
1
2
From iris.prelude Require Import functions.
From iris.algebra Require Import upred_big_op.
Robbert Krebbers's avatar
Robbert Krebbers committed
3
4
5
From iris.program_logic Require Import saved_prop.
From iris.heap_lang Require Import proofmode.
From iris.proofmode Require Import sts.
Ralf Jung's avatar
Ralf Jung committed
6
7
From iris.heap_lang.lib.barrier Require Export barrier.
From iris.heap_lang.lib.barrier Require Import protocol.
8
9
Import uPred.

10
(** The CMRAs we need. *)
11
12
13
(* Not bundling heapG, as it may be shared with other users. *)
Class barrierG Σ := BarrierG {
  barrier_stsG :> stsG heap_lang Σ sts;
14
  barrier_savedPropG :> savedPropG heap_lang Σ idCF;
15
}.
16
(** The Functors we need. *)
17
Definition barrierGF : gFunctorList := [stsGF sts; savedPropGF idCF].
18
19
20
(* Show and register that they match. *)
Instance inGF_barrierG `{H : inGFs heap_lang Σ barrierGF} : barrierG Σ.
Proof. destruct H as (?&?&?). split; apply _. Qed.
21
22
23

(** Now we come to the Iris part of the proof. *)
Section proof.
24
Context `{!heapG Σ, !barrierG Σ} (N : namespace).
Robbert Krebbers's avatar
Robbert Krebbers committed
25
Implicit Types I : gset gname.
26
27
Local Notation iProp := (iPropG heap_lang Σ).

28
Definition ress (P : iProp) (I : gset gname) : iProp :=
29
  ( Ψ : gname  iProp,
30
     (P - [ set] i  I, Ψ i)  [ set] i  I, saved_prop_own i (Ψ i))%I.
31
32

Coercion state_to_val (s : state) : val :=
33
  match s with State Low _ => #0 | State High _ => #1 end.
Robbert Krebbers's avatar
Robbert Krebbers committed
34
Arguments state_to_val !_ / : simpl nomatch.
35

36
37
Definition state_to_prop (s : state) (P : iProp) : iProp :=
  match s with State Low _ => P | State High _ => True%I end.
Robbert Krebbers's avatar
Robbert Krebbers committed
38
Arguments state_to_prop !_ _ / : simpl nomatch.
39

40
Definition barrier_inv (l : loc) (P : iProp) (s : state) : iProp :=
41
  (l  s  ress (state_to_prop s P) (state_I s))%I.
42
43

Definition barrier_ctx (γ : gname) (l : loc) (P : iProp) : iProp :=
44
  ( (heapN  N)  heap_ctx  sts_ctx γ N (barrier_inv l P))%I.
45
46
47
48
49
50
51

Definition send (l : loc) (P : iProp) : iProp :=
  ( γ, barrier_ctx γ l P  sts_ownS γ low_states {[ Send ]})%I.

Definition recv (l : loc) (R : iProp) : iProp :=
  ( γ P Q i,
    barrier_ctx γ l P  sts_ownS γ (i_states i) {[ Change i ]} 
52
    saved_prop_own i Q   (Q - R))%I.
53

54
Global Instance barrier_ctx_persistent (γ : gname) (l : loc) (P : iProp) :
55
  PersistentP (barrier_ctx γ l P).
56
57
58
59
Proof. apply _. Qed.

Typeclasses Opaque barrier_ctx send recv.

60
(** Setoids *)
61
62
63
64
Global Instance ress_ne n : Proper (dist n ==> (=) ==> dist n) ress.
Proof. solve_proper. Qed.
Global Instance state_to_prop_ne n s :
  Proper (dist n ==> dist n) (state_to_prop s).
65
Proof. solve_proper. Qed.
66
Global Instance barrier_inv_ne n l :
67
68
  Proper (dist n ==> eq ==> dist n) (barrier_inv l).
Proof. solve_proper. Qed.
69
Global Instance barrier_ctx_ne n γ l : Proper (dist n ==> dist n) (barrier_ctx γ l).
70
Proof. solve_proper. Qed. 
71
Global Instance send_ne n l : Proper (dist n ==> dist n) (send l).
72
Proof. solve_proper. Qed.
73
Global Instance recv_ne n l : Proper (dist n ==> dist n) (recv l).
74
Proof. solve_proper. Qed.
75
76

(** Helper lemmas *)
77
Lemma ress_split i i1 i2 Q R1 R2 P I :
78
  i  I  i1  I  i2  I  i1  i2 
79
80
  saved_prop_own i Q  saved_prop_own i1 R1  saved_prop_own i2 R2 
    (Q - R1  R2)  ress P I
81
   ress P ({[i1;i2]}  I  {[i]}).
82
Proof.
83
  iIntros (????) "(#HQ&#H1&#H2&HQR&H)"; iDestruct "H" as (Ψ) "[HPΨ HΨ]".
84
  iDestruct (big_sepS_delete _ _ i with "HΨ") as "[#HΨi HΨ]"; first done.
85
  iExists (fn_insert i1 R1 (fn_insert i2 R2 Ψ)). iSplitL "HQR HPΨ".
86
  - iPoseProof (saved_prop_agree i Q (Ψ i) with "[#]") as "Heq"; first by iSplit.
87
88
89
    iNext. iRewrite "Heq" in "HQR". iIntros "HP". iSpecialize ("HPΨ" with "HP").
    iDestruct (big_sepS_delete _ _ i with "HPΨ") as "[HΨ HPΨ]"; first done.
    iDestruct ("HQR" with "HΨ") as "[HR1 HR2]".
90
    rewrite -assoc_L !big_sepS_fn_insert'; [|abstract set_solver ..].
91
    by iFrame.
92
  - rewrite -assoc_L !big_sepS_fn_insert; [|abstract set_solver ..]. eauto.
93
Qed.
94
95

(** Actual proofs *)
Ralf Jung's avatar
Ralf Jung committed
96
Lemma newbarrier_spec (P : iProp) (Φ : val  iProp) :
97
  heapN  N 
98
  heap_ctx  ( l, recv l P  send l P - Φ #l)  WP newbarrier #() {{ Φ }}.
99
Proof.
100
  iIntros (HN) "[#? HΦ]".
101
  rewrite /newbarrier. wp_seq. wp_alloc l as "Hl".
Robbert Krebbers's avatar
Robbert Krebbers committed
102
  iApply "HΦ".
103
  iPvs (saved_prop_alloc (F:=idCF) _ P) as (γ) "#?".
104
  iPvs (sts_alloc (barrier_inv l P) _ N (State Low {[ γ ]}) with "[-]")
105
    as (γ') "[#? Hγ']"; eauto.
106
  { iNext. rewrite /barrier_inv /=. iFrame.
107
    iExists (const P). rewrite !big_sepS_singleton /=. eauto. }
Robbert Krebbers's avatar
Robbert Krebbers committed
108
109
  iAssert (barrier_ctx γ' l P)%I as "#?".
  { rewrite /barrier_ctx. by repeat iSplit. }
110
  iAssert (sts_ownS γ' (i_states γ) {[Change γ]}
111
     sts_ownS γ' low_states {[Send]})%I with "|==>[-]" as "[Hr Hs]".
Robbert Krebbers's avatar
Robbert Krebbers committed
112
  { iApply sts_ownS_op; eauto using i_states_closed, low_states_closed.
113
    + set_solver.
114
    + iApply (sts_own_weaken with "Hγ'");
Robbert Krebbers's avatar
Robbert Krebbers committed
115
        auto using sts.closed_op, i_states_closed, low_states_closed;
116
        abstract set_solver. }
Robbert Krebbers's avatar
Robbert Krebbers committed
117
  iPvsIntro. rewrite /recv /send. iSplitL "Hr".
118
  - iExists γ', P, P, γ. iFrame. auto.
119
  - auto.
120
121
122
Qed.

Lemma signal_spec l P (Φ : val  iProp) :
123
  send l P  P  Φ #()  WP signal #l {{ Φ }}.
124
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
125
  rewrite /signal /send /barrier_ctx.
126
  iIntros "(Hs&HP&HΦ)"; iDestruct "Hs" as (γ) "[#(%&Hh&Hsts) Hγ]". wp_let.
Robbert Krebbers's avatar
Robbert Krebbers committed
127
  iSts γ as [p I]; iDestruct "Hγ" as "[Hl Hr]".
128
  wp_store. iPvsIntro. destruct p; [|done].
Robbert Krebbers's avatar
Robbert Krebbers committed
129
130
  iExists (State High I), ( : set token).
  iSplit; [iPureIntro; by eauto using signal_step|].
131
  iSplitR "HΦ"; [iNext|by auto].
Robbert Krebbers's avatar
Robbert Krebbers committed
132
  rewrite {2}/barrier_inv /ress /=; iFrame "Hl".
133
  iDestruct "Hr" as (Ψ) "[Hr Hsp]"; iExists Ψ; iFrame "Hsp".
Robbert Krebbers's avatar
Robbert Krebbers committed
134
  iIntros "> _"; by iApply "Hr".
135
136
137
Qed.

Lemma wait_spec l P (Φ : val  iProp) :
138
  recv l P  (P - Φ #())  WP wait #l {{ Φ }}.
139
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
140
  rename P into R; rewrite /recv /barrier_ctx.
141
  iIntros "[Hr HΦ]"; iDestruct "Hr" as (γ P Q i) "(#(%&Hh&Hsts)&Hγ&#HQ&HQR)".
142
  iLöb as "IH". wp_rec. wp_focus (! _)%E.
Robbert Krebbers's avatar
Robbert Krebbers committed
143
  iSts γ as [p I]; iDestruct "Hγ" as "[Hl Hr]".
144
  wp_load. iPvsIntro. destruct p.
Robbert Krebbers's avatar
Robbert Krebbers committed
145
146
  - (* a Low state. The comparison fails, and we recurse. *)
    iExists (State Low I), {[ Change i ]}; iSplit; [done|iSplitL "Hl Hr"].
147
    { iNext. rewrite {2}/barrier_inv /=. by iFrame. }
Robbert Krebbers's avatar
Robbert Krebbers committed
148
    iIntros "Hγ".
149
    iAssert (sts_ownS γ (i_states i) {[Change i]})%I with "|==>[Hγ]" as "Hγ".
150
    { iApply (sts_own_weaken with "Hγ"); eauto using i_states_closed. }
151
    wp_op=> ?; simplify_eq; wp_if. iApply ("IH" with "Hγ [HQR] HΦ"). auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
152
153
154
155
  - (* a High state: the comparison succeeds, and we perform a transition and
    return to the client *)
    iExists (State High (I  {[ i ]})), ( : set token).
    iSplit; [iPureIntro; by eauto using wait_step|].
156
    iDestruct "Hr" as (Ψ) "[HΨ Hsp]".
157
    iDestruct (big_sepS_delete _ _ i with "Hsp") as "[#HΨi Hsp]"; first done.
158
    iAssert ( Ψ i   [ set] j  I  {[i]}, Ψ j)%I with "[HΨ]" as "[HΨ HΨ']".
Robbert Krebbers's avatar
Robbert Krebbers committed
159
160
161
    { iNext. iApply (big_sepS_delete _ _ i); first done. by iApply "HΨ". }
    iSplitL "HΨ' Hl Hsp"; [iNext|].
    + rewrite {2}/barrier_inv /=; iFrame "Hl".
162
      iExists Ψ; iFrame. auto.
163
    + iPoseProof (saved_prop_agree i Q (Ψ i) with "[#]") as "Heq"; first by auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
164
165
      iIntros "_". wp_op=> ?; simplify_eq/=; wp_if.
      iPvsIntro. iApply "HΦ". iApply "HQR". by iRewrite "Heq".
166
167
Qed.

168
Lemma recv_split E l P1 P2 :
169
  nclose N  E  recv l (P1  P2) ={E}=> recv l P1  recv l P2.
170
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
171
  rename P1 into R1; rename P2 into R2. rewrite {1}/recv /barrier_ctx.
172
  iIntros (?). iDestruct 1 as (γ P Q i) "(#(%&Hh&Hsts)&Hγ&#HQ&HQR)".
Robbert Krebbers's avatar
Robbert Krebbers committed
173
174
  iApply pvs_trans'.
  iSts γ as [p I]; iDestruct "Hγ" as "[Hl Hr]".
175
  iPvs (saved_prop_alloc_strong _ (R1: %CF iProp) I) as (i1) "[% #Hi1]".
Robbert Krebbers's avatar
Robbert Krebbers committed
176
  iPvs (saved_prop_alloc_strong _ (R2: %CF iProp) (I  {[i1]}))
177
    as (i2) "[Hi2' #Hi2]"; iDestruct "Hi2'" as %Hi2; iPvsIntro.
Robbert Krebbers's avatar
Robbert Krebbers committed
178
  rewrite ->not_elem_of_union, elem_of_singleton in Hi2; destruct Hi2.
179
180
  iExists (State p ({[i1; i2]}  I  {[i]})).
  iExists ({[Change i1; Change i2 ]}).
Robbert Krebbers's avatar
Robbert Krebbers committed
181
182
  iSplit; [by eauto using split_step|iSplitL].
  - iNext. rewrite {2}/barrier_inv /=. iFrame "Hl".
183
    iApply (ress_split _ _ _ Q R1 R2); eauto. iFrame; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
184
  - iIntros "Hγ".
185
    iAssert (sts_ownS γ (i_states i1) {[Change i1]}
186
       sts_ownS γ (i_states i2) {[Change i2]})%I with "|==>[-]" as "[Hγ1 Hγ2]".
Robbert Krebbers's avatar
Robbert Krebbers committed
187
188
    { iApply sts_ownS_op; eauto using i_states_closed, low_states_closed.
      + set_solver.
189
190
      + iApply (sts_own_weaken with "Hγ");
          eauto using sts.closed_op, i_states_closed.
191
        abstract set_solver. }
Robbert Krebbers's avatar
Robbert Krebbers committed
192
    iPvsIntro; iSplitL "Hγ1"; rewrite /recv /barrier_ctx.
193
194
    + iExists γ, P, R1, i1. iFrame; auto.
    + iExists γ, P, R2, i2. iFrame; auto.
195
196
Qed.

197
Lemma recv_weaken l P1 P2 : (P1 - P2)  recv l P1 - recv l P2.
198
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
199
  rewrite /recv.
200
  iIntros "HP HP1"; iDestruct "HP1" as (γ P Q i) "(#Hctx&Hγ&Hi&HP1)".
201
  iExists γ, P, Q, i. iFrame "Hctx Hγ Hi".
Robbert Krebbers's avatar
Robbert Krebbers committed
202
  iIntros "> HQ". by iApply "HP"; iApply "HP1".
203
Qed.
204

205
Lemma recv_mono l P1 P2 : (P1  P2)  recv l P1  recv l P2.
206
Proof.
207
  intros HP%entails_wand. apply wand_entails. rewrite HP. apply recv_weaken.
208
Qed.
209
End proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
210
211

Typeclasses Opaque barrier_ctx send recv.