Scholarly article on topic 'Termination of Lazy Rewriting Revisited'

Termination of Lazy Rewriting Revisited Academic research paper on "Computer and information sciences"

CC BY-NC-ND
0
0
Share paper
Keywords
{"term rewriting" / "lazy rewriting" / termination / "context-sensitive system"}

Abstract of research paper on Computer and information sciences, author of scientific article — Felix Schernhammer, Bernhard Gramlich

Abstract Lazy rewriting is a proper restriction of term rewriting that dynamically restricts the reduction of certain arguments of functions in order to obtain termination. In contrast to context-sensitive rewriting, reductions at such argument positions are not completely forbidden but delayed. Based on the observation that the only existing (non-trivial) approach to prove termination of such lazy rewrite systems is flawed, we develop a modified approach for transforming lazy rewrite systems into context-sensitive ones that is sound and complete with respect to termination. First experimental results with this transformation based technique are encouraging.

Academic research paper on topic "Termination of Lazy Rewriting Revisited"

Available online at www.sciencedirect.com

ScienceDirect

Electronic Notes in Theoretical Computer Science 204 (2008) 35-51

www.elsevier.com/locate/entcs

Termination of Lazy Rewriting Revisited

Felix Schernhammer and Bernhard Grämlich1

Theory and Logic Group Institute of Computer Languages TU Wien, Austria

Abstract

Lazy rewriting is a proper restriction of term rewriting that dynamically restricts the reduction of certain arguments of functions in order to obtain termination. In contrast to context-sensitive rewriting, reductions at such argument positions are not completely forbidden but delayed. Based on the observation that the only existing (non-trivial) approach to prove termination of such lazy rewrite systems is flawed, we develop a modified approach for transforming lazy rewrite systems into context-sensitive ones that is sound and complete with respect to termination. First experimental results with this transformation based technique are encouraging.

Keywords: term rewriting, lazy rewriting, termination, context-sensitive system

1 Introduction

In functional programming languages, evaluations are often carried out in a lazy fashion. This means that in the evaluation of an expression, the result of certain subexpressions is not computed until it is known that the particular result is actually needed. A very similar idea is used in lazy rewriting ([3]) where the reduction of certain subterms is delayed as long as possible (cf. also [1], [8], and [10]).

Termination analysis of functional programs has recently become a major subject of research in the rewriting community. Due to the similarity of lazy rewriting and the lazy evaluation strategy of functional programs, the use of lazy rewriting seems promising to find new methods for proving termination of functional programs. In particular lazy rewriting and lazy evaluation in functional programming share the idea of postponing certain evaluation steps. More precisely, arguments of functions are only evaluated if their final result is needed to compute the function.

Example 1.1 Consider the following functional program given in term rewriting system (TRS) syntax.

1 Email: {felixs,gramlich}@logic.at

1571-0661/$ - see front matter © 2008 Elsevier B.V. All rights reserved. doi:10.1016/j.entcs.2008.03.052

from(x) ^ x : from(s(x)) take(x, []) ^ []

take(0,xs) ^ [] take(s(x),y : ys) ^ y : take(x,ys)

Without an evaluation strategy the input term take(s(0),from(0)) is non-terminating. Yet, when using a lazy evaluation strategy it is terminating and the result is 0. The crucial difference is that in a term of the shape take(_ : from(S)) the from subterm may not be evaluated under lazy evaluation, because its result is not needed to evaluate any more outer function.

