【发布时间】:2016-07-27 09:23:48
【问题描述】:
我正在使用 Dafny 在您收到的地方创建一个删除方法:
字符数组
line数组长度
l一个职位
at要删除的字符数
p
首先删除at到at + p的行字符,然后必须将at + p右边的所有字符移动到at。
例如,如果你有[e][s][p][e][r][m][a]、at = 3、p = 3,那么最终结果应该是[e][s][p][a]
我正在尝试证明一个有意义的后置条件,例如:
ensures forall j :: (at<=j<l) ==> line[j] == old(line[j+p]);
确保 at + p 右侧的所有字符都在新位置。
但 Dafny 输出两个错误:
索引超出范围 7 53
后置条件可能在此返回路径上不成立。 19 2
method delete(line:array<char>, l:int, at:int, p:int)
requires line!=null;
requires 0 <= l <= line.Length && p >= 0 && at >= 0;
requires 0 <= at+p <= l;
modifies line;
ensures forall j :: (at<=j<l) ==> line[j] == old(line[j+p]) ;
{
var tempAt:int := at;
var tempAt2:int := at;
var tempPos:int := at+p;
while(tempAt < at + p)
invariant at<=tempAt<=at + p;
{
line[tempAt] := ' ';
tempAt := tempAt + 1;
}
while(tempPos < line.Length && tempAt2 < at + p)
invariant at + p<=tempPos<=line.Length;
invariant at<=tempAt2<=at+p;
{
line[tempAt2] := line[tempPos];
tempAt2 := tempAt2 + 1;
line[tempPos] := ' ';
tempPos := tempPos + 1;
}
}
【问题讨论】:
标签: induction formal-verification dafny