Abstract It is shown that formulas in monadic second order logic (mso) with one free variable can be mimicked by attribute grammars with a designated

Similar documents
Equational Logic. Chapter Syntax Terms and Term Algebras

Foundations of Mathematics

Automata Theory and Formal Grammars: Lecture 1

Extracted from a working draft of Goldreich s FOUNDATIONS OF CRYPTOGRAPHY. See copyright notice.

Tribhuvan University Institute of Science and Technology Micro Syllabus

Mathematical Preliminaries. Sipser pages 1-28

Foundations of Informatics: a Bridging Course

Abstract We consider several language generating formalisms from the literature, such as string-valued attribute grammars with only s-attributes, para

Automata on linear orderings

A shrinking lemma for random forbidding context languages

of acceptance conditions (nite, looping and repeating) for the automata. It turns out,

The limitedness problem on distance automata: Hashiguchi s method revisited

3. Only sequences that were formed by using finitely many applications of rules 1 and 2, are propositional formulas.

CHAPTER 10. Gentzen Style Proof Systems for Classical Logic

How to Pop a Deep PDA Matters

A MODEL-THEORETIC PROOF OF HILBERT S NULLSTELLENSATZ


Weak ω-automata. Shaked Flur

1 CHAPTER 1 INTRODUCTION 1.1 Background One branch of the study of descriptive complexity aims at characterizing complexity classes according to the l

Foundations of Mathematics MATH 220 FALL 2017 Lecture Notes

Chapter 0 Introduction. Fourth Academic Year/ Elective Course Electrical Engineering Department College of Engineering University of Salahaddin

Computability and Complexity

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Sampled Semantics of Timed Automata

Lecture Notes On THEORY OF COMPUTATION MODULE -1 UNIT - 2

An Algebraic View of the Relation between Largest Common Subtrees and Smallest Common Supertrees

Seminaar Abstrakte Wiskunde Seminar in Abstract Mathematics Lecture notes in progress (27 March 2010)

CPSC 421: Tutorial #1

Tableau Calculus for Local Cubic Modal Logic and it's Implementation MAARTEN MARX, Department of Articial Intelligence, Faculty of Sciences, Vrije Uni

Lecture Notes on Inductive Definitions

Tableau-based decision procedures for the logics of subinterval structures over dense orderings

Computability and Complexity

Computability and Complexity

INDEPENDENCE OF THE CONTINUUM HYPOTHESIS

Lecture 2: Connecting the Three Models

Logic Part I: Classical Logic and Its Semantics

Asynchronous cellular automata for pomsets. 2, place Jussieu. F Paris Cedex 05. Abstract

Vakgroep Informatica

Logic and Automata I. Wolfgang Thomas. EATCS School, Telc, July 2014

On the Myhill-Nerode Theorem for Trees. Dexter Kozen y. Cornell University

CS243, Logic and Computation Nondeterministic finite automata

Lecture 17: Language Recognition

1 Introduction We study classical rst-order logic with equality but without any other relation symbols. The letters ' and are reserved for quantier-fr

CSE 105 Homework 1 Due: Monday October 9, Instructions. should be on each page of the submission.

THEORY OF COMPUTATION

What are the recursion theoretic properties of a set of axioms? Understanding a paper by William Craig Armando B. Matos

Binary Decision Diagrams

Computational Theory

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

3 Propositional Logic

The Importance of Being Formal. Martin Henz. February 5, Propositional Logic

Fall 1999 Formal Language Theory Dr. R. Boyer. Theorem. For any context free grammar G; if there is a derivation of w 2 from the

Expressive power of pebble automata

Design of abstract domains using first-order logic

Final exam study sheet for CS3719 Turing machines and decidability.

