Termination of Triangular Integer Loops is Decidable

05/21/2019 ∙ by Florian Frohn, et al. ∙ 0

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.



There are no comments yet.


page 1

page 2

page 3

page 4

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

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

while  do 

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  do 

While termination of affine loops is known to be decidable if the variables range over the real [15] or the rational numbers [5], 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 [15] uses Brouwer’s Fixed Point Theorem which is not applicable if the variables range over and [5] relies on the density of in . However, certain special cases have been solved: Braverman [5] 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. [4] 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. [14] proved decidability for the case and for the case that is diagonalizable.

Ben-Amram et al. [3] 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 [5].

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


while  do 

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 [10]. But in contrast to [10], 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  do  (2)
Example 2.1.

Chaining Thm. 1.1 yields

while  do

which simplifies to the following nnt-loop:

while  do 

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.


Immediate consequence of Def. 2 and Lemma 1. ∎

Lemma 2 (Equivalence of Chaining).

(1) terminates (2) terminates.


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

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.


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 [2]).

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.

Result: such that
if  then
      if  then return else return  
           if  then else   return
Algorithm 1 compute_
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 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.

Result: which is equivalent to
rearrange to as in (3) foreach  do
      if  then compute as in (5) and (6) else if  then compute as in (7) else
  • [label=]

  • split into two sums and as in (8)

  • compute from as in (9)

  • compute from as in (10) and (11) using Procedure 1

Algorithm 2 symbolic_sum
Example 3.2.

We compute an equivalent poly-exponential expression for


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 :

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


It suffices to find a that satisfies


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

and for , (13) implies

By Lemma 4, we can compute a such that


if , then and (14)
if , then . (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

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: