Arrays and quantifiers

An array is many objects at once, and most facts about one are facts about all of its elements: every element was written, none is negative, none is larger than the result. This chapter indexes a local array, passes a range of elements to a function, and then states a fact about every element with cerastes_forall: in a loop that fills an array, and in the contract of a function that finds the largest element.

A local array

#include <cerastes.h>

int mark_and_read(int i)
    CERASTES_REQUIRES(0 <= i && i < 3)
    CERASTES_ENSURES(r, r == 7)
{
  int a[3] = {0, 0, 0};
  a[i] = 7;
  return a[i];
}
arrays_local.c: verified (1 function; one translation unit: assumes that functions defined elsewhere satisfy the contracts declared here)

The precondition keeps i inside the array, so the write and the read are both within its three elements, and the read returns what was written. Every index is checked against the array’s bound. Let i reach 3, one past the end:

#include <cerastes.h>

int mark_and_read(int i)
    CERASTES_REQUIRES(0 <= i && i <= 3)
    CERASTES_ENSURES(r, r == 7)
{
  int a[3] = {0, 0, 0};
  a[i] = 7;
  return a[i];
}
arrays_local_oob.c:13: error: out-of-bounds: writes `a[i]`, whose index may be outside `a`

The error is at the write, the first access that may fall outside. In C a write one past the end of an array is undefined behaviour, even when nothing seems to go wrong; the address &a[3] may be formed, but never used to read or write.

A function over a range

A function that works on an array receives a pointer, and its contract says how many elements it may use: cerastes_reads_range(a, n) to read the n elements from a, cerastes_owns_range(a, n) to read and write them.

#include <cerastes.h>

int middle(const int *a, int n)
    CERASTES_REQUIRES(cerastes_reads_range(a, n) && n >= 3)
    CERASTES_ENSURES(r, cerastes_reads_range(a, n) && r == a[1])
{
  (void)n;
  return a[1];
}

void set_middle(int *a, int n, int v)
    CERASTES_REQUIRES(cerastes_owns_range(a, n) && n >= 3)
    CERASTES_ENSURES(cerastes_owns_range(a, n) && a[1] == v)
{
  (void)n;
  a[1] = v;
}

int caller(void)
    CERASTES_ENSURES(r, r == 9)
{
  int a[4] = {1, 2, 3, 4};
  set_middle(a, 4, 9);
  return middle(a, 4);
}
arrays_range.c: verified (3 functions; one translation unit: assumes that functions defined elsewhere satisfy the contracts declared here)

set_middle owns the range, so it may write a[1], and gives the range back in its ensures, with the element’s new value. middle only reads. caller passes its local array, whose four elements cover both ranges, and learns from the two contracts that the result is 9. A caller that passed an array of two elements, or that held the array only for reading, would be reported at the call.

A fact about every element

A loop that fills an array declared without an initializer has to say which elements it has written, or nothing is known about them after the loop. A fact about every element is written with cerastes_forall(f, a, i): f is a pure function whose one return states the fact for the element k, and the quantifier says that it holds for every k.

#include <cerastes.h>

CERASTES_PURE bool filled(int k, const int *a, int m)
{
  return !(0 <= k && k < m) || a[k] == k;
}

int first_two(void)
    CERASTES_ENSURES(r, r == 1)
{
  int a[4];
  int i = 0;
  CERASTES_INVARIANT(0 <= i && i <= 4 && cerastes_forall(filled, a, i))
  CERASTES_DECREASES(4 - i)
  while (i < 4) {
    a[i] = i;
    i = i + 1;
  }
  return a[0] + a[1];
}
arrays_fill.c: verified (1 function; one translation unit: assumes that functions defined elsewhere satisfy the contracts declared here)

filled(k, a, m) says that if k is below m, a[k] holds k. The invariant states it for every element below i: before the loop no element is claimed, and each iteration writes a[i] and moves i past it. After the loop i is 4, so every element is claimed, and a[0] + a[1] is 1. An invariant that reads elements also says that they hold values, so the read after the loop is not reported as uninitialized.

The arguments after filled are passed to its parameters in order, and k takes every value. f is not called: its body is read as part of the annotation, and it must be a pure function with one return (forall has the rules).

An invariant that claims one element too many fails before the loop starts:

