The Universality of Higher–Order Attributed Tree ... - CiteSeerX

2 downloads 0 Views 281KB Size Report
We prove the universality of this formalism by showing that the class of functions ... exploit this approach by establishing the universality of HO–AGs with respect ...
The Universality of Higher–Order Attributed Tree Transducers Thomas Noll Department of Teleinformatics, Royal Institute of Technology (KTH) Electrum 204, S–16440 Kista, Sweden∗ e–mail: [email protected] Heiko Vogler Institut f¨ ur Softwaretechnik I, Dresden University of Technology D–01062 Dresden, Germany e–mail: [email protected]

Abstract In this paper we present higher–order attributed tree transducers as a formal computational model for higher–order attribute grammars. The latter is a generalization of the classical concept of attribute grammars in the sense that during attribute evaluation, the input tree can be enlarged by inserting subtrees which were computed during attribute evaluation. We prove the universality of this formalism by showing that the class of functions described by higher–order attributed tree transducers coincides with the class of (partial) recursive tree functions.

1

Introduction

Attribute grammars were devised by Knuth [Knu68, Knu71] to describe semantical aspects of context–free languages. They give a meaning to every derivation tree of the underlying context– free grammar by assigning values to various attributes associated with the nodes of the tree. Here, the derivation tree is static; it is not possible to modify its structure. Higher–order attribute grammars (HO–AGs for short) were developed by Vogt, Swierstra, and Kuiper in [VSK89] in order to overcome this restriction. They remove the distinction between tree construction and attribute evaluation by allowing the derivation tree to be extended by new subtrees which were computed as attribute values. More exactly, they introduce so–called nonterminal attributes which act as both attributes and nonterminal symbols of the underlying grammar. Thus a tree extension is caused by assigning a (tree) value to a nonterminal attribute occurrence. Then the usual attribute evaluation is performed on the extended derivation tree. This concept is useful in practical applications such as multi–pass compilers (cf. [VSK89]), syntax–directed programming environments ([TC90]), or compilers for supercombinators ([SV91]). It is obvious that these higher–order features increase the programming comfort of the attribute grammar formalism. In this paper we raise the question whether they also have any impact on its computational power. We will answer it in the affirmative by showing that there ∗

Most of the work has been done at Lehrstuhl f¨ ur Informatik II, Aachen University of Technology, Germany.

1

exist functions mapping derivation trees to attribute values which can not be computed without tree extensions. Certainly, this statement must be founded on some computational model of HO–AGs. In the first–order case (i.e., for usual attribute grammars), attributed tree transducers (henceforth called ATTs) were developed by F¨ ul¨op ([F¨ ul81]; also cf. [FV98]) as an appropriate formalism which abstracts from the concrete representation of attribute grammars in the following sense: instead of distinguishing the production symbols of the underlying context–free grammar and the operation symbols which occur on the right–hand sides of attribute rules, a general ranked alphabet is used to represent both. This is particularly appropriate in the higher–order case since, as explained above, the HO–AG approach does not distinguish between the domain of derivation trees and the domain of attributes. Furthermore, computations are performed in the semantic domain of the initial term algebra for this ranked alphabet; that is, an operation symbol ω of rank n is applied to its arguments s1 , . . . , sn just by forming the tree ω(s1 , . . . , sn ). In this way, the expressive power of attribute grammars has been characterized as a certain class of tree functions (i.e., functions from trees to trees) which is properly contained in the class PREC of primitive recursive tree functions (cf. [Hup78]; also see [EV91]). In fact, in [FHVV93] it is proved that PREC is closed under attributed tree transducers if they are considered as schemes of primitive recursion. On the other hand, [VSK89] expresses the conjecture that HO–AGs “give us the power to define and evaluate [arbitrary] recursive functions.” However a proof is missing and demands for a formalization of the rather informal presentation in that paper. Here we provide this basis by presenting higher–order attributed tree transducers (HO–ATTs for short) as a computational model for HO–AGs. They enable us to prove the above conjecture formally by showing that HO–ATTs are universal in the following sense: their computational power is characterized by the class of partial recursive tree functions. The latter is defined in [Hup78], and it is also shown there, using appropriate encoding and decoding mappings, that it corresponds to the class of partial recursive functions over natural numbers. The proof of our claim is given constructively by associating with every partial recursive tree function f a HO–ATT Mf which computes f . More exactly, we inductively define a “control tree” which is used as a nonterminal attribute in Mf and which controls the computation of f by mapping the arguments of f , given as inherited attribute values at the root of the control tree, to the function result by evaluating the corresponding synthesized attribute. Not surprisingly, it turns out that the handling of unbounded recursion (iteration) requires the tree extension capability. (The latter is also used to implement the primitive recursion operator.) Thus the contribution of this paper is twofold. On the one hand, we provide HO–ATTs as a formal basis for reasoning about the computational behavior of HO–AGs. On the other hand, we exploit this approach by establishing the universality of HO–AGs with respect to computations in initial term algebras. The remainder of this paper is organized as follows. In Sections 2 and 3 we collect the fundamental definitions dealing with partial recursive tree functions and (HO–)ATTs, respectively. The equivalence of these formalisms is proved in Section 4.

2 2.1

Preliminaries Reduction systems

The computational behavior of devices like tree transducers can be described in the abstract setting of reduction systems. We only give a sketch of the basic notions; for a more thorough representation, the reader is referred e.g. to [BN98].

2

Definition 2.1 A reduction system is a pair (A, ⇒) where A is a set and ⇒ ⊆ A × A. The transitive and the reflexive–transitive closure of ⇒ is denoted by ⇒+ and ⇒∗ , respectively. An element a ∈ A is called reducible if there exists b ∈ A such that a ⇒ b, otherwise irreducible. In the former case, b is called a direct successor of a. If a ⇒∗ b, then b is called a successor of a. If there is exactly one irreducible successor of a, then this element is called the normal form of a and it is denoted by nf ⇒ (a). The relation ⇒ has the weak diamond property if every pair of different direct successors of some element of A possesses a common direct successor. It is called confluent if every pair of successors of some element of A possesses a common successor, and it is called terminating if there is no infinite chain of the form a0 ⇒ a1 ⇒ . . . .  It is well known that in a confluent reduction system the irreducible successors of an element are unique if they exist. In particular the weak diamond property, which clearly implies confluence, has the consequence that every element either has a normal form or an outgoing infinite reduction. Moreover in a convergent (i.e., confluent and terminating) reduction system every element has a normal form.

2.2

Partial recursive tree functions

Now we are going to define the class µREC of partial recursive tree functions. The presentation is inspired by the definition of “partiell–rekursive Baumfunktionen” of Hupbach in [Hup78]. The latter is universal for the class of initial algebras (called “Peano–Algebren” in [Hup78]); i.e., every computable function over trees is a partial recursive tree function (see also [KU85]). For the sake of simplicity, we only consider homogeneous algebras, and we do not take tuple– valued functions into account. We start with some basic notions related to trees. In the following let X = {x1 , x2 , x3 , . . . } be a set of variables. Definition 2.2 A ranked alphabet Σ is a finite set of symbols in which every symbol is associated with a natural number, called its rank. For every n ≥ 0, Σ(n) is the set of symbols of rank n. Let H be an arbitrary set, called index set. The set of Σ–labeled trees indexed by H, denoted by TΣ (H) (or TΣ , if H is empty), is the least set T such that • H ⊆ T and • for every n ≥ 0, σ ∈ Σ(n) , and t1 , . . . , tn ∈ T , σ(t1 , . . . , tn ) ∈ T . (We usually omit the parentheses if n = 0.) The substitution of every occurrence of a variable x ∈ X in a tree t ∈ TΣ (X) by s ∈ TΣ (X) is denoted by t[x/s]. For every t ∈ TΣ (X), path(t) ⊆ {1, 2, . . . }∗ is the set of all paths of t, each uniquely identifying a node of t. In particular, ε denotes the root of t. Every path w ∈ path(t) refers to a corresponding label and a subtree of t, denoted by labelt (w) and t|w , respectively, where t|ε = t. The tree t[w ← s] ∈ TΣ (X) is obtained from t by replacing the subtree at w ∈ path(t) by the tree s ∈ TΣ (X).  Definition 2.3 Let r ≥ 0, let [Σ] S = (Σ1 , . . . , Σr ) be a sequence of ranked alphabets, and let Ω be a ranked alphabet such that ri=1 Σi ⊆ Ω. Let T[Σ] denote the set TΣ1 × · · · × TΣr .

