1
$\begingroup$

Would it be correct to characterize loop invariants as a type of tautology? I ask since the invariant must basically always be true, before the loop starts, before each iteration and after the loop terminates. I realize that there is the possibility that the invariant could become false during the body of the loop. But since inside the loop "doesn't count" is it fair to characterize the invariant as a tautology?

$\endgroup$

3 Answers 3

5
$\begingroup$

A Tautology is a formula (in a certain logic) that is true under every model of that logic. That is, it is equivalent to the formula "$True$".

A loop invariant, however, is a certain claim that is usually true under some models, and false under others (a model in this case is an algorithm). Then, you prove that the invariant is true under your specific model.

If you add axioms to your logic that forces then only model to be the one of your specific program, then indeed this would be a tautology. But such a process (adding axioms and proving that something is a tautology), is what is more commonly known as "proof". (For clarification: even if you add enough axioms, you may not be able to prove your claim, even if it's a tautology, in case of incomplete systems).

For example, consider a loop that increases a variable $i$ by $1$. An invariant of the loop may be that if before the loop $i>0$, then after the loop $i>0$. Indeed, this loop satisfies it. But it is not a tautology, since we can come up with other loops that do not satisfy it.

$\endgroup$
8
  • $\begingroup$ But since loop invariants are used typically in the context of proving the correctness of a specific loop / model then wouldn't it be correct, in that context, to call it a tautology? That seems to be what you're saying anyways in your third paragraph. $\endgroup$ Commented Mar 12, 2013 at 8:20
  • $\begingroup$ @RobertS.Barnes: If the loop doesn't satisfy the invariant, then it won't be true, thus it cannot be a tautology. $\endgroup$ Commented Mar 12, 2013 at 8:28
  • $\begingroup$ Mathematically, after axiomatically forcing the specific loop - it's ok to say it. But it is really not standard (or useful). It won't save you any work, or make anything clearer. $\endgroup$
    – Shaull
    Commented Mar 12, 2013 at 8:30
  • $\begingroup$ OK, I personally find it useful to be able to think about it in these terms - to each his own :-) $\endgroup$ Commented Mar 12, 2013 at 8:53
  • 2
    $\begingroup$ @RobertS.Barnes Because the inv is not always true, which is the defining property of a tautology. Say you have a logic $T$ and assumptions $G$, then $P$ is a tautology iff $T, G \vdash P$ regardless of the assumptions in $G$. That is not true for non-trivial loop invariants, which will depend on program-specific assumptions. Even if you consider only the loop body it is still not a tautology, because an invariant need not hold throughout the body. You can of course call it a tautology, but that only causes confusion since it is not in line with the common understanding of tautologies. $\endgroup$ Commented Mar 12, 2013 at 11:23
5
$\begingroup$

The word tautology is a technical word. The following is a tautology of classical propositional logic.

$\vdash p \lor \neg p$

When interpreted over the natural numbers, the following is a theorem.

$ (\mathbb{N},<)\vdash \forall x. \exists y. x < y$

But we do not say it is a tautology in the strict logical sense of the word because there are structures where this is not true.

Considering $S = \{a,b\}$, $<$ defined as $\{(a,b)\}$, we have $(S,<) \not \vdash \forall x. \exists y. x < y$

Similarly, if you think of a loop $P$ as implicitly defining the axioms of a theory, then a loop invariant $I$ as satisfying

$P \vdash \text{Every execution satisfies } I $

Thus, in exactly the same way as the existence of successors is a theorem of arithmetic, a loop invariant is a theorem of a logical theory defined by the program. A loop invariant is not a tautology in the standard mathematical sense of the word tautology. A tautology in this context would satisfy

$ \vdash \text{Every execution satisfies } I $

From which we can conclude that every tautology is a loop invariant, but not every loop invariant is a tautology.

$\endgroup$
4
$\begingroup$

I am guessing that, by "tautology," you mean a property that is true in all states. (I have seen some Lecturers use the term in that way, e.g., $x > 1 \Longrightarrow x > 0$, which is true in all states no matter what $x$ is, might be called a "tautology". The technical definition of "tautology" in logic is more narrow, but I will continue to use your terminology.)

A loop invariant is only true at a particular program point in the loop. It is true for every state encountered at that point, but it might be false for states encountered at other program points (inside the loop as well as outside the loop). So, clearly, it is not a "tautology" in the sense I stated above.

However, there is an interesting proof rule formulated in Reynolds's extension of Hoare Logic. If, in a particular piece of code, there are no operations that affect the truth/falsity of an assertion, and we know that the assertion is true at the beginning of the code, then we can pretend that the assertion is a "tautology" in the middle of that code.

A good example of this is a binary search procedure for an array $A$. Before the procedure starts, the pre-condition states that the array is sorted. Inside the binary search procedure, we don't do anything to modify the array. So, it will continue to be sorted throughout the procedure. Reynolds's rule says that, for the duration of the procedure, we can pretend that "$A$ is sorted" is a "tautology". This is a useful trick to use. Without it we would need to add "$A$ is sorted" in every assertion in the middle of the procedure, and we can see that it is quite pointless to keep repeating this silly condition because we are never modifying the array. Reynolds's rule allows to avoid the silliness.

For interesting applications of this rule, see the Chapter 5 of Reynolds's Craft of Programming.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.