如何在方法中覆盖标记为已修改的数组? 或者Dafny中有没有办法只增加一个数组的长度?
class ownerIndexs{
var oi : map<int, int>;
constructor(){
new;
}
}
class Pendingstate{
var yetNeeded : int;
var ownersDone : bv256;
var index : int;
}
class mo{
var m_pendingIndex : array<int>;
var m_ownerIndex : ownerIndexs;
var m_pending : map<int, Pendingstate>;
var m_required : int;
method confirmAndCheck(operation : int, msgsender : int) returns
(boo : bool, ownerIndex :int,pending : Pendingstate)
requires m_pendingIndex != null
modifies this.m_pendingIndex
ensures m_pendingIndex != null && pending != null
==> 0 < pending.index < m_pendingIndex.Length
&& m_pendingIndex[pending.index] == operation
{
pending := new Pendingstate;
pending.index := m_pendingIndex.Length;
this.m_pendingIndex := extendArrayByOne(this.m_pendingIndex); //Problem with modify clause
m_pendingIndex[pending.index] := operation;
}
method extendArrayByOne(oldarray:array<int>) returns (newarray:array<int>)
requires oldarray!=null
ensures newarray != null
ensures fresh(newarray)
ensures newarray.Length == oldarray.Length+1
ensures forall k::0 <= k <oldarray.Length ==> oldarray[k] == newarray[k]
modifies oldarray
{
newarray := new int[oldarray.Length+1];
var i:=0;
while (i < oldarray.Length)
invariant newarray.Length == oldarray.Length+1
invariant i<=oldarray.Length
invariant forall k::0 <= k < i ==> oldarray[k] == newarray[k]
{
newarray[i] := oldarray[i];
i := i + 1;
}
}
}
正如您在此代码中看到的那样。 我试图在extendArrayByOne方法中将数组的长度增加一。 之后,我在新数组的末尾添加了元素操作,该操作是从extendArrayByOne返回的方法confirmAndCheck中添加的。 这是一个官方编译器的链接,可以编译这段代码: https://rise4fun.com/Dafny/WtjA
以下是我之前关于extendArrayByOne的问题的链接: