-
Notifications
You must be signed in to change notification settings - Fork 1
/
Iterators.dfy
51 lines (47 loc) · 879 Bytes
/
Iterators.dfy
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
function method abs(x: int): int
{
if x < 0 then - x else x
}
iterator AllInts() yields (x: int)
yield ensures x !in xs[..|xs|-1]
yield ensures forall y :: abs(y) < x ==> y in xs
yield ensures x < 0 ==> -x in xs
{
assert xs == [];
x := 0;
yield;
while true
{
x := x + 1;
yield;
x := -x;
yield;
x := -x;
}
}
method FindInt(n: int)
{
var it := new AllInts();
var more := it.MoveNext();
assert more;
if it.x == n {
return;
}
while true
invariant it.x <= 0
invariant fresh(it._new)
decreases n + it.x
modifies it
{
more := it.MoveNext();
assert more;
if it.x == n {
break;
}
more := it.MoveNext();
assert more;
if it.x == n {
break;
}
}
}