-
Notifications
You must be signed in to change notification settings - Fork 0
/
atomic.v
366 lines (339 loc) · 16.6 KB
/
atomic.v
1
2
3
4
5
6
7
8
9
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
From stdpp Require Import namespaces.
From iris.bi Require Import telescopes.
From iris.bi.lib Require Export atomic.
From iris.proofmode Require Import proofmode classes environments.
From iris.program_logic Require Export weakestpre.
From iris.base_logic Require Import invariants.
From iris.prelude Require Import options.
(* We need non-empty inner mask because SMR rules need their internal
invariant. (Similarly, iRC11 hardcodes inner mask to [↑histN].) To prove SMR
rules with tight masks (i.e. only SMR invariants), we need non-top "base" mask. *)
Definition atomic_wp `{!irisGS Λ Σ} {TA TB : tele}
(e: expr Λ) (* expression *)
(Eb E Ei : coPset) (* base mask, *implementation* mask, inner mask *)
(α: TA → iProp Σ) (* atomic pre-condition *)
(β: TA → TB → iProp Σ) (* atomic post-condition *)
(POST: TA → TB → iProp Σ)
(f: TA → TB → val Λ) (* Turn the return data into the return value *)
: iProp Σ :=
∀ (Φ : val Λ → iProp Σ),
(* The (outer) user mask is what is left after the implementation
opened its things. *)
atomic_update (Eb∖E) Ei α β (λ.. x y, POST x y -∗ Φ (f x y)) -∗
WP e @ Eb {{ Φ }}.
(* The way to read the [tele_app foo] here is that they convert the n-ary
function [foo] into a unary function taking a telescope as the argument. *)
Notation "'<<<' ∀∀ x1 .. xn , α '>>>' e @ Eb , E , Ei '<<<' ∃∃ y1 .. yn , β , 'RET' v '>>>'" :=
(atomic_wp (TA:=TeleS (λ x1, .. (TeleS (λ xn, TeleO)) .. ))
(TB:=TeleS (λ y1, .. (TeleS (λ yn, TeleO)) .. ))
e%E
Eb E Ei
(tele_app $ λ x1, .. (λ xn, α%I) ..)
(tele_app $ λ x1, .. (λ xn,
tele_app (λ y1, .. (λ yn, β%I) .. )
) .. )
(tele_app $ λ x1, .. (λ xn,
tele_app (λ y1, .. (λ yn, True%I) .. )
) .. )
(tele_app $ λ x1, .. (λ xn,
tele_app (λ y1, .. (λ yn, v%V) .. )
) .. )
)
(at level 20, E, α, β, v at level 200, x1 binder, xn binder, y1 binder, yn binder,
format "'[hv' '<<<' '[' ∀∀ x1 .. xn , '/' α ']' '>>>' '/ ' e @ Eb , E , Ei '/' '<<<' '[' ∃∃ y1 .. yn , '/' β , '/' 'RET' v ']' '>>>' ']'")
: bi_scope.
Notation "'<<<' ∀∀ x1 .. xn , α '>>>' e @ Eb , E , Ei '<<<' ∃∃ y1 .. yn , β , 'RET' v , POST '>>>'" :=
(atomic_wp (TA:=TeleS (λ x1, .. (TeleS (λ xn, TeleO)) .. ))
(TB:=TeleS (λ y1, .. (TeleS (λ yn, TeleO)) .. ))
e%E
Eb E Ei
(tele_app $ λ x1, .. (λ xn, α%I) ..)
(tele_app $ λ x1, .. (λ xn,
tele_app (λ y1, .. (λ yn, β%I) .. )
) .. )
(tele_app $ λ x1, .. (λ xn,
tele_app (λ y1, .. (λ yn, POST%I) .. )
) .. )
(tele_app $ λ x1, .. (λ xn,
tele_app (λ y1, .. (λ yn, v%V) .. )
) .. )
)
(at level 20, E, α, β, v at level 200, x1 binder, xn binder, y1 binder, yn binder,
format "'[hv' '<<<' '[' ∀∀ x1 .. xn , '/' α ']' '>>>' '/ ' e @ Eb , E , Ei '/' '<<<' '[' ∃∃ y1 .. yn , '/' β , '/' 'RET' v , POST ']' '>>>' ']'")
: bi_scope.
Notation "'<<<' ∀∀ x1 .. xn , α '>>>' e @ E '<<<' ∃∃ y1 .. yn , β , 'RET' v '>>>'" :=
(atomic_wp (TA:=TeleS (λ x1, .. (TeleS (λ xn, TeleO)) .. ))
(TB:=TeleS (λ y1, .. (TeleS (λ yn, TeleO)) .. ))
e%E
⊤ E ∅
(tele_app $ λ x1, .. (λ xn, α%I) ..)
(tele_app $ λ x1, .. (λ xn,
tele_app (λ y1, .. (λ yn, β%I) .. )
) .. )
(tele_app $ λ x1, .. (λ xn,
tele_app (λ y1, .. (λ yn, True%I) .. )
) .. )
(tele_app $ λ x1, .. (λ xn,
tele_app (λ y1, .. (λ yn, v%V) .. )
) .. )
)
(at level 20, E, α, β, v at level 200, x1 binder, xn binder, y1 binder, yn binder,
format "'[hv' '<<<' '[' ∀∀ x1 .. xn , '/' α ']' '>>>' '/ ' e @ E '/' '<<<' '[' ∃∃ y1 .. yn , '/' β , '/' 'RET' v ']' '>>>' ']'")
: bi_scope.
Notation "'<<<' ∀∀ x1 .. xn , α '>>>' e @ E '<<<' ∃∃ y1 .. yn , β , 'RET' v , POST '>>>'" :=
(atomic_wp (TA:=TeleS (λ x1, .. (TeleS (λ xn, TeleO)) .. ))
(TB:=TeleS (λ y1, .. (TeleS (λ yn, TeleO)) .. ))
e%E
⊤ E ∅
(tele_app $ λ x1, .. (λ xn, α%I) ..)
(tele_app $ λ x1, .. (λ xn,
tele_app (λ y1, .. (λ yn, β%I) .. )
) .. )
(tele_app $ λ x1, .. (λ xn,
tele_app (λ y1, .. (λ yn, POST%I) .. )
) .. )
(tele_app $ λ x1, .. (λ xn,
tele_app (λ y1, .. (λ yn, v%V) .. )
) .. )
)
(at level 20, E, α, β, v at level 200, x1 binder, xn binder, y1 binder, yn binder,
format "'[hv' '<<<' '[' ∀∀ x1 .. xn , '/' α ']' '>>>' '/ ' e @ E '/' '<<<' '[' ∃∃ y1 .. yn , '/' β , '/' 'RET' v , POST ']' '>>>' ']'")
: bi_scope.
Notation "'<<<' ∀∀ x1 .. xn , α '>>>' e @ Eb , E , Ei '<<<' β , 'RET' v '>>>'" :=
(atomic_wp (TA:=TeleS (λ x1, .. (TeleS (λ xn, TeleO)) .. ))
(TB:=TeleO)
e%E
Eb E Ei
(tele_app $ λ x1, .. (λ xn, α%I) ..)
(tele_app $ λ x1, .. (λ xn, tele_app β%I) .. )
(tele_app $ λ x1, .. (λ xn, tele_app True%I) .. )
(tele_app $ λ x1, .. (λ xn, tele_app v%V) .. )
)
(at level 20, E, α, β, v at level 200, x1 binder, xn binder,
format "'[hv' '<<<' '[' ∀∀ x1 .. xn , '/' α ']' '>>>' '/ ' e @ Eb , E , Ei '/' '<<<' '[' β , '/' 'RET' v ']' '>>>' ']'")
: bi_scope.
Notation "'<<<' ∀∀ x1 .. xn , α '>>>' e @ Eb , E , Ei '<<<' β , 'RET' v , POST '>>>'" :=
(atomic_wp (TA:=TeleS (λ x1, .. (TeleS (λ xn, TeleO)) .. ))
(TB:=TeleO)
e%E
Eb E Ei
(tele_app $ λ x1, .. (λ xn, α%I) ..)
(tele_app $ λ x1, .. (λ xn, tele_app β%I) .. )
(tele_app $ λ x1, .. (λ xn, tele_app POST%I) .. )
(tele_app $ λ x1, .. (λ xn, tele_app v%V) .. )
)
(at level 20, E, α, β, v at level 200, x1 binder, xn binder,
format "'[hv' '<<<' '[' ∀∀ x1 .. xn , '/' α ']' '>>>' '/ ' e @ Eb , E , Ei '/' '<<<' '[' β , '/' 'RET' v , POST ']' '>>>' ']'")
: bi_scope.
Notation "'<<<' ∀∀ x1 .. xn , α '>>>' e @ E '<<<' β , 'RET' v '>>>'" :=
(atomic_wp (TA:=TeleS (λ x1, .. (TeleS (λ xn, TeleO)) .. ))
(TB:=TeleO)
e%E
⊤ E ∅
(tele_app $ λ x1, .. (λ xn, α%I) ..)
(tele_app $ λ x1, .. (λ xn, tele_app β%I) .. )
(tele_app $ λ x1, .. (λ xn, tele_app True%I) .. )
(tele_app $ λ x1, .. (λ xn, tele_app v%V) .. )
)
(at level 20, E, α, β, v at level 200, x1 binder, xn binder,
format "'[hv' '<<<' '[' ∀∀ x1 .. xn , '/' α ']' '>>>' '/ ' e @ E '/' '<<<' '[' β , '/' 'RET' v ']' '>>>' ']'")
: bi_scope.
Notation "'<<<' ∀∀ x1 .. xn , α '>>>' e @ E '<<<' β , 'RET' v , POST '>>>'" :=
(atomic_wp (TA:=TeleS (λ x1, .. (TeleS (λ xn, TeleO)) .. ))
(TB:=TeleO)
e%E
⊤ E ∅
(tele_app $ λ x1, .. (λ xn, α%I) ..)
(tele_app $ λ x1, .. (λ xn, tele_app β%I) .. )
(tele_app $ λ x1, .. (λ xn, tele_app POST%I) .. )
(tele_app $ λ x1, .. (λ xn, tele_app v%V) .. )
)
(at level 20, E, α, β, v at level 200, x1 binder, xn binder,
format "'[hv' '<<<' '[' ∀∀ x1 .. xn , '/' α ']' '>>>' '/ ' e @ E '/' '<<<' '[' β , '/' 'RET' v , POST ']' '>>>' ']'")
: bi_scope.
Notation "'<<<' α '>>>' e @ Eb , E , Ei '<<<' ∃∃ y1 .. yn , β , 'RET' v '>>>'" :=
(atomic_wp (TA:=TeleO)
(TB:=TeleS (λ y1, .. (TeleS (λ yn, TeleO)) .. ))
e%E
Eb E Ei
(tele_app α%I)
(tele_app $ tele_app (λ y1, .. (λ yn, β%I) .. ))
(tele_app $ tele_app (λ y1, .. (λ yn, True%I) .. ))
(tele_app $ tele_app (λ y1, .. (λ yn, v%V) .. ))
)
(at level 20, E, α, β, v at level 200, y1 binder, yn binder,
format "'[hv' '<<<' '[' α ']' '>>>' '/ ' e @ Eb , E , Ei '/' '<<<' '[' ∃∃ y1 .. yn , '/' β , '/' 'RET' v ']' '>>>' ']'")
: bi_scope.
Notation "'<<<' α '>>>' e @ Eb , E , Ei '<<<' ∃∃ y1 .. yn , β , 'RET' v , POST '>>>'" :=
(atomic_wp (TA:=TeleO)
(TB:=TeleS (λ y1, .. (TeleS (λ yn, TeleO)) .. ))
e%E
Eb E Ei
(tele_app α%I)
(tele_app $ tele_app (λ y1, .. (λ yn, β%I) .. ))
(tele_app $ tele_app (λ y1, .. (λ yn, POST%I) .. ))
(tele_app $ tele_app (λ y1, .. (λ yn, v%V) .. ))
)
(at level 20, E, α, β, v at level 200, y1 binder, yn binder,
format "'[hv' '<<<' '[' α ']' '>>>' '/ ' e @ Eb , E , Ei '/' '<<<' '[' ∃∃ y1 .. yn , '/' β , '/' 'RET' v , POST ']' '>>>' ']'")
: bi_scope.
Notation "'<<<' α '>>>' e @ E '<<<' ∃∃ y1 .. yn , β , 'RET' v '>>>'" :=
(atomic_wp (TA:=TeleO)
(TB:=TeleS (λ y1, .. (TeleS (λ yn, TeleO)) .. ))
e%E
E ∅
(tele_app α%I)
(tele_app $ tele_app (λ y1, .. (λ yn, β%I) .. ))
(tele_app $ tele_app (λ y1, .. (λ yn, True%I) .. ))
(tele_app $ tele_app (λ y1, .. (λ yn, v%V) .. ))
)
(at level 20, E, α, β, v at level 200, y1 binder, yn binder,
format "'[hv' '<<<' '[' α ']' '>>>' '/ ' e @ E '/' '<<<' '[' ∃∃ y1 .. yn , '/' β , '/' 'RET' v ']' '>>>' ']'")
: bi_scope.
Notation "'<<<' α '>>>' e @ E '<<<' ∃∃ y1 .. yn , β , 'RET' v , POST '>>>'" :=
(atomic_wp (TA:=TeleO)
(TB:=TeleS (λ y1, .. (TeleS (λ yn, TeleO)) .. ))
e%E
E ∅
(tele_app α%I)
(tele_app $ tele_app (λ y1, .. (λ yn, β%I) .. ))
(tele_app $ tele_app (λ y1, .. (λ yn, POST%I) .. ))
(tele_app $ tele_app (λ y1, .. (λ yn, v%V) .. ))
)
(at level 20, E, α, β, v at level 200, y1 binder, yn binder,
format "'[hv' '<<<' '[' α ']' '>>>' '/ ' e @ E '/' '<<<' '[' ∃∃ y1 .. yn , '/' β , '/' 'RET' v , POST ']' '>>>' ']'")
: bi_scope.
Notation "'<<<' α '>>>' e @ E , Ei '<<<' β , 'RET' v '>>>'" :=
(atomic_wp (TA:=TeleO)
(TB:=TeleO)
e%E
E Ei
(tele_app α%I)
(tele_app $ tele_app β%I)
(tele_app $ tele_app True%I)
(tele_app $ tele_app v%V)
)
(at level 20, E, α, β, v at level 200,
format "'[hv' '<<<' '[' α ']' '>>>' '/ ' e @ E , Ei '/' '<<<' '[' β , '/' 'RET' v ']' '>>>' ']'")
: bi_scope.
Notation "'<<<' α '>>>' e @ E , Ei '<<<' β , 'RET' v , POST '>>>'" :=
(atomic_wp (TA:=TeleO)
(TB:=TeleO)
e%E
E Ei
(tele_app α%I)
(tele_app $ tele_app β%I)
(tele_app $ tele_app POST%I)
(tele_app $ tele_app v%V)
)
(at level 20, E, α, β, v at level 200,
format "'[hv' '<<<' '[' α ']' '>>>' '/ ' e @ E , Ei '/' '<<<' '[' β , '/' 'RET' v , POST ']' '>>>' ']'")
: bi_scope.
Notation "'<<<' α '>>>' e @ E '<<<' β , 'RET' v '>>>'" :=
(atomic_wp (TA:=TeleO)
(TB:=TeleO)
e%E
⊤ E ∅
(tele_app α%I)
(tele_app $ tele_app β%I)
(tele_app $ tele_app True%I)
(tele_app $ tele_app v%V)
)
(at level 20, E, α, β, v at level 200,
format "'[hv' '<<<' '[' α ']' '>>>' '/ ' e @ E '/' '<<<' '[' β , '/' 'RET' v ']' '>>>' ']'")
: bi_scope.
Notation "'<<<' α '>>>' e @ E '<<<' β , 'RET' v , POST '>>>'" :=
(atomic_wp (TA:=TeleO)
(TB:=TeleO)
e%E
⊤ E ∅
(tele_app α%I)
(tele_app $ tele_app β%I)
(tele_app $ tele_app POST%I)
(tele_app $ tele_app v%V)
)
(at level 20, E, α, β, v at level 200,
format "'[hv' '<<<' '[' α ']' '>>>' '/ ' e @ E '/' '<<<' '[' β , '/' 'RET' v , POST ']' '>>>' ']'")
: bi_scope.
(** Theory *)
Section lemmas.
Context `{!irisGS Λ Σ} {TA TB : tele}.
Notation iProp := (iProp Σ).
Implicit Types (α : TA → iProp) (β : TA → TB → iProp) (f : TA → TB → val Λ).
(* Atomic triples imply sequential triples. *)
Lemma atomic_wp_seq e Eb E Ei α β POST f :
Ei ⊆ Eb ∖ E →
atomic_wp e Eb E Ei α β POST f -∗
∀ Φ, ∀.. x, α x -∗ (∀.. y, β x y -∗ POST x y -∗ Φ (f x y)) -∗ WP e @ Eb {{ Φ }}.
Proof.
iIntros "% Hwp" (Φ x) "Hα HΦ".
iApply (wp_frame_wand with "HΦ"). iApply "Hwp".
iAuIntro. iAaccIntro with "Hα"; first by eauto. iIntros (y) "Hβ !>".
(* FIXME: Using ssreflect rewrite does not work, see Coq bug #7773. *)
rewrite ->!tele_app_bind. iIntros "HPOST HΦ". iSpecialize ("HΦ" with "Hβ HPOST"). iApply "HΦ".
Qed.
(** This version matches the Texan triple, i.e., with a later in front of the
[(∀.. y, β x y -∗ Φ (f x y))]. *)
Lemma atomic_wp_seq_step e Eb E Ei α β POST f :
Ei ⊆ Eb ∖ E →
TCEq (to_val e) None →
atomic_wp e Eb E Ei α β POST f -∗
∀ Φ, ∀.. x, α x -∗ ▷ (∀.. y, β x y -∗ POST x y -∗ Φ (f x y)) -∗ WP e @ Eb {{ Φ }}.
Proof.
iIntros (??) "H"; iIntros (Φ x) "Hα HΦ".
iApply (wp_step_fupd _ _ Eb _ (∀.. y : TB, β x y -∗ POST x y -∗ Φ (f x y))
with "[$HΦ //]"); first done.
iApply (atomic_wp_seq with "H Hα"); [done..|].
iIntros (y) "Hβ HPOST HΦ". iSpecialize ("HΦ" with "Hβ HPOST"). iApply "HΦ".
Qed.
(* Sequential triples with the inner mask for a physically atomic [e] are atomic. *)
Lemma atomic_seq_wp_atomic e Eb E Ei α β POST f `{!Atomic WeaklyAtomic e} :
Ei ⊆ Eb ∖ E →
(∀ Φ, ∀.. x, α x -∗ (∀.. y, β x y -∗ POST x y -∗ Φ (f x y)) -∗ WP e @ Ei {{ Φ }}) -∗
atomic_wp e Eb E Ei α β POST f.
Proof.
iIntros "% Hwp" (Φ) "AU". iMod "AU" as (x) "[Hα [_ Hclose]]".
iApply wp_mask_mono; last iApply ("Hwp" with "Hα"); [solve_ndisj|]. iIntros (y) "Hβ HPOST".
iMod ("Hclose" with "Hβ") as "HΦ".
rewrite ->!tele_app_bind. by iApply "HΦ".
Qed.
(** Sequential triples with a persistent precondition and no initial quantifier
are atomic. *)
Lemma persistent_seq_wp_atomic e Eb E Ei (α : [tele] → iProp) (β : [tele] → TB → iProp)
(POST : [tele] → TB → iProp) (f : [tele] → TB → val Λ) {HP : Persistent (α [tele_arg])} :
Ei ⊆ Eb ∖ E →
(∀ Φ, α [tele_arg] -∗ (∀.. y, β [tele_arg] y -∗ POST [tele_arg] y -∗ Φ (f [tele_arg] y)) -∗ WP e @ (Eb∖E) {{ Φ }}) -∗
atomic_wp e Eb E Ei α β POST f.
Proof.
simpl in HP. iIntros "% Hwp" (Φ) "HΦ". iApply fupd_wp.
iMod ("HΦ") as "[#Hα [Hclose _]]". iMod ("Hclose" with "Hα") as "HΦ".
iApply wp_fupd. iApply wp_mask_mono; last iApply ("Hwp" with "Hα"); [solve_ndisj|]. iIntros "!>" (y) "Hβ HPOST".
iMod ("HΦ") as "[_ [_ Hclose]]". iMod ("Hclose" with "Hβ") as "HΦ".
(* FIXME: Using ssreflect rewrite does not work, see Coq bug #7773. *)
rewrite ->!tele_app_bind. by iApply "HΦ".
Qed.
Lemma atomic_wp_mask_weaken e Eb E1 E2 Ei α β POST f :
E1 ⊆ E2 → atomic_wp e Eb E1 Ei α β POST f -∗ atomic_wp e Eb E2 Ei α β POST f.
Proof.
iIntros (HE) "Hwp". iIntros (Φ) "AU". iApply "Hwp".
iApply atomic_update_mask_weaken; last done. set_solver.
Qed.
(** We can open invariants around atomic triples.
(Just for demonstration purposes; we always use [iInv] in proofs.) *)
Lemma atomic_wp_inv e Eb E Ei α β POST f N I :
↑N ⊆ Eb →
↑N ⊆ E →
Ei ⊆ Eb ∖ E →
atomic_wp e Eb (E ∖ ↑N) Ei (λ.. x, ▷ I ∗ α x) (λ.. x y, ▷ I ∗ β x y) POST f -∗
inv N I -∗ atomic_wp e Eb E Ei α β POST f.
Proof.
intros ???. iIntros "Hwp #Hinv" (Φ) "AU". iApply "Hwp". iAuIntro.
iInv N as "HI". iApply (aacc_aupd with "AU"); first solve_ndisj.
iIntros (x) "Hα". iAaccIntro with "[HI Hα]"; rewrite ->!tele_app_bind; first by iFrame.
- (* abort *)
iIntros "[HI $]". by eauto with iFrame.
- (* commit *)
iIntros (y). rewrite ->!tele_app_bind. iIntros "[HI Hβ]". iRight.
iExists y. rewrite ->!tele_app_bind. by eauto with iFrame.
Qed.
End lemmas.