sequentiality, second order monadic logic and tree automata

23

Upload: lamngoc

Post on 31-Dec-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sequentiality, Second Order Monadic Logic and Tree Automata

Sequentiality, Second Order Monadic Logic and Tree AutomataHubert ComonCNRS and LRI, Bat. 490, Universit�e de Paris Sud, 91405 ORSAY cedex, France.E-mail [email protected] a term rewriting system R and a normalizable term t, a redex is needed if inany reduction sequence of t to a normal form, this redex will be contracted. Roughly, Ris sequential if there is an optimal reduction strategy in which only needed redexes arecontracted. More generally, G. Huet and J.-J. L�evy de�ne in [9] the sequentiality of apredicate P on partially evaluated terms. We show here that the sequentiality of P isde�nable in SkS, the second-order monadic logic with k successors, provided P is de�nablein SkS. We derive several known an new consequences of this remark: 1{strong sequentiality,as de�ned in [9], of a left linear (possibly overlapping) rewrite system is decidable, 2{ NV-sequentiality, as de�ned in [17] is decidable, even in the case of overlapping rewrite systems3{ sequentiality of any linear shallow rewrite system is decidable.Then we describe a direct construction of a tree automaton recognizing the set of termsthat do have needed redexes, which, again, yields immediate consequences: 1{ Strong se-quentiality of possibly overlapping linear rewrite systems is decidable in EXPTIME, 2{ Forstrongly sequential rewrite systems, needed redexes can be read directly on the automaton.1 IntroductionBesides con uence, there are two important issues concerning non-terminating computations interm rewriting theory. One is to �nd a normalizing reduction strategy, which has been inves-tigated in, e.g., [16, 11, 1]. The other is to �nd an optimal reduction strategy, for which onlyneeded redexes are contracted. This question was �rst investigated by Huet and L�evy in 1978[9]. They call sequential a rewrite system for which there exists such an optimal strategy. Wefocus here on the latter issue.A typical example is the \parallel or", whose de�nition contains the two rules > _ x ! >and x _ > ! >. Given an expression e1 _ e2, which of e1 and e2 should be evaluated �rst? Ife1 is tried �rst, its evaluation may be unnecessary because e2 evaluates to >, and the wholeexpression can be reduced to >. Hence, this strategy is not optimal. Evaluating e2 �rst is notoptimal either: there is no optimal (sequential) reduction strategy for the \parallel or".Given a term rewriting system R, can we decide whether R is sequential ? In case it is, is itpossible to compute (and compile) an optimal strategy? These questions have been addressed inseveral papers, starting with [9]. Unfortunately, the sequentiality of R is in general undecidable.In their landmark paper, Huet and L�evy introduce a su�cient criterion: strong sequentiality,and show that this property is decidable for orthogonal term rewriting systems, in which lefthand sides do not overlap nor contain repeated occurrences of a same variable. The originalproof is quite intricate. J.-W. Klop and A. Middeldorp [14] give a simpler proof to the price of1

Page 2: Sequentiality, Second Order Monadic Logic and Tree Automata

an increased complexity. The case of linear, possibly overlapping rewrite systems was considered�rst by Toyama [24] and later shown decidable by Jouannaud and Sad� [10]. M. Oyamaguchide�nes NV-sequentiality a property intermediate between sequentiality and strong sequentiality,which is also decidable for orthogonal rewrite systems [17].In this paper we use another quite simple approach, though less elementary: we show thatthe sequentiality of P is de�nable in SkS (resp. WSkS), the second-order monadic logic withk successors, provided that P is de�nable in SkS (resp. WSkS). It allows to easily derive allaforementioned decidability results. Relying on automata theory, the decidability of strong se-quentiality (resp. NV-sequentiality) of possibly overlapping left linear rewrite systems becomesstraightforward. This sheds new light on which properties of rewrite systems are indeed neces-sary in proving (NV-, strong-) sequentiality. Then, it becomes possible to derive new decidabilityresults, for example NV-sequentiality for overlapping left linear rewrite systems or sequentialityof shallow linear rewrite systems. We may also add a sort discipline to the rewrite systemswithout loosing decidability.This method has however several drawbacks: �rst, the complexity of SkS is non-ELEMENTARY,which is far too complex in general for \e�ective" methods. Second, for non-left linear rewritesystem, the reducibility predicate is not expressible in SkS. Hence we cannot derive that strongsequentiality of R is expressible in SkS in such a case. Last, but not least, even if we know thatthe formula expressing the sequentiality of R is valid, how do we e�ectively �nd needed redexesin a term?In order to answer these questions, we construct directly a tree automaton which accepts allterms that have a needed redex. By the well-known correspondence between WSkS and �nitetree automata (see e.g. the survey [23]), we know in advance that such an automaton exists.Here, we show that it can be constructed in exponential time (for k �xed). This has several con-sequences. First, deciding strong sequentiality of any left linear rewrite system is in EXPTIME,since it reduces to an emptiness problem for tree automata, which can be decided in polynomialtime. Then, the automaton which accepts all terms that have a needed redex yields directly thealgorithm for searching needed redexes in a term.There are still many issues to be investigated with pro�t in this framework: is strong se-quentiality decidable for any (possibly non-linear) rewrite systems? Though automata withconstraints [2, 3] cannot be used directly, we might consider some tree automata inspired bythese de�nitions. What is the exact complexity of all decision questions in this area? We haveonly shown and EXPTIME inclusion. However there is no evidence that this is the best we cando. Also, what happens in the case of orthogonal rewrite systems? The automata should havea particular form, from which it might be possible to deduce more e�cient procedures. Finally,we do not show how to compile an optimal reduction strategy, avoiding any backtrack in theinput term, as done in [9]. Again, this should be possible from the tree automaton. Finally,other (sequential) reduction strategies as in [1] should also be investigated within this framework.The paper is organized as follows: section 2 gives the de�nitions of an index and sequentialityand we recall the necessary background on SkS and tree automata. In section 3, we show howto express the sequentiality of a predicate P in SkS and apply this result to rewrite systems insection 4. In section 5 we construct directly the automaton accepting all terms that have anindex (using the characterization of [14]) and derive extensions as well as complexity results.We also explain how an index search can be read on the automaton.2

Page 3: Sequentiality, Second Order Monadic Logic and Tree Automata

2 Basic De�nitions2.1 TermsT is the set of terms built over a �xed alphabet F of function symbols. Each f 2 F comes withits arity a(f), a nonnegative integer. Terms may also be viewed as labeled trees, i.e. mappingsfrom a �nite pre�x-closed subset of words of positive integers (the positions in the tree) into F ,in such a way that the successors of a position p are exactly the strings p � i for 1 � i � a(f)when p is labeled with f . We will use the notations of [7]: tjp is the subterm at position p, t[u]pis the term obtained by replacing tjp with u. F is assumed to be �nite. 1T is the set of terms obtained by augmenting the set F of function symbols with a newconstant (which stands intuitively for \unevaluated terms"). We assume that terms in Talways contain at least one occurrence of . Such a set of terms is classically considered as theset of terms which are partially evaluated, i.e. terms in T which are \cut" on some branches.De�nition 2.1 Let t; u 2 T [ T, t v u i� u can be obtained from t by replacing some occur-rences of in t with terms in T.s v t intuitively means that \t is more evaluated than s".2.2 SequentialityDe�nition 2.2 (index,[9]) Let P be a predicate on T [ T . Let t 2 T and p 2 Pos(t). p isan index of P in t i� tjp = and8u 2 T [ T; (t v u ^ P (u) = true)) ujp 6= The set of indexes of a term t 2 T (which were also called needed redexes in the introduction)is written Index(t). Intuitively, p is an index for P in t if, for all successful evaluations of t (thepredicate P becomes true), the term at position p has been evaluated.De�nition 2.3 (sequentiality,[9]) A predicate P on T [ T is sequential if8t 2 T [ T; (9u 2 T [ T; P (u) = true ^ t v u)) (P (t) = true _ 9p 2 Pos(t); p 2 Index(t))Intuitively, P is sequential if, for every partially evaluated term t such that P is false and Pbecomes true for some further evaluation of t, then there is an index of P in t.1Note that if one wants to consider terms with possibly in�nitely many \variables" (actually constants, butwe use the standard terminology), it is always possible to represent the variables x0; x1; : : : ; xn; : : : using anadditional constant x and an additional unary function symbol s; they will be respectively represented byx;s(x); : : : ; s(s(: : : (s(x)) : : :)); : : :. In such a case, T is a regular subset of the set of all terms, but this doesnot cause any additional problem, as we will see later in the general case of typed terms. The status of variablesin T (or T) is di�erent from the status of the variables in the rewrite system: the former are actually consideredas constants along the evaluation process, while the latter may be instanciated since several distinct instances ofthe rules can be used. 3

Page 4: Sequentiality, Second Order Monadic Logic and Tree Automata

2.3 Term RewritingX is an in�nite set of constant function symbols called variables and the set of terms build onF [ X is traditionally written T (F ;X ). For any s 2 T (F ;X ), Var(s) is the set of variablesoccurring in s. Substitutions are mappings from X into T (F ;X ), which are extended intoendomorphisms of T (F ;X ). We use the post�x notation for substitution applications.A term rewriting system is a (�nite) set of pairs of terms in T (F ;X ), each pair (s; t) iswritten s ! t (we do not require Var(t) � Var(s)). A term t rewrites to s through a rewritesystem R, which is written t �!R s if there is a position p in t, a substitution � and a rulel ! r 2 R such that tjp = l� and s = t[r�]p. Rewriting using zero or many single rewritingsteps, i.e. the re exive transitive closure of �!R is written ��!R .2.4 Tree AutomataWe recall here some basic de�nitions about tree automata (see e.g. [8]).De�nition 2.4 A �nite (bottom-up) tree automaton consists of a ranked alphabet F , a �nite setof states Q, a subset Qf of �nal states and a set of transition rules of the form f(q1; : : : ; qn)! qwhere f 2 F , n = a(f) and q1; : : : ; qn; q 2 Q or q ! q0 where q; q0 2 Q (the latter transitionrules are called �-transitions). A tree automaton accepts t if t can be rewritten to a �nal stateusing the transition rules (see e.g. [8] for more details).De�nition 2.5 The language accepted by a tree automaton A is the set of terms t which areaccepted by A.A set L of trees is recognizable when there is a tree automaton A such that L is the languageaccepted by A.De�nition 2.6 A run of the automaton on a tree t is a mapping � from the positions of t intoQ such that �(p) = q; �(p � 1) = q1; : : :�(p �n) = qn and t(p) = f only if there is a transition rulef(q1; : : : ; qn)! q0 and a sequence of �-transitions from q0 to q. A run � is successful if �(�) isa �nal state. t is accepted by A i� there is a successful run of A on t.We will see in what follows several examples of recognizable sets of terms.2.5 The logic (W)SkSMissing de�nitions can be found in [23]. Terms of SkS are formed out of individual variables(x; y; z; : : :), the empty string � and right concatenation with 1; : : : ; k. Atomic formulas areequations between terms, inequations w < w0 between terms or expressions \w 2 X" where wis a term and X is a (second-order) variable. Formulas are built from atomic formulas usingthe logical connectives ^;_;);:; ::: and the quanti�ers 9; 8 of both individual and second-order variables. Individual variables are interpreted as elements of f1; : : : ; kg� and second-ordervariables as subsets of f1; : : : ; kg�. Equality is the string equality and inequality is the strictpre�x ordering. In the weak second-order monadic logic WSkS, second-order variables only rangeover �nite sets. Finite union an �nite intersection, as well as inclusion and equality of sets arede�nable in (W)SkS in an obvious way. Hence we may use these additional connectives in thefollowing.The most remarkable result is the decidability of SkS (a result due to M. O. Rabin, see e.g.[19, 23] for comprehensive surveys). The main idea of the proof is to associate each formula� whose free variables are X1; : : : ; Xn with a (Rabin) tree automaton which accepts the set4

Page 5: Sequentiality, Second Order Monadic Logic and Tree Automata

of n-uples of trees (or sets of strings) that satisfy the formula. Then decidability follows fromclosure and decidability properties of the corresponding class of tree languages. We only usehere the weak case, in which only �nite state tree automata are used. We will extensively usethe following without any further mention:Theorem 2.7 (Thatcher and Wright, 1969) A set of �nite trees is de�nable in WSkS i� itis recognized by a �nite tree automaton.Formally, this correspondence needs to de�ne a term in WSkS. We recall below how it canbe done.3 Relationship between Sequentiality and RecognizabilityLet k be the maximal arity of a function symbol in F and n be the cardinal of F . A term t isrepresented in WSkS using n+ 2 set variables X , X and Xf ; f 2 F (which will be written ~Xin the following). X will be the set of positions of t and X and each Xf will be the sets ofpositions that are labeled with the corresponding function symbol. We express in WSkS thatsome n+ 2-uple of �nite sets of words are indeed encoding a term, which can be achieved usingthe formula Term( ~X) def= X = X [ n[i=1Xfi^ i6=j(Xfi \Xfj = ; ^X \Xfi = ;)^ 8x 2 X; 8y < x; y 2 X^ ^f2F[fg8x 2 Xf ; a(f)l=1 x � l 2 X kl=a(f)+1x � l 62 XIn this setting, it is quite easy to express the sequentiality of P in (W)SkS as shown by thefollowing lemmas.Lemma 3.8 v is de�nable in WSkS.ProofAssume that t; u are represented by ~X and ~Y respectively. Then t v u i�X � Y ^ ^f2F;f 6=Xf � Yf 2Lemma 3.9 Let P be a predicate on T[T which is de�nable in (W)SkS. Then the set of termsin T which have an index w.r.t. P is de�nable in (W)SkS.ProofLet �( ~X) be the de�nition of P in (W)SkS. Then the set of terms which have an index is de�nedby translating de�nition 2.2:Index( ~X) def= Term( ~X) ^ 9x 2 X: x 2 X ^ 8~Y :(Term(~Y ) ^ ~X v ~Y ^ �(~Y ))) x 2 Y 25

Page 6: Sequentiality, Second Order Monadic Logic and Tree Automata

Theorem 3.10 If P is de�nable in (W)SkS, then the sequentiality of P is decidable.ProofUsing the previous lemma and assuming that P is de�ned by �( ~X), P is sequential i� thefollowing formula holds:8 ~X; (Term( ~X) ^ 9~Y ;Term(~Y ) ^ �(~Y ) ^ ~X v ~Y )) (�( ~X) _ Index( ~X))which is a translation of de�nition 2.3. Then we conclude using Rabin's theorem [18, 19]. 24 Application to term rewriting systemsIn this section, we show how to apply theorem 3.10 to various sequentiality results for termrewriting. We assume here the reader familiar with term rewriting (see e.g. [7] for missingde�nitions). We will say in particular that a term t is linear if each variable occurs at most oncein t ( A term is shallow if it is a variable or if all its variables occur at depth 1. A rewrite systemR is left linear (resp. linear, resp. shallow) if all its left hand sides are linear (resp. all left andright hand sides are linear, resp. all left and right hand sides are shallow).This section is organized as follows: we �rst state the basic de�nitions of sequentialityand strong sequentiality in the case of rewrite systems in section 4.1. Then, we establish basicproperties about the reducibility predicate in section 4.2. Then we show in section 4.3 that the so-called \NVNF-sequentiality" is decidable for left linear (possibly overlapping) rewrite systems.This is a new result which is an application of theorem 3.10: we show that an appropriatepredicate is de�nable in WSkS. We give actually two proofs of the latter property: one is a5 lines proofs, relying on previous results by Dauchet et al and the other is a 1 page directconstruction.The decidability of NVNF sequentiality implies in particular the decidability of strong se-quentiality of possibly overlapping left linear rewrite systems (a result proved in [10]).Then we show in section 4.4 that sequentiality (which is in general undecidable) is decidablefor shallow rewrite system, again as an application of theorem 3.10.4.1 Strong sequentiality of left linear term rewriting systemsLet NR be the predicate symbol on T [ T which holds true i� t has a normal form (w.r.t. R)belonging to T .De�nition 4.11 ([9]) A term rewriting system R is sequential if the predicateNR is sequential.This captures the intuitive notion sketched in introduction: when R is sequential, then thereis an optimal reduction strategy. Since sequentiality of R is undecidable in general, a su�cientcondition for sequentiality (called strong sequentiality) has been introduced in [9].Let RedR be the predicate symbol on T [ T which holds true i� t is reducible by R. If Lis a set of terms, we also write ?�!L the binary relation on T [ T de�ned by s ?�!L t i� there isa position p in s, a term l 2 L, a substitution � and a term u 2 T [ T such that sjp = l� andt = s[u]p. This is the usual de�nition of rewriting, except that we do not consider right handsides: the left hand side can be replaced with any term u.6

Page 7: Sequentiality, Second Order Monadic Logic and Tree Automata

Let N?R be the predicate on T [ T which is true on t i� t has a normal form in T for therelation ?�!L where L is the set of left hand sides of R. Of course, NR � N?R.De�nition 4.12 ([9]) A term rewriting system R is strongly sequential if the predicate N?Ris sequential.Since NR � N?R, an index for N?R is also an index for NR, hence any strongly sequentialrewrite system is also sequential. (But the converse is false).We will show, as a consequence of lemma 4.17 that N?R is actually recognized by a �nitetree automaton. We will also give a direct construction of the automaton which accepts theterms having an index (resp. no index) in section 5. As a consequence, we have:Corollary 4.13 The strong sequentiality of left linear (possibly overlapping) rewrite systems isdecidable.ProofThis is a consequence of lemma 4.19 and theorem 3.10 since recognizable sets of terms are de-�nable in WSkS [22]. 2A result which is also known from [10].4.2 The reducibility and normal form predicatesIn the following constructions we will basically compute �xed points starting from the set ofreducible (resp. irreducible) terms. Let us therefore state and prove some basic (well-known)results about RedR.Lemma 4.14 When R is left linear, RedR is recognized by a �nite bottom-up tree automaton.ProofFor each non-variable strict subterm u of a left hand side of a rule, consider a state qu. In addi-tion, we have a state qr (the �nal state, or the state in which we know that the term is reducible)and the state q> which accepts all terms. Then, to each u = f(u1; : : : ; un), we associate theproduction rule f(qu1 ; : : : ; qun) ! qu where qui is understood as q> when ui is a variable. Toeach left hand side of a rule l = f(t1; : : : ; tn), we associate the rule f(qt1 ; : : : ; qtn)! qr and thestates qr are propagated: we have the rules f(q>; : : : ; q>; qr; q>; : : : ; q>) ! qr for all functionsymbols f . Finally, if not already present, we add the rules f(q>; : : : ; q>)! q>. 2Note that this doesn't work for non-left linear rewrite systems because then RedR is notde�nable by a �nite bottom-up tree automaton: we need disequality tests. Actually, adding thecorresponding tests to the logic (W)SkS yields an undecidable logic.Another consequence of lemma 4.14 is the recognizability of the set of irreducible terms inT (F): this is a consequence of the closure property of recognizable tree languages by complement.Let us show however an explicit construction of such an automaton, since we will re-use it forfurther analysis in the following.Given two terms s; t 2 T (F ;X ), we write s # t for a most general instance (when it exists)of s and a renaming t0 of t such that t0 and s do not share variables. Given a left linear rewritesystem R, let S(R) be the set of strict subterms of the left hand sides of R, up to similarity,which we close under #. (This may yield an exponential number of+ terms in S(R):: one for7

Page 8: Sequentiality, Second Order Monadic Logic and Tree Automata

each set of subterms of the lhs of R). With each term t in S(R), which is not an instance of alhs of R, we associate a state qt. (we write qx the state associated with all variables. We assumethat qx is in the set of states). Let Q = fqt j t 2 S(R)g [ fqrg and Qf be all states but qr .Intuitively, all reducible terms will be accepted in qr. The terms accepted in a state qt will beall instances of t that are not instances of any t�. More precisely, we consider the following setof production rules:f(qt1 ; : : : ; qtn) ! qtIf f(t1; : : : ; tn) is an instance of tand not an instance of some t� 6= t s.t. qt� 2 Qf(qt1 ; : : : ; qtn) ! qrIf f(t1; : : : ; tn) is an instanceof some lhs of Rf(q1; : : : ; qn) ! qrIf qr 2 fq1; : : : ; qngLet us call ANF (R) the above constructed automaton.Example 1 R = 8><>: h(x) ! g(f(a; a))f(x; a) ! af(g(a); x) ! f(x; g(a))The automaton ANF (R) consists of the production rules (assuming for simplicity that there areno additional function symbols besides h; f; g; a)a ! qa g(qa) ! qg(a)f(qa; qa) ! qr h(qa) ! qrf(qa; qg(a)) ! qx f(qg(a); qa) ! qrf(qg(a); qg(a)) ! qr h(qg(a)) ! qrg(qg(a)) ! qx f(qx; qa) ! qrf(qx; qg(a)) ! qx f(qx; qx) ! qxf(qa; qx) ! qx f(qg(a); qx) ! qrh(qx) ! qr g(qx) ! qxf(qr; q) ! qr f(q; qr) ! qrg(qr) ! qr h(qr) ! qrwhere q stands for any state. Final states will be qa; qg(a); qx.Lemma 4.15 ANF (R) accepts the set of irreducible terms in T (F). This automaton is deter-ministic and completely speci�ed.ProofThe automaton is deterministic since, assuming that f(t1; : : : ; tn) is an instance of both t andu (t; u 2 S(R), then it is an instance of t # u, hence the only rule which can be applied tof(qt1 ; : : : ; qtn) is either f(qt1 ; : : : ; qtn) ! qr (when f(t1; : : : ; tn) is an instance of a lhs of R) orf(qt1 ; : : : ; qtn)! qu1#:::#um if fu1; : : :umg is the set of terms in S(R) which are not instances ofa lhs of R and such that f(t1; : : : ; tn) is an instance of each ui (and not an instance of a lhs).8

Page 9: Sequentiality, Second Order Monadic Logic and Tree Automata

The automaton is completely speci�ed since every term is (at least) an instance of x. Theneither one of its direct subterms is accepted in state qr, or it is itself accepted in state qr, or theris a state qt in which it is accepted.We show by induction on the size of u that u is accepted in the state qt i� u is not reducible,it is an instance of t and not an instance of some other t� 2 S(R). If u is a constant, theneither u 2 S(R) (in which case it is accepted in state qr or qu depending on its reducibility) orit is accepted in state qx. Now, consider a term t = f(t1; : : : ; tn). If some ti is reducible, thenit is accepted in state qr by induction hypothesis and t is accepted in state qr too. Otherwise,by induction hypothesis, for every i, ti is accepted in state qui s.t. ti = ui�i and ti is not aninstance of any other ui�. Then either t is reducible, hence an instance of a lhs of a rule, andit is accepted in state qr , or else, there is a rule f(qu1 ; : : : ; qun) ! qu such that t is acceptedin state qu, f(u1; : : : ; un) is an instance of u and not an instance of any other u�. Then, ift = f(v1; : : : ; vn)� for some �, ti = vi� for all i. Hence, for all i, ui is an instance of vi, whichimplies that f(v1; : : : ; vn) is an instance of u (assuming the disjointness of variables).It follows that t is reducible i� it is accepted in the state qr, hence it is irreducible i� it isaccepted in another state, thanks to determinism and complete speci�cation. 2These constructions can be simpli�ed for a particular class of rewrite system:Lemma 4.16 Assume that for any two strict subterms s; t of some left hand side(s) of R, if sand t are uni�able, then either s is an instance of t or t is an instance of s. Then RedR andNF (R) are accepted by deterministic bottom-up tree automata with O(jRj) states.This is a direct consequence of the construction of ANF (R): the set of states is O(jRj) in thiscase.4.3 NVNF-sequentiality of left linear rewrite systemsInstead of approximating the rewrite system by forgetting about its right hand sides, as in thecase of strong sequentiality, Oyamaguchi introduced in [17] a re�ned approximation, forgettingonly the relationship between the variables of the left and right hand sides respectively. Moreprecisely, if R is a rewrite system, we consider the rewrite system RV where all occurrences ofvariables in the right hand sides have been replaced with new distinct variables. The strongsequentiality of R implies the sequentiality of RV which in turn implies the sequentiality ofR. (All implications are strict). Oyamaguchi has shown in [17] the decidability, for orthogonalrewrite systems, of the predicate � which is true on t when there is a s 2 T (i.e. without s)such that t ���!RV s. This does not correspond exactly to the sequentiality of RV since s is notrequired to be in normal form. When s is moreover required to be in normal form, we �ndagain the sequentiality of RV , which is called NVNF-sequentiality in [15]. In this latter paper,the authors show how to compute an index, assuming NVNF-sequentiality. It turns out thatNVNF-sequentiality is again de�nable in WSkS (without the orthogonality assumption of [17]).Lemma 4.17 For any linear rewrite system R, NRV is de�nable in WSkS.ProofWe only have to prove that NRV is recognized by a �nite tree automaton. We start withthe automaton of lemma 4.15 and add a state qt for each subterm t of a right hand side of RV ,up to literal similarity. (If this state is not already present). We also add production rules in9

Page 10: Sequentiality, Second Order Monadic Logic and Tree Automata

such a way that the set of terms accepted in such a state qt is the set of instances (in T ) of t. Inaddition, we consider states q0t, for each strict subterm t of a left hand side of RV , up to litteralsimilarity. We add the production rules in such a way that the terms accepted in state q0t arethe instances in T [T of t. Let us call again A the resulting automaton; its �nal states are the�nal states of ANF (RV ). Of course A is now non-deterministic.We saturate A with the following inference rules:g(l1; : : : ; ln)! f(r1; : : : ; rm) 2 RV f(q�1; : : : ; q�m)! q� 2 Pg(q0l1; : : : ; q0ln)! q� 2 PIf, for every i there is an instance of ri which is accepted in state q�i and q�i is either qi or q0i(Note that this condition is decidable as the set of instances of ri is accepted by a �nite treeautomaton and by decision properties for tree automata).g(l1; : : : ; ln)! x 2 RV q� 2 Qg(q0l1; : : : ; q0ln)! q� 2 PIf x is a variable and q� is either q or q0.The saturation process does terminate since no new state is added.We claim that the resulting automaton accepts the terms of T [T that have a normal formin T . For, we have two inclusions to prove.Any term accepted by A can be reduced to a normal form in T Let AN be the automa-ton obtained after applying N inference steps. We prove, by induction on N that AN onlyaccepts terms in T [ T that have a normal form in T .If N = 0, then by lemma 4.15, theautomaton accepts the terms that are in normal form in T . Now, assume we obtain AN+1by adding a new rule r (using, say, the �rst inference rule). Let t be a term accepted byAN+1: t ����!AN+1 qf 2 Qf . We prove, by induction on the number of times r is used inthis reduction, that t has a normal form in T . If r is not used at all, t ���!AN qf and, byhypothesis on AN , t has a normal form in T . Now, assume that t ���!AN t1 �!r t2 ����!AN+1 qf .Let r be g(q0l1; : : : ; q0ln)! q�, t1jp = g(q0l1 ; : : : ; q0ln) and t2 = t1[q�]p. By construction, thereis a rule g(l1; : : : ; ln) ! f(r1; : : : ; rm) in RV , a production rule f(q�1 ; : : : ; q�n) ! q� in ANand, for each i, a term ui which is an instance of ri accepted in state q�i . Now, let u bet[f(u1; : : : ; um)]p. u ���!AN t2 and hence u has a normal form in T by induction hypothesis.Moreover, t ��������������!g(l1;:::;ln)!f(r1;:::;rm) u since the variables of the right and left hand sides aredisjoint. Hence t has a normal form in T .Any term that can be reduced to a normal form in T is accepted by A We prove thisresult by induction on the length of the reduction sequence from t to one of its normal formin T . If this length is 0, then t is accepted by the automatonANF (RV ) and hence byA. Nowassume that t ��������������!g(l1;:::;ln)!f(r1;:::;rm) u: tjp = g(l1�; : : : ; ln�) and u = t[f(r1�; : : : ; rm�)]p.By induction hypothesis, u is accepted by A:u ��!A u[f(q�1; : : : ; q�m)]p ���������!f(q1;:::;qm)!q u[q�]p ��!A qfBy de�nition, for each i, there is an instance of ri (ri�) which is accepted in state q�i .Hence, using the �rst inference rule, there is production rule g(q0l1; : : : ; q0ln) ! q� 2 P .Now, for each i, li� ��!A q0li , hence tjp ��!A q� and t is accepted by the automaton.10

Page 11: Sequentiality, Second Order Monadic Logic and Tree Automata

2Example 2 Let us consider the very simple example:R = 8><>: h(h(x)) ! f(g(x))g(x) ! h(a)f(a) ! f(a)In the system RV , the variable on the right side of the �rst rule is replaced with another variabley. The automaton ANF (R) contains, besides the rules yielding qr , the following productions:a ! qa h(qa) ! qh(x) f(qh(x)) ! qxh(qx) ! qx f(qx) ! qxFinal states will be qa; qh(a) and qx.We add the following production rules:h(qa) ! qh(a) f(qa) ! qf(a) a ! q>h(q>) ! q> f(q>) ! q> g(q>) ! q>g(q>) ! qg(x) ! q0x a ! q0xh(q0x) ! q0x g(q0x) ! q0x f(q0x) ! q0xh(q0x) ! q0h(x) a ! q0aWe arrive at the saturation process which produces the following rules (we exclude the rulesyielding q>): h(q0h(x)) ! q0x g(q0x) ! qh(x) g(q0x) ! qh(a)g(q0x) ! q0h(x) h(q0h(x)) ! qxThe last rule is obtained as follows: there is an instance of g(y) which is accepted in state qh(x)(thanks to the rule g(q0x)! qh(x)). Hence, from the rules h(h(x))! f(g(y)) and f(qh(x))! qx,we deduce h(q0h(x))! qx.Corollary 4.18 NVNF-sequentiality is decidable for left linear (possibly overlapping) rewritesystems.ProofThis is a consequence of theorem 3.10 and lemma 4.17. 2Similarly, we have the decidability of NV -sequentiality (as de�ned in [17]): it su�ces to startwith T instead of starting the construction with NF (R). Note that this gives a much simplerproof than in [17], and in a more general case.We could also prove this result using ideas similar to [5, 6]:4.3.1 An indirect (very short) proof of lemma 4.17:From lemma 4.14, the relation ��!RV is recognized by a Ground Tree Transducer (as de�ned in[5] and hereafter called GTT). As shown in [5], the class of binary relations which are accepted11

Page 12: Sequentiality, Second Order Monadic Logic and Tree Automata

by a GTT is closed under transitive closure: ���!RV is recognized by a GTT, hence recognizableas a set of pairs: ���!RV is de�nable in WSkS (see e.g. [6]). Now, NF (R) is also de�nable inWSkS (lemma 4.15), hence NRV is also de�nable in WSkS. 2Note the tricks of this proof: there are (at least) three notions of recognizability for sets ofpairs of trees (i.e. binary relations on T [ T):� Rec1 is the class of cartesian products of recognizable sets.� Rec2 is the class of languages accepted by a GTT.� Rec3 is the class of trees over the square of the alphabet that are recognizable (each treeover the square alphabet corresponds roughly to a pair of trees according to the well knownencoding: see e.g. [6]).These three classes are distinct and ordered according to the hierarchy:Rec1 � Rec2 � Rec3Rec3 corresponds to the de�nability in WSkS. However, if R is in Rec3, the transitive closure ofR might not be in Rec3. Having no relations between the variables of the left and right handsides implies immediately that ��!RV is in Rec3. But the main trick is to notice that it is actuallyin Rec2, which is closed under transitive closure.4.3.2 Back to strong sequentialityStrong sequentiality of R can actually be viewed as the sequentiality of a rewrite system R0V inwhich all right hand sides have been replaced by a variable not occurring in the correspondingleft hand side. Hence, we get, as a corollary of lemma 4.17:Lemma 4.19 For left linear rewrite systems R, N?R is recognized by a �nite tree automaton.This lemma can also be proved, of course, using the Ground Tree Transducers.4.4 Sequentiality of shallow linear rewrite systemsLemma 4.20 If R is a shallow linear rewrite system, then NR is recognized by a �nite treeautomaton.ProofWe start with the automatonANF (R) which accepts the set of irreducible terms in T (F) (thanksto lemma 4.15). Then we complete it in such a way that for every ground term t occurring ina right side of R there is a state qt which accepts t (and only this term), and a state q> whichaccepts all terms in T [ T . (This di�ers from qx in which all terms of T are accepted). Let Abe the resulting automaton.Then, we build an automaton A1 which accepts all terms in T [T that have a normal formin T . For, we use the same set of states as A (and the same set of �nal states). We start withthe set P of production rules of A and saturate P using the following set of inference rules:f(t1; : : : ; tn)! g(u1; : : : ; um) 2 R g(q1; : : : ; qm)! q 2 Pf(q01; : : : ; q0n)! q 2 PIf 12

Page 13: Sequentiality, Second Order Monadic Logic and Tree Automata

� when ui is a ground term, then ui is accepted in state qi (by the current automaton)� q0i = qj whenever ti = uj� q0i = qti when ti 2 T� q0i = q> when ti is a variable not occurring in the right side of the ruleand f(t1; : : : ; tn)! x 2 R q 2 Qf(q1; : : : ; qn)! q 2 PIf � x is a variable� qi = q whenever ti = x� qi = qti whenever ti is a ground term.� qi = q> when ti is a variable distinct from x.This terminates since the set of states being �xed, the number of possible inferred productionrules is bounded. It yields a �nite bottom-up tree automaton accepting all terms in T [T thathave a normal form in T . There are two inclusions to prove:All terms accepted by A1 do have a normal form in T .This is proved by induction on the number of times the above inference rules have beenapplied. If they cannot be applied, then the inclusion follows from lemma 4.14. Otherwise,consider the automaton A�N computed after N inference steps and assume that A�N onlyaccepts terms that have a normal form. Let A�N+1 be the automaton obtained by aug-menting the set of production rules of A�N with the rule r def= f(q01; : : : ; q0n) ! q followingthe conditions of one of the inference rules. Assume t ����!A�N+1 qf with qf 2 Qf . We proveby induction on the number of times r is applied in this reduction that t has a normal form.If r is not applied at all, then t ���!A�N qf and, by hypothesis, t has a normal form. Now, ift ���!A�N t1 �!r t2 ����!A�N+1 qf , then there is a position p of t1 such that t1jp = f(q01; : : : ; q0n)and t2jp = q.We investigate now the possible constructions of r:First inference rule We build a term u as follows: u = t[g(u1; : : : ; um)]p where uj = tjp�iwhenever q0i = qj and uj = v whenever qj = qv (for v a ground term). Then t!R u.On the other hand, u ���!A�N t2, by construction of r and since for every i = 1; : : : ; n,tjp�i ���!A�N q0i. Hence, by induction hypothesis, u has a normal form in T . Now, sincet!R u, t has also a normal form in T .Second inference rule We proceed in a similar way: we let u = t[tjp�i]p t �!R u.tjp�i ���!A�N qi and qi = q. Hence u ���!A�N t2 by construction. Now, using the in-duction hypothesis on u, u has a normal form in T , and t has a normal form in T toosince t �!R u. 13

Page 14: Sequentiality, Second Order Monadic Logic and Tree Automata

All terms that do have a normal form in T are accepted by A1 . This is proved by in-duction on length of the reduction of t to one of its normal forms in T . If t is in normalform, then, by de�nition of A1, t is accepted. If t N+1���!R t0 and t0 2 NF , then lett !R t1 N�!R t0. Let tjp = f(l1; : : : ; ln)� and t1 = t[g(r1; : : : ; rm)�]p for some rewrite rulef(l1; : : : ; ln)! g(r1; : : : ; rm) 2 R (the case where r is a variable is similar). By inductionhypothesis, t1 is accepted by A1. Let us consider a successful run of the automaton on t1:t1 ���!A1 t1[g(q1; : : : ; qm)]p ���������!g(q1;:::;qm)!q t1[q]p ���!A1 qfand qf 2 Qf . By construction of A1, there is a production rule r def= f(q01; : : : ; q0n) ! q inP such that ri is accepted in state qi when ri is a ground term, q0i = qj whenever ti = uj ,q0i = qli when li is ground and q0i = q> when li is a variable which does not occur ing(r1; : : : ; rm). Then there is a reduction of each tjp�i to q0i:� if li is a variable which does not occur in r, then li� ���!A1 q> by de�nition of q>� if li 2 T , then li ���!A1 qli by de�nition of qli� if li is a variable and li = rj for some j, then li� = rj� and hence li� ���!A1 qj = q0iNow, there is a reduction of tjp to f(q01; : : : ; q0n), yielding the desired t ���!A1 t1[q]p.Finally, we intersect A1 with T (which is obviously recognizable). 2Let us show an example of the automaton accepting all terms that can be reduced to anormal form.Example 3 We use the example 1. In what follows, for sake of simplicity, we will not considerthe rules which involve a state qr (they do not play any role).We add �rst the rules:f(qa; qa) ! qf(a;a) g(qf(a;a)) ! qg(f(a;a)) a ! q>g(q>) ! q> h(q>) ! q> f(q>; q>) ! q> ! q>Now, we start the saturation process, yielding to the new rules (in order of computation,and excluding the rules yielding q> which are irrelevant):h(q>) ! qg(f(a;a)) f(q>; qa) ! qa f(qg(a); qx) ! qxf(qg(a); qa) ! qx h(q>) ! qg(a)This last rule is obtained after noticing that f(a; a) ��! qa, hence from h(x) ! g(f(a; a)) 2 Rand g(qa)! qg(a), we can deduce h(q>)! qg(a).Let us consider two examples of computations using the resulting automaton:h()! h(q>)! qg(a)hence h() is accepted by the automaton.f(g(a);)! f(g(qa);)! f(g(qa); q>)! f(qg(a); q>)14

Page 15: Sequentiality, Second Order Monadic Logic and Tree Automata

the reduction cannot be continued any longer (except by going to q>). Moreover, there is noother computation sequence: f(g(a);) is not accepted.As a consequence of theorem 3.10 and lemma 4.20 we have the new decidability result:Corollary 4.21 The sequentiality of shallow linear rewrite systems is decidable.4.5 Sorted systemsAll above results can be extended to order-sorted rewrite systems. In such systems, variables arerestricted to range over some regular sets of trees2. In particular, we �nd again some decidabilityresults of [13] as well as their extension to arbitrary left-linear rewrite systems.5 Direct construction of the automatonFor reasons which have already been explained, we construct here directly the automaton ac-cepting all terms that have an index w.r.t RedR. We only consider left linear rewrite systems.For the direct construction of automata, it is more convenient to use the formalism of [14].Of course, we could construct an automaton using the correspondence between automata andlogic, but this construction would be too complex.5.1 Another characterization of indexesLet t 2 T (F [ fg;X ) and u 2 T (F ; X). We say that t is compatible with u if there is someinstance v of u such that t v v.3 Let R be a rewrite system. Then ��!R is the relation on Tde�ned by t ��!R u i� there is a p 2 Pos(t) such that tjp 6= and tjp is compatible with someleft hand side of a rule and u = t[]p. ��!R is a convergent reduction relation. The normalform of a term t w.r.t to ���!R is written t #R.Theorem 5.22 ([14]) Let x 2 X . The position p such that tjp = is an index of t (w.r.t.RedR) i� (t[x]p #R)jp = x.5.2 Construction of the automaton AnoindWe construct �rst an automaton accepting the set of terms which that can be reduced to byR.Lemma 5.23 For every linear rewrite system, there is an automaton AR with O(jRj) stateswhich recognizes the set of terms t 2 T that can be reduced to using R.Actually this lemma can be seen as a particular case of lemma 4.17. Let us however show aslightly di�erent construction in detail since we will need additional properties.ProofWith each strict subterm t of a left hand side of a rule (up to literal similarity), we associate astate qt. In addition, we have the �nal state q and, if necessary, the state qx (x is a variable).Then, for each f 2 F and each qt1 ; : : : ; qtn, we add the production rules2Order-sorted signatures, which include subsort declarations and overloading are exactly tree automata, asnoticed e.g. in [4]3Note that this comptatibility relation is not symmetric.15

Page 16: Sequentiality, Second Order Monadic Logic and Tree Automata

S1: f(qt1 ; : : : ; qtn) ! qu if f 6= and f(t1; : : : ; tn) is compatible with u and qu is in the set ofstates.S2: f(qt1 ; : : : ; qtn)! q if, f(t1; : : : ; tn) is compatible with a left hand side of a ruleS3: ! qThe number of states in AR is O(jRj) and the number of rules is O(jRjk+1) where k is themaximal arity of a function symbol.We have now to show two inclusions.Every term accepted by AR can be reduced to by R . We prove, by induction onthe length of the reduction (i.e. the size of t) that, if t ����!AR qu then t ���!R v for somev which is compatible with u. When the length is 1, t is a constant and t = u or u is avariable.Assume now that f(t1; : : : ; tn) ����!AR f(qu1 ; : : : ; qun) ���!AR qu. By induction hypothesis,for every i, ti ���!R vi for some vi which is compatible with ui. Moreover, either u = andf(u1; : : : ; un) is compatible with a left hand side of a rule, in which case f(v1; : : : ; vn) isalso compatible with a left hand side of a rule and we have f(v1; : : : ; vn) ��!R . Or elsef(u1; : : : ; un) is compatible with u, in which case f(v1; : : : ; vn) is also compatible with u.In all situations f(t1; : : : ; tn) ���!R v for some v which is compatible with u.Now, applying this to the case u = , we have that t ����!AR q implies t ���!R since is the only term compatible with .Every term in T that can be reduced to by R is accepted by AR. We prove thispart by induction on the length of the reduction to . If the term t is itself, thenthere is nothing to prove. If t ��!R u ���!R , by induction hypothesis u ����!AR q. Letu = t[]p and tjp ��!R . By de�nition of R, this means that replacing 's with termsin tjp we get an instance l� of a left hand side of a rule l. l� itself is accepted in state qby construction. Let �1 be a successful run of the automaton on l�. Now, we construct arun on tjp as follows: if p0 is a position of both l� and tjp and t(p � p0) = l�(p0), then welet �(p0) = �1(p0). Otherwise, by hypothesis, we have t(p � p0) = , in which case, we let�(p0) = q. � is a run indeed.2 Note that AR is in general non-deterministic and that determinizing it may require ex-ponentially many states. For example, if g(f(x; a)) and h(f(x; b)) are two left members of R,from f(qx; q) we can reach the two states qf(x;a) and qf(x;b) and we cannot commit to any ofthem before knowing what is the symbol above. One way to prevent this situation is to add,for every subterm of a left hand side of a rule a state for each term obtained by replacing somesubterms of t with . But this step is exponential. It is not, in principle, better than deter-minizing AR . Following this, it is possible to compute an automaton Aind which accepts allterms that have an index. The automaton would be non-deterministic and contain exponetiallymany states. We will show its construction later on. However, for the decision problem, we need16

Page 17: Sequentiality, Second Order Monadic Logic and Tree Automata

to decide wether all irreducible terms in T are accepted by Aind. This question can only bedecided in exponential time w.r.t. the number of states of the automaton (automata inclusionis EXPTIME-complete when the right member of the inclusion can be non-deterministic [20]).This would yield a doubly exponential test. It is however possible to reduce the complexity to asingle exponential, computing directly an automaton for the complement of Aind and decidingits inclusion in the set of reducible terms. That is what we are doing now.Lemma 5.24 For every left linear rewrite system R, it is possible to compute in O(2jRj) timean automaton Anoind which accepts the terms t 2 T that do not have an index.ProofLet QR be the set of states of AR . The states Q of our automaton will consist of pairs ofsubsets of QR. The �rst components of such pairs will be written as disjunctions of statesq1_ : : :_qn, whereas the second components will be written as conjunction of states q1^ : : :^qn.The �nal states will be pairs [S; ;]. Intuitively, the second component in the pair correspond toterms that have to be \eliminated" by R.The production rules are de�ned as follows: �rst the rule for is ! [fqg; fqxg]On the �rst component, we will �nd the behaviour of the automaton as in AR . The secondcomponent correspond to index guess: if has been replaced with x, we enter the state qx.The progression rules are de�ned as follows:f([S1;S 01]; : : : ; [Sn;S 0n])! [S;S 0]if � S = fq j 8i = 1 : : :n; 9ti 2 Si; f(qt1; : : : ; qtn) ���!AR qg� S 0 = f�(t0; i) j (t0; i) 2 Eg where � is a mapping from E to states such thatf(qt1 ; : : : qti�1 ; qt0 ; qti+1 ; : : : ; qtn) ���!AR �(t0; i)for some qt1 ; : : : ; qtn belonging respectively to S1; : : : ; Sn.� E is the set of pairs (t0; i), i = 1 : : :n, t0 2 S 0i such that there is no states qt1 ; : : : ; qtnbelonging to S1; : : : ; Sn respectively such that f(qt1 ; : : : ; qti�1; qt0 ; qti+1 ; : : : qtn) ���!AR q.Intuitively, in the �rst component we record all possible behaviours of AR , whereas in thesecond component, we superpose all behaviours corresponding to all index guesses.We claim that a term t 2 T is accepted by this automaton i� it has no index, i.e. i� for allpositions p of in t, p =2 Pos((t[x]p) #R).We have to prove two inclusions.If t 2 T is accepted by A, then t has no indexFirst note that, by construction, t ��!A [S;S 0] for some S 0 i� t ����!AR qu for all qu 2 S.Assume t ��!A [S; ;]. Let p be a position of in t. We will show that there is pre�x p0 ofp such that t[x]pjp0 ���!R . More precisely, let T;x be the set of terms in T (F [ f; xg)that contain at most one occurrence of x and Tx be the subset of Tx of terms that do notcontain any . If � is a run of A on t 2 T;x, we show that17

Page 18: Sequentiality, Second Order Monadic Logic and Tree Automata

�(�) = [S;S 0] implies that, if there is a t0 2 Tx such that t[x]p #R is incompatiblewith all u such that qu 2 S 0, then t[x]p ���!R v where v 2 T.As a particular case, when S 0 = ;, we will have the desired result.We show the property by induction on the size of t. If t = , then t �!A [fqg; fqxg] andt[x]� #R= x is compatible with x and qx 2 S 0.Assume t ��!A f([S1;S 01]; : : : ; [Sn;S 0n]) �!A [S;S 0]. Assume moreover that t[x]p #R isincompatible with any u such that qu 2 S 0. Finally, assume w.l.o.g that for all indices i 6= 1,t[x]pji 2 T (in other words, the �rst symbol of p is assumed to be 1). If t[x]pj1 #R2 Tor if it is incompatible with any u1 such that qu1 2 S 01, then by induction hypothesis,t[x]pj1 ���!R v1 2 T, hence t[x]p ���!R v 2 T. Otherwise, there is a term t01 2 Tx s.t.t[x]pj1 #Rv t01 and t01 is an instance of qu1 2 S 01. By contradiction, suppose that (u1; 1) 2 Eand let qv = �(u1; 1). Then f(qu1 ; qt2 ; : : : ; qtn) ���!AR qv for some ti s.t. tji ����!AR qti . Now,we have, for every i � 2, tji #Rv tji, if ti 6= then tji is compatible with ti and ti iscompatible with vji (actually ti is either or an instance of vji). Hence, for every i � 2,tji #R is compatible with vji. On the other hand, t[x]pj1 #R is compatible with u1, hencewith vj1 (u1 is an instance of vj1). It follows that t[x]p #R is either orf(t[x]pj1 #R; tj2 #R; : : : ; tn #R)and is both cases it is compatible with v. Hence a contradiction.If t 2 T has no index, then it is accepted by A. We prove, by induction on the size of tthat there is a pair [S;S 0] such that t ��!A [S;S 0] and q 2 S i� t ����!AR q and qu 2 S 0 i�u is a maximal term (w.r.t. v) s.t. there is a position p of in t s.t. t[x]p #R =2 T andt[x]p #R is compatible with u. This will of course imply the desired property.If t = , then t �!A [fqg; fqxg] and t[x]� #R =2 T and t[x]� #R= x is compatible withx.Now let t = f(t1; : : : ; tn) and ti ��!A [Si; S 0i] following the induction hypothesis. Let S 0 bethe set of qu s.t u is a maximal term (w.r.t. v) s.t. there is a position p of in t s.t.t[x]p #R =2 T and t[x]p #R is compatible with u. Similarly, let S be as in the inductionconclusion. We only have to show that f([S1;S 01]; : : : ; [Sn;S 0n]) �!A [S;S 0]. Let p be aposition of in t such that t[x]p #R =2 T. (If there is no such position, then S 0 = ;and the property holds true). Assume w.l.o.g that p = 1 � p0. Then t[x]pj1 #R =2 T andt[x]p #R= f(t[x]pj1 #R; tj2 #R; : : : tjn #R) is compatible with u. By maximality of u1,it must be an instance of uj1. For i � 2, let tji #R be accepted in state qti . Then letting�(u1; 1) be qu, we have indeed f(qu1 ; qt2 ; : : : ; qtn) ���!AR qu2 18

Page 19: Sequentiality, Second Order Monadic Logic and Tree Automata

Example 4 Assume that the left hand sides of R are ff(x; g(a)); f(a; a); h(a; x); h(f(b; y); a)gand let us show a run of the automaton Anoind on h(f(; g());): ! [q; qx]g([q; qx]) ! [qg(a); qx]f([q; qx]; [qg(a); qx]) ! [q; qf(b;y)]h([q; qf(b;y)]; [q; qx]) ! [q; ;]Example 5 If the set of left hand sides of R is fh(f(x; a); a); h(f(a; x); a)g, a run of Anoind onh(f(;);) will be given by: ! [q; qx]f([q; qx]; [q; qx]) ! [qf(a;x) _ qf(x;a); qf(a;x) ^ qf(x;a)]h([qf(a;x) _ qf(x;a); qf(a;x) ^ qf(x;a)]; [q; qx]) ! [q; qx]5.3 Complexity issuesAs a consequence of lemma 5.24, we get a complexity result:Theorem 5.25 Strong sequentiality is in EXPTIME when R is left linear (possibly overlap-ping).ProofR is strongly sequential i� the set of terms that do not have an index is contained in the setof reducible terms. Or, equivalently, R is strongly sequential if there is no term in T whichis accepted by both Anoind and ANF (R). Both automata can be computed in exponential timethanks to lemmas 5.24 and 4.15. Intersection can be done in quadratic time and the emptinessdecision is again polynomial. 2The complex construction of lemma 5.24 can only be avoided when any two strict subtermof left hand sides are comparable w.r.t. v whenever they are headed with the same functionsymbol. In such a situation, AR can be made deterministic without adding any state (this isquite straightforward) and Anoind can be computed in polynomial time. We have also seen that,in such a case, the automaton ANF (R) can be computed in polynomial time, hence:Corollary 5.26 For rewrite systems R such that any two strict subterms of a left hand side ofa rule which have the same top symbol are comparable w.r.t. v, strong sequentiality is decidablein polynomial time.Note that R can be overlapping here.Example 6 R = 8>>><>>>: f(f(x; y); z) ! f(x; f(y; z))f(0; x) ! 0f(s(x); y) ! s(f(x; y))f(x; y) ! f(y; x)R satis�es the condition of corollary 5.26: any two strict subterms of the left hand sides whichare headed with f (actually there is only one such term here) are comparable w.r.t. v.On the other hand, there are orthogonal rewrite systems that do not satisfy the conditionsof corollary 5.26. 19

Page 20: Sequentiality, Second Order Monadic Logic and Tree Automata

Example 7 R = ( g(f(a; x)) ! g(a)h(f(x; a)) ! h(a)R is non-overlapping and linear. However, the two strict subterms of left hand sides: f(a; x)and f(x; a) are not comparable w.r.t. v.In the case of non-left linear rewrite systems, the construction does not work, even if we useautomata with constraints [2, 3] instead of bottom-up tree automata. For example, consider therewrite system with only one rule whose left side is f(x; x). Thenf(f(gk(f(a; a)); b); f(gk(b); b)) ��!R f(f(gk(); b); f(gk(b); b)) ��!R However, the replacement for in f(gk(); b) is known only when we reach the root of the term,i.e. arbitrary \far" from the �rst reduction. It is not possible to keep such an information inthe �nite memory of an automaton with constraints.5.4 Construction of AindNow, instead of constructing Anoind , let us construct directly Aind. This will show how to�nd indexes in a term. We start from a deterministic (completely speci�ed) version of ARand complete it as follows. Each state q of AR is duplicated: we add the state q� which willintuitively mean that we found an index below. Then we add the following rules:� ! q�x (this is a guess of an index position; we will express that it has to be applied oncein each successful run)� For each rule f(q1; : : : qi; : : : ; qn) ! q where q is not a �nal state, we add the rulesf(q1; : : : ; q�i ; : : : ; qn)! q�Final states are those which are marked with a �.Example 8 Consider a rewrite system whose left hand sides are f(a; x); f(f(x; y); a) (with 3function symbols, f; a; b).The states of Aind are fqa; qf(x;y); q; qx; q�f(x;y); q�xg (states q�a and q�b have been removed sincethey are useless). The rules are: ! q�x f(qx; qx) ! qf(x;y) f(q1; q�x) ! q�f(x;y) ! q f(qf(x;y); qf(x;y)) ! qf(x;y) f(q�f(x;y); q1) ! q�f(x;y)a ! qa f(qx; qf(x;y)) ! qf(x;y) f(q1; q�f(x;y)) ! q�f(x;y)b ! qx f(qx; qa) ! qf(x;y) f(q�x; q2) ! q�f(x;y)f(qf(x;y); qa) ! q f(qx; q) ! qf(x;y) f(qa; q3) ! qf(q�f(x;y); qa) ! q f(qf(x;y); qx) ! qf(x;y) f(q; q3) ! qwhere q1 is any state in fqx; qf(x;y)g, q2 is any state in fqx; qf(x;y); qa; qg and q3 is any state infqa; q; qx; qf(x;y); q�x; q�f(x;y)g. The �nal states are q�x and q�f(x;y).For example, f(;) is accepted since f(q�x; q)! q�f(x;y). But f(f(;);) is not accepted.Moreover, this term being irreducible, the system is not strongly sequential.Lemma 5.27 The automaton Aind accepts all terms in T that have an index.20

Page 21: Sequentiality, Second Order Monadic Logic and Tree Automata

ProofFor convenience, we confuse here AR and its deterministic version. We have to prove twoinclusions:Every term accepted by Aind has an index Let t n���!Aind q�. We show that t has an indexby induction on n: if n = 0 then t = and � is an index. If n > 0, by de�nition ofthe rules, t n�1���!Aind f(q1; : : : ; q�i ; : : : ; qn) ! q�. By induction hypothesis, tji has an indexp (since it is accepted). Now, by determinacy of AR and by lemma 5.23, t can not bereduced to by R. Hence, there is no redex w.r.t R in t, along the path i � p. Whichmeans that i � p is an index in t by theorem 5.22.Every term in T that has an index is accepted by Aind Let p be an index in t 2 T.We show by induction on the size of p that t is accepted by Aind. If p = �, then t = is indeed accepted. Assume now that p = 1 � q and t = f(t1; : : : ; tn). Then q is an indexin tj1 and hence, by induction hypothesis, there is a state q� such that tj1 ����!Aind q�. Let,for i > 1 qi be the state in which tji is accepted by AR . p being an index and by lemma5.23, f(q; q2; : : : ; qn) cannot be rewritten into a �nal state of AR . Hence there is a rulef(q�; q2; : : : ; qn)! q0� in Aind, which shows that t can be reduced to a �nal state q0�.2 Now, if we come back to the problem of �nding an index in a term, we can use Aind: allsuccessful runs on t contain a �-marked path from the root to an index. Consider for example theterm f(f(;); f(;)), the only successful run is q�f(x;y)(q�f(x;y)(q�x; q); qf(x;y)(q; q)), whichshows the path 11, which is the only index.There is still some work to do w.r.t. index search. Indeed, so far, we have to apply theautomaton on the whole term after each reduction step. Huet and L�evy, on the other hand, givea deterministic algorithm which never visits twice a node in the input term. To do somethingsimilar, we would have �rst to consider our automaton top-down (instead of bottom-up as wedid through all the paper). Such an automaton is in general non-deterministic (and cannot bedeterminized). In order to avoid backtracking on the input term we would have to keep a stackof choice points and derive simultaneously the possible runs on the branch which is explored.This requires some additional implementation machinery, which is out of the scope of this paper.Concerning reduction strategies, we should also note that index reduction is a normalizingstrategy for sequential orthogonal term rewriting systems, as shown by Huet and L�evy. Foroverlapping systems, or for non-left linear systems this is no longer true. Hence the extension ofdecidability results to these cases are only interesting for subclasses of rewrite systems (as e.g.described in [24]).6 Further applicationsWe believe that the tree automata approach can be used successfully to other works on reductionstrategies. For example the strong root stability of [12] is also expressible in WSkS for left linearrewrite systems. The use of automata should also be investigated in parallel reduction strategies,such as in [21]: a run of the automaton not only gives an index position, but all index positions.21

Page 22: Sequentiality, Second Order Monadic Logic and Tree Automata

AcknowledgmentsI thank Takahashi Nagoya, Masahito Sakai, Yoshihito Toyama and Ralf Treinen for their carefulreading of a former version of this paper: they found several mistakes. This helped me toimprove the paper.References[1] S. Antoy and A. Middeldorp. A sequential reduction strategy. In Proc. Algebraic and LogicProgramming, volume 850 of LNCS, pages 168{185. Springer-Verlag, 1994.[2] B. Bogaert and S. Tison. Equality and disequality constraints on brother terms in treeautomata. In A. Finkel, editor, Proc. 9th Symp. on Theoretical Aspects of Computer Science,Paris, 1992. Springer-Verlag.[3] A.-C. Caron, J.-L. Coquid�e, and M. Dauchet. Encompassment properties and automatawith constraints. In C. Kirchner, editor, Proc. 5th. Int. Conf. on Rewriting Techniquesand Applications, Lecture Notes in Computer Science, vol. 690, Montreal, Canada, 1993.Springer-Verlag.[4] H. Comon and C. Delor. Equational formulae with membership constraints. Informationand Computation, 112(2):167{216, Aug. 1994.[5] M. Dauchet, T. Heuillard, P. Lescanne, and S. Tison. The con uence of ground term rewrit-ing systems is decidable. In Proc. 3rd IEEE Symp. Logic in Computer Science, Edinburgh,1988.[6] M. Dauchet and S. Tison. The theory of ground rewrite systems is decidable. In Proc. 5thIEEE Symp. Logic in Computer Science, Philadelphia, 1990.[7] N. Dershowitz and J.-P. Jouannaud. Rewrite systems. In J. van Leeuwen, editor, Handbookof Theoretical Computer Science, volume B, pages 243{309. North-Holland, 1990.[8] M. G�ecseg and M. Steinby. Tree Automata. Akademia Kiad�o, Budapest, 1984.[9] G. Huet and J.-J. L�evy. Computations in orthogonal rewriting systems II. In J.-L. Lassezand G. Plotkin, editors, Computational Logic: Essays in Honor of Alan Robinson, pages415{443. MIT Press, 1991. This paper was written in 1979.[10] J.-P. Jouannaud and W. Sad�. Strong sequentiality of left-linear overlapping rewrite sys-tems. In Workshop on Conditional Term Rewriting systems, Jerusalem, July 1994.[11] J. Kennaway. Sequential evaluation strategies for parallel-or and related reduction systems.Annals of Pure and Applied Logic, 43:31{56, 1989.[12] R. Kennaway. A con ict between call-by-need computation and parallelism. In N. Der-showitz, editor, Workshop on Conditional Term Rewriting Systems, Jerusalem, 1994.[13] D. Kesner. La d�e�nition de fonctions par cas �a l'aide de motifs dans des langages applicatifs.Th�ese de Doctorat, Universit�e de Paris-Sud, France, D�ecembre 1993.[14] J. W. Klop and A. Middeldorp. Sequentiality in orthogonal term rewriting systems. Journalof Symbolic Computation, 12:161{195, 1991.22

Page 23: Sequentiality, Second Order Monadic Logic and Tree Automata

[15] T. Nagaya, M. Sakai, and Y. Toyama. NVNF-sequentiality of left-linear term rewritingsystems. In Proc. Japanese Workhop on Term Rewriting, Kyoto, July 1995.[16] M. J. O'Donnell. Computing in systems described by equations. In Lecture Notes inComputer Science, volume 58. Springer, Berlin, West Germany, 1977.[17] M. Oyamaguchi. NV-sequentiality: a decidable condition for call-by-need computations interm rewriting systems. SIAM J. Comput., 22(1):114{135, 1993.[18] M. Rabin. Decidability of second-order theories and automata on in�nite trees. Trans.Amer. Math. Soc., 141:1{35, 1969.[19] M. Rabin. Decidable theories. In J. Barwise, editor, Handbook of Mathematical Logic, pages595{629. North-Holland, 1977.[20] H. Seidl. Deciding equivalence of �nite tree automata. Siam Journal of Computing,19(3):424{437, 1990.[21] R. Sekar and I. Ramakrishnan. Programming in equational logic : beyond strong sequen-tiality. In Proc. 5th IEEE Symp. Logic in Computer Science, Philadelphia, 1990.[22] J. Thatcher and J. Wright. Generalized �nite automata with an application to a decisionproblem of second-order logic. Math. Systems Theory, 2:57{82, 1968.[23] W. Thomas. Automata on in�nite objects. In J. van Leeuwen, editor, Handbook of Theo-retical Computer Science, pages 134{191. Elsevier, 1990.[24] Y. Toyama. Strong sequentiality of left linear overlapping term rewriting systems. In Proc.7th IEEE Symp. on Logic in Computer Science, Santa Cruz, CA, 1992.

23