Manuscript Offprint
arXiv:2610.11337Type-Checking for Pattern-Based Tree Transformations
Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.
Abstract
We introduce and study pattern-based tree transformations. As an illustrating example, consider a source pattern and a target pattern as a pair. This source pattern matches any expression of the form (by substituting with , with , and with ) and the pair transforms it into the expression 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 → Tree languages; Theory of computation → Transducers
Keywords and phrases Tree Transducers, Type-checking, Alternating Tree Automata, Pattern-matching
Related Version Full version of a preprint accepted at FSTTCS 2026
1 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 , where is any number. An equivalent expression is . Transformations like this are routinely used in Horner's method, an efficient way to evaluate polynomials. We consider the corresponding expression trees and
Corresponding author.
Type-Checking for Pattern-Based Tree Transformations
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 in the above example, which must repeat in multiple positions. The number can be arbitrary, so we need infinitely many pairs of patterns. For the example above, for each we need , as a (source pattern, target pattern) pair, where is a placeholder variable intended to match any expression . 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 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 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 and two morphisms . The transduction is defined as the relation . 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 . This is a regular word language over the alphabet . We define the source and target morphisms : as given by the following: and similarly we define: . We note that this bimorphism generates pairs of the form where is a nonzero binary number, from the seed word . We may add a guard to saying that can take values only from , 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 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 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 and . This can be checked by verifying that if the transformation is applied to any tree from a language L consisting of trees with alternation depth bounded by a constant c, the resulting tree is in
L 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 ∧ and ∨ 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 and output trees are required to have a property represented by another (possibly different) regular language .
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 S, satisfying multiple conditions under multiple transformations. On the source side, applying the morphism to the seed tree and then applying a substitution for the placeholder variables should result in a tree in . On the output side, applying the morphism to the same seed tree and then applying the same substitution for the placeholder variables should result in a tree not in . 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.
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 k-Pebble Transducers [14]. See Appendix A 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.
Type-Checking for Pattern-Based Tree Transformations
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.
2 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.
Source expression
Target expression

Figure 1 The transformation of an expression following distributivity. Here are arbitrary expressions. If 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 . The patterns would require a placeholder symbol 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 and to be identity on this part. However, gets inserted above in the source pattern, and at the root in the target pattern. In order to handle this we add new unary symbols and in the seed tree.
The seed tree and the morphisms for this transformation are depicted in Figure 2.
Note that, we require one such seed tree for each possible choice of 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 , every path has exactly one , and the segment between and has only ).
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 × in that branch. The product-depth of an expression is the maximum of the product-depths of its branches. The classes of k-pd-bounded expressions are those with