logic, cf. [1]. Another area of application is descriptive complexity. It turns out that familiar logics capture complexity classes over classes of (o

1 Introduction A general problem that arises in dierent areas of computer science is the following combination problem: given two structures or theori

Preliminaries. Introduction to EF-games. Inexpressivity results for first-order logic. Normal forms for first-order logic

Easy Shortcut Definitions

Theory of Computation (II) Yijia Chen Fudan University

VAUGHT S THEOREM: THE FINITE SPECTRUM OF COMPLETE THEORIES IN ℵ 0. Contents

Lecture Notes 1 Basic Concepts of Mathematics MATH 352

Hierarchy among Automata on Linear Orderings

Fundamenta Informaticae 30 (1997) 23{41 1. Petri Nets, Commutative Context-Free Grammars,

Propositional Logic: Models and Proofs

Theory of Computation

Classes of Boolean Functions

Informal Statement Calculus

Binary Decision Diagrams. Graphs. Boolean Functions

Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:

Closure under the Regular Operations

Non-context-Free Languages. CS215, Lecture 5 c

Lecture Notes on Inductive Definitions

REVIEW QUESTIONS. Chapter 1: Foundations: Sets, Logic, and Algorithms

One Quantier Will Do in Existential Monadic. Second-Order Logic over Pictures. Oliver Matz. Institut fur Informatik und Praktische Mathematik

Computability and Complexity

Electronic Notes in Theoretical Computer Science 18 (1998) URL: 8 pages Towards characterizing bisim

Preliminaries to the Theory of Computation

Chapter 11: Automated Proof Systems

The rest of the paper is organized as follows: in Section 2 we prove undecidability of the existential-universal ( 2 ) part of the theory of an AC ide

Classical Propositional Logic

Automata Theory for Presburger Arithmetic Logic

First-Order Logic. Chapter Overview Syntax

Fall 1999 Formal Language Theory Dr. R. Boyer. 1. There are other methods of nding a regular expression equivalent to a nite automaton in

Propositional Logic, Predicates, and Equivalence

Math 42, Discrete Mathematics

First-Order Logic. 1 Syntax. Domain of Discourse. FO Vocabulary. Terms

UNIT II REGULAR LANGUAGES

Chapter 6. Properties of Regular Languages

DM17. Beregnelighed. Jacob Aae Mikkelsen

Functions on languages:

CS:4330 Theory of Computation Spring Regular Languages. Finite Automata and Regular Expressions. Haniel Barbosa

Multiplicative Conjunction and an Algebraic. Meaning of Contraction and Weakening. A. Avron. School of Mathematical Sciences

Models of Computation. by Costas Busch, LSU

Solvability of Word Equations Modulo Finite Special And. Conuent String-Rewriting Systems Is Undecidable In General.

A version of for which ZFC can not predict a single bit Robert M. Solovay May 16, Introduction In [2], Chaitin introd

Extending temporal logic with!-automata Thesis for the M.Sc. Degree by Nir Piterman Under the Supervision of Prof. Amir Pnueli Department of Computer

CS 455/555: Finite automata

Automata theory. An algorithmic approach. Lecture Notes. Javier Esparza

Introduction to Theory of Computing

Transcription:

Attribute Grammars and Monadic Second Order Logic Roderick Bloem October 15, 1996

Abstract It is shown that formulas in monadic second order logic (mso) with one free variable can be mimicked by attribute grammars with a designated boolean attribute and vice versa. We prove that mso formulas with two free variables have the same power in dening binary relations on nodes of a tree as regular path languages have. For graphs in general, mso formulas turn out to be stronger. We also compare path languages against the routing languages of Klarlund and Schwartzbach. We compute the complexity of evaluating mso formulas with free variables, especially in the case where there is a dependency between free variables of the formula. Last, it is proven that mso tree transducers have the same strength as attributed tree transducers with the single use requirement and ags.

Introduction The main purpose of this paper is to investigate the relation between tree transductions dened by attribute grammars on one hand and tree transductions dened by monadic second order logic on the other. Along the way, we will cover path languages and we will go into some complexity issues. Attribute grammars have been introduced by Knuth [Knu68] as a way to assign a meaning to a string from a context-free language, in a syntax directed way. Attribute grammars have become popular as a tool for building compilers and as an object for study in formal language theory. One can take a slightly dierent view from Knuth's, and see an attribute grammar as assigning a meaning to a derivation tree of a context-free language, or, in our case, to a tree over an operator alphabet. If we limit the possible meanings to trees, we obtain attributed tree transducers [Eng81, Ful81]. Tree transducers dene functions from trees over one alphabet to trees over another. Our main interest lies in attributed tree transducers that have attributes whose value can range over trees (tree attributes), and attributes with a nite semantic domain (ags). Furthermore, we will forbid a tree attribute to be used more than once, in order to limit the power of attributed tree transducers. This is Giegerich's single use requirement [Gie88]. Our second important formalism, monadic second order logic, is used to dene properties of graphs. It is popular because it combines great ease and strength of expression with desirable decidability properties. Closed monadic second order formulas dene sets of graphs, while open monadic order formulas dene relations on nodes of graphs. We also use monadic second order logic to dene tree transductions, as in [Eng91, Cou92]. The monadic second order tree transducers that we consider are deterministic ones that can copy nodes. A closed mso formula denes the domain of the transducer, while formulas with one free variable dene the nodes in the output and formulas with two free variables dene the edges. Regular path languages are regular languages over a set of directives that tell you how to move through a graph, while checking properties of the nodes by mso formulas with one free variable. Path languages dene binary relations on nodes of a graph: a pair of nodes is in the relation if you can walk from the rst to the second, following the directions in a string from the language. Routing languages, a concept akin to path languages are used by Klarlund and Schwartzbach [KS93] to extend the concept of recursive data structures. They allow the denition of data structures such as circular linked lists in an elegant 1

way, avoiding explicit use of pointers. It will turn out that there are structures that cannot be dened by a routing language, while they can be dened by a path language. Path languages can be evaluated eciently, like the routing languages of Klarlund and Schwartzbach. In Chapter 1, we will introduce the necessary formalisms and notation. An operational description of closed mso formulas exists in the form of tree automata. In Chapters 2 and 3 we will explore operational characterizations of mso formulas with one and two free variables, respectively. An attribute grammar with a designated boolean attribute recognizes exactly those nodes of a tree for which the boolean attribute has decoration `true'. In Chapter 2, we will prove that in this way, monadic second order formulas with one free variable are equivalent to attribute grammars with a designated boolean attribute. From this we derive the complexity of computing whether a xed formula with free variables holds for a given tree and nodes. This can be done in linear time. We also infer the complexity of computing all node sequences for which a xed formula holds, given a tree. For an mso formula with k free variables, we can do this in O(n k ) time. In the third chapter, we show that regular path languages and monadic second order formulas with two free variables dene the same binary relations on nodes in trees. On graphs in general, monadic second order formulas will turn out to be stronger than regular path languages. We will show that path languages that dene a partial function can be evaluated in linear time. From this result, we conclude that if there is a dependency in the relation dened by a monadic second order formula with k free variables, we can compute the relation dened by the formula in O(n k 1 ) time. This is a factor n more ecient than was shown in Chapter 2. Finally, in Chapter 4, with help from the results of Chapters 2 and 3, we prove that monadic second order tree transducers have the same strength as attributed tree transducers with ags and the single use requirement. We also show that monadic second order tree transductions can be evaluated in linear time. 2

Chapter 1 Denitions and Notation In this chapter we will dene the terminology and notations we use throughout this paper. First we give some common mathematical denitions, then we dene regular languages, terms and algebras, graphs and trees, tree automata, attribute grammars, and last of all monadic second order logic. 1.1 Preliminaries We will discuss the basic mathematics here: sets, relations, functions and strings. Sets The set of boolean values is B = ffalse; trueg: For the sake of brevity, we sometimes use 0 instead of false and 1 instead of true. The set of natural numbers is N = f0; 1; 2; : : : g: The set of positive natural numbers is N + = N n f0g. We use the following notation for sets of integers (a; b 2 N): [a; b] = fx 2 N j a x bg: Sequences are denoted (a 1 ; a 2 ; : : : ; a n ) or ha 1 ; a 2 ; : : : ; a n i. By (a 1 ; : : : ; b i ; : : : ; a n ), we mean the sequence (a 1 ; : : : ; a i 1 ; b; a i+1 ; : : : ; a n ). Also, 8 < (a 1 ; : : : ; b; : : : ; b 0 ; : : : ; a (a 1 ; : : : ; a i 1 ; b; a i+1 ; : : : ; a j 1 ; b 0 ; a j+1 ; : : : ; a n ) if i < j i n) = j :(a 1 ; : : : ; a j 1 ; b 0 ; a j+1 ; : : : ; a i 1 ; b; a i+1 ; : : : ; a n ) if i > j For a set A, P(A) is the power set of A, i.e., the set of all subsets of A. The cardinality of A is denoted #A. A set fa 1 ; : : : ; A n g of non-empty subsets of A is a partition of A if S i2[1;n] A i = A and A i \ A j = ; for i 6= j. If fa 1 ; : : : ; A n g is a partition of A, the sequence (A 1 ; : : : ; A n ) is an ordered partition of A. 3

Relations and Functions For sets A 1 ; : : : ; A n, a n-ary relation is a subset R A 1 A n. Its restriction to A 0 1 A 1 is R A 0 1 = R \ (A 0 1 A 2 A n ). In particular, for sets A and B, a (binary) relation is a subset R A B. The image of A 0 A under R is R(A 0 ) = fb j 9a 2 A 0 : (a; b) 2 Rg. If A 0 = fag, we also write R(a), instead of R(fag), and whenever R(A 0 ) = fbg, we can write R(A 0 ) = b. The identity on a set A is the relation id A = f(a; a) j a 2 Ag. The composition of two relations R B C and S A B is R S = f(a; c) 2 A C j 9b : (b; c) 2 R and (a; b) 2 Sg. We also use S ; R = R S. If R A A, we dene R 0 = id A, and R i = R i 1 ; R for i 1. The transitive, reexive closure of R is R = S i2n R i. Let A be a set. A set A 0 A is closed under a relation R A A if for all x 2 A 0 : if (x; y) 2 R, then y 2 A 0. A relation R A B is a (total) function, denoted R : A! B, if #R(a) = 1 for every a 2 A. So, for any function f : A! B and A 0 A, f(a 0 ) = ff(a) 2 B j a 2 A 0 g. A function f : fa 1 ; : : : ; a n g! fb 1 ; : : : ; b n g, dened as f = f(a 1 ; b 1 ); : : : ; (a n ; b n )g is also written as an enumerated function: [a 1 7! b 1 ; : : : ; a n 7! b n ]. A relation R A B is a partial function, denoted R : A * B, if #R(a) 1 for every a 2 A. The domain of R is the subset dom(r) = fa 2 A j #R(a) = 1g. We use the letters f and g for total and partial functions. Strings and String Languages Let A be a nite set, or alphabet. A string over A is a sequence (a 1 ; : : : ; a n ), with n 0, and a i 2 A, for all i 2 [1; n]. The string (a 1 ; : : : ; a n ) has length n. The empty string is the string with length 0, denoted ". The set of all strings over A is denoted A, and A + = A n f"g. If a = (a 1 ; : : : ; a n ), and b = (b 1 ; : : : ; b m ) are strings over A, then the concatenation of a and b is a b = (a 1 ; : : : ; a n ; b 1 ; : : : ; b m ). The empty string is the unity with respect to concatenation. Usually, we leave out the brackets and commas, and write a 1 a 2 a n for (a 1 ; a 2 ; : : : ; a n ). Let A be an alphabet. A (string) language is a set of strings L A. We have the following operations on string languages Concatenation: for languages K; L A, Powers: For L A, K L = fa b 2 A j a 2 K and b 2 Lg: L 0 = f"g, and L n+1 = L n L, for n 2 N : 4

Kleene star and Kleene plus: for L A, L = [ L n ; and L + = [ n2n Naturally, the usual set operators can be used on string languages, as in K [L, K \L, and K n L. 1.2 Regular Languages In this section we discuss three well-known ways of dening regular string languages. See, for example [Gin75] for more details. Later on, we give an example of one language dened in these three dierent ways. Finite State Automata Denition 1.1. A nite state (string) automaton (fsa, for short) over is a quintuple A = (Q; ; ; q 0 ; F ), with Q is a nite set of states, is the input alphabet, Q Q is the transition relation, q 0 2 Q is the start state, F Q is the set of nal states. A nite state automaton A = (Q; ; ; q 0 ; F ) induces a step relation `A ( Q) ( Q), as follows. For every 2, x 2, and q 2 Q, if (q; ; q 0 ) 2, then (x; q) `A (x; q 0 ). n2n+ Denition 1.2. An fsa A over recognizes the string language kak : L n : kak = fx 2 j 9q f 2 F : (x; q 0 ) ` A ("; q f )g In most of the literature, the language kak is denoted L(A). A language is called recognizable if there is an fsa that recognizes it. For reasons of notational convenience, we extend to a relation over Q Q. We inductively extend the denition of : for all q, (q; "; q) 2, and for x 2 +, 2 : (q; x ; q 0 ) 2 if 9q 00 : (q; x; q 00 ) 2 and (q 00 ; ; q 0 ) 2. This is equivalent to (q; x; q 0 ) 2 i (x; q) ` A ("; q 0 ). A deterministic nite state automaton is a nite state automaton for which the transition relation is a function Q! Q. Accordingly, we also write (q; x) = q 0 for (q; x; q 0 ) 2. Deterministic automata recognize exactly the same languages as nondeterministic ones do. 5

Right-Linear Grammars Right-linear grammars are very much akin to nite state automata. They dene a way to derive, rather than recognize a regular string language. Denition 1.3. A right-linear grammar is a quadruple G = (N; ; P; S), where N is a nite set of nonterminals, is the input alphabet, P (N N) [ (N f"g), is the set of productions, and S is the start symbol. If (n; ; n 0 ) 2 P, we write n! n 0, for (n; ") 2 P we write n! ". We dene the language of a right-linear grammar by transforming it into an equivalent fsa. Let G = (; N; P; S) be a right-linear grammar. The corresponding fsa A G is (N; ; ; S; fn j (n; ") 2 P g), where = (P \ (N N)) The language dened by G is simply ka G k. We use two ways of abbreviating a group of productions: if 1 ; : : : ; n 2, n 1, 1. A! 1 n B is an abbreviation for A! 1 A 1, A 1! 2 A 2, : : :, A n 1! n B (where A 1 ; : : : ; A n 1 are new non-terminals), and 2. A! 1 n is an abbreviation for A! 1 A 1, A 1! 2 A 2, : : :, A n 1! n A n, A n! " (where A 1 ; : : : ; A n are new non-terminals). Regular Expressions Regular expressions are the third way to dene a regular string language. First we dene its syntax. Denition 1.4. Let be an alphabet. A regular expression over is recursively dened as follows. 1. ;, " and (for every 2 ) are regular expressions, 2. if r and r 0 are regular expressions over, so are r + r 0, r r 0 and r. A regular expression r over denes a language krk, dened as follows. Denition 1.5. Let be an alphabet. 6

1. k;k = ;, k"k = f"g, and kk = fg for all 2, 2. if r and r 0 are regular expressions over, kr + r 0 k = krk [ kr 0 k, kr r 0 k = krk kr 0 k, and kr k = krk. A language L is called regular if there is a regular expression r with krk = L. The set of all regular languages is denoted reg. It is well known that reg is equal to the set of all recognizable languages, dened by a nite state automaton or a right-linear grammar. Example 1.6. We specify one language in the three notations. The alphabet is = fa; b; cg. As a nite state automaton: A = (fs; T g; ; ; S; fsg), where 8 < S if either q = S and = c, or q = T and = b, = q; : : T if q = S and = a. See Figure 1.1 for the `transition graph'. As a right-linear grammar: G = (fs; T g; ; P; S), where As a regular expression: r = (ab + c). P = fs! at; S! cs; S! "; T! bsg b T S a c Figure 1.1: Transition Graph 7

1.3 Terms and Algebras We here dene terms, which are strings over an operator alphabet of a certain form, and algebras, a way to assign meaning to a term by giving a meaning to each element of the alphabet. Denition 1.7. An operator alphabet is a pair (; rk ), with a nite set and a rank function rk :! N. For all k 2 N, k = f 2 j rk () = kg. We will often keep the rank function implicit and refer to as the operator alphabet. The subscript to rk is omitted whenever the set is clear from the context. The subset k is the set of operators of rank k. The elements of 0 are called constants. The rank of an operator alphabet is the maximum of the rank of its elements, rk() = maxfrk() j 2 g. An operator alphabet has a rank interval, rks() = [1; rk()]. The set T of terms over is a subset of +, inductively dened as follows. Denition 1.8. Let be an operator alphabet. T is the smallest set satisfying the following condition. If k 0, 2 k ; and t 1 ; t 2 ; : : : ; t k 2 T, then t 1 t 2 t k 2 T. Please note that the base in this induction is for the operators with rank 0. Every term has a unique decomposition in an operator and sub-terms. We also need terms with variables. These variables are treated as constants. For variables we use the letter. Denition 1.9. Let be an operator alphabet and N a nite set (of variables), such that \ N = ;. (N) is the operator alphabet with (N) 0 = 0 [ N and (N) k = k for k 1. T (N) = T (N) is the set of terms over with variables in N. A term t 2 T (N) is linear if every variable appears no more than once in t. It is non-deleting if every variable appears at least once. These terms are merely syntactical. In order to be able to interpret them, we use an algebra. Denition 1.10. Let be an operator alphabet. A (deterministic) -algebra is a set A, together with, for every 2 k (k 0), a (total) function A : A k! A. The algebra is nite if A is nite. We usually keep these functions implicit and refer to the algebra as A. We identify the nullary functions A for 2 0 with the corresponding elements in A. It is not strictly necessary to associate functions with the elements of. We can also use relations, and obtain a `non-deterministic algebra'. Denition 1.11. Let be an operator alphabet. A non-deterministic algebra is a set A with, for every 2 k (k 0), a relation A A k A. 8

Denition 1.12. Let A be a non-deterministic -algebra. The subset algebra P(A) is the (deterministic) algebra consisting of the set P(A) of subsets of A, with for every 2 k, and A 1 ; : : : ; A k A P(A) (A 1 ; : : : ; A k ) = A (A 1 ; : : : ; A k ) (= f A (a 1 ; : : : ; a k ) j a i 2 A i for 1 i kg): Every algebra is accompanied by an interpreting function, the valuation function `val'. First we give the valuation for terms without variables. Denition 1.13. Given an operator alphabet and a -algebra A, the function val A : T! A is dened as follows. For every 2 k (k 0) and t 1 ; t 2 ; : : : ; t k 2 T, val A (t 1 t 2 t k ) = A (val A (t 1 ); val A (t 2 ); : : : ; val A (t k )) The valuation of terms with variables depends on the value given to the variables. So, a term with variables needs a variable assignment, i.e., a function N! A. A term over T (N) denes a function (N! A)! A. Denition 1.14. Let be an operator alphabet, N a set of variables, t 2 T (N), and : N! A. Then, 8 < A (val A (t 1 )(); : : : ; val A (t k )()) if t = t 1 t k for some 2 k (k 0), val A (t)() = : (t) otherwise (t 2 N) Note that if t 2 T (N), then t 2 T (N 0 ) for any N 0 N. Also, if t 2 T (N) and : N! A then val A (t)() = val A (t)( 0 ) for any 0 : N 0! A with 0 N =. Example 1.15. Consider the operator alphabet (; rk ), where = 0 [ 2, with 0 = f0; 1; 2g, and 2 = f+g. Some terms over are 0, +01, and +2+11. Mind the prex notation. An example of a -algebra is N, with 0 N = 0, 1 N = 1, 2 N = 2 and + N = x; y:x+y, addition of natural numbers. Then, val N (0) = 0 N = 0, and val N (+01) = + N (val N (0); val N (1)) = val N (0) + val N (1) = 0 N + 1 N = 0 + 1 = 1. Similarly, val N (+2+11) = 4. Another -algebra is A = f; ; g, with 0 A =, 1 A =, 2 A = and + A = x; y:x y, concatenation of strings. Now, val A (0) = 0 A =, val A (+01) = =, and val N (+2+11) = ( ) =. Introducing variables, +0+1 is a term from T (fg). If we assign a value from the algebra to, we can evaluate the term. For example, using algebra N, and assigning 5 to, we obtain val N (+2+1)[ 7! 5] = val N (2)[ 7! 5] + val N (+1)[ 7! 5] = 2 + (val N (1)[ 7! 5] + val N ()[ 7! 5]) = 2 + (1 + 5) = 8. We can consider T (N) to be an algebra itself, with very simple operations: 9

Denition 1.16. Let be an operator alphabet and N a nite set of variables, then T (N) is the term -algebra generated by N, in which for all k 0, 2 k and t 1 ; : : : ; t k 2 T (N), T (N)(t 1 ; : : : ; t k ) = t 1 t k : This may not seem very useful, but we can use it to dene substitution of terms for variables. Denition 1.17. Let be an operator alphabet, N a nite set of variables, t 2 T (N), and : N! T (N), a substitution function. Then, is the result of applying to t. t = val T (N)(t)() Applying to t has the result of substituting () for every occurrence of in t, for every 2 N, leaving the rest of t unchanged. This is easily proven by induction on the structure of t. We can now show that, in a way, val A is distributive over substitution. In the next proposition, we omit the round brackets around an enumerated function. We will do this more often. Proposition 1.18. For any -algebra A, variable set N = f 1 ; : : : ; n g, variable assignment : N! A, and terms t; s 1 ; : : : ; s n 2 T (N), val A (t[ 1 7! s 1 ; : : : ; n 7! s n ])() = val A (t)[ 1 7! val A (s 1 )(); : : : ; n 7! val A (s n )()] Proof. In this proof, we use the shorthand [ i 7! s i ] for [ 1 7! s 1 ; : : : ; n 7! s n ] and [ i 7! val A (s i )()] for [ 1 7! val A (s 1 )(); : : : ; n 7! val A (s n )()]. We prove the proposition by induction on the structure of t. Base: Let t = j 2 N. Then, val A ( j [ i 7! s i ])() = val A (s j )() [by denition of substitution] = val A ( j )[ i 7! val A (s i )()] [by denition of val] Induction: Let t = t 1 t k, where k 0 and 2 k. Then, val A (t 1 t k [ i 7! s i ])() = [by def. of substitution] val A (t 1 [ i 7! s i ] t k [ i 7! s i ])() = [by denition of val] A (val A (t 1 [ i 7! s i ])(); : : : ; val A (t k [ i 7! s i ])()) = [induction hypothesis] A (val A (t 1 )[ i 7! val A (s i )()]; : : : ; val A (t k )[ i 7! val A (s i )()]) = [by denition of val] val A (t 1 t k )[ i 7! val A (s i )()] As a special case of this, if s 1 ; : : : ; s n 2 T, then val A (t[ 1 7! s 1 ; : : : ; n 7! s n ]) = val A (t)[ 7! val A (s 1 ); : : : ; n 7! val A (s n )]. For a more thorough description of algebras see [Coh81]. 10

1.4 Graphs and Trees Graphs Denition 1.19. Let and be alphabets. A graph over (; ) is a quadruple (V; E; nlab; elab), with V a nite set of nodes and E V V the set of edges, nlab : V! is the node-labelling function and elab : E! is the edge-labelling function. The set of all graphs over (; ) is denoted GR(; ). We say that G is a graph if there are and, such that G is a graph over (; ). So, we consider nite, directed graphs with no multiple edges. Loops are allowed. is the alphabet of node labels and is the alphabet of edge labels. For a given graph G, its nodes, edges, node-labelling function and edge-labelling function are denoted V G ; E G ; nlab G, and elab G respectively. Sometimes we do not need edge labels and node labels and we simply leave them out. If G is a graph, we say that an edge (with label l) runs from node v to node w if (v; w) 2 E G (and elab G (v; w) = l). This is denoted v! G w (v! l G w). We say that the edge is outgoing from v and incoming on w. A sequence (v 0 ; v 1 ; : : : ; v n ) 2 V + is a (directed) path of length n, running from v 0 to v n, if (v i ; v i+1 ) 2 E for i 2 [0; n 1]. The length of a path can be 0, so for every node v, there is a path from v to v. We say that a graph G is cyclic if there exists a path (v 0 ; v 1 ; : : : ; v n ; v 0 ), for some n 1. Denition 1.20. Two graphs G and H over (; ) are isomorphic if there exists a bijection f : V G! V H, such that for all v; w 2 V G : (v; w) 2 E G i (f(v); f(w)) 2 E H, nlab G (v) = nlab H (f(v)) and elab G (v; w) = elab H (f(v); f(w)). A graph is automorphic if it is isomorphic to itself through a function f 6= id VG. We want to be able to `cut a piece' out of a graph and obtain a subgraph. Denition 1.21. Let G be a graph and V 0 V G. The graph H induced by V 0 is a subgraph of G, with V H = V 0, E H = E G \ (V 0 V 0 ), nlab H = nlab G V H and elab H = elab G E H. Forests and Trees Forests and trees are specic kinds of graphs. The trees and forests we consider are rooted and ordered. Denition 1.22. Let be an operator alphabet and = rks(). A graph G over (; ) is a forest over if it is acyclic, no node has more than one incoming edge, and 11

for every node v and for every i 2 [1; rk(nlab G (v))], there is exactly one edge outgoing from v with label i, and v has only outgoing edges with labels in [1; rk(nlab G (v))]. A tree over is a forest over consisting of one connected component. The set of all trees is denoted trees. In a forest, a node without incoming edges is called a root. A forest has as many roots as it has connected components, so a tree t has only one root, denoted root(t). Thus, a forest is a disjoint union of trees. Edges are sometimes called branches and nodes with no outgoing edges are called leaves. Each node of a forest has a rank equal to the rank of its label: rk(u) = rk(nlab(u)) for each node u. The depth or level of a node u is the length of the (unique) path from a root to u. The depth of a forest is the maximum of the depths of its nodes. For any nodes v and w, if an edge runs from v to w (with label i), w is called the (i-th) child of v and v is the parent of w. We use Dewey notation and denote the i-th child of v by v i. Also, it is convenient to dene v 0 = v. If a path runs from v to w, then v is an ancestor of w and w is a descendant of v. In particular, every node is both its own ancestor and its own descendant. A proper ancestor (descendant) of v is an ancestor (descendant) of v not equal to v itself. In a tree, every pair of nodes u and v has common ancestors. The least common ancestor of u and v, denoted lca(u; v) is the common ancestor with the highest depth (closest to nodes u and v). For a forest G over and a node v of G, the subtree of G rooted in v, denoted by sub G (v) is the subgraph of G induced by v and all its descendants. Note that sub G (v) is a tree over with root v. For a tree t over and a node v of t, the context of v, denoted ctx t (v), is the subgraph induced by v and all nodes that are not a descendant of v, in which the label of v is changed into, a xed variable. Thus, ctx t (v) is a tree over (fg). The yield of a tree t over is the string yield(t) over 0, consisting of the concatenation of the labels of the leaves of t in left-to-right order. Trees and Terms It should be apparent that terms have a tree structure and vice-versa. We now formalize that correspondence, and hereafter simply identify terms with trees. Denition 1.23. For any operator alphabet, tree is the bijection from T to the set of trees over (modulo isomorphism), recursively dened as follows. Let t = t 1 t k, for some k 0, 2 k, and t 1 ; : : : ; t k 2 T. Then tree(t 1 t k ) = (V; E; nlab; elab); with V = fvg [ [ V tree(ti ) i2[1;k] 12

(where we assume that the V tree(ti ) are mutually disjoint, and that v 62 S i2[1;k] V tree(ti )), E = n v; root(tree(t i )) j i 2 [1; k] o [ [ nlab = n (v; ) o [ [ 1.5 Tree Automata i2[1;k] nlab tree(ti ), and i2[1;k] elab = n v; root(tree(t i )) ; i j i 2 [1; k] o [ [ E tree(ti ), i2[1;k] elab tree(ti ) : A tree automaton denes a tree language, i.e., a set of trees. We consider only nite state, bottom-up tree automata. Denition 1.24. Let be an operator alphabet. A deterministic tree automaton over is a tuple A = (Q; F ), where Q is a nite -algebra and F Q. The language recognized by the tree automaton (Q; F ) is L(A) = ft 2 T j val Q (t) 2 F g: A more operational description may be given in terms of automata theory. Elements of Q are called states and F is the set of nal states. A tree automaton A starts reading at each leaf of the tree, in a state determined by the label of that leaf. It then works its way up in the tree. When a node v has label 2 k, the automaton will reach state Q (s 1 ; : : : ; s k ) at v, where s i is the state of A at the i-th child of v. A tree is accepted if the automaton reaches a nal state in the root of the tree. A tree language L is called recognizable or regular if there is a tree automaton that recognizes L. The set of all regular tree languages is denoted regt. Denition 1.25. Let be an operator alphabet. A non-deterministic tree automaton over is a tuple A = (Q; F ), where Q is a non-deterministic nite -algebra and F Q is the set of nal states. The language recognized by A is L(A) = ft 2 T j val P(Q) (t) \ F 6= ;g: If you wish, val P(Q) (sub t (v)) gives the states in which the automaton can arrive at v, and the automaton succeeds if it can arrive at the root in a nal state. Non-deterministic tree automata recognize exactly the same tree languages as deterministic tree automata do. See [GS84] for this result and a more comprehensive introduction to tree automata. Proposition 1.26. For a tree automaton A over the following are decidable: 1. t 2 L(A), for any t 2 T, 13

2. L(A) = ;, and 3. L(A) = T. 1.6 Attribute Grammars Attribute grammars were introduced by [Knu68], as a way to assign a semantics to a context-free language. An overview and extensive bibliography can be found in [DJL88]. To simplify the comparison with other formal models, our denition of attribute grammars is a bit dierent from Knuth's original. 1.6.1 Denition As in [Ful81], attribute grammars act on trees over an operator alphabet, instead of parse trees of an underlying context-free grammar. The semantic rules are grouped by operator, instead of by grammar rule. All operators have the same set of attributes, and there are special rules for the inherited attributes of the root. Moreover, for each operator one semantic condition is given. Denition 1.27. An attribute grammar over is a six-tuple where is an operator alphabet; G = h; ; B; R; C; mean i; is a nite set of sets, the semantic domains of the attributes; B = hs; I; W i, is the attribute description. Here, { S is a nite set, the set of synthesized attributes, { I, disjoint with S, is a nite set, the set of inherited attributes, { W : (I [ S)! is the domain assignment. We also use A for I [ S, the set of all attributes; R = hr int ; R root i describes the semantic rules. Here, { R int is a function associating with every 2 a set of internal semantic rules. For every, R int () contains one rule h 0 ; i 0 i = f(h 1 ; i 1 i; : : : ; h k ; i k i); for every pair h 0 ; i 0 i, where either 0 is a synthesized attribute and i 0 = 0, or 0 is an inherited attribute and i 0 2 [1; rk()]. Furthermore, k 0, 1 ; : : : ; k 2 A, 14

i 1 ; : : : ; i k 2 [0; rk()], f is a function from W ( 1 ) W ( k ) to W ( 0 ), and the h j ; i j i are mutually distinct. Usually, we simply write R() for R int (); { R root is the set of semantic root rules; R root contains one rule h 0 ; 0i = f(h 1 ; 0i; : : : ; h k ; 0i); for every 0 2 I, where k 0, 1 ; : : : ; k 2 A, f is a function from W ( 1 ) W ( k ) to W ( 0 ), and the h j ; i j i are mutually distinct; C is a function associating with every 2 a semantic condition C() of the form f(h 1 ; i 1 i; : : : ; h k ; i k i) where k 0, 1 ; : : : ; k 2 A, i 1 ; : : : ; i k 2 [0; rk()], and f is a function from W ( 1 ) W ( k ) to B ; mean 2 S is the attribute giving the meaning of a tree. The semantic conditions can be left out if they are all tautologies. An attribute grammar without semantic conditions is called an unconditional attribute grammar. Also, mean can be left out if it is not needed. If all sets in are nite, then G is said to have nite semantic domains. Usually, for an (internal or root) rule h 0 ; i 0 i = f(h 1 ; i 1 i; : : : ; h k ; i k i); the function f is given as f = x 1 ; : : : ; x k :e for some expression e with variables in fx 1 ; : : : ; x k g. We will then informally denote the rule by h 0 ; i 0 i = e 0 ; where e 0 is obtained from e by substituting h j ; i j i for x j for all j. For an operator 2, we dene the set A() of attributes of as fh; ii j 2 A and i 2 [0; rk()]g. If h 0 ; i 0 i = f(h 1 ; i 1 i; : : : ; h k ; i k i) is a rule in R(), we say h 0 ; i 0 i is dened in terms of h 1 ; i 1 i; : : : ; h k ; i k i (at ). Likewise, if h 0 ; 0i = f(h 1 ; 0i; : : : ; h k ; 0i) is a rule in R root, then h 0 ; 0i is said to be dened in terms of h 1 ; 0i; : : : ; h k ; 0i (at the root). For attributes and in A, if there are i; j such that h; ii is dened in terms of h; ji at some, or at the root, then we say is dened in terms of. Remark 1.28. We do not require Bochmann normal form [Boc76]. 15

1.6.2 Semantic Instructions and Tests Let t be a tree over. The set of attributes of t is A(t) = A V t : Denition 1.29. For every tree t 2 T, and node u 2 V t, if nlab(u) =, and is a rule in R(), then h 0 ; i 0 i = f(h 1 ; i 1 i; : : : ; h k ; i k i) h 0 ; u i 0 i = f(h 1 ; u i 1 i; : : : ; h k ; u i k i) is an internal semantic instruction of t. Analogously, if h 0 ; 0i = f(h 1 ; 0i; : : : ; h k ; 0i) is a root rule of t, then h 0 ; root(t)i = f(h 1 ; root(t)i; : : : ; h k ; root(t)i) is a semantic root instruction of t. Likewise for the semantic conditions: if C() = f(h 1 ; i 1 i; : : : ; h k ; i k i)), then f(h 1 ; u i 1 i; : : : ; h k ; u i k i)) is a semantic test of t. The set of all internal semantic instructions of t is R int (t). The set of all semantic root instructions is R root (t). The set of all semantic instructions of t is R(t) = R int (t) [ R root (t). The set of all semantic tests of t is C(t). So, semantic rules and conditions are associated with labels, and instructions and tests are associated with nodes of a tree. 1.6.3 Decoration We dene how to give the correct values to the attributes of the tree. Denition 1.30. Let be an operator alphabet, G an attribute grammar over, and t a tree over. Let dec be a function from A(t) to [, such that dec(h; ui) 2 W () for all h; ui 2 A(t). The function dec is a decoration of t if the following two conditions hold. 1. All semantic root instructions are obeyed, i.e., for every instruction in R root (t), h 0 ; root(t)i = f(h 1 ; root(t)i; : : : ; h k ; root(t)i) dec(h 0 ; root(t)i) = f(dec(h 1 ; root(t)i); : : : ; dec(h k ; root(t)i)): 2. The internal semantic instructions are obeyed, i.e., for every instruction in R int (t), h 0 ; u i 0 i = f(h 1 ; u i 1 i; : : : ; h k ; u i k i) dec(h 0 ; u i 0 i) = f(dec(h 1 ; u i 1 i); : : : ; dec(h k ; u i k i)): 16

Moreover, dec is a valid decoration if the following condition holds: 3. The semantic tests are passed, i.e., for every test f(h 1 ; u i 1 i; : : : ; h k ; u i k i) in C(t), we have f(dec(h 1 ; u i 1 i); : : : ; dec(h k ; u i k i)) = true : Note that the rst two conditions can be merged to the following: for every instruction h 0 ; u 0 i = f(h 1 ; u 1 i; : : : ; h k ; u k i) in R(t), dec(h 0 ; u 0 i) = f(dec(h 1 ; u 1 i); : : : ; dec(h k ; u k i)): 1.6.4 Weak Non-circularity We introduce a condition that implies that an attribute grammar has a unique decoration on every tree. This criterion is a bit more relaxed than the classical concept of noncircularity, but it suers from a computability problem. For classical non-circularity [Knu68], we demand that the dependency graph is acyclic for every tree. For some of our purposes, specically in Chapter 4 (where we consider conditional semantic rules in combination with decomposition in phases), this condition is too conservative. Therefore we introduce the concept of a weak-dependency graph, that has edges corresponding to `real' dependencies only. Denition 1.31. A function f depends on its ith argument if 9a 1 ; : : : ; a i 1 ; a; a 0 ; a i+1 ; : : : ; a k : f(a 1 ; : : : ; a i ; : : : ; a k ) 6= f(a 1 ; : : : ; a 0 i ; : : : ; a k): For example, the function a; b; c:(a + b)=c does depend on it's third argument, but the function a; b; c:a + b does not depend on its third argument. It is undecidable whether f depends on its ith argument. This follows from Rice's theorem (`every non-trivial property of a computable function is undecidable'), see e.g. [Wei87]. In a weak-dependency graph we depict how one attribute of a node depends on another. Denition 1.32. For an attribute grammar G, the weak-dependency graph of a tree t over is the unlabelled graph WD G (t) = (V; E), where V = A(t) E = f(h; ui; h 0 ; u 0 i) j 9i : (h 0 ; u 0 i = f(: : : ; h; ui; : : : )) 2 R(t) i and f depends on its ith argumentg: 17

Now, we call an attribute grammar G weakly non-circular if for every t, WD G (t) is acyclic. Unfortunately, because it is undecidable whether f depends on its ith argument, the weak-dependency graph is not computable, and weak non-circularity is in general not decidable. In the special case of attribute grammars with nite semantic domains, however, weak non-circularity is decidable, because dependencies are computable for nite functions. Note that weak non-circularity is indeed implied by classical non-circularity. Proposition 1.33. If an attribute grammar G is weakly non-circular, then for every t, there is exactly one decoration of t. The truth of this proposition can most easily be seen by the fact that any weakly noncircular ag can be changed into a classically non-circular ag with the same decorations, in the following straightforward manner. For every semantic rule h 0 ; i 0 i = f(h 1 ; i 1 i; : : : ; h k ; i k i) and j 2 [1; k], we remove attribute h j ; i j i if f does not depend on j (and changing f accordingly). This does not change the eect of the rules, but makes the attribute grammar classically non-circular. Weak non-circularity only makes a real dierence if we use a decomposition in phases, dened below in Subsection 1.6.7. There, we will present an example in which weak non-circularity really diers from classical non-circularity. If t has a unique decoration, we denote it by dec G;t. We will also write h; ui G;t for dec G;t (h; ui). We will leave out the subscripts whenever they are clear from context. Note that weak non-circularity is a sucient, but not a necessary condition for uniqueness of the decoration, see for example [CM79]. Denition 1.34. A weakly non-circular attribute grammar G denes a partial function G : T * W ( mean ), as follows G(t) = dec G;t (h mean ; root(t)i); if dec G;t is valid. For weakly non-circular unconditional attribute grammars, G is a total function (because in that case, all decorations are valid). 1.6.5 Weak Single Use Requirement The `weak single use requirement' [Gie88] states that in no tree an attribute should be used more than once. We will use this in Chapter 4 to limit the power of tree transducers. Denition 1.35. An AG G over is wsur if for every t 2 T, WD G (t) has no node with more than one outgoing edge. Wsur is in general not decidable, because dependencies are not computable. 18

1.6.6 Computing the decoration We show here how we can compute a valid decoration bottom-up, in a non-deterministic way, if one exists. This is not the way it is usually done, but we need this for Chapter 2, where we will have a tree automaton simulate an attribute grammar. See [Eng84], but also [FV95] for the usual ways to compute decorations. Denition 1.36. Let be an operator alphabet, G a weakly non-circular attribute grammar over and t a tree over. A top-less decoration of t is a function dec assigning to every h; ui 2 A(t) a value dec(h; ui) 2 W (), such that conditions 2 and 3 of Denition 1.30 are satised. Loosely speaking, a top-less decoration is a valid decoration that does not necessarily satisfy the root rules. A tree can have more than one top-less decoration (given an ag). Intuitively, this is because the inherited attributes of the root are not prescribed. However, a top-less decoration that satises condition 1 of Denition 1.30, is a valid decoration. Using top-less decorations we can build a valid decoration bottom-up. We will have to do this non-deterministically, guessing the right top-less decoration out of the possible ones, on every step up. The following lemma shows how to make a step up in the tree. Lemma 1.37. Let t = t 1 t k be a tree over, and G a weakly non-circular ag over. A function dec is a top-less decoration of t i the following conditions hold: dec A(t i ) is a top-less decoration of t i, for all i 2 [1; k], for every rule in R(), h 0 ; i 0 i = f(h 1 ; i 1 i; : : : ; h r ; i r i) dec(h 0 ; root(t) i 0 i) = f dec(h 1 ; root(t) i 1 i); : : : ; dec(h r ; root(t) i r i) ; and if C() = f(h 1 ; i 1 i; : : : ; h r ; i r i), then f dec(h 1 ; root(t) i 1 i); : : : ; dec(h r ; root(t) i r i) = true : The straightforward proof is omitted. 1.6.7 Decomposition in Phases In an attribute grammar, there may be a (natural) order in the attributes. Sometimes the value of one attribute does not depend on the value of another. Then, the value of all occurrences of one attribute can be evaluated before the value of any occurrence of another attribute is known. In such a case, the evaluation of the attributes can be decomposed into phases. 19

Denition 1.38. Let G = h; ; B; R; C; mean i be a not necessarily weakly non-circular attribute grammar. An ordered partition P = (A 1 ; : : : ; A n ) of A is a decomposition in phases if for all ; 2 A, if is dened in terms of, and 2 A k, then 2 A j for some j k. If P is a decomposition in phases, we dene phase P () = p i 2 A p. A decomposition in phases not only separates the attributes, but also the evaluation of the attributes. We can rst evaluate the attributes in phase 1, then the attributes in phase 2, and so on, resulting in the original decoration (if it exists). For every phase p 2 [1; n] and tree t 2 T, we have the set of tree attributes A p (t) = A p V t. Furthermore, we have the sets of semantic instructions R p int(t), R p root(t), and R p (t), and the decoration dec p G;t (if it exists). We will dene them using simultaneous induction on p, assuming dec q G;t exists for all q < p. First, R p int(t) consists of the instructions in R int (t) with left-hand side h; ii for certain i and 2 A p, where dec q (h; ji) takes the place of h; ji, for all j and with phase() = q < p. Technically, if and 0 2 A p, then h 0 ; u 0 i = f(h 1 ; u 1 i; : : : ; h k ; u k i) 2 R int (t); h 0 ; u 0 i = f 0 (h 1 ; u 1 i; : : : ; h k ; u k i) 2 R p int(t); where for all w 1 2 W ( 1 ); : : : ; w k 2 W ( k ), f 0 (w 1 ; : : : ; w k ) = f(x 1 ; : : : ; x k ), where 8 < w l if phase( l ) p, x l = : dec q (h l ; i l i) if phase( l ) = q < p. Note that in the second case, f 0 does not depend on its lth argument. Analogously we dene R p root(t), and last, R p (t) = R p int(t) [ R p root(t). A phase-p decoration is dened like in Denition 1.30: it is a function from A p (t) to S (that assigns values from the appropriate semantic domains) such that 1. all semantic root instructions in R p root(t) are obeyed, and 2. all internal semantic instructions in R p int(t) are obeyed. If t has a unique a phase-p decoration, it is denoted dec p G;t. Note that dec p G;t exists for all p i dec G;t exists, and that if that is the case, dec G;t = S p2[1;n] dec p G;t. Note that for any p, phases 1 up to p constitute an attribute grammar in its own right. The fact that we have attributes and instructions for every phase means that we can dene a weak-dependency graph for every phase. 20

Denition 1.39. For ag G with decomposition in phases P = (A 1 ; : : : ; A n ), the phase-p weak-dependency graph of a tree t over is the unlabelled graph WD p G;P (t) = (V; E), where V = A p (t) E = f(h; ui; h 0 ; u 0 i) j 9i : h 0 ; u 0 i = f(: : : ; h; ui; : : : ) 2 R p (t) i and f depends on its ith argumentg: This means the concepts of weak non-circularity and wsur generalize naturally to phases. Denition 1.40. Let G be a (not necessarily weakly non-circular) attribute grammar, and P a decomposition in phases of G. Attribute grammar G is phase-p wsur with respect to P if for every tree t, no node in WD p (t) has more than one outgoing edge. It is phase-p weakly non-circular with respect to P if for every t, WD p (t) is acyclic. If G is phase-p weakly non-circular for every phase p, G is weakly non-circular with respect to P. Lemma 1.41. If an attribute grammar G is weakly non-circular with respect to a decomposition in phases P, then, for every t, there is exactly one decoration of t. The proof is analogous to the classical proof that non-circular attribute grammars induce a unique decoration on every tree. Note that if an attribute grammar is weakly non-circular, it is weakly non-circular with respect to any decomposition in phases, because WD p G;P is a subgraph of WD G (t). The opposite need not hold: an attribute grammar can be non-circular with respect to a decomposition in phases while it is not non-circular. As shown in the next example, this happens in particular when conditional semantic rules are used (as will be done in Chapter 4). Example 1.42. Consider the attribute grammar G = h; ; (S; I; W ); Ri, with = 0 [ 1, 0 = f; #g, 1 = fg, = fng, S = f; ; g, I = ;, W maps every element of S to N, and the semantic rules are as follows. The set of rules R() is The set of rules R(#) is h; 0i = 0 h; 0i = 0 h; 0i = 0 h; 0i = 1 h; 0i = 1 h; 0i = 1 21

1 1 1 1 1 1 # # 1 1 1 # Figure 1.2: The dependency graph of #, its phase-1 dependency graph and its phase-2 dependency graph The set of rules R() is 8 < h; 0i if h; 1i = 0 h; 0i = : 1 otherwise 8 < 0 if h; 1i = 0 h; 0i = : h; 0i otherwise h; 0i = h; 1i The leftmost picture in Figure 1.2 shows the weak-dependency graph WD(t) of the tree t = #. Clearly, the attribute grammar is circular. We can however decompose it, with decomposition P = (fg; f; g). If we do this, we see that the weak-dependency graphs of both phases are acyclic, for example, in the case of tree t, the phase-1 and phase-2 dependency graphs WD 1 (t) and WD 2 (t) are depicted in Figure 1.2. Note that the value of is known in the second phase, and hence in both the rules for and, one of the cases falls away, which means that some dependencies no longer exist. It follows that G is weakly non-circular with respect to P. Indeed, there is a unique decoration for every tree: A tree of the form has dec G (h; ui) = dec G (h; ui) = dec G (h; ui) = 0 for all nodes u, and a tree of the form # has dec G (h; ui) = dec G (h; ui) = dec G (h; ui) = 1 for all nodes u. 1.7 Monadic Second Order Logic on Graphs Monadic second order logic is used to describe properties of graphs. We will informally introduce it here. The interested reader is refered to [Cou90, Eng91, Oos89]. 22

Syntax For alphabets and, we use the language MSOL(; ), of monadic second order (mso) formulas over (; ). Formulas in MSOL(; ) describe properties of graphs over (; ). This logical language has node variables x; y; : : :, and node-set variables X; Y; : : :. For a given graph G over (; ), node variables range over the elements of V G, and node-set variables range over the subsets of V G. There are three types of atomic formulas in MSOL(; ): 1. lab (x), for every 2, denoting that x has label, 2. edg (x; y), for every 2, denoting that an edge labelled runs from x to y, and 3. x 2 X, denoting that x is an element of X. The formulas are built from the atomic formulas using the connectives ^ (conjunction), _ (disjunction), : (negation), and! (implication), as usual. We can quantify both node variables and node-set variables, using the quantiers 9 and 8. The quantiers and negation bind more strongly than the binary connectives do. For every n, the set of mso formulas over (; ) with n free node variables and no free set variables is denoted MSOL n (; ). Because we are predominantly interested in trees, an mso formula over (; rks()) will also simply be called an mso formula over (where is an operator alphabet). Also, MSOL(; rks()) and MSOL n (; rks()) will be abbreviated to MSOL() and MSOL n (), respectively. Semantics For a closed formula 2 MSOL 0 (; ) and a graph G 2 GR(; ), we write G j= if G satises. Given a graph G, a valuation b is a function that assigns to each node variable an element of V G, and to each node-set variable a subset of V G. We write (G; b) j=, if holds in G, where the free variables of are assigned values according to the valuation b. If a formula has free variables, say, x; X; y and no others, we also write (x; X; y). If it is not ambiguous, we write (G; u; U; v) j= (x; X; y) for (G; [x 7! u; X 7! U; y 7! v]) j= (x; X; y), listing the values for the variables in alphabetical order of the variables (capital letters come immediately after their lower-case counterparts). An mso formula denes a relation on the nodes of any graph. Denition 1.43. Given a graph G 2 GR(; ), a formula (x 1 ; : : : ; x m ) 2 MSOL m (; ) denes a relation R G () = f(u 1 ; : : : ; u m ) 2 V m G j (G; u 1 ; : : : ; u m ) j= (x 1 ; : : : ; x m )g: Associated with an mso formula are its graph and tree semantics. 23

Denition 1.44. Let ; be alphabets, m 0, and (x 1 ; : : : ; x m ) 2 MSOL m (; ). The graph semantics of (x 1 ; : : : ; x k ) is L GR () = f(g; u 1 ; : : : ; u m ) j G 2 GR(; ); u 1 ; : : : ; u m 2 V G ; and (G; u 1 ; : : : ; u m ) j= (x 1 ; : : : ; x m )g; and, if is an operator alphabet and = rks(), the tree semantics of (x 1 ; : : : ; x k ) is L() = L GR () T : For any ;, a graph language L GR(; ) is called mso denable if there is a closed formula such that L GR () = L. Analogously, for any, a tree language L T is called mso denable if there is a closed formula such that L() = L. The set of all mso denable tree languages is denoted msot. It is equal to the set of regular tree languages. Proposition 1.45. [Don70, TW68] msot = regt Because the proof of this lemma is constructive, from Proposition 1.26 we can conclude the following. Proposition 1.46. The following are decidable for any formula 2 MSOL 0 (): 1. Membership: t 2 L(), 2. is a contradiction: L() = ;, and 3. is a tautology: L() = T. More Notation We use syntactic substitution on formulas, in the same manner as we did for terms in Section 1.3. We will also abbreviate substitution. For example, if has free variables x; y, we write (z; y) for [x 7! z; y 7! y], listing the substitutes in alphabetical order of the substituted variables. If A = fa 1 ; : : : ; a n g is a xed set and [a 7! a j ] is an MSO formula for all j, we write W a2a, or 9a 2 A :, for [a 7! a 1 ] [a 7! a n ]. Also, we write V a2a, or 8a 2 A : for [a 7! a 1 ] ^ ^ [a 7! a n ]. For example, 9a 2 f; g edg a (x; y) denotes edg (x; y) _ edg (x; y). We use some more abbreviations of formulas in MSOL(; ), listed in Table 1.1. The meaning of most of them is obvious. The abbreviation x = y k, for a constant k means that x is the kth child of y if k > 0, and x = y if k = 0. If (G; U) j= closed (x;y) (X), then U is closed under R G (). The formula path (x; y) states that x is connected to y through 24

abbreviation stands for $ (! ) ^ (! ) x = y 8X : (x 2 X $ y 2 X) x 6= y :(x = y ) false 9x : x 6= x true : false 9!x : 9x( ^ 8y([x 7! y]! y = x)) x = y k closed (x;y) (X) x = y if k = 0, and edg k (y; x) if k 2 N+ 8x; y (x 2 X ^ (x; y))! y 2 X udg (x; y) edg (x; y) _ edg (y; x) (undirected edge) edg(x; y) 9 2 : edg (x; y) udg(x; y) 9 2 : udg (x; y) path(x; y) 8X : ((x 2 X ^ closed edg(x;y) (X))! y 2 X) path (x; y) 8X : ((x 2 X ^ closed edg (x;y)(x))! y 2 X) acpath(x; y) 9X : (x 2 X ^ 8z 2 X(z = y _ 9z 0 2 X : edg(z; z 0 ))) upath(x; y) 8X : ((x 2 X ^ closed udg(x;y) (X))! y 2 X) root(x) :9y : edg(y; x) leaf(x) :9y : edg(x; y) Table 1.1: Abbreviations for mso a path of edges with label only. In an acyclic graph, acpath is equivalent to path. The abbreviations root(x) and leaf(x) mean that x is a root (no incoming edges) or leaf (no outgoing edges) respectively. Transitive, Reexive Closure For any formula (x; y), we dene the formula (x; y) = 8X((x 2 X ^ closed (x;y) (X))! y 2 X): The relation dened by (x; y) denes the transitive, reexive closure of the relation dened by (x; y) ([Cou90]). Lemma 1.47. Let ; be alphabets, G 2 GR(; ), and (x; y) 2 MSOL 2 (; ). Then, R G ( ) = R G (). Proof. First we prove (u; v) 2 R G ( ) ) (u; v) 2 R G (), and then we prove (u; v) 2 R G () ) (u; v) 2 R G ( ). Suppose (G; u; v) j= 8X : ((x 2 X ^ closed (x;y) (X))! y 2 X). The set U = fw 2 V G j (u; w) 2 R G () g is closed under R G (). So, since u 2 U, it follows that v 2 U, and thus (u; v) 2 R G (). 25