Manuscript Offprint
arXiv:2610.40378On Deciding the Constraint Loop Problem in Dimension Two
Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.
Abstract
Recently, Guilmant, Lefaucheux, Ouaknine, and Worrell [2] proved decidability of the termination problem for linear constraint loops over two real-valued variables, thus settling a well-known open problem. Their algorithm is based on a theoretical result that such a loop does not terminate if and only if it has a witness of an infinite run of a specific shape. By analysing the structure of the witness carefully, we show that termination such loops can be decided in polynomial time, improving on the polynomial space bound argued by Guilmant et al. We also present an efficient implementation of the polynomial-time algorithm based on the satisfiability modulo theories (SMT) solver Z3.
1 Introduction
The problem of program verification is essential in modern software engineering. Ensuring that a program executes safely without getting stuck in infinite execution requires proving loop termination. In this work, we focus on termination for non-deterministic linear constraint loops, which has been extensively studied: see [1] for a recent survey. In this setting, both the loop guard and the loop update are specified as conjunctions of linear inequalities over program variables:
where are respectively the column vectors of program variables before and after the loop iteration. A, B are matrices with rational entries, a, b are vectors of rational numbers. By folding the guard condition into the update we can write every such loop as
The termination question (over ) is then equivalent to existence of an infinite sequence of vectors of real numbers such that for all .
Given as above, we define
.
This is a report of the results produced by the first author while she was a research intern at the MPI-SWS, supervised by the second author.
The set contains all information about the loop. When , i.e. the program has two variables and , recently [2] gave an algorithm for deciding termination. Their argument is that the loop does not terminate if and only if there exists a witness as below; from the witness it is then easy to extract an explicit infinite trajectory. A witness consists of a linear map , a closed convex cone , and vectors satisfying four conditions:
•
•
•
•
Here
is the recession cone of K. The witnessing infinite trajectory is then [2, Prop. 27]
So we have the partial sums of the geometric sequence translated by ; this form of a witnessing infinite trajectory was already known in [3].
The original goal of this project was to implement an automated verification tool in Python using the Z3 SMT solver to search for such non-termination witnesses. However, we quickly realised that the naive translation of the requirements into first-order logic requires quantifier alternations (albit with a fixed number of variables) that Z3 cannot handle. In fact, Guilmant et al. note the complexity of checking existence of a witness as PSPACE, which comes from the aforementioned naive translation. We therefore first show how to decide existence of a witness in polynomial time, and then implement that in Z3. The implemented tool is available at loop-termination.mpi-sws.org.
Firstly, let us consider witnesses where for two boundary vectors and that may be zero: in this case is either a two-dimensional pointed cone, a ray, a line, or a singleton.
2.1 Encoding the witness conditions
The program formulates four sets of constraints corresponding to the four witness conditions of [2]. To get polynomial time, what needs to be done is to not go for the direct transcription of the property into first-order logic (which may involve quantifier alternations), and instead use a mathematical argument.
- . The generator vectors and transformed by matrix must remain inside :
To see that this is valid, suppose and for . Take a vector where . Then
Since , , we have that .
- . To eliminate the universal quantifier, we only check that the generators of satisfy the recession cone condition:
This is justified as follows. Assume the condition for and . Take with . Then, applying Lem. 1,
From Lem. 1 it follows that .
-
. This does not require any quantifiers, and can be written in the straightforward way as a Z3 formula. This condition enforces that state v satisfies the loop guard and transition obeys loop constraints.
-
. This enforces that the step vector falls within the cone , and can be encoded as
The global formulas to be checked are then of the form where is a quantifier-free formula containing the usual logical operations as well as and belongs_to_C. Next we will show how to implement these using quantifier-free Z3 formulas (that use non-linear arithmetic).
2.2 Testing membership in a given cone and the recession cone
First we show how to check using no quantifiers and just linear operations.
Lemma 1. Assuming , we have that if and only if where works entrywise on vectors.
Proof. Suppose . Take and . Then
and hence .
Now suppose . Let be a row of , viewed as a column vector, and be the corresponding entry of . We have to show that . Take any . By assumption for any we have that . This cannot hold if .
Let us move on to defining belongs_to_C. A vector belongs to the cone C generated by s, t if it can be represented as
Solving this system for and using Cramer's rule yields
To avoid division inside Z3 constraints, we clear the denominator . Since the order of the generators is arbitrary (recall that both and will be existentially quantified), we enforce without loss of generality.
Case . In this case is equivalent to
Case . Vectors and are collinear. We check whether lies directly on the ray defined by and .
Thus in Z3, belongs_to_C(x, s, t) is formulated as
where on_ray(x, y) (meaning x belongs to the ray generated by y) for is
which belongs to the non-linear real arithmetic fragment. Note that the formula is quantifier-free.
2.3 Different type of witness check
The witness includes two vectors that form a cone. However, the two vector cone might not be necessary for working witness. The cone could have simpler form. The program would have a shorter running time if additional information was known about s and t.
The program does a check to find a witness with C in form of point, ray, line, cone. To ensure the result is a cone of the required form, the additional constraints are added to the solver, on top of witness constraints:
• point:
• ray:
• line:
- non-degenerate two-dimensional cone: , , ,
These checks ensure that the cone would have criteria for required form, but also would not be of simpler form than required.
2.4 The case of a half-plane C
A separate type of possible cone is a half-plane, which is generated by three rather than two vectors. However, the three vectors generating such a cone can be taken from the restricted family : note that the latter is orthogonal to . The algorithm had to be changed to properly check different parts of witness for the half-plane cone.
- . The generator vectors , and transformed by matrix must remain inside , therefore they have to be inside one of cones generated by and or and :
- . As argued above, it suffices to reason only about the three generators:
-
. Same as before.
-
. This time we have
since the cone generated by s, t, u is the union of the two cones generated by s, u and u, t.
2.5 Prechecks
To increase the program run speed, before running the witness check, program checks if the polytope formed by constraints is not empty and is not bounded. These are done using ordinary linear programming. In case of the polytope being empty no further checks are needed, as the loop would terminate after one iteration for any initial value. When the polytope is bounded, the loop does not terminate if and only if it has a fixed point [2, Prop. 26].
3 Our implementation
The program is implemented in Python using the SMT solver library z3-solver. The exact linear programming on Python integers (which do not have a size restriction) as well as Fractions is performed using pycddlib. Table 1 shows the running time of our program as a function of the number of constraints: the indicates one standard deviation of the distribution of running times. In all experiments, A had entries of absolute value 100, and the constraints were generated randomly but in a controlled way so that K was unbounded and non-empty: otherwise the pre-checks would have stopped the execution. For each number of constraints, 300 instances were constructed. We see that our program is able to handle a very large number of constraints, even if it takes up to a minute.
The code can be found in https://github.com/DariaGoliasova/loop-termination
The online interface loop-termination.mpi-sws.org was implemented using Flask in Python. Based on the data from our experiment, it has a timeout limit of 10 seconds.
| Number of constraints | Approximate time (s) |
|---|---|
| 100 | |
| 1000 | |
| 10000 |
Table 1: Approximate execution time by number of constraints
4 Open problems
A natural question that arises from checking witnesses with simpler "cones" is whether loops sharing a specific solution type—a point, ray, line, half-plane, full-plane or no simpler solution—possess common underlying characteristics. Furthermore, is there a method to determine which type of solution exists for a given loop a priori, without having to construct it explicitly?
A related challenge concerns how the algorithm behaves in higher dimensions. Developing more efficient search or verification strategies for higher dimensions remains an important direction for future work: currently there is no theoretical result for three variables, and even checking membership in a cone generated by many extremal vectors (in 3D, unlike in 2D, a cone need not be generated by a fixed number of vectors) already causes implementation difficulties.
References
[1] Amir M. Ben-Amram, Samir Genaim, Joël Ouaknine, and James Worrell. Termination analysis of linear-constraint programs. Foundations and Trends in Programming Languages, 10(1-2):1–145, 2026.
[2] Quentin Guilmant, Engel Lefaucheux, Joël Ouaknine, and James Worrell. The 2-Dimensional Constraint Loop Problem Is Decidable. In Karl Bringmann, Martin Grohe, Gabriele Puppis, and Ola Svensson, editors, 51st International Colloquium on Automata, Languages, and Programming (ICALP 2024), volume 297 of Leibniz International Proceedings in Informatics (LIPICS), pages 140:1–140:21, Dagstuhl, Germany, 2024. Schloss Dagstuhl – Leibniz-Zentrum für Informatik.
[3] Jan Leike and Matthias Heizmann. Geometric nontermination arguments. In International Conference on Tools and Algorithms for the Construction and Analysis of Systems, pages 266–283. Springer, 2018.
[4] James Renegar. On the computational complexity and geometry of the first-order theory of the reals. Part I: Introduction. Preliminaries. The geometry of semi-algebraic sets. The decision problem for the existential theory of the reals. Journal of symbolic computation, 13(3):255–299, 1992.