-
Notifications
You must be signed in to change notification settings - Fork 18
/
structured_conc.v
458 lines (403 loc) · 13.9 KB
/
structured_conc.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
From iris.algebra Require Import excl.
From iris.base_logic.lib Require Export invariants token.
From iris.heap_lang Require Import lang proofmode notation.
(* ################################################################# *)
(** * Structured Concurrency *)
(* ================================================================= *)
(** ** Introduction *)
(**
As the reader might recall from the HeapLang chapter, the only
construct for concurrency supported natively by HeapLang is [Fork].
The [Fork] construction is an example of _unstructured_ concurrency.
When we use [Fork] to create a new thread, there are no control flow
constructs to reason about the termination of the forked thread – it
just runs until it is done and, upon completion, disappears.
When such control flow constructs are available, we call it
_structured_ concurrency. It turns out that we can implement
structured concurrency from unstructured concurrency, and we have
already seen two such examples: [spawn] and [par]. Both of these
constructs are part of HeapLang's library, but under the hood, they
are simply HeapLang programs defined in terms of the [Fork] primitive.
The library definitions additionally give and prove specifications for
the constructs, which we have used in previous chapters. In this
chapter, we will define the constructs from scratch and write our own
specifications for them.
*)
(* ================================================================= *)
(** ** The Fork Construct *)
(**
Let us begin by revisiting the [Fork] construct. The operation takes
an expression [e] as an argument and spawns a new thread that executes
[e] concurrently. The operation itself returns the unit value on the
spawning thread.
[Fork] does not have dedicated tactical support. Instead, we simply
apply the lemma [wp_fork] – the specification for [Fork]. The lemma is
as follows.
*)
About wp_fork.
(**
For convenience, we include it here as well in `simplified' form.
[WP e {{_, True}} -∗ ▷ Φ #() -∗ WP Fork e {{v, Φ v}}]
That is, to show a weakest precondition of [Fork e], we have to show
the weakest precondition of [e] for a trivial postcondition. The key
point is that we only require the forked-off thread to be safe – we do
not care about its return value, hence the trivial postcondition.
*)
(* ================================================================= *)
(** ** The Spawn Construct *)
(**
The first structured concurrency construct we study is [spawn]. This
consists of two functions: [spawn], which spawns a thread and returns
a `handle', and [join], which uses the handle to wait for a thread to
finish.
We define the functions as follows.
*)
Definition spawn : val :=
λ: "f",
let: "c" := ref NONE in
Fork ("c" <- SOME ("f" #()));;
"c".
Definition join: val :=
rec: "join" "c" :=
match: !"c" with
NONE => "join" "c"
| SOME "x" => "x"
end.
(**
The idea with [spawn] is to create a `shared channel' which can be
used to signal when the forked-off thread has terminated. In this
case, the shared channel is simply a location containing an optional
value. When forking off the function ["f"], we wrap it around a store
operation, which writes the result of ["f"] into the location. The
location is then returned to the spawning thread, which can use this
so-called `handle' in the [join] function. The [join] function
continuously checks if the location has been updated to contain a
value. If this happens, the spawning thread knows that the forked-off
thread has finished, so it can extract the return value from the
location.
Considering this behaviour, we give [spawn] the specification:
[[
{{{ P }}} f #() {{{ v, RET v; Ψ v }}} -∗
{{{ P }}} spawn f {{{ h, RET h; join_handle h Ψ }}}
]]
This states that to get a specification for [spawn f], we first must
prove a specification for [f] which captures which resources [f]
needs, [P], and what the value [f] terminates at satisfies, [Ψ]. If we
can prove such a specification for [f], then, given [P], we can also
run [spawn f], which will return a value [h] which satisfies a
`join-handle' predicate. This predicate is a promise that if we
invoke [join] with [h], then the value we get back satisfies [Ψ]. This
is reflected in the specification for [join]:
[[
{{{ join_handle h Ψ }}} join h {{{ v, RET v; Ψ v }}}.
]]
Let us now prove these specifications.
*)
Section spawn.
Context `{!heapGS Σ}.
Let N := nroot .@ "handle".
(**
Since we are using a shared channel, we will use an invariant to allow
the two (concurrently running) threads to access it. An initial
attempt at stating this invariant looks as follows.
*)
Definition handle_inv1 (l : loc) (Ψ : val → iProp Σ) : iProp Σ :=
∃ v : val, l ↦ v ∗ (⌜v = NONEV⌝ ∨ ∃ w : val, ⌜v = SOMEV w⌝ ∗ Ψ w).
(**
The stated invariant governs the shared channel (some location [l])
and states that either no value has been sent yet or some value has
been sent that satisfies the predicate [Ψ].
We can then use the invariant to define the [join_handle] predicate.
*)
Definition join_handle1 (h : val) (Ψ : val → iProp Σ) : iProp Σ :=
∃ l : loc, ⌜h = #l⌝ ∗ inv N (handle_inv1 l Ψ).
(** Let us now attempt to prove the specification for [join]. *)
Lemma join_spec (h : val) (Ψ : val → iProp Σ) :
{{{ join_handle1 h Ψ }}} join h {{{ v, RET v; Ψ v }}}.
Proof.
iIntros (Φ) "(%l & -> & #I) HΦ".
iLöb as "IH".
wp_rec.
wp_bind (! _)%E.
iInv "I" as "(%v & Hl & [>-> | (%w & >-> & HΨ)])".
- wp_load.
iModIntro.
iSplitL "Hl".
{
iNext.
iExists NONEV.
iFrame.
by iLeft.
}
wp_pures.
by iApply "IH".
- wp_load.
iModIntro.
(**
Now we need [HΨ] to reestablish the invariant, but we also need it
for the postcondition. We are stuck...
*)
Abort.
(**
We need a way to keep track of whether [Ψ w] has been `taken out' of
the invariant or not. However, we do not have any program state to
link it to. Instead, we will use _ghost state_ to track this
information. In particular, we will use _tokens_, which we introduced
in the Resource Algebra chapter. We here use the token implementation
from the Iris library, but it is similar to the version we
implemented.
*)
Context `{!tokenG Σ}.
(**
The trick is to have an additional state in the invariant which
represents the case where [Ψ w] has been taken out of the invariant.
This state simply mentions the token.
*)
Definition handle_inv (γ : gname) (l : loc) (Ψ : val → iProp Σ) : iProp Σ :=
∃ v, l ↦ v ∗ (⌜v = NONEV⌝ ∨ (∃ w, ⌜v = SOMEV w⌝ ∗ Ψ w) ∨ token γ).
(**
This enables the owner of the token to open the invariant, extract
[Ψ w], and close the invariant in the case that mentions the token. As
such, we include the token in the join handle so that [join] gets
access to the token.
*)
Definition join_handle (h : val) (Ψ : val → iProp Σ) : iProp Σ :=
∃ γ (l : loc), ⌜h = #l⌝ ∗ token γ ∗ inv N (handle_inv γ l Ψ).
(**
Let us now try to prove the specifications again. We start with
[spawn].
*)
Lemma spawn_spec (P : iProp Σ) (Ψ : val → iProp Σ) (f : val) :
{{{ P }}} f #() {{{ v, RET v; Ψ v }}} -∗
{{{ P }}} spawn f {{{ h, RET h; join_handle h Ψ }}}.
Proof.
iIntros "#Hf %Φ !> HP HΦ".
wp_lam.
wp_alloc l as "Hl".
wp_pures.
iMod token_alloc as "[%γ Hγ]".
iMod (inv_alloc N _ (handle_inv γ l Ψ) with "[Hl]") as "#I".
{
iNext.
iExists NONEV.
iFrame.
by iLeft.
}
wp_apply (wp_fork with "[Hf HP]").
- iNext.
wp_apply ("Hf" with "HP").
iIntros "%v HΨ".
wp_pures.
iInv "I" as "(%w & Hl & _)".
wp_store.
iModIntro.
iSplitL; last done.
iNext.
iExists (SOMEV v).
iFrame.
iRight.
iLeft.
iExists v.
by iFrame.
- wp_pures.
iModIntro.
iApply "HΦ".
iExists γ, l.
by iFrame "Hγ I".
Qed.
Lemma join_spec (Ψ : val → iProp Σ) (h : val) :
{{{ join_handle h Ψ }}} join h {{{ v, RET v; Ψ v }}}.
Proof.
iIntros (Φ) "(%γ & %l & -> & Hγ & #I) HΦ".
iLöb as "IH".
wp_rec.
wp_bind (! #l)%E.
(** We open the invariant and consider the three possible states. *)
iInv "I" as "(%_ & Hl & [>-> | [(%w & >-> & HΨ) | >Hγ']])".
- (** Case: The forked-off thread is not yet finished. *)
wp_load.
iModIntro.
iSplitL "Hl".
{
iNext.
iExists NONEV.
iFrame.
by iLeft.
}
wp_pures.
iApply ("IH" with "Hγ HΦ").
- (** Case: The forked-off thread has finished. *)
wp_load.
iModIntro.
(**
Note that now, since we own the token, we do not need to use [Ψ w]
to close the invariant – we close it with the token.
*)
iSplitL "Hγ Hl".
{
iNext.
iExists (SOMEV w).
iFrame.
}
wp_pures.
by iApply "HΦ".
- (**
Case: [Ψ w] has already been taken out of the invariant.
This case is impossible as we own the token.
*)
iPoseProof (token_exclusive with "Hγ Hγ'") as "[]".
Qed.
End spawn.
(* ================================================================= *)
(** ** The Par Construct *)
(**
With [spawn] and [join] defined and their specifications proved, we
can move on to study a classical parallel composition operator: [par].
Its definition is quite straightforward – building on the [spawn]
construct.
*)
Definition par : val :=
λ: "f1" "f2",
let: "h" := spawn "f1" in
let: "v2" := "f2" #() in
let: "v1" := join "h" in
("v1", "v2").
(** We introduce familiar notation for [par] that hides the thunks. *)
Notation "e1 ||| e2" := (par (λ: <>, e1)%E (λ: <>, e2)%E) : expr_scope.
Notation "e1 ||| e2" := (par (λ: <>, e1)%V (λ: <>, e2)%V) : val_scope.
(**
Our desired specification for [par] is going to look as follows:
[[
{{{ P1 }}} e1 {{{ v, RET v; Ψ1 v }}} -∗
{{{ P2 }}} e2 {{{ v, RET v; Ψ2 v }}} -∗
{{{ P1 ∗ P2 }}} e1 ||| e2 {{{ v1 v2, RET (v1, v2); Ψ1 v1 ∗ Ψ2 v2 }}}
]]
The rule states that we can run [e1] and [e2] in parallel if they have
_disjoint_ footprints and that we can verify the two components
separately. For this reason, the rule is sometimes also referred to as
the _disjoint concurrency rule_. Note that this specification looks
slightly different from the specification in the [par] library:
[wp_par]. However, the differences are mainly notational.
*)
Section par.
(**
Since [par] is implemented with [spawn], we will use [spawn_spec] and
[par_spec] to prove the specification for [par]. As such, we will need
to include the resource algebra that those specifications rely on:
[token].
*)
Context `{!heapGS Σ, !tokenG Σ}.
(**
It is actually quite straightforward to prove the [par] specification
as most of the heavy lifting is done by [spawn_spec] and [join_spec].
*)
Lemma par_spec (P1 P2 : iProp Σ) (e1 e2 : expr) (Q1 Q2 : val → iProp Σ) :
{{{ P1 }}} e1 {{{ v, RET v; Q1 v }}} -∗
{{{ P2 }}} e2 {{{ v, RET v; Q2 v }}} -∗
{{{ P1 ∗ P2 }}} (e1 ||| e2)%V {{{ v1 v2, RET (v1, v2); Q1 v1 ∗ Q2 v2 }}}.
Proof.
iIntros "#H1 #H2 %Φ !> [HP1 HP2] HΦ".
rewrite /par.
wp_pures.
(**
We use [spawn_spec] to spawn a thread running [e1]. This requires us
to prove that if [e1] terminates at value [v1], then [Q1 v1].
However, this follows by our assumption ["H1"], so we easily prove
this.
*)
wp_apply (spawn_spec P1 Q1 with "[] HP1").
{
iIntros "%Φ1 !> HP1 HΦ1".
wp_pures.
iApply ("H1" with "HP1 HΦ1").
}
(**
We now get a join handle for the spawned thread, which guarantees us
that the value we get upon joining with it satisfies [Q1].
*)
iIntros "%h Hh".
wp_pures.
(**
Next, we execute [e2] in the current thread using its specification,
["H2"], which gives us that if [e2] terminates at some value [v2],
then [Q2 v2].
*)
wp_apply ("H2" with "HP2").
iIntros "%v2 HQ2".
wp_pures.
(**
Finally, we join with the spawned thread using our join handle and
[join_spec].
*)
wp_apply (join_spec with "Hh").
iIntros "%v1 HQ1".
wp_pures.
iModIntro.
iApply "HΦ".
iFrame.
Qed.
End par.
(**
Let us try to use the [par] specification to prove a specification for a
simple client. The client performs two `fetch and add' operations on
the same location in parallel. The expression [FAA "l" #i] atomically
fetches the value at location [l], adds [i] to it, and stores the
result back in [l].
Our specification will state that the resulting value is even.
*)
Definition parallel_add : expr :=
let: "r" := ref #0 in
(FAA "r" #2)
|||
(FAA "r" #6)
;;
!"r".
Section parallel_add.
(**
We must again assume the presence of the [token] resource algebra as
we will be using the [par] specification, which relies on it through
[spawn].
*)
Context `{!heapGS Σ, !tokenG Σ}.
Let N := nroot .@ "par_add".
(**
We will have an invariant stating that [r] points to an even integer.
*)
Definition parallel_add_inv (r : loc) : iProp Σ :=
∃ n : Z, r ↦ #n ∗ ⌜Zeven n⌝.
Lemma parallel_add_spec :
{{{ True }}} parallel_add {{{ n, RET #n; ⌜Zeven n⌝ }}}.
Proof.
iIntros "%Φ _ HΦ".
rewrite /parallel_add.
wp_alloc r as "Hr".
wp_pures.
iMod (inv_alloc N _ (parallel_add_inv r) with "[Hr]") as "#I".
{
iNext.
iExists 0.
iFrame.
}
(**
We don't need information back from the threads, so we will simply
use [λ _, True] as the postconditions. Similarly, we only need the
invariant to prove the threads, and since this is in the persistent
context, we let the preconditions be [True].
*)
wp_apply (par_spec (True%I) (True%I) _ _ (λ _, True%I) (λ _, True%I)).
- iIntros (Φ') "!> _ HΦ'".
iInv "I" as "(%n & Hr & >%Hn)".
wp_faa.
iModIntro.
iSplitL "Hr".
{
iModIntro.
iExists (n + 2)%Z.
iFrame.
iPureIntro.
by apply Zeven_plus_Zeven.
}
by iApply "HΦ'".
(* exercise *)
Admitted.
End parallel_add.