Rewriting Higher-Order Stack Trees

Higher-order pushdown systems and ground tree rewriting systems can be seen as extensions of suffix word rewriting systems. Both classes generate infinite graphs with interesting logical properties. Indeed, the model-checking problem for monadic second order logic (respectively first order logic with a reachability predicate) is decidable on such graphs. We unify both models by introducing the notion of stack trees, trees whose nodes are labelled by higher-order stacks, and define the corresponding class of higher-order ground tree rewriting systems. We show that these graphs retain the decidability properties of ground tree rewriting graphs while generalising the pushdown hierarchy of graphs.


Introduction
Since Rabin's proof of the decidability of monadic second order logic (MSO) over the full infinite binary tree ∆ 2 [14], there has been an effort to characterise increasingly general classes of structures with decidable MSO theories. This can be achieved for instance using families of graph transformations which preserve the decidability of MSO -such as the unfolding or the MSO-interpretation and applying them to graphs of known decidable MSO theories, such as finite graphs or the graph ∆ 2 .
This approach was followed in [8], where it is shown that the prefix (or suffix) rewriting graphs of recognisable word rewriting systems, which coincide (up to graph isomorphism) with the transition graphs of pushdown automata (contracting ε-transitions), can be obtained from ∆ 2 using inverse regular substitutions, a simple class of MSO-compatible transformations. They also coincide with those obtained by applying MSO interpretations to ∆ 2 [1]. Alternately unfolding and applying inverse regular mappings to these graphs yields a strict hierarchy of classes of trees and graphs with a decidable MSO theory [9,7] coinciding with the transition graphs of higher-order pushdown automata and capturing the solutions of safe higher-order program schemes 1 , whose MSO decidability had already been established in [12]. We will henceforth call this the pushdown hierarchy and the graphs at its n-th level n-pushdown graphs for simplicity.
Also well-known are the automatic and tree-automatic structures (see for instance [2]), whose vertices are represented by words or trees and whose edges are characterised using finite automata running over tuples of vertices. The decidability of first-order logic (FO) over these graphs stems from the well-known closure properties of regular word and tree languages, but it can also be related to Rabin's result since tree-automatic graphs are precisely the class of graphs obtained from ∆ 2 using finite-set interpretations [10], a generalisation of WMSO interpretations mapping structures with a decidable MSO theory to structures with a decidable FO theory. Applying finite-set interpretations to the whole pushdown hierarchy therefore yields an infinite hierarchy of graphs of decidable FO theory, which is proven in [10] to be strict.
Since prefix-recognisable graphs can be seen as word rewriting graphs, another variation is to consider similar rewriting systems over trees. This yields the class of ground tree rewriting graphs, which strictly contains that of real-time order 1 pushdown graphs. This class is orthogonal to the whole pushdown hierarchy since it contains at least one graph of undecidable MSO theory, for instance the infinite 2-dimensional grid. The transitive closures of ground tree rewriting systems can be represented using ground tree transducers, whose graphs were shown in [11] to have decidable FO[ * − →] theories by establishing their closure under iteration and then showing that any such graph is tree-automatic.
The purpose of this work is to propose a common extension to both higherorder stack operations and ground tree rewriting. We introduce a model of higher-order ground tree rewriting over trees labelled by higher-order stacks (henceforth called stack trees), which coincides, at order 1, with ordinary ground tree rewriting and, over unary trees, with the dynamics of higher-order pushdown automata. Following ideas from the works cited above, as well as the notion of recognisable sets and relations over higher-order stacks defined in [5], we introduce the class of ground (order n) stack tree rewriting systems, whose derivation relations are captured by ground stack tree transducers. Establishing that this class of relations is closed under iteration and can be finite-set interpreted in n-pushdown graphs yields the decidability of their FO[ * − →] theories. The remainder of this paper is organised as follows. Section 2 recalls some of the concepts used in the paper. Section 3 defines stack trees and stack tree rewriting systems. Section 4 explores a notion of recognisability for binary relations over stack trees. Section 5 proves the decidability of FO[

Definitions and Notations
Trees. Given an arbitrary set Σ, an ordered Σ-labelled tree t of arity at most d ∈ N is a partial function from {1, . . . , d} * to Σ such that the domain of t, dom(t) is prefix-closed (if u is in dom(t), then every prefix of u is also in dom(t)) and leftclosed (for all u ∈ {1, . . . , d} * and 2 ≤ j ≤ d, t(uj) is defined only if t(ui) is for every i < j). Node uj is called the j-th child of its parent node u. Additionally, the nodes of t are totally ordered by the natural length-lexicographic ordering ≤ llex over {1, . . . , d} * . By abuse of notation, given a symbol a ∈ Σ, we simply denote by a the tree {ǫ → a} reduced to a unique a-labelled node. The frontier of t is the set fr(t) = {u ∈ dom(t) | u1 ∈ dom(t)}. Trees will always be drawn in such a way that the left-to-right placement of leaves respects ≤ lex . The set of finite trees labelled by Σ is denoted by T (Σ). In this paper we only consider finite trees, i.e. trees with finite domains.
Given nodes u and v, we write u ⊑ v if u is a prefix of v, i.e. if there exists w ∈ {1, · · · , d} * , v = uw. We will say that u is an ancestor of v or is above v, and symmetrically that v is below u or is its descendant. We call v ≤i the prefix of v of length i. For any u ∈ dom(t), t(u) is called the label of node u in t and t u = {v → t(uv) | uv ∈ dom(t)} is the sub-tree of t rooted at u. For any u ∈ dom(t), we call # t (u) the arity of u, i.e. its number of children. When t is understood, we simply write #(u). Given trees t, s 1 , . . . , s k and a ktuple of positions u = (u 1 , . . . , u k ) ∈ dom(t) k , we denote by t[s 1 , . . . s k ] u the tree obtained by replacing the sub-tree at each position u i in t by s i , i.e. the tree in which any node v not below any u i is labelled t(v), and any node u i .v with v ∈ dom(s i ) is labelled s i (v). In the special case where t is a k-context, i.e. contains leaves u 1 , . . . , u k labelled by special symbol ⋄, we omit u and simply write t[s 1 , . . . , s k ] = t[s 1 , . . . , s k ] u .
Directed Graphs. A directed graph G with edge labels in Γ is a pair (V G , E G ) where V G is a set of vertices and E G ⊆ (V G × Γ × V G ) is a set of edges. Given two vertices x and y, we write x There is a directed path in G from x to y labelled by w = w 1 . . . w k ∈ Γ * , written x w − → G y, if there are vertices x 0 , . . . , x k such that x = x 0 , x k = y and for all 1 ≤ i ≤ k, x i−1 wi −→ G x i . We additionally write x * − → G y if there exists w such that x w − → G y , and x + − → G y if there is such a path with |w| ≥ 1. A directed graph G is connected if there exists an undirected path between any two vertices x and y, meaning that (x, y) ∈ (− → G ∪ − → −1 G ) * . We omit G from all these notations when it is clear from the context. A directed graph D is acyclic, or is a DAG, if there is no x such that x + − → x. The empty DAG consisting of a single vertex (and no edge, hence its name) is denoted by . Given a DAG D, we denote by I D its set of vertices of in-degree 0, called input vertices, and by O D its set of vertices of out-degree 0, called output vertices. The DAG is said to be of in-degree |I D | and of out-degree |O D |. We henceforth only consider finite DAGs.
Rewriting Systems. Let Σ and Γ be finite alphabets. A Γ -labelled ground tree rewriting system (GTRS) is a finite set R of triples (ℓ, a, r) called rewrite rules, with ℓ and r finite Σ-labelled trees and a ∈ Γ a label. The rewriting graph of

Higher-Order Stacks
We briefly recall the notion of higher-order stacks (for details, see for instance [5]). In order to obtain a more straightforward extension from stacks to stack trees, we use a slightly tuned yet equivalent definition, whereby the hierarchy starts at level 0 and uses a different set of basic operations.
In the remainder, Σ will denote a fixed finite alphabet and n a positive integer. We first define stacks of order n (or n-stacks). Let Stacks 0 (Σ) = Σ denote the set of 0-stacks. For n > 0, the set of n-stacks is Stacks n (Σ) = (Stacks n−1 (Σ)) + , the set of non-empty sequences of (n − 1)-stacks. When Σ is understood, we simply write Stacks n . For s ∈ Stacks n , we write s = [s 1 , · · · , s k ] n , with k > 0 and n > 0, for an n-stack of size |s| = k whose topmost (n − 1)- Basic Stack Operations. Given two letters a, b ∈ Σ, we define the partial function rew a,b : Stacks 0 → Stacks 0 such that rew a,b (c) = b, if c = a and is not defined otherwise. We also consider the identity function id : Stacks 0 → Stacks 0 . For n ≥ 1, the function copy n : Stacks n → Stacks n is defined by copy n (s) = [s 1 , · · · , s k , s k ] n , for every s = [s 1 , · · · , s k ] n ∈ Stacks n . As it is injective, we denote by copy n its inverse (which is a partial function).

Stack Trees
We introduce the set ST n (Σ) = T (Stacks n−1 (Σ)) (or simply ST n when Σ is understood) of n-stack trees. Observe that an n-stack tree of degree 1 is isomorphic to an n-stack, and that ST 1 = T (Σ). Figure 1 shows an example of a 3-stack tree. The notion of stack trees therefore subsumes both higher-order stacks and ordinary trees.
Basic Stack Tree Operations. We now extend n-stack operations to stack trees. There are in general several positions where one may perform a given operation on a tree. We thus first define the localised application of an operation to a specific position in the tree (given by the index of a leaf in the lexicographic ordering of leaves), and then derive a definition of stack tree operations as binary relations, or equivalently as partial functions from stack trees to sets of stack trees.
Any operation of Ops n−1 is extended to ST n as follows: given θ ∈ Ops n−1 , and an integer i ≤ |fr(t)|, where u i is the i th leaf of the tree, with respect to the lexicographic order. If θ is not applicable to s, θ i (t) is not defined. We define θ(t) = {θ (i) (t) | i ≤ |fr(t)|}, i.e. the set of stack trees obtained by applying θ to a leaf of t.
The k-fold duplication of a stack tree leaf and its label is denoted by copy k n : ST n → 2 STn . Its application to the i th leaf of a tree t is: |} be the set of stack trees obtained by applying copy k n to a leaf of t. The inverse operation, written copy k n , is such that t ′ = copy k n(i) (t) if t = copy k n(i) (t ′ ). We also define copy k . For simplicity, we will henceforth only consider the case where stack trees have arity at most 2 and k ≤ 2, but all results go through in the general case. We denote by T Ops n = Ops n−1 ∪ {copy k n , copy k n | k ≤ 2} the set of basic operations over ST n .