Figure 2 The seed tree and the morphisms generating the (source pattern, target pattern) pair capturing the transformation depicted in Figure 1.
product-depth at most k. We may be interested in checking whether k-pd-boundedness is preserved by the above transformation. This is an instance of type-checking problem, since k-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.
3 Formal Model
3.1 Preliminaries
Sets and Relations
Let be the set of strictly positive integers and let . For , we denote the set by . As a convention, we let . For a set and , denotes the usual Cartesian power of , consisting of all -tuples of elements in . As a convention, we let , a special singleton set.
For , an n-ary relation over is a subset . We denote the set of all n-ary relations on by Relations . Finally, we denote by Relations .
We consider automata over finite, ranked trees, following definitions from [4].
▶ Definition 1 (Alphabet). A finite set is called a finite ranked alphabet or alphabet, where are called the letters, and are nonnegative integers called their corresponding arities for all . We define pairwise disjoint sets for all . We require that . We also define the arity of .
▶ Definition 2 (Term, Tree). A term or a tree over a ranked alphabet is a partial function with the domain Positions(t) satisfying:
Positions(t) is nonempty and prefix-closed.
We say that a tree t is finite if Positions(t) is finite.
In this paper, we will only deal with finite trees. We denote the set of all finite trees over by Trees( ). A tree language is any subset of Trees( ). Regular tree languages are those recognised by tree automata as defined below.
Type-Checking for Pattern-Based Tree Transformations
▶ Definition 3 (Nondeterministic Bottom-up Tree {Transition System, Automaton}). Let be a finite alphabet. A Nondeterministic Bottom-up Tree Transition System or NBTTS over is defined as , where is a finite set of states, and is a function such that , for all . A Nondeterministic Bottom-up Tree Automaton or an NBTA is a tuple , where is a NBTTS over and is a set of final states. If for each we have that is such that for all , there exists a unique such that , 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 ( for NBTA). Given an NBTTS and a tree , a run of on is defined as a tree which is ‘compatible’ with , i.e., for every position , if , and for all , then, . The result of a run is . Given an NBTA , and a tree , we say that is accepted by if and only if there exists a run of on such that . The set of all trees accepted by a NBTA is called the language recognised by , and is denoted by .
Just like a word w 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 denotes disjoint union.
▶ Definition 5 (Context). A context with arity or an -context over is a tree over the alphabet such that each letter appears exactly once in the tree, for all . We refer to the positions occupied by as the holes of the context for all .
In this paper, we shall only deal with finite contexts. Intuitively, the leaves labeled with , or the holes of the context are the designated positions where other trees can be appended. We denote the set of all finite -contexts over by . We denote by , where we let .
3.2 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 , be two finite ranked alphabets, possibly not disjoint. Let be a countable set of variables of arity zero that is pairwise disjoint from , . For each , define the section of by by . Note that with this definition, we also get that .
Let be a mapping such that for all . Then, we define the tree homomorphism determined by as follows:
for each with arity 0.
for each , where the right-hand side is the result of replacing all occurrences of the symbol in by the term for all .
By abuse of notation, whenever there is no ambiguity, we shall drop the tilde and just refer to the tree homomorphism determined by just as simply .
We let 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 be a countable set of ranked variables or variables. For a variable , denotes its arity. We define pairwise disjoint sets for all . Further we also define the degree of a variable as .
▶ Definition 7 (Pattern). A tree pattern or simply a pattern over is defined to be a tree Pat Trees . We also let be the set of all variables appearing in Pat. Note that is necessarily finite. We also define the degree of a pattern Pat denoted by .
A pattern can represent multiple trees by substituting variables in the pattern by contexts of the same arity.
▶ Definition 8 (Substitution). A substitution over is a partial function such that for all , we have that , where .
Given a substitution , we can naturally extend it to the set of patterns over as follows. Define a tree homomorphism by setting
and extending it homomorphically. Since , by abuse of notation we shall write as 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 is a partial function , which assigns to each variable in its domain, a tree language over . The following formally defines when a pattern matches a tree.
▶ Definition 9 (Match). Let Pat be a pattern over and be a tree. Let be a guard such that . We say that matches pattern Pat under if and only if there exists a substitution such that
for each .
In such a case, is called a matching substitution, and we say the substitution matches the tree t to the pattern Pat under the guard G.
There may exist more than one matching substitution for a given match. Eg., if are letters and are variables, the tree matches the pattern with 2 distinct substitutions given by whereas .
The following formalises the tree transformations defined by our model.
▶ Definition 10 (Atomic Transformation). Let be a finite alphabet and be a countable set of variables. An atomic tree transformation or simply, an atomic transformation is an ordered triple , where are patterns over called
Type-Checking for Pattern-Based Tree Transformations
the source pattern and target pattern respectively, and is a guard over such that we have . The relation over trees induced by is
We denote by the effect of on the tree . We naturally extend the effect of to tree languages by setting . We also define . Our model consists of a possibly infinite set of atomic transformations, represented by a bimorphism.
▶ Definition 11 (Transformation). Let , be two finite alphabets. Let be a countably infinite set of variables. A transformation is a tuple where
■ is called the seed language.
are tree homomorphisms.
■ is a guard over .
We define the relation induced by a transformation as follows: for each seed tree , we define the atomic transformation . Then
For a tree and a tree language , and as before. We further define and .
4 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.
Let us bound the number of for loops in our programs by some fixed . Consider the alphabet where the unary 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 are written as mere sequences wrapped in a box for the sake of readability and space.
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.
Type-Checking for Pattern-Based Tree Transformations
int[] A = new int[m]; for int i=0; null i<A.length; null i++ null A[i] = getInputFromUser(); null for int j=0; null j<A.length; null j++ null int minIndex = j; int minValue = A[minIndex]; for int k=j; null k<A.length; null k++ null if(A[k] < minValue) { minIndex = k; minValue = A[k]; } null int temp = A[j]; int A[j] = A[k]; int A[k] = temp; null return A; null
int[] A = new int[m]; int i=0; while i<A.length; null A[i] = getInputFromUser(); i++ null int j=0; while j<A.length; null int minIndex = j; int minValue = A[minIndex]; int k=j; while k<A.length; null if(A[k] < minValue) { minIndex = k; minValue = A[k]; } k++ null int temp = A[j]; A[j] = A[k]; A[k] = temp; j++ null return A; null
Figure 4 Source and target code snippets from Fig 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 be the set of variables. Recall that , bound on the number of for loops in our programs, is a constant and hence we have only a fixed finite number of variables.

