In this section, we first recall the definition of two-way visibly pushdown automata and later on extend them to two-way visibly pushdown Parikh automata.
We consider a structured alphabet \(\varSigma \) defined as the disjoint union of call symbols \(\varSigma _c\), return symbols \(\varSigma _r\) and internal symbols \(\varSigma _i\). The set of words over \(\varSigma \) is \(\varSigma ^*\). As usual, \(\epsilon \) denotes the empty word. Amongst nested words, the set of well-nested words \(\varSigma ^*_{\textsf {wn}}\) is defined as the least set such that \(\varSigma _i \cup \{\epsilon \}\) is included into \(\varSigma ^*_{\textsf {wn}}\) and if \(w_1,w_2 \in \varSigma ^*_{\textsf {wn}}\) then both \(w_1w_2\) and \(cw_1r\) (for all \(c \in \varSigma _c\) and \(r \in \varSigma _r\)) belong to \(\varSigma ^*_{\textsf {wn}}\).
When dealing with two-way machines, we assume the structured alphabet \(\varSigma \) to be extended to \(\overline{\varSigma }\) by adding a left and right marker symbols \(\triangleright ,\triangleleft \) in \(\overline{\varSigma }_c\) and \(\overline{\varSigma }_r\) respectively, and we consider words in the language \(\triangleright \varSigma ^* \triangleleft \).
Definition 1
A two way visibly pushdown automaton (\(\mathsf {2VPA}\) for short) A over \(\overline{\varSigma }\) is given by \((Q,q_I,F,\varGamma ,\delta )\) where Q is a finite set of states, \(q_I \in Q\) is the initial state, \(F \subseteq Q\) is a set of final states and \(\varGamma \) is a finite stack alphabet. Given the set \(\mathbb {D}=\{\leftarrow , \rightarrow \}\) of directions, the transition relation \(\delta \) is defined by \(\delta ^{\text {push}} \cup \delta ^{\text {pop}} \cup \delta ^{\text {int}}\) where
-
\(\delta ^{\text {push}} \subseteq ((Q \times \{\rightarrow \} \times \varSigma _c ) \cup (Q \times \{\leftarrow \} \times \varSigma _r)) \times ((Q \times \mathbb {D}) \times \varGamma )\)
-
\(\delta ^{\text {pop}} \subseteq ((Q \times \{\leftarrow \} \times \varSigma _c \times \varGamma ) \cup (Q \times \{\rightarrow \} \times \varSigma _r \times \varGamma )) \times (Q \times \mathbb {D})\)
-
\(\delta ^{\text {int}} \subseteq ((Q \times \mathbb {D}\times \varSigma _i) \times (Q \times \mathbb {D})\)
Additionally, we require that for any states \(q,q'\) and any stack symbol \(\gamma \), if \((q,\leftarrow ,\triangleright ,\gamma ,q',d) \in \delta ^{\text {pop}}\) then \(d=\rightarrow \) and if \((q,\rightarrow ,\triangleleft ,\gamma ,q',d) \in \delta ^{\text {pop}}\) then \(d=\leftarrow \) ensuring that the reading head stays within the bounds of the input word.
Informally, a \(\mathsf {2VPA}\) has a reading head pointing between symbols (and possibly on the left of \(\triangleright \) or the right of \(\triangleleft \)). A configuration of the machine is given by a state, a direction d and a stack content. The next symbol to be read is on the right of the head if \(d=\rightarrow \) and on the left if \(d=\leftarrow \). Note that when reading the left marker from right to left \(\leftarrow \) (resp. the right marker from left to right \(\rightarrow \)), the next direction can only be \(\rightarrow \) (resp. \(\leftarrow \)). The structure of the alphabet induces the behavior of the machine regarding the stack when reading the input word: when reading on the right, a call symbol leads to push one symbol onto the stack while a return symbol pops one symbol from the stack. When reading on the left, a dual behaviour holds. In any direction internal transitions from \(\delta ^{\text {int}}\) read internal symbols and do not affect the stack; hence, at a given position in the input word, the height of the stack is always constant at each visit of that position in the run of the machine. The triggering of a transition leads to the update of the state of the machine, the future direction as well as the stack content. For a direction d, a natural i (\(0 \le i \le |w|\)) and a word w, we denote by
-
\({\text {move}}(d,i)\) the integer \(i-1\) if \(d=\leftarrow \) and \(i+1\) if \(d=\rightarrow \).
-
\({\text {read}}(w,d,i)\) the symbol w(i) if \(d=\leftarrow \) and \(w(i+1)\) if \(d=\rightarrow \).
Note that when switching directions (i.e. when the direction of the first part of the transition is different from the second part), we read twice the same letter. This ensures the good behavior of the stack, as reading a call letter from left to right pushes a stack symbol, we need to pop it if we start moving from right to left.
Formally, a stack \(\sigma \) is a finite word over \(\varGamma \). The empty stack/word over \(\varGamma \) is denoted \(\bot \). For a word w from \(\overline{\varSigma }\) and a \(\mathsf {2VPA}\) \(A=(Q,q_I,F,\varGamma ,\delta )\), a configuration \(\kappa \) of A is a tuple \((q,i,d,\sigma )\) where \(q \in Q\), \(0 \le i \le |w|\), \(d \in \mathbb {D}\) and \(\sigma \) is a stack. A run of A on a word w is a finite sequence \(\rho \) from \(K(\delta K)^*\), where K is the set of all configurations \(\kappa \) (that is a sequence starting and ending with a configuration and alternating between configurations and transitions); a run \(\rho \) is of the form \((q_0,i_0,d_0,\sigma _0) \tau _1 (q_1,i_1,d_1,\sigma _1) \tau _2 \ldots \tau _\ell (q_\ell ,i_\ell ,d_\ell ,\sigma _\ell )\) where for all \(0\le j < \ell \), we have:
-
either \(d_j=\rightarrow \) and \({\text {read}}(w,d_j,i_j) \in \varSigma _c\) or \(d_j=\leftarrow \) and \({\text {read}}(w,d_j,i_j) \in \varSigma _r\), \(\tau _{j+1}=(q_j,d_j,{\text {read}}(w,d_j,i_j),q_{j+1},d_{j+1},\gamma ) \in \delta ^{\text {push}}\), \(i_{j+1}={\text {move}}(i_j,d_j)\) and \(\sigma _{j+1}=\sigma _j\gamma \)
-
either \(d_j=\leftarrow \) and \({\text {read}}(w,d_j,i_j) \in \varSigma _c\) or \(d_j=\rightarrow \) and \({\text {read}}(w,d_j,i_j) \in \varSigma _r\), \(\tau _{j+1}=(q_j,d_j,{\text {read}}(w,d_j,i_j),\gamma ,q_{j+1},d_{j+1}) \in \delta ^{\text {pop}}\), \(i_{j+1}={\text {move}}(i_j,d_j)\) and \(\sigma _{j+1}\gamma =\sigma _j\)
-
\({\text {read}}(w,d_j,i_j)\in \varSigma _i\), \(\tau _{j+1}=(q_j,d_j,{\text {read}}(w,d_j,i_j),q_{j+1},d_{j+1})\in \delta ^{\text {int}}\), \(i_{j+1}=i_j\) and \(\sigma _{j+1}=\sigma _j\).
Note that any configuration is actually a run on the empty word \(\epsilon \). The initial configuration is \((q_I,0,\rightarrow ,\bot )\). A configuration \((q,i,d,\bot )\) is final if \(q \in F\) and i is the last position. A run for the word w is accepting if its first configuration is initial and its last configuration is final. A two-way visibly pushdown automaton A is:
-
deterministic (denoted \(\mathsf {D2VPA}\)) if \(\delta ^{\text {push}}\) (resp. \(\delta ^{\text {pop}}\), \(\delta ^{\text {int}}\)) is a function from \(Q\times \mathbb {D}\times \varSigma \) (resp. \(Q\times \mathbb {D}\times \varSigma \times \varGamma \), \(Q\times \mathbb {D}\times \varSigma \)) to \(Q\times \mathbb {D}\times \varGamma \) (resp. \(Q\times \mathbb {D}\), \(Q\times \mathbb {D}\)).
-
one-way (denoted \(\mathsf {VPA}\)) if all transitions in A have \(\rightarrow \) for direction.
-
finite-visit if for some \(k\ge 0\), any run visits at most k times the same input position.
The size of a \(\mathsf {2VPA}\) is the number of states times the size of the stack alphabet. For A an automaton, we denote by L(A) the language recognized by A.
Lemma 1
([8]). Given a \(\mathsf {2VPA}\) A, deciding if L(A) is empty is ExpTime-complete.
Parikh automata. Parikh automata were introduced in [22]. Informally, they are automata with counters that can only be incremented, and do not act on the transition relation. Acceptance of runs is done by evaluating a Presburger formula whose free variables are set to the counter values. In our setting, a Presburger formula is a positive formula \(\psi (x_1,\ldots ,x_n)=\exists y_1\ldots y_m \varphi (x_1,\ldots ,x_n,y_1,\ldots ,y_m)\) such that \(\varphi \) is a boolean combination of atoms \(s+s'\le t+t'\), for \(s,s',t,t'\in \{0,1,x_1,\ldots ,x_n,y_1,\ldots ,y_m\}\). For a set S and some positive number m, we denote by \(S^m\) the set of all mappings from \([1\ldots m]\) to S. If \((s_1,\ldots ,s_m)\) and \((t_1,\ldots ,t_m)\) are two tuples of \(S^m\) and \(+\) is an binary operation on S, we extend \(+\) to \(S^m\) by considering the operation element-wise, i.e. \((s_1,\ldots ,s_m)+(t_1,\ldots ,t_m)=(s_1+t_1,\ldots ,s_m+t_m)\).
Definition 2
A two-way visibly pushdown Parikh automaton (\(\mathsf {2VPPA}\) for short) is a tuple \(P = (A,\lambda ,\phi )\) where A is a \(\mathsf {2VPA}\) and for some natural \( dim \), \(\lambda \) is a mapping from \(\delta \) to \(\mathbb {N}^ dim \), the set of vectors of length \( dim \) of naturals and \(\phi (x_1, \ldots , x_ dim )\) is a Presburger formula with \( dim \) free variables.
When clear from context, we may omit the free variables from the Presburger formula, and simply note \(\phi \). A run of a \(\mathsf {2VPPA}\) is a run of its underlying \(\mathsf {2VPA}\). We extend canonically the mapping \(\lambda \) to runs. For a run \(\rho \) of the form \((q_0,i_0,d_0,\sigma _0) \tau _1 (q_1,i_1,d_1,\sigma _1) \tau _2 \ldots \tau _\ell (q_\ell ,i_\ell ,d_\ell ,\sigma _\ell )\), we set
$$\lambda (\rho ) = \lambda (\tau _1) + \lambda (\tau _2) + \ldots + \lambda (\tau _\ell )$$
We recall that a single configuration c is a run over the empty word \(\epsilon \). For such a run c, we set \(\lambda (c)=0^ dim \). A run \((q_0,i_0,d_0,\sigma _0) \tau _1 (q_1,i_1,d_1,\sigma _1) \tau _2 \ldots \tau _\ell (q_\ell ,i_\ell ,d_\ell ,\sigma _\ell )\) is accepted if \((q_0,i_0,d_0,\sigma _0)\), \((q_\ell ,i_\ell ,d_\ell ,\sigma _\ell )\) are respectively an initial and a final configuration of the underlying automaton and for \(\lambda (\rho ) = (n_1,\ldots , n_ dim )\), \([x_1 \leftarrow n_1,\ldots , x_\ell \leftarrow n_ dim ] \models \phi (x_1, \ldots , x_ dim )\). The language L(P) is the set of words which admit an accepting run. We define the set of values computed by P as \( Val (P)=\{\lambda (\rho )\mid \rho \text { a valid run of the underlying automaton of P}\}\). We define the size of P as the size of A plus the number of symbols in \(\phi \) and \(|\delta |\cdot dim\cdot log(W)\) where W is the maximal value occurring in the codomain of \(\lambda \).
It is deterministic (resp. one-way), denoted \(\mathsf {D2VPPA}\) (resp. \(\mathsf {VPPA}\)) if its underlying automaton is deterministic (resp. one-way). It is known from [4] that \(\mathsf {DPA}\) (i.e. deterministic one-way and stack-free Parikh automata in our setting) are strictly less expressive than their nondeterministic counterpart. As a counter example, they exhibit the language \(L=\{w\mid w_{\#_a(w)}=b\}\), ie all words w such that if n is the number of a in w, the letter at the nth position is a b. Note that even in the two-way case, a deterministic machine recognizing L needs to either have access, during the computation, to the number of a’s, or be able to store, in counters, the position of each b. As the first solution cannot be done since Parikh automata only access their counters at the end of the run, and the second is also impossible since there are only a finite number of counters, this language is also non definable by a \(\mathsf {D2VPPA}\), furthering the separation between deterministic and nondeterministic Parikh automata.
Example 1
As an example, we give a deterministic \(\mathsf {2VPPA}\) P that, given an input \( i^n c^k i^\ell r^k\) with c, i, r in \(\varSigma _c\), \(\varSigma _i\) and \(\varSigma _r\) respectively, accepts if \(k=\ell \) and \(n=k^2\). The \(\mathsf {2VPPA}\) P uses 4 variables \(x_n\), \(x_k\), \(x_\ell \) and y. The first 3 variables are used to count the number of the first block of is, the number of calls and the second block of is respectively. The handling of these 3 variables is straight-forward and can be done in a single pass over the input. The fourth variables y counts the multiplication \(k\cdot \ell \) and doing so is more involved. The part of the underlying \(\mathsf {2VPA}\) of P handling y is given in Fig. 2. On this part, the mapping \(\lambda \) simply increments the counter on transitions going to state 2 (i.e. on reading the letters i from left to right). It makes as many passes on the set of internal symbols in state 2 as there are call symbols, and the state of the stack upon reading \(i^\ell \) for the jth time is \(1^j0^{k-j}\). Finally, the accepting formula \(\phi \) of P is defined by \(x_n=y \wedge x_k=x_\ell \). Note that this widget allows us to compute the set \(\{(k^2,k,k,k^2)\mid k\in \mathbb {N}\}\) which is not semilinear.
As we have seen in the previous example, the set \( Val (P)\) is not necessarily semi-linear, even with P a \(\mathsf {D2VPPA}\). We use this fact to encode diophantine equations, and get the following undecidability result:
Theorem 1
The emptiness problem of \(\mathsf {D2VPPA}\) is undecidable.
Single-useness. In order to recover decidability, we adapt to Parikh Automata the notion of single-useness introduced in [8]. Simply put, a \(\mathsf {2VPPA}\) is single-use (denoted \(\mathsf {2VPPA_{su}}\)) if the transitions that affect the variables can only be taken once on any given input position, thus effectively bounding the size of variables linearly with respect to the size of the input. Formally, a state p of a \(\mathsf {2VPPA}\) P is producing if there exists a transition t from p on some symbol and \(\lambda (t)\ne 0^ dim \). A \(\mathsf {2VPPA}\) is single-use if for every input w and every accepting run \(\rho \) over w, there do not exist two different configurations \((p,i,d,\sigma )\) and \((p,i,d,\sigma ')\) with p a producing state, meaning that \(\rho \) does not reach any position in the same direction twice in any given state of P. This property is a syntaxic restriction of the model. However, since this property is regular, it can equivalently be seen as a semantic one. Moreover, deciding the single-useness of a \(\mathsf {2VPPA}\) is ExpTime-c (see [8] for the same result but on transducers). Note that the Parikh automaton given in Example 1 is not single-use, since it passes over the second subword of internal letters i in state 2 as many times as there are call symbols. In the following, we prove that \(\mathsf {2VPPA_{su}}\) have the same expressiveness as \(\mathsf {VPPA}\), while being exponentially more succinct. In particular, this equivalence implies by Parikh’s Theorem [24], semi-linearity of \( Val (P)\) for any \(\mathsf {2VPPA_{su}}\) P.