Reasoning about strings is a fundamental problem in computer science and mathematics. The full first order theory over strings and concatenation is undecidable. A seminal result by Makanin  (see also [12, 18]) shows that the satisfiability problem for the existential fragment is decidable, by showing an algorithm to check satisfiability of word equations. Precisely, a word equation consists of two words and over an alphabet of constants and variables. Such an equation is satisfiable if there is a mapping from the variables to strings over the constants such that and are syntactically identical.
An original motivation for studying word equations was to show undecidability of Hilbert’s 10th problem (see, e.g., ). While Makanin’s later result shows that word equations could not, by themselves, show undecidability, Matiyasevich in 1968 considered an extension of word equations with length constraints as a possible route to showing undecidability of Hilbert’s 10th problem . A length constraint constrains the solution of a word equation by requiring a linear relationship to hold on the lengths of words in a solution . For example, a length constraint might require that a solution maps variable and variable to words of the same length. The decidability of word equations with length constraints remains open.
In recent years, reasoning about strings with length constraints has found renewed interest through applications in program verification and reasoning about security vulnerabilities. The focus of most research has been on developing practical string solvers [34, 32, 6, 1, 24, 3, 14, 15, 19, 36, 35, 8, 17, 33]. These solvers are sound but make no claims of completeness. Relatively few results are known about the decidability status of strings with length and other constraints (see  for an overview of the results in this area). The main idea in most existing decidability results is the encoding of length constraints into Presburger arithmetic [16, 10]. However, the length abstraction of a word equation, that is, the set of possible lengths of variables in its solutions, need not be Presburger definable. (Indeed, this was Matiyasevich’s motivation in studying this problem as a way to prove undecidability of Hilbert’s 10th problem.)
In this paper, we consider the case of quadratic word equations, in which each variable can appear at most twice [22, 13], together with length and regularity constraints. For quadratic word equations, there is a simpler decision procedure (called the Nielsen transform or Levi’s method) based on a non-deterministic proof tree construction. The technique can be extended to handle regular constraints . However, we show that already for this class (even for a simple equation like , where are variables and are constants), the length abstraction need not be Presburger-definable. Thus, techniques based on Presburger encodings are not sufficient to prove decidability.
Our first observation in this paper is a connection between the problem of quadratic word equations with length constraints and a class of counter systems with Presburger transitions. Informally, the counter system has control states corresponding to the nodes of the proof tree constructed by Levi’s method, and a counter standing for the length each word variable. Each step of Levi’s method may decrease at most one counter. Thus, from any initial state, the counter system terminates. We show that the set of initial counter values which can lead to a successful leaf (i.e., one containing the trivial equation ) is precisely the length abstraction of the word equation.
Our second observation is that the reachability relation for a simple loop of the counter system can be encoded in the existential theory of Presburger arithmetic with divisibility . The encoding is non-trivial in the presence of regular constraints, and depends on structural results on semilinear sets. As is decidable [25, 21], we obtain a technique to symbolically represent the reachability relation for flat counter systems, in which each node belongs to at most one loop.
Moreover, the same encoding shows decidability for word equations with length constraints, provided the proof tree is associated with flat counter systems. In particular, we show that the class of regular-oriented word equations, introduced by , have flat proof trees. Thus, the satisfiability problem for quadratic regular-oriented word equations with length constraints is decidable (and in NEXP).111 In fact, the decision procedure is NP with an oracle access to a decision procedure for Presburger arithmetic with decidability. The best complexity bounds for the latter are NEXP and NP-hardness .
While our decidability result is for a simple subclass, this class is already non-trivial without length and regular constraints: satisfiability of regular-oriented word equations is NP-complete . Our result generalizes previous decidability results . Moreover, we believe that the techniques introduced in this paper, such as the connection between acceleration and word equations, and the use of existential Presburger with divisibility, can open the way to more sophisticated decision procedures or tools based on acceleration designed for counter systems.
General notation: Let be the set of all natural numbers. For integers , we use to denote the set of integers. If , let denote . We use to denote the component-wise ordering on , i.e., iff for all . If and , we write .
If is a set, we use to denote the set of all finite sequences over . The length of is . The empty sequence is denoted by . Notice that forms a monoid with the concatenation operator . If is a prefix of , we write . Additionally, if (i.e. a strict prefix of ), we write . Note that the operator is overloaded here, but the meaning should be clear from the context.
Words and Automata: We assume basic familiarity with word combinatorics and automata theory. Fix a (finite) alphabet . For each finite word , we write , where , to denote the segment . We write for the empty word.
Two words and are conjugates if there exist words and such that and . Equivalently, for some and for the cyclic permutationoperation , defined as , and for and .
Given a nondeterministic finite automaton (NFA) , a run of on is a function with that obeys the transition relation . We may also denote the run by the word over the alphabet . The run is said to be accepting if , in which case we say that the word is accepted by . The language of is the set of words in accepted by . In the sequel, for we will write to denote the NFA with initial state replaced by and final is replaced by .
Word equations: Let be a (finite) alphabet of constants and a set of variables; we assume . A word equation is an expression of the form , where . A system of word equations is a nonempty set of word equations. The length of a system of word equations is the length . A system is called quadratic if each variable occurs at most twice in all. A solution to a system of word equations is a homomorphism which maps each to itself that equates the l.h.s. and r.h.s. of each equation, i.e., for each .
For each variable , we shall use to denote a formal variable that stands for the length of variable . Let be the set . A length constraint is a formula in Presburger arithmetic whose free variables are in .
A solution to a system of word equations with a length constraint is a homomorphism which maps each to itself such that for each and moreover holds. That is, the homomorphism maps each variable to a word in such that each word equation is satisfied, and the lengths of these words satisfy the length constraint.
The satisfiability problem for word equations with length constraints asks, given a system of word equations and a length constraint, whether it has a solution.
We also consider the extension of the problem with regular constraints. For a system of word equations, a variable , and a regular language , a regular constraint imposes the additional restriction that any solution must satisfy . Given a system of word equations, a length constraint, and a set of regular constraints, the satisfiability problem asks if there is a solution satisfying the word equation, the length constraints, as well as the regular constraints.
In the sequel, for clarity of exposition, we restrict our discussion to a system consisting of a single word equation (w.l.o.g.).
Linear arithmetic with divisibility: Let be a first-order language with equality, with binary relation symbol , and with terms being linear polynomials with integer coefficients. We write , , etc., for terms in integer variables . Atomic formulas in Presburger arithmetic have the form or . The language of Presburger arithmetic with divisibility extends the language with a binary relation (for divides). An atomic formula has the form have the form or of , where and are linear polynomials with integer coefficients. The full first order theory of is undecidable, but the existential fragment is decidable [25, 21].
Note that the divisibility predicate is not expressible in Presburger arithmetic: a simple way to see this is that is not a semi-linear set.
Counter systems: In this paper, we specifically use the term “counter systems” to mean counter systems with Presburger transition relations (e.g. see ). These more general transition relations can be simulated by standard Minsky’s counter machines, but they are more useful for coming up with decidable subclasses of counter systems. A counter system is a tuple , where is a finite set of counters, is a finite set of control states, and is a finite set of transitions of the form , where and is a Presburger formula with free variables . A configuration of is a tuple .
The semantics of counter systems is given as a transition system. A transition system is a tuple , where is a set of configurations and is a binary relation over . A path in is a sequence of configurations .
A counter system generates the transition system , where is the set of all configurations of , and if there exists a transition such that is satisfiable.
3 Solving Quadratic Word Equations
We start by recalling a simple textbook recipe (called Nielsen transformation, a.k.a., Levi’s Method) [12, 22] for solving quadratic word equations, both for the cases with and without regular constraints. We then discuss the length abstractions of solutions to quadratic word equations, and provide several natural examples that are not Presburger-definable.
3.1 Nielsen transformation
We will define a rewriting relation between quadratic word equations . Let be an equation of the form with and . Then, there are several possible :
Rules for erasing an empty prefix variable. These rules can be applied if (symmetrically, ). In this case, we can nondeterministically guess that be the empty word . That is, is . The symmetric case of is similar.
Rules for removing a nonempty prefix. These rules are applicable if each of and is either a constant or a variable that we nondeterministically guess to be a nonempty word. There are several cases:
(syntactic equality). In this case, is .
and . In this case, is .
and . In this case, is .
. In this case, we nondeterministically guess if or . In the former case, the equation is . In the latter case, the equation is is .
Note that the transformation keeps an equation quadratic.
is solvable iff . Furthermore, checking if is solvable is in PSPACE.
See  for a proof. Roughly speaking, the proof uses the fact that each step either decreases the size of the equation, or the length of a length-minimal solution. It runs in PSPACE (in fact, linear space) because each rewriting does not increase the size of the equation.
3.2 Handling regular constraints
Nielsen transformation easily extends to quadratic word equations with regular constraints (e.g. see ). We assume that a regular constraint is given as an NFA representing . [If and are the initial and final states (respectively) of an NFA , we can be more explicit and write instead of .]
Our rewriting relation now works over a pair consisting of an equation and a set of regular constraints over variables in . Let be an equation of the form with and . We now define by extending the definition of without regular constraints. In particular, it has to be the case that and additionally do the following:
Rules for erasing an empty prefix variable . When applied, ensure that each regular constraint in satisfies . Define as minus all regular constraints of the form .
Rules for removing a nonempty prefix. For (P1), we have set to be minus all constraints of the form if is a variable. For (P2)–(P4), assume that is ; the other case is symmetric. For each regular constraint , we nondeterministically guess a state , and add and to . In the case when , we could immediately perform the check : a positive outcome implies removing this constraint from , while on a negative outcome our algorithm simply fails on this branch. For any variable that is distinct from , we add all regular constraints in to .
is solvable iff . Furthermore, checking if is solvable is in PSPACE.
Note that this is still a PSPACE algorithm because it never creates a new NFA or adds new states to existing NFA in the regular constraints, but rather adds a regular constraint to a variable , where is an NFA that is already in the regular constraint.
3.3 Generating all solutions using Nielsen transformation
One result that we will need in this paper is that Nielsen transformation is able to generate all solutions of quadratic word equations with regular constraints. To clarify this, we extend the definition of so that each a configuration or in the graph of is also annotated by an assignment of the variables to concrete strings. We write if and is the modification from according to the operation used to obtain from . That is, suppose that and and and . In this case, but since we have taken off the prefix from . This definition for the case with regular constraints is identical.
where has the empty domain iff is a solution of .
This proposition immediately follows from the proof of correctness of Nielsen transformation for quadratic word equations .
3.4 Length abstractions and semilinearity
Given a quadratic word equation with constants and variables , its length abstraction is defined as follows
namely the set of tuples of numbers corresponding to lengths of solutions to .
Consider the quadratic equation , where and contains at least two letters and . We will show that its length abstraction can be captured by the Presburger formula . Observe that each must satisfy by a length argument on . Conversely, we will show that each triple satisfying must be in . To this end, we will define a solution to such that . Consider . Let for some and . Let be a prefix of of length . Define . Therefore, is a prefix of . That is, for some , it is the case that . Letting suffices to make satisfy . Also, , which satisfies the formula . ∎
However, it turns out that Presburger Arithmetic is not sufficient for capturing length abstractions of quadratic word equations.
There is a quadratic word equation whose length abstraction is not Presburger-definable.
To this end, we show that the length abstraction of , where and , is not Presburger definable.
The length abstraction coincides with tuples of numbers satisfying the expression defined as:
Observe that this would imply non-Presburger-definability: for otherwise, since the first three disjuncts are Presburger-definable, the last disjunct would also be Presburger-definable, which is not the case since the property that two numbers are relatively prime is not Presburger-definable. Note however, that the expression is definable in existential Presburger arithmetic with divisibility.
Let us prove this lemma. Let . We first show that given any numbers satisfying , there are solutions to with for each . If they satisfy the first disjunct in (i.e. ), then set to an arbitrary word . If they satisfy the second disjunct, then and so set . The same goes with the third disjunct. For the fourth disjunct (assuming the first three disjuncts are false), let . Define so that for . It follows that .
We now prove the converse. So, we are given a solution to and let , . Assume to the contrary that is false and that and are the shortest such solutions. We have several cases to consider:
. Then, , contradicting that is false.
. Then, and so , which implies that . Contradicting that is false.
. Same as previous item and that .
. Since is false, we have . It cannot be the case that since then, comparing prefixes of , the letter at position would be on l.h.s. and on r.h.s., which is a contradiction. Therefore . Let , i.e., but with its prefix of length removed. By Nielsen transformation, we have . It cannot be the case that ; for, otherwise, implies and so , implying that 2 divides both and , contradicting that . Therefore, . Since , we have a shorter solution to , contradicting minimality.
. Same as previous item.
4 Reduction to Counter Systems
In this section, we will provide an algorithm for computing a counter system from , where is a quadratic word equation and is a set of regular constraints. We will first describe this algorithm for the case without regular constraints, after which we show the extension to the case with regular constraints.
Given the quadratic word equation , we show how to compute a counter system such that the following theorem holds.
The length abstraction of coincides with .
Before defining , we define some notation. Define the following formulas:
Note that the symbol in the guard of denotes syntactic equality (i.e. not equality in Preburger Arithmetic). We omit mention of the free variables and when they are clear from the context.
We now define the counter system. Given a quadratic word equation with constants and variables , we define a counter system as follows. The counters will be precisely all variables that appear in , i.e., . The control states are precisely all equations that can be rewritten from using Nielsen transformation, i.e., . The set is finite (at most exponential in ) as per our discussion in the previous section.
We now define the transition relation . We use to enumerate in some order. Given with , we then add the transition , where is defined as follows:
If applies a rule for erasing an empty prefix variable , then .
If applies a rule for removing a nonempty prefix:
If (P1) is applied, then .
If (P2) is applied, then .
If (P3) is applied, then .
If (P4) is applied and , then . If , then .
Observe that if , then and . In addition, if , then . This implies the following lemma.
The counter system terminates from every configuration .
Extension to the case with regular constraints:
In this extension, we will only need to assert that the counter values belong to the length abstractions of the regular constraints, which are effectively semilinear due to Parikh’s Theorem . Given a quadratic word equation with a set of regular constraints, we define the counter system as follows. Let . Let . Let be the finite set of all configurations reachable from , i.e., . Given , we add the transition as follows. Suppose that was added to by . Then,
The size of the NFA for is exponential in the number of constraints of the form in (of which there are polynomially many). The constraint is well-known to be effectively semilinear , and in fact we can compute using the algorithm of Chrobak-Martinez [9, 27, 30] in polynomial time two finite sets of integers and an integer such that, for each , is true iff . Note that is a fintie union of arithmetic progressions (with period 0 and/or ). In fact, each number (resp. the number ) is at most quadratic in the size of the NFA, and so it is a polynomial 222Note that we mean polynomial in the size of the NFA, which can be exponential in . size even when they are written in unary. Therefore, treating as an existential Presburger formula with one free variable (an existential quantifier is needed to guess the coefficient such that for some ), the resulting is a polynomial-sized existential Presburger formula.
The length abstraction of coincides with .
5 Decidability via Linear Arithmetic with Divisibility
5.1 Accelerating a 1-variable-reducing cycle
Consider a counter system with , and for some the transition relation consists of precisely the following transition , for each , such that is either (with a variable distinct from ) or . Such a counter system is said to be a 1-variable-reducing cycle.
There exists a polynomial-time algorithm which given a 1-variable-reducing cycle and two states computes an formula in existential Presburger with divisibility such that iff is satisfiable.
This lemma can be seen as a special case of the acceleration lemma for flat parametric counter automata  (where all variables other than are treated as parameters). However, its proof is in fact quite simple. Without loss of generality, we assume that and , for some . Any path can be decomposed into the cycle and the simple path of length . Therefore, the reachability relation can be expressed as
Thus, it suffices to show that is expressible in . Define a multiset of counter decrements as follows:
The number of the integer constant 1 can contain is defined as the number of such that .
For each variable , the number of times could appear in is defined as the number of such that .
For any variable/constant , we will write to denote the number of times appears in . Therefore, for some we have , or equivalently
The formula can be defined as follows:
Handling unary Presburger guards: Recalling our reduction for the case with regular constraints from Section 4 reveals that we also need unary Presburger guards on the counters. We will show how to extend our aforementioned acceleration lemma to handle such guards. As we will see shortly, we will need a bit of the theory of semilinear sets.
As before, our counter system has counters , and the control structure is a simple cycle of length , i.e., the transitions in are precisely for some Presburger formula , for each . We say that is 1-variable-reducing with unary Presburger guards if there exists a counter such that each is of the form , where is either (with a variable distinct from ) or , and is a conjunction of formulas of the form , where both and are finite sets of natural numbers and . For each counter , we use to denote the set of conjuncts in that refers to the counter .
There exists a polynomial-time algorithm which given a 1-variable-reducing cycle with unary Presburger guards and two states computes an formula in existential Presburger with divisibility such that iff is satisfiable.
Unlike Lemma 3, this lemma does not immediately follow from the results of  on flat parametric counter automata. To prove this, let us first take the formula from Lemma 3 applied to , which is obtained from by first removing the unary Presburger guards. We can insert these unary Presburger guards to , but this is not enough because we need to make sure that all “intermediate” values of have to also satisfy the Presburger guards corresponding to on that control state. More precisely, let the counter decrement in be (which can either be a variable distinct from or 1). For , we use to denote . Write for . Then, we can write
This is a correct expression that captures the reachability relation , but the problem is that it has a universal quantifier and therefore is not a formula of . To fix this problem, we will need to exploit the semilinear structure of unary Presburger guards. To this end, we first notice that, by taking the big conjunction over and the big conjuncton over out, the formula is equivalent to:
Therefore, it suffices to rewrite each conjunct as an existential Presburger formula, for each and constraint . To this end, let and let denote . We claim that
Simply put, we distinguish the cases when is “small” (i.e., less than the maximum threshold that can keep this number in an arithmetic progression with 0 period), and when this number is “big” (i.e. must be in an arithmetic progression with a nonzero period). To prove this equivalence, it suffices to show that if with and , then we can find such that . Suppose to the contrary that such does not exist. Then, since there are numbers in between and , by pigeonhole principle there is an arithmetic progression and two different numbers such that , for . Let . Note that denotes the difference between and , and this difference is of the form , for some positive integer . We now find a number with for some positive integer . Since for some , it must be the case that for , contradicting that .
We have proven correctness, and what remains is to analyse the size of the formula . To this end, it suffices to show that each formula is of polynomial size. This is in fact the case since there are at most polynomially many numbers in and and that the size of all numbers in are of polynomial size even when they are written in unary.
5.2 An extension to flat control structures and an acceleration scheme
There exists a polynomial-time algorithm which, given a flat Presburger counter system , each of whose simple cycle is 1-variable-reducing with unary Presburger guards and two states , computes an formula in existential Presburger with divisibility such that iff is satisfiable.
Indeed, to prove this theorem, we can simply use Lemma 4 to accelerate all cycles and the fact that transition relations expressed in existential Presburger with divisibility is closed under composition.
5.3 Application to word equations with length constraints
Theorem 5.1 gives rise to a simple and sound (but not complete) technique for solving quadratic word equations with length constraints: given a quadratic word equation with regular constraints, if the counter system is flat, each of whose simple cycle is 1-variable-reducing with unary Presburger guards, then apply the decision procedure from Theorem 5.1. In this section, we show completeness of this method for the class of regular-oriented word equations recently defined in , which can be extended with regular constraints given as 1-weak NFA . A word equation is regular if each variable occurs at most once on each side of the equation. Observe that is regular, but is not. It is easy to see that a regular word equation is quadratic. A word equation is said to be oriented if there is a total ordering on such that the occurrences of variables on each side of the equation preserve , i.e., if