Stack Tree Rewriting
As already mentioned, ST 1 is the set of trees labelled by Σ. In contrast with basic stack tree operations, a tree rewrite rule (ℓ, a, r) expresses the replacement of an arbitrarily large ground subtree ℓ of some tree s = c[ℓ] into r, yielding the tree c [r]. Contrarily to the case of order 1 stacks (which are simply words), composing basic stack tree operations does not allow us to directly express such an operation, because there is no guarantee that two successive operations will be applied to the same part of a tree. We thus need to find a way to consider compositions of basic operations acting on a single sub-tree. In our notations, the effect of a ground tree rewrite rule could thus be seen as the localised application of a sequence of rew and copy 2 1 operations followed by a sequence of rew and copy 2 1 operations. The relative positions where these operations must be applied could be represented as a pair of trees with edge labels in Ops 0 .
From level 2 on, this is no longer possible. Indeed a localised sequence of operations may be used to perform introspection on the stack labelling a node without destroying it, by first performing a copy 2 operation followed by a sequence of level 1 operations and a copy 2 operation. It is thus impossible to directly represent such a transformation using pairs of trees labelled by stack tree operations. We therefore adopt a presentation of compound operations as DAGs, which allows us to specify the relative application positions of successive basic operations. However, not every DAG represents a valid compound operation, so  we first need to define a suitable subclass of DAGs and associated concatenation operation. An example of the model we aim to define can be found in Fig. 2.

Concatenation of DAGs. Given two DAGs
j+m for some 0 ≤ m < d, and f (x) = x otherwise. We call D ′′ the (i, j)-concatenation of D and D ′ . Note that the (i, j)-concatenation of two connected DAGs remains connected.
Compound Operations. We represent compound operations as DAGs. We will refer in particular to the set of DAGs D n = {D θ | θ ∈ T Ops n } associated with basic operations, which are depicted in Fig. 3. Compound operations are inductively defined below, as depicted in Fig. 4.

Definition 1.
A DAG D is a compound operation (or simply an operation) if one of the following holds: .
where D 1 , D 2 , D 3 and D 4 are compound operations.
Additionally, the vertices of D are ordered inductively in such a way that every vertex of D i in the above definition is smaller than the vertices of D i+1 , the order over being the empty one. This induces in particular an order over the input vertices of D, and one over its output vertices.

Definition 2.
Given a compound operation D, we define D (i) (t), its localised application starting at the i-th leaf of a stack tree t, as follows: )))).
Remark 1. An operation may admit several different decompositions with respect to Def. 1. However, its application is well-defined, as one can show that this process is locally confluent.
Given two stack trees t, t ′ and an operation D, we say that t ′ ∈ D(t) if there is a position i such that t ′ = D (i) (t). Figure 2 shows an example. We define R D the relation induced by D as follows: for any stack trees t, t ′ , R D (t, t ′ ) if and only if t ′ ∈ D(t). Finally, given a k-tuple of operationsD = (D 1 , . . . , D k ) of respective in-degrees d 1 , . . . , d k and a k-tuple of indices i = (i 1 , . . . , i k ) with i j+1 ≥ i j + d j for all 1 ≤ j < k, we denote byD (i) (t) the parallel application . . , D k to t,D(t) the set of all such applications and RD the induced relation.
Since the (i, j)-concatenation of two operations as defined above is not necessarily a licit operation, we need to restrict ourselves to results which are wellformed according to Def. 1. Given D and D ′ , we let D · D ′ = {D · i,j D ′ | D · i,j D ′ is an operation}. Given n > 1, we define 2 D n = i<n D i · D n−i , and let D * = n≥0 D n denote the set of iterations of D. These notations are naturally extended to sets of operations. Proof. Recall that D n denotes the set of DAGs associated with basic operations. By definition of iteration, any DAG in D * n is an operation. Conversely, by Def. 1, any operation can be decomposed into a concatenation of DAGs of D n .

