Abstract 1 Introduction 2 Programs & Termination 3 On Constraint Terms 4 Positive Eigenvalues 5 Towards Non-Termination Witnesses 6 Deciding PAST 7 Implementation and Conclusion References

Deciding Termination of Simple Randomized Loops

Éléanore Meyer ORCID RWTH Aachen University, Germany Jürgen Giesl ORCID RWTH Aachen University, Germany
Abstract

We show that universal positive almost sure termination (UPAST) is decidable for a class of simple randomized programs, i.e., it is decidable whether the expected runtime of such a program is finite for all inputs. Our class contains all programs that consist of a single loop, with a linear loop guard and a loop body composed of two linear commuting and diagonalizable updates. In each iteration of the loop, the update to be carried out is picked at random, according to a fixed probability. We show the decidability of UPAST for this class of programs, where the program’s variables and inputs may range over various sub-semirings of the real numbers. In this way, we extend a line of research initiated by Tiwari in 2004 into the realm of randomized programs.

Keywords and phrases:
decision procedures, randomized programs, linear loops, positive almost sure termination
Copyright and License:
[Uncaptioned image] © Éléanore Meyer and Jürgen Giesl; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Theory of computation Probabilistic computation
; Theory of computation Program analysis
Related Version:
Full version, including all proofs: https://arxiv.org/abs/2506.18541 [26]
Supplementary Material:

Software  (Source Code): https://github.com/aprove-developers/SiRop [24]
  archived at Software Heritage Logo swh:1:dir:3ca664cebef79bfeb95ec944ddc8441d3b528bf6
Funding:
DFG Research Training Group 2236 UnRAVeL.
Acknowledgements:
We thank Sophia Greiwe for her help with the implementation of our decision procedure in SiRop.
Editors:
Paweł Gawrychowski, Filip Mazowiecki, and Michał Skrzypczak

1 Introduction

We consider the problem of universal positive almost sure termination (UPAST), i.e., deciding whether a given randomized program has finite expected runtime on all inputs [6, 34]. This is a stronger property than universal almost sure termination (UAST) which requires that the probability of termination is 1. Our programs are simple randomized loops of the form

while 𝐂x>0:x𝐀xp𝐁x (1)

Here, x=(x1,,xn) denotes the vector of program variables that range over a semiring 𝒮, and 𝐂m×n is a matrix representing the loop guard with m linear constraints over the program variables. In each execution of the loop body, a matrix is chosen among 𝐀,𝐁𝒮n×n according to the probability p[0,1] and the value x is updated accordingly.

Our Contribution.

We show that UPAST is decidable for all 𝒮{,,𝔸} when limited to loops with diagonalizable commuting matrices 𝐀 and 𝐁, where 𝔸 is the set of algebraic real numbers.111Our approach only considers algebraic real p, 𝐀, 𝐁, and 𝐂, as it is not possible to represent arbitrary real numbers on computers. However, in Sect. 6 we will see that such a loop terminates for all algebraic real inputs iff it terminates for all real inputs. Thus, we extend previous results on the termination of linear and affine222In an affine (or non-homogeneous) loop, the guard may have the form 𝐂x>c and the update may have the form x𝐀x+a for arbitrary vectors c and a. non-randomized loops to the randomized setting. In addition to deciding universal termination, our approach can compute a non-termination witness x𝒮n, i.e., if the loop is non-terminating, then x is an input leading to an infinite expected runtime.

Our programs go beyond single path loops as we might have 𝐀𝐁. Thus, for every k,there is not just a single execution of length k but one has a “range” of possible executions of length k where each execution occurs according to a known probability. To ensure tractability of the resulting problem we require commutativity of both updates, so that we can focus on how often each update has been selected in an execution, but we do not have to take the 2k different orders into account in which the two updates might have been chosen. Moreover, we require diagonalizability to obtain closed forms of a certain shape, which allows us to analyze the behavior of a “range” of different executions at once. To demonstrate the practical applicability of our decision procedure and the computation of non-termination witnesses, we provide a prototype implementation for the case 𝒮=𝔸 with our tool SiRop.

Related Work.

We continue a line of research started in 2004 by Tiwari [35] who showed decidability of universal termination for loops with an affine guard and an affine update as its body, where the guard, updates, and inputs range over the real numbers. In his proof, Tiwari reduced the affine to the linear case. In 2006, Braverman [7] proved that the problem remains decidable for loops and inputs ranging over the rational numbers , and if the guard and update are linear, then he also showed decidability over the integers . Similar to Tiwari, Braverman also reduced the affine case for to the linear case. In 2015, Ouaknine et al. proved [31] that the affine case is decidable over the integers whenever the update is of the form x𝐀x+a, provided that 𝐀n×n is diagonalizable. This restriction was removed by Hosseini et al. in 2019 [18]. In a related line of work, we proved decidability of universal termination over the integers for triangular affine loops, i.e., where the matrix 𝐀n×n is triangular [12]. Later, we extended these results to triangular weakly non-linear loops which extend triangular loops by allowing certain non-linear updates [15, 16].

The only decidability results for termination of randomized programs that we are aware of consider probabilistic vector addition systems [8] or constant probability programs [13], i.e., loops whose guards consist of only one affine inequation and whose bodies consist of several probabilistic branches (with fixed probabilities) that may increase or decrease the program variables by fixed constants. The programs in [8, 13] are orthogonal to the ones considered in our approach as they only allow to modify the program variables by adding constants, but do not allow for multiplication. Another related area of research [4, 21, 28] deals with prob-solvable loops and moment invariants. Given such a loop, these techniques can compute closed forms for all moments of program variables for a given iteration of the loopand, by taking a limit, also upon the loop’s termination. Thus, if restricted to almost surely terminating programs, they can decide UPAST. However, in contrast to our method, these techniques require that all variables in the loop guard may only take finitely many values. Moreover, there are many automated approaches for tackling UPAST using so-called ranking supermartingales (RSM), e.g., [1, 3, 5, 9, 10, 25, 27, 30, 36]. To generate a suitable RSM, one often uses techniques based on affine or polynomial templates, which renders the approach incomplete. In [19], the authors showed that deciding UPAST is harder than deciding universal termination for non-randomized programs in terms of the arithmetic hierarchy.

Outline of our Approach.

We assume familiarity with basics from probability theory and linear algebra (we recapitulate some main concepts in [26]). Sect. 2 formally introduces simple randomized loops, gives their semantics in terms of a probability space, and presents most of the definitions used throughout the paper. For a loop as in 1, we consider (finite) executions f corresponding to words over the alphabet {A,B}, where the i-th symbol in f indicates which update matrix was used in the i-th application of the assignment x𝐀xp𝐁x. For such executions f, |f|A and |f|B denote the number of A- and B-symbols in f, respectively. Moreover, we introduce the function 𝒱alx that maps finite executions f to the values 𝒱alx(f)m of the constraints in the loop guard after executing f on a concrete input xn, i.e., 𝒱alx(f)=𝐂𝐀|f|A𝐁|f|Bx, since 𝐀 and 𝐁 commute. Our decision procedure does not search for a non-terminating input directly, but for an eventually non-terminating input x. An input x is eventually non-terminating if by repeated execution of the loop body on x (while ignoring the guard), a non-terminating input can eventually be reached. In Lemma 12, we show that a loop has an eventually non-terminating input iff it also has a non-terminating input. Later (in Sect. 6) we will show how to lift such an eventually non-terminating input to an actual non-terminating input.

In Sect. 3, we introduce a mapping 𝒰 that maps executions f to the difference between the relative number |f|A|f| of times that the update matrix 𝐀 has been chosen in f and the probability p of choosing 𝐀, i.e., 𝒰(f)=|f|A|f|p. Moreover, we essentially partition the set of indices {1,,n} of all program variables into suitable sets 𝔇(𝔦,𝔬) with (𝔦,𝔬) for some finite set >02. We will then show that for all c{1,,m}, and all executions f with |f|A,|f|B1, the value (𝒱alx(f))c of the c-th constraint after executing f on x is

(𝒱alx(f))c=(𝔦,𝔬)(𝔦𝔬𝒰(f))|f|i𝔇(𝔦,𝔬)ζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x). (2)

Here, all ζi,𝐀,ζi,𝐁 are complex numbers of modulus 1, i.e., |ζi,𝐀|=|ζi,𝐁|=1 for all i{1,,n}, and the functions γc,i are linear maps n. The maps γc,i and the values ζi,𝐀,ζi,𝐁 only depend on the matrices 𝐂, 𝐀, and 𝐁, but not on the specific input x. While weaker requirements would suffice to ensure that (𝒱alx(f))c has some closed form, diagonalizability of 𝐀 and 𝐁 guarantees that it has the form (2), which is crucial for our procedure. Lemma 21 shows that by a lexicographic comparison of those (𝔦,𝔬) for which the inner sum in 2 is not 0 for all executions f, one can compute which of the pairs (𝔦,𝔬) is the “dominant” one. Here, a pair (𝔦,𝔬) is considered dominant whenever the value of the first factor (𝔦𝔬𝒰(f))|f| of 2 grows the fastest if the execution of f is continued (i.e., if |f|) and the corresponding inner sum is not 0 for all executions f. The dominant pair depends on the specific input x and on whether 𝒰(f) is 𝔭ositive or 𝔫egative, and correspondingly, one has to use different lexicographic comparisons to determine the dominant pair. For d{𝔫,𝔭}, let 𝔇d,c,x denote the set 𝔇(𝔦,𝔬) where the pair (𝔦,𝔬) is dominant for input x and c{1,,m} (and positive 𝒰(f) if d=𝔭 or negative 𝒰(f) for d=𝔫). Then, the sign of the “coefficient” v(f)=i𝔇d,c,xζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x) of the dominant pair eventually determines the sign of (𝒱alx(f))c, provided that |v(f)| is large enough (Lemma 23).

In Sect. 4, we consider the rearrangement

v(f)=id,c,xγc,i(x)=R+id,c,xζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x) (3)

where d,c,x={i𝔇d,c,xζi,𝐀=ζi,𝐁=1} and d,c,x={i𝔇d,c,x{ζi,𝐀,ζi,𝐁}{1}}.
Lemma 28 then gives a necessary condition for non-termination (and hence a sufficient condition for universal termination): If x is an eventually non-terminating input, then there must be a d{𝔫,𝔭} such that we have R>0 for all constraints c, with R as in 3.

Sect. 5 turns this necessary condition for non-termination into a sufficient condition. To that end, we define the set W of witnesses for eventual non-termination containing all inputs x𝒮n for which there is some d{𝔫,𝔭} such that R>id,c,x|γc,i(x)| holds for all constraints c. Lemma 31 shows that all xW are eventually non-terminating. While this condition is only sufficient for non-termination, we show in Lemma 32 that if the program is non-terminating, then there is also an input in W. So the considered program is non-terminating iff W, i.e., the program is universally terminating iff W= (Cor. 33).

Finally, our decision procedure for UPAST is presented in Sect. 6. Lemma 34 shows that W is semialgebraic and thus the emptiness problem is decidable over the real algebraic numbers, i.e., if 𝒮=𝔸. Moreover, Lemma 36 shows that W can be represented as a finite union of convex semialgebraic sets. Hence, emptiness of W can also be decided over the rationals and integers [20]. Cor. 39 shows how witnesses for non-termination can be obtained from eventually non-terminating inputs xW. We discuss our implementation in the tool SiRop in Sect. 7. For all proofs, we refer to [26]. Moreover, [26] also contains proof sketches for our main results to help understanding the essential proof ideas.

2 Programs & Termination

As usual, let ¯ denote the set of algebraic numbers, i.e., the set of all roots of (univariate) polynomials from [x]. As mentioned, 𝔸=¯ denotes the set of algebraic reals, and [n] denotes the set of positive natural numbers below and including n for every n with =>0{0}, i.e., [n]={1,,n1,n} for n1 and [0]=.

We now define our class of simple randomized loops. The program variables range over a semiring 𝒮𝔸 with a guard consisting of a conjunction of m strict linear inequations over the program variables, represented by a matrix 𝐂𝔸m×n, and two commuting linear updates 𝐀,𝐁𝒮n×n that are diagonalizable (over ). In each loop iteration, the applied update is chosen among 𝐀,𝐁 according to the outcome of a (possibly biased) coin toss.

It is well known (e.g., [17, Thm. 1.3.12]) that two matrices 𝐀,𝐁n×n for n are commuting and diagonalizable iff they are simultaneously diagonalizable, i.e., there is a regular matrix 𝐒n×n such that 𝐀=𝐒𝐀𝐃𝐒1 and 𝐁=𝐒𝐁𝐃𝐒1, where 𝐀𝐃=diag(a1,,an)n×n and 𝐁𝐃=diag(b1,,bn)n×n are complex diagonal matrices, and a1,,an and b1,,bn are the eigenvalues of 𝐀 and 𝐁, respectively. Moreover, if 𝐀,𝐁¯n×n are algebraic, then 𝐒,𝐀𝐃,𝐁𝐃¯n×n can also be chosen to be algebraic.

Definition 1 (Simple Randomized Loops).

Let m,n>0, 𝐂𝔸m×n, p[0,1]𝔸, and 𝐀,𝐁𝒮n×n such that 𝐀 and 𝐁 are simultaneously diagonalizable.333In the following, we assume 𝒮 and exclude the trivial semiring 𝒮={0} as every simple randomized loop terminates for the input 0. Then,

while 𝐂x>0:x𝐀xp𝐁x

is called a simple randomized loop (over 𝒮) of dimension n with m constraints. In the remainder, we will omit “simple randomized” and just refer to these programs as “loops”.

The meaning of “x𝐀xp𝐁x” is that x is updated to 𝐀x with probability p and to 𝐁x with probability 1p. The comparison 𝐂x>0 is understood componentwise, i.e., 𝐂x>0 iff (𝐂x)c>0 for all c[m], where (𝐂x)c is the c-th entry of the vector 𝐂x. To simplify the notation, from now on we will consider a fixed loop 𝒫 of dimension n with m constraints. Moreover, w.l.o.g. we assume p(0,1), i.e., p{0,1}, as otherwise one can set 𝐀 to 𝐁 if p=0 and 𝐁 to 𝐀 if p=1, and then replace p by an arbitrary number from (0,1).

A run 𝔯 of the loop 𝒫 is an infinite word 𝔯=𝔯1𝔯2{A,B}ω, where Runs denotes the set of all runs. For a run 𝔯=𝔯1, the value 𝔯i{A,B} indicates which of the updates 𝐀 or 𝐁 was used in the i-th iteration of the loop. Here, A and B are distinct markings even when 𝐀=𝐁. To simplify the notation, we introduce random variables 𝔯i:Runs{A,B} for i>0 that map a run to its i-th element. Note that all suchrandom variables 𝔯i are independent and identically distributed. A (finite) execution fk{A,B}k is a (possibly empty) prefix of a run. Let Path denote the (countable) set of all such finite executions. Given a finite execution f=𝔯1𝔯k with 𝔯i{A,B}, let |f|=k denote its length. Furthermore, for s{A,B}, |f|s=|{i[k]𝔯i=s}| denotes the number of performed updates with update matrix 𝐀 or 𝐁, respectively, during the execution of f.

Since the definition of runs is independent from the specific input of the loop, the semantics of the loop 𝒫 depend only on the value of p(0,1). To obtain a probability measure for 𝒫, one first considers cylinder sets Pref={f𝔯1𝔯2Runs𝔯i{A,B} for i1} for all fPath, i.e., Pref contains all runs with prefix f. By requiring (Pref)=p|f|A(1p)|f|B for all fPath, one obtains a (unique) probability measure :(0,1) on the σ-field generated by all cylinder sets Pref, see, e.g., [2, Thm. 2.7.2].

Definition 2 (Semantics of Loops).

The semantics of 𝒫 is given as a probability space (Runs,,) where =σ({PreffPath}) and (Pref)=p|f|A(1p)|f|B.

To capture the behavior of 𝒫 on some specific input x𝒮n, we introduce a function 𝒱alx:Path𝔸m that associates finite executions fPath with the values of 𝒫’s guard 𝐂x after the execution of f. Recall that 𝐀 and 𝐁 commute. Hence, we define

