summaryrefslogtreecommitdiff
path: root/Test/civl/Program4.bpl
blob: 11ba8afabde18aed5f101f7eb52121d27bc23b2e (plain)
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
// RUN: %boogie -noinfer -typeEncoding:m -useArrayTheory "%s" > "%t"
// RUN: %diff "%s.expect" "%t"
var {:layer 0,2} a:[int]int;
var {:layer 0,1} count: int;
var {:layer 1,1} {:linear "tid"} allocated:[int]bool;

procedure {:yields} {:layer 2} main()
requires {:layer 1} allocated == MapConstBool(false);
{
    var {:layer 1} {:linear "tid"} tid:int;
    var i: int;

    yield;
    assert {:layer 1} AllocInv(count, allocated);
    while (true)
    invariant {:layer 1} AllocInv(count, allocated);
    { 
        call tid, i := Allocate(); 
	async call P(tid, i); 
	yield;
        assert {:layer 1} AllocInv(count, allocated);	
    }
    yield;
}

procedure {:yields} {:layer 2} P({:layer 1} {:linear "tid"} tid: int, i: int)
requires {:layer 1} tid == i;
requires {:layer 1} AllocInv(count, allocated);
ensures {:layer 1} AllocInv(count, allocated);
ensures {:layer 2} a[tid] == old(a)[tid] + 1; 
{ 
    var t:int;

    yield;
    assert {:layer 1} AllocInv(count, allocated);
    assert {:layer 2} a[tid] == old(a)[tid];
    call t := Read(tid, i); 
    yield;
    assert {:layer 1} AllocInv(count, allocated);
    assert {:layer 2} a[tid] == t; 
    call Write(tid, i, t + 1); 
    yield;
    assert {:layer 1} AllocInv(count, allocated);
    assert {:layer 2} a[tid] == t + 1; 
}

procedure {:yields} {:layer 1,2} Allocate() returns ({:layer 1} {:linear "tid"} tid: int, i: int)
requires {:layer 1} AllocInv(count, allocated);
ensures {:layer 1} AllocInv(count, allocated);
ensures {:layer 1} tid == i;
ensures {:atomic}
|{A:
  return true;
}|;
{
    yield;
    assert {:layer 1} AllocInv(count, allocated);
    call i := AllocateLow();
    call tid := MakeLinear(i);
    yield;
    assert {:layer 1} AllocInv(count, allocated);    
}

procedure {:yields} {:layer 1,2} Read({:layer 1} {:linear "tid"} tid: int, i: int) returns (val: int)
requires {:layer 1} tid == i;
requires {:layer 1} AllocInv(count, allocated);
ensures {:layer 1} AllocInv(count, allocated);
ensures {:atomic}
|{A:
  val := a[tid]; return true;
}|;
{
    yield;
    assert {:layer 1} AllocInv(count, allocated);
    call val := ReadLow(i);
    yield;
    assert {:layer 1} AllocInv(count, allocated);
}

procedure {:yields} {:layer 1,2} Write({:layer 1} {:linear "tid"} tid: int, i: int, val: int)
requires {:layer 1} tid == i;
requires {:layer 1} AllocInv(count, allocated);
ensures {:layer 1} AllocInv(count, allocated);
ensures {:atomic}
|{A:
  a[tid] := val; return true;
}|;
{
    yield;
    assert {:layer 1} AllocInv(count, allocated);
    call WriteLow(i, val);
    yield;
    assert {:layer 1} AllocInv(count, allocated);
}

function {:inline} AllocInv(count: int, allocated:[int]bool): (bool)
{
    (forall x: int :: allocated[x] ==> x < count)
}

procedure {:yields} {:layer 0,1} ReadLow(i: int) returns (val: int);
ensures {:atomic}
|{A:
  val := a[i]; return true;
}|;

procedure {:yields} {:layer 0,1} WriteLow(i: int, val: int);
ensures {:atomic}
|{A:
  a[i] := val; return true;
}|;

procedure {:yields} {:layer 0,1} AllocateLow() returns (i: int);
ensures {:atomic}
|{A:
  i := count;
  count := i + 1;
  return true;
}|;

// We can prove that this primitive procedure preserves the permission invariant locally.
// We only need to using its specification and the definitions of TidCollector and TidSetCollector.
procedure {:layer 1} MakeLinear(i: int) returns ({:linear "tid"} tid: int);
requires !allocated[i];
modifies allocated;
ensures tid == i && allocated == old(allocated)[i := true];

function {:builtin "MapConst"} MapConstBool(bool): [int]bool;
function {:builtin "MapOr"} MapOr([int]bool, [int]bool) : [int]bool;

function {:inline} {:linear "tid"} TidCollector(x: int) : [int]bool
{
  MapConstBool(false)[x := true]
}
function {:inline} {:linear "tid"} TidSetCollector(x: [int]bool) : [int]bool
{
  x
}