CENTRAL CIRCULATION BOOKSTACKS The person charging this material is re- sponsible for its renewal or its return to the library from which it was borrowed on or before the Latest Date stamped below. You may be charged a minimum fee of $75.00 for each lost book. Thoft, mutilation, and underlining of book* are reason* for disciplinary action and may result In dismissal from the University. TO RENEW CALl TELEPHONE CENTER, 333-8400 UNIVERSITY OF ILLINOIS LIBRARY AT URBANA-CHAMPAIGN DEC 141998 JUL 5 2000 When renewing by phone, write new due date below previous due date. L162 Digitized by the Internet Archive in 2013 http://archive.org/details/recursivelydefin943plai tO 9^/? UIUCDCS - R -78-943 TVM UILU-ENG 78 1736 NSF MCS 77-22830 A Recursively Defined Ordering for Proving Termination of Term Rewriting Systems by David A. Plaisted September 1978 UIUCDCS-R-78-943 A Recursively Defined Ordering for Proving Termination of Term Rewriting Systems by David A. Plaisted Department of Computer Science University of Illinois at Urbana-Champaign Urbana, Illinois 61801 September 1978 This research was supported in part by the National Science Foundation under Grant MCS 77-22830. -2- 1. Introduction Many programs can easily be expressed as sets of term rewriting rules of the form s n ■> t- , . . . , s -*■ t where s, -*■ t. indicates that a linn i 1 term of the form s. can be replaced by a term of the form t . . For example, the factorial function can be expressed as fact(s(x)) -> s(x)*fact(x) fact(O) ■> 1. Algebraic manipulation routines can often be expressed in this way also. For example, the following rules will transform a Boolean expression in- volving a , v, and ~l into disjunctive normal form: -l(xAy) + Ox) v Oy) n(xvy) ■* Ox) a Oy) ~I~lx ■*- X XA(yvz) -*■ (xAy) v (xaz) To show such programs are correct, one only needs to show that the equations are valid in the intended interpretations, and that the final expression will have some desired form. We are interested in proving termination of such programs. Namely, we want to show that there is no infinite sequence u.. , u„, u~, ... such that for all i, u. +1 can be obtained from u . by a replacement using a term rewriting rule. It is easy to show [3] that termination is in general an undecidable property of such programs. However, we are interested in methods for proving termination that frequently work in cases of practical interest. These methods are based on well-founded orderings. We presented some work in this area in [9 ], but at that time we did not have a good way to deal with the replacement -3- x*(y + z) -*■ x+y + x*z and similar replacements. We now present a general proof technique which easily handles such replacements, together with replacements such as those mentioned in [ 9 ] . For other work in this area, see [1], [2], [4], [5], [6], [7], [8]. We present an ordering on terms called the path of sub terms ordering . It is similar to the path ordering defined in [ 9 ] but is defined recursively. We show that this is a partial ordering, and then that it is a "partial simplification ordering" as defined in [9 ]. Proving well-foundedness turns out to be somewhat difficult. We then present efficient methods for computing the ordering on ground terms and on non-ground terms. Finally, we present a general class of replacements s -*■ t all of which are simplifications in this ordering. That is, t < s in the ordering. Any term rewriting system all of whose replacements are of this form is guaranteed to terminate. The two examples mentioned above are both of this form. Given terms s and t, we can in many cases efficiently compute whether t < s in the path of subterms ordering. If t . < s. in this ordering for 1 < i < n, then the set s, -*■ t, , . . . , s -*- t of rewrite — ~ linn rules is guaranteed to terminate. This research therefore gives an efficient, simple method for providing proofs of termination of sets of rewrite rules. 2. Simplification Orderings Definition : A term is an expression formed from function symbols, constant symbols, and variables, properly combined. Thus f(x, g(c, y)) is a term. We consider constant symbols as function symbols having no arguments. A term without variables in it is a ground term. -4- We use the usual notion of substitution- A substitution is considered as a multiple replacement, simultaneously replacing all variables of a term by terms. Thus {x ■*■ f(x), y «- g(c)} is a substitution. We assume all function and constant symbols are taken from same set F of symbols . Definition : We say an ordering "<" is well-founded if there is no infinite sequence t n , t„, t~, ... such that t. > t.., for all 12 3 1 l+l i >. 1. (Such a sequence t , t_, t , ... is called an infinite descending sequence . ) Definition : A partial ordering "<" on terms is a simplification ordering if it has the following four properties: 1. It is a total ordering on ground terms. 2. It is a well-founded ordering. 3. (Consistency with respect to substitution) If t.. and t„ are terms and t 1 < t„ in the ordering, then for all substitutions 9, t 8 < t„8 in the ordering. 4. (Consistency with respect to subterm replacement) If s is a subterm of t, , and t„ is obtained from t. by replacing s 1 by s,,, and s, < s~ in the ordering, then t.. < t~ in the ordering. Intuitively, these are desirable properties for a simplification ordering to have. Property 2 guarantees that the simplification process must terminate. Property A guarantees that simplifying a subterm will also simplify the whole term, so simplification can be done "locally". Definition : A partial ordering on terms is a partial simpli- fication ordering if it has properties 2, 3, and A as above. Thus it need not be a total ordering on ground terms. -5- A replacement is an equation that can only be used in one direction. We write s.. ■> s~ to mean that any instance of s.. can be replaced by the corresponding instance of s„. Definition : A replacement s -> s„ is a simplification with respect to a simplification ordering if s„ is less than s^ with respect to the ordering. Definition : A term t is obtained from term s using the replacement s. ■*■ t. if there is some substitution 8 with the following properties: s.8 is a subterm of s, and t is obtained from s by replacing one occurrence of s.9 in s by t.8. 1 J l Note that if t. < s. in some partial simplification ordering then t.8 < s.8 also by consistency with respect to substitution. Hence l l J J r t < s by consistency with respect to subterm replacement. We say that a set s_ -*■ t, , s ■*■ t„, . . . , s + t of rewrite J 1 1 2 2 n n rules fails to terminate on input u if there is an infinite sequence u , u , u„, ... such that for all i > 1, u -+i ^ s obtained from u. using some replacement in the set. If no such infinite sequence exists, we say the set of rewrite rules terminates on input u n . Theorem [8 ] Suppose s_ -*■ t_ , . . . . s ■> t is a set of rewrite — rr 11 n n rules. Suppose there is a partial simplification ordering "<" such that t. < s. in the ordering for 1 < i < n. Then the set of rewrite rules terminates on all inputs. Proof : Assume the rules fail to terminate on input u . Let u , u_, u_, ... be an infinite sequence of terms such that for all i .> 1, u , is obtained from u. by using some replacement in the set of rewrite rules. We showed above that u . , ., < u. in the ordering, for all i. l+l l -6- Hence u , u_, u^, ... is an infinite descending sequence in the ordering. But this is impossible, because a partial simplification ordering is well-founded. Thus the given set of rewrite rules is guaranteed to terminate on all inputs. 3. Multisets A multiset is a set in which an element can occur more than once. We write multiset union as ^ or sometimes U. The number of occurrences of an element in A U B is the sum of the number of its occurrences in A and B. Definition : If S is a multiset of elements ordered by some total order relation, let List(S) be a list {s. , s_, ..., s } 1 2. m of the elements of S in non- increasing order. Each element must appear the same number of times in List(S) as in S. Definition : Suppose "<' is a partial ordering such that there exists an equivalence relation "=" satisfying the following conditions 1. x 1 = x 2 and y = y 2 implies x ;[ < y iff x 2 < y . 2. x, = x_ iff x. and x» are unrelated in the ordering (that is, ~l(x < x ) and ~l(x 2 < x )). We then write x, <_ x~ to indicate x.. < x~ or x 1 = x„ . Also, v;e v;ritc [x] to refer to the equivalence class of x in this equivalence relation. That is, [x] ■ {y: yEx). Given such a partial ordering on some set S, we define a partial ordering having the same property, on finite multisets of elements of S as follows: Suppose U and V are multisets of elements of S. Suppose U j V. Let {u r u 2 , ..., u R } be List(U) and let {v r v^ ..., v £ } be List(V). Tf [ Ul ], [u 2 3 [u k l is a nrcfix of [v ± ] , [Vg] , [v £ ] we say U < V. If [v^, [Vj], ..., [vj is a prefix of [u^, [u 2 ], ..., [u k ] we say V < U. Otherwise, let j be min{i: u < v. or v, < u.}- -7- Then we say U < V if u < v,, U > V if u, > v.. We call this ordering j J J I on multisets the ordering induced by the ordering on S. 4. The Path of Subterms Ordering We now define the path of subterms ordering and show that it is a partial simplification ordering. We assume that there is a total ordering on the function and constant symbols F appearing in terms t that we are dealing with. If g > f in this ordering, we intuitively think of g as being more complicated than f, and we say g is larger than f. 4.1 Definition of the Ordering Definition: If t is a term of form f(t n , ..., t ), a 1 n path of subterms of t is the sequence consisting of t itself followed by a path of subterms for t. for some i, 1 < 1 < n. If t is a variable, then t itself is the only path of subterms for t. Thus a path of subterms for f(g(a, b), c) is the sequence f(g(a, b) , c) , g(a, b) , b. Also, a path of subterms of the term f(x, g(y, c)) is the sequence f(x, g(y, c)), g(y, c), y. Definition : Let SPaths(t) be the multiset of paths of subterms of t. More precisely, if t is of the form fCt^, ..., t R ) n then SPaths(t) is {t} ^ SPaths(t i >. If t is a constant symbol c, then SPaths(t) is {c}. If t is a variable, then SPaths(t) is {t}. Definition : If t is a term of the form fCt^, ..., t^) , n let PSPaths(t) be V£ SPathsCt^) . -8- Definition : If a is a sequence of terms, then Subseq(a) is the multiset of subsequences of a. Thus Subseq(tS) - {t, A}Subseq(g) = {t}Subseq(3) W Subseq(B). Also, Subseq(A) = A. (Here A is the sequence of length zero.) Definition : Suppose t and u are terms. We say t and u are equivalent up to a permutation of arguments if t and u are both variables, or if a) the top-level function symbols of t and u are identical. (Suppose t is of the form f(t.., ..., t ) and u is of the form f(u , ..., u )), and b) there is a one-to-one correspondence between the multiset {t.. , . . . , t } and {u,. ..., u } such that if t. and u. correspond to each In 1 j other then t and u. are equivalent up to a permutation of arguments. Example: f(g(a, b), h(a, c)) and f(h(a, c) , g(b, a)) are equivalent up to a permutation of arguments. We write s ^ t if s and t are identical up to a permutation of arguments. Definition : If t is a term, then Size(t) is the sum of the number of occurrences of all function symbols, constant symbols, and variables in t. For example, Size(f(x, f(x, c))) = 5. Given ground terms s and t, the following procedure will efficiently determine if s and t are identical up to a permutation of arguments. -9- Suppose s is of form f(s , ..., s ) and t is of form f(t n , ..., t ). -L n in (If the top-level function symbols of s and t are not the same, s and t are clearly not identical up to a permutation of arguments.) Let s! and t! be "canonical forms" of s and t , respectively. We obtain the canonical form recursively in a manner to be described. Let s 1 " s 2 " ... s n " and t^" t 2 " ... t " be a listing of the multisets ^ S l ' ""' S n'^ and ^l'' *'*' t n'^' res P ectivel y> in non- increasing order. Any total ordering on ground terms will do here; lexicographic ordering is probably the simplest to use. The canonical form of s is defined to be f(s ", . .., s ") 1 n and that of t is defined to be f(t ", ..., t ") . If these canonical i n forms are identical, then s and t are identical up to a permutation of arguments. Otherwise, s and t are not identical up to a permutation of arguments. Suppose s and t are not ground terms. Let x....x be the rr ° 1 m variables appearing in s and t. Let c....c be new constant symbols. Let s* and t* be s and t with x. replaced by c. everywhere, for 1 <. i <. m. Then s* and t* are ground terms, and s* ^ t* iff s ^ t. Hence the above procedure can be applied to s* and t*. Assuming that the set of function symbols is fixed, the 2 time required to put s in canonical form is 0(Size(s) ) using this method. The time to test if s and t are identical up to a permutation 2 2 of arguments is then 0(Size(s) + Size(t) ). Later we will give a more complicated method for deciding if s ^ t, that has a better asymptotic time bound. -10- Def inltion : Suppose s , s„, ..., s and t.. , t ? , ..., t are paths of subterms. We write s, , s„, ..., s ^ t, , t„, ..., t iff a) m = n and b) for all i, 1 < i < m, s ^ t . . In this case, we say that the paths s n , s„, .... s and t, , t~, .... t J r 12' 'm 12' 'n are identical up to a permutation of arguments. Definition : The intermediate path of subterms ordering on ground terms is defined as follows: Suppose f is the top-level function symbol of ground term t and g is the top-level function symbol of ground term u. Then t < u in the intermediate path of subterms ordering if f < g in the function symbol ordering. Suppose both t and u have the same top-level function symbol f. Suppose t is of the form f(t, , t„, ..., t ) and u is of the form 1 Z. n f(Uj, u_j ..., u ). In this case, we define the ordering recursively, assuming that we already know how proper subterms of t and u are ordered with respect to each other in the intermediate path of subterms ordering. Let S be the set of terms r such that r is a proper subterm of t or u. Assume inductively that all elements of S are ordered with respect to each other by the intermediate path of subterms ordering, except those terms which are equivalent up to a permutation of arguments. Order sequences of elements of S lexicographically. That is, if q = q, , . . . , q n and r = r, , ..., r are two sequences of elements 1 x, 1 m of S, then q < r if a) q is empty and r is non-empty b) q 1 < r 1 in the intermediate path of subterms ordering, or -11- c) q.. and r are equivalent up to a permutation of arguments, and (q , ..., q p ) < (r_, ..., r ) in the lexicographic z. ~ z m ordering on sequences of elements of S. Order multisets of sequences of elements of S by the multiset ordering induced by the above ordering on sequences. If a and 3 are paths in PSPaths(t) ^ PSPaths(u), then order a and 3 by the above multiset ordering on Subseq(a) and Subseq(3). We say t < u in the intermediate path of subterms ordering if PSPaths(t) < PSPaths(u) in the multiset ordering induced by the above ordering on paths. It is not difficult to show that t and u are ordered with respect to each other in this ordering unless they are equivalent up to a permutation of arguments. Thus we obtain by induction the result that ground terms are ordered by this ordering unless they are equivalent up to a permutation of arguments. Definition : Let t and u be arbitrary ground terms. Then t < u in the path of subterms ordering if SPaths(t) < SPaths(u). That is, we order SPaths(t) and SPaths(u) by the multiset ordering induced by the ordering on paths of subterms. The purpose of this definition is to eliminate the large dependence of the ordering on the top-level function symbols of t and u. The intuition behind this definition is that we want to weight function symbols more if they occur at the top of a more complex subterm. Thus the * in x*(y + z) would be weighted more highly than either * in x*y + x*z, and so we get that x*(y + z) -*■ x*y + x*z is a simplification in the path of subterms ordering, extended to non- ground terms in the usual way. -12- Def inition : Suppose s and t are terras, possibly with variables in them. Then s < t in the path of subterms ordering if for all substi- tutions 8 such that s9 and t9 are ground terms, s6 < t9 in the path of subterms ordering on ground terms. Note that this definition guarantees that the path of subterms ordering is consistent with respect to substi- tution. 4.2 Proof that the Ordering is a Partial Simplification Ordering We now show that the path of subterms ordering is a partial ordering, is consistent with respect to subterm replacement, and is well-founded. We have just noted that it is consistent with respect to substitution. Theorem 4.1. The intermediate path of subterms ordering on ground terms is a partial ordering. That is, it is non-reflexive, anti-symmetric, and transitive. Proof : Suppose t.. and t ? and t„ are ground terms. Let Sub- terms (t) be the set of proper subterms of t. Let S be Subterms (t) U Subterms (t ? ) U Subterms (t„) . Assume inductively that the intermediate path of subterms ordering is a partial ordering on S. Also, note that if s 1 e S and s_ e S and s.. ^ s„ fails to hold, then either s.. < s_ or s ? < s.. in the intermediate path of subterms ordering. It follows that this ordering is a partial ordering on sequences of elements of S, ordered as specified. Hence it is a partial ordering on multisets of sequences of elements of S, ordered by the induced multiset ordering. Hence if t,, t~> and t^ have the same top-level function symbol, the intermediate ordering is a partial ordering on {t , t„, t~}. If the top-level function symbols are not the same, it is easy to show that the interme- diate ordering is still a partial ordering on {t., t ? , t„}. -13- Furthermore, since the intermediate path of subterms ordering is a partial ordering on all sets of three terms, it is a partial ordering on the set of all terms. Corollary : The path of subterms ordering is a partial ordering. Proof : The path of subterms ordering on t.. and t„ is the same as the intermediate path of subterms ordering on f(t..) and f(t„) for some function symbol f. We now recall some basic properties of the subsequence ordering adapted from [ 9 ] . Suppose a and 3 are sequences of terms. Suppose s and t are terms, and s ^ t . Then Subseq(a) < Subseq(3) iff Subseq(sa) < Subseq(t3). Also, Subseq(a) < Subseq(3) iff Subseq(as) < Subseq(3t). In addition, if a ^ 3 does not hold, then either Subseq(a) < Subseq(3) or Subseq(3) < Subseq(a). It follows that if a is a proper prefix or a proper suffix of 3, then Subseq(a) < Subseq(3). Suppose a and 3 are sequences of terms. Suppose a = a sa„ and 3 = 3-,t3 9 where a , a~, $ , and 3 9 are sequences of terms and s and t are terms. Suppose s is a maximal term in a and t is a maximal term in 3 in the intermediate path of subterms ordering . Suppose 3 does not occur in a. and t does not occur in 3, • Then Subseq(a) < Subseq(3) iff any of the following conditions are true: 1. s < t in the intermediate path of subterms ordering 2. s ^ t and Subseq(a ) < Subseq(3 2 > 3. s ^ t and a 2 % 3 and SubseqCcO < Subseq(3,). Theorem 4.2 Suppose s and t are ground terms and s is a strict subterm of t. Then s < t in the path of subterms ordering. -14- Proof: We know that s < t iff SPaths(s) < SPaths(t) in the multiset ordering. For every path a in SPaths(s), there is a path g in SPaths(t) such that a is a proper suffix of $. Hence a < 3 in the ordering on paths of subterms. Hence the maximal element of SPaths(s) is less than the maximal element of SPaths(t) in the ordering on paths of subterms. Hence SPaths(s) < SPaths(t), and s < t in the path of subterms ordering. Theorem 4.3 Suppose s and t are ground terms and s is a strict subterm of t. Suppose that the top-level function symbols of s and t are identical. Then s < t in the intermediate path of subterms ordering. Proof : We know that s < t in this ordering if PSPaths(s) < PSPaths(t) in the ordering on multisets of paths of terms. But PSPaths(s) < PSPaths(t), by an argument similar to that used in the above theorem. Theorem 4.4 Suppose t is a ground term of form f(t.., ..., t ). Suppose t! is a ground term which is simpler than t. in the path of -J J subterms ordering, for some j, li j <- n. Let t' be the term f(t lf ..., t. 1 , t!, t. +1 , ..., t ). Then t ' < t in the path of subterms ordering. Proof : We know that t ' < t iff SPaths(t') < SPaths(t). Now, SPaths(t') = {t'}PSPaths(t') and SPaths(t) = { t }PSPaths (t) . We first show that t ' < t in the intermediate path of subterms ordering. Since t' and t have the same top-level function symbol, t' < t in this^ordering iff PSPaths(t') PSPaths(t). But n PSPaths(t') .W- SPaths(t.) i=l l *n v*» SPaths(t!) and PSPaths(t) = .W SPaths(t i ). Hence PSPaths(t') < PSPaths(t) iff SPaths(t!) < SPaths(t.). But this is true because t! < t. in the path J J J J of subterms ordering. -15- We now show that t' < t in the path of subterms ordering. We need to show that {t ' }PSPaths (t ') < { t}PSPaths (t) . This is true because t' < t in the intermediate path of subterms ordering and because PSPaths(t') < PSPaths(t) as shown above. This completes the proof. Corollary 1 : Suppose t is a ground term and s is a subterm of t. Suppose s' is a ground term and s' < s in the path of subterms ordering. Suppose t 1 is t with an occurrence of s replaced by s'. Then t' < t in the path of subterms ordering. Proof : By induction, using the above theorem. Corollary 2 : Suppose t is an arbitrary term, possibly with variables in it. Suppose s is a subterm of t. Suppose s' is another term and s' < s in the path of subterms ordering. Suppose t ' is t with an occurrence of s replaced by s'. Then t' < t in the path of subterms ordering. Proof : Using Corollary 1 and the definition of the path of subterms ordering on non-ground terms. 4.2.1 Well-Foundedness We now show that the path of subterms ordering is well- founded. Theorem 4. 5. Suppose a is a maximal path of subterms in SPaths(t) for ground term t. Suppose a is t , t„, ..., t . (Thus each t. is a top-level subterm of t. , , for 2 < i <. n, and t, is t i l-l ' 1 and t is a constant.) Then for all i, 2 <. i <_ n, the path t., t JM , n l i+1 ..., t is a maximal path of subterms in PSPaths(t. n ). n i-1 Proof: If not, we could replace t., t . , n , ..., t by a l i+1 n larger path to get a larger element of SPaths(t). -16- Theorem 4. 6. Suppose t.. and t~ are ground terms and a and cu are maximal paths in SPaths(t ) and SPaths(t 2 ), respectively. Suppose a = 3,v Y, and a = $ 2 v 2 Y 2 where 3,, 3 2 > Y-.> Y 2 are sequences of terms and v.. , v~ are terms. Suppose that v, and v~ are the same to within a permutation of arguments. Then a) Y-i a nd y 9 are the same to within a permutation of arguments, and b) a < a in the path ordering iff 3-. < 3 9 in the path ordering. Proof : By the above result, Yi an d Y 9 are maximal elements of SPaths(v ) and SPaths(v„), respectively. Since v.. and v„ are the same to within a permutation of arguments, so are Y-i a ^d Yv Therefore a^ and a_ have essentially the same suffix (namely, v 1 y 1 or v 2 y 2 ). Hence SubseqCc^) < Subseq(a ) iff 3 < 3 . This completes the proof. Definition : If a is a path of subterms, let mtf(a) be the maximum top-level function symbol of any term in a, in the function symbol ordering. Definition : If a is a path of subterms, let mt(a) be the first term in a whose top-level function symbol is mtf(a). Theorem 4. 7. The term mt(a) is the largest term in a in the intermediate path of subterms ordering. Proof : The largest term in a in this ordering must have mtf(a) as its top-level function symbol, by definition of the intermediate path of subterms ordering. Also, if v and v„ both occur in a and both have f as their top-level function symbol, and if v.. occurs before -17- v in a, then v 9 is a proper subterm of v and so v~ < v.. in the intermediate path of subterms ordering by Theorem 4.3. This completes the proof. Theorem 4.8* Suppose t- and t„ are ground terms and a and a. are maximal paths in SPaths(t..) and SPaths(t„), respectively. Suppose a = 6,v Y, and a = S-yVfll where &t_ > &o' Y l' Y 2 are sec l uences of terms and v.., v„ are terms. Suppose that v.. and v~ are the maximal terms in a. and a~, respectively, in the intermediate path of subterms ordering. Then a) if v.. < v~ in the intermediate path of subterms ordering, then a < a_ in the path ordering, and b) if v.. and v~ are identical to within a permutation of arguments, then a.. < a~ in the path ordering iff 3, < B- in the path ordering. Proof : The first part follows because Subseq(a ) < Subseq(a ) if the maximal element of a is less than the maximal element of a„. The second part follows by theorem 4.6. Note that this result, together with the previous result, gives us a reasonably fast way to decide whether a < a in the path ordering, assuming we know how all terms in a and ou are ordered in the intermediate path of subterms ordering. Definition : If s is a ground term and a is a path of subterms of s, define the stepping sequence of a to be the sequence v , v_ , of elements of a defined as follows: a) v, is mt(a) b) For 1 <- i < m, let a. be the portion of a up to but not including v.. Then v.... is mt (a . ) for 1 <. i < m. l l+l l c) v = s . n -18- Theorem 4.9 . Suppose s and t are ground terms and a and 3 are paths of subterms from s and t, respectively. Let s.. , .. s m and t 1 , . . . , t be the stepping sequences of a and 3, respectively. Let j be min{i: s. and t. are not the same to within a permutation of arguments}, if it exists. Then 1. a < 3 in the path ordering iff a) the stepping sequence of a is a proper prefix of that of 3 or b) i as defined above exists and s. < t. in the inter- 3 3 mediate path of subterms ordering. 2. If j as defined above exists, then for all i >. j such that i _< min(m, n) , s. and t. are not the same to within a permutation of arguments. Proof : A combination of previous results. Note that this allows us to compare paths in time proportional to the number of distinct function symbols, assuming that the stepping sequences are known and that we can in constant time compare terms in the paths in the intermediate path of subterms ordering. In fact, we can do it in 0(log|F|) time, where F is the set of function symbols, by doing binary search and making use of observation 2. Theorem 4.1Q . Suppose s and t are ground terms. Then s < t in the path of subterms ordering iff a) s and t have the same top-level function symbol and s < t in the intermediate path of subterms ordering or b) s and t have different top-level function symbols and a < 3 in the path ordering, where a = max(SPaths(s) ) and 3 = max(SPaths(t) ) . -19- In fact, even if a) is true, a < 3 where a = max(SPaths (s) ) and 3 - max(SPaths (t) ) . However, since a begins with s and 3 with t, we need to know the intermediate path of subterms ordering on s and t in order to compute the ordering on a and 3. For the remainder of this section, we TT ill allor the 3et o.f function symbols to be infinite. We require the function symbol ordering to be total and well-founded. The use of infinite sets of function symbols will be useful for other applications (not discussed in this paper) We write the sequence t n , t„, t„, ... as {t.}.. Thus 12 3 11 1 i i . . r ii ,12 3 . . r 1-, t,, t~, t~, ... is written it.)., and t., t., t., ... is written it./.. 1 2' 3' 3 3 3 3 3 3 1 Definition : Suppose {a.}, is an infinite sequence of arbitrary objects. We say a is the limit of {a.}, if there exists k such that l l for all i > k, a. = a. — l Definition : Suppose it.}, is an infinite sequence of terms. Suppose i , i , i„, ... is a monotone increasing sequence of positive integers. Suppose iu.}. is a sequence of terms such that for all j >^ 1, u. is a proper subterm of t. . Then we say iu.}. is a subsequence of 3 ij J J a proper subterms of {t.}.. - — c l l We now show that every infinite descending sequence of ground terms has an infinite descending subsequence of proper subterms, all with the same top-level function symbol. Let it.}, be an infinite descending sequence of ground terms. Then there must exist an infinite descending sequence {a.}, of paths such that a. e SPaths(t.) for all b M l l r l l i _> 1. By considering the stepping sequences of the a., and making use of the fact that the function symbol ordering is well-founded, we can show that there is an infinite descending sequence {v.}. of terms such J J ■20- that all v. have the same top-level function symbol and such that for some n ^ 0, for all j ^_ 1, v. occurs on the path a . . By considering elements of PSPaths(v.) for j >_ 1, we can show that {v.}. has an infinite decreasing subsequence of proper subterms, all with the same top-level function symbol. Hence {t.}. has an infinite descending subsequence of proper subterms, all with the same top-level function symbol. Definition : Suppose t is a ground term and f is a function symbol. Then msub f (t) = max{u: u is a proper subterm of t and the top-level function symbol of u is greater than or equal to f}, if some such subterm u exists. By "max" we mean a maximal term in the path of subterms ordering, chosen in some consistent way. Definition : Suppose t is a ground term and f is a function symbol. Then Msub f (t) is max{u: u is a subterm of t and the top-level function symbol of u is f or larger than f}, if some such subterm u exists. Thus Msub f (t) is the same as msub f (t) except that we do not require Msub f (t) to be a proper subterm of t. In fact, if the top-level function symbol of t is f or larger than f, then Msub f (t) = t. Also, if the top-level function symbol of t is smaller than f, then Msub f (t) exists iff msub,_(t) does, and in that case Msub^Ct) = msub c (t). r f r In particular, Msub f (s) exists iff s contains a function symbol larger than or equal to f, and msub f (s) exists iff some proper subterm of s contains a function symbol larger than or equal to f. Definition : Suppose {t.}. is an infinite sequence of ground terms and f is a function symbol. Suppose u. ^ Msub (t.) for all i >_ 1 . -21- Then we call {u.}. a main sequence of subterms of {t.}.. Usually we say {u.}. is a main sequence of {t.}.. Note that all maximal 7 J l l ^ l l elements of SPaths(t.) contain a term equivalent to u. up to a permutation of arguments. For convenience, we say u is equivalent to v if u ^ v. Definition : Suppose {t. }. is an infinite sequence of terms. Then f is a top-level function symbol for {t.}. if f is the top-level function symbol of infinitely many elements of the sequence {t.}.. That is, {i: f is the top-level function symbol of t.} is infinite. Definition : Suppose {t.}. is an infinite descending sequence of ground terms. Then the coarseness of {t.}. is the smallest function G l l symbol g having the following properties: 1. For all function symbols h >_ g, no subsequence {u.}. of proper subterms of {t.}. which has h as a top-level function symbol, is an infinite descending sequence. 2. The set of function symbols h >_ g such that for some subsequence {u.}. of proper subterms of {t.}., the J J i i following are true, is finite: a) {u.}. is an infinite descending sequence. J J b) {u.}. has a main sequence with h as a top-level J J function symbol. We call a function symbol g having properties 1. and 2. a coarseness bound for {t.}.. We will show that if there is an infinite l l descending sequence whose coarseness is g, then there is an infinite descending sequence whose coarseness is less than g. -22- We now show that the coarseness of any infinite descending sequence of ground terms is well-defined. Let {t.}. be an infinite descending sequence of ground terms, and let g. be the maximal function symbol in t.. Then {g.}. is an infinite non-increasing sequence, which must reach a limiting value since the function symbol ordering is a well-founded ordering. Let this limit be g. Let h be the smallest function symbol larger than g, if it exists. Then h is a coarseness bound for {t.}.. Hence there is a smallest coarseness bound for {t.}., 1 1 l i since the symbol ordering is well-founded. This smallest coarseness bound is then the coarseness of {t.}.. If no such h exists (i.e., g 11 ° is the maximal symbol in the set of function symbols) , we can add such an h to the set of symbols in order to define coarseness uniformly. Lemma 1 : Suppose {t.}. is an infinite descending sequence of ground terms. Let h be a function symbol. Let W be any subset of the set of terms w satisfying the following properties: 1. For some subsequence {u.}. of proper subterms of {t.}., J J ii w occurs infinitely often in some main sequence of {u.}.. J J 2. The top-level function symbol of w is h. If W has no minimal element, then there is an infinite descending sequence {v.}. having the following properties: 1. {v.}. is a subsequence of proper subterms of {t.}.. J J y ii 2. The top-level function symbol of v. is h, for all j >^ 1. Proof of lemma : Let v.. be an arbitrary element of W. For j > 1, let v.. n be an element of W smaller than v.. Since W has no smallest element in the path of subterms ordering, such a term v always exists. Then {v.}. is an infinite descending sequence, all of J J whose elements have h as the top-level function symbol. Also, since -23- each v. occurs as a proper subterm of t. for infinitely many i, {v.}. J i J J is a subsequence of proper sub terms of {t.}.. (No v. can occur in the 11 j sequence {t.}. itself, since {t, }. is a descending sequence. Hence the v. must all be proper subterms of t. for infinitely many i.) Lemma 2 : Suppose s.. and s~ are ground terms, and f is a function symbol. Suppose s and s„ have the same top-level function symbol (not necessarily f) . Then a) If msub f (s,) and msub f (s_) exist and s.. >_ s„ in the path of subterms ordering, msub f (s-.) >_ msub f (s„) in the path of subterms ordering. b) If msub f (s 1 ) exists and msub f (s 9 ) does not exist, then s.. > s_ in the path of subterms ordering. c) If msub f (s,) exists, then something equivalent to msub f (s,) occurs on a maximal element of SPaths(s 1 ). Proof : By considering the stepping sequences of maximal elements of PSPaths(s ) and PSPaths(s ), respectively. Also, if s.. and s_ have different top-level function symbols, both smaller than f, and s.. >_ s„, and msub f (s ) and msub f (s 9 ) exist, then msub f (s 1 ) >_ msub f (s 9 ). Definition : Suppose t is a ground term and f is a function symbol. Let msub'(t) be max{u: u is a proper subterm of t and the top-level function symbol of u is larger than f}, if some such subterm u exists. We can show that msub'(t) satisfies all three properties given in lemma 2 for msub f (t), in a similar way. -24- Lemma 3 : Suppose s and s are ground terms, and f is a function symbol. Then a) If Msub f (s..) and Msub f (s_) exist and s.. _> s~ in the path of subterms ordering, then Msub (s..) >_ Msub f (s„) in the path of subterms ordering. b) If Msub f (s,) exists and Msub f (s„) does not exist, then s.. > s„ in the path of subterms ordering. c) If Msub f (s 1 ) exists, then Msub f (s 1 ) or something equivalent to it up to a permutation of arguments, occurs on a maximal element of SPaths(s ). Proof : By considering the stepping sequences of maximal paths of s and s„, respectively. Definition : Suppose t is a ground term and f is a function symbol. Then Msub'(t) is max{u: u is a subterm of t and the top-level function symbol of u is strictly larger than f}, if some such subterm u exists. We can show that Msub'(t) has properties similar to Msub f (t) , by similar arguments. Lemma 4 : Suppose f and g are two function symbols, and f < g. Suppose t is a ground term, and Msub (t) exists. Then Msub (t) ^ Msub (Msub^(t)). g g f Proof : Msub (t) <_ t hence Msub (Msub (t)) £ Msub (t) . However, Msub c (t) > Msub (t) so Msub (Msub-Ct)) > Msub (Msub (t)) = f - g g f g g Msub (t). Thus Msub (t) ^ Msub (Msub (t)). Similarly, Msub (t) ^ Msub (MsubMt)). g f -25- Lemma 5 : Suppose that s and s 9 are ground terms and g is a function symbol. Suppose the top-level function symbols of s and s 9 are both smaller than g. Also, suppose s _> s„ in the path of subterms ordering and that msub (s..) and msub (s„) exist. Let fl be the largest function symbol such that Msub (s..) exists and has a proper subterm t-, such that t ^ msub (s ) . Similarly, let f2 be the largest function symbol such that Msub f (s„) exists and has a proper subterm t» such that t» ^ msub (s„) . Then either msub (s.) > tmsub (s„) or fl > f2. g 1 g 2 - Proof: We know by lemma 2 that msub (s n ) > msub (s_) . g 1 - g 2 Suppose msub (s..) ^ msub (s_) and f, < f_. Then Msub f (s ) ^ msub (s 1 ) g -L g Z -L z rz X g -L and Msub..„(s_) > msub (s_) . Hence Msub.. (s_) > Msub r „(s,) and so f2 2 g 2 f2 2 f2 1 s„ > s 1 , contrary to hypothesis. Lemma 6 : Let T for i >_ 1 represent the sequence t.. , t ? , t„, ... of ground terms. Suppose that T is an infinite descending sequence for all i >_ 1, and that the top-level function symbol of t . is f for all i >_ 1, j >_ 1. Suppose that T ' is a subsequence of proper subterms of T , for all i >_ 1. Then for some i, some t e T , msub'(t) exists and * i X* for some j >_ 1, 6 >_ 1, msub'(t) >^ t . Proof : For all i >_ 1, j >_ 1 there exists I >_ 0, m >^ 1 such that t is a proper subterm of t.,„. Hence msub^Ct.,.) exists. m j+£ f j+J£ Therefore msub c (t.) must also exist. In addition, msub .. (t . . _ ) > t f j ' f j+£ - m Also, msub r (t.) > msub_(t..,) > ... > msub_(t.. n ). Hence msub c (t.) > t f j — f j+1 - - f j+£ f j - m We have shown that for all i >_ 1, for all j >_ 1, msub (t.) exists and for some m > 1, msub.-Ct.) > t — f j — m Let z° . be t. for all i > 1, j > 1. Let z.. be msub,(z..) ij j - ij f ij for all k > 1 such that msub,.(z..) exists and such that the top-level function symbol of msub^Cz..) is f. Let n.. be the maximum I such that £ z is thus defined. Let n be min{n..: i > 1, j > l}. Note that if n > 1, then -26- 11 2 2 z..>z. ..,; if n > 2, then z.. > z. , ,_. In general, if n > k, then ij - i,j+l ij - i,j+l k k Z ij - z i,j+r We showed above that for all i, j there exists m. >_ 1 such that msub,.(t.) > t . Similarly, there exists m~> 1 such that f j - m 1 2 - msub._(t ) > t . Hence if n > 1, msub ,. (msub _(t .) ) > t .In f m — m» — f f j — m- general, if n > k then for all i, j there exists m >_ 1 such that k o ij — i+k,m In particular, choose i and j so that n.. = n; then there exists m > 1 so that z.. > z., . Now, z., = t and — ij — i+n,m i+n,m m msub..(t ) must exist, as we showed earlier. Hence msub^(z..) exists, f m f ij and therefore has top-level function symbol strictly larger than f. Also, for some I > 1, msub,.(t ) > t„ as we showed earlier. Hence — r m — x. , i / n N , , n ,. , , o , i+n+1 „, , , . i N i+n+1 msub.-(z..) = msub^Cz..) > msub r (z.. ) > t n . Thus msub-Ct.) > t„ f ij f ij — f i+n,m — I f j — £ Thsorem 4.11 ; Suppose {t.}. is an infinite descending sequence of ground terms. Suppose the coarseness of {t.}. is g. Then {t.}. a rr 11° 11 has an infinite descending subsequence of proper subterms whose coarseness is less than g. Proof ; Let H be the set of function symbols h such that h 21 g and such that for some infinite descending subsequence {u.}. of proper subterms of { t . } ., the sequence {Msub (u.)}. has h as a top- ii g J J level function symbol. We know that H is finite, by definition of coarseness. For h e H, let w, be a minimal term having h as a top- level function symbol such that w occurs infinitely often in some such main sequence {Msub (u.)}.. We know that w, exists for h e H by g J J h lemma 1. Let w be min{w, : h e H}. We have two cases. h -27- Case 1: Some infinite descending subsequence {u.}. of proper subterms of {t.}. has no main sequence with any element of H as a top- level function symbol. Let f be the largest symbol occurring infinitely often in {u.}.. Then {u.}. has a main sequence with f as a top-level J J J J symbol. We know that f < g since f t H. Case la: Suppose that {u.}. itself or some subsequence 3 3 {v.}. of proper subterms of {u.}. has the following properties: 3 3 3 3 a) {v.}. is an infinite descending sequence. b) {v.}. has no infinite descending subsequence of proper subterms with f as a top-level function symbol. We show that f is a coarseness bound for {v.}.. Since no function J 3 symbol larger than f occurs in infinitely many u., f satisfies the first part of the definition of a coarseness bound. The second part is true for the same reason. Case lb: The sequence {u.}. and every infinite descending ]] subsequence of proper subterms of {u.}. has an infinite descending 3 3 subsequence of proper subterms with f as a top-level function symbol. In this case, we can show that there exist sequences U = {u.}. of 3 3 ground terms with the following properties, for all i >_ 1: a) U is an infinite descending sequence. b) U " is a subsequence of proper subterms of U . c) For all i _> 1, j _> 1, the top-level function symbol of u. is f, and no function symbol larger than f appears i in u. . 3 d) U is a subsequence of proper subterms of {u.}.. -28- By lemma 6, for some i, some u e U , msub'(u) exists. However, this cannot be true, by property c) above. Case 2 : Assume case 1 does not apply. Then some infinite descending subsequence {u.}. of proper subterms of {t.}. has w occurring infinitely often in the sequence {Msub (u.)}.. Since a main sequence g J J must be non-increasing, some suffix of {u.}. has Msub (u) ^ w for all J J O u in the suffix. Assume without loss of generality that w ^ Msub (u . ) g J for all j _> 1. If v is a proper subterm of u., then Msub (v) <_ Msub (u.) J o & J Hence if {v.}. is an infinite descending subsequence of proper subterms of (u.}.j then Msub (v.) ^ w for all j >_ 1. Hence w, w, w ... is a J J o J main sequence of {v.}. also. J J Let f be the smallest function symbol such that Msub'(u..) ^ Msub (u.. ) . It is easy to show that f occurs in u and that f < g. Also, {Msub'(u.)}. is an infinite non-increasing sequence, hence Msub'(u.) ^ Msub (u.) ^ w for all j > 1. Similarly, if {v.}. f J gJ - ] ] is an infinite descending subsequence of proper subterms of {u.}., J J then Msub' (v.) ^ Msub (v.) ^ w for all j > 1. We have two subcases f J g J - to consider: Case 2a: The sequence {u.}. itself or some subsequence J J {v.}. of proper subterms of {u.}. has the following properties: a) {v.}. is an infinite descending sequence. J J b) No infinite descending subsequence of proper subterms of {v.}. has f as a top-level function symbol. J J In this case, it is easy to show that f is a coarseness bound for {v.}.. J J Hence the coarseness of {v.}. is strictly less than that of {t.}.. J J .1 J -29- Case 2b: The sequence {u.}. itself and every infinite J J descending subsequence of proper subterms of {u.}. has an infinite J J descending subsequence of proper subterms with f as a top-level function symbol. In this case, we can show that there exist sequences U = {u.} of ground terms with the following properties for all i >_ 1: a) U is an infinite descending sequence. b) U is a subsequence of proper subterms of U . c) For all i >^ 1, j _> 1, the top-level function symbol of u. is f. J d) U is a subsequence of proper subterms of {u.}.. J J By lemma 6, for some i >_ 1 , for some u e U , msub'(u) exists and for some j _> 1, 6 >_ 1, msub'(u) >_ u . . However, msub'(u) 'v- Ilsub' (u) ^ w so w >_ u. . Also, Msub'(u. ) ^ w and w < u. . This is because u. has f as a top-level function symbol, which implies that ., , , , i+6. . , ,. i+6 TT i+6 , i+6 Msub'(u. ) is a proper subterm of u. . Hence w >_ u. and w < u. , contradiction. Thus case 2b cannot be true. This completes the proof. Corollary 1 : The path of subterms ordering on ground terms is well-founded. Proof : If not, an infinite descending sequence of ground terms would exist. Then we could construct an infinite descending sequence of coarsenesses, using the theorem. However, this is impossible since the function symbol ordering is a well-founded ordering. Corollary 2 : The path of subterms ordering on arbitrary terms is a well-ordering. 30- 5. Computing the Ordering on Ground Terms We now give a reasonably efficient procedure for computing the path of subterms ordering on ground terms. In fact, the procedure will sort a set of ground terms in non-decreasing order in the path of subterms ordering. The procedure will also determine which of the terms are equivalent to within a permutation of arguments. Later we present methods for computing the ordering on non-ground terms. The algorithm makes use of ideas and notation from [ 9 ] . Theorem 5 •! » Suppose s and t are ground terms of the form f(s-, ..., s ) and f(t,, ..., t ), respectively. Suppose a e PSPaths(s) and 6 e PSPaths(t) and a and 8 are identical to within a permutation of arguments. Then there exists i, j, 1 <. i <- n, 1 .£. j <_ n such that s. and t. are identical to within a permutation of arguments. Proof: Choose i, i such that s. is the first term in the J l path a and t. is the first term in the path 6. Since a and 6 are j identical to within a permutation of arguments, so are s. and t . Co rollary: Let a. be the maximal element of SPaths(s.) "^ l i for 1 .£. i .£. n and let 6. be the maximal element of SPaths(t.) for l l 1 i i < n, Let A be the multiset {a_ , a„, . . . , a } and let B be the 12 n multiset {B, , 8 , ..., 8 }. Let {y, , y , •■•> Y ) be a listing of A 12 n 12 n in non- increasing order and let {6 , 6 , ..., 6 } be a listing of B in non-increasing order. Assume that s, t are not identical to within a permutation of arguments. Then s < t in the intermediate path of subterms ordering iff y. < <"> . where i = min{i: y., 6. are not identical to within a permutation of arguments}. -31- n Proof : PSPaths(s) = .W SPaths(s ± ) and PSPaths(t) = n .W SPaths(t.). Let {u, , .... u } be a listing of {s 1 , ..., s } i=l 1 In In such that Y- = max(SPaths (u. ) ) and let {v.. , ..., v } be a listing of {t , ..., t } such that 6. = max(SPaths(v. ) ) . Then u. and v. are identical to within a permutation of arguments, for all i < j. Hence the ordering of PSPaths(s) and PSPaths(t) is the same as the n n ordering of W. SPaths(u.) and .^. SPaths(v.). But y, is a maximal i=J i i-j i J n " element of .^. SPaths(u.) and 6 . is a maximal element of ,W . SPaths(v.). i=J i J i-j i Also, we know that y. and 6. are not identical to within a permutation J J n n of arguments. Hence .¥. SPaths(u.) < .W. SPaths(v.) iff y. < <5 . . Hence s < t in the intermediate path of subterms ordering iff y . < 6.. The following procedure will output a multiset (t.., ..., t } of ground terms in non-decreasing order in the path of subterms ordering. It actually outputs all subterms of t., 1 <. i <_ p, along with the terms t. themselves, in non-decreasing order. For purposes of this algorithm, we consider different occurrences of the same subterm to be different subterms. We assume as usual that the function and constant symbols of the terms t. are identified with the integers {l, 2, 3, ..., k} The symbol ordering is then the usual arithmetic ordering on integers. Thus k is the largest function symbol and 1 is the smallest. We say a term t is eligible if all proper subterms of t have been output already but t itself has not been output. We make use of queues Q 1 , Q 9 , ..., Q, ith queue operations indicated as usual. Thus t *= Q means remove w an element from the front of Q. and assign it to t. Also, Q . ^ t means add t to the back of queue Q.. By "Delete S from Q " we mean to remove elements of S from Q , wherever they occur in Q (not necessarily at the front). The meaning of "Delete T from Q " is similar. -32- The running time of this algorithm is 0(L(logD + logk)) where P L i- s j--i Size(t.) and D is the maximum number of occurrences of any subterm in {t n , t„, ..., t }, including constants. Note that D <. L. 12 P The factor logD appears because of the necessity to sort the list T. Perhaps the algorithm can be restructured to avoid this factor of logD. This analysis assumes that all subterms can be output in unit time. The whole subterm does not actually need to be written out, but only enough information to identify it. Hence this assumption seems to be realistic. To make this sorting possible, we keep integer indices with each subterm telling its position in the queue relative to other subterms. procedure sortt2({t 1 , t„, ..., t }); for i=ltokdoQ. + A; for i = 1 to_ p dj3 for all subterms t of t. do l — (f ■*■ top-level function symbol of t; Q f *■ t); while (a.)(Q. contains an eligible term) do_ (i ■*- min{ j : Q. contains an eligible term}; II use a priority queue to find ij S «- the multiset of elements of Q. all of which are identical to the first eligible term in Q , up to a permutation of arguments ; Delete S from Q ; for j = 1 _to_ k dj3 if S ± then repeat [T •*■ list of the multiset of elements of Q. having at least one element of S as a top-level subterm. Also, T is sorted in the same order as the elements were in Q.; Delete T from Q. and append it to the end of Q.; J for all t e T(let s e S be some top-level subterm of t. Delete s from S and output s) ] until T = A) ; end sortt2; -33- The reason that this algorithm works is the following: Let Q 1 Q„...Q, be a concatenation of the queues, listed from front to back. At the start of the "while" statement, the following is always true: 1. If u and v are both eligible and u occurs before v in the list Q..CL...Q, , then the maximal path of subterms of u is less than that of v, or else u and v are identical up to a permutation of arguments. 2. Suppose u and v are arbitrary terms in Q. and u occurs before v in Q.. Let {u,, •••, u } be the multiset of J 1 m top-level subterms of u that have already been output, and let {v. , . . . , v } be the multiset of top-level sub- 1 n v terms of v that have already been output. Then {u. , . . . , u } < {v n , . . . , v } in the multiset ordering 1 m — 1 n ° induced by the path of subterms ordering on terms. 3. Eligible terms occur in non-decreasing order in the path of subterms ordering in the list Q-Q-...Q, . It is not difficult to show that 1. implies 3. By 3., we know that terms will be output in the desired order. Byl., we know that the procedure sortt2 simulates the path ordering algorithm "sortb" of [ 9 ] that scans paths from back to front. Thus we know that terms will be output in non-decreasing order according to the ordering on their maximal paths of subterms. By 2., we know that whenever a term becomes eligible, it will be in the proper place in the list Q Q . ..Q . Also, it is not difficult to see that 2. is preserved by the operations within the "repeat" statement. We show this as follows: -34- Suppose u occurs in Q. and has an element s of S as a top-level subterm. Then, since s is larger in the path of subterms ordering than any term output so far, the multiset {s, u , ..., u } will be larger than any multiset of a term in Q. not having an element of S as a top- level subterm. Also, if u and v both have s as a top-level subterm, then the ordering on {s, u. , ..., u } and {s, v, , . . . , v } is the same as & 1 m 1 n the ordering on {u. , ..., u } and {v n , ..., v }. Hence deleting T ° 1 m In b from Q., appending it (appropriately ordered) to the end of Q. and outputing an appropriate sub-multiset of S preserves condition 2 for Q.. We may have to repeat more than once for Q. if some term has more than one element of S as a top-level subterm. Note that non-eligible terns nay appear before eligible terns in Q. . For exanple, if no top-level subterms of u have been output, then u may appear first in Q.. We have not specified how to determine if two terms are identical up to a permutation of arguments. We can easily modify the above algorithm to keep track of which subterms are identical to within a permutation of arguments. To do this, we keep a bit with each term v as follows: Suppose v e Q. and {v,, ..., v } is the multiset of top- j 1 n level subterms of v output so far. Suppose u e Q. and u occurs immediately before v in the queue. Let {u.,, ..., u } be the multiset J ^ 1 m of top-level subterms of u that have been output so far. Then the bit for v is 1 if {v 1 , ..., v } is identical to {u. , ..., u } up to a In 1 m permutation of arguments, and the bit is otherwise. That is, the bit is 1 if m = n and u. ^ v. for 1 <. i <. m, and otherwise, assur.ing i l that in , ..., u ) and (v,, ..., v } are both lister! in non-increasing 1 m 1 n -35- order. It is easy to update these bits when top-level subterms of u or v are output, making use of the fact that all elements of S are identical up to a permutation of arguments. In particular, {v , ..., v } ^ {u, , ..., u } unless at some time so far, T has included one of u and 1 m v but not the other. Also, if u and v are both eligible, and u occurs just before v in a queue, then u ^ v iff the bit for v is 1. Hence the set S can easily be obtained using these bits. Finally, two subterms of terms in {t , t„, ..., t } are equivalent up to a permu- tation of arguments iff they are output in the same set S. Thus we can decide if two terms are identical up to a permutation of arguments in time 0(L(logD + logk)), the same time complexity as the algorithm to sort terms in the path of subterms ordering. 6. Computing the Ordering on Non-ground Terms We now give algorithms to compute the path of subterms ordering on terms with variables in them. We do not yet have a completely general procedure but we present methods that will work on many replacements occurring in practice, and give ideas for extending these methods. Our methods always terminate and if they yield an answer it is correct. However, sometimes the algorithm returns "don't know." Suppose a and 3 are Paths of subterms and mtf(a) < mtf(3) in the symbol ordering. Then a < 3 in the path ordering, since the maximal term in a will be less than the maximal term in 3 in the inter- mediate path of subterms ordering. Definition : If a is a path of subterms and f is a function or constant symbol, let # f (a) be the number of terms in a whose top-level function symbol is f. ■36- Theorem 6.1 . Suppose a and 3 are maximal paths of subterms from ground terms s and t, respectively. Suppose that mtf(a) = mtf(3) and # f (a) < # f (3) where f = mtf(a). Then a < 3 in the path ordering. Proof : Let f be mtf(a). Now, a and 3 will be ordered by the intermediate path of subterms ordering on mt(a) and mt(3), if mt(a) and mt(3) are not identical to within a permutation of arguments. Let u and v be mt(a) and mt(3), respectively. (Perhaps u is s or v is t.) Let a' be max(PSPaths(u) ) and let 3' be max(PSPaths (v) ) . Note that a' and 3' are suffixes of a and 3, respectively. Also, # (a ' ) = // f (a) - 1 and # f (3') = # f (3) - 1. Hence # f (a') + # f (B') so a' and 3' are not identical to within a permutation of arguments. Hence u and v are not identical to within a permutation of arguments. Also, u < v in the inter- mediate path of subterms ordering iff a' < 3' in the path ordering. If //.(a 1 ) = then a' < 3' in the path ordering since mtf(a') < mtf(B'). If # f (a*) ± then // f (a') < # f (3') and so we can apply the theorem inductively to show that a' < 3' in the path ordering. Hence u < v in the intermediate path of subterms ordering, and so a < 3 in the path ordering, as desired. Theorem 6.2. Suppose s and t are two ground terms and a and 3 are maximal paths of subterms from s and t, respectively. Let Sj, S2, ..., s be the maximal descending subsequence of a and let t,, t„, ••., t be the maximal descending subsequence of 3. Let f^ be the top-level function symbol of s. for 1 <^ i <_ m and let g, be the top-level function symbol of t. for 1 <. i < n. Then a < 3 1 in the path ordering if the sequence f f ...f is less than the sequence 12m 8 l g 2"*' g n ln the lexic °g ra P hic ordering. -37- This theorem can be extended to give a general method of comparing maximal paths, as follows: Definition : Suppose that s and t are two ground terms and a and 3 are arbitrary paths of subterms from s and t, respectively. Then the surface ordering on a and 3 is defined as follows: Let Y-i an d Yo De the longest possible suffixes of a and 3, respectively, such that y. ^ y 9 - Let a be a'y-, and let 3 be 3'Yo' Let s_~, s_, ..., s be the maximal descending sequence of a' and let t , t„, ..., t be the maximal descending subsequence of 3'. Let f . be the top-level function symbol of s. for 1 <_ i <_ m and let g. be the top-level function symbol of t. for 1 <. i <_ n. Then a < 3 in the surface ordering iff a) the sequence f f_...f is less than the sequence g..g_...g in the lexicographic ordering on sequences of function symbols, or b) m = n and f . is identical to g. for 1 <. i <_ m, and s^ < t^ in the path of subterms ordering on terms. (Note that s and t are not the same to within a permutation of arguments m m because s occurs immediately before y. in a and t occurs immediately m J 1 m before y„ in 3. Hence either s < t or s > t in the path of subterms '2 m m m m ordering on terms.) Theorem 6.3. Suppose that s and s„ are two ground terms and a and a are maximal paths of subterms from s^ and s_, respectively. (By "maximal" we mean maximal in the subsequence ordering.) Suppose a > a in the subsequence ordering. Then a.. > a„ in the surface ordering, ■38- Proof : Let a be a 'y-i and let a be o^'Yo wnere Y i % Y 2 and Y-, and y 2 are the longest suffixes of o^ and a 2 with this property. Assume inductively that the theorem is true for all paths of subterms 6 , 6. as in the theorem for which 6 < a. and 6_ < a„ in the subsequence ordering. Let a ' be 3-,v 3 ' where v, = mt(a ') and let a ' be 3 ? v 2 3 ' where v~ = mt(a '). We cannot have v ^ v„ by definition of y and y 9 - Hence v > v ? in the intermediate path of subterms ordering. Let f, and f„ be the top-level function symbols of v.. and v , respectively. If f.. and f„ are not identical, then f > f„. It follows that a > a„ in the surface ordering. Suppose that f 1 and f_ are identical. Since Y-, and Yo are the longest suffixes of a and a» such that Y-i ^ Yo> we cannot have 3 ' ^ 3 2 ' unless 3 1 ' and 3' are both empty. If 3 ' and 3' are empty, the surface ordering on a and a„ is the path of subterms ordering on v and v . Since v.. > v in the path of subterms ordering, a^ > a^ in the surface ordering. Suppose 3 ' and 3 ? ' are not both empty. Then we cannot have 3 ' ^ B ' . Hence we cannot have 3 1 'y ;l * ^ 2 ' y 2' N ° W " B l' Y l ls 3 Tnaxi:ial element of PSPaths(v ) and B 2 'y 2 is a maximal element of PSPaths(v 2 ) in the subsequence ordering. Since v > v in the path of subterms ordering, 3,'y, > B 9 'y 9 in the subsequence ordering. We can assume by induction, then, that B'y, > & 2 ' Y 2 in the surface ordering. Hence a > a in the surface ordering. This completes the proof. Diagram: -39- f s. r s. / a i > «; M y -/ Theorem 6.4 . Suppose that a is a maximal path of sub terms of a ground term s and a~ is a non-maximal path of subterms of s. (By "maximal" we mean maximal in the subsequence ordering on paths of subterms.) Then a > a_ in the surface ordering. Proof : Let a be a. 'y, and let a be a 9 'y 9 where Y-, % Y 9 and Y-i and y 9 are the longest suffixes of a, and a_ with this property, Assume inductively that the theorem is true for all paths of subterms 6 , 6„ as in the theorem for which 6 1 < a 1 and 6„ < a„ in the subsequence ordering. Let a ' be 3 v 3, ' where v = mt(a') and let a' be B^v-ft' where v_ = mt(a'). Let f, and f„ be the top-level function symbols of v and v„, respectively. If f- and f_ are not identical, then f, > f 9 because v >. v„ in the intermediate path of subterms ordering. It follows that a > a in the surface ordering. -40- Suppose f and f„ are identical. We cannot have 3-. 'Y-i ^ 3 9 'y 9 unless 3,' and 3 9 ' are both empty, by choice of y-i an d Yo« If 3-, ' and 3 9 ' are both empty, then we cannot have v. ^ v 9 , by similar reasoning. Suppose 3,' and 3 9 ' are both empty. Then the surface ordering on a., and a„ is the same as the path of subterms ordering on v.. and v„ . Since ~Kv.. ^ v„) and since a > a„ , we must have v.. > v 9 in the path of subterms ordering. Hence a > a_ in the surface ordering on paths of subterms. Suppose 3,' and 3 9 are not both empty. Then we cannot have 3, 'Y-i ^ 3 9 *Y 9 - We know that v, >. v_ in the intermediate path of subterms ordering. Also, 3, ' Y-, is a maximal element of PSPaths(v 1 ). Let 3 be a maximal element of PSPaths(v 2 ); thus 3 ] _ , Y 1 ^ 3 in the subsequence ordering. Also, 3'y 2 — 3 in the subsequence ordering. If 3 'y-. % 3 then 3 9 'y 9 < 3 and we can apply the theorem inductively to obtain that 3 9 'y 2 < ^i'^i in tne surface ordering. Hence ot^ > a,, in the surface ordering. If 3, 'y-i > 3 then we know that 6-,'y-. > 3 in the surface ordering since 3-, 'Y-. and 3 are maximal paths of v and v„ , respectively. Also, applying the theorem inductively to 3 and 32^2 we ODta i n tnat 3 >- 3 9 'y ? in the surface ordering. Hence B-^Y-, > 3 2 'y 2 in tne surface ordering and a > a in the surface ordering. This completes the proof. -41- Diagram: \ P y Theorem 6.5. Suppose s and t are ground terms. Then s > t in the path of subterms ordering iff SPaths(s) > SPaths(t) in the multiset ordering induced by the surface ordering on paths of subterms. Proof : Suppose s and t have different top-level function symbols. Let a be the maximal element of SPaths(s) in the subsequence ordering, and let B be the maximal element of SPaths (t) in the subsequence ordering. Then a > B in the subsequence ordering. Now, a and B are also maximal elements of SPaths (s) and SPaths (t) in the surface ordering, by Theorem 6.4. Also, a > B in the surface ordering by Theorem 6.3. Hence SPaths(s) > SPaths(t) in the multiset ordering induced by the surface ordering on paths of subterms. -42- Suppose s and t have the same top-level function symbol f. Let s be of the form f(s_., ..., s ) and let t be of the form 1 n f(t l5 ..., t ). Let S be the multiset {s 1( ..., s } and let T be the In in multiset {t, , ..., t }. Let SI and Tl be maximal sub-multisets of 1 n S and T satisfying the following condition.* There is a 1-1 correspondence between elements of SI and elements of Tl such that if a e SI, 3 £ Tl and a corresponds to 3 then a ^ 3. Since s and t have the same top-level function symbol, we know that PSPaths(s) > PSPaths(t) in the multiset ordering induced by the subsequence ordering on paths of subterms. (That is, s > t in the intermediate path of subterms ordering.) It follows that ^{SPaths(s.):s. e S-Sl} > ^{SPaths(t . ) :t . e T-Tl}. Also, the maximal 11 11 ' elements of these multisets of paths differ, and so by reasoning as before we get that ^{SPaths(s . ) :s . e S-Sl} > ^{SPaths (t . ) : t . e T-Tl} in the 11 11 multiset ordering induced by the surface ordering on paths of subterms. Hence, by choice of SI and Tl, PSPaths(s) > PSPaths(t) in the multiset ordering induced by the surface ordering on paths of subterms. Since s r in the intermediate path of subterms ordering, SPaths(s) > SPaths(t) in the multiset ordering induced by the surface ordering on paths of subterms. This completes the proof. This result implies that it does not matter whether we use the surface ordering or the subsequence ordering on naths of subterms, when computing the path of subterms ordering on ground terms. The surface ordering is often easier to work with, especially when dealing with non-ground terms, so we sometimes use it instead of the subsequence ordering on paths. -43- Def inition : Suppose that s and t are two not necessarily ground terms and a and $ are paths of subterms from s and t, respectively. Suppose that a', g', Yi , Y 9 , s , ..., s , t.. , ..., t , f , ..., f , g , ..., g - 1 - z l ml ni ml r are as in the definition of the surface ordering for paths from ground terms. Then a < 3 in the surface ordering iff a) the sequence f f ...f is less than the sequence g g ...g in the lexicographic ordering, or b) the sequence f f ...f is identical to the sequence g n g_...g , and s < t in the path of subterms ordering. °1°2 & n' m m r b The only difference from the previous definition is that s m and t need not be ground terms. Note that if a < g in the surface m ° ordering as defined here, then for all substitutions 6, a9 < B8 in the surface ordering. Suppose we are given terms t.. and t~, possibly containing variables, and we want to decide if t 1 < t« in the path of subterms ordering. Given paths a. and ou we write R_ (a.. , a_) to denote that a < a in the surface ordering. We write R (a , a„) to denote the following relation: Let Yj and y 2 be maximal suffixes of a and a , respectively, such that y x % Y 2 - L et a ± be a 1 ' Y;L and let a 2 be a 2 'Y 2 . Let f , f , ..., f m be top-level function symbols obtained from a ' as in the definition of the surface ordering, and let g , g , ..., g be top-level function symbols obtained from a ' in the same way. We say that R (a , a ) is true iff the sequence f f ...f is less than the sequence g,g^...g 1 i. m 12 n in the lexicographic ordering. This completes the definition of R. . ± Note that R, (a , a^) implies R (o •••, f, } • Then u < t in the path of subterms ordering. Example: Let t be x*(y+z) and let t.. be x*y, t„ be x*z, and f be "+". Assume "+" < "*" in the symbol ordering. We have t < t and t < t in the path of subterms ordering. Let u be (x*z) + (y*z). By the above theorem, u < t in the path of subterms ordering. Thus x*(y+z) -> x*z + y*z is a simplification in the path of subterms ordering. 8. Examples We now give some examples of systems of rewrite rules whose termination can be proven using the path of subterms ordering. That is, all of the replacements are simplifications in the path of subterms ordering, when the function symbol ordering is as specified. Example 1 : Let the c unction symbol ordering be "**" > "*" > binary "-'" > unary "-" > "+" > 1 > and assume 2 is represented as 1+1, 3 as (1 + 1)+1 etc. Here "**" represents exponentiation. -46- x + ■* x x - y ->■ x + (-y) -(-(x)) -> x x + (-x) -> (-x) + x -> -(x + y) -> (-x) + (-y) x * 1 -> X 1 * x -> X x*(-y) ■+ -(x*y) (-x) * y ■* -(x*y) x*(y + z) -*■ x*y + x*z (y+z)*x -*■ y*x + z*x x**l -*■ x x**(y+l) ■> x*(x**y) (x*y)**z -*■ (x**z)*(y**z) x**(y+z) ->■ (x**y)*(x**z) (x+y)**2 ■+ x**2 + 2*x*y + y**2 (x+y)**3 ■* x**3 + 3*(x**2)*y + 3*x*(y**2) + (y**3) et cetera We could also allow the conditional replacement if x 4- then x**0 -*■ 1, since x**0 ■* 1 is a simplification. However, we are not able to include the following replacements: (x**y)**z •*■ x**(y*z) x+(y+ z ) -*■ (x+y)+z x*(y*z) -*■ (x*y)*z Example 2 : For this example, it suffices to choose any ordering in which "D" is the largest function symbol. This example is intended to illustrate some of the operations of symbolic differentiation. D(c) ■* D(c*x) -> c*D(x) D(x+y) -»■ D(x) + D(y) D(x*y) ■*■ x*D(y) + y*D(x) D(x/y) -> (y*D(x) - x*D(y)) / (y**2) D(x**n) ■»■ n*(x**(n-l)*D(x) -47- In fact, these replacements, together with those of Example 1, can be proven to terminate by choosing the ordering of Example 1 with "D" added as more complex than any other symbol. Example 3 : This example illustrates definitions of primitive recursive functions, a topic to which we shall return later. Assume "fact" > "*" > "+" > "s" > "0". fact(s(x)) ■*- s(x)*fact(x) fact(0) + s(0) s(x)*y ■+ y+(x*y) 0*y ■> s (x)+y -*■ s (x+y) 0+y -> y Example 4 : This example illustrates an application of the path of subterms ordering to LISP functions. Here pairs ( (x, , ..., x ), (y , . . . , y )) is the multiset {cons (x., y.) : 1 £ i < m, 1 <. j ^- n} represented as a list. Order function symbols by "pairs" > "pairl" > "append" > "cons" > "NIL". pairs (cons (x, y) , z) -> append (pairl (x, z) , pairs (y, z)) pairs (NIL, z) -> NIL pairl (x, cons(y, z)) -> cons(cons(x, y) , pairl(x, z)) pairl (x, NIL) ■* NIL pairs (append (x, y) , z) ■* append (pairs (x, z) , pairs(y, z)) pairs(x, append (y, z)) ■> append (pairs (x, y) , pairs(x, z)) append (NIL, z) -*■ z append (cons (x, y) , z) -+ cons(x, append (y, z)) Example 5 : We show the termination of a set of rewrite rules for converting a Boolean formula to disjunctive normal form. Assume "=" > "3" > "1" > "a" > "v" > (all propositional variables) . X = y ->■ (x A y) V (~lx A ~ly) X 3 y -> (~lx) V y H(x A y) -> Ox) V Oy) ~l(x V y) -v (Hx) A Oy) nix ■> x X a (y V Z ) -> (x A y) V (x A z) -48- Th e idea given in Example 3 of defining a primitive recursive function by a set of simplifications in the path of subterms ordering works in general. We now discuss relations between the path of subterms ordering and the class of primitive recursive functions. Definition : Suppose T is a set of rewrite rules. Let It- be the relation on terms defined by R^s, t) iff a) t can be obtained from s by a finite sequence of replacements using the rewrite rules in T and b) no other terms can be obtained from t by using the rewrite rules in T. Suppose that if R T (s, t.. ) and R T (s, t~) are both true, and s is a ground term, then t 1 and t„ are identical. In this case, we call the mapping from ground term s into term t such that R T (s, t) , the function computed by T . Theorem 8 : Suppose we represent non-negative integer n as s (0). Thus 1 is represented as s(0), 2 as s(s(0)), et cetera. Then for every primitive recursive function h, there is a set T of rewrite rules, all of which are simplifications in the path of subterms ordering, such that h is the function computed by T. That is, h(a , ..., a.) = b X K. a i a V K iff R T ("h(s X (0), ..., s *(0)) M , "s b (0)") is true. Proof : By definition of primitive recursive functions and properties of the path of subterms ordering. We order the primitive recursive function symbols in the order that they are defined. Thus functions defined last are considered more complex in the symbol ordering, Note that if g < f in the symbol ordering, then the replacement f(x , ..., x , s(y)) ■*- g(x , ..., x , y, f (x , ..., x , y) ) is a J- n i n l n simplification in the path of subterms ordering by the preceding characterization. -49- We conjecture that if T is a set of rewrite rules all of which are simplifications in the path of subterms ordering, and if T computes some function h, then h is primitive recursive. If true, this would give us a new characterization of the primitive recursive functions. We could try to prove this by bounding the number of replacements that can be done on a starting term s, using a bound that is primitive recursive in s. A consequence of this conjecture would be that the path of subterms ordering could never be sufficient to prove termination of rewrite rules for computing a non-primitive recursive function. However, since non-primitive recursive functions cannot be computed in polynomial (or exponential) time, this does not seem to be much of a limitation in practice. 9 . More Results The following results may be of some interest in devising better ways to compute the path of subterms ordering for terms with variables in them. Theorem 9 • Suppose s and t are two not necessarily ground terms, such that there exist substitutions 9 and 6 such that s6 and t9 are identical to within a permutation of arguments but s8„ < t6 in the path of subterms ordering. Also, suppose that for no substi- tution do we have s6 > t6 in the path of subterms ordering. Then either a) s is the minimal constant symbol and t is a variable or b) s is of form f(s 1} ..., s ) and t is of form f (t, , ..., t ) In In for some function symbol f, and there exist i, j with 1 <. i, j < n such that s. and t. also satisfy the hypotheses of the theorem. -50- Proof : If s is a variable, then this variable must occur in t or else we could find 9 such that s6 > t0. However, if this variable occurs in t then either s and t are identical, in which case 0„ cannot exist, or else s is a proper subterm of t, in which case 6 cannot exist. Hence s is not a variable. If t is a variable, then s must be the minimal constant symbol or else we could find 6 such that s8 > t0 (replace t by the minimal constant symbol) . Otherwise, neither s nor t is a variable. Since sG.. and t6 1 are identical, both s and t must have the same top-level function symbol. Suppose s is of the form f (s 1 , ..., s ) and t is of the form f(t,, ..., t ). Assume without loss of generality that s6„ and t0~ are ground terms. Let u., . . .u and v,...v be listings of the multisets In In {s n 9_, ..., s 6_} and {t,6_, ..., t O } respectively, in non- increasing 1 z n Z ± Z n z order. Let k be min{£: u and v are not identical to within a permutation of arguments}. Then u < v, in the path of subterms ordering, Let i be such that t.0„ is v, . Let i be such that s.6 and t.0, J j 2 k lljl are identical to within a permutation of arguments. If s G„ < t 8^ then we have i and j as desired. If not, then delete s.0 9 and t.0„ from the lists u, ...u and v n ...v , respectively, and repeat j 2 In 1 n r ■" r this argument. Eventually i and j as desired will be obtained. This can be done because of the following facts: -51- a) The multiset {u. , ..., u } is less than the multiset 1 n {v^, ..., v } in the ordering induced by the path of subterms ordering on terms. b) If s.9 >. t.8 , then the multiset {u 1( ..., u } - {sjj 1 z j z In i 2 is still less than the multiset {v, , .... v } - {t 6„} 1 n j 2 in the ordering on multisets of terms. Corollary 1: Suppose that s, t, and 8- are as in the above theorem. Then 8 must replace at least one variable of t by the minimal constant symbol. Proof : If a) of the above theorem is true, this is immediate, If b) is true, we can use a simple inductive argument. Corollary 2 : Suppose s and t are not necessarily ground terms, neither of which contain the minimal constant symbol. Suppose that there exists substitution 8 such that s8 and t8 1 are identical to within a permutation of arguments. Then either s and t are identical to within a permutation of arguments, or there exist substitutions 8 and 8 such that s8 < t8 and s8„ > t8,. in the path of subterms ordering. It may simplify the computation of the path of subterms ordering on non-ground terms to assume that there is a minimal constant symbol that never actually appears in any of the terms. This assumption causes us to fail to recognize some replacements as simplifications, however. For example, the replacement f(g(x),d) ■> f(g(c),c) will not be recognized as a simplification if c is the minimal constant symbol actually appearing in terms. -52- 10. Conclusions We have exhibited a new partial ordering on terns and have shown that it is a simplification ordering. Also, we have given efficient algorithms for computing this ordering on ground terms. The algorithms for non-ground terms are not so efficient and are not completely general. In fact, we do not have any algorithm that will compute the ordering on non-ground terms in all cases. However, we do have a characterization of a class of simplifications including many that involve non-ground terms. This characterization appears to apply to commonly occurring sets of rewrite rules. We have given several examples to illustrate the wide applicability of this "path of subterms" ordering for proving termination of systems of rewrite rules. These examples involve algebraic simplification, symbolic differentiation, number theoretic functions, LISP functions, and Boolean formulae. In fact, we have shown that any primitive recursive function can be computed by a system of rewrite rules for which termination can be proven using the path of subterms ordering. We conjecture that a function is primitive recur- sive iff it can be computed by a system of rewrite rules for which termination can be proven using the path of subterms ordering. It would be interesting to see if there is a simple description of a class of systems of rewrite rules such that a function can be computed in poly- nomial time iff it can be computed by a system of rewrite rules in the class, Perhaps this can be done for other time or space bounds on computations. In future work, we hope to give a general method for combining this ordering with various specialized orderings to get a proof technique of even greater applicability. -53- References [1] Dershowitz, N. and Manna, Zohar, Proving termination with multiset orderings, Stanford Artificial Intelligence Laboratory Memo AIM-310 (1978). [2] Huet, Gerard, Confluent reductions: abstract properties and applications to term rewriting systems. Proceedings of the 18th Annual Symposium on Foundations of Computer Science (1977) . [3] Huet, Gerard and Lankford, Dallas, On the uniform halting problem for term rewriting systems. Report No. 283, IRIA (March 1978). [4] Iturriaga, R. , Contributions to mechanical mathematics. Ph.D. Thesis, Carnegie-Mellon University (1967). [5] Knuth, D. E., and Bendix, P. B. , Simple word problems in universal algebras. Computational Problems in Abstract Algebra, Leech, Ed., Pergamon Press (1970), pp. 263-297. [6] Lankford, Dallas S., Canonical algebraic simplification in compu- tational logic. Report No. ATP-25, Southwestern University, Department of Mathematics, Georgetown, Texas (May 1975). [7] Lipton, R. J., and Snyder, L. , On the halting of tree replacement systems. Proceedings of a Conference on Theoretical Computer Science, University of Waterloo, Canada (1977). [8] Manna, Z. , and Ness, S., On the termination of Markov algorithms. Proceedings of the Third Hawaii International Conference on System Sciences (1970). [9] Plaisted, D. , Well-founded orderings for proving termination of systems of rewrite rules. University of Illinois Report No. UIUCDCS-R-78-932 (1978). BIBLIOGRAPHIC DATA SHEET 1. Report No. UIUCDCS-R-78-943 3. Recipient's Accession No. 4. Title and Subtitle "A Recursively Defined Ordering for Proving Termination of Term Rewriting Systems" 5. Report Date September 1978 7. Author(s) David A. Plaisted 8. Performing Organization Rept. No. 9. Performing Organization Name and Address Department of Computer Science University of Illinois Urbana, Illinois 61801 10. Project/Task/Work Unit No. 11. Contract/Grant No. NSF MCS 77-22830 12. Sponsoring Organization Name and Address National Science Foundation Washington, D.C. 13. Type of Report & Period Covered 14. 15. Supplementary Notes 16. Abstracts A new or dering on terms is defined and its properties are presented. This partial ordering is well-founded and appears to be useful for proving termination of commonly occurring systems of rewrite rules. It also appears useful for general algebraic simplification routines, and for mechanical theorem proving programs dealing with equality. In previous work, the author presented a similar ordering which could not prove termination of systems of rewrite rules containing the distributive law x * (y+z) -* x*y + x*z. This new ordering, called the "path of subterms" ordering, can handle the distributive law and many similar replacements, together with replacements like those the former ordering could handle. The path of subterms ordering is syntactically defined and requires a total ordering on function and constant symbols to be specified. An nlogn algorithm for computing the ordering on ground terms is given. Less efficient algorithms for computing the ordering on a subclass of the pairs of non-ground terms are also given. A characterization of a fairly general class of replacements which the path of subterms of rewrite rules for which termination can be proven using this ordering | are given. Also, this ordering has an interesting relationship to the class of primitive recursive functions. 17. Key Words and Document Analysis. proof of termination, total correctness, well-founded sets, rewrite rules, term rewriting systems, tree replacement systems, equational systems, reductions, mathematical induction, algebraic simplification, theorem proving, recursive programs, recursive schemata, partial orderings, primitive recursive functions. 17b. Identifiers /Open-Ended Terms 17c. COSATI Field/Croup 18. Availability Statement FORM NTIS-38 110-70) 19. Security Class (This Report) UNCLASSIFIED 20. Security Class (This Page UNCLASSIFIED 21. No. of Pages 22. USCOMM-DC 40329-P7I ;m v ■ WiJ i IMS