(

TΩ . If it is total, then it • A partial tree function f from T[Σ] to TΩ is denoted by T[Σ] is denoted by T[Σ] → TΩ . Usually we drop the word “partial”, and we indicate the sort of tree function (i.e., partial or total) by the appropriate sort of arrow (i.e., by or →, respectively).

(

• Let ω ∈ Ω(r) . The tree function topω : T[Σ] → TΩ , given by topω (s1 , . . . , sr ) = ω(s1 , . . . , sr ) for every (s1 , . . . , sr ) ∈ T[Σ] , is called a top concatenation. 3

(r)

• Let i ∈ [r], where [r] = {1, . . . , r}. The i–th projection is the tree function proji TΩ , given by

(r) proji (s1 , . . . , sr )

: T[Σ] →

= si for every (s1 , . . . , sr ) ∈ T[Σ] .

• Let Sk k ≥ 1, and let [∆] = (∆1 , . . . , ∆k ) be another sequence of ranked alphabets with T∆i be a tree function, and let f : i=1 ∆i ⊆ Ω. For every i ∈ [k], let gi : T[Σ] T[∆] TΩ be a tree function. The composition of g1 , . . . , gk with f is the tree function TΩ , given by comp(g1 , . . . , gk ; f ) : T[Σ]

(

(

(

comp(g1 , . . . , gk ; f )(~s) = f (g1 (~s), . . . , gk (~s)) for every ~s = (s1 , . . . , sr ) ∈ T[Σ] . (k)

• Let Σ0 ⊆ Ω be another ranked alphabet, and let k ≥ 0. For every σ ∈ Σ0 , let gσ : TΩ be a tree function, and let G = {gσ | σ ∈ Σ0 }. The primitive T[Σ] × TΣk0 × TΩk recursion determined by G, denoted by prec(G), is the tree function f : TΣ0 × T[Σ] TΩ such that

(

(

f (σ(s1 , . . . , sk ), ~t) = gσ (~t, s1 , . . . , sk , f (s1 , ~t), . . . , f (sk , ~t)). (k)

for every σ ∈ Σ0 , s1 , . . . , sk ∈ TΣ0 , and ~t = (t1 , . . . , tr ) ∈ T[Σ] .

(

(

• Let g : TΣ0 TΣ0 and h : TΣ0 TΩ be tree functions, and let t ∈ TΣ0 . The iteration of TΣ0 such that, for every s ∈ TΣ0 , g, h, and t is the tree function iter(g, h, t) : TΣ0  n if n ≥ 0, h(gn (s)) = t, and, for every m < n, g (s) iter(g, h, t)(s) = h(gm (s)) is defined and different from t  undefined otherwise.

(

• The class µREC of (partial) recursive tree functions is the least class of tree functions which contains the top concatenations and projections and which is closed under composition,  primitive recursion, and iteration.

A well–known subclass of µREC is the class PREC of primitive recursive tree functions which is obtained by omitting iteration in the above definition. It is easy to see that it consists of total tree functions only. The iteration operator can alternatively be defined as follows. The tree iter(g, h, t)(s) is computed by the WHILE program while h(s) 6= t do s := g(s). Note that the class of partial recursive functions over natural numbers can be obtained as a special instance of µREC by encoding numbers as successor terms, choosing Nat = {zero (0) , suc (1) } for every ranked alphabet. In particular, the minimization can be defined by instantiating h and t in the scheme of iteration by the identity function and the tree zero, respectively. Further note that the above definition enables us to represent every recursive tree function as an operator term. For example, the binary addition operation on natural numbers, given by the equations plus(zero, t) = t plus(suc(s), t) = suc(plus(s, t)) is represented by the operator term plus = prec({gzero , gsuc }) 4

(1)

(3)

where gzero = proj1 and gsuc = comp(proj3 ; topsuc ). Thus we obtain the notion of subfunction in a natural way by considering subterms. This approach will be exploited in Section 4 where we show that every recursive tree function f is computable by a HO–ATT whose construction is guided by the operator term associated with f .

3

Higher–order attributed tree transducers

HO–ATTs serve as a formal computation model of HO–AGs ([VSK89]). They are derived from ATTs which were investigated in [F¨ ul81] to study the computational behavior of attribute grammars without higher–order features. We present this concept first and generalize it later to the higher–order case.

3.1

The basic concept: attributed tree transducers

First we define the set of trees which can occur on the right–hand sides of attribute rules in both ATTs and HO–ATTs. Here we obey the usual convention that the dataflow of synthesized (inherited) attributes is oriented upwards (downwards) the tree. This is guaranteed by letting the synthesized attributes of a node and the inherited attributes of its direct successors depend on its inherited attributes and on the synthesized attributes of its direct successors. Definition 3.1 Let ∆ be a ranked alphabet, let Syn and Inh be arbitrary sets, and let k ≥ 0. The set RHS (∆, Syn, Inh, k) of right–hand sides over ∆, Syn, Inh, and k is given by RHS (∆, Syn, Inh, k) = T∆ (Syn × [k] ∪ Inh × {ε}).



Since HO–ATTs will compute tree functions with an arbitrary finite number of arguments (and not only one argument), we also introduce ATTs with this feature. The symbol δ0 will serve as a gluing symbol for the arguments (cf. Definition 3.7). Definition 3.2 An attributed tree transducer (ATT) is a tuple M = (Syn, Inh, a0 , [Σ], ∆, δ0 , R) where • Syn is a finite set of symbols, called synthesized attributes, • Inh is a finite set of symbols disjoint from Syn, called inherited attributes, • a0 ∈ Syn is the initial attribute, • [Σ] = (Σ1 , . . . , Σr ) is the sequence of ranked alphabets of input symbols where r ≥ 0, S • ∆ is the ranked alphabet of working symbols where ri=1 Σi ⊆ ∆, • δ0 ∈ ∆(r) is the initial symbol, and

S • R = {Rσ | σ ∈ Σ} is a family of sets with index set Σ = ri=1 Σi ∪ {δ0 }, where every Rσ is a finite set of rules satisfying the following conditions. For every σ ∈ Σ(k) with k ≥ 0, – for every a ∈ Syn, there is exactly one rule in Rσ of the form ha, εi → η, and – for every b ∈ Inh and j ∈ [k], there is exactly one rule in Rσ of the form hb, ji → η where, in both cases, η ∈ RHS (∆, Syn, Inh, k) if σ 6= δ0 (respectively, η ∈ RHS (∆, Syn, ∅, k) if σ = δ0 ). (Since in case k = 0 we have [k] = ∅, for a nullary σ the second item does not contribute to Rσ .) As an abbreviation, we let Att = Syn ∪ Inh.

 5

Example 3.3 We consider an ATT which computes the sum of two natural numbers. In order to illustrate the way in which an ATT is derived from an attribute grammar, we first present a corresponding attribute grammar. Productions attribute rules S → N 1 + N2 ini(N1 ) = 0 ini(N2 ) = sum(N1 ) sum(S) = sum(N2 ) N0 → 1N1 ini(N1 ) = ini(N0 ) sum(N0 ) = sum(N1 ) + 1 N →ε sum(N ) = ini(N ) Here, the natural number n ≥ 0 is represented by the string 1n . In the attribution, the sum of the two numbers derived from the start symbol S of the underlying context–free grammar is computed by incrementing the synthesized attribute sum on encountering a 1 symbol, and by assigning appropriate initial values to the inherited ini attributes of both subtrees. As explained in Section 1, an ATT is derived from an attribute grammar by removing the distinction between derivation trees and attribute values: both are represented as terms over a general ranked alphabet ∆. In particular, the natural number n ≥ 0 is represented by the term suc n (zero). For the above attribute grammar, we obtain M = (Syn, Inh, a0 , [Σ], ∆, δ0 , R) where • Syn = {sum}, • Inh = {ini}, • a0 = sum, • [Σ] = (Σ1 , Σ2 ) with Σ1 = Σ2 = Nat = {zero (0) , suc (1) }, (2)

• ∆ = Σ1 ∪ {δ0 }, • R = {Rσ | σ ∈ {δ0 , suc, zero}} with Rδ0 = {hini , 1i → zero,

(1)

hini, 2i → hsum, 1i,

(2)

hsum, εi → hsum, 2i},

(3)

Rsuc = {hini , 1i → hini, εi hsum, εi → suc(hsum, 1i)}, Rzero = {hsum, εi → hini, εi}.

(4) (5) (6) 

Now we give the semantics of ATTs by associating a reduction system with M where M denotes an ATT as in Definition 3.2. The definitions are illustrated by Example 3.8 below. Definition 3.4 The set of computation trees of M , denoted by Comp M , is the least set Comp which satisfies the following conditions: • For every δ ∈ ∆(k) with k ≥ 0 and ϕ1 , . . . , ϕk ∈ Comp , δ(ϕ1 , . . . , ϕk ) ∈ Comp . • For every c ∈ Att, t ∈ TΣ , and w ∈ path(t), hc, wi(t) ∈ Comp . The reduction relation of M , denoted by ⇒M , is the binary relation on Comp M defined as follows. For every ϕ, ψ ∈ Comp M , ϕ ⇒M ψ holds iff ϕ|v = hc, wi(t) for some path v ∈ path(ϕ), some attribute c ∈ Att, some tree t ∈ TΣ , and some path w ∈ path(t), and one of the following conditions holds. 6

(i) A synthesized attribute is evaluated, i.e., c ∈ Syn, labelt (w) = σ for some σ ∈ Σ(k) with k ≥ 0, there is a rule hc, εi → η in Rσ , and ψ = ϕ[v ← η[hd, ji/hd, w.ji(t); d ∈ Att, j ∈ [k]ε ]] where [k]ε denotes the set [k] ∪ {ε}. (ii) An inherited attribute is evaluated, i.e., c ∈ Inh, w = w′ .i for some i ≥ 1, labelt (w′ ) = σ for some σ ∈ Σ(k) with k ≥ i, there is a rule hc, ii → η in Rσ , and ψ = ϕ[v ← η[hd, ji/hd, w′ .ji(t); d ∈ Att, j ∈ [k]ε ]].  The notion of circularity can be defined in analogy to the attribute grammar case. We recall Definition 5.10 of [FV98]. Definition 3.5 An ATT M is called circular if there exist computation trees hc, wi(t), ϕ ∈ Comp M and some path v ∈ path(ϕ) such that hc, wi(t) ⇒+ M ϕ[v ← hc, wi(t)]. Otherwise it is called noncircular.



From now on we will only consider noncircular ATTs. The resulting properties of the reduction relation are recalled from [FV98]. Lemma 3.6

(i) (Lemma 5.24) ⇒M is terminating.

(ii) (Lemma 5.25; holds for arbitrary ATTs) ⇒M is confluent. (iii) (Lemma 5.26) Every ϕ ∈ Comp M possesses a normal form nf ⇒M (ϕ). (iv) (Lemma 5.32) If s1 , . . . , sr ∈ T[Σ] , then nf ⇒M (ha0 , εi(δ0 (s1 , . . . , sr ))) ∈ T∆ .



Actually the proof of (ii) in [FV98] exhibits that ⇒M possesses the weak diamond property, which will also be shown later for the higher–order case. Part (iv) is a straightforward consequence of [FV98, Lemma 5.32], exploiting the fact that there are no inherited attribute occurrences in the right–hand sides of δ0 –rules (cf. Definition 3.2). Definition 3.7 The tree function computed by M is the total function τM : T[Σ] → T∆ such that, for every (s1 , . . . , sr ) ∈ T[Σ] , τM (s1 , . . . , sr ) = nf ⇒M (ha0 , εi(δ0 (s1 , . . . , sr ))). The class of tree functions computed by ATTs is denoted by ATT .



Example 3.8 Let M be the ATT of Example 3.3, computing the sum of two natural numbers. The following reduction shows that τM (suc(zero), suc(zero)) = suc(suc(zero)), where we let t = δ0 (suc(zero), suc(zero)) and where the superscripts of the reduction relation

7

refer to the respective attribute rules: (3)

hsum, εi(t) ⇒M

hsum, 2i(t)

(5) ⇒M

suc(hsum, 2.1i(t))

(6) ⇒M

suc(hini , 2.1i(t))

(4)

⇒M (2)

suc(hini , 2i(t))

⇒M

suc(hsum, 1i(t))

(5) ⇒M

suc(suc(hsum, 1.1i(t)))

(6) ⇒M

suc(suc(hini , 1.1i(t)))

(4)

⇒M (1)

⇒M

suc(suc(hini , 1i(t))) suc(suc(zero)).



The following lemma gives an important property of ATT–computable functions, which will be used in Section 4 to compare the expressive power of ATTs and HO–ATTs. It states that the height of a tree computed by an ATT (i.e., the length of its maximal paths) is linearly bounded by the size (i.e., the overall number of nodes) of the argument trees, and is a simple application of the corresponding result for the unary case (cf. [F¨ ul81, Eng82]). Lemma 3.9 Let M be an ATT and τM : T[Σ] → T∆ . There is a constant cM > 0 such that for every sequence of input trees (s1 , . . . , sr ) ∈ T[Σ] of M , height(τM (s1 , . . . , sr )) ≤ cM · size(s0 ) where s0 = δ0 (s1 , . . . , sr ). 

3.2

Extending ATTs to HO–ATTs

Just as HO–AGs are an extension of attribute grammars, we now generalize the concept of ATTs to the higher–order case. To do so, we formalize the effect that the input tree can be enlarged by expanding a nonterminal attribute (which will from now on be called nonterminal symbol, because it is a particular output symbol). More exactly, if during the computation a synthesized attribute a of a nonterminal symbol has to be evaluated, then the tree which is stored in the designated inherited attribute tree has to be used to compute the value of a. Definition 3.10 A higher–order attributed tree transducer (HO–ATT) is a tuple M = (Syn, Inh, a0 , tree, [Σ], ∆, δ0 , α, R) where • Syn, Inh, Att, a0 , [Σ], and ∆ are given in the same way as in Definition 3.2, • tree ∈ Inh is a distinguished inherited attribute, • δ0 ∈ ∆(r+1) is the initial symbol, • α ∈ ∆(0) is the nonterminal symbol, and • R = {Rδ | δ ∈ ∆ \ {α}} where every Rδ is a finite set of rules satisfying the following conditions. For every δ ∈ ∆(k) \ {α} with k ≥ 0, – for every a ∈ Syn, there is exactly one rule in Rδ of the form ha, εi → η, and 8

– for every b ∈ Inh and j ∈ [k], there is exactly one rule in Rδ of the form hb, ji → η where, in both cases, η ∈ RHS (∆, Syn, Inh, k) if σ 6= δ0 (respectively, η ∈ RHS (∆, Syn, ∅, k)  if σ = δ0 ). Let M denote a HO–ATT as in Definition 3.10. Its semantics is given as follows. Just as in the first–order case (cf. Definition 3.4), we associate a set of computation terms and a corresponding reduction system with M . Here we have to take into account that, due to tree expansions, attribute occurrences can be nested within a computation term. Definition 3.11 The set of computation trees of M , denoted by Comp M , is the least set Comp which satisfies the following conditions: • For every δ ∈ ∆(k) with k ≥ 0 and ϕ1 , . . . , ϕk ∈ Comp , δ(ϕ1 , . . . , ϕk ) ∈ Comp . • For every c ∈ Att, ϕ ∈ Comp , and w ∈ path(ϕ) such that, for every proper prefix v of w, labelϕ (v) ∈ ∆, hc, wi(ϕ) ∈ Comp . The reduction relation of M , denoted by ⇒M , is the binary relation on Comp M defined as follows. For every ϕ, ψ ∈ Comp M , ϕ ⇒M ψ holds iff ϕ|v = hc, wi(t) for some path v ∈ path(ϕ), some attribute c ∈ Att, some tree t ∈ T∆ , and some path w ∈ path(t) and one of the following conditions holds. (i) A synthesized attribute is evaluated (cf. Figure 1), i.e., c ∈ Syn, labelt (w) = δ for some δ ∈ ∆(k) \ {α} with k ≥ 0, there is a rule hc, εi → η in Rδ , and ψ = ϕ[v ← η[hd, ji/hd, w.ji(t); d ∈ Att, j ∈ [k]ε ]]. (ii) An inherited attribute is evaluated (cf. Figure 2), i.e., c ∈ Inh, w = w′ .i for some i ≥ 1, labelt (w′ ) = δ for some δ ∈ ∆(k) with k ≥ i, there is a rule hc, ii → η in Rδ , and ψ = ϕ[v ← η[hd, ji/hd, w′ .ji(t); d ∈ Att, j ∈ [k]ε ]]. (iii) The input tree is enlarged (cf. Figure 3), i.e., c ∈ Syn, labelt (w) = α, and ψ = ϕ[v ← hc, wi(t[w ← htree, wi(t)])]. d

We write ⇒M to indicate that case d of the above definition applies, where d ∈ {(i), (ii), (iii)}. Furthermore, ⇒∗,≤l M denotes a computation in which (iii) is applied at most l times, where l ≥ 0. (i)

(ii)

∗ (Hence we have ⇒∗,≤0 M = (⇒M ∪ ⇒M ) and, for every l ≥ 0, (iii)

∗,≤l+1 ∗,≤l ∗,≤0 ⇒M = ⇒∗,≤l .) M ∪ ⇒M ◦ ⇒ M ◦ ⇒ M

Furthermore, for every l ≥ 0 and ϕ ∈ Comp M , we let λ≤l M (ϕ) denote the least upper bound of the number of ⇒M –steps in any ⇒∗,≤l –computation starting in ϕ.  M The following shows that the ATT properties stated in Lemma 3.6 part (ii) and (iv) are shared by HO–ATTs. Remember that the weak diamond property of the reduction relation implies its confluence. Lemma 3.12

(i) ⇒M has the weak diamond property.

(ii) If t ∈ T∆ and ψ ∈ Comp M such that ψ is an irreducible successor of ha0 , εi(t) with respect  to ⇒M , then ψ ∈ T∆ .

9

v hc, wi ϕ1 w

v Rδ : hc, εi → η ⇒M

ϕ2 η[hd, ji/ hd, w.ji(t)]

t δ

Figure 1: Evaluation of a synthesized attribute v

v hc, w′ .ii ϕ1 w′ t

Rδ : hc, ii → η ⇒M

ϕ2 η[hd, ji/ hd, w′ .ji(t)]

δ

Figure 2: Evaluation of an inherited attribute Proof (i) Let ϕ, ξ1 , ξ2 ∈ Comp M such that ϕ ⇒M ξ1 and ϕ ⇒M ξ2 where ξ1 6= ξ2 . According to Definition 3.11, every reducible subterm is of the form hc, wi(t) where c ∈ Att, w ∈ path(t), and, in particular, t ∈ T∆ . Hence no nested applications of rules are possible, and so the two reduction steps occur at independent positions in ϕ. Thus, by a case analysis on the type of attribute being evaluated, it is possible to show that the two involved rules can again be applied to ξ2 and ξ1 , respectively, yielding ψ ∈ Comp M with ξ1 ⇒M ψ and ξ2 ⇒M ψ (similar to the proof of Lemma 3.9 in [FV98]). (ii) in analogy to the first–order case (Lemma 3.6), based on the fact that every attribute occurrence in a computation tree has an associated rule and that no references to inherited  attributes at the root are allowed. Since the weak diamond property implies confluence we can conclude as follows. Corollary 3.13 If ψ1 , ψ2 ∈ Comp M are irreducible successors of ϕ ∈ Comp M with respect to ⇒M , then ψ1 = ψ2 . 

(

T∆ Definition 3.14 The tree function computed by M is the partial tree function τM : T[Σ] such that, for every (s1 , . . . , sr ) ∈ T[Σ] , τM (s1 , . . . , sr ) = u iff there is a normal form u ∈ T∆ of ha0 , εi(δ0 (s1 , . . . , sr , α)) with respect to ⇒M . The class of tree functions computed by HO–ATTs is denoted by HOATT .  Note that Lemma 3.12 implies that the function which is computed by a HO–ATT is partial recursive. Since the reduction relation has the weak diamond property, every computation term either has a normal form or an outgoing infinite reduction. Thus the value of τM can be computed by applying computation rules as far as possible. It remains the question under which conditions the existence of normal forms can be guaranteed. As in the first–order case, one possible source of non–terminating computations is the 10

v

v

hc, wi ϕ1 w

⇒M

hc, wi ϕ2 w

t htree, wi

α

t Figure 3: Tree expansion cyclic behavior of the reduction relation. The corresponding notion of (non-)circularity can be carried over to the higher–order case by associating with every HO–ATT an ATT as follows. Definition 3.15 Let M = (Syn, Inh, a0 , tree, (Σ1 , . . . , Σr ), ∆, δ0 , α, R) be a HO–ATT. Its underlying ATT, att(M ) = (Syn, Inh, a0 , ∆r+1 , ∆′ , δ0 , R′ ), is given by ∆′ = ∆ ∪ {⊥(0) } ′ R′ = R ∪ Rα′ ∪ R⊥

Rα′ = {ha, εi → ⊥ | a ∈ Syn} ′ R⊥ = {ha, εi → ⊥ | a ∈ Syn}

where ⊥ is a new symbol. The HO–ATT M is called (non-)circular if att(M ) is.



We remark that a corresponding formal definition of circularity for HO–AGs is missing in [VSK89]. It is obvious that, in contrast to the first–order case, noncircularity is not sufficient to ensure the termination of the reduction relation because repeated extensions of the input tree can lead to an infinite computation (in which case the corresponding HO–AG does not fulfil the well–definedness property of [VSK89]). We will now justify this observation by showing that, in the case of a noncircular HO–ATT, the restriction of tree expansions will exclude infinite computations. From now on let M denote a noncircular HO–ATT. The following lemma is really equivalent with the fact that there are no infinite computations with with finitely many tree expansions, by K¨onig’s Lemma. Lemma 3.16 For every l ≥ 0 and every ϕ ∈ Comp M , λ≤l M (ϕ) is finite.



Proof We employ induction on l: l = 0: Every ⇒∗,≤0 M –computation starting in some computation term ϕ ∈ Comp M can be simulated by a composition of computation sequences of att(M ). This can be seen as follows. In every step of the computation, M evaluates some attribute occurrence hc, wi(t), using rules of type (i) or (ii) in Definition 3.11. In the first case, the reduction step is of the 11

form ϕ[v ← hc, wi(t)] ⇒M ϕ[v ← η[hd, ji/hd, w.ji(t); d ∈ Att, j ∈ [k]ε ]] where hc, εi → η in Rlabelt (w) . Hence, according to Definitions 3.15 and 3.4, att(M ) can perform the corresponding reduction hc, wi(t) ⇒att(M ) η[hd, ji/hd, w.ji(t); d ∈ Att, j ∈ [k]ε ]. A similar argument holds for inherited attribute occurrences. The HO–ATT M proceeds until a tree in T∆ or a synthesized attribute occurrence of a nonterminal symbol is reached. In the first case, M continues with some “outer” attribute occurrence, which can again be simulated by att(M ). In the second case, the computation of M in this part of the computation tree stops since rules of type (iii) are not admitted. (The rules of the form ha, εi → ⊥ ∈ Rα′ are just added to complete the definition of att(M ); they do not play any role here.) Since M is assumed to be noncircular, att(M ) is, by Definition 3.15, noncircular as well. Hence, by Lemma 3.6, ⇒att(M ) is terminating. This implies that every ⇒∗,≤0 M –computation terminates as well. l ⇒ l + 1: The claim follows by applying the induction hypothesis to the decomposition (iii)

∗,≤l+1 ∗,≤l ∗,≤0 ⇒M = ⇒∗,≤l . M ∪ ⇒M ◦ ⇒ M ◦ ⇒ M



According to the previous lemma, the actual reason for the existence of non–terminating computations in noncircular HO–ATTs is the fact that infinitely many tree expansions can occur. Since the corresponding computation rule can only be applied at the leaves of the current computation tree, that is, at “innermost” positions, it might be worthwhile to consider an alternative semantics, choosing an “outside–in” strategy for selecting the reduction position. This could be achieved by changing Definition 3.11 such that attribute occurrences of the form hc, wi(ϕ) become reduction candidates where ϕ is an arbitrary computation term, i.e., may itself contain attribute occurrences. It is clear that this “demand–driven” generalization would yield results for some examples where the original, “data–driven” reduction relation does not terminate. A comparable situation occurs in functional programming languages where it is well known that the “call–by–name” strategy for evaluating function calls is, in the above sense, more powerful than “call–by–value”. Another analogy is the use of the Y operator in untyped λ–calculi for the simulation of recursion. However we have chosen the “inside–out” semantics for the following reasons: • In the original paper [VSK89] on HO–AGs, the value of a nonterminal attribute has to be computed before it can be added to the parse tree. Thus an “outside–in” strategy, which admits attribute evaluations in partially evaluated computation trees, is inconsistent with the original semantics. • The generalized semantics complicates large parts of the theory. For example, nested attribute occurrences have to be considered in the confluence proof (cf. Lemma 3.12). Moreover, the above–mentioned question of semi–computability becomes open again since it might be the case that a computation tree has both a normal form and an infinite outgoing computation. (Indeed, such examples can be constructed.) • Additional steps have to be taken in order to ensure the strictness of the function implementation which is described in Section 4. To conclude this section, we consider a HO–ATT which computes the sum of two natural numbers. According to the operator term representation (1)

(3)

plus = prec({proj1 , comp(proj3 ; topsuc )})

12

(cf. Definition 2.3), the addition function is given as follows: plus(zero, t) = t plus(suc(s), t) = suc(plus(s, t))

(7) (8)

We remark that the HO–ATT presented here is a simplified version of the one which is obtained from the general formal construction in Section 4. Example 3.17 Mplus = (Syn, Inh, a0 , tree, [Σ], ∆, δ0 , α, R) is given as follows: • Syn = {a0 , a1 , aΩ }, • Inh = {tree, b1 , b2 , bzero , bsuc }, • [Σ] = (Σ1 , Σ2 ) with Σ1 = Σ2 = Nat, and (3)

• ∆ = {δ0 , zero (0) , suc (1) , add (2) , snd (0) , α(0) }. The working principle of Mplus is based on the following invariant. Every ∆–node of the current computation tree computes a certain function which maps its inherited attribute values to the value of its designated initial attribute a0 . For example, the evaluation of the computation tree ha0 , εi(add (., .)) yields the sum plus(s, t) provided that hb1 , εi(add (., .)) and hb2 , εi(add (., .)) have the value s and t, respectively. Since plus is defined by means of primitive recursion, the subtrees of the add node, which control the computation of the sum, cannot statically be fixed. Instead, they are initialized with the nonterminal symbol and get expanded according to the current recursion argument s. The details of the evaluation are explained describing the rules in R. Furthermore Figure 4 shows some of the intermediate steps of the computation of τMplus (suc(zero), suc(zero)); the numbers and letters enclosed in parentheses refer to the corresponding attribute rules and subfigures, respectively. • Rδ0 : (a) shows the initial computation tree of Mplus according to Definition 3.14. Here, s1 = s2 = suc(zero) ∈ TNat are (tree encodings of) the two numbers to be added. First, the nonterminal symbol α is instantiated with an add call (9) which computes the sum of s1 (10) and s2 (11) and passes the result to the root afterwards (12). htree, 3i → add (α, α)

(9)

hb1 , 3i → ha0 , 1i

(10)

hb2 , 3i → ha0 , 2i

(11)

ha0 , εi → ha0 , 3i

(12)

Note that the computation of Mplus proceeds in a demand–driven way, that is, the evaluation order of the “bottom–up” description above is reversed. Thus we start with Rule (12), resulting in the computation tree (b) where ha0 , 3i refers to a nonterminal symbol which has to be expanded (c, d). • Radd : As explained above, in the initial attribute a0 of the add node the sum of the values of its inherited attributes b1 and b2 has to be synthesized. This is done by “unfolding” the recursion using the second successor node, which is labeled by a nonterminal symbol. The appropriate expansion tree is selected according to the recursion argument stored in b1 : if the value is zero, then according to Equation (7) above the result is the second argument. Otherwise, it is obtained by incrementing the sum of the second argument and of the predecessor of the first argument (8). The corresponding operator terms are respectively stored in the inherited attributes bzero (14) and bsuc (15). They are selected by the first 13

(12)

ha0 , εi δ0 suc suc α zero zero



(a)

(Def. 3.11(iii))



ha0 , 3i δ0 suc suc α zero zero

(b)

(9)

(19),(16)





ha0 , 3i δ0 suc suc htree, 3i zero zero δ0 suc suc α zero zero (c)

ha0 , 3i δ0 suc suc add zero zero α α

(d)

(13),(10)



ha0 , 3.2i δ0 suc suc add zero zero α haΩ , 3.1i δ0 suc suc add zero zero α α

(e) (22),(25),(24),(15),(22)



(19),(16),(13),(20),(17),(13),(10),(22),(25)

suc ha0 , 3.2.1i δ0 suc suc add zero zero α suc add α α



(g) (23),(25),(26),(14)



ha0 , 3.2i δ0 suc suc add zero zero α haΩ , 3.1i δ0 suc suc add zero zero ha0 , 1i α δ0 suc suc add zero zero α α (f) suc ha0 , 3.2.1.2i δ0 suc suc add zero zero α suc add α haΩ , 3.2.1.1i δ0 suc suc add zero zero α suc add ha1 , 3.1i α δ0 suc suc add zero zero suc add zero α α (h)

(27),(18),(21),(18),(11),(22),(25)



suc ha0 , 3.2.1.2i δ0 suc suc add zero zero α suc add α snd (i)

suc suc zero

(j)

Figure 4: Computation protocol of Mplus

14

argument itself (see (13), Rsuc (24), and Rzero (26)) and synthesized in the aΩ attribute, whose value is used for the tree expansion (16). For the recursive call of plus, the recursion argument is decremented (17), the recursion parameter is taken without change (18), and the result is returned to the original add node afterwards (19). htree, 1i → hb1 , εi

(13)

hbzero , 1i → snd

(14)

hbsuc , 1i → suc(add (α, α))

(15)

htree, 2i → haΩ , 1i

(16)

hb1 , 2i → ha1 , 1i

(17)

hb2 , 2i → hb2 , εi

(18)

ha0 , εi → ha0 , 2i

(19)

In the concrete computation, both tree expansions are successively performed (e, f). • Rsuc : Here, the inherited arguments are passed downwards (20, 21). Furthermore, the value of the whole tree and of its direct subtree is synthesized in a0 (22) and a1 (23), respectively, and according to the suc label the corresponding inherited attribute value is stored in the synthesized attribute aΩ (24). hb1 , 1i → hb1 , εi

(20)

hb2 , 1i → hb2 , εi

(21)

ha0 , εi → suc(ha0 , 1i)

(22)

ha1 , εi → ha0 , 1i

(23)

haΩ , εi → hbsuc , εi

(24)

In Figure 4 this means that because of the non–zero value of the recursion argument a recursive computation has to be initialized (g). • Rzero : The rules are similar to those of Rsuc , where no successor node has to be considered. ha0 , εi → zero

(25)

haΩ , εi → hbzero , εi

(26)

They are used in the inner evaluation of plus where the first argument is zero (h, i). • Rsnd : This symbol is used to select the second of the two input arguments (27), which is the result of the addition in case of a zero recursion argument (7, 14). ha0 , εi → hb2 , εi

(27)

This rule is applied first in the subsequent reductions in which the term suc(zero) is computed as the value of the inner sum, leading to the expected overall result suc(suc(zero)) (j). Furthermore we add some dummy rule for every attribute occurrence which is not mentioned in the above construction. 

4

The universality of higher–order attributed tree transducers

Theorem 4.1 HOATT = µREC . 15

Proof The claim HOATT ⊆ µREC follows trivially from Church’s Thesis. In order to prove TΩ in µREC where the reverse inclusion, given a partial recursive tree function f : T[Σ] [Σ] = (Σ1 , . . . , Σr ), we construct a HO–ATT Mf such that τMf = f . Let

(

Mf = (Syn, Inh, a0 , tree, [Σ], ∆, δ0 , α, R) be defined by • Syn = {a0 , a1 , . . . , an , aΩ } where n = max{k ≥ 0 | Ω(k) 6= ∅}, • Inh = {tree} ∪ {bω | ω ∈ Ω ∪ {⊥}} ∪ {b1 , . . . , bm } with m being the maximal arity of any subfunction of f , and • ∆= Ω∪ (r+1) (1) {δ0 , α(0) , ⊥(0) , δ (2)

(1)

proj1

}∪

{δcomp(g;iter(g,h,t)) | iter(g, h, t) is a subfunction of f } ∪ (k+1)

{δ=t′

| iter(g, h, t) is a subfunction of f, t′ = ω(t′1 , . . . , t′k ) is a subtree of t} ∪

(r ′ ) {δg′ gω | prec({gω | ω ∈ Ω}) is a ω (r ) {δg g | g is a subfunction of f }

subfunction of f, ω ∈ Ω} ∪

where  0     k rg = k + 1    2   3

if if if if if

g g g g g

= topω (k) = proji = comp(h1 , . . . , hk ; h0 ) = prec({hω | ω ∈ Ω}) = iter(h1 , h2 , t),

and where gω′ is given by Equation (53). The rules in R are specified in the subsequent construction which is based on the following principles: • For the given tree function f , we will inductively construct a computation tree θf ∈ T∆ , called the control tree of f , which reflects the structure of f according to Definition 2.3 (and according to the discussion after this definition). The tree θf controls the computation of f in the following sense: if, for every i ∈ [r], the inherited attribute bi at the root of θf evaluates to the argument tree si , then the initial attribute a0 at the root of θf evaluates to f (s1 , . . . , sr ). Thus, in a certain sense, the control tree reflects the control flow of f whereas the semantic rules describe the data flow. • The structure of θf is given as follows: top concatenations correspond to leaves. The tree for a projection contains the arguments as subtrees, and the tree associated with a composition is built up from the trees of the constituent functions. Both primitive recursion and iteration are first “approximated” by trees of height one with nonterminal symbols at the leaves which get expanded when the recursion is “unfolded” later in the computation (cf. the use of add in Example 3.17). According to Definition 3.14, Mf starts with the computation tree ha0 , εi(δ0 (s1 , . . . , sr , α)) where (s1 , . . . , sr ) ∈ T[Σ] are the input arguments of f . This situation is depicted in Figure 5 where we use the “classical” representation of attribute occurrences as boxes in order to illustrate the flow of information. Now, the set Rδ0 contains the rules htree, r + 1i → θf

(28)

hbi , r + 1i → ha0 , ii ha0 , εi → ha0 , r + 1i 16

(i ∈ [r])

(29) (30)

which insert the control tree of f (to be defined inductively later) at the nonterminal symbol position (28), transfer the argument values to the inherited attributes at the root of θf (29), and yield the final result after the application of f (30).

δ0 a0 θf a0

...

a0

tree b1 . . . br

α

a0

sr

s1

Figure 5: Initial computation tree The argument values (see Figure 6) are computed by associating with every symbol ω ∈ Ω(k) a rule (31) which synthesizes the tree with root symbol ω in the attribute a0 by concatenating the subtrees. The latter are also stored (32) in respective attributes a1 , . . . , ak . In addition, the value of the inherited attribute bω is assigned to the synthesized attribute aΩ (33). Thus some kind of case operation is implemented which will be used later when handling recursion. Formally, Rω contains the rules ha0 , εi → ω(ha0 , 1i, . . . , ha0 , ki)

(31)

hai , εi → ha0 , ii

(i ∈ [k])

haΩ , εi → hbω , εi bω

ω

a0

(32) (33)

a0 a1 . . . ak aΩ

...

s1

a0 sk

Figure 6: Basic symbol As explained above, the control tree θf ∈ T∆ is inductively defined on the structure of f according to Definition 2.3: • f = topω where ω ∈ Ω(r) (Figure 7): here, we just have to compose the inherited argument values by means of an ω operation. Therefore, let θf = δf ,

(34)

and include the rule ha0 , εi → ω(hb1 , εi, . . . , hbr , εi)

(35)

in Rδf . (r)

• f = proji for some i ∈ [r] (Figure 8): since the function result is determined by the ith argument, it would be sufficient to choose Rδf = {ha0 , εi → hbi , εi} in principle. 17

b1 . . . br

δf

a0

Figure 7: Top concatenation However, this simple implementation would contradict the strictness requirement: due to a non–terminating iteration, the value of one of the remaining arguments might be undefined. We therefore ensure the strictness of the projection function by exploiting the bω /aΩ mechanism (39, 40) of the basic symbols, passing the function result (38) in a depth–first left–to–right traversal through the argument trees (37). This is accomplished as follows. Let θf = δf (α, . . . , α), | {z }

(36)

r times

and let Rδf contain the rules

htree, ji → hbj , εi

(j ∈ [r])

(37)

hbω , 1i → hbi , εi

(ω ∈ Ω)

(38)

(j ∈ [r − 1], ω ∈ Ω)

(39)

hbω , j + 1i → haΩ , ji ha0 , εi → haΩ , ri

(40)

δf

b1 . . . bi . . . br

tree bω1 . . . bωl

α aΩ

...

a0

tree bω1 . . . bωl

α aΩ

Figure 8: Projection, assuming that Ω = {ω1 , . . . , ωl } • f = comp(g1 , . . . , gk ; h) for some k ≥ 1 (Figure 9): here we proceed as follows. First we apply the functions g1 , . . . , gk to the inherited arguments (42) and use their results as the input for h (43), which yields the final result (44). To this aim, let θf = δf (θg1 , . . . , θgk , θh ),

(41)

and include the rules hbi , ji → hbi , εi hbj , k + 1i → ha0 , ji

(i ∈ [r], j ∈ [k])

(42)

(j ∈ [k])

(43)

ha0 , εi → ha0 , k + 1i

(44)

in Rδf . • f = prec({gω | ω ∈ Ω}) (Figure 10): in this case, the control tree of f contains two nonterminal symbols: θf = δf (α, α). 18

(45)

b1 . . . br

b1 . . . br

a0

...

δf

b1 . . . br

θg1

a0

a0

b1 . . . bk

θgk

a0 θh

Figure 9: Composition Using Rule (47), the first one is replaced by the recursion argument s1 which is stored in b1 and whose top symbol ω ∈ Ω(k) determines, according to the equation (cf. Definition 2.3) f (ω(s′1 , . . . , s′k ), s2 , . . . , sr ) = gω (s2 , . . . , sr , s′1 , . . . , s′k , f (s′1 , s2 , . . . , sr ), . . . , f (s′k , s2 , . . . , sr )), (46) how to proceed with the recursion. For every possible situation, the corresponding control tree is stored in the respective inherited attribute (48), and the result of the selection (cf. Rule (33) above) replaces the second nonterminal symbol (49). Here, the recursion parameters s2 , . . . , sr (50) and the direct subtrees s′1 , . . . , s′k of the recursion argument s1 (51) are assigned to the inherited attributes. When the recursion is finished, the final result is passed towards the root (52). Thus Rδf contains the rules htree, 1i → hb1 , εi

(47)

hbω , 1i → θgω′

(k ≥ 0, ω ∈ Ω

(k)

)

(48)

htree, 2i → haΩ , 1i

(49)

hbi , 2i → hbi+1 , εi

(i ∈ [r − 1])

hbr−1+j , 2i → haj , 1i

(j ∈ [max{k ≥ 0 | Ω

(50) (k)

6= ∅}])

ha0 , εi → ha0 , 2i

(51) (52)

where, for every ω ∈ Ω(k) , gω′ (s2 , . . . , sr , s′1 , . . . , s′k ) is equal to the right–hand side of Equation (46), also incorporating the recursive calls of f : (r−1+k)

(r−1+k)

(r−1+k)

(r−1+k)

gω′ = comp(proj1 , . . . , projr−1 , projr , . . . , projr−1+k , (r−1+k) (r−1+k) (r−1+k) comp(projr , proj1 , . . . , projr−1 ; f ), . . . , (r−1+k) (r−1+k) (r−1+k) , . . . , projr−1 ; f ); comp(projr−1+k , proj1 gω ).

(53)

• f = iter(g, h, t) (Figure 11): here, the implementation of f exploits an alternative description of iteration, namely the one given by the functional equation  s if h(s) = t f (s) = f (g(s)) otherwise which can easily be shown equivalent to the formulation in Definition 2.3. In our context, it is more appropriate since it expresses that f can be computed by first evaluating h, testing the result for equality with t, and stopping or continuing the recursion depending on the outcome. This procedure is reflected in the following control tree: θf = δf (θh , θ=t , α), 19

(54)

b1 θgω′ 1

b2 . . . br

δf

a0

θgω′ l

tree bω1 . . . bωl

α

tree b1 . . . br−1 br . . . br−1+k α a0

a1 . . . ak aΩ

Figure 10: Primitive recursion where the rules in Rδf implement the data flow. First, the argument s is transferred to the input position of h (55). Afterwards, the result of h is compared with the tree t (56) where Rules (57) and (58) specify the two possible outcomes which are control trees inserted at the nonterminal symbol position (59), whose input argument is defined by Rule (60). Finally, in case of a terminating computation, the result is passed towards the root (61). hb1 , 1i → hb1 , εi

(55)

hb1 , 2i → ha0 , 1i

(56)

hb2 , 2i → θproj(1)

(57)

hb3 , 2i → θcomp(g;f )

(58)

1

htree, 3i → ha0 , 2i

(59)

hb1 , 3i → hb1 , εi

(60)

ha0 , εi → ha0 , 3i

(61)

b1

δf

a0

θproj(1) θcomp(g;f ) 1

b1

a0 θh

b1

b2

b3

a0

tree b1

α

a0

θ=t Figure 11: Iteration

The computation tree θ=t (see Figure 12) implements a choice operation of the form if b1 = t then b2 else b3 where, for every i ∈ [3], bi denotes the value of the respective inherited attribute at the root of this tree, and is given by induction on the structure of t = ω(t1 , . . . , tk ) as follows: θ=t = δ=t (α, θ=t1 , . . . , θ=tk ). The rules in Rδ=t exploit the fact that the value of b1 coincides with t if and only if both trees have the same symbol at the root position and if the corresponding (direct) subtrees are pairwise equal. First, the nonterminal symbol is replaced by b1 (62). Then, the first property is tested by means of the case mechanism (33), the inherited attributes being defined by Rules (63) and (64). Afterwards, the comparison is inductively performed on the k subtrees of t where the tree to be tested is the respective subtree of b1 (65), the b2 20

argument is the result of the preceding test (66, 67), thus implementing the conjunction sequentially, and where the b3 argument is taken from the root (68). Again, the overall result is passed towards the root (69, 70). htree, 1i → hb1 , εi

(62)

hbω , 1i → hb2 , εi

(63) ′

hbω′ , 1i → hb3 , εi hb1 , i + 1i → hai , 1i

(ω ∈ Ω \ {ω} ∪ {⊥})

(64)

(i ∈ [k])

(65)

hb2 , 2i → haΩ , 1i

(66)

hb2 , i + 1i → ha0 , ii

(i ∈ {2, . . . , k})

(67)

hb3 , i + 1i → hb3 , εi

(i ∈ [k])

(68)

ha0 , εi → haΩ , 1i

(k = 0)

(69)

ha0 , εi → ha0 , k + 1i

(k ≥ 1)

(70)

Note that the above construction also works correctly if the rank of the symbol at the root position of b1 differs from k. If it is less, then the superfluous subtree attributes ai at the root of b1 yield the special “dummy” value ⊥ (see below) which is used in the corresponding subtree test. If it is greater, then only k comparisons are performed. In both cases, the result is b3 since the topmost symbol of b1 is not ω. b1 b2 b3 δ=t a0

tree bω1 . . . bω . . . bωl α a1 . . . ak aΩ

b1 b2 b3

a0 θ=t1

...

b1 b2 b3

a0 θ=tk

Figure 12: Test for equality Furthermore we let R⊥ contain the rule haΩ , εi → hb⊥ , εi

(71)

and we add a “dummy rule” of the form hc, ii → ⊥

(72)

for every attribute occurrence whose value is not yet specified. This finishes the construction of Mf .  Let M = Mf in the following. We still have to show that M behaves as expected, i.e., that it computes f . First we establish four auxiliary statements. The first one expresses that the value of an inherited attribute occurrence does not depend on the subtree to whose root it is assigned. Thus it remains unchanged even after expansions of this subtree. Statement 1 Let t ∈ T∆ , w ∈ path(t), and b ∈ Inh. If hb, wi(t) ⇒∗M s for some s ∈ T∆ , then hb, wi(t[w ← t′ ]) ⇒∗M s for every t′ ∈ T∆ . The proof of Statement 1 is straightforward, since attribute dependencies are only running from left to right. The following two statements claim that the rules associated with every symbol of the original ranked alphabet Ω behave as expected, and that the implementation of the choice operation is correct. 21

Statement 2 Let t ∈ T∆ and w ∈ path(t). If t|w = ω(t1 , . . . , tk ) for some k ≥ 0, ω ∈ Ω(k) , and t1 , . . . , tk ∈ TΩ , then (i) ha0 , wi(t) ⇒∗M ω(t1 , . . . , tk ), (ii) for every i ∈ [k], hai , wi(t) ⇒∗M ti , and (iii) haΩ , wi(t) ⇒∗M hbω , wi(t). The proof of this statement is again simple, using Rules (31) to (33) and, in the first case, induction on the structure of t|w . Statement 3 If ϕ ∈ Comp M and w ∈ path(ϕ) such that, for every prefix v of w, labelϕ (v) ∈ ∆, and ϕ|w has no normal form with respect to ⇒M , then haΩ , wi(ϕ) has no normal form either. The proof of Statement 3 is trivial since ϕ (and hence ϕ|w ) must be reduced to normal form before some rule for haΩ , wi(ϕ) can be applied. Statement 4 Let ϕ ∈ Comp M and w ∈ path(ϕ) such that, for every prefix v of w, labelϕ (v) ∈ ∆, and ϕ|w = θ=t for some t ∈ TΩ . (i) If, for every j ∈ [3], hbj , wi(ϕ) ⇒∗M sj for some s1 ∈ TΩ ∪ {⊥} and s2 , s3 ∈ T∆ , then  s2 if s1 = t ha0 , wi(ϕ) ⇒∗M s3 otherwise. (ii) If hb1 , wi(ϕ) has no normal form, then ha0 , wi(ϕ) has no normal form either. This statement can be proved by induction on the structure of t using Statements 1 and 2 and, in the second case, Statement 3. After having established these auxiliary statements, we can now continue with the main line of the proof. Proof (of τMf = f ) TΩ ∈ µREC and (s1 , . . . , sr ) ∈ T[Σ] , and let t ∈ T∆ and w ∈ path(t) such that Let f : T[Σ] t|w = θf .

(

(i) We first consider the case that f is defined for this argument tuple, i.e., that there exists a tree u ∈ TΩ such that f (s1 , . . . , sr ) = u. Claim: If, for every i ∈ [r], hbi , wi(t) ⇒∗M si

(73)

ha0 , wi(t) ⇒∗M u.

(74)

then

This claim allows us to conclude that (30)

ha0 , εi(δ0 (s1 , . . . , sr , α)) ⇒M (28) ⇒∗M (74) ⇒∗M

22

ha0 , r + 1i(δ0 (s1 , . . . , sr , α)) ha0 , r + 1i(δ0 (s1 , . . . , sr , θf )) u

since, for every i ∈ [r], (29)

hbi , r + 1i(δ0 (s1 , . . . , sr , θf ))

⇒M

ha0 , ii(δ0 (s1 , . . . , sr , θf ))

Stm. 2(i) ⇒∗M

si

and thus, by Definition 3.14, τM (s1 , . . . , sr ) = f (s1 , . . . , sr ). The proof of (74) is given by induction on the structure of f according to Definition 2.3 as follows. • f = topω where ω ∈ Ω(r) : (35)

ha0 , wi(t) ⇒M

ω(hb1 , wi(t), . . . , hbr , wi(t))

(73)

⇒∗M = (r)

• f = proji

ω(s1 , . . . , sr ) u.

for some i ∈ [r]:

ha0 , wi(t)

(40)

⇒M

haΩ , w.ri(t)

(37)

⇒∗M

haΩ , w.ri(t[w.r ← hbr , wi(t)])

(73)

⇒∗M

haΩ , w.ri(t[w.r ← sr ])

Stm. 2(iii) ⇒∗M (39) ⇒M ⇒∗M (38) ⇒M

hbωr , w.ri(t[w.r ← sr ]), where labelsr (ε) = ωr haΩ , w.(r − 1)i(t[w.r ← sr ]), if r > 1 hbω1 , w.1i(t[w.j ← sj ; j ∈ [r]]), where labels1 (ε) = ω1 hbi , wi(t[w.j ← sj ; j ∈ [r]])

(73),Stm. 1 ⇒∗M

si

=

u.

• f = comp(g1 , . . . , gk ; h) for some k ≥ 1: let tj = gj (s1 , . . . , sr ) for every j ∈ [k]. Since, for every i ∈ [r] and every j ∈ [k], (42)

hbi , w.ji(t) ⇒M (73) ⇒∗M

hbi , wi(t) si , (74)

it follows from the induction hypothesis, applied to gj , that ha0 , w.ji(t) ⇒∗M tj . Thus, (43)

hbj , w.(k + 1)i(t) ⇒M ⇒∗M

ha0 , w.ji(t) tj ,

and hence, by exploiting the induction hypothesis for h, (44)

ha0 , wi(t) ⇒M ⇒∗M 23

ha0 , w.(k + 1)i(t) u.

• f = prec({gω | ω ∈ Ω}): we employ a second induction on the structure of s1 = ω(s′1 , . . . , s′k ) with k ≥ 0, ω ∈ Ω(k) , and s′j ∈ TΩ for every j ∈ [k]: ha0 , wi(t)

(52)

⇒M (49) ⇒∗M (47) ⇒∗M (73) ⇒∗M Stm. 2(iii) ⇒∗M (48) ⇒∗M

ha0 , w.2i(t) ha0 , w.2i(t[w.2 ← haΩ , w.1i(t)]) ha0 , w.2i(t[w.2 ← haΩ , w.1i(t[w.1 ← hb1 , wi(t)])]) ha0 , w.2i(t[w.2 ← haΩ , w.1i(t[w.1 ← s1 ])]) ha0 , w.2i(t[w.2 ← hbω , w.1i(t[w.1 ← s1 ])]) ha0 , w.2i(t[w.2 ← θgω′ ]).

Additionally we have, for every i ∈ [r − 1], (50)

hbi , w.2i(t[w.2 ← θgω′ ])

⇒M

hbi+1 , wi(t[w.2 ← θgω′ ])

(73),Stm. 1 ⇒∗M

si+1

and, for every j ∈ [max{k ≥ 0 | Ω(k) 6= ∅}], hbr−1+j , w.2i(t[w.2 ← θgω′ ]) (51)

⇒M

haj , w.1i(t[w.2 ← θgω′ ])

(47)

⇒∗M (73),Stm. 1 ⇒∗M Stm. 2(ii) ⇒∗M

haj , w.1i(t[w.2 ← θgω′ , w.1 ← hb1 , wi(t[w.2 ← θgω′ ])]) haj , w.1i(t[w.2 ← θgω′ , w.1 ← s1 ]) s′j .

Now, the claim follows similarly to the previous case by exploiting the structure of θgω′ according to (53), using the first induction hypothesis for gω and the second for s′j in the recursive calls of f . The technical details are omitted. • f = iter(g, h, t0 ): let s = s1 . According to Definition 2.3, there exists n ≥ 0 such that h(gn (s)) = t0 and, for every m < n, h(gm (s)) is defined and different from t0 . Since (55)

hb1 , w.1i(t) ⇒M

hb1 , wi(t)

(73)

⇒∗M

s,

the induction hypothesis, applied to h, yields (56)

hb1 , w.2i(t) ⇒M (74) ⇒∗M

ha0 , w.1i(t) h(s)

We prove the claim by induction on n. n = 0: since (57)

hb2 , w.2i(t) ⇒M 24

θproj(1) 1

and since h(s) = t0 , Statement 4(i) yields ha0 , w.2i(t) ⇒∗M

θproj(1) 1

such that, using the induction hypothesis together with (60)

hb1 , w.3i(t′ )

hb1 , wi(t′ )

⇒M (73),Stm. 1 ⇒M

s

where t′ = t[w.3 ← θproj(1) ], we have 1

(61)

ha0 , wi(t) ⇒M

ha0 , w.3i(t)

(59)

⇒∗M

ha0 , w.3i(t[w.3 ← ha0 , w.2i(t)])

⇒∗M (74) ⇒∗M

ha0 , w.3i(t′ ) s

=

u

n ⇒ n + 1: here, (58)

hb3 , w.2i(t) ⇒M

θcomp(g;f )

(41)

=

δcomp(g;f ) (θg , θf )

Let t′ = t[w.3 ← θcomp(g;f ) ]. Since hb1 , w.3.1i(t′ )

(42)

hb1 , w.3i(t′ )

(60)

hb1 , wi(t′ )

⇒M ⇒M (73),Stm. 1 ⇒∗M

s,

the induction hypothesis, applied to g, yields (43)

hb1 , w.3.2i(t′ ) ⇒M

ha0 , w.3.1i(t′ )

(74)

⇒∗M

g(s).

Therefore, by using h(s) 6= t0 and the induction hypothesis for n, ha0 , wi(t)

(61)

⇒M

ha0 , w.3i(t)

(59) ⇒∗M Stm. 4(i) ⇒∗M (44) ⇒M ⇒∗M

=

ha0 , w.3i(t[w.3 ← ha0 , w.2i(t)]) ha0 , w.3i(t′ ) ha0 , w.3.2i(t′ ) gn (g(s)) u.

(ii) If, on the other hand, f (s1 , . . . , sr ) is undefined, then we have to show that our implementation of f does not yield any result either. Formally we prove by induction on the structure of f : if t ∈ T∆ and w ∈ path(t) such that t|w = θf and if, with respect to ⇒M , 25

(a) hbi , wi(t) has no normal form for some i ∈ [r] or (b) hbi , wi(t) ⇒∗M si ∈ T∆ for every i ∈ [r] and f (s1 , . . . , sr ) is not defined, then ha0 , wi(t) has no normal form. In the first two of the following cases, only (a) has to be considered since the respective tree functions are total. • f = topω where ω ∈ Ω(r) : (35)

ha0 , wi(t) ⇒M

ω(hb1 , wi(t), . . . , hbr , wi(t))

which has no normal form. (r)

• f = projj for some j ∈ [r]: let i ∈ [r] be the greatest index such that (a) holds. As in the proof for the terminating case above, we have ha0 , wi(t) ⇒∗M

haΩ , w.ii(t[w.i ← hbi , wi(t)][w.j ← sj ; j ∈ {i + 1, . . . , r}])

which, according to Statement 3, has no normal form. • f = comp(g1 , . . . , gk ; h) for some k ≥ 1: here, the following situations can occur. (a) As above or (b) f (s1 , . . . , sr ) is not defined since i. gj (s1 , . . . , sr ) is not defined for some j ∈ [k] or ii. tj = gj (s1 , . . . , sr ) for every j ∈ [k], and h(t1 , . . . , tk ) is undefined. In every case, the claim follows immediately from the induction hypothesis. • f = prec({gω | ω ∈ Ω}): the following cases can be distinguished. (a) One of the arguments is undefined, i.e., i. hb1 , wi(t) has no normal form, or ii. hbi , wi(t) has no normal form for some i ∈ {2, . . . , r}, or (b) there exists a subterm s′ = ω(s′1 , . . . , s′k ) of s1 such that gω (s2 , . . . , sr , s′1 , . . . , s′k , f (s′1 , s2 , . . . , sr ), . . . , f (s′k , s2 , . . . , sr )) is undefined. As above, ha0 , wi(t) ⇒∗M

ha0 , w.2i(t[w.2 ← haΩ , w.1i(t[w.1 ← hb1 , wi(t)])]).

According to Statement 3, this term has no normal form in case (a)i. Otherwise, the induction hypothesis, applied to gω , yields the desired result. • f = iter(g, h, t0 ): again we distinguish the following cases. (a) hb1 , wi(t) has no normal form or (b) f (s1 ) is not defined since i. h(gn (s1 )) is undefined for some n ≥ 0 (and h(gm (s1 )) is defined and different from t0 for every m < n) or ii. h(gn (s1 )) is defined and different from t0 for every n ≥ 0.

26

As above, ha0 , wi(t) ⇒∗M ha0 , w.3i(t[w.3 ← ha0 , w.2i(t)]). By the induction hypothesis for h and by Statement 4(ii), this term has no normal form in case (a). In case (b)i, an induction on n together with the induction hypothesis for g and h can be used to prove the claim. In case (b)ii, in analogy with the argumentation for the terminating case above, one can show that if t ∈ T∆ and w ∈ path(t) such that t|w = θf , and if hb1 , wi(t) ⇒∗M gn (s) for some n ≥ 0, then ha0 , wi(t) ⇒∗M ha0 , w.3.2i(t′ ) where t′ = t[w.3 ← θcomp(g;f ) ] and hb1 , w.3.2i(t′ ) ⇒∗M gn+1 (s). Because t|w.3.2 = θf by Equation (41), this argument can repeatedly be applied such that ha0 , wi(t) has an infinite outgoing computation. Since, by Lemma 3.12, ⇒M has the weak diamond  property, this attribute occurrence has no normal form with respect to ⇒M . We conclude with two simple applications of the above universality theorem. The first compares the computational power of HO–ATTs with the corresponding first–order devices, i.e., with ATTs. Let t–HOATT be the class of total tree functions in HOATT . Remark ATT ( t–HOATT .



Proof The inclusion ATT ⊆ t–HOATT follows trivially from the definitions in Section 3 and the fact that ATT contains only total tree functions. Let f : T[Nat,Nat] → TNat denote the binary tree function defined as follows (cf. [Eng82]): f (zero, y) = suc(y) f (suc(x), y) = f (x, f (x, y)). n

Clearly, for every n, m ≥ 0, f (suc n (zero), suc m (zero)) = suc 2 +m (zero). Lemma 3.9 immediately implies (also cf. [Eng82] and Lemma 6.2 of [FV98]) that f ∈ / ATT . On the other hand, by Church’s thesis, f ∈ µREC and f is total. Hence, f ∈ t–HOATT \ ATT .  In the second conclusion we address the question under which circumstances the computation of a given HO–ATT M does not yield any result. This is clearly possible if M is circular. If we forbid this, however, then the attribute evaluation terminates if M fulfils the following condition. Definition 4.2 A HO–ATT M has the finite expansion property if, for all input trees s1 , . . . , sr , there is a number l ≥ 0 such that {ψ ∈ Comp M | ϕ0 ⇒∗M ψ} = {ψ ∈ Comp M | ϕ0 ⇒∗,≤l M ψ} where ϕ0 = ha0 , εi(δ0 (s1 , . . . , sr , α)).



Note that this property is equivalent to the fact that there are no infinite computations with infinitely many tree expansions. Theorem 4.3 It is generally undecidable whether a given noncircular HO–ATT has the finite expansion property.

27

Proof Let M be a noncircular HO–ATT with input trees s1 , . . . , sr . By the weak diamond property of ⇒M (cf. Lemma 3.12), ha0 , εi(δ0 (s1 , . . . , sr , α)) either has a normal form or an outgoing infinite reduction. In the second case, M can not have the finite expansion property according to Lemma 3.16. Hence M has the finite expansion property iff its associated tree function τf is total. On the other hand, Theorem 4.1 shows that every partial recursive tree function f ∈ µREC is computable by a HO–ATT Mf , which is moreover noncircular. (In fact, the attribute dependencies are running from left to right, i.e. it is even L–attributed; cf. [Boc76].) And it is well known that the totality of such functions is undecidable.  Acknowledgements We would like to thank the editor, Joost Engelfriet, and the anonymous referee for many valuable comments on a draft of this paper.

References [BN98]

F. Baader and T. Nipkow. Term Rewriting and All That. Cambridge University Press, New York, 1998.

[Boc76]

G.V. Bochmann. Semantic evaluation from left to right. Communications of the ACM, 19(2):55–62, February 1976.

[Eng82]

J. Engelfriet. Tree transducers and syntax–directed semantics. In 7th Coll. on Trees in Algebra and Programming (CAAP’82), pages 82–107. March 1982.

[EV91]

J. Engelfriet and H. Vogler. Modular tree transducers. Theoretical Computer Science, 78:267–304, 1991.

[FHVV93] Z. F¨ ul¨op, F. Herrmann, S. V´agv¨olgyi, and H. Vogler. Tree transducers with external functions. Theoretical Computer Science, 108:185–236, 1993. [F¨ ul81]

Z. F¨ ul¨op. On attributed tree transducers. Acta Cybernetica, 5:261–279, 1981.

[FV98]

Z. F¨ ul¨op and H. Vogler. Syntax–Directed Semantics — Formal Methods Based on Tree Transducers. Monographs in Theoretical Computer Science. An EATCS Series. Springer–Verlag, 1998.

[Hup78]

U.L. Hupbach. Rekursive Funktionen in mehrsortigen Peano–Algebren. Elektronische Informationsverarbeitung und Kybernetik, 14(10):491–506, 1978.

[Knu68]

D.E. Knuth. Semantics of Context-Free Languages. Mathematical Systems Theory, 2(2):127–145, June 1968.

[Knu71]

D.E. Knuth. Semantics of Context-Free Languages: Correction. Mathematical Systems Theory, 5(1):95–96, June 1971.

[KU85]

A.J. Kfoury and P. Urzyczyn. Necessary and sufficient conditions for the universality of programming formalisms. Acta Informatica, 22:347–377, 1985.

[SV91]

S.D. Swierstra and H.H. Vogt. Higher order attribute grammars. In H. Alblas and B. Melichar, editors, Attribute Grammars, Applications and Systems, volume 545 of Lecture Notes in Computer Science, pages 256–296. Springer–Verlag, June 1991.

[TC90]

T. Teitelbaum and R. Chapman. Higher–order attribute grammars and editing environments. ACM SIGPLAN Notices, 25(6):197–208, 1990. 28

[VSK89]

H.H. Vogt, S. D. Swierstra, and M.F. Kuiper. Higher order attribute grammars. ACM SIGPLAN Notices, 24(7):131–145, 1989.

29

Suggest Documents