𝒱alx(𝔯1𝔯k)=𝐂(i=1k{𝐀if fi=A𝐁otherwise)x=𝐂𝐀|𝔯1𝔯k|A𝐁|𝔯1𝔯k|Bx

where for every matrix 𝐌n×n, 𝐌0 is the n-dimensional identity matrix.

Lemma 3 (Values of Constraints).

For any fPath and x=(x1,,xn)𝔸n we have

𝒱alx(f)=𝐂𝐒diag(a1|f|Ab1|f|B,,an|f|Abn|f|B)𝐒1x.
Example 4.

Consider the loop “while 𝐂x>0:x𝐀xp𝐁x” with

𝐂=(111)𝐀=(11589158716)𝐁=(7516175169712)𝐒=(ii3ii7111)𝐒1=(1+7i2013i201217i201+3i20121101100)

Then, for x𝔸3 and all fPath, 𝒱alx(f) equals

𝐂𝐒diag((68i)|f|A(12+16i)|f|B,(6+8i)|f|A(1216i)|f|B,  20|f|A10|f|B)𝐒1x.

The value of the c-th constraint after the execution of fPath on the initial value x𝔸n is given by the expression

(𝒱alx(f))c =(𝐂𝐀|f|A𝐁|f|Bx)c
=(𝐂𝐒𝐀𝐃|f|A𝐁𝐃|f|B𝐒1x)c (Lemma 3)
=i[n](𝐂𝐒)c,iai|f|Abi|f|B(𝐒1x)i
=i[n]ai|f|Abi|f|Bγc,i(x) (4)

for linear maps γc,i:𝔸n with

γc,i(x)=(𝐂𝐒)c,i(𝐒1x)i. (5)

Here, (𝐂𝐒)c,i denotes the entry of 𝐂𝐒 at row c and column i. Moreover, since 𝐂𝔸m×n, 𝐀,𝐁𝔸n×n, and x𝔸n, we have γc,i(x)¯ for all (c,i)[m]×[n].444This observation will be needed in the final SMT encoding for our decision procedure (see Lemma 34), as we have to encode the coefficients γc,i(x) for a given x𝔸n. In the following, we refer to the addends ai|f|Abi|f|Bγc,i(x) of the sum in 4 as constraint terms.

Example 5.

Reconsider Ex. 4. Then, (𝒱alx(f))1=i[3]ai|f|Abi|f|Bγ1,i(x) with eigenvalues a1=68i,a2=6+8i,a3=20,b1=12+16i,b2=1216i,b3=10, and

γ1,1(x) =(120+7i20)x1(120+3i20)x2+12x3
γ1,2(x) =(1207i20)x1(1203i20)x2+12x3
γ1,3(x) =1110x1+1110x2.
Corollary 6.

For all (c,i)[m]×[n] and all x,y𝔸n, due to the definition of γc,i we have γc,i(𝐀x+y)=aiγc,i(x)+γc,i(y) and γc,i(𝐁x+y)=biγc,i(x)+γc,i(y).

The following lemma shows that if one has two pairs of eigenvalues (a,b) and (a¯,b¯) where a¯ and b¯ are the complex conjugates of a and b, then the sum of all linear maps γc,i(x) where (ai,bi)=(a,b) is the complex conjugate of the sum of all linear maps γc,i(x) where (ai,bi)=(a¯,b¯). For instance in Ex. 5, (a1,b1) are the complex conjugates of (a2,b2) and indeed, we have γ1,2(x)=γ1,1(x)¯. This lemma will later be needed to show that when representing (𝒱alx(f))c and summing up the coefficients of its addends in a suitable way, all resulting coefficients are real numbers (see Remark 18).

Lemma 7 (Sums of Conjugated Constraint Terms are Real-Valued).

Let c[m], let a,b, and let γc,i be the linear map from 5. Then, for all inputs x𝔸n we have γ2=γ1¯ where

γ1=i[n],(ai,bi)=(a,b)γc,i(x)andγ2=i[n],(ai,bi)=(a¯,b¯)γc,i(x).

In order to define the notion of termination for 𝒫, we first introduce the concept of a run’s length by counting the number of iterations until the guard is violated for the first time. Throughout the paper, we use the convention min=.

Definition 8 (Length Of Runs).

For any x𝔸n, we define the random variable x:Runs{} as x(𝔯1𝔯2)=min{k𝒱alx(𝔯1𝔯k)0}.

We now define the expected runtime of 𝒫 for the input x as the expectation 𝔼(x). If 𝔼(x)=, we call the corresponding input x non-terminating. So we consider positive almost sure termination [6, 34], where termination corresponds to a finite expected runtime.

Definition 9 (Non-Terminating Inputs).

The set of non-terminating inputs is NT={x𝔸n𝔼(x)=}.

Consequently, we call 𝒫 terminating whenever NT= and non-terminating otherwise.

As in [7, 15, 16, 18, 31, 35], we focus on eventual non-termination instead of actual non-termination as this allows us to ignore a finite number of initial updates of the loop. In our setting, an input x is eventually non-terminating if a non-terminating input y can be reached by repeated application of the updates in the loop body to x.

Definition 10 (Eventual Non-Termination).

We define the set of eventually non-terminating inputs as ENT=j,k{x𝔸n𝐀j𝐁kxNT}.

The motivation behind considering ENT instead of NT is that it allows us to “jump” over the first iterations of the loop (where the loop guard might be violated). In this way, we can focus only on the longterm behavior of the loop on a given input.

Example 11 (Difference Between NT & ENT).

Consider the loop “while 𝐂x>0:x𝐀x0.5𝐁x” with 𝐂=(11) and 𝐀=𝐁=(2001). Then, x=(12)NT as 𝐂x=10, i.e., x violates the loop guard. While 𝐀x also violates the loop guard (since 𝐂𝐀x=00), we have 𝐂𝐀jx>0 for all j2. Thus, 𝐀2xNT and therefore, xENT.

Considering ENT instead of NT is justified by the fact that NT= iff ENT=, as shown by Lemma 12.

Lemma 12 (Correspondence of ENT & NT).

For any semiring 𝒮𝔸, we have NT𝒮n= iff ENT𝒮n=.

3 On Constraint Terms

In this section we consider the value 𝒱alx(f) for |f|, motivated by our interest in eventual non-termination. In the first part of this section, we represent (𝒱alx(f))c, for c[m], as a sum over so-called “constraint term groups”, expressed using a quantity 𝒰(f) corresponding to “how much f has deviated from the expected execution”. The section’s second part then shows that for specific fPath it suffices to only consider certain addends of this sum in order to decide whether (𝒱alx(f))c>0 as |f|. This observation will lead to a necessary condition for eventual non-termination in Sect. 4, which will subsequently be turned into a sufficient criterion in Sect. 5.

When executing the loop 𝒫, the relative number of times that update 𝐀 is selected over 𝐁 will intuitively tend towards p with increasing number of iterations. We now consider this relative quantity and additionally subtract p to center its distribution around 0.

Definition 13 (Deviation From Equilibrium).

The mapping 𝒰:Path[p,1p] is defined as 𝒰(f)=|f|A|f|p for every non-empty fPath and 𝒰(f)=0 otherwise.

We will investigate which addends determine the sign of (𝒱alx(f))c for |f|. To this end, we want to express the value of the constraint terms after some finite execution f in terms of 𝒰(f) and |f|. The advantage is that for sufficiently long paths f, we know how |f| and 𝒰(f) “behave” (i.e., |f| “tends towards” and 𝒰(f) is “expected to tend towards” 0).

Lemma 14 (Normal Form of Constraint Terms).

Let a|f|Ab|f|Bγ be a constraint term with a,b0. We write a,b in polar form as |a|ζ𝐀 and |b|ζ𝐁, respectively, where ζ𝐀=a|a|,ζ𝐁=b|b| are complex units, i.e., |ζ𝐀|=|ζ𝐁|=1. Then, for any (non-empty) finite execution fPath with 𝒰(f)(p,1p) we have

a|f|Ab|f|Bγ=ζ𝐀|f|Aζ𝐁|f|B(|a|p|b|p1(|a||b|)𝒰(f))|f|γ.

Note that in Lemma 14 we excluded all constraint terms with a=0 or b=0 in order to avoid a division by zero. However, we additionally required555The set of runs 𝔯 where 𝒰(f){p,1p} for every prefix f of 𝔯 has probability 0, as 𝒰(f){p,1p} means that only 𝐀 or 𝐁 has been selected in f. However, we had required 0<p<1. 𝒰(f){p,1p} implying 0<|f|A and 0<|f|B, since |f|>0. Hence, for all constraint terms a|f|Ab|f|Bγ with a=0 orb=0 and all considered fPath, we have a|f|Ab|f|B=0 and thus the value of such constraint terms can safely be ignored when computing (𝒱alx(f))c. This leads to the equation

(𝒱alx(f))c =i[n]ai|f|Abi|f|Bγc,i(x)
=i[n],ai,bi0ζi,𝐀|f|Aζi,𝐁|f|B(|ai|p|bi|p1(|ai||bi|)𝒰(f))|f|γc,i(x), (6)

where ζi,𝐀=ai|ai| and ζi,𝐁=bi|bi|.

Example 15.

Transforming the sum from Ex. 5 into the form 6 and setting p=12 yields

(𝒱alx(f))1= ζ1,𝐀|f|Aζ1,𝐁|f|B(102(12)𝒰(f))|f|γ1,1(x)+ζ2,𝐀|f|Aζ2,𝐁|f|B(102(12)𝒰(f))|f|γ1,2(x)
+ζ3,𝐀|f|Aζ3,𝐁|f|B(1022𝒰(f))|f|γ1,3(x)

with ζ1,𝐀=eiarctan(4/3),ζ2,𝐀=eiarctan4/3,ζ3,𝐀=1 and ζ1,𝐁=ei(πarctan(4/3)),ζ2,𝐁=ei(arctan(4/3)π),ζ3,𝐁=1 for all fPath.

By inspecting the right-hand side of 6 it becomes clear that the subexpressions |ai|p|bi|p1 and |ai||bi| govern the overall asymptotic growth of (𝒱alx(f))c as |f| increases. In the following, we group all constraint terms into so-called constraint term groups which are sets of indices i corresponding to constraint terms where |ai|p|bi|p1 and |ai||bi| have common values 𝔦 and 𝔬. Here, 𝔦=|ai|p|bi|p1 is the expression that is important in 6 if 𝒰(f) is in a region close to 0.666Note that ai|f|Abi|f|B is the |f|-th power of the weighted geometric mean aipbi1p whenever 𝒰(f)=0. If one is outside such a region, then 𝔬=|ai||bi| is important as well.

Definition 16 (Constraint Term Groups).

For any (𝔦,𝔬)>02, let

𝔇(𝔦,𝔬)={i[n]0{ai,bi},|ai|p|bi|p1=𝔦,|ai||bi|=𝔬}.

Moreover, we define the finite set of all pairs (𝔦,𝔬)>02 with 𝔇(𝔦,𝔬). For c[m] and x𝔸n, let 𝔇(𝔦,𝔬),c,x= whenever i𝔇(𝔦,𝔬)ζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x)=0 holds for all fPath.777We will show how to check this in Lemma 24. Note that this is not implied by i𝔇(𝔦,𝔬)γc,i(x)=0. As a counterexample, consider γ1=1, γ2=1 (and thus, γ1+γ2=0), and ζ1,𝐀=1, ζ1,𝐁=ζ2,𝐀=ζ2,𝐁=1 (and hence, ζ1,𝐀ζ1,𝐁γ1+ζ2,𝐀ζ2,𝐁γ2=2). Otherwise let 𝔇(𝔦,𝔬),c,x=𝔇(𝔦,𝔬). We refer to the sets 𝔇(𝔦,𝔬),c,x as constraint term groups.