However, a reduction of the problem of proving termination of functional programs to the problem of proving termination of lazy TRSs is non-trivial. The reason is that functional programming languages typically allow for features such as higherorder functions, typing and strategies other than lazy evaluation; e.g., in Haskell always the "first" defining equation is applied to an object term when several equations are applicable (at the same position) (this has already been pointed out in

Therefore, lazy rewriting can only approximate lazy (Haskell) evaluations but clearly not simulate them in a one-to-one fashion. Yet, the described features of functional programs can be encoded or approximated through standard transformations such that finally lazy rewriting can be used for a termination analysis (cf.

In this work we present a transformation from lazy TRSs into context-sensitive ones that is sound and complete w.r.t. termination and thus allows us to reduce the problem of deciding whether a lazy TRS is terminating or not to the same problem for context-sensitive TRSs, which has already been studied to some extent (cf. eg. [12] ). As this is the first sound and complete transformation for lazy TRSs, it enables us for the first time to investigate the use of lazy rewriting in the area of termination analysis of functional programs.

Lazy rewriting was initially introduced by [3] in a graph rewriting setting (although the basic underlying idea is much older, cf. e.g. [4], [15], [14]). However, for the termination analysis of functional programs it makes sense to consider term rewriting instead of graph rewriting, for the following reasons. First of all, using term rewriting instead of graph rewriting is more general and does not prohibit certain evaluations a priori. Moreover, the theory of ordinary term rewriting is much further developed as compared to graph rewriting. In particular, existing methods for the termination analysis of ordinary as well as context-sensitive rewrite systems can be applied where possible. Hence, in this work we will use the notion of lazy term rewriting introduced in [11].

In [11] a transformation from lazy rewrite systems into context-sensitive ones was proposed, which was supposed to preserve non-termination and conjectured to be complete w.r.t. termination. Unfortunately, a counterexample (see Example 3.4 below) proves that this transformation is unsound w.r.t. termination. In this paper we repair the transformation and prove both soundness and completeness of the new transformation w.r.t. termination.

e.g. [7]).

In Section 2 of this paper we will present basic definitions and notations of lazy rewriting. In Section 3 we introduce the transformation of [11] and give a counterexample to its soundness w.r.t. termination. We then propose a modified version of the transformation which is proved to be sound and complete w.r.t. termination. Section 4 contains a discussion of the presented approach and of some experimental results.2

2 Preliminaries

We assume familiarity with the basic concepts and notations in term rewriting as well as context-sensitive term rewriting as provided for instance in [2,9].

As in [3] and [11] we are concerned with left-linear lazy rewrite systems in this work.

General assumption: Throughout the paper we assume that all lazy rewrite systems are left-linear 3 and finite.

Lazy rewriting operates on labelled terms. Each function and variable symbol of a term has either an eager label e or a lazy label l which we will write as superscripts. So, given a signature £ = [f1,..., fn}, we consider a new signature £' = [fi,fi,..., fn,,fn}. We denote by V' the set of labelled variables, so T(£',V') is the set of labelled terms of a labelled signature £'. The notation te (resp. tl) for a labelled term t indicates that t has an eager (resp. lazy) root label. Following [11] we use a replacement map f to specify for each function f e £ which arguments should be evaluated eagerly. Given a replacement map f we define the canonical labelling of terms as a mapping labelM: T (£,V) ^ T(£',V'), where £' is the labelled signature and V' are the labelled variables [11]:

label^(t) = labele^(t)

labela(x) =xa (a e [e, l})

labela(f (t1,..., tn)) = f a(labela (t{), ...,labelan (tn))

where ai = e if i e f(f), l otherwise, and a e[e,l}

Given a labelled term t, the unlabeled term erase(t) is constructed from t by omitting all labels. A position p of a term t is said to be eager (resp. lazy), if the symbol at the root of the subterm starting at position p of t has an eager (resp. lazy) label. Note that the lazy positions of a term are not the same as the non-replacing positions in context-sensitive rewriting. The reason is that in lazy rewriting eager positions may occur below lazy ones whereas in context-sensitive rewriting all positions which are below a non-replacing position are non-replacing.

However, rewrite steps may only be performed at so-called active positions. A position p is called active if all positions on the path from the root to p are eager. Note that, given an unlabeled term t and a replacement map f , the active positions of labelp(t) are exactly the replacing positions w.r.t. context-sensitive rewriting.

2 Due to lack of space, the proofs of some results (e.g., of Lemmata 3.13, 3.18 and 3.19) have been omitted here. They can be found in the technical report version of this paper [17].

3 Nevertheless, for clarity we will mention this assumption in the main results.

Definition 2.1 ([11], [3]) The active positions of a labelled term t (denoted Act(t)) are recursively defined as follows.

• The root position e of t is active.

• If p is an active position and position p.i is eager, then position p.i is active.

Example 2.2 Consider a labelled term fe(al,ge(hl(ae))). Positions e, 2 and 2.1.1 are eager. Positions 1 and 2.1 are lazy and positions e and 2 are active.

Definition 2.3 ([11], [3]) Let l e T(E,V) be linear, t e T(E',V') be a labelled term and let p be an active position of t. Then l matches t\p modulo laziness if either

• l e V or

• if l = f (h,...,ln) and t\p = fe(ta, ...,£%) (a e{e,l}), then for all eager subterms te, li matches modulo laziness te.

If ti at position p.i is a lazy subterm and li e V, then position p.i is called essential.

Informally, a matching modulo laziness is a partial matching ignoring (possible) clashes at lazy positions. Positions where such clashes occur may be activated (i.e., their label may be changed from lazy to eager).

Definition 2.4 ([11]) Let R = (£,R) be a (left-linear) TRS. Let t be a labelled term and let l be the left-hand side of a rule of R. If l matches modulo laziness t\p, and this matching gives rise to an essential position p.i (t\pi = fl(t\, ...,tn)), then t t[fe(t\, ...,tn)]p,i. The relation is called activation relation.

Example 2.5 Let l = f (a,b) be a linear unlabeled left-hand side of a rule, and t = fe(ae,cC). Then l matches t modulo laziness giving rise to the essential position 2 and we have fe(ae,C) fe(ae,ce). The left-hand side l does not match the labelled term f e(ae,ce) modulo laziness.

Definition 2.6 ([11]) Let l be the (linear) left-hand side of a rewrite rule and let t be a labelled term. If l matches erase(t), then the mapping ai,t : Var(l) ^ T(E',V) is defined as follows. For all x e V, with l\q = x: ,t(x) = t\q.

Informally, ai,t is the matcher when matching l against erase(t), where one adds the appropriate labels of t.

This substitution is modified to operate on labelled terms in the following way, yielding the mapping as,t: V' ^ T(E',V') [11]:

, e, ,ye if ast(x) = ya e V'

as t(xe)

' f e(ti,...,tn) if as ,t(x) = f a(ti,..,tn)

, ,, ,yl if as ,t(x) = ya e V'

as ,t(xl) = { ,

f l(ti ,...,tn) if as,t(x) = f a(ti,...,tn)

a is homeomorphically extended to a mapping T(T,',V') ^ T(E',V') as usual. Definition 2.7 ([11]) Let R = (X,R) be a (left-linear) TRS with replacement map

¡. The active rewrite relation —T(T',V') x T(T',V') is defined as follows: Let t be a labelled term such that the left-hand side of a rewrite rule l — r matches

erase(t\p) with ul,t\p and let p £ Act(t). Then t t[ul,t\p (label^(r))]p.

Informally, the active rewrite relation performs rewrite steps according to rewrite rules as usual, but only at active positions, and taking labels into account when constructing the contractum.

Example 2.8 Let l = f (x,b) — g(x) be a rewrite rule and t = fe(he(al),be) be a labelled term. Furthermore, consider a replacement map ¡(f) = ¡(h) = {1},i(g) = ty. Then uitt(x) = he(al) and uitt(xl) as appearing in the labelled right-hand side of

the rule is hl(al). Thus we have fe(he(al),be) ge(hl(al)).

The lazy rewrite relation is the union of the activation relation and the active rewrite relation.

Definition 2.9 ([11]) Let R be a (left-linear) TRS and let i be a replacement map for R. The lazy rewrite relation induced by (R,i) is the union of the two relations —— and ——^ (—>=— r U ——^).

Example 2.10 Consider the functional program of Example 1.1.

from(x) — x : from(s(x)) take(0,xs) — []

take(x, []) — [] take(s(x),y : ys) — y : take(x, ys)

Now we interpret it as lazy term rewriting system with a replacement map i given by i(f) = 0 for all functions f. When evaluating the canonically labelled term takee(sl(0l),froml(0l)) it

is obvious that the subterm from1 (0l) must be activated and evaluated before take can be computed. According to the lazy rewrite relation we have: takee(sl(0l),froml(0l)) takee(sl(0l),frome(0l)) takee(sl(0l), 0l : froml(sl(0l))) 0l : takee(0l,frome(sl(0l))) 0l.

Definition 2.11 Let R be a TRS with a replacement map ¡. Then R is LR(i)-terminating if there is no infinite L——^-sequence starting from a term t, whose labelling is canonical or more liberal (i.e., whenever label^(erase(t))\p is eager, then t\p is eager as well).

Informally, we call a labelled term t more liberal than its canonically labelled version label^(erase(t)) if it has strictly more eager labels. The reason for considering terms with canonical or more liberal labelling in the definition of LR(i)-termination is that only such terms appear in lazy reduction sequences starting from canonically labelled terms, in which we are actually interested.

Example 2.12 Consider a lazy TRS consisting of one rule f (a) — b with a replacement map ¡(f) = ty. Now, when considering the canonically labelled term f e(al) we can reduce it to f e(ae) according to the lazy rewrite relation. The latter term is more liberally labelled than its canonically labelled version.

Note that LR( j)-termination and well-foundedness of do not coincide in general. The reason is that LR( j)-termination concerns the non-existence of lazy reduction sequences starting from canonically (or more liberally) labelled terms, while there may still be infinite reduction sequences starting from other terms. Example 2.13 shows that the two notions are indeed different.

Example 2.13 Consider the TRS {g(f (a),c) — a, h(x, f (b)) — g(x, h(x, x))} with a replacement map j(f) = ¡(g) = {1} and ¡(h) = {1, 2}. This system is LR(j)-terminating. This can be shown with the transformation of Definition 3.8 and Theorem 3.20. However, is not well-founded: ge(fe(bl),hl(fe(bl),fe(bl)))

ge(fe(bl),he(fe(bl),fe(bl))) ge(fe(bl),ge(fe(bl),hl(fe(bl),fe(bl)))) ... Note that the term bl at position 1.1 of the starting term is lazy while it would have been eager in the canonically labelled version of the starting term. This being more lazy is the key for the existence of the infinite reduction sequence.

3 Transforming Lazy Rewrite Systems

3.1 Lucas' Transformation

We start with the definition of the transformation of [11], because it provides the basic ideas for our new one. The main idea of the transformation is to explicitly mimic activation steps of lazy rewriting through special activation rules in the transformed system which basically exchange function symbols to make them more eager (this goes back to [13]). Activations in lazy rewriting are possible at positions which correspond to a non-variable position of the left-hand side of some rule in a partial matching. This is why in the transformation we are concerned with non-variable lazy positions of left-hand sides of rules.

The transformation is iterative. In each iteration new rules are created until a fixed point is reached. The following definition identifies for a rule l ^ r and a position p the positions p.i which are lazy in label^(l). These positions are dealt with in parallel in one step of the transformation.

Definition 3.1 ([11]) Let l ^ r be a rewrite rule and p a non-variable position of l, then

I(l,p) = {i G {1,..., ar(root(l\p))} | i G ¡(root(l\p)) A p.i G Poss(l)}.

Example 3.2 Consider a rewrite rule l = f (a,b,x) ^ r and a replacement map ¡(f ) = {1}. Then I(l, e) = {2}.

Definition 3.3 ([11]) Let R = (£,R) be a TRS with replacement map i and let I(l,p) = {i-[,...,in} = 0 for some rule l ^ r G R and p G Poss(l) where root(l\p) = f. The transformed system R® = (E°,R°) and ¡f are defined as follows:

• E® = £ U{fj \ 1 < j < n} where fj are new function symbols of arity ar(fj) = ar(f)

• ¡®(fj) = i(f) u {ij} for all>1 < j < n and i®(g) = i(g) for aU g G £

• R = R -{l - r}U {lj - r | 1 < j < n}U [l[x]p.ij - [x]p,ij | 1 < j < n}

where lj = l[fj(llp1, ..,llp.m)]p if ar(f) = m, and x is a fresh variable.

The transformation of Definition 3.3 is iterated until arriving at a system R = (Y,\RR) and 1 such that I(l,p) = 0 for every rule l — r £ R^ and every position p £ Posy,(1). For further motivation and examples concerning Definition 3.3 we refer to [11].

In [11] it remains unspecified how the pair l,p is selected in one step of the transformation. However, it turns out that the order in which those pairs are considered can be essential.

Example 3.4 Consider the TRS {f (g(a),a) — a,b — f (g(c),b)} with a replacement map ¡(f) = {1} and ¡(g) = 0. This system is not LR(i)-terminating:

be % fe(ge(clfe(ge(cl),be) fe(ge(cl),fe(ge(cl),bl)) ..

However, if we start the transformation with the first rule and position p = e, and consider position 1 of the first rule in the second step of the transformation, then we arrive at the context-sensitive system

f2(gi(a),a) — a f (g1(a),x) — f2(g(a),x)

f2(g(x),a) — f2(gi(x),a) f (g(x),y) — f (g[(x),y) b - f (g(c),b)

with ¡(f) = ¡(g1) = ¡(g[) = {1} and ¡(f2) = {1,2}.4 This system is ¡-terminating (proved with AProVE [5]). The lazy reduction sequence starting from b cannot be mimicked anymore, because due to the two transformation steps first the argument of g has to be activated which prevents the activation of the b in the second argument

3.2 The New Transformation 3.2.1 Definition

In Lucas' transformation, positions that are dealt with last during the transformation must be activated first in rewrite sequences of the transformed system. This can be seen in Example 3.4 where I(f (g(a),a),e) is considered in the first step of the transformation but position 2 must be activated after position 1.1 (whose activation is enabled by a later transformation step considering I(f (g(a),x), 1)).

Thus, the order in which lazy positions of rules are dealt with during the transformation is the reverse order in which they may be activated in the resulting transformed system. Since we want to simulate lazy rewriting, and in lazy rewriting only outermost lazy positions may be activated in a labelled term, we consider more inner lazy positions first in our new transformation. Therefore, with the resulting context-sensitive system more outer positions may be activated only before

4 Here and subsequently the subscripts of function symbols indicate additional replacing positions. So the replacement map of a symbol fi differs from that of f in that i is replacing in fi.

42 F. Schernhammer, B. Gramlich / Electronic Notes in Theoretical Computer Science 204 (2008) 35-51 more inner ones.

Despite considering more inner positions first in the transformation, we do not want to prioritize any (orthogonal) lazy positions. Thus, we define I(l) which identifies the innermost lazy positions in a term with respect to a given replacement map j.

Definition 3.5

I(l) = {p G Poss(l) | p is lazy in label^(l) A

A ($q G Poss(l) : q lazy in label^(l) A q > p)} .

Before presenting the formal definition of our new transformation (see Definition 3.8 below), which crucially relies on Definition 3.5, we want give an informal explanation and illustration of its essential features. In our transformation we distinguish two kinds of rules that are generated. On the one hand we have activation rules which are characterized by the fact that in these rules the left- and right-hand side differ only at exactly one position, where in the right-hand side a different function symbol as in the left-hand side is used. While having the same arity, the different function symbol in the right-hand side has exactly one more replacing position and the argument at that position is a variable (in both sides). All other rules are active rewrite rules.

The actual transformation proceeds in 3 stages. First, a set of initial activation rules is created. These rules enable the activation of one innermost position of a left-hand side of the original rules of the lazy TRS. As already indicated, by a rule activating position p.i we mean a rule l — r where l and r differ only in the function symbol at position p and p.i is replacing in r but non-replacing in l.

In the second stage one rule l — r (activating a position p) created in stage 1 (or stage 2) is replaced by a set of activation rules. This set contains two activation rules for each innermost lazy non-variable position of l. Let q be such a position (note that q is either above or orthogonal to p). Then the first of these two rules is a rule which activates q. Apart from that, both sides of this rule are identical to l (i.e. position p is still inactive). The second rule activates position p. However, in this rule position q is already active. So the second rule differs from the initial rule l — r only in that position q is replacing in both sides (cf. Example 3.6).

Example 3.6 Assume an activation rule l = f (g(x),a) — f (g\(x),a) was generated in step 1 of the transformation where ¡(g) = ¡(f) = 0 and ¡(gi) = {1}. The rule activates position p = 1.1. The left-hand side of this rule (if canonically labelled) has two innermost non-variable lazy positions I(l) = {1, 2}. Thus, it will be replaced by a set of new rules. We first consider the innermost lazy position q = 1. So first, a rule is created which activates q where p is non-replacing (in this special case position p does not even occur in the rule). This rule is f (y, a) — fi(y, a). Second, we create a rule which activates p while q already is active: fi(g(x),a) — fi(g1(x),a). These two rules illustrate that in a system obtained by this kind of transformation more outer positions (like position 1) may be activated only before more inner ones (like 1.1). For the second innermost lazy position of the original rule we also obtain two rules:

f (g(x),z) — f2(g(x),z) f2(g(x),a) — f2(gi(x),a)

We have ¡(fi) = {1} and ¡(f2) = {2}. Note that all of the generated rules still have non-replacing non-variable positions in their left-hand sides. Hence, each of them must be transformed (and replaced) further in the same way as the original rule of this example was processed.

This construction ensures that with the obtained rules in every derivation q (which was considered after p in the transformation) is activated before p, which is sound as p is a more inner (or parallel) position compared to q (since it was considered first).

The latter construction is repeated until the rules obtained do not have any lazy (non-variable) positions. We would like to point out once again that, as we consider innermost positions of terms in stage one and one iteration of stage two in our transformation, the outermost lazy positions of the initial rules of the lazy system are dealt with last. Hence, these are the positions which may be activated first in reduction sequences of the transformed system.

In the third stage of the transformation for each rule of the original lazy system one active rewrite rule is created. This rule differs from the original rule from which it was created only in the fact that the left-hand side is fully activated, i.e. it contains no lazy non-variable position. The reason is that whenever an active reduction step is performed on a lazy sequence it can be simulated by first fully activating the redex with the generated activation rules and afterwards performing the actual active step. This is done in derivations of our transformed system.

Since all new function symbols which are introduced by the transformation are substituted for function symbols of the original signature, we define the mapping orig from the signature of the transformed system into the original signature which identifies for each new function symbol the original one for which it was substituted.

Definition 3.7 Let R = (E,R) be a TRS with replacement map ¡. If in one step of the transformation f G E is replaced by a new function symbol f', then orig(f') = f. Furthermore, if f' is substituted for a function symbol g G E, then orig(f') = orig(g). For function symbols h G E, we set orig(h) = h and for variables we have orig(x) = x.

Definition 3.8 Let R = (E,R) be a TRS with replacement map ¡. The transformed system R = (E, R) with ¡ is constructed in the following three stages.

1 Generation of Initial Activation Rules. The transformed signature E D E and the set A(l) for every rule l — r G R are defined as the least sets satisfying

l[x]p.i — l'[x\p.i G A(l) if p.i G I(l) and l' = l[fi(l|p.1, ...,llp.n)]p (1)

A fi G E

A orig(g) = orig(h) A ¡(g) = ¡(h) ^ g = h for all g,h G E

where ¡1 is defined by ¡(f) = ¡(f) for all f G E and ¡(fi) = ¡(orig(fi)) U {i} if fi was introduced in (1). Then we have R := (Jl^reR A(l).

2 Saturation of Activation Rules.

2.a Processing one Activation Rule Let R = A(l1) U ... U A(lm) and let l — r £ A(lj) for some j £ {1,...,m} such that I(l) is not empty. Then we modify the set A(lj) in the following way:

A(lj) = A(lj) -{l — r} U {l[x]p.z — l'[x]p.i} U {l' — r'}

for all p.i £ I(l) where l' = l[fi(l|p.i, ...,llp.n)\p and r' = r[f(r|p.i, ...,rlp.n)\p. If there is no g £ £ with orig(g) = orig(fi) and ¡(g) = ¡(root^p)) U {i}, then £ = £ U {fi} and ¡(fi) = i(root(l^)) U {i}, otherwise fi = g. Analogously, if there is no g £ £ with orig(g) = orig(f') and ¡(g) = ¡(root(r|p)) U {i}, then £ = £ U {f'} and ¡(f) = ¡(root(r|p)) U {i}, otherwise f[ = g.

R := U A(l)

2.b Iteration Step 2.a is iterated until for all rules l — r of R we have that I (l) = 0.

3 Generation of Active Rewrite Rules. For each rule l — r £ R we add

one active rewrite rule to R. For every position p £ Poss(l), we consider the set

Symb(p,l) = {root(r'|p) | l' — r' £ A(l) A p £ Pos^(r')}.

The function symbol which is least restrictive in this set (i.e., the maximal element of ¡(f) w.r.t. the subset relation of all f £ Symb(p,l)) is unique and denoted by maxSymb(p,l). We set

R := R U [J l

where l" is given by Pos(l) = Pos(l''), root(l''^) = maxSymb(p,l) for all p £ Poss(l) and root^''^) = root^p) for all p £ Posy(l). The signature of the transformed system is not altered in this stage.

The transformation is terminating and produces a finite context-sensitive rewrite system (CSRS) provided that the input is finite. Furthermore, the function symbol maxSymb(p,l) is unique for every rule l — r £ R and every p £ Pos^(l) (cf. [17] for further details).

Example 3.9 Consider the TRS from Example 3.4 {l1 = f (g(a),a) — a,l2 = b — f (g(c),b)} with a replacement map ¡i s.t. ¡(f) = {1} and ¡(g) = 0. In the first stage of the transformation we have I(l1) = {1.1, 2} and the following two initial activation rules are added (i.e. A(l1)). {f(g(x),a) — f(g1 (x),a),f(g(a),x) — f2(g(a),x)} with ¡(g1) = {1} and ¡(f2) = {1,2}. A(l2) = 0, because l2 does not contain any lazy non-variable positions. In step 2.a, the first rule of A(l1) is replaced by {f(g(x),y) — f'2(g(x),y),f2(g(x),a) — f2(g1(x),a)} where the position p.i that was used is 2 (i.e., e.2) and thus the new function symbol introduced is f2. In the second iteration, the second rule of A(l1) is replaced by {f (g(x),y) — f (g1(x),y),f(g1(a),x) — f2(g1(a),x)}. Here, the position p.i that was used is

1.1 and thus the new function symbol is g1. Finally, the following active rewrite rules are added: {/2(g^_(a),a) ^ a,b ^ f (g(c),b)}. For l\ we have Symb(e,l{) = {/,/2}, Symb(1,li) = {g,g1} and Symb(1.1,li) = Symb(2,l1) = {a}. Therefore, maxSymb(e,l1) = f2, maxSymb(1,l1) = g1, maxSymb(1.1,l1) = a and maxSymb(2,l1) = a. For l2 we trivially have maxSymb(e,l2) = b. Hence, the system R consists of

f (g(x),y) ^ h(g(x),y) /2(g(x),a) ^ /2(g1(x),a)

f (g(x),y) ^ f (g1 (x),y) f (g1(a),x) ^ h(g1(a),x)

f2(g1(a),a) ^ a b ^ f (g(c),b)

with Z(f ) = î(g1) = {1}, j(/2) = {1, 2} and jZ(g) = $. R is not jZ-terminating:

b f (g(c),b) /2(g(c),k) f2(g(c),/(g(c),b)) ...

Remark 3.10 Note that the above system could not have been derived with Lucas' transformation regardless of the order in which the positions are processed there. The reason is that when applied to this example, Lucas' transformation always enforces some order of activation of the two orthogonal lazy positions, whereas the new transformation does not.

The rest of the paper is concerned with the proof of soundness and completeness of the transformation of Definition 3.8 w.r.t. termination. First, we will deal with the simpler case of completeness.

3.2.2 Soundness and Completeness

Theorem 3.11 Let R = (£,R) be a left-linear TRS with replacement map ¡, and let R = (E,R), ¡1 be the transformed system (resp. replacement map) according to Definition 3.8. If R is LR(^-terminating, then R is ¡-terminating.

In order to prove the soundness of our transformation, we are going to show the existence of an infinite reduction sequence in the transformed system, whenever there is an infinite lazy reduction sequence in the original system. So assume there is an infinite lazy reduction sequence in a TRS R with replacement map ¡. The first observation is that every lazy reduction sequence naturally corresponds to a context-free (i.e. ordinary) —^-sequence, which performs the active rewrite steps of the lazy reduction sequence. We will construct a —^-reduction sequence in the transformed system R that corresponds to a context-free —^-sequence, cf. Figure 1. Terms in the context-free —^-sequence and terms in the corresponding ¡-sequence are in a special relationship.

Definition 3.12 Let R = (E,R) be a TRS, ¡ a replacement map and let s,t G T(E,V) be two terms. Abusing notation we write s t if and only if

(i) for all positions p G Pos^(t) we have root(t|p) = root(slp), and

(ii) for all minimal positions q G Pos(t) \Pos^(t) we have s|q s' and s' tlq. The idea behind —*c is that context-free reduction steps which occur at positions

erase(ti)-»- erase(t2)->- erase(t3)->- erase(t4) ■

Fig. 1. Relation between the various rewrite sequences occurring in the soundness proof.

that are in the replacing part of the simulating term should be simulated, thus the replacing parts of two terms s and t with s —*Mc t must be entirely equal. On the other hand, context-free steps that occur at positions which are forbidden in the simulating term are ignored. Yet, if the forbidden subterm in which they occur eventually gets activated, then these steps may still be simulated.

As minimal non-replacing positions in a term are always strictly below the root, the recursive description of —*Mc in Definition 3.12 is well-defined.

We have s = t ^ s —*Mc t. Figure 1 illustrates the correspondence between a lazy reduction sequence, the corresponding context-free one, and the ^¿¿-sequence. Note that if the lazy reduction sequence is infinite, then there are infinitely many nonempty steps in the context-free reduction sequence, as every labelled term admits only finitely many activation steps.

In the first part of the soundness proof we show the existence of a ^^-sequence of the shape as in Figure 1.

The next lemma establishes the relationship between a context-free reduction sequence and a corresponding —m reduction of Figure 1.

Lemma 3.13 Let (R = (£,R),i) be a TRS with replacement map and let (R = (£ ,R),]j) be the system obtained by the transformation of Definition 3.8. Let s and t be terms from T(£,V), such that s —Mc t. If t — t* (with a rule l — r) and

p £ PosM(s), then s s* and s* —Mc t*. Otherwise, if t — t* and p £ PosM(s), M '

then s —M s* and s* —Mc t*. M M

Unfortunately, the last lemma and the correspondence of lazy, context-free and —M-reduction sequences of Figure 1 are not sufficient to prove the existence of an infinite —M-sequence in the presence of an infinite lazy reduction sequence, since there may be only finitely many non-empty —M-reductions in the simulating sequence.

Example 3.14 Consider a TRS R consisting of the rules {a — f (a),f(b) — b}. This system is not LRM-terminating when considering a replacement i with ¡(f) = 0. Consider the infinite lazy reduction sequence

ae R fe(al) R f e(ae) % ■■■

The transformed system is {a — f (a),f(x) — fi(x),fi(b) — b} with ¡(f) = 0 and ¡(fi) = {1}. We can simulate the infinite lazy reduction sequence by a —m f (a) —m fi(a) —e ■■■ Note that in this simulating derivation function symbols of

the extended signature are introduced which are not immediately "consumed" by a more outer active rewrite step. Example 3.15 shows that this can lead to problems.

Example 3.15 Consider a non-terminating TRSR = {f (g(x)) ^ f (g(x)),g(a) ^ g(b),a ^ c} with a replacement map ¡ given by ¡(f) = {1} and ¡(g) = ty. The transformed system R is {f (g(x)) ^ f (g(x)),g(x) ^ gi(x),gi(a) ^ g(b),a ^ c} with ¡(f) = ¡(gi) = {1} and ¡(g) = ty. Consider the context-free reduction sequence f(g(a)) ^ f(g(c)) ^ f(g(c)) ^ .... If we activate position 1.1 in f(g(a)) in the simulating ^¿¿-sequence, we cannot further extend f (g(a)) f (gi(a)) f (gi(c)), because the term f (gi(c)) is a ^¿-normal form.

The crucial difference why the activation of a subterm is essential in Example 3.14 and unnecessary in Example 3.15 is that in the former example the activated subterm itself initiates an infinite lazy reduction sequence. This observation will be used in the second part of the soundness proof (cf. Theorem 3.20).

When constructing an infinite reduction sequence in the transformed system corresponding to an infinite lazy sequence in the soundness proof, we will identify those activations that activate a non-terminating subterms sinf and simulate them by activating the corresponding subterm s'inf in the simulating sequence. Afterwards, we will focus only on an infinite lazy reduction sequence initiated by sinf. This way the simulated activation, i.e., the introduction of a function symbol of the new signature, is of no relevance for the further simulation as it happened outside

of sinf.

With the following definition we intend to identify labelled terms in an infinite lazy reduction sequence with non-terminating proper subterms that have possibly been activated. For such terms t, the predicate mininf (t) does not hold.

Definition 3.16 Let £ be a signature and ¡ be a replacement map for £. A labelled term t is said to be minimal non-terminating if it admits an infinite lazy reduction sequence and for each eager labelled proper subterm t\p of t, either t\p does not initiate an infinite lazy rewrite sequence, or position p is eager in the term labele(erase(t)). We write mininf (t) if t has this property.

Definition 3.17 Let R = (£,R) be a TRS with replacement map ¡. Let t be a labelled term t and t LSe s be an activation step. This activation step is called inf-activating (thus it is an inf-activation stepj if and only if mininf (t) but not mininf (s).

It is easy to see that whenever mininf (t) holds for a labelled term t, there is

no active position p e Act(t) which is non-active in labele(erase(t)), such that t\p

initiates an infinite lazy reduction sequence.

In the second part of the soundness proof we will show that each infinite lazy

reduction sequence contains either an inf-activation step or an active rewrite step LR

s ^e t at position p such that p is ¡-replacing in erase(s). Furthermore, such steps result in non-empty simulations by the ^¿-sequence.

Lemma 3.18 Let R = (£,R) be a TRS with replacement map ¡. Let t be a labelled term satisfying mininf (t). Then we have:

(i) If t LRM s with an inf-activation step at position q1 activating position q2 and qi < p < q2 is the maximal (w.r.t. <) eager position in s which does initiate an infinite reduction sequence s.t. t\p does not, then we have mininf (s\p).

(ii) If t LRM s with any other step than in (i) (i.e., activation steps which are not inf-activating, or active rewrite steps), then mininf(s).

The next lemma characterizes infinite lazy reduction sequences starting from minimal non-terminating labelled terms. It states that in such an infinite lazy reduction sequence after finitely many steps there is either an active rewrite step si -RM si+i at some position p which is active in labelM(erase(si)) or there is an inf-activation step. We already know from Lemma 3.13 that active rewrite steps at such positions can be simulated by a non-empty sequence in the transformed system (remember that the active rewrite steps of a lazy reduction sequence correspond to a context-free derivation). In Theorem 3.20 we will prove that the same is true for inf-activation steps.

Lemma 3.19 Let R = (£,R) be a TRS with a replacement map ¡. Let t0 be a labelled term with the property mininf (to). Let P : t0 -RM ti -RM ■ ■■ -RM tn -RM ■ ■■ be an infinite lazy reduction sequence starting from t0. Then,, eAther there is an active rewrite step ti -RM ti+i at position p, where p is active in labelM(erase(ti)), or there is an inf-activation step in P.

Theorem 3.20 Let (R = (£,R),i) be a left-linear TRS with replacement map and let (R = (£ ,R),Ji) be the system obtained by the transformation of Definition 3.8. If R is ¡-terminating, then R is LR(i)-terminating.

Proof. (Sketch) 5 We will show that the existence of an infinite lazy reduction

sequence P : t0 -RM ti -RM ■ ■■ (where t0 is canonically or more liberally labelled) implies the existence of an infinite reduction sequence in the transformed system. The following invariant will be maintained for every labelled term ti of an infinite reduction sequence P. Let s0 —m si —m ■■■ be the simulating reduction sequence we are going to construct:

There is a sj and a position o such that

erase(ti\o) A mininf (ti\o)

and position o is ¡-replacing in sj and active in ti. Furthermore, U\o is at least as eager as its canonically labelled version (i.e., whenever labelM(erase(ti\o)) has an eager label at some position q, then the label of t^\o is eager at that position, too). Note that the latter condition is trivially fulfilled by all terms ti in P, and thus by all subterms, since no "deactivations" are possible in lazy rewriting and active rewrite steps only introduce canonically labelled terms.

We show that a finite initial subsequence of P implies the existence of a nonempty reduction sequence in the transformed system which preserves the invariant. As each term ti\o itself initiates an infinite lazy reduction sequence, this suffices to

5 For a more detailed version of the proof we refer to [i7]

show that there is an infinite reduction sequence in the transformed system.

In order to apply Lemma 3.19, we assume mininf (to). This minimality constraint can be satisfied, as w.l.o.g. we can find a t0 such that each proper subterm of to with an eager label does not initiate an infinite lazy reduction sequence.

The infinite reduction sequence we are going to construct in the transformed system starts with the term s0 = erase(t0). We have s0 erase(t0).

Lemma 3.19 states that in the lazy reduction sequence starting from t0 there

is either an active rewrite step ti ^^ ti+i at position p such that p is active in

label ll(erase(ti)), or there is an inf-activation step. Let tj ^^ tj+i be the first such step.

The goal is to show that the reduction sequence t0 ^^ tj+i can be simulated

by a sequence s0 si such that si\o erase(tj+1\o) and mininf (tj+1|o) holds

for some position o which is active in tj+i and replacing in si. We make a case

distinction on whether the step tj ^ M tj+i is an active rewrite step or an inf-activation step.

(i) Assume the step tj ^M tj+i is an active rewrite step at position p such that p is active in label^(erase(tj)). Then, by using Lemmata 3.13 and 3.18 we

on sequence s0 —^ si —+

si+i erase(tj+i) and mininf (tj+i

can derive that there exists a reduction sequence s0 si ^+ si+i such that

f~l fLi

(ii) Assume the step tj ^M tj+i is an inf-activation step. This implies that a sub-term tj+i\p is activated, which is non-L^(^)-terminating (furthermore, mininf (tj+i\p) holds according to Lemma 3.18). Then we can construct a simulating sequence s0 si s* where in the non-empty subsequence si ^+ s* position p is activated and the subterm at position p is reduced in order to obtain s*i\p erase(tj+i\p) (cf. Definition 3.12).

Now given an infinite lazy reduction sequence P starting from a labelled term t0 and a term s0 with s0 c erase(t0), we have shown that a finite subse-

quence t0 ^ + ti of a special shape implies the existence of a non-empty sequence s0 sj such that sj\p erase(ti\p), where p is active in ti and replacing in sj, mininf (ti\p) holds and ti\p initiates an infinite lazy reduction sequence. Thus, again the infinite lazy sequence starting at tj\p has a finite subsequence, that can be simulated by a non-empty reduction sequence in the transformed system. By repeating this construction, we get an infinite reduction sequence in the transformed system starting at s0. □

4 Experiments and Discussion

In the proof of Theorem 3.20 we saw that the context-sensitive rewrite system (CSRS) obtained by our transformation cannot simulate lazy reduction sequences in a one-to-one fashion. When simulating infinite lazy reduction sequences, after every inf-activation step in the lazy reduction an entirely new infinite lazy sequence was considered, namely the one initiated by the activated subterm. Thus, the ques-

tion arises whether we can define a transformation from lazy rewrite systems into context-sensitive ones, such that the transformed system is able to fully simulate the lazy reduction system. We conjecture that this is indeed possible ([16]), but would render termination proofs more difficult. The reason is that such a transformation would need to introduce even more rules that alter the status (i.e., lazy or eager) of positions in lazy terms (to be more precise, more activation rules would be needed).

Regarding the size of the transformed system, the number of rules created by our transformation is in general exponentially higher than the number of lazy nonvariable subterms in left-hand sides of rules of the lazy system. However, we found that in our test series that was taken from the termination problem database ([18]) this number was not too high in most of the systems. This leads to the question whether our transformation is practically feasible in proofs of termination of real-world lazy TRSs. A series of tests were performed to answer this question, cf. [17]. As this is the first experimental termination analysis of lazy TRSs, neither lazy test TRSs nor benchmarks of other methods for proving lazy termination were readily available. Thus, we used the context-sensitive TRSs from the [18] and interpreted these systems as lazy ones. Out of 53 lazy systems that were tested, lazy termination of 35 could be shown. The tests were performed using AProVE ([5]) for proving context-sensitive termination of the CSRSs obtained by our transformation (with a time limit of 10 seconds). When interpreting these results one has to keep in mind that the example TRSs considered were actually supposed to be considered context-sensitive by their authors. Thus, in many cases the changes made by our transformation were only minimal. There were only 3 systems (namely Exl_Zan97.trs, Ex9_Luc06 and Ex14_AEGL02) for which termination could be proved in the context-sensitive case but not in the lazy case. However, all these systems actually become non-terminating when considered to be lazy instead of context-sensitive. This indicates that in many practical cases the analysis of lazy termination with our approach may well be feasible and a priori not too hard. Furthermore, lazy termination analysis can greatly benefit from ongoing research in the field of context-sensitive termination.

Acknowledgement

We would like to thank the anonymous reviewers of the previous workshop submission for numerous useful hints and criticisms.

References

[1] M. Alpuente, S. Escobar, B. Gramlich, and S. Lucas. On-demand strategy annotations revisited. Technical Report DSIC-II/18/03, UPV, Valencia, Spain, 2003.

[2] F. Baader and T. Nipkow. Term rewriting and all that. Cambridge University Press, New York, NY, USA, 1998.

[3] W. Fokkink, J. Kamperman, and P. Walters. Lazy rewriting on eager machinery. ACM Transactions on Programming Languages and Systems, 22(l):45—86, 2000.

D. P. Friedman and D. S. Wise. CONS should not evaluate its arguments. In S. Michaelson and R. Milner, eds., Proc. 3rd ICALP, pp. 257-284. Edinburgh University Press, 1976.

J. Giesl, R. Thiemann, and P. Schneider-Kamp. AProVE 1.2: Automatic termination proofs in the dependency pair framework. In Ulrich Furbach and Natarajan Shankar eds., Proc. IJCAR'06, LNCS

4130, pp. 281-286, 2006.

J. Giesl, S. Swiderski, R. Thiemann, and P. Schneider-Kamp. Automated Termination Analysis for Haskell: From Term Rewriting to Programming Languages In F. Pfenning, ed., Proc. RTA'06, LNCS 4098, pp. 297-312. Springer, 2006.

J. Giesl, R. Thiemann, and P. Schneider-Kamp. Proving and Disproving Termination of Higher-Order Functions In B. Gramlich, ed., Proc. FROCOS>05, LNAI 3717, pp. 216-231. Springer, 2005.

P. Henderson and J. H. Morris Jr. A lazy evaluator. In Conference Record of the Third ACM Symp. on Principles of Programming Languages, Atlanta, Georgia, Jan. 1976, pp. 95-103, 1976.

S. Lucas. Context-sensitive computations in functional and functional logic programs. Journal of Functional and Logic Programming, l998(l), l998.

S. Lucas. Termination of on-demand rewriting and termination of OBJ programs. In H. Sondergaard, ed., Proc. PPDP'01, pp. 82-93, 2001. ACM Press, New York.

S. Lucas. Lazy rewriting and context-sensitive rewriting. In M. Hanus, ed., Proc. WFLP'01, ENTCS 64, Elsevier, 2002.

S. Lucas. Proving termination of context-sensitive rewriting by transformation. Information and Computation, 204(l):l782-l846, 2006.

Q. H. Nguyen. Compact normalisation trace via lazy rewriting. In B. Gramlich and S. Lucas, eds., Proc. WRS'01, ENTCS 57, 2001.

M. J. Plasmeijer and M. C. J. D. van Eekelen. Functional programming and parallel graph rewriting. Addison-Wesley, l993.

R. Strandh. Classes of equational programs that compile into efficient machine code. In N. Dershowitz, ed., Proc. RTA'89, LNCS 355, pp. 449-461. Springer, 1989.

F. Schernhammer. On context-sensitive term rewriting. Master's thesis, TU Wien, February 2007. 2007.

F. Schernhammer and B. Gramlich. Termination of lazy rewriting revisited. Technical Report El852-2007-01 (available at http://www.logic.at/people/schernhammer/papers/), TU Wien, 2007.

Termination Problem Database. Available at http://www.lri.fr/~marche/tpdb.