Termination of Triangular Integer Loops is Decidable

We consider the problem whether termination of affine integer loops is decidable. Since Tiwari conjectured decidability in 2004, only special cases have been solved. We complement this work by proving decidability for the case that the update matrix is triangular.


Introduction
We consider affine integer loops of the form Here, A ∈ Z d×d for some dimension d ≥ 1, x is a column vector of pairwise different variables x 1 , . . . , x d , a ∈ Z d , and ϕ is a conjunction of inequalities of the form α > 0 where α ∈ A [x] is an affine expression with rational coefficients 3 over x (i.e., A [x] = {c T x + c | c ∈ Q d , c ∈ Q}). So ϕ has the form B x + b > 0 where 0 is the vector containing k zeros, B ∈ Q k×d , and b ∈ Q k for some k ∈ N.
Def. 1 formalizes the intuitive notion of termination for such loops.
Definition 1 (Termination). Let f : Z d → Z d with f (x) = A x + a. If ∃c ∈ Z d . ∀n ∈ N. ϕ[x/f n (c)], then (1) is non-terminating and c is a witness for non-termination. Otherwise, (1) terminates.
Here, f n denotes the n-fold application of f , i.e., we have f 0 (c) = c and f n+1 (c) = f (f n (c)). We call f the update of (1). Moreover, for any entity s ⋆ funded by DFG grant 389792660 as part of TRR 248 and by DFG grant GI 274/6 3 Note 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.

Example 2. 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 y + z > 0 do   While termination of affine loops is known to be decidable if the variables range over the real [14] or the rational numbers [4], the integer case is a wellknown open problem [2,3,4,13,14]. 4 However, certain special cases have been solved: Braverman [4] showed that termination of linear loops is decidable (i.e., loops of the form (1) where a is 0 and ϕ is of the form B x > 0). Bozga et al. [3] showed decidability for the case that the update matrix A in (1) has the finite monoid property, i.e., if there is an n > 0 such that A n is diagonalizable and all eigenvalues of A n are in {0, 1}. Ouaknine et al. [13] proved decidability for the case d ≤ 4 and for the case that A is diagonalizable.
Ben-Amram et al. [2] showed undecidability of termination for certain extensions of affine integer loops, e.g., for loops where the body is of the form if x > 0 then x ← A x else x ← A ′ x where A, A ′ ∈ Z d×d and x ∈ x.
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 A is a triangular matrix (i.e., all entries of A 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 A can be transformed into a triangular matrix by reordering A, x, and a accordingly. 5 So essentially, triangularity means that the program variables x 1 , . . . , x d can be ordered such that in each loop iteration, the new value of x i only depends on the previous values of x 1 , . . . , x i−1 , x i . Hence, this excludes programs with "cyclic dependencies" of variables (e.g., where the new values of x and y both depend on the old values of both x and y). 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., [5,6,7,8,10,11,12]) 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 [3,13]. Since we consider affine instead of linear loops, it is also orthogonal to [4].
To see the difference between our and previous results, note that a triangular matrix A where c 1 , . . . , c k are the distinct entries on the diagonal is diagonalizable iff (A − c 1 I) . . . (A − c k I) is the zero matrix. 6 Here, I 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., [2]): while x > 0 do x ← x + y; y ← y − 1 It terminates as y eventually becomes negative and then x decreases in each iteration. In matrix notation, the loop body is x y ← 1 1 0 1 x y + 0 −1 , 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 A is c = 1 and A − c I = 0 1 0 0 is not the zero matrix. So A (and in fact each A n where n ∈ N) is not diagonalizable. Hence, extensions of this example to a dimension greater than 4 where the loop is still triangular are not covered by any of the previous results. 7 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 A does not contain negative entries. Second, we show how to compute closed forms for nnt-loops, i.e., vectors q of d expressions over the variables x and n such that q[n/c] = f c (x) for all c ≥ 0, see Sect. 3. Here, triangularity of the matrix A allows us to treat the variables step by step. So for any 1 ≤ i ≤ d, we already know the closed forms for x 1 , . . . , x i−1 when computing the closed form for x i . 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 [9]. But in contrast to [9], 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 nontermination, i.e., vectors c such that f n (c) witnesses non-termination for some n ∈ N. All missing proofs can be found in Appendix C.

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. 6 The reason is that in this case, (x − c1) . . . (x − c k ) is the minimal polynomial of A and diagonalizability is equivalent to the fact that the minimal polynomial is a product of distinct linear factors. 7 For instance, consider while x > 0 do x ← x + y + z1 + z2 + z3; y ← y − 1 Definition 3 (Chaining). Chaining the loop (1) yields: which simplifies to the following nnt-loop: The following lemma is needed to prove that (2) is an nnt-loop if (1) is triangular (see Appendix C.1 for the straightforward proof of Lemma 5).

