Loop invariant

In computer science, a loop invariant is a property of a program loop that is true before (and after) each iteration. It is a logical assertion, sometimes checked with a code assertion. Knowing its invariant(s) is essential in understanding the effect of a loop.

In formal program verification, particularly the Floyd-Hoare approach, loop invariants are expressed by formal predicate logic and used to prove properties of loops and by extension algorithms that employ loops (usually correctness properties). The loop invariants will be true on entry into a loop and following each iteration, so that on exit from the loop both the loop invariants and the loop termination condition can be guaranteed.

From a programming methodology viewpoint, the loop invariant can be viewed as a more abstract specification of the loop, which characterizes the deeper purpose of the loop beyond the details of this implementation. A survey article covers fundamental algorithms from many areas of computer science (searching, sorting, optimization, arithmetic etc.), characterizing each of them from the viewpoint of its invariant.

Because of the similarity of loops and recursive programs, proving partial correctness of loops with invariants is very similar to proving the correctness of recursive programs via induction. In fact, the loop invariant is often the same as the inductive hypothesis to be proved for a recursive program equivalent to a given loop.

Informal example
The following C subroutine  returns the maximum value in its argument array , provided its length   is at least 1. Comments are provided at lines 3, 6, 9, 11, and 13. Each comment makes an assertion about the values of one or more variables at that stage of the function. The highlighted assertions within the loop body, at the beginning and end of the loop (lines 6 and 11), are exactly the same. They thus describe an invariant property of the loop. When line 13 is reached, this invariant still holds, and it is known that the loop condition  from line 5 has become false. Both properties together imply that  equals the maximum value in , that is, that the correct value is returned from line 14.

Following a defensive programming paradigm, the loop condition  in line 5 should better be modified to , in order to avoid endless looping for illegitimate negative values of. While this change in code intuitively shouldn't make a difference, the reasoning leading to its correctness becomes somewhat more complicated, since then only  is known in line 13. In order to obtain that also  holds, that condition has to be included into the loop invariant. It is easy to see that, too, is an invariant of the loop, since   in line 6 can be obtained from the (modified) loop condition in line 5, and hence   holds in line 11 after   has been incremented in line 10. However, when loop invariants have to be manually provided for formal program verification, such intuitively too obvious properties like  are often overlooked.

Floyd–Hoare logic
In Floyd–Hoare logic, the partial correctness of a while loop is governed by the following rule of inference:


 * $$\frac{\{C\land I\}\;\mathrm{body}\;\{I\}} {\{I\}\;\mathtt{while}\ (C)\ \mathrm{body}\;\{\lnot C\land I\}}$$

This means:
 * If some property $I$ is preserved by the code $$\mathrm{body}$$ —more precisely, if $I$ holds after the execution of $$\mathrm{body}$$ whenever both $C$ and $I$ held beforehand— (upper line) then
 * $C$ and $I$ are guaranteed to be false and true, respectively, after the execution of the whole loop $$\mathtt{while}\ (C)\ \mathrm{body}$$, provided $I$ was true before the loop (lower line).

In other words: The rule above is a deductive step that has as its premise the Hoare triple $$\{C\land I\}\;\mathrm{body}\;\{I\}$$. This triple is actually a relation on machine states. It holds whenever starting from a state in which the boolean expression $$C\land I$$ is true and successfully executing some code called $$\mathrm{body}$$, the machine ends up in a state in which $I$ is true. If this relation can be proven, the rule then allows us to conclude that successful execution of the program $$\mathtt{while}\ (C)\ \mathrm{body}$$ will lead from a state in which $I$ is true to a state in which $$\lnot C\land I$$ holds. The boolean formula $I$ in this rule is called a loop invariant.

With some variations in the notation used, and with the premise that the loop halts, this rule is also known as the Invariant Relation Theorem. As one 1970s textbook presents it in a way meant to be accessible to student programmers:

Let the notation  mean that if   is true before the sequence of statements   run, then   is true after it. Then the invariant relation theorem holds that


 * implies
 * implies

Example
The following example illustrates how this rule works. Consider the program

