 # 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.

## Authors

##### This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

## 1 Introduction

We consider affine integer loops of the form

 while φ do ¯¯¯x←A¯¯¯x+¯¯¯a. (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).

Let with . If

 ∃¯¯c∈Zd. ∀n∈N. φ[¯¯¯x/fn(¯¯c)],

then (1) is non-terminating and is a witness for non-termination. Otherwise, (1) terminates.

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 .

###### Example 1.1.

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 of

are 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:

 while φ∧φ[¯¯¯x/A¯¯¯x+¯¯¯a] do  ¯¯¯x←A2¯¯¯x+A¯¯¯a+¯¯¯a (2)
###### Example 2.1.

Chaining Thm. 1.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 LABEL:lem:quadratic_proof for the straightforward proof of Lemma 1).

###### 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.

###### Corollary 1 (Chaining Loops).

If (1) is triangular, then (2) is an nnt-loop.

###### Proof.

Immediate consequence of Def. 2 and Lemma 1. ∎

###### Lemma 2 (Equivalence of Chaining).

(1) terminates (2) terminates.

###### Proof.

By Def. 1, (1) does not terminate iff

 ∃¯¯c∈Zd. ∀n∈N. φ[¯¯¯x/fn(¯¯c)]⟺∃¯¯c∈Zd. ∀n∈N. φ[¯¯¯x/f2⋅n(¯¯c)]∧φ[¯¯¯x/f2⋅n+1(¯¯c)]⟺∃¯¯c∈Zd. ∀n∈N. φ[¯¯¯x/f2⋅n(¯¯c)]∧φ[¯¯¯x/Af2⋅n(¯¯c)+¯¯¯a](by Def.\ of f),

i.e., iff (2) does not terminate as is the update of (2). ∎

###### Theorem 2.2 (Reducing Termination to nnt-Loops).

Termination of triangular loops is decidable iff termination of nnt-loops is decidable.

###### Proof.

Immediate consequence of Cor. 1 and Lemma 2. ∎

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

 r−c⋅r[n/n−1]=r−r[n/n−1]=c0⋅n−c0⋅(n−1)=c0=q.

If , then we fix and we get

 r−c⋅r[n/n−1]=c01−c−c⋅c01−c=c01−c−c⋅c01−c=c0⋅(1−c)1−c=c0=q.

For the induction step, let , i.e., . Let if and , otherwise. Moreover, let . If , then we have

 t=q−cd⋅nd+1d+1+cd⋅(n−1)d+1d+1=q−cd⋅nd+1−cd⋅(n−1)d+1d+1=q−cd⋅nd+1−cd⋅∑d+1k=0(d+1k)⋅nd+1−k⋅(−1)kd+1=q−−cd⋅∑d+1k=1(d+1k)⋅nd+1−k⋅(−1)kd+1=q+cd⋅∑d+1k=1(d+1k)⋅nd+1−k⋅(−1)kd+1=q+cd⋅(∑d+1k=2(d+1k)⋅nd+1−k⋅(−1)k)−cd⋅(d+1)⋅ndd+1=q+cd⋅(∑d+1k=2(d+1k)⋅nd+1−k⋅(−1)k)d+1−cd⋅nd=∑di=0ci⋅ni+cd⋅(∑d+1k=2(d+1)k⋅nd+1−k⋅(−1)k)d+1−cd⋅nd=∑d−1i=0ci⋅ni+cd⋅(∑d+1k=2(d+1k)⋅nd+1−k⋅(−1)k)d+1

which is a polynomial whose degree is at most . If , then we have

 t=q−cd⋅nd1−c+c⋅cd⋅(n−1)d1−c=q−cd⋅nd1−c+c⋅cd⋅(n−1)d1−c=q−cd⋅nd−c⋅cd⋅(n−1)d1−c
 =q−cd⋅nd−c⋅cd⋅∑dk=0(dk)⋅nd−k⋅(−1)k1−c=q−cd⋅nd−c⋅cd⋅nd−c⋅cd⋅∑dk=1(dk)⋅nd−k⋅(−1)k1−c=q−(1−c)⋅cd⋅nd−c⋅cd⋅∑dk=1(dk)⋅nd−k⋅(−1)k1−c=q−cd⋅nd+c⋅cd⋅∑dk=1(dk)⋅nd−k⋅(−1)k1−c=∑di=0ci⋅ni−cd⋅nd+c⋅cd⋅∑dk=1(dk)⋅nd−k⋅(−1)k1−c=∑d−1i=0ci⋅ni+c⋅cd⋅∑dk=1(dk)⋅nd−k⋅(−1)k1−c

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:

 r−c⋅r[n/n−1]=s+r′−c⋅s[n/n−1]−c⋅r′[n/n−1](by % definition of r)=s+t−c⋅s[n/n−1](as t=r′−c⋅r′[n/n−1])=t−t+q(by definition of t)=q

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.

