一个非常简单的乘法代码:
$('.payment_type').on('click', function () {
var row = $('input[name="payment_type[]"]:checked').val();
console.log('Row : ' + row)
});
当我用dafny验证时,它说:
method Product1 (m: nat, n: nat) returns (res:nat)
ensures res == m * n;
{
var m1: nat := 0;
var n1: nat := 0;
res := 0;
while (m1 < m)
{
n1 := 0;
while (n1 < n)
{
res := res + 1;
n1 := n1 + 1;
}
m1 := m1 + 1;
}
}
我知道在某些条件下,res!= m * n,但我无法弄明白。
答案 0 :(得分:3)
已更新!
在online website中尝试过dafny,看起来好像是bug?
Description Line Column
1 A postcondition might not hold on this return path. 8 4
2 This is the postcondition that might not hold. 2 16
更多尝试:
method Test(m: nat) returns (r: nat)
{
var m1: nat := 0;
while (m1 < m) {
m1 := m1 + 1;
}
assert m == m1; // fail assert
}
经过深入了解后,我知道应该使用method Test(m: nat) returns (r: nat)
{
var m1: nat := 0;
while (m1 < m) {
assert m1 < m;
m1 := m1 + 1;
}
assert !(m1 < m); // pass
assert m1 == m || m1 > m; // pass
assert m1 == m; // fail
}
让dafny来解决这个问题。
我修改过的代码:
Loop Invariants
然后删除tmp var:
method Product1 (m: nat, n: nat) returns (res:nat)
ensures res == m * n;
{
var m1: nat := 0;
var n1: nat := 0;
res := 0;
while (m1 < m)
invariant 0 <= m1 <= m
invariant res == m1 * n
{
var temp: nat := res;
n1 := 0;
while (n1 < n)
invariant 0 <= n1 <= n
invariant res == temp+n1
{
res := res + 1;
n1 := n1 + 1;
}
m1 := m1 + 1;
}
assert m1 == m; // success
}
答案 1 :(得分:0)
你的循环没有任何循环不变量。至少,您在两个循环中都需要不变量。否则,Dafny无法弄清楚循环后的内容......