Skip to content

Inductive Invariants

How to use a loop invariant to prove a program correct.

You need to show these three things:

  • Initialisation: The invariant holds before the loop.
  • Maintenance: If the invariant holds at the start of an iteration (and the guard is true, since you entered the loop), then it still holds at the end of the iteration.
  • Termination: When the loop exits, the invariant (and the guard is false, since you left the loop) gives you what you wanted.

This is just induction on the number of iterations. Initialisation is the base case, and maintenance is the inductive step, so the invariant holds after any number of iterations. That's why an invariant you can prove this way is called an inductive invariant.

In Hoare logic, write \(I\) for the invariant and \(G\) for the loop guard. Maintenance is then the premise of the while rule.

\[ \dfrac{\{I \land G\}\ S\ \{I\}}{\{I\}\ \text{while}\ G\ \text{do}\ S\ \{I \land \lnot G\}} \]

Example: Sum of odd numbers

As an example, let's sum up the first \(n\) odd numbers, \(1 + 3 + \dots + (2n - 1)\).

Goal: Show that the variable sum returned is \(n^2\). For example, \(n = 5\) gives \(1 + 3 + 5 + 7 + 9 = 25\).

int sum_odd(int n) {
    // Assume n >= 0
    int sum = 0;

    int i = 1;
    // Initialisation: 1 <= i <= n + 1 and sum == (i - 1)^2
    while (i <= n) {
        // Maintenance (start): 1 <= i <= n + 1 and sum == (i - 1)^2
        sum += 2 * i - 1;
        i++;
        // Maintenance (end): 1 <= i <= n + 1 and sum == (i - 1)^2
    }
    // Termination: i == n + 1 and sum == n^2

    return sum;
}

Formally, with \(n \ge 0\),

\[ \begin{aligned} I &\equiv 1 \le i \le n + 1 \land \text{sum} = (i - 1)^2 \\ G &\equiv i \le n \\ Q &\equiv \text{sum} = n^2 \end{aligned} \]

Informally, the variable sum stores the "sum-so-far" up till but not including \(2i - 1\).

Checking that initialisation and maintenance property holds

This part is not that important. For now, you can take it by faith that the loop invariant is correct. However, read on if you're interested.

Initialisation: After the setup, \(i = 1\) and \(\text{sum} = 0\). Then \(1 \le 1 \le n + 1\) since \(n \ge 0\), and \(0 = (1 - 1)^2\), so \(I\) holds.

Maintenance: Assume \(I \land G\) at the start of an iteration, so \(1 \le i \le n\) and \(\text{sum} = (i - 1)^2\). Write \(i'\) and \(\text{sum}'\) for the values at the end of the iteration. The body gives

\[ \begin{aligned} \text{sum}' &= \text{sum} + (2i - 1) = (i - 1)^2 + (2i - 1) = i^2 = (i' - 1)^2 \\ i' &= i + 1 \in [2, n + 1] \end{aligned} \]

so \(I\) holds for \(i'\) and \(\text{sum}'\).

Termination: When the loop exits, \(I \land \lnot G\) holds, that is

\[ 1 \le i \le n + 1 \land i > n \land \text{sum} = (i - 1)^2 \]

If you put the two information together, \(i = n + 1\) since \(i\) is an integer, and so \(\text{sum} = n^2\), which is \(Q\), what you were trying to prove in the beginning.

Example: Integer square root

This one shows why termination needs both the invariant and the false guard.

int isqrt(int n) {
    // Assume n >= 0
    int r = 0;
    // r^2 <= n
    while ((r + 1) * (r + 1) <= n) {
        r++;
    }
    // r^2 <= n < (r + 1)^2
    return r;
}
\[ \begin{aligned} I &\equiv r^2 \le n \\ G &\equiv (r + 1)^2 \le n \\ Q &\equiv r = \lfloor \sqrt{n} \rfloor \end{aligned} \]

Initialisation and maintenance are short. Before the loop, \(r = 0\) and \(0 \le n\). In the body, \(G\) gives \((r + 1)^2 \le n\), so after r++, \(r^2 \le n\) again.

Termination is the interesting part. On its own, \(I\) is weak, since \(r = 0\) satisfies it. On its own, \(\lnot G\) is weak too, since any big \(r\) satisfies it. Together they give

\[ r^2 \le n < (r + 1)^2 \]

which is exactly \(r = \lfloor \sqrt{n} \rfloor\).

Example: Fibonacci, strengthening the invariant

Sometimes the obvious invariant is true, but you can't prove it.

int fib(int n) {
    // Assume n >= 0
    int a = 0, b = 1;
    int i = 0;
    while (i < n) {
        int t = a + b;
        a = b;
        b = t;
        i++;
    }
    return a;
}

The obvious invariant is \(a = \text{fib}(i)\). Maintenance gets stuck, since the new \(a\) is the old \(b\), and the invariant says nothing about \(b\).

So strengthen it to also say what \(b\) is.

\[ \begin{aligned} I &\equiv 0 \le i \le n \land a = \text{fib}(i) \land b = \text{fib}(i + 1) \\ G &\equiv i < n \\ Q &\equiv a = \text{fib}(n) \end{aligned} \]

Now maintenance goes through, with \(i' = i + 1\).

\[ \begin{aligned} a' &= b = \text{fib}(i + 1) = \text{fib}(i') \\ b' &= a + b = \text{fib}(i) + \text{fib}(i + 1) = \text{fib}(i + 2) = \text{fib}(i' + 1) \end{aligned} \]

A stronger claim is easier to prove, because you get to assume more at the start of each iteration.

The \(1 \le i \le n + 1\) in the sum of odd numbers is the same trick. Without it, termination can't get \(i = n + 1\).

Summary

To prove \(Q\) after a loop, you don't prove \(Q\) directly. You find an invariant \(I\) and check initialisation and maintenance, so the while rule gives you \(\{I \land \lnot G\}\) after the loop. Then you show \(I \land \lnot G \implies Q\).

\(I\) doesn't have to look like \(Q\).

  • In the sum of odd numbers, \(I\) talks about \((i - 1)^2\), and only becomes \(n^2\) once \(\lnot G\) pins \(i = n + 1\).
  • In the integer square root, \(I\) is only half of \(Q\), and \(\lnot G\) supplies the other half.
  • In Fibonacci, \(I\) says more than \(Q\), since \(Q\) never mentions \(b\).

What \(I\) has to be is inductive, and strong enough that \(I \land \lnot G \implies Q\). If you get stuck proving either, strengthen the invariant, like in Fibonacci.

Partial vs total correctness

Everything above is partial correctness, which says that if the loop stops, then \(\text{sum} = n^2\). Whether the loop stops at all is a separate problem. Proving both gives you total correctness.