在阅读了 Dafny 入门:指南之后,我决定创建我的第一个程序:给定一个整数序列,计算其元素的总和。但是,我很难让 Dafny 验证该程序。
function G(a: seq<int>): int
decreases |a|
{
if |a| == 0 then 0 else a[0] + G(a[1..])
}
method sum(a: seq<int>) returns (r: int)
ensures r == G(a)
{
r := 0;
var i: int := 0;
while i < |a|
invariant 0 <= i <= |a|
invariant r == G(a[0..i])
{
r := r + a[i];
i := i + 1;
}
}
我明白了
stdin.dfy(12,2): Error BP5003: A postcondition might not hold on this return path.
stdin.dfy(8,12): Related location: This is the postcondition that might not hold.
stdin.dfy(14,16): Error BP5005: This loop invariant might not be maintained by the loop.
我怀疑 Dafny 需要一些“帮助”来验证程序(可能是引理?),但我不知道从哪里开始。