summaryrefslogtreecommitdiff
path: root/Test/aitest0/Intervals.bpl
blob: 565b68230245bb9b40a76a2b3ea969c109c6f676 (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
325
326
327
328
329
330
331
332
333
334
// RUN: %boogie -infer:j "%s" > "%t"
// RUN: %diff "%s.expect" "%t"
const N: int;
axiom 0 <= N;

procedure P(K: int)
  requires 0 <= K;
{
  var b: bool, x, k: int;

  if (!b) {
    b := !b;
  }
  x := if b then 13 else 10;
  k := K;
  while (k != 0) {
    x := x + k;
    k := k - 1;
  }
  assert 13 <= x;
}

procedure Thresholds0()
{
  var i: int;
  i := 0;
  while (i < 200)
  {
    i := i + 1;
  }
  assert i == 200;
}

procedure Thresholds1()
{
  var i: int;
  i := 0;
  while (i <= 199)
  {
    i := i + 1;
  }
  assert i == 200;
}

procedure Thresholds2()
{
  var i: int;
  i := 100;
  while (0 < i)
  {
    i := i - 1;
  }
  assert i == 0;
}

procedure Thresholds3()
{
  var i: int;
  i := 0;
  while (i < 200)
  {
    i := i + 1;
  }
  assert i == 199;  // error
}

procedure Thresholds4()
{
  var i: int;
  i := 0;
  while (i + 3 < 203)
  {
    i := i + 1;
  }
  assert i * 2 == 400;  // error: this would hold in an execution, but /infer:j is too weak to infer invariant i<=200
}

procedure UnaryNegation0() returns (x: int)  // this was once buggy
{
  x := -1;
  loop_head:
    x := x;
    goto loop_head, after_loop;
  after_loop:
  assert x == -1;
}
procedure UnaryNegation1() returns (x: int)  // this was once buggy
{
  x := -1;
  loop_head:
    x := x;
    goto loop_head, after_loop;
  after_loop:
  assert x == 1;  // error
}

// --------------------------- test {:identity} annotation --------------------

function {:identity} MyId(x: int): int;
function MyStealthyId(x: int): int;  // this one messes up the abstract interpretation
function {:identity false} {:identity}/*the last attribute rules*/ MyPolyId<T>(x: T): T;
function {:identity /*this is a lie*/} MyBogusId(x: int): int { -x }
function {:identity /*ignored, since the function takes more than one argument*/} MultipleArgs(x: int, y: int): int;
function {:identity /*ignored, since the return type is not equal to the argument type*/} BoolToInt(b: bool): int;
function {:identity true/*in some contexts, the type of this function makes sense as an identity*/} SometimesId0<T>(x: T): int;
function {:identity true/*in some contexts, the type of this function makes sense as an identity*/} SometimesId1<T>(x: int): T;
function {:identity true/*in some contexts, the type of this function makes sense as an identity*/} SometimesId2<T,U>(x: T): U;


procedure Id0(n: int)
{
  var i: int;
  i := 0;
  while (i < n)
  {
    i := i + 1;
  }
  assert 0 <= i;
}

procedure Id1(n: int)
{
  var i: int;
  i := MyId(0);
  while (i < n)
  {
    i := i + MyId(1);
  }
  assert 0 <= i;
}

procedure Id2(n: int)
{
  var i: int;
  i := MyStealthyId(0);
  while (i < n)
  {
    i := i + 1;
  }
  assert 0 <= i;  // error: abstract interpreter does not figure this one out
}

procedure Id3(n: int)
{
  var i: int;
  i := 0;
  while (i < n)
  {
    i := i + MyStealthyId(1);
  }
  assert 0 <= i;  // error: abstract interpreter does not figure this one out
}

procedure Id4(n: int)
{
  var i: int;
  i := MyPolyId(0);
  while (i < n)
  {
    i := i + MyPolyId(1);
  }
  assert 0 <= i;
}

procedure Id5(n: int)
{
  var i: int;
  var b: bool;
  i, b := 0, false;
  while (i < n)
  {
    i, b := i + 1, false;
  }
  assert !b;
}

procedure Id6(n: int)
{
  var i: int;
  var b: bool;
  i, b := 0, MyPolyId(false);
  while (i < n)
  {
    i, b := i + 1, false;
  }
  assert !b;
}

procedure Id7(n: int)
{
  var i, k, y, z: int;
  i, k := 0, 0;
  while (i < n)
  {
    i := i + 1;
    y, z := MyBogusId(5), -5;
    k := k + z;
    if (*) {
      assert y == z;  // fine
    }
  }
  assert 0 <= k;  // error: this does not hold -- k may very well be negative
}

procedure Id8(n: int)
{
  var i, k: int;
  i, k := 0, 0;
  while (i < n)
  {
    i := i + 1;
    k := k + MyBogusId(5);
  }
  assert 0 <= k;  // since we lied about MyBogusId being an {:identity} function, the abstract interpreter gives us this bogus invariant
}

procedure Id9(n: int)
  requires 0 < n;
{
  var i, k: int;
  i, k := 0, 0;
  while (i < n)
    invariant i <= n && -k == 5*i;
  {
    i := i + 1;
    k := k + MyBogusId(5);
  }
  assert -k == 5*n;
  assert false;  // this just shows the effect of MyBogusId even more; there is no complaint about this assert
}

procedure Id10(n: int)
{
  var i: int;
  i := 0;
  while (i < n)
  {
    i := i + MultipleArgs(19, 23);
  }
  assert 0 <= i;  // error: no information is known about i
}

procedure Id11(n: int)
{
  var i, k: int;
  i, k := 0, 0;
  while (i < n)
  {
    i := i + 1;
    k := k + BoolToInt(false);  // this should not be treated as an identity function, since it goes from one type to another
  }
  assert 0 <= k;  // error: no information is known about k
}

procedure Id12(n: int)
{
  var i: int;
  i := 0;
  while (i < n)
  {
    i := i + SometimesId0(false);
  }
  assert 0 <= i;  // error: no information is known about i
}

procedure Id13(n: int)
{
  var i: int;
  i := 0;
  while (i < n)
  {
    i := i + SometimesId0(1);
  }
  assert 0 <= i;
}

procedure Id14(n: int)
{
  var i: int;
  i := 0;
  while (i < n)
  {
    i := i + SometimesId0(-1);
  }
  assert 0 <= i;  // error: this does not hold
}

procedure Id15(n: int)
{
  var i: int;
  i := 0;
  while (i < n)
  {
    i := i + SometimesId1(1);
  }
  assert 0 <= i;  // fine: SometimesId1 claims to be an identity and the use of it is int->int
}

procedure Id16(n: int)
{
  var i: int;
  i := 0;
  while (i < n)
  {
    i := i + SometimesId2(false);
  }
  assert 0 <= i;  // error: no information is known about i
}

procedure Id17(n: int)
{
  var i: int;
  i := 0;
  while (i < n)
  {
    i := i + SometimesId2(1);
  }
  assert 0 <= i;  // fine: SometimesId2 claims to be an identity and the use of it is int->int
}

// real numbers

procedure W0(N: real)
{
  var i, bf0: real;
  i := 0.0;
  while (i < N) {
    bf0 := N - i;
    i := i + 1.0;
    // check termination:
    assert 0.0 <= bf0;
    assert N - i <= bf0 - 1.0;
  }
}