#include <cerastes.h>

CERASTES_PURE bool filled(int k, const int *a, int m)
{
  return !(0 <= k && k < m) || a[k] == k;
}

int first_two(void)
    CERASTES_ENSURES(r, r == 1)
{
  int a[4];
  int i = 0;
  CERASTES_INVARIANT(0 <= i && i <= 4 && cerastes_forall(filled, a, i + 1)) // <- error
  CERASTES_DECREASES(4 - i)
  while (i < 4) {
    a[i] = i;
    i = i + 1;
  }
  return a[0] + a[1];
}
arrays_fill_offbyone.c:18: error: uninitialized: in `filled`: this loop invariant reads `a[k]` before it is given a value
arrays_fill_offbyone.c:10: note: this is the operation in `filled`

On entry i is 0, and the invariant claims a[0], which nothing has written. The error is reported at the invariant, the place that claims too much, and the note shows the line of filled that reads the element: one pure function can serve many quantifiers.

The largest element

A contract can state a fact about every element too. max_of promises a result that no element exceeds:

#include <cerastes.h>

CERASTES_PURE bool at_most(int k, const int *a, int n, int m)
{
  return !(0 <= k && k < n) || a[k] <= m;
}
CERASTES_PURE bool below(int k, const int *a, int n, int m)
{
  return !(0 <= k && k < n) || a[k] < m;
}

int max_of(const int *a, int n)
    CERASTES_REQUIRES(cerastes_reads_range(a, n) && n > 0)
    CERASTES_ENSURES(r, cerastes_reads_range(a, n) && cerastes_forall(at_most, a, n, r))
{
  int m = a[0];
  int i = 1;
  CERASTES_INVARIANT(1 <= i && i <= n && cerastes_forall(at_most, a, i, m))
  CERASTES_DECREASES(n - i)
  while (i < n) {
    if (a[i] > m) {
      m = a[i];
    }
    i = i + 1;
  }
  return m;
}
arrays_max.c: verified (1 function; one translation unit: assumes that functions defined elsewhere satisfy the contracts declared here)

The loop’s invariant is the postcondition for the elements seen so far: every element below i is at most m. When the loop ends, i is n, and the invariant is the ensures. A caller that knows the elements can use the promise for any one of them.

Promise a little more, that every element is below the result, and the proof fails: the element the result was taken from is equal to it, not below it.

#include <cerastes.h>

CERASTES_PURE bool at_most(int k, const int *a, int n, int m)
{
  return !(0 <= k && k < n) || a[k] <= m;
}
CERASTES_PURE bool below(int k, const int *a, int n, int m)
{
  return !(0 <= k && k < n) || a[k] < m;
}

int max_of(const int *a, int n)
    CERASTES_REQUIRES(cerastes_reads_range(a, n) && n > 0)
    CERASTES_ENSURES(r, cerastes_reads_range(a, n) && cerastes_forall(below, a, n, r))
{
  int m = a[0];
  int i = 1;
  CERASTES_INVARIANT(1 <= i && i <= n && cerastes_forall(at_most, a, i, m))
  CERASTES_DECREASES(n - i)
  while (i < n) {
    if (a[i] > m) {
      m = a[i];
    }
    i = i + 1;
  }
  return m;
}
arrays_max_strict.c:31: error: postcondition: the postcondition of `max_of` may not hold
arrays_max_strict.c:19: note: this is the postcondition

The exact sum

A quantifier says something about each element. The sum of the elements is one value computed from all of them, and a contract names it with a pure function that cerastes verify reads: here prefix_sum(a, n) adds a[0] to a[n - 1], one element at a time from the end.

#include <cerastes.h>

CERASTES_PURE long long prefix_sum(const int *a, int n)
    CERASTES_REQUIRES(0 <= n && cerastes_reads_range(a, n))
    CERASTES_ENSURES(r, -2147483648LL * n <= r && r <= 2147483647LL * n)
    CERASTES_DECREASES(n)
{
  return n == 0 ? 0 : prefix_sum(a, n - 1) + a[n - 1];
}

long long sum(const int *a, int n)
    CERASTES_REQUIRES(0 <= n && cerastes_reads_range(a, n))
    CERASTES_ENSURES(r, cerastes_reads_range(a, n) && r == prefix_sum(a, n))
{
  long long s = 0;
  int i = 0;
  CERASTES_INVARIANT(0 <= i && i <= n && s == prefix_sum(a, i))
  CERASTES_DECREASES(n - i)
  while (i < n) {
    s = s + a[i];
    i = i + 1;
  }
  return s;
}
arrays_sum.c: verified (2 functions; one translation unit: assumes that functions defined elsewhere satisfy the contracts declared here)

prefix_sum has a contract of its own, and cerastes verify checks it once:

  • cerastes_reads_range(a, n) in its requires says which elements its body may read; a read outside them would be an error in prefix_sum.

  • CERASTES_DECREASES(n) is its measure: the recursive call is made with n - 1, so the recursion ends.

  • The ensures bounds the result. The body computes in long long, and the bound is what shows that prefix_sum(a, n - 1) + a[n - 1] cannot overflow; a caller that adds to a sum learns the same bound from it.

The loop’s invariant says that s is the sum of the elements read so far. On entry i is 0 and the sum of no element is 0; after an iteration, prefix_sum(a, i + 1) is prefix_sum(a, i) + a[i], which cerastes verify gets by unfolding the definition once; on exit i is n, and the invariant is the ensures. Each call of prefix_sum in an annotation is checked where it stands: the invariant may call it only over elements that the loop holds.

Claim one element too many, s == prefix_sum(a, i + 1), and the proof fails in several ways at once (the requires also bounds n below INT_MAX, so that i + 1 cannot overflow and the errors are only these): the invariant reads a[i], which the loop does not hold when i is n; it is false on entry; an iteration does not keep it; and the ensures, which rests on the invariant, may not hold at the return.

#include <cerastes.h>

CERASTES_PURE long long prefix_sum(const int *a, int n)
    CERASTES_REQUIRES(0 <= n && cerastes_reads_range(a, n))
    CERASTES_ENSURES(r, -2147483648LL * n <= r && r <= 2147483647LL * n)
    CERASTES_DECREASES(n)
{
  return n == 0 ? 0 : prefix_sum(a, n - 1) + a[n - 1];
}

long long sum(const int *a, int n)
    CERASTES_REQUIRES(0 <= n && n < 2147483647 && cerastes_reads_range(a, n))
    CERASTES_ENSURES(r, cerastes_reads_range(a, n) && r == prefix_sum(a, n))
{
  long long s = 0;
  int i = 0;
  CERASTES_INVARIANT(0 <= i && i <= n && s == prefix_sum(a, i + 1)) // <- error
  CERASTES_DECREASES(n - i)
  while (i < n) {
    s = s + a[i];
    i = i + 1;
  }
  return s;
}
arrays_sum_offbyone.c:25: error: invariant-entry: this loop invariant may not hold on entry
arrays_sum_offbyone.c:25: error: invariant-preserved: this loop invariant may not be preserved by an iteration
arrays_sum_offbyone.c:25: error: permission: this loop invariant calls `prefix_sum`, which reads `a[0..i + 1)`, which this loop does not hold
arrays_sum_offbyone.c:31: error: postcondition: the postcondition of `sum` may not hold
arrays_sum_offbyone.c:21: note: this is the postcondition

A value computed from an array lists what cerastes verify does not compute by itself, and Pure functions the rules for a pure function’s body and contract.

Arrays in aggregates

An array may hold structures, and a contract speaks of a range of them as it does of a range of integers. Here are three points, a function that clears the y of each, and one that adds up their x:

#include <cerastes.h>

struct point {
  int x;
  int y;
};

void clear_y(struct point *p, int n)
    CERASTES_REQUIRES(cerastes_owns_range(p, n) && 0 <= n)
    CERASTES_ENSURES(cerastes_owns_range(p, n))
{
  int i = 0;
  CERASTES_INVARIANT(0 <= i && i <= n)
  CERASTES_DECREASES(n - i)
  while (i < n) {
    p[i].y = 0;
    i = i + 1;
  }
}

