J. Symbolic Computation
(1994)
11, 1{000
Termination of term rewriting: interpretation and type elimination H. Zantema
Utrecht University, Department of Computer Science P.O. box 80.089, 3508 TB Utrecht, The Netherlands e-mail:
[email protected] (Received 6 April 1994) We investigate proving termination of term rewriting systems by interpretation of terms in a well-founded monotone algebra. The well-known polynomial interpretations can be considered as a particular case in this framework. A classi cation of types of termination, including simple termination, is proposed based on properties in the semantic level. A transformation on term rewriting systems eliminating distributive rules is introduced. Using this distribution elimination a new termination proof of the system SUBST of Hardin and Laville (1986) is given. This system describes explicit substitution in calculus. Another tool for proving termination is based on introduction and removal of type restrictions. A property of many-sorted term rewriting systems is called persistent if it is not aected by removing the corresponding typing restriction. Persistence turns out to be a generalization of direct sum modularity, but is more powerful for both proving con uence and termination. Termination is proved to be persistent for the class of term rewriting systems for which not both duplicating rules and collapsing rules occur, generalizing a similar result of Rusinowitch for modularity. This result has nice applications, in particular in undecidability proofs.
1. Introduction
One of the main problems in the theory of term rewriting systems (TRS) is the detection of termination: for a xed system of rewrite rules, detect whether there exist in nite reductions or not. In general this problem is undecidable as was proved by Huet and Lankford (1978). However, there are several methods for deciding termination that are successful for many special cases. Roughly these methods can be divided into two main types: syntactical methods and semantical methods. In a syntactical method terms are ordered by a careful analysis of the term structure. A well-known representative of this type is the recursive path order, see e.g. Dershowitz (1987). All these orderings are simpli cation orderings, i.e., a term is always greater than its proper subterms. An overview and comparison of syntactical simpli cation orderings has been given by Steinbach (1989). In a semantical method terms are interpreted in some well-known well-founded ordered 0747{7171/90/000000 + 00 $03.00/0
c 1994 Academic Press Limited
2
H. Zantema
set in such a way that each rewrite chain will map to a descending chain, and hence will terminate. Until now most semantical methods have focussed on choosing the natural numbers as the well-founded ordered set. The method of polynomial interpretations as given by Lankford (1979), Ben-Cherifa and Lescanne (1987) can be seen as a particular case of a semantical method on natural numbers. In the rst part of this paper we introduce the notion of a monotone algebra as the natural concept for semantical methods. Though we focus on `pure' TRS, the ideas are easily extended to conditional TRS, typed TRS and TRS modulo equations. We propose a classi cation of types of termination based upon the types of orderings of the underlying monotone algebras. Some remarks and examples are not claimed to be new but are included for completeness and for illustrating the setting of monotone algebras. We present a transformation of term rewriting systems eliminating one particular operation symbol. Using the framework of monotone algebras we prove that under some restrictions termination of the original system follows from termination of the eliminated system. Since in this construction distributive rules are removed it is called distribution elimination. As an application of distribution elimination we give a new termination proof for the system SUBST describing explicit substitution in -calculus; for an overview on this view on substitution we refer to Abadi et al. (1991). Two papers by Hardin and Laville (1986) and by Curien et al. (1992) are devoted only to the termination proof of SUBST. Our proof is much simpler than the existing proofs and gives a stronger result: we prove that the system is simply terminating. Another application is given by Middeldorp and Gramlich (1993). They prove that simple termination of one rule systems is an undecidable property and use our theorem on distribution elimination. The second part of this paper is independent of the rst part, and describes a new tool for both proving termination and con uence. It is based on switching between one-sorted and many-sorted rewriting. Here many-sorted means that a xed set of sorts is given and each operation symbol has a sort (one of the sorts) and an arity. This arity is not simply a number, but a sequence of sorts. Each variable symbol has a sort. For every sort the set of terms of that sort is de ned inductively in a straightforward way. This de nition of terms is standard in the theory of algebraic speci cations, see for example Ehrig and Mahr (1985). A many-sorted TRS is de ned to be a set of pairs l r, where l and r are terms of the same sort. As usual l is not allowed to be a single variable and all variables in r also occur in l. Contexts, substitutions and the reduction relation are de ned as expected, inducing de nitions of normal forms, termination and con uence. Any many-sorted TRS is trivially mapped to a one-sorted TRS by removing all sort information and keeping the same rewrite rules. The many-sorted terms can be considered as a subset of all (one-sorted) terms: the terms that satisfy the typing restriction. We call a property of many-sorted TRS's persistent if a many-sorted TRS has the property if and only if its adjoined one-sorted TRS has the property. We show that every reasonable persistent property of term rewriting systems is also a modular property, i.e., invariant under taking disjoint unions of (one-sorted) TRS's. As a main result we prove that restricting to TRS's in which not both duplicating rules and collapsing rules occur, termination is a persistent property, generalizing a similar result for modularity obtained by Rusinowitch (1987). !
Termination: interpretation and type elimination
3
This can be used as a tool for proving termination of (one-sorted) TRS's as follows. Given a (one-sorted) TRS not containing both collapsing and duplicating rules, try to nd a typing in such a way that all rules are well-typed. This can be done systematically in a way similar to uni cation; in this way a many-sorted TRS is obtained. Our main result states that for proving termination of the original one-sorted system it suces to prove termination of the many-sorted system. In many applications this is much simpler to prove since only well-typed terms have to be considered. In particular this is useful in decidability questions as considered by Dauchet (1992) and Middeldorp and Gramlich (1993). In our view persistence is more basic than modularity; modularity can be considered as a particular case of persistence, while persistence turns out to be a more powerful tool for proving con uence or termination than modularity. Many results on modularity seem to generalize to persistence, in particular modularity of con uence. It has been shown by van de Pol (1992) that persistence is equivalent to a straightforward extension of the notion of modularity to many-sorted TRS's.
2. Interpretation
2.1. Monotone algebras
Let F be a set of operation symbols each having a xed arity. We de ne a wellfounded monotone F -algebra (A; >) to be an F -algebra A for which the underlying set is provided with a well-founded order > and each algebra operation is strictly monotone in all of its coordinates. More precisely, for each operation symbol f 2 F and all a1 ; : : : ; an ; b1 ; : : : ; bn 2 A for which ai > bi for some i and aj = bj for all j 6= i we have fA(a1 ; : : : ; an ) > fA (b1 ; : : : ; bn ): Let (A; >) be a well-founded monotone F -algebra. Let : X ! A. We de ne the term evaluation [] : T (F ; X ) ! A inductively by [](x) = x ; [](f (t1 ; : : : ; tn )) = fA ([](t1 ); : : : ; [](tn )) for x 2 X ; f 2 F ; t1 ; : : : ; tn 2 T (F ; X ). This function induces a partial order >A on T (F ; X ) as follows: t >A t0 () (8 : X ! A : [](t) > [](t0 )): Intuitively, t >A t0 means that for each interpretation of the variables in A the interpreted value of t is greater than that of t0 . We say that a non-empty well-founded monotone algebra (A; >) is compatible with a TRS if l >A r for every rule l ! r in the TRS. Proposition 1. A TRS is terminating if and only if it admits a compatible non-empty
well-founded monotone algebra.
It seems that Lankford already arrived at essentially the same result in some unpublished note in 1975. In order to give the proof we need two lemmas.
4
H. Zantema
Lemma 2. Let : X
! T
( ; ) be any substitution and let : [](t ) = [[] ](t) F X
X !
A. Then
for all t 2 T (F ; X ).
Proof. Induction on the structure of t. 2 Lemma 3. The partial order >A is closed under substitution and context. Proof. Let t >A t0 for t; t0
( ; ) and let : X ( ; ) be any substitution. A. From lemma 2 we obtain [](t ) = [[] ](t) > [[] ](t0 ) = [](t0 ): This holds for all : A, so t >A t0 . Hence >A is closed under substitution. For proving closedness under context let t >A t0 for t; t0 ( ; ), and let f . Since t >A t0 we have [](t) > [](t0 ) for all : A. Applying the monotonicity condition of fA we obtain [](f (: : : ; t; : : :)) = fA (: : : ; [](t); : : :) > fA (: : : ; [](t0 ); : : :) = [](f (: : : ; t0 ; : : :)): This holds for all : A, so f (: : : ; t; : : :) >A f (: : : ; t0 ; : : :);
Let :
2 T
F X
! T
F X
X !
X !
2 T
X
F X
2 F
!
X !
which we had to prove. 2
A well-founded order closed under substitution and context is called a reduction order. It is well-known (Lankford (1979)) that a TRS is terminating if and only if there is a reduction order such that l r for all rules l r. Now we give the proof of proposition 1.
!
Proof. Assume the TRS admits a compatible non-empty well-founded monotone algebra (A; >), and allows an in nite reduction. Then according to lemma 3 we have t >A t0 for every reduction step t ! t0 . Now applying any assignment in A (here we use non-
emptiness) on the in nite reduction gives rise to an in nite decreasing chain in A, which is a contradiction. On the other hand, assume the system is terminating. De ne A = ( ; ), and de ne > to be the transitive closure of the rewrite relation. One easily veri es that (A; >) is a non-empty well-founded monotone algebra. We still have to prove that l >A r for each rewrite rule l r. Let : A. Since A = ( ; ) we see that is a substitution. Then [](t) = t for each term t, which is easily proved by induction on the structure of t. Since l r is a rewrite rule, the term l can be reduced in one step to r . So [](l) = l > r = [](r): This holds for every : A, so l >A r, which we had to prove. 2 T
!
X !
T
F X
F X
!
X !
The way of proving termination of a TRS is now as follows: choose a well-founded poset (A; >), de ne for each operation symbol f a corresponding operation fA that is
Termination: interpretation and type elimination
5
strictly monotone in all of its coordinates, and for which [](l) > [](r) for all rewrite rules l r and all : A. Then according to proposition 1 the TRS is terminating. The problem is how to choose the poset and the operations. We do not give a xed strategy; in section 2.5 we present some basic constructions. The simplest useful choice for (A; >) is ( + ; >), the set of strictly positive integers with the ordinary ordering. For example, for the system f (f (x; y); z ) f (x; f (y; z )) we can choose (A; >) = ( + ; >) and fA(x; y) = 2x + y. Clearly fA is strictly monotone in both coordinates, and fA (fA (x; y); z ) = 4x + 2y + z > 2x + 2y + z = fA (x; fA (y; z ) for all x; y; z A. Hence f (f (x; y); z ) >A f (x; f (y; z )), proving termination according to proposition 1. As we shall see the poset ( + ; >) will not always suce. We conclude this section by three examples in which for proving termination by means of a well-founded monotone algebra we even need non-total orders; in section 2.3 it will become clear why. De ne for all three cases: + and (a; n) > (b; m) A = 0; 1 a = b n > m; note that (A; >) is indeed a well-founded poset. (1) Consider the TRS consisting of the rule: f (f (x)) f (g(f (x))): De ne fA(0; n) = (0; n + 1); fA(1; n) = (0; n); gA (0; n) = (1; n); gA (1; n) = (1; n) for all n . Both fA and gA are strictly monotone, while fA (fA (0; n)) = (0; n + 2) > (0; n + 1) = fA (gA (fA (0; n))); fA (fA (1; n)) = (0; n + 1) > (0; n) = fA (gA (fA (1; n))) for all n , proving termination. (2) Consider the TRS with the two rules: f (g(x)) f (f (x)); g(f (x)) g(g(x)): De ne fA (0; n) = (1; 2n); fA(1; n) = (1; n + 1); !
X !
IN
!
IN
2
IN
f
g IN
()
^
!
2 IN
2 IN
!
!
gA (0; n) = (0; n + 1); gA (1; n) = (0; 2n):
Both fA and gA are strictly monotone, while fA (gA (0; n)) = (1; 2n + 2) > fA (gA (1; n)) = (1; 4n) > gA (fA (0; n)) = (0; 4n) > gA (fA (1; n)) = (0; 2n + 2) > for all n , proving termination. 2 IN
(1; 2n + 1) (1; n + 2) (0; n + 2) (0; 2n + 1)
= = = =
fA (fA (0; n)); fA (fA (1; n)); gA (gA (0; n)); gA (gA (1; n)):
6
H. Zantema
(3) Let the TRS consist of the rule:
f (0; 1; x)
!
f (x; x; x):
The origin of this TRS is the example of Toyama (1987a) showing that termination is not modular. It will serve as a crucial example in this paper several times. De ne 0A = (0; 1); 1A = (1; 1);
(0; n + m + k) if a = b (0; n + m + 3k) if a = b The function fA is strictly monotone in all three coordinates. For all (a; n) A we have fA (0A; 1A ; (a; n)) = (0; 3n + 2) > (0; 3n) = fA ((a; n); (a; n); (a; n)); proving termination.
fA ((a; n); (b; m); (c; k)) =
6
2
If no confusion is possible it is sometimes convenient to remove subscripts, writing
f; g; : : : instead of fA ; gA; : : :.
2.2. Simple termination
The basic method of proving termination of a TRS we propose consists of choosing a well-founded monotone algebra compatible with the TRS. The problem is how to choose the algebra. One approach is to start with a well-known well-founded poset, like . Another approach is to start with the set of terms and try to de ne recursively a suitable well-founded order on it. In fact this is the approach of syntactical methods like recursive path order. For such recursively de ned orders it is usually very dicult to check well-foundedness. However, in case of ntie signature it is possible to replace the well-foundedness condition in the de nition of a well-founded monotone algebra by a simplicity condition. In recursive de nitions this simplicity condition is far easier to verify. First we give the de nition. A simple monotone -algebra (A; >) is de ned to be an -algebra for which the underlying set A is provided with a partial order > such that each algebra operation is strictly monotone in all of its coordinates, and fA (a1 ; : : : ; an ) ai for each f , a1 ; : : : ; an A; i 1; : : : ; n . For a set of operation symbols we de ne Emb( ) to be the TRS consisting of all the rules f (x1 ; : : : ; xn ) xi with f and i 1; : : :; n . The following two propositions state that for proving termination by a simple monotone -algebra, well-foundedness is essentially obtained for free if is nite. IN
F
F
2 F
2
2 f
g
F
F
!
2 F
F
2 f
g
F
Proposition 4. Let F be nite and let (A; >) be a simple monotone F -algebra. Let A0 be the smallest subalgebra of A, i.e., A0 is the homomorphic image of the ground terms. Then (A0 ; >) is well-founded.
Termination: interpretation and type elimination
7
Proof. Assume the restriction of > to A0 is not well-founded. Then there is an in nite
chain
h(t0 ) > h(t1 ) > h(t2 ) > h(t3 ) > ; where h is the homomorphism from ground terms to A. The key argument is the tree theorem of Kruskal (1960); in our notation this theorem states that for any chain ui i2 of terms over a nite and containing nitely many variables, there is some i < j such that uj Emb(F ) ui . Applying this to the above terms yields tj Emb(F ) ti for some i < j . Using the simplicity condition of (A; >) it easily follows that h(t) h(u) for ground terms t; u satisfying t Emb(F ) u. Hence h(tj ) h(ti ), contradicting irre exivity and transitivity of >. 2
f
g
IN
F
!
!
!
F be nite and let (A; >) be a non-empty simple monotone F algebra. Let R be a TRS such that l >A r for all rewrite rules l ! r of R. Then R is terminating.
Proposition 5. Let
Proof. Apply propositions 1 and 4: A0 is a well-founded monotone algebra compatible
with R. In the case that does not contain constants, add one dummy constant symbol. Choose an arbitrary interpretation in A for this dummy constant. This forces A0 = . 2 F
6
;
A TRS is called simply terminating if it admits a compatible non-empty simple wellfounded monotone algebra. The above propositions state that for nite the wellfoundedness condition can be removed without changing this de nition. F
Proposition 6. Let R be a TRS over a set F of operation symbols. Then the following assertions are equivalent:
(1) R is simply terminating; (2) R Emb( ) is simply terminating; (3) R Emb( ) is terminating. [
F
[
F
Proof. The implication (2) (1) is trivial. For proving (1) (2) let (A; >) be a simple well-founded monotone -algebra compatible with R. Since we allow equality in the de nition of simplicity but need strict inequality for compatibility, we have to modify the algebra. Choose B=A having the lexicographic order (a; k) > (a0 ; k0 ) a > a0 (a = a0 k > k0 ): De ne n X fB ((a1 ; k1 ); : : : ; (an ; kn )) = (fA (a1 ; : : : ; an ); 1 + ki ): )
)
F
IN
()
_
^
i=1
Now (B; >) is a simple well-founded monotone algebra compatible with both R and Emb( ), proving (2). The implication (2) (3) is trivial. Finally, assume that (3) holds. Then according to proposition 1 there is a non-empty well-founded monotone -algebra (A; >) compatible F
)
F
8
H. Zantema
with R Emb( ). Since it is compatible with Emb( ) it is also a simple well-founded monotone -algebra. This implies (2). 2 [
F
F
F
Other de nitions of simple termination have been given by Kurihara and Ohuchi (1990) and Ohlebusch (1992); for nite they are all equivalent. A simpli cation ordering is de ned to be an ordering > on terms, closed under substitution and context, for which C [t] > t for all terms t and all non-trivial contexts C . A TRS R is called simplifying (Kaplan (1987)) if there exists a simpli cation ordering > such that t > t0 for all terms t; t0 satisfying t R t0 . From the above observations it is easy to see that a TRS over a nite is simply terminating if and only if it is simplifying (choose >B to be the simpli cation ordering, where B is as in the proof of proposition 6). If is in nite not every simplifying TRS is terminating and not every simplifying terminating TRS is simply terminating, see Ohlebusch (1992). By Middeldorp and the author the notion of simple termination is revisited in such a way that all these notions coincide, and Kruskal's theorem is applicable in the in nite case too; this work is still in progress. F
!
F
F
2.3. Total termination
A TRS is called totally terminating if it admits a compatible well-founded monotone -algebra (A; >) for which the order > is total on A. Most syntactical orderings, like precedence based orderings such as lexicographic and recursive path order, are total on ground terms, or on ground terms modulo the order of arguments. Hence they prove total termination.
F
Proposition 7. Let (A; >) be a well-founded monotone F -algebra for which the order
> is total on A. Then (A; >) is a simple monotone -algebra. F
Proof. Assume it is not simple. Then there exist f 2 F ; a1 ; : : : ; an 2 A and i 2 f1; : : : ; ng such that fA (a1 ; : : : ; an ) ai does not hold. From totality we conclude: ai > fA (a1 ; : : : ; an ): De ne g : A ! A by g(x) = fA(a1 ; : : : ; ai?1 ; x; ai+1 ; : : : ; an ), then g is strictly monotone.
We obtain an in nite chain
ai > g(ai ) > g(g(ai )) > g(g(g(ai ))) > contradicting the well-foundedness of (A; >). 2
;
A useful consequence is the following: Proposition 8. Let R be a TRS over a set F of operation symbols. Then R is totally
terminating if and only if R [ Emb(F ) is totally terminating.
Proof. The `if' part is trivial. The `only if' part is similar to the proof of (1) proposition 6, using proposition 7. 2
)
(2) in
In particular we see that if R Emb( ) allows an in nite reduction then R is not totally terminating. So for examples (1) and (3) at the end of section 2.1 we indeed need non-total orders. [
F
Termination: interpretation and type elimination
9
For any TRS R we de ne the truncation closure TC (R) of R to be the TRS consisting of all rules t t0 for which there is a context C such that C [t] +R C [t0 ]: Clearly R is contained in TC (R). !
!
Proposition 9. Let R be a TRS. Then R is totally terminating if and only if TC (R)
is totally terminating.
Proof. The `if' part is trivial. For the `only if' part let (A; >) be a well-founded monotone algebra compatible with R for which the order > is total on A. Let t; t0 be two terms and C be a context such that C [t] !+R C [t0 ]. Let : X ! A be arbitrary. By replacing every variable x in C by (x) and replacing every f in C by fA we obtain a
strictly monotone function F : A A satisfying F ([](u)) = [](C [u]) for all terms u. We obtain F ([](t)) = [](C [t]) > [](C [t0 ]) = F ([](t0 )): Since > is total and F is strictly monotone we conclude [](t) > [](t0 ). This holds for any : A, so (A; >) is compatible with TC (R). Hence TC (R) is totally terminating. 2 !
X !
Combining propositions 8 and 9 yield that a TRS R is totally terminating if and only if TC (R Emb( )) is totally terminating. This provides a powerful tool for proving that a TRS is not totally terminating, and hence techniques like recursive path order are not applicable: if TC (R Emb( )) allows an in nite reduction then R is not totally terminating. For example, the system f (a) f (b); g(b) g(a) is not totally terminating. The same holds for example (2) at the end of section 2.1. Both systems are simply terminating. The converse does not hold: the system f (a; a) f (b; b); g(b; b) g(a; a) is not totally terminating, while the corresponding system TC (R Emb( )) is terminating. A more detailed study of total termination is presented by Ferreira and Zantema (1993). [
F
[
F
!
!
!
!
[
F
2.4. The hierarchy
Let (A; >) be a well-founded monotone algebra. Depending on its properties we propose a hierarchy of types of termination. If A = and > is the ordinary order on and fA is a polynomial for all f , we speak about polynomial termination. If A = and > is the ordinary order on , we speak about !-termination. In these cases for any xed N we may have n n > N instead of , which gives equivalent de nitions due to linear transformation. An implementation based on polynomial termination is described by Ben-Cherifa and Lescanne (1987); an extension to elementary functions in which also exponents may occur is given by Lescanne (1992). The following implications hold: polynomial termination = !-termination = total termination = simple termination = termination. IN
IN
2 F
IN
IN
f
2 IN j
g
IN
)
)
)
)
10
H. Zantema
The implication from total termination to simple termination is immediate from proposition 7. All of the other implications are trivial. None of the implications in the hierarchy holds in the reverse direction: for the last two implications we already gave examples; for the rst two implications it follows from the next propositions. Proposition 10. The TRS
f (g(h(x)))
g(f (h(g(x))))
!
is !-terminating but not polynomially terminating.
Proof. De ne f (x) = 3x , g(x) = 2x and h(x) = x + 1. Then f (g(h(x))) = 32x+2 > 2 32x+1 = g(f (h(g(x))))
for all x , so the system is !-terminating. Assume the system is polynomially terminating. Then there exist strictly monotone polynomials f; g and h such that f (g(h(x))) > g(f (h(g(x)))) for all x . Then degree(f g h) degree(g f h g), hence degree(g) = 1. So g(x) = ax + b for some a 1 and b 0. Further the leading coecient of f g h has to be greater or equal than that of g f h g; this yields a = 1. Using strict monotonicity one easily proves by induction on n and m that g(n) n and h(n + m) h(n)+ m for all n; m . Hence g(f (h(g(x)))) f (h(g(x))) = f (h(x + b)) f (h(x) + b) = f (g(h(x))); contradiction. 2 2 IN
2
IN
2 IN
Proposition 11. The term rewriting system
f (g(x))
!
is totally terminating but not !-terminating.
g(f (f (x)))
Proof. For proving total termination choose A = IN IN with the lexicographic order (n; n0 ) > (m; m0 ) () n > m _ (n = m ^ n0 > m0 ):
Further de ne
f (n; n0) = (n; n + n0 ) and g(n; n0 ) = (2n + 1; n0 ): Monotonicity of f and g is easily veri ed; for the monotonicity of f it is essential to
choose this lexicographic order and not the reversed one. Now we have f (g(n; n0)) = (2n + 1; 2n + n0 + 1) > (2n + 1; 2n + n0 ) = g(f (f (n; n0 ))) for all (n; n0 ) A, so the system is totally terminating. On the other hand assume that the system is !-terminating. Then there exist strictly monotonic f; g : such that n : f (g(n)) > g(f (f (n))): (2.1) One easily proves by induction on n that n : g(n) n. So for k = 0 we have n : g(n) f k (n). Assume that the assertion n : g(n) f k (n) holds for 2
IN ! IN
8
2 IN
8
8
2 IN
2 IN
8
2 IN
Termination: interpretation and type elimination
k
. Then
11
2 IN
: f (g(n)) > g(f (f (n))) f k (f (f (n))) = f (f k+1 (n)); since f is monotone we conclude n : g(n) > f k+1 (n). Now we have proved by induction on k that n; k : g(n) f k (n): (2.2) If there is some n with f (n) > n then n < f (n) < f (f (n)) < f (f (f (n))) < contradicting (2.2), otherwise f (n) = n for all n , contradicting (2.1). 2
n
8
2 IN
8
8
2 IN
2 IN
2 IN
2 IN
After a question on the electronic newsnet, the proof of the impossibility of (2.1) has been given independently by several people. Note that in our notion of !-termination there is no restriction on the kind of monotone functions allowed. Hofbauer (1992) proved that any TRS which can be proven terminating by a recursive path order, is !-terminating. By giving restrictions on the kind of functions allowed, a more detailed hierarchy between polynomial termination and !termination can be given. An extensive study of the hierarchy between !-termination and total termination is given by Ferreira and Zantema (1993). One can show that the TRS f (g(h(x))) f (h(h(g(g(x))))) is simply terminating, but its truncation closure admits an in nite reduction, hence it is not totally terminating. We conclude that even if we restrict to the class of one rule string rewriting systems (a string rewriting system is a TRS over a signature only containing unary sysmbols) none of the implications in the hierarchy holds in the reverse direction. !
2.5. Basic constructions
In order to nd termination proofs for particular term rewriting systems by monotone algebras, it is useful to investigate some basic constructions of how to build a monotone algebra. For the well-founded set (A; >) the basic choice is the natural numbers. Further there are two basic constructions building a well-founded order from simpler well-founded orders: lexicographic product and nite multisets. For any order (A; >) the nite multisets over A are de ned by M (A) = X : A # a A X (a) = 0 < ; with the order X>Y X = Y ( x A : X (x) Y (x) ( a A : a > x X (a) > Y (a))): If (A; >) is well-founded then (M (A); >) is well-founded too; for the proof we refer to Dershowitz and Manna (1979). This construction corresponds to exponentiation of ordinals, see for example Kuratowski and Mostowski (1968). If (A; >) has order type , then (M (A); >) has order type ! . One result of Ferreira and Zantema (1993) states that any useful (A; >) in which the order is total and the order type is below 0 , can be constructed in a nite number of steps using only the above constructions. The order type 0 can be obtained by taking nested multisets. The above constructions are totality preserving. However, from section 2.3 we know f
()
6
^
8
! INj
2
f
2
j
_
6
9
2
g
1g
^
12
H. Zantema
that for proving termination of non-simply terminating systems we need non-total orders. A useful non-total well-founded order on M A is (m; a) > (m0 ; a0 ) m = m0 a > a0 for any set M and any well-founded (A; >). This construction is the basis of semantic labelling (Zantema (1992)), a general powerful technique for proving termination of nonsimply terminating TRS's. After investigating the underlying well-founded set the next step is how to choose appropriate monotone functions. A lot of basic constructions of monotone functions are easily found. For example, a binary monotone function on A B is ((a; b); (a0 ; b0 )) ((a; a0 ); (a; a0 ; b; b0 )); where is any binary monotone function on A and (x; y) (a; a0 ; x; y) is any binary 0 monotone function on B for any a; a A; monotonicity in the rst and second coordinate of is not required. Now we introduce a useful kind of lifting of monotone functions to multisets; in the next section this lifting plays an essential role. Write [ ] for the empty multiset, de ned by [ ](a) = 0 for all a A. For every a A the singleton [a] is de ned by [a](a) = 1 and [a](x) = 0 for x = a. Multiset union is de ned by (X Y )(a) = X (a) + Y (a); multiset union is associative and commutative. Write M 0 (A) = M (A) [ ] , the set of nite non-empty multisets over A. Note that every nite non-empty multiset can be obtained as a nite multiset union of singletons. For every f : Ak A the function f : (M 0 (A))k M 0 (A) is de ned as follows: f ([a1 ]; [a2 ]; : : : [ak ]) = [f (a1 ; a2 ; : : : ; ak )]; f (X1 ; : : : Xi?1 ; Y Z; Xi+1 ; : : : Xk ) = f (X1 ; : : : Xi?1 ; Y; Xi+1 ; : : : Xk ) f (X1 ; : : : Xi?1 ; Z; Xi+1 ; : : : Xk ): Since multiset union is associative and commutative this f is well-de ned. The intuition behind f is as follows. To compute f (X1 ; : : : Xk ), apply f to all possible choices of elements of X1 ; : : : Xk , and collect all results in one multiset. Clearly an element x is contained in f (X1 ; : : : Xk ) if and only if there exist xi Xi such that x = f (x1 ; : : : ; xn ). One easily shows that if f is monotone in every coordinate then also f is monotone in every coordinate; here for k > 1 it is essential to restrict to non-empty multisets.
()
^
7!
7!
2
2
2
6
[
[
n f
g
!
h
h
!
i
h
h
i
i
[
i
[ h
i
h
h
i
h
h
i
i
i
2
h
i
2.6. Distribution elimination
In this section we introduce a transformation of TRS's in which a particular operation symbol is eliminated, and prove that if the eliminated TRS is terminating then the original TRS is also terminating. Since the eliminated TRS is simpler than the original one, this provides a method for proving termination of complicated TRS's by proving termination of simpler ones. Let a be any xed operation symbol of arity n 1. A rewrite rule is called a distribution rule for a if it can be written as C [a(x1 ; : : : ; xn )] a(C [x1 ]; : : : ; C [xn ]) for some non-trivial context C [ ] in which the symbol a does not occur. For example, b(z; f (a(x; y)) a(b(z; f (x)); b(z; f (y)))
!
!
Termination: interpretation and type elimination
13
is a distribution rule for a. Problems with distribution rules have been recognized before; for example a particular ordering for proving termination of AC rewriting systems is introduced for systems containing distribution rules by Gnaedig and Lescanne (1986). Write for powerset. The function Ea : ( ; ) ( ( ; )) is de ned inductively as follows: Ea (x) = x for all x , Ea (f (t1 ; : : : ; tk )) = Sfn(u1; : : : ; uk ) i : ui Ea (ti ) for all f ; f = a Ea (a(t1 ; : : : ; tn )) = i=1 Ea (ti ): Let R be a TRS for which each rule is either a distribution rule for a or a rule in which a does not occur in the left hand side. Then the TRS Ea (R) is de ned by Ea (R) = l u l r is a non-distribution rule of R for a and u Ea (r) : For example, if R is de ned by f (a(x; y)) a(f (x); f (y)) g(a(x; y)) a(g(x); g(y)) f (f (x)) f (a(g(f (x)); g(f (x)))) then Ea (R) consists only of the rule f (f (x)) f (g(f (x))). This system is known to be terminating; the next proposition states that we can conclude that also R is terminating. As usual a term is de ned to be linear if no variable occurs more than once, and a TRS is de ned to be right-linear if for every rule the right hand side is linear. P
T
f
!
j
! P T
F X
2
g
g
2 X
f
f
F X
j 8
2 F
!
2
6
g
!
!
!
!
Theorem 12. Let R be a TRS for which each rule is either a distribution rule for the
symbol a or a rule in which the symbol a does not occur in the left hand side. Then
(1) Ea (R) is totally terminating if and only if R is totally terminating; (2) if Ea (R) is right-linear, then Ea (R) is simply terminating if and only if R is simply terminating; (3) if Ea (R) is terminating and right-linear then R is terminating. Before giving the proof we give examples showing that some restrictions in this theorem are essential. First choose R to consist of the rule f (f (x)) f (a(f (x))). Then R is terminating and satis es the conditions (there are no distribution rules), while Ea (R) consists of f (f (x)) f (f (x)) which is not terminating. Hence the converse of (3) does not hold. Next choose R to be: f (0; 1; x; x) f (x; x; a(0; 1); a(0; 1)) f (a(x; y); z; v; w) a(f (x; z; v; w); f (y; z; v; w)) f (x; a(y; z ); v; w) a(f (x; y; v; w); f (x; z; v; w)) The second and third rules are distribution rules for a; the system Ea (R) consists of f (0; 1; x; x) f (x; x; 0; 0) f (0; 1; x; x) f (x; x; 0; 1) f (0; 1; x; x) f (x; x; 1; 0) f (0; 1; x; x) f (x; x; 1; 1); !
!
!
!
!
!
!
!
!
14
H. Zantema
and is terminating, even simply terminating since the size of a term is not changed by reduction. However, in R we have the reduction f (0; 1; a(0; 1); a(0; 1)) f (a(0; 1); a(0; 1); a(0; 1); a(0; 1)) a(f (0; a(0; 1); a(0; 1); a(0; 1)); f (1; : : :)) a(a(f (0; 0; a(0; 1); a(0; 1)); f| (0; 1; a(0{z ; 1); a(0; 1))}); f (1; : : :)) !
!
!
in which the starting term occurs as a subterm. This can be expanded to an in nite Rreduction. We conclude that the right-linearity requirement is essential in both part (2) and part (3) in theorem 12. This example even shows that the right-linearity restriction in the theorem may not be weakened to non-duplication. As a conjecture we state that if R is a TRS in which a does not occur in any left hand side, then termination of R can be concluded from termination of Ea (R) without further restrictions. Since no distribution rules are involved in this case, this transformation is not called distribution elemination any more but dummy elimination. Part (2) of theorem 12 is used in the proof by Middeldorp and Gramlich (1993) that simple termination of one rule systems is an undecidable property. We now prove theorem 12. Proof. The `if' part of part (1) and (2) easily follows from the observation that Ea (R) only contains rules of the shape l u satisfying r Emb(F ) u for some rule l r of R. For part (3) and the `only if' part of part (1) and (2) let (A; >) be a well-founded monotone algebra for Ea (R). For part (1) we assume it is total, for part (2) we assume it is simple. As before we write M 0 (A) for the set of nite non-empty multisets over A. We de ne the well-founded monotone algebra for R to be B = M 0 (A) + with the lexicographic order (X; k) > (Y; m) X > Y (X = Y k > m): As operations we de ne !
!
!
IN
()
_
aB ((X1 ; m1 ); : : : ; (Xn ; mn )) = ( and
^
n [
i=1
Xi ; 1 +
n X
fB ((X1 ; m1 ); : : : ; (Xk ; mk )) = ( fA (X1 ; : : : ; Xk ); h
i
mi )
i=1
k Y m2i )
i=1
for all f ; f = a. Here fA is de ned as in section 2.5. Note that these operations are strictly monotone in all coordinates. Further if (A; >) is a simple monotone algebra then (B; >) is also simple; if (A; >) is total then (B; >) is also total. We have to prove that for every rule of R the left hand side is greater than the right hand side, interpreted in B . + for Let : B be arbitrary, and write [ ](t) = (1 (t); 2 (t)) M 0 (A) t ( ; ), where [ ] is the term evaluation function for the well-founded monotone algebra B as de ned in section 2.1. By de nition we have 1 (x) = (x ) for x , where : B M 0 (A) is the 2 F
X
2 T
6
h
i
!
2
F X
2 X
!
IN
Termination: interpretation and type elimination
projection on the rst coordinate
n [
1 (ti ) i=1 1 (f (t1 ; : : : ; tk )) = hfA i(1 (t1 ); : : : ; 1 (tk )) for f 1 (a(t1 ; : : : ; tn )) =
15
2 F
; f = a: 6
Let C [ ] be any non-trivial context in which a does not occur. Since fA (X1 ; : : : Xi?1 ; Y Z; Xi+1 ; : : : Xk ) = h
i
[
fA (X1 ; : : : Xi?1 ; Y; Xi+1 ; : : : Xk ) fA (X1 ; : : : Xi?1 ; Z; Xi+1 ; : : : Xk ) for all operation symbols f occurring in C [ ], we see that n [ 1 (C [a(x1 ; : : : ; xn )] = 1 (C [xi ]) = 1 (a(C [x1 ]; : : : ; C [xn ])): h
i
[ h
i
i=1
Since a does not occur in C [ ], for every term t we have 2 (C [t]) = c 2 (t)p for some c 1 and p > 1. As a consequence we have
2 (C [a(x1 ; : : : ; xn )] = c (1 +
> 1+
n X i=1
n X i=1
2 (xi ))p
c 2 (xi )p = 2 (a(C [x1 ]; : : : ; C [xn ])):
As a consequence, for any distribution rule C [a(x1 ; : : : ; xn )] a(C [x1 ]; : : : ; C [xn ]) in R we have C [a(x1 ; : : : ; xn )] >B a(C [x1 ]; : : : ; C [xn ]): In order to prove l >B r for the other rules l r we rst prove by induction on t that for all terms t and all s 1 (t) there is some u Ea (t) such thatSsn 1 (u). For t we simply choose u = t. For t = a(t1 ; : : : ; tn ) we have 1 (t) = i=1 1 (ti ), so there is some i such that s 1 (ti ). Now the induction hypothesis yields some u Ea (ti ) Ea (t) for which s 1 (u). Finally, for t = f (t1 ; : : : ; tk ) for f = a we have 1 (t) = fA (1 (t1 ); : : : ; 1 (tk )). If s 1 (t) then from the de nition of fA it follows that there are si 1 (ti ) such that s = fA(s1 ; : : : ; sk ). Now the induction hypothesis yields ui Ea (ti ) for which si 1 (ui ). Now choose u = f (u1; : : : ; uk ), then u Ea (t) and s 1 (u), which we had to prove. Let l r be any rule in R in which a does not occur in l. Choose s 1 (r) arbitrarily. We shall prove that in any case 1 (l) contains an element strictly greater than s. From the above observation we conclude that there is some u Ea (r) such that s 1 (u). We shall construct : A such that s [](u) and x 1 (x); here we need to distinguish between the cases of right-linearity and totality. For the rst case we need to prove the following fact: !
!
2
2
2
2 X
2
2
2
h
6
i
2
h
i
2
2
2
2
2
!
2
2
X !
2
2
Fact. Let t be a linear term in which the symbol a does not occur and let e 1 (t). 2
De ne t to be the set of variables occurring in t. Then there exists : t such that x 1 (x) for all x t and e = [](t). X
X
2
2 X
!
A
16
H. Zantema
We prove this fact by induction on the structure of t. For the basis of the induction the term t is a variable x and we de ne x = e. For the induction step we have t = f (t1 ; : : : ; tk ); then we have e 1 (t) = 1 (f (t1 ; : : : ; tk )) = fA (1 (t1 ); : : : ; 1 (tk )): From the de nition of fA follows that there are ei 1 (ti ) for i = 1; : : : ; k such A with that e = fA (e1 ; : : : ; ek ). From the induction hypothesis we obtain i : t ei = [i ](ti ) for i = 1; : : : ; k. Since the term t is linear all t are disjoint. So there exists : t A such that x = x if x t . This gives x 1 (x); further we obtain e = fA (e1 ; : : : ; ek ) = fA ([](t1 ); : : : ; [](tk )) = [](t); proving the fact. If Ea (R) is right-linear, the term u is linear and we can apply the fact giving a particular : u A. By choosing x arbitrarily in 1 (x) for x A u we obtain : for which x 1 (x) for all x and s = [](u). In the other case we assumed that (A; >) is total. Then for every x the nite non-empty multiset 1 (x) has a unique maximum. De ne : A by choosing x to be this maximum for every x . Then one easily shows by induction on t that for this we have e [](t) for all terms t not containing a and all e 1 (t); here we use again that every e 1 (f (t1 ; : : : ; tk )) can be written as fA (e1 ; : : : ; ek ) for some ei 1 (ti ). In all cases we have constructed some : A for which x 1 (x) for all x and s [](u). Since (A; >) is a well-founded monotone algebra for Ea (R) and l u is a rule of Ea (R), we obtain [](l) > [](u) s: One easily shows by induction on t that [](t) 1 (t) for all terms t not containing a. Since l is a term not containing a, we conclude that we have found an element [](l) in 1 (l) which is strictly greater than s. Since this construction can be done for every s 1 (r), we conclude that the multiset 1 (l) is strictly greater than the multiset 1 (r). Hence [ ](l) > [ ](r): This holds for all : B , so l >B r, which concludes the proof of theorem 12. 2 2
h
h
i
i
2
X i !
X i
X
X
i
!
2
2 X i
!
2 X nX
2
X !
2 X
2 X
X !
2 X
2
2
2
X !
2
2 X
!
2
2
X !
2.7. Termination of SUBST
Let and be binary symbols, a unary symbol, and 1, id and constants. Consider the TRS (x) y (x (1 (y ))) (Abs) (x y) z (x z ) (y z ) (Map) (x y) z x (y z ) (Ass)
"
!
id x 1 id id 1 (x y) (x y)
!
!
!
x
!
1
"
!
"
"
! !
"
x y;
named 0 by Curien et al. (1992), which is essentially the same as the system SUBST of Hardin and Laville (1986). This system describes the process of substitution in combinatory categorical logic. Here `' corresponds to currying, ` ' to composition, `id' to
Termination: interpretation and type elimination
17
the identity, ` ' to pairing and `1' and ` ' to projections. The original termination proof of SUBST by Hardin and Laville (1986) is very complicated; the same holds for the newer proof by Curien et al. (1992). Both papers are devoted only to the termination proof of this particular system. The result implies termination of the process of explicit substitution in untyped -calculus; an overview of explicit substitutions is given by Abadi et al. (1991). In this section we prove that this system is totally terminating. According to proposition 8 it suces to prove total termination of the system consisting only of the three rules (Abs), (Map) and (Ass), since for all other rules the right hand side can be embedded in the left hand side. We see that the rule (Map) is a distribution rule for the operation ` ', and that ` ' does not occur in the left hand sides of (Abs) and (Ass). Hence according to theorem 12 it suces to prove total termination of the eliminated system: (x) y (x 1) (Abs1) (x) y (x (y )) (Abs2) (x y) z x (y z ) (Ass): As a total well-founded monotone algebra we choose , where consists of the integers 0, with the lexicographic order (x1 ; x2 ; x3 ) > (y1 ; y2 ; y3 ) x1 > y1 (x1 = y1 (x2 > y2 (x2 = y2 x3 > y3 ))): We de ne the operations in this algebra as follows: 1 = = (0; 0; 0); (x1 ; x2 ; x3 ) = (x1 + 1; x2 ; x3 );
"
!
!
!
"
IN IN IN
IN
()
_
^
_
^
"
(x1 ; x2 ; x3 ) (y1 ; y2 ; y3 ) = (x1 + y1 ; x1 (y1 + 1) + x2 + y2 ; 2 x3 + y3 + 1): One easily shows that these operations are strictly monotone in all coordinates and that every left hand side of a rule in the eliminated system is strictly greater than the corresponding right hand side. This proves that the system is totally terminating, and hence 0 and SUBST are totally terminating.
3. Type elimination 3.1. Many-sorted term rewriting
In this section we introduce some standard terminology. Let S be a nite set representing the set of types or sorts. An S -sorted set X is de ned to be a family of sets (Xs )s2S . For S -sorted sets X and Y , an S -sorted map : X Y is de ned to be a family of maps (s : Xs Ys )s2S . By S we denote the set of nite sequences of elements of S , including the empty sequence. Let be a set of symbols, called operation symbols. For every operation symbol an arity and a sort is given, described by functions ar : S and st : S: Let be an S -sorted set of symbols, called variables, where the sets s are pairwise disjoint. We de ne the S -sorted set ( ; ) of terms inductively by !
!
F
F !
F !
X
X
T
X
s T (F ; X )s for s 2 S ;
F X
18
H. Zantema
f (t1 ; : : : ; tn ) ( ; )s for f ti ( ; )s for i = 1; : : : ; n. 2 T
2 T
F X
F X
2 F
with ar(f ) = (s1 ; : : : ; sn ) and st(f ) = s, and
i
An S -sorted term rewriting system (TRS) is de ned to be an S -sorted set of rules: a rule l r of sort s consists of two terms l and r of sort s for which l is no variable and r contains no variables that do not occur in l. The corresponding reduction relation is de ned as expected. !
3.2. Persistence
By removing all sort information every many-sorted term can be mapped to a onesorted term as follows. Let 0 be the set of symbols obtained by adding a prime (0 ) to 0 every symbol of . For f with ar(f ) = (s1 ; : : : ; sn ) we de ne the arity of f 0 0 to be n. In this way de nes a one-sorted signature. Since there S is only one sort there is no need for an explicit notation for the sort. We choose 0 = s2S s to be the set of one-sorted variable symbols; recall that the sets s are assumed to be pairwise disjoint. Every term over of any sort can be mapped to a term over 0 by adding prime symbols to all operation symbols. This type elimination map [ : ( ; )s ( 0 ; 0 ); F
F
2 F
2 F
F
X
X
X
F
F
s2S
is inductively de ned by
T
F X
!
T
F
(x) = x for every x s , for every s S ; (f (t1; : : : ; tn )) = f 0 ((t1 ); : : : ; (tn )) for all f right sort. 2 X
S
X
2
2 F
and terms t1 ; : : : ; tn of the
The set s2S ( ; )s can be considered as a subset of ( 0 ; 0 ), namely the set of well-typed terms. The type elimination map is de ned on TRS's in an obvious way: for any manysorted TRS R the one-sorted TRS (R) is de ned to consist of the rules (l) (r) for the rules l r from R. One easily observes that for t1 ; t2 ( ; )s : t1 R t2 (t1 ) (R) (t2 ): A property of binary relations is called persistent if for every many-sorted TRS R the property holds for R if and only if it holds for (R). The notion of persistence is closely related to the notion of modularity as it has been studied extensively by Middeldorp (1990), Toyama and many others. A property of onesorted TRS's is called modular if for every pair of (one-sorted) TRS's R1 and R2 with disjoint sets of operation symbols the property holds for both R1 and R2 if and only if it holds for R1 R2 . Here R1 R2 denotes the union of both TRS's; it is a one-sorted TRS over the disjoint union of the sets of operation symbols. To point out the relationship between modularity and persistence we need the following notion. A property of binary relations is called component closed if it holds for ( ; ) if and only if it holds for (C; (C C ) ) for all components C of ( ; ). Here a component is de ned to be an equivalence class of the equivalence relation generated by . In particular, properties like con uence, weak con uence, termination and weak normalization are component closed properties. T
F X
T
F
X
!
!
2 T
!
()
F X
!
T
!
\ !
T
!
!
Termination: interpretation and type elimination
19
Theorem 13. Every component closed persistent property is modular. Proof. Let be any component closed persistent property. Let R1 and R2 be onesorted TRS's with disjoint sets of operation symbols. We de ne a two-sorted TRS R as follows. The sorts are denoted by s1 and s2 , the operation symbols are the operation symbols from both R1 and R2 . The arity of an operation symbol from R1 is de ned to be (s1 ; s1 ; : : : ; s1 ), and its sort is de ned to be s1 . Similarly the arities and sorts of the operation symbols from R2 envolve solely the sort s2 . The TRS R is de ned to be the union of R1 and R2 with this typing. Now the terms of sort s1 of R correspond one-to-one to the terms of R1 . Further the reduction relation at sort s1 of R corresponds one-to-one to the reduction relation of R1 . The same holds for `1' replaced by `2'; the components corresponding to R are the disjoint union of the components of R1 and the components of R2 . Since is component closed we conclude that (R) (R1 ) (R2 ): On the other hand the terms of (R) correspond one-to-one to the terms of R1 R2 and the reduction relation of (R) corresponds one-to-one to the reduction relation of R1 R2 . So ((R)) (R1 R2 ): Since is persistent (R) and ((R)) are equivalent; combining this with the above results gives (R1 R2 ) (R1 ) (R2 ); which we had to prove. 2 P
P
P
()
P
^ P
P
P
P
()
P
P
P
()
P
^ P
3.3. Termination
Since termination is not a modular property we conclude from theorem 13 that termination is neither a persistent property. The basic counterexample from Toyama (1987a) and the proof of theorem 13 lead to the following counterexample. Let S = s1 ; s2 ; the following variables and operation symbols are de ned: f
g
x is a variable of sort s1 ; y; z are variables of sort s2 ; 0; 1 are constants of sort s1 ; f is an operation symbol of sort s1 and arity (s1 ; s1 ; s1 ); g is an operation symbol of sort s2 and arity (s2 ; s2 ). Let the S -sorted TRS R consist of the following rules: f (0; 1; x) f (x; x; x) g(y; z ) y g(y; z ) z: The S -sorted TRS R is terminating, see example (3) of section 2.1; in the next section we give an alternative proof. On the other hand f (g(0; 1); g(0; 1); g(0; 1)) f (0; g(0; 1); g(0; 1)) f (0; 1; g(0; 1)) f (g(0; 1); g(0; 1); g(0; 1)) !
!
!
!
!
!
!
20
H. Zantema
is an in nite reduction in (R). This implies that termination is not a persistent property. However, we can de ne a particular class of many-sorted TRS's such that termination is persistent for that class. First we need some de nitions. A reduction rule is called a collapsing rule if its right hand side is a single variable. A reduction rule is called a duplicating rule if for some variable the number of occurrences in the right hand side is greater than the number of occurrences in the left hand side. In the above example the rst rule is duplicating and the second and the third rules are collapsing. It has been shown by Rusinowitch (1987) that termination is modular for the class of one-sorted TRS's without collapsing rules and also in the class of one-sorted TRS's without duplicating rules. The following theorem generalizes this result: Theorem 14. Termination is persistent for the class of many-sorted TRS's not con-
taining both collapsing and duplicating rules.
Any in nite reduction of R is trivially translated to an in nite reduction of (R). As a consequence, termination of (R) implies termination of R. The dicult part is the converse: assume termination of R and derive termination of (R). A proof can be given similar to the proofs for modularity presented by Rusinowitch (1987), Middeldorp (1990) and Ohlebusch (1993). However, in all of these proofs many details have been omitted, while lling in these details requires a lot of extra notation and long tedious proofs. One reason for this phenomenon is that two distinct inductive structures of terms are used: any term is uniquely written as f (: : :), but also as C [ : : :] . Though the latter has an intuitive clear meaning, we prefer to avoid it in order to avoid long and tedious proofs that are left to the reader. In the next section we give an alternative proof not referring to C [ : : :] ; the `minimal counterexample'-style has been inspired by Gramlich (1992) and Ohlebusch (1993). A detailed earlier proof has been given in Zantema (1991). There the one-sorted terms are interpreted in the nite multisets over the lexicographic product of the naturals and the many-sorted terms. 3.4. The proof of theorem 14
Let R be a terminating S -sorted TRS over function symbols . S Choose one variable symbol ys for every sort s, let be de ned by s = ys . Let T = s2S ( ; )s . As before let 0 be the one-sorted version of ; to force the existence of ground terms we assume the existence of at least one symbol of arity zero, otherwise we add such a symbol of arbitrary sort to . We introduce the following functions: : ( 0 ) S; : ( 0) ; F
Y
Y
F
f
T
g
F Y
F
F
sort
T
F
!
rank
T
F
! IN
: ( 0 ) T; : ( 0) : Let f be any operation symbol of arity (s1 ; : : : ; sn ) and sort s, and let u1; : : : ; un We de ne (f 0 (u1 ; : : : ; un )) = s: We de ne a partition of 1; : : : ; n into two subsets A and B by A = i 1; : : : ; n (ui ) = si ; top
T
F
!
size
T
F
! IN
sort
f
g
f
B= i f
2 f
g j sort
1; : : : ; n
2 f
g
(ui ) = si :
g j sort
6
g
2 T
( 0 ). F
Termination: interpretation and type elimination
Now
;
rank top
21
and are de ned inductively by (f 0 (u1 ; : : : ; un)) = max(max (ui ); max ( (ui ) + 1)); i2A i2B (f 0 (u1 ; : : : ; un)) = f (t1 ; : : : ; tn ); where ti = (ui ) if i A; if i B; ti = ys size
rank
rank
rank
top
top
size
(f 0 (u1 ; : : : ; un)) = #B +
n X i=1
2
2
i
size
(ui ):
The basis of these inductive de nitions is in the operation symbols of empty arity; the maximum of an empty set of natural numbers is de ned to be 0. In these technical de nitions type clashes are collected in the set B ; in modularity terminology this corresponds to a colour switch. As in modularity represents the nesting depth of these type clashes and represents the maximal topmost part without type clashes. The total number of type clashes is given by . For any one-sorted ground substitution : 0 ( 0 ) we de ne a corresponding S -sorted substitution ~ for which ~s : s ( ; )s is de ned by )) = s ~s (x) = ys ((x)) ifif ((((xx)) = s: rank
top
size
X
X
! T
! T
F
F Y
sort
top
6
sort
0 0 Lemma S 15. Let : X ! T (F ) be any one-sorted groundSsubstitution and let t 2 s2S T (F ; X )s be any term for which either t 62 s2S Xs or t 2 Xs for s =
((t) ). Then
sort
((t) ) = t~ :
top
Proof. By induction on the structure of t; if t is a variable then the assertion holds by de nition. Let t = f (t1 ; : : : ; tn ) for some f of sort s and arity (s1 ; : : : ; sn ). De ne for
i = 1; : : : ; n:
if ((ti ) ) = si if ((ti ) ) = si : If ti is not a variable then ((ti ) ) = si . From the induction hypothesis we conclude ti~ = t~i . If ti is a variable, then ti~ = t~i according to the de nition of ~ . So for all i = 1; : : : ; n we have ti~ = t~i . We conclude ((t) ) = ((f (t1 ; : : : ; tn )) ) = (f 0 ((t1 ) ; : : : ; (tn ) )) = f (t~1 ; : : : ; t~n ) = f (t1~ ; : : : ; tn~ ) = t~ :
t~i =
ys
top((ti ) ) i
sort
6
sort
sort
top
top
top
2
Lemma 16. Let u !(R) v for u; v 2 T (F 0 ) such that sort(u) 6= sort(v). Then rank(u) > and v = (r) for some : X 0 ! T (F 0 ) and some rank(v ). Moreover, u = (l )
collapsing rule l ! r of R.
22
H. Zantema
Proof. The second part is just application of the de nitions; the rst part follows from the second. 2 Lemma 17. Let u
(u)
rank
(v).
!
(R) v for u; v
2 T
( 0 ) and any S -sorted TRS R over . Then F
F
rank
Proof. Let : X 0 ! T (F 0 ) be any one-sorted ground substitution. For any term t in T
( ; ) de ne F X
A(t) = and
B (t) =
[ s2S
[ s2S
x
2 X
x
2 X
f
f
(x ) = s ;
s j x occurs in t and
sort
s j x occurs in t and
sort
g
(x ) = s : 6
g
Using the de nition of one easily proves by induction on the structure of t that ((t) ) = max(xmax (x ); xmax ( (x ) + 1)) 2A(t) 2B(t) rank
rank
rank
rank
for every non-variable term t. Let l r be a rule of R. Since all variables of r occur in l we have A(r) A(l) and B (r) B (l). From the above equality we conclude ((l) ) ((r) ). For reduction steps in context the proof is straightforward by induction on the context and using lemma 16. 2 !
rank
rank
We de ne an outer destructive step of (R) to be a step u (R) v with (v). We de ne an inner destructive step of (R) inductively as follows: !
(u) =
sort
6
sort
Let f be a symbol of arity (s1 ; : : : ; sn ), let 1 i n and let t1 ; : : : ; ti?1 ; ti+1 ; : : : ; tn ( 0 ). Let u (R) v be an outer destructive step with si = (u). Then f 0 (t1 ; : : : ; ti?1 ; u; ti+1 ; : : : ; tn ) (R) f 0 (t1 ; : : : ; ti?1 ; v; ti+1 ; : : : ; tn ) is an inner destructive step. Let u (R) v be an inner destructive step. Then f 0 (: : : ; u; : : :) (R) f 0 (: : : ; v; : : :) is an inner destructive step.
T
F
!
6
2
sort
!
!
!
A destructive step is de ned to be either an inner destructive step or an outer destructive step. Note that application of a non-collapsing rule cannot aect the sort of a term, hence in a TRS without collapsing rules no destructive steps exist at all. Lemma 18. Let u top(u) !R top(v ).
!
(R) v be a non-destructive step. Then either
(u) =
top
(v) or
top
Proof. For a reduction step of the shape (l) (R) (r) we apply lemma 15. For a reduction step of the shape f 0 (: : : ; u; : : :) (R) f 0 (: : : ; v; : : :) we apply the de nitions of and destructive step. 2 !
!
top
Termination: interpretation and type elimination
23
From now on we assume that R is terminating and (R) is not terminating. We say that an in nite reduction
u1
!
u2
u3
!
!
in (R) is of minimal rank if no in nite reduction exists for which the rst term has a smaller . From lemma 17 it is clear that all ui have the same . rank
rank
Lemma 19. Let u1 ! u2 ! u3 ! be an in nite reduction in (R) of minimal rank. Then there is some i such that top(ui ) 6= top(ui+1 ). Proof. Assume one of the steps is of the shape (l) !(R) (r) . From termination
of R and lemma 15 we conclude ((l) ) = l~ = r~ = ((r) ): In the remaining case we do induction on the depth of u1 . We have ui = f 0 (ui;1 ; : : : ; ui;n ) where for every i there is one j such that ui;j ui+1;j and ui;k = ui+1;k for k = j . Since f 0 has only nitely many arguments there is for some j an in nite reduction top
6
top
!
6
u1;j (R) u:::;j (R) u:::;j (R) : From the induction hypothesis we obtain some i for which (ui;j ) = (ui+1;j ). From the minimality of rank we see (ui;j ) = (ui ), hence (ui;j ) = sj where (s1 ; : : : ; sn ) is the arity of f . So (ui ) = (ui+1 ). 2 !
!
!
top
rank
top
6
rank
6
top
sort
top
Lemma 20. Any in nite reduction in (R) of minimal rank contains in nitely many
destructive steps.
Proof. Assume there is an in nite reduction in (R) of minimal rank with only nitely many destructive steps. After skipping a nite part we obtain an in nite reduction of minimal rank without destructive steps. Apply on each term in the reduction. From lemma 18 we see that each step translates either to equality or to a reduction step of R. Since R is terminating the latter occurs only nitely often. After skipping this nite part we arrive at an in nite reduction of minimal rank on which is constant, contradicting lemma 19. 2 top
top
Theorem 21. Let R be an S -sorted TRS without collapsing rules. Then R is terminating
if and only if (R) is terminating.
Proof. The `if'-part is trivial. The `only if'-part follows from lemma 20 since according
to lemma 16 a TRS without collapsing rules does not allow destructive steps at all. 2
For proving persistence of termination of TRS's without duplicating rules, we need some lemmas on the function . size
Lemma 22. Let u !(R) v be a destructive step. Then size(u) > size(v). Proof. First assume it is an outer destructive step. Then u = (l) and v = x for some : X 0 ! T (F 0 ) and some collapsing rule l ! x. Let (l) = f 0(u1 ; : : : ; un ). If uk = x for some k then the lemma follows from the de nition. Otherwise x occurs in
24
H. Zantema
some non-variable uk , and assuming (uk ) > (x ) as an induction hypothesis we obtain (u) = (f 0 (: : : ; uk ; : : :) ) (uk ) > (x ) = (v): An inner destructive step is, by de nition, of the shape u = f 0 (: : : ; ui ; : : :) (R) f 0 (: : : ; vi ; : : :) = v where ui vi is either an outer destructive step for which (ui ) does not t in the i-the coordinate of f , or an inner destructive step. In both cases the lemma follows by induction on the depth of u. 2 size
size
size
size
size
size
size
!
!
sort
Lemma 23. Let u !(R) v be the application of a non-duplicating rule for u; v 2 T (F 0 ).
Then size(u) size(v).
Proof. Let : X 0 ! T (F 0 ) be any one-sorted ground substitution. Then for any term t in T (F ; X ) and any x 2 X 0 let n(t; x) be the number of occurrences of x in t, and let
B (t) =
[
s2S
x
f
2 X
s j x occurs in t and
(x ) = s :
sort
6
g
Using the de nition of one easily proves by induction on the structure of t that X X n(t; x) ((t) ) = ( n(t; x) (x )) + size
size
x2X
size
0
x2B (t)
for every non-variable term t. Since n(l; x) n(r; x) for all x 0 we conclude ((l) ) ((r) ) for any rule l r. For reduction steps in context the proof is straightforward by induction on the context and using lemma 22. 2
size
2 X
size
!
Theorem 24. Let R be an S -sorted TRS without duplicating rules. Then R is terminat-
ing if and only if (R) is terminating.
Proof. The `if'-part is trivial. For the `only if'-part assume that R is terminating and (R) allows an in nite reduction. Take an in nite reduction of minimal rank, then lemmas 20, 22 and 23 yield an in nite descending sequence of natural numbers, giving a contradiction. 2 3.5. Applications
Theorem 14 can be used as a tool for proving termination of TRS as follows. Given a (one-sorted) TRS not containing both collapsing and duplicating rules, we try to nd a typing in such a way that all rules are well-typed. This can be done straightforwardly in a way similar to uni cation. Once we get a many-sorted TRS, theorem 14 states that for proving termination of the original one-sorted system it suces to prove termination of the many-sorted system. For example, termination of the (one-sorted) system f (0; 1; x) f (x; x; x) !
Termination: interpretation and type elimination
25
from Toyama's example can be proved by considering the typing in which 0; 1; x have sort s1 , and f has sort s2 and arity (s1 ; s1 ; s1 ). Then this many-sorted system is trivially terminating since at most one reduction step can be done. From theorem 14 we conclude that also the original one-sorted system is terminating. Another example is s(x) + (y + z ) x + (s(s(y)) + z ) s(x) + (y + (z + w)) x + (z + (y + w)) from Hofbauer and Lautemann (1989). The termination proof can be given via the typing s : s1 s1 ; + : (s1 ; s2 ) s2 : Many-sorted terms of sort s2 then represent lexicographically decreasing sequences of natural numbers. From these examples we see that type elimination provides a far more powerful tool for proving termination than the similar modularity result. !
!
!
!
As another application we mention a simpli cation of some undecidability proofs. We sketch a short proof of the undecidability of termination of arbitrary TRS. Let any arbitrary Turing machine be given; we will associate a TRS such that the TRS is terminating if and only if the Turing machine terminates on all arbitrary initial states. For every machine state a binary symbol qi is introduced; for every alphabet symbol a unary symbol ai . The end of the tape will be denoted by a constant 0. Every con guration of the Turing machine is uniquely coded by a term qi (Sl (0); Sr (0)) where Sl is the string of ai symbols denoting the tape left of the head and Sr is the string of ai symbols denoting the tape right of the head. Now the transition function can be described by a nite number of rewrite rules, each of the shape qi (Sl (x); Sr (y)) qj (Sl0 (x); Sr0 (y)); where Sl ; Sr ; Sl0 ; Sr0 consist of zero, one or two symbols ai . We have to prove that the TRS constructed in this way is terminating if and only if the Turing machine terminates on arbitrary input. This is not trivial since not every term corresponds to a state of the Turing machine. However, from theorem 14 we know that the (one-sorted) TRS is terminating if and only if the many-sorted TRS obtained by the typing 0 : s1 ; ai : s1 s1 ; qi : (s1 ; s1 ) s2 is terminating. The terms of this many-sorted system of sort s2 correspond exactly to the states of the Turing machine, while on terms of sort s1 no rules are applicable, proving the statement. The rst proof of undecidability of termination was given in Huet and Lankford (1978), using only unary symbols. The suggestion to represent machine states by binary symbols like we did above can also be found in Klop (1991). Our theorem 14 can also be applied for simplifying the proof of undecidability of termination of a single left-linear rule as given in Dauchet (1992); the notion of Mcomponent and all its technical details can simply be removed there. In their proof of undecidability of simple termination for one rule systems based on Dauchet's work, Middeldorp and Gramlich (1993) make use of theorem 14. !
!
!
!
26
H. Zantema
3.6. Confluence
It has been shown by van de Pol (1992) that persistence is equivalent to the manysorted extension of the notion of modularity. Although it has not yet been executed in full detail, it seems that Toyama's proof of modularity of con uence (Toyama (1987b)) can straightforwardly be modi ed to a proof of many-sorted modularity of con uence, and hence to a proof of persistence of con uence. Persistence of con uence can be used as a tool for proving con uence of (one-sorted) TRS. We illustrate this by an example, due to Y. Toyama. Consider the one-sorted TRS: a(x; y) a(f (x); f (x)) f (x) g(x) b(f (x); x) b(x; f (x)) b(g(x); x) b(x; g(x)): Proving con uence of this TRS is not easy, and modularity of con uence can not be applied. However, we can de ne a many-sorted version of this TRS with the same rules, in which f and g both have arity and sort s1 , a and b have arity (s1 ; s1 ), a has sort s2 and b has sort s3 . Proving con uence of this many-sorted TRS can be done using standard techniques: !
!
!
!
terms of sort s1 are con uent since only the second rule is applicable; terms of sort s2 are con uent since the rst two rules of the system form an orthogonal TRS; terms of sort s3 are con uent since the TRS consisting of the last three rules is terminating and all critical pairs have a common reduct. Now the con uence of the original TRS follows from persistence of con uence. This example illustrates how type elimination provides a tool for proving con uence of onesorted TRS's. This tool is more powerful than applying modularity.
4. Concluding remarks
This paper consists of two independent parts of which earlier versions appeared as Zantema (1993a) and Zantema (1993b). The rst part is an investigation of proving termination by interpretation of terms in a well-founded monotone algebra. This can be considered as a generalization of polynomial interpretations. It allows a full characterization of termination: a TRS is terminating if and only if it allows a compatible well-founded monotone algebra. We gave a classi cation of types of termination built on properties of the underlying order. In particular, if the underlying order is total the TRS is called totally terminating. Total termination is the basis of further research of Ferreira and Zantema (1993). It is not clear whether total termination can be characterized apart from the monotone algebra framework. We introduced a transformation on TRS's eliminating distributive rules. Using the monotone algebra framework we proved a theorem on preservation of termination for this distribution elimination. In particular we gave a new termination proof of the system SUBST of Hardin and Laville (1986) that is far simpler than the existing proofs. In the second part of this paper we introduced the notion of persistence and proved
Termination: interpretation and type elimination
27
that it is a generalization of direct sum modularity. We proved that termination is a persistent property for a similar class of TRS's for which Rusinowitch (1987) proved termination to be modular. This result provides a new and powerful tool for proving termination of TRS's. Proving persistence of weak normalization and weak con uence is not dicult; proving persistence of con uence seems to be feasible. For notions like simple termination as considered in the rst part of the paper the notion of persistence does not make sense since it is not clear how to de ne simple termination for many-sorted TRS-s. As a conjecture we state that termination is a persistent property for the class of TRS's in which all variables are of the same sort.
References
M. Abadi, L. Cardelli, P.-L. Curien, and J.-J. Levy. Explicit substitutions. Journal of Functional Programming, 1(4):375{416, 1991. A. Ben-Cherifa and P. Lescanne. Termination of rewriting systems by polynomial interpretations and its implementation. Science of Computing Programming, 9(2):137{159, 1987. P.-L. Curien, T. Hardin, and A. Ros. Strong normalization of substitutions. In I. M. Havel and V. Koubek, editors, Proceedings Mathematical Foundations of Computer Science 1992, volume 629 of Lecture Notes in Computer Science, pages 209{217. Springer, 1992. M. Dauchet. Simulation of Turing machines by a regular rewrite rule. Theoretical Computer Science, 103(2):409{420, 1992. Appeared before in Proceedings of RTA89, Lecture Notes in Computer Science 355, Springer, 1989. N. Dershowitz and Z. Manna. Proving termination with multiset orderings. Communications ACM, 22(8):465{476, 1979. N. Dershowitz. Termination of rewriting. Journal of Symbolic Computation, 3(1 and 2):69{116, 1987. H. Ehrig and B. Mahr. Fundamentals of Algebraic Speci cation, volume 1 of EATCS monographs on Theoretical Computer Science. Springer, 1985. M. C. F. Ferreira and H. Zantema. Total termination of term rewriting. In C. Kirchner, editor, Proceedings of the 5th Conference on Rewriting Techniques and Applications, volume 690 of Lecture Notes in Computer Science, pages 213{227. Springer, 1993. I. Gnaedig and P. Lescanne. Proving termination of associative commutative rewriting systems by rewriting. In J. H. Siekmann, editor, Proceedings of the 8th Conference on Automated Deduction, volume 230 of Lecture Notes in Computer Science, pages 52{61. Springer, 1986. B. Gramlich. Generalized sucient conditions for modular termination of rewriting. In H. Kirchner and G. Levi, editors, Algebraic and Logic Programming, volume 632 of Lecture Notes in Computer Science, pages 53 { 68. Springer, 1992. T. Hardin and A. Laville. Proof of termination of the rewriting system SUBST on CCL. Theoretical Computer Science, 46:305{312, 1986. D. Hofbauer and C. Lautemann. Termination proofs and the length of derivations (preliminary version). In N. Dershowitz, editor, Proceedings of the 3rd Conference on Rewriting Techniques and Applications, volume 355 of Lecture Notes in Computer Science, pages 167{177. Springer, 1989. D. Hofbauer. Termination proofs by multiset path orderings imply primitive recursive derivation lengths. Theoretical Computer Science, 105(1):129{140, 1992. G. Huet and D. S. Lankford. On the uniform halting problem for term rewriting systems. Rapport Laboria 283, INRIA, 1978. S. Kaplan. Simplifying conditional term rewriting systems: uni cation, termination and con uence. Journal of Symbolic Computation, 4(3):295{334, 1987. J. W. Klop. Term rewriting systems. In D. Gabbay S. Abramski and T. Maibaum, editors, Handbook of Logic in Computer Science, volume 1. Oxford University Press, 1991. J.B. Kruskal. Well-quasi-ordering, the tree theorem, and Vazsonyi's conjecture. Trans. American Mathematical Society, 95:210{225, 1960. K. Kuratowski and A. Mostowski. Set Theory. North-Holland Publishing Company, 1968. M. Kurihara and A. Ohuchi. Modularity of simple termination of term rewriting systems. Journal of IPS Japan, 31(5):633{642, 1990. D. S. Lankford. On proving term rewriting systems are noetherian. Technical Report MTP{3, Louisiana Technical University, Ruston, 1979. P. Lescanne. Termination of rewrite systems by elementary interpretations. In H. Kirchner and G. Levi, editors, Algebraic and Logic Programming, volume 632 of Lecture Notes in Computer Science, pages 21 { 36. Springer, 1992.
28
H. Zantema
A. Middeldorp and B. Gramlich. Simple termination is dicult. In C. Kirchner, editor, Proceedings of the 5th Conference on Rewriting Techniques and Applications, Lecture Notes in Computer Science. Springer, 1993. A. Middeldorp. Modular Properties of Term Rewriting Systems. PhD thesis, Free University Amsterdam, 1990. E. Ohlebusch. A note on simple termination of in nite term rewriting systems. Technical Report 7, Universitat Bielefeld, 1992. E. Ohlebusch. A simple proof of sucient conditions for the termination of the disjoint union of term rewriting systems. Bulletin of the EATCS, 49:178{183, 1993. M. Rusinowitch. On termination of the direct sum of term rewriting systems. Information Processing Letters, 26:65{70, 1987. J. Steinbach. Extensions and comparison of simpli cation orderings. In N. Dershowitz, editor, Proceedings of the 3rd Conference on Rewriting Techniques and Applications, volume 355 of Lecture Notes in Computer Science, pages 434{448. Springer, 1989. Y. Toyama. Counterexamples to termination for the direct sum of term rewriting systems. Information Processing Letters, 25:141{143, 1987. Y. Toyama. On the Church-Rosser property for the direct sum of term rewriting systems. Journal of the ACM, 34(1):128{143, 1987. J. van de Pol. Modularity in many-sorted term rewriting systems. Master's thesis, Utrecht University, 1992. H. Zantema. Termination of term rewriting: from many-sorted to one-sorted. In J. van Leeuwen, editor, Computing Science in the Netherlands, volume 2, pages 617 { 629, November 1991. Also appeared as report RUU-CS-91-18, Utrecht University. H. Zantema. Termination of term rewriting by semantic labelling. Technical Report RUU-CS-92-38, Utrecht University, 1992. H. Zantema. Termination of term rewriting by interpretation. In M. Rusinowitch and J.L. Remy, editors, Conditional Term Rewriting Systems, proceedings third international workshop CTRS-92, volume 656 of Lecture Notes in Computer Science, pages 155{167. Springer, 1993. Full version appeared as report RUU-CS-92-14, Utrecht University. H. Zantema. Type removal in term rewriting. In M. Rusinowitch and J.L. Remy, editors, Conditional Term Rewriting Systems, proceedings third international workshop CTRS-92, volume 656 of Lecture Notes in Computer Science, pages 148{154. Springer, 1993.