How can I overwrite an array, which is marked as modified, in a method? Or is there a way in Dafny just increase the length of an array by one?
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;
}
}
}
As you can see in this code. I am trying to increase the length of an array by one in the method extendArrayByOne. After that I am adding the element operation at the end of the new array, which was returned from extendArrayByOne, in the method confirmAndCheck. Here is a link to a official compiler, which can compile this code: https://rise4fun.com/Dafny/WtjA
And here is the link to my previous question about extendArrayByOne: