Termination of logic programs via labelled term ... - Semantic Scholar

2 downloads 0 Views 202KB Size Report
append2(v; cons(h; u); l); append1(v; u; w); perm(w; t). 2.2. Term rewrite systems ..... KKS91] M.R.K. Krishna Rao, Deepak Kapur, and R.K. Shyamasundar.
Termination of logic programs via labelled term rewrite systems Thomas Arts and Hans Zantema Utrecht University, Department of Computer Science P.O. Box 80.089, 3508 TB Utrecht e-mail: [email protected]

Abstract

We propose proving left-termination of well-moded logic programs by transforming them into term rewrite systems (TRSs). We introduce the new notions single-redex reduction, a reduction in which exactly one redex occurs in each term, and single-redex normalising, normalising with respect to the single-redex reduction. We describe a transformation of wellmoded logic programs into TRSs for which termination of the logic program follows from single-redex normalisation of the TRS, which is far stronger than previous results, since termination, innermost normalisation etc., all imply single-redex normalisation. A powerful tool for proving termination of TRSs that are obtained by the proposed transformation is semantic labelling. We use it for proving termination of implementations of quick-sort and generation of permutations.

1. Introduction A proof of the correctness of a program normally consists of a proof that the program meets the speci cation (often called `partial correctness') and a proof that the program terminates, i.e., it is not possible that the program will loop forever. Among other applications, this is one of the reasons that research is carried out on techniques that are able to prove termination of programs. Recall that for all powerful programming languages, the question whether a program is terminating (the so called halting problem) is undecidable in general. Therefore, there is no automatic technique applicable to all programs, that always succeeds in deciding whether a program is terminating. In both the eld of logic programs and the eld of term rewrite systems (TRSs) research is carried out to nd techniques to prove termination for subclasses of logic programs and TRSs respectively. Applying these techniques on programs (logic program or TRS) typically results in: `Yes, the program terminates' or `Can't say anything about the program'. Thus, the techniques always results in an answer. In case the technique is not able to conclude that the program is terminating, one may try another technique. The larger the number of applicable techniques, the better. Since a lot of techniques have been developed for TRSs, it is useful to make these techniques also applicable to logic programs. One way to achieve this is by giving a transformation of logic programs into TRSs, such that the termination property is preserved. Thus, if the TRS terminates, then the original logic program terminates. This property is called soundness of the transformation, whereas the transformation is complete if termination of the logic program implies termination of the TRS. Several approaches to transform logic programs into TRSs exist. In this paper we propose a new transformation and prove its soundness. The proposed transformation is an improvement of earlier transformations [KKS91, GW92, CR93, AM93]. It is applicable to a wider class of logic programs and more techniques can be applied to the resulting TRSs. Like the other transformations, the transformation algorithm can only be applied to well-moded logic programs. Although this is a subclass of the logic programs,

many interesting algorithms are expressible as well-moded logic program. The natural notion of termination that is considered with well-moded logic programs is called left-termination. We prove not only soundness but prove that termination of the logic program can already be inferred from single-redex normalisation of the TRS. This is a much stronger result, since strong normalisation, innermost normalisation etc., all imply single-redex normalisation. As a result, techniques to prove innermost normalisation [DH93] or any other weak termination criteria, may be applied to prove termination of the logic program. Furthermore, the TRSs are of a special form, so called constructor systems (CSs), and special techniques, able to prove termination of these CSs [AZ95], may be applied. In [Mar94] a transformation is proposed that is sound for well-moded programs, but also complete for a subclass of well-moded programs. However, if one aims at a practical application to prove left-termination of well-moded logic programs, the transformation in [Mar94] has as a drawback that it is rather dicult and the obtained TRSs are complex, such that standard techniques are not applicable. Several transformations are proposed in [Mar94] to simplify the TRSs, but still the resulting TRSs of large programs will be complex. The transformation algorithm proposed in this paper is easy to understand and to implement. Without any non-trivial computation, a well-moded logic program is transformed into a TRS with as many rewrite rules as there are atoms in the logic program. Since implementations to prove automatically termination of some TRSs exist, we propose the following approach for automatically proving left-termination of well-moded logic programs: transform the logic program into a TRS and prove termination of the TRS by existing techniques like RPO and by techniques to be developed. The result in [AZ94] states that this approach indeed covers a great and important class of logic programs called structural recursive logic programs. But it covers far more logic programs. For example, termination of the logic program geq(x; x): p(0): geq(s(x); y) geq(x; y): p(s(x)) geq(x; y); p(y): is not easily seen directly without taking care of the e ect of the de ning procedure for geq . But by our transformation it transforms into a TRS of which termination is automatically checked by RPO, i.e., given a TRS to an implementation of the recursive path order (RPO), the computer returns the answer `Yes, the program terminates'. In other cases RPO is not able to answer positive and then the answer `Termination can not be concluded with this technique' is returned. Thus the intuition behind `a TRS is terminating by RPO' is that the TRS can easily be proved terminating, completely automatically. As mentioned before, most transformations are only de ned on well-moded logic programs. The rst transformation was published in [KKS91]. It was improved by the transformation in [GW92] which is able to prove terminating of more logic programs. This algorithm of [GW92] transforms logic programs into conditional rewrite systems. In [CR93] a two-step transformation was presented, which extended the transformation in [GW92] with a second transformation of the resulting conditional rewrite system into a TRS. Our transformation is inspired by this twostep transformation. Independently, in [AM93] a transformation was presented to transform a logic program together with a goal into a TRS. We prove that for our transformation single-redex normalisation of the TRS is sucient to conclude termination of the logic programs, which is stronger than the other results.

2. Preliminaries 2.1. Well-moded logic programs For a good introduction in logic programming we refer to [Llo87]. In this paper we will only use some standard de nitions. For reasons of simplicity we mean `de nite logic program' if we

write `logic program', and `program clause' if we write `clause'. Just like in [CR93] and [GW92] we will use a xed computation rule for the SLD-derivations. 2.1. Definition. The left-to-right computation rule is a computation rule that always selects the leftmost atom of a goal. An LD-derivation of P [ fGg is an SLD-derivation of P [ fGg via the left-to-right computation rule. 2.2. Definition (cf. [Llo87]). Let P be a program and G a goal. The LD-tree for P [ fGg is the tree in which each node is labelled by a (possibly empty) goal, inductively de ned by 1. The root node is labelled by G. 2. Let A1 ; A2; : : :; Am (m  1) be a label of a node in the tree. Then, for each program clause A B1; : : :; Bk in P such that A1 and A are uni able with mgu , the node has a child labelled by (B1; : : :; Bk ; A2; : : :; Am): 3. Nodes labelled by the empty clause have no children. Note that this is a slightly modi ed version of the standard SLD-tree de nition, with the di erence that we use the left-to-right computation rule. Like the standard SLD-tree, the LD-tree is nitely branching but can be in nite. 2.3. Definition. A mode m of an n-ary predicate p is a function from f1; : : :; ng to the set fin; outg. The set fi j m(i) = ing is the set of input positions of p and fo j m(o) = outg is the set of output positions of p. We say that a variable x occurs in an input (output) position of a literal p(t1 ; : : :; tn ) if it occurs in some tj such that m(j ) = in (m(j ) = out). 2.4. Definition. Let x be a variable in the clause A B1; : : :; Bk . The head A is called a producer (consumer) of x, if x occurs in an input (output) position of A, conversely, a body literal Bj is called a consumer (producer) of x, if x occurs in an input (output) position of Bj . 2.5. Definition. A clause B0 B1; : : :; Bn is called LR-well-moded, if every variable x in the clause has a producer Bi (0  i  n) and i < j for every consumer Bj (1  i  n) of x in the body of the clause. A program is called LR-well-moded, if each of its clauses is LR-well-moded. An LR-well-moded query is an LR-well-moded clause without a head. We consider a notion of termination that depends on the evaluation order of the literals in a query. This evaluation order is motivated by the leftmost selection rule used in Prolog. 2.6. Definition. We call a logic program left-terminating if there is no query Q such that the LD-resolution tree of this query is in nite. 2.7. Example. The well-moded logic program to compute all even numbers less then a hundred given by even(0) even(s(s(x))) even(x) less(0; s(y)) less(s(x); s(y)) less(x; y) p(x) less(x; s100(0)); even(x) with modings even(in), less(out; in) and p(out) is terminating by our de nition. But this program does not terminate for the goal p(x) if the rightmost literal of the body is always selected, as is allowed in a general SLD-derivation. Note that the program is not LR-well-moded any more if the two literals in the body of the last clause are interchanged.

Although we assume the modes of the predicates to be given, this does not imply that mode declarations are to be supplied by the programmer. The problem of automatic generation of mode declarations has been studied by many authors, e.g. [DW88]. Without loss of generality we may assume that every predicate has exactly one mode: if a predicate p is used with n di erent modes in a program, we consider it as being n distinct predicates, each having one xed mode. This is illustrated in the following example. 2.8. Example. Consider the following logic program to compute permutations of a list: append(nil; l; l) append(cons(h; l1); l2; cons(h; l3)) append(l1; l2; l3) perm(nil; nil) perm(l; cons(h; t)) append(v; cons(h; u); l); append(v; u; w); perm(w; t) append is used with modes append(out; out; in) and append(in; in; out), such that the last clause of the program is not well-moded. However, this program is well-moded if we choose di erent predicate symbols for every di erent moding. append1 (nil; l; l) append1 (cons(h; l1); l2; cons(h; l3)) append1(l1; l2; l3) append2 (nil; l; l) append2 (cons(h; l1); l2; cons(h; l3)) append2 (l1; l2; l3)

perm(nil; nil) perm(l; cons(h; t))

append2 (v; cons(h; u); l); append1 (v; u; w); perm(w; t)

2.2. Term rewrite systems In this section we summarise some preliminaries from term rewriting that we need in this paper. 2.9. Definition. A signature is a set F of function symbols. Associated with every f 2 F is a natural number denoting its arity, i.e., the number of arguments it is supposed to have. The function symbols of arity 0 are called constants. 2.10. Definition. Let F be a signature and V a set of variables disjoint from F . The set T (F ; V ) of terms built from F and V is the smallest set with the following two properties: (i) every variable is a term, (ii) if f 2 F is an n-ary function symbol and t1 ; : : :; tn are terms then f (t1 ; : : :; tn ) is a term. If c is a constant then we write c to denote the term c(). For a every term t, V ar(t) denotes the set of all variables occurring in t. 2.11. Definition. A rewrite rule or reduction rule is a pair (l; r) of terms satisfying the following two constrains: (i) the left-hand side l is not a variable, (ii) the variables which occur in the right-hand side r also occur in l. Rewrite rules (l; r) will henceforth be written as l ! r. 2.12. Definition. A term rewrite system (TRS) is a pair (F ; R) consisting of a signature F and a set R of rewrite rules between terms in T (F ; V ). A TRS is called nite if both F and R are nite.

2.13. Definition. The rewrite rules of a TRS (F ; R) inductively de ne the rewrite relation !R on T (F ; V ) by (i) If l ! r is a rewrite rule, then l !R r for every substitution  , (ii) If f 2 F is a function symbol with arity n and t1 ; : : :; tn and tk are terms such that tk !R tk , then f (t1; : : :; tk ; : : :; tn) !R f (t1 ; : : :; tk ; : : :; tn). A term t 2 T (F ; V ) is called a normal form if there is no term s 2 T (F ; V ) such that t !R s. A TRS R is called terminating if there exist no in nite reduction of the rewrite relation !R. 0

0

0

2.14. Definition. For a set F of operation symbols we de ne Emb(F ) to be the TRS consisting of all the rules f (x1; : : :; xn ) ! xi with f 2 F and i 2 f1; : : :; ng. These rules are called the embedding rules. We can also de ne a stronger notion of termination called simple termination. The de nition of simple termination is motivated by [Zan94]. 2.15. Definition. A TRS R over a set R [ Emb(F ) is terminating.

F

of function symbols is called simply terminating if

A standard technique to prove termination of TRSs, of which several implementations exists, is called RPO (recursive path order). This technique is not applicable to all TRSs. For example it is not applicable to terminating TRSs that are not simply terminating. 2.16. Theorem ([Der87, FZ94]). Let (F ; R) be a TRS and . a well-founded precedence on the ( nite or in nite) signature F and rpo the recursive path order de ned upon this precedence. If for all rewrite rules l ! r of R one has l rpo r, then the TRS (F ; R) is terminating.

3. Transforming a logic program into a rewrite system The intuition of the transformation given below is that the LD-resolution of the logic program is simulated by a TRS. Moreover, every resolution step corresponds to one or two rewrite steps. Normal forms of terms corresponding to well-moded goals, correspond to computed answer substitutions of these goals. We introduce a new notion of termination, called single-redex normalising, and prove the strong result: if the TRS obtained by transforming a logic program is single-redex normalising, then the logic program is left-terminating. In the algorithm the literals are denoted in such a way that the rst arguments are the in-positions and the last arguments are the out-positions. In the algorithm we write

p(t1 ; : : :; tk ; tk+1 ; : : :; tk+k ) 0

to denote that t1 ; : : :; tk are the arguments on input positions and tk+1 ; : : :; tk+k are the arguments on output positions. We introduce new function symbols outi for every arity i 2 IN and function symbols ki for i 2 IN to separate the distinct atoms in the body of a clause. In Section 5 some examples are given to clarify how this algorithm transforms logic programs into TRSs. It turns out that well-moded logic programs transform into TRSs of a special form, so called constructor systems. 0

3.1. Definition (cf. [MT91], [Gra92]). A constructor system (CS for short) is a TRS (F ; R) with the property that F can be partitioned into disjoint sets D and C such that every left-hand side F (t1 ; : : :; tn ) of a rewrite rule of R satis es F 2 D and t1 ; : : :; tn 2 T (C ; V ). Function symbols in D are called de ned symbols and those in C constructors.

In the transformation of a well-moded logic program, the de ned symbols of the resulting constructor system correspond to the predicate symbols and the k-symbols introduced in the algorithm. The constructor symbols correspond to the function symbols and constants of the logic program, also denoted by F un, together with the out-symbols introduced in the algorithm, also denoted by Out. For details see [AZ94]. 3.2. Definition. Let t1 ; : : :; tn be terms. We write var(t1 ; : : :; tn ) for the ordered sequence (with respect to some xed total order on variables) of all variables in t1 ; : : :; tn . For example var(p(z); max(x; x; x); less(0; y)) is the sequence x; y; z. program Transform (P :in, RP :out); begin RP :=;; index:=0; for each clause B0 B1; : : :; Bm 2 P do begin Let B0 be p0 (t1; : : :; tk ; tk+1 ; : : :; tk+k ); Out := outk (tk+1 ; : : :; tk+k ); if (m = 0) /* Program clause without body */ then RP := RP [ fp0 (t1 ; : : :; tk ) ! Outg; else /* Program clause with body */ begin for j = 1 to m do begin Let Bj ?1 be pj ?1 (t1 ; : : :; tk ; tk+1 ; : : :; tk+k ); Let Bj be pj (s1; : : :; sl ; sl+1; : : :; sl+l ); index := index + 1; V arold := V ar; V ar := var(t1; : : :; tk ); if (j = 1) then RP := RP [ fp0(t1; : : :; tk ) ! kindex(V ar; pj (s1; : : :; sl))g; else RP := RP [ fkindex?1 (V ar; outk (tk+1 ; : : :; tk+k )) ! kindex (V arold; pj (s1; : : :; sl))g; end RP := RP [ fkindex (V ar; outk (sl+1 ; : : :; sl+l )) ! Outg; end end end 0

0

0

0

0

0

0

0

0

4. Correctness and soundness of the transformation Note that there are no while loops in the transformation algorithm, hence the algorithm terminates. 4.1. Lemma. For each rule l ! r in RP one has V ar(r)  V ar(l). From the transformation algorithm it is clear that the obtained TRS RP is nite. Now we will prove that if the TRS, obtained from a well-moded logic program via the given algorithm, terminates, then the logic program terminates. Moreover, we will prove the stronger result that every goal is translated in a term such that reduction of this term corresponds exactly to a path in the LD-resolution tree.

We introduce a new notion of termination because of the observation that the behaviour of a well-moded logic programs is simulated by the transformed TRS in a very speci c way; only one redex occurs in the terms of a reduction corresponding to a resolution. 4.2. Definition. A reduction step t ! s is called a single-redex reduction step if there occurs only one redex in t. A single-redex reduction is a reduction such that in every term that is rewritten in this reduction only one redex occurs. If a term has not exactly one redex, it is in single-redex normal form. 4.3. Definition. A rewrite system R is called single-redex normalising if all single-redex reductions are nite. Termination (which is the same as strong normalisation), innermost normalisation and for example left-most innermost normalisation, all imply single-redex normalisation, since every single-redex reduction is nite for such a TRS. Although single-redex normalisation is a weak form of normalisation, it does not coincide with weak normalisation. 4.4. Example. The TRS a ! f (a; a) is not weakly normalising and therefore not terminating, nor innermost normalising, but it is single-redex normalising. On the other hand, the TRS fa ! a; a ! bg is weakly normalising, but not single-redex normalising. 4.5. Lemma. Let (D; C ; R) be a constructor system and c 2 C , such that if c occurs as root symbol in a right-hand side r of a rewrite rule of R, then r 2 T (C ; X ). If a term d(t1; : : :; tn ) with d 2 D reduces in a single-redex reduction step to a term c(s1; : : :; sm ), then c(s1 ; : : :; sm) is a normal form. Proof. Assume d(t1; : : :; tn ) ! c(s1; : : :; sm ), then there exists a rewrite rule l ! r in R and a

substitution  such that

l  d(u1; : : :; un)  d(t1; : : :; tn) ! c(s1 ; : : :; sm)  c(v1; : : :; vm)  r : Since there occurs only one redex in d(t1 ; : : :; tn ),  is a substitution of normal forms and by the assumption that r 2 T (C ; X ), r is a normal form. 4.6. Lemma. Let q (t1 ; : : :; tk ; tk+1 ; : : :; tk+k ) be a well-moded query to a well-moded program P . If there is an LD-refutation from the query and the logic program P , then a single-redex reduction q (t1 ; : : :; tk ) !+RP outk (tk+1 ; : : :; tk+k ) exists for some substitution . 0

0

0

Note that in a well-moded query q (: : :) there are no variables on the in-positions. Hence t1 ; : : :; tk are ground. Therefore the terms tk+1 ; : : :; tk+k  are ground. In the proof it is very important that the terms on the in-positions are ground and that all the mgu's in the LD-derivation are substitutions of variables in the out-positions. 0

Proof. The proof is by induction on the length of the LD-path in the LD-resolution tree. 

If the length of the LD-path is 1, then there exists a program clause

q(s1 ; : : :; sk ; sk+1 ; : : :; sk+k ) 0

that resolves with the query by mgu , i.e., tj  = sj  for all j 2 f1; : : :; k; k +1; : : :; k +k0g. The associated rewrite rule of this program clause is: q (s1; : : :; sk ) ! outk (sk+1 ; : : :; sk+k ). 0

0

Since the query is well-moded, all parameters on the in-positions are ground. Hence tj = tj  = sj  for 1  j  k and

q(t1; : : :; tk ) q(s1 ; : : :; sk )



= ! RP

outk (sk+1 ; : : :; sk+k ): 0

0

Since  is a mgu, we have sj  = tj  for k + 1  j  k + k0. The reduction step is a single-redex reduction step, since no predicate symbol, and hence no redex, occurs in the term sj (1  j  k). If the length of the LD-path is n+1, then the query resolves with a program clause

q(s1; : : :; sk; sk+1 ; : : :; sk+k ) 0

p1 (: : :); p2(: : :); : : :; pm(: : :):

via mgu 0 , say. Since the query q (: : :) has an LD-refutation, all atoms pj (: : :) have an LD-refutation of shorter length. The associated rewrite rules of this program clause are of the form:

q(s1; : : :; sk ) k1(var; out(1)(: : :))

!

k1 (var; p1(: : :)) k2 (var(1); p2(: : :))

km?1 (var(m?2); out(m?1) (: : :)) km(var(m?1); out(m) (: : :))

! !

km(var(m?1); pm(: : :)) outk (sk+1 ; : : :; sk+k )

!

.. .

0

0

Since the query is well-moded, all parameters on the in-positions are ground. Hence, tj = tj 0 = sj 0 for 1  j  k are ground and also var0 is ground.

q(t1 ; : : :; tk ) q(s10; : : :; sk 0)

= ! RP

k1(var0 ; p1(: : :)0 ):

The terms sj (1  j  k) do not contain a redex, hence the reduction step is a single-redex reduction step. Note that there is only one possible redex in k1 (var0; p1(: : :)0 ), viz. p1(: : :)0. By the induction hypothesis there is a single-redex reduction

p1 (: : :)0

!+ RP

out(1)(: : :)0 1

for some substitution 1 . Since var0 is ground, var0 = var0 1 . Since 0 1 is a substitution,

k1 (var01; out(1)(: : :)0 1 ) !RP k2(var(1)0 1 ; p2(: : :)0 1 ): By Lemma 4.5, this reduction is a single-redex reduction. Note that also out(1) (: : :)0 1 is ground, thus var(1)0 1 is ground. Using those arguments m times we obtain:

k2(var(1)0 1 ; p2(: : :)0 1 ) k2(var(1)0 1 ; out(2)(: : :)0 1 2 ) k3(var(2)0 1 2 ; p3(: : :)0 1 2 )

! RP ! RP

! RP

.. . (m?1) km(var 0 : : : m?2 ; pm(: : :)0 : : : m?2 ) (m?1) km(var 0 : : : m?2 ; out(m)(: : :)0 : : : m?1 ) outk (sk+1 ; : : :; sk+k ) outk (tk+1 ; : : :; tk+k )

! RP

! RP

=

0

0

0

0

where  = 0 : : : m and this reduction is a single-redex reduction. 4.7. Lemma. Let q (t1 ; : : :; tk ; tk+1 ; : : :; tk+k ) be a well-moded query to a well-moded program P . If there is an in nite LD-derivation from this query, then there is an in nite single-redex reduction in RP starting with q (t1 ; : : :; tk ). Proof. We prove this by induction on the structure of the in nite LD-tree with root node q(t1 ; : : :; tk ; tk+1 ; : : :; tk+k ). Since there is an in nite LD-derivation the query resolves with a program clause of the form q(s1; : : :; sk ; sk+1 ; : : :; sk+k ) p1(: : :); p2(: : :); : : :; pm(: : :) via mgu 0 , say. The associated rewrite rules of this program clause are of the form: q(s1 ; : : :; sk) ! k1(var; p1(: : :)) k1 (var; out(1)(: : :)) ! k2(var(1); p2(: : :)) .. . (m?2) (m?1) km?1 (var ; out (: : :)) ! km(var(m?1); pm (: : :)) km (var(m?1); out(m)(: : :)) ! outk (sk+1 ; : : :; sk+k ) Since the query is well-moded all parameters on the in-positions are ground. Hence tj = tj 0 = sj 0 for 1  j  k. We have: q(t1; : : :; tk ) = q(s1 0 ; : : :; sk 0 ) !RP k1(var0; p1(: : :)0): The reduction is a single-redex reduction, since no predicate symbol, and hence no redex, occurs in the term sj (1  j  k). Now we have two possibilities: 1. p1(: : :) has an in nite LD-derivation. Then, by induction, we have an in nite single-redex reduction of p1(: : :)0 . Note that p1(: : :)0 is the only redex in k1(var0; p1(: : :)0). Moreover, all terms in the in nite reduction are of the form k1(var0 ; s) and by Lemma 4.5 the root symbol of s can not be an out-symbol. Therefore, we have an in nite singleredex reduction of q (t1 ; : : :; tk ). 2. p1(: : :) has no in nite LD-derivation, hence this query has an LD-refutation. By Lemma 4.6 we know that there is a single-redex reduction 0

0

0

0

0

p1 (: : :)0 !+RP out(1)(: : :)0 1 for some substitution 1 . Note also that var0 is ground and hence var0 = var0 1 . Therefore, we have

k1 (var0; p1(: : :)0 1)

!+ RP ! RP

k1(var0; out(1)(: : :)01) k2 (var(1)0 1 ; p2(: : :)01 )

where by the induction hypothesis and Lemma 4.5, all reductions are single-redex reductions. By induction we have an in nite single-redex reduction of q (t1 ; : : :; tk ).

From Lemma 4.7 we infer immediate the main theorem. 4.8. Theorem. Let P be a well-moded logic program RP the associated TRS. If the TRS RP is single-redex normalising, then the logic program P is left-terminating. For the moment no tools are available to prove single-redex normalisation directly. Since termination implies single-redex normalisations, all standard techniques can be used. For the same reason, techniques to prove innermost normalisation, for example by computing forward closures [DH93], can be very helpful. If constructor systems R0 and R1 are innermost normalising and ful l some speci c demands, then R0 [ R1 is innermost normalising [Kri93]. Whereas this modularity result does not hold for termination. Theorem 4.8 enables us to use this and other kind of modularity results for TRSs.

5. Examples of the transformation The purpose of the following examples is to illustrate the transformation algorithm. 5.1. Example. The example from the introduction transforms into the rewrite system geq(x) ! out1 (x) geq(s(x)) ! k1 (x; geq (x)) k1(x; out1(y)) ! out1 (y) p(0) ! out p(s(x)) ! k2 (x; geq (x)) k2(x; out1(y)) ! k3 (x; y; p(y)) k3(x; y; out) ! out As we already mentioned in the introduction this TRS can automatically be proved terminating by RPO. 5.2. Example. The one-line logic program q (s(x)) q (c(s(x))) is transformed into the terminating TRS q(s(x)) ! k1 (x; q(c(s(x)))) k1(x; out) ! out Note that this TRS is not simply terminating since the rst rewrite rule is self-embedding, hence can not be ordered by RPO. By using the technique of descending chains [AZ95], this TRS is completely automatically proved to be terminating. Proving termination of the logic programs in the following three examples goes beyond syntactical analysis. No straightforward method to automatically prove termination of these programs is known. We use the semantical information that there is an argument that decreases (in a certain way) for every recursive call. One way to use the semantical information is semantic labelling; a technique to transform the obtained TRS to another TRS, called the labelled TRS, in such a way that the termination property is preserved. For the examples given below, the labelled TRSs can all be proved terminating by RPO. For detailed proofs we refer to [AZ94]. 5.3. Example. The logic program less(0; s(x)): less(s(x); s(y)) less(x; y):

max(x; x; x): max(x; y; x) max(x; y; z)

less(y; x): less(x; y); max(y; x; z):

with modings less(in; in) and max(in; in; out) is an inecient but correct way to compute the maximum of two given numbers. It is hard to prove termination of this logic program. With our method it transforms into a TRS that is not simply terminating and for this TRS a termination proof is still dicult. With semantic labelling the obtained TRS can be transformed into a labelled TRS, which can be proved terminating by RPO. 5.4. Example. The logic program to compute all permutations of a given list as described in Example 2.8, is transformed into the TRS: append1 (nil; l) ! out1 (l) append1 (cons(h; l1); l2) ! k1(h; l1; l2; append1(l1; l2)) k1(h; l1; l2; out1 (l3)) ! out1 (cons(h; l3))

append2(l) append2 (cons(h; l3)) k2(h; l3; out2(l1; l2)) perm(nil) perm(l) k3(l; out2(v; cons(h; u))) k4(l; h; v; u; out1(w)) k5(l; h; v; u; w; out1(t))

out2 (nil; l) k2(h; l3; append2(l3)) out2 (cons(h; l1); l2) out1 (nil) k3(l; append2(l)) k4(l; h; v; u; append1(v; u)) k5(l; h; v; u; w; perm(w)) out1 (cons(h; t)) Note that we obtain two di erent transformations for the append predicate, depending on the two modings of append in the program. Semantic labelling succeeds in proving termination of ! ! ! ! ! ! ! !

the TRS. Choose the natural numbers IN as the carrier set of an F -algebra M. Choose the functions nilM = 0 and consM (h; t) = 1 + t in such a way that they represent the length of a list. The functions for the predicate symbols, ranging over lists, are chosen to be the length of the lists that are given as input arguments for these predicates. The functions corresponding with the other symbols are chosen in such a way that M is indeed a model for the above TRS. As set of labels we choose the natural numbers. The obtained labelled TRS can be proved terminating by RPO. 5.5. Example. The following logic program (see [Plu90, Example 5.1.2]) is an implementation of the well-known quick-sort algorithm. split(h; nil; a; b; a; b): split(h; cons(x; l); a; b; a1; b1) more(x; h); split(h; l; a; cons(x; b); a1; a2): split(h; cons(x; l); a; b; a1; b1) lesseq(x; h); split(h; l; cons(x; a); b; a1; a2): qsort(nil; nil): qsort(cons(h; l); s) split(h; l; nil; nil; a; b); qsort(a; a1); qsort(b; b1); append(a1; cons(h; b1); s): Choose for more, lesseq and append the standard procedure de nitions. A termination proof for the obtained TRS uses, as in the previous example, semantic labelling and the observation that a recursive call is always applied to a shorter list.

6. Conclusions We presented a simply implementable transformation from logic programs into TRSs such that termination of the logic program follows from single-redex normalisation of the TRS. Analysing

termination of a TRS is much more basic than termination of a logic program since it does not depend on a particular computation rule and no uni cation is involved. As a technique for automatically proving termination of logic programs this is very promising. In [AZ94] we proved that all TRSs obtained from the class of structural recursive logic programs are indeed terminating. For other kind of programs we gave some examples. It turns out that the basic techniques of proving termination of TRSs, like RPO, only cover a small class of logic programs. However, if some labelling is applied to the TRS obtained from a logic program, motivated on semantic information of ingredients of the logic program, like lengths of lists, then often a TRS is obtained for which RPO succeeds in proving termination. Extracting the right semantic information and choosing the right labelling is subject of further research, of which the ultimate goal consists of an implementation having logic programs as input and termination proofs as output for a wide class of logic programs.

References [AM93] G. Aguzzi and U. Modigliani. Proving termination of logic programs by transforming them into equivalent term rewriting systems. Proceedings of FST&TCS 13, Lecture Notes in Computer Science(761):114{124, December 1993. [AZ94] Thomas Arts and Hans Zantema. Termination of logic programs via labelled term rewrite systems. Technical Report UU-CS-1994-20, Utrecht University, May 1994. [AZ95] Thomas Arts and Hans Zantema. Descending chains, a technique to prove termination of constructor systems. Second international workshop on termination, La Bresse, France, May 1995. [CR93] Maher Chtourou and Michael Rusinowitch. Methode transformationnelle pour la preuve de terminaison des programmes logiques. Unpublished manuscript in French, 1993. [Der87] N. Dershowitz. Termination of rewriting. Journal of Symbolic Computation, 3(1):69{116, 1987. [DH93] Nachum Dershowitz and Charles Hoot. Topics in termination. Proceedings of RTA-93,, LNCS(690):198{212, June 1993. [DW88] Saumya K. Debray and David S. Warren. Automatic mode inference for logic programs. Journal of Logic Programming, 5:207{229, 1988. [FZ94] Maria Ferreira and Hans Zantema. Syntactical analysis of total termination. Proceedings of ALP'94, LNCS(850):204{222, September 1994. [Gra92] Bernhard Gramlich. Relating innermost, weak, uniform and modular termination of term rewriting systems. Proceedings of LPAR'92, LNAI(624):285{296, 1992. Subseries of LNCS. [GW92] Harald Ganzinger and Uwe Waldmann. Termination proofs of well-moded logic programs via conditional rewrite systems. Proceedings of CTRS, LNCS(656):430{437, July 1992. [KKS91] M.R.K. Krishna Rao, Deepak Kapur, and R.K. Shyamasundar. A transformational methodology for proving termination of logic programs. Proceedings of CSL, LNCS(626):213{226, 1991. [Kri93] M.R.K. Krishna Rao. Termination characteristics of logic programs. PhD thesis, University of Bombay, India, 1993. [Llo87] J.W. Lloyd. Foundations of Logic Programming. Springer-Verlag, second extended edition, 1987. [Mar94] Massimo Marchiori. Logic programs as term rewrite systems. Proceedings of ALP'94, LNCS(850):223{241, September 1994. [MT91] Aart Middeldorp and Yoshihito Toyama. Completeness of combinations of constructor systems. Proceedings of RTA-91, LNCS(488):188{199, April 1991. [Plu90] Lutz Plumer. Termination Proofs for Logic Programs, volume 446 of LNAI. Springer-Verlag, 1990. Subseries of LNCS. [Zan94] H. Zantema. Termination of term rewriting: interpretation and type elimination. Journal of Symbolic Computation, 17:23{50, 1994. Our papers are available at http://www.cs.ruu.nl/thomas