⊓ ⊔
Ground Stack Tree Rewriting Systems. By analogy with order 1 trees, given some finite alphabet of labels Γ , we call any finite subset of labelled operations in D * n × Γ a labelled ground stack tree rewriting system (GSTRS). We straightforwardly extend the notions of rewriting graph and derivation relation to these systems. Note that for n = 1, this class coincides with ordinary ground tree rewriting systems. Moreover, one can easily show that the rewriting graphs of ground stack tree rewriting systems over unary n-stack trees (trees containing only unary operations, i.e. no edge labelled by 2 or2) are isomorphic to the configuration graphs of order n pushdown automata performing a finite sequence of operations at each transition.

Operation Automata
In this section, in order to provide finite descriptions of possibly infinite sets of operations, in particular the derivation relations of GSTRS, we extend the notion of ground tree transducers (or GTT) of [11] to ground stack tree rewriting systems.
A GTT T is given by a tuple (A i , B i ) 1≤i≤k of pairs of finite tree automata.
It is also shown that, given a relation R recognised by a GTT, there exists another GTT recognising its reflexive and transitive closure R * .
Directly extending this idea to ground stack tree rewriting systems is not straightforward: contrarily to the case of trees, a given compound operation may be applicable to many different subtrees. Indeed, the only subtree to which 2 This unusual definition is necessary because · is not associative. For example, (D copy 2 n ·2,1 D copy 2 n ) ·1,1 D copy 2 n is in (D copy 2 n ) 2 · D copy 2 n but not in D copy 2 n · (D copy 2 n ) 2 .
a ground tree rewriting rule (s, t) can be applied is the tree s. On stack trees, this is no longer true, as depicted in Fig. 2: an operation does not entirely describe the labels of nodes of subtrees it can be applied to (as in the case of trees), and can therefore be applied to infinitely many different subtrees. Moreover, the resulting tree depends of the source tree. We will thus express relations by describing sets of compound operations over stack trees. Following [5], where recognisable sets of higher-order stacks are defined, we introduce operation automata and recognisable sets of operations.

Definition 3. An automaton over
An operation D is accepted by A if there is a labelling of its vertices by states of Q such that all input vertices are labelled by initial states, all output vertices by final states, and this labelling is consistent with ∆, in the sense that for all x, y and z respectively labelled by states p, q and r, and for all θ ∈ Ops n−1 ∪ {copy 1 n , copy 1 n }, We denote by Op(A) the set of operations recognised by A. Rec denotes the class of sets of operations recognised by operation automata. A pair of stack trees (t, t ′ ) is in the relation R(A) defined by A if for some k ≥ 1 there is a k-tuple of operationsD = (D 1 , · · · , D k ) in Op(A) k such that t ′ ∈D(t). At order 1, we have already seen that stack trees are simply trees, and that ground stack tree rewriting systems coincide with ground tree rewriting systems. Similarly, we also have the following:

Proposition 2. The classes of relations recognised by order 1 operation automata and by ground tree transducers coincide.
At higher orders, the class Rec and the corresponding binary relations retains several of the good closure properties of ground tree transductions.

Proposition 3. Rec is closed under union, intersection and iterated concatenation. The class of relations defined by operation automata is closed under composition and iterated composition.
The construction of automata recognising the union and intersection of two recognisable sets, the iterated concatenation of a recognisable set, or the composition of two automata-definable relations, can be found in the appendix. Given automaton A, the relation defined by the automaton accepting Op(A) * is R(A) * .
Normalised automata. Operations may perform "unnecessary" actions on a given stack tree, for instance duplicating a leaf with a copy 2 n operation and later destroying both copies with copy 2 n . Such operations which leave the input tree unchanged are referred to as loops. There are thus in general infinitely many operations representing the same relation over stack trees. It is therefore desirable to look for a canonical representative (a canonical operation) for each considered relation. The intuitive idea is to simplify operations by removing occurrences of successive mutually inverse basic operations. This process is a very classical tool in the literature of pushdown automata and related models, and was applied to higher-order stacks in [5]. Our notion of reduced operations is an adaptation of this work.
There are two main hurdles to overcome. First, as already mentioned, a compound operation D can perform introspection on the label of a leaf without destroying it. If D can be applied to a given stack tree t, such a sequence of operations does not change the resulting stack tree s. It does however forbid the application of D to other stack trees by inspecting their node labels, hence removing this part of the computation would lead to an operation with a possibly strictly larger domain. To adress this problem, and following [5], we use test operations ranging over regular sets of (n − 1)-stacks, which will allow us to handle non-destructive node-label introspection.
A second difficulty appears when an operation destroys a subtree and then reconstructs it identically, for instance a copy 2 n operation followed by copy 2 n . Trying to remove such a pattern would lead to a disconnected DAG, which does not describe a compound operation in our sense. We thus need to leave such occurrences intact. We can nevertheless bound the number of times a given position of the input stack tree is affected by the application of an operation by considering two phases: a destructive phase during which only copy i n and order n−1 basic operations (possibly including tests) are performed on the input stack tree, and a constructive phase only consisting of copy i n and order n − 1 basic operations. Similarly to the way ground tree rewriting is performed at order 1.
Formally, a test T L over Stacks n is the restriction of the identity operation to L ∈ Rec(Stacks n ) 3 . In other words, given s ∈ Stacks n , T L (s) = s if s ∈ L, otherwise, it is undefined. We denote by T n the set of test operations over Stacks n . We enrich our basic operations over ST n with T n−1 . We also extend compound operations with edges labelled by tests. We denote by D T n the set of basic operations with tests. We can now define the notion of reduced operation analogously to that of reduced instructions with tests in [5]. However, as in this work, there is not a unique reduced operation representing a given relation, due to the presence of tests, but it limits the number of times a same stack tree can be obtained during its application to a stack tree, which is exactly what we need in the proof of the formula of the next section.

Definition 4.
For i ∈ {0, · · · , n}, we define the set of words Red i over Ops n ∪ T n ∪ {1, 2,1,2} as: Observe that, in the decomposition of a reduced operation D, case 5 of the inductive definition of compound operations (Def. 1) should never occur, as otherwise, there would be a path on which 1 appears before1, which contradicts the definition of reduced operation.
An automaton A is said to be normalised if it only accepts reduced operations, and distinguished if there is no transition ending in an initial state or starting in a final state. The following proposition shows that any operation automaton can be normalised and distinguished.

Proposition 4. For every automaton A, there exists a distinguished normalised automaton with tests
The idea of the construction is to transform A in several steps, each modifying the set of accepted operations but not the recognised relation. The proof relies on the closure properties of regular sets of (n − 1)-stacks and an analysis of the structure of A. We show in particular, using a saturation technique, that the set of states of A can be partitioned into destructive states (which label the destructive phase of the operation, which does not contain the copy i n operation) and the constructive states (which label the constructive phase, where no copy i n occurs). These sets are further divided into test states, which are reached after a test has been performed (and only then) and which are the source of no test-labelled transition, and the others. This transformation can be performed without altering the accepted relation over stack trees.

