Skip to content

Program Termination

How can you ensure that your program will always terminate?

Program termination is all about finding a strictly-decreasing measure that's well-founded.

Mathematical Formality: Verification conditions

For a loop while G do S with invariant \(I\), pick a measure \(m\), a number computed from the program state. You need to show two things, where \(m\) is the measure at the start of an iteration, and \(m'\) is the measure at the end.

\[ \begin{aligned} &\text{Bounded:} && I \land G \implies m \ge 0 \\ &\text{Decreasing:} && I \land G \implies m' < m \end{aligned} \]

As a Hoare rule, with square brackets for total correctness,

\[ \dfrac{[I \land G]\ S\ [I \land m' < m] \qquad I \land G \implies m \ge 0} {[I]\ \text{while}\ G\ \text{do}\ S\ [I \land \lnot G]} \]

Why this works is that if the loop ran forever, \(m\) would give an infinite sequence \(m > m' > m'' > \dots \ge 0\) of integers, which can't exist. That's what well-founded means.

For recursion, the same two conditions apply to each recursive call instead of each iteration.

Proving the measure decreases

Take one iteration. Write \(m\) for the measure at the start, and \(m'\) for the measure at the end, by substituting what the body does. Then show

\[ \forall\, \text{state}.\ I \land G \implies m' < m \]

You get to assume \(I \land G\), since you're inside the loop. For recursion, \(m\) is the caller's measure and \(m'\) is the callee's.

Simple Cases

Consider a simple while loop

int i = 0;
while (i < 10) {
  i++;
}
The decreasing measure is 10 - i.

Mathematical Formality
\[ \forall i.\ i < 10 \implies \underbrace{10 - (i + 1)}_{m'} < \underbrace{10 - i}_{m} \]

Divide and Conquer Algorithms

The example I've given above might seem trivial.

Then what about this program, does it terminate?

/**
 * Returns the index of key in the array in the range of length len starting at start
 * Returns -1 if key not found
 */
int find_item(int key, int arr[], int start, int len) {
    if (len == 0) {
        // Empty range
        return -1;
    }

    int mid = start + len / 2;

    // If mid is the key, then we've found it
    if (arr[mid] == key) {
        return mid;
    }

    // Search on the left first
    int left_index = find_item(key, arr, start, len / 2);
    if (left_index != -1) {
        return left_index;
    }

    // If cannot find on the left, then search right
    return find_item(key, arr, mid + 1, len - len / 2 - 1);
}
Solution

The decreasing measure is len. Both recursive calls only happen when \(len \ge 1\).

\[ \forall len.\ len \ge 1 \implies \underbrace{len / 2}_{m'_\text{left}} < len \land \underbrace{len - len / 2 - 1}_{m'_\text{right}} < len \]

This only reasons about whether the function terminates. It says nothing about the correctness of the algorithm.

Not every program can be proved to terminate

It's not possible to prove termination for all programs. This follows from Gödel's first incompleteness theorem (1931). Any consistent proof system strong enough for arithmetic has true statements it can't prove. For termination, that means some programs halt on every input, but no proof of that exists in the system.

Example: Goodstein

A known example is computing Goodstein sequences. They always reach 0, but Peano arithmetic can't prove it (Kirby and Paris, 1982).

typedef unsigned long long u64;

u64 power(u64 base, u64 exp) {
    u64 result = 1;
    while (exp-- > 0) {
        result *= base;
    }
    return result;
}

// Write n in hereditary base b, then replace every b with b + 1
u64 bump(u64 n, u64 b) {
    u64 result = 0;
    u64 exp = 0;
    while (n > 0) {
        result += (n % b) * power(b + 1, bump(exp, b));
        n /= b;
        exp++;
    }
    return result;
}

// Number of steps for the Goodstein sequence of m to reach 0
u64 goodstein(u64 m) {
    u64 b = 2;
    u64 steps = 0;
    while (m > 0) {
        m = bump(m, b) - 1;
        b++;
        steps++;
    }
    return steps;
}

The numbers outgrow any fixed-size integer, so this only works for tiny m. goodstein(3) is 5, but starting from 4 the sequence goes 4, 26, 41, 60, 83, 109, ... and takes far longer than the age of the universe to come back down.

The proof that it terminates still uses a decreasing measure, but the measure is an ordinal below \(\varepsilon_0\), not a natural number, and Peano arithmetic can't prove that those ordinals are well-founded.

Example: Collatz

int collatz(u64 n) {
    if (n == 1) {
        return 0;
    }
    if (n % 2 == 0) {
        return 1 + collatz(n / 2);
    }
    return 1 + collatz(3 * n + 1);
}

Nobody knows whether this terminates for every \(n \ge 1\). That's the Collatz conjecture, still an open problem in mathematics. No one has found a decreasing measure for it.

Note

This is related to the halting problem (Turing, 1936), but not the same. The halting problem says no algorithm can automatically decide whether any program halts. Incompleteness says some termination facts have no proof in a given system, automated or by hand.