For c[m] and non-empty fPath with 𝒰(f)(p,1p), 6 can be rearranged to

(𝒱alx(f))c=(𝔦,𝔬)(𝔦𝔬𝒰(f))|f|i𝔇(𝔦,𝔬),c,xζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x) (7)

with Def. 16.

Lemma 17 shows that for all a1,b1,a2,b20 we have |a1||b1|=|a2||b2| and |a1|p|b1|p1=|a2|p|b2|p1 iff |a1|=|a2| and |b1|=|b2|. Thus, if i,i𝔇(𝔦,𝔬), then |ai|=|ai| and |bi|=|bi|. We will first return to this result in Sect. 4, where we will use that for all i,i𝔇(𝔦,𝔬) with ai,bi,ai,bi>0 we have (ai,bi)=(ai,bi). Later on, we will revisit it in Sect. 5.

Lemma 17 (Equality of Eigenvalues).

Let a1,b1,a2,b2>0 be positive reals such that a1b1=a2b2 and a1pb1p1=a2pb2p1. Then we have (a1,b1)=(a2,b2).

Due to 7, we have to consider sums i𝔇(𝔦,𝔬),c,xζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x) for x𝔸n and c[m]. While ζi,𝐀, ζi,𝐁, and γc,i(x) are complex numbers in general, such sums are always real-valued. The corresponding Remark 18 is an immediate consequence of Lemma 7. Later, this remark will allow us to make statements about the signs of such sums, see Lemma 23.

 Remark 18 (Coefficients of Constraint Term Groups are Real-Valued).

Let c[m] and (𝔦,𝔬). Then, for all fPath we have i𝔇(𝔦,𝔬),c,xζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x)𝔸.

Example 19.

We continue Ex. 15. There are two different non-empty sets 𝔇(𝔦,𝔬), i.e., 𝔇(102,1/2)={1,2} and 𝔇(102,2)={3}. As indicated by Lemma 17, this implies |a1|=|a2| and |b1|=|b2|. Hence, ζ2,𝐀=a2|a2|=a2|a1|=a1|a1|¯=ζ1,𝐀¯ and similarly, ζ2,𝐁=ζ1,𝐁¯. Moreover, recall that γ1,2(x)=γ1,1(x)¯. Therefore, we have

𝔇(102,1/2),1,xthere is a fPath with ζ1,𝐀|f|Aζ1,𝐁|f|Bγ1,1(x)+ζ2,𝐀|f|Aζ2,𝐁|f|Bγ1,2(x)0there is a fPath with ζ1,𝐀|f|Aζ1,𝐁|f|Bγ1,1(x)+ζ1,𝐀¯|f|Aζ1,𝐁¯|f|Bγ1,1(x)¯0there is a fPath with ζ1,𝐀|f|Aζ1,𝐁|f|Bγ1,1(x)+ζ1,𝐀|f|Aζ1,𝐁|f|Bγ1,1(x)¯0there is a fPath with Re(ζ1,𝐀|f|Aζ1,𝐁|f|Bγ1,1(x))0there is a fPath with ζ1,𝐀|f|Aζ1,𝐁|f|Bγ1,1(x)0(by Remark 18)γ1,1(x)0

For the last step, the direction “” is clear, since γ1,1(x)=0 implies ζ1,𝐀|f|Aζ1,𝐁|f|Bγ1,1(x)=0 for all fPath. The direction “” is also clear by choosing f to be the empty path.

Hence, 𝔇(102,1/2),1,xγ1,1(x)010x3x1+x27x13x2. Similarly, 𝔇(102,2),1,xγ1,3(x)0x1+x20 for x𝔸3.

By inspecting  7 again, one observes that if 𝒰(f) is sufficiently close to 0, then the terms belonging to the non-empty constraint term group 𝔇(𝔦,𝔬),c,x with maximal (𝔦,𝔬) in the lexicographic ordering will at some point (when |f|) outgrow all other terms whenever 𝒰(f)|f|>0 is positive and sufficiently large. If 𝒰(f)|f|<0 is negative and sufficiently small, then, however, we have to focus our attention on the non-empty group 𝔇(𝔦,𝔬),c,x for which (𝔦,𝔬) is maximal in the lexicographic ordering.888Since 𝔦 and 𝔬 are always positive reals, the expressions i𝔇(𝔦,𝔬),c,xζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x) determine whether (𝒱alx(f))c is positive or negative. However, for |f|, this expression (and also 𝒰(f)) could alternate between being positive, negative, or even 0. This will be regarded in Sect. 4 and 5.

Definition 20 (Eventually Dominating Constraint Term Group).

Let lex,𝔭={((𝔦1,𝔬1),(𝔦2,𝔬2))𝔦1<𝔦2 or both 𝔦1=𝔦2 and 𝔬1𝔬2} denote the usual lexicographic ordering on and let lex,𝔫={((𝔦1,𝔬1),(𝔦2,𝔬2))𝔦1<𝔦2 or both 𝔦1=𝔦2 and 𝔬2𝔬1} be the lexicographic ordering where the comparison on the second component is flipped.

For all d{𝔫,𝔭}, c[m], and x𝔸n we define 𝔇d,c,x= if 𝔇(𝔦,𝔬),c,x= for all (𝔦,𝔬), and 𝔇d,c,x=𝔇(𝔦,𝔬),c,x for (𝔦,𝔬)=maxlex,d{(𝔦,𝔬)𝔇(𝔦,𝔬),c,x} otherwise, where maxlex,d denotes the maximum w.r.t. the ordering lex,d.