Lemma 5 (Squares of Triangular Matrices). For every triangular matrix
A, A 2 is a triangular matrix whose diagonal entries are non-negative.
i.e., iff (2) does not terminate as f 2 (x) = A 2 x + A a + a is the update of (2).
Theorem 8 (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.

Computing Closed Forms
The next step towards our decidability proof is to show that f n (x) 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 n. This is crucial for our decidability proof in Sect. 4. Let N ≥1 = {b ∈ N | b ≥ 1} (and Q >0 , N >1 , etc. are defined analogously). Moreover, A [x] is again the set of all affine expressions over x.
Definition 9 (Poly-Exponential Expressions). Let C be the set of all finite conjunctions over the literals n = c, n = c where n is a designated variable and c ∈ N. Moreover for each formula ψ over n, let ψ be the characteristic function of ψ, i.e., ψ (c) = 1 if ψ[n/c] is valid and ψ (c) = 0, otherwise. The set of all poly-exponential expressions over x is As n ranges over N, we use n > c as syntactic sugar for c i=0 n = i . So an example for a poly-exponential expression is n > 2 · (2 · x + 3 · y − 1) · n 3 · 3 n + n = 2 · (x − y). Moreover, note that if ψ contains a positive literal (i.e., a literal of the form "n = c" for some number c ∈ N), then ψ is equivalent to either 0 or n = c .
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 12. To construct these expressions, we use a variant of [1, Lemma 3.5]. As usual, Q[x] is the set of all polynomials over x with rational coefficients.
Lemma 10 (Expressing Polynomials by Differences [1]). If q ∈ Q[n] and c ∈ Q, then there is an r ∈ Q[n] such that q = r − c · r[n/n − 1] for all n ∈ N.
So Lemma 10 expresses a polynomial q via the difference of another polynomial r at the positions n and n − 1, where the additional factor c can be chosen freely. A detailed proof of Lemma 10 can be found in Appendix C.2. It is by induction on the degree of q and its structure resembles the structure of the following algorithm to compute r. Using the Binomial Theorem, one can verify that q − s + c · s[n/n − 1] has a smaller degree than q, which is crucial for the proof of Lemma 10 and termination of Alg. 1.
Proof. Let p = ℓ j=1 ψ j · α j · n aj · b n j . We have: 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 m = 0. Here, the expression (4) can be simplified to From now on, let m ≥ 1. If ψ contains a positive literal n = c, then we get The step marked with ( †) holds as we have n > i − 1 = 1 for all i ∈ {1, . . . , n} and the step marked with ( † †) holds since i = c + 1 implies ψ (i − 1) = 0. If ψ does not contain a positive literal, then let c be the maximal constant that occurs in ψ or −1 if ψ is empty. We get: Again, the step marked with ( †) holds since we have n > i − 1 = 1 for all i ∈ {1, . . . , n}. The last step holds as i ≥ c + 2 implies ψ (i − 1) = 1. 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 1 . For the second addend, we have: . It remains to show that the addend n > c + 1 · α b · r · b n is equivalent to a poly-exponential expression.
The proof of Lemma 12 gives rise to a corresponding algorithm.
if m = 0 then compute q j as in (5) and (6) else if p j = . . . ∧ n = c ∧ . . . · . . . then compute q j as in (7) else • split p j into two sums p j,1 and p j,2 as in (8) • compute q j,1 from p j,1 as in (9) • compute q j,2 from p j,2 as in (10) and (11) using Alg. 1 • q j ← q j,1 + q j,2 return ℓ j=1 q j Example 13. We compute an equivalent poly-exponential expression for where w is a variable. (It will later on be needed to compute a closed form for Ex. 4, see Ex. 18.) According to Alg. 2 and (3), we get . We search for q 1 , q 2 , q 3 ∈ PE[w] that are equivalent to p 1 , p 2 , p 3 , i.e., q 1 + q 2 + q 3 is equivalent to (12). We only show how to compute q 2 . See Appendix A for the computation of q 1 = n = 0 · 1 2 ·w ·4 n and q 3 = 2 3 − 2 3 ·4 n . Analogously to (8), we get: The next step is to rearrange the first sum as in (9). In our example, it directly simplifies to 0 and hence we obtain Finally, by applying the steps from (10) we get: The step marked with ( †) holds by Lemma 10 with q = 1 and c = 4. Thus, we have r = − 1 3 , cf. Ex. 11. Recall that our goal is to compute closed forms for loops. As a first step, instead of the n-fold update function h(n, x) = f n (x) of (1) where f is the update of (1), we consider a recursive update function for a single variable x ∈ x: g(0, x) = x and g(n, x) = m · g(n − 1, x) + p[n/n − 1] for all n > 0 Here, m ∈ N and p ∈ PE[x]. Using Lemma 12, it is easy to show that g can be represented by a poly-exponential expression.
To see why (13) is sufficient, note that (13) implies So both addends are equivalent to poly-exponential expressions.
Example 15. We show how to compute the closed forms for the variables w and x from Ex. 4. We first consider the assignment w ← 2, i.e., we want to compute a q w ∈ PE[w, x, y, z] with q w [n/0] = w and q w = (m w · q w + p w ) [n/n − 1] for n > 0, where m w = 0 and p w = 2. According to (13) and (14), The restriction to triangular matrices now allows us to generalize Lemma 14 to vectors of variables. The reason is that due to triangularity, the update of each program variable x i only depends on the previous values of x 1 , . . . , x i . So when regarding x i , we can assume that we already know the closed forms for x 1 , . . . , x i−1 . This allows us to find closed forms for one variable after the other by applying Lemma 14 repeatedly. In other words, it allows us to find a vector q of poly-exponential expressions that satisfies q [n/0] = x and q = A q[n/n − 1] + a for all n > 0.
The last step holds as A is lower triangular. By Lemma 14, we can compute a q 1 ∈ PE[x] that satisfies for all n > 0. As A j;1 · q 1 + p j ∈ PE[x] for each 2 ≤ j ≤ d, the claim follows from the induction hypothesis.
Together, Lemmas 14 and 16 and their proofs give rise to the following algorithm to compute a solution for (16) and (17). It computes a closed form q 1 for x 1 as in the proof of Lemma 14, constructs the argument p for the recursive call based on A, q 1 , and the current value of p as in the proof of Lemma 16, and then determines the closed form for x 2,...,d recursively.
So the closed form of Ex. 4 for the values of the variables after n iterations is:

Deciding Non-Termination of nnt-Loops
Our proof uses the notion of eventual non-termination [4,13]. Here, the idea is to disregard the condition of the loop during a finite prefix of the program run.

Definition 19 (Eventual Non-Termination). A vector c ∈ Z d witnesses eventual non-termination
If there is such a witness, then (1) is eventually non-terminating.
Clearly, (1) is non-terminating iff (1) is eventually non-terminating [13] (see Appendix B for a proof). Now Thm. 17 gives rise to an alternative characterization of eventual non-termination in terms of the closed form q instead of f n (c).
Corollary 20 (Expressing Non-Termination with PE). If q is the closed form of (1), then c ∈ Z d witnesses eventual non-termination iff Proof. Immediate, as q is equivalent to f n (x). See Appendix C.3 for details.
So to prove that termination of nnt-loops is decidable, we will use Cor. 20 to show that the existence of a witness for eventual non-termination is decidable. To do so, we first eliminate the factors ψ from the closed form q. Assume that q has at least one factor ψ where ψ is non-empty (otherwise, all factors ψ are equivalent to 1) and let c be the maximal constant that occurs in such a factor. Then all addends ψ · α · n a · b n where ψ contains a positive literal become 0 and all other addends become α · n a · b n if n > c. Thus, as we can assume n 0 > c in (18) without loss of generality, all factors ψ can be eliminated when checking eventual non-termination.
Corollary 21 (Removing ψ from PEs). Let q be the closed form of an nntloop (1). Let q norm result from q by removing all addends ψ · α · n a · b n where ψ contains a positive literal and by replacing all addends ψ · α · n a · b n where ψ does not contain a positive literal by α · n a · b n . Then c ∈ Z d is a witness for eventual non-termination iff Proof. See above for the proof idea and Appendix C.4 for a detailed proof.
By removing the factors ψ from the closed form q of an nnt-loop, we obtain normalized poly-exponential expressions.

Recall that the loop condition ϕ is a conjunction of inequalities of the form
and we need to decide if there is an instantiation of these inequalities that is valid "for large enough n". To do so, we order the coefficients α j of the addends α j · n aj · b n j of normalized poly-exponential expressions according to the addend's asymptotic growth when increasing n. Lemma 24 shows that α 2 · n a2 · b n 2 grows faster than α 1 · n a1 · b n 1 iff b 2 > b 1 or both b 2 = b 1 and a 2 > a 1 .
Here, > lex is the lexicographic order, i.e., Proof. By considering the cases b 2 > b 1 and b 2 = b 1 separately, the claim can easily be deduced from the definition of O. See Appendix C.5 for details.
Definition 25 (Ordering Coefficients). Marked coefficients are of the form , where α j = 0 for all 1 ≤ j ≤ ℓ. The marked coefficients of p are Example 26. In Ex. 23 we saw that the loop from Ex. 2 is non-terminating iff there are w, x, y, z ∈ Z, n 0 ∈ N such that p ϕ 1 > 0 ∧ p ϕ 2 > 0 for all n > n 0 . We get: Now it is easy to see that the asymptotic growth of a normalized polyexponential expression is solely determined by its ≻-maximal addend.
Proof. Clear, as c · n a · b n is the asymptotically dominating addend of p. See Appendix C.6 for a detailed proof.
Note that Cor. 27 would be incorrect for the case c = 0 if we replaced . Building upon Cor. 27, we now show that, for large n, the sign of a normalized poly-exponential expression is solely determined by its ≻-maximal coefficient. Here, we define Lemma 28 (Sign of NPEs). Let p ∈ NPE. Then lim n →∞ p ∈ Q iff p ∈ Q and otherwise, lim n →∞ p ∈ {∞, −∞}. Moreover, we have sign (lim n →∞ p) = sign(unmark(max ≻ (coeffs(p)))).
Proof. If p / ∈ Q, then the limit of each addend of p is in {−∞, ∞} by definition of NPE. As the asymptotically dominating addend determines lim n →∞ p and unmark(max ≻ (coeffs(p))) determines the sign of the asymptotically dominating addend, the claim follows. See Appendix C.7 for a detailed proof.
Lemma 29 shows the connection between the limit of a normalized poly-exponential expression p and the question whether p is positive for large enough n. The latter corresponds to the existence of a witness for eventual non-termination by Cor. 21 as ϕ[x/q norm ] is a conjunction of inequalities p > 0 where p ∈ NPE[x].
Proof. By case analysis over lim n →∞ p. See Appendix C.8 for details. Now we show that Cor. 21 allows us to decide eventual non-termination by examining the coefficients of normalized poly-exponential expressions. As these coefficients are in A [x], the required reasoning is decidable.
Proof. For any p i with 1 ≤ i ≤ k and any c ∈ Z d , we have p i [x/c] ∈ NPE. Hence: Hence by the considerations above, (20) is valid iff is valid. By multiplying each (in-)equality in (22) with the least common multiple of all denominators, one obtains a first order formula over the theory of linear integer arithmetic. It is well known that validity of such formulas is decidable.
Note that (22) is valid iff k i=1 max coeff pos(p i ) is satisfiable. So to implement our decision procedure, one can use integer programming or SMT solvers to check satisfiability of k i=1 max coeff pos(p i ). Lemma 30 allows us to prove our main theorem.
Theorem 31. Termination of triangular loops is decidable.
Proof. By Thm. 8, termination of triangular loops is decidable iff termination of nnt-loops is decidable. For an nnt-loop (1) we obtain a q norm ∈ NPE[x] d (see Thm. 17 and Cor. 21) such that (1) is non-terminating iff where ϕ is a conjunction of inequalities of the form α > 0, α ∈ A [x]. Hence, . Thus, by Lemma 30, validity of (20) is decidable.
The following algorithm summarizes our decision procedure.
Ex. 33 shows that our technique does not yield witnesses for non-termination, but it only proves the existence of a witness for eventual non-termination. While such a witness can be transformed into a witness for non-termination by applying the loop several times, it is unclear how often the loop needs to be applied.
Example 33. Consider the following non-terminating loop: The closed form of x is q = n = 0 · x + n = 0 · (x + y + n − 1). Replacing x with q norm in x > 0 yields x + y + n − 1 > 0. The maximal marked coefficient of However, the final formula constructed by Alg. 4 precisely describes all witnesses for eventual non-termination (see Appendix C.9 for the proof).
Lemma 34 (Witnessing Eventual Non-Termination). Let (1) be a triangular loop, let q norm be the normalized closed form of (2), and let Then c ∈ Z d witnesses eventual non-termination of (1) iff [x/c] is a model for k i=1 max coeff pos(p i ).

Conclusion
We presented a decision procedure for termination of affine integer loops with triangular update matrices. In this way, we contribute to the ongoing challenge of proving the 15 years old conjecture by Tiwari [14] that termination of affine integer loops is decidable. After linear loops [4], loops with at most 4 variables [13], and loops with diagonalizable update matrices [3,13], triangular loops are the fourth important special case where decidability could be proven.
The key idea of our decision procedure is to compute closed forms for the values of the program variables after a symbolic number of iterations n. While these closed forms are rather complex, it turns out that reasoning about firstorder formulas over the theory of linear integer arithmetic suffices to analyze their behavior for large n. This allows us to reduce (non-)termination of triangular loops to integer programming. In future work, we plan to investigate generalizations of our approach to other classes of integer loops.

C.1 Proof of Lemma 5
Proof. Let A be a lower triangular matrix of dimension d (the proof for upper triangular matrices is analogous). We have A 2 i;j = d k=1 A i;k · A k;j (where A i;j is the i th entry in A's j th column).
If i < j, then d k=1 A i;k · A k;j = 0 as, for each addend, either A i;k = 0 (if i < k) or A k;j = 0 (if k ≤ i < j), which proves that A 2 is triangular.
If i = j, then d k=1 A i;k · A k;i ≥ 0 as for each addend, either , which proves that all diagonal entries of A 2 are non-negative.

C.2 Proof of Lemma 10
Proof. We use induction on the degree d of q. In the induction base, let d = 0, i.e., q = c 0 ∈ Q. If c = 1, then we fix r = c 0 · n and we get If c = 1, then we fix r = c0 1−c and we get For the induction step, let d > 0, i.e., q = which is a polynomial whose degree is at most d − 1. If c = 1, then we have which is again a polynomial whose degree is at most d − 1. By the induction hypothesis, there exists some r ′ ∈ Q[n] such that t = r ′ − c · r ′ [n/n − 1] for all n ∈ N. Let r = s + r ′ . Then we get: Proof. We have: c witnesses eventual non-termination (as q is the closed form of (1) and thus, q = f n (x), cf. Thm. 17)

⇐⇒ (18)
C.4 Proof of Cor. 21 Proof. By Cor. 20, c is a witness for eventual non-termination iff Let c be the maximal constant that occurs in a sub-expression ψ in q. Then

C.6 Proof of Cor. 27
Proof. If p = 0, then c = 0 by Def. 25 and hence O(p) = O(c · n a · b n ) = O(0). Otherwise, p has the form c · n a · b n + ℓ j=1 c j · n aj · b n i for c = 0 and ℓ ≥ 0. We have c (bj ,aj ) j ∈ coeffs(p) and hence (b, a) > lex (b j , a j ) for all 1 ≤ j ≤ ℓ. Thus, Lemma 24 implies O(n aj · b n j ) O(n a · b n ) and hence we get O(p) = O c · n a · b n + ℓ j=1 c j · n aj · b n j = O(n a · b n ) = O(c · n a · b n ).
Let p = ℓ j=1 c j · n aj · b n j with ℓ ≥ 1 and c j = 0 for all 1 ≤ j ≤ ℓ, since p = 0. We use induction on ℓ.

C.9 Proof of Lemma 34
Proof. We have: