1 Introduction

Termination is one of the most important properties of a program. In this work, we study complete approaches for analyzing termination of certain classes of polynomial loops. Compared to incomplete techniques, such approaches have the advantage that they always yield a definite result. In particular, we investigate decidability of termination for our classes of loops.

In the following, we give a short overview on the contributions of our article and highlight how it extends our earlier conference paper [20]. There are already several decidability results for termination of linear loops [8, 10, 19, 31, 42, 45, 49, 62, 66], but only few results on the decidability of termination for certain forms of non-linear loops [43, 44, 47, 64, 65]. Moreover, these previous works only deal with loops whose guards only contain conjunctions, besides [47] which is restricted to guards defining compact sets. In this work, we regard possibly non-linear loops with arbitrary guards, i.e., they may also contain disjunctions and define non-compact sets. More precisely, we consider so-called twn-loops, where the update is mildly restricted to be “triangular” and “weakly non-linear” (see Sect. 2 for a formal definition). We study such loops over \(\mathbb {Z}\), \(\mathbb {Q}\), \(\mathbb {R}_{\mathbb {A}}\) (the real algebraic numbers), and \(\mathbb {R}\), whereas existing decidability results for non-linear loops are restricted to loops over the reals.

Most techniques for proving termination of loops rely on polynomial ranking functions, see, e.g., [1, 5,6,7, 9, 51]. However, such ranking functions are only sound for proving termination, i.e., in general, they cannot refute termination. In contrast to ranking functions, we use the computability of closed forms for the iterated update of the loop (Sect. 3). In this way, we can reduce termination of a loop to (in)validity of a certain formula. This reduction, which is a generalization of our earlier results for linear loops over \(\mathbb {Z}\) with conjunctive guards [19], is sound and complete, i.e., validity of the resulting formula proves non-termination, whereas invalidity implies termination. Moreover, our reduction is computable. Analogously to our earlier work for linear loops [19], our decidability results on termination then follow from existing results on the decidability of certain theories. In this way, we show that termination of twn-loops is decidable over \(\mathbb {R}_{\mathbb {A}}\) and \(\mathbb {R}\), and non-termination is semi-decidable over \(\mathbb {Z}\) and \(\mathbb {Q}\) (Sect. 4).

In Sect. 5 we use concepts from algebra to enlarge the classes of loops to which our reduction is applicable. This is done by transforming (certain) non-twn loops into twn-form without affecting their termination behavior (Sect. 5.1). In Sect. 5.2, we discuss for which loops our transformation is applicable. In this way, we generalize our results to a broader class of polynomial loops (Sect. 5.3).

In contrast to our earlier conference paper [20], in Sect. 6 we formalize the technique of [48] to linearize (the updates of) twn-loops in our setting. Using this formalization, we develop novel results on the complexity of linearization.

Afterwards, based on our decision procedure for termination in Sect. 4, on the transformation of Sect. 5, and on our complexity results for linearization from Sect. 6, we study the complexity of deciding termination in Sect. 7.

In Sect. 7.1 we show that deciding termination of linear loops with rational spectrum over \(\mathbb {Z}\), \(\mathbb {Q}\), \(\mathbb {R}_{\mathbb {A}}\), and \(\mathbb {R}\) is Co-NP-complete. Moreover, we show that deciding termination of linear-update loops (where the update is linear but the guard may be non-linear) with real spectrum over \(\mathbb {R}_{\mathbb {A}}\) and \(\mathbb {R}\) is \(\forall {\mathbb {R}}\)-complete. Here, a loop has rational or real spectrum, respectively, if its update matrix has rational or real eigenvalues only, and \(\forall \mathbb {R}\) is the complexity class of problems which can be reduced to validity of a universally quantified formula of polynomial inequations over the reals. We also analyze the complexity of deciding termination for arbitrary twn-loops (with possibly non-linear updates) in Sect. 7.2. In our conference paper [20, Thm. 45], we had only analyzed this case for a bounded number of variables. In contrast, we now extend our analysis to the general case where the number of variables is not restricted (Theorem 7.6). To this end, we need our new results from Sect. 6 on the complexity of linearizing twn-loops.

Finally, in contrast to [20], we identify a class of linear loops (uniform loops) where termination can be interpreted as a parameterized problem which is decidable in polynomial time when fixing such a parameter (Sect. 7.3). Based on the transformation of Sect. 5, we show that the closed forms arising from uniform loops have a special structure. Therefore, here (in)validity of the formula from Sect. 4 which encodes termination can be checked in polynomial time.

Related work is discussed in Sect. 8 and all missing proofs can be found in App. A. So the current paper extends [20] by the following new material:

  • Section 6 on the linearization of twn-loops and its complexity.

  • Theorem 7.6 on the complexity of deciding termination for arbitrary twn-loops where the number of variables is not restricted.

  • Section 7.3 on uniform loops where the parameterized complexity of deciding termination is polynomial.

  • Several additional explanations and remarks.

2 Preliminaries

A (polynomial) loop over a ring \(\mathcal {S}\) has the form in Fig. 1, where \(\mathbb {Z}\le \mathcal {S}\le \mathbb {R}_{\mathbb {A}}\) and \(\le\) denotes the subring relation. Here, \(\vec {x}\) is a vector of \(d \ge 1\) pairwise different variables that range over \(\mathcal {S}\) and \(\vec {u}\in \left( \mathcal {S}[\vec {x}]\right) ^{d}\) where \(\mathcal {S}[\vec {x}]\) is the set of polynomials over \(\vec {x}\) with coefficients in \(\mathcal {S}\). To improve readability, we use row- and column-vectors interchangeably. The guard \(\varphi\) is an arbitrary propositional (i.e., quantifier-free) formula over the atoms \(\{p \triangleright 0 \mid p \in \mathcal {S}[\vec {x}], {\triangleright }\in {\{\ge ,} {>\}\}}\). We denote the set of all such formulas by \({{\,\mathrm{Th_{{qf}}}\,}}(\mathcal {S})\). In our setting, negation is syntactic sugar as, e.g., \(\lnot (p > 0)\) is equivalent to \(-p \ge 0\). So w.l.o.g. the guard (or condition) \(\varphi\) of a loop is built from atoms, \(\wedge\), and \(\vee\).

Fig. 1
figure 1

Polynomial loop

Fig. 2
figure 2

Loop \(\textbf{L}_{ ex }\) over \(\mathbb {Z}\)

We require \(\mathcal {S}\le \mathbb {R}_{\mathbb {A}}\) instead of \(\mathcal {S}\le \mathbb {R}\), as it is unclear how to represent transcendental numbers on computers. However, in Sect. 4 we will see that the loops considered in this work terminate over \(\mathbb {R}\) iff they terminate over \(\mathbb {R}_{\mathbb {A}}\). Thus, our results immediately carry over to loops where the variables range over \(\mathbb {R}\). Hence, we sometimes also consider loops over \(\mathcal {S}= \mathbb {R}\). However, even then we restrict ourselves to loops where all constants in \(\varphi\) and \(\vec {u}\) are algebraic.

We often represent a loop as in Fig. 1 by the tuple \((\varphi , \vec {u})\) of the condition \(\varphi\) and the update \(\vec {u}= (u_1,\ldots ,u_d)\). Unless stated otherwise, \((\varphi , \vec {u})\) is always a loop over \(\mathcal {S}\) using the variables \(\vec {x} = (x_1,\ldots ,x_d)\) where \(\mathbb {Z}\le \mathcal {S}\le \mathbb {R}_{\mathbb {A}}\). We use the following notions for certain classes of loops: A linear-update loop has the form \((\varphi , A \cdot \vec {x} + \vec {b})\), and it has rational or real spectrum, respectively, if A has rational or real eigenvalues only. Then all eigenvalues of A are real algebraic numbers, since \(\vec {u}\in \left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right) ^{d}\) implies that all constants in \(\vec {u}\) (and thus in A) are algebraic. A linear loop is a linear-update loop where \(\varphi\) is linear (i.e., its atoms are only linear inequations). Here, “linear” refers to “linear polynomials”, i.e., of degree at most 1, so it includes affine functions. A conjunctive loop is a loop \((\varphi ,\vec {u})\) where \(\varphi\) does not contain disjunctions.

For any entity s, s[x/t] results from s by replacing all free occurrences of x by t. Similarly, if \(\vec {x} = (x_1,\ldots ,x_d)\) and \(\vec {t} = (t_1,\ldots , t_d)\), then \(s[\vec {x} / \vec {t}]\) results from s by replacing all free occurrences of \(x_i\) by \(t_i\), for each \(1 \le i \le d\).

Any vector of polynomials \(\vec {u}\in \left( \mathcal {S}[\vec {x}]\right) ^d\) can also be regarded as a function \(\vec {u}:\left( \mathcal {S}[\vec {x}]\right) ^d \rightarrow \left( \mathcal {S}[\vec {x}]\right) ^d\) where for any \(\vec {p} \in \left( \mathcal {S}[\vec {x}]\right) ^d\), \(\vec {u}(\vec {p}) = \vec {u}[\vec {x}/\vec {p}]\) results from applying the polynomials \(\vec {u}\) to the polynomials \(\vec {p}\). Similarly, we can also apply a formula to polynomials \(\vec {p} \in \left( \mathcal {S}[\vec {x}]\right) ^d\). To this end, we define \(\psi (\vec {p}) = \psi [\vec {x}/\vec {p}]\) for first-order formulas \(\psi\) with the free variables \(\vec {x}\). As usual, function application associates to the left, i.e., \(\vec {u}(\vec {b}) (\vec {p})\) stands for \((\vec {u}(\vec {b})) (\vec {p})\). However, obviously \((\vec {u}(\vec {b})) (\vec {p}) = \vec {u}(\vec {b} (\vec {p}))\) since applying polynomials only means that one instantiates variables.

Definition 2.1 formalizes the intuitive notion of termination for a loop \((\varphi , \vec {u})\) and the related notion of eventual termination [10, 62]. Here, \(\vec {u}^n\) denotes the n-fold application of \(\vec {u}\), i.e., \(\vec {u}^0(\vec {e}) = \vec {e}\) and \(\vec {u}^{n+1}(\vec {e}) = \vec {u}(\vec {u}^n(\vec {e}))\).

Definition 2.1

(Termination) Let \((\varphi ,\vec {u})\) be a loop over \(\mathcal {S}\) and \(\vec {e}\in \mathcal {S}^{d}\).

If \(\forall n \in \mathbb {N}.\ \varphi (\vec {u}^n(\vec {e}))\) holds, then \(\vec {e}\) is a witness for non-termination.

If \((\varphi , \vec {u})\) does not have any witnesses for non-termination, it terminates (over \(\mathcal {S}\)).

If \(\vec {u}^{n_0}(\vec {e})\) is a witness for non-termination for some \(n_0 \in \mathbb {N}\), then \(\vec {e}\) is called a witness for eventual non-termination.

\((E)NT _{(\varphi , \vec {u})}\) denotes the set of witnesses for (eventual) non-termination of \((\varphi , \vec {u})\) and we define \(T_{(\varphi ,\vec {u})} = \mathcal {S}^d {\setminus } NT _{(\varphi ,\vec {u})}\).

For any entity s, \(\mathcal {V}(s)\) is the set of all its free variables. Given an assignment \(\vec {x} \leftarrow \vec {u}\), the relation \({\succ _{\vec {u}}} \in \mathcal {V}(\vec {u}) \times \mathcal {V}(\vec {u})\) is the transitive closure of \(\{(x_i,x_j) \mid i,j \in \{1,\ldots ,d\}, i \ne j, x_j \in \mathcal {V}(u_i)\}\), i.e., \(x_i \succ _{\vec {u}} x_j\) means that \(x_i\) depends on \(x_j\). For example, if \(\vec {u}= (x_1 + x_2^2, \, x_2 + 1)\) then we have \({\succ _{\vec {u}}} = \{ (x_1,x_2) \}\).

Now we can introduce the class of twn-loops. A loop \((\varphi ,\vec {u})\) is triangular if \({\succ _{\vec {u}}}\) is well founded. It is weakly non-linear if there is no \(1 \le i \le d\) such that \(x_i\) occurs in a non-linear monomial of \(u_i\), i.e., \(u_i = c_i \cdot x_i + p_i\) where \(c_i \in \mathcal {S}\) and \(p_i \in \mathcal {S}[\vec {x}]\) does not contain \(x_i\). A twn-loop is triangular and weakly non-linear. We call a loop non-negative if it is weakly non-linear and the coefficient \(c_i\) of the monomial \(x_i\) in \(u_i\) is non-negative for all \(1 \le i \le d\). A tnn-loop is triangular and non-n egative, i.e., a tnn-loop is a special form of a twn-loop.

The restriction to triangular loops prohibits “cyclic dependencies” of variables (e.g., where the new values of \(x_1\) and \(x_2\) both depend on the old values of \(x_1\) and \(x_2\)). For example, the loop whose body consists of the assignment \((x_1,x_2) \leftarrow (x_1 + x_2^2,\, x_2+1)\) is triangular since \({\succ } = \{(x_1,x_2)\}\) is well founded, whereas a loop with the body \((x_1,x_2) \leftarrow (x_1 + x_2^2,\,x_1+1)\) is not triangular.

By the restriction to twn-loops we can compute a closed form for the n-fold application of the update \(\vec {u}\) by handling one variable after the other. A vector \(\vec {q}\) of d arithmetic expressions over \(\vec {x}\) and a distinguished variable n is a closed form for \(\vec {u}\in (\mathcal {S}[\vec {x}])^d\) if \(\vec {q}[\vec {x}/\vec {e},n/n'] = \vec {u}^{n'}(\vec {e})\) for all \(\vec {e}\in \mathcal {S}^d\) and \(n' \in \mathbb {N}\), i.e., both vectors of expressions evaluate to the same element of \(\mathcal {S}^d\). Thus, \(\vec {q}= \vec {u}^n\).

Triangular loops are very common in practice. For example, in [21], 1511 polynomial loops were extracted from the Termination Problems Data Base (TPDB) [63], the benchmark collection used at the annual Termination and Complexity Competition [25], and only 26 of them were non-triangular.

A loop with the body \((x_1,x_2) \leftarrow (x_1 +x_2^2,\, x_2+1)\) is weakly non-linear, while a loop with \((x_1,x_2) \leftarrow (x_1 \cdot x_2,\, x_2+1)\) is not. In particular, weak non-linearity excludes assignments like \(x_1 \leftarrow x_1^2\) that need exponential space, as \(x_1\) grows doubly exponentially. By permuting variables, the update of every twn-loop can be transformed to the following form where \(c_i \in \mathcal {S}\) and \(p_i \in \mathcal {S}[x_{i+1},\ldots ,x_{d}]\):

$$\begin{aligned} \begin{bmatrix} x_1\\ \ldots \\ x_d \end{bmatrix} \leftarrow \begin{bmatrix} c_1 \cdot x_1 + p_1\\ \ldots \\ c_d \cdot x_d + p_d \end{bmatrix} \end{aligned}$$

Example 2.2

Consider the loop \(\textbf{L}_{ ex }\) over the ring \(\mathbb {Z}\) in Fig. 2. This loop is triangular since \({\succ _{\vec {u}}} = \{(x_1,x_2),(x_1,x_3),(x_2,x_3)\}\) is well founded. Moreover, it is weakly non-linear. Since the coefficient of \(x_i\) is 1 in the update of \(x_i\) for all \(1 \le i \le 3\), this loop is also non-negative, i.e., \(\textbf{L}_{ ex }\) is tnn.

Our twn-loops are a special case of solvable loops.

Definition 2.3

(Solvable Loops [56]) A loop \((\varphi ,\vec {u})\) is solvable if there is a partitioning \(\mathcal {J} = \{J_1,\ldots ,J_k\}\) of \(\{1,\ldots ,d\}\) such that for each \(1 \le i \le k\) we have \(\vec {u}_{J_i} = A_i \cdot \vec {x}_{J_i} + \vec {p}_i\), where \(\vec {u}_{J_i}\) is the vector of all \(u_j\) with \(j \in J_i\) (and \(\vec {x}_{J_i}\) is defined analogously), \(d_i = \vert {}J_i\vert {}\), \(A_i \in \mathcal {S}^{d_i \times d_i}\), and \(\vec {p}_i \in (\mathcal {S}[\vec {x}_{J_{i+1}}, \ldots , \vec {x}_{J_k}])^{d_i}\).

The eigenvalues of a solvable loop are the union of the eigenvalues of all \(A_i\).

So solvable loops allow for blocks of variables with linear dependencies, and twn-loops correspond to the case that all blocks have size 1. While our approach could be generalized to solvable loops with real eigenvalues, Theorem 5.15 (Sect. 5) shows that this generalization does not increase its applicability.

For our decidability results in Sect. 4, we reduce termination to the existential fragment \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S})\) of the first-order theory of \(\mathcal {S}\) (see, e.g., [52, 57]). \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S})\) consists of all formulas \(\exists \vec {y} \in \mathcal {S}^k. \, \psi\) where \(k \in \mathbb {N}\) and the propositional formula \(\psi\) is built from \(\wedge\) and \(\vee\) over the atoms \(\{p \triangleright 0 \mid p \in \mathbb {Q}[\vec {y}, \vec {z}],\ {\triangleright } \in \{{\ge }, {>}\}\}\). Here, \(\vec {y}\) and \(\vec {z}\) are pairwise disjoint vectors of variables. The free variables \(\vec {z}\) range over \(\mathbb {R}_{\mathbb {A}}\) and they are needed in Sect. 5 to characterize subsets of real (algebraic) numbers by formulas.

The existential fragment of the first-order theory of \(\mathcal {S}\) and \(\mathbb {R}_{\mathbb {A}}\) is the set \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\) of all formulas \(\exists \vec {y}\,' \in \mathbb {R}_{\mathbb {A}}^{k'}, \vec {y} \in \mathcal {S}^k.\, \psi\), with a propositional formula \(\psi\) over \(\{p \triangleright 0 \mid p \in \mathbb {Q}[\vec {y}\,', \vec {y}, \vec {z}],\ {\triangleright } \in \{{\ge }, {>}\}\}\) where \(k', k \in \mathbb {N}\) and the variables \(\vec {y}\,'\), \(\vec {y}\), and \(\vec {z}\) are pairwise disjoint. A formula without free variables is closed.

In the following, we also consider formulas over inequations \(p \triangleright 0\) where p’s coefficients are from \(\mathbb {R}_{\mathbb {A}}\) to be elements of \({{\,\mathrm{Th_\exists }\,}}(\mathbb {R}_{\mathbb {A}})\) (resp. \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\)). The reason is that real algebraic numbers are \({{\,\mathrm{Th_\exists }\,}}(\mathbb {R}_{\mathbb {A}})\)-definable.

Validity of closed formulas from \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S})\) or \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\) is decidable if \(\mathcal {S}\in \{\mathbb {R}_{\mathbb {A}},\mathbb {R}\}\) and semi-decidable if \(\mathcal {S}\in \{\mathbb {Z},\mathbb {Q}\}\) [13, 61]. By undecidability of Hilbert’s Tenth Problem [46], it is undecidable for \(\mathcal {S}= \mathbb {Z}\). While validity of full first-order formulas (i.e., also containing universal quantifiers) over \(\mathcal {S}= \mathbb {Q}\) is undecidable [53], it is still open whether validity of closed formulas from \({{\,\mathrm{Th_\exists }\,}}(\mathbb {Q})\) or \({{\,\mathrm{Th_\exists }\,}}(\mathbb {Q},\mathbb {R}_{\mathbb {A}})\) is decidable. However, validity of linear closed formulas from \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S})\) or \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\) is decidable for all \(\mathcal {S}\in \{\mathbb {Z},\mathbb {Q},\mathbb {R}_{\mathbb {A}},\mathbb {R}\}\) [15, 26, 33, 37]. Here, a formula is linear if it only contains atoms \(p \triangleright 0\) where p is linear.

3 Reducing termination of twn-loops to termination of tnn-loops

For analyzing termination of twn-loops, we can restrict ourselves to tnn-loops, as twn-loops can be (automatically) transformed into tnn-loops via chaining.

Definition 3.1

(Chaining) Chaining a loop \((\varphi , \vec {u})\) yields \((\varphi \wedge \varphi (\vec {u}), \vec {u}(\vec {u}))\).

So for example, chaining the loop \((x_1 > 0, -x_1)\) (i.e., “while \((x_1>0)\) do \(x_1 \leftarrow -x_1\)”) yields \((x_1>0 \wedge -x_1 > 0, \, x_1)\). Analogous to [19] where chaining was used for triangular linear loops, we obtain the following theorem.

Theorem 3.2

(Soundness of Chaining) Let \((\varphi ,\vec {u})\) be a twn-loop on \(\mathcal {S}^d\).

  1. (a)

    \((\varphi \wedge \varphi (\vec {u}), \vec {u}(\vec {u}))\) is tnn, i.e., it is triangular and the coefficient of each \(x_i\) in \(u_i(\vec {u})\) is non-negative.

  2. (b)

    \((\varphi ,\vec {u})\) terminates on \(\vec {e}\in \mathcal {S}^d\) iff \((\varphi \wedge \varphi (\vec {u}), \vec {u}(\vec {u}))\) terminates on \(\vec {e}\).

As chaining is clearly computable, we get the following corollary.

Corollary 3.3

Termination of twn-loops is reducible to termination of tnn-loops.

It is well known that closed forms for tnn-loops are computable (see, e.g., [35, 67]) since their bodies correspond to C-finite recurrences, which are known to be solvable [34]. The resulting closed forms may contain polynomial arithmetic and exponentiation w.r.t. n (as, e.g., \(x_1 \leftarrow 2 \cdot x_1\) has the closed form \(x_1 \cdot 2^n\)) as well as certain piecewise defined functions. For example, the closed form \(x_1^{(n)}\) of \(x_1 \leftarrow 1\) is \(x_1^{(0)} = x_1\) and \(x_1^{(n)} = 1\) for all \(n \in \mathbb {N}\) with \(n > 0\).

We represent closed forms using poly-exponential expressions [19], where instead of handling piecewise defined functions via disjunctions (as in [35]), we simulate them via Iverson brackets. For a formula \(\psi\) over n, its Iverson bracket \(\left[ {\psi } \right] :\mathbb {N}\rightarrow \{0,1\}\) evaluates to 1 iff \(\psi\) is satisfied (i.e., \(\left[ {\psi } \right] (e) = 1\) if \(\psi [n/e]\) holds and \(\left[ {\psi } \right] (e) = 0\), otherwise). Later, Iverson brackets can be replaced by the constants 0 or 1, as we only use them for formulas \(\psi\) that are constantly false or true for large enough values of n, see Sect. 4. Poly-exponential expressions are sums of arithmetic terms over the variables \(\vec {x}\) and the additional designated variable n, where it is always clear which addend determines the asymptotic growth of the expression when increasing n. This is crucial for our reducibility proof in Sect. 4. Definition 3.4 slightly generalizes the poly-exponential expressions from [19, Def. 9] by allowing arbitrary polynomials over \(\vec {x}\) (instead of just linear expressions) as coefficients. In the following, for any set \(X\subseteq \mathbb {R}\), any \(k \in X\), and \({\triangleright } \in \{ \ge , > \}\), let \(X_{\triangleright k} = \left\{ {x \in X} \mid {x \triangleright k} \right\}\).

Definition 3.4

(Poly-Exponential Expressions) Let \(\mathcal {C}\) be the set of all finite conjunctions over \(\{n = c, n \ne c \mid c \in \mathbb {N}\}\) where n is a designated variable and let \(\mathcal {Q}_{\mathcal {S}} = \left\{ {\tfrac{r}{s}} \mid {r \in \mathcal {S}, s \in \mathcal {S}_{> 0}} \right\}\) be the quotient field of \(\mathcal {S}\). Then the set of all poly-exponential expressions with the variables \(\vec {x}\) over \(\mathcal {S}\) is

$$\begin{aligned} \mathbb{P}\mathbb{E}_{\mathcal {S}}[\vec {x}] = \{ \sum \nolimits _{j=1}^\ell \left[ {\psi _j} \right] \cdot \alpha _j \cdot n^{a_j} \cdot b_j^n \mid \ell , a_j \in \mathbb {N}, \, \psi _j \in \mathcal {C}, \, \alpha _j \in \mathcal {Q}_{\mathcal {S}}[\vec {x}], \, b_j \in \mathcal {S}_{> 0} \}. \end{aligned}$$

An example for a poly-exponential expression over \(\mathbb {Z}\) (with \(\mathcal {Q}_{\mathbb {Z}} = \mathbb {Q}\)) is

$$\begin{aligned} \left[ {n \ne 0 \wedge n \ne 1} \right] \cdot \left( \tfrac{1}{2} \cdot {x_1^2} + \tfrac{3}{4} \cdot {x_2} -1 \right) \cdot n^3 \cdot 3^n + \left[ {n = 1} \right] \cdot ({x_1}-{x_2}). \end{aligned}$$

The restriction to tnn-loops ensures that for the closed form \(\vec {q}\) of the update we indeed have \(\vec {q}\in \left( \mathbb{P}\mathbb{E}_{\mathcal {S}}[\vec {x}]\right) ^d\). For example, for arbitrary matrices \(A \in \mathbb {R}_{\mathbb {A}}^{d\times d}\), the update \(\vec {x} \leftarrow A \cdot \vec {x}\) is known to admit a closed form as in Definition 3.4 with complex \(b_j\)’s, whereas real numbers suffice for triangular matrices. Moreover, non-negativity is required to ensure \(b_j > 0\) (e.g., a non-tnn loop with the update \(x_1 \leftarrow -x_1\) has the closed form \(x_1 \cdot (-1)^n\)).

Example 3.5

For \(\textbf{L}_{ ex }\) in Fig. 2, the closed form is \(\vec {q}\in \left( \mathbb{P}\mathbb{E}_{\mathbb {Z}}[\vec {x}]\right) ^3\) with

$$\begin{aligned} \vec {q}= \begin{bmatrix} \tfrac{4}{3}\cdot x_3^{5}\cdot {n}^{3}+ \left( -2\cdot x_3^{5}-2\cdot x_2\cdot x_3^{3} \right) \cdot {n}^{2}+ \left( x_2^{2}\cdot x_3 +\tfrac{2}{3}\cdot x_3^{5}+2\cdot x_2\cdot x_3^{3} \right) \cdot n+x_1 \\ -2\cdot x_3^{2}\cdot n+x_2 \\ x_3 \end{bmatrix}. \end{aligned}$$

4 Reducing termination of tnn-loops to \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S})\)

It is known that the bodies of tnn-loops can be linearized [48], i.e., one can reduce termination of a tnn-loop \((\varphi ,\vec {u})\) to termination of a linear-update tnn-loop \((\varphi ',\vec {u}')\), where \(\varphi '\) may be non-linear. See Sect. 6 for a discussion of linearization and novel results on the linearization procedure. Moreover, [64, 65] showed decidability of termination for certain classes of conjunctive linear-update loops over \(\mathbb {R}\), which cover conjunctive linear-update tnn-loops. So, by combining the results of [48] and [64, 65], one can conclude that termination of conjunctive tnn-loops over \(\mathbb {R}\) is decidable.

In contrast, we now present a reduction of termination of tnn-loops to \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S})\) which applies to tnn-loops over any ring \(\mathbb {Z}\le \mathcal {S}\le \mathbb {R}\) and which can also handle disjunctions in the loop condition. Moreover, our reduction yields tight complexity results on termination of linear loops over \(\mathbb {Z}\), \(\mathbb {Q}\), \(\mathbb {R}_{\mathbb {A}}\), and \(\mathbb {R}\), and on termination of linear-update loops over \(\mathbb {R}_{\mathbb {A}}\) and \(\mathbb {R}\) (see Sect. 7.1).

Similar to [19], our reduction exploits that for tnn-loops \((\varphi ,\vec {u})\) there is a closed form \(\vec {q}\) for \(\vec {u}\) with \(\vec {q}\in \left( \mathbb{P}\mathbb{E}_{\mathcal {S}}[\vec {x}]\right) ^d\). However, in [19] we only considered conjunctive linear loops over \(\mathbb {Z}\). In contrast, we now analyze loops over \(\mathcal {S}\) for any \(\mathbb {Z}\le \mathcal {S}\le \mathbb {R}_{\mathbb {A}}\) and allow non-linearity and arbitrary propositional formulas in the condition. Thus, the correctness proofs differ substantially from [19].

More precisely, we show that there is a function with the following specification that is computable in polynomial time:

$$\begin{aligned} \begin{array}{rl} \text {Input}: &{} \quad \text {a } \textit{tnn} \text {-loop} (\varphi ,\vec {u}) \text { over } \mathcal {S}\text { with closed form } \vec {q}\in \left( \mathbb{P}\mathbb{E}_{\mathcal {S}}[\vec {x}]\right) ^d \\ \text {Result}: &{} \quad \text {a closed formula } \chi \in {{\,\mathrm{Th_\exists }\,}}(\mathcal {S}) \text { such that} \\ &{} \quad \chi \text { is valid iff } (\varphi ,\vec {u}) \text { does not terminate on } \mathcal {S}^d \end{array} \end{aligned}$$
(1)

We use the concept of eventual non-termination, i.e., the condition of the loop may be violated finitely often, see Definition 2.1. Clearly, \((\varphi ,\vec {u})\) is non-terminating iff it is eventually non-terminating [49]. The formula \(\chi\) in (1) will encode the existence of a witness for eventual non-termination. By the definition of \(\vec {q}\), eventual non-termination of \((\varphi ,\vec {u})\) on \(\mathcal {S}^d\) is equivalent to validity of

$$\begin{aligned} \exists \vec {x} \in \mathcal {S}^d, \, n_0 \in \mathbb {N}.\ \forall n \in \mathbb {N}_{>n_0}.\ \varphi (\vec {q}). \end{aligned}$$
(2)

Example 4.1

Continuing Example 3, \(\textbf{L}_{ ex }\) is eventually non-terminating over \(\mathbb {Z}\) iff there is a corresponding witness \(\vec {e}\in \mathbb {Z}^3\), i.e., iff

$$\begin{aligned}&\exists x_1,x_2,x_3 \in \mathbb {Z}, \, n_0 \in \mathbb {N}.\ \forall n \in \mathbb {N}_{>n_0}. \; p>0 \\ \text {is valid where} \quad p = \;&(x_1 + x_2^{2} ) + ( x_2^{2}\cdot x_3 + \tfrac{2}{3}\cdot x_3^{5} +2\cdot x_2\cdot x_3^{3} -4\cdot x_2\cdot x_3^{2} )\cdot n \nonumber \\ &{} + ( -2\cdot x_3^{5}-2\cdot x_2\cdot x_3^{3}+4\cdot x_3^{4} )\cdot {n}^{2} + (\tfrac{4}{3}\cdot x_3^{5})\cdot {n}^{3}.\nonumber \end{aligned}$$
(3)

Let \(\vec {q}_{ norm }\) be like \(\vec {q}\), but each factor \(\left[ {\psi } \right]\) is replaced by 0 if it contains an equation and by 1, otherwise. The reason is that for large enough n, equations in \(\psi\) become \(\textrm{false}\) and negated equations become \(\textrm{true}\). So (2) is equivalent to

$$\begin{aligned} \exists \vec {x} \in \mathcal {S}^d, \, n_0 \in \mathbb {N}.\ \forall n \in \mathbb {N}_{>n_0}.\ \varphi (\vec {q}_{ norm }). \end{aligned}$$
(4)

In this way, we obtain normalized poly-exponential expressions.

Definition 4.2

(Normalized \(\mathbb{P}\mathbb{E}_{}\)s) We call \(p \in \mathbb{P}\mathbb{E}_{\mathcal {S}}[\vec {x}]\) normalized if it is in

$$\begin{aligned} \mathbb {NPE}_{\mathcal {S}}[\vec {x}] = \Bigg\{\sum \nolimits _{j=1}^\ell \alpha _j \cdot n^{a_j} \cdot b_j^n \mid \ell , a_j \in \mathbb {N}, \; \alpha _j \in \mathcal {Q}_{\mathcal {S}}[\vec {x}], \; b_j \in \mathcal {S}_{> 0} \Bigg\}, \end{aligned}$$

where w.l.o.g. \((b_i,a_i) \ne (b_j,a_j)\) if \(i \ne j\). We define \(\mathbb {NPE}_{\mathcal {S}} = \mathbb {NPE}_{\mathcal {S}}[\emptyset ]\).

As \(\varphi\) is a propositional formula over \(\mathcal {S}[\vec {x}]\)-inequations, \(\varphi (\vec {q}_{ norm })\) is a propositional formula over \(\mathbb {NPE}_{\mathcal {S}}[\vec {x}]\)-inequations. By (4), we need to check whether \(\exists \vec {x} \in \mathcal {S}^d.\ \varphi (\vec {q}_{ norm })\) is valid for large enough n. To this end, we will examine the dominant terms in the inequations of \(\varphi (\vec {q}_{ norm })\).

Definition 4.3

(Asymptotic Domination [38]) A function \(g: \mathbb {N}\rightarrow \mathbb {R}\) dominates a function \(f: \mathbb {N}\rightarrow \mathbb {R}\) asymptotically (\(f \in o(g)\)) if for all \(m>0\) there is an \(n_0 \in \mathbb {N}\) such thatFootnote 1\(\vert {}f(n)\vert {} < m \cdot \vert {}g(n)\vert {}\) for all \(n \in \mathbb {N}_{>n_0}\).

Now we can state the following lemma which generalizes [19, Lemma 24].

Lemma 4.4

Let \(b_1,b_2 \in \mathcal {S}_{> 0}\) and \(a_1, a_2 \in \mathbb {N}\). If \((b_2, a_2) >_{lex} (b_1, a_1)\), then \(n^{a_1} \cdot b_1^n \in o(n^{a_2} \cdot b_2^n)\), where \((b_2,a_2) >_{lex} (b_1,a_1)\) iff \(b_2 > b_1\) or \(b_2 = b_1 \wedge a_2 > a_1\).

In the following, let \(p\ge 0\) or \(p > 0\) occur in \(\varphi (\vec {q}_{ norm })\). We can order the coefficients of p according to the asymptotic growth of their addends w.r.t. n.

Definition 4.5

(Ordering Coefficients) Marked coefficients are of the form \(\alpha ^{(b,a)}\) where \(\alpha \in \mathcal {Q}_{\mathcal {S}}[\vec {x}], b \in \mathcal {S}_{>0}\), and \(a \in \mathbb {N}\). We define \({{\,\textrm{unmark}\,}}(\alpha ^{(b,a)}) = \alpha\) and \(\alpha _2^{(b_2,a_2)} \succ _{ coef }\alpha _1^{(b_1,a_1)}\) if \((b_2,a_2) >_{lex} (b_1,a_1)\). Let \(p = \sum _{j=1}^\ell \alpha _j \cdot n^{a_j} \cdot b_j^n \in \mathbb {NPE}_{\mathcal {S}}[\vec {x}]\), where \(\alpha _j \ne 0\) for all \(1 \le j \le \ell\). Then the marked coefficients of p are

$$\begin{aligned} {{\,\textrm{coefs}\,}}\left( p\right) = {\left\{ \begin{array}{ll} \{0^{(1,0)}\}, &{} \text {if } \ell = 0 \\ \{\alpha _j^{(b_j,a_j)} \mid 1 \le j \le \ell \}, &{} \text {otherwise.} \end{array}\right. } \end{aligned}$$

Example 4.6

Continuing Example 4.1, \({{\,\textrm{coefs}\,}}\left( p\right)\) is \(\{ \alpha _1^{(1,0)}, \alpha _2^{(1,1)}, \alpha _3^{(1,2)}, \alpha _4^{(1,3)} \}\) where

$$\begin{aligned} \begin{array}{l@{\qquad}l} \alpha _1 = x_1 + x_2^{2} &{} \alpha _2 = x_2^{2}\cdot x_3 + \tfrac{2}{3}\cdot x_3^{5}+ 2\cdot x_2\cdot x_3^{3} -4\cdot x_2\cdot x_3^{2} \\ \alpha _3 = -2\cdot x_3^{5}-2\cdot x_2\cdot x_3^{3}+4\cdot x_3^{4} &{} \alpha _4 = \tfrac{4}{3}\cdot x_3^{5} \end{array} \end{aligned}$$

Note that \(p(\vec {e}) \in \mathbb {NPE}_{\mathcal {S}}\) for any \(\vec {e}\in \mathcal {S}^d\), i.e., the only variable in \(p(\vec {e})\) is n. Now the \(\succ _{ coef }\)-maximal addend determines the asymptotic growth of \(p(\vec {e})\):

$$\begin{aligned} o(p(\vec {e})) = o(k \cdot n^a \cdot b^n) \quad \text {where } k^{(b,a)} = \max \nolimits _{\succ _{ coef }}\left( {{\,\textrm{coefs}\,}}\left( p(\vec {e})\right) \right) . \end{aligned}$$
(5)

Note that (5) would be incorrect for the case \(k = 0\) if we replaced \(o(p(\vec {e})) = o(k \cdot n^a \cdot b^n)\) with \(o(p(\vec {e})) = o(n^a \cdot b^n)\) as \(o(0) = \emptyset \ne o(1)\). Obviously, (5) implies

$$\begin{aligned} \exists n_0 \in \mathbb {N}.\ \forall n \in \mathbb {N}_{> n_0}.\ {{\,\textrm{sign}\,}}\left( p(\vec {e})\right) = {{\,\textrm{sign}\,}}\left( k\right) , \end{aligned}$$
(6)

where \({{\,\textrm{sign}\,}}\left( 0\right) = 0\), \({{\,\textrm{sign}\,}}\left( k\right) = 1\) if \(k > 0\), and \({{\,\textrm{sign}\,}}\left( k\right) = -1\) if \(k < 0\). This allows us to reduce eventual non-termination to \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S})\) if \(\varphi\) is an atom. In the following, let \({{\,\textrm{coefs}\,}}\left( p\right) = \{\alpha _1^{(b_1,a_1)}, \ldots , \alpha ^{(b_{\ell },a_{\ell })}_{\ell }\}\), where \(\alpha ^{(b_i,a_i)}_i \prec _{ coef }\alpha ^{(b_{j},a_{j})}_{j}\) for all \(1 \le i < j \le \ell\). Then we define

$$\begin{aligned} \begin{array}{ll} &{} {{\,\textrm{red}\,}}(p> 0) \, = \, \bigvee _{i=1}^\ell (\alpha _i> 0 \wedge \bigwedge _{j=i+1}^{\ell } \alpha _j = 0) \\ \text {and} &{} {{\,\textrm{red}\,}}(p \ge 0) \, = \, {{\,\textrm{red}\,}}(p>0) \vee \bigwedge _{i=1}^{\ell } \alpha _i = 0. \end{array} \end{aligned}$$
(7)

Lemma 4.7

Given \(p \in \mathbb {NPE}_{\mathcal {S}}[\vec {x}]\) and \({\triangleright } \in \{{\ge }, {>}\}\), validity of

$$\begin{aligned} \begin{array}{l} \exists \vec {x} \in \mathcal {S}^d, \, n_0 \in \mathbb {N}.\ \forall n \in \mathbb {N}_{>n_0}.\ p \triangleright 0 \end{array} \end{aligned}$$
(8)

can be reduced to validity of the closed formula \(\exists \vec {x} \in \mathcal {S}^d.\ {{\,\textrm{red}\,}}(p \triangleright 0)\) from \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S})\). This reduction takes polynomially many steps in the size of p.

To gain intuition for the formula \({{\,\textrm{red}\,}}(p \triangleright 0)\), note that by (6), we have \(p(\vec {e}) > 0\) for large enough values of n iff the coefficient of the asymptotically fastest growing addend \(\alpha (\vec {e}) \cdot n^a \cdot b^n\) that does not vanish (i.e., where \(\alpha (\vec {e}) \ne 0\)) is positive. Similarly, we have \(p(\vec {e}) < 0\) for large enough n iff \(\alpha (\vec {e}) < 0\). If all addends of p vanish when instantiating \(\vec {x}\) with \(\vec {e}\), then \(p(\vec {e}) = 0\). In other words, (8) holds iff there is an \(\vec {e}\in \mathcal {S}^d\) such that \({{\,\textrm{unmark}\,}}\left( \max \nolimits _{\succ _{ coef }}\left( {{\,\textrm{coefs}\,}}\left( p(\vec {e})\right) \right) \right) \, \triangleright \, 0\). The formula \({{\,\textrm{red}\,}}(p\triangleright 0)\) expresses the latter in \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S})\).

Example 4.8

We continue Example 4.6. By the construction in Lemma 4.7, (3) is valid iff \(\exists x_1,x_2,x_3 \in \mathbb {Z}.\ {{\,\textrm{red}\,}}(p > 0)\) is valid, where \({{\,\textrm{red}\,}}(p > 0)\) is

$$\begin{aligned} \begin{array}{rlcl} &{} \left( \alpha _1> 0 \wedge \alpha _2 = 0 \wedge \alpha _3 = 0 \wedge \alpha _4 = 0\right) &{} \vee &{} \left( \alpha _2> 0 \wedge \alpha _3 = 0 \wedge \alpha _4 = 0\right) \\ \vee &{} \left( \alpha _3> 0 \wedge \alpha _4 = 0 \right) &{} \vee &{} \alpha _4 > 0. \end{array} \end{aligned}$$

For example, \([x_1/-4, x_2/2,x_3/1]\) satisfies \(\alpha _4 >0\) as \(\left( \frac{4}{3} \cdot x_3^5\right) [x_1/-4, x_2/2, x_3/1] > 0\). Thus, \((-4,2,1)\) witnesses eventual non-termination of \(\textbf{L}_{ ex }\) over \(\mathbb {Z}\).

Now we lift our reduction to propositional formulas. To handle disjunctions, the proof of Theorem 4.9 exploits the crucial insight that a tnn-loop \((\varphi \vee \varphi ', \vec {u})\) terminates iff \((\varphi , \vec {u})\) and \((\varphi ', \vec {u})\) terminate, which is not true in general (as, e.g., witnessed by the loop \((x_1>0 \vee -x_1>0, -x_1)\)). In the following, the formula \({{\,\textrm{red}\,}}(\xi )\) results from \(\xi\) by replacing each atom \(p \triangleright 0\) in \(\xi\) by \({{\,\textrm{red}\,}}(p \triangleright 0)\).

Theorem 4.9

(Reducing Eventual Non-Termination) For a propositional formula \(\xi\) over the atoms \(\{p \triangleright 0 \mid p \in \mathbb {NPE}_{\mathcal {S}}[\vec {x}], \triangleright \in \{\ge , >\}\}\), validity of

$$\begin{aligned} \exists \vec {x} \in \mathcal {S}^d, n_0 \in \mathbb {N}.\ \forall n \in \mathbb {N}_{>n_0}.\ \xi \end{aligned}$$
(9)

can be reduced to validity of the closed formula \(\exists \vec {x} \in \mathcal {S}^d.{{\,\textrm{red}\,}}(\xi ) \in {{\,\mathrm{Th_\exists }\,}}(\mathcal {S})\). This reduction takes polynomially many steps in the size of \(\xi\).

The time needed to compute the formula (9) is polynomial in the sum of the sizes of all poly-exponential expressions in \(\xi\). So the function (1) is computable in polynomial time w.r.t. the size of its input: \(\vec {q}_{ norm }\) can clearly be computed in polynomial time from \(\vec {q}\) and we can then apply Theorem 4.9 to \(\varphi (\vec {q}_{ norm })\). Combining Corollary 3.3, (4), and Theorem 4.9 leads to the main result of this section.

Theorem 4.10

(Reducing Termination) Termination of tnn-loops (resp. twn-loops) on \(\mathcal {S}^d\) is reducible to \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S})\).

However, if the update contains non-linear terms, then its closed form and hence this reduction are not always computable in polynomial space (and thus, also not in polynomial time). Consider the following tnn-loop \(\textbf{L}_{non-pspace}\):

$$\begin{aligned} \textbf{while}\,(\textrm{true} )\,\textbf{do} \left( x_1,\, x_2, \ldots , \,x_{d-1},\, x_d\right) \leftarrow \left( x_2^d,\, x_3^d, \ldots ,\, x_d^d, \, d \cdot x_d\right) \end{aligned}$$
(10)

The closed form for \(x_i\) (i.e., the value of \(x_i\) after n loop iterations) is \(q_i = d^{(d^{d-i}\cdot (n-d+i))}\cdot x_d^{(d^{d-i})}\) for all \(n \ge d\). Thus, the closed form \(q_1\) for \(x_1\) contains constants like \(d^{(d^{d-1})}\) whose logarithm grows faster than any polynomial in d. Hence, \(q_1\) cannot be computed in polynomial space.

Instead of computing closed forms directly, one could first linearize the loop (see [48] and Sect. 6) and then compute the closed form for the resulting linear-update loop. However, this approach cannot be computed in polynomial space either, because the linearization of \(\textbf{L}_{non-pspace}\) contains the constant \(d^{(d^{d-1})}\) as well (see Example 6.14 in Sect. 6). We refer to Sect. 7 for an analysis of the complexity of deciding termination for twn-loops.

Our reduction also works if \(\mathcal {S}= \mathbb {R}\), i.e., termination over \(\mathbb {R}\) is reducible to \({{\,\mathrm{Th_\exists }\,}}(\mathbb {R})\), since \(\mathbb {R}\) and \(\mathbb {R}_{\mathbb {A}}\) are elementary equivalent (i.e., a first-order formula is valid over \(\mathbb {R}\) iff it is valid over \(\mathbb {R}_{\mathbb {A}}\), see, e.g., [2]). Thus, we get the following corollary by using that validity of closed formulas from \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S})\) is decidable for \(\mathcal {S}\in \{\mathbb {R}_{\mathbb {A}},\mathbb {R}\}\) and semi-decidable for \(\mathcal {S}\in \{\mathbb {Z},\mathbb {Q}\}\) [13, 61].

Corollary 4.11

((Semi-)Deciding (Non-)Termination) Let \((\varphi ,\vec {u})\) be a twn-loop.

  1. (a)

    The loop \((\varphi ,\vec {u})\) terminates over \(\mathbb {R}_{\mathbb {A}}\) iff it terminates over \(\mathbb {R}\).

  2. (b)

    Termination of \((\varphi ,\vec {u})\) on \(\mathcal {S}^d\) is decidable if \(\mathcal {S}= \mathbb {R}_{\mathbb {A}}\) or \(\mathcal {S}= \mathbb {R}\).

  3. (c)

    Non-termination of \((\varphi ,\vec {u})\) on \(\mathcal {S}^d\) is semi-decidable if \(\mathcal {S}= \mathbb {Z}\) or \(\mathcal {S}= \mathbb {Q}\).

Our technique does not yield witnesses for non-termination, but the formula constructed by Theorem 4.9 describes the set of all witnesses for eventual non-termination. So this set can be characterized by a formula from \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S})\) (i.e., it is \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S})\)-definable, see Sect. 5.1), while in general the set of witnesses for non-termination cannot be characterized in this way (see [14]).

Lemma 4.12

Let \(\xi = \varphi (\vec {q}_{ norm })\). Then \(\vec {e}\in \mathcal {S}^d\) witnesses eventual non-termination of \((\varphi ,\vec {u})\) on \(\mathcal {S}^d\) iff \({{\,\textrm{red}\,}}(\xi ) (\vec {e})\) holds.

In [28], we show how to compute witnesses for non-termination from witnesses for eventual non-termination of twn-loops. Thus, combining Lemma 4.12 with [28] shows that \(NT _{(\varphi , \vec {u})}\) is recursively enumerable for twn-loops over \(\mathbb {Z}\le \mathcal {S}\le \mathbb {R}_{\mathbb {A}}\). Algorithm 1 summarizes our technique to check termination of twn-loops.

Algorithm 1
figure a

Checking Termination

5 Transformation to triangular weakly non-linear form

In this section, we show how to handle loops that are not yet twn. To this end, we introduce a transformation of loops via polynomial automorphisms in Sect. 5.1 and show that our transformation preserves (non-)termination (Theorem 5.10). In Sect. 5.2, we use results from algebraic geometry to show that the question whether a loop can be transformed into twn-form is reducible to validity of \({{\,\mathrm{Th_\exists }\,}}(\mathbb {R}_{\mathbb {A}})\)-formulas (Theorem 5.19). Moreover, we show that it is decidable whether a linear automorphism can transform a loop into a special case of the twn-form (Theorem 5.22). Finally, based on the transformation of Sects. 5.1 and 5.2 we generalize our results from Sect. 4 to certain non-twn loops in Sect. 5.3.

5.1 Transforming loops

Clearly, the polynomials \(x_1,\ldots ,x_d\) are generators of the \(\mathcal {S}\)-algebra \(\mathcal {S}[\vec {x}]\), i.e., every polynomial from \(\mathcal {S}[\vec {x}]\) can be obtained from \(x_1,\ldots ,x_d\) and the operations of the algebra (i.e., addition and multiplication). So far, we have implicitly chosen a special “representation” of the loop based on the generators \(x_1,\ldots ,x_d\).

We now change this representation, i.e., we use d different polynomials which are also generators of \(\mathcal {S}[\vec {x}]\). Then the loop has to be modified accordingly to adapt it to this new representation. This modification does not affect the loop’s termination behavior, but it may transform a non-twn-loop into twn-form. This change of representation is described by \(\mathcal {S}\)-automorphisms of \(\mathcal {S}[\vec {x}]\).

Definition 5.1

(\(\mathcal {S}\)-Endomorphisms) A mapping \(\eta :\mathcal {S}[\vec {x}] \rightarrow \mathcal {S}[\vec {x}]\) is an \(\mathcal {S}\)-endomorphism of \(\mathcal {S}[\vec {x}]\) if it is \(\mathcal {S}\)-linear and multiplicative, i.e., for all \(c,c' \in \mathcal {S}\) and all \(p,p' \in \mathcal {S}[\vec {x}]\) we have \(\eta (c \cdot p + c' \cdot p') = c \cdot \eta (p) + c' \cdot \eta (p')\), \(\eta (1)=1\), and \(\eta (p \cdot p') = \eta (p)\cdot \eta (p')\). We denote the set of all \(\mathcal {S}\)-endomorphisms of \(\mathcal {S}[\vec {x}]\) by \(\textrm{End}_{\mathcal {S}}\left( \mathcal {S}[\vec {x}]\right)\). The set \(\textrm{Aut}_{\mathcal {S}}\left( \mathcal {S}[\vec {x}]\right)\) of \(\mathcal {S}\)-automorphisms of \(\mathcal {S}[\vec {x}]\) consists of those \(\eta \in \textrm{End}_{\mathcal {S}}\left( \mathcal {S}[\vec {x}]\right)\) which are invertible, i.e., there exists an \(\eta ^{-1} \in \textrm{End}_{\mathcal {S}}\left( \mathcal {S}[\vec {x}]\right)\) with \(\eta \circ \eta ^{-1} = \eta ^{-1} \circ \eta = id_{\mathcal {S}[\vec {x}]}\), where \(id_{\mathcal {S}[\vec {x}]}\) is the identity function on \(\mathcal {S}[\vec {x}]\). \(\textrm{Aut}_{\mathcal {S}}\left( \mathcal {S}[\vec {x}]\right)\) is a group under function composition \(\circ\) with identity \(id_{\mathcal {S}[\vec {x}]}\).

Example 5.2

Let \(\eta \in \textrm{End}_{\mathcal {S}}\left( \mathcal {S}[x_1,x_2]\right)\) with \(\eta (x_1) = x_2\) and \(\eta (x_2) = x_1 - x_2^2\). Then \(\eta \in \textrm{Aut}_{\mathcal {S}}\left( \mathcal {S}[x_1,x_2]\right)\), where \(\eta ^{-1}(x_1) = x_1^2 + x_2\) and \(\eta ^{-1}(x_2) = x_1\).

As \(\mathcal {S}[\vec {x}]\) is free on the generators \(\vec {x}\), an endomorphism \(\eta \in \textrm{End}_{\mathcal {S}}\left( \mathcal {S}[\vec {x}]\right)\) is uniquely determined by the images of the variables, i.e., by \(\eta (x_1), \ldots , \eta (x_d)\). Hence, we have a one-to-one correspondence between elements of \(\left( \mathcal {S}[\vec {x}]\right) ^d\) and \(\textrm{End}_{\mathcal {S}}\left( \mathcal {S}[\vec {x}]\right)\). In particular, every tuple \(\vec {u}= (u_1,\ldots ,u_d) \in \left( \mathcal {S}[\vec {x}]\right) ^d\) corresponds to the unique endomorphism \(\widetilde{u} \in \textrm{End}_{\mathcal {S}}\left( \mathcal {S}[\vec {x}]\right)\) which is defined as follows:

$$\begin{aligned} \widetilde{u}(x_i) = u_i \qquad \text {for all } 1 \le i \le d \end{aligned}$$

So for any \(p \in \mathcal {S}[\vec {x}]\) we have \(\widetilde{u}(p) = p(\vec {u})\). Thus, the update of a loop induces an endomorphism which operates on polynomials.

Example 5.3

Consider the loop \(\textbf{L}_{ aut } = (\varphi , \vec {u})\) where \(\vec {u}= (u_1, u_2)\):

$$\begin{aligned} \textbf{while}\, ({x_2^3 + x_1-x_2^2 > 0}) \,\textbf{do}\, {{\begin{bmatrix}x_1\\ x_2 \end{bmatrix}}\leftarrow {\begin{bmatrix}((-x_2^{2}+x_1)^{2}+x_2)^{2}-2\cdot x_2^{2}+2\cdot x_1\\ (-x_2^{2}+x_1 )^{2}+x_2 \end{bmatrix}}} \end{aligned}$$
(11)

Then \(\vec {u}\) induces the endomorphism \(\widetilde{u}\) with \(\widetilde{u}(x_1) = u_1\) and \(\widetilde{u}(x_2) = u_2\). So we have \(\widetilde{u}(2 \cdot x_1 + x_2^3) = (2 \cdot x_1 + x_2^3)(\vec {u}) = 2 \cdot u_1 + u_2^3\).

Therefore, for a tuple of numbers like \(\vec {e}= (5,2)\), the induced endomorphism \(\widetilde{e}\) is \(\widetilde{e}(x_1) = 5\) and \(\widetilde{e}(x_2) = 2\). Thus, we have \(\widetilde{e}(x_2^3 + x_1 - x_2^2) = (x_2^3 + x_1 - x_2^2) (5,2) = 2^3 + 5 - 2^2 = 9\).

We extend the application of endomorphisms \(\eta : \mathcal {S}[\vec {x}] \rightarrow \mathcal {S}[\vec {x}]\) to vectors of polynomials \(\vec {u}= (u_1,\ldots ,u_d)\) by defining \(\eta (\vec {u}) = (\eta (u_1),\ldots , \eta (u_d))\) and to formulas \(\varphi \in {{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\) by defining \(\eta (\varphi ) = \varphi (\eta (\vec {x}))\), i.e., \(\eta (\varphi )\) results from \(\varphi\) by applying \(\eta\) to all polynomials that occur in \(\varphi\). This allows us to transform \((\varphi , \vec {u})\) into a new loop \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\) using any automorphism \(\eta \in \textrm{Aut}_{\mathcal {S}}\left( \mathcal {S}[\vec {x}]\right)\).

Definition 5.4

(\({{\,\mathrm{ T\!r\mathrm }\,}}\)) For \(\eta \in \textrm{Aut}_{\mathcal {S}}\left( \mathcal {S}[\vec {x}]\right)\), we define \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u}) = (\varphi ', \vec {u}\,')\) where

$$\begin{aligned} \varphi ' = \eta ^{-1}(\varphi ) \qquad \text {and} \qquad \vec {u}\,' = (\eta ^{-1}\circ \widetilde{u}\circ \eta )(\vec {x}). \end{aligned}$$

In other words, we have \(\vec {u}\,' = (\eta (\vec {x})) \, (\vec {u}) \, (\eta ^{-1}(\vec {x}))\) since \((\eta ^{-1}\circ \widetilde{u}\circ \eta )(\vec {x}) = \eta ^{-1}(\eta (\vec {x})[\vec {x}/\vec {u}]) = \eta (\vec {x})[\vec {x}/\vec {u}][\vec {x} / \eta ^{-1}(\vec {x})] = (\eta (\vec {x}))(\vec {u})(\eta ^{-1}(\vec {x}))\).

Example 5.5

We transform the loop \(\textbf{L}_{ aut }\) in (11) with the automorphism \(\eta\) from Example 5.2. We obtain \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u}) = (\varphi ', \vec {u}\,')\) where

$$\begin{aligned} \begin{array}{rcccc} \varphi ' &{} = &{} {\eta ^{-1}(\varphi ) = ((\eta ^{-1}(x_2))^3 + \eta ^{-1}(x_1) - (\eta ^{-1}(x_2))^2> 0 )} \\ &{} = &{} {(x_1^3 + x_1^2 + x_2 - x_1^2> 0) = (x_1^3 + x_2 > 0) \text { and } } \\ \vec {u}\,' {} = {} ((\eta ^{-1}\circ \widetilde{u}\circ \eta )(x_1), (\eta ^{-1}\circ \widetilde{u}\circ \eta )(x_2)) {} = {} (\eta ^{-1}(\widetilde{u}(x_2)), \eta ^{-1}(\widetilde{u}(x_1- x_2^2))) \\ &{} = &{} (\eta ^{-1}(u_2), \eta ^{-1}(u_1 - u_2^2)) {} = {} (x_1 + x_2^2, 2 \cdot x_2). \end{array} \end{aligned}$$

So the resulting transformed loop is \((x_1^3 +x_2 > 0,\ (x_1 + x_2^2, 2 \cdot x_2))\). Note that while the original loop \((\varphi , \vec {u})\) is neither triangular nor weakly non-linear, the resulting transformed loop is twn. Also note that we used a non-linear automorphism with \(\eta (x_2) = x_1 - x_2^2\) for the transformation.

While the above example shows that our transformation can indeed transform non-twn-loops into twn-loops, it remains to prove that this transformation preserves (non-)termination. Then we can use our techniques for termination analysis of twn-loops for twn-transformable-loops as well, i.e., for all loops \((\varphi , \vec {u})\) where \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\) is twn for some automorphism \(\eta\). (The question how to find such automorphisms will be addressed in Sect. 5.2.)

As a first step, by Lemma 5.6, our transformation is “compatible” with the operation \(\circ\) of the group \(\textrm{Aut}_{\mathcal {S}}\left( \mathcal {S}[\vec {x}]\right)\), i.e., it is an action.

Lemma 5.6

\(\textrm{Aut}_{\mathcal {S}}\left( \mathcal {S}[\vec {x}]\right)\) acts via \({{\,\mathrm{ T\!r\mathrm }\,}}\) on loops, i.e., for \(\eta _1, \eta _2 \in \textrm{Aut}_{\mathcal {S}}\left( \mathcal {S}[\vec {x}]\right)\), we have \({{\,\mathrm{ T\!r\mathrm }\,}}_{id_{\mathcal {S}[\vec {x}]}}(\varphi ,\vec {u}) = (\varphi , \vec {u})\) and \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta _1 \circ \eta _2}(\varphi ,\vec {u}) = Tr _{\eta _2}({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta _1}(\varphi ,\vec {u}))\).

The following lemma will enable us to generalize our results on witnesses for (eventual) non-termination to loops which can be transformed into twn-form.

Lemma 5.7

Let \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\!=\!(\varphi ', \vec {u}\,')\) and let \(\widehat{\eta }\!:\!\mathcal {S}^d\!\rightarrow \!\mathcal {S}^d\) map \(\vec {e}\) to \(\widehat{\eta }(\vec {e}) = \widetilde{e} (\eta (\vec {x})) = (\eta (\vec {x})) (\vec {e})\). Then \(\varphi (\vec {u}^n(\vec {e})) = \varphi '((\vec {u}\,')^n(\widehat{\eta }(\vec {e})))\) for all \(\vec {e}\in \mathcal {S}^d\) and \(n \in \mathbb {N}\).

Lemma 5.7 yields the following corollary which shows that \(\eta (\vec {x})\) transforms witnesses for (eventual) non-termination of \((\varphi ,\vec {u})\) into witnesses for \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\).

Corollary 5.8

If \(\vec {e}\) witnesses (eventual) non-termination of \((\varphi , \vec {u})\), then \(\widehat{\eta }(\vec {e})\) witnesses (eventual) non-termination of \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\).

Example 5.9

For the tuple \(\vec {e}= (5,2)\) from Example 5.3 and the automorphism \(\eta\) from Example 5.2 with \(\eta (x_1) = x_2\) and \(\eta (x_2) = x_1 - x_2^2\), we obtain

$$\begin{aligned} \widehat{\eta }(\vec {e}) =(\eta (x_1), \eta (x_2)) \, (\vec {e}) = (2, 5 - 2^2) = (2,1). \end{aligned}$$

As \(\vec {e}= (5,2)\) witnesses non-termination of the loop \(\textbf{L}_{ aut } = (\varphi ,\vec {u})\) in (11), \(\widehat{\eta }(\vec {e}) =(2,1)\) witnesses non-termination of \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\) due to Corollary 5.8.

Finally, Theorem 5.10 states that transforming loops preserves (non-)termination.

Theorem 5.10

(\({{\,\mathrm{ T\!r\mathrm }\,}}\) Preserves Termination) If \(\eta \in \textrm{Aut}_{\mathcal {S}}\left( \mathcal {S}[\vec {x}]\right)\), then \(\widehat{\eta }\) is a bijection between the respective sets of witnesses for (eventual) non-termination, i.e., for \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})=(\varphi ',\vec {u}\,')\) we have \(\vec {e}\in {(E)NT}_{(\varphi , \vec {u})}\) iff \(\widehat{\eta }(\vec {e}) \in {(E)NT}_{(\varphi ', \vec {u}\,')}\). Therefore, \((\varphi , \vec {u})\) terminates iff \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})=(\varphi ',\vec {u}\,')\) terminates.

Up to now, we only transformed a loop \((\varphi , \vec {u})\) on \(\mathcal {S}^d\) using elements of \(\textrm{Aut}_{\mathcal {S}}\left( \mathcal {S}[\vec {x}]\right)\). To see that this is a limitation, consider a linear-update loop where \(\vec {u}= A \cdot \vec {x}\) and A only has real eigenvalues. In Sect. 7.1 we will show that these loops can always be transformed into twn-form and a suitable automorphism \(\eta\) can be obtained by computing the Jordan normal form of A. This automorphism \(\eta\) is only an element of \(\textrm{Aut}_{\mathcal {S}}\left( \mathcal {S}[\vec {x}]\right)\) if the eigenvalues of A are from \(\mathcal {S}\). So if \(\mathcal {S}= \mathbb {Z}\), then this transformation is only applicable if all eigenvalues of A are integers.

However, we can also transform \((\varphi , \vec {u})\) into the loop \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\) on \(\mathbb {R}_{\mathbb {A}}^d\) using an automorphism \(\eta \in \textrm{Aut}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\). Nevertheless, our goal remains to prove termination on \(\mathcal {S}^d\) instead of \(\mathbb {R}_{\mathbb {A}}^d\), which is not equivalent in general. Thus, in Sect. 5.3 we will show how to analyze termination of loops on certain subsets F of \(\mathbb {R}_{\mathbb {A}}^d\). This allows us to analyze termination of \((\varphi , \vec {u})\) on \(\mathcal {S}^d\) by checking termination of \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\) on the subset \(\widehat{\eta }(\mathcal {S}^d) \subseteq \mathbb {R}_{\mathbb {A}}^d\) instead.

By our definition of loops over a ring \(\mathcal {S}\), we have \(\vec {u}(\vec {e}) \in \mathcal {S}^d\) for all \(\vec {e}\in \mathcal {S}^d\), i.e., \(\mathcal {S}^d\) is \(\vec {u}\)-invariant. This property is preserved by our transformation.

Definition 5.11

(\(\vec {u}\)-Invariance) Let \((\varphi ,\vec {u})\) be a loop on \(\mathcal {S}^d\) and let \(F \subseteq \mathcal {S}^d\). We call F \(\vec {u}\)-invariant or update-invariant if for all \(\vec {e}\in F\) we have \(\vec {u}(\vec {e}) \in F\).

Lemma 5.12

Let \((\varphi ,\vec {u})\) be a loop on \(\mathcal {S}^d\), \(F\subseteq \mathcal {S}^d\) be \(\vec {u}\)-invariant, \(\eta \in \textrm{Aut}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\), and let \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u}) = (\varphi ', \vec {u}\,')\). Then \(\widehat{\eta }(F)\) is \(\vec {u}\,'\)-invariant.

Our goal is to reduce termination to a \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S}, \mathbb {R}_{\mathbb {A}})\)-formula. Clearly, termination on F cannot be encoded with such a formula if F cannot be defined via \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S}, \mathbb {R}_{\mathbb {A}})\). Thus, we require that F is \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\)-definable, i.e., that there is a \(\psi \in {{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\) with free variables \(\vec {x}\) such that we have \(\vec {e}\in F\) iff \(\psi (\vec {e})\) is valid. Then we also say that \(\psi\) defines F. An example for a \({{\,\mathrm{Th_\exists }\,}}(\mathbb {Z},\mathbb {R}_{\mathbb {A}})\)-definable set is \(\left\{ (a, 0, a) \mid a \in \mathbb {Z}\right\}\), which is characterized by the formula \(\exists a \in \mathbb {Z}.\ x_1 = a \wedge x_2 = 0 \wedge x_3 = a\).

To analyze termination of \((\varphi ,\vec {u})\) on \(\mathcal {S}^d\), we can analyze termination of \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\) on \(\widehat{\eta } (\mathcal {S}^d)\subseteq \mathbb {R}_{\mathbb {A}}^d\) instead. The reason is that \(\vec {e}\in \mathcal {S}^d\) is a witness for (eventual) non-termination of \((\varphi ,\vec {u})\) iff \(\widehat{\eta }(\vec {e})\) is a witness for \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\) due to Corollary 5.8, i.e., \(\mathcal {S}^d\) contains a witness for (eventual) non-termination of \((\varphi ,\vec {u})\) iff \(\widehat{\eta }(\mathcal {S}^d)\) contains a witness for \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\). While \(\mathcal {S}^d\) is clearly \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\)-definable, Lemma 5.13 shows that \(\widehat{\eta } (\mathcal {S}^d)\) is \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\)-definable, too. More precisely, \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\)-definability is preserved by polynomial endomorphisms.

Lemma 5.13

Let \(\mathbb {Z}\le \mathcal {S}\le \mathbb {R}_{\mathbb {A}}\) and let \(\eta \in \textrm{End}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\). If \(F\subseteq \mathbb {R}_{\mathbb {A}}^d\) is \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\)-definable then so is \(\widehat{\eta }(F)\).

Example 5.14

The set \(\mathbb {Z}^2\) is \({{\,\mathrm{Th_\exists }\,}}(\mathbb {Z},\mathbb {R}_{\mathbb {A}})\)-definable as we have \((x_1,x_2)\in \mathbb {Z}^2\) iff

$$\begin{aligned} \exists a,b \in \mathbb {Z}. \; x_1=a \wedge x_2= b. \end{aligned}$$

Let \(\eta \in \textrm{End}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\) with \(\eta (x_1) = \frac{1}{2}\cdot x_1^2 + x_2^2\) and \(\eta (x_2) = x_2^2\).

Then \(\widehat{\eta }(\mathbb {Z}^2)\) is also \({{\,\mathrm{Th_\exists }\,}}(\mathbb {Z},\mathbb {R}_{\mathbb {A}})\)-definable because for \(x_1, x_2 \in \mathbb {R}_{\mathbb {A}}\) we have \((x_1,x_2) \in \eta (\mathbb {Z}^2)\) iff

$$\begin{aligned} \exists y_1,y_2 \in \mathbb {R}_{\mathbb {A}}, \; a,b \in \mathbb {Z}. \;\; y_1=a \wedge y_2 = b \; \wedge \; x_1 = \tfrac{1}{2}\cdot y_1^2 + y_2^2 \wedge x_2 = y_2^2. \end{aligned}$$

Theorem 5.15 shows that instead of regarding solvable loops [56], w.l.o.g. we can restrict ourselves to twn-loops. The reason is that every solvable loop with real eigenvalues can be transformed into a twn-loop by a linear automorphism \(\eta\), i.e., the degree \(\deg (\eta )\) of \(\eta\) is 1, where \(\deg (\eta )=\max \{ \deg (\eta (x_i)) \mid 1 \le i \le d \}\).

Theorem 5.15

(twn-Transformability of Solvable Loops) Let \((\varphi ,\vec {u})\) be a solvable loop with real eigenvalues. Then one can compute a linear automorphism \(\eta \in \textrm{Aut}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\) such that \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\) is twn.

We recapitulate our most important results on \({{\,\mathrm{ T\!r\mathrm }\,}}\) in the following corollary. Here, we generalize the result of Theorem 5.10 to the setting where we consider termination on some update-invariant and \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\)-definable set.

Corollary 5.16

(Properties of \({{\,\mathrm{ T\!r\mathrm }\,}}\)) Let \((\varphi , \vec {u})\) be a loop, \(\eta \in \textrm{Aut}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\), \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u}) = (\varphi ',\vec {u}\,')\), and \(F\subseteq \mathcal {S}^d\) be \(\vec {u}\)-invariant and \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\)-definable.

  1. (a)

    \(\widehat{\eta } (F)\subseteq \mathbb {R}_{\mathbb {A}}^d\) is \(\vec {u}\,'\)-invariant and \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\)-definable.

  2. (b)

    \((\varphi , \vec {u})\) terminates on F iff \((\varphi ',\vec {u}\,')\) terminates on \(\widehat{\eta } (F)\).

  3. (c)

    \(\vec {e}\in F\) witnesses (eventual) non-termination of \((\varphi , \vec {u}) \;\) iff \(\; \widehat{\eta }(\vec {e}) \in \widehat{\eta }(F)\) witnesses (eventual) non-termination of \((\varphi ',\vec {u}\,')\).

5.2 Finding automorphisms to transform loops into twn-form

The goal of \({{\,\mathrm{ T\!r\mathrm }\,}}_\eta\) from Sect. 5.1 is to transform \((\varphi ,\vec {u})\) into twn-form such that our techniques from Sect. 4 can be used to decide termination. So the two remaining challenges are to find a suitable automorphism \(\eta \in \textrm{Aut}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\) such that \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\) is twn, and to adapt our techniques from Sect. 4 such that they can be applied to twn-loops where one only wants to show termination on an update-invariant \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\)-definable subset. We discuss the latter challenge in Sect. 5.3. In this section, we present two techniques to check the existence of automorphisms for the transformation into twn-form constructively, i.e., these techniques can also be used to compute such automorphisms.

The search for suitable automorphisms is closely related to the question if a polynomial automorphism can be conjugated into a “de Jonquiéres”-automorphism, a difficult question from algebraic geometry [18]. So future advances in this field may help to improve the results of the current section.

The first technique (Theorem 5.19) reduces the search for a suitable automorphism of bounded degree to \({{\,\mathrm{Th_\exists }\,}}(\mathbb {R}_{\mathbb {A}})\). For any automorphism, the degree of its inverse is bounded in terms of the length d of \(\vec {x}\).

Theorem 5.17

(Degree of Inverse [18, Corollary 2.3.4]) Let \(\eta \in \textrm{Aut}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\). Then we have \(\deg (\eta ^{-1}) \le (\deg (\eta ))^{d-1}\).

By Theorem 5.17, checking if an endomorphism is an automorphism can be reduced to \({{\,\mathrm{Th_\exists }\,}}(\mathbb {R}_{\mathbb {A}})\). To do so, one encodes the existence of coefficients for the polynomials \(\eta ^{-1}(x_1), \ldots , \eta ^{-1}(x_d)\), which all have at most degree \(\left( \deg (\eta )\right) ^{d-1}\).

Lemma 5.18

Let \(\eta \in \textrm{End}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\). Then the question if \(\eta \in \textrm{Aut}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\) holds is reducible to \({{\,\mathrm{Th_\exists }\,}}(\mathbb {R}_{\mathbb {A}})\).

Based on Lemma 5.18, we now present our first technique to find an automorphism \(\eta\) that transforms a loop into twn-form.

Theorem 5.19

(\({{\,\mathrm{ T\!r\mathrm }\,}}\) With Automorphisms of Bounded Degree) For any \(\delta \ge 0\), the question whether there exists an \(\eta \in \textrm{Aut}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\) with \(\deg (\eta ) \le \delta\) such that \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\) is twn is reducible to \({{\,\mathrm{Th_\exists }\,}}(\mathbb {R}_{\mathbb {A}})\).

So if the degree of \(\eta\) is bounded a priori, it is decidable whether there exists an \(\eta \in \textrm{Aut}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\) such that \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\) is twn since \({{\,\mathrm{Th_\exists }\,}}(\mathbb {R}_{\mathbb {A}})\) is decidable.

We call a loop twn -transformable if there is an \(\eta \in \textrm{Aut}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\) such that \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\) is twn. By Theorem 5.19, twn-transformability is semi-decidable since one can increment \(\delta\) until a suitable automorphism is found. So in other words, any loop which is transformable to a twn-loop can be transformed via Theorem 5.19.

We call our transformation \({{\,\mathrm{ T\!r\mathrm }\,}}\) complete for a class of loops if every loop from this class is twn-transformable. For such classes, a suitable automorphism is computable by Theorem 5.19. Together with Theorem 5.15, we get Corollary 5.20.

Corollary 5.20

\({{\,\mathrm{ T\!r\mathrm }\,}}\) is complete for solvable loops with real eigenvalues.

Note that for solvable loops \((\varphi ,\vec {u})\), instead of computing \(\eta\) using Theorem 5.19, the proof of Theorem 5.15 yields a more efficient way to compute a linear automorphism \(\eta\) such that \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\) is twn. For this, one computes the Jordan normal form of each \(A_i\) (see Definition 2.3), which is possible in polynomial time (see, e.g., [23, 54]).

Our second technique to find suitable automorphisms for our transformation is restricted to linear automorphisms. In this case, it is decidable whether a loop can be transformed into a twn-loop \((\varphi ',\vec {u}')\) where the monomial for \(x_i\) has the coefficient 1 in each \(u'_i\). The decision procedure checks if a certain Jacobian matrix is strongly nilpotent, i.e., it is not based on a reduction to \({{\,\mathrm{Th_\exists }\,}}(\mathbb {R}_{\mathbb {A}})\).

Definition 5.21

(Strong Nilpotence) Let \(J \in \left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right) ^{d \times d}\) be a matrix of polynomials. For all \(1 \le i \le d\), let \(\vec {y}^{(i)}\) be a vector of fresh variables. J is strongly nilpotent if \(\prod \nolimits _{i=1}^d \; J[\vec {x}/\vec {y}^{(i)}]=0^{d\times d}\), where \(0^{d\times d}\) is the zero matrix.

Our second technique is formulated in the following theorem which follows from an existing result in linear algebra [17, Thm. 1.6.].

Theorem 5.22

(\({{\,\mathrm{ T\!r\mathrm }\,}}\) With Linear Automorphisms [17]) Let \((\varphi ,\vec {u})\) be a loop. The Jacobian matrix \(\left( \frac{\partial (u_i - x_i)}{\partial x_j}\right) _{1 \le i,j \le d} \in \left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right) ^{d \times d}\) is strongly nilpotent iff there exists a linear automorphism \(\eta \in \textrm{Aut}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\) with

$$\begin{aligned} {{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u}) = (\varphi ',(x_1 + p_1,\ldots ,x_d + p_d)) \end{aligned}$$
(12)

and \(p_i \in \mathbb {R}_{\mathbb {A}}[x_{i+1},\ldots ,x_{d}]\) for all \(1 \le i \le d\).

Thus, \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,\vec {u})\) is twn.

As strong nilpotence of the Jacobian matrix is clearly decidable, Theorem 5.22 gives rise to a decision procedure for the existence of a linear automorphism that transforms \((\varphi , \vec {u})\) to the form (12).

Fig. 3
figure 3

Loop \(\textbf{L}_{ non-twn }\)

Example 5.23

The loop \(\textbf{L}_{ non-twn }\) on \(\mathcal {S}^3\) in Fig. 3 shows how our results enlarge the class of loops where termination is reducible to \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S}, \mathbb {R}_{\mathbb {A}})\). This loop is clearly not in twn-form. To transform it, we use Theorem 5.22. The Jacobian matrix J of \((u_1 - x_1, u_2 - x_2, u_3 - x_3)\) is:

$$\begin{aligned} \left[ {\begin{matrix} 8\cdot x_2^2 &{} 16\cdot x_1 \cdot x_2 +48\cdot x_2^2 + 32 \cdot x_2 \cdot x_3 &{}16\cdot x_2^2 \\ -2 \cdot x_1 - 4 \cdot x_2 - 4 \cdot x_3 &{} -4 \cdot x_1 - 8 \cdot x_2 - 8 \cdot x_3 &{} -4 \cdot x_1 - 8 \cdot x_2 - 8 \cdot x_3 \\ -4 \cdot x_2^2+2 \cdot x_1 + 4 \cdot x_2 +4 \cdot x_3 \, &{} \, -8 \cdot x_1 \cdot x_2 -24 \cdot x_2^2-16 \cdot x_2 \cdot x_3 +4 \cdot x_1 +8 \cdot x_2 +8 \cdot x_3 \, &{}\, -8 \cdot x_2^2+4 \cdot x_1 +8 \cdot x_2 +8 \cdot x_3 \end{matrix}}\right] \end{aligned}$$

One easily checks that J is strongly nilpotent. Thus, by Theorem 5.22 the loop can be transformed into twn-form by a linear automorphism. Indeed, consider the linear automorphism \(\eta \!\in \!\textrm{Aut}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\) induced by the matrix \(M\!=\!\left[ {\begin{matrix} 1 &{} 1 &{} 1 \\ 0 &{} 2 &{} 0 \\ 1 &{} 2 &{} 2 \end{matrix}} \right]\), i.e.,

$$\begin{aligned} \begin{array}{lllll} \eta : &{} x_1 \mapsto x_1 + x_2 + x_3, &{} x_2 \mapsto 2 \cdot x_2, &{} x_3 \mapsto x_1 + 2 \cdot x_2 + 2 \cdot x_3 &{} \text {and} \\ \eta ^{-1}: &{} x_1 \mapsto 2 \cdot x_1 - x_3, &{} x_2 \mapsto \tfrac{1}{2}\cdot x_2, &{} x_3 \mapsto -x_1-\tfrac{1}{2}\cdot x_2 + x_3. &{} \end{array} \end{aligned}$$

If we transform \(\textbf{L}_{ non-twn }\) with \(\eta\), we obtain the twn-loop \(\textbf{L}_{ ex }\) in Fig. 2. If \(\mathcal {S}= \mathbb {R}_{\mathbb {A}}\), then \(\textbf{L}_{ ex }\) terminates on \(\mathbb {R}_{\mathbb {A}}^3\) iff \(\textbf{L}_{ non-twn }\) terminates on \(\mathbb {R}_{\mathbb {A}}^3\) by Theorem 5.10. Thus, as seen in Example 4.8, \(\textbf{L}_{ non-twn }\) does not terminate on \(\mathbb {R}_{\mathbb {A}}^3\). Now assume \(\mathcal {S}= \mathbb {Z}\), i.e., we analyze termination of \(\textbf{L}_{ non-twn }\) on \(\mathbb {Z}^3\) instead of \(\mathbb {R}_{\mathbb {A}}^3\). Note that \(\widehat{\eta }\) maps \(\mathbb {Z}^3\) to the set of all \(\mathbb {Z}\)-linear combinations of columns of M, i.e.,

$$\begin{aligned} \widehat{\eta }(\mathbb {Z}^3)=\left\{ a \cdot (1,0,1) + b \cdot (1,2,2) + c \cdot (1,0,2) \mid a,b,c \in \mathbb {Z}\right\} . \end{aligned}$$

By Corollary 5.16, \(\textbf{L}_{ ex }\) terminates on \(\widehat{\eta }(\mathbb {Z}^3)\) iff \(\textbf{L}_{ non-twn }\) terminates on \(\mathbb {Z}^3\). Moreover, \(\widehat{\eta }(\mathbb {Z}^3)\) is \({{\,\mathrm{Th_\exists }\,}}(\mathbb {Z},\mathbb {R}_{\mathbb {A}})\)-definable: We have \((x_1,x_2,x_3) \in \widehat{\eta }(\mathbb {Z}^3)\) iff

$$\begin{aligned} \exists a,b,c\in \mathbb {Z}.~ x_1= a \cdot 1 + b \cdot 1 + c \cdot 1 \wedge x_2 = b \cdot 2 \wedge x_3 = a\cdot 1 + b \cdot 2 + c \cdot 2. \end{aligned}$$
(13)

We will discuss how to analyze termination of tnn-loops like \(\textbf{L}_{ ex }\) on update-invariant and \({{\,\mathrm{Th_\exists }\,}}(\mathbb {Z},\mathbb {R}_{\mathbb {A}})\)-definable sets like \(\widehat{\eta }(\mathbb {Z}^3)\) in Sect. 5.3.

To summarize, if a loop is twn-transformable, then we can always find a suitable automorphism via Theorem 5.19. So whenever Theorem 5.22 is applicable, a suitable linear automorphism can also be obtained by Theorem 5.19. Hence, our first technique from Theorem 5.19 subsumes our second one from Theorem 5.22. However, while Theorem 5.19 is always applicable, Theorem 5.22 is easier to apply. The reason is that for Theorem 5.19 one has to check validity of a possibly non-linear formula over the reals, where the degree of the occurring polynomials depends on \(\delta\) and the update \(\vec {u}\) of the loop, and the number of variables can be exponential in d, see Theorem 5.17 and Lemma 5.18. So even when searching for a linear automorphism, one may obtain a non-linear formula if the loop is non-linear. In contrast, Theorem 5.22 only requires linear algebra. So it is preferable to first check whether the loop can be transformed into a twn-loop \((\varphi ',(x_1 + p_1, \ldots , x_d + p_d))\) with \(x_i \notin \mathcal {V}(p_i)\) via a linear automorphism. This check is decidable by Theorem 5.22.

Note that the proof of Theorem 5.19 is constructive. Moreover, the proof of [17, Thm. 1.6.] which implies Theorem 5.22 is also constructive: the idea is to use basic results from linear algebra to compute an invertible matrix \(T \in \mathbb {R}_{\mathbb {A}}^{d \times d}\) such that \(T\cdot J \cdot T^{-1}\) is triangular where J is the Jacobian matrix \(\left( \frac{\partial (u_i - x_i)}{\partial x_j}\right) _{1 \le i,j \le d}\). Then \(\eta\) with \(\eta (\vec {x}) = T \cdot \vec {x}\) transforms the loop into the form (12). Hence, Theorem 5.22 is also constructive. Thus, we can not only check the existence of a suitable automorphism, but we can also compute it whenever it exists.

5.3 Analyzing twn-transformable loops

In this section, we generalize our results from Sect. 4 to twn-transformable loops. Our transformation from Sect. 5.1 and 5.2 transforms twn-transformable loops over update-invariant and \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\)-definable sets into twn-loops over update-invariant and \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\)-definable sets. Thus, in this section we fix a twn-loop \((\varphi ,\vec {u})\) and such a set \(F \subseteq \mathbb {R}_{\mathbb {A}}^d\). Let \(\psi _F \in {{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\) define F.

While in Sect. 4 we were concerned with the termination of loops on a set \(\mathcal {S}^d\) for a ring \(\mathcal {S}\), we now show that termination of \((\varphi ,\vec {u})\) on F can also be reduced to an existential formula (from \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\)). Here, we indeed rely on the update-invariance of F as otherwise eventual non-termination and non-termination of \((\varphi ,\vec {u})\) on F would not be equivalent. In Sect. 4, this equivalence is crucial since we reduce non-termination via eventual non-termination to \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\).

Again, let \(\vec {q}_{ norm }\) be the normalized closed form of \(\vec {u}\). Similar to (4), \((\varphi ,\vec {u})\) is eventually non-terminating on F iff

$$\begin{aligned} \exists \vec {x} \in F, \, n_0 \in \mathbb {N}.\ \forall n \in \mathbb {N}_{>n_0}.\ \varphi (\vec {q}_{ norm }). \end{aligned}$$
(14)

In Theorem 4.9, we have seen that given a propositional formula \(\xi\) over the atoms \(\{p \triangleright 0\mid p \in \mathbb {NPE}_{\mathcal {S}}[\vec {x}], \triangleright \in \{\ge , >\}\}\), one can reduce validity of \(\exists \vec {x} \in \mathcal {S}^d, n_0 \in \mathbb {N}.\ \forall n \in \mathbb {N}_{>n_0}.\ \xi\) to validity of \(\exists \vec {x} \in \mathcal {S}^d.{{\,\textrm{red}\,}}(\xi ) \in {{\,\mathrm{Th_\exists }\,}}(\mathcal {S})\) and the resulting formula can be computed in polynomial time from \(\xi\). Thus, by using the formula \(\exists \vec {x} \in \mathbb {R}_{\mathbb {A}}^d.\ \psi _F \wedge {{\,\textrm{red}\,}}(\xi ) \in {{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\) instead, we obtain Corollary 5.24.

Corollary 5.24

(Reducing Eventual Non-Termination on a Set) For a propositional formula \(\xi\) over \(\{p \triangleright 0\mid p \in \mathbb {NPE}_{\mathbb {R}_{\mathbb {A}}}[\vec {x}], \triangleright \in \{\ge , >\}\}\), validity of

$$\begin{aligned} \exists \vec {x} \in F, n_0 \in \mathbb {N}.\ \forall n \in \mathbb {N}_{>n_0}.\ \xi \end{aligned}$$

can be reduced to validity of a closed formula in \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S}, \mathbb {R}_{\mathbb {A}})\) in polynomial time.

By combining (14) and Corollary 5.24, one obtains the following refined version of Theorem 4.10.

Corollary 5.25

(Reducing Termination on Sets) Termination of tnn- or twn-loops, respectively, on update-invariant \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S}, \mathbb {R}_{\mathbb {A}})\)-definable sets is reducible to \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S}, \mathbb {R}_{\mathbb {A}})\).

Example 5.26

Reconsider Example 5.23, where we have seen that \(\textbf{L}_{ non-twn }\) (see Fig. 3) terminates on \(\mathbb {Z}^3\) iff \(\textbf{L}_{ ex }\) (see Fig. 2) terminates on the update-invariant and \({{\,\mathrm{Th_\exists }\,}}(\mathbb {Z},\mathbb {R})\)-definable set \(\widehat{\eta }(\mathbb {Z}^3) = F\) defined by the formula (13). In Example 4.8, we showed that \((-4,2,1)\) witnesses eventual non-termination of \(\textbf{L}_{ ex }\). As \(\widehat{\eta }(-9,1,4) = (-9+1+4, 1 \cdot 2, -9+1\cdot 2 + 4 \cdot 2) = (-4,2,1)\), we have \((-4,2,1) \in F\). Furthermore, \((-9,1,4)\) witnesses eventual non-termination of \(\textbf{L}_{ non-twn }\) on \(\mathbb {Z}^3\) by Corollary 5.16 (c). Hence, \(\textbf{L}_{ non-twn }\) does not terminate on \(\mathbb {Z}^3\).

In addition, we get the following refined version of Corollary 4.11.

Corollary 5.27

((Semi-)Deciding (Non-)Termination on a Set) Let \((\varphi ,\vec {u})\) be a twn-loop and let \(F \subseteq \mathbb {R}_{\mathbb {A}}^d\) be update-invariant and \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\)-definable.

  1. (a)

    The loop \((\varphi ,\vec {u})\) terminates over \(\mathbb {R}_{\mathbb {A}}\) iff it terminates over \(\mathbb {R}\).

  2. (b)

    Termination of \((\varphi ,\vec {u})\) on F is decidable if \(\mathcal {S}= \mathbb {R}_{\mathbb {A}}\) or \(\mathcal {S}= \mathbb {R}\).

  3. (c)

    Non-termination of \((\varphi ,\vec {u})\) on F is semi-decidable if \(\mathcal {S}= \mathbb {Z}\) or \(\mathcal {S}= \mathbb {Q}\).

Of course, Lemma 4.12 also holds in this setting.

Corollary 5.28

Let \(\xi = \varphi (\vec {q}_{ norm })\). Then \(\vec {e}\in \mathbb {R}_{\mathbb {A}}^d\) witnesses eventual non-termination of \((\varphi ,\vec {u})\) on F iff \(\psi _F(\vec {e}) \wedge {{\,\textrm{red}\,}}(\xi ) (\vec {e})\) holds.

Finally, Algorithm 2 generalizes Algorithm 1 to twn-transformable loops.

Algorithm 2
figure b

Checking Termination on Sets

6 Linearization of tnn-loops

In [48], a technique was proposed to linearize polynomial loops. As mentioned, by combining the linearization with existing decidability results [64, 65], one can conclude decidability of termination for conjunctive twn-loops over \(\mathbb {R}\) (whereas Corollary 4.11 (b) extends this result also to non-conjunctive loops).

In this section, we adapt the linearization technique of [48] to our setting and formalize it. This allows us to obtain novel results on the complexity of linearization which we use to analyze the complexity of deciding termination for arbitrary twn-loops in Sect. 7.2. We start with the definition of linearization.

Definition 6.1

(Linearization) Let \(\vec {u}\in (\mathcal {S}[\vec {x}])^d\) and \(\vec {y}\) be a vector of \(d'\) fresh variables with \(d' \ge d\). Let \(\vec {u}\,' \in (\mathcal {S}[\vec {y}])^{d'}\) be linear and \(\vec {w} \in (\mathcal {S}[\vec {x}])^{d'}\) with \(w_i = x_i\) for all \(1 \le i \le d\). Then \(\vec {u}\,'\) is a linearization of \(\vec {u}\) via \(\vec {w}\) if \(\vec {w}(\vec {u}(\vec {e})) = \vec {u}\,'(\vec {w}(\vec {e}))\) holds for all \(\vec {e}\in \mathcal {S}^d\), where \(\vec {u}\,'(\vec {w}(\vec {e}))\) stands for \(\vec {u}\,'[\vec {y}/\vec {w}(\vec {e})]\). Instead of \(y_i\) we often write \(y_{w_i}\) for all \(1 \le i \le d'\).

So \(y_1,\ldots ,y_d\) (i.e., \(y_{w_1}, \ldots , y_{w_d}\)) correspond to the variables \(x_1, \ldots , x_d\), whereas \(y_{d+1},\ldots ,y_{d'}\) are used to mimic the non-linear part of \(\vec {u}\) in a linear way in \(\vec {u}\,'\). This non-linear behavior is captured by the polynomials \(w_{d+1},\ldots ,w_{d'}\).

Example 6.2

Let \(\vec {u}=(x_2^2,x_3^2,x_3) \in (\mathbb {Z}[x_1,x_2,x_3])^3\). Then \(\vec {u}\,' = (y_{x_2^2}, y_{x_3^2}, y_{x_3}, y_{x_3^4}, y_{x_3^2}, y_{x_3^4})\) over the variables \((y_{x_1},y_{x_2},y_{x_3},y_{x_2^2}, y_{x_3^2}, y_{x_3^4})\) is a linearization of \(\vec {u}\) via \(\vec {w} = (x_1, x_2, x_3, x_2^2,x_3^2,x_3^4)\), since for all \(\vec {e}= (e_1,e_2,e_3) \in \mathbb {Z}^3\) we have:

$$\begin{aligned} \vec {w}(\vec {u}(\vec {e})) = (e_2^2,e_3^2,e_3,e_3^4,e_3^2,e_3^4) = \vec {u}\,'(e_1,e_2,e_3,e_2^2,e_3^2,e_3^4) = \vec {u}\,'(\vec {w}(\vec {e})). \end{aligned}$$

Here, the non-linear part of \(\vec {u}\) is mimicked by the variables \(y_{x_2^2}\), \(y_{x_3^2}\), and \(y_{x_3^4}\).

The linearization of Definition 6.1 also works when applying the update repeatedly.

Corollary 6.3

(Iterated Update of Linearization) Let \(\vec {u}\in (\mathcal {S}[\vec {x}])^d\) and \(\vec {u}\,' \in (\mathcal {S}[\vec {y}])^{d'}\) be its linearization via \(\vec {w} \in (\mathcal {S}[\vec {x}])^{d'}\).

Then for all \(\vec {e}\in \mathcal {S}^d\) and all \(n \in \mathbb {N}\) we have \(\vec {w}(\vec {u}^n(\vec {e})) = (\vec {u}\,')^n(\vec {w}(\vec {e}))\).

We now define the linearization of a loop to be a linearization of its update where the loop guard is extended to ensure that the fresh variables \(y_{w_{d+1}}, \ldots , y_{w_{d'}}\) indeed correspond to \(w_{d+1}, \ldots , w_{d'}\).

Definition 6.4

(Linearization of a Loop) Let \((\varphi ,\vec {u})\) be a loop on \(\mathcal {S}^d\) using the variables \(\vec {x}\). A loop \((\varphi ',\vec {u}\,')\) on \(\mathcal {S}^{d'}\) using the variables \(\vec {y}\) is a linearization of \((\varphi ,\vec {u})\) via \(\vec {w}\in (\mathcal {S}[\vec {x}])^{d'}\) if both

  1. (a)

    \(\vec {u}\,'\) is a linearization of \(\vec {u}\) via \(\vec {w}\)

  2. (b)

    \(\varphi ' = \varphi [x_1/y_{x_1}, \ldots , x_d/y_{x_d}] \wedge \bigwedge _{i = d+1}^{d'} \left( y_{w_i} - w_i[x_1/y_{x_1}, \ldots , x_d/y_{x_d}] = 0\right)\).

Example 6.5

Consider the loop \((\varphi ,\vec {u})\) on \(\mathbb {Z}^3\) where \(\varphi\) is \(x_2 > x_3\) and \(\vec {u}=(x_2^2,x_3^2,x_3)\). Then the linearization of \((\varphi ,\vec {u})\) via \(\vec {w}\) is \((\varphi ',\vec {u}\,')\) where \(\vec {u}\,'\) is as in Example 6.2 and \(\varphi '\) is

$$\begin{aligned}&\varphi [x_1/y_{x_1}, x_2/y_{x_2}, x_3/y_{x_3}]&\wedge \; y_{x_2^2} - y_{x_2}^2 = 0 \wedge y_{x_3^2} - y_{x_3}^2 = 0 \wedge y_{x_3^4} - y_{x_3}^4 = 0 \\ {} = {}&\hspace{1cm} y_{x_2} > y_{x_3}&\wedge \; y_{x_2^2} - y_{x_2}^2 = 0 \wedge y_{x_3^2} - y_{x_3}^2 = 0 \wedge y_{x_3^4} - y_{x_3}^4 = 0 \end{aligned}$$

To illustrate the correspondence between \((\varphi ,\vec {u})\) and \((\varphi ',\vec {u}\,')\), consider the initial value \(\vec {e} = (1,3,2)\). Here, the original loop yields the trace \((\vec {e}, \, \vec {u}(\vec {e}), \, \vec {u}^2(\vec {e}), [2] \ldots ) = ((1,3,2), \, (9,4,2), \, (16,4,2), \,\ldots )\). The linearized loop operates over the variables \((y_{x_1},y_{x_2},y_{x_3},y_{x_2^2}, y_{x_3^2}, y_{x_3^4})\). Thus, the first three variables correspond to \(x_1, x_2, x_3\) and the latter ones correspond to \(x_2^2, x_3^2, x_3^4\). So the corresponding initial value is \(\vec {e}\,' = (1,3,2,9,4,16)\) and the resulting trace is \((\vec {e}\,', \, \vec {u}\,'(\vec {e}\,'),[2] (\vec {u}\,')^2(\vec {e}), \ldots ) = ((1,3,2,9,4,16), \; (9,4,2,16,4,16), \; (16,4,2,16,4,16), \;\ldots )\).

Lemma 6.6 shows that linearization preserves the termination behavior.

Lemma 6.6

Let \((\varphi ',\vec {u}\,')\) on \(\mathcal {S}^{d'}\) be a linearization of \((\varphi ,\vec {u})\) on \(\mathcal {S}^d\) via \(\vec {w}\).

  1. (a)

    \((\varphi ',\vec {u}\,')\) terminates on \(\vec {e}\,' \in \mathcal {S}^{d'}\) if there is no \(\vec {e}\in \mathcal {S}^d\) such that \(\vec {e}\,'= \vec {w}(\vec {e})\).

  2. (b)

    The loop \((\varphi ,\vec {u})\) terminates on \(\vec {e}\in \mathcal {S}^d\) iff \((\varphi ',\vec {u}\,')\) terminates on \(\vec {w}(\vec {e})\).

While Lemma 6.6 proves the soundness of linearization, we now show how to find \(\vec {u}\,'\) and \(\vec {w}\) automatically, where it suffices to only use monomials (instead of arbitrary polynomials) in \(\vec {w}\). A monomial over \(\vec {x}\) has the form \(x_1^{z_1} \cdot \ldots \cdot x_d^{z_d}\) with \(z_i \in \mathbb {N}\) for all \(1 \le i \le d\). Let \(\vec {x}^{\vec {z}}\) with \(\vec {z} = (z_1,\ldots ,z_d)\) abbreviate \(x_1^{z_1} \cdot \ldots \cdot x_d^{z_d}\).

The original update \(\vec {u}\) consists of polynomials \(u_i\) to update the variable \(x_i\), for all \(1 \le i \le d\). The linearized update \(\vec {u}\,'\) consists of polynomials \(u_{m}'\) to update the variables \(y_m\) for all monomials m in \(\vec {w}\). Here, for any monomial \(m = x_1^{z_1} \cdot \ldots \cdot x_d^{z_d}\), the polynomial \(u'_m\) results from replacing each monomial p in \(u_1^{z_1} \cdot \ldots \cdot u_d^{z_d}\) (i.e., the monomial p in each addend \(c \cdot p\) of the polynomial \(u_1^{z_1} \cdot \ldots \cdot u_d^{z_d}\)) by the variable \(y_{p}\). More precisely, if \(u_1^{z_1} \cdot \ldots \cdot u_d^{z_d}\) has the form \(c_1 \cdot p_1 +\ldots + c_k \cdot p_k\) for monomials \(p_1,\ldots ,p_k\) and numbers \(c_1,\ldots ,c_k \in \mathcal {S}\), then \(u_m' = c_1 \cdot y_{p_1} +\ldots + c_k \cdot y_{p_k}\).

Algorithm 3 summarizes the linearization procedure. The vector \(\vec {v}\) always contains those monomials p for which we still have to define the linearized update \(u'_p\). So initially, \(\vec {v}\) consists of the original variables, i.e., \(\vec {v} = \vec {x}\). Whenever a new variable \(y_p\) is introduced in the linearized update, p is inserted into \(\vec {v}\) at the end.

Algorithm 3
figure c

Linearizing tnn-Loops

Example 6.7

We apply Algorithm 3 to linearize the loop \((\varphi ,\vec {u})\) from Example 6.5 where \(\varphi\) is \(x_2 > x_3\) and \(\vec {u}= (x_2^2, x_3^2, x_3)\). In the beginning, we have \(\vec {v}=(x_1, x_2, x_3)\). We start with \(x_1\) and remove it from \(\vec {v}\). In \(u_1 = x_2^2\) we have to replace the monomial \(x_2^2\) by the fresh variable \(y_{x_2^2}\) when constructing \(u'_{x_1}\). Hence, \(u'_{x_1} = y_{x_2^2}\) and we obtain \(\vec {v} = (x_2, x_3, x_2^2)\).

Next, we consider \(x_2\), where \(u_2 =x_3^2\). Thus, we obtain \(u'_{x_2} = y_{x_3^2}\) and \(\vec {v} = (x_3, x_2^2, x_3^2)\). Then we take \(x_3\), where \(u_3 = x_3\). Hence, \(u'_{x_3} = y_{x_3}\), but we do not insert \(x_3\) into \(\vec {v}\) again, since we just computed \(u'_{x_3}\). So we have \(\vec {v} = (x_2^2, x_3^2)\).

We now handle \(x_2^2\). For the linearized update, we take \(u_2^2 = x_3^4\) but replace the monomial \(x_3^4\) by a fresh variable \(y_{x_3^4}\). Hence, \(u'_{x_2^2} = y_{x_3^4}\) and \(\vec {v} = ( x_3^2, x_3^4)\).

Next, we take the monomial \(x_3^2\) and in \(u_3^2 = x_3^2\) we have to replace the monomial by \(y_{x_3^2}\). This leads to the linearized update \(u'_{x_3^2} = y_{x_3^2}\), but we do not insert \(x_3^2\) into \(\vec {v}\) again, since we just computed \(u'_{x_3^2}\). Hence, we have \(\vec {v} = ( x_3^4)\).

Finally, we treat \(x_3^4\) and in \(u_3^4 = x_3^4\) we replace the monomial by \(y_{x_3^4}\), i.e., \(u'_{x_3^4} = y_{x_3^4}\).

Now \(\vec {v}=(\,)\). Hence, Algorithm 3 terminates and returns the loop with the guard \(\varphi '\) from Example 6.5 and the (linear) update \((y_{x_2^2}, y_{x_3^2}, y_{x_3}, y_{x_3^4}, y_{x_3^2}, y_{x_3^4})\) over the variables \((y_{x_1}, y_{x_2}, y_{x_3}, y_{x_2^2}, y_{x_3^2}, y_{x_3^4})\), as in Example 6.2.

Now we infer an upper bound on Algorithm 3’s complexity. To this end, we will show that the degrees of the monomials in \(\vec {w}\) which are used for the linearization can be bounded by the maximal dependency degree of the loop’s update \(\vec {u}\). For \(1 \le i \le d\), the dependency degree \({{\,\textrm{depdeg}\,}}_{\vec {u}}(x_i)\) is the degree of \(u_i\), but this degree is expressed in terms of those variables that are minimal w.r.t. \(\succ _{\vec {u}}\). Recall that \(u_i\) has the form \(c_i \cdot x_i + p_i\) where \(p_i\) is a polynomial which only contains variables that are smaller than \(x_i\) w.r.t. \(\succ _{\vec {u}}\). W.l.o.g. we may assume that \(x_i \succ _{\vec {u}} x_j\) implies \(i > j\) for all \(1 \le i,j \le d\). Then for every monomial \(x_{i+1}^{z_{i+1}} \cdot \ldots \cdot x_d^{z_d}\) in \(p_i\), the corresponding dependency degree is \(z_{i+1} \cdot {{\,\textrm{depdeg}\,}}_{\vec {u}}(x_{i+1}) + \ldots + z_{d} \cdot {{\,\textrm{depdeg}\,}}_{\vec {u}}(x_{d})\). The dependency degree of \(p_i\) is the maximal dependency degree of its monomials.

Definition 6.8

(Dependency Degree) Let \((\varphi ,\vec {u})\) be a twn-loop with \(u_i = c_i \cdot x_i + p_i\) for all \(1 \le i \le d\), where \(p_i\) only contains variables that are smaller than \(x_i\) w.r.t. \(\succ _{\vec {u}}\). We define the dependency degree w.r.t. \(\vec {u}\) as follows:

  • \({{\,\textrm{depdeg}\,}}_{\vec {u}}(x_i) = \max \{ 1, {{\,\textrm{depdeg}\,}}_{\vec {u}}(p_i) \}\) for all \(1 \le i \le d\).

  • \({{\,\textrm{depdeg}\,}}_{\vec {u}}(p) = \max \{ {{\,\textrm{depdeg}\,}}_{\vec {u}}(m) \mid m \text { is a monomial in } p\}\) for every non-zero \(p \in \mathcal {S}[\vec {x}]\) and \({{\,\textrm{depdeg}\,}}_{\vec {u}}(0) = -\infty\).

  • \({{\,\textrm{depdeg}\,}}_{\vec {u}}(x_{1}^{z_{1}} \cdot \ldots \cdot x_d^{z_d}) = \sum \nolimits _{i=1}^d z_i \cdot {{\,\textrm{depdeg}\,}}_{\vec {u}}(x_i)\) for all \(z_1,\ldots ,z_d \in \mathbb {N}\).

Since \(\succ _{\vec {u}}\) is well founded by the triangularity of the loop, \({{\,\textrm{depdeg}\,}}_{\vec {u}}\) is well defined: for the variables \(x_i\) which are minimal w.r.t. \(\succ _{\vec {u}}\), \(p_i\) is a constant and thus, \({{\,\textrm{depdeg}\,}}_{\vec {u}}(x_i) = 1\). For other variables \(x_i\) with \(p_i \ne 0\), we can compute \({{\,\textrm{depdeg}\,}}_{\vec {u}}(p_i)\) because \({{\,\textrm{depdeg}\,}}_{\vec {u}}(x_j)\) is already known for all variables \(x_j\) occurring in \(p_i\). Lemma 6.9 states three easy observations on \({{\,\textrm{depdeg}\,}}\). Here, \(\deg\) denotes the degree of monomials or polynomials, i.e., \(\deg (x_1^{z_1} \cdot \ldots \cdot x_d^{z_d}) = z_1 + \ldots + z_d\).

Lemma 6.9

Let \((\varphi ,\vec {u})\) be a twn-loop.

  1. (a)

    For every monomial m over \(\vec {x}\), we have \(\deg (m) \le {{\,\textrm{depdeg}\,}}_{\vec {u}}(m)\).

  2. (b)

    If \(deg\) is the maximum of 1 and the highest degree of any polynomial in \(\vec {u}\), then for any \(1 \le i \le d\) we have \({{\,\textrm{depdeg}\,}}_{\vec {u}}(x_i) \le deg ^{d-i}\).

  3. (c)

    For \(mdepdeg = \max \{ {{\,\textrm{depdeg}\,}}_{\vec {u}}(x_i) \mid 1 \le i \le d \}\), we have \(mdepdeg \le deg ^{d-1}\).

Example 6.10

Again, we consider a loop as in Example 6.7 with update \(\vec {u}= (x_2^2, x_3^2, x_3)\). Then, \({{\,\textrm{depdeg}\,}}_{\vec {u}}(x_3) = \max \{1,{{\,\textrm{depdeg}\,}}_{\vec {u}}(0) \} = \max \{1,-\infty \} = 1\), \({{\,\textrm{depdeg}\,}}_{\vec {u}}(x_2) = {{\textrm{depdeg}}}_{\vec {u}}(x_3^2) = 2 \cdot {{\,\textrm{depdeg}\,}}_{\vec {u}}(x_3) = 2\cdot 1 = 2\), and \({{\,\textrm{depdeg}\,}}_{\vec {u}}(x_1) = {{\,\textrm{depdeg}\,}}_{\vec {u}}(x_2^2) = 2 \cdot {{\,\textrm{depdeg}\,}}_{\vec {u}}(x_2) = 2\cdot 2 = 4\). So intuitively, the update of \(x_1\) has degree 4 in terms of the \(\succ _{\vec {u}}\)-minimal variable \(x_3\) since the update of \(x_2\) is quadratic in \(x_3\) and \(x_2^2\) then has degree 4 w.r.t. \(x_3\). Here, \(mdepdeg = 4\) and for the maximal degree \(deg =2\) occurring in the update, we indeed have \(mdepdeg = deg ^{d-1} = 2^{3-1}\). So the bound on \(mdepdeg\) in Lemma 6.9 (c) is tight.

As another example, consider the update \(\vec {u}= (3 \cdot x_1 + 5 \cdot x_2^4 \cdot x_3^6 + 7 \cdot x_3^8, x_3^2, 9)\). Now we have \({{\,\textrm{depdeg}\,}}_{\vec {u}}(x_3) = \max \{1, {{\,\textrm{depdeg}\,}}_{\vec {u}}(9)\} = \max \{1, 0\} = 1\), \({{\,\textrm{depdeg}\,}}_{\vec {u}}(x_2) ={{\,\textrm{depdeg}\,}}_{\vec {u}}(x_3^2) = 2 \cdot {{\,\textrm{depdeg}\,}}_{\vec {u}}(x_3) = 2 \cdot 1 = 2\), and \({{\,\textrm{depdeg}\,}}_{\vec {u}}(x_1) = {{\,\textrm{depdeg}\,}}_{\vec {u}}(5 \cdot x_2^4 \cdot x_3^6 + 7 \cdot x_3^8) = \max \{ {{\,\textrm{depdeg}\,}}_{\vec {u}}(x_2^4 \cdot x_3^6), {{\,\textrm{depdeg}\,}}_{\vec {u}}(x_3^8) \} = \max \{ 4 \cdot {{\,\textrm{depdeg}\,}}_{\vec {u}}(x_2) + 6 \cdot {{\,\textrm{depdeg}\,}}_{\vec {u}}(x_3), 8 \cdot {{\,\textrm{depdeg}\,}}_{\vec {u}}(x_3) \} = \max \{ 4 \cdot 2 + 6 \cdot 1, 8 \cdot 1 \} = 14\).

Now we prove that Algorithm 3 only constructs updates \(u'_m\) for monomials m with \({{\,\textrm{depdeg}\,}}(m) \le mdepdeg\). Hence, this also proves termination of the algorithm since there are only finitely many such monomials, and it allows us to give a bound on the number of iterations of the algorithm’s while-loop.

Theorem 6.11

(Dependency Degree Suffices for Linearization)

  1. (a)

    Algorithm 3 only computes \(u'_m\) for monomials m with \({{\,\textrm{depdeg}\,}}(m) \le mdepdeg\).

  2. (b)

    The while-loop of Algorithm 3 is executed at most \(\left( {\begin{array}{c}d + mdepdeg \\ mdepdeg \end{array}}\right) - 1\) times.

  3. (c)

    Algorithm 3 terminates.

The following theorem summarizes the main properties of Algorithm 3.

Theorem 6.12

(Soundness of Algorithm 3)

  1. (a)

    For any tnn-loop \((\varphi ,\vec {u})\), Algorithm 3 computes a linearization \((\varphi ',\vec {u}')\) via \(\vec {w} = (m_1,\ldots ,m_{d'})\), where \(\deg (m_i) \le {{\,\textrm{depdeg}\,}}(m_i) \le mdepdeg\) for all \(1 \le i \le d'\).

  2. (b)

    The loop \((\varphi ,\vec {u})\) terminates iff \((\varphi ',\vec {u}')\) does.

  3. (c)

    The loop \((\varphi ',\vec {u}')\) is a linear-update tnn-loop.

As mentioned, the technique in this section is based on the linearization method of [48], where instead of tnn-loops as in Theorem 6.12, [48] works in the setting of solvable loops (Definition 2.3). But [48] has no notion like the dependency degree of Definition 6.8. Instead they only consider the degree of the polynomials in the update \(\vec {u}\). However, Example 6.7 shows that the polynomials in \(\vec {w}\) that are used for the linearization may have a higher degree than the ones in \(\vec {u}\). Here, the polynomials in \(\vec {u} = (x_2^2, x_3^2, x_3)\) only have degree 2. However, \(x_1\) is (eventually) updated to \(x_3^4\). Thus, to linearize this loop, polynomials up to degree 2 do not suffice, but \(\vec {w}\) must contain a polynomial of degree 4 like \(x_3^4\).

As we showed in Theorem 6.11 (a), the dependency degree (and hence, also the degree) of the polynomials in \(\vec {w}\) is bounded by \(mdepdeg = \max \{ {{\,\textrm{depdeg}\,}}_{\vec {u}}(x_i) \mid 1 \le i \le d \}\). Indeed, in Example 6.7 we have \({{\,\textrm{depdeg}\,}}_{\vec {u}}(x_1) = 4\). Hence, our new concept of the dependency degree was needed for the upper bound on the number of iterations of the linearization algorithm in Theorem 6.11 (b). Based on this, we can now infer the asymptotic complexity of Algorithm 3. As mentioned, we will need this in Sect. 7.2 to analyze the complexity of deciding termination of twn-loops.

By Theorem 6.11 (b), the while-loop of Algorithm 3 is executed at most \(\left( {\begin{array}{c}d + mdepdeg \\ mdepdeg \end{array}}\right) - 1\) times. Since \(\left( {\begin{array}{c}n\\ k\end{array}}\right) \in \mathcal {O}(n^k)\) for any natural numbers \(n\ge k\), we have

$$\begin{aligned} \left( {\begin{array}{c}d + mdepdeg \\ mdepdeg \end{array}}\right) = \left( {\begin{array}{c}d + mdepdeg \\ d\end{array}}\right) \in \mathcal {O}((d + mdepdeg )^{ mdepdeg }) \cap \mathcal {O}((d + mdepdeg )^{d}). \end{aligned}$$

By Lemma 6.9 (c), we have \(mdepdeg \le deg ^{d-1}\) where \(deg\) is the maximum of 1 and the highest degree of any polynomial in the update \(\vec {u}\). Hence,

$$\begin{aligned} \left( {\begin{array}{c}d + mdepdeg \\ mdepdeg \end{array}}\right) \in \mathcal {O}((d + deg ^{d-1})^d) \cap \mathcal {O}((d + deg ^{d-1})^{ deg ^{d-1}}) \subseteq \mathcal {O}((d + deg ^{d-1})^d). \end{aligned}$$

For the expression \((d + deg ^{d-1})^d\) we have (see also App. A.23):

$$\begin{aligned} (d + deg ^{d-1})^d \le 2 \cdot 2^{d + {{\,\textrm{ld}\,}}( deg )\cdot (d-1)\cdot d} \end{aligned}$$
(15)

Here, as usual, \({{\,\textrm{ld}\,}}\) denotes the logarithm to the base 2.

Thus, \((d+ deg ^{d-1})^d\) is at most exponential in d, i.e., the number of iterations of Algorithm 3 is at most exponential in d. In each such iteration, one has to compute a new polynomial \(u_1^{z_1} \cdot \ldots \cdot u_d^{z_d}\). By Theorem 6.11 (a), this polynomial only contains monomials m with \({{\,\textrm{depdeg}\,}}(m) \le mdepdeg\) and there are \(\left( {\begin{array}{c}d + mdepdeg \\ mdepdeg \end{array}}\right) \in \mathcal {O}((d + deg ^{d-1})^d)\) many such monomials (see Theorem 6.11 (b)). To compute their coefficients, one has to multiply up to \(z_1 + \ldots + z_d\) factors, where \(z_1 + \ldots + z_d \le mdepdeg \le deg ^{d-1}\). This corresponds to a nested multiplication of two factors, where the result of one multiplication step is the input to the next multiplication, and the depth of the nesting is exponential in d. So the results and the factors of the multiplications grow at most doubly exponentially in d. Therefore, this proves Lemma 6.13 (a), i.e., the runtime of Algorithm 3 is at most double exponential.

However, if the number of variables d is bounded by a constant D, then the number of iterations of Algorithm 3 and the number of monomials in the linearized updates is bounded by \(\left( {\begin{array}{c}d + mdepdeg \\ mdepdeg \end{array}}\right) \in \mathcal {O}(( deg ^D + D)^D)\), which is polynomial in \(deg\). For their coefficients, one has to multiply up to \(mdepdeg \le deg ^{D-1}\) (i.e., polynomially) many factors, i.e., this corresponds to a nested multiplication where the depth of the nesting is polynomial in \(deg\). So the results and the factors of the multiplications grow at most exponentially in \(deg\). Therefore, then linearization can be computed in exponential time. This proves Lemma 6.13 (b).

Lemma 6.13

Let \(D\in \mathbb {N}\) be fixed. The linearization of a tnn-loop

  1. (a)

    can be computed in double exponential time.

  2. (b)

    can be computed in exponential time if the number of variables d is at most D.

Example 6.14

While Lemma 6.13 only gives upper bounds on the complexity of linearization, the loop \(\textbf{L}_{ non-pspace }\) from (10) can be used to infer lower bounds. Here, the linearized loop operates on the variables

$$\begin{aligned} y_{x_1},\ldots ,y_{x_d}, \;\; y_{x_2^d},\ldots ,y_{x_d^d}, \;\; y_{x_3^{(d^2)}},\ldots ,y_{x_d^{(d^2)}}, \;\; \ldots \;\; y_{x_{d-1}^{(d^{d-2})}}, y_{x_d^{(d^{d-2})}}, \;\; y_{x_{d}^{(d^{d-1})}} \end{aligned}$$

and the corresponding linearized update \(\vec {u}'\) instantiates

  • \(y_{x_1}\) by \(y_{x_2^d}\),

  • \(y_{x_2^{(d^i)}}\) with \(y_{x_3^{(d^{(i+1)})}}\) for all \(0 \le i \le 1\),

  • ...

  • \(y_{x_{d-1}^{(d^i)}}\) with \(y_{x_d^{(d^{(i+1)})}}\) for all \(0 \le i \le d-2\), and

  • \(y_{x_d^{(d^i)}}\) with \(d^{(d^i)} \cdot y_{x_d^{(d^i)}}\) for all \(0 \le i \le d-1\).

So in particular, the update contains the constant \(d^{(d^{d-1})}\) which shows that this linearization requires exponential space if d is not bounded.

7 Complexity of deciding termination

In this section, we study the complexity of deciding termination for different classes of loops by using our results from Sects. 4 to 6. We first regard linear-update loops in Sect. 7.1, where the update is of the form \(\vec {x} \leftarrow A \cdot \vec {x} + \vec {b}\) with \(A \in \mathcal {S}^{d \times d}\) and \(\vec {b} \in \mathcal {S}^d\). The reason for this restriction is that such loops can always be transformed into twn-form by our transformation \({{\,\mathrm{ T\!r\mathrm }\,}}\) from Sect. 5. More precisely, we show that termination of linear loops with rational spectrum is Co-NP-complete if \(\mathcal {S}\in \{ \mathbb {Z}, \mathbb {Q}, \mathbb {R}_{\mathbb {A}}\}\) and that termination of linear-update loops with real spectrum is \(\forall \mathbb {R}\)-complete if \(\mathcal {S}= \mathbb {R}_{\mathbb {A}}\). Since our proof is based on a reduction to \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\), and \(\mathbb {R}_{\mathbb {A}}\) and \(\mathbb {R}\) are elementary equivalent, our results also hold if the program variables range over \(\mathbb {R}\). By combining these results with our observations on the complexity of linearization from Sect. 6, we then analyze the complexity of deciding termination for arbitrary twn-loops in Sect. 7.2. In Sect. 7.3, we show that there is an important subclass of linear loops where our decision procedure for termination works efficiently, i.e., when the number of eigenvalues of the update matrix is bounded, then termination can be decided in polynomial time. Here, we again use our transformation \({{\,\mathrm{ T\!r\mathrm }\,}}\) from Sect. 5.

For our complexity results, we assume the usual dense encoding of univariate polynomials, i.e., a polynomial of degree k is represented as a list of \(k+1\) coefficients. As discussed in [55], many problems which are considered to be efficiently solvable become intractable if polynomials are encoded sparsely (e.g., as lists of monomials where each monomial is a pair of its non-zero coefficient and its degree). With densely encoded polynomials, all common representations of algebraic numbers can be converted into each other in polynomial time [3].

7.1 Complexity of deciding termination for linear-update loops

Fig. 4
figure 4

Linear-update loop

Fig. 5
figure 5

Homogeneous linear-update loop

When analyzing linear-update loops, w.l.o.g. we can assume \(\vec {b} = \vec {0}\) since a loop of the form in Fig. 4 terminates iff the loop in Fig. 5 terminates, where \(x_{\vec {b}}\) is a fresh variable (see [31, 49]). Moreover, \(\vec {e}\) witnesses (eventual) non-termination for the loop in Fig. 4 iff \(\begin{bmatrix} \vec {e}\\ 1 \end{bmatrix}\) witnesses (eventual) non-termination for the loop in Fig. 5. Note that the only eigenvalue of \(\begin{bmatrix} A &{} \vec {b} \\ \vec {0}^T &{} 1 \end{bmatrix}\) whose multiplicity increases in comparison to A is 1. Thus, to decide termination of linear-update loops with rational or real spectrum, respectively, it suffices to decide termination of homogeneous loops of the form \((\varphi , A \cdot \vec {x})\) where A has only rational or real eigenvalues.

Such loops can always be transformed into twn-form using our transformation \({{\,\mathrm{ T\!r\mathrm }\,}}\) from Sect. 5. To compute the required automorphism \(\eta\), we compute the Jordan normal form Q of A together with the corresponding transformation matrix T, i.e., T is an invertible real matrix such that \(A=T^{-1} \cdot Q \cdot T\). Then Q is a triangular real matrix whose diagonal consists of the eigenvalues \(\lambda \in \mathbb {R}_{\mathbb {A}}\) of A. We define \(\eta \in \textrm{End}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\) by \(\eta (\vec {x}) = T \cdot \vec {x}\). Then \(\eta \in \textrm{Aut}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\) has the inverse \(\eta ^{-1}(\vec {x}) = T^{-1} \cdot \vec {x}\). Thus, \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,A \cdot \vec {x})\) is a twn-loop with the update

$$\begin{aligned} (\eta (\vec {x})) \, (A \cdot \vec {x}) \, (\eta ^{-1}(\vec {x})) = T \cdot A \cdot T^{-1} \cdot \vec {x} = Q \cdot \vec {x}. \end{aligned}$$

To analyze termination of the loop on \(\mathcal {S}^d\), we have to consider termination of the transformed loop on \(F = \widehat{\eta }(\mathcal {S}^d) = T \cdot \mathcal {S}^d\) (see Corollary 5.16).

The Jordan normal form Q as well as the matrices T and \(T^{-1}\) can be computed in polynomial time [23, 54]. Hence, we can decide whether all eigenvalues are rational or real numbers in polynomial time by checking the diagonal entries of Q. Thus, we obtain the following lemma.

Lemma 7.1

Let \((\varphi , A \cdot \vec {x})\) be a linear-update loop.

  1. (a)

    It is decidable in polynomial time whether A has only rational or real eigenvalues.

  2. (b)

    If A has only real eigenvalues, we can compute a linear \(\eta \in \textrm{Aut}_{\mathbb {R}_{\mathbb {A}}}\left( \mathbb {R}_{\mathbb {A}}[\vec {x}]\right)\) such that \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,A\cdot \vec {x})\) is a linear-update twn-loop in polynomial time.

  3. (c)

    If \((\varphi , A \cdot \vec {x})\) is a linear loop, then so is \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,A\cdot \vec {x})\).

So every linear(-update) loop with real spectrum can be transformed into a linear(-update) twn-loop, i.e., the transformation \({{\,\mathrm{ T\!r\mathrm }\,}}\) from Sect. 5 is complete for such linear(-update) loops. Note that Lemma 7.1 (a) yields an efficient check whether a given linear(-update) loop has rational or real spectrum.

As chaining (Definition 3.1) can clearly be done in polynomial time, w.l.o.g. we may assume that \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,A \cdot \vec {x}) = (\varphi ', Q \cdot \vec {x})\) is tnn. Next, to analyze termination of a tnn-loop, our technique of Sect. 4 (resp. Sect. 5.3) uses a closed form for the update. For tnn-loops \((\varphi ', Q \cdot \vec {x})\) where Q is a triangular matrix with non-negative diagonal entries, a suitable (i.e., poly-exponential) closed form can be computed in polynomial time analogously to [35, Prop. 5.2]. This closed form is linear in \(\vec {x}\) (we will discuss this closed form in Sect. 7.3, see Lemma 7.12).

According to our approach in Sect. 5.3, we now proceed as in Algorithm 2 and construct the formula \(\exists \vec {x} \in \mathbb {R}_{\mathbb {A}}^d.\ \psi _F \wedge {{\,\textrm{red}\,}}(\varphi (\vec {q}_{ norm })) \in {{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\) in polynomial time due to Corollary 5.24. Hence, we get the following lemma.

Lemma 7.2

Let \((\varphi , A\cdot \vec {x})\) be a linear-update loop with real spectrum. Then we can compute a closed formula \(\psi \in {{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\) in polynomial time such that \(\psi\) is valid iff the loop is non-terminating. If \(\varphi\) is linear, then so is \(\psi\).

Note that \(\psi\) is existentially quantified. Hence, if the loop has rational spectrum and coefficients, \(\varphi\) (and thus also \(\psi\)) is linear, and \(\mathcal {S}\in \{ \mathbb {Z}, \mathbb {Q}, \mathbb {R}_{\mathbb {A}}, \mathbb {R}\}\), then invalidity of \(\psi\) is in Co-NP as validity of such formulas is in NP, see [50]. Thus, we get the first main result of this section. Here, we fix an inaccuracy in [20, Thm. 42], where we also allowed irrational eigenvalues and coefficients, and thus \(\psi\) may contain irrational coefficients. However, to the best of our knowledge, it is not known whether validity of linear formulas from \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\) with irrational algebraic coefficients is in NP.

Theorem 7.3

(Co-NP-Completeness) For linear loops \((\varphi , A \cdot \vec {x} + \vec {b})\) with rational spectrum where \(\varphi \in {{\,\mathrm{Th_{{qf}}}\,}}(\mathbb {Q}), A \in \mathbb {Q}^{d \times d}\), and \(\vec {b} \in \mathbb {Q}^d\), termination over \(\mathbb {Z}\), \(\mathbb {Q}\), \(\mathbb {R}_{\mathbb {A}}\), and \(\mathbb {R}\) is Co-NP-complete.

Co-NP-hardness follows from Co-NP-hardness of unsatisfiability of propositional formulas: let \(\xi\) be a propositional formula over the variables \(\vec {x}\). Then the loop \((\xi [x_i / (x_i > 0) \mid 1 \le i \le d], \vec {x})\) terminates iff \(\xi\) is unsatisfiable.

We now consider linear-update loops with real spectrum (and possibly non-linear conditions) on \(\mathbb {R}_{\mathbb {A}}^d\) and \(\mathbb {R}^d\). Here, non-termination is \(\exists \mathbb {R}\)-complete.

Definition 7.4

(\(\exists \mathbb {R}\) [57, 58]) Let

$$\begin{aligned} {{\,\mathrm{Th_\exists }\,}}(\mathbb {R})_\top = \{\psi \in {{\,\mathrm{Th_\exists }\,}}(\mathbb {R}) \mid \psi \text { closed and valid}\}. \end{aligned}$$

The complexity class \(\exists \mathbb {R}\) is the closure of \({{\,\mathrm{Th_\exists }\,}}(\mathbb {R})_\top\) under poly-time-reductions.

We have \(\textsf {NP} \subseteq \exists \mathbb {R}\subseteq \textsf {PSPACE}\) (see [12]). By Lemma 7.2, non-termination of linear-update loops over \(\mathbb {R}_{\mathbb {A}}\) with real spectrum is in \(\exists \mathbb {R}\). It is also \(\exists \mathbb {R}\)-hard since \((\varphi ,\vec {x})\) is non-terminating iff \(\exists \vec {x} \in \mathbb {R}_{\mathbb {A}}^d.\, \varphi\) is valid. So non-termination is \(\exists \mathbb {R}\)-complete, i.e., termination is Co-\(\exists \mathbb {R}\)-complete (where Co-\(\exists \mathbb {R}= \forall \mathbb {R}\) [58]).

Theorem 7.5

(\(\forall \mathbb {R}\)-Completeness) Termination of linear-update loops with real spectrum over \(\mathbb {R}_{\mathbb {A}}\) and \(\mathbb {R}\) is \(\forall \mathbb {R}\)-complete.

7.2 Complexity of deciding termination for twn-loops over \(\mathcal {S}\in \{\mathbb {R}_{\mathbb {A}},\mathbb {R}\}\)

By Corollary 4.11 (b) termination is decidable for arbitrary twn-loops over \(\mathbb {R}_{\mathbb {A}}\) and \(\mathbb {R}\). So in this section, we discuss the complexity of this decision problem. First of all, deciding termination of arbitrary twn-loops over \(\mathbb {R}_{\mathbb {A}}\) and \(\mathbb {R}\) is \(\forall \mathbb {R}\)-hard since termination of linear-update loops with real spectrum over \(\mathbb {R}_{\mathbb {A}}\) and \(\mathbb {R}\) is \(\forall \mathbb {R}\)-hard by Theorem 7.5 and any such linear-update loop can be transformed into a twn-loop in polynomial time by Lemma 7.1 (b). Thus, we have a lower bound on the complexity of deciding termination for arbitrary twn-loops.

To find an upper asymptotic bound for deciding termination of twn-loops, first note that we can restrict ourselves to tnn-loops again, as any twn-loop can be transformed into a tnn-loop by chaining (Definition 3.1) in polynomial time.

The next step of our decision procedure in Algorithm 1 is to compute closed forms for the update. However, the loop \(\textbf{L}_{ non-pspace }\) in (10) showed that in general, the computation of closed forms cannot be done in polynomial space. On the other hand, as mentioned in Sect. 7.1, for linear-update loops, closed forms can be computed in polynomial time. To benefit from this upper bound, we therefore do not proceed directly as in Algorithm 1, but instead we first linearize the tnn-loop. While linearization cannot be computed in polynomial space either (see Example 6.14), in Sect. 6 we formalized and analyzed the complexity of the linearization technique from [48]. Given a tnn-loop \((\varphi ,\vec {u})\) we can compute a linear-update tnn-loop \((\varphi ',\vec {u}\,')\) such that \((\varphi ,\vec {u})\) terminates if and only if \((\varphi ',\vec {u}\,')\) does (Theorem 6.12). Clearly, linear-update tnn-loops over \(\mathbb {R}_{\mathbb {A}}\) and \(\mathbb {R}\) always have real spectrum as the eigenvalues of a triangular matrix are its diagonal entries. So by using this linearization, we can give an upper complexity bound for deciding termination of arbitrary twn-loops.

As linearization can be computed in double exponential time and thus, also in double exponential space, and termination of linear-update loops is in \(\forall \mathbb {R}\subseteq\) PSPACE \(\subseteq\) EXPTIME by Lemma 7.2 (where the size of the linear-update loop may be at most double exponential), we obtain that deciding termination of twn-loops is in 3-EXPTIME (i.e., it is between \(\forall \mathbb {R}\) and 3-EXPTIME). Moreover, if the number of variables is bounded, checking validity of a formula in \({{\,\mathrm{Th_\exists }\,}}(\mathbb {R}_{\mathbb {A}})\) is in P (see [2]). In this case, combining linearization which can be computed in exponential time and thus, also in exponential space when d is bounded (Lemma 6.13 (b)), and deciding termination of linear-update loops which is polynomial in this case (where the size of the linear-update loop may be at most exponential), we obtain Theorem 7.6 (b).

Theorem 7.6

(Membership in 3-EXPTIME) Let \(D\in \mathbb {N}\) be fixed. Termination of twn-loops over \(\mathbb {R}_{\mathbb {A}}\) and \(\mathbb {R}\)

  1. (a)

    is in 3-EXPTIME.

  2. (b)

    is in EXPTIME if the number of variables d is at most D.

7.3 Complexity of deciding termination for uniform loops

In Sect. 7.1, we showed that termination of linear loops with rational spectrum is Co-NP-complete. For proving Co-NP-hardness, we used the trivial update \({\vec {x}}\leftarrow {\vec {x}}\) induced by the identity matrix. Therefore, the question arises whether imposing suitable restrictions to the update matrix (which exclude the identity matrix) leads to a “more efficient” decision procedure for termination (assuming P \(\ne\) NP). We now analyze a special case of linear loops (so-called uniform loops) and show that for these loops deciding termination is polynomial, if one fixes the number of eigenvalues of the update matrix.

In Sect. 7.3.1, we introduce uniform loops and parameterized decision problems, and state the main result of Sect. 7.3 (Theorem 7.10). To prove it, we show that for uniform loops, instantiating the variables in the loop guard by \(\vec {q}_{ norm }\) (as required by our decision procedure from Sect. 4) results in formulas of a special structure (so-called interval conditions, see Sects. 7.3.2 and 7.3.3). Validity of these formulas can be checked in polynomial time (Sect. 7.3.4) which proves Theorem 7.10 for uniform loops over \(\mathbb {Q}\), \(\mathbb {R}_{\mathbb {A}}\), and \(\mathbb {R}\). In Sect. 7.3.5 we show that our result holds for uniform loops over \(\mathbb {Z}\) as well.

7.3.1 Uniform loops and the parameterized complexity class XP

Definition 7.7

(Uniform Loop) A linear loop \((\varphi ,A \cdot \vec {x})\) over \(\mathcal {S}\in \{ \mathbb {Z}, \mathbb {Q}, \mathbb {R}_{\mathbb {A}}\}\) is uniform if each eigenvalue \(\lambda\) of A is a non-negative number from \(\mathcal {S}\) whose eigenspace w.r.t. A is one-dimensional, i.e., \(\lambda\) has geometric multiplicity 1.

The latter property is equivalent to requiring that there is exactly one Jordan block for each eigenvalue in A’s Jordan normal form. To grasp uniform loops intuitively, consider triangular linear loops with updates \(x_i \leftarrow \lambda \cdot x_i + p_i\) for all \(1 \le i \le d\), where the factor \(\lambda \ge 0\) is the same for all i. These loops are uniform iff the relation \({\succ _{\vec {u}}}\) is total (or equivalently, iff the variables can be ordered such that the super-diagonal of A does not contain zeros).

Lemma 7.8

A triangular linear loop \((\varphi , A \cdot \vec {x})\) where all diagonal entries are identical and non-negative is uniform iff \({\succ _{\vec {u}}}\) is a total ordering.

Thus, loops like the leading example from [7] in Fig. 6 which is equivalent to Fig. 7 are uniform. In contrast, a loop is not uniform if each \(x_i\) is updated to \(\lambda \cdot x_i + c_i\) for constants \(c_i \in \mathcal {S}\). The reason is that the \(x_i\) do not occur in each other’s updates. Hence, we have \(x_i \not \succ _{\vec {u}} x_j\) and \(x_j \not \succ _{\vec {u}} x_i\) for all \(1 \le i,j \le d\).

Fig. 6
figure 6

Uniform loop [7] via Polynomials

Fig. 7
figure 7

Uniform loop [7] via Matrix

So in particular, a uniform loop cannot have more than one update of the form \(x_i \leftarrow x_i\). However, the loop condition can still be an arbitrary Boolean formula over linear inequations. Thus, our complexity result is quite surprising since it shows that for this class of loops, termination is easier to decide than satisfiability of the condition (e.g., unsatisfiability of linear formulas over \(\mathbb {R}_{\mathbb {A}}\) is Co-NP-complete). Intuitively, the reason is that our class prohibits multiple updates like \(x_i \leftarrow x_i\) where variables “stabilize” and where termination is essentially equivalent to unsatisfiability of the condition.

To give an intuition how hard the restriction to uniform loops is, we analyzed the TPDB [63] used at the Termination and Complexity Competition [25]. In the category for “Termination of Integer Transition Systems (ITSs)” we identified 467 polynomial loops with non-constant guard (i.e., termination is not trivial) and 290 (62 %) of them are uniform loops over \(\mathbb {Z}\). Similarly, in the category for “Complexity Analysis of ITSs” we found 1,258 such polynomial loops and 452 (36 %) are uniform. In fact, in practice one is often interested in termination of triangular loops where after chaining, all variables belong to the eigenvalues 0 or 1. The reason is that termination is usually easy to show if there are eigenvalues greater than 1, because they lead to exponential growth. Thus, if the loop terminates, termination is usually reached after few steps (e.g., consider a loop \((x \le c, 2 \cdot x)\) for any constant c). Hence, the number of eigenvalues k is usually smaller than the number d of program variables. In this section we show that the complexity for deciding termination of uniform loops is exponential in k but not in d. More precisely, termination of uniform loops is in the parameterized complexity class XP, where the parameter is the number k of eigenvalues.

Definition 7.9

(Parameterized Decision Problem, XP [16]) A parameterized decision problem is a language \(\mathcal {L}\subseteq \Sigma ^* \times \mathbb {N}\), where \(\Sigma\) is a finite alphabet.

The second component (from \(\mathbb {N}\)) is called the parameter of the problem.

A parameterized problem \(\mathcal {L}\) is slicewise polynomial if the time needed for deciding the question “\((x, k ) \in \mathcal {L}?\)” is in \(\mathcal {O}(\vert {}x\vert {}^{f(k)})\) where f is a computable function depending only on k.

The corresponding complexity class is called XP.

In the remainder of this section, we prove that for any fixed \(k \in \mathbb {N}\), termination of uniform loops with k eigenvalues is decidable in polynomial time.

Theorem 7.10

(Parameterized Complexity of k-Termination) We define the parameterized decision problem k-termination as follows: \(((\varphi ,A \cdot \vec {x}),k) \in \mathcal {L}_{k\text {-termination}}\) iff the loop \((\varphi ,A \cdot \vec {x})\) terminates over \(\mathcal {S}\) and A has k eigenvalues.

For uniform loops, k-termination is in XP. Moreover, for such loops, k-termination over \(\mathbb {R}\) is in XP as well.

7.3.2 Hierarchical expressions and partitions

We now elaborate on the closed forms arising from uniform loops. To this end, we fix a uniform loop \((\varphi , A \cdot \vec {x})\). Let \({{\,\textrm{spec}\,}}(A) = \{\lambda _1,\ldots ,\lambda _k\}\) be A’s eigenvalues where \(0 \le \lambda _1< \ldots < \lambda _k\), let Q be A’s Jordan normal form where the Jordan blocks are ordered such that the numbers on the diagonal are weakly monotonically increasing, and let T be the corresponding transformation matrix, i.e., \(A = T^{-1} \cdot Q \cdot T\). Moreover, let \(\eta\) be the automorphism defined by \(\eta (\vec {x}) = T \cdot \vec {x}\) and let \({{\,\mathrm{ T\!r\mathrm }\,}}_{\eta }(\varphi ,A \cdot \vec {x}) = (\eta ^{-1}(\varphi ), Q \cdot \vec {x}) = (\varphi ', Q \cdot \vec {x})\) as in Sect. 7.1.

Instead of termination of the original loop on \(\mathcal {S}^d\), we now have to prove termination of the transformed loop on \(\widehat{\eta }(\mathcal {S}^d) = T \cdot \mathcal {S}^d\). For \(\mathcal {S}\in \{ \mathbb {Q}, \mathbb {R}_{\mathbb {A}}\}\), if the eigenvalues of A are from \(\mathcal {S}\), then the transformation matrix T is an invertible matrix over \(\mathcal {S}\). Therefore, we obtain \(T \cdot \mathcal {S}^d = \mathcal {S}^d\). Hence, we now have to analyze termination of \((\varphi ', Q \cdot \vec {x})\) over \(\mathcal {S}\). In contrast, if \(\mathcal {S}= \mathbb {Z}\), then even if the eigenvalues of A are integers, the transformation matrix T or its inverse may contain non-integer rational numbers. Thus, we first regard uniform loops over \(\mathcal {S}\in \{ \mathbb {Q}, \mathbb {R}_{\mathbb {A}}\}\) and discuss the case \(\mathcal {S}= \mathbb {Z}\) in Sect. 7.3.5.

Fig. 8
figure 8

Jordan block

Fig. 9
figure 9

Multiplication of Jordan block

By uniformity, \(Q = {{\,\textrm{diag}\,}}(Q_{\lambda _1},\ldots ,Q_{\lambda _k})\) has k Jordan blocks \(Q_{\lambda _1}\), ..., \(Q_{\lambda _k}\) where \(Q_{\lambda }\) is as in Fig. 8. For each eigenvalue \(\lambda\), let \(\nu (\lambda )\) be the dimension of \(Q_{\lambda }\). Since each eigenvalue has geometric multiplicity 1, \(\nu (\lambda )\) is the algebraic multiplicity of \(\lambda\), i.e., the multiplicity as a root of the characteristic polynomial of A. For \(\nu = \nu (\lambda )\), Fig. 9 shows the form of \(Q_\lambda ^n\) as in, e.g., [35, 49], where \(\left( {\begin{array}{c}n\\ s\end{array}}\right) = 0\) if \(n < s\). This directly yields a closed form \(\vec {q}\) for the n-fold application of the update \(Q \cdot \vec {x}\) of the transformed loop. Since our approach from Sect. 4 works by analyzing eventual non-termination, we are only interested in validity of formulas for large enough n. Thus, we may assume that n is larger than the algebraic multiplicities \(\nu (\lambda )\) of all eigenvalues \(\lambda \in {{\,\textrm{spec}\,}}(A)\). Then one obtains a resulting normalized closed form \(\vec {q}_{ norm }\) which consists of normalized poly-exponential expressions of a special form, so-called hierarchical expressions. Here, for any \(\alpha \in \mathcal {Q}_\mathcal {S}[\vec {x}]\), \(\deg _{x_i}(\alpha )\) is the highest power of \(x_i\) occurring in a monomial of \(\alpha\), i.e., it is the degree of \(\alpha\) when interpreting all variables besides \(x_i\) as constants.

Definition 7.11

(Hierarchical Expression) Let \(\mathcal {Q}_\mathcal {S}[\vec {x}]_{\textrm{lin}}\) denote the set of linear polynomials from \(\mathcal {Q}_\mathcal {S}[\vec {x}]\), i.e., of degree at most 1. An expression \(h \in \mathbb {NPE}_{\mathcal {S}}[\vec {x}]\) for some ring \(\mathbb {Z}\le \mathcal {S}\le \mathbb {R}_{\mathbb {A}}\) is a hierarchical expression over the indices \(1\le i_1<\ldots <i_{\nu } \le d\) if there exist \(1 \le r \le \nu\) and \(\lambda \in \mathcal {S}_{> 0}\) such that

$$\begin{aligned} h = \sum \nolimits _{s=r}^{\nu } \alpha _s \cdot n^{s-r} \cdot \lambda ^{n}, \end{aligned}$$

where \(\alpha _s \in \mathcal {Q}_\mathcal {S}[x_{i_s},\ldots ,x_{i_{\nu }}]_{\textrm{lin}}\), \(\alpha _s(0,\ldots ,0) = 0\), and \(\deg _{x_{i_s}}(\alpha _s) = 1\) for \(r \le s \le \nu\). Here, \(\alpha _s(v_s,\ldots ,v_\nu )\) abbreviates \(\alpha _s [x_{i_s}/v_s,\ldots ,x_{i_\nu }/v_\nu ]\) for \(v_s,\ldots ,v_\nu \in \mathbb {R}_{\mathbb {A}}\). We call \({{\,\textrm{off}\,}}(h) = r\) the offset, \(\nu\) the order, and \({{\,\textrm{base}\,}}\left( h\right) = \lambda\) the base of h.

Lemma 7.12 states this observation on \(\vec {q}_{ norm }\) formally, where the index \({{\,\textrm{idx}\,}}(\lambda )\) is the sum of the algebraic multiplicities of all smaller eigenvalues than \(\lambda\), i.e.,

$$\begin{aligned} {{\,\textrm{idx}\,}}(\lambda ) = \sum \nolimits _{\lambda ' \in {{\,\textrm{spec}\,}}(A), \lambda ' < \lambda } \nu (\lambda '). \end{aligned}$$

If A’s smallest eigenvalue \(\lambda _1\) is 0, then all entries \(q_1, \ldots , q_{{{\,\textrm{idx}\,}}(\lambda _2)}\) of \(\vec {q}_{ norm }\) are 0, i.e., when inserting \(\vec {q}_{ norm }\) into the loop condition, the variables \(x_1,\ldots ,x_{{{\,\textrm{idx}\,}}(\lambda _2)}\) vanish. So from now on we assume that 0 is not an eigenvalue of A. (Note that since we just ignore the variables which belong to the eigenvalue zero, we can also permit uniform loops where the eigenvalue 0 may have a higher geometric multiplicity.)

Lemma 7.12

For all \(\lambda \in {{\,\textrm{spec}\,}}(A)\) and \(1 \le r \le \nu (\lambda )\), the \(({{\,\textrm{idx}\,}}(\lambda )+r)\)-th element of \(\vec {q}_{ norm }\) is a hierarchical expression over the indices \({{\,\textrm{idx}\,}}(\lambda ) + 1,{{\,\textrm{idx}\,}}(\lambda ) + 2, \ldots , {{\,\textrm{idx}\,}}(\lambda ) + \nu (\lambda )\) with offset r, order \(\nu (\lambda )\), and base \(\lambda\).

Fig. 10
figure 10

Uniform loop and normalized closed form

Example 7.13

Consider the uniform loop and its normalized closed form \(\vec {q}_{ norm }\) in Fig. 10, where the update matrix A has \(k = 2\) eigenvalues \(\lambda _1 = 1\) of algebraic multiplicity \(\nu _1 = 2\) and \(\lambda _2 = 2\) of algebraic multiplicity \(\nu _2 = 3\), both of which have geometric multiplicity one. Moreover, \({{\,\textrm{idx}\,}}(\lambda _1) = 0\) and \({{\,\textrm{idx}\,}}(\lambda _2) = 0+ \nu _1= 2\). Here, we have \(d = 5\).

For \(\vec {q}_{ norm }\) in Fig. 10, \(h=q_4=x_4 \cdot 2^n + \tfrac{x_5}{2} \cdot n \cdot 2^n\) is a hierarchical expression over the indices \(i_1=3\), \(i_2=4\), \(i_3=5\) with offset 2, order 3, and base 2, as \(h = \sum \nolimits _{s=2}^3 \alpha _s \cdot n^{s-2} \cdot 2^n\) for \(\alpha _2 = x_4 \in \mathbb {R}_{\mathbb {A}}[x_4,x_5]\) and \(\alpha _3 = \tfrac{x_5}{2} \in \mathbb {R}_{\mathbb {A}}[x_5]\).

To describe the form of the whole vector \(\vec {q}_{ norm }\), we now introduce hierarchical partitions. To this end, similar to the concept of solvable loops in Definition 2.3, we consider a partitioning of \(\{1,\ldots ,d\}\).

Definition 7.14

(Hierarchical Partition) For \(k\ge 1\), let \(\nu _1,\ldots ,\nu _k \in \mathbb {N}\) form a k-partition of d, i.e., \(\nu _1 + \ldots + \nu _k = d\) and \(\nu _i > 0\) for all \(1 \le i \le k\).

The blocks associated to the partition \(\nu _1,\ldots ,\nu _k\) are \(B_1 = \{1,\ldots , \nu _1\}\), \(B_2 = \{\nu _1 + 1, \ldots , \nu _1 + \nu _2\}\), ..., and \(B_k = \{\nu _1 + \ldots + \nu _{k-1} + 1, \ldots , d\}\).

The hierarchical expressions \(h_1,\ldots ,h_d\) are a hierarchical k-partition via \(\nu _1,\ldots ,\nu _k\) with bases \(0< \lambda _1< \ldots < \lambda _k\) from \(\mathcal {S}\) if for all \(1 \le i \le k\):

  1. (a)

    \(h_j\) is a hierarchical expression over the indices \(B_i\) for all \(j \in B_i\),

  2. (b)

    \({{\,\textrm{base}\,}}\left( h_j\right) = \lambda _i\) for all \(j \in B_i\),

  3. (c)

    \(h_j\) has order \(\nu _i\) for all \(j \in B_i\),

  4. (d)

    \({{\,\textrm{off}\,}}(h_{\min (B_i)})=1\), and

  5. (e)

    If \(j,j+1 \in B_i\), then \({{\,\textrm{off}\,}}(h_j) + 1 = {{\,\textrm{off}\,}}(h_{j+1})\).

Indeed, when transforming the update of a uniform loop to Jordan normal form, then the normalized closed form is always a hierarchical partition.

Corollary 7.15

(\(\vec {q}_{ norm }\) is Hierarchical Partition) Let A be the update matrix of a uniform loop with eigenvalues \(0< \lambda _1< \ldots < \lambda _k\) and algebraic multiplicities \(\nu _1,\ldots ,\nu _k\), and let Q be its Jordan normal form where the numbers on the diagonal are weakly monotonically increasing. Then the normalized closed form \(\vec {q}_{ norm }\) of the update Q is a hierarchical k-partition via \(\nu _1,\ldots ,\nu _k\) and bases \(\lambda _1< \ldots < \lambda _k\). Here the i-th block is \(B_i = \{{{\,\textrm{idx}\,}}(\lambda _i) + 1, \ldots , {{\,\textrm{idx}\,}}(\lambda _i) + \nu _i\}\).

Example 7.16

In Fig. 10, \(\vec {h} = \vec {q}_{ norm }\) is a hierarchical 2-partition via \(\nu _1 = 2\) and \(\nu _2 = 3\), blocks \(B_1 = \{1,2\}\), \(B_2 = \{3,4,5\}\), and \(\lambda _1 = 1 < 2 = \lambda _2\). Moreover, \({{\,\textrm{off}\,}}(h_1) = 1\) and \({{\,\textrm{off}\,}}(h_2) = 2\), while \({{\,\textrm{off}\,}}(h_3) = 1\), \({{\,\textrm{off}\,}}(h_4) = 2\), and \({{\,\textrm{off}\,}}(h_5) = 3\).

7.3.3 Interval conditions

Our decision procedure in Sect. 4 instantiates the variables in the polynomials f of the loop guard by \(\vec {q}_{ norm }= (h_1,\ldots ,h_d)\), resulting in poly-exponential expressions \(p = f(h_1,\ldots ,h_d)\). We now prove that in the setting of a hierarchical k-partition \(\vec {q}_{ norm }\), the atoms in \({{\,\textrm{red}\,}}(p \triangleright 0)\) (see Lemma 4.7 and (7)) are equivalent to so-called interval conditions, whose satisfiability is particularly easy to check (Sect. 7.3.4). In Lemma 7.19, Lemma 7.21, and Corollary 7.23, we introduce the different subformulas occurring in \({{\,\textrm{red}\,}}(p \triangleright 0)\). Here, it is convenient to define the active variables of polynomials.

Definition 7.17

(Active Variables) Let \(f = c_0 + \sum _{i=1}^d c_i \cdot x_i\in \mathcal {S}[\vec {x}]_{\textrm{lin}}\). We define \({{\,\textrm{actVar}\,}}(f) = \left\{ {x_i} \mid {c_i \ne 0} \right\}\). If \(x_i \in {{\,\textrm{actVar}\,}}(f)\), then \({{\,\textrm{coeff}\,}}(f,x_i) = c_i\).

Example 7.18

Consider \(f = -x_1 + 3 \cdot x_3 + 4 \in \mathcal {S}[x_1,\ldots ,x_5]_{\textrm{lin}}\). Then \({{\,\textrm{actVar}\,}}(f) = \{x_1,x_3\}\), \({{\,\textrm{coeff}\,}}(f,x_1) = -1\), and \({{\,\textrm{coeff}\,}}(f,x_3) = 3\).

From the addends \(\alpha _j \cdot n^{a_j} \cdot b_j^n\) of \(p = f(h_1,\ldots ,h_d)\) with \(\alpha _j \in \mathcal {Q}_\mathcal {S}[\vec {x}]_{\textrm{lin}}\), \(a_j \in \mathbb {N}\), and \(b_j \in \mathcal {S}_{>0}\), we again compute the set \({{\,\textrm{coefs}\,}}(p)\) of marked coefficients as in Definition 4.5, which have the form \(\alpha _j^{(b_j,a_j)}\). For any \(b \in \mathcal {S}_{>0}\) and \(a \in \mathbb {N}\), we now define a formula \({{\,\textrm{zero}\,}}(b,a)\) which is equivalent to requiring that all addends \(\alpha _j \cdot n^{a_j} \cdot b_j^n\) vanish where \(b_j = b\) and where \(a_j \ge a\).

Lemma 7.19

(Formulas for Vanishing of Addends) Let \(h_1,\ldots ,h_d\) be a hierarchical k-partition via \(\nu _1,\ldots ,\nu _k\) with bases \(0< \lambda _1< \ldots < \lambda _k\), and let \(f \in \mathcal {S}[\vec {x}]_{\textrm{lin}}\). For any \(1 \le i \le k\), let \(F(i) = \{j \in B_i \mid x_j \in {{\,\textrm{actVar}\,}}(f)\}\). Let \(M = \frac{-f(0,\ldots ,0)}{{{\,\textrm{coeff}\,}}(f,x_{\min F(i)}) \cdot c_{\min F(i)}}\) if \(\lambda _{i} = 1\) and \(F(i) \ne \emptyset\), where \(c_{\min F(i)}\) is the coefficient of \(x_{\min F(i)}\) in \(h_{\min F(i)}\).Footnote 2

For any \(b \in \mathcal {S}_{>0}\) and \(a \in \mathbb {N}\), we define \({{\,\textrm{zero}\,}}(b,a)\):

  1. (a)

    If \(\lambda _i \ne 1\) for all \(1 \le i \le k\) or \(\lambda _{i} = 1\) for some \(1 \le i \le k\) and \(F(i) = \emptyset\), then \({{\,\textrm{zero}\,}}(1,0)\) is the formula \(f(0,\ldots ,0) = 0\).

  2. (b)

    If \(\lambda _{i} = 1\) for some \(1 \le i \le k\) and \(F(i) \ne \emptyset\), then \({{\,\textrm{zero}\,}}(1,0)\) is the formula \(x_{\min F(i)} = M \wedge \bigwedge \nolimits _{j \in B_{i}, \min F(i) < j} \, (x_j = 0)\).

  3. (c)

    If \(\lambda _{i} = b\) for some \(1 \le i \le k\), \(F(i) \ne \emptyset\), and \((b,a) \ne (1,0)\), then \({{\,\textrm{zero}\,}}(b,a) = \bigwedge \nolimits _{j \in B_{i}, a + \min F(i) \le j} \, (x_j = 0)\).

  4. (d)

    Otherwise, we define \({{\,\textrm{zero}\,}}(b,a) = \textrm{true} \,\).

Let \(p = f(h_1,\ldots ,h_d) \in \mathbb {NPE}_{\mathcal {S}}[\vec {x}]\). As in Definition 4.5, let \({{\,\textrm{coefs}\,}}(p)=\{\alpha _1^{(b_1,a_1)},\ldots , \alpha _{\ell }^{(b_\ell ,a_\ell )}\}\) where \(\alpha ^{(b_i,a_i)}_i \prec _{ coef }\alpha ^{(b_{j},a_{j})}_{j}\) for all \(1 \le i < j \le \ell\).

Then \({{\,\textrm{zero}\,}}(b,a)\) is equivalent to the requirement that \(\alpha _s = 0\) holds for all \(\alpha _s^{(b_s,a_s)} \in {{\,\textrm{coefs}\,}}(p)\) with \(b_s = b\) and \(a_s \ge a\).

Example 7.20

Consider \(f = -x_1 + 3 \cdot x_3 + 4\) from Example 7.18 and \(\vec {h} = \vec {q}_{ norm }\) from Fig. 10.

We have \(p = f(h_1,\ldots ,h_5) = -h_1 + 3 \cdot h_3 + 4 =\)

$$\begin{aligned} (-x_1 + 4) -x_2 \cdot n + 3 \cdot x_3 \cdot 2^n + \left( \tfrac{3 \cdot x_4}{2}-\tfrac{3 \cdot x_5}{8}\right) \cdot n \cdot 2^n + \tfrac{3 \cdot x_5}{8} \cdot n^2 \cdot 2^n. \end{aligned}$$

In the notation of Lemma 7.19, since \(B_1 = \{ 1,2 \}\), \(B_2 = \{3,4,5\}\), and \({{\,\textrm{actVar}\,}}(f) = \{ x_1, x_3\}\), we have \(F(1) = \{ j \in \{1,2\} \mid x_j \in \{ x_1, x_3\} \}= \{1\}\) and \(F(2) = \{ j \in \{3,4,5\} \mid x_j \in \{ x_1, x_3\} \}= \{3\}\). Thus, \(\min F(1) = 1\), \(c_{\min F(1)} = 1\) is the coefficient of \(x_1\) in \(h_1\), \({{\,\textrm{coeff}\,}}(f,x_{\min F(1)}) = -1\) and thus \(M = \tfrac{-4}{-1} = 4\). Hence,

$$\begin{aligned} {{\,\textrm{zero}\,}}(1,0) = (x_1 = 4) \wedge (x_2 = 0) \text { and } {{\,\textrm{zero}\,}}(2,0) = (x_3 = 0) \wedge (x_4 = 0) \wedge (x_5 = 0). \end{aligned}$$

So \(\vec {v} \in \mathbb {R}_{\mathbb {A}}^5\) satisfies \({{\,\textrm{zero}\,}}(1,0)\) or \({{\,\textrm{zero}\,}}(2,0)\), respectively, iff all terms with base 1 or 2 vanish in \(f(h_1,\ldots ,h_5)[\vec {x}/\vec {v}]\).

After introducing \({{\,\textrm{zero}\,}}(b,a)\), we are now ready to show that the formulas in Lemma 4.7 and Theorem 4.9 have a special form when considering uniform loops. In the following lemmas, corollaries, and definitions, let \(h_1,\ldots ,h_d\), \(\nu _1, \ldots , \nu _k\), f, F(i), p, \({{\,\textrm{coefs}\,}}\left( p\right)\), and \(\ell\) always be as in Lemma 7.19.

Lemma 7.21

For any \(1 \le s_0 \le \ell\), the formula \(\alpha _{s_0} > 0 \wedge \bigwedge _{s = s_0+1}^{\ell } \left( \alpha _s = 0\right)\) is equivalent to the following formula \(\rho _{f,s_0}\), which can be computed in polynomial time from \(h_1,\ldots ,h_d\) and f:

  1. (a)

    If \((b_{s_0},a_{s_0}) = (1,0)\) and either \(\lambda _{i} \ne 1\) for all \(1\le i \le k\) or \(\lambda _{i_0} = 1\) for some \(1\le i_0 \le k\) and \(F(i_0) = \emptyset\), then \(\rho _{f,s_0}\) is

    $$\begin{aligned} f(0,\ldots ,0)> 0 \wedge \bigwedge \nolimits _{i \in \{1,\ldots ,k\}, \lambda _i > 1} {{\,\textrm{zero}\,}}(\lambda _i,0). \end{aligned}$$
  2. (b)

    If \((b_{s_0},a_{s_0}) = (1,0)\), \(\lambda _{i_0} = 1\) for some \(1\le i_0 \le k\), and \(F(i_0) \ne \emptyset\), then there is aFootnote 3\(C \in \mathcal {Q}_\mathcal {S}\) with \(C \ne 0\) such that \(\rho _{f,s_0}\) is

    $$\begin{aligned} {{\,\textrm{sign}\,}}\left( C\right) \cdot x_{\min F(i_0)} + \tfrac{f(0,\ldots ,0)}{\left| C\right| } > 0 \; \wedge {{\,\textrm{zero}\,}}(1,1) \wedge \bigwedge \nolimits _{i = i_0 + 1}^{k} {{\,\textrm{zero}\,}}(\lambda _i,0). \end{aligned}$$
  3. (c)

    If \(b_{s_0} < 1\), \(f(0,\ldots ,0) \ne 0\), and either \(\lambda _{i} \ne 1\) for all \(1\le i \le k\) or \(\lambda _{i_0} = 1\) for some \(1\le i_0 \le k\) and \(F(i_0) = \emptyset\), then \(\rho _{f,s_0}\) is \(\textrm{false}\).

  4. (d)

    Otherwise, we have \(\lambda _{i_0} = b_{s_0}\) for some \(1\le i_0 \le k\), \(F(i_0) \ne \emptyset\), and there is a numberFootnote 4\(sg \in \{1,-1\}\) such that \(\rho _{f,s_0}\) is

    $$\begin{aligned} sg \cdot x_{\min F(i_0)+ a_{s_0}} > 0 \wedge {{\,\textrm{zero}\,}}(\lambda _{i_0},a_{s_0}+1) \wedge \bigwedge \nolimits _{i = i_0+1}^k {{\,\textrm{zero}\,}}(\lambda _i,0). \end{aligned}$$

Example 7.22

For \(f, \vec {h}, p\) of Example 7.20, \({{\,\textrm{coefs}\,}}(p)\) is

$$\begin{aligned}&\{\alpha _1^{(1,0)}, \alpha _2^{(1,1)}, \alpha _3^{(2,0)}, \alpha _4^{(2,1)}, \alpha _5^{(2,2)} \} \hspace{7em} \text {where} \\&\alpha _1 = -x_1 + 4, \quad \alpha _2 = -x_2, \quad \alpha _3 = 3 \cdot x_3, \quad \alpha _4 = \tfrac{3 \cdot x_4}{2}-\tfrac{3 \cdot x_5}{8}, \quad \alpha _5 = \tfrac{3 \cdot x_5}{8}. \end{aligned}$$

Let us compute \(\rho _{f,4}\). For \(\alpha _4^{(2,1)}\), we have \(i_0 = 2\), \(\min F(i_0) = 3\), and \(a_4 = 1\). Moreover, we have \(sg = {{\,\textrm{sign}\,}}\left( {{\,\textrm{coeff}\,}}(f,x_3) \cdot c_4\right) = {{\,\textrm{sign}\,}}\left( 3 \cdot \frac{1}{2}\right) = 1\) and \({{\,\textrm{zero}\,}}(2,2) = \bigwedge _{j \in \{3,4,5\}, \, 2 + 3 \le j} \, (x_j = 0) = (x_5 = 0)\) and hence by Lemma 7.21:

$$\begin{aligned} \begin{array}{rcl} (\alpha _4> 0) \wedge (\alpha _5 = 0) &{} \iff &{} (sg \cdot x_{a_4 + \min F(i_0)}> 0) \wedge {{\,\textrm{zero}\,}}(2,a_4 + 1) \\ &{} \iff &{} (x_4 > 0) \wedge (x_5 = 0) \end{array} \end{aligned}$$

For \(\alpha _1^{(1,0)}\!\), as \(\lambda _1 = 1\) and \(F(1) = \{1\} \ne \emptyset\), by Lemma 7.21 we use \(C = {{\,\textrm{coeff}\,}}(f,x_1) \cdot 1 = -1\), \(f(0,\ldots ,0) = 4\), and \({{\,\textrm{zero}\,}}(1,1) =\bigwedge _{j \in \{1,2\}, 1 + 1 \le j} (x_j = 0) = (x_2 = 0)\) to obtain \(\rho _{f,1}\), where \({{\,\textrm{zero}\,}}(2,0)= \bigwedge _{j=3}^5 (x_j = 0)\) by Example 7.20:

$$\begin{aligned} \begin{array}{rcl} (\alpha _1> 0) \wedge \bigwedge \nolimits _{s = 2}^5 (\alpha _s = 0)\iff & {} (-x_1 + 4 > 0) \wedge (x_2 = 0) \wedge {{\,\textrm{zero}\,}}(2,0) \end{array} \end{aligned}$$

In addition to the formulas \(\rho _{f,s}\) for \(1 \le s \le \ell\), we also introduce a formula \(\rho _{f,0}\) which expresses that all coefficients of p vanish.

Corollary 7.23

\(\bigwedge \limits _{s = 1}^{\ell } \left( \alpha _s = 0\right)\) is equivalent to \(\rho _{f,0}\): \({{\,\textrm{zero}\,}}(1,0) \wedge \hspace{-.4cm} \bigwedge \limits _{\lambda \in \{1, \ldots , k\}, \lambda \ne 1} \hspace{-.5cm} {{\,\textrm{zero}\,}}(\lambda ,0)\)

Example 7.24

Reconsider Example 7.22. By Corollary 7.23, \(\bigwedge \nolimits _{s = 1}^5 (\alpha _s = 0)\) is equivalent to \(\rho _{f,0} = {{\,\textrm{zero}\,}}(1,0) \wedge {{\,\textrm{zero}\,}}(2,0)\), where \({{\,\textrm{zero}\,}}(1,0) = (x_1 = 4) \wedge (x_2 = 0)\) and \({{\,\textrm{zero}\,}}(2,0)=(x_3 = 0) \wedge (x_4 = 0) \wedge (x_5 = 0)\) by Example 7.20.

We can now combine Lemma 7.21 and Corollary 7.23 with Lemma 4.7 to obtain the following result. Here, “\({{\,\textrm{ic}\,}}\)” stands for interval conditions.

Corollary 7.25

For \({\triangleright }\in {\{\ge ,} {>\}}\), \({{\,\textrm{red}\,}}(p \triangleright 0)\) is equivalent to the formula \({{\,\textrm{ic}\,}}(p \triangleright 0)\), where \({{\,\textrm{ic}\,}}(p > 0) = \bigvee \nolimits _{s = 1}^{\ell } \rho _{f,s}\) and \({{\,\textrm{ic}\,}}(p \ge 0) = {{\,\textrm{ic}\,}}(p > 0) \vee \rho _{f,0}\).

Example 7.26

Reconsider Examples 7.22 and 7.24. By Corollary 7.25, \({{\,\textrm{red}\,}}(p > 0)\) is equivalent to \({{\,\textrm{ic}\,}}(p>0) = \rho _{f,1} \vee \rho _{f,2} \vee \rho _{f,3} \vee \rho _{f,4} \vee \rho _{f,5} =\)

$$\begin{aligned} \begin{array}{l@{}l@{}l@{}l@{}l@{}l@{}l} &{} \left( -x_1 + 4> 0 \wedge{x}_{2} = 0 \wedge {{\,\textrm{zero}\,}}(2,0)\right) &{} \vee &{} \left( -x_2> 0 \wedge {{\,\textrm{zero}\,}}(2,0)\right) &{} &{}&{}\\ \vee &{} \left( x_3> 0 \wedge x_4 = 0 \wedge x_5 = 0\right) &{} \vee &{} (x_4> 0 \wedge x_5 = 0) &{} \vee &{} (x_5 > 0), &{} \text{and} \end{array} \end{aligned}$$

\({{\,\textrm{red}\,}}(p \ge 0)\) is equivalent to \({{\,\textrm{ic}\,}}(p \ge 0) = {{\,\textrm{ic}\,}}(p > 0) \vee \left( {{\,\textrm{zero}\,}}(1,0) \wedge {{\,\textrm{zero}\,}}(2,0)\right)\).

The formulas \(\rho _{f,s}\) in Lemma 7.21 and Corollary 7.23 are so-called interval conditions.

Definition 7.27

(Interval Condition) For \(1\le i,i' \le d\), \(i \ne i'\), \(I \subseteq \{1,\ldots ,d\}\), \(sg \in \{-1,1\}\), and \(0 \ne c \in \mathcal {Q}_\mathcal {S}\), an interval condition has the following forms:

$$\begin{aligned} \begin{array}{lll@{\;\;}l@{}ll} \text {(a)} &{} &{}&{} &{} \bigwedge \nolimits _{j \in I} &{} (x_j = 0) \\ \text {(b)} &{} sg \cdot x_i &{}> 0 \;\;\wedge &{} &{} \bigwedge \nolimits _{j \in I\setminus \{ i \}}&{} (x_j = 0) \\ \text {(c)} &{} &{}&{} x_{i'} = c \;\; \wedge &{} \bigwedge \nolimits _{j \in I\setminus \{ i' \}} &{} (x_j = 0) \\ \text {(d)} &{} sg \cdot x_i &{}> 0 \;\; \wedge &{} x_{i'} = c \;\; \wedge &{} \bigwedge \nolimits _{j \in I\setminus \{ i,i' \}} &{} (x_j = 0) \\ \text {(e)} &{} sg \cdot x_i + c &{} > 0 \;\; \wedge &{} &{} \bigwedge \nolimits _{j \in I\setminus \{ i \}} &{} (x_j = 0) \end{array} \end{aligned}$$

Example 7.28

The formulas \(\rho _{f,4} = (x_4 > 0) \wedge (x_5 = 0)\) and \(\rho _{f,1} = (-x_1 + 4 > 0) \wedge \bigwedge _{j=2}^5 (x_j = 0)\) from Example 7.22 are interval conditions as in Definition 7.27 (b) and (e).

7.3.4 Checking satisfiability of interval conditions

We now show that to decide satisfiability of the formulas \({{\,\textrm{ic}\,}}(p \triangleright 0)\), we only have to regard instantiations of the variables with values from \(\{0,{{\,\textrm{1}\,}},{{\,\mathrm{-1}\,}},{{\,\mathrm{\star }\,}}\}\), where \({{\,\mathrm{\star }\,}}\) stands for one additional non-zero value. There are only polynomially many such instantiations and the particular value for \({{\,\mathrm{\star }\,}}\) is later determined by SMT solving. This SMT solving only takes polynomial time, because the resulting SMT problem only contains a single variable. Definition 7.29 instantiates variables accordingly and performs Boolean simplifications as much as possible.

Definition 7.29

(Evaluation) Let \(\rho\) be a propositional formula built from the connectives \(\wedge\) and \(\vee\) over atoms of the form \(sg \cdot x + c > 0\) and \(x = c\) for \(sg \in \{1,-1\}\), \(c \in \mathcal {Q}_\mathcal {S}\), and \(x \in \{x_1,\ldots ,x_d\}\). Moreover, let \(\vec {v} \in \{0,{{\,\textrm{1}\,}},{{\,\mathrm{-1}\,}},{{\,\mathrm{\star }\,}}\}^d\). The evaluation of \(\rho\) w.r.t. \(\vec {v}\) (written \(\rho (\vec {v}){\downarrow }\)) results from \(\rho (\vec {v}) = \rho [\vec {x}/\vec {v}]\) by simplifying (in)equations without \({{\,\mathrm{\star }\,}}\) to \(\textrm{true} \,\) or \(\textrm{false}\), and by simplifying conjunctions and disjunctions with \(\textrm{true} \,\) resp. \(\textrm{false}\). We write \(\vec {v} \vDash ^{\scriptscriptstyle ?}\rho\) if \(\rho (\vec {v}){\downarrow } \ne \textrm{false}\).

For example, if \(\rho\) is the formula \((x_1 - \tfrac{5}{2} > 0) \wedge (x_2 = 0)\) and \(\vec {v} = ({{\,\mathrm{\star }\,}}, 0)\), then \(\rho (\vec {v}){\downarrow }\) is \({{\,\mathrm{\star }\,}}- \tfrac{5}{2} > 0\). Hence, \(\vec {v} \vDash ^{\scriptscriptstyle ?}\rho\). So in general, \(\vec {v} \vDash ^{\scriptscriptstyle ?}\rho\) means that \(\rho (\vec {v}){\downarrow } = \textrm{true} \,\) or that there could be a value w for \({{\,\mathrm{\star }\,}}\) such that \(\rho [\vec {x}/\vec {v}, {{\,\mathrm{\star }\,}}/ w]{\downarrow } = \textrm{true} \,\).

Now we define candidate assignments \({{\,\textrm{cndAssg}\,}}(\rho _{f,s})\) for the formulas \(\rho _{f,s}\) in Lemma 7.21 and Corollary 7.23 which contain all \(\vec {v} \in \{0,{{\,\textrm{1}\,}},{{\,\mathrm{-1}\,}},{{\,\mathrm{\star }\,}}\}^d\) that may satisfy \(\rho _{f,s}\) (if a suitable value for \({{\,\mathrm{\star }\,}}\) is found). However, for each Block \(B_i\), at most one variable \(x_j\) with \(j \in B_i\) may be assigned a non-zero value (i.e., \({{\,\textrm{1}\,}}\), \({{\,\mathrm{-1}\,}}\), or \({{\,\mathrm{\star }\,}}\)). Moreover, the value \({{\,\mathrm{\star }\,}}\) may only be used in the block for the eigenvalue \(\lambda _i = 1\).

Definition 7.30

(Sets of Candidate Assignments) For all \(0 \le s \le \ell\), we define:

$$\begin{aligned} \begin{array}{ll} {{\,\textrm{cndAssg}\,}}(\rho _{f,s})\; = \; \{\vec {v} \in &{} \{0,{{\,\textrm{1}\,}},{{\,\mathrm{-1}\,}},{{\,\mathrm{\star }\,}}\}^d \, \mid \, \vec {v} \vDash ^{\scriptscriptstyle ?}\rho _{f,s}, \\ &{} \forall \ 1 \le i \le k. \ \text {there is at most one } j \in B_i \text { with } v_{j} \ne 0, \\ &{} v_j = {{\,\mathrm{\star }\,}}\implies j \in B_{i_0} \text { where } \lambda _{i_0} = 1 \} \end{array} \end{aligned}$$

Example 7.31

In Examples 7.22 and 7.24, for \(\rho _{f,4} = (x_4 > 0) \wedge (x_5 = 0)\), \(\vec {v} \vDash ^{\scriptscriptstyle ?}\rho _{f,4}\) implies \(v_4 = {{\,\textrm{1}\,}}\) and \(v_5 = 0\). Here, \(v_4 = {{\,\mathrm{\star }\,}}\) is not possible, because 4 does not belong to the block \(B_1 = \{1,2 \}\) for the eigenvalue 1. Since at most one value for each block may be non-zero, we have \(v_3 = 0\). In contrast, \(v_1\) and \(v_2\) can be arbitrary, but at most one of them may be non-zero. Hence, we obtain the following for \(\rho _{f,4}\) and for \(\rho _{f,0} = (x_1 = 4) \wedge \bigwedge _{j=2}^5 (x_j = 0)\):

$$\begin{aligned} {{\,\textrm{cndAssg}\,}}(\rho _{f,4})=\left\{\begin{bmatrix}{{\,\textrm{1}\,}}\\ 0 \\ 0 \\ {{\,\textrm{1}\,}}\\ 0 \end{bmatrix}\!,\begin{bmatrix}{{\,\mathrm{\star }\,}}\\ 0 \\ 0 \\ {{\,\textrm{1}\,}}\\ 0 \end{bmatrix}\!,\begin{bmatrix}{{\,\mathrm{-1}\,}}\\ 0 \\ 0 \\ {{\,\textrm{1}\,}}\\ 0 \end{bmatrix}\!,\begin{bmatrix}0 \\ {{\,\textrm{1}\,}}\\ 0 \\ {{\,\textrm{1}\,}}\\ 0 \end{bmatrix}\!,\begin{bmatrix}0 \\ {{\,\mathrm{\star }\,}}\\ 0 \\ {{\,\textrm{1}\,}}\\ 0 \end{bmatrix}\!,\begin{bmatrix}0 \\ {{\,\mathrm{-1}\,}}\\ 0 \\ {{\,\textrm{1}\,}}\\ 0 \end{bmatrix}\!,\begin{bmatrix}0 \\ 0 \\ 0 \\ {{\,\textrm{1}\,}}\\ 0 \end{bmatrix}\right\}, \; {{\,\textrm{cndAssg}\,}}(\rho _{f,0}) = \left\{\begin{bmatrix}{{\,\mathrm{\star }\,}}\\ 0 \\ 0 \\ 0 \\ 0 \end{bmatrix}\right\} \end{aligned}$$

Lemma 7.32

\(| {{\,\textrm{cndAssg}\,}}(\rho _{f,s})| \le \left( 3\!\cdot \!\max \{ \nu _i \mid 1 \le i \le k\} + 1\right) ^{k}\!\) for all \(0\!\le \!s\!\le \!\ell\).

Lemma 7.32 is crucial for our algorithm to decide k-termination for uniform loops: \(| {{\,\textrm{cndAssg}\,}}(\rho _{f,s})|\) is bounded by a polynomial in d if k is assumed to be a parameter. This is because the \(\nu _i\) form a k-partition of d, i.e., \(\nu _i \le d\) for \(1 \le i \le k\). Hence, computing \({{\,\textrm{cndAssg}\,}}(\rho _{f,s})\) can be done in polynomial time for fixed k.

Example 7.33

In Example 7.31, we have \(\nu _1=2\) and \(\nu _2=3\), and thus, \(k = 2\) and \(\max \{ \nu _i \mid 1 \le i \le k\} = 3\). Here, \(| {{\,\textrm{cndAssg}\,}}(\rho _{f,4})| = 7 \le 100 = 10^2 = (3 \cdot 3 + 1 )^2\).

This example shows that the bound in Lemma 7.32 is coarse, but it suffices for our analysis. We now combine Corollary 7.25 and Definition 7.30 to obtain the sets of candidate assignments for the disjunctions \({{\,\textrm{ic}\,}}(p > 0)\) and \({{\,\textrm{ic}\,}}(p \ge 0)\).

Corollary 7.34

We lift \({{\,\textrm{cndAssg}\,}}\) to inequations by defining

$$\begin{aligned} \begin{array}{lrcl} &{} {{\,\textrm{cndAssg}\,}}({{\,\textrm{ic}\,}}(p> 0))&{} {}={} &{} \bigcup \nolimits _{s=1}^\ell {{\,\textrm{cndAssg}\,}}(\rho _{f,s}) \\ \text {and} &{} {{\,\textrm{cndAssg}\,}}({{\,\textrm{ic}\,}}(p \ge 0)) &{} {}={} &{} {{\,\textrm{cndAssg}\,}}({{\,\textrm{ic}\,}}(p>0)) \cup {{\,\textrm{cndAssg}\,}}(\rho _{f,0}). \end{array} \end{aligned}$$

Then we have

$$\begin{aligned} \left| {{\,\textrm{cndAssg}\,}}({{\,\textrm{ic}\,}}(p \triangleright 0))\right| \le (d+2) \cdot \left( 3 \cdot \max \{ \nu _i \mid 1 \le i \le k\} + 1\right) ^{k}\!. \end{aligned}$$

For a uniform loop with condition \(\varphi\) and normalized closed form \(\vec {q}_{ norm }= \vec {h}\), let \(\varphi (\vec {h})\) contain the atoms \(f(\vec {h}) \triangleright 0\), where \(f \in \mathcal {S}[\vec {x}]_{\textrm{lin}}\). To decide termination, our algorithm computes \({{\,\textrm{cndAssg}\,}}({{\,\textrm{ic}\,}}(f(\vec {h})\triangleright 0))\) for all these atoms \(f(\vec {h}) \triangleright 0\), and then checks for each of the candidate assignments whether it is a witness for eventual non-termination. We first lift \({{\,\textrm{ic}\,}}\) and \({{\,\textrm{cndAssg}\,}}\) to linear formulas.

Definition 7.35

(\({{\,\textrm{ic}\,}}\) and \({{\,\textrm{cndAssg}\,}}\) for Linear Formulas) Let \(\varphi\) be a linear formula over the atoms \(\{f \triangleright 0 \mid f \in \mathcal {S}[\vec {x}]_{\textrm{lin}}{} , \triangleright \in \{>,\ge \}\}\) and let \(\vec {h} = (h_1,\ldots ,h_d)\) be a hierarchical k-partition. Then the formula \({{\,\textrm{ic}\,}}(\varphi (\vec {h}))\) results from replacing each atom \(f(\vec {h}) \triangleright 0\) in \(\varphi (\vec {h}) = \varphi [\vec {x}/\vec {h}]\) by \({{\,\textrm{ic}\,}}(f(\vec {h}) \triangleright 0)\). By \({{\,\textrm{cndAssg}\,}}({{\,\textrm{ic}\,}}(\varphi (\vec {h})))\) we denote the set \(\bigcup \nolimits _{f(\vec {h}) \triangleright 0 \text { atom in } \varphi (\vec {h})} \, {{\,\textrm{cndAssg}\,}}({{\,\textrm{ic}\,}}(f(\vec {h}) \triangleright 0))\).

To analyze termination of uniform loops, we now present an algorithm to decide whether for a hierarchical k-partition \(\vec {h}\) and a linear formula \(\varphi\),

$$\begin{aligned} \exists \vec {x} \in \mathcal {S}^d, n_0 \in \mathbb {N}.\ \forall n \in \mathbb {N}_{> n_0}.\ \varphi (\vec {h}) \qquad (\hbox {see } (9)) \end{aligned}$$

is valid. Our algorithm calls a method SMT(\(\psi\),\(\mathcal {V}\),\(\mathcal {S}\)) which checks whether the linear formula \(\psi\) in the variables \(\mathcal {V}\) is satisfiable. Here, the variables \(\mathcal {V}\) range over \(\mathcal {S}\in \{\mathbb {Z}, \mathbb {Q},\mathbb {R}_{\mathbb {A}}\}\) and the coefficients of the polynomials are from \(\mathcal {Q}_\mathcal {S}\). (So for \(\mathcal {S}= \mathbb {Z}\), one can first multiply all inequations in \(\psi\) by the main denominator to result in coefficients from \(\mathbb {Z}\).) In our case, \(\mathcal {V} = \{ {{\,\mathrm{\star }\,}}\}\) and thus, \(\left| \mathcal {V}\right| = 1\). With these restrictions, the method SMT has polynomial runtime (see [2, 41]). More precisely, SMT is called in Algorithm 4 to determine whether \({{\,\mathrm{\star }\,}}\) can be assigned a non-zero value such that \(\psi (\vec {v}){\downarrow }\) is satisfiable. Here, we have to assign all occurrences of \({{\,\mathrm{\star }\,}}\) in the formula \(\psi (\vec {v}){\downarrow }\) the same value.

Algorithm 4
figure d

Checking Interval Conditions

Let us discuss the complexity of Algorithm 4. The formula \({{\,\textrm{ic}\,}}(\varphi (\vec {h}))\) and each element of \({{\,\textrm{cndAssg}\,}}({{\,\textrm{ic}\,}}(\varphi (\vec {h})))\) can be computed in polynomial time. By Corollary 7.34, \({{\,\textrm{cndAssg}\,}}({{\,\textrm{ic}\,}}(\varphi (\vec {h})))\) has at most \(\left| \varphi \right| \cdot (d + 2)\cdot \left( 3 \cdot \max \{ \nu _i \mid 1 \le i \le k \}+ 1\right) ^k\) elements, where \(\left| \varphi \right|\) is the number of atoms in \(\varphi\) and \(\nu _i \le d\) for all \(1 \le i \le k\). Thus, when considering k to be a parameter, \({{\,\textrm{cndAssg}\,}}({{\,\textrm{ic}\,}}(\varphi (\vec {h})))\) can be computed in polynomial time. Moreover, evaluating a formula w.r.t. \(\vec {v}\) according to Definition 7.29 is possible in polynomial time, too. Finally, SMT has polynomial runtime as discussed before. So the runtime of the algorithm is polynomial when regarding k as a parameter. We now prove that Algorithm 4 is sound and complete.

Theorem 7.36

Algorithm 4 returns \(\top\) iff \(\exists \vec {x} \in \mathcal {S}^d. \ {{\,\textrm{ic}\,}}(\varphi (\vec {h}))\) is valid.

Example 7.37

Consider the uniform loop in Fig. 10 where \(\varphi = f> 0 \wedge f' > 0\) for \(f = -x_1 + 3 \cdot x_3 + 4\) and \(f' = 2 \cdot x_1 - 5\). Let \(\vec {h} = \vec {q}_{ norm }\) as in Fig. 10 and let \(p = f(\vec {h})\) and \(p' = f'(\vec {h}) = (2 \cdot x_1 - 5) + 2 \cdot x_2 \cdot n\). Here, \(\psi = {{\,\textrm{ic}\,}}(\varphi (\vec {h})) = {{\,\textrm{ic}\,}}(p>0) \wedge {{\,\textrm{ic}\,}}(p' > 0)\), where \({{\,\textrm{ic}\,}}(p>0) = \bigvee _{s=1}^5\rho _{f,s}\) is stated in Example 7.26. Note that \({{\,\textrm{coefs}\,}}(p') = \{ {\alpha '_1}^{(1,0)}, {\alpha '_2}^{(1,1)} \}\) with \(\alpha _1' = 2 \cdot x_1 - 5\) and \(\alpha _2' = 2 \cdot x_2\). Hence, \({{\,\textrm{ic}\,}}(p' > 0) = \rho _{f',1} \vee \rho _{f',2}\). To compute \(\rho _{f',1}\), for \(C = {{\,\textrm{coeff}\,}}(f',x_1) \cdot c_1 = 2 \cdot 1 = 2\) and \(f'(0,\ldots ,0) = -5\), Lemma 7.21 (b) results in \(\rho _{f',1} = (x_1 - \tfrac{5}{2} > 0) \wedge (x_2 = 0)\). For \(\rho _{f',2}\), with \(sg = {{\,\textrm{sign}\,}}\left( {{\,\textrm{coeff}\,}}(f',x_1) \cdot c_2\right) = {{\,\textrm{sign}\,}}\left( 2 \cdot 1\right) = 1\), Lemma 7.21 (d) results in \(\rho _{f',2} = (x_2 > 0)\). Now from \({{\,\textrm{ic}\,}}(p > 0)\) let us choose the disjunct \(\rho _{f,1} = (-x_1 + 4 > 0) \wedge \bigwedge _{j=2}^5 (x_j = 0)\) and from \({{\,\textrm{ic}\,}}(p' > 0)\) let us choose the disjunct \(\rho _{f',1} = (x_1 - \tfrac{5}{2} > 0) \wedge (x_2 = 0)\). We consider \(\vec {v} = ({{\,\mathrm{\star }\,}}, 0, 0, 0, 0)\). Then

$$\begin{aligned} (\rho _{f,1} \wedge \rho _{f',1})(\vec {v}){\downarrow } \;\;= \;\; ( -{{\,\mathrm{\star }\,}}+ 4> 0) \; \wedge \; ({{\,\mathrm{\star }\,}}- \tfrac{5}{2} > 0) \end{aligned}$$

is satisfiable with the model \({{\,\mathrm{\star }\,}}= 3\). Hence, this model also satisfies \(\psi (\vec {v}){\downarrow } \wedge {{\,\mathrm{\star }\,}}\ne 0\). Thus, for both \(\mathcal {S}\in \{ \mathbb {Q}, \mathbb {R}_{\mathbb {A}}\}\), Algorithm 4 proves validity of \(\exists \vec {x} \in \mathcal {S}^d. \ {{\,\textrm{ic}\,}}(\varphi (\vec {h}))\) and therefore, non-termination of the uniform loop over \(\mathcal {S}\).

So for a uniform loop over \(\mathcal {S}\in \{ \mathbb {Q}, \mathbb {R}_{\mathbb {A}}\}\), non-termination is equivalent to validity of \(\exists \vec {x} \in \mathcal {S}^d.\ \forall n \in \mathbb {N}_{> n_0}.\ \varphi (\vec {q}_{ norm })\), which in turn is equivalent to a formula only containing interval conditions. This insight reduces the search space for proving validity drastically. Thus, we can now prove Theorem 7.10 for \(\mathcal {S}\in \{ \mathbb {Q}, \mathbb {R}_{\mathbb {A}}, \mathbb {R}\}\).

Proof of Theorem 7.10

For \(\mathcal {S}\in \{ \mathbb {Q}, \mathbb {R}_{\mathbb {A}}\}\), we first transform the uniform loop such that the update matrix is in Jordan normal form and then compute the normalized closed form as in Lemma 7.12 in polynomial time. This closed form is a hierarchical partition by Corollary 7.15. By combining Corollary 7.25 and Theorem 7.36, Algorithm 4 can decide validity of the formula from Theorem 4.9, i.e., termination of the transformed loop (which is equivalent to termination of the original loop by Corollary 5.16).

As the computation of the equivalent interval conditions in Corollary 7.25 clearly works in polynomial time and we have discussed that Algorithm 4 runs in polynomial time when k is assumed to be a parameter, this proves the statement.

Finally, these loops terminate over \(\mathbb {R}_{\mathbb {A}}\) iff they terminate over \(\mathbb {R}\) by Corollary 4.11. \(\square\)

For Theorem 7.10, it was crucial to transform the loop such that the update matrix is in Jordan normal form. Here we relied on a special closed form for the Jordan normal form, while in Sect. 7.1 we only used the transformation to argue why the closed form is computable in polynomial time. Thus, the transformation from Sect. 5 does not only generalize our results from Sect. 4 to a wider class of loops but it also gives rise to novel results like Theorem 7.10.

The approach in the proof of Theorem 7.10 also works for uniform loops over \(\mathbb {Z}\) if the update matrix is already in Jordan normal form. But otherwise, in addition to \(\varphi (\vec {q}_{ norm })\) we also have an update-invariant and \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S},\mathbb {R}_{\mathbb {A}})\)-definable subset F which stems from the transformation into Jordan normal form (see Sect. 5). Thus, to decide termination we have to decide validity of \(\exists \vec {x} \in \mathbb {R}_{\mathbb {A}}^d.\ \forall n \in \mathbb {N}_{> n_0}.\ \varphi (\vec {q}_{ norm }) \wedge \psi _F\). We will discuss this in the next section.

7.3.5 Termination of uniform loops over the integers

Now we show that deciding termination of uniform loops \((\varphi ,A\cdot \vec {x})\) over the integers is also in XP. Let \(A \in \mathbb {Z}^{d \times d}\) with k integer eigenvalues each of geometric multiplicity one. Then there is a matrix \(T \in \mathbb {Q}^{d \times d}\) such that \(A = T^{-1} \cdot Q \cdot T\) for a matrix Q in Jordan normal form. However, in general we do not have \(T,T^{-1}\in \mathbb {Z}^{d \times d}\) (see, e.g., [59]). As before, let \(\eta (\vec {x}) = T \cdot \vec {x}\) and \(\varphi ' = \eta ^{-1}(\varphi )\). Then termination of \((\varphi ,A\cdot \vec {x})\) on \(\mathbb {Z}^d\) is equivalent to termination of \((\varphi ',Q \cdot \vec {x})\) on \(\widehat{\eta }(\mathbb {Z}^d) = T \cdot \mathbb {Z}^d\) by Corollary 5.16. Here, \(\mathcal {L}_T = T \cdot \mathbb {Z}^d\) is the set of all integer linear combinations of T’s columns, i.e., their lattice. In general, we have \(\mathcal {L}_T \ne \mathbb {Z}^d\).

Hence, we now call Algorithm 4 with the input \((\varphi ',\vec {h},\mathbb {Q})\), where \(\vec {h} = \vec {q}_{ norm }\) is the normalized closed form for the update \(Q \cdot \vec {x}\). For \(\psi = {{\,\textrm{ic}\,}}(\varphi '(\vec {h}))\), we want to find out if \(\psi (\vec {v})\) holds for some \(\vec {v} \in \mathcal {L}_T\). Such a \(\vec {v} \in \mathcal {L}_T\) would witness eventual non-termination of \((\varphi ',Q \cdot \vec {x})\), and since \(\mathcal {L}_T\) is update-invariant under \(Q \cdot \vec {x}\) by Lemma 5.12, this is equivalent to non-termination of \((\varphi ',Q \cdot \vec {x})\) on \(\mathcal {L}_T\).

We modify Algorithm 4 such that it computes all \(\vec {v} \in {{\,\textrm{cndAssg}\,}}(\psi )\) where \(\psi (\vec {v}){\downarrow } \wedge {{\,\mathrm{\star }\,}}\ne 0\) is satisfied by some \(\vec {v}'\) that results from \(\vec {v}\) by instantiating \({{\,\mathrm{\star }\,}}\) with a suitable number from \(\mathbb {Q}\). As shown in Corollary 7.34, for a fixed number of eigenvalues k, there are only polynomially many such candidate assignments \(\vec {v}\). Note that the formula \(\varphi '\) is only built from the connectives \(\wedge\) and \(\vee\), and \(\psi\) results from \(\varphi '\) by replacing each atom \(f \triangleright 0\) by \({{\,\textrm{ic}\,}}(f(\vec {h}) \triangleright 0)\). Hence, for every such \(\vec {v}\) there is a subset \(\{ {{\,\textrm{ic}\,}}(f_1(\vec {h}) \triangleright _1 0), \ldots , {{\,\textrm{ic}\,}}(f_e(\vec {h}) \triangleright _e 0) \}\) of these formulas such that \(\vec {v}'\) satisfies them all and such that satisfying these formulas is sufficient for satisfying \(\psi\). For each \(1 \le r \le e\), let \(\ell _r = |{{\,\textrm{coefs}\,}}(f_r(\vec {h}))|\). By Corollary 7.25, \({{\,\textrm{ic}\,}}(f_r(\vec {h}) \triangleright _r 0)\) has the form \(\bigvee _{s = 1}^{\ell _r} \rho _{f_r,s}\) or \(\bigvee _{s = 0}^{\ell _r} \rho _{f_r,s}\). So for every r there is at least one s where \(\rho _{f_r,s}(\vec {v}')\) is true. But due to the construction of \(\rho _{f_r,s}\) in Lemma 7.21 and Corollary 7.23, there is at most one \(0 \le s \le \ell _r\) where \(\rho _{f_r,s}(\vec {v}')\) is true. Thus, for every \(1 \le r \le e\), there is a unique \(0 \le s_r \le \ell _r\) where \(\rho _{f_r,s_r}(\vec {v}')\) is true.

By Lemma 7.21 and Corollary 7.23, all \(\rho _{f_r,s_r}\) are interval conditions. Thus, for each entry \(v_j\) of \(\vec {v}'\) we can find out whether \(x_j = v_j\) is required by some \(\rho _{f_r,s_r}\), or whether \(v_j = 0\) is just due to setting variables to zero by default, i.e., the formula would still hold when assigning an arbitrary value from \(\mathbb {Q}\) to \(v_j\). So every \(\vec {v} \in {{\,\textrm{cndAssg}\,}}(\psi )\) gives rise to a certain set of formulas \(\{ \rho _{f_1,s_1}, \ldots , \rho _{f_e,s_e} \}\), which in turn results in a certain abstract assignment that indicates for each entry of \(\vec {v}'\) whether its actual value is necessary to be a model.

Definition 7.38

(Abstract Assignment) Let \(\mathbb {I}\) be the set of all intervals of the forms [cc], \((-\infty , c)\), \((c, \infty )\), \((-\infty , \infty )\), or (cd) for \(c,d \in \mathbb {Q}\) with \(c \le d\). Then an abstract assignment is an element of \(\mathbb {I}^d\).

For each of the obtained abstract assignments, we now have to check whether it is satisfied by some value from \(\mathcal {L}_T\). Let \(N \subseteq \{1,\ldots ,d\}\) be those indices where \(j \in N\) iff the j-th component of the abstract assignment is [0, 0], i.e., iff the j-th component must be 0 in order to satisfy all \(\rho _{f_s,m_s}\). Then we compute a basis of the sublattice \(\mathcal {L}_N = \{\vec {w} \in \mathcal {L}_T \mid w_j =0 \text { for } j \in N\}\). To this end, we solve the system of linear equations \(\bigwedge _{j \in N} (T \cdot \vec {x})_j = 0\) where \(\vec {x} \in \mathbb {Z}^d\). Here as usual, \((T \cdot \vec {x})_j\) denotes the j-th component of the vector \(T \cdot \vec {x}\). This problem can be solved in polynomial time (see, e.g., [22, 60]). Since in general this system contains more variables than equations, the solutions yield a certain linear dependence between the variables. This dependence can then be used to reduce the number of variables in the system, i.e., it gives rise to a basis of \(\mathcal {L}_N\), where each basis vector is represented by a \(\mathbb {Z}\)-linear combination of the columns of T. Let \(d' \le d\) be the rank of the sublattice \(\mathcal {L}_N\) (i.e., the number of its basis vectors) and let \(P \in \mathbb {Q}^{d \times d'}\) be the matrix whose columns form the basis of \(\mathcal {L}_N\).

Let \(N' \subseteq \{1,\ldots ,d\}\) be those indices where \(j \in N'\) iff the j-th component of the abstract assignment is neither [0, 0] nor \((-\infty ,\infty )\). So the j-th component must be from a certain interval in order to satisfy all \(\rho _{f_s,m_s}\). To ease notation, we define \(I_j = (-\infty ,\infty )\) if \(j \notin N'\) and let \(K=\prod _{j=1}^d I_j \subseteq \mathbb {Q}^d\). Now we have to decide whether there exists an \(X \in \mathbb {Z}^{d'}\) such that \(P \cdot X \in K\).

Let \(B_1, \ldots , B_k\) again be the blocks from the k-partition \(h_1,\ldots ,h_d\). Note that if there is a block \(B_i\) where some \(\rho _{f_r,s_r}\) requires \(x_j\) with \(j \in B_i\) to be non-zero (i.e., \(j \in N'\)), then \(\rho _{f_r,s_r}\) requires all \(x_{j'}\) with \(j' > j\) and \(j' \in B_i\) to be zero (i.e., \(j' \in N\)). Thus, since \(\vec {v}'\) satisfies all formulas \(\rho _{f_r,s_r}\) for \(1 \le r \le e\), for each block \(B_i\) there can be at most one \(r \in B_i\) where some \(\rho _{f_r,s_r}\) requires \(x_j\) to be non-zero. Hence, for each block \(B_i\) there is at most one \(j \in B_i\) where \(j \in N'\). Note that containment in an interval can be described by at most 2 inequations, where the strict inequations can be turned into weak ones since the variables only range over the integers. Thus, to describe the required containment in the intervals for all \(x_j\) with \(j \in N'\), we need at most \(2 \cdot k\) inequations. In other words, the requirement \(P \cdot X \in K\) can be described by \(2 \cdot k\) linear inequations where the coefficients are from \(\mathbb {Q}\). Since linear integer programming with rational coefficients and a fixed number of constraints is possible in polynomial time (see [41]), this shows that checking whether a candidate assignment \(\vec {v}\) gives rise to a solution in \(\mathcal {L}_T\) can be done in XP. As \(\left| {{\,\textrm{cndAssg}\,}}(\psi )\right|\) is also polynomial for fixed k, k-termination of uniform loops over \(\mathbb {Z}\) is in XP as well.

8 Conclusion and related work

In this work, we studied termination of twn-loops, i.e., loops where the update \({\vec {x}}\leftarrow {\vec {u}}\) is a triangular system of polynomial equations and the use of non-linearity in \(\vec {u}\) is mildly restricted. We first presented a reduction from termination of twn-loops to \({{\,\mathrm{Th_\exists }\,}}(\mathcal {S})\) in Sects. 3 and 4. This implies decidability of termination over \(\mathcal {S}\in \{\mathbb {R}_{\mathbb {A}},\mathbb {R}\}\) and semi-decidability of non-termination over \(\mathcal {S}\in \{\mathbb {Z},\mathbb {Q}\}\).

In addition, we showed how to transform certain non-twn-loops into twn-form in Sect. 5, and discussed how this generalizes our results to a wider class of loops. We also showed that twn-transformability is semi-decidable.

Afterwards, we analyzed the complexity of deciding termination for different subclasses of twn-loops. In Sect. 6, we first showed that linearizing twn-loops can be done in double exponential time. In Sect. 7, we used our transformation and decision procedure to prove Co-NP-completeness (\(\forall \mathbb {R}\)-completeness) of termination of linear (linear-update) loops with rational (real) spectrum, and based on linearization, that deciding termination of arbitrary twn-loops over \(\mathbb {R}_{\mathbb {A}}\) or \(\mathbb {R}\) is in 3-EXPTIME.

Finally, we showed that for the subclass of uniform loops over \(\mathcal {S}\in \{\mathbb {Z},\mathbb {Q},\mathbb {R}_{\mathbb {A}}, \mathbb {R}\}\), termination can be decided in polynomial time, if the number of eigenvalues of the update matrix is fixed. So here our decision procedure can be used as an efficient technique for termination analysis.

8.1 Related work

In contrast to automated termination analysis (see e.g., [1, 6, 7, 9, 11, 24, 25, 39, 40, 51]), we investigated decidability of termination for certain classes of loops in Sect. 4. As termination is undecidable in general, decidability results can only be obtained for very restricted classes of programs.

Nevertheless, many techniques used in automated tools for termination analysis (e.g., ranking functions [1, 5,6,7, 9, 51]) focus on similar classes of loops, since such loops occur as sub-programs in (abstractions of) real programs. Tools based on these techniques have turned out to be very successful, also for larger classes of programs. Thus, these tools could benefit from integrating our (semi-)decision procedures and applying them instead of incomplete techniques for any sub-program that can be transformed into a twn-loop.

Related work on decidability of termination also considers similar (and often more restricted) classes of loops. For linear conjunctive loops, termination over \(\mathbb {R}\) [42, 45, 62, 66] and \(\mathbb {Q}\) [10] is decidable. Decidability of termination of linear conjunctive loops over \(\mathbb {Z}\) was conjectured to be decidable in [62]. After several partial results [8, 19, 49] this conjecture was confirmed recently in [31]. However, [4] shows that for slight generalizations of linear conjunctive loops over \(\mathbb {Z}\), where a non-deterministic update or a single piecewise update of a variable are allowed, termination is undecidable. Tiwari [62] uses the special case of our twn-transformation from Sect. 5 where the loop and the automorphism are linear. In contrast to these results, our approach applies to non-linear loops with arbitrary conditions over various rings.

Linearization is another attempt to handle non-linearity, see Sect. 6. While the update of solvable loops can be linearized [48], the condition cannot. Otherwise, one could linearize any loop \((p = 0, \vec {x})\), which terminates over \(\mathbb {Z}\) iff p has no integer root. By [31], this would imply decidability of Hilbert’s Tenth Problem.

In the non-linear case, [43] proves decidability of termination for conjunctive loops on \(\mathbb {R}^d\) for the case that the condition defines a compact and connected subset of \(\mathbb {R}^d\). In [65], decidability of termination of conjunctive linear-update loops on \(\mathbb {R}^d\) with the non-zero minimum property is shown, which covers conjunctive linear-update loops with real spectrum. For general conjunctive linear-update loops on \(\mathbb {R}^d\) undecidability is conjectured. Moreover, [64] shows that termination of conjunctive linear-update loops where the update matrix has only periodic real eigenvalues is decidable, which also covers conjunctive linear-update loops with real spectrum. Here, a special case of our transformation from Sect. 5 with linear automorphisms is used. In combination with [48], the papers [64, 65] both yield a decision procedure for termination of conjunctive twn-loops over \(\mathbb {R}\). Furthermore, [47] proves that termination of (not necessarily conjunctive) linear-update loops is decidable if the condition describes a compact set. Finally, [67] gives sufficient criteria for (non-)termination of solvable loops and [44] presents sufficient conditions under which termination of non-deterministic non-linear loops on \(\mathbb {R}^d\) can be reduced to satisfiability of a semi-algebraic system.

For linear-update loops with real spectrum over \(\mathbb {R}\), we prove \(\forall \mathbb {R}\)-completeness of termination, whereas [64, 65] do not give tight complexity results. The approach from [67] is incomplete, whereas we present a complete reduction from termination to the respective existential fragment of the first-order theory. The work in [44] is orthogonal to ours as it only applies to loops that satisfy certain non-trivial conditions. Moreover, we consider loops with arbitrary conditions over various rings, while [43, 44, 64, 65] only consider conjunctive loops over \(\mathbb {R}\) and [47] only considers loops over \(\mathbb {R}\) where the condition defines a compact set.

Regarding complexity, [49] proves that termination of conjunctive linear loops over \(\mathbb {Z}\) with update \(\vec {x} \leftarrow A \cdot \vec {x} + \vec {b}\) is in PSPACE if \(\vert {}\vec {x}\vert {} \le 4\) resp. in EXPSPACE if A is diagonalizable. Moreover, in [5] it is shown that existence of a linear (lexicographic) ranking function for linear conjunctive loops over \(\mathbb {Q}\) or \(\mathbb {Z}\) is Co-NP-complete.

Our Co-NP-completeness result is orthogonal to those results as we allow disjunctions in the condition. Moreover, Co-NP-completeness also holds for termination over \(\mathbb {Z}\), while [10, 62] only consider termination over \(\mathbb {Q}\) resp. \(\mathbb {R}\). Additionally, we showed that k-termination of uniform loops over \(\mathbb {Z}\), \(\mathbb {Q}\), \(\mathbb {R}_{\mathbb {A}}\), and \(\mathbb {R}\) is in XP, where the parameter k is the number of eigenvalues. This result is also orthogonal to [10, 62] since we again allow disjunctions in the condition. Furthermore, existence of a linear (lexicographic) ranking function is not necessary for termination of linear loops. We refer to [29] for further discussion on possible extensions of our results to uniform loops over \(\mathcal {S}\), where however the eigenvalues are not from \(\mathcal {S}\).

Several works exploit the existence of closed forms for solvable (or similar classes of) loops, e.g., to analyze termination on a given input, to infer runtime bounds, or to reason about invariants [28, 32, 35, 36, 48, 56]. While our approach covers solvable loops with real eigenvalues (by Corollary 5.20), it also applies to loops which are not solvable, see Example 5.23. Our transformation of Sect. 5 may also be of interest for other techniques for solvable or other sub-classes of polynomial loops, as it may be used to extend the applicability of such approaches.