Rewriting Graphs of Stack Trees
In this section, we study the properties of ground stack tree rewriting graphs. Our goal is to show that the graph of any Γ -labelled GSTRS has a decidable FO[ * − →] theory. We first state that there exists a distinguished and reduced automaton A recognising the derivation relation * − → R of R, and then show, following [10], that there exists a finite-set interpretation of * − → R and every a − → R for (D, a) ∈ R from a graph with decidable WMSO-theory.
To prove this theorem, we show that the graph To do so, we show that H R is finite-set interpretable inside a structure with a decidable WMSO-theory, and conclude using Corollary 2.5 of [10]. Thus from Section 5.2 of the same article, it follows that the rewriting graphs of GSTRS (and also the H R ) are in the tree-automatic hierarchy.
Given a Γ -labelled GSTRS R over ST n , we choose to interpret H R inside the order n Treegraph ∆ n over alphabet Σ ∪{1, 2}. Each vertex of this graph is an nstack, and there is an edge s θ − → s ′ if and only if s ′ = θ(s) with θ ∈ Ops n ∪T n . This graph belongs to the n-th level of the pushdown hierarchy and has a decidable WMSO theory 4 .
Given a stack tree t and a position u ∈ dom(t), we denote by where push w (s) is obtained by adding the word w at the top of the top-most 1-stack in s, and w i = #(u ≤i )u i+1 . This stack Code(t, u) is the encoding of the node at position u in t. Informally, it is obtained by storing in an n-stack the sequence of (n − 1)-stacks labelling nodes from the root of t to position u, and adding at the top of each (n − 1)-stack the number of children of the corresponding node of t and the next direction taken to reach node u. Any stack tree t is then encoded by the finite set of n-stacks X t = {Code(t, u) | u ∈ f r(t)}, i.e. the set of encodings of its leaves. Observe that this coding is injective.
Example 1. The coding of the stack tree t depicted in Fig. 1 is: We now represent any relation S between two stack trees as a WMSO-formula with two free second-order variables, which holds in ∆ n over sets X s and X t if and only if (s, t) ∈ S. Proposition 5. Given a Γ -labelled GSTRS R, there exist WMSO-formulae δ, Ψ a and φ such that: First note that the intuitive idea behind this interpretation is to only work on those vertices of ∆ n which are the encoding of some node in a stack tree. Formula δ will distinguish, amongst all possible finite sets of vertices, those which correspond to the set of encodings of all leaves of a stack tree. Formulae Ψ a and φ then respectively check the relationship through a − → R (resp. * − → R ) of a pair of stack trees. We give here a quick sketch of the formulae and a glimpse of their proof of correctness. More details can be found in appendix C.
Let us first detail formula δ, which is of the form OnlyLeaves(X) holds if every element of X codes for a leaf. TreeDom(X) holds if the induced domain is the domain of a tree and the arity of each node is consistent with the elements of X. UniqueLabel(X) holds if for every position u in the induced domain, all elements which include u agree on its label. From here on, variables X and Y will respectively stand for the encoding of some input stack tree s and output stack tree t. For each a ∈ Γ , Ψ a (X, Y ) is the disjunction of a family of formulae Ψ D (X, Y ) for each (D, a) ∈ R. Each Ψ D is defined by induction over D, simulating each basic operations in D, ensuring that they are applied according to their respective positions, and to a single closed subtree of s (which simply corresponds to a subset of X), yielding t.
Let us now turn to formula φ. Since the set of DAGs in R is finite, it is recognisable by an operation automaton. Since Rec is closed under iteration (Cf. Sec. 4), one may build a distinguished normalised automaton accepting * − → R . What we thus really show is that given such an automaton A, there exists a formula φ such that φ(X, Y ) holds if and only if t ∈D(s) for some vector Following a common pattern in automata theory, this formula expresses the existence of an accepting run of A over some tuple of reduced DAGsD, and states that the operation corresponding toD, when applied to s, yields t. Here, Z = Z q1 , · · · , Z q |Q A | defines a labelling of a subset of ∆ n Σ∪{1,2} with the states of the automaton, each element Z q of Z representing the set of nodes labelled by a given control state q. Sub-formula Init checks that only the elements of X (representing the leaves of s) are labelled by initial states, and only those in Y (leaves of t) are labelled by final states, and that the non-labelled leaves of X are the non-labelled leaves of Y . Trans ensures that the whole labelling respects the transition rules of A. For each component D ofD, and since every basic operation constituting D is applied locally and has an effect on a subtree of height and width at most 2, this amounts to a local consistency check between at most three vertices, encoding two nodes of a stack tree and their parent node. The relative positions where basic operations are applied is checked using the sets in Z, which represent the flow of control states at each step of the transformation of s into t. Finally, Diff ensures that no stack is labelled by two states belonging to the same part (destructive, constructive, testing or non-testing) of the automaton, thus making sure we simulate a unique run of A. This is necessary to ensure that no spurious run is generated, and is only possible because A is normalised.

Perspectives
There are several open questions arising from this work. The first one is the strictness of the hierarchy, and the question of finding simple examples of graphs separating each of its levels with the corresponding levels of the pushdown and tree-automatic hierarchies. A second interesting question concerns the trace languages of stack tree rewriting graphs. It is known that the trace languages of higher-order pushdown automata are the indexed languages [8], that the class of languages recognised by automatic structures are the context-sensitive languages [15] and that those recognised by tree-automatic structures form the class Etime [13]. However there is to our knowledge no characterisation of the languages recognised by ground tree rewriting systems. It is not hard to define a 2-stack tree rewriting graph whose path language between two specific vertices is {u ¡ u | u ∈ Σ * }, which we believe cannot be recognised using tree rewriting systems or higher-order pushdown automata 5 . Finally, the model of stack trees can be readily extended to trees labelled by trees. Future work will include the question of extending our notion of rewriting and Theorem 1 to this model.

A Properties of Operation Automata
In this section, we show that Rec is closed under union, intersection, iteration and contains the finite sets of operations. Proof. We will construct an automaton which witness Prop 6. First, we ensure that the two automata are complete by adding a sink state if some transitions do not exist. We construct then the automaton A which is the product automaton of A 1 and A 2 : ) ∈ ∆ A2 } If an operation admits a valid labelling in A 1 and in A 2 , then the labelling which labels each states by the two states it has in its labelling in A 1 and A 2 is valid. If an operation admits a valid labelling in A, then, restricting it to the states of A 1 (resp A 2 ), we have a valid labelling in A 1 (resp A 2 ).

Proposition 7. Given two automata A 1 and A 2 , there exists an automaton
Proof. We take the disjoint union of A 1 and A 2 : If an operation admits a valid labelling in A 1 (resp A 2 ), it is also a valid labelling in A. If an operation admits a valid labelling in A, as A is a disjoint union of A 1 and A 2 , it can only be labelled by states of A 1 or of A 2 (by definition, there is no transition between states of A 1 and states of A 2 ) and then the labelling is valid in A 1 or in A 2 .

