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
|
// RUN: %dafny /compile:0 /dprint:"%t.dprint" "%s" > "%t"
// RUN: %diff "%s.expect" "%t"
/* Lists */
// Here are some standard definitions of List and functions on Lists
datatype List<T> = Nil | Cons(T, List)
function length(l: List): nat
{
match l
case Nil => 0
case Cons(x, xs) => 1 + length(xs)
}
function concat(l: List, ys: List): List
{
match l
case Nil => ys
case Cons(x, xs) => Cons(x, concat(xs, ys))
}
function reverse(l: List): List
{
match l
case Nil => Nil
case Cons(x, xs) => concat(reverse(xs), Cons(x, Nil))
}
function revacc(l: List, acc: List): List
{
match l
case Nil => acc
case Cons(x, xs) => revacc(xs, Cons(x, acc))
}
function qreverse(l: List): List
{
revacc(l, Nil)
}
// Here are two lemmas about the List functions.
lemma Lemma_ConcatNil(xs : List)
ensures concat(xs, Nil) == xs;
{
}
lemma Lemma_RevCatCommute(xs : List)
ensures forall ys, zs :: revacc(xs, concat(ys, zs)) == concat(revacc(xs, ys), zs);
{
}
// Here is a theorem that says "qreverse" and "reverse" calculate the same result. The proof
// is given in a calculational style. The proof is not minimal--some lines can be omitted
// and Dafny will still fill in the details.
lemma Theorem_QReverseIsCorrect_Calc(l: List)
ensures qreverse(l) == reverse(l);
{
calc {
qreverse(l);
// def. qreverse
revacc(l, Nil);
{ Lemma_Revacc_calc(l, Nil); }
concat(reverse(l), Nil);
{ Lemma_ConcatNil(reverse(l)); }
reverse(l);
}
}
lemma Lemma_Revacc_calc(xs: List, ys: List)
ensures revacc(xs, ys) == concat(reverse(xs), ys);
{
match (xs) {
case Nil =>
case Cons(x, xrest) =>
calc {
concat(reverse(xs), ys);
// def. reverse
concat(concat(reverse(xrest), Cons(x, Nil)), ys);
// induction hypothesis: Lemma_Revacc_calc(xrest, Cons(x, Nil))
concat(revacc(xrest, Cons(x, Nil)), ys);
{ Lemma_RevCatCommute(xrest); } // forall xs,ys,zs :: revacc(xs, concat(ys, zs)) == concat(revacc(xs, ys), zs)
revacc(xrest, concat(Cons(x, Nil), ys));
// def. concat (x2)
revacc(xrest, Cons(x, ys));
// def. revacc
revacc(xs, ys);
}
}
}
// Here is a version of the same proof, as it was constructed before Dafny's "calc" construct.
lemma Theorem_QReverseIsCorrect(l: List)
ensures qreverse(l) == reverse(l);
{
assert qreverse(l)
== // def. qreverse
revacc(l, Nil);
Lemma_Revacc(l, Nil);
assert revacc(l, Nil)
== concat(reverse(l), Nil);
Lemma_ConcatNil(reverse(l));
}
lemma Lemma_Revacc(xs: List, ys: List)
ensures revacc(xs, ys) == concat(reverse(xs), ys);
{
match (xs) {
case Nil =>
case Cons(x, xrest) =>
assert revacc(xs, ys)
== // def. revacc
revacc(xrest, Cons(x, ys));
assert concat(reverse(xs), ys)
== // def. reverse
concat(concat(reverse(xrest), Cons(x, Nil)), ys)
== // induction hypothesis: Lemma_Revacc(xrest, Cons(x, Nil))
concat(revacc(xrest, Cons(x, Nil)), ys);
Lemma_RevCatCommute(xrest); // forall xs,ys,zs :: revacc(xs, concat(ys, zs)) == concat(revacc(xs, ys), zs)
assert concat(revacc(xrest, Cons(x, Nil)), ys)
== revacc(xrest, concat(Cons(x, Nil), ys));
assert forall g: _T0, gs :: concat(Cons(g, Nil), gs) == Cons(g, gs);
assert revacc(xrest, concat(Cons(x, Nil), ys))
== // the assert lemma just above
revacc(xrest, Cons(x, ys));
}
}
/* Fibonacci */
// To further demonstrate what the "calc" construct can do, here are some proofs about the Fibonacci function.
function Fib(n: nat): nat
{
if n < 2 then n else Fib(n - 2) + Fib(n - 1)
}
lemma Lemma_Fib()
ensures Fib(5) < 6;
{
calc {
Fib(5);
Fib(4) + Fib(3);
<
calc {
Fib(2);
Fib(0) + Fib(1);
0 + 1;
1;
}
6;
}
}
/* List length */
// Here are some proofs that show the use of nested calculations.
lemma Lemma_Concat_Length(xs: List, ys: List)
ensures length(concat(xs, ys)) == length(xs) + length(ys);
{}
lemma Lemma_Reverse_Length(xs: List)
ensures length(xs) == length(reverse(xs));
{
match (xs) {
case Nil =>
case Cons(x, xrest) =>
calc {
length(reverse(xs));
// def. reverse
length(concat(reverse(xrest), Cons(x, Nil)));
{ Lemma_Concat_Length(reverse(xrest), Cons(x, Nil)); }
length(reverse(xrest)) + length(Cons(x, Nil));
// induction hypothesis
length(xrest) + length(Cons(x, Nil));
calc {
length(Cons(x, Nil));
// def. length
// 1 + length(Nil); // ambigious type parameter
// def. length
1 + 0;
1;
}
length(xrest) + 1;
// def. length
length(xs);
}
}
}
lemma Window(xs: List, ys: List)
ensures length(xs) == length(ys) ==> length(reverse(xs)) == length(reverse(ys));
{
calc {
length(xs) == length(ys) ==> length(reverse(xs)) == length(reverse(ys));
{ if (length(xs) == length(ys)) {
calc {
length(reverse(xs));
{ Lemma_Reverse_Length(xs); }
length(xs);
length(ys);
{ Lemma_Reverse_Length(ys); }
length(reverse(ys));
} } }
length(xs) == length(ys) ==> length(reverse(xs)) == length(reverse(xs));
true;
}
}
// In the following we use a combination of calc and forall
function ith<a>(xs: List, i: nat): a
requires i < length(xs);
{
match xs
case Cons(x, xrest) => if i == 0 then x else ith(xrest, i - 1)
}
lemma lemma_zero_length(xs: List)
ensures length(xs) == 0 <==> xs.Nil?;
{}
lemma lemma_extensionality(xs: List, ys: List)
requires length(xs) == length(ys); // (0)
requires forall i: nat | i < length(xs) :: ith(xs, i) == ith(ys, i); // (1)
ensures xs == ys;
{
match xs {
case Nil =>
calc {
true;
// (0)
length(xs) == length(ys);
0 == length(ys);
{ lemma_zero_length(ys); }
Nil == ys;
xs == ys;
}
case Cons(x, xrest) =>
match ys {
case Cons(y, yrest) =>
calc {
xs;
Cons(x, xrest);
calc {
x;
ith(xs, 0);
// (1) with i = 0
ith(ys, 0);
y;
}
Cons(y, xrest);
{
forall (j: nat | j < length(xrest)) {
calc {
ith(xrest, j);
ith(xs, j + 1);
// (1) with i = j + 1
ith(ys, j + 1);
ith(yrest, j);
}
}
lemma_extensionality(xrest, yrest);
}
Cons(y, yrest);
ys;
}
}
}
}
|