It is not a priori clear, how, for a constraint index c[m] and a given input x𝔸n, the sets 𝔇𝔫,c,x,𝔇𝔭,c,x can be computed automatically since one has to decide whether a1pb1p1<a2pb2p1 where a1,b1,a2,b2𝔸>0 are assumed to be positive algebraic reals, but aip and bip1 are in general non-algebraic reals. This is due to the well-known Gelfond-Schneider theorem (see, e.g., [29, Thm. 3.0.1]), which states that ap¯ whenever a¯{0,1} and p¯ is irrational. However, Lemma 21 ensures the decidability of such comparisons.

Lemma 21 (Comparing Constraint Term Groups).

Let T1=a1pb1p1 and T2=a2pb2p1 for positive algebraic reals a1,b1,a2,b2𝔸>0 and p𝔸(0,1). Then the statement T1<T2 is decidable.

Example 22.

Recall the two non-empty constraint term groups 𝔇(102,1/2),1,x and 𝔇(102,2),1,x from Ex. 19. Then, for x𝔸3, we have

𝔇𝔫,1,x ={𝔇(102,1/2)={1,2}if 10x3x1+x2 or 7x13x2𝔇(102,2)={3}if 10x3=x1+x27x1=3x2, and x1+x20otherwise
𝔇𝔭,1,x ={𝔇(102,2)={3}if x1+x20𝔇(102,1/2)={1,2}if x1+x2=0 and (10x3x1+x2 or 7x13x2)otherwise
Figure 1: Illustration of the General Idea Using Safe Regions.

Lemma 23 states the main property of the eventually dominating constraint term groups 𝔇𝔫,c,x and 𝔇𝔭,c,x. Fig. 1 depicts this lemma (and also the following lemmas) graphically. Here, the horizontal axis represents the length |f| of the path and the vertical axis represents the value of the function 𝒰(f)|f|=|f|Ap|f|, which expresses the deviation of the number of A-symbols in the execution f from the expected number of A-symbols. We depicted 𝒰(f)|f| by a gray line. The lemma essentially states that whenever 𝒰(f)|f| reaches one of the two “safe” regions marked in green, then the coefficient v(f) of the dominant addend determines the sign of (𝒱alx(f))c, provided that its absolute value |v(f)| is large enough. The upper safe region is the one for d=𝔭, i.e., here the path f is long enough (i.e., |f|l), 𝒰(f)[0,ε] (i.e., 0𝒰(f)|f|ε|f|), and 𝒰(f)|f|r. Similarly, the lower safe region corresponds to the case d=𝔫. This lemma also indicates why an extension of our approach to programs with three instead of two update matrices would be problematic. Then instead of 𝒰(f) we would need a vector to express how much an execution deviates from the probabilities in the program. This would break our concepts of eventually dominating constraint term groups and safe regions, since instead of d{𝔫,𝔭}, we would have to consider the “direction” of this deviation.

Lemma 23 (Domination of Eventually Dominating Constraint Term Groups).

Let c[m], d{𝔫,𝔭}, and x𝔸n. We define v:Path𝔸 (see Remark 18) as

v(f)=i𝔇d,c,xζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x)

Then for every ρ𝔸>0, there exist constants ε𝔸>0, r, and l>0 (for a bound on the length of the path), such that for all fPath with |f|l, |v(f)|ρ, |𝒰(f)|f||r, 𝒰(f)[ε,0] if d=𝔫, and 𝒰(f)[0,ε] if d=𝔭, we have sign((𝒱alx(f))c)=sign(v(f)).

4 Positive Eigenvalues

Recall that we are interested in sign((𝒱alx(f))c) as the execution progresses, i.e., for |f|. By Lemma 23, to this end we have to consider the sign of v(f)=i𝔇d,c,xζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x), where ζi,𝐀=ai|ai| and ζi,𝐁=bi|bi|. If both ai and bi are positive reals, then for |f|, the sign of ζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x) does not change. Thus, we now investigate i𝔇d,c,xζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x) restricted to all i𝔇d,c,x where ai or bi is not from 𝔸>0. In Lemma 24, we will show that this sum is either always 0 (for all paths f) or it becomes negative for large enough |f|.

Assume that for some constraint c[m], d{𝔫,𝔭}, and input x, some eigenvalue of each constraint term in the eventually dominating constraint term group is not positive real, i.e., for all i𝔇d,c,x one has ζi,𝐀1 or ζi,𝐁1. Then, the sign of the real part of this constraint term will change throughout the program’s execution (i.e., for |f|). Lemma 24 shows that if the sum of these constraint terms is not always 0, then irrespective of the updates that were already performed in previous iterations, this sum becomes smaller than some negative constant C after a number of further iterations. This is expressed in Lemma 24(b), where we have already performed j0 updates with the matrix 𝐀 and k0 updates with the matrix 𝐁. Then by extending the run long enough, the real part of the sum becomes smaller than a constant C that does not depend on j0 and k0. Our Lemma 24 is a generalization of a similar result by Braverman [7, Lemma 4] to products of orbits of complex units, i.e., to products of ζj for |ζ|=1.999Note that Lemma 24 allows us to check whether ζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x)=0 holds for all fPath, see Footnote 7. By Remark 18, the sum i𝔇(𝔦,𝔬)ζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x) is a real number and thus, in the case of (a), i𝔇(𝔦,𝔬)ζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x)=0 holds for all fPath. So given an actual input x, one just has to check the condition of Lemma 24(a). If that condition does not hold, then by Lemma 24(b), i𝔇(𝔦,𝔬)ζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x)=0 does not hold for every fPath.

Lemma 24 (Coefficients of Complex Eigenvalues Become Negative).

Let γ1,,γl be complex numbers and let ζ1,1,,ζ1,l,ζ2,1,,ζ2,l{z|z|=1} be complex units such that ζ1,i1 or ζ2,i1 for all i{1,,l}. For all j,k, let zj,k=i=1lζ1,ijζ2,ikγi. If all tuples (ζ1,i,ζ2,i) for i{1,,l} are pairwise different, then there exist constants C𝔸<0 and K such that we either have (a) or (b):

  1. (a)

    For all i[l] with γi0 there is some i[l] with ζ1,i=ζ1,i¯, ζ2,i=ζ2,i¯, and γi=γi¯, which implies Re(zj,k)=zj,k+zj,k¯2=0 for all j,k.

  2. (b)

    For all j0,k0 there exist j,k{0,,K} such that Re(zj0+j,k0+k)C and there are j,j,k,k such that zj,kzj,k.

Example 25.

To illustrate Lemma 24, we continue Ex. 22 and consider the constraint term group 𝔇(102,1/2)={1,2}. Let x𝔸3. According to Ex. 15, the coefficient of this constraint term group (for c=1) and fPath is

i𝔇(102,1/2)ζi,𝐀|f|Aζi,𝐁|f|Bγ1,i(x) =(eiarctan(4/3))|f|A(ei(πarctan(4/3)))|f|Bγ1,1(x)
+(eiarctan4/3)|f|A(ei(arctan(4/3)π))|f|Bγ1,2(x)

As already explored in Ex. 19, this coefficient is 0 for all fPath whenever γ1,1(x)=0γ1,1(x)=γ1,2(x)¯ which corresponds to Lemma 24(a). On the other hand, (b) statesthat whenever this is not the case, i.e., γ1,1(x)0, then there are constants C𝔸<0,K such that for every fPath there are f,f′′Path with |f|A,|f|BK where

0>Ci𝔇(102,1/2)ζi,𝐀|ff|Aζi,𝐁|ff|Bγ1,i(x)i𝔇(102,1/2)ζi,𝐀|ff′′|Aζi,𝐁|ff′′|Bγ1,i(x),

which ends our example to illustrate Lemma 24.

Let ={i[n]ζi,𝐀=ζi,𝐁=1} be the set of indices i such that both eigenvalues ai and bi are positive reals and let =[n]={i[n]ζi,𝐀1 or ζi,𝐁1} be the set where at least one of the eigenvalues is not a positive real. To simplify the notation we also denote 𝔇d,c,x and 𝔇d,c,x by d,c,x and d,c,x, respectively, for (d,c){𝔫,𝔭}×[m]. So for all id,c,x, we have ζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x)=γc,i(x), i.e., the sign of the corresponding addend does not change for |f|. For the other eigenvalues, by Lemma 24, id,c,xζi,𝐀|f|Aζi,𝐁|f|Bγc,i(x) is either always 0 (for all paths f) or it becomes negative for suitable |f|A and |f|B.

