In computer science, a loop invariant is an invariant used to prove properties of loops. Informally, a loop invariant is a statement of the conditions that should be true on entry into a loop and that are guaranteed to remain true on every iteration of the loop. This means that on exit from the loop both the loop invariant and the loop termination condition can be guaranteed.
Specifically in Floyd-Hoare logic[1][2], the partial correctness of a while loop is governed by the following rule of inference:
This means:
- A while loop does not have the side effect of falsifying I—if the loop's body does not change an invariant I from true to false given the condition C, then I will still be true after the loop has run as long as it was true before.
- while(C)... runs as long as the condition C is true—after the loop has run, if it terminates, C is false.
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 program called 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 while (C) body will lead from a state in which I is true to a state in which
holds. The boolean formula I in this rule is known as the loop invariant.
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:
The condition C of the while loop is x < 10. A useful loop invariant I is
. 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 x < 10 is true. The computation adds 1 to x, 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
(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 loop invariant plays an important role in the intuitive argument for soundness of the Floyd-Hoare rule for while loops. The loop invariant has to be true before each iteration of the loop body, and also after each iteration of the loop body. Since a while loop is precisely the repeated iteration of the loop body, it follows that if the invariant is true before entering the loop, it must also be true after exiting the loop.
Because of the fundamental similarity of loops and recursive programs, proving partial correctness of loops with invariants is very similar to proving correctness of recursive programs via induction. In fact, the loop invariant is often the inductive property one has to prove of a recursive program that is equivalent to a given loop.
See also
References
- ^ R. W. Floyd. "Assigning meanings to programs." Proceedings of the American Mathematical Society Symposia on Applied Mathematics. Vol. 19, pp. 19–31. 1967. ([1])
- ^ C. A. R. Hoare. "An axiomatic basis for computer programming". Communications of the ACM, 12(10):576–585, October 1969. doi:10.1145/363235.363259
Further reading
- Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Pages 17–19 of section 2.1: Insertion sort.
- David Gries. "A note on a standard strategy for developing loop invariants and loops." Science of Computer Programming, vol 2, pp.207–214. 1984.
- Michael D. Ernst, Jake Cockrell, William G. Griswold, David Notkin. "Dynamically Discovering Likely Program Invariants to Support Program Evolution." International Conference on Software Engineering, pp.213–224. 1999.
- Robert Paige. "Programming with Invariants." IEEE Software, 3(1):56–69. January 1986.
- Yanhong A. Liu, Scott D. Stoller, and Tim Teitelbaum. Strengthening Invariants for Efficient Computation. Science of Computer Programming, 41(2):139–172. October 2001.
- Michael Huth, Mark Ryan. "Logic in Computer Science.", Second Edition.
This entry is from Wikipedia, the leading user-contributed encyclopedia. It may not have been reviewed by professional editors (see full disclaimer)








