-
Notifications
You must be signed in to change notification settings - Fork 26
/
channels.gobra
48 lines (42 loc) · 1.05 KB
/
channels.gobra
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
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/
package tutorial
pred sendInvariant(v *int) {
acc(v) && *v > 0
}
requires acc(c.SendChannel(), 1/2)
requires acc(c.RecvChannel(), 1/2)
requires c.SendGivenPerm() == sendInvariant!<_!>
requires c.SendGotPerm() == PredTrue!<!>
requires c.RecvGivenPerm() == PredTrue!<!>
requires c.RecvGotPerm() == sendInvariant!<_!>
func incChannel(c chan *int) {
fold PredTrue!<!>()
res, ok := <- c
if (ok) {
unfold sendInvariant!<_!>(res)
// we now have write access after unfolding the invariant:
*res = *res + 1
// fold the invariant and send pointer and permission back:
fold sendInvariant!<_!>(res)
c <- res
}
}
func clientChannel() {
var c@ = make(chan *int)
var x@ int = 42
var p *int = &x
c.Init(sendInvariant!<_!>, PredTrue!<!>)
go incChannel(c)
assert *p == 42
fold sendInvariant!<_!>(p)
c <- p
fold PredTrue!<!>()
res, ok := <- c
if (ok) {
unfold sendInvariant!<_!>(res)
assert *res > 0
// we have regained write access:
*res = 1
}
}