while (x < 10) x := x+1;

One can then prove the following Hoare triple:


 * $$\{x\leq10\}\; \mathtt{while}\ (x<10)\ x := x+1\;\{x=10\}$$

The condition C of the  loop is $$x<10$$. A useful loop invariant $I$ has to be guessed; it will turn out that $$x\leq10$$ is appropriate. Under these assumptions it is possible to prove the following Hoare triple:


 * $$\{x<10 \land x\leq10\}\; x := x+1 \;\{x\leq10\}$$

While this triple can be derived formally from the rules of Floyd-Hoare logic governing assignment, it is also intuitively justified: Computation starts in a state where $$x<10 \land x\leq10$$ is true, which means simply that $$x<10$$ is true. The computation adds 1 to $x$, which means that $$x\leq10$$ is still true (for integer x).

Under this premise, the rule for  loops permits the following conclusion:


 * $$\{x\leq10\}\; \mathtt{while}\ (x<10)\ x := x+1 \;\{\lnot(x<10) \land x\leq10\}$$

However, the post-condition $$\lnot(x<10)\land x\leq10$$ ($x$ is less than or equal to 10, but it is not less than 10) is logically equivalent to $$x=10$$, which is what we wanted to show.

The property $$0 \leq x$$ is another invariant of the example loop, and the trivial property $$\mathrm{true}$$ is another one. Applying the above inference rule to the former invariant yields $$\{0 \leq x\}\; \mathtt{while}\ (x<10)\ x := x+1\;\{10 \leq x\}$$. Applying it to invariant $$\mathrm{true}$$ yields $$\{\mathrm{true}\}\; \mathtt{while}\ (x<10)\ x := x+1\;\{10 \leq x\}$$, which is slightly more expressive.

Eiffel
The Eiffel programming language provides native support for loop invariants. A loop invariant is expressed with the same syntax used for a class invariant. In the sample below, the loop invariant expression  must be true following the loop initialization, and after each execution of the loop body; this is checked at runtime.

Whiley
The Whiley programming language also provides first-class support for loop invariants. Loop invariants are expressed using one or more  clauses, as the following illustrates:

The  function determines the largest element in an integer array. For this to be defined, the array must contain at least one element. The postconditions of  require that the returned value is: (1) not smaller than any element; and, (2) that it matches at least one element. The loop invariant is defined inductively through two  clauses, each of which corresponds to a clause in the postcondition. The fundamental difference is that each clause of the loop invariant identifies the result as being correct up to the current element, whilst the postconditions identify the result as being correct for all elements.

Use of loop invariants
A loop invariant can serve one of the following purposes:
 * 1) purely documentary
 * 2) to be checked within in the code, e.g. by an assertion call
 * 3) to be verified based on the Floyd-Hoare approach

For 1., a natural language comment (like  in the above example) is sufficient.

For 2., programming language support is required, such as the C library assert.h, or the above-shown  clause in Eiffel. Often, run-time checking can be switched on (for debugging runs) and off (for production runs) by a compiler or a runtime option.

For 3., some tools exist to support mathematical proofs, usually based on the above-shown Floyd–Hoare rule, that a given loop code in fact satisfies a given (set of) loop invariant(s).

The technique of abstract interpretation can be used to detect loop invariant of given code automatically. However, this approach is limited to very simple invariants (such as ).

Distinction from loop-invariant code
Loop-invariant code consists of statements or expressions that can be moved outside a loop body without affecting the program semantics. Such transformations, called loop-invariant code motion, are performed by some compilers to optimize programs. A loop-invariant code example (in the C programming language) is where the calculations  and   can be moved before the loop, resulting in an equivalent, but faster, program: In contrast, e.g. the property  is a loop invariant for both the original and the optimized program, but is not part of the code, hence it doesn't make sense to speak of "moving it out of the loop".

Loop-invariant code may induce a corresponding loop-invariant property. For the above example, the easiest way to see it is to consider a program where the loop invariant code is computed both before and within the loop: A loop-invariant property of this code is, indicating that the values computed before the loop agree with those computed within (except before the first iteration).