{'fraction_non_alphanumeric': 0.06149262852341303, 'fraction_numerical': 0.0151758205382833, 'mean_word_length': 3.602601664127505, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Monadic Decomposability of Regular Relations

Monadic decomposibility --- the ability to determine whether a formula in a given logical theory can be decomposed into a boolean combination of monadic formulas --- is a powerful tool for devising a decision procedure for a given logical theory. In this paper, we revisit a classical decision problem in automata theory: given a regular (a.k.a. synchronized rational) relation, determine whether it is recognizable, i.e., it has a monadic decomposition (i.e. a representation as a boolean combination of regular languages). Regular relations are expressive formalisms which, using an appropriate string encoding, can capture relations definable in Presburger Arithmetic. In fact, their expressive power coincide with relations definable in a universal automatic structure; equivalently, those definable by finite set interpretations in WS1S (Weak Second Order Theory of One Successor). Determining whether a regular relation admits a recognizable relation was known to be decidable (and in exponential time for binary relations), but its precise complexity still hitherto remains open. Our main contribution is to fully settle the complexity of this decision problem by developing new techniques employing infinite Ramsey theory. The complexity for DFA (resp. NFA) representations of regular relations is shown to be NLOGSPACE-complete (resp. PSPACE-complete).

a given n-ary relation R ⊆ U n are sufficiently independent from (i.e. not tightly coupled, or interdependent, with) each other. Some examples are in order. Given two subsets X, Y ⊆ U , then X × Y is an instance of relations whose two components are completely independent from each other. On the other hand, the equality relation {(x, x) : x ∈ U } is an example of relations whose two components are tightly coupled. In this paper, we will adopt the commonly studied notion of component-independence 1 (e.g. [25,38,6,37]) in a relation R ⊆ U n that lies between the extremes as exemplified in the above examples, i.e., that R is expressible as a finite union r i=1 X i,1 × · · · × X i,n of products, where each X i,j is expressible in the same language L (e.g. a logic or a machine model) wherein R is expressed.
Why should one care about monadic decomposable relations? The main reason is that applying appropriate monadic restrictions could make an undecidable problem decidable, and in general turn a difficult problem into one more amenable to analysis. Several examples are in order. Firstly, the well-known cartesian abstractions in abstract interpretation [16] overapproximate the set R ⊆ U n of reachable states at a certain program point by a relation R ⊆ X 1 × · · · × X m such that R ⊆ R . Having R instead of R sometimes allows a static analysis tool to prove correctness properties about a program that is otherwise difficult to do with only R. Another example includes restrictions to monadic predicates in undecidable logics that result in decidability, e.g., monadic first-order logic and extensions ( [8,9,4]), as well as monadic second-order theory of successors [9]. Monadic decomposability also found applications in more efficient variable elimination in constraint logic programming (e.g. [22]), as well as constraint processing algorithms for constraint database queries (e.g. [25,24]). Finally, monadic decompositions in the context of SMT (Satisfiability Modulo Theories), whose study was recently initiated in [38], have numerous applications, including constraint solving over strings [38,13].
The focus of this paper is to revisit a classical problem of determining monadic decomposability of regular relations, which are also known as synchronized rational relations [19,5,7]. The study of classes of relations over words definable by different classes of multi-tape (finite) automata is by now a well-established subfield of formal language theory. This study was initiated by Elgot, Mezei, and Nivat in the 1960s [17,30]; also see the surveys [6,14]. In particular, we have a strict hierarchy of classes of relations as follows: recognizable relations, synchronized rational relations, deterministic rational relations, and rational relations. All these classes over unary relations (i.e. languages) coincide with the class of regular languages. Rational relations are relations R ⊆ (Σ * ) n definable by multi-tape automata, where the tape heads move from left to right (in the usual way for finite automata) but possibly at different speeds (e.g. in a transition, the first head could stay at the same position, whereas the second head moves to the right by one position). Deterministic rational relations are simply those rational relations that can be described by deterministic multi-tape automata. So far, the heads of the tapes can move at different speeds. Regular relations (a.k.a. synchronized rational relations) are those relations that are definable by multi-tape automata, all of whose heads move to the right in each transition. Unlike (non)deterministic rational relations, regular relations are extremely well-behaved, e.g., they are closed under first-order operations and, therefore, have decidable first-order theories [21]. Regular relations are also known to coincide with those relations that are first-order definable over a universal automatic structure [5,7]; equivalently, those relations that are definable by finite-set interpretations in the weak-monadic theory of one successor (WS1S) [15]. Finally, the weakest class of relations in the hierarchy are recognizable relations: those relations that are definable as a finite union of products of regular languages or, equivalently, relations that can be defined as a boolean combination of regular constraints (i.e. atomic formulas of the form x ∈ L, where L is a regular language, asserting that the word x is in L). Recognizable relations are, therefore, those relations definable by multi-tape automata that exhibit monadic decomposability.
One of the earliest results on deciding whether a relation is monadic decomposable follows from Stearns in 1967 [33] and the characterization of a binary relation is the mirror image of u. In [11] it was proven that L R is a regular language if and only if R has a monadic decomposition and if R is a deterministic rational relation, then L R is a deterministic context-free language. Due to this characterization, Stearns's result implies that whether a deterministic n-ary rational relation is monadic decomposable (i.e. recognizable) is decidable in the case when n = 2. Shortly thereafter, Fischer and Rosenberg [18] showed that the same problem is unfortunately undecidable for the full class of binary rational relations. A few years later Valiant [37] improved the upper bound complexity for the case solved by Stearns to double exponential-time. This is still the best known upper bound for the monadic decomposability problem for deterministic binary rational relations to date and, furthermore, no specific lower bounds are known. More recently Carton et al. [11] adapted the techniques from [33,37] to show that this decidability extends to general n-ary relations, though no complexity analysis was provided. The problem of monadic decomposability for regular relations has also been studied in the literature. Of course decidability with a double exponential-time upper bound for the binary case follows from [37]. In 2000 Libkin [25] gave general conditions for monadic decomposability for first-order theories, which easily implies decidability for monadic decomposability for general k-ary regular relations. This is because regular relations are simply those relations that are definable in a universal automatic structures [5,7]. The result of Libkin was not widely known in the automata theory community and in fact the problem was posed as an open problem in French version of [31] in 2003 and later on, Carton et al. [11] provided a double-exponential-time algorithm for deciding whether an n-ary regular relation is monadic decomposable. More precisely, even though it was claimed in the paper that the algorithm runs in single-exponential time, it was noted in a recent paper by Löding and Spinrath [27,28] (which the authors of [11] also agreed, as claimed in [28]) that the algorithm actually runs in double-exponential time. Löding and Spinrath [27,28] gave a single-exponential-time algorithm (inspired by techniques from [37]) for monadic decomposability of binary regular relations.

Contributions
In this paper we provide the precise complexity of monadic decomposability of regular relations, closing the open questions left by Carton et al. [11] and Löding and Spinrath [27,28]. In particular, we show the following.

Theorem 1. Deciding whether a given regular relation R is monadic decomposable is
The lower bounds hold already for binary relations (Lemma 5 and Lemma 6 in Section 3). To prove the upper bounds, we first prove the upper bounds for binary relations (Lemma 12 in Section 4) and then extend them to n-ary relations for any given n > 2 (Lemma 13 in Section 5).
The existing proof techniques (e.g. in [11,28,25]) for deciding monadic decomposability typically aim for finding proofs that the relations are monadic decomposable. In contrast, our proof technique relies on finding a proof that a relation is not monadic decomposable. As a brief illustration, suppose we want to show that the regular relation R = {(v, v) : v ∈ Σ * } is not monadic decomposable. We define an equivalence relation ∼ ⊆ Σ * × Σ * as This relation is regular since regular relations are closed under first-order operations (a fact that was also used in [11]), but the size of the automaton for this relation is unfortunately quite large. Therefore, we will only use the complement ∼, which has a substantially smaller representation: polynomial (resp. exponential) size if R is given as a DFA (resp. an NFA). Now, that R is not monadic decomposable amounts to the existence of an ω-sequence σ = {v i } i∈N of words such that v i ∼ v j for each pair i, j ∈ N. By applying the pigeonhole principle and König's lemma, we will first construct a nicer sequence α (see the top half of Figure 2) and then by exploiting Ramsey Theorem over infinite graphs, we will show that there is an even nicer sequence α (see the bottom half of Figure 2), where the automaton for ∼ synchronizes its states in particular points of the computation, no matter which pair of words from the sequence is being read. Moreover, we prove that one of the synchronizing states has a pumping property. This leads to our NLOGSPACE algorithm as we can guess the synchronizing states and verify that there is an accepting run that can be pumped. This technique was inspired by a technique for proving recurrent reachability in regular model checking [34,35].
The exponential-time upper bound for the binary case from Löding and Spinrath [28] (which is inspired by the techniques used by Stearns [33] and Valiant [37]) relied on characterization of a relation R using the language L R = {rev(u)#v | (u, v) ∈ R} and used a suitable machinery that is able to decide whether L R is regular or not. Their result is not easily extensible to n-ary relations as the encoding of a binary rational relation as a context-free language L R does not generalize to n-ary relations. In Section 5, we show that proving monadic decomposability for an n-ary regular relation is LOGSPACE-reducible to testing whether linearly many induced binary relations are monadic decomposable.
We conclude in Section 6 with some perspectives from formal verification and a future research direction.

Preliminaries
A finite alphabet is denoted by Σ and the free monoid it generates by Σ * . That is, Σ * consists of all finite words over Σ. The empty word is ε. We denote by |w| the length of word w ∈ Σ * . We have that |ε| = 0. The word u ∈ Σ * is a prefix of w ∈ Σ * if w = uv for some v ∈ Σ * . We denote this by u ≤ w. We also write v = u −1 w, when u is a prefix of w, to state that v is the suffix of w that is obtained after prefix u is removed. Sometimes we want to consider a suffix of w after a prefix of particular length is removed without specifying the actual prefix as defined above. To this end, we define σ : Similarly, we define τ : Σ * × N → Σ * such that τ (w, i) = u, where |u| = i and u ≤ w.
In this paper we study relations R ⊆ Σ * × · · · × Σ * with particular structural properties. Namely, monadic decomposable relations that are a finite union of direct products of regular languages, and regular relations defined by n-tape finite automata, where the heads move in synchronized manner. See, for example, [31] for more details on such relations.

Definition 2.
A subset R ⊆ Σ × · · · × Σ is a monadic decomposable relation iff it is of the form m i=1 (X 1,i × · · · × X n,i ), where m is finite and each X j,i ⊆ Σ * is a regular language.
As mentioned earlier, this can be intuitively seen as the components of R being independent in some sense. Note that in the literature, monadic decomposable relations are sometimes called recognizable. The monadic decomposable relations can be defined using multi-tape automata as is done, e.g., in [11]. The above definition is more suitable for our considerations.
Let ⊥ be a fresh symbol not found in Σ. We use it to pad words in a relation R ⊆ Σ * × · · · × Σ * in order for each component to be of the same length. Formally, a tuple (w 1 , . . . , w n ) is transformed into (w 1 ⊥ 1 , . . . , w n ⊥ n ), where i = −|w i | + max 1≤j≤n |w j | for each i = 1, . . . , n. We extend this to the relation R ⊥ in the expected way. We also denote Σ ∪ {⊥} by Σ ⊥ . An n-tape automaton over alphabet Σ ⊥ is a tuple (Q, → A , q 0 , F ), where Q is the finite set of states, q 0 is the initial state, F is the set of final states, and

Definition 3. An n-ary relation
That is, in a regular relation the n heads of the automaton are moving in synchronized manner and the n-tuple of symbols seen determines the state transition. Naturally, the state transition can be deterministic or non-deterministic. We say that a regular relation is defined by an NFA if the underlying n-tape automaton is non-deterministic, otherwise we say that the relation is defined by a DFA. Note that in the literature, regular relations are sometimes called synchronous rational or automatic relations.

Lemma 4 ([11]
). The n-ary regular relation R is monadic decomposable if and only if the equivalence relation ∼ j has finite index for each j = 1, . . . , n − 1. That is, there are finitely many equivalence classes over ∼ j .
In other words, R is not monadic decomposable iff there is an infinite sequence {u i } i≥0 such that for each 0 ≤ i < it is the case that u i = u and u i ∼ j u , for some j = 1, . . . , n − 1.
In Section 4, we focus on binary relations and we can simplify the notation as there is only one possible value of j. We write ∼ instead of ∼ j and R ∼ for the binary regular relation defined as That is, R ∼ consists of all words w, w ∈ Σ * for which there exists a word u ∈ Σ * such that one of R(w, u) and R(w , u) is accepted while the other is not.
We assume that the reader is familiar with complexity classes and logarithmic space reductions via logarithmic space transducers; see for example [32].

Hardness of deciding monadic decomposability of regular relations
In this section, we consider binary regular relations given by NFA and provide a PSPACE lower bound for deciding if such a relation is monadic decomposable. Then, we prove that the same problem for DFA is NLOGSPACE-hard.

Lemma 5. The problem of deciding whether a binary regular relation given by an NFA is monadic decomposable is PSPACE-hard.
Proof. We give a logarithmic space reduction from the universality problem for NFA, which is PSPACE-hard [29]. Recall that in this problem, we are asked to decide whether L(A) = Σ * given an NFA A over Σ.
Let A be an NFA over alphabet Σ, and let {#} be a fresh symbol that we will use as a separator symbol. We assume that # = ⊥. We construct relation R = R 1 ∪ R 2 using the language L of A, where For the other direction, assume that R is monadic decomposable, i.e., ∈ R 1 as the lengths of the two words are different. It follows that ((w#) j , (w#) k ) ∈ R 2 and hence (w#) j ∈ (L · {#}) * . This implies that w ∈ L.

Lemma 6. The problem of deciding whether a binary regular relation given by a DFA is monadic decomposable is NLOGSPACE-hard.
Proof. We prove the hardness by a logarithmic space reduction from the reachability problem for directed acyclic graphs, which is an NLOGSPACE-hard problem [23]. Let G be a directed acyclic graph, s, t two vertices of G, and we are asked whether t is reachable from s. Let d be the degree of the graph.
We construct DFA A out of G. The states are the vertices of G together with new sink state ⊥. The initial state is s and the final state is t. The alphabet is Σ = {a 1 , . . . , a d , a d+1 }. Let s 1 be a vertex of outdegree d ≤ d that has edges to t 1 , . . . t d . For each edge from s 1 to t i , we add a transition (s 1 , (a i , a i ), s i ). Finally, we add a self-loop (t, (a d+1 , a d+1 ), t) and transitions (s , (a i , a j ), ⊥) for every state s and all i = j.
Observe that A is a DFA by our choice of labels on transitions. Moreover, the relation defined by A consists of words (u, u) for some u ∈ Σ * . The relation has finitely many different elements if and only if t is not reachable from s. Recall that all relations with finitely many elements are monadic decomposable. On the other hand, if t is reachable, then the relation is not monadic decomposable, which completes the proof.

Deciding monadic decomposability of binary regular relations
In this section we prove our main technical result.

Lemma 7.
There is an NLOGSPACE algorithm that takes as input an NFA for R ∼ , where R is a binary regular relation, and decides whether R is monadic decomposable.
We start by defining some notation. We assume any binary regular relation R ∼ to be given as an NFA over set of states Q. The R ∼ -type of a pair (w 1 , w 2 ) of words over Σ is an element of the transition monoid. Recall that the transition monoid transforms any given state q ∈ Q to a set Q ⊆ Q of states when reading (w 1 , w 2 ). We denote this by R ∼ w1,w2 (q) for each q ∈ Q. We write types(R ∼ ) for the set of all R ∼ -types.
Consider an infinite sequence {w i } i≥0 of words over Σ as defined in Lemma 4. Additionally, we assume that the words in the sequence are of strictly increasing length and that for each i > 0 the words w i and w i+1 have a common prefix of length w i−1 . That is, w i can be written as β 0 · · · β i−1 α i , where each β j and α i is a non-empty word. To simplify notation, we (In fact, it is the prefix of w j of the same length as w i ). We will show how to construct such sequence in Proposition 8.
With each pair (i, j), where i < j, we associate the following quinary tuple over types(R ∼ ): Intuitively, the first component corresponds to the computation of ( The final two components are used to compute the set of states reachable after the whole word in the first component is read. That is Figure 1 for a pictorial depiction. We can then establish the following important proposition. Consider an infinite sequence of words that are pairwise from different equivalence classes as in Lemma 4. We show next that we can extract an infinite subsequence with additional structural properties. Perhaps the most important property is that C i,j is the same for all i, j. This subsequence will allow us to prove the main lemma.

Proposition 8. A binary regular relation
Proof. By Lemma 4, the existence of such sequences directly implies that the relation is not monadic decomposable. Assume then that R is not monadic decomposable. By Lemma 4, there exists a sequence {u i } i≥0 such that R ∼ (u j , u ) for all j = . It remains to show how to construct the three sequences satisfying the additional properties from {u i } i≥0 . First, we construct an auxiliary sequence {w i } i≥0 in the following way. Let u j be the first non-empty word of {u i } i≥0 . Denote u j = u 0 = α 0 . Consider prefixes of u i of length |α 0 |. Since |α 0 | is finite and the sequence is infinite, there exists a prefix that appears infinitely often by the pigeonhole principle. Denote this prefix by β 0 . Now we consider an infinite subsequence {u i } i≥0 of {u i } i≥0 where u 0 = u j and u i , where i > 0, has β 0 as the proper prefix. We can write u 1 = β 0 α 1 and repeat the procedure. By König's Lemma, we can always repeat the procedure and obtain the desired sequence {w i } i≥0 in the limit.
From Infinite Ramsey's Theorem, there is an infinite sequence 0 ≤ 0 < 1 < · · · and a tuple C ∈ types(R ∼ ) 5 such that for each 0 ≤ i < j we have C i, j = C. Namely, we consider a complete infinite graph with natural numbers as vertices. An edge between vertices i and j is coloured with C i,j ∈ types(R ∼ ) 5 . Now there is an infinite clique coloured with C which gives us our infinite sequence 0 ≤ 0 < 1 < · · · .
We then define the u i s, γ i s, and δ i s, for i ≥ 0, as follows.
, and C satisfy the conditions in the statement of the proposition. See Figure 2 for a pictorial depiction of the construction.
In other words, by Proposition 8, there is a sequence {u i } i≥0 and a C such that for each i, j, the runs on R ∼ are synchronized after . . . . . .
This is a regular relation as we can construct a DFA recognizing this relation; see the left automaton of Figure 3 where the sink state and the transitions to the sink state are omitted. On the other hand, the relation does not have a monadic decomposition as a rather convoluted sequence {u i } i≥0 defined by satisfies the properties of Lemma 4. Indeed, it is easy to see that for any u i and u j , where i = j, u i ∼ u j . (In fact, for any u, v ∈ Σ * , such that u = v, then u ∼ v.) An automaton for R ∼ is presented on the right of Figure 3. Next, we follow the steps of Proposition 8. First, we construct subsequence {w i } i≥0 . In this sequence, w i = u 2i . Further, α i = b if i ≡ 0 mod 2 and α i = a otherwise, and β i = a for every i. Next, we highlight the importance of the second step of the construction. Consider three words of the sequence, v 1 = 0 n 1, v 2 = 0 n and v 3 = 0 n 1, where n < n < n , and the runs on (v 1 , v 2 ) and (v 2 , v 3 ) in R ∼ . The pair (v 1 , v 2 ) is accepted in state q 2 , while (v 2 , v 3 ) is accepted in q 3 . We can extract a subsequence where all accepting runs visit the same states, e.g., {u i } i≥0 , where u 0 = 00 and u i = (00) i−1 for i > 0. Now runs on any pair of words will be accepted in q 3 . That is, all pairs of words from this sequence visit exactly the same states of R ∼ .
We can then prove the following crucial result. We assume here that R is a binary regular relation over Σ × Σ such that R ∼ is given as an NFA over Σ × Σ whose set of states is Q. We further assume that q 0 is the initial state of R ∼ and F its set of final states.

Lemma 10.
It is the case that R is not monadic decomposable iff there are an infinite sequence {(x i , y i )} i≥0 of pairs of words over Σ and states q, q , p, r ∈ Q, such that p ∈ F , it is the case that q ∈ R ∼ x0,y0 (q 0 ), and the following statements hold for each i ≥ 0.

1.
It is the case that |x i | = |y i | and y i is a prefix of both x i+1 and y i+1 .

3.
If i > 0, we also have that p ∈ R ∼ ε,y −1 i xi+1 (r) and r ∈ R ∼ ε,y −1 i yi+1 (r). Proof. Assume first that R is not monadic decomposable. By Proposition 8, there are infinite sequences {u i } i≥0 , {γ i } i≥0 , and {δ i } i≥0 of words over Σ and a quinary tuple C over types(R ∼ ) such that for each i ≥ 0 it is the case that for each j > i, and 4. C i,j = C, for each j > i. We then define a sequence {(x i , y i )} i≥0 such that x i := u i , for each i ≥ 0, and y i is the prefix of x i+1 = u i+1 that has the same length than x i = u i , i.e., y i = τ (x i+1 , |x i |). Hence, y i = ρ(u i ) = δ 0 · · · δ i . Clearly, |x i | = |y i | ≥ 0 and y i is a prefix of both x i+1 and y i+1 , for each i ≥ 0. We prove next that the sequence {(x i , y i )} i≥0 also satisfies the remaining conditions. Before defining q, q , p, r ∈ Q, let us highlight the intuition why such states exist for every i. We can find such states because by our assumption C i,j = C for each i < j. Further, whether q is reachable from q 0 is stored in the first component of C. Similarly, the second and third components of C allows us to find q that is reachable from q 0 and such that q is reachable from q . Finally, the fourth component is for checking whether p is reachable from q and r, while the fifth component that r is reachable from both q and r.
Let us define q, q , p, r ∈ Q as follows. q and p are states such that p ∈ F and it is the case that q ∈ R ∼ x0,y0 (q 0 ) and p ∈ R ∼ ε,y −1 0 x1 (q). Notice that such q and p must exist as ( . Notice that such a q must exist. Indeed, since C 0, 0 y1 , as we know that q ∈ R ∼ x0,y0 (q 0 ) and there must be an intermediate state q that is reached after reading (y 0 , y 0 ). We have that r is a state such that r ∈ R ∼ ε,y −1 1 y2 (r). The existence of such state r is not obvious. We prove this as Lemma 11 after this proof.
We now prove that q, q , p, r satisfy all the requirements in the statement of the Lemma. By definition, q ∈ R ∼ x0,y0 (q 0 ) and p ∈ F . We can then prove by induction that for each i ≥ 0 it is the case that and, in addition, that for each i > 0 it is the case that p ∈ R ∼ ε,y −1 i xi+1 (r) and r ∈ R ∼ ε,y −1 i yi+1 (r). The base case i = 0 holds by definition.
We now prove by induction that for each i ≥ 0 it is the case that (1) and, in addition, that for each i > 0 it is the case that p ∈ R ∼ ε,y −1 i xi+1 (r) and r ∈ R ∼ ε,y −1 i yi+1 (r). (2) We start with (1), which we prove by induction.
Base case i = 0. We have already proven it when we showed the existence of q, q , p and r.
. This is the case since q ∈ R ∼ y0,y0 (q 0 ) and R ∼ yi+1,yi+1 (q 0 ) = R ∼ y0,y0 (q 0 ). The latter holds since C 0,1 = C i+1,i+2 = C, and thus R ∼ . This holds as we have that q ∈ R ∼ yi,yi (q 0 ) by the previous item, and it is the case that q ∈ R ∼ . The latter is the case as C 0,1 = C i,i+1 = C, and thus

Now the result follows from the fact that
The proof is analogous to the previous case.
This finishes the proof of (1).
We now prove (2) by induction.
x2 . This follows from the fact that C 0,1 = C i+1,i+2 = C. Therefore, This concludes the proof of the first direction. Let us assume now that there are an infinite sequence {(x i , y i )} i≥0 of pairs of words over Σ and states q, q , p, r ∈ Q that satisfy the conditions stated in the statement of the lemma. We prove next that R is not monadic decomposable by showing that there are infinite sequences and {β i } i≥0 satisfy the conditions stated in Lemma 4.
Base case j = 1. We only have to consider i = 0. Then it is the case that and, in addition, that Inductive case j + 1, for j ≥ 1. Consider an arbitrary i with 0 ≤ i < j. We have that where the containment holds by induction hypothesis. Analogously, we have that This finishes the proof of the theorem.
Next we prove the existence of state r satisfying , q ∈ Q and p ∈ F and C ∈ types(R ∼ ) 5 as defined in the proof of Lemma 10. There is a state r ∈ Q such that Proof. First of all, let r 1 , r 2 , . . . be an infinite sequence of states in Q that satisfies the following.
We have that r 1 is any state that satisfies r 1 ∈ R ∼ ε,y −1 0 y1 (q) and p ∈ R ∼ ε,y −1 1 x2 (r 1 ). For each j > 1 it is the case that r j ∈ R ∼ ε,y −1 j−1 yj (r j−1 ) and p ∈ R ∼ ε,y −1 j xj+1 (r j ). We explain next why the sequence r 1 , r 2 , . . . is well-defined. Notice first that state r 1 must exist as p ∈ R ∼ ε,y −1 0 x2 (q) and The former is the case as C 0,1 = C 0,2 = C, and thus R ∼ ε,y −1 Assume now that we have identified states r 1 , r 2 , . . . , r k , for k ≥ 1, such that for each j ≤ k it is the case that r j ∈ R ∼ ε,y −1 Again, the former is the case as C k,k+1 = C k,k+2 = C, and thus R ∼ This implies that p ∈ R ∼ ε,y −1 k x k+2 (r k ), as we know that p ∈ R ∼ ε,y −1 k x k+1 (r k ) by hypothesis. An important property of the sequence r 1 , r 2 , . . ., as defined above, is that for each k ≥ 1, and r k ∈ R ∼ ε,y −1 j y k (r j ), for each 1 ≤ j < k. This can be proved easily by induction on k ≥ 1.
Since the sequence r 1 , r 2 , . . . is infinite, there must be integers 1 ≤ j < k such that r j = r k . Therefore, it is the case that , which holds by definition of r j in the sequence r 1 , r 2 , . . .; It follows then that In fact: r j ∈ R ∼ ε,y −1 0 y1 (q), as r j ∈ R ∼ ε,y −1 0 yj (q) by hypothesis and, in addition, we have that C 0,1 = C 0,j = C, i.e., R ∼ ε,y −1 ε,y −1 j xj+1 (r j ) by hypothesis and, in addition, we have that C j,j+1 = C 1,2 = C, i.e., R ∼ ε,y −1 j y k (r j ) by hypothesis and, in addition, we have that . We can then set r = r j . This finishes the proof of the claim.
The runs as extracted from the sequence {(x i , y i }) i≥0 satisfying the conditions of Lemma 10 are depicted in Figure 4.
Let us briefly return to Example 9. Now using the notation of Lemma 10, the states q , q, r and p are q = q 0 , q = q 1 and r = p = q 3 . Observe that a run from q 0 to p through r (i.e., a run from q 0 to q 3 ) can be used to construct an infinite sequence of runs on (w, w ) ∈ R ∼ . Lemma 10 allows us to reduce the monadic decomposability problem to a set of reachability checks on types. With the help of this property, we can then prove Lemma 7.
Check if there are words w 0 , v 0 , w 1 , v 1 such that |w 0 | = |v 0 | > 0, |w 1 | = |v 1 | > 0, and it holds that ε,w1 (q), and (vi) r ∈ R ∼ ε,v1 (q). Check if there are words w, v such that |w| = |v| > 0, and it holds that . If this holds for any such a tuple, then R is not monadic decomposable. Else, R is monadic decomposable. It is easy to see that this algorithm can be implemented in NLOGSPACE.
We have the necessary ingredients to prove a part of Theorem 1.

Lemma 12.
Deciding whether a given binary regular relation R is monadic decomposable is in NLOGSPACE (resp. in PSPACE), if R is given by a DFA (resp. an NFA).
Proof. The claim follows from Lemma 7. Namely, from the definition of R ∼ , it follows that, if R is given by a DFA, then R ∼ can be constructed in LOGSPACE. Indeed, this can be done as disjunctions, conjunctions and projections can all be done in LOGSPACE and then via composability of LOGSPACE transducers we can construct R ∼ of logarithmic size. (Note that the output of a LOGSPACE transducer is of at most polynomial size.) Then by Lemma 7, we obtain the decidability of monadic decomposability in NLOGSPACE for R given by a DFA.
Analogously, if R is given by an NFA, we construct R ∼ of polynomial size since an NFA can be transformed into a DFA using a PSPACE transducer. (Again, note that the output of a PSPACE transducer is of at most exponential size.) Thus monadic decomposability is in PSPACE.

Deciding monadic decomposability of regular relations
In this section, we finish the proof of Theorem 1. The remaining component is showing that monadic decomposability of n-any regular relations is decidable in NLOGSPACE for DFA and PSPACE for NFA.

Lemma 13.
Deciding whether a given n-ary regular relation R is monadic decomposable is in NLOGSPACE (resp. in PSPACE), if R is given by a DFA (resp. an NFA).
Proof of Theorem 1. The upper bounds follow from Lemma 13 and the lower bound follows from Lemma 5 for NFA and from Lemma 6 for DFA.
In order to prove Lemma 13, we extend Lemma 12 to n-ary relations. Let us first define some helpful notation used throughout the section.
Recall that words of regular relations are padded to be of the same length using ⊥. We denote this function by PAD ⊥ . For example, PAD ⊥ ((a, ε, ab)) = (a⊥, ⊥⊥, ab). Let us now define a padding function δ n that acts slightly differently. Instead of padding the words in a tuple to make them of the same length, the new function pads a sequence of tuples with tuples where some elements are ⊥. Let us describe δ n in more details. Define Σ n = (Σ ⊥ ) n \ {⊥ n }, i.e., an alphabet consisting of n-tuples of letters from Σ ⊥ , excluding (⊥, . . . , ⊥). Now δ n : (Σ * ) n → Σ * n is an injective mapping that uses ⊥ to extend the shorter words to the same length as the longest word. For example, δ 3 maps (a, ε, ab) ∈ (Σ * ) 3 to (a, ⊥, a)(⊥, ⊥, b) ∈ Σ * 3 as follows:
For example, a ternary relation R = {(a, ε, ab)} over (Σ * ) 3 uniquely determines a binary relation R 1,2 = {(a, (⊥, a)(⊥, b))} over Σ * 1 × Σ * 2 through the correspondence ∆ 1,2 . For the sake of readability, if the integers x 1 , . . . , x m have a constant subsequence of length k, i.e., x i = x i+1 = · · · = x i+k−1 for some i, we write the relation as R x1,...,xi−1,x k i ,x i+k ,...,xm . In the following, we shall use R k to denote the binary relation R k,n−k induced by R. It turns out that being able to check monadic decomposability for binary relations is sufficient to check monadic decomposability for general n-ary relations.

Lemma 15.
Let R be an n-ary regular relation and let R 1 , . . . , R n−1 be the induced binary relations. Then R is monadic decomposable iff R 1 , . . . , R n−1 are monadic decomposable.
To see the other direction, we say that an n-ary relation R is k-decomposable if the induced k-ary relation R 1 k−1 ,n−k+1 of R is monadic decomposable. Now it suffices to show that R is n-decomposable since R = R 1 n . We shall prove this by induction on k ∈ {2, . . . , n}. Note that R is 2-decomposable by the assumption that R 1 is monadic decomposable. For 2 ≤ k ≤ n − 1, suppose that R k = j A j × B j and R is k-decomposable, say R 1 k−1 ,n−k+1 = i X i,1 × · · · × X i,k−1 × Y i . Then R is (k + 1)-decomposable as we have is regular since A j and {(x 1 , . . . , x k , y) | δ k (x 1 , . . . , x k ) = y} are regular (cf. [7]). Hence A i,j is also regular. The claim that R is n-decomposable then follows by induction.
We can then obtain our desired result.

Proof.
To prove the lemma, we show that if R is regular, then so are the induced relations R 1 , . . . , R n−1 . Moreover, given the automaton of R, one can construct the automaton for each R i in logarithmic space from R. We first show that if an n-ary relation R is regular, so are the induced binary relations R 1 , . . . , R n−1 . Let A ⊥ = (Σ ⊥ , Q, → A , q 0 , F ) be an n-tape automaton recognizing R ⊥ and fix k ∈ {1, . . . , n−1}. We argue that there exists a two-tape automaton B ⊥ = (Σ ⊥ , Q, → B , q 0 , F ) recognizing (R k ) ⊥ . The definitions of B ⊥ and A ⊥ differ only in their alphabet, padding symbols, and transition relations: the alphabet of B ⊥ is Σ = Σ k ∪ Σ n−k ; the padding symbol ⊥ is a fresh symbol not used in Σ ∪ Σ ; the transition relation of B ⊥ is determined from that of A ⊥ in the following way: for each transition τ = (q, s 1 , . . . , s n , S) in → A , there is a transition τ in → B such that τ = (q, λ k (s 1 , . . . , s k ), λ n−k (s k+1 , . . . , s n ), S) and vice versa. s 1 , . . . , s i ) = (s 1 , . . . , s i ), if s j = ⊥ for some j ∈ {1, . . . , i}; ⊥ , otherwise.
In order to construct the n − 1 automata for R i 's (i.e., B ⊥ ) with a logarithmic space transducer, observe that each transition in each automaton B ⊥ is simply a projection of some transition in R, and hence the number of transitions in B ⊥ is at most the number of transitions in A ⊥ . Then, the logarithmic space transducer would need to keep track of which transition in A ⊥ , which letter in Σ ⊥ , and finally which position in the product label (a 1 , . . . , a n ) is being transformed. This can be done in O(log |A ⊥ | + log |Σ ⊥ | + log n) = O(log |A ⊥ |) space. To show the NLOGSPACE (resp. PSPACE) complexity, we invoke the algorithm of Lemma 12 for each R i .

Concluding Remarks
Monadic decomposability for rational relations (and subclasses thereof) is a classical problem in automata theory that dates back to the late 1960s (the work of Stearns [33] and Fischer and Rosenberg [18]). While the general problem is undecidable, the subcase of regular relations (i.e. those recognized by synchronized multi-tape automata) provides a good balance between decidability [25,11] and expressiveness. The complexity of this subcase remained open for over a decade (exponential-time upper bound for the binary case [27,28], double exponential-time upper bound in the general case [11], and no specific lower bounds). This paper closes this question by providing the precise complexity for the problem: NLOGSPACE (resp. PSPACE) for DFA (resp. NFA) representations.
Some perspectives from formal verification and future work: Researchers from the area of formal verification have increasingly understood the importance of the monadic decompositions techniques, e.g., see [38]. Directly pertinent to monadic decomposability of regular relations is the line of work of constraint solving over strings, wherein increasingly more complex string operations are needed and thus added to solvers [36,3,26,1,12,2,13].
As an example, let us take a look at the recent work of Chen et al. [13], which spells out a string constraint language with semantic conditions for decidability that directly use the notion of monadic decomposability of relations over strings. Loosely speaking, a constraint is simply a sequence of program statements, each being either an assignment or a conditional: S ::= y := f (x 1 , . . . , x r ) | assert(g(x 1 , . . . , x r )) | S; S where f : (Σ * ) r → Σ * is a partial string function and g ⊆ (Σ * ) r is a string relation. The meaning of a constraint is what one would expect in a program written in a standard imperative programming language, which should support assignments and assertions. Note that loops are not allowed in the language since their target application is symbolic executions (e.g. see [10]). They provided two semantic conditions for ensuring decidability, one of which requires that each conditional g is effectively monadic decomposable. There is evidence (e.g. [20,13]) that some form of length reasoning in g is indeed required for many applications symbolic executions of string-manipulating programs, but much of the length constraints could be (not yet fully automatically) translated to regular constraints. A potential application for our results is therefore to provide support for complex string relations for g in the form regular relations, which permit a rather expressive class of conditionals (e.g. some form of length reasoning, etc.). Despite this, this application also highlights what is currently missing in the entire literature of monadic decomposability of rational relations: a study of the problem of outputting the monadic decompositions of the relations, if monadic decomposable.
(In fact, this is also true of other logical theories before the recent work of Veanes et al. [38].) What is the complexity of this problem with various representations of recognizable relations (e.g. finite unions of products, boolean combinations of regular constraints, etc.)? Although our results provide a first step towards solving this function problem, we strongly believe this to be a highly challenging open problem in its own right that deserves more attention.