###### Example 3.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 PE under Sums of Products and Exponentials).

If and , then one can compute a which is equivalent to .

###### Proof.

Let . We have:

 (3)

As is closed under addition, it suffices to show that we can compute an equivalent poly-exponential expression for any expression of the form

 (4)

We first regard the case . Here, the expression (4) can be simplified to

 (5)

Clearly, there is a such that is equivalent to . Moreover, where . Hence, due to the Binomial Theorem

 (6)

which is a poly-exponential expression as .

From now on, let . If contains a positive literal , then we get

 (7)

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:

 (8)

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

 (9)

which is a poly-exponential expression as . For the second addend, we have:

 (10)

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

 (11)

The proof of Lemma 4 gives rise to a corresponding algorithm.

###### Example 3.2.

We compute an equivalent poly-exponential expression for

 (12)

where is a variable. (It will later on be needed to compute a closed form for Thm. 2.1, see Thm. 3.5.) According to Procedure 2 and (3), we get

with , , and . We search for that are equivalent to , i.e., is equivalent to (12). We only show how to compute . See Appendix 0.A for the computation of and . Analogously to (8), we get:

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:

The step marked with holds by Lemma 3 with and . Thus, we have , cf. Thm. 3.1.

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 :

 g(0,¯¯¯x)=xandg(n,¯¯¯x)=m⋅g(n−1,¯¯¯x)+p[n/n−1]for all n>0

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

 q[n/0]=xandq=(m⋅q+p)[n/n−1]% for all n>0.
###### Proof.

It suffices to find a that satisfies

 q=mn⋅x+∑ni=1mn−i⋅p[n/i−1]. (13)

To see why (13) is sufficient, note that (13) implies

 q[n/0]=m0⋅x+∑0i=1m0−i⋅p[n/i−1]=x

and for , (13) implies

 q=mn⋅x+∑ni=1mn−i⋅p[n/i−1]=mn⋅x+(∑n−1i=1mn−i⋅p[n/i−1])+p[n/n−1]=m⋅(mn−1⋅x+∑n−1i=1mn−i−1⋅p[n/i−1])+p[n/n−1]=m⋅q[n/n−1]+p[n/n−1]=(m⋅q+p)[n/n−1].

By Lemma 4, we can compute a such that

 mn⋅x+∑ni=1mn−i⋅p[n/i−1]=mn⋅x+q′.

Moreover,

 if m=0, then and (14) if m>0, then mn⋅x∈PE[¯¯¯x]. (15)

So both addends are equivalent to poly-exponential expressions. ∎

###### Example 3.3.

We show how to compute the closed forms for the variables and from Thm. 2.1. We first consider the assignment , i.e., we want to compute a with and for , where and . According to (13) and (14), is

For the assignment , we search for a such that and for , where and . By (13), is

 mnx⋅x+∑ni=1mn−ix⋅px[n/i−1]=1n⋅x+∑ni=11n−i⋅2=x+2⋅n.

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

 ¯¯¯q[n/0]=¯¯¯xand¯¯¯q=A¯¯¯q[n/n−1]+¯¯¯afor all n>0.

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:

 ¯¯¯q[n/0] =¯¯¯x1,…,dand (16) ¯¯¯q =(A¯¯¯q+¯¯¯p)[n/n−1]for all n>0 (17)
###### Proof.

Assume that is lower triangular (the case that is upper triangular works analogously). We use induction on . For any we have:

 ¯¯¯q=(A¯¯¯q+¯¯¯p)[n/n−1]⟺¯¯¯qj=(Aj⋅¯¯¯q+¯¯¯pj)[n/n−1]for all 1≤j≤d⟺¯¯¯qj=(Aj;2,…,d⋅¯¯¯q2,…,d+Aj;1⋅¯¯¯q1