Verification 达夫尼,循环后post条件不成立

Verification 达夫尼,循环后post条件不成立,verification,dafny,Verification,Dafny,在下面的方法中,Dafny报告说后置条件可能不成立,尽管我很确定它成立 method toArrayConvert(s:seq<int>) returns (a:array<int>) requires |s| > 0 ensures |s| == a.Length ensures forall i :: 0 <= i < a.Length ==> s[i] == a[i] // This is the postcondi

在下面的方法中,Dafny报告说后置条件可能不成立,尽管我很确定它成立

method toArrayConvert(s:seq<int>) returns (a:array<int>)
    requires |s| > 0
    ensures |s| == a.Length
    ensures forall i :: 0 <= i < a.Length ==> s[i] == a[i]  // This is the postcondition that might not hold.
{
    a := new int[|s|];
    var i:int := 0;
    while i < |s|
        decreases |s| - i
        invariant 0 <= i <= |s|
    {
        a[i] := s[i];
        i := i + 1;
    }

    return a;  // A postcondition might not hold on this return path.
}
方法toArrayConvert(s:seq)返回(a:array)
需要| s |>0
确保| s |=a.长度
确保所有i::0 s[i]==a[i]//这是可能不成立的后置条件。
{
a:=新整数[|s |];
变量i:int:=0;
而我|
减少| s |-i

不变量0的确,后置条件总是成立的,但达夫尼说不出来

这是因为缺少循环不变注释,例如

invariant forall j :: 0 <= j < i ==> s[j] == a[j]
所有j的不变量::0s[j]==a[j]
将该行添加到循环后,该方法将进行验证

有关Dafny有时报告正确程序错误的更多解释,请参阅(全新)。有关循环不变量的更多信息,请参阅rise4fun中的相应部分。

谢谢,James:)我完全忘记了不变量