// Array-writing basics // Methods must declare all arrays they are allowed to modify. // A caller of a method may assume that every other array retains its value. method SetEndPoints(a: array, left: int, right: int) requires a.Length != 0 modifies a { a[0] := left; a[a.Length - 1] := right; } // Array variables are allowed to *alias* each other, or have identical values. method Aliases(a: array, b: array) requires 100 <= a.Length modifies a { a[0] := 100; var c := a; if b == a { b[10] := b[0] + 1; } c[20] := a[14] + 2; } // The 'old' syntax lets us refer, in postconditions, to the values of arrays // when the method was called. method UpdateElements(a: array) requires a.Length == 10 modifies a ensures old(a[4]) < a[4] ensures a[6] <= old(a[6]) ensures a[8] == old(a[8]) { a[4], a[8] := a[4] + 3, a[8] + 1; a[7], a[8] := 516, a[8] - 1; } // Note that 'old' has an effect on array dereferencing but *not* on values of // function parameters. method OldVsParameters(a: array, i: int) returns (y: int) requires 0 <= i < a.Length modifies a ensures old(a[i] + y) == 25 // A confusion about that behavior leads to the following bad specification. // How should we modify the 'ensures' clause? method Increment(a: array, i: int) requires 0 <= i < a.Length modifies a ensures a[i] == old(a)[i] + 1 { a[i] := a[i] + 1; } // A method may return a new array. method NewArray() returns (a: array) ensures a.Length == 20 { a := new int[20]; var b := new int[30]; a[6] := 216; b[7] := 343; } // However, we must declare that the return value is *fresh* if we want a caller // to know this array is safe to modify, as it doesn't alias others from the // call. method NewArray'() returns (a: array) ensures a.Length == 20 ensures fresh(a) { a := new int[20]; var b := new int[30]; a[6] := 216; b[7] := 343; } method Caller() { var a := NewArray'(); a[8] := 512; } // Functions may read but not write arrays, and they must declare which arrays // they read. predicate IsZeroArray(a: array, lo: int, hi: int) requires 0 <= lo <= hi <= a.Length reads a decreases hi - lo { lo == hi || (a[lo] == 0 && IsZeroArray(a, lo + 1, hi)) } // No such clause is required for the analogous function on sequences. predicate IsZeroSeq(a: seq, lo: int, hi: int) requires 0 <= lo <= hi <= |a| decreases hi - lo { lo == hi || (a[lo] == 0 && IsZeroSeq(a, lo + 1, hi)) } // Let's now practice initializing arrays using loops. method InitArray(a: array, d: T) modifies a ensures forall i :: 0 <= i < a.Length ==> a[i] == d { var n := 0; while n != a.Length invariant 0 <= n <= a.Length invariant forall i :: 0 <= i < n ==> a[i] == d { a[n] := d; n := n + 1; } } // The same is doable in two dimensions. method InitMatrix(a: array2, d: T) modifies a ensures forall i, j :: 0 <= i < a.Length0 && 0 <= j < a.Length1 ==> a[i, j] == d { var m := 0; while m != a.Length0 invariant 0 <= m <= a.Length0 invariant forall i, j :: 0 <= i < m && 0 <= j < a.Length1 ==> a[i, j] == d { var n := 0; while n != a.Length1 invariant 0 <= n <= a.Length1 invariant forall i, j :: 0 <= i < m && 0 <= j < a.Length1 ==> a[i, j] == d invariant forall j :: 0 <= j < n ==> a[m, j] == d { a[m, n] := d; n := n + 1; } m := m + 1; } } // We can also modify prior array values. method IncrementArray(a: array) modifies a ensures forall i :: 0 <= i < a.Length ==> a[i] == old(a[i]) + 1 { var n := 0; while n != a.Length invariant 0 <= n <= a.Length invariant forall i :: 0 <= i < n ==> a[i] == old(a[i]) + 1 invariant forall i :: n <= i < a.Length ==> a[i] == old(a[i]) { // The sequence of 'assert' commands here documents how we realize the need // for the final loop invariant. assert a[n] + 1 == old(a[n]) + 1; a[n] := a[n] + 1; assert forall i :: 0 <= i < n ==> a[i] == old(a[i]) + 1; assert a[n] == old(a[n]) + 1; assert forall i :: 0 <= i < n + 1 ==> a[i] == old(a[i]) + 1; n := n + 1; assert forall i :: 0 <= i < n ==> a[i] == old(a[i]) + 1; } } // Note that this copying routine works even if the two arrays alias. method CopyArray(src: array, dst: array) requires src.Length == dst.Length modifies dst ensures forall i :: 0 <= i < src.Length ==> dst[i] == old(src[i]) ensures dst[..] == old(src[..]) { var n := 0; while n != src.Length invariant 0 <= n <= src.Length invariant forall i :: 0 <= i < n ==> dst[i] == old(src[i]) invariant forall i :: 0 <= i < src.Length ==> src[i] == old(src[i]) { dst[n] := src[n]; n := n + 1; } } // Here are some examples of handy syntax for initializing an array by giving a // function to produce its elements. method MakeSomeArrays(F: int -> int, n: nat, d: int) { var a1 := new int[25](F); var a2 := new int[25](i => i * i); var a3 := new int[n](_ => 0); var a4 := new int[50, 50]((i, j) => if i == j then d else 0); var b := new int[a1.Length](i requires 0 <= i < a1.Length reads a1 => a1[i]); // Note how this last example needs precondition information on an anonymous // function, to make an array access legal. } // A similar feature is available for sequences. method MakeASequence() { var s : seq := seq(500, i => i % 2 == 0); } // Check out the following examples of *aggregate* statements, to modify many // array cells at once without loops. method Aggregate1(a: array) ensures forall i :: 0 <= i < a.Length ==> a[i] == old(a[i]) + i * i modifies a { forall i | 0 <= i < a.Length { a[i] := a[i] + i * i; } } method Aggregate2(a: array) ensures forall i :: 0 <= i < a.Length ==> a[i] == if i % 2 == 0 then old(a[i]) + 1 else old(a[i]) modifies a { forall i | 0 <= i < a.Length && i % 2 == 0 { a[i] := a[i] + 1; } } method Aggregate3(a: array2, d: T) ensures forall i, j :: 0 <= i < a.Length0 && 0 <= j < a.Length1 ==> a[i, j] == d modifies a { forall i, j | 0 <= i < a.Length0 && 0 <= j < a.Length1 { a[i, j] := d; } } // Dafny has heuristics to confirm it can enumerate all possible values of the // variables used in an aggregate statement. // Dutch national flag // This example appeared in the associated reading. // Here it is for simplicity of accessing it in one place. // Here's an idiosyncratic sorting algorithm over arrays with elements from the // following type, with the given "<=" predicate. datatype Color = Red | White | Blue ghost predicate Below(c: Color, d: Color) { c == Red || c == d || d == Blue } // Nice: linear-time sorting in this special case! method DutchFlag(a: array) modifies a ensures forall i, j :: 0 <= i < j < a.Length ==> Below(a[i], a[j]) // In other words, the final array is sorted. ensures multiset(a[..]) == old(multiset(a[..])) // Note the above pattern to express that the "after" state of the array is a // permutation of the "before" state. { var r, w, b := 0, 0, a.Length; while w < b invariant 0 <= r <= w <= b <= a.Length // The array is divided into four regions, delineated by these variables. invariant forall i :: 0 <= i < r ==> a[i] == Red // The first region is all red. invariant forall i :: r <= i < w ==> a[i] == White // The second region is all white. invariant forall i :: b <= i < a.Length ==> a[i] == Blue // The fourth region is all blue. // And the third region is lawless! We don't know what's in there. // When the loop begins, *all* cells are in the third region. invariant multiset(a[..]) == old(multiset(a[..])) { // And now, some clever swaps and adjustments of region boundaries match a[w] case Red => a[r], a[w] := a[w], a[r]; r, w := r + 1, w + 1; case White => w := w + 1; case Blue => a[w], a[b-1] := a[b-1], a[w]; b := b - 1; } } // Now a slow sort that is able to work on infinite element types. method SelectionSort(a: array) modifies a ensures forall i, j :: 0 <= i < j < a.Length ==> a[i] <= a[j] ensures multiset(a[..]) == old(multiset(a[..])) { var n := 0; while n != a.Length invariant 0 <= n <= a.Length invariant forall i, j :: 0 <= i < j < n ==> a[i] <= a[j] invariant forall i, j :: 0 <= i < n <= j < a.Length ==> a[i] <= a[j] // That last invariant is the trickiest: all elements already sorted are // less than or equal to all elements not sorted yet. invariant multiset(a[..]) == old(multiset(a[..])) { var mindex, m := n, n + 1; while m != a.Length invariant n <= mindex < m <= a.Length invariant forall i :: n <= i < m ==> a[mindex] <= a[i] { if a[m] < a[mindex] { mindex := m; } m := m + 1; } a[n], a[mindex] := a[mindex], a[n]; assert forall i, j :: 0 <= i < j < n ==> a[i] <= a[j]; // That last assertion was inserted just as a check of how much Dafny knows // here. n := n + 1; } } // This predicate is a handy name for a pattern we just used in the trickiest // loop invariant. ghost predicate SplitPoint(a: array, n: int) requires 0 <= n <= a.Length reads a { forall i, j :: 0 <= i < n <= j < a.Length ==> a[i] <= a[j] }