<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Loop_variant</id>
	<title>Loop variant - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Loop_variant"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Loop_variant&amp;action=history"/>
	<updated>2026-05-07T17:57:36Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Loop_variant&amp;diff=7228199&amp;oldid=prev</id>
		<title>imported&gt;Cedar101: \mathtt</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Loop_variant&amp;diff=7228199&amp;oldid=prev"/>
		<updated>2021-08-24T08:05:26Z</updated>

		<summary type="html">&lt;p&gt;\mathtt&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In [[computer science]], a &amp;#039;&amp;#039;&amp;#039;loop variant&amp;#039;&amp;#039;&amp;#039; is a [[function (mathematics)|mathematical function]] defined on the [[State (computer science)|state space]] of a computer program whose value is monotonically decreased with respect to a (strict) [[well-founded relation]] by the iteration of a [[while loop]] under some [[Loop invariant|invariant conditions]], thereby [[Termination analysis|ensuring its termination]].  A loop variant whose range is restricted to the non-negative integers is also known as a &amp;#039;&amp;#039;&amp;#039;bound function&amp;#039;&amp;#039;&amp;#039;, because in this case it provides a trivial upper bound on the number of iterations of a loop before it terminates.  However, a loop variant may be [[Transfinite number|transfinite]], and thus is not necessarily restricted to integer values.&lt;br /&gt;
&lt;br /&gt;
A well-founded relation is characterized by the existence of a minimal element of every non-empty subset of its domain.  The existence of a variant proves the termination of a [[while loop]] in a computer program by &amp;#039;&amp;#039;&amp;#039;[[Transfinite induction|well-founded descent]]&amp;#039;&amp;#039;&amp;#039;.&amp;lt;ref&amp;gt;{{cite book|last=Winskel|first=Glynn|title=The Formal Semantics of Programming Languages: An Introduction|year=1993|publisher=Massachusetts Institute of Technology|pages=32–33, 174–176}}&amp;lt;/ref&amp;gt; A basic property of a well-founded relation is that it has no [[infinite descending chain]]s.  Therefore a loop possessing a variant will terminate after a finite number of iterations, as long as its body terminates each time.&lt;br /&gt;
&lt;br /&gt;
A [[while loop]], or, more generally, a computer program that may contain while loops, is said to be &amp;#039;&amp;#039;&amp;#039;totally correct&amp;#039;&amp;#039;&amp;#039; if it is [[Partial correctness|partially correct]] and it terminates.&lt;br /&gt;
&lt;br /&gt;
==Rule of inference for total correctness==&lt;br /&gt;
In order to formally state the rule of inference for the termination of a while loop we have demonstrated above, recall that in [[Floyd–Hoare logic]], the rule for expressing the partial correctness of a while loop is:&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{\{I \land C\}\;S\;\{I\}} {\{I\}\;\mathtt{while}\;C\; \mathtt{do}\; S \;\{I\land\lnot C\}},&amp;lt;/math&amp;gt;&lt;br /&gt;
where {{mvar|I}} is the &amp;#039;&amp;#039;[[loop invariant|invariant]]&amp;#039;&amp;#039;, &amp;#039;&amp;#039;C&amp;#039;&amp;#039; is the &amp;#039;&amp;#039;condition&amp;#039;&amp;#039;, and &amp;#039;&amp;#039;S&amp;#039;&amp;#039; is the &amp;#039;&amp;#039;body&amp;#039;&amp;#039; of the loop.  To express total correctness, we write instead:&lt;br /&gt;
:&amp;lt;math&amp;gt;\frac{&amp;lt; \text{ is well-founded},\;[I \land C \land V=z ]\;S\;[I \land V &amp;lt; z]}&lt;br /&gt;
            {[I]\;\mathtt{while}\;C\; \mathtt{do}\; S \;[I\land\lnot C]},&amp;lt;/math&amp;gt;&lt;br /&gt;