long long sum_x(struct point *p, int n)
    CERASTES_REQUIRES(cerastes_reads_range(p, n) && 0 <= n)
    CERASTES_ENSURES(r, cerastes_reads_range(p, n) && -2147483648LL * n <= r && r <= 2147483647LL * n)
{
  long long s = 0;
  int i = 0;
  CERASTES_INVARIANT(0 <= i && i <= n && -2147483648LL * i <= s && s <= 2147483647LL * i)
  CERASTES_DECREASES(n - i)
  while (i < n) {
    s = s + p[i].x;
    i = i + 1;
  }
  return s;
}

int demo(void)
    CERASTES_ENSURES(r, r == 1)
{
  struct point v[3] = {{1, 2}, {3, 4}, {5, 6}};
  clear_y(v, 3);
  long long s = sum_x(v, 3);
  return s <= 6442450941LL;
}
aggregates_points.c: verified (3 functions; one translation unit: assumes that functions defined elsewhere satisfy the contracts declared here)

cerastes_owns_range(p, n) gives clear_y every member of each of the n points, so the loop may write p[i].y; sum_x only reads, and asks for cerastes_reads_range(p, n). The caller holds its local array v and lends it to each in turn. sum_x returns a long long and states bounds for the sum that follow from n alone; saying what the sum is would take a function over the range, as in The exact sum above, and such a function cannot read a member of a structure yet.

Ownership is of objects, whatever pointer reaches them. A function that owns the range and also owns one member of one of its elements asks for that member twice:

#include <cerastes.h>

struct point {
  int x;
  int y;
};

void clear_y(struct point *p, int n, int *marker)
    CERASTES_REQUIRES(cerastes_owns_range(p, n) && 0 <= n && cerastes_owns(*marker))
    CERASTES_ENSURES(cerastes_owns_range(p, n) && cerastes_owns(*marker))
{
  int i = 0;
  CERASTES_INVARIANT(0 <= i && i <= n)
  CERASTES_DECREASES(n - i)
  while (i < n) {
    p[i].y = 0;
    i = i + 1;
  }
  *marker = 1;
}

int demo(void)
{
  struct point v[3] = {{1, 2}, {3, 4}, {5, 6}};
  clear_y(v, 3, &v[0].y); // <- error
  return v[0].x;
}
aggregates_points_alias.c:31: error: permission: passes `v[0..3)` and `v[0].y` to `clear_y`, which may be one object, once to own it

Passing a pointer to another object, a separate int, verifies. Ranges of structures has the rules, and Arrays of structures what a local array of structures can do.

An array may also hold arrays. A matrix is indexed twice, and each index is checked against its own bound, the second against the row it reaches into:

#include <cerastes.h>

int sum(void)
    CERASTES_ENSURES(r, 0 <= r && r <= 36)
{
  int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
  int s = 0;
  int i = 0;
  CERASTES_INVARIANT(0 <= i && i <= 2 && 0 <= s && s <= 18 * i)
  CERASTES_DECREASES(2 - i)
  while (i < 2) {
    int j = 0;
    CERASTES_INVARIANT(0 <= j && j <= 3 && 0 <= s && s <= 18 * i + 6 * j)
    CERASTES_DECREASES(3 - j)
    while (j < 3) {
      s += m[i][j];
      j += 1;
    }
    i += 1;
  }
  return s;
}
aggregates_matrix.c: verified (1 function; one translation unit: assumes that functions defined elsewhere satisfy the contracts declared here)

The outer invariant bounds the sum of the rows done so far, the inner one adds the cells of the current row, and each loop has its own measure. Neither loop writes to m, so nothing need be said about the matrix itself: its cells keep their values and the bounds i < 2 and j < 3 make both index checks hold. With j <= 3 as the inner loop’s condition the read is reported (out-of-bounds: the message reads m[i][j], whose second index may be outside m[i]), although m[0][3] would land on m[1][0] in memory: a row ends where its type says it does. Arrays of arrays has the rules, and how a function takes a matrix as a parameter.

What is next

A loop that changes an array in place, such as adding one to every element, needs to speak of each element’s value on entry to the function inside its invariant (cerastes_old in a loop invariant). That, pointer arithmetic (p + i), and a quantifier inside another are not supported yet; nor is a quantifier over a member of a structure or over the cells of a matrix. An array of structures may also be a global or a member of a structure (Global arrays, Array members). Arrays of scalars in global and static storage and as members of structures are on Global arrays and Array members. Facts about every element has the details of quantifiers in contracts, loops and checks, and the limits that remain.