Proposition 8. Given an automaton A, there exists
The set of transition ∆ contains the transitions of A together with multiple copies of each transition ending with a state in F A , modified to end in a state belonging to I A Proof. If D = (V, E), we take: Proof. The first thing to remark is that if we don't have any tree transitions, we have a higher-order stack automaton as in [5] and that the notions of normalised automaton coincide. The idea is thus to separate the automaton in two parts, one containing only tree transitions and the other stack transitions, to normalise each part separately and then to remove the useless transitions used to separate the automaton. Step 1: In this transformation, we will use a new special basic operation: id such that its associated operation D id is the following DAG: For every stack tree t and any integer i ≤ |fr(t)|, id (i) (t) = t.
We will use this operation to separate our DAGs in several parts linked with id operations, and will remove them at the end of the transformation. We suppose that we start with an automaton without such id transitions. We begin by splitting the set of control states of the automaton into three parts. We create three copies of Q: The idea of what we want to obtain is depicted in Fig. 5. Formally, we replace the automaton A = (Q, I, F, ∆) by A 1 = (Q ′ , I ′ , F ′ , ∆ ′ ) with: where for every q ∈ Q, q t1 , q t2 , q s are fresh states.

Lemma 1. A and A 1 recognise the same relation.
Proof. To prove this lemma, we prove that for every operation D recognised by A, there is an operation D ′ recognised by A 1 such that R D = R D ′ , and vice versa.
Let us take D recognised by A. We prove, by induction on the structure of D that we can construct D ′ such that R D = R D ′ and for every labelling ρ D of D consistent with ∆, with I D labelled by q and O D by q ′ , there exists ρ ′ D a labelling of D ′ consistent with ∆ ′ such that I D ′ is labelled by q s and O D ′ by q ′ s . If D = , we take D ′ = . We have R D = R D ′ . For every labelling ρ D which labels the unique node of D by q, we take ρ D ′ which labels the unique node of D ′ by q s . These labellings are consistent by ∆ and ∆ ′ , by vacuity.
Suppose now that we have F and F ′ such that for every labelling ρ F we can define a labelling ρ F ′ satisfying the previous condition. Let us consider the following cases: We call x the output node of F and y the input node of G.
By induction hypothesis, we consider F ′ and G ′ , and construct where x ′ is the output node of F ′ and y ′ the input node of G ′ . We take ρ D a labelling of D and ρ F (resp. ρ G ) its restriction to F (resp. G). We have ρ D (x) = q and ρ D (y) = q ′ . By induction hypothesis, we consider To prove that R D = R D ′ , we just have to remark that, from the definition of application of operation, we have for every stack tree t and integer i, The other cases being similar, we just give D ′ and ρ D ′ and leave the details to the reader. -D = (F · 1,1 D θ ) · 1,1 G, for θ ∈ Ops n−1 ∪ T n−1 . We call x the output node of F and y the input node of G. We have By induction hypothesis, we consider F ′ and G ′ , and construct D ′ = (F ′ · 1,1 where x ′ is the output node of F ′ and y ′ the input node of G ′ . We take ρ D a labelling of D and ρ F (resp. ρ G ) its restriction to F (resp. G). We have ρ D (x) = q and ρ D (y) = q ′ . By induction hypothesis, we consider We call x the output node of F , y the input node of G and z the input node of H. We have By induction hypothesis, we consider F ′ , G ′ and H ′ , and construct D ′ = (((((F · 1,1 D id )D copy 2 n ) · 2,1 D id ) · 2,1 H) where x ′ is the output node of F ′ , y ′ the input node of G ′ and z ′ the input node of H ′ . We take ρ D a labelling of D and ρ F (resp. ρ G , ρ H ) its restriction to F (resp. G, H). We have ρ D (x) = q, ρ D (y) = q ′ and ρ D (z) = q ′′ . By induction hypothesis, we consider ρ F ′ (resp. ρ G ′ ,ρ H ′ ) the corresponding labelling of F ′ (resp.
We call x the output node of F , y the output node of G and z the input node of H.
By induction hypothesis, we consider F ′ , G ′ and H ′ , and construct where x ′ is the output node of F ′ , y ′ the input node of G ′ and z ′ the input node of H ′ . We take ρ D a labelling of D and ρ F (resp. ρ G , ρ H ) its restriction to F (resp. G, H). We have ρ D (x) = q, ρ D (y) = q ′ and ρ D (z) = q ′′ . By induction hypothesis, we consider ρ F ′ (resp. ρ G ′ , ρ H ′ ) the corresponding labelling of We call x the output node of F , y 1 the input node of G and y 2 its output node, z 1 the input node of H and z 2 its output node and w the input node of K. We have By induction hypothesis, we consider F ′ , G ′ , H ′ and K ′ , and construct D ′ = ((((((F ′ · 1,1 D id ) · 1,1 D copy 2 n ) · 2,1 (D id · 1,1 H ′ )) · 1,1 (D id · 1,1 G ′ )) · 1,1 D copy 2 1 the input node of G ′ , y ′ 2 its output node, z ′ 1 the input node of H ′ , z ′ 2 its output node and t ′ the input node of K ′ . We take ρ D a labelling of D D and ρ F (resp. ρ G , ρ H , ρ K ) its restriction to F (resp. G, H, K). We have ρ D (x) = q, ρ D (y 1 ) = q ′ , ρ D (z 1 ) = q ′′ , ρ D (y 2 ) = r ′ , ρ D (z 2 ) = r ′′ and ρ D (t) = r ′′ . By induction hypothesis, we consider ρ F ′ (resp. To do the other direction, we take D ′ recognised by A 1 and show that we can construct D recognised by A with R D = R D ′ by an induction on the structure of D ′ similar to the previous one (for each id transition, we do not modify the constructed DAG and for all other transition, we add them to the DAG). All the arguments are similar to the previous proof, so we let the reader detail it.

⊓ ⊔
We start by normalising the tree part of the automaton. To do so, we just have to prevent the automaton to recognise DAGs which contain ((D copy 2 n · 1,1 F 1 ) · 2,1 F 2 ) · 1,1 D copy 2 n , or (D copy 1 n · 1,1 F ) · 1,1 D copy 1 n as a subDAG. Such a subDAG will be called a bubble. However, we do not want to modify the recognised relation. We will do it in two steps: first we allow the automaton to replace the bubbles with equivalent tests (after remarking that a bubble can only be a test) in any recognised DAG (step 2), and then by ensuring that there won't be any copy i n transition below the first copy j n transition (step 3).
Step 2: Let A 1 = (Q, I, F, ∆) be the automaton obtained after step 1. Given two states q 1 , q 2 , we denote by L Aq 1 ,q 2 the set {s ∈ Stacks n−1 | ∃D ∈ D(A 1 ), D (1) (s) = s} where A q1,q2 is a copy of A 1 in which we take q 1 as the unique initial state and q 2 as the unique final state. In other words, L Aq 1 ,q 2 is the set of (n − 1)-stacks such that the trees with one node labelled by this stack remains unchanged by an operation recognised by A q1,q2 . We define The idea of the construction is depicted in Fig. 6. We give the following lemma for the binary bubble. The case of the unary bubble is very similar and thus if left to the reader.
be two automata recognising DAGs without tree operations. The two automata B 1 = (Q 1 , I, F, ∆ 1 ) and B 2 = (Q 2 , I, F, ∆ 2 ), with Proof. An operation D recognised by B 2 is of the form D = D copy 2 n · 1,1 (F 1 · 1,1 (F 2 · 2,2 D copy 2 n )), where F 1 is recognised by C 1 and F 2 by C 2 . We have: So this operation is defined if and only if F 1 (t(u i )) = F 2 (t(u i )) = t(u i ). In this case, D i (t) = t. Thus, B 2 accepts only operations which are tests, and these tests are the intersection of the tests recognised by C 1 and C 2 . So the relation recognised by B 2 is exactly the relation recognised by T LC 1 ∩LC 2 , which is the only operation recognised by B 1 . ⊓ ⊔

20
We have the following corollary as a direct consequence of this lemma.

Corollary 1. A 1 and A 2 recognises the same relation.
Indeed, all the new operations recognised do not modify the relation recognised by the automaton as each test was already present in the DAGs containing a bubble. Fig. 6: Step 2: The added test transition to shortcut the bubble is depicted with a dotted line Step 3: Suppose that A 2 = (Q, I, F, ∆) is the automaton obtained after step 2.
We now want to really forbid these bubbles. To do so, we split the control states automaton in two parts: We create 2 copies of Q: -Q d which are target of no copy d n transition, -Q c which are source of no copy d n transition. Iterating this process, we obtain an operation D ′ without any bubble such that R D ⊆ R D ′ and D ′ is recognised by A 2 . As it contains no bubble, it is also recognised by A 3 .
Then every relation recognised by an operation with bubbles is already included in the relation recognised by an operation without bubbles. Then A 2 and A 3 recognise the same relation.

⊓ ⊔
We call the destructive part the restriction A 3,d of A 3 to Q d and the constructive part its restriction A 3,c to Q c .
Step 4: We consider an automaton A 3 obtained after the previous step. Observe that in the two previous steps, we did not modify the separation between Q t1 , Q t2 and Q s . We call A 3,s the restriction of A 3 to Q s .
We now want to normalise A 3,s . As this part of the automaton only contains transitions labelled by operations of Ops n−1 ∪ T n−1 , we can consider it as an automaton over higher-order stack operations. So we will use the process of normalisation over higher-order stack operations defined in [5]. For each pair (q s , q ′ s ) of states in Q s , we construct the normalised automaton We suppose that these automata are distinguished, i.e. that states of I A qs ,q ′ s are target of no transitions and states of F A qs ,q ′ s are source of no transitions. We moreover suppose that it is not possible to do two test transitions in a row (this is not a strong supposition because such a sequence would not be normalised, but it is worth noticing it).
We replace A 3,s with the union of all the A qs,q ′ s : we define A 4 = (Q ′ , I ′ , F ′ , ∆ ′ ):  Fig. 7: Step 4: The splitting of the stack part of the automaton Step 5: We now have a normalised automaton, except that we have id transitions. We remove them by a classical saturation mechanism. Observe that in all the previous steps, we never modified the separation between Q t1 , Q s and Q t2 , so that all id transitions are from Q t1 to Q s and from Q s to Q t2 . We take A 4 = (Q, I, F, ∆) obtained after the previous step. We construct Lemma 5. A 4 and A 5 recognise the same relation.
Proof. We prove it by an induction on the structure of relations similar to the one of step 1, so we leave it to the reader. ⊓ ⊔ Step 6: We now split the control states set into two parts: -Q T , the states which are target of all and only test transitions and source of no test transition, -Q C , the states which are source of all test transitions and target of no test transition.
Given automaton A 5 = (Q, I, F, ∆) obtained from the previous step, we define Lemma 6. A 5 and A 6 recognise the same relation.
Proof. As, from step 4 it is not possible to have two successive test transitions, the set of recognised operations is the same in both automata, only the labelling is modified. The details are left to the reader. ⊓ ⊔ Finally, we suppose that an automaton obtained by these steps is distinguished, i.e. initial states are target of no transition and final states are source of no transition. If not, we can distinguish it by a classical transformation (as in the case of word automata). We now have a normalised automaton with tests A 6 obtained after the application of the six steps which recognises the same relation as the initial automaton A. In subsequent constructions, we will be considering the subsets of states Q T , Q C , Q d , Q c as defined in steps 6 and 3, and ⊓ ⊔

C Finite set interpretation
In this section, we formally define a finite set interpretation I R from ∆ n Σ∪{1,2} to the rewriting graph of a GSTRS R. In the whole section, we consider a distinguished normalised automaton with tests A = (Q, I, F, ∆) recognising R * , constructed according to the process of the previous section. Let us first formally define a possible presentation of the graph ∆ n Σ∪{1,2} . Vertices of this graph are n-stacks over alphabet Σ ∪ {1, 2}, and there is an edge (x, θ, y) in ∆ n Σ∪{1,2} if θ ∈ Ops n (Σ ∪ {1, 2}) ∪ T n and y = θ(x). Since we are building an unlabelled graph, our interpretation consists of these formulae: δ(X) which describes which subsets of Stacks n (Σ ∪ {1, 2}) are in the graph,

C.1 Notations and Technical Formulae
We will use the push d and pop d operations to simplify the notations. They have the usual definition (as can be encountered in [5]), but notice that we can define them easily with our operations: Observe that push d (x) and pop d (x) are well defined as there can only be one a such that the definition holds: the a which is the topmost letter of x. We extend this notations to push and pop words to simplify notations.
We first define some formulae over ∆ n Σ∪{1,2} which will be used to construct the set of stacks used to represent stack trees over ∆ n Σ∪{1,2} . Given θ ∈ Ops n−1 (Σ) ∪ T n−1 , we define ψ θ such that, given two n-stacks x, y, ψ θ (x, y) = x θ − → y. ψ copy i n ,d (x, y) = ∃a ∈ Σ, z 1 , z 2 , z 3 , z 4 , z 5 , z 6 , z 7 , z 8 ∈ V, x y) is true if y is obtained by applying θ to x. ψ copy i n ,d (x, y) is true if y is obtained by adding i and d to the topmost 1-stack of x, duplicating its topmost (n − 1)-stack and then removing d and i from its topmost 1-stack.
We now give a technical formula which ensures that a given stack y is obtained from a stack x using only the previous formulae: Reach(x, y) This formula is true if for every set of n-stacks X, if x is in X and X is closed by the relations defined ψ θ and ψ copy i n ,d , then y is in X.

Lemma 7.
For all n-stacks x = [x 1 , · · · , x m ] n and y = [y 1 , · · · , y m ′ ] n , there exists a sequence of operations ρ j ∈ (Ops n−1 (Σ) ∪ T n−1 ) * such that ρ j (x m , y j ). For every n-stack x and a ∈ Σ, Reach([a] n , x) holds if and only if there exist a stack tree t and a node u such that x = Code(t, u).

Corollary 2.
Proof. Suppose that there exist a stack tree t and a node u such that x = Code(t, u).
We take a tree domain U such that d 0 · · · d m−1 ∈ U . We define a tree t of domain U such that for every j, t(d 0 · · · d j ) = x j+1 , t(ε) = x 0 , every node d 0 · · · d j has i j+1 sons, the node ε has i 0 sons, and for every u ∈ U which is not
Formula OnlyLeaves ensures that an element x in X encodes a node in some stack tree. TreeDom ensures that the prefix closure of the set of words d 0 · · · d m−1 such that is a valid domain of a tree, and that the set of words i 0 · · · i m−1 is included in this set (in other words, that the arity announced by the i j is respected). an Finally UniqueLabel ensures that for any two elements i.e. for any two elements, the (n − 1)-stacks labelling common ancestors are equal, and x and y cannot encode the same leaf (as . Moreover, it also prevents x to code a node on the path from the root to the node coded by y. where X ranges only over finite sets of Stacks n (Σ ∪ {1, 2}).
Let us take x ∈ X t such that x = Code(t, u) with u = u 0 · · · u i 2u i+2 · · · u |u| . As t is a tree, u 0 · · · u i 2 ∈ dom(t) and so is u 0 · · · u i 1. Then, there exists v ∈ f r(t) such that ∀j ≤ i, v j = u j , v i+1 = 1, and Code(t, v) ∈ X t . Let us now take x ∈ X t such that x = Code(t, u) with u = u 0 · · · u i 1u i+2 · · · u |u| and #(u 0 · · · u i 1) = 2, then u 0 · · · u i 2 is in dom(t) and there exists v ∈ f r(t) such that ∀j ≤ i, v j = u j , v i+1 = 2 and Code(t, v) ∈ X t . Thus TreeDom holds.
Let x and y in X t such that x = y, x = Code(t, u) and y = Code(t, v), and let i be the smallest index such that u i = v i . Suppose that u i = 1 and v i = 2 (the other case is symmetric). We call z = Code(t, u 0 · · · u i−1 ), and take z ′ and z ′′ such that ψ copy 2 n ,1 (z, z ′ ) and ψ copy 2 n ,2 (z, z ′′ ). We have then Reach(z ′ , x) and Reach(z ′′ , y). And thus UniqueLabel holds. Therefore, for every stack tree t, δ(X t ) holds.
Let us now show that for every X ⊆ Stacks n (Σ ∪ {1, 2}) such that δ(X) holds, there exists t ∈ ST n , such that X = X t . As OnlyLeaves holds, for every x ∈ X, with, for all j, x j ∈ Stacks n−1 , i j ∈ {1, 2} and u j ≤ i j . In the following, we denote by u x the word u 0 · · · u k−1 for a given x, and by U = {u | ∃x ∈ X, u ⊑ u x }. U is closed under prefixes. As TreeDom holds, for all u, if u2 is in U , then u1 is in U as well. Therefore U is the domain of a tree. Moreover, if there is a x such that u1 ⊑ u x and i |u| = 2, then TreeDom ensures that there is y such that u2 ⊑ u y and thus u2 ∈ U . As UniqueLabel holds, for every x and y two distinct elements of X, there exists j such that for all k < j we have u x k = u y k , and u x j = u y j . Then, for all k ≤ j, we have x k = y k and i k = i ′ k . Thus, for every u ∈ U , we can define σ u such that for every x such that u ⊑ u x , x |u| = σ u , and the number of sons of each node is consistent with the coding.
Consider the tree t of domain U such that for all u ∈ U , t(u) = σ u . We have X = X t , which concludes the proof.

C.3 The formula Ψ D associated with an operation
We now take an operation D which we suppose to be reduced, for the sake of simplicity (but we could do so for a non reduced operation, and for any operation, there exists a reduced operation with tests defining the same relation, from the two previous appendices). We define inductively ψ D as follow: As D is a finite DAG, every ψ D is a finite formula, and is thus a monadic formula.
This formula is true if its two arguments are related by R D .

Proposition 10. Given two stack trees s, t and an operation D, t ∈ D(t) if and only if
Proof. We show it by induction on the structure of D: exists i such that t = D (i) (t). By definition, t = G (i) (θ (i) (F (i) (s))). We call r = F (i) (s). By induction hypothesis, we have Ψ F (X s , X r ). By definition, we have, for all j < i, Code(s, u j ) = Code(r, u j ), and for all j > i, And by definition, we have ψ θ (Code(r, u i ), θ(Code(r, u i ))). We have t = G (i) (r ′ ), thus, by induction hypothesis, Suppose that Ψ D (X s , X t ) is true. We call r the tree such that X r = Z. By induction hypothesis, we have r ∈ F (s). Moreover, we have z = Code(r, u i ) such that X r \{z} = X s \X ′ . Thus, by definition, r = F (i) (s), and X ′ = {Code(s, u j ) | i ≤ |I F | − 1}. We have z ′ = θ(z), as ψ θ (z, z ′ ) is true. We call r ′ = θ (i) (r), and we have X r ′ = X r \{z} ∪ {z ′ }. As we have Ψ G (X r ′ , Y ), by induction, we have t ∈ G(r ′ ). As we moreover have Y \Y ′ = Z\{z}, we thus have t = G (i) (r ′ ). Thus, we have t = G (i) (θ (i) (F (i) (s))) = D (i) (s).
The other cases are similar and left to the reader.

C.4 The formula φ associated with an automaton
Let us now explain φ(X, Y ), which can be written as ∃Z q1 , · · · , Z q |Q| , φ ′ (X, Y, Z) with φ ′ (X, Y, Z) = Init(X, Y, Z)∧Diff(Z)∧Trans(Z). We detail each of the three subformulas Init, Diff and Trans below: This formula is here to ensure that only leaves of X are labelled by initial states, only leaves of Y are labelled by final states and outside of their labelled leaves, X and Y are equal (i.e. not modified).
This formula is here to ensure that a given stack (and thus a given leaf in a tree of the run) is labelled by at most a state of each subpart of Q: Q T,d , Q C,d , Q T,c , Q C,c . So if we have a non deterministic choice to do we will only choose one possibility.
where ρ q is true if and only if q is a final state, and This formula ensures that the labelling respects the rules of the automaton, and that for every stack labelled by q, if there is a rule starting by q, there is at least a stack which is the result of the stack by one of those rules. And also that it is possible for a final state to have no successor.
Proposition 11. Given s, t two stack trees, φ(s, t) if and only if there are some operations D 1 , · · · , D k recognised by A such that t is obtained by applying D 1 , · · · , D k at disjoint positions of s.
Proof. First suppose there exist such D 1 , · · · , D k . We construct a labelling of Stacks n (Σ ∪ {1, 2}) which satisfies φ(X s , X t ). We take a labelling of the D i by A. We will label the Stacks n according to this labelling. If we obtain a tree t ′ at any step in the run of the application of D i to s, we label Code(t ′ , u) by the labelling of the node of D i appended to the leaf at position u of t ′ . Notice that this does not depend on the order we apply the D i to s nor the order of the leaves we choose to apply the operations first. We suppose that t = D ki k (· · · D 1i 1 (s) · · · ). Given a node x of an D i , we call l(x) its labelling.
-The ∅ labelling is the empty labelling.
Lemma 9. The labelling previously defined Z satisfies φ ′ (X s , X t , Z).
Proof. Let us first cite a technical lemma which comes directly from the definition of the labelling: Lemma 10. Given a reduced operation D, a labelling of D, ρ D , a stack tree t, a i ∈ N and a j ≤ |I D |, the label of For the sake of simplicity, let us consider for this proof that D is a reduced operation (if it is a set of reduced operations, the proof is the same for every operations).
First, let us prove that Init is satisfied. From the previous lemma, all nodes of X s are labelled with the labels of input nodes of D (or not labelled), thus they are labelled by initial states (as we considered an accepting labelling of D). Furthermore, as the automaton is distinguished, only these one can be labelled by initial states. Similarly, the nodes of X t , and only them are labelled by final states (or not labelled).
We now show that Trans is satisfied. Let us suppose that a Code(t ′ , u i ) is labelled by a q. By construction of the labelling, it has been obtained by a ( , i, t ′ ) labelling. If q is final, then we have nothing to verify, as ρ q is true. If not, the node x labelled by q which is the unique node of the which labelled Code(t ′ , u i ) by q has at least one son in D. Suppose, for instance that D = (F 1 · 1,1 D θ ) · 1,1 F 2 such that x is the output node of F 1 . We call y the input node of F 2 . As D is recognised by A, it is labelled by a q ′ such that (q, θ, q ′ ) ∈ ∆ A . By construction, we take the (F 1 , i, s), (F 2 , i, θ (i) (t ′ )) labelling, with t ′ = F 2(i) (s). Thus we have Code(θ (i) (t ′ ), u i ) labelled by q ′ (from Lemma 10), and thus Trans (q,θ, The other possible cases for decomposing D (D = (((F 1 · 1,1 D copy 1 n )· 2,1 F 3 )· 1,1 F 2 or D = ((F 1 · 1,1 (F 2 · 2,1 copy 2 n )) · 1,1 F 3 ) are very similar and are thus left to the reader. Observe that D may not be decomposable at the node x, in which case we decompose D and consider the part containing x until we can decompose the DAG at x, where the argument is the same.
Let us now prove that the labelling satisfies Diff. Given q, q ′ ∈ Q C,d , suppose that there is a Code(t ′ , u i ) which is labelled by q and q ′ . By construction, this labelling is obtained by a (F 1 , i, t ′ 1 ), (F 2 , i, t ′ 2 ) labelling, where F 1 and F 2 are both , and t ′ 1 (u i ) = t ′ 1 (u i ). We call x (resp. y) the unique node of F 1 (resp. F 2 ). x is labelled by q and y by q ′ .
Suppose that D can be decomposed as (G· 1,1 D θ )· 1,1 H (or ((G· 1,1 D copy 2 n )· 2,1 K) · 1,1 H, or ((G · 1,1 (H · 1,2 D copy 2 n ) · 1,1 K) such that y is the output node of G (if not, decompose D until you can obtain such a decomposition). Then, suppose you can decompose G = G 1 · 1,1 D θ · 1,1 G 2 (or ((G 1 · 1,1 (G 3 · 1,2 D copy 2 n )· 1,1 G 2 . As we are considering states of Q C,d , there is no other possible case) such that x is the input node of G 2 . Thus, we have by construction G 2 (Code(t ′ , u i )) = Code(t ′ , u i ). So G 2 defines a relation contained in the identity. As it is a part of D and thus labelled by states of A, with q and q ′ in Q C,d , there is no copy j n nor copy j n transitions in G 2 . Moreover, as q and q ′ are in Q C,d , G 2 is not a single test transition. Then it is a sequence of elements of Ops n−1 ∪ T n−1 defining a relation included into the identity. As A is normalised, this is impossible, and then Code(t ′ , u i ) cannot be labelled by both q and q ′ .
Taking two states in the other subsets of Q yields the same contradiction with few modifications and are thus left to the reader.
Then, as all its sub-formulae are true, φ ′ (X s , X t , Z) is true with the described labelling Z. And then φ(X s , X t ) is true.
Proof. Suppose that D is not a DAG, then there exists (x, q) ∈ V such that (x, q) + − → (x, q), then there exists a sequence of operations in A d (for A c it is symmetric, and there is no transition from A c to A d , thus a cycle cannot have states of the both parts) which is the identity (and thus it is an sequence of operations of Ops n−1 ∪ T n−1 ). As A d is normalised, it is not possible to have such a sequence. Then, there is no cycle in D which is therefore a DAG.
By definition of E D , it is labelled by Ops n−1 ∪ T n−1 ∪ {1,1, 2,2}. We choose an D i . Suppose that it is not an operation. Thus, there exists a node (x, q) of D i such that D i cannot be decomposed at this node (i.e, in the inducted decomposition, there will be no case which can be applied to cut either D i or one of its subDAG to obtain (x, q) as the output node of a sub-DAG obtained (or the input node). Let us consider the following cases for the neighbourhood of (x, q): -(x, q) has a unique son (y, q ′ ), which has no other father such that (x, q) 2 − → (y, q ′ ). By definition of Trans, we have that ψ copy 2 n ,2 (x, y), and thus we have a (q, (q ′′ , q ′ )) ∈ ∆ and a z such that ψ copy 2 n ,1 (x, z) which is in Z q ′′ . This contradicts that (x, q) has a unique son in D i . If (x, q)2 − → (y, q ′ ), the case is similar. For every other θ ∈ Ops n−1 ∪ T n−1 ∪ {1,1}, we can decompose the subDAG {(x, q) θ − → (y, q ′ )} as ( · 1,1 D θ ) · 1,1 .
The other cases are similar and are thus left to the reader. It then suffices to construct this way successively t 1 = D 1(i 1) (s), t 2 = D 2(i 2 ) (t 1 ), etc, to obtain t and prove the lemma.

⊓ ⊔
We have proved both directions: for every n-stack trees s and t, there exists a set of operations D i recognised by A such that t is obtained by applying the D i to disjoint positions of s if and only if φ(X s , X t ).
⊓ ⊔ We then have a monadic interpretation with finite sets (all sets are finite), and then, the graph has a decidable FO theory, which concludes the proof.

D Example of a language
We can see a rewriting graph as a language acceptor in a classical way by defining some initial and final states and labelling the edges. We present here an example of a language recognised by a stack tree rewriting system. The recognised language is {u ¡ u | u ∈ Σ}. Fix an alphabet Σ and two special symbols ↑ and ↓.
We consider ST 2 (Σ ∪ {↑, ↓}). We now define a rewriting system R, whose rules are given in Fig. 8. To recognise a language with this system, we have to fix an initial set of stack trees and a final set of stack trees. We will have a unique initial tree and a recognisable set of final trees. They are depicted on Fig. 9.
A word w ∈ R * is accepted by this rewriting system if there is a path from the initial tree to a final tree labelled by w. The trace language recognised is {P a1 · · · P an · Dupl · ((D an · · · D a1 ) ¡ (D an · · · D a1 )) | a 1 , · · · , a n ∈ Σ}.
Let us informally explain why. We start on the initial tree, which has only a leaf labelled by a stack whose topmost symbol is ↓. So we cannot apply a D a to it. If we apply a P a to it, we remain in the same situation, but we added an a to the stack labelling the unique node. So we can read a sequence P a1 · · · P an . From this situation, we can also apply a Dupl, which yields a tree with three nodes whose two leaves are labelled by [a 1 · · · a n ↑] 1 , if we first read P a1 · · · P an . From this new situation, we can only apply D a rules. If the two leaves are labelled by [b 1 · · · b m ↑] 1 and [c 1 · · · c ℓ ↑] 1 , we can apply D bm or D c ℓ , yielding the same tree in which we removed b m or c ℓ from the adequate leaf. We can do this until a final tree remains. So, on each leaf, we will read D an · · · D a1 in this order, but we have no constraint on the order we will read these two sequences. So we effectively can read any word in (D an · · · D a1 ) ¡ (D an · · · D a1 ). And this is the only way to reach a final tree.
To obtain the language we announced at the start, we just have to define a labelling λ of each operation of R as follows: λ(Dupl) = ε, for every a ∈ Σ, λ(P a ) = ε and λ(D a ) = a, and remark that if w is of the previous form, then λ(w) = (a 1 · · · a n ) ¡ (a 1 · · · a n ), and we indeed recognise {u ¡ u | u ∈ Σ}.