When executing the loop 𝒫 on input x, one expects that eventually (for |f|) the constraint term group 𝔇d,c,x for either d=𝔫 or d=𝔭 dominates the sign of constraint c[m] (Lemma 23). Whenever id,c,xγc,i(x)0 and 𝒰(f)|f| has reached one of the two “safe” regions marked in green in Fig. 1, by Lemma 24 one can extend the current path f by a path gf such that the coefficient v(fgf)=i𝔇d,c,xζi,𝐀|fgf|Aζi,𝐁|fgf|Bγc,i(x) of the dominating constraint term group is negative. Thus, the execution f can be extended by a path gf such that it leads to termination. This observation is captured in Lemma 26.

Lemma 26 (Finite Execution leading to Termination).

Let c[m], x𝔸n, and d{𝔫,𝔭}, such that id,c,xγc,i(x)0. Then there are constants ε𝔸>0, r,u, and l>0, such that for all fPath with |f|l, |𝒰(f)|f||r, 𝒰(f)[ε,0] if d=𝔫, and 𝒰(f)[0,ε] if d=𝔭, there is a finite execution gfPath of length |gf|u with (𝒱alx(fgf))c0.

Finally, Lemma 28 builds upon Lemma 26 and gives a sufficient criterion for termination of an input x𝔸n. The negation of this criterion is a necessary criterion for every input x𝔸n that is eventually non-terminating. This necessary criterion states that if x is eventuallynon-terminating, then for all constraints c, the sum id,c,xγc,i(x) of the addends for the “dual positive eigenvalues” (where both ai and bi are positive reals) must be positive.

So whenever id,c,xγc,i(x)0, Lemma 28 states that the expected number of steps until 𝒰(f)|f| reaches a “safe” (green) area in Fig. 1 and executes gf afterwards is finite. In other words, the expected number of steps 𝔼(x) until termination is finite.

Example 27.

To motivate Lemma 28 further, we continue Ex. 22. Let x𝔸3. We have ={3}, as only a3 and b3 are positive real eigenvalues.

First, suppose 𝔇𝔫,1,x𝔇(102,2)={3} and 𝔇𝔭,1,x𝔇(102,2)={3}. Then, for all d{𝔫,𝔭} we have d,1,x= and hence id,1,xγ1,i(x)=0. Thus, xENT by Lemma 28.

On the other hand, if for some d{𝔫,𝔭} we have 𝔇d,1,x=𝔇(102,2)={3}, i.e., d,1,x={3}, then Lemma 28 states that γ1,3(x)0 (i.e., 1110x1+1110x20) implies xENT. However, if γ1,3(x)>0, then Lemma 28 does not make any statement about whether xENT or xENT.101010Sect. 5 will show that in this case one indeed has xENT, see Ex. 30.

Lemma 28 (Dual Positive Eigenvalues for Eventually Dominating Constraints).

Let x𝔸n. If for every d{𝔫,𝔭} there is a c[m] with id,c,xγi(x)0, then xENT. Thus, if xENT, then there is some d{𝔫,𝔭} such that for all c[m] we have id,c,xγc,i(x)>0.

5 Towards Non-Termination Witnesses

Lemma 28 provides a necessary condition that must hold for all xENT. It requires that thesum of the addends γc,i(x) for all positive real eigenvalues ai,bi must be >0. This condition is however not sufficient for xENT. To turn this into a sufficient criterion, we now increase the lower bound 0. More precisely, we replace 0 by the sum of the addends |γc,i(x)| for all those eigenvalues where ai or bi are not a positive real number. In this way, we obtain a sufficient (but no longer necessary) criterion for ENT. To turn this into a sufficient and necessarycriterion, we then introduce a “boosting lemma” (Lemma 32), which states that if there is aninput in ENT, then there is also a (possibly different) input in ENT that satisfies our sufficient criterion. To prove this boosting lemma, we need the necessary condition of Lemma 28.

For our sufficient (but not necessary) condition for ENT, we define the set of witnesses for eventual non-termination as those inputs meeting this criterion.

Definition 29 (Witnesses for Eventual Non-Termination).

We define the set W=W𝔫W𝔭 of witnesses for eventual non-termination, where for d{𝔫,𝔭}, we have

Wd=c[m]{x𝔸nid,c,xγc,i(x)>id,c,x|γc,i(x)|}.

Note that the sum on the left-hand side in the definition of Wd is real-valued due to Lemma 7.

So Wd are all inputs x where for all constraints c, the sum of the dominating addends γc,i(x) for positive real eigenvalues ai,bi is greater than the sum of the |γc,i(x)| for the other eigenvalues ai,bi. Lemma 31 shows that the witness condition of Def. 29 is indeed a sufficient criterion for ENT. Before presenting this lemma, we will apply it to our running example.

Example 30.

We continue Ex. 27. Let x𝔸3 with γ1,3(x)>0 such that for some d{𝔫,𝔭} we have 𝔇d,1,x=𝔇(102,2). Then we have 𝔇d,1,x=d,1,xd,1,x={3} and thus id,1,xγ1,i(x)=γ1,3(x)>0=id,1,x|γ1,i(x)|. Hence, xWdW and thus by the following Lemma 31, we obtain xENT, answering the question from Ex. 27.

Hence, xENT iff γ1,3(x)>0 and there is d{𝔫,𝔭} with 𝔇d,1,x=𝔇(102,2). (The “only if” direction is due to Lemma 28, see Ex. 27.)

Lemma 31 (Witness Criterion is Sufficient for Eventual Non-Termination).

Let x𝔸n be a witness for eventual non-termination, i.e., xW. Then we have xENT, i.e., x is indeed an eventually non-terminating input.

Def. 29 introduced a set W𝔸n that, as shown by Lemma 31, under-approximates the set of eventually non-terminating inputs ENTW. While in general we may have ENTW, for the program from Ex. 30 we have ENT=W as for every d{𝔫,𝔭} one either has 𝔇d,1,x=d,1,x or 𝔇d,1,x=d,1,x. So here, xENT implies id,1,xγ1,i(x)>0d,1,xd,1,x= for some d{𝔫,𝔭} by Lemma 28 and hence id,1,xγ1,i(x)>id,1,x|γ1,i(x)|, i.e., xW.

As the set W is rather simple to characterize in contrast to ENT, our goal is to only check for the existence of some xW. This input then witnesses the eventual non-termination of the loop 𝒫. The following Lemma 32 establishes that whenever 𝒫 is eventually non-terminating, then such a witness xW does indeed exist. This then leads to our overall decision procedure, because we have that 𝒫 is non-terminating 𝒫 is eventually non-terminating W, see Cor. 33. In Sect. 6, we will show that emptiness of W is decidable (not only over the algebraic reals, but also over different sub-semirings 𝒮 of 𝔸 such as the naturals, integers, or rationals) and if W, then an element of W is computable.

The intuition behind Lemma 32 is as follows: Given an input xNT𝒮n, we want to construct a non-terminating input in W𝒮n. Recall that for any constraint c[m] and d{𝔫,𝔭}, the set d,c,x contains those indices i{1,,n} from the dominant constraint term group where the corresponding eigenvalues ai and bi of both update matrices 𝐀 and 𝐁 are positive reals. On the other hand, d,c,x contains the remaining indices from the dominant constraint term group. Moreover, the γc,i(x) help to determine the sign of the corresponding dominant pair’s coefficient. If xW, then

id,c,x|γc,i(x)|id,c,xγc,i(x) (8)

for some c[m]. One can now modify x to make 8’s left-hand side smaller. For every id,c,x, if ai is not positive real, then we multiply x by 𝐀, and otherwise by 𝐁. Since we have γc,i(𝐀x)=aiγc,i(x) by Cor. 6, this “shifts” the phase of at least γc,i(x) on the left-hand side, but not for the addends on the right-hand side of 8. By performing such multiplications repeatedly and taking a linear combination of the obtained inputs, 8’s left-hand side becomes arbitrarily small since addends “cancel out”, whereas this is not the case for the right-hand side. So one obtains a non-terminating input where 8 does not hold for any c[m]. Thus, xW.

Lemma 32 (Boosting).

Let 𝒫 be a non-terminating loop over 𝒮, i.e., 𝐀,𝐁𝒮n×n and NT𝒮n. Then there is a corresponding witness in W𝒮n.

The following corollary summarizes our results so far, i.e., it shows that non-termination is equivalent to the existence of an element in W.

Corollary 33 (Characterizing Termination).

A loop is terminating over a semiring 𝒮 iff W𝒮n=.

6 Deciding PAST

Finally, we present our novel technique for deciding whether a loop is (positively almost surely) terminating, i.e., whether its expected runtime is finite for every input. As discussed in Sect. 5, to this end we only have to show decidability of W for the set of witnesses W for eventual non-termination from Def. 29. We now explain how to translate this emptiness problem into an SMT problem. More precisely, we show that the witness set W is semialgebraic, i.e., it corresponds to a formula over polynomial arithmetic (which is linear in the variables x). For this we have to take into account that for different values of x, different addends may be eventually dominating. Then, decidability over the algebraic reals is clear.

As before, are those indices from [n] where both eigenvalues ai and bi are positive reals, and are the remaining indices.

Lemma 34 (Semialgebraic Sets of Witnesses for Algebraic Loops).

Let 𝐀,𝐁𝒮n×n, 𝐂𝔸m×n. We define the sets 𝒞c,(𝔦,𝔬)=0,𝒞c,(𝔦,𝔬)>0𝔸n as

𝒞c,(𝔦,𝔬)=0 ={x𝔸ni𝔇(𝔦,𝔬)γc,i(x)=0}i𝔇(𝔦,𝔬)Hi,(𝔦,𝔬)
Hi,(𝔦,𝔬) ={x𝔸nj𝔇(𝔦,𝔬)ζj,𝐀=ζi,𝐀ζj,𝐁=ζi,𝐁γc,j(x)+j𝔇(𝔦,𝔬)ζj,𝐀=ζi,𝐀¯ζj,𝐁=ζi,𝐁¯γc,j(x)¯=0}
𝒞c,(𝔦,𝔬)>0 ={x𝔸ni𝔇(𝔦,𝔬)γc,i(x)>i𝔇(𝔦,𝔬)|γc,i(x)|}

for all (c,(𝔦,𝔬))[m]×. Then, for all d{𝔫,𝔭} and 𝔠=((𝔦1,𝔬1),,(𝔦m,𝔬m))m we define Wd,𝔠Wd as

Wd,𝔠={xWd|c[m]𝔇d,c,x=𝔇(𝔦c,𝔬c),c,x}.

Then

Wd,𝔠=c[m](𝒞c,(𝔦c,𝔬c)>0(𝔦,𝔬)(𝔦,𝔬)>lex,d(𝔦c,𝔬c)𝒞c,(𝔦,𝔬)=0). (9)

Furthermore, we have W=W𝔫W𝔭=𝔠mW𝔫,𝔠𝔠mW𝔭,𝔠. The sets Wd,𝔠 and the set W are moreover semialgebraic.

Example 35.

We continue Ex. 30 and consider x=(110)T3. By Ex. 30 we have x𝒞1,(102,2)>0. Moreover, there is no (𝔦,𝔬)={(102,2),(102,1/2)} with (𝔦,𝔬)>lex,𝔭(102,2). For 𝔠=(102,2) this implies xW𝔭,𝔠W𝔭WENTNT.

Thus, the loop initially introduced in Ex. 4 is non-terminating for all 𝒮{,,𝔸}.111111We did not consider 𝒮{,0,𝔸0} as for such a choice of 𝒮 we do not have 𝐀,𝐁𝒮n×n.

Note that while for (𝔦,𝔬) the number 𝔦 is not necessarily algebraic, the representation of Wd𝔸n as a finite union/intersection of the semialgebraic sets 𝒞(𝔦,𝔬),c=0,𝒞(𝔦,𝔬),c>0 is still computable by Lemma 21 as one simply has to determine the corresponding ordering >lex,d on . This is the reason why we restricted p to the set of algebraic reals.

To show that emptiness of W is also decidable over various sub-semirings 𝒮 of the algebraic reals, we prove the convexity of the sets Wd,𝔠. Note that the set W as well as the sets W𝔫,W𝔭 themselves are in general not convex.

Lemma 36 (Wd as Finite Union of Convex Sets).

For d{𝔫,𝔭} and 𝔠m, the set Wd,𝔠 is convex, i.e., tx+(1t)yWd,𝔠 for all x,yWd,𝔠 and t(0,1).

Note that Lemmas 34 and 36 imply that for d{𝔫,𝔭} the set W𝔸n is semialgebraic and a finite union of convex sets.

Theorem 37 (Deciding PAST).

Let 𝒮{,,0,,𝔸0,𝔸}. Then, the question whether a loop is terminating on 𝒮n is decidable, and if the loop is non-terminating, then a witness xW𝒮n for eventual non-termination can be computed.

 Remark 38.

The theory of the reals and the algebraic reals are elementary equivalent as both are real closed fields. Thus, Thm. 37 directly entails that the question whether there exists a non-terminating non-negative real input x0n or real input xn for an algebraic loop 𝒫 is decidable as well, if one extends the set NT and the corresponding definitions to real inputs xn. Note that in this case NT iff NT𝔸n.

While the procedure outlined in the proof of Thm. 37 only allows for the computation of a witness xENT𝒮n for eventual non-termination, one can lift this to the computation of a witness yNT𝒮n according to the constructive proofs of Lemmas 23 and 31.

Corollary 39 (Computing Witnesses for Non-Termination).

Let 𝒮{,,0,,𝔸0,𝔸}. If a loop is non-terminating, then a witness for non-termination from NT𝒮n can be computed.

7 Implementation and Conclusion

Prototype Implementation.

To demonstrate the practical applicability of our decision procedure, we implemented it in our prototype tool SiRop (for “Simple Randomized Loops”). The tool and a corresponding collection of exemplary programs can be obtained from

The tool is implemented in Python and uses the SageMath open-source mathematics software system [33] in order to perform necessary computations such as simultaneous diagonalization and determining the mappings γc,i. SiRop tries to compute a witness xW for eventual non-termination by creating a corresponding SMT problem which is then solved using the SMT-RAT [11] solver. If the SMT problem is unsatisfiable, then the program is terminating. In contrast, if such a witness x is found, then the program is non-terminating and the tool computes a non-terminating input yNT from x. Currently, SiRop handles loops over the algebraic reals only, i.e., 𝒮=𝔸, as for all other considered sub-semirings of 𝔸, the decision procedure relies on the technique presented in [20] which (to the best of our knowledge) has not yet been implemented.

Conclusion.

We have shown the decidability of universal positive almost sure termination (UPAST) for the class of simple randomized loop ranging over numerous semirings 𝒮, thereby transferring a line of research started in 2004 by Tiwari [35] on universal termination of linear loops to the realm of randomized programs. To that end, we devised a corresponding decision procedure and presented a prototype implementation for the case 𝒮=𝔸, showing the practical applicability of the presented approach. In particular, our tool managed to find a non-terminating algebraic input for one121212https://github.com/TermCOMP/TPDB/blob/11.3/C_Integer/Stroeder_15/ChenFlurMukhopadhyay-SAS2012-Ex2.06_false-termination.c of the only two problems from the category C Integer which were not solved by any tool at the 2023 Termination Competition [14],131313This category was not part of the 2024 competition. the other one being the Collatz problem. While our tool only considers 𝒮=𝔸 (whereas the problem is formulated over the integers), the constraints generated by SiRop are unsatisfiable over , which implies universal termination of the program over the integers.

Future Work.

While our procedure can decide positive almost sure termination for all inputs, in the future we want to improve it such that it can also compute bounds on expected runtimes. Moreover, decision procedures for termination or complexity of subclasses of non-randomized programs (e.g., [12, 15, 16, 32]) have been integrated in (incomplete) tools that analyze general programs [22, 23], and we would like to investigate such an integration for randomized programs as well. Finally, we plan to adapt our approach to a decision procedure for universal almost sure termination (UAST), i.e., whether a program terminates with probability 1 on all inputs. Clearly, UPAST implies UAST but the converse does not hold in general.

References

  • [1] Sheshansh Agrawal, Krishnendu Chatterjee, and Petr Novotný. Lexicographic ranking supermartingales: An efficient approach to termination of probabilistic programs. Proc. ACM Program. Lang., 2(POPL):34:1–34:32, 2018. doi:10.1145/3158122.
  • [2] Robert B. Ash and Catherine A. Doléans-Dade. Probability and Measure Theory. Academic Press, 2nd ed. edition, 2000.
  • [3] Martin Avanzini, Georg Moser, and Michael Schaper. A modular cost analysis for probabilistic programs. Proc. ACM Program. Lang., 4(OOPSLA), 2020. doi:10.1145/3428240.
  • [4] Ezio Bartocci, Laura Kovács, and Miroslav Stankovic. Automatic generation of moment-based invariants for prob-solvable loops. In Proc. ATVA ’19, LNCS 11781, pages 255–276, 2019. doi:10.1007/978-3-030-31784-3_15.
  • [5] Kevin Batz, Mingshuai Chen, Sebastian Junges, Benjamin Lucien Kaminski, Joost-Pieter Katoen, and Christoph Matheja. Probabilistic program verification via inductive synthesis of inductive invariants. In Proc. TACAS ’23, LNCS 13994, pages 410–429, 2023. doi:10.1007/978-3-031-30820-8_25.
  • [6] Olivier Bournez and Florent Garnier. Proving positive almost-sure termination. In Proc. RTA ’05, LNCS 3467, pages 323–337, 2005. doi:10.1007/978-3-540-32033-3_24.
  • [7] Mark Braverman. Termination of integer linear programs. In Proc. CAV ’06, LNCS 4144, pages 372–385, 2006. doi:10.1007/11817963_34.
  • [8] Tomás Brázdil, Krishnendu Chatterjee, Antonín Kucera, Petr Novotný, and Dominik Velan. Deciding fast termination for probabilistic VASS with nondeterminism. In Proc. ATVA ’19, LNCS 11781, pages 462–478, 2019. doi:10.1007/978-3-030-31784-3_27.
  • [9] Aleksandar Chakarov and Sriram Sankaranarayanan. Probabilistic program analysis with martingales. In Proc. CAV ’13, LNCS 8044, pages 511–526, 2013. doi:10.1007/978-3-642-39799-8_34.
  • [10] Krishnendu Chatterjee, Hongfei Fu, and Amir Kafshdar Goharshady. Termination analysis of probabilistic programs through positivstellensatz’s. In Proc. CAV ’16, LNCS 9779, pages 3–22, 2016. doi:10.1007/978-3-319-41528-4_1.
  • [11] Florian Corzilius, Gereon Kremer, Sebastian Junges, Stefan Schupp, and Erika Ábrahám. SMT-RAT: an open source C++ toolbox for strategic and parallel SMT solving. In Proc. SAT ’15, LNCS 9340, pages 360–368, 2015. doi:10.1007/978-3-319-24318-4_26.
  • [12] Florian Frohn and Jürgen Giesl. Termination of triangular integer loops is decidable. In Proc. CAV ’19, LNCS 11562, pages 426–444, 2019. doi:10.1007/978-3-030-25543-5_24.
  • [13] Jürgen Giesl, Peter Giesl, and Marcel Hark. Computing expected runtimes for constant probability programs. In Proc. CADE ’19, LNCS 11716, pages 269–286, 2019. doi:10.1007/978-3-030-29436-6_16.
  • [14] Jürgen Giesl, Albert Rubio, Christian Sternagel, Johannes Waldmann, and Akihisa Yamada. The termination and complexity competition. In Proc. TACAS ’19, LNCS 11429, pages 156–166, 2019. Website of the Annual Termination Competition: https://termination-portal.org/wiki/Termination_Competition. doi:10.1007/978-3-030-17502-3_10.
  • [15] Marcel Hark, Florian Frohn, and Jürgen Giesl. Polynomial loops: Beyond termination. In Proc. LPAR ’20, EPiC 73, pages 279–297, 2020. doi:10.29007/nxv1.
  • [16] Marcel Hark, Florian Frohn, and Jürgen Giesl. Termination of triangular polynomial loops. Formal Methods in Syst. Des., 65(1), 2025. doi:10.1007/s10703-023-00440-z.
  • [17] Roger A. Horn and Charles R. Johnson. Matrix Analysis. Cambridge University Press, 2 edition, 2012. doi:10.1017/CBO9781139020411.
  • [18] Mehran Hosseini, Joël Ouaknine, and James Worrell. Termination of linear loops over the integers. In Proc. ICALP ’19, LIPIcs 132, 2019. doi:10.4230/LIPIcs.ICALP.2019.118.
  • [19] Benjamin Lucien Kaminski, Joost-Pieter Katoen, and Christoph Matheja. On the hardness of analyzing probabilistic programs. Acta Informatica, 56(3):255–285, 2019. doi:10.1007/S00236-018-0321-1.
  • [20] Leonid Khachiyan and Lorant Porkolab. Computing integral points in convex semi-algebraic sets. In Proc. FOCS ’97, pages 162–171, 1997. doi:10.1109/SFCS.1997.646105.
  • [21] Andrey Kofnov, Marcel Moosbrugger, Miroslav Stankovic, Ezio Bartocci, and Efstathia Bura. Exact and approximate moment derivation for probabilistic loops with non-polynomial assignments. ACM Trans. Model. Comput. Simul., 34(3):18:1–18:25, 2024. doi:10.1145/3641545.
  • [22] Nils Lommen and Jürgen Giesl. Targeting completeness: Using closed forms for size bounds of integer programs. In Proc. FroCoS ’23, LNCS 14279, pages 3–22, 2023. doi:10.1007/978-3-031-43369-6_1.
  • [23] Nils Lommen, Fabian Meyer, and Jürgen Giesl. Automatic complexity analysis of integer programs via triangular weakly non-linear loops. In Proc. IJCAR ’22, LNCS 13385, pages 734–754, 2022. doi:10.1007/978-3-031-10769-6_43.
  • [24] Éléanore Meyer, Jürgen Giesl, and Sophia Greiwe. SiRop. Software, DFG-Research Training Group 2236 UnRAVeL, swhId: swh:1:dir:3ca664cebef79bfeb95ec944ddc8441d3b528bf6 (visited on 2025-08-05). URL: https://github.com/aprove-developers/SiRop, doi:10.4230/artifacts.24333.
  • [25] Fabian Meyer, Marcel Hark, and Jürgen Giesl. Inferring expected runtimes of probabilistic integer programs using expected sizes. In Proc. TACAS ’21, LNCS 12651, pages 250–269, 2021. doi:10.1007/978-3-030-72016-2_14.
  • [26] Éléanore Meyer and Jürgen Giesl. Deciding termination of simple randomized loops, 2025. doi:10.48550/arXiv.2506.18541.
  • [27] Marcel Moosbrugger, Ezio Bartocci, Joost-Pieter Katoen, and Laura Kovács. The probabilistic termination tool Amber. Formal Methods Syst. Des., 61(1):90–109, 2022. doi:10.1007/S10703-023-00424-Z.
  • [28] Marcel Moosbrugger, Miroslav Stankovic, Ezio Bartocci, and Laura Kovács. This is the moment for probabilistic loops. Proc. ACM Program. Lang., 6(OOPSLA2):1497–1525, 2022. doi:10.1145/3563341.
  • [29] Saradha Natarajan and Ravindranathan Thangadurai. Pillars of Transcendental Number Theory. Springer, 2020. doi:10.1007/978-981-15-4155-1.
  • [30] Van Chan Ngo, Quentin Carbonneaux, and Jan Hoffmann. Bounded expectations: Resource analysis for probabilistic programs. In Proc. PLDI ’18, pages 496–512, 2018. doi:10.1145/3192366.3192394.
  • [31] Joël Ouaknine, João Sousa Pinto, and James Worrell. On termination of integer linear loops. In Proc. SODA ’15, pages 957–969, 2015. doi:10.1137/1.9781611973730.65.
  • [32] Enric Rodríguez-Carbonell and Deepak Kapur. Automatic generation of polynomial loop invariants: Algebraic foundations. In Proc. ISSAC ’04, pages 266–273, 2004. doi:10.1145/1005285.1005324.
  • [33] SageMath, the Sage Mathematics Software System (Version 10.3), 2024. https://www.sagemath.org.
  • [34] Nasser Saheb-Djahromi. Probabilistic LCF. In Proc. MFCS ’78, LNCS 64, pages 442–451, 1978. doi:10.1007/3-540-08921-7_92.
  • [35] Ashish Tiwari. Termination of linear programs. In Proc. CAV ’04, LNCS 3114, pages 70–82, 2004. doi:10.1007/978-3-540-27813-9_6.
  • [36] Di Wang, David M. Kahn, and Jan Hoffmann. Raising expectations: Automating expected cost analysis with types. Proc. ACM Program. Lang., 4(ICFP), 2020. doi:10.1145/3408992.