Figure 5 The seed tree generating the source and target patterns in Figure 6.
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 .
In general, the seed language will contain all trees in Trees( ) which contain each of the letters loop and endLoop 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 n loops, where n is a fixed natural number. We start with the various alphabets we use:
Tree alphabet , where is, for example, all the symbols available on a standard keyboard, each with an arity of 1.
Pattern alphabet , where .
Seed alphabet .
In the seed alphabet, we have binary letters for all . We interpret this as follows: the letter is a placeholder for the 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 , 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 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.
Type-Checking for Pattern-Based Tree Transformations


Figure 6 Source and target patterns. The source and target morphisms are identity everywhere, except for loop and endLoop , which are indicated by colors blue and red respectively.
For this condition to hold, we define for each , a finite-state tree automaton
where is given by
We see that each of the automata accepts all trees over such that the letters , both appear either once each, or not at all in . Thus having implies that every tree contains the pair of letters , 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 as:
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 s over the seed alphabet for the case , along with a run of the automaton shown in parenthesis, in blue. Note that the dashed edges represent the presence of any number of unary letters from ; since they “bubble up” any state, they can be represented as such for purposes of better understanding.

Finally, we define the seed language to be , which is a regular language.
In general, we see that for any program with no more than n for loops, we will be able to find a seed tree, which will give us a source pattern and a target pattern through homomorphisms respectively. The source pattern will match the program code with not more than n 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.
Type-Checking for Pattern-Based Tree Transformations
5 Decision Problems for Tree Transformations
A simple question is to check whether there is a matching.
| Matching Problem | |
|---|---|
| Input: | Pattern Pat, tree , guard . // The guard languages are represented by NBTA. |
| Question: | Does match Pat under ? |
We will show that this problem is already NP-complete.
▶ Theorem 12 ([13]). The Matching Problem is NP-complete.
Proof. (Upper Bound) Let be an instance of Matching Problem, where Pat is a pattern over , and where each value of the function is given by an NBTA. Let the size of the input be . Given a certificate, which is a particular substitution whose size is bounded by , we can verify whether in time, which is linear in the size of the input . We can also verify that for every , . 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 without negated variables. |
| Question: | Is there a truth assignment to the variables of such that every clause contains exactly one literal that is true and evaluates to true? |
We know from [16] that 1-in-3 SAT is NP-complete. Consider the following instance of 1-in-3 SAT, where is a finite set of variables and . We also have to be a function which evaluates to true iff exactly one of its arguments is set to true, and false otherwise.
Let be a ranked alphabet and be a set of ranked variables. Consider a pattern Pat over given by
and a tree over given by , where the number of 's in the tree is . We see that the only substitution which matches to is the one which assigns each true variable to and every false variable to . 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 , source language , target language . // The languages are represented by NBTA. |
| Question: | Do we have that ? |
▶ 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 is such that , then we just define/rename patterns , .
Proof. (Upper Bound) Let be an atomic action, and let be NBTAs with and . To determine whether , we need to find a substitution such that and under . We further simplify the problem at hand. Consider a bigger alphabet . Let Pat be a pattern over and Var given by . Let be an NBTA over which recognises all trees of the form with . Then Atomic Transformation Reachability Problem now reduces to finding a substitution over conforming to such that .
Let . Let us assume that each variable appears number of times in Pat. By Theorem 20, for each variable , we choose a pair such that is a relation realised by context on under with the added condition that . Once we have all such ordered pairs for individual variables, we simply use the substitution and check whether the resulting tree belongs to . This can be done in time given by
The number of times we have to perform this check operation is bounded above by the value . Hence the total time required for this is given by
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 be an instance of the Intersection Nonemptiness of DBTA, where each is DBTA over the alphabet for all . We construct a corresponding instance of Atomic Transformation Reachability Problem as follows. Let be a larger alphabet. Consider the atomic action with . Let be a NBTA such that . Let be an NBTA such that .
We can see that if and only if there exists such that for all . 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 , source language , target language . // The languages are represented by NBTA. |
| Question: | Do we have that ? |
Type-Checking for Pattern-Based Tree Transformations
▶ Theorem 14. The Transformation Reachability Problem is in 2-EXPTIME. More precisely, it can be solved in time , where poly is a polynomial of degree .
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 , source language , target language . // The languages are represented by NBTA. |
| Question: | Do we have that ? |
▶ 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.
6
Core Idea Behind the Automata Construction

