betaFeedback

WorksComputer Science

Type-Checking for Pattern-Based Tree Transformations

Abstract

We introduce and study pattern-based tree transformations. As an illustrating example, consider a source pattern (x⋅y)+(x⋅z)(x \cdot y) + (x \cdot z) and a target pattern x⋅(y+z)x \cdot (y + z) as a pair. This source pattern matches any expression ee of the form (e1⋅e2)+(e1⋅e3)(e_1 \cdot e_2) + (e_1 \cdot e_3) (by substituting xx with e1e_1, yy with e2e_2, and zz with e3e_3) and the pair transforms it into the expression e1⋅(e2+e3)e_1 \cdot (e_2 + e_3) as dictated by the target pattern. Note that in this example, the set of expressions that match the source pattern is not a regular tree language. We propose a model of tree transformations given by a finite representation of a (possibly infinite) set of such (source pattern, target pattern) pairs. The expressive power of this model comes at the cost of undecidability of checking equivalence. Nevertheless, we show that the type-checking problem is decidable for our model of pattern-based tree transformations. The type-checking problem asks whether applying a given transformation to trees having a given regular property (type) preserves the property. Our decision procedure is by a reduction to the emptiness problem of alternating tree automata.

2012 ACM Subject Classification Theory of computation →\to Tree languages; Theory of computation →\to Transducers

Related Version Full version of a preprint accepted at FSTTCS 2026

Introduction

Tree transformations in various forms of machines and logics have been studied for applications such as XML processing, programming languages, and HTML sanitisation [5] [11, 12, 14]. Classical formalisms for specifying tree transductions such as finite-state top-down and bottom-up tree transducers, macro tree transducers, attribute grammars and MSO (Monadic Second-Order Logic)-definable graph transductions typically aim for robustness with respect to operations involving regular tree languages and decidability of the equivalence problem.

We introduce a model for tree transductions targeting scenarios where the domain of the transduction is not necessarily regular, and undecidability of equivalence does not render the model useless. Expanding on the example from the abstract, consider expressions of the form t⋅e1+t⋅e2+⋯+t⋅ent \mathbin{\cdot} e_1 + t \mathbin{\cdot} e_2 + \cdots+ t \mathbin{\cdot} e_n, where nn is any number. An equivalent expression is t⋅(e1+e2+⋯+en)t \mathbin{\cdot} (e_1 + e_2 + \cdots+ e_n). Transformations like this are routinely used in Horner’s method, an efficient way to evaluate polynomials. We consider the corresponding expression trees and model this transformation by matching the source tree with a source pattern and the target tree with a target pattern. The patterns are themselves trees, which can have placeholder variables to represent subtrees, like tt in the above example, which must repeat in multiple positions. The number nn can be arbitrary, so we need infinitely many pairs of patterns. For the example above, for each n>0n > 0 we need (x⋅e1+x⋅e2+⋯+x⋅en, x⋅(e1+e2+⋯+en))(x \cdot e_1 + x \cdot e_2 + \cdots+ x \cdot e_n,\ x \cdot(e_1 + e_2 + \cdots+ e_n)) as a (source pattern, target pattern) pair, where xx is a placeholder variable intended to match any expression tt. We represent such infinite sets (of pairs of patterns) using (standard) tree transductions. We allow placeholder variables in patterns to be substituted with any tree, as long as it satisfies a guard condition associated with the variable. The guard condition is given by a regular tree language called the guard language. It allows, for example, to restrict the expression tt above to be a valid expression. In some situations we may want to restrict it further, say, to not contain any products inside. In this example, the set of source trees to which the transformation is applicable needs to have the same subtree tt repeat in multiple positions. Thus the set of source trees on which transformations are defined need not be a regular tree language in general, which makes our model different from classical tree transductions.

