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
|
// RUN: %dafny /compile:0 /print:"%t.print" /dprint:"%t.dprint" "%s" > "%t"
// RUN: %diff "%s.expect" "%t"
function Fib(n: int): int
requires 0 <= n;
ensures 0 <= Fib(n);
{
if n < 2 then n else
Fib(n-2) + Fib(n-1)
}
datatype List = Nil | Cons(int, List);
function Sum(a: List): int
ensures 0 <= Sum(a);
{
match a
case Nil => 0
case Cons(x, tail) => if x < 0 then 0 else Fib(x)
}
function FibWithoutPost(n: int): int
requires 0 <= n;
{
if n < 2 then n else
FibWithoutPost(n-2) + FibWithoutPost(n-1)
}
function SumBad(a: List): int
ensures 0 <= Sum(a); // this is still okay, because this is calling the good Sum
ensures 0 <= SumBad(a); // error: cannot prove postcondition
{
match a
case Nil => 0
case Cons(x, tail) => if x < 0 then 0 else FibWithoutPost(x)
}
function FibWithExtraPost(n: int): int
ensures 2 <= n ==> 0 <= FibWithExtraPost(n-1); // This is fine, because the definition of the function is discovered via canCall
ensures 1 <= n ==> 0 <= FibWithExtraPost(n-1); // Error: In the current implementation of Dafny, one needs to actually call the
// function in order to benefit from canCall. This may be improved in the future.
ensures 0 <= FibWithExtraPost(n);
{
if n < 0 then 0 else
if n < 2 then n else
FibWithExtraPost(n-2) + FibWithExtraPost(n-1)
}
function DivergentPost(n: int): int
requires 0 <= n;
ensures 1 <= n ==> DivergentPost(n-1) == DivergentPost(n-1);
ensures DivergentPost(2*n - n) == DivergentPost(2*(n+5) - 10 - n); // these are legal ways to denote the result value of the function
ensures DivergentPost(n+1) == DivergentPost(n+1); // error: call may not terminate
{
if n < 2 then n else
DivergentPost(n-2) + DivergentPost(n-1)
}
function HoldsAtLeastForZero(x: int): bool
ensures x == 0 ==> HoldsAtLeastForZero(x);
{
x < -2 // error: this does not hold for 0
}
// ----- Some functions that deal with let-such-that and if-then-else expressions and having them pass
// ----- the subrange test (which they didn't always do).
function IncA(x: nat): nat
ensures x < IncA(x);
{
if x == 17 then
18
else
var y :| x < y;
y
}
ghost method M() {
var z := IncA(10);
assert z != 0;
}
function IncB(i: nat): nat
{
var n :| n>i; n
}
function IncC(i: nat): int
ensures IncC(i)>=0;
{
var n :| n>i; n
}
/////////////////////////////////////////////////////////////
// Test :opaque functions
/////////////////////////////////////////////////////////////
// Test basic function hiding
function {:opaque} secret(x:int, y:int) : int
requires 0 <= x < 5;
requires 0 <= y < 5;
ensures secret(x, y) < 10;
{ x + y }
method test_secret()
{
assert secret(2, 3) >= 5; // Should fail because secret's body is hidden
reveal_secret();
assert secret(2, 3) == 5; // Should pass now that the body is "visible"
assert secret(4, 1) == 7; // Make sure it catches incorrect applications as well
}
// Check that opaque doesn't break recursion unrolling
// Also checks that opaque functions that do terminate are verified as such
function {:opaque} recursive_f(x:int) : int
requires x >= 0;
{
if x == 0 then 0
else 1 + recursive_f(x - 1)
}
method test_recursive_f()
{
if * {
assert recursive_f(4) == 4; // Should fail because body is hidden
} else {
reveal_recursive_f();
assert recursive_f(4) == 4; // Should pass now body is visible and can be unrolled
assert recursive_f(3) == 5; // Make sure it catches incorrect applications as well
}
}
// Check that opaque doesn't interfere with ensures checking
function {:opaque} bad_ensures(x:int, y:int):int
requires x >= 0 && y >= 0;
ensures bad_ensures(x, y) > 0;
{
x + y
}
// Check that opaque doesn't interfere with termination checking
function {:opaque} f(i:int):int
decreases i;
{
f(i) + 1
}
method m()
ensures false;
{
reveal_f();
assert f(0) == f(0) + 1;
}
// Try a sneakier (nested) version of the test above
function {:opaque} g(i:int):int
decreases i;
{
h(i) + 1
}
function h(i:int):int
{
g(i)
}
method n()
ensures false;
{
reveal_g();
assert g(0) == g(0) + 1;
}
// Check that using the reveal_ lemma to prove the well-definedness of a function
// in a lower SCC does not cause a soundness problem
function A(): int
ensures A() == 5;
{
reveal_B(); // error: should not be allowed to invoke reveal_B() here
6 // this result value is not what the postcondition specification says
}
function {:opaque} B(): int
ensures B() == 5;
{
A()
}
method m_noreveal()
ensures false;
{
assert f(0) == f(0) + 1;
}
method n_noreveal()
ensures false;
{
assert g(0) == g(0) + 1;
}
|