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 for the candidate condition, for the set of initial states, for the set of states one step away from some state in , and for the set of states that satisfy the safety property. The three obligations are
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 . While , a step adds 1 to both; the arithmetic is that of mathematical integers, with no overflow. The reachable states are , so the safety property holds in all 11 of them. Yet is not inductive. The state satisfies it, the test allows a step, and the step leads to , which violates it. No run reaches , but consecution cannot use that fact.
The repair is to strengthen the condition with the relationship that excludes such states. Let be the condition and . Initiation: satisfies . Consecution: a step needs , so before it ; after it both variables equal the old plus 1, a value from 1 to 10, so holds again. When , no step is possible and there is nothing to check. Implication: every state that satisfies has . 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 above passes every test and still fails consecution.
Strengthening can overshoot. The condition and is false in the reachable state , and it fails consecution at . 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: and start at 0, and while both increase by 1. Every reachable state has , so is true. The unreachable state also satisfies , and one step takes it to , so the induction step, called consecution, fails. The repair is to strengthen the property with the facts that exclude such states. The condition and holds initially, survives every step, and implies .
Related entries: Hoare triple (§3.31), Relational abstract domain (§15.12), Safety property (§1.11), Inductive safety closure and widening termination (§19.2.8).