Context-Sensitive Rewriting, Lazy Rewriting, and On-demand Rewriting? Salvador Lucas DSIC, Universidad Politecnica de Valencia Camino de Vera s/n, E-46022 Valencia, Spain e.mail:
[email protected]
Abstract. On-demand rewriting provides an operational model for term rewriting controlled by annotations consisting of (sets of) positive and negative integers referring the arguments of function symbols. Contextsensitive rewriting and lazy rewriting provide (dierent) operational models for positive annotations. In this paper we prove that, under certain conditions, the three operational models coincide. In this case, it makes sense using context-sensitive rewriting as it is the simplest one. This fact also permits proving termination of lazy rewriting by proving termination of context-sensitive rewriting for a transformed rewrite system. Keywords: Declarative programming, replacement restrictions, term rewriting, termination.
1 Introduction Syntactic annotations (i.e., associated to the arguments of symbols) have been used in programming languages such as Lisp, Haskell, Clean, OBJ*, CafeOBJ, Maude, etc., to improve termination and eciency of computations. Lazy languages (e.g., Haskell, Clean) interpret them as strictness annotations to become `more eager' and ecient. Eager languages (e.g., Lisp, OBJ*, CafeOBJ, Maude) use them as replacement restrictions to become `more lazy' thus (hopefully) avoiding nontermination. For instance, [FW76] studied implementations of Lisp where the list constructor operator cons did not evaluate its arguments, during certain stages of the computation. Algebraic languages, such as OBJ2 [FGJM85], OBJ3 [GWMFJ00], CafeOBJ [FN97], or Maude [CELM96] admit the speci cation of local strategies which are sequences of integers in parentheses. Replacement restrictions have demonstrated their usefulness in practice, since they actually improve eciency of computations by reducing the number of attempted matchings (see [FGJM85]). However, the possibility of introducing replacement restrictions should be made available together with some additional formal support allowing the programmer a clear (and simple) understanding of their in uence on the operational behavior of the program. In [Luc01] we have introduced on-demand rewriting (ODR ), an extension of context-sensitive rewriting (CSR [Luc98]) which can be used to provide an operational model for ?
Work partially supported by Spanish CICYT and Conselleria de Cultura y Educacio de la Generalitat Valenciana.
computations issued with OBJ (like) programs. In ODR , positive and negative annotations are managed by using a pair h; D i of replacement maps (for positive annotations) and D (for absolute values of negative ones). Following [Luc98], a replacement map for a signature is a mapping : ! P (N) that satis es (f) f1; : : :; ar(f)g. Given a TRS R and a pair h; D i, the ondemand reduction relation ,!h;D i is associated to R (see Section 3). A positive annotation i for f permits reductions on the i-th argument of f; a negative annotation ?i activates reductions `on-demand'. The absence of both i and ?i forbids reductions on the i-th argument of f. Termination of ODR is investigated in [Luc01]. In this paper, we study monotony of ODR : in Section 4, we de ne an ordering vD on pairs of replacement maps which is compatible with the ondemand rewrite relation, i.e., if h; D i vD h0 ; 0D i, then ,!h;D i ,!h0 ;0D i . In this way, the programmer can compare dierent annotations for the program. In Section 5, we consider Fokkink et al.'s lazy graph rewriting (LR [FKW00]), a restriction of (graph) rewriting that provides a (non-deterministic) operational model for using replacement restrictions speci ed by a single replacement map . Lazy rewriting is intended to improve the termination behavior of TRSs [FKW00]. Unfortunately, no analysis of termination of LR is available yet. In this paper, we adapt Fokkink et al.'s framework to lazy term rewriting and provide a transformation for proving termination of LR by using the techniques for proving termination of CSR (see, e.g., [GM99,Luc98,Zan97]). We show that under some conditions (namely, that all non-variable subterms of the left-hand sides of rules are -replacing), CSR , LR , and ODR coincide. In this case, it makes sense using CSR as it is the simplest one. This is interesting since, looking for better implementations of LR , [FKW00,Ngu01] pay some attention to develop transformation techniques for achieving this condition thus (silently) using CSR rather than LR . Section 6 concludes.
2 Preliminaries Given a set A, P (A) denotes the set of all subsets of A. Given a binary relation R on a set A, we denote its transitive closure by R+ and its re exive and transitive closure by R . An element a 2 A is an R-normal form, if there is no b such that a R b. NFR is the set of normal forms of R. We say that b is an R-normal form of a, if b is an R-normal form and a R b. We say that R is terminating i there is no in nite sequence a1 R a2 R a3 . Throughout the paper, X denotes a countable set of variables and denotes a signature, i.e., a set of function symbols ff; g; : : :g, each having a xed arity given by a mapping ar : ! N. The set of terms built from and X is T (; X ). A term is said to be linear if it has no multiple occurrences of a single variable. Terms are viewed as labelled trees in the usual way. Positions p; q; : : : are represented by chains of positive natural numbers used to address subterms of t. Given positions p; q, we denote its concatenation as p:q. Positions are ordered by the standard pre x ordering . Given a set of positions P, minimal (P) is the set of minimal positions of P w.r.t. . If p is a position, and Q is a set of positions, p:Q =
fp:q j q 2 Qg. We denote the empty chain by . The set of positions of a term t is P os(t). Positions of non-variable symbols in t are denoted as P os (t), and P osX (t) are the positions of variables. The subterm at position p of t is denoted as tjp, and t[s]p is the term t with the subterm at position p replaced by s. The symbol labelling the root of t is denoted as root(t). The chain of symbols lying on positions above/on p 2 P os(t) is pre x t() = root(t), pre x t(i:p) = root(t):pre x tji (p). The strict pre x is spre x t() = , spre x t (p:i) = pre x t (p). A rewrite rule is an ordered pair (l; r), written l ! r, with l; r 2 T (; X ), l 62 X and V ar(r) V ar(l). The left-hand side (lhs) of the rule is l and r is the right-hand side (rhs). A TRS is a pair R = (; R) where R is a set of rewrite rules. L(R) denotes the set of lhs's of R. A TRS R is left-linear if for all l 2 L(R), l is a linear term. A term t 2 T (; X ) rewrites to s (at position p), written t !p R s (or just t ! s), if tjp = (l) and s = t[(r)]p, for some rule l ! r 2 R, p 2 P os(t) and substitution .
3 Context-sensitive rewriting and on-demand rewriting Given a signature , a mapping : ! P (N) is a replacement map (or map) if for all f 2 ; (f) f1; : : :; ar(f)g [Luc98]. The inclusion ordering on P (N) extends to an ordering v on M , the set of all -maps: v 0 if for all f 2 , (f) 0 (f). The Boolean algebra (P (N); ; ?; N; [; \; c) induces a Boolean algebra (M ; v; ?; > ; t; u; c): The minimum (maximum) element is ? (> ), given by ? (f) = ? (> (f) = f1; : : :; ar(f)g) for all f 2 . The least upper bound (lub), t, and greatest lower bound (glb), u, are given by ( t 0 )(f) = (f) [ 0(f) and ( u 0 )(f) = (f) \ 0(f) for all f 2 . The complement c of 2 M is c (f) = f1; : : :; ar(f)g ? (f) for all f 2 . We also de ne the dierence 1 ? 2 of 1; 2 2 M by 1 ? 2 = 1 u c2 (or (1 ? 2 )(f) = 1 (f) ? 2(f) for all f 2 ). Given 2 M , the set ofS-replacing positions P os (t) is: P os (t) = fg, if t 2 X and P os (t) = fg [ i2(root(t)) i:P os (tji), if t 62 X . The non-replacing positions are P os (t) = P os(t) ? P os (t). In context-sensitive rewriting (CSR [Luc98]), we (only) rewrite redexes at replacing positions: t -rewrites to s, p written t ,! s, if t ! R s and p 2 P os (t). Example 1. Consider the TRS [NO01]: 2nd(x:y:z) from(x)
! !
y x:from(s(x))
and (:) = (2nd) = (from) = (s) = f1g. Then we have: 2nd(from(0)) ,! 2nd(0:from(s(0))) where, since 1:2 62 P os (2nd(0:from(s(0)))), -rewritting stops here. A TRS R is -terminating if ,! is terminating. Given a pair h; D i of replacement maps and D , we de ne on-demand rewriting (ODR [Luc01]) as an extension of CSR (under ), where we also permit reductions on-demand according to D . Given f 2 , indices j 2 D (f) are aimed at enabling reductions on
a subterm tj of a function call f(t1 ; : : :; tj ; : : :; tk ) if they can eventually lead to matching a pattern of a rule de ning f (i.e., l ! r 2 R such that root(t) = f). Let Lazyh;D i (t) = minimal (P os (t)) \ P ostD (t). De nition 1 (On-demand rewriting [Luc01] ). Let R = (; R) be a TRS p s and either and ; D 2 M . Then, t ,!h;D i s, if t ! 1. p 2 P os (t), or 2. p 2 P ostD (t) ? P os (t) and there exist e 2 P os (t), r1; : : :; rn; t0 2 T (; X ), p1 ; : : :; pn 2 Lazy h;D i (t), l ! r 2 R, and substitution s.t. (1) e p, t0 = t[r1]p1 [rn]pn , t0 je = (l) and (2) for all q 2 P os(l) s.t. spre xtje (q) = spre xl (q), whenever e:q p, we have that ljq 62 X .
p Thus, a rewriting step t !p s is on-demand (w.r.t. and D ) if either t ,! s; p or t ,!tD s and reducing tjp may contribute to a future -rewriting step at -replacing position e, using some rule l ! r. Such a contribution is approximated by checking whether changing some non--replacing maximal subterms of t would eventually make the matching possible (condition 2(1) of De nition 1). Indices in D determine the positions (in Lazyh;D i (t)) of the subterms of t that can be re ned. Note that p is always covered by some position pi 2 Lazy h;D i (t), i.e., pi p (Figure 1, left). t t
e e µ - replacing
l
non- µ - replacing
µ - replacing
l
non- µ - replacing
p non- µ - replacing
p redex
redex non- µ - replacing
variable of l
Fig. 1. Scheme of a possible/forbidden on-demand step On the other hand, the position p on which the rewriting step is performed is constrained to having no variable position of l covering p (condition 2(2) of De nition 1); otherwise, the reduction at tjp would not improve the matching (Figure 1, right). Example 2. Consider R and as in Example 1. Let D (:) = f2g. We have: 2nd(from(0)) ,!h;D i 2nd(0:from(s(0))) ,!h;D i 2nd(0:s(0):from(s(s(0)))) ,!h;D i s(0) but 2nd(0:s(0):from(s(s(0)))) ,6 !h;D i 2nd(0:s(0):s(s(0)):from(s(s(s(0)))) since spre xt (1:2:2) = spre xl (1:2:2), but lj1:2:2 2 X (where l = 2nd(x:y:z)). ,!h;D i -normal forms are called h; D i-normal forms.
4 Monotony of on-demand rewriting A simple fact about CSR is that it is monotone w.r.t. the ordering v. Proposition 1. [Luc98] Let R = (; R) be a TRS and ; 0 2 M . If v 0, then ,! ,!0 . Monotony of CSR con rms what is intuitively clear: ,! provides less posibilities than ,!0 for reduction, whenever v 0. Concerning monotony of ODR , the most natural extension of v on M M , i.e., the product order v given by h; D i v h0 ; 0D i if and only if v 0 ^ D v 0D , does not work. Example 3. Consider the TRS R: c d
! !
h(c) b
f(a,b)
!
h(a)
and t = f(c,c). Let (f) = (h) = ?, D (f) = f1; 2g, and D (h) = ?. Let 0(f) = f1g, 0(h) = ?, and 0D = D . Obviously, h; D i v h0 ; 0D i. Note that 1; 2 2 Lazy h;D i (t). Then, since t[a]1[b]2 = f(a,b), we have f(c,c) ,!h;D i f(c,h(c)) However, since 1 62 Lazyh0 ;0D i (t), it follows that f(c,c) 6,!h0 ;0D i f(c,h(c)) since t[b]2 = f(c,b) 6= f(a,b). Hence, ,!h;D i 6 ,!h0 ;0D i . Thus, ODR could exhibit the counterintuitive property that a more restrictive pair of replacement maps h; D i v h0; 0D i enables reductions on h0 ; 0D inormal forms. De nition 2. Let be a signature. We de ne the preorder vD on M M as follows: given ; D ; 0; 0D 2 M , h; D i vD h0; 0D i , v 0 ^ (D ? ) v (0D ? 0) Theorem 1. Let R = (; R) be a TRS and ; D ; 0; 0D 2 M . If h; Di vD h0; 0D i, then ,!h;D i ,!h0 ;0D i . Corollary 1. Let R = (; R) be a TRS and ; D; 0D 2 M . If D v 0D , then ,!h;D i ,!h;0D i . Having a preorder instead of a partial order is not completely satisfactory. The following proposition allows us to improve this situation. Proposition 2. Let R be a TRS and ; D 2 M . Then, ,!h;D i =,!h;D ?i . Therefore, replacement maps and D are not completely independent regarding the de nition of ODR . We formalize this fact by only considering pairs of replacement maps and D that are independent in the sense that u D = ? . De nition 3. Given a signature , we let MD = fh; Di 2 M M j u D = ? g.
The mapping given by h; D i 7! h; D ? i for all h; D i 2 M M maps M M onto MD . Since Proposition 2 ensures that ,!h;D i =,!h;D ?i , it follows that restricting the attention to MD does not entail lack of generality. Given an order (A; ) and B A, jB denotes the order jB = f(a; b) 2 j a; b 2 B g on B. Now we have the following. Proposition 3. Let be a signature. Then, vDjMD = vjMD . Thus, Proposition 3 allows us to use the ordered set (MD ; v ), where we write just v rather than vjMD .
5 Lazy rewriting In lazy graph rewriting [FKW00], reductions are issued on labelled graphs. We adapt the framework to lazy term rewriting on labelled terms. An alternative adaptation can be found in [Ngu01]. We believe that ours is simpler and closer to [FKW00]. Terms are built from a lazy signature, i.e., a signature supplied with a laziness predicate on N that holds for (f; i) if and only if 1 i ar(f) and the ith argument of f is lazy (De nition 3.1.1 of [FKW00]). Laziness predicate can actually be identi ed with a replacement map : 8f 2 ; ( i 2 (f) , :(f; i) ) We also label each node (or position) of a term t using e for eager positions or ` for lazy ones. Let L = fe; `g; labelled terms are terms in T ( L; X L), which we denote as T (L ; XL). The labelling of a symbol f 2 is denoted f e or f ` rather than hf; ei or hf; `i. The arities are naturally extended: ar(f e ) = ar(f l ) = ar(f) for all f 2 . Given 2 M , the mapping label : T (; X ) ! T (L ; XL) (or just label if no confusion arises) provides a canonical labelling of a term: given t 2 T (; X ), the topmost position of label(t) is always eager; given a position p 2 P os(label(t)) and i 2 f1; : : :; ar(root(tjp))g, position p:i of label(t) is lazy if and only i 62 (root(tjp )); otherwise, it is eager (De nition 3.1.2 of [FKW00]). Example 4. Consider R and as in Example 1. Then, the labelling of term s = 2nd(0:from(s(0))) is t = label(s) = 2nde(0e :efrom`(se (0e))): It means that ; 1; 1:1; 1:2:1, and 1:2:1:1 are eager positions; position 1:2 is lazy. Given t 2 T (L ; XL ), erase(t) is the term in T (; X ) that (obviously) corresponds to t after removing labels. Note that erase label = idT (;X ) but label erase 6= idT (L;XL ) . Given t 2 T (L ; XL), a position p 2 P os(t) is eager (resp. lazy) if root(tjp ) is labelled with e (resp. `). The so called active positions of t are de ned inductively as follows: is an active position; if p 2 P os(t) is active, then p:i is active for all eager position p:i, 1 i ar(root(tjp )) of t (De nition 3.1.3 of [FKW00]). Active positions are always reachable from the root of the term via a path of eager positions. Eager positions do not necessarily satisfy this.
Example 5. (continuing Example 4) Positions ; 1, and 1:1 are active in
t = 2nde(0e:e from`(se(0e ))) Positions 1:2:1 and 1:2:1:1 are eager but not active, since position 1:2 below is lazy in t. Given t 2 T (; X ) and 2 M , the set of active positions of label(t) coincides with P os (t). An important feature of lazy rewriting on labelled terms is that the set of active nodes may increase as reduction of labelled terms proceeds. Each lazy reduction step on labelled terms may have two dierent eects: 1. changing the status (active or not) of a given position within a term, or 2. performing a rewriting step (always on an active position). In the following, we formally describe them as reduction of labelled terms.
5.1 Activating positions for reduction The activity status of a lazy position immediately below an active position within a (labelled) term can be modi ed if the position is `essential', i.e., its contraction may lead to new redexes at active nodes [FKW00]. De nition 4 (Matching modulo laziness [FKW00]). Let l 2 T (; X ) be linear, t 2 T (L ; XL), and p be an active position of t. Then, l matches modulo laziness s = tjp if either l 2 X , or l = f(l1 ; : : :; lk ), s = f e (s1 ; : : :; sk ) and, for all i 2 f1; : : :; kg, if p:i is eager, then li matches modulo laziness si . If position p:i is lazy and li 62 X , then position p:i is called essential. Example 6. Consider the TRS R of Example 1. Then, the lhs 2nd(x:y:z) matches modulo laziness the labelled term t = 2nde(0e :efrom`(se (0e))). According to De nition 4, position 1:2 of t becomes essential. Note that if l 2 T (; X ) matches modulo laziness an active labelled subterm s = tjp without producing essential positions, then l matches erase(s) in the usual sense. Changes in `activity' of positions are formalized by the following. De nition 5. Let R = (; R) be a left-linear TRS. The relation of activation A ! between labelled terms is de ned as follows. Let p be active in t 2 T (L ; XL) and l ! r 2 R be such that l matches modulo laziness tjp . Let q be an essential A lazy position of t and tjq = f ` (s1 ; : : :; sk ). Then, t ! t[f e (s1 ; : : :; sk )]q . A Note that ! is a terminating relation: only a nite number of relabellings (from A lazy to eager) is possible for nite terms. In general, ! is not con uent. Example 7. Consider the TRS R: f(c(d,a)) b
! !
a f(c(b,d))
Then, we have
fe (c`(b` ,d`))
A e e ` ` A ! f (c (b ,d )) ! fe (ce(be,d` )) A and fe (ce(be,d` )) is a ! -normal form, since f(c(d,a)) does not match term
fe(ce (be,d` ))
modulo laziness. However,
A A fe (c`(b` ,d`)) ! fe (ce(b` ,d`)) ! fe (ce(b` ,de))
A thus leading to a dierent ! -normal form. The canonical replacement map can R associated to a TRS R = (; R), is the most restrictive replacement map (of M ) that permits replacements at every non-variable position of the lhs's of R, i.e., the minimum -map such that 8l 2 L(R); P os (l) P os (l) [Luc98]. According to this, we have the following. Proposition 4. Let R = (; R) be a a left-linear TRS and 2 M be such A that can R v . Then, label(t) is a !-normal form.
5.2 Reducing active positions Let l 2 T (; X ) be a linear term, t 2 T (L ; XL), p 2 P os(t), and s = tjp . If l matches erase(s), then we let the mapping l;s : X ! T (L ; XL) to be l;s (x) = sjq if 9q 2 P os(l) such that ljq = x 2 X
xe otherwise, where the labelling e for x is arbitrary From l;s , we obtain a substitution on labelled terms as the homomorphic extension of the following: for all x 2 X , e (x) = y 2 XL (xe ) = fy e (t ; : : :; t ) ifif l;s (x) k l;s = f (t1 ; : : :; tk ) ` 1 = y 2 XL (x` ) = yf ` (t ; : : :; t ) ifif l;s (x) 1 k l;s (x) = f (t1 ; : : :; tk ) for 2 fe; `g. This is according to De nition 3.2.3 in [FKW00]: when a substitution on labelled terms applies to a labelled term t, the labelling that corresponds to the symbol in position q in (t) is that of q in t, for every variable position q 2 P osX (t). Thus, we de ne the following. De nition 6. Let R = (; R) be a left-linear TRS and 2 M . The relation R of active rewriting ! between labelled terms is de ned as follows. Let p be an active position of t 2 T (L ; XL), s = tjp and l ! r 2 R be such that l matches R erase(s). Let l;s be the corresponding mapping. Then, t ! t[(label(r))]p . Example 8. Consider the TRS f(b,x)
!
g(x)
and (f) = (g) = f1g. Let t = fe (be,a` ); notice that label(g(x)) = ge (xe). Then, f(b,x) matches erase(t). We have f(b,x);t(x) = a` . We obtain the substitution given by (xe ) = ae and (x` ) = a` . Then,
fe (be,a` )
!R ge (ae)
R The following connection between ! and CSR is interesting.
Proposition 5. Let R = (; R) be a left-linear TRS, 2 M , t 2 T (; X ) R and s0 2 T (L ; XL). Then, label(t) ! s0 if and only if 9s 2 T (; X ); t ,! s and s0 = label(s)
R Remark 1. Example 8 shows that ! -steps can also indirectly activate lazy po-
sitions after contracting a (labelled) redex. For instance, we can thought of the R ! -step on t = fe(be ,a`) as activating the lazy occurrence of a in t when it is reduced into ge (ae).
5.3 Lazy rewriting vs. other restrictions of rewriting The lazy graph rewriting as given in De nition 3.2.3 of [FKW00] corresponds LR A R to relation ! = ! [ ! on labelled terms, LR . Whenever LR is used for LR evaluating an unlabelled term t 2 T (; X ), we are actually interested in ! reductions issued from label(t). In this way, as done in [NO01,OF00] for OBJ (like) languages (and it is implicit in [FKW00]), we can de ne an evaluation semantics, i.e., a mapping LR -eval : T (; X ) ! P (T (; X )) that obtains the evaluation of a given term by using LR : LR LR -eval (t) = ferase(s) 2 T (; X ) j label(t) ?! LR g s ^ s 2 NF!
According to this, given R = (; R) and 2 M , we say that
LR R is LR ()-terminating if, for all t 2 T (; X ), there is no in nite ! -
rewrite sequence starting from label(t). We have the following. Theorem 2. Let R = (; R) be a left-linear TRS, 2 M , and t; s 2 T (; X ). LR Then, t ,! s if and only if label(t) ! label(s). Theorem 2 expresses that CSR can always be seen as a restriction of LR that only considers `canonically labelled' terms. We also have the following. Theorem 3. Let R = (; R) be a left-linear TRS, 2 M be such that can R v LR 0 , t 2 T (; X ), and s0 2 T (L ; XL). Then, label(t) ! s if and only if 9s 2 T (; X ); t ,! s and s0 = label(s) Theorem 3 expresses than, whenever can R v , LR can be simulated by CSR , thus providing an alternative (simpler) evaluation mechanism:let CSR -eval (t) = fs 2 T (; X ) j t ,! s ^ s 2 NF,! g. We have:
Corollary 2. Let R = (; R) be a left-linear TRS and 2 M . If can R v , then, LR-eval = CSR-eval .
Concerning LR ()-termination, Theorem 3 also has the following consequence. Corollary 3. Let R = (; R) be a left-linear TRS and 2 M be such that can R v . Then, R is -terminating if and only if R is LR()-terminating. Example 9. Consider the following TRS R: nth(x,y:z) nth(s(x),y:z)
! !
y nth(x,z)
inf(x)
!
x:inf(s(x))
with (:) = (s) = (inf) = f1g and (nth) = f1; 2g [FKW00]. Replacement map (or the corresponding laziness predicate ) was selected by Fokking et al. to (hopefully) `avoid in nite reductions (with lazy rewriting)' ([FKW00], page 47). Since can R v , by Corollary 3, LR ()-termination and -termination coincide. Since R is easily proved -terminating (see Example 2 in [Zan97]), Corollary 3 permits a formal proof of Fokkink et al.'s claim. In general, CSR and ODR are related as follows. Theorem 4. [Luc01] Let R = (; R) be a TRS and ; D 2 M . Then, ,! = ,!h;?i and ,! ,!h;D i ,!tD . On the other hand, we also have the following. Theorem 5. Let R = (; R) be a left-linear TRS, and ; D 2 M be such that can R v . Then, ,! = ,!h;D i . Thus, if can R v , both lazy and on-demand rewriting collapse into contextsensitive rewriting. In general, ODR and LR are not (easily) comparable. Example 10. Consider R and as in Example 3. Let t = f(c,b). We have: A e e ` R label(t) = fe(c`,b`) ! f (c ,b ) ! fe (he(c` ),b` ) LR where fe (he(c` ),b`) is a ! -normal form. However, t gives rise to an in nite ,!h;c i -computation: f(c,b) ,!h;c i f(h(c),b) ,!h;ci Example 11. Consider the TRS R f(c(x,a)) b
! !
a f(c(b,d))
and (f) = ?, (c) = f1; 2c g. Then, t = f(c(b,d)) is a h; ci-normal form, since position 1:1 2 P ost (t) corresponds to a variable of the lhs f(c(x,a)). LR However, label(t) gives rise to an in nite ! -computation: R A e e e e e e e ` e e e e ` e e label(t) = f (c (b ,d )) ! f (c (b ,d )) ! f (c (f (c (b ,d )),d )) A e e e e e e R ! f (c (f (c (b ,d )),de)) !
5.4 Termination of lazy rewriting Corollary 3 provides quite a restricted framework for proving termination of LR. However, it provides the basis for proving it as termination of CSR for a transformed TRS. In [Ngu01], a transformation of pairs hR; i of TRSs and replacement maps is proposed to force that non-variable subterms of all left-hand sides of rules in R be -replacing, i.e., to achieve can R v . The transformation is as follows (see Section 6.1 of [Ngu01]): let R = (; R) be a TRS and 2 M . Let l ! r 2 R, p 2 P os (l), root(ljp ) = f, and i 62 (f) be such that p:i 2 P os (l). Then we (nondeterministically) obtain R0 = ( 0 ; R0) and 0 2 M 0 as follows: 0 = [ ff 0 g, where f 0 is a new symbol of arity ar(f 0 ) = ar(f) and such that 0(f 0 ) = (f) [ fig, and 0 (g) = (g) for all g 2 . On the other hand, R0 = R ? fl ! rg [ fl0 ! r; l[x]p:i ! l0[x]p:ig where l0 = l[f 0 (ljp:1; : : :; ljp:k )]p if ar(f) = k, and x is a new variable. Example 12. Consider R as in Example 7 and = ? . Then, R0 is:
!a b ! f(c(b,d)) ! f1 (x) and 0 (f) = 0 (c) = ?, 0(f1 ) = f1g. A dierent possibility (among others) is f1 (c(d,a)) f(x)
the following:
f(c'(d,a)) f(c(x,a))
! !
a f(c'(x,a))
b
!
f(c(b,d))
and 0 (f) = 0 (c) = ?, 0 (c') = f1g. The transformation proceeds in this way (starting now from R0 and 0 ) until # can obtaining R# and # such that can R# v . In particular, if R v , then # # R = R and = . Example 13. Continuing Example 12, R# is:
! f1(c3(x,y)) ! f1 (x) ! f(c(b,d)) # # # # # and is given by (f) = (c) = ?, (f1 ) = (c1 ) = f1g, # (c2 ) = # f1; 2g, and # (c3 ) = f2g. Notice that can R# v . f1 (c2(d,a)) f1 (c1(d,x)) f1 (c3(x,a))
!a ! f1(c2(d,x)) ! f1(c1(x,a))
f1(c(x,y)) f(x) b
Corollary 3 suggests using such a transformation for proving LR ()-termination of R as # -termination of R# , provided that the transformation preserves LR()-termination of R. Unfortunately, this is not true. Example 14. Consider R as in Example 7, = ? , and R# and # as in Example 13. Note that R is not LR()-terminating. For t = f(c(b,d)), we have: A e e ` ` A label(t) = fe(c`(b`,d` )) ! f (c (b ,d )) ! fe(ce (be,d` )) R A ! fe(ce(fe(c`(b` ,d`)),d`)) ?!+ fe(ce(fe(ce(be,d`)),d`)) !R
However, R# is LR (# )-terminating. The problem is that some activations of lazy subterms are not possible now:
R !R # fe1 (ce(b` ,d`)) ! # fe1(ce3 (b`,de )) R The lazy subterm b` cannot be activated; fe1 (ce3(b`,de )) is a ! # -normal form. fe (c`(b` ,d`))
A simple modi cation of Nguyen's transformation provides a sound technique for proving LR()-termination. The trick is to include all possible activations of lazy (problematic) arguments for each considered symbol: given l ! r 2 R and p 2 P os(l), we let I (l; p) = fi 2 f1; : : :; ar(root(ljp ))g j i 62 (root(ljp )) ^ p:i 2 P os (l)g Assume that I (l; p) = fi1 ; : : :; ing for some n > 0 (i.e., I (l; p) 6= ?) and let f = root(ljp ). Then, R = ( ; R) and 2 M are as follows: = [ffj j 1 j ng, where each fj is a new symbol of arity ar(fj ) = ar(f). We let (fj ) = (f) [fij g for 1 j n, and (g) = (g) for all g 2 . On the other hand, R = R ? fl ! rg [ flj0 ! r j 1 j ng [ fl[x]p:ij ! lj0 [x]p:ij j 1 j ng where lj0 = l[fj (ljp:1 ; : : :; ljp:k)]p if ar(f) = k, and x is a new variable. Example 15. Consider R as in Example 7 and = ? . With the new transformation, we could obtain R to be as the rst R0 obtained in Example 12. On the other hand, if symbol c (rather than f) of lhs f(c(d,a)) ! a is considered, now we obtain: f(c'(d,a)) f(c''(d,a)) b
! ! !
a a f(c(b,d))
f(c(x,a)) f(c(d,x))
! !
f(c'(x,a)) f(c''(d,x))
and (f) = (c) = ?, (c') = f1g, (c'') = f2g. Again, the transformation proceeds in this way (starting now from R and ) \ can until obtaining R\ = ( \ ; R\ ) and \ such that can R\ v . If R v , then \ \ R = R and = . Example 16. Consider R as in Example 7 and = ? . Then, R\ is f1 (c02(d,a)) f1 (c2(x,a)) f1 (c01(d,a)) f1 (c1(d,x)) f1 (c3(x,a))
!a f1(c(x,y)) ! f1 (c3(x,y)) ! f1(c02(x,a)) f1 (c4(d,y)) ! f1(c2 (d,y)) !a f1(c(x,y)) ! f1 (c4(x,y)) ! f1(c01(d,x)) f(x) ! f1 (x) ! f1(c1(x,a)) b ! f(c(b,d)) and \ is given by \ (f) = \ (c) = ?, \ (f1 ) = \ (c1) = \ (c4 ) = f1g, \ (c01 ) = \ \ (c02 ) = f1; 2g, and \ (c2 ) = \ (c3 ) = f2g. Notice that can R\ v . Consider the term t of Example 14. Now we have: R R e e ` ` e e e ` label(t) = fe(c`(b`,d` )) ! \ f1 (c (b ,d )) !\ f1 (c4 (b ,d )) R R + ` e e e e e ` !R \ fe1 (ce4 (fe(c`(b`,d`)),d`)) ?! \ f1 (c4 (f1(c4 (b ,d )),d )) !\
Now, we have the following. Theorem 6. Let R = (; R) be a left-linear TRS and 2 M . If R\ is \ terminating, then R is LR()-terminating. Example 17. Consider R and as in Example 1. Then, R\ is: 2nd(x:'(y:z)) 2nd(x:y)
! !
y 2nd(x:'y)
2nd(x:'(y:z)) 2nd(x:y) from(x)
! ! !
y 2nd(x:'a(y)) x:from'(s(x))
from(x)
!
x:from(s(y))
and \ is given by \ (2nd) = \ (:) = \ (from) = f1g and \ (:') = f1; 2g. In fact, in this case R\ and R# coincide (see Example 21 of [Ngu01]). However, using Theorem 6, we can prove LR ()-termination of R, which was an open question in [Ngu01]: \ -termination of R\ is proved by using Zantema's transformation [Zan97]: the TRS a(from'(x)) from(x) a(x)
! ! !
from(x) from'(x) x
which is obtained in this way (where a and from' are new symbols) is terminating (use an automatic tool such as Contejean and Marche's CiME 2.0 system1 ). Note that, since can R 6v , Corollary 3 does not directly apply to R and .
6 Conclusions and future work We have characterized the set MD of pairs of replacement maps that may produce dierent on-demand rewrite relations for TRSs on a signature . Ordering v between pairs of replacement maps in MD provides a good framework for comparing on-demand rewriting computations, since monotony of ODR is guaranteed. We have provided an adaptation of lazy graph rewriting of [FKW00] to lazy term rewriting, LR . If we use replacement maps that are less restrictive than the canonical replacement map can R , then CSR , LR and ODR coincide for left-linear TRSs. This allows us to prove termination of LR by proving termination of CSR for a transformed rewrite system. As far as the author knows, this is the rst proposal for proving termination of LR . We think that our results may contribute to formally address the problem of specifying local strategies in OBJ (see [OF00,NO01]). Lazy rewriting is invoked in these works as being a kind of `underlying' or `inspirating' mecanism for dealing with annotations in local strategies [Ngu01,OF00]. As discussed in [Luc01], we believe that ODR can better achieve this, as LR considers a single replacement map, thus giving no explicit treatment for negative indices of local strategies. We hope that our analysis of connections between the dierent mechanisms helps to better understand their use in implementations. However, as noticed in [Luc01], further work should be done for describing OBJ computations as ODR reductions, since determinism introduced by the use of lists (rather than sets) of annotations should be expressed as some kind of ODR strategy. 1 Available at http://www.lri.fr/~demons/cime.html.
References [CELM96] M. Clavel, S. Eker, P. Lincoln, and J. Meseguer. Principles of Maude. In J. Meseguer, editor, Proc. 1st International Workshop on Rewriting Logic and its Applications, Electronic Notes in Theoretical Computer Science, volume 4, 25 pages, Elsevier Sciences, 1996. [FGJM85] K. Futatsugi, J. Goguen, J.-P. Jouannaud, and J. Meseguer. Principles of OBJ2. In Conference Record of the 12th Annual ACM Symposium on Principles of Programming Languages, POPL'85, pages 52-66, ACM Press, 1985. [FKW00] W. Fokkink, J. Kamperman, and P. Walters. Lazy Rewriting on Eager Machinery. ACM Transactions on Programming Languages and Systems, 22(1):45-86, 2000. [FN97] K. Futatsugi and A. Nakagawa. An Overview of CAFE Speci cation Environment { An algebraic approach for creating, verifying, and maintaining formal speci cation over networks {. In Proc. of 1st International Conference on Formal Engineering Methods, 1997. [FW76] D.P. Friedman and D.S. Wise. CONS should not evaluate its arguments. In S. Michaelson and R. Milner, editors, Automata, Languages and Programming, pages 257-284, Edinburgh University Press, 1976. [GM99] J. Giesl and A. Middeldorp. Transforming Context-Sensitive Rewrite Systems. In P. Narendran and M. Rusinowitch, editors, Proc. of 10th International Conference on Rewriting Techniques and Applications, RTA'99, LNCS 1631:271-285, Springer-Verlag, Berlin, 1999. [GWMFJ00] J.A. Goguen, T. Winkler, J. Meseguer, K. Futatsugi, and J.-P. Jouannaud. Introducing OBJ. In J. Goguen and G. Malcolm, editors, Software Engineering with OBJ: algebraic speci cation in action, Kluwer, 2000. [Luc98] S. Lucas. Context-sensitive computations in functional and functional logic programs. Journal of Functional and Logic Programming, 1998(1):161, January 1998. [Luc01] S. Lucas. Termination of on-demand rewriting and termination of OBJ programs. In Proc. of 3rd International Conference on Principles and Practice of Declarative Programming, PPDP'01, ACM Press, to appear, 2001. [Ngu01] Q.-H. Nguyen. Compact Normalisation Trace via Lazy Rewriting. In B. Gramlich and S. Lucas, editors, Proc. of 1st International Workshop on Reduction Strategies in Rewriting and Programming, WRS'01, pages 7996, volume 2359, Servicio de Publicaciones de la Universidad Politecnica de Valencia, 2001. [NO01] M. Nakamura and K. Ogata. The evaluation strategy for head normal form with and without on-demand ags. In K. Futatsugi, editor, Proc. of 3rd International Workshop on Rewriting Logic and its Applications, WRLA'2000, Electronic Notes in Theoretical Computer Science, volume 36, 17 pages, 2001. [OF00] K. Ogata and K. Futatsugi. Operational Semantics of Rewriting with the On-demand Evaluation Strategy. In Proc of 2000 International Symposium on Applied Computing, SAC'00, pages 756-763, ACM Press, 2000. [Zan97] H. Zantema. Termination of Context-Sensitive Rewriting. In H. Comon, editor, Proc. of 8th International Conference on Rewriting Techniques and Applications, RTA'97, LNCS 1232:172-186, Springer-Verlag, Berlin, 1997.