Skip to content

Commit e4f2bc9

Browse files
author
Rustan Leino
committed
New test file, for recursive and iterative versions of McCarthy's 91 function
1 parent bf31e9f commit e4f2bc9

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

Test/dafny4/McCarthy91.dfy

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// RUN: %dafny /compile:3 /rprint:"%t.rprint" /autoTriggers:1 "%s" > "%t"
2+
// RUN: %diff "%s.expect" "%t"
3+
// The usual recursive method for computing McCarthy's 91 function
4+
5+
method Main() {
6+
var s := [3, 99, 100, 101, 1013];
7+
8+
var n := 0;
9+
while n < |s| {
10+
var m := M(s[n]);
11+
print "M(", s[n], ") = ", m, "\n";
12+
n := n + 1;
13+
}
14+
15+
n := 0;
16+
while n < |s| {
17+
print "mc91(", s[n], ") = ", mc91(s[n]), "\n";
18+
n := n + 1;
19+
}
20+
21+
n := 0;
22+
while n < |s| {
23+
var m := Mc91(s[n]);
24+
print "Mc91(", s[n], ") = ", m, "\n";
25+
n := n + 1;
26+
}
27+
28+
n := 0;
29+
while n < 5 {
30+
var m := iter(n, mc91, 40);
31+
print "iter(", n, ", mc91, 40) = ", m, "\n";
32+
n := n + 1;
33+
}
34+
}
35+
36+
method M(n: int) returns (r: int)
37+
ensures r == if n <= 100 then 91 else n - 10
38+
decreases 100 - n
39+
{
40+
if n <= 100 {
41+
r := M(n + 11);
42+
r := M(r);
43+
} else {
44+
r := n - 10;
45+
}
46+
}
47+
48+
// Same as above, but as a function
49+
50+
function method mc91(n: int): int
51+
ensures n <= 100 ==> mc91(n) == 91
52+
decreases 100 - n
53+
{
54+
if n <= 100 then
55+
mc91(mc91(n + 11))
56+
else
57+
n - 10
58+
}
59+
60+
// Iterating a function f e times starting from n
61+
62+
function method iter(e: nat, f: int -> int, n: int): int
63+
requires forall x :: f.requires(x) && f.reads(x) == {}
64+
{
65+
if e == 0 then n else iter(e-1, f, f(n))
66+
}
67+
68+
// Iterative version of McCarthy's 91 function, following in lockstep
69+
// what the recursive version would do
70+
71+
method Mc91(n0: int) returns (r: int)
72+
ensures r == mc91(n0)
73+
{
74+
var e, n := 1, n0;
75+
while e > 0
76+
invariant iter(e, mc91, n) == mc91(n0)
77+
decreases 100 - n + 10 * e, e
78+
{
79+
if n <= 100 {
80+
e, n := e+1, n+11;
81+
} else {
82+
e, n := e-1, n-10;
83+
}
84+
}
85+
return n;
86+
}

Test/dafny4/McCarthy91.dfy.expect

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Dafny program verifier finished with 8 verified, 0 errors
3+
Program compiled successfully
4+
Running...
5+
6+
M(3) = 91
7+
M(99) = 91
8+
M(100) = 91
9+
M(101) = 91
10+
M(1013) = 1003
11+
mc91(3) = 91
12+
mc91(99) = 91
13+
mc91(100) = 91
14+
mc91(101) = 91
15+
mc91(1013) = 1003
16+
Mc91(3) = 91
17+
Mc91(99) = 91
18+
Mc91(100) = 91
19+
Mc91(101) = 91
20+
Mc91(1013) = 1003
21+
iter(0, mc91, 40) = 40
22+
iter(1, mc91, 40) = 91
23+
iter(2, mc91, 40) = 91
24+
iter(3, mc91, 40) = 91
25+
iter(4, mc91, 40) = 91

0 commit comments

Comments
 (0)