Figure 7 The source tree is annotated with states of . We want to simulate the effect of running on the source tree in the seed tree itself. This means, when processing , it should guess and validate the potential transformation of the tuple to by a context (colored blue) in the source tree that matches , and simultaneously make sure that there is a substitution for that preserves the transformations to as well as to . An alternating tree automaton on the seed tree can achieve this.
To solve the Transformation Reachability Problem, we need to check for the existence of a seed tree giving rise to an atomic transformation , a source tree , a target tree and a substitution satisfying two constraints. First, the source tree should be in and second, the target tree should be in . The source tree is obtained from the seed tree by first applying the homomorphism and then applying the substitution . Suppose a node in the seed tree is transformed into a context as shown in the middle of Fig. 7. Suppose a variable is substituted with the tree as shown in the right side of Fig. 7. Consider a run of on the annotated source tree, in particular the state transformation to when the run parses the portion of the source tree that comes from the node 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 , it simulates 's state transformation to . To determine that is transformed to , the far-sighted automaton needs to know that is transformed to by .
The far-sighted automaton doesn’t know what is (in fact, our goal is to check if a suitable exists). There might be infinitely many substitutions 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 is not important; the important thing is that it transforms to . Any other which does the same transformation will work equally well in place of . 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 may occur multiple times in the source pattern. In the run of on the source tree, one occurrence of may encounter (which is transformed to ). Some other occurrence of may encounter some other pair (which is transformed to some other state, say ). What we need is a substitution that transforms to and also transforms to (and does other transformations too, if there are more occurrences of 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 . It similarly needs to check that the target tree is in . 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.
7 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 is a tuple , where is a finite set of states, is a set of initial states, and such that , for all , . Here, for any set , denotes the set of all positive boolean combinations of elements in .
The semantics of ATTA are defined using runs, which are themselves trees satisfying some conditions.
Type-Checking for Pattern-Based Tree Transformations
▶ Definition 17 (Run, Acceptance for ATTA). Given a tree and an ATTA over , we define a run of on to be a tree over such that for some state and every position satisfies the following condition: if , , , then there is a subset such that , the successor positions of in are , and for all .
A run is successful if for some initial state . A tree is accepted by ATTA if there exists at least one successful run of on . The set of all trees accepted by ATTA is called the tree language recognised by , and denoted by . A tree language is called regular if there exists an ATTA such that . The set of all regular tree languages over is denoted by .
Suppose is a word automaton with set of states and transition relation given by , we can extend it to such that iff has a run on the word starting from and ending at . The following defines a similar extension for NBTTS.
▶ Definition 18 (Extended Transition Relation(ETR)). Let be a NBTTS. We extend the transition function to a larger function called the Extended Transition Relation or ETR as follows. Let . For a tuple of states, define the NBTTS . Let us define the set of results to be the set given by is the result of a run of on . Finally, we also define the set . We also refer to the relation as the relation induced by the context on the states of the NBTTS . Note that .
Suppose a word is extended by appending another word to its end. Now, is obtained by simply composing the relations and . For trees, it is more complicated: a tree 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 be a set and be a nonnegative integer. Let be a relation. We define the relational lift of as a new relation given as follows: For , we say that if and only if
■ , where for all are such that .
■ For all , for all , we have iff there exist such that and .
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 and any , if we set , then . In case of NBTTS running on contexts, suppose are the children of a node labeled by the letter and we set , then implies that .
The following problem and its complexity is an important intermediate technical lemma used later.
| Relation Realisability Problem | |
|---|---|
| Input:Question: | NBTTS , relation , NBTA .Is realisable by under i.e., Does there exist such that ? |
As a matter of nomenclature, if the above condition holds for a given , then we say that the relation is realisable by under .
▶ 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 . |
| Question: | Do we have that ? |
We know from [4] that Intersection Nonemptiness of DBTA is EXPTIME-complete. Let be a given finite alphabet, and let be an instance of Intersection Nonemptiness of DBTA, where are DBTAs. If for some , we must have and hence the intersection . Hence we assume that for all .
Consider NBTA , with is defined as
Let be given by , where each is an arbitrarily chosen but fixed final state in for each . Also consider DBTA Shape which recognises all contexts of the form where . Consider the instance of the realisability problem given by .
We can see that is realisable by if and only if there exists a tree . Clearly, is realisable by there exists a context which induces the relation on the states of under for each and the labelling of the hole with , the result of a run of on yields a final state is recognised by for each . 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 be a transition system, relation , and NBTA be a given instance of Relation Realisability Problem. We reduce the problem of finding a context such that to the problem of NBTA nonemptiness. We will define an NBTA with relations over as states such that for any context , the result of a run of on is precisely the relation induced by . More formally, we define a NBTA as: , where for all we set , the relational lift of . We also set for all .
A straightforward induction on the height of a tree gives us that for a context , we have if and only if . Hence to find a context which 'realises' the relation , all we need to do is check for nonemptiness. Let automaton have states. We know from [4] that checking this NBTA for nonemptiness
Type-Checking for Pattern-Based Tree Transformations
can be performed in time which is a polynomial in . Since we have that , we see that given , 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 is realised by the context on the NBTTS , under . We also say that induces on under . For a given pair with NBTTS and NBTA , we define the set .
8 Deciding Transformation Reachability
Let be a given instance of Transformation Reachability Problem, where
S is the seed language over given by the NBTA .
are tree homomorphisms.
is a guard over such that , where we define(by abuse of notation) and also .
are the source and target languages respectively, which are regular tree languages given by the NBTAs for .
We see that solving the Transformation Reachability Problem is equivalent to finding whether there exists and a substitution such that for . Let be given by . We will construct a number of ATTAs such that the union of the languages they recognise will be equal to . This will imply that solving Transformation Reachability Problem will be a matter of checking each of the correspondingly constructed ATTAs for nonemptiness.
For each , using Theorem 20 we compute a priori, the set of relations of all the relations and corresponding contexts realisable by under . Similarly for each variable , we compute . For all variables , we compute the set
From this set, we obtain another set of pairs of relations that are simultaneously realisable by and along with a context that realises both of them. The idea is that given an element , we 'split' it into two components and . We do this for all elements in to get a pair of relations over respectively. Then, we define as
In other words, we have .
We fix a pair of choice functions of realisable relations over respectively such that
and for all . Note that this also provides us with corresponding contexts which realise the chosen relations.
Armed with this, we compute the ETR s for all and by simply considering each as a separate letter with the 'transition relation' given by the corresponding and running the corresponding automaton on it.
▶ Lemma 21. For each pair of choice functions , there exists an ATTA of size such that
where is a function which assigns to each variable , a context that induces the relation for and satisfies the guard .
We first give an explicit construction of the ATTA . To describe it succinctly, we define a few related concepts.
▶ Definition 22 (Destination Function). Let be tree languages over finite ranked alphabets respectively and let be a tree homomorphism. For each letter with , we define a function as follows. Since is a context of arity , we label the holes of by the set according to the precedence in the inorder traversal of . Then, hole of is labelled for all .
We illustrate this with an example. Let be a letter and let be given as in Figure 8. The numbers in blue denote the order of the vertex in the inorder traversal of .

Figure 8 Example computation of
Then would be given by
| i | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| {1,3,6} | {4} | {2,7} | {5} | ∅ |
Note that as demonstrated by this example, in each case we must necessarily have that the set is a partition of . Intuitively, we also consider the function 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 , we define ATTA given by
Type-Checking for Pattern-Based Tree Transformations
where we define as follows. For a fixed letter ,
■ For a state , we set
This transition basically ensures that the ATTA simulates .
■ For a state , we set
Intuitively, this transition simulates the running of on .
■ Similarly, for a state , we set
Intuitively, this transition simulates the running of on .
We also have an -transition
We illustrate this with an example. Let , and , i.e., is a letter with arity 5. Also let be defined as in Figure 8. Note that we have already completely computed the extended transition relation given the choice function . In such a case we define
Proof. For the given pair of choice functions , let be a function that assigns to each , a context that induces the relation for and satisfies the guard .
(⇔) Let such that and for some . This implies that for an accepting run of on , the result of that run is some final state . Similarly for an accepting run of on , the result of that run is some final state . Also, since , there is an accepting run of on such that the result of that run is . Then we can see that the set , the starting formula for . From here the run of simulates the run of on , and the runs of on for conforming to the transition . Since all of these are successful runs, the run of we described on is also a successful run, and hence .
(⇒) Conversely, let . This implies that there is an accepting run of on . Given the nature of the starting formula , we must have an , , such that . Since simulates on and , we see that . Since also simulates on conforming to for all , we see that for . 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 . Let 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 . We know that is bounded above by . . Let stateSize := . This gives an upper bound which eventually simplifies to the bound . We also know from Lemma 21 that the size of the ATTA is bounded above by and hence each nonemptiness check requires time given by . Hence the total time required for all the nonemptiness checks is given by:
Total running time is in .
Since , on simplifying the above, we get that the running time of the algorithm we have provided is , where poly is a polynomial of degree . This completes the proof of Theorem 14.
9 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.
References
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.
2 Rajeev Alur and Loris D'antoni. Streaming tree transducers. J. ACM, 64(5), Aug 2017. doi:10.1145/3092842.
3 Mikolaj Bojańczyk. Transducers with origin information. In Javier Esparza, Pierre Fraigniaud, Thore Husfeldt, and Elias Koutsoupias, editors, Automata, Languages, and Programming, pages 26–37, Berlin, Heidelberg, 2014. Springer Berlin Heidelberg.
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 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.
6 Joost Engelfriet. Bottom-up and top-down tree transformations - a comparison. Mathematical Systems Theory, 9:198–231, 06 1975. doi:10.1007/BF01704020.
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.
Type-Checking for Pattern-Based Tree Transformations
8 Patrick C. Fischer and Arnold L. Rosenberg. Multitape one-way nonwriting automata. Journal of Computer and System Sciences, 2(1):88–101, 1968. URL: https://www.sciencedirect.com/science/article/pii/S0022000068800066, doi:10.1016/S0022-0000(68)80006-6.
9 Blaise Genest, Anca Muscholl, Olivier Serre, and Marc Zeitoun. Tree pattern rewriting systems. In Sungdeok (Steve) Cha, Jin-Young Choi, Moonzoo Kim, Insup Lee, and Mahesh Viswanathan, editors, Automated Technology for Verification and Analysis, pages 332–346, Berlin, Heidelberg, 2008. Springer Berlin Heidelberg.
10 T. V. Griffiths. The unsolvability of the equivalence problem for -free nondeterministic generalized machines. J. ACM, 15(3):409–413, jul 1968. doi:10.1145/321466.321473.
11 Haruo Hosoya. Foundations of XML Processing: The Tree-Automata Approach. Cambridge University Press, 2010.
12 Armin Kühnemann. Benefits of tree transducers for optimizing functional programs. In Vikraman Arvind and Sundar Ramanujam, editors, Foundations of Software Technology and Theoretical Computer Science, pages 146–157, Berlin, Heidelberg, 1998. Springer Berlin Heidelberg.
13 Florin Manea and Markus L. Schmid. Matching patterns with variables. In Robert Mercas and Daniel Reidenbach, editors, Combinatorics on Words, pages 1–27, Cham, 2019. Springer International Publishing.
14 Tova Milo, Dan Suciu, and Victor Vianu. Typechecking for xml transformers. In Proceedings of the Nineteenth ACM SIGMOD-SIGACT-SIGART Symposium on Principles of Database Systems, PODS '00, page 11–22, New York, NY, USA, 2000. Association for Computing Machinery. doi:10.1145/335168.335171.
15 Maurice Nivat. Transductions des langages de Chomsky. Annales de l'Institut Fourier, 18(1):339–455, 1968. URL: http://www.numdam.org/articles/10.5802/aif.287/, doi:10.5802/aif.287.
16 Thomas J. Schaefer. The complexity of satisfiability problems. In Richard J. Lipton, Walter A. Burkhard, Walter J. Savitch, Emily P. Friedman, and Alfred V. Aho, editors, Proceedings of the 10th Annual ACM Symposium on Theory of Computing, May 1-3, 1978, San Diego, California, USA, pages 216–226. ACM, 1978. doi:10.1145/800133.804350.
A Comparison with k-Pebble Transducers
We use the notion of k-Pebble Tree Transducer as defined in [14]. For the sake of completeness, we include the definition here.
▶ Definition 23. A k-Pebble Tree Transducer is , where
are ranked input and output alphabets respectively.
Q is a finite set of states.
is the initial state.
is a finite set of transitions, each of which has one of the following forms:
where , , , .
Let be a finite alphabet. Let be the function defined as follows: For each with , , we let . Let be a finite ranked alphabet with the ranking function given by
We define the tree-representation-of-string function given by defining for with , as

Note that as defined above is an injective function. We define a function by setting . By abuse of notation, we shall write as just when there is no need to distinguish between the two.
▶ Theorem 24. [14] There exists a 2-Pebble Tree Transducer T such that for all .
Proof. Consider the 2-Pebble Tree Transducer given by where we have . Since there are only 2 pebbles, the bit-vector has almost one dimension, which is the presence or absence of the first pebble. Hence we shall denote it as or respectively whenever applicable. The transition rules in are as follows:
We can observe that the above 2-Pebble Tree Transducer does reverse strings, i.e., for all .
▶ Theorem 25. There does not exist a triple where is a regular tree language over ranked alphabet and are tree homomorphisms and a subset such that .
Type-Checking for Pattern-Based Tree Transformations
Proof. We present a proof by contradiction. Let us assume to the contrary that there exists a triple as specified above such that for a subset , we have that . Then we see that when as homomorphisms are restricted to the set , would necessarily have to be trees of height 1, for all . In other words, we see that are delabelings. Hence, from Nivat's Theorem for tree transductions as cited in [4], we know that the bimorphism 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 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, and completes the proof.
▶ Theorem 26. There does not exist an action such that on .
Proof. We prove the theorem by contradiction. If possible, let be such that on . We know that by definition, . Let . Since , let be such that matches . Let be a substitution witnessing the match, i.e., .
If there no common variable appearing in , then we can substitute multiple contexts from the guard language for a variable appearing in the target pattern for the same context substituted in place of a variable in the source pattern . This would contradict the fact that Reverse is a bijection.
Let be a variable in that also appears in . Then we see that . If has height at least 2, then since also appears in , can never be . This implies that is a letter-to-letter substitution, i.e., is a relabeling of , and consequently, is the ‘reverse’ of . Since this is true for every element in , we must have that the bimorphism is such that , which contradicts Theorem 25.