summaryrefslogtreecommitdiff
path: root/Test/civl/multiset.bpl
blob: 7fb0a0817ab4a1401802ea954392602900cbd213 (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
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
// RUN: %boogie -noinfer -typeEncoding:m -useArrayTheory "%s" > "%t"
// RUN: %diff "%s.expect" "%t"
type X;

const unique null : int;
const unique nil: X;
const unique done: X;

var {:layer 0} elt : [int]int;
var {:layer 0} valid : [int]bool;
var {:layer 0} lock : [int]X;
var {:layer 0} owner : [int]X;
const max : int;

function {:builtin "MapConst"} MapConstBool(bool) : [X]bool;
function {:inline} {:linear "tid"} TidCollector(x: X) : [X]bool
{
  MapConstBool(false)[x := true]
}

axiom (max > 0);

procedure {:yields} {:layer 0} acquire(i : int, {:linear "tid"} tid: X);
ensures {:right} |{ A: 
                      assert 0 <= i && i < max;
                      assert tid != nil && tid != done;
                      assume lock[i] == nil;
	              lock[i] := tid;
                      return true;
                    }|;


procedure {:yields} {:layer 0} release(i : int, {:linear "tid"} tid: X);
ensures {:left} |{ A: 
                     assert 0 <= i && i < max;
                     assert lock[i] == tid;
                     assert tid != nil && tid != done; 
	             lock[i] := nil;
                     return true;
                    }|;


procedure {:yields} {:layer 0,1} getElt(j : int, {:linear "tid"} tid: X) returns (elt_j:int);
ensures {:both} |{ A: 
                     assert 0 <= j && j < max;
                     assert lock[j] == tid;
                     assert tid != nil && tid != done;
	             elt_j := elt[j];
                     return true;
                    }|;


procedure {:yields} {:layer 0,1} setElt(j : int, x : int, {:linear "tid"} tid: X);
ensures {:both} |{ A: 
                     assert x != null;
                     assert owner[j] == nil;
                     assert 0 <= j && j < max;
                     assert lock[j] == tid;
                     assert tid != nil && tid != done;
	             elt[j] := x;
                     owner[j] := tid;
                     return true;
                    }|;


procedure {:yields} {:layer 0,2} setEltToNull(j : int, {:linear "tid"} tid: X);
ensures {:left} |{ A: 
                     assert owner[j] == tid;
                     assert 0 <= j && j < max;
                     assert lock[j] == tid;
                     assert !valid[j];
                     assert tid != nil  && tid != done;
	             elt[j] := null;
                     owner[j] := nil;
                     return true;
                    }|;

procedure {:yields} {:layer 0,2} setValid(j : int, {:linear "tid"} tid: X);
ensures {:both} |{ A: 
                     assert 0 <= j && j < max;
                     assert lock[j] == tid;
                     assert tid != nil && tid != done;
                     assert owner[j] == tid;
	             valid[j] := true;
                     owner[j] := done;
                     return true;
                    }|;

procedure {:yields} {:layer 0,2} isEltThereAndValid(j : int, x : int, {:linear "tid"} tid: X) returns (fnd:bool);
ensures {:both} |{ A: 
                       assert 0 <= j && j < max;
                       assert lock[j] == tid;
                       assert tid != nil && tid != done;
                       fnd := (elt[j] == x) && valid[j];
                       return true;
                    }|;

procedure {:yields} {:layer 1,2} FindSlot(x : int, {:linear "tid"} tid: X) returns (r : int)
requires {:layer 1} Inv(valid, elt, owner) && x != null && tid != nil && tid != done; 
ensures {:layer 1} Inv(valid, elt, owner);
ensures {:right}  |{   A: assert tid != nil && tid != done; 
                          assert x != null; 
			  goto B, C;
                       B: assume (0 <= r && r < max); 
                                            assume elt[r] == null; 
                                            assume owner[r] == nil;
					    assume !valid[r];
                                            elt[r] := x; 
                                            owner[r] := tid;
                                            return true;
                       C: assume (r == -1); return true; 
                   }|;
{
	var j : int;
	var elt_j : int;
        
        par Yield1();

	j := 0;
	while(j < max)
        invariant {:layer 1} Inv(valid, elt, owner);
	invariant {:layer 1} 0 <= j;
	{
                call acquire(j, tid);
                call elt_j := getElt(j, tid);
		if(elt_j == null)
		{
                        call setElt(j, x, tid);
			call release(j, tid);
			r := j;

                        par Yield1();
			return;		
		}
		call release(j,tid);

                par Yield1();

		j := j + 1;	
	}
	r := -1;

        par Yield1();
	return;
}

procedure {:yields} {:layer 2} Insert(x : int, {:linear "tid"} tid: X) returns (result : bool) 
requires {:layer 1} Inv(valid, elt, owner) && x != null && tid != nil && tid != done; 
ensures {:layer 1} Inv(valid, elt, owner);
requires {:layer 2} Inv(valid, elt, owner) && x != null && tid != nil && tid != done; 
ensures {:layer 2} Inv(valid, elt, owner);
ensures {:atomic}  |{ var r:int;
                        A: goto B, C;
                        B: assume (0 <= r && r < max); 
                           assume valid[r] == false; 
                           assume elt[r] == null; 
                           assume owner[r] == nil; 
                           elt[r] := x; valid[r] := true; owner[r] := done;
                           result := true; return true;
                        C: result := false; return true; 
                      }|;
 {
        var i: int;
        par Yield12();
        call i := FindSlot(x, tid);
        
	if(i == -1)
	{
		result := false;
                par Yield12();
		return;
	}
        par Yield1();
        assert {:layer 1} i != -1;
        assert {:layer 2} i != -1;
	call acquire(i, tid);
        assert {:layer 2} elt[i] == x;
	assert {:layer 2}  valid[i] == false;
        call setValid(i, tid);   
	call release(i, tid);
	result := true; 
        par Yield12();
	return;
}

procedure {:yields} {:layer 2} InsertPair(x : int, y : int, {:linear "tid"} tid: X) returns (result : bool)
requires {:layer 1} Inv(valid, elt, owner) && x != null && y != null && tid != nil && tid != done; 
ensures {:layer 1} Inv(valid, elt, owner);
requires {:layer 2} Inv(valid, elt, owner) && x != null && y != null && tid != nil && tid != done; 
ensures {:layer 2} Inv(valid, elt, owner);
ensures {:atomic}  |{ var rx:int;
                        var ry:int;
                        A: goto B, C;
                        B: assume (0 <= rx && rx < max && 0 <= ry && ry < max && rx != ry); 
                           assume valid[rx] == false; 
                           assume valid[ry] == false; 
                           assume elt[rx] == null; 
                           assume elt[rx] == null; 
                           elt[rx] := x; 
                           elt[ry] := y; 
                           valid[rx] := true; 
                           valid[ry] := true;
                           owner[rx] := done;
                           owner[ry] := done; 
                           result := true; return true;
                        C: result := false; return true; 
                      }|;
 {
        var i : int; 
        var j : int;
        par Yield12();

        call i := FindSlot(x, tid);
        
	if (i == -1)
	{
	 	result := false;
                par Yield12();
		return;
	}

        par Yield1();
        call j := FindSlot(y, tid);
        
	if(j == -1)
	{
                par Yield1();
                call acquire(i,tid);  
                call setEltToNull(i, tid);
                call release(i,tid);  
		result := false;
                par Yield12();
		return;
	}

        par Yield1();
        assert {:layer 2} i != -1 && j != -1;
	call acquire(i, tid);
	call acquire(j, tid);
        assert {:layer 2} elt[i] == x;
        assert {:layer 2} elt[j] == y;
	assert {:layer 2}  valid[i] == false;
	assert {:layer 2}  valid[j] == false;
        call setValid(i, tid);   
        call setValid(j, tid);   
	call release(j, tid);
	call release(i, tid);
	result := true; 
        par Yield12();
	return;
}

procedure {:yields} {:layer 2} LookUp(x : int, {:linear "tid"} tid: X, old_valid:[int]bool, old_elt:[int]int) returns (found : bool)
requires {:layer 1} {:layer 2} old_valid == valid && old_elt == elt;
requires {:layer 1} {:layer 2} Inv(valid, elt, owner);
requires {:layer 1} {:layer 2} (tid != nil && tid != done);
ensures {:layer 1} {:layer 2} Inv(valid, elt, owner);
ensures {:atomic}  |{ A: assert tid != nil && tid != done; 
                          assert x != null;
                          assume found ==> (exists ii:int :: 0 <= ii && ii < max && valid[ii] && elt[ii] == x);
			  assume !found ==> (forall ii:int :: 0 <= ii && ii < max ==> !(old_valid[ii] && old_elt[ii] == x));
                          return true;
                    }|;
{
	var j : int;
	var isThere : bool;
        
        par Yield12() | YieldLookUp(old_valid, old_elt);

	j := 0;

	while(j < max)
        invariant {:layer 1} {:layer 2} Inv(valid, elt, owner);
        invariant {:layer 1} {:layer 2} (forall ii:int :: 0 <= ii && ii < j ==> !(old_valid[ii] && old_elt[ii] == x));
	invariant {:layer 1} {:layer 2} (forall ii:int :: 0 <= ii && ii < max && old_valid[ii] ==> valid[ii] && old_elt[ii] == elt[ii]);
	invariant {:layer 1} {:layer 2} 0 <= j;
	{
                call acquire(j, tid);
                call isThere := isEltThereAndValid(j, x, tid);
		if(isThere)
		{
			 call release(j, tid);
                         found := true; 
                         par Yield12() | YieldLookUp(old_valid, old_elt);
		 	 return;		
		}
		call release(j,tid);
                par Yield12() | YieldLookUp(old_valid, old_elt);
		j := j + 1;	
	}
	found := false;

        par Yield12() | YieldLookUp(old_valid, old_elt);
	return;
}

procedure {:yields} {:layer 1} Yield1()
requires {:layer 1} Inv(valid, elt, owner);
ensures {:layer 1} Inv(valid, elt, owner);
{
	yield;
	assert {:layer 1} Inv(valid, elt, owner);
}

procedure {:yields} {:layer 2} Yield12()
requires {:layer 1} {:layer 2} Inv(valid, elt, owner);
ensures {:layer 1} {:layer 2} Inv(valid, elt, owner);
{
        yield;
        assert {:layer 1} {:layer 2} Inv(valid, elt, owner);
}

function {:inline} Inv(valid: [int]bool, elt: [int]int, owner: [int]X): (bool)
{
   (forall i:int :: 0 <= i && i < max ==> (elt[i] == null <==> (!valid[i] && owner[i] == nil)))
}

procedure {:yields} {:layer 2} YieldLookUp(old_valid: [int]bool, old_elt: [int]int)
requires {:layer 1} {:layer 2} (forall ii:int :: 0 <= ii && ii < max && old_valid[ii] ==> valid[ii] && old_elt[ii] == elt[ii]);
ensures {:layer 1} {:layer 2} (forall ii:int :: 0 <= ii && ii < max && old_valid[ii] ==> valid[ii] && old_elt[ii] == elt[ii]);
{
    yield;
    assert {:layer 1} {:layer 2} (forall ii:int :: 0 <= ii && ii < max && old_valid[ii] ==> valid[ii] && old_elt[ii] == elt[ii]);
}