assert Gcd(2,2)==2 fails #3971
-
Dafny version4.0.0 Code to produce this issueIn the Power User note KRML279[1], a Gcd function is defined. The code is also present at Test/dafny4/gcd.dfy. If in Main() one adds Test(2,2);
assert Gcd(2,2)==2; the assertion fails, even though the result of running the code is correct. [1] http://leino.science/papers/krml279.html Command to run and resulting output
What happened?I expected the assert to pass. What type of operating system are you experiencing the problem on?Mac |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
I converted the issue to a discussion because if Dafny can't prove something, it does not mean the thing isn't true or that Dafny is bogus, it only means Dafny needs more guidance. It's not because the Fermat theorem is true that Dafny should be able to prove Here is what you can enter to help Dafny prove what you want: assert Gcd(2,2)==2 by {
assert Factors(2) == {1, 2} by {
calc {
Factors(2);
set p: pos | p <= 2 && IsFactor(p, 2);
{
assert IsFactor(1, 2) by {
assert 1 * 2 == 2;
}
assert IsFactor(2, 2) by {
assert 2 * 1 == 2;
}
}
({1, 2});
}
}
assert {1, 2} * {1, 2} == {1, 2};
assert Max({1, 2}) == 2;
} Once you establish such a proof, you can try to omit some terms because Dafny can automate some of it. Here is the minimal proof I've found: assert Gcd(2,2)==2 by {
assert 1 * 2 == 2 * 1; // Just there to trigger IsFactor
assert Factors(2) == {1, 2};
assert Factors(2) * Factors(2) == {1, 2};
} |
Beta Was this translation helpful? Give feedback.
I converted the issue to a discussion because if Dafny can't prove something, it does not mean the thing isn't true or that Dafny is bogus, it only means Dafny needs more guidance. It's not because the Fermat theorem is true that Dafny should be able to prove
assert forall x, y, z, n | n > 2 ::Power(x, n) + Power(y, n) == Power(z, n) ==> x == y == z == 0
. Try to prove that GCD(2, 2) == 2 by hand. Given the mathematics definitions, it's not obvious.Here is what you can enter to help Dafny prove what you want: