Section 19.2.4

Inductive invariant and its three obligations

Definition

An inductive invariant is a condition on a system’s states that holds in every initial state and is preserved by every step; by induction on the number of steps, it holds in every state the system can reach. Using it to prove a safety property §1.11, a statement that no bad state is ever reached, takes three obligations. Initiation: every initial state satisfies it. Consecution: one step from any state that satisfies it leads to a state that satisfies it. Implication: every state that satisfies it satisfies the safety property.

Where it fits

Main entry: Loop invariant (§3.24).

The Loop invariant entry treats the most common case: a state is the values of the variables when execution reaches the loop test, a step is one run of the loop body taken when the test is true, and the third obligation, called exit there, delivers the loop’s result. The same three checks apply to any system described by initial states and steps, such as a protocol or a whole program, with a safety property as the target of the third. Treat a condition as the set of states that satisfy it. Write II for the candidate condition, S0S_0 for the set of initial states, post(I)\mathrm{post}(I) for the set of states one step away from some state in II, and PP for the set of states that satisfy the safety property. The three obligations are S0⊆I,post(I)⊆I,I⊆P.S_0 \subseteq I, \qquad \mathrm{post}(I) \subseteq I, \qquad I \subseteq P.

Two words must be kept apart. A condition is an invariant when it holds in every reachable state §3.21, a state that some finite sequence of steps leads to from an initial state. A condition is inductive when it passes consecution. Consecution ranges over every state that satisfies the condition, reachable or not, because which states are reachable is the very thing the proof sets out to establish. A condition that passes initiation and consecution is an invariant, by induction on the number of steps. The converse fails: a condition can hold in every reachable state while an unreachable state satisfies it and steps out of it.

Two integer variables start at x=y=0x = y = 0. While y<10y < 10, a step adds 1 to both; the arithmetic is that of mathematical integers, with no overflow. The reachable states are (x,y)=(0,0),(1,1),…,(10,10)(x, y) = (0, 0), (1, 1), \dots, (10, 10), so the safety property x≤10x \le 10 holds in all 11 of them. Yet x≤10x \le 10 is not inductive. The state (10,0)(10, 0) satisfies it, the test 0<100 < 10 allows a step, and the step leads to (11,1)(11, 1), which violates it. No run reaches (10,0)(10, 0), but consecution cannot use that fact.

The repair is to strengthen the condition with the relationship that excludes such states. Let II be the condition x=yx = y and 0≤x≤100 \le x \le 10. Initiation: (0,0)(0, 0) satisfies II. Consecution: a step needs y<10y < 10, so before it x=y≤9x = y \le 9; after it both variables equal the old xx plus 1, a value from 1 to 10, so II holds again. When y=10y = 10, no step is possible and there is nothing to check. Implication: every state that satisfies II has x≤10x \le 10. The stronger condition admits fewer states, and that is why it passes the check that the weaker one fails.

Limits and common mistakes

Consecution must cover every step the model allows: resets, error paths, interrupts §6.11 and the steps of other threads, not only the ordinary loop body. A condition shown inductive for a model that leaves out a step says nothing about runs that take that step.

Tests cannot establish consecution. A test visits only reachable states, so the condition x≤10x \le 10 above passes every test and still fails consecution.

Strengthening can overshoot. The condition x=yx = y and x≤5x \le 5 is false in the reachable state (6,6)(6, 6), and it fails consecution at (5,5)(5, 5). When the safety property is true, the set of reachable states itself passes all three obligations, so an inductive invariant always exists; the work is to find one that can be written down and checked. Finding one automatically is undecidable §19.2.1 in general.

An inductive invariant proves a safety property only. It says nothing about termination, or about something good eventually happening, which is a liveness property §1.12.

Questions answered

Why can the desired safety property fail to be inductive even when true?

A true safety property can fail to be inductive because the inductive step checks every state that satisfies the property, not only the states the system can reach, and an unreachable state can satisfy the property yet leave it in one step. Being true means holding in every reachable state. Being inductive means that one step from any state that satisfies the property, reachable or not, leads to a state that satisfies it again.

Example: xx and yy start at 0, and while y<10y < 10 both increase by 1. Every reachable state has x=y≤10x = y \le 10, so x≤10x \le 10 is true. The unreachable state x=10,y=0x = 10, y = 0 also satisfies x≤10x \le 10, and one step takes it to x=11x = 11, so the induction step, called consecution, fails. The repair is to strengthen the property with the facts that exclude such states. The condition x=yx = y and 0≤x≤100 \le x \le 10 holds initially, survives every step, and implies x≤10x \le 10.

Related entries: Hoare triple (§3.31), Relational abstract domain (§15.12), Safety property (§1.11), Inductive safety closure and widening termination (§19.2.8).