{'fraction_non_alphanumeric': 0.06751911237399363, 'fraction_numerical': 0.011140428026971563, 'mean_word_length': 3.373606864582306, 'pattern_counts': {'":': 1, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 79, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Higher-Order Nonemptiness Step by Step

We show a new simple algorithm that checks whether a given higher-order grammar generates a nonempty language of trees. The algorithm amounts to a procedure that transforms a grammar of order n to a grammar of order n-1, preserving nonemptiness, and increasing the size only exponentially. After repeating the procedure n times, we obtain a grammar of order 0, whose nonemptiness can be easily checked. Since the size grows exponentially at each step, the overall complexity is n-EXPTIME, which is known to be optimal. More precisely, the transformation (and hence the whole algorithm) is linear in the size of the grammar, assuming that the arity of employed nonterminals is bounded by a constant. The same algorithm allows to check whether an infinite tree generated by a higher-order recursion scheme is accepted by an alternating safety (or reachability) automaton, because this question can be reduced to the nonemptiness problem by taking a product of the recursion scheme with the automaton. A proof of correctness of the algorithm is formalised in the proof assistant Coq. Our transformation is motivated by a similar transformation of Asada and Kobayashi (2020) changing a word grammar of order n to a tree grammar of order n-1. The step-by-step approach can be opposed to previous algorithms solving the nonemptiness problem"in one step", being compulsorily more complicated.


Introduction
Higher-order grammars, also known as higher-order OI grammars [8,16], generalize contextfree grammars: nonterminals of higher-order grammars are allowed to take arguments. Such grammars have been studied actively in recent years, in the context of automated verification of higher-order programs. In this paper we concentrate on a very basic problem of language nonemptiness: is the language generated by a given higher-order grammar nonempty. This problem, being easy for most devices, is not so easy for higher-order grammars. Indeed, it is n-EXPTIME-complete for grammars of order n [15]. We give a new simple algorithm solving the language nonemptiness problem. The algorithm amounts to a procedure that transforms a grammar of order n to a grammar of order n − 1, preserving nonemptiness, and increasing the size only exponentially. After repeating the procedure n times, we obtain a grammar of order 0, whose nonemptiness can be easily checked. Since the size grows exponentially at each step, we reach the optimal overall complexity of n-EXPTIME. In a more detailed view, the complexity looks even better: the size growth is exponential only in the arity of types appearing in the grammar; if the maximal arity is bounded by a constant, the transformation (and hence the whole algorithm) is linear in the size of the grammar.
While a higher-order grammar is a generator of a language of (finite) trees, virtually the same object can be seen as a generator of a single infinite tree (encompassing the whole language). In this context, the grammars are called higher-order recursion schemes. The nonemptiness problem for grammars is easily equivalent to the question whether the arXiv:2009.08174v1 [cs.FL] 17 Sep 2020 tree generated by a given recursion scheme is accepted by a given alternating safety (or reachability) automaton; for the right-to-left reduction, it is enough to product the recursion scheme with the automaton. Thus, our algorithm solves also the latter problem, called a model-checking problem. This problem is decidable and n-EXPTIME-complete not only for safety or reachability automata, but actually for all parity automata, with multiple proofs using game semantics [17], collapsible pushdown automata [10], intersection types [14], or Krivine machines [20], and with several extensions [5,3,6,21,18]. The problem for safety automata was tackled in particular by Aehlig [1] and by Kobayashi [12]. To those algorithms we add another one. The main difference between our algorithm and all the others is that we solve the problem step by step, repeatedly reducing the order by one, while most previous algorithms work "in one step", being compulsorily more complicated. The only proofs that have been reducing the order by one, were proofs using collapsible pushdown automata [10,3,6], being very technical (and contained only in unpublished appendices). A reduction of order was also possible for a subclass of recursion schemes, called safe recursion schemes [11], but it was not known how to extend it to all recursion schemes.
Comparing the two variants of the model-checking problem for higher-order recursion schemes-involving safety and reachability automata, and involving all parity automata-we have to mention two things. First, while most theoretical results can handle all parity automata, actual tools solving this problem in practice mostly deal only with safety and reachability automata (called also trivial and co-trivial automata) [13,4,22,19]. Second, there exists a polynomial-time (although nontrivial) reduction from the variant involving parity automata to the variant involving safety automata [9].
Our transformation is directly motivated by a recent paper of Asada and Kobayashi [2]. They show how to transform a grammar of order n generating a language of words to a grammar of order n − 1 generating a language of trees, so that words of the original language are written in leaves of trees of the new language. Unexpectedly, this transformation increases the size of the grammar only polynomially. Our transformation is quite similar, but we start from a grammar generating a language of trees, not words. In effect, on the one hand, we do not say anything specific about the language after the transformation (except that nonemptiness is preserved), and on the other hand, the size growth is exponential, not polynomial.
The set of (simple) types is constructed from a unique ground type o using a binary operation →; namely o is a type, and if α and β are types, so is α → β. By convention, → associates to the right, that is, α → β → γ is understood as α → (β → γ). We often abbreviate α → · · · → α → β as α → β. The order of a type α, denoted ord(α), is defined Having a finite set of typed nonterminals X , and a finite set of typed variables Y, terms over (X , Y) are defined by induction: every nonterminal X ∈ X of type α is a term of type α; every variable y ∈ Y of type α is a term of type α; if K 1 , . . . , K k are terms of type o, then • K 1 , . . . , K k and ⊕ K 1 , . . . , K k are terms of type o; if K is a term of type α → β, and L is a term of type α, then K L is a term of type β.
The type of a term K is denoted tp(K). The order of a term K, written ord(K), is defined as the order of its type. We write Ω for ⊕ , and • for • . The construction ⊕ K 1 , . . . , K k is an alternative; such a term reduces to one of the terms K 1 , . . . , K k . This construction is used to introduce nondeterminism to grammars (defined below). In the special case of k = 0 (when we write Ω) no reduction is possible; thus Ω denotes divergence.
The construction • K 1 , . . . , K k can be seen as a generator of a tree node with k children; subtrees starting in these children are described by the terms K 1 , . . . , K k . In a usual presentation, nodes are labeled by letters from some finite alphabet. In this paper, however, we do not care about the exact letters contained in generated trees, only about language nonemptiness, hence we do not write these letters at all (in other words, we use a single-letter alphabet, where • is the only letter). Actually, in the sequel we even do not consider trees; we rather say that • K 1 , . . . , K k is convergent if all K 1 , . . . , K k are convergent (which can be rephrased as: the language generated from • K 1 , . . . , K k is nonempty if the languages generated from all K 1 , . . . , K k are nonempty).
A (higher-order) grammar is a tuple G = (X , X 0 , R), where X a finite set of typed nonterminals, X 0 ∈ X is a starting nonterminal of type o, and R a function assigning to every nonterminal X ∈ X a rule of the form X y 1 . . . y k → R, where tp(X) = (tp(y 1 ) → · · · → tp(y k ) → o), and R is a term of type o over (X , {y 1 , . . . , y k }). The order of a grammar is defined as the maximum of orders of its nonterminals.
Having a grammar G = (X , X 0 , R), for every set of variables Y we define a reduction relation −→ G between terms over (X , Y) and sets of such terms, as the least relation such that We say that a term M is G-convergent if M −→ G N for some set N of G-convergent terms. This is an inductive definition; in particular, the base case is when M −→ G ∅. In other words, M is G-convergent if there is a finite tree labeled by terms where for each node, the node and its children satisfy one of (1)-(3). Moreover, the grammar G is convergent if its starting nonterminal X 0 is G-convergent.

Transformation
In this section we present a transformation, called order-reducing transformation, resulting in the main theorem of this paper: Theorem 3.1. For any n ≥ 1, there exists a transformation from order-n grammars to order-(n − 1) grammars, and a polynomial p n such that, for any order-n grammar G, the resulting grammar G † is convergent if and only if G is convergent, and |G † | ≤ 2 pn(|G|) .
Intuitions. Let us first present intuitions behind our transformation. While reducing the order, we have to replace, in particular, order-1 functions by order-0 terms. Consider for example a term K L of type o, where K has type o → o. Notice that L generates trees that are inserted somewhere in contexts generated by K. Thus, when is K L convergent? There are two possibilities. First, maybe K is convergent without using its argument at all. Second, maybe K can be convergent but only using its argument, and then L also has to be convergent. Notice that in the first case K Ω is convergent (i.e., K is convergent even if the argument is not convergent), and in the second case K • is convergent (i.e., K is convergent if its argument is convergent). In the transformation, we transform K into two order-0 terms, K 0 and K 1 corresponding to K Ω and K •, and then we replace K L by ⊕ K 0 , • K 1 , L .
As a full example, consider an order-1 grammar with the following rules: It will be transformed to the order-0 grammar with the following rules: Notice that the original grammar is convergent "for two reasons": the ⊕ node in the rule for Y may reduce either to the first possibility (i.e., to •), or to the second possibility (i.e., to x), in which case convergence follows from convergence of the argument Z. This is reflected by the two possibilities available for the ⊕ node in the new rule for X: we either choose the first possibility and we depend only on convergence of Y 0 , or we choose the the second possibility and we depend on convergence of both Y 1 and Z. Notice that after replacing the (old and new) rule for Z by Z → Ω, the modified grammars remain convergent thanks to the first possibility above. Likewise, after replacing the original rule for Y by Y x → x, the new rules will be Y 0 → Ω and Y 1 → •, and the modified grammars remain convergent thanks to the second possibility above. However, after applying both these replacements simultaneously, the grammars stop to be convergent. If our term K takes multiple order-0 arguments, say we have K L 1 . . . L k , while transforming K we need 2 k variants of the term: each of the arguments may be either used (replaced by •) or not used (replaced by Ω). This is why we have the exponential blow-up. Let us compare this quickly with the transformation of Asada and Kobayashi [2], which worked for grammars generating words (i.e., trees where every node has at most one child). In their case, at most one of the arguments L i could be used, so they needed only k + 1 variants of K; this is why their transformation was polynomial.
For higher-order grammars we apply the same idea: functions of order 1 are replaced by terms of order 0, and then the order of any higher-order function drops down by one. For example, consider a grammar with the following rules: The nonterminal Y is again of type o → o, hence it is replaced by two nonterminals Y 0 , Y 1 of type o, describing the situation when the parameter x is either not used or used. Likewise, the corresponding parameter y of T is replaced by two parameters y 0 , y 1 . The resulting grammar will have the following rules: Formal definition. We now formalize the above intuitions. Having a type, we are interested in cutting off its suffix being of order 1. Thus, we use the notation α 1 → · · · → α k ⇒ o → o for a type α 1 → · · · → α k → o → o such that either k = 0 or α k = o. Notice that every type α can be uniquely represented in this form. We remark that some among the types α 1 , . . . , α k−1 (but not α k ) may be o. For a type α we write gar(α) ("ground arity") for the number for which we can write α = (α 1 → · · · → α k ⇒ o → o); we also extend this to terms: gar(M ) = gar(tp(M )).
We transform terms of type α to terms of type α † , which is defined by induction: Thus, we remove all trailing order-0 arguments, and we multiplicate (and recursively transform) remaining arguments. For a finite set S, we write 2 S for the set of functions A : S → {0, 1}. Moreover, we assume some fixed order on functions in 2 S , and we write P (Q A ) A∈2 S for an application P Q A1 . . . Q A 2 |S| , where A 1 , . . . , A 2 |S| are all the function from 2 S listed in the fixed order. The only function in 2 ∅ is denoted ∅.
Fix a grammar G = (X , X 0 , R). For every nonterminal X and for every function A ∈ 2 [gar(X)] we consider a nonterminal X † A of type (tp(X)) † . As the new set of nonterminals we take Likewise, for every variable y and for every function A ∈ 2 [gar(y)] we consider a variable y † A of type (tp(y)) † , and for a set of variables Y we denote Y † = {y † A | y ∈ Y, A ∈ 2 [gar(y)] }. We now define a function tr transforming terms. Its value tr(A, Z, M ) is defined when M is a term over some (X , Y), and A ∈ 2 [gar(M )] , and Z : Y {0, 1} is a partial function such that dom(Z) contains only variables of type o. The intention is that A specifies which among trailing order-0 arguments can be used, and Z specifies which order-0 variables (among those in dom(Z)) can be used. The transformation is defined by induction on the structure of M , as follows: For every rule X y 1 . . . y k z 1 . . . z → R in R, where = gar(X), and for every function A ∈ 2 [ ] , to R † we take the rule In the function A it is more convenient to count arguments from right to left (then we do not need to shift the domain in Case (7) above), but it is more natural to have variables z 1 , . . . , z numbered from left to right; this is why in the rule for X † A we assign to z i the value Finally, the resulting grammar

Complexity
In this section we analyze complexity of our transformation. First, we formally define the size of a grammar. The size of a term is defined by induction on its structure: Then |G|, the size of G is defined as the sum of |R| + k over all rules X y 1 . . . y k → R of G.
In Asada and Kobayashi [2] such a size is called Curry-style size; it does not include sizes of types of employed variables. We say that a type α is a subtype of a type β if either α = β, or β = (β 1 → β 2 ) and α is a subtype of β 1 or of β 2 . We write A G for the largest arity of subtypes of types of nonterminals in a grammar G. Notice that types of other objects appearing in G, namely variables and subterms of right sides of rules, are subtypes of types of nonterminals, hence their arity is also bounded by A G . It is reasonable to consider large grammars, consisting of many rules, where simultaneously the maximal arity A G is respectively small.
While the exponential bound mentioned in Theorem 3.1 is obtained by applying the order-reducing transformation to an arbitrary grammar, the complexity becomes slightly better if we first apply a preprocessing step. This is in particular necessary, if we want to obtain linear dependence in the size of G (and exponential only in the maximal arity A G ). The preprocessing, making sure that the grammar is in a simple form (defined below) amounts to splitting large rules into multiple smaller rules. A similar preprocessing is present already in prior work [13,2,7], however our definition of a simple form is slightly more liberal, so that the order reduction applied to a grammar in a normal form gives again a grammar in a normal form.
An application depth of a term R is defined as the maximal number of applications on a single branch in R, where a compound application K L 1 . . . L k counts only once. More formally, we define by induction: We say that a grammar G is in a simple form if the right side of each its rule has application depth at most 2.
Any grammar G can be converted to a grammar in a simple form, as follows. Consider a rule X y 1 . . . y k → R, and a subterm of R of the form f K 1 . . . K m , where f is a nonterminal or a variable, but some K i already has application depth 2. Then we replace the occurrence of K i with Y y 1 . . . y k (being a term of application depth 1) for a fresh nonterminal Y , and we add the rule Y y 1 . . . y k x 1 . . . x s → K i x 1 . . . x s (whose right side already has application depth 2; the additional variables x 1 , . . . , x s are added to ensure that the type is o). By repeating such a replacement for every "bad" subterm of every rule, we clearly obtain a grammar in a simple form. Types of nonterminals originating from G remain unchanged. The type of a fresh nonterminal Y introduced in the procedure is of the form where all α i and β i are types present also in G. The arity of the whole type is k + s, where k is the arity of the original nonterminal X (hence it is bounded by A G ), and s is bounded by the arity of the type of K i (hence also by A G ).
In order to bound the size of the resulting grammar, notice that the considered replacement is performed at most once for every subterm of the right side of every rule, hence the number of replacements is bounded by |G|. Each such a replacement increases the size of the grammar by at most O(A G ).

Lemma 4.2. For every grammar G in a simple form, the grammar G † (i.e., the result of the order-reducing transformation) is also in a simple form, and ord(G
. Moreover, the transformation can be performed in time linear in its output size. Proof. The part about the running time is obvious. It is also easy to see by induction that ord(α † ) = max(0, ord(α) − 1). It follows that the order of the grammar satisfies the same equality, because nonterminals of G † have type α † for α being the type of a corresponding nonterminal of G.
Recall that in the type α † obtained from α = (α 1 → · · · → α k → o), every α i either disappears or becomes (transformed and) repeated 2 gar(αi) times, that is, at most 2 A G times. This implies the inequality concerning A G † .
Every compound application can be written as f K 1 . . . K k L 1 . . . L , where f is a nonterminal or a variable, and = gar(f ). In such a term, every K i (after transforming) becomes repeated 2 gar(Ki) times, that is, at most 2 A G times. Then, for every L i we duplicate the outcome and we append a small prefix; this duplication happens times, that is, at most A G times. In consequence, we easily see by induction that while transforming a term of application depth d, its size gets multiplicated by at most O(2 2d·A G ). Moreover, every nonterminal X is repeated 2 gar(X) times, that is, at most 2 A G times. Because the application depth of right sides of rules is at most 2, this bounds the size of the new grammar by O(|G| · 2 5·A G ).
Looking again at the above description of the transformation, we can notice that the application depth cannot grow; in consequence the property of being in a simple form is preserved.
Thus, if we want to check nonemptiness of a grammar G of order n, we can first convert it to a simple form, and then apply the order-reducing transformation n times. This gives us a grammar of order 0, whose nonemptiness can be checked in linear time. By Lemmata 4.1 and 4.2, the whole algorithm works in time n-fold exponential in A G and linear in |G|.
If the original grammar G generates a language of words, we can start by applying the polynomial-time transformation of Asada and Kobayashi [2], which converts G into an equivalent grammar of order n − 1 (generating a language of trees); then we can continue as above. Because their transformation is also linear in |G|, and increases the arity only quadratically, in this case we obtain an algorithm working in time (n − 1)-fold exponential in A G and linear in |G|.

Correctness
In this section we finish a proof of Theorem 3.1 by showing that the grammar G † resulting from transforming a grammar G is convergent if and only if the original grammar G is convergent. This proof is also formalised in the proof assistant Coq, and available at GitHub (https://github.com/pparys/ho-transform-sbs). The strategy of our proof is similar as in Asada and Kobayashi [2]. Namely, we first show that reductions performed by G can be reordered, so that we can postpone substituting for (trailing) variables of order 0. To store such postponed substitutions, called explicit substitutions, we introduce extended terms. Then, we show that such reordered reductions in G are in a direct correspondence with Extended terms. In the sequel, terms defined previously are sometimes called non-extended terms, in order to distinguish them from extended terms defined below. Having a finite set of typed nonterminals X , and a finite set Z of variables of type o, extended terms over (X , Z) are defined by induction: if z ∈ Z is a variable of type o, and E is an extended term over (X , Z {z}), and L is a non-extended term of type o over (X , Z), then E L/z is an extended term over (X , Z); every non-extended term of type o over (X , Z) is an extended term over (X , Z). The construction of the form E L/z is called an explicit substitution. Intuitively, it denotes the term obtained by substituting L for z in E. Notice that the variable z being free in E becomes bound in E L/z , and that explicit substitutions are allowed only for the ground type o.
Of course a (non-extended or extended) term over (X , Z) can be also seen as a term over (X , Z ), where Z ⊇ Z. In the sequel, such extending of the set of variables is often performed implicitly.
Having a grammar G = (X , X 0 , R), for every set Z of variables of type o we define an ext-reduction relation G between extended terms over (X , Z) and sets of such terms, as the least relation such that We say that an extended term E over ( There is an "expand" function from extended terms to non-extended terms, which performs all the explicit substitutions written in front of an extended term: We also write exp(F) for {exp(F ) | F ∈ F} (where F is a set of extended terms). The following lemma, saying that we can consider ext-convergence instead of convergence, can be proved in a standard way (actually, Asada and Kobayashi have a very similar lemma [2, Lemma 18]); for completeness we attach a proof in Appendix A.
We extend the transformation function to extended terms, by adding the following rule, where E L/z is an extended term over (X , Z), and Z ∈ 2 Z (the first argument is always ∅, because all extended terms are of type o): Between ext-convergence and convergence of G † . Once we know that convergence and ext-convergence of G are equivalent (cf. Lemma 5.1), it remains to prove that ext-convergence of G is equivalent to convergence of G † , which is the subject of Lemma 5.2: The remaining part of this section is devoted to a proof of this lemma. Fix a grammar G = (X , X 0 , R). Of course the second part (concerning the grammars) follows from the first part (concerning an extended term) applied to the starting nonterminal X 0 . It is thus enough to prove the first part. We start with the left-to-right direction (i.e., from G-ext-convergence of E to G † -convergence of tr(∅, ∅, E)). We need two simple auxiliary lemmata. The first of them says that the tr function commutes with substitution:

Proof.
A straightforward induction on the structure of R.
The second lemma says that by increasing values of the function Z we can make the transformed term only more convergent: Lemma 5.4. Let E be an extended term over (X , Z {z}), and let Z ∈ 2 Z . If tr(∅, Proof. Denote P 0 = tr(∅, Z[z → 0], E) and P 1 = tr(∅, Z[z → 1], E). Tracing the rules of the transformation function, we can see that P 0 and P 1 are created in the same way, with the exception that occurrences of z in E are transformed to Ω in P 0 , and to • in P 1 . Thus, P 1 can be obtained from P 0 by replacing some occurrences of Ω to •. We know that P 0 is G † -convergent, which means that it can be rewritten using the −→ G relation until reaching empty sets. Moreover, the subterms Ω (which are present in P 0 , but not in P 1 ) cannot be reached during this rewriting, because Ω is not G † -convergent. Thus, P 1 can be rewritten in exactly the same way as P 0 , so it is also G † -convergent.
The next lemma shows how ext-reductions of G are reflected in G † : Proof. Induction on the definition of E G F. We analyze particular cases appearing in the definition. Missing details are given in Appendix B.
In Case (1) E consists of an application of arguments to some nonterminal X. For simplicity of presentation, suppose that X has two arguments: y of positive order, and z of order 0 (the general case is handled in the appendix). Then First, we prove that P j −→ G † {Q j }. By definition we have that and by Lemma 5.3 we have that where the second equality holds because the z does not appear in K and the variables from dom(Z) do not appear in R. Recalling that the rule for X † A is Having this, we recall that for appropriate L (obtained by transforming L). Recall that, by definition, a term M is G † -convergent if and only if M −→ G † N for some set N of G † -convergent terms. Thus, the only way why tr(∅, Z, F ) can be G † -convergent (which holds by assumption) is that either Q 0 is G † -convergent, or both Q 1 and L are G † -convergent. Because of the reduction P j −→ G † {Q j } we have that either P 0 is G † -convergent, or both P 1 and L are G † -convergent, which implies that tr(∅, Z, E) is G † -convergent. In Cases (2) and (3), when E = • K 1 , . . . , K k or E = ⊕ K 1 , . . . , K k , we have a reduction from tr(∅, Z, E) to {tr(∅, Z, F ) | F ∈ F }, because tr distributes over • . . . and ⊕ . . . . In Cases (4) and (5) (elimination of explicit substitution) we also have similar reductions.
Finally, in Case (6) we have that By definition, for every i ∈ {0, . . . , k} we have that Thus, tr(∅, Z, E i L/z ) is G † -convergent if and only if either P 0 i is G † -convergent, or both P 1 i and L are G † -convergent. By assumption this is the case for all i ∈ [k], and we have to prove this for i = 0. If for every i ∈ [k] we have the former case (i.e., P 0 i is G † -convergent), by the induction hypothesis (used with the function Z[z → 0]) we have that P 0 0 is G † -convergent, and we are done. In the opposite case, for some i ∈ [k] (but for at least one of them) we have that both P 1 i and L are G † -convergent, and for the remaining i ∈ [k] we have that P 0 i is G † -convergent. Using Lemma 5.4 we deduce that if P 0 i is G † -convergent, then also P 1 i is G † -convergent. Thus actually P 1 i is G † -convergent for every i ∈ [k], and additionally L is G † -convergent. By the induction hypothesis (used with the function Z[z → 1]) we have that P 1 0 is G † -convergent, and we are also done.
We can now conclude with the left-to-right direction of Lemma 5.2: Lemma 5.6. Let E be an extended term over (X , ∅). If E is G-ext-convergent, then tr(∅, ∅, E) is G † -convergent.
Proof. Induction on the fact that E is G-ext-convergent. Because E is G-ext-convergent, E G F for some set F of G-ext-convergent extended terms, for which we can apply the induction hypothesis. The induction hypothesis says that tr(∅, ∅, F ) is G † -convergent for every F ∈ F. In such a situation Lemma 5.5 implies that tr(∅, ∅, E) is also G † -convergent, as required.
For a proof in the opposite direction we need the following definition. We say that a term M G † -convergent in n steps if M −→ G † {N 1 , . . . , N k }, and every N i is G † -convergent in n i steps, and n = 1 + n 1 + · · · + n k (i.e., we count 1 for the above reduction, and we sum the numbers of steps needed to reduce all N i ). Clearly a term M is G † -convergent if and only if it is G † -convergent in n steps for some n ∈ N. Notice that the number n is not determined by M (i.e., that the same term M can be G † -convergent in n steps for multiple values of n). We can now state the converse of Lemma 5.5: Lemma 5.7. Let E be an extended term over (X , Z) and let Z ∈ 2 Z . If tr(∅, Z, E) is G † -convergent in n steps and E is not a variable, then there exists a set F of extended terms such that E G F and tr(∅, Z, F ) is G † -convergent in less than n steps for every F ∈ F.

Proof. Induction on the number of explicit substitutions in E.
Depending on the shape of E, we have several cases. Missing details are given in Appendix C.
One case is E consists of a nonterminal X with some arguments applied. For simplicity of presentation, we again suppose that X has two arguments: y of positive order, and z of order 0. Thus, E is of the form E = X K L. Let X y z → R be the rule for X, and let z be a fresh variable of type o not appearing in Z. In such a situation, taking F = R[K/y, z /z] L/z we have that E G {F }. Recall the terms P j and Q j (for j ∈ {0, 1}) from the proof of Lemma 5.5. In that proof we have observed that P j −→ G † {Q j }. But clearly this is the only way how P j can reduce, so if P j is G † -convergent in n j steps, then necessarily Q j is G † -convergent in n j − 1 steps. By Equalities (1) we have that if tr(∅, Z, E) is G † -convergent in n steps, then either P 0 is G † -convergent in n 0 = n − 1 steps, or both P 1 and L are G † -convergent in, respectively, n 1 and n − n 1 − 2 steps, for some n 1 ∈ N. In the former case, Q 0 is G † -convergent in n 0 − 1 = n − 2 steps, so tr(∅, Z, F ) is G † -convergent in n − 1 steps, and we are done. In the latter case, Q 1 is G † -convergent in n 1 − 1 steps, so tr(∅, Z, F ) is G † -convergent in (n 1 − 1) + (n − n 1 − 2) + 2 = n − 1 steps, and we are done again.
Notice that we do not have a similar case for a variable with some arguments applied, because the whole E is not a variable, and because (by definition of an extended term) all free variables of E are of type o.
The cases of E = • K 1 , . . . , K k and E = ⊕ K 1 , . . . , K k are straightforward. It remains to assume that E is an explicit substitution. If E = z L/z , we should take F = {L}, and if E = z L/z for z = z, we should take F = {z } (in these two subcases we cannot use the induction assumption, because it does not work for an extended term being a single variable). Otherwise E = E 0 L/z , where E 0 is not a variable. Recall that tr(∅, Z, E) = ⊕ P 0 0 , • P 1 0 , L for P 0 0 , P 1 0 , L as in the proof of Lemma 5.5. By assumption tr(∅, Z, E) is G † -convergent in n steps, so either P 0 0 is G † -convergent in n = n − 1 steps, or both P 1 0 and L are G † -convergent in, respectively, n and n − n − 2 steps, for some n ∈ N. Let j = 0 in the former case and j = 1 in the latter case. The induction hypothesis gives us a set {E 1 , . . . , E k } such that E 0 G {E 1 , . . . , E k } and tr (∅, Z[z → j], E i ) is G † -convergent in less than n steps for every i ∈ [k]. We then take Equality (2) holds now for all i ∈ {0, . . . , k}. For j = 0 we use that the fact that tr(∅, i }, which implies that tr(∅, Z, E i L/z ) is G † -convergent in less than n + 1 = n steps, as required. For j = 1 we use that the fact that tr(∅, is G † -convergent in less than n + (n − n − 2) + 2 = n steps, as required.
The next lemma finishes the proof of Lemma 5.2, and thus the proof of correctness of our transformation: Proof. Induction on the (smallest) number n such that tr(∅, ∅, E) is G † -convergent in n steps. By assumption E is not a variable, because it is an extended term over (X , ∅) (no free variables). So, by Lemma 5.5 there exists a set F of extended terms such that E G F and tr(∅, ∅, F ) is G † -convergent in less than n steps for every F ∈ F. By the induction hypothesis every F ∈ F is G-ext-convergent, so by definition also E is G-ext-convergent.

Conclusions
We have presented a new, simple algorithm checking whether a higher-order grammar generates a nonempty language. One may ask whether this algorithm can be used in practice.
Of course the complexity n-EXPTIME for grammars of order n is unacceptably large (even if we take into account the fact that we are n-fold exponential only in the arity of types, not in the size of a grammar), but one has to recall that there exist tools solving the considered problem in such a complexity. The reason why these tools work is that the time spent by them on "easy" inputs is much smaller than the worst-case complexity (and many "typical inputs" are indeed easy). Unfortunately, this is not the case for our algorithm: the size of the grammar resulting from our transformation is always large, even if the original grammar generated a nonempty (or empty) language for some "easy reason". Thus, our algorithm is mainly of a theoretical interest. The presented transformation preserves nonemptiness, and thus can be used to solve the nonemptiness problem for higher-order grammars. However, it seems feasible that other problems concerning higher-order grammars (higher-order recursion schemes), like modelchecking against parity automata or the simultaneous unboundedness problem [7], can be solved using similar transformations. Developing such transformations is a possible direction for further work.

A Proof of Lemma 5.1
In this section we prove Lemma 5.1. To this end, fix a grammar G = (X , X 0 , R). Of course the second part (about convergence/ext-convergence of the grammar) follows from the first part (about convergence/ext-convergence of an extended term) applied to the starting nonterminal X 0 . It is thus enough to prove the first part. We start with the left-to-right direction (i.e., from ext-convergence to convergence). We need a simple auxiliary lemma:

Proof. A trivial case analysis on the definition of
The main ingredient of the proof is the following lemma: Proof. Induction on the definition of E G F. (2) If E = • K 1 , . . . , K k and F = {K 1 , . . . , K k }, then We can now conclude with the left-to-right direction of Lemma 5.1: Proof. Induction on the fact that E is G-ext-convergent. Because E is G-ext-convergent, E G F for some set F of G-ext-convergent extended terms (for which we can apply the induction hypothesis). Using the induction hypothesis, for every extended term F in F we obtain that exp(F ) is G-convergent, that is, all terms in exp(F) are G-convergent. By Lemma A.2, we have that either exp(E) −→ G exp(F), or exp(F) = {exp(E)}. In the latter case we already know that exp(E) (as an element of exp(F)) is G-convergent. In the former case, we use the definition of G-convergence, and we also obtain that exp(E) is G-convergent.
We now come to the opposite direction: from convergence to ext-convergence. We say that an extended term E is simplified if E is not of the form z L 1 /z 1 . . . L /z (i.e., if the non-extended term inside all explicit substitutions is not a variable). It turns out that every extended term can be ext-reduced to a simplified one (this is shown in the proof of Lemma A.5). Then, for a simplified extended term we can find an ext-reduction corresponding to a given standard reduction: Lemma A.4. If E is a simplified extended term over (X , ∅), and if exp(E) −→ G N , then E G F for some F such that exp(F) = N .

Proof. The extended term E can be written in the form
. Below, we prove that there exists F such that E G F and {Ξ(exp(F )) | F ∈ F } = N . This already gives the thesis for Indeed, on the one hand, because of E G F , due to rule (6) of the definition of G (applied s times), we have that On the other hand, due to {Ξ(exp(F )) | F ∈ F } = N , we have that Thus, it remains to prove existence of the aforementioned set F . We have four cases depending on the shape of E : The term E is of the form y M 1 . . . M m for some variable y. Then y has to be one of the variables z 1 , . . . , z s , because the whole E has no free variables; in particular z is of order 0. However, this is actually impossible. Indeed, for m = 0 this is impossible, because E is simplified (i.e., E is not a variable), and for m ≥ 1 this is impossible, because then y would be of positive order. (1) The term E is of the form X K 1 . . . K k L 1 . . . L for a nonterminal X, where = gar(X).
where the second equality holds because R does not contain the variables z 1 , . . . , z s . We In the last lemma we prove the right-to-left direction of Lemma 5.1: Proof. Induction on the fact that exp(E) is G-convergent, and internally on the number of explicit substitutions in E. One case is that E is simplified. Because exp(E) is G-convergent, exp(E) −→ G N for some set N of G-convergent terms (for which we can apply the induction hypothesis). By Lemma A.4, there is a set F such that E G F and exp(F) = N . The latter means that N = {exp(F ) | F ∈ F }. Using the induction hypothesis for every term in N , we obtain that all extended terms F in F are G-ext-convergent. Due to E G F, this implies that E is G-ext-convergent.
The opposite case is that the extended term E is not simplified, that is, it is of the form E = z L 1 /z 1 L 2 /z 2 . . . L k /z k , where z is one of the variables z 1 , . . . , z k (the whole E does not have free variables). Suppose first that z = z 1 , and take F = L 1 L 2 /z 2 . . . L k /z k . Then Notice that F has less explicit substitutions than E (the term L 1 is non-extended), so we can use the internal induction hypothesis, obtaining that F is G-ext-convergent. Moreover, we have that z L 1 /z 1 G {L 1 } by rule (4) of the definition of G , thus also E G {F } by rule (6) of this definition (used k − 1 times). In consequence, also E is G-ext-convergent.
When z = z i for i ≥ 2, we proceed similarly. Taking F = z L 2 /z 2 . . . L k /z k we have that Because F has less explicit substitutions than E, we can use the internal induction hypothesis, obtaining that F is G-ext-convergent. Moreover, we have that z L 1 /z 1 G {z} by rule (5) of the definition of G , thus also E G {F } by rule (6) of this definition (used k − 1 times). In consequence, also E is G-ext-convergent.

B
Additional details for the proof of Lemma 5.5 We now complement the proof of Lemma 5.5 with missing details. Recall that we are given an extended term E over (X , Z) and a function Z ∈ 2 Z . Knowing that E G F and that tr(∅, Z, F ) is G † -convergent for every F ∈ F, we have to prove that tr(∅, Z, E) is also G † -convergent.
As already said, we proceed by induction on the definition of E G F, and we analyze particular cases of this definition.
(1) Suppose that where = gar(X), and R(X) = (X y 1 . . . y k z 1 . . . z → R), and z 1 , . . . , z are fresh variables of type o not appearing in Z. For every s ∈ {0, . . . , } and every function Recall that the rule for X † A is Next, suppose that s ≥ 1. Let us denote Simultaneously L = tr(∅, Z s,A , L s ), because variables z s+1 , z s+2 , . . . , z do not appear in L s . By definition we have that By assumption Q s,A is G † -convergent, so either Q 0 is G † -convergent, or both Q 1 and L are G † -convergent. In the former case, P 0 is G † -convergent by the induction hypothesis, and we have P s,A −→ G † {P 0 }, so P s,A is G † -convergent, as required. In the latter case, P 1 is G † -convergent by the induction hypothesis, and we have • P 1 , L −→ G † {P 1 , L } and P s,A −→ G † {• P 1 , L }, so also P s,A is G † -convergent, as required.
By assumption, elements of the latter set are G † -convergent, so also tr(∅, Then, by definition, By assumption tr(∅, Z, K i ) is G † -convergent, so also tr(∅, Z, E) is G † -convergent. By assumption tr(∅, Z, L) is G † -convergent, so also tr(∅, Z, E) is G † -convergent. By assumption P is G † -convergent, so also also tr(∅, Z, E) is G † -convergent. (6) The last case, when was completely resolved in Section 5, so we do not repeat the proof here.

C Additional details for the proof of Lemma 5.7
In this section we give missing details for the proof of Lemma 5.7. Recall that we are given an extended term E over (X , Z), which is not a variable, and a function Z ∈ 2 Z . Knowing that tr(∅, Z, E) is G † -convergent in n steps, we have to prove that there exists a set F of extended terms such that E G F and tr(∅, Z, F ) is G † -convergent in less than n steps for every F ∈ F. The proof is by induction on the number of explicit substitutions in E. Depending on the shape of E, we have six cases.
(1) Suppose that E starts with an application. Then necessarily it can be written as E = X K 1 . . . K k L 1 . . . L , where = gar(X). In particular, notice that instead of the nonterminal X we cannot have a variable, because (by definition of an extended term) all variables in Z are of type o. Let X y 1 . . . y k z 1 . . . z → R be the rule for X, and let z 1 , . . . , z be fresh variables of type o not appearing in Z. In such a situation we have that E G {F } for F = R[K 1 /y 1 , . . . , K k /y k , z 1 /z 1 , . . . , z /z ] L 1 /z 1 . . . L /z .
For every s ∈ {0, . . . , } and every function A ∈ 2 [ −s] , let us define P s,A , Z s,A , and Q s,A as in Appendix B. We prove, by induction on s, that if P s,A is G † -convergent in n s steps (for some n s ∈ N) then Q s,A is G † -convergent in n s − 1 steps. For s = and A = ∅ this gives the thesis, taking F = {F } (because tr(∅, Z, E) = P ,∅ and tr(∅, Z, F ) = Q ,∅ ). Suppose first that s = 0. Recall that in the proof of Lemma 5.5 we have observed that P s,A −→ G † {Q s,A }. Actually, if P s,A −→ G † N then necessarily N = {Q s,A } (because P s,A is a nonterminal with applied arguments, and in this case the reduction is completely deterministic). By assumption P s,A is G † -convergent in n s steps, which, by definition, immediately implies that Q s,A is G † -convergent in n s − 1 steps, as required.
Next, suppose that s ≥ 1. Using the definition of P 0 , P 1 , Q 0 , Q 1 , and L from Appendix B, we have that P s,A = ⊕ P 0 , • P 1 , L and Q s,A = ⊕ Q 0 , • Q 1 , L .
We can have P s,A −→ G † N only for N = {P 0 } and for N = {• P 1 , L }, and we can have • P 1 , L −→ G † N only for N = {P 1 , L }. By assumption P s,A is G † -convergent in n s steps, so either P 0 is G † -convergent in n s−1 = n s − 1 steps, or both P 1 and L are G † -convergent in, respectively, n s−1 and n s − n s−1 − 2 steps, for some n s−1 ∈ N. In the former case, Q 0 is G † -convergent in n s−1 − 1 = n s − 2 steps by the induction hypothesis, and we have Q s,A −→ G † {Q 0 }, so Q s,A is G † -convergent in n s − 1 steps, as required. In the latter case, Q 1 is G † -convergent in n s−1 − 1 steps by the induction hypothesis, and we have • Q 1 , L −→ G † {Q 1 , L } and Q s,A −→ G † {• Q 1 , L }, so Q s,A is G † -convergent in (n s−1 − 1) + (n s − n s−1 − 2) + 2 = n s − 1 steps, as required.
(2) Suppose that E starts with •. Then it can be written as E = • K 1 , . . . , K k , and, by definition, tr(∅, Z, E) = • tr(∅, Z, K 1 ), . . . , tr(∅, Z, K k ) , and tr(∅, Z, E) −→ G † N only for N = {tr(∅, Z, K 1 ), . . . , tr(∅, Z, K k )}. We know that tr(∅, Z, E) is G † -convergent in n steps, so necessarily the terms tr(∅, Z, K i ) for i ∈ [k] are G † -convergent in n i steps, for some numbers n i such that n 1 + · · · + n k + 1 = n. In particular all n i are smaller than n, so F = {K 1 , . . . , K k } satisfies the thesis, because E G F, by definition. We know that tr(∅, Z, E) is G † -convergent in n steps, so, by the above, P is G † -convergent in less than n steps. In consequence F = {z } satisfies the thesis, because E G F, by definition. (6) Finally, suppose that E = E 0 L/z , where E 0 is not a variable. This case was completely resolved in Section 5, so we do not repeat the proof here.