I am trying to write a dafny program that has an array of a fixed size. This array can then be added to via a method if it has not been filled and the values being added do not already exist in the array. At first it seemed to run fine, however, when I call the method more than 4 times, I get an error
SimpleTest.dfy(37,15): Error: A precondition for this call might not hold.
SimpleTest.dfy(17,23): Related location: This is the precondition that might not hold.
Execution trace:
(0,0): anon0
which highlights the line requires x !in arr[..]
from the MCVE below.
Why does the precondition start to fail once the method has been called more than four times? It seemingly happens no matter how large the fixed size of the array is
class {:autocontracts} Test
{
var arr: array<nat>;
var count: nat;
constructor(maxArrSize: nat)
requires maxArrSize > 1
ensures count == 0
ensures arr.Length == maxArrSize
ensures forall i :: 0 <= i < arr.Length ==> arr[i] == 0
{
arr := new nat[maxArrSize](_ => 0);
count := 0;
}
method AddIn(x: nat)
requires x !in arr[..]
requires x > 0
requires 0 < arr.Length
requires count < arr.Length
ensures arr[..] == old(arr[.. count]) + [x] + old(arr[count + 1 ..])
ensures count == old(count) + 1
ensures arr == old(arr)
{
arr[count] := x;
count := count + 1;
}
}
method Main()
{
var t := new Test(20);
t.AddIn(345);
t.AddIn(654);
t.AddIn(542);
t.AddIn(56);
t.AddIn(76);
t.AddIn(8786);
print t.arr[..];
print "\n";
print t.count;
print " / ";
print t.arr.Length;
print "\n";
}