where, in addition, &amp;#039;&amp;#039;V&amp;#039;&amp;#039; is the &amp;#039;&amp;#039;variant&amp;#039;&amp;#039;, and by convention the unbound symbol &amp;#039;&amp;#039;z&amp;#039;&amp;#039; is taken to be [[Universal quantification|universally quantified]].&lt;br /&gt;
&lt;br /&gt;
==Every loop that terminates has a variant==&lt;br /&gt;
The existence of a variant implies that a while loop terminates.  It may seem surprising, but the converse is true, as well, as long as we assume the [[axiom of choice]]: every while loop that terminates (given its invariant) has a variant.  To prove this, assume that the loop&lt;br /&gt;
:&amp;lt;math&amp;gt;\mathtt{while}\;C\; \mathtt{do} \; S&amp;lt;/math&amp;gt;&lt;br /&gt;
terminates given the invariant {{mvar|I}} where we have the total correctness assertion&lt;br /&gt;
:&amp;lt;math&amp;gt;[I \land C ]\;S\;[I].&amp;lt;/math&amp;gt;&lt;br /&gt;
Consider the &amp;quot;successor&amp;quot; relation on the state space {{math|&amp;amp;Sigma;}} induced by the execution of the statement &amp;#039;&amp;#039;S&amp;#039;&amp;#039; from a state satisfying both the invariant {{mvar|I}} and the condition &amp;#039;&amp;#039;C&amp;#039;&amp;#039;.  That is, we say that a state {{mvar|&amp;amp;sigma;&amp;amp;prime;}} is a &amp;quot;successor&amp;quot; of {{mvar|&amp;amp;sigma;}} if and only if&lt;br /&gt;
* {{mvar|I}} and &amp;#039;&amp;#039;C&amp;#039;&amp;#039; are both true in the state {{mvar|&amp;amp;sigma;}}, and&lt;br /&gt;
* {{mvar|&amp;amp;sigma;&amp;amp;prime;}} is the state that results from the execution of the statement &amp;#039;&amp;#039;S&amp;#039;&amp;#039; in the state {{mvar|&amp;amp;sigma;}}.&lt;br /&gt;
We note that &amp;lt;math&amp;gt;\sigma&amp;#039; \neq \sigma,&amp;lt;/math&amp;gt; for otherwise the loop would fail to terminate.&lt;br /&gt;
&lt;br /&gt;
Next consider the reflexive, transitive closure of the &amp;quot;successor&amp;quot; relation.  Call this &amp;#039;&amp;#039;iteration&amp;#039;&amp;#039;: we say that a state {{mvar|&amp;amp;sigma;&amp;amp;prime;}} is an &amp;#039;&amp;#039;iterate&amp;#039;&amp;#039; of {{mvar|&amp;amp;sigma;}} if either  &amp;lt;math&amp;gt;\sigma&amp;#039; = \sigma,&amp;lt;/math&amp;gt; or there is a finite chain &amp;lt;math&amp;gt;\sigma_0, \sigma_1,\,\dots\,,\sigma_n&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;\sigma_0 = \sigma,&amp;lt;/math&amp;gt; &amp;lt;math&amp;gt;\sigma_n = \sigma&amp;#039;&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;\sigma_{i+1}&amp;lt;/math&amp;gt; is a &amp;quot;successor&amp;quot; of {{tmath|\sigma_i;}} for all {{mvar|I}}, &amp;lt;math&amp;gt;0 \le i &amp;lt; n.&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We note that if {{mvar|&amp;amp;sigma;}} and {{mvar|&amp;amp;sigma;&amp;amp;prime;}} are two distinct states, and {{mvar|&amp;amp;sigma;&amp;amp;prime;}} is an iterate of {{mvar|&amp;amp;sigma;}}, then {{mvar|&amp;amp;sigma;}} cannot be an iterate of {{mvar|&amp;amp;sigma;&amp;amp;prime;,}} for again, otherwise the loop would fail to terminate.  In other words, iteration is antisymmetric, and thus, a [[partial order]].&lt;br /&gt;
&lt;br /&gt;
Now, since the while loop terminates after a finite number of steps given the invariant {{mvar|I}}, and no state has a successor unless {{mvar|I}} is true in that state, we conclude that every state has only finitely many iterates, every descending chain with respect to iteration has only finitely many distinct values, and thus there is no [[infinite descending chain]], i.e. loop iteration satisfies the [[descending chain condition]].&lt;br /&gt;
&lt;br /&gt;
Therefore—assuming the [[axiom of choice]]—the &amp;quot;successor&amp;quot; relation we originally defined for the loop is [[Well-founded relation|well-founded]] on the state space {{math|&amp;amp;Sigma;}}, since it is strict (irreflexive) and contained in the &amp;quot;iterate&amp;quot; relation.  Thus the identity function on this state space is a variant for the while loop, as we have shown that the state must strictly decrease—as a &amp;quot;successor&amp;quot; and an &amp;quot;iterate&amp;quot;—each time the body &amp;#039;&amp;#039;S&amp;#039;&amp;#039; is executed given the invariant {{mvar|I}} and the condition &amp;#039;&amp;#039;C&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Moreover, we can show by a counting argument that the existence of any variant implies the existence of a variant in &amp;#039;&amp;#039;&amp;#039;{{var|ω}}&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;&amp;#039;&amp;#039;&amp;#039;, the [[first uncountable ordinal]], i.e.,&lt;br /&gt;
:&amp;lt;math&amp;gt;V:\Sigma\rightarrow\omega_1.&amp;lt;/math&amp;gt;&lt;br /&gt;
This is because the collection of all states reachable by a finite computer program in a finite number of steps from a finite input is countably infinite, and &amp;#039;&amp;#039;&amp;#039;{{var|ω}}&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;&amp;#039;&amp;#039;&amp;#039; is the enumeration of all [[well-order]] [[Order type|types]] on countable sets.&lt;br /&gt;
&lt;br /&gt;
==Practical considerations==&lt;br /&gt;
In practice, loop variants are often taken to be non-negative [[integer]]s, or even required to be so,&amp;lt;ref&amp;gt;{{cite web|url=http://archive.eiffel.com/doc/faq/variant.html|title=Why loop variants are integers|last=Bertrand Meyer|first=Michael Schweitzer|date=27 July 1995|work=The Eiffel Support Pages|publisher=Eiffel Software|accessdate=2012-02-23}}&amp;lt;/ref&amp;gt; but the requirement that every loop have an integer variant removes the expressive power of &amp;#039;&amp;#039;&amp;#039;[[μ operator|unbounded iteration]]&amp;#039;&amp;#039;&amp;#039; from a programming language.  Unless such a (formally verified) language allows a transfinite proof of termination for some other equally powerful construct such as a [[Recursion (computer science)|recursive function call]], it is no longer capable of full &amp;#039;&amp;#039;&amp;#039;[[μ-recursive function|μ-recursion]]&amp;#039;&amp;#039;&amp;#039;, but only &amp;#039;&amp;#039;&amp;#039;[[Primitive recursive function|primitive recursion]]&amp;#039;&amp;#039;&amp;#039;.  [[Ackermann&amp;#039;s function]] is the canonical example of a recursive function that cannot be computed in a [[For loop|loop with an integer variant]].&lt;br /&gt;
&lt;br /&gt;
In terms of their [[Computational complexity theory|computational complexity]], however, functions that are not primitive recursive lie far beyond the realm of what is usually considered [[tractable problem|tractable]].  Considering even the simple case of exponentiation as a primitive recursive function, and that the composition of primitive recursive functions is primitive recursive, one can begin to see how quickly a primitive recursive function can grow.  And any function that can be computed by a [[Turing machine]] in a running time bounded by a primitive recursive function is itself primitive recursive.  So it is difficult to imagine a practical use for full &amp;#039;&amp;#039;μ&amp;#039;&amp;#039;-recursion where primitive recursion will not do, especially since the former can be simulated by the latter up to exceedingly long running times.&lt;br /&gt;
&lt;br /&gt;
And in any case, [[Kurt Gödel]]&amp;#039;s first [[Gödel&amp;#039;s incompleteness theorems|incompleteness theorem]] and the [[halting problem]] imply that there are while loops that always terminate but cannot be proven to do so; thus it is unavoidable that any requirement for a formal proof of termination must reduce the expressive power of a programming language.  While we have shown that every loop that terminates has a variant, this does not mean that the well-foundedness of the loop iteration can be proven.&lt;br /&gt;
&lt;br /&gt;
===Example===&lt;br /&gt;
Here is an example, in [[C_(programming_language)|C]]-like [[pseudocode]], of an integer variant computed from some upper bound on the number of iterations remaining in a while loop.  However, [[C_(programming_language)|C]] allows side effects in the evaluation of expressions, which is unacceptable from the point of view of formally verifying a computer program.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/** condition-variable, which is changed in procedure S() **/&lt;br /&gt;
bool C;&lt;br /&gt;
/** function, which computes a loop iteration bound without side effects **/&lt;br /&gt;
inline unsigned int getBound();&lt;br /&gt;
&lt;br /&gt;
/** body of loop must not alter V **/ &lt;br /&gt;
inline void S(); &lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    unsigned int V = getBound(); /* set variant equal to bound */&lt;br /&gt;
    assert(I); /* loop invariant */&lt;br /&gt;
    while (C) {&lt;br /&gt;
        assert(V &amp;gt; 0); /* this assertion is the variant&amp;#039;s raison d&amp;#039;être (reason of existence) */&lt;br /&gt;
        S(); /* call the body */&lt;br /&gt;
        V = min(getBound(), V - 1); /* variant must decrease by at least one */&lt;br /&gt;
    };&lt;br /&gt;
    assert(I &amp;amp;&amp;amp; !C); /* invariant is still true and condition is false */&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Why even consider a non-integer variant?===&lt;br /&gt;
Why even consider a non-integer or transfinite variant?  This question has been raised because in all practical instances where we want to prove that a program terminates, we also want to prove that it terminates in a reasonable amount of time.  There are at least two possibilities:&lt;br /&gt;
&lt;br /&gt;
* An upper bound on the number of iterations of a loop may be conditional on proving termination in the first place.  It may be desirable to separately (or progressively) prove the three properties of&lt;br /&gt;
** partial correctness,&lt;br /&gt;
** termination, and&lt;br /&gt;
** running time.&lt;br /&gt;
* Generality: considering transfinite variants allows all possible proofs of termination for a while loop to be seen in terms of the existence of a variant.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[While loop]]&lt;br /&gt;
* [[Loop invariant]]&lt;br /&gt;
* [[Transfinite induction]]&lt;br /&gt;
* [[Descending chain condition]]&lt;br /&gt;
* [[Large countable ordinal]]&lt;br /&gt;
* [[Correctness (computer science)]]&lt;br /&gt;
* [[Predicate_transformer_semantics#While_loop | Weakest-preconditions of While loop]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Formal methods]]&lt;br /&gt;
[[Category:Control flow]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Cedar101</name></author>
	</entry>
</feed>