In computer science, a loop invariant is a property of a Computer 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 Hoare logic, loop invariants are expressed by formal predicate logic and used to prove properties of loops and by extension 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 (Search algorithm, sorting, optimization, arithmetic etc.), characterizing each of them from the viewpoint of its invariant.
Because of the similarity of loops and recursion 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.
int m = a[0];
// m equals the maximum value in a[0...0]
int i = 1;
while (i != n) {
// m equals the maximum value in a[0...i-1]
if (m < a[i])
m = a[i];
// m equals the maximum value in a[0...i]
++i;
// m equals the maximum value in a[0...i-1]
}
// m equals the maximum value in a[0...i-1], and i==n
return m;
}
This means:
In other words: The rule above is a deductive step that has as its premise the Hoare triple . This triple is actually a relation on machine states. It holds whenever starting from a state in which the boolean expression is true and successfully executing some code called , the machine ends up in a state in which is true. If this relation can be proven, the rule then allows us to conclude that successful execution of the program will lead from a state in which is true to a state in which holds. The Boolean formula 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 P { seq } Q mean that if P is true before the sequence of statements seq runs, then Q is true after it. Then the invariant relation theorem holds that
while (x < 10)
x := x+1;
One can then prove the following Hoare triple:
The condition C of the while loop is . A useful loop invariant has to be guessed; it will turn out that is appropriate. Under these assumptions, it is possible to prove the following Hoare triple:
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 is true, which means simply that is true. The computation adds 1 to , which means that is still true (for integer x).
Under this premise, the rule for while loops permits the following conclusion:
However, the post-condition ( is less than or equal to 10, but it is not less than 10) is logically equivalent to , which is what we wanted to show.
The property is another invariant of the example loop, and the trivial property is another one. Applying the above inference rule to the former invariant yields . Applying it to invariant yields , which is slightly more expressive.
x := 0
invariant
x <= 10
until
x > 10
loop
x := x + 1
end
//
nat i = 1
int m = items[0]
//
while i < |items|
// (1) No item seen so far is larger than m
where all { k in 0..i | items[k] <= m }
// (2) One or more items seen so far matches m
where some { k in 0..i | items[k] == m }:
if items[i] > m:
m = items[i]
i = i + 1
//
return m
The max() 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 max() 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 where 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 i, whilst the postconditions identify the result as being correct for all elements.
For 1., a natural language comment (like // m equals the maximum value in a[0...i-1] in the above example) is sufficient.
For 2., programming language support is required, such as the C library assert.h, or the above-shown invariant 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 invariants of given code automatically. However, this approach is limited to very simple invariants (such as 0<=i && i<=n && i%2==0).
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:
|
|