The backbone of our model is a finite representation of the infinite set of (source pattern, target pattern) pairs. Such a finite representation can be any formalism for tree transductions; in this paper we have chosen to use bimorphisms for technical convenience. Bimorphisms are given by a regular seed language SS and two morphisms ϕsrc,ϕtgt\phi_{\mathrm{src}}, \phi_{\mathrm{tgt}}. The transduction is defined as the relation {(ϕsrc(t),ϕtgt(t))∣t∈S}\{(\phi_{\mathrm{src}}(t), \phi_{\mathrm{tgt}}(t)) \mid t \in S\}. Over words, bimorphisms are equivalent to rational transductions [15]. Over trees, bimorphisms capture subclasses of tree transducers (cf. [4 Section 6.5] and [6].

For example, consider the seed language given by ab1(0+1)∗(⊕b1(0+1)∗)∗cab1(0 + 1)^*(\oplus b1(0 + 1)^*)^*c. This is a regular word language over the alphabet {a,b,c,⊕,0,1}\{a,b,c,\oplus,0,1\}. We define the source and target morphisms ϕsrc,ϕtgt:{a,b,c,⊕,0,1}→{0,1,+,⋅,x,(,)}∗\phi_{\mathrm{src}}, \phi_{\mathrm{tgt}}: \{a,b,c,\oplus,0,1\} \to\{0,1,+,\cdot,x,(,)\}^* as given by the following: ϕsrc:={a↦ϵ,b↦x⋅,c↦ϵ,⊕↦+,0↦0,1↦1}\phi_{\mathrm{src}} := \{a \mapsto\epsilon, b \mapsto x \cdot, c \mapsto\epsilon, \oplus\mapsto+, 0 \mapsto0, 1 \mapsto1\} and similarly we define: ϕtgt:={a↦x⋅(,b↦ϵ,c↦),⊕↦+,0↦0,1↦1}\phi_{\mathrm{tgt}} := \{a \mapsto x \cdot(, b \mapsto\epsilon, c \mapsto), \oplus\mapsto+, 0 \mapsto0, 1 \mapsto1\}. We note that this bimorphism generates pairs of the form (x⋅e1+x⋅e2+⋯+x⋅en, x⋅(e1+e2+⋯+en))(x \cdot e_1 + x \cdot e_2 + \cdots+ x \cdot e_n,\ x \cdot(e_1 + e_2 + \cdots+ e_n)) where eie_i is a nonzero binary number, from the seed word abel1e2⋯en⊕be1e2⋯enc\mathit{abel}_1 \mathit{e}_2 \cdots\mathit{e}_n \oplus\mathit{b}e_1 \mathit{e}_2 \cdots\mathit{e}_n c. We may add a guard to xx saying that xx can take values only from 1(0+1)∗1(0 + 1)^*, capturing completely the distributivity of multiplication over addition for binary numbers. Note that this only requires bimorphisms over words and defines transduction over words. If instead we allow the guard language of xx to be any syntactically valid expression tree, this now defines transformations from trees to trees, though given by a bimorphism over words. In fact, if we wish e1,e2…ene_1,e_2\ldots e_n to be also expressions, we would require a bimorphism over trees. This case is detailed as an example in Section 2.

Notice that bimorphisms over words which do not produce placeholder variables anywhere already capture rational relations over words. Since equivalence-checking is undecidable for rational word relations [8, 10], it is undecidable for our model as well. However we show that the type-checking problem is decidable, even for transductions defined by bimorphisms over trees and potentially producing variables in the output of both morphisms. We first motivate the need for type-checking in the next paragraph, and give insights to our algorithm in the paragraphs following that.

Continuing with our example from above, we may want to check that applying the transformation does not increase the alternation between the operators ⋅\cdot and ++. This can be checked by verifying that if the transformation is applied to any tree from a language LL consisting of trees with alternation depth bounded by a constant cc, the resulting tree is in LL as well. On the other hand, for reasons of numerical stability, we may be interested in ensuring that size of subtrees made up of only the + operator is bounded. It can be checked that this regular property is not preserved by the transformation. Instead of arithmetic expressions, if we consider Boolean expressions made up of ∧\wedge and ∨\vee operators, we can reason about expressions being in normal forms like conjunctive normal form (CNF), disjunctive normal form (DNF) etc. We consider the type-checking problem in general where the source trees have a property represented by a regular language LsrcL_{src} and output trees are required to have a property represented by another (possibly different) regular language LtgtL_{tgt}.

The main idea of our decision procedure for the type-checking problem is the following observation: if the given instance of the type-checking problem is a negative instance, a counterexample is a single tree in the seed language SS, satisfying multiple conditions under multiple transformations. On the source side, applying the morphism ϕsrc\phi_{src} to the seed tree and then applying a substitution for the placeholder variables should result in a tree in LsrcL_{src}. On the output side, applying the morphism ϕtgt\phi_{tgt} to the same seed tree and then applying the same substitution for the placeholder variables should result in a tree not in LtgtL_{tgt}. These conditions can be checked by an alternating tree automaton running on trees in the seed language, thus reducing the type-checking problem to the emptiness-checking of alternating tree automata.

Along the way, we consider some related/simpler problems. The simple question of whether a given tree matches a given pattern is NP-complete, a result known already for words [4]. The special case of the type-checking problem where the transformation is given by a single pair of patterns is EXPTime-complete. This generalises the work of pattern-based word transformations [1] to trees.

Pattern-based word transformations were considered in [1], but these consisted only of a single pair of patterns. We extend this in two ways — by having transformations of trees, and by having a formalism that consists of potentially infinitely many pairs of patterns.

There are many transducer models for trees. Our transformations subsume classical top-down and bottom-up tree transducers, even without any variables, thanks to the bimorphism feature. In fact it subsumes any relation on trees definable by bimorphisms [4 Chapter 6]. However it is incomparable to streaming tree transducers [2] or macro tree transducers [7]. The domains of transformations defined by the latter are regular tree languages, which need not be the case in our model due to the presence of variables. On the other hand our model cannot recognize transformations which reverse a word (a tree with unary alphabet and an end marker as a leaf), while macro tree transducers and streaming tree transducers can. For exactly the same reasons, our model is incomparable with kk-Pebble Transducers [14]. See Appendix § 10 for details.

A model of transition systems was studied in [9] where states are unranked, unordered forests and edges are given by tree rewriting using a pattern-based query language. These patterns are weaker than our patterns since a variable can represent only a single node in the tree and not a subtree like in our patterns. Reachability in this transition system is undecidable, but if the depth of every forest in the transition system is bounded it becomes decidable though nonelementary. Our type-checking problem is not addressing reachability in a transition system, but only a one-step rewriting. While the decidability result in [9] is limited to bounded depth trees, our model can handle unrestricted trees. Further, all our algorithms are of elementary complexity.

Paper organisation

In Section 2, we illustrate the correspondence between the artefacts of our model and the parts of trees and transformations that the model represents. We recall some preliminaries about trees, contexts, nondeterministic tree automata and regular tree languages in Section 3.1 and formally define the model in Section 3.2. In Section 4, we give some examples of possible applications, which motivate our model as well as the type-checking problem that we consider, which is formally defined in Section 5. We give some intuition underlying the decision procedure in Section 6, motivating the concept of alternating tree automata and relational lifts that are formally defined in Section 7. The proof of decidability is put together in Section 8. Some of the detailed proofs are moved to the appendix due to space constraints.

Illustrating Example

As mentioned in the introduction, our objective of this section is to illustrate our model through an example. We want to capture transformations of the form depicted in Figure 1. We will define a suitable seed language and give bimorphisms that attain this.

Figure 1. The transformation of an expression following distributivity. Here ff, e1,…,e6e_1,\ldots,e_6 are arbitrary expressions. If ff is a variable instead of an expression, we get a (source pattern, target pattern) pair that captures this transformation.

Consider the transformation depicted in Figure 1. The blue shaded parts in the source expression and the target expression remain unchanged. Expressions are formed by the alphabet {+,×,0,1}\{+, \times, 0, 1\}. The patterns would require a placeholder symbol yy in addition to the above alphabet. In order to generate the (source pattern, target pattern) pair capturing this transformation, we require a seed tree which carries this blue shaded part intact. We expect the morphisms ϕsrc\phi_{src} and ϕtgt\phi_{tgt} to be identity on this part. However, yy gets inserted above eie_i in the source pattern, and at the root in the target pattern. In order to handle this we add new unary symbols aa and bb in the seed tree.

The seed tree and the morphisms ϕsrc,ϕtgt\phi_{src},\phi_{tgt} for this transformation are depicted in Figure 2. Note that, we require one such seed tree for each possible choice of eie_i and the shape of the “++-kernel”, thus giving rise to an infinite seed language and hence an infinite set of (source pattern, target pattern) pairs capturing the transformation. Thankfully, the set of seed trees is tree-regular (need to check that the root is an aa, every path has exactly one bb, and the segment between an aa and bb has only ++s).

Figure 2. The seed tree and the morphisms generating the (source pattern, target pattern) pair capturing the transformation depicted in Figure 1.

We now give an example of type-checking problem on this transformation. We define the product-depth of an expression as follows. The product-depth (pd) of a branch is the number of occurrences of ×\times in that branch. The product-depth of an expression is the maximum of the product-depths of its branches. The classes of kk-pd-bounded expressions are those with product-depth at most kk. We may be interested in checking whether kk-pd-boundedness is preserved by the above transformation. This is an instance of type-checking problem, since kk-pd-bounded expressions form a regular set of trees.

Further examples, in particular the ones where we need multiple variables, are presented after formally defining our model, which is done next.

Formal Model

Preliminaries

Sets and Relations

Let N:={1,2,3,…}\mathbb{N} := \{1, 2, 3, \ldots\} be the set of strictly positive integers and let N0:=N∪{0}\mathbb{N}_0 := \mathbb{N} \cup\{0\}. For n∈Nn \in\mathbb{N}, we denote the set {1,…,n}\{1, \ldots, n\} by [n][n]. As a convention, we let [0]:=∅[0] := \emptyset. For a set XX and n∈Nn \in\mathbb{N}, XnX^n denotes the usual Cartesian power of XX, consisting of all nn-tuples of elements in XX. As a convention, we let X0:={♮}X^0 := \{\natural\}, a special singleton set.

For n∈N0n \in\mathbb{N}_0, an nn-ary relation over XX is a subset R⊆XnR \subseteq X^n. We denote the set of all nn-ary relations on XX by Relations⁡(X,n)\operatorname{Relations}(X,n). Finally, we denote ⋃k=0∞Relations⁡(X,k)\bigcup_{k=0}^{\infty} \operatorname{Relations}(X,k) by Relations⁡(X)\operatorname{Relations}(X).

We consider automata over finite, ranked trees, following definitions from [4].

Definition 1 (Alphabet). A finite set Σ:={a1/k1,…,an/kn}\Sigma:= \{a_1/k_1, \ldots, a_n/k_n\} is called a finite ranked alphabet or alphabet, where aia_i are called the letters, and ki:=arity⁡(ai)k_i := \operatorname{arity}(a_i) are nonnegative integers called their corresponding arities for all i∈[n]i \in[n]. We define pairwise disjoint sets Σk:={a∈Σ∣arity⁡(a)=k}⊆Σ\Sigma_k := \{a \in\Sigma\mid\operatorname{arity}(a) = k\} \subseteq\Sigma for all k∈N0k \in\mathbb{N}_0. We require that Σ0≠∅\Sigma_0 \ne\emptyset. We also define the arity of Σ:=max⁡iki\Sigma:= \max_i k_i.

Definition 2 (Term, Tree). A term or a tree over a ranked alphabet Σ\Sigma is a partial function t:N∗⇀Σt : \mathbb{N}^{*} \rightharpoonup\Sigma with the domain Positions⁡(t)\operatorname{Positions}(t) satisfying:

  • Positions⁡(t)\operatorname{Positions}(t) is nonempty and prefix-closed.

  • ∀u∈Positions⁡(t), t(u)∈Σk, k∈N0⟹{j∣uj∈Positions⁡(t)}=[k]\forall u \in\operatorname{Positions}(t),\ t(u) \in\Sigma_k,\ k \in\mathbb{N}_0 \Longrightarrow\{j \mid uj \in\operatorname{Positions}(t)\} = [k].

We say that a tree tt is finite if Positions⁡(t)\operatorname{Positions}(t) is finite.

In this paper, we will only deal with finite trees. We denote the set of all finite trees over Σ\Sigma by Trees⁡(Σ)\operatorname{Trees}(\Sigma). A tree language is any subset of Trees⁡(Σ)\operatorname{Trees}(\Sigma). Regular tree languages are those recognised by tree automata as defined below.

Definition 3 (Nondeterministic Bottom-up Tree {Transition System, Automaton}). Let Σ\Sigma be a finite alphabet. A Nondeterministic Bottom-up Tree Transition System or NBTTS over Σ\Sigma is defined as T:=(Q,Σ,Δ)\mathcal{T} := (Q, \Sigma, \Delta), where QQ is a finite set of states, and Δ:Σ→Relations(Q)\Delta: \Sigma\to\mathit{Relations}(Q) is a function such that Δ(a/k)∈Relations(Q,k+1)\Delta(a/k) \in\mathit{Relations}(Q,k+1), for all a/k∈Σa/k \in\Sigma. A Nondeterministic Bottom-up Tree Automaton or an NBTA is a tuple A:=(Q,Σ,Δ,F)\mathcal{A} := (Q, \Sigma, \Delta,F), where (Q,Σ,Δ)(Q, \Sigma, \Delta) is a NBTTS over Σ\Sigma and F⊆QF \subseteq Q is a set of final states. If for each a/k∈Σa/k \in\Sigma we have that Δ(a/k)\Delta(a/k) is such that for all q1,…,qk∈Qq_1,\ldots,q_k \in Q, there exists a unique q∈Qq \in Q such that (q1,…,qk,q)∈Δ(a)(q_1,\ldots,q_k,q) \in\Delta(a), then the corresponding NBTTS is called a Deterministic Bottom-up Tree Transition System or DBTTS, and the NBTA is called a Deterministic Bottom-up Tree Automaton or DBTA.

Definition 4 ({Run, Result, Acceptance} for NBTA). Given an NBTTS T:=(Q,Σ,Δ)\mathcal{T} := (Q, \Sigma, \Delta) and a tree t∈Trees(Σ)t \in\mathit{Trees}(\Sigma), a run of T\mathcal{T} on tt is defined as a tree ρ:Positions(t)→Q\rho: \mathit{Positions}(t) \to Q which is ‘compatible’ with Δ\Delta, i.e., for every position u∈Positions(t)u \in\mathit{Positions}(t), if t(u)=a/kt(u) = a/k, ρ(u)=q\rho(u) = q and ρ(ui):=qi\rho(ui) := q_i for all i∈[k]i \in[k], then, (q1,…,qk,q)∈Δ(a)(q_1,\ldots,q_k,q) \in\Delta(a). The result of a run ρ\rho is result(ρ):=ρ(ε)\mathit{result}(\rho) := \rho(\varepsilon). Given an NBTA A:=(Q,Σ,Δ,F)\mathcal{A} := (Q, \Sigma, \Delta,F), and a tree t∈Trees(Σ)t \in\mathit{Trees}(\Sigma), we say that tt is accepted by A\mathcal{A} if and only if there exists a run ρ\rho of (Q,Σ,Δ)(Q, \Sigma, \Delta) on tt such that result(ρ)∈F\mathit{result}(\rho) \in F. The set of all trees tt accepted by a NBTA A\mathcal{A} is called the language recognised by A\mathcal{A}, and is denoted by L(A)⊆Trees(Σ)L(\mathcal{A}) \subseteq\mathit{Trees}(\Sigma).

Just like a word ww can be extended by appending another word to its end, a tree can be extended by appending other trees to designated leaves. This is formalised below, where ⊎\uplus denotes disjoint union.

Definition 5 (Context). A context with arity nn or an nn-context over Σ\Sigma is a tree over the alphabet Σ⊎{□1/0,…,□n/0}\Sigma\uplus\{\square_1/0,\ldots,\square_n/0\} such that each letter □i\square_i appears exactly once in the tree, for all i∈[n]i \in[n]. We refer to the positions occupied by □i\square_i as the holes of the context for all i∈[n]i \in[n].

In this paper, we shall only deal with finite contexts. Intuitively, the leaves labeled with □1,…,□n\square_1,\ldots,\square_n, or the holes of the context are the designated positions where other trees can be appended. We denote the set of all finite nn-contexts over Σ\Sigma by Contextsn(Σ)\mathit{Contexts}_n(\Sigma). We denote ⋃n=0∞Contextsn(Σ)\bigcup_{n=0}^{\infty}\mathit{Contexts}_n(\Sigma) by Contexts(Σ)\mathit{Contexts}(\Sigma), where we let Contexts0(Σ):=Trees(Σ)\mathit{Contexts}_0(\Sigma) := \mathit{Trees}(\Sigma).

Tree Transformations

In this section, we define our tree transformation model and describe some associated problems that we wish to study. We start by defining and describing tree homomorphisms, which are essentially extensions of homomorphisms from words to trees. We more or less follow the definition as presented in [4], with minor modifications for ease of writing and mathematical rigour.

Definition 6 (Tree Homomorphism). Let Σ,Γ\Sigma,\Gamma be two finite ranked alphabets, possibly not disjoint. Let X:={xi/0∣i∈N}X := \{x_i/0 \mid i \in\mathbb{N}\} be a countable set of variables of arity zero that is pairwise disjoint from Σ,Γ\Sigma,\Gamma. For each n∈N0n \in\mathbb{N}_0, define the section of XX by nn by Xn:={xi/0∣i∈[n]}X_n := \{x_i/0 \mid i \in[n]\}. Note that with this definition, we also get that X0=∅X_0 = \varnothing.

Let h:Σ→Trees(Γ⊎X)h : \Sigma\to\mathit{Trees}(\Gamma\uplus X) be a mapping such that h(f)∈Trees(Γ⊎Xarity(f))h(f) \in\mathit{Trees}(\Gamma\uplus X_{\mathit{arity}(f)}) for all f∈Σf \in\Sigma. Then, we define the tree homomorphism h~:Trees(Σ)→Trees(Γ)\widetilde{h} : \mathit{Trees}(\Sigma) \to\mathit{Trees}(\Gamma) determined by hh as follows:

  • h~(a):=h(a)∈Trees(Γ)\widetilde{h}(a) := h(a) \in\mathit{Trees}(\Gamma) for each a∈Σa \in\Sigma with arity 00.

  • h~(f(t1,…,tn)):=h(f){xi←h~(ti)∣i∈[n]}\widetilde{h}(f(t_1,\ldots,t_n)) := h(f)\{x_i \leftarrow\widetilde{h}(t_i) \mid i \in[n]\} for each f/n∈Σf/n \in\Sigma, where the right-hand side is the result of replacing all occurrences of the symbol xix_i in h(f)h(f) by the term h~(ti)\widetilde{h}(t_i) for all i∈[n]i \in[n].

By abuse of notation, whenever there is no ambiguity, we shall drop the tilde and just refer to the tree homomorphism h~\tilde{h} determined by hh just as simply hh.

We let Σ\Sigma be a finite ranked alphabet. We will need trees, some parts of which are not completely specified and we use variables to denote those parts. Let Var⁡\operatorname{Var} be a countable set of ranked variables or variables. For a variable x∈Var⁡x \in\operatorname{Var}, arity⁡(x)∈N0\operatorname{arity}(x) \in\mathbb{N}_0 denotes its arity. We define pairwise disjoint sets Var⁡k:={x∈Var⁡∣arity⁡(x)=k}⊆Var⁡\operatorname{Var}_k := \{x \in\operatorname{Var} \mid\operatorname{arity}(x) = k\} \subseteq\operatorname{Var} for all k∈Nk \in\mathbb{N}. Further we also define the degree of a variable xx as deg⁡(x):=1+arity⁡(x)\deg(x) := 1 + \operatorname{arity}(x).

Definition 7 (Pattern). A tree pattern or simply a pattern over (Σ,Var⁡)(\Sigma, \operatorname{Var}) is defined to be a tree Pat⁡∈Trees⁡(Σ⊎Var⁡)\operatorname{Pat} \in\operatorname{Trees}(\Sigma\uplus\operatorname{Var}). We also let Var⁡(Pat⁡)\operatorname{Var}(\operatorname{Pat}) be the set of all variables appearing in Pat⁡\operatorname{Pat}. Note that Var⁡(Pat⁡)⊆Var⁡\operatorname{Var}(\operatorname{Pat}) \subseteq\operatorname{Var} is necessarily finite. We also define the degree of a pattern Pat⁡\operatorname{Pat} denoted by deg⁡(Pat⁡):=max⁡x∈Var⁡(Pat⁡)deg⁡(x)\deg(\operatorname{Pat}) := \max_{x \in\operatorname{Var}(\operatorname{Pat})} \deg(x).

A pattern can represent multiple trees by substituting variables in the pattern by contexts of the same arity.

Definition 8 (Substitution). A substitution over Σ\Sigma is a partial function σ:Var⁡⇀Contexts⁡(Σ)\sigma: \operatorname{Var} \rightharpoonup\operatorname{Contexts}(\Sigma) such that for all x∈Domain⁡(σ)x \in\operatorname{Domain}(\sigma), we have that σ(x)∈Contexts⁡k(Σ)\sigma(x) \in\operatorname{Contexts}_k(\Sigma), where k=arity⁡(x)k = \operatorname{arity}(x).

Given a substitution σ\sigma, we can naturally extend it to the set of patterns over Σ\Sigma as follows. Define a tree homomorphism σ~:Trees⁡(Σ⊎Domain⁡(σ))→Trees⁡(Σ)\tilde{\sigma} : \operatorname{Trees}(\Sigma\uplus\operatorname{Domain}(\sigma)) \to\operatorname{Trees}(\Sigma) by setting

σ~(x):={xif x∈Σ;σ(x)if x∈Domain⁡(σ)\tilde{\sigma}(x) := \begin{cases} x & \text{if } x \in\Sigma;\\ \sigma(x) & \text{if } x \in\operatorname{Domain}(\sigma) \end{cases}

and extending it homomorphically. Since σ~∣Domain⁡(σ)=σ\tilde{\sigma}|_{\operatorname{Domain}(\sigma)} = \sigma, by abuse of notation we shall write σ~\tilde{\sigma} as σ\sigma in places where the distinction is irrelevant.

We would like to control the set of contexts that can be substituted for variables in patterns, for which we use guards. A guard over a finite alphabet Σ\Sigma is a partial function G:Var⁡→2Trees⁡(Σ)\mathcal{G} : \operatorname{Var} \to2^{\operatorname{Trees}(\Sigma)}, which assigns to each variable in its domain, a tree language over Σ\Sigma. The following formally defines when a pattern matches a tree.

Definition 9 (Match). Let Pat⁡\operatorname{Pat} be a pattern over (Σ,Var⁡)(\Sigma, \operatorname{Var}) and t∈Trees⁡(Σ)t \in\operatorname{Trees}(\Sigma) be a tree. Let G\mathcal{G} be a guard such that Var⁡(Pat⁡)⊆Domain⁡(G)\operatorname{Var}(\operatorname{Pat}) \subseteq\operatorname{Domain}(\mathcal{G}). We say that tt matches pattern Pat⁡\operatorname{Pat} under G\mathcal{G} if and only if there exists a substitution σ\sigma such that

  • Var⁡(Pat⁡)⊆Domain⁡(σ)⊆Domain⁡(G)\operatorname{Var}(\operatorname{Pat}) \subseteq\operatorname{Domain}(\sigma) \subseteq\operatorname{Domain}(\mathcal{G}).

  • σ(x)∈G(x)\sigma(x) \in\mathcal{G}(x) for each x∈Var⁡(Pat⁡)x \in\operatorname{Var}(\operatorname{Pat}).

  • σ(Pat⁡)=t\sigma(\operatorname{Pat}) = t.

In such a case, σ\sigma is called a matching substitution, and we say the substitution σ\sigma matches the tree tt to the pattern Pat⁡\operatorname{Pat} under the guard G\mathcal{G}.

There may exist more than one matching substitution for a given match. Eg., if a/1,b/1,c/0a/1, b/1, c/0 are letters and x/1,y/0x/1, y/0 are variables, the tree a−b−b−ca-b-b-c matches the pattern x−b−yx-b-y with 2 distinct substitutions σ1,σ2\sigma_1, \sigma_2 given by σ1(x):=a\sigma_1(x) := a, σ1(y):=b−c\sigma_1(y) := b-c whereas σ2(x):=a−b\sigma_2(x) := a-b, σ2(y):=c\sigma_2(y) := c.

The following formalises the tree transformations defined by our model.

Definition 10 (Atomic Transformation). Let Σ\Sigma be a finite alphabet and Var⁡\operatorname{Var} be a countable set of variables. An atomic tree transformation or simply, an atomic transformation is an ordered triple T:=(Pat⁡src,Pat⁡tgt,G)T := (\operatorname{Pat}_{\mathrm{src}}, \operatorname{Pat}_{\mathrm{tgt}}, \mathcal{G}), where Pat⁡src,Pat⁡tgt\operatorname{Pat}_{\mathrm{src}}, \operatorname{Pat}_{\mathrm{tgt}} are patterns over (Σ,Var⁡)(\Sigma, \operatorname{Var}) called

the source pattern and target pattern respectively, and GG is a guard over Σ\Sigma such that we have Var⁡(T):=Var⁡(Patsrc)∪Var⁡(Pattgt)⊆Domain⁡(G)\operatorname{Var}(T) := \operatorname{Var}(\mathrm{Pat}_{src}) \cup\operatorname{Var}(\mathrm{Pat}_{tgt}) \subseteq\operatorname{Domain}(G). The relation over trees induced by TT is

Rel⁡(T):={(t,t′)∣∃σ such that σ matches t to Patsrc under Gand σ matches t′ to Pattgt under G}\operatorname{Rel}(T) := \{(t,t') \mid\exists\sigma\text{ such that } \sigma\text{ matches } t \text{ to } \mathrm{Pat}_{src} \text{ under } G \\ \text{and } \sigma\text{ matches } t' \text{ to } \mathrm{Pat}_{tgt} \text{ under } G\}

We denote by T(t)={t′∣(t,t′)∈Rel⁡(T)}⊆Trees⁡(Σ)T(t) = \{t' \mid(t,t') \in\operatorname{Rel}(T)\} \subseteq\operatorname{Trees}(\Sigma) the effect of TT on the tree tt. We naturally extend the effect of TT to tree languages LL by setting T(L):=⋃t∈LT(t)T(L) := \bigcup_{t \in L} T(t). We also define deg⁡(T):=max⁡x∈Var⁡(T)deg⁡(x)\deg(T) := \max_{x \in\operatorname{Var}(T)} \deg(x). Our model consists of a possibly infinite set of atomic transformations, represented by a bimorphism.

Definition 11 (Transformation). Let Σ,Γ\Sigma,\Gamma be two finite alphabets. Let VarVar be a countably infinite set of variables. A transformation T\mathbf{T} is a tuple T:=(S,ϕsrc,ϕtgt,G)\mathbf{T} := (S,\phi_{src},\phi_{tgt},G) where

  • S⊆Trees⁡(Γ)S \subseteq\operatorname{Trees}(\Gamma) is called the seed language.

  • ϕsrc,ϕtgt:Trees⁡(Γ)→Trees⁡(Σ⊎Var)\phi_{src}, \phi_{tgt} : \operatorname{Trees}(\Gamma) \to\operatorname{Trees}(\Sigma\uplus Var) are tree homomorphisms.

  • GG is a guard over Σ\Sigma.

We define the relation induced by a transformation T:=(S,ϕsrc,ϕtgt,G)\mathbf{T} := (S,\phi_{src},\phi_{tgt},G) as follows: for each seed tree s∈Ss \in S, we define the atomic transformation Ts:=(ϕsrc(s),ϕtgt(s),G)T_s := (\phi_{src}(s),\phi_{tgt}(s),G). Then Rel⁡(T):=⋃s∈SRel⁡(Ts)\operatorname{Rel}(\mathbf{T}) := \bigcup_{s \in S} \operatorname{Rel}(T_s)

For a tree tt and a tree language LL, T(t)={t′∣(t,t′)∈Rel⁡(T)}⊆Trees⁡(Σ)T(t) = \{t' \mid(t,t') \in\operatorname{Rel}(T)\} \subseteq\operatorname{Trees}(\Sigma) and T(L):=⋃t∈LT(t)T(L) := \bigcup_{t \in L} T(t) as before. We further define Var⁡(T)=⋃γ∈ΓVar⁡(ϕsrc(γ))∪Var⁡(ϕtgt(γ))\operatorname{Var}(\mathbf{T}) = \bigcup_{\gamma\in\Gamma} \operatorname{Var}(\phi_{src}(\gamma)) \cup\operatorname{Var}(\phi_{tgt}(\gamma)) and deg⁡(T)=max⁡x∈Var⁡(T)deg⁡(x)\deg(\mathbf{T}) = \max_{x \in\operatorname{Var}(\mathbf{T})} \deg(x).

Example - Code Snippet Translation

As a motivating example, we consider a piece of code, written in a language like C++ or Java, containing a certain bounded number of for loops. We would like to transform this into a corresponding piece of code with while loops. A concrete example is depicted in Figure 3.

Source and target code snippets for Selection Sort

Source code

int[] A = new int[m];
for(int i=0;i<A.length;i++) {
    A[i] = getInputFromUser();
}
for(int j=0;j<A.length;j++) {
    int minIndex = j;
    int minValue = A[minIndex];
    for(int k=j;k<A.length;k++) {
        if(A[k] < minValue) {
            minIndex = k;
            minValue = A[k];
        }
    }
    int temp = A[j];
    int A[j] = A[k];
    int A[k] = temp;
}
return A;

Target code

int[] A = new int[m];
int i=0;
while (i<A.length) {
    A[i] = getInputFromUser();
    i++;
}
int j=0;
while (j<A.length) {
    int minIndex = j;
    int minValue = A[minIndex];
    int k=j;
    while(k<A.length) {
        if (A[k] < minValue) {
            minIndex = k;
            minValue = A[k];
        }
        k++;
    }
    int temp = A[j];
    int A[j] = A[k];
    int A[k] = temp;
    j++;
}
return A;

Figure 3. The source and target code snippets for Selection Sort, before and after transforming for loops to while loops.

Let us bound the number of for loops in our programs by some fixed n∈Nn \in\mathbb{N}. Consider the alphabet Σ:=Σcore⊎{for/5,while/3,null/0}\Sigma:= \Sigma_{\mathrm{core}} \uplus\{\mathrm{for}/5,\mathrm{while}/3,\mathrm{null}/0\} where the unary Σcore\Sigma_{\mathrm{core}} refers to the alphabet used for programming, which for instance, could be all the characters appearing on a standard keyboard. With this alphabet, the source and target codes are seen as trees, as depicted in Figure 4. Long chains of characters from Σcore\Sigma_{\mathrm{core}} are written as mere sequences wrapped in a box for the sake of readability and space.

Source and target code snippets represented as trees

Figure 4. Source and target code snippets from Figure 3 as trees.

We would like to capture these codes with patterns containing variables, which will allow us to move around the sections of code as needed. Let Var⁡:={xiinit/1,xicond/1,xiincr/1∣i∈[n]}\operatorname{Var} := \{x_i^{\mathrm{init}}/1, x_i^{\mathrm{cond}}/1, x_i^{\mathrm{incr}}/1 \mid i \in[n]\} be the set of variables. Recall that nn, bound on the number of for loops in our programs, is a constant and hence we have only a fixed finite number of variables.

The source and target patterns are given in Figure 6 and a seed tree generating these patterns is given in Figure 5. Note that the seed alphabet is given by Γ:=Σcore⊎{loopi/2,endLoopi/0∣i∈[n]}⊎{null/0}\Gamma:= \Sigma_{\mathrm{core}} \uplus\{\mathrm{loop}_i/2, \mathrm{endLoop}_i/0 \mid i \in[n]\} \uplus\{\mathrm{null}/0\}.

The seed tree generating the source and target patterns in Figure 6

Figure 5. The seed tree generating the source and target patterns in Figure 6.

Source and target patterns for code snippet translation

Figure 6. Source and target patterns. The source and target morphisms are identity everywhere, except for loopi_i and endLoopi_i, which are indicated by colors blue and red respectively.

In general, the seed language will contain all trees in Trees⁡(Γ)\operatorname{Trees}(\Gamma) which contain each of the letters loopi\mathrm{loop}_i and endLoopi\mathrm{endLoop}_i at most once and are well-bracketed. Let us take a detailed look at how the language looks like. Note that we a priori limit the scope of code we transform to instances having at most nn loops, where nn is a fixed natural number. We start with the various alphabets we use:

  • Tree alphabet Σ:=Σcore⊎{for/5,while/3,null/0}\Sigma:= \Sigma_{\mathrm{core}} \uplus\{\mathrm{for}/5,\mathrm{while}/3,\mathrm{null}/0\}, where Σcore\Sigma_{\mathrm{core}} is, for example, all the symbols available on a standard keyboard, each with an arity of 1.

  • Pattern alphabet Σ⊎Var⁡\Sigma\uplus\operatorname{Var}, where Var⁡:={xiinit/1,xicond/1,xiincr/1∣i∈[n]}\operatorname{Var} := \{x_i^{\mathrm{init}}/1, x_i^{\mathrm{cond}}/1, x_i^{\mathrm{incr}}/1 \mid i \in[n]\}.

  • Seed alphabet Γ:=Σcore⊎{loopi/2,endLoopi/0∣i∈[n]}⊎{null/0}\Gamma:= \Sigma_{\mathrm{core}} \uplus\{\mathrm{loop}_i/2,\mathrm{endLoop}_i/0 \mid i \in[n]\} \uplus\{\mathrm{null}/0\}.

In the seed alphabet, we have binary letters loopi\mathrm{loop}_i for all i∈[n]i \in[n]. We interpret this as follows: the letter loopi\mathrm{loop}_i is a placeholder for the ithi^{\mathrm{th}} loop in the program. The left child of the letter will be a tree representing the part of the code that is inside the loop, ending with an endLoopi\mathrm{endLoop}_i, which is a leaf letter to signify the end of the loop. The right child will be a tree representing the part of the code that follows after the loop, eventually ending with a null leaf letter. Hence in the loop language, we would want that each pair of letters (loopi,endLoopi)(\mathrm{loop}_i,\mathrm{endLoop}_i) which signify loop initiation and loop termination respectively to appear at most once. This is a necessary ‘hygiene’ condition upon which we will build more structure.

For this condition to hold, we define for each i∈[n]i \in[n], a finite-state tree automaton

Ui:=({q0,q1,q2,q3,q4},Γ,Δi,{q0,q3}) where Δi is given by\mathcal{U}_i := (\{q_0,q_1,q_2,q_3,q_4\},\Gamma,\Delta_i,\{q_0,q_3\}) \text{ where } \Delta_i \text{ is given by}
Δi:={(endLoop⁡i,q1),(null⁡,q0)}∪{(endLoop⁡j,q0)∣j∈[n],j≠i}∪{(qx,qy,loop⁡i,qmin⁡(4,x+y+2))}∪{(qx,a,qx)∣x∈{0,1,2,3,4}}\Delta_i := \{(\operatorname{endLoop}_i,q_1),(\operatorname{null},q_0)\} \cup\{(\operatorname{endLoop}_j,q_0)\mid j\in[n],j\ne i\} \cup\{(q_x,q_y,\operatorname{loop}_i,q_{\min(4,x+y+2)})\}\cup\{(q_x,a,q_x)\mid x\in\{0,1,2,3,4\}\}
∪{(qx,qy,loop⁡j,qmin⁡(4,x+y))∣j∈[n],j≠i,(x,y)≠(1,1)}∪{(q1,q1,loop⁡j,q4)}\cup\{(q_x,q_y,\operatorname{loop}_j,q_{\min(4,x+y)})\mid j\in[n],j\ne i,(x,y)\ne(1,1)\} \cup\{(q_1,q_1,\operatorname{loop}_j,q_4)\}

We see that each of the automata Ui\mathcal{U}_i accepts all trees ss over Γ\Gamma such that the letters loop⁡i,endLoop⁡i\operatorname{loop}_i,\operatorname{endLoop}_i both appear either once each, or not at all in ss. Thus having S⊆[⋂i=1nL(Ui)]S\subseteq\left[\bigcap_{i=1}^{n}L(\mathcal{U}_i)\right] implies that every tree ss contains the pair of letters loop⁡i,endLoop⁡i\operatorname{loop}_i,\operatorname{endLoop}_i at most once.

On top of this requirement, we also need that every tree in the seed language be “well-nested” in terms of the containment of loops. For this, we define an automaton A:=(Q,Γ,Δ,F)\mathcal{A}:=(Q,\Gamma,\Delta,F) as:

Q:={qi∣i∈[n]}∪{q0}Q:=\{q_i\mid i\in[n]\}\cup\{q_0\}
F:={q0}F:=\{q_0\}
Δ:={(null⁡,q0)}∪{(endLoop⁡i,qi)∣i∈[n]}∪{(qi,qj,loop⁡i,qj)∣i,j∈[n]∪{0}}∪{(q,a,q)∣q∈Q,a∈Σcore}\Delta:=\{(\operatorname{null},q_0)\}\cup\{(\operatorname{endLoop}_i,q_i)\mid i\in[n]\}\cup\{(q_i,q_j,\operatorname{loop}_i,q_j)\mid i,j\in[n]\cup\{0\}\}\cup\{(q,a,q)\mid q\in Q,a\in\Sigma_{\mathrm{core}}\}

We demonstrate the working of this automaton on an example word we would like to have in the seed language. Below, we consider a tree ss over the seed alphabet Γ\Gamma for the case n=9n=9, along with a run of the automaton A\mathcal{A} shown in parenthesis, in blue. Note that the dashed edges represent the presence of any number of unary letters from Σcore\Sigma_{\mathrm{core}}; since they “bubble up” any state, they can be represented as such for purposes of better understanding.

Figure 1 (PDF p. 13).

Finally, we define the seed language to be S:=L(A)∩[⋂i=1nL(Ui)]S:=L(\mathcal{A})\cap\left[\bigcap_{i=1}^{n}L(\mathcal{U}_i)\right], which is a regular language.

In general, we see that for any program with no more than nn for loops, we will be able to find a seed tree, which will give us a source pattern and a target pattern through homomorphisms ϕsrc,ϕtgt\phi_{\mathrm{src}},\phi_{\mathrm{tgt}} respectively. The source pattern will match the program code with not more than nn for loops with a substitution, which when applied to the target pattern will give us the transformed version of the program code which has all for loops transformed into while loops.

We may want to verify that applying a transformation to source trees belonging to some regular language (such as syntactically correct programs that use for loops) will never result in target trees violating desired properties (such as syntactically correct programs that use while loops). This motivates the problem we consider for our model, defined next.

Decision Problems for Tree Transformations

A simple question is to check whether there is a matching.

Matching Problem
Input:Pattern Pat, tree tt, guard G\mathcal{G}. // The guard languages are represented by NBTA.
Question:Does tt match Pat under G\mathcal{G}?

Table Matching Problem.

We will show that this problem is already NP-complete.

Theorem 12 ([13]). The Matching Problem is NP-complete.

Proof. (Upper Bound) Let (Pat,t,G)(\mathrm{Pat},t,\mathcal{G}) be an instance of Matching Problem, where Pat is a pattern over (Σ,Var)(\Sigma,\mathrm{Var}), t∈Trees⁡(Σ)t \in\operatorname{Trees}(\Sigma) and G:Var→2Trees⁡(Σ)\mathcal{G}:\mathrm{Var}\to2^{\operatorname{Trees}(\Sigma)} where each value of the function is given by an NBTA. Let the size of the input be N:=∣Pat∣+∣t∣+∣G∣N := |\mathrm{Pat}|+|t|+|\mathcal{G}|. Given a certificate, which is a particular substitution whose size is bounded by ∣t∣|t|, we can verify whether σ(Pat)=t\sigma(\mathrm{Pat})=t in O(∣t∣)O(|t|) time, which is linear in the size of the input NN. We can also verify that for every x∈Var⁡(Pat)x \in\operatorname{Var}(\mathrm{Pat}), σ(x)∈G(x)\sigma(x)\in\mathcal{G}(x). Since there exists a polynomial-time verifier algorithm for the problem, we see that Matching Problem is in NP.

(Lower Bound) For this proof, we present a version adapted from [13]. The proof proceeds by establishing a reduction from 1-in-3 SAT, which is defined below.

1-in-3 SAT
Input:3CNF formula ϕ\phi without negated variables.
Question:Is there a truth assignment to the variables of ϕ\phi such that every clause contains exactly one literal that is true and ϕ\phi evaluates to true?

Table 1-in-3 SAT.

We know from [16] that 1-in-3 SAT is NP-complete. Consider the following instance {J(vf(3i−2),vf(3i−1),vf(3i))∣i∈[n]}\{J(v_{f(3i-2)},v_{f(3i-1)},v_{f(3i)})\mid i\in[n]\} of 1-in-3 SAT, where V:={v1,…,vk}V:=\{v_1,\ldots,v_k\} is a finite set of variables and f:[3n]→[k]f:[3n]\to[k]. We also have JJ to be a function which evaluates to true iff exactly one of its arguments is set to true, and false otherwise.

Let Σ:={a/1,#/1,λ/0}\Sigma:=\{a/1,\mathbin{\#}/1,\lambda/0\} be a ranked alphabet and Var:={xi/1∣i∈N}\mathrm{Var}:=\{x_i/1\mid i\in\mathbb{N}\} be a set of ranked variables. Consider a pattern Pat over (Σ,Var)(\Sigma,\mathrm{Var}) given by

xf(1)xf(2)xf(3)#⋯#xf(3i−2)xf(3i−1)xf(3i)#⋯#xf(3n−2)xf(3n−1)xf(3n)λ.x_{f(1)}x_{f(2)}x_{f(3)}\mathbin{\#}\cdots\mathbin{\#}x_{f(3i-2)}x_{f(3i-1)}x_{f(3i)}\mathbin{\#}\cdots\mathbin{\#}x_{f(3n-2)}x_{f(3n-1)}x_{f(3n)}\lambda.

and a tree over Σ\Sigma given by t:=a#a#⋯#a#⋯#at:=a\mathbin{\#}a\mathbin{\#}\cdots\mathbin{\#}a\mathbin{\#}\cdots\mathbin{\#}a, where the number of aa’s in the tree is nn. We see that the only substitution σ:V→{a,ε}\sigma:V\to\{a,\varepsilon\} which matches tt to α\alpha is the one which assigns each true variable to aa and every false variable to ε\varepsilon. The guard languages can be specified easily by a simple NBTA. Hence we get that a solution for the original instance of 1-in-3 SAT exists if and only if a solution for the corresponding Matching Problem has a solution. This completes the reduction.

Next we consider the reachability problem for atomic transformations and establish it is EXPTime-complete.

Atomic Transformation Reachability Problem
Input:Atomic transformation T:=(Patsrc,Pattgt,G)T := (\mathrm{Pat}_{src},\mathrm{Pat}_{tgt},\mathcal{G}), source language LsrcL_{src}, target language LtgtL_{tgt}. // The languages are represented by NBTA.
Question:Do we have that T(Lsrc)∩Ltgt≠∅T(L_{src})\cap L_{tgt}\ne\varnothing?

Table Atomic Transformation Reachability Problem.

Theorem 13. The Atomic Transformation Reachability Problem is EXPTIME-complete.

We give a proof which is a special case of the Transformation Reachability Problem. Clearly, it is easy to see that this is indeed a case of Transformation Reachability Problem when the seed language is singleton. If A\mathcal{A} is such that L(A)={τ}L(\mathcal{A}) = \{\tau\}, then we just define/rename patterns Patsrc:=ϕsrc(τ),Pattgt:=ϕtgt(τ)\mathrm{Pat}_{src} := \phi_{src}(\tau), \mathrm{Pat}_{tgt} := \phi_{tgt}(\tau).

Proof. (Upper Bound) Let T:=(Patsrc,Pattgt,G)T := (\mathrm{Pat}_{src}, \mathrm{Pat}_{tgt}, \mathcal{G}) be an atomic action, and let Bsrc,Btgt\mathcal{B}_{src},\mathcal{B}_{tgt} be NBTAs with Lsrc:=L(Bsrc)L_{src} := L(\mathcal{B}_{src}) and Ltgt:=L(Btgt)L_{tgt} := L(\mathcal{B}_{tgt}). To determine whether T[Lsrc]∩Ltgt≠∅T[L_{src}] \cap L_{tgt} \ne\varnothing, we need to find a substitution σ\sigma such that σ(Patsrc)∈Lsrc\sigma(\mathrm{Pat}_{src}) \in L_{src} and σ(Pattgt)∈Ltgt\sigma(\mathrm{Pat}_{tgt}) \in L_{tgt} under G\mathcal{G}. We further simplify the problem at hand. Consider a bigger alphabet Γ:=Σ⊎{#/2}\Gamma:= \Sigma\mathbin{\uplus} \{\# / 2\}. Let Pat\mathrm{Pat} be a pattern over Γ\Gamma and Var\mathrm{Var} given by Pat:=#(Patsrc,Pattgt)\mathrm{Pat} := \#(\mathrm{Pat}_{src}, \mathrm{Pat}_{tgt}). Let B\mathcal{B} be an NBTA over Γ\Gamma which recognises all trees of the form #(psrc,ptgt)\#(p_{src},p_{tgt}) with psrc∈Lsrc,ptgt∈Ltgtp_{src} \in L_{src},p_{tgt} \in L_{tgt}. Then Atomic Transformation Reachability Problem now reduces to finding a substitution σ\sigma over Γ\Gamma conforming to G\mathcal{G} such that σ(Pat)∈L:=L(B)\sigma(\mathrm{Pat}) \in L := L(\mathcal{B}).

Let M:=max⁡{arity⁡(xi)∣xi∈Var(Pat)}M := \max\{\operatorname{arity}(x_i) \mid x_i \in\mathrm{Var}(\mathrm{Pat})\}. Let us assume that each variable x/k∈Var(Pat)x/k \in\mathrm{Var}(\mathrm{Pat}) appears b(x)b(x) number of times in Pat\mathrm{Pat}. By Theorem 20, for each variable xx, we choose a pair (R(x),c(x))(R(x),c(x)) such that R(x)⊆Qk+1R(x) \subseteq Q^{k+1} is a relation realised by context c(x)∈Contextsk(Γ)c(x) \in\mathrm{Contexts}_k(\Gamma) on B\mathcal{B} under G(x)\mathcal{G}(x) with the added condition that ∣R(x)∣≤b(x)|R(x)| \le b(x). Once we have all such ordered pairs for individual variables, we simply use the substitution σ:=c\sigma:= c and check whether the resulting tree belongs to TT. This can be done in time given by

O(∣B∣⋅max⁡x∣G(x)∣+∣Pat∣⋅2∣B∣⋅max⁡x∣G(x)∣)O\left(|\mathcal{B}| \cdot\max_x |\mathcal{G}(x)| + |\mathrm{Pat}| \cdot2^{|\mathcal{B}|\cdot\max_x |\mathcal{G}(x)|}\right)

The number of times we have to perform this check operation is bounded above by the value

(max⁡x∣Qdeg⁡(x)b(x)∣)∣Var(Pat)∣.\left(\max_x \left|\frac{Q^{\deg(x)}}{b(x)}\right|\right)^{|\mathrm{Var}(\mathrm{Pat})|}.

Hence the total time required for this is given by

(max⁡x∣Qdeg⁡(x)b(x)∣)∣Var(Pat)∣⋅O(∣B∣⋅max⁡x∣G(x)∣+∣Pat∣⋅2∣B∣⋅max⁡x∣G(x)∣)\left(\max_x \left|\frac{Q^{\deg(x)}}{b(x)}\right|\right)^{|\mathrm{Var}(\mathrm{Pat})|} \cdot O\left(|\mathcal{B}| \cdot\max_x |\mathcal{G}(x)| + |\mathrm{Pat}| \cdot2^{|\mathcal{B}|\cdot\max_x |\mathcal{G}(x)|}\right)

which we can see is exponential in the size of the input.

(Lower Bound) We show a reduction from Intersection Nonemptiness of DBTA to Atomic Transformation Reachability Problem. Since we know that Intersection Nonemptiness of DBTA is EXPTIME-hard, we will conclude that Atomic Transformation Reachability Problem is also EXPTIME-hard. Let A1,…,An\mathcal{A}_1,\ldots,\mathcal{A}_n be an instance of the Intersection Nonemptiness of DBTA, where each Ai:=(Qi,Σ,Δi,Fi)\mathcal{A}_i := (Q_i,\Sigma,\Delta_i,F_i) is DBTA over the alphabet Σ\Sigma for all i∈[n]i \in[n]. We construct a corresponding instance of Atomic Transformation Reachability Problem as follows. Let Γ:=Σ⊎{#/n,♭/0}\Gamma:= \Sigma\mathbin{\uplus} \{\# / n,\flat/ 0\} be a larger alphabet. Consider the atomic action T:=(#(x,x,…,x),♭,G)T := (\#(x,x,\ldots,x),\flat,\mathcal{G}) with G(x):=Trees(Σ)\mathcal{G}(x) := \mathrm{Trees}(\Sigma). Let Bsrc\mathcal{B}_{src} be a NBTA such that Lsrc:=L(Bsrc):={#(t1,t2,…,tn)∣ti∈Trees(Σ), ∀i∈[n]}L_{src} := L(\mathcal{B}_{src}) := \{\#(t_1,t_2,\ldots,t_n) \mid t_i \in\mathrm{Trees}(\Sigma),\ \forall i \in[n]\}. Let Btgt\mathcal{B}_{tgt} be an NBTA such that Ltgt:=L(Btgt):={♭}L_{tgt} := L(\mathcal{B}_{tgt}) := \{\flat\}.

We can see that T[Lsrc]∩Ltgt≠∅T[L_{src}] \cap L_{tgt} \ne\varnothing if and only if there exists t∈Trees(Σ)t \in\mathrm{Trees}(\Sigma) such that t∈L(Ai)t \in L(\mathcal{A}_i) for all i∈[n]i \in[n]. This completes the reduction, and thus the proof.

Our central problem of study is whether a tree in a regular target language can be reached by a given transformation, starting from a tree in the regular source language, which we show is decidable.

Transformation Reachability Problem
Input:Transformation T:=(S,ϕsrc,ϕtgt,G)T := (S,\phi_{src},\phi_{tgt},\mathcal{G}), source language LsrcL_{src}, target language LtgtL_{tgt}.
// The languages are represented by NBTA.
Question:Do we have that T(Lsrc)∩Ltgt≠∅T(L_{src}) \cap L_{tgt} \ne\varnothing?

Table 1.

Theorem 14. The Transformation Reachability Problem is in 22-EXPTIME. More precisely, it can be solved in time 2poly⁡(∣input∣)2^{\operatorname{poly}(|\text{input}|)}, where poly is a polynomial of degree deg⁡(T)\deg(T).

Since NBTA are closed under complementation (with an exponential blow-up, see [4]) we get that the type-checking problem defined below is in 3-EXPTIME.

Type-Checking Problem
Input:Transformation T:=(S,φsrc,φtgt,G)T := (S, \varphi_{\mathrm{src}}, \varphi_{\mathrm{tgt}}, G), source language LsrcL_{\mathrm{src}}, target language LtgtL_{\mathrm{tgt}}. // The languages are represented by NBTA.
Question:Do we have that T(Lsrc)⊆LtgtT(L_{\mathrm{src}}) \subseteq L_{\mathrm{tgt}}?

Table 2.

Corollary 15. The Type-Checking Problem is in 3-EXPTIME.

Our decision procedure for the Transformation Reachability Problem is automata-theoretic. We first give the main ideas behind our decision procedures in the next section.

Core Idea Behind the Automata Construction

To solve the Transformation Reachability Problem, we need to check for the existence of a seed tree ss giving rise to an atomic transformation TsT_s, a source tree tsrct_{src}, a target tree ttgtt_{tgt} and a substitution σ\sigma satisfying two constraints. First, the source tree tsrct_{src} should be in LsrcL_{src} and second, the target tree ttgtt_{tgt} should be in LtgtL_{tgt}. The source tree is obtained from the seed tree by first applying the homomorphism ϕsrc\phi_{src} and then applying the substitution σ\sigma. Suppose a node η\eta in the seed tree is transformed into a context as shown in the middle of Figure 7. Suppose a variable xx is substituted with the tree σ(x)\sigma(x) as shown in the right side of Figure 7. Consider a run of ALsrcA_{L_{src}} on the annotated source tree, in particular the state transformation ((q7,q8,q9)((q_7,q_8,q_9) to (q10)(q_{10}) when the run parses the portion of the source tree that comes from the node η\eta of the seed tree. The idea of our decision procedure is to design a tree automaton (we will call this the far-sighted automaton in the next two paragraphs) that parses the seed tree, and when it traverses the node η\eta, it simulates ALsrcA_{L_{src}}'s state transformation (q7,q8,q9)(q_7,q_8,q_9) to (q10)(q_{10}). To determine that (q7,q8,q9)(q_7,q_8,q_9) is transformed to q10q_{10}, the far-sighted automaton needs to know that (q1,q2)(q_1,q_2) is transformed to q3q_3 by σ(x)\sigma(x).

Diagram of a seed tree, source pattern, and annotated source tree

Figure 7. The source tree is annotated with states of ALsrc\mathcal{A}_{L_{\mathrm{src}}}. We want to simulate the effect of running ALsrc\mathcal{A}_{L_{\mathrm{src}}} on the source tree in the seed tree itself. This means, when processing η\eta, it should guess and validate the potential transformation of the tuple (q7,q8,q9)(q_7,q_8,q_9) to q10q_{10} by a context (colored blue) in the source tree that matches φsrc(η)\varphi_{\mathrm{src}}(\eta), and simultaneously make sure that there is a substitution for xx that preserves the transformations (q1,q2)(q_1,q_2) to q3q_3 as well as (q4,q5)(q_4,q_5) to q6q_6. An alternating tree automaton on the seed tree can achieve this.

The far-sighted automaton doesn't know what is σ\sigma (in fact, our goal is to check if a suitable σ\sigma exists). There might be infinitely many substitutions σ\sigma out of which one might work; but our far-sighted automaton is supposed to be a finite state automaton, incapable of picking one choice from infinitely many. To work around this, we observe that the actual tree σ(x)\sigma(x) is not important; the important thing is that it transforms (q1,q2)(q_1,q_2) to (q3)(q_3). Any other σ′(x)\sigma'(x) which does the same transformation will work equally well in place of σ(x)\sigma(x). The number of such transformations is finite (since they are transformations on a finite set of states) and the far-sighted automaton only needs to check if one of these finitely many transformations work.

One thing we ignored in the above explanation is that the variable xx may occur multiple times in the source pattern. In the run of ALsrcA_{L_{src}} on the source tree, one occurrence of σ(x)\sigma(x) may encounter (q1,q2)(q_1,q_2) (which is transformed to q3q_3). Some other occurrence of σ(x)\sigma(x) may encounter some other pair (q4,q5)(q_4,q_5) (which is transformed to some other state, say q6q_6). What we need is a substitution that transforms (q1,q2)(q_1,q_2) to q3q_3 and also transforms (q4,q5)(q_4,q_5) to q6q_6 (and does other transformations too, if there are more occurrences of xx in the source pattern). To capture all these transformations simultaneously, we introduce the concept of relational lifts that is formally defined in the next section. The far-sighted automaton nondeterministically picks out one such possible set of transformations to check that the source tree is in LsrcL_{src}. It similarly needs to check that the target tree is in LtgtL_{tgt}. Alternating tree automata offer a convenient way to achieve all these tasks of the far-sighted automaton. The proof idea is to design an alternating tree automaton whose nonemptiness is equivalent to the existence of a seed tree that satisfies the two required constraints.

Alternating Tree Automata and Relational Lifts

We introduce concepts that will be essential for the final proof of the theorem.

Definition 16 (Alternating Top-down Tree Automata). An Alternating Top-down Tree Automaton or ATTA over the alphabet Σ\Sigma is a tuple A:=(Q,Σ,Δ,I)\mathcal{A}:=(Q,\Sigma,\Delta,I), where QQ is a finite set of states, I⊆QI \subseteq Q is a set of initial states, and Δ:Q×Σ→B+(Q×N)\Delta: Q \times\Sigma\to B^{+}(Q \times\mathbb{N}) such that Δ(q,a/k)∈B+(Q×[k])\Delta(q,a/k) \in B^{+}(Q \times[k]), for all q∈Q,a/k∈Σq \in Q,a/k \in\Sigma. Here, for any set XX, B+(X)B^{+}(X) denotes the set of all positive boolean combinations of elements in XX.

The semantics of ATTA are defined using runs, which are themselves trees satisfying some conditions.

Definition 17 (Run, Acceptance for ATTA). Given a tree t∈Trees⁡(Σ)t \in\operatorname{Trees}(\Sigma) and an ATTA AA over Σ\Sigma, we define a run of AA on tt to be a tree ρ\rho over Q×N∗Q \times\mathbb{N}^{*} such that ρ(ε)=(q,ε)\rho(\varepsilon) = (q,\varepsilon) for some state qq and every position u∈Positions⁡(ρ)u \in\operatorname{Positions}(\rho) satisfies the following condition: if ρ(u)=(q,x)\rho(u) = (q,x), t(x)=a/kt(x) = a/k, Δ(q,a)=ϕ\Delta(q,a) = \phi, then there is a subset S:={(q1,i1),…,(qn,in)}⊆Q×[k]S := \{(q_1,i_1),\ldots,(q_n,i_n)\} \subseteq Q \times[k] such that S⊨ϕS \models\phi, the successor positions of uu in ρ\rho are {u1,…,un}\{u_1,\ldots,u_n\}, and ρ(uj)=(qj,xij)\rho(u_j) = (q_j,x_{i_j}) for all j∈[n]j \in[n].

A run is successful if ρ(ε)=(q,ε)\rho(\varepsilon) = (q,\varepsilon) for some initial state q∈Iq \in I. A tree tt is accepted by ATTA AA if there exists at least one successful run of AA on tt. The set of all trees accepted by ATTA AA is called the tree language recognised by AA, and denoted by L(A)⊆Trees⁡(Σ)L(A) \subseteq\operatorname{Trees}(\Sigma). A tree language L⊆Trees⁡(Σ)L \subseteq\operatorname{Trees}(\Sigma) is called regular if there exists an ATTA AA such that L=L(A)L = L(A). The set of all regular tree languages over Σ\Sigma is denoted by Regular⁡(Σ)⊆2Trees⁡(Σ)\operatorname{Regular}(\Sigma) \subseteq2^{\operatorname{Trees}(\Sigma)}.

Suppose AA is a word automaton with set of states QQ and transition relation given by Δ:Σ→Relations⁡(Q,2)\Delta: \Sigma\to\operatorname{Relations}(Q,2), we can extend it to Δ~:Σ∗→Relations⁡(Q,2)\widetilde{\Delta} : \Sigma^* \to\operatorname{Relations}(Q,2) such that (q1,q2)∈Δ~(w)(q_1,q_2) \in\widetilde{\Delta}(w) iff AA has a run on the word ww starting from q1q_1 and ending at q2q_2. The following defines a similar extension for NBTTs.

Definition 18 (Extended Transition Relation(ETR)). Let T:=(Q,Σ,Δ)T := (Q,\Sigma,\Delta) be a NBTT. We extend the transition function Δ\Delta to a larger function Δ~:Contexts⁡(Σ)→Relations⁡(Q)\widetilde{\Delta} : \operatorname{Contexts}(\Sigma) \to\operatorname{Relations}(Q) called the Extended Transition Relation or ETR as follows. Let c∈Contexts⁡k(Σ)c \in\operatorname{Contexts}_k(\Sigma). For a tuple qˉ:=(q1,q2,…,qk)\bar{q} := (q_1,q_2,\ldots,q_k) of states, define the NBTT T‾qˉ:=(Q,Σ⊎{□1/0,□2/0,…,□k/0},Δ⊎{(□i,{qi})∣i∈[k]})\overline{T}_{\bar{q}} := (Q,\Sigma\uplus\{\Box_1/0,\Box_2/0,\ldots,\Box_k/0\},\Delta\uplus\{(\Box_i,\{q_i\}) \mid i \in[k]\}). Let us define the set of results to be the set given by Results⁡(qˉ):={(qˉ,q)∈Qk+1∣q is the result of a run of T‾qˉ on c}\operatorname{Results}(\bar{q}) := \{(\bar{q},q) \in Q^{k+1} \mid q \text{ is the result of a run of } \overline{T}_{\bar{q}} \text{ on } c\}. Finally, we also define the set Δ~(c):=⋃qˉ∈QkResults⁡(qˉ)\widetilde{\Delta}(c) := \bigcup_{\bar{q} \in Q^k} \operatorname{Results}(\bar{q}). We also refer to the relation Δ~(c)\widetilde{\Delta}(c) as the relation induced by the context cc on the states of the NBTT TT. Note that Δ~(a)=Δ(a)\widetilde{\Delta}(a) = \Delta(a), ∀a∈Σ\forall a \in\Sigma.

Suppose a word w1w_1 is extended by appending another word w2w_2 to its end. Now, Δ~(w1⋅w2)\widetilde{\Delta}(w_1 \cdot w_2) is obtained by simply composing the relations Δ~(w1)\widetilde{\Delta}(w_1) and Δ~(w2)\widetilde{\Delta}(w_2). For trees, it is more complicated: a tree t1t_1 can have multiple “end points” where it can be extended by appending other trees. We will need a more complicated way of composing relations.

Definition 19 (Relational Lifts). Let XX be a set and n∈N0n \in\mathbb{N}_0 be a nonnegative integer. Let op⊆Xn+1op \subseteq X^{n+1} be a relation. We define the relational lift of opop as a new relation op~⊆Relations⁡(X)n+1\widetilde{op} \subseteq\operatorname{Relations}(X)^{n+1} given as follows: For R1,…,Rn,R∈Relations⁡(X)R_1,\ldots,R_n,R \in\operatorname{Relations}(X), we say that (R1,R2,…,Rn,R)∈op~(R_1,R_2,\ldots,R_n,R) \in\widetilde{op} iff and only if

  • R∈Relations⁡(X,1+∑j=1nij)R \in\operatorname{Relations}\left(X,1+\sum_{j=1}^{n} i_j\right), where ij∈N0i_j \in\mathbb{N}_0 for all j∈[n]j \in[n] are such that Rj∈Relations⁡(X,ij+1)R_j \in\operatorname{Relations}(X,i_j+1).

  • For all j∈[n]j \in[n], for all xˉj∈Xij\bar{x}_j \in X^{i_j}, we have (xˉ1,xˉ2,…,xˉn,y)∈R(\bar{x}_1,\bar{x}_2,\ldots,\bar{x}_n,y) \in R iff there exist y1,y2,…,yn∈Xy_1,y_2,\ldots,y_n \in X such that (xˉj,yj)∈Rj(\bar{x}_j,y_j) \in R_j and (y1,y2,…,yn,y)∈op(y_1,y_2,\ldots,y_n,y) \in op.

We can think about relational lift as a procedure to compose arbitrarily many relations. Another, more abstract way to think about it is as a way of “lifting” an operator over a set to an operator over relations over that same set. In case of automata running on words, for any a∈Σa \in\Sigma and any w1∈Σ∗w_1 \in\Sigma^*, if we set op:=Δ(a)op := \Delta(a), then (Δ~(w1),Δ~(w1⋅a))∈op~(\widetilde{\Delta}(w_1),\widetilde{\Delta}(w_1 \cdot a)) \in\widetilde{op}. In case of NBTTs running on contexts, suppose c1,…,ckc_1,\ldots,c_k are the children of a node labeled by the letter a/ka/k and we set op:=Δ(a)⊆Qk+1op := \Delta(a) \subseteq Q^{k+1}, then (Δ~(c1),…,Δ~(ck),R)∈op~(\widetilde{\Delta}(c_1),\ldots,\widetilde{\Delta}(c_k),R) \in\widetilde{op} implies that R=Δ~(a(c1,…,ck))R = \widetilde{\Delta}(a(c_1,\ldots,c_k)).

The following problem and its complexity is an important intermediate technical lemma used later.

Relation Realisability Problem
Input:NBTTS T:=(Q,Σ,Δ)\mathcal{T} := (Q, \Sigma, \Delta), relation R∈Relations(Q)R \in\mathrm{Relations}(Q), NBTA D\mathcal{D}.
Question:Is RR realisable by T\mathcal{T} under D\mathcal{D} i.e., Does there exist c∈Contexts(Σ)∩L(D)c \in\mathrm{Contexts}(\Sigma) \cap L(\mathcal{D}) such that Δ~(c)=R\widetilde{\Delta}(c) = R?

Table 20.

As a matter of nomenclature, if the above condition holds for a given T\mathcal{T}, RR, D\mathcal{D}, then we say that the relation RR is realisable by T\mathcal{T} under D\mathcal{D}.

Theorem 20. The Relation Realisability Problem is EXPTIME-complete.

Proof. (Lower Bound) We show a reduction from Intersection Nonemptiness of DBTA to Relation Realisability Problem. We recall the problem Intersection Nonemptiness of DBTA as:

Intersection Nonemptiness of DBTA
Input:DBTAs A1,A2,…,An\mathcal{A}_1,\mathcal{A}_2,\ldots,\mathcal{A}_n.
Question:Do we have that ⋂i=1nL(Ai)≠∅\bigcap_{i=1}^{n} L(\mathcal{A}_i) \ne\varnothing?

Table 21.

We know from [4] that Intersection Nonemptiness of DBTA is EXPTIME-complete.

Let Σ\Sigma be a given finite alphabet, and let A1,…,An\mathcal{A}_1,\ldots,\mathcal{A}_n be an instance of Intersection Nonemptiness of DBTA, where Ai:=(Qi,Σ,Δi,Fi)\mathcal{A}_i := (Q_i,\Sigma,\Delta_i,F_i) are DBTAs. If Fi=∅F_i = \varnothing for some i∈[n]i \in[n], we must have L(Ai)=∅L(\mathcal{A}_i) = \varnothing and hence the intersection ⋂i=1nL(Ai)=∅\bigcap_{i=1}^{n} L(\mathcal{A}_i) = \varnothing. Hence we assume that Fi≠∅F_i \ne\varnothing for all i∈[n]i \in[n].

Consider NBTA B:=((⨄i=1nQi)⊎{⊤},Γ:=Σ⊎{#/2},Δ,{⊤})\mathcal{B} := ((\mathop{\biguplus}_{i=1}^{n} Q_i) \uplus\{\top\}, \Gamma:= \Sigma\uplus\{\#/2\}, \Delta, \{\top\}), with Δ:Σ⊎{#/2}→Relations(Q)\Delta: \Sigma\uplus\{\#/2\} \to\mathrm{Relations}(Q) is defined as

Δ(a):={⨄i=1nΔi(a)if a∈Σ;{(q,q′,⊤)∣q,q′∈Fi for some i∈[n]}if a=#\Delta(a) := \begin{cases} \mathop{\biguplus}_{i=1}^{n} \Delta_i(a) & \text{if } a \in\Sigma;\\ \{(q,q',\top) \mid q,q' \in F_i \text{ for some } i \in[n]\} & \text{if } a = \# \end{cases}

Let R∈Relations(Q,2)R \in\mathrm{Relations}(Q,2) be given by R:={(fi,⊤)∣i∈[n]}R := \{(f_i,\top) \mid i \in[n]\}, where each fif_i is an arbitrarily chosen but fixed final state in FiF_i for each i∈[n]i \in[n]. Also consider DBTA Shape which recognises all contexts of the form #(t,□)\#(t,\square) where t∈Trees(Σ)t \in\mathrm{Trees}(\Sigma). Consider the instance of the realisability problem given by (B,R,Shape)(\mathcal{B},R,\mathit{Shape}).

We can see that RR is realisable by B∩Shape\mathcal{B} \cap\mathit{Shape} if and only if there exists a tree t∈L(A1)∩⋯∩L(An)t \in L(\mathcal{A}_1) \cap\cdots\cap L(\mathcal{A}_n). Clearly, RR is realisable by B∩Shape⟺\mathcal{B} \cap\mathit{Shape} \Longleftrightarrow there exists a context c:=#(t,□)c := \#(t,\square) which induces the relation RR on the states of B\mathcal{B} under Shape ⟺\Longleftrightarrow for each i∈[n]i \in[n] and the labelling of the hole □\square with fif_i, the result of a run of B\mathcal{B} on tt yields a final state fi′∈Fif_i' \in F_i ⟺\Longleftrightarrow tt is recognised by Ai\mathcal{A}_i for each i∈[n]i \in[n] ⟺\Longleftrightarrow t∈⋂i=1nL(Ai)t \in\bigcap_{i=1}^{n} L(\mathcal{A}_i). Since we know that Intersection Nonemptiness of DBTA is EXPTIME-hard, we conclude that the realisability problem is also EXPTIME-hard.

(Upper Bound) Let NBTTS T:=(Q,Σ,Δ)\mathcal{T} := (Q,\Sigma,\Delta) be a transition system, relation R∈Relations(Q,k+1)⊆Relations(Q)R \in\mathrm{Relations}(Q,k+1) \subseteq\mathrm{Relations}(Q), and NBTA D\mathcal{D} be a given instance of Relation Realisability Problem. We reduce the problem of finding a context c∈Contextsk(Σ)∩L(D)c \in\mathrm{Contexts}_k(\Sigma) \cap L(\mathcal{D}) such that Δ~(c):=R\widetilde{\Delta}(c) := R to the problem of NBTA nonemptiness. We will define an NBTA A\mathcal{A} with relations over QQ as states such that for any context cc, the result of a run of A\mathcal{A} on cc is precisely the relation induced by cc. More formally, we define a NBTA A\mathcal{A} as:

A:=((⋃i=1k+1Relations(Q,i))∪{□i/0∣i∈[k]},Δ′,{R})\mathcal{A} := ((\bigcup_{i=1}^{k+1}\mathrm{Relations}(Q,i)) \cup\{\square_i/0 \mid i \in[k]\},\Delta',\{R\}), where for all a/p∈Σa/p \in\Sigma we set Δ′(a):=Δ~(a)\Delta'(a) := \widetilde{\Delta}(a), the relational lift of Δ(a)\Delta(a). We also set Δ′(□i):=Q\Delta'(\square_i) := Q for all i∈[k]i \in[k].

A straightforward induction on the height of a context c∈Contextsk(Σ)∩L(D)c \in\mathrm{Contexts}_k(\Sigma) \cap L(\mathcal{D}), we have Δ~(c)=R\widetilde{\Delta}(c) = R if and only if c∈L(A)∩L(D)c \in L(\mathcal{A}) \cap L(\mathcal{D}). Hence to find a context which ‘realises’ the relation RR, all we need to do is check A∩D\mathcal{A} \cap\mathcal{D} for nonemptiness. Let automaton D\mathcal{D} have mm states. We know from [4] that checking this NBTA for nonemptiness

can be performed in time which is a polynomial in ∣Q∣|Q|. Since we have that m∣Q∣:=m∣⋃i=1k+1Relations⁡(Q,i)∣=m∑i=1k+1∣Relations⁡(Q,i)∣=m∑i=1k+12∣Q∣im|Q| := m\left|\bigcup_{i=1}^{k+1}\operatorname{Relations}(Q,i)\right| = m\sum_{i=1}^{k+1}|\operatorname{Relations}(Q,i)| = m\sum_{i=1}^{k+1}2^{|Q|^i}, we see that given T,R,D\mathcal{T},R,\mathcal{D}, the problem of Relation Realisability Problem can be solved in EXPTIME.

In the proof for the above result, we also get that for each relation which we are checking for realisability, we can also get, in EXPTIME, a context over the alphabet which realises it(if it is realisable). In such a case, we will say that the relation RR is realised by the context cc on the NBTTS T\mathcal{T}, under D\mathcal{D}. We also say that cc induces RR on T\mathcal{T} under D\mathcal{D}. For a given pair (T,D)(\mathcal{T},\mathcal{D}) with NBTTS T\mathcal{T} and NBTA D\mathcal{D}, we define the set IndRel⁡(T,D):={(R,c)∣R is realised by c on T under D}\operatorname{IndRel}(\mathcal{T},\mathcal{D}):=\{(R,c)\mid R\text{ is realised by }c\text{ on }\mathcal{T}\text{ under }\mathcal{D}\}.

Deciding Transformation Reachability

Let T:=((S,ϕsrc,ϕtgt,G),Lsrc,Ltgt)\mathcal{T}:=((S,\phi_{src},\phi_{tgt},\mathcal{G}),L_{src},L_{tgt}) be a given instance of Transformation Reachability Problem, where

  • SS is the seed language over Γ\Gamma given by the NBTA A:=(P,Γ,Θ,E)\mathcal{A}:=(P,\Gamma,\Theta,E).

  • ϕsrc,ϕtgt:Trees⁡(Γ)→Trees⁡(Σ⊎Var⁡)\phi_{src},\phi_{tgt}:\operatorname{Trees}(\Gamma)\to\operatorname{Trees}(\Sigma\uplus\operatorname{Var}) are tree homomorphisms.

  • G\mathcal{G} is a guard over Σ\Sigma such that Var⁡(ϕsrc)∪Var⁡(ϕtgt)⊆Domain⁡(G)\operatorname{Var}(\phi_{src})\cup\operatorname{Var}(\phi_{tgt})\subseteq\operatorname{Domain}(\mathcal{G}), where we define(by abuse of notation) Var⁡(ϕsrc):=⋃γ∈ΓVar⁡(ϕsrc(γ))\operatorname{Var}(\phi_{src}):=\bigcup_{\gamma\in\Gamma}\operatorname{Var}(\phi_{src}(\gamma)) and also Var⁡(ϕtgt):=⋃γ∈ΓVar⁡(ϕtgt(γ))\operatorname{Var}(\phi_{tgt}):=\bigcup_{\gamma\in\Gamma}\operatorname{Var}(\phi_{tgt}(\gamma)).

  • Lsrc,LtgtL_{src},L_{tgt} are the source and target languages respectively, which are regular tree languages given by the NBTAs Bi:=(Qi,Σ,Δi,Fi)\mathcal{B}_i:=(Q_i,\Sigma,\Delta_i,F_i) for i∈{src,tgt}i\in\{src,tgt\}.

We see that solving the Transformation Reachability Problem is equivalent to finding whether there exists τ∈S\tau\in S and a substitution σ\sigma such that σ(ϕi(τ))∈Li\sigma(\phi_i(\tau))\in L_i for i∈{src,tgt}i\in\{src,tgt\}. Let J⊆SJ\subseteq S be given by J:={τ∈S∣∃σ such that σ(ϕi(τ))∈Li,i∈{src,tgt}}J:=\{\tau\in S\mid\exists\sigma\text{ such that }\sigma(\phi_i(\tau))\in L_i,i\in\{src,tgt\}\}. We will construct a number of ATTAs such that the union of the languages they recognise will be equal to JJ. This will imply that solving Transformation Reachability Problem will be a matter of checking each of the correspondingly constructed ATTAs for nonemptiness.

For each x∈Var⁡(ϕsrc)∖Var⁡(ϕtgt)x\in\operatorname{Var}(\phi_{src})\setminus\operatorname{Var}(\phi_{tgt}), using Theorem 20 we compute a priori, the set of relations IndRel⁡(Bsrc,G(x))\operatorname{IndRel}(\mathcal{B}_{src},\mathcal{G}(x)) of all the relations and corresponding contexts realisable by Bsrc\mathcal{B}_{src} under G(x)\mathcal{G}(x). Similarly for each variable y∈Var⁡(ϕtgt)∖Var⁡(ϕsrc)y\in\operatorname{Var}(\phi_{tgt})\setminus\operatorname{Var}(\phi_{src}), we compute IndRel⁡(Btgt,G(y))\operatorname{IndRel}(\mathcal{B}_{tgt},\mathcal{G}(y)). For all variables z∈Var⁡(ϕsrc)∩Var⁡(ϕtgt)z\in\operatorname{Var}(\phi_{src})\cap\operatorname{Var}(\phi_{tgt}), we compute the set

IndRel⁡(Bsrc∩Btgt,G(z))⊆Relations⁡(Qsrc×Qtgt)×Contexts⁡(Σ).\operatorname{IndRel}(\mathcal{B}_{src}\cap\mathcal{B}_{tgt},\mathcal{G}(z))\subseteq\operatorname{Relations}(Q_{src}\times Q_{tgt})\times\operatorname{Contexts}(\Sigma).

From this set, we obtain another set IndRel⁡(Bsrc,Btgt,G(z))\operatorname{IndRel}(\mathcal{B}_{src},\mathcal{B}_{tgt},\mathcal{G}(z)) of pairs of relations that are simultaneously realisable by Bsrc\mathcal{B}_{src} and Btgt\mathcal{B}_{tgt} along with a context c∈L(G(z))c\in L(\mathcal{G}(z)) that realises both of them. The idea is that given an element ((qsrc(1),qtgt(1)),(qsrc(2),qtgt(2)),…,(qsrc(k+1),qtgt(k+1)))∈R((q^{(1)}_{src},q^{(1)}_{tgt}),(q^{(2)}_{src},q^{(2)}_{tgt}),\ldots,(q^{(k+1)}_{src},q^{(k+1)}_{tgt}))\in R, we ‘split’ it into two components (qsrc(1),qsrc(2),…,qsrc(k+1))(q^{(1)}_{src},q^{(2)}_{src},\ldots,q^{(k+1)}_{src}) and (qtgt(1),qtgt(2),…,qtgt(k+1))(q^{(1)}_{tgt},q^{(2)}_{tgt},\ldots,q^{(k+1)}_{tgt}). We do this for all elements in RR to get a pair of relations Rsrc,RtgtR_{src},R_{tgt} over Qsrc,QtgtQ_{src},Q_{tgt} respectively. Then, we define IndRel⁡(Bsrc,Btgt,G(z))\operatorname{IndRel}(\mathcal{B}_{src},\mathcal{B}_{tgt},\mathcal{G}(z)) as

:={((Rsrc,Rtgt),c)∣(R,c)∈IndRel⁡(Bsrc∩Btgt,G(z))}.:=\{((R_{src},R_{tgt}),c)\mid(R,c)\in\operatorname{IndRel}(\mathcal{B}_{src}\cap\mathcal{B}_{tgt},\mathcal{G}(z))\}.

In other words, we have IndRel⁡(Bsrc,Btgt,G(z)):={((Rsrc,Rtgt),c)∣c∈L(G(z)),Δ~i(c)=Ri,i∈{src,tgt}}\operatorname{IndRel}(\mathcal{B}_{src},\mathcal{B}_{tgt},\mathcal{G}(z)):=\{((R_{src},R_{tgt}),c)\mid c\in L(\mathcal{G}(z)),\widetilde{\Delta}_i(c)=R_i,i\in\{src,tgt\}\}.

We fix a pair of choice functions of realisable relations Rsrc,RtgtR_{src},R_{tgt} over $\operatorname{Var}(\phi_{src}),\operatorname{Var}(\phi_{tgt}) respectively such that

Rsrc(z):={R∈π1[IndRel⁡(Bs,G(z))]if z∈Var⁡(ϕsrc)∖Var⁡(ϕtgt);R′∈π1[π1[IndRel⁡(Bsrc,Btgt,G(z))]]if z∈Var⁡(ϕsrc)∩Var⁡(ϕtgt)(1)R_{src}(z):=\begin{cases} R\in\pi_1[\operatorname{IndRel}(\mathcal{B}_{s},\mathcal{G}(z))] & \text{if }z\in\operatorname{Var}(\phi_{src})\setminus\operatorname{Var}(\phi_{tgt});\\ R'\in\pi_1[\pi_1[\operatorname{IndRel}(\mathcal{B}_{src},\mathcal{B}_{tgt},\mathcal{G}(z))]] & \text{if }z\in\operatorname{Var}(\phi_{src})\cap\operatorname{Var}(\phi_{tgt}) \end{cases} \tag*{(1)}
Rtgt(z):={R∈π1[IndRel⁡(Btgt,G(z))]if z∈Var⁡(φtgt)∖Var⁡(φsrc);R′′∈π2[π1[IndRel⁡(Bsrc,Btgt,G(z))]]if z∈Var⁡(φsrc)∩Var⁡(φtgt)R_{\mathrm{tgt}}(z) := \begin{cases} R \in\pi_1[\operatorname{IndRel}(B_{\mathrm{tgt}},G(z))] & \text{if } z \in\operatorname{Var}(\varphi_{\mathrm{tgt}}) \setminus\operatorname{Var}(\varphi_{\mathrm{src}});\\ R'' \in\pi_2[\pi_1[\operatorname{IndRel}(B_{\mathrm{src}},B_{\mathrm{tgt}},G(z))]] & \text{if } z \in\operatorname{Var}(\varphi_{\mathrm{src}}) \cap\operatorname{Var}(\varphi_{\mathrm{tgt}}) \end{cases}

and (R′,R′′)∈π1[IndRel⁡(Bsrc,Btgt,G(z))](R',R'') \in\pi_1[\operatorname{IndRel}(B_{\mathrm{src}},B_{\mathrm{tgt}},G(z))] for all z∈Var⁡(φsrc)∩Var⁡(φtgt)z \in\operatorname{Var}(\varphi_{\mathrm{src}}) \cap\operatorname{Var}(\varphi_{\mathrm{tgt}}). Note that this also provides us with corresponding contexts which realise the chosen relations.

Armed with this, we compute the ETRs Δi(φi(γ))\Delta_i(\varphi_i(\gamma)) for all γ∈Γ\gamma\in\Gamma and i∈{src,tgt}i \in\{\mathrm{src},\mathrm{tgt}\} by simply considering each x∈Var⁡(T)x \in\operatorname{Var}(\mathcal{T}) as a separate letter with the ‘transition relation’ given by the corresponding Ri(x)R_i(x) and running the corresponding automaton on it.

Lemma 21. For each pair of choice functions Rsrc,RtgtR_{\mathrm{src}},R_{\mathrm{tgt}}, there exists an ATTA ZRsrc,RtgtZ_{R_{\mathrm{src}},R_{\mathrm{tgt}}} of size O(max⁡{∣Qsrc∣,∣Qtgt∣})O(\max\{|Q_{\mathrm{src}}|,|Q_{\mathrm{tgt}}|\}) such that

L(ZRsrc,Rtgt)={τ∣σ(φsrc(τ))∈Lsrc, σ(φtgt(τ))∈Ltgt}L(Z_{R_{\mathrm{src}},R_{\mathrm{tgt}}})=\{\tau\mid\sigma(\varphi_{\mathrm{src}}(\tau))\in L_{\mathrm{src}},\ \sigma(\varphi_{\mathrm{tgt}}(\tau))\in L_{\mathrm{tgt}}\}

where σ\sigma is a function which assigns to each variable xx, a context that induces the relation Ri(x)R_i(x) for i∈{src,tgt}i \in\{\mathrm{src},\mathrm{tgt}\} and satisfies the guard GG.

We first give an explicit construction of the ATTA ZRsrc,RtgtZ_{R_{\mathrm{src}},R_{\mathrm{tgt}}}. To describe it succinctly, we define a few related concepts.

Definition 22 (Destination Function). Let S,LS,L be tree languages over finite ranked alphabets Γ,Σ\Gamma,\Sigma respectively and let φ:Trees⁡(Γ)→Trees⁡(Σ)\varphi:\operatorname{Trees}(\Gamma)\to\operatorname{Trees}(\Sigma) be a tree homomorphism. For each letter γ/k∈Γ\gamma/k\in\Gamma with m:=arity⁡(φ(γ))m:=\operatorname{arity}(\varphi(\gamma)), we define a function Dφ(γ):[k]→2[m]D_\varphi(\gamma):[k]\to2^{[m]} as follows. Since φ(γ)\varphi(\gamma) is a context of arity mm, we label the holes of φ(γ)\varphi(\gamma) by the set [m][m] according to the precedence in the inorder traversal of φ(γ)\varphi(\gamma). Then, Dφ(γ)(i):={j∈[m]∣jth hole of φ(γ) is labelled ui}D_\varphi(\gamma)(i):=\{j\in[m]\mid j^{\mathrm{th}}\text{ hole of }\varphi(\gamma)\text{ is labelled }u_i\} for all i∈[k]i\in[k].

We illustrate this with an example. Let γ/5∈Γ\gamma/5\in\Gamma be a letter and let φ(γ/5)\varphi(\gamma/5) be given as in Figure 8. The numbers in blue denote the order of the vertex in the inorder traversal of φ(γ)\varphi(\gamma).

Figure 8. Example computation of Dφ(γ)D_\varphi(\gamma)

Then Dφ(γ):[5]→2[7]D_\varphi(\gamma):[5]\to2^{[7]} would be given by

ii12345
Dφ(γ)(i)D_\varphi(\gamma)(i){1,3,6}\{1,3,6\}{4}\{4\}{2,7}\{2,7\}{5}\{5\}∅\varnothing

Table 8.

Note that as demonstrated by this example, in each case we must necessarily have that the set {Dφ(γ)(i)∣i∈[k]}\{D_\varphi(\gamma)(i)\mid i\in[k]\} is a partition of [arity⁡(φ(γ))][\operatorname{arity}(\varphi(\gamma))]. Intuitively, we also consider the function Dφ(γ)D_\varphi(\gamma) as a kind of “inverse” of the origin function as mentioned in [3].

Coming to the construction of the ATTA, for each pair of choice functions Rsrc,RtgtR_{\mathrm{src}},R_{\mathrm{tgt}}, we define ATTA ZRsrc,RtgtZ_{R_{\mathrm{src}},R_{\mathrm{tgt}}} given by

ZRsrc,Rtgt:=((P⊎Qsrc⊎Qtgt⊎{start}),Γ,I:={start},Δ′)Z_{R_{\mathrm{src}},R_{\mathrm{tgt}}}:=\left((P\mathbin{\uplus}Q_{\mathrm{src}}\mathbin{\uplus}Q_{\mathrm{tgt}}\mathbin{\uplus}\{\mathrm{start}\}),\Gamma,I:=\{\mathrm{start}\},\Delta'\right)

where we define Δ′\Delta' as follows. For a fixed letter γ∈Γ\gamma\in\Gamma,

  • For a state p∈Pp \in P, we set

Δ′(p,γ/k):=⋁(p1,p2,…,pk,p)∈Θ(γ)(p1,1)∧(p2,2)∧⋯∧(pk,k)\Delta'(p,\gamma/k) := \bigvee_{(p_1,p_2,\ldots,p_k,p)\in\Theta(\gamma)} (p_1,1)\wedge(p_2,2)\wedge\cdots\wedge(p_k,k)

This transition basically ensures that the ATTA ZRsrc,Rtgt\mathcal{Z}_{R_{\mathrm{src}},R_{\mathrm{tgt}}} simulates A\mathcal{A}.

  • For a state q∈Qsrcq \in Q_{\mathrm{src}}, we set

Δ′(q,γ/k):=⋁(q1,…,qm,q)∈Δgsrc(ϕsrc(γ))⋀i∈[k][⋀j∈Dϕsrc(i)(qj,i)]\Delta'(q,\gamma/k) := \bigvee_{(q_1,\ldots,q_m,q)\in\Delta_{g_{\mathrm{src}}}(\phi_{\mathrm{src}}(\gamma))}\bigwedge_{i\in[k]}\left[\bigwedge_{j\in D_{\phi_{\mathrm{src}}}(i)}(q_j,i)\right]

Intuitively, this transition simulates the running of Bsrc\mathcal{B}_{\mathrm{src}} on ϕsrc(τ)\phi_{\mathrm{src}}(\tau).

  • Similarly, for a state q∈Qtgtq \in Q_{\mathrm{tgt}}, we set

Δ′(q,γ/k):=⋁(q1,…,qm,q)∈Δ~tgt(ϕtgt(γ))⋀i∈[k][⋀j∈Dϕtgt(i)(qj,i)]\Delta'(q,\gamma/k) := \bigvee_{(q_1,\ldots,q_m,q)\in\widetilde{\Delta}_{\mathrm{tgt}}(\phi_{\mathrm{tgt}}(\gamma))}\bigwedge_{i\in[k]}\left[\bigwedge_{j\in D_{\phi_{\mathrm{tgt}}}(i)}(q_j,i)\right]

Intuitively, this transition simulates the running of Btgt\mathcal{B}_{\mathrm{tgt}} on ϕtgt(τ)\phi_{\mathrm{tgt}}(\tau).

  • We also have an ε\varepsilon-transition Δ′(start,ε/1):=⋁p∈E,qsrc∈Fsrc,qtgt∈Ftgt((p,1)∧(qsrc,1)∧(qtgt,1))\Delta'(\mathrm{start},\varepsilon/1):=\bigvee_{p\in E,q_{\mathrm{src}}\in F_{\mathrm{src}},q_{\mathrm{tgt}}\in F_{\mathrm{tgt}}}((p,1)\wedge(q_{\mathrm{src}},1)\wedge(q_{\mathrm{tgt}},1))

We illustrate this with an example. Let q∈Qsrcq \in Q_{\mathrm{src}}, and γ/5∈Γ\gamma/5 \in\Gamma, i.e., γ\gamma is a letter with arity 5. Also let ϕsrc(γ)\phi_{\mathrm{src}}(\gamma) be defined as in Figure 8. Note that we have already completely computed the extended transition relation Δgsrc(ϕsrc(γ))\Delta_{g_{\mathrm{src}}}(\phi_{\mathrm{src}}(\gamma)) given the choice function RsrcR_{\mathrm{src}}. In such a case we define Δ′(q,γ/5):=\Delta'(q,\gamma/5):=

⋁(q1,…,q7,q)∈Δsrc(ϕsrc(γ))[(q1,1)∧(q3,1)∧(q6,1)⏟Dϕsrc(1)={1,3,6}∧(q4,2)⏟Dϕsrc(2)={4}∧(q2,3)∧(q7,3)⏟Dϕsrc(3)={2,7}∧(q5,4)⏟Dϕsrc(4)={5}]\bigvee_{\substack{(q_1,\ldots,q_7,q)\\ \in\Delta_{\mathrm{src}}(\phi_{\mathrm{src}}(\gamma))}} \left[ \underbrace{(q_1,1)\wedge(q_3,1)\wedge(q_6,1)}_{D_{\phi_{\mathrm{src}}(1)}=\{1,3,6\}} \wedge \underbrace{(q_4,2)}_{D_{\phi_{\mathrm{src}}(2)}=\{4\}} \wedge \underbrace{(q_2,3)\wedge(q_7,3)}_{D_{\phi_{\mathrm{src}}(3)}=\{2,7\}} \wedge \underbrace{(q_5,4)}_{D_{\phi_{\mathrm{src}}(4)}=\{5\}} \right]

Proof. For the given pair of choice functions Rsrc,RtgtR_{\mathrm{src}},R_{\mathrm{tgt}}, let CRsrc,RtgtC_{R_{\mathrm{src}},R_{\mathrm{tgt}}} be a function that assigns to each x∈Var⁡(ϕsrc)∪Var⁡(ϕtgt)x\in\operatorname{Var}(\phi_{\mathrm{src}})\cup\operatorname{Var}(\phi_{\mathrm{tgt}}), a context that induces the relation Ri(x)R_i(x) for i∈{src,tgt}i\in\{\mathrm{src},\mathrm{tgt}\} and satisfies the guard G\mathcal{G}.

(⇐)(\Leftarrow) Let σ:=CRsrc,Rtgt\sigma:=C_{R_{\mathrm{src}},R_{\mathrm{tgt}}} such that σ(ϕsrc(τ))∈Lsrc\sigma(\phi_{\mathrm{src}}(\tau))\in L_{\mathrm{src}} and σ(ϕtgt(τ))∈Ltgt\sigma(\phi_{\mathrm{tgt}}(\tau))\in L_{\mathrm{tgt}} for some τ∈Trees⁡(Γ)\tau\in\operatorname{Trees}(\Gamma). This implies that for an accepting run of Bsrc\mathcal{B}_{\mathrm{src}} on σ(ϕsrc(τ))\sigma(\phi_{\mathrm{src}}(\tau)), the result of that run is some final state fsrc∈Fsrcf_{\mathrm{src}}\in F_{\mathrm{src}}. Similarly for an accepting run of Btgt\mathcal{B}_{\mathrm{tgt}} on σ(ϕtgt(τ))\sigma(\phi_{\mathrm{tgt}}(\tau)), the result of that run is some final state ftgt∈Ftgtf_{\mathrm{tgt}}\in F_{\mathrm{tgt}}. Also, since τ∈S\tau\in S, there is an accepting run of A\mathcal{A} on τ\tau such that the result of that run is e∈Ee\in E. Then we can see that the set {e,fsrc,ftgt}⊨I\{e,f_{\mathrm{src}},f_{\mathrm{tgt}}\}\models I, the starting formula for ZRsrc,Rtgt\mathcal{Z}_{R_{\mathrm{src}},R_{\mathrm{tgt}}}. From here the run of ZRsrc,Rtgt\mathcal{Z}_{R_{\mathrm{src}},R_{\mathrm{tgt}}} simulates the run of A\mathcal{A} on τ\tau, and the runs of Bi\mathcal{B}_i on ϕi(τ)\phi_i(\tau) for i∈{src,tgt}i\in\{\mathrm{src},\mathrm{tgt}\} conforming to the transition CRsrc,RtgtC_{R_{\mathrm{src}},R_{\mathrm{tgt}}}. Since all of these are successful runs, the run of ZRsrc,Rtgt\mathcal{Z}_{R_{\mathrm{src}},R_{\mathrm{tgt}}} we described on τ\tau is also a successful run, and hence τ∈L(ZRsrc,Rtgt)\tau\in L(\mathcal{Z}_{R_{\mathrm{src}},R_{\mathrm{tgt}}}).

(⇒)(\Rightarrow) Conversely, let τ∈L(ZRsrc,Rtgt)\tau\in L(\mathcal{Z}_{R_{\mathrm{src}},R_{\mathrm{tgt}}}). This implies that there is an accepting run of ZRsrc,Rtgt\mathcal{Z}_{R_{\mathrm{src}},R_{\mathrm{tgt}}} on τ\tau. Given the nature of the starting formula II, we must have an e∈Ee\in E, fsrc∈Fsrcf_{\mathrm{src}}\in F_{\mathrm{src}}, ftgt∈Ftgtf_{\mathrm{tgt}}\in F_{\mathrm{tgt}} such that {e,fsrc,ftgt}⊨I\{e,f_{\mathrm{src}},f_{\mathrm{tgt}}\}\models I. Since ZRsrc,Rtgt\mathcal{Z}_{R_{\mathrm{src}},R_{\mathrm{tgt}}} simulates A\mathcal{A} on τ\tau and e∈Ee\in E, we see that τ∈L(A)\tau\in L(\mathcal{A}). Since ZRsrc,Rtgt\mathcal{Z}_{R_{\mathrm{src}},R_{\mathrm{tgt}}} also simulates Bi\mathcal{B}_i on ϕi(τ)\phi_i(\tau) conforming to CRsrc,RtgtC_{R_{\mathrm{src}},R_{\mathrm{tgt}}} for all i∈{src,tgt}i\in\{\mathrm{src},\mathrm{tgt}\}, we see that σ(ϕi(τ))∈Li\sigma(\phi_i(\tau))\in L_i for i∈{src,tgt}i\in\{\mathrm{src},\mathrm{tgt}\}. This completes the proof.

We know from [4] that the emptiness problem for ATTA is EXPTime-complete in the size of the automaton. Note that this computation has been performed for a specific choice of Rsrc,RtgtR_{\mathrm{src}}, R_{\mathrm{tgt}}. Let R\mathcal{R} be the set of all pairs of choice functions. To complete the solution of the problem, we need to perform this computation for each pair of choice functions (Rsrc,Rtgt)∈R(R_{\mathrm{src}}, R_{\mathrm{tgt}}) \in\mathcal{R}. We know that ∣R∣|\mathcal{R}| is bounded above by ∏x∈Var⁡(φsrc)2∣Qsrc∣deg⁡(x)⋅∏y∈Var⁡(φtgt)2∣Qtgt∣deg⁡(y)\prod_{x\in\operatorname{Var}(\varphi_{\mathrm{src}})}2^{|Q_{\mathrm{src}}|^{\deg(x)}} \cdot\prod_{y\in\operatorname{Var}(\varphi_{\mathrm{tgt}})}2^{|Q_{\mathrm{tgt}}|^{\deg(y)}}. Let stateSize:=max⁡{∣Qsrc∣,∣Qtgt∣}\mathrm{stateSize}:=\max\{|Q_{\mathrm{src}}|,|Q_{\mathrm{tgt}}|\}. This gives an upper bound ∣R∣≤∏x∈Var⁡(φsrc)2stateSizedeg⁡(T)⋅∏y∈Var⁡(φtgt)2stateSizedeg⁡(T)|\mathcal{R}|\leq\prod_{x\in\operatorname{Var}(\varphi_{\mathrm{src}})}2^{\mathrm{stateSize}^{\deg(T)}}\cdot\prod_{y\in\operatorname{Var}(\varphi_{\mathrm{tgt}})}2^{\mathrm{stateSize}^{\deg(T)}} which eventually simplifies to the bound ∣R∣≤(2stateSizedeg⁡(T))∣Var⁡(T)∣|\mathcal{R}|\leq\left(2^{\mathrm{stateSize}^{\deg(T)}}\right)^{|\operatorname{Var}(T)|}. We also know from Lemma 21 that the size of the ATTA ZRsrc,Rtgt\mathcal{Z}_{R_{\mathrm{src}},R_{\mathrm{tgt}}} is bounded above by O(stateSize)O(\mathrm{stateSize}) and hence each nonemptiness check requires time given by 2O(stateSize)2^{O(\mathrm{stateSize})}. Hence the total time required for all the nonemptiness checks is given by:

Total running time is in 2O(stateSize)⋅2∣Var⁡(T)∣⋅stateSizedeg⁡(T)2^{O(\mathrm{stateSize})}\cdot2^{|\operatorname{Var}(T)|\cdot\mathrm{stateSize}^{\deg(T)}}.

Since ∣Var⁡(T)∣+stateSize≤∣input∣|\operatorname{Var}(T)|+\mathrm{stateSize}\leq|\mathrm{input}|, on simplifying the above, we get that the running time of the algorithm we have provided is 2poly⁡(∣input∣)2^{\operatorname{poly}(|\mathrm{input}|)}, where poly⁡\operatorname{poly} is a polynomial of degree deg⁡(T)\deg(T). This completes the proof of Theorem 14.

Conclusion

We have introduced a formal model of tree transformations which is highly expressive. While the equivalence checking of tree transformations is undecidable we show that the type-checking problem is in 3-EXPTIME by giving an automata-theoretic algorithm.

A future direction is to look at other variants of expressive tree transformations where the (source pattern, target pattern) pair is generated by more generic versions of tree transducers and study the decidability of type-checking problem.

Comparison with kk-Pebble Transducers

We use the notion of kk-Pebble Tree Transducer as defined in [14]. For the sake of completeness, we include the definition here.

Definition 23. A kk-Pebble Tree Transducer is T:=(Σ,Σ′,Q,s,Δ)T := (\Sigma, \Sigma', Q, s, \Delta), where

  • Σ,Σ′\Sigma, \Sigma' are ranked input and output alphabets respectively.

  • QQ is a finite set of states.

  • s∈Qs \in Q is the initial state.

  • Δ\Delta is a finite set of transitions, each of which has one of the following forms:

    • (a,bˉ,q)→(q′,stay)(a,\bar{b},q) \rightarrow(q', stay),

    • (a,bˉ,q)→(q′,downLeft)(a,\bar{b},q) \rightarrow(q', downLeft),

    • (a,bˉ,q)→(q′,downRight)(a,\bar{b},q) \rightarrow(q', downRight),

    • (a,bˉ,q)→(q′,upLeft)(a,\bar{b},q) \rightarrow(q', upLeft),

    • (a,bˉ,q)→(q′,upRight)(a,\bar{b},q) \rightarrow(q', upRight),

    • (a,bˉ,q)→(q′,addPebble)(a,\bar{b},q) \rightarrow(q', addPebble),

    • (a,bˉ,q)→(q′,removePebble)(a,\bar{b},q) \rightarrow(q', removePebble),

    • (a,bˉ,q)→(a′,output0)(a,\bar{b},q) \rightarrow(a', output0),

    • (a,bˉ,q)→(a′(q1,q2),output2)(a,\bar{b},q) \rightarrow(a'(q_1,q_2), output2),

where a∈Σ,a′∈Σ′,bˉ∈{0,1}i−1,q,q′,q1,q2∈Qa \in\Sigma, a' \in\Sigma', \bar{b} \in\{0,1\}^{i-1}, q,q',q_1,q_2 \in Q.

Let Γ\Gamma be a finite alphabet. Let Reverse⁡:Γ∗→Γ∗\operatorname{Reverse} : \Gamma^* \to\Gamma^* be the function defined as follows: For each w:=a1a2…an∈Γ∗w := a_1a_2\ldots a_n \in\Gamma^* with ai∈Γa_i \in\Gamma, ∀i∈[n]\forall i \in[n], we let Reverse⁡(w):=anan−1…a2a1\operatorname{Reverse}(w) := a_na_{n-1}\ldots a_2a_1. Let Σ:=Γ⊎{#}\Sigma:= \Gamma\uplus\{\#\} be a finite ranked alphabet with the ranking function rank⁡:Σ→N0\operatorname{rank} : \Sigma\to\mathbb{N}_0 given by

rank⁡(a):={2if a∈Γ;0if a=#.\operatorname{rank}(a) := \begin{cases} 2 & \text{if } a \in\Gamma;\\ 0 & \text{if } a = \#. \end{cases}

We define the tree-representation-of-string function ϕ:Γ∗→Trees⁡(Σ)\phi: \Gamma^* \to\operatorname{Trees}(\Sigma) given by defining for w:=a1a2…an∈Γ∗w := a_1a_2\ldots a_n \in\Gamma^* with ai∈Γa_i \in\Gamma, ϕ(w)\phi(w) as

ϕ(w):=# a1/\#a2#/\##a3#/\##⋱#\#an#/\###\phi(w) := \begin{array}{c} \phantom{\#}\ a_1\\[-2pt] /\quad\backslash\\[-2pt] \#\quad a_2\\[-2pt] \phantom{\#}\quad/\quad\backslash\\[-2pt] \phantom{\#}\quad\#\quad a_3\\[-2pt] \phantom{\#}\qquad\quad/\quad\backslash\\[-2pt] \phantom{\#}\qquad\quad\#\quad\ddots\\[-2pt] \phantom{\#}\qquad\qquad\quad\backslash\\[-2pt] \phantom{\#}\qquad\qquad\quad a_n\\[-2pt] \phantom{\#}\qquad\qquad/\quad\backslash\\[-2pt] \phantom{\#}\qquad\qquad\#\quad\# \end{array}

Note that ϕ\phi as defined above is an injective function. We define a function Reverse⁡~:ϕ[Γ∗]→ϕ[Γ∗]\widetilde{\operatorname{Reverse}} : \phi[\Gamma^*] \to\phi[\Gamma^*] by setting Reverse⁡~:=ϕ∘Reverse⁡∘ϕ−1\widetilde{\operatorname{Reverse}} := \phi\circ\operatorname{Reverse} \circ\phi^{-1}. By abuse of notation, we shall write Reverse⁡\operatorname{Reverse} as just Reverse⁡\operatorname{Reverse} when there is no need to distinguish between the two.

Theorem 24. [14] There exists a 2-Pebble Tree Transducer TT such that ⟦T⟧(t):=Reverse⁡(t)\llbracket T \rrbracket(t) := \operatorname{Reverse}(t) for all t∈ϕ[Γ∗]t \in\phi[\Gamma^*].

Proof. Consider the 2-Pebble Tree Transducer TT given by T:=(Σ,Σ,Q,q0,Δ)T := (\Sigma,\Sigma,Q,q_0,\Delta) where we have Q:={q0,qup,qdown,qout,qlleaf,qrleaf}Q := \{q_0,q_{\mathrm{up}},q_{\mathrm{down}},q_{\mathrm{out}},q_{\mathrm{lleaf}},q_{\mathrm{rleaf}}\}. Since there are only 2 pebbles, the bit-vector b⃗\vec{b} has at most one dimension, which is the presence or absence of the first pebble. Hence we shall denote it as ⊤\top or ⊥\bot respectively whenever applicable. The transition rules in Δ\Delta are as follows:

  • (a,∅,q0)→(qdown,addPebble⁡)(a,\varnothing,q_0) \to(q_{\mathrm{down}},\operatorname{addPebble}), ∀a∈Γ\forall a \in\Gamma,

  • (#,∅,q0)→(#,output0⁡)(\#,\varnothing,q_0) \to(\#,\operatorname{output0}),

  • (a,⊤,qdown)→(qdown,downRight⁡)(a,\top,q_{\mathrm{down}}) \to(q_{\mathrm{down}},\operatorname{downRight}), ∀a∈Γ\forall a \in\Gamma,

  • (a,⊥,qdown)→(qdown,downRight⁡)(a,\bot,q_{\mathrm{down}}) \to(q_{\mathrm{down}},\operatorname{downRight}), ∀a∈Γ\forall a \in\Gamma,

  • (#,⊥,qdown)→(qout,upLeft⁡)(\#,\bot,q_{\mathrm{down}}) \to(q_{\mathrm{out}},\operatorname{upLeft}),

  • (a,⊥,qout)→(a(qlleaf,qup),output2⁡)(a,\bot,q_{\mathrm{out}}) \to(a(q_{\mathrm{lleaf}},q_{\mathrm{up}}),\operatorname{output2}), ∀a∈Γ\forall a \in\Gamma,

  • (a,⊥,qup)→(qout,upLeft⁡)(a,\bot,q_{\mathrm{up}}) \to(q_{\mathrm{out}},\operatorname{upLeft}), ∀a∈Γ\forall a \in\Gamma,

  • (a,⊥,qlleaf)→(qout,downLeft⁡)(a,\bot,q_{\mathrm{lleaf}}) \to(q_{\mathrm{out}},\operatorname{downLeft}), ∀a∈Γ\forall a \in\Gamma,

  • (#,⊥,qout)→(#,output0⁡)(\#,\bot,q_{\mathrm{out}}) \to(\#,\operatorname{output0}),

  • (a,⊤,qup)→(a(qlleaf,qrleaf),output2⁡)(a,\top,q_{\mathrm{up}}) \to(a(q_{\mathrm{lleaf}},q_{\mathrm{rleaf}}),\operatorname{output2}), ∀a∈Γ\forall a \in\Gamma,

  • (a,⊤,qrleaf)→(qout,downRight⁡)(a,\top,q_{\mathrm{rleaf}}) \to(q_{\mathrm{out}},\operatorname{downRight}), ∀a∈Γ\forall a \in\Gamma.

We can observe that the above 2-Pebble Tree Transducer TT does reverse strings, i.e., ⟦T⟧(t)=Reverse⁡(t)\llbracket T \rrbracket(t) = \operatorname{Reverse}(t) for all t∈ϕ[Γ∗]t \in\phi[\Gamma^*].

Theorem 25. There does not exist a triple (L,ψ1,ψ2)(L,\psi_1,\psi_2) where LL is a regular tree language over ranked alphabet Λ\Lambda and ψ1,ψ2:Trees⁡(Λ)→Trees⁡(Γ)\psi_1,\psi_2 : \operatorname{Trees}(\Lambda) \to\operatorname{Trees}(\Gamma) are tree homomorphisms and a subset X⊆Trees⁡(Λ)X \subseteq\operatorname{Trees}(\Lambda) such that {(ψ1(t),ψ2(t))∣t∈X}⊆Reverse⁡\{(\psi_1(t),\psi_2(t)) \mid t \in X\} \subseteq\operatorname{Reverse}.

Proof. We present a proof by contradiction. Let us assume to the contrary that there exists a triple (L,ψ1,ψ2)(L, \psi_1, \psi_2) as specified above such that for a subset X⊆Trees⁡(Λ)X \subseteq\operatorname{Trees}(\Lambda), we have that {(ψ1(t),ψ2(t))∣t∈X}⊆Reverse⁡\{(\psi_1(t), \psi_2(t)) \mid t \in X\} \subseteq\operatorname{Reverse}. Then we see that when ψ1,ψ2\psi_1,\psi_2 as homomorphisms are restricted to the set Λ\Lambda, ψ1(x),ψ2(x)\psi_1(x),\psi_2(x) would necessarily have to be trees of height 1, for all x∈Λx \in\Lambda. In other words, we see that ψ1,ψ2\psi_1,\psi_2 are delabelings. Hence, from Nivat’s Theorem for tree transductions as cited in [4], we know that the bimorphism (L,ψ1,ψ2)(L,\psi_1,\psi_2) is equivalent to a bottom-up tree transducer over the alphabet. Since this transducer is by default a finite-state transducer, we see that it cannot realise Reverse⁡\operatorname{Reverse} as a transduced function, since the height of the tree to be reversed is unbounded. This contradicts our assumption about the existence of a bimorphism equivalent to Reverse⁡\operatorname{Reverse}, and completes the proof.

Theorem 26. There does not exist an action T:=(S,ψsrc,ψtgt,G)\mathsf{T}:=(S,\psi_{\mathrm{src}},\psi_{\mathrm{tgt}},G) such that T=Reverse⁡\mathsf{T}=\operatorname{Reverse} on ϕ[Γ∗]\phi[\Gamma^*].

Proof. We prove the theorem by contradiction. If possible, let T\mathsf{T} be such that T=Reverse⁡\mathsf{T}=\operatorname{Reverse} on ϕ[Γ∗]\phi[\Gamma^*]. We know that by definition, T(t):=⋃s∈STs(t)\mathsf{T}(t):=\bigcup_{s\in S}T_s(t). Let t0∈ϕ[(a+b)∗]t_0\in\phi[(a+b)^*]. Since T(t0)=Reverse⁡(t0)≠∅\mathsf{T}(t_0)=\operatorname{Reverse}(t_0)\ne\varnothing, let s0∈Ss_0\in S be such that ψsrc(s0)\psi_{\mathrm{src}}(s_0) matches t0t_0. Let σ\sigma be a substitution witnessing the match, i.e., σ(ψsrc(s0))=t0\sigma(\psi_{\mathrm{src}}(s_0))=t_0.

If there no common variable appearing in ψsrc(s0),ψtgt(s0)\psi_{\mathrm{src}}(s_0),\psi_{\mathrm{tgt}}(s_0), then we can substitute multiple contexts from the guard language for a variable appearing in the target pattern ψtgt(s0)\psi_{\mathrm{tgt}}(s_0) for the same context substituted in place of a variable in the source pattern ψsrc(s0)\psi_{\mathrm{src}}(s_0). This would contradict the fact that Reverse⁡\operatorname{Reverse} is a bijection.

Let xx be a variable in ψsrc(s0)\psi_{\mathrm{src}}(s_0) that also appears in ψtgt(s0)\psi_{\mathrm{tgt}}(s_0). Then we see that σ(x)∈Σ\sigma(x)\in\Sigma. If σ(x)∈Contexts⁡(Σ)\sigma(x)\in\operatorname{Contexts}(\Sigma) has height at least 2, then since xx also appears in ψtgt(s0)\psi_{\mathrm{tgt}}(s_0), σ(ψtgt(s0))\sigma(\psi_{\mathrm{tgt}}(s_0)) can never be Reverse⁡(t0)\operatorname{Reverse}(t_0). This implies that σ\sigma is a letter-to-letter substitution, i.e., ψsrc(s0)\psi_{\mathrm{src}}(s_0) is a relabeling of t0t_0, and consequently, ψtgt(s0)\psi_{\mathrm{tgt}}(s_0) is the ‘reverse’ of ψsrc(s0).\psi_{\mathrm{src}}(s_0). Since this is true for every element in ϕ[(a+b)∗]\phi[(a+b)^*], we must have that the bimorphism (S,ψsrc,ψtgt)(S,\psi_{\mathrm{src}},\psi_{\mathrm{tgt}}) is such that {(ψsrc(s),ψtgt(s))∣s∈S}⊆Reverse⁡\{(\psi_{\mathrm{src}}(s),\psi_{\mathrm{tgt}}(s)) \mid s\in S\}\subseteq\operatorname{Reverse}, which contradicts Theorem 25.

References

  1. [1]C. Aiswarya, Sahil Mhaskar, and M. Praveen. Checking regular invariance under tightly-controlled string modifications. In Developments in Language Theory: 26th International Conference, DLT 2022, Tampa, FL, USA, May 9–13, 2022, Proceedings, page 57–68, Berlin, Heidelberg, 2022. Springer-Verlag. doi:10.1007/978-3-031-05578-2_4.DOI
  2. [2]Rajeev Alur and Loris D’antoni. Streaming tree transducers. J. ACM, 64(5), aug 2017. doi:10.1145/3092842.DOI
  3. [3]Mikolaj Bojanczyk. Transducers with origin information. In Javier Esparza, Pierre Fraigniaud, Thore Husefeldt, and Elias Koutsoupias, editors, Automata, Languages, and Programming, pages 26–37, Berlin, Heidelberg, 2014. Springer Berlin Heidelberg.arxiv.org/abs/1309.6124
  4. [4]Hubert Comon, Max Dauchet, Rémi Gilleron, Florent Jacquemard, Denis Lugiez, Christof Löding, Sophie Tison, and Marc Tommasi. Tree Automata Techniques and Applications. 2008. URL: https://inria.hal.science/hal-03367725.
  5. [5]Loris D’antoni, Margus Veanes, Benjamin Livshits, and David Molnar. Fast: A transducer-based language for tree manipulation. ACM Trans. Program. Lang. Syst., 38(1), oct 2015. doi:10.1145/2791292.DOI
  6. [6]Joost Engelfriet. Bottom-up and top-down tree transformations - a comparison. Mathematical Systems Theory, 9:198–231, 06 1975. doi:10.1007/BF01704020.DOI
  7. [7]Joost Engelfriet and Heiko Vogler. Macro tree transducers. Journal of Computer and System Sciences, 31(1):71–146, 1985. URL: https://www.sciencedirect.com/science/article/pii/0022000085900662, doi:10.1016/0022-0000(85)90066-2.DOI

Paper details

vlnc:W_aftqm4qc
Version
v1 · latest
Published
Fresh — added to the Hub within the last 30 days
Added
License
CC BY 4.0
Usage

views · 9

downloads · 0

A view counts once per browser per day, a download each time the PDF is requested.

  • Computer Science

Not recorded for this paper

View statement
Looking for related papers…
Contents