We consider affine integer loops of the form
|while do .||(1)|
Here, for some dimension ,
is a column vector of pairwise different variables, , and is a conjunction of inequalities of the form where is an affine expression with rational coefficients111Note that multiplying with the least common multiple of all denominators yields an equivalent constraint with integer coefficients, i.e., allowing rational instead of integer coefficients does not extend the considered class of loops. over (i.e., ). So has the form where is the vector containing zeros, , and for some . Def. 1 formalizes the intuitive notion of termination for such loops.
Definition 1 (Termination).
Here, denotes the -fold application of , i.e., we have and . We call the update of (1). Moreover, for any entity , denotes the entity that results from by replacing all occurrences of by . Similarly, if and , then denotes the entity resulting from by replacing all occurrences of by for each .
Consider the loop
where the update of all variables is executed simultaneously. This program belongs to our class of affine loops, because it can be written equivalently as follows.
While termination of affine loops is known to be decidable if the variables range over the real  or the rational numbers , the integer case is a well-known open problem [3, 4, 5, 14, 15].222The proofs for real or rational numbers do not carry over to the integers since  uses Brouwer’s Fixed Point Theorem which is not applicable if the variables range over and  relies on the density of in . However, certain special cases have been solved: Braverman  showed that termination of linear loops is decidable (i.e., loops of the form (1) where is and is of the form ). Bozga et al.  showed decidability for the case that the update matrix in (1) has the finite monoid property, i.e., if there is an such that
is diagonalizable and all eigenvalues ofare in Ouaknine et al.  proved decidability for the case and for the case that is diagonalizable.
Ben-Amram et al.  showed undecidability of termination for certain extensions of affine integer loops, e.g., for loops where the body is of the form where and .
In this paper, we present another substantial step towards the solution of the open problem whether termination of affine integer loops is decidable. We show that termination is decidable for triangular loops (1) where is a triangular matrix (i.e., all entries of below or above the main diagonal are zero). Clearly, the order of the variables is irrelevant, i.e., our results also cover the case that can be transformed into a triangular matrix by reordering , , and accordingly.333Similarly, one could of course also use other termination-preserving pre-processings and try to transform a given program into a triangular loop. So essentially, triangularity means that the program variables can be ordered such that in each loop iteration, the new value of only depends on the previous values of . Hence, this excludes programs with “cyclic dependencies” of variables (e.g., where the new values of and both depend on the old values of both and ). While triangular loops are a very restricted subclass of general integer programs, integer programs often contain such loops. Hence, tools for termination analysis of such programs (e.g., [6, 7, 8, 9, 11, 12, 13]) could benefit from integrating our decision procedure and applying it whenever a sub-program is an affine triangular loop.
Note that triangularity and diagonalizability of matrices do not imply each other. As we consider loops with arbitrary dimension, this means that the class of loops considered in this paper is not covered by [4, 14]. Since we consider affine instead of linear loops, it is also orthogonal to .
To see the difference between our and previous results, note that a triangular matrix where are the distinct entries on the diagonal is diagonalizable iff
is the zero matrix.444The reason is that in this case, is the minimal polynomial of and diagonalizability is equivalent to the fact that the minimal polynomial is a product of distinct linear factors. Here,
is the identity matrix. So an easy example for a triangular loop where the update matrix is not diagonalizable is the following well-known program (see, e.g.,):
It terminates as eventually becomes negative and then decreases in each iteration. In matrix notation, the loop body is , i.e., the update matrix is triangular. Thus, this program is in our class of programs where we show that termination is decidable. However, the only entry on the diagonal of the update matrix is and is not the zero matrix. So (and in fact each where ) is not diagonalizable. Hence, extensions of this example to a dimension greater than where the loop is still triangular are not covered by any of the previous results.555For instance, consider while do
Our proof that termination is decidable for triangular loops proceeds in three steps. We first prove that termination of triangular loops is decidable iff termination of non-negative triangular loops (nnt-loops) is decidable, cf. Sect. 2. A loop is non-negative if the diagonal of does not contain negative entries. Second, we show how to compute closed forms for nnt-loops, i.e., vectors of expressions over the variables and such that for all , see Sect. 3. Here, triangularity of the matrix allows us to treat the variables step by step. So for any , we already know the closed forms for when computing the closed form for . The idea of computing closed forms for the repeated updates of loops was inspired by our previous work on inferring lower bounds on the runtime of integer programs . But in contrast to , here the computation of the closed form always succeeds due to the restricted shape of the programs. Finally, we explain how to decide termination of nnt-loops by reasoning about their closed forms in Sect. 4. While our technique does not yield witnesses for non-termination, we show that it yields witnesses for eventual non-termination, i.e., vectors such that witnesses non-termination for some . All missing proofs can be found in LABEL:app:proofs.
2 From Triangular to Non-Negative Triangular Loops
To transform triangular loops into nnt-loops, we define how to chain loops. Intuitively, chaining yields a new loop where a single iteration is equivalent to two iterations of the original loop. Then we show that chaining a triangular loop always yields an nnt-loop and that chaining is equivalent w.r.t. termination.
Definition 2 (Chaining).
Chaining the loop (1) yields:
Chaining Thm. 1.1 yields
which simplifies to the following nnt-loop:
Lemma 1 (Squares of Triangular Matrices).
For every triangular matrix , is a triangular matrix whose diagonal entries are non-negative.
lem:quadratic Let be a lower triangular matrix of dimension (the proof for upper triangular matrices is analogous). We have (where is the entry in ’s column).
If , then as, for each addend, either (if ) or (if ), which proves that is triangular.
If , then as for each addend, either (if ) or (if ) or (if ), which proves that all diagonal entries of are non-negative.
Theorem 2.2 (Reducing Termination to nnt-Loops).
Termination of triangular loops is decidable iff termination of nnt-loops is decidable.
Thus, from now on we restrict our attention to nnt-loops.
3 Computing Closed Forms
The next step towards our decidability proof is to show that is equivalent to a vector of poly-exponential expressions for each nnt-loop, i.e., the closed form of each nnt-loop can be represented by such expressions. Here, equivalence means that two expressions evaluate to the same result for all variable assignments.
Poly-exponential expressions are sums of arithmetic terms where it is always clear which addend determines the asymptotic growth of the whole expression when increasing a designated variable . This is crucial for our decidability proof in Sect. 4. Let (and , , etc. are defined analogously). Moreover, is again the set of all affine expressions over .
Definition 3 (Poly-Exponential Expressions).
Let be the set of all finite conjunctions over the literals where is a designated variable and . Moreover for each formula over , let
be the characteristic function of, i.e., if is valid and , otherwise. The set of all poly-exponential expressions over is
As ranges over , we use as syntactic sugar for . So an example for a poly-exponential expression is
Moreover, note that if contains a positive literal (i.e., a literal of the form “” for some number ), then is equivalent to either or .
The crux of the proof that poly-exponential expressions can represent closed forms is to show that certain sums over products of exponential and poly-exponential expressions can be represented by poly-exponential expressions, cf. Lemma 4. To construct these expressions, we use a variant of [2, Lemma 3.5]. As usual, is the set of all polynomials over with rational coefficients.
Lemma 3 (Expressing Polynomials by Differences ).
If and , then there is an such that for all .
lem:pol We use induction on the degree of . In the induction base, let , i.e., . If , then we fix and we get
If , then we fix and we get
For the induction step, let , i.e., . Let if and , otherwise. Moreover, let . If , then we have
which is a polynomial whose degree is at most . If , then we have
which is again a polynomial whose degree is at most . By the induction hypothesis, there exists some such that for all . Let . Then we get:
So Lemma 3 expresses a polynomial via the difference of another polynomial at the positions and , where the additional factor can be chosen freely. A detailed proof of Lemma 3 can be found in LABEL:lem:pol_proof. It is by induction on the degree of and its structure resembles the structure of the following algorithm to compute . Using the Binomial Theorem, one can verify that has a smaller degree than , which is crucial for the proof of Lemma 3 and termination of Procedure 1.
As an example, consider (i.e., ) and . Then we search for an such that , i.e., . According to Procedure 1, the solution is .
Lemma 4 (Closure of under Sums of Products and Exponentials).
If and , then one can compute a which is equivalent to .
Let . We have:
As is closed under addition, it suffices to show that we can compute an equivalent poly-exponential expression for any expression of the form
We first regard the case . Here, the expression (4) can be simplified to
Clearly, there is a such that is equivalent to . Moreover, where . Hence, due to the Binomial Theorem
which is a poly-exponential expression as .
From now on, let . If contains a positive literal , then we get
The step marked with holds as we have for all and the step marked with holds since implies . If does not contain a positive literal, then let be the maximal constant that occurs in or if is empty. We get:
Again, the step marked with holds since we have for all . The last step holds as implies . Similar to the case where contains a positive literal, we can compute a poly-exponential expression which is equivalent to the first addend. We have
which is a poly-exponential expression as . For the second addend, we have:
Lemma 3 ensures , i.e., we have for some and . Thus, which implies . It remains to show that the addend is equivalent to a poly-exponential expression. As , we have
The proof of Lemma 4 gives rise to a corresponding algorithm.
We compute an equivalent poly-exponential expression for
The next step is to rearrange the first sum as in (9). In our example, it directly simplifies to and hence we obtain
Finally, by applying the steps from (10) we get:
Recall that our goal is to compute closed forms for loops. As a first step, instead of the -fold update function of (1) where is the update of (1), we consider a recursive update function for a single variable :
Here, and . Using Lemma 4, it is easy to show that can be represented by a poly-exponential expression.
Lemma 5 (Closed Form for Single Variables).
If , , and , then one can compute a which satisfies
The restriction to triangular matrices now allows us to generalize Lemma 5 to vectors of variables. The reason is that due to triangularity, the update of each program variable only depends on the previous values of . So when regarding , we can assume that we already know the closed forms for . This allows us to find closed forms for one variable after the other by applying Lemma 5 repeatedly. In other words, it allows us to find a vector of poly-exponential expressions that satisfies
To prove this claim, we show the more general Lemma 6. For all , we define (and the notation for column vectors is defined analogously). Moreover, for a matrix , is ’s row and is the matrix with rows . So for , we have .
Lemma 6 (Closed Forms for Vectors of Variables).
If is a vector of at least pairwise different variables, is triangular with for all , and , then one can compute such that:
Assume that is lower triangular (the case that is upper triangular works analogously). We use induction on . For any we have: