Logic Programs as Term Systems

Massimo Marchiori

Department of Pure and Applied Mathematics

University of Padova

Via Belzoni Padova Italy

maxhilbertmathunipdit

Abstract

This pap er studies the relationship b etween programs and term rewrit

ing systems TRSs A comp ositional transform is dened whichgiven a logic

program computes a TRS For a relevant class of logic programs called Simply

Well Mo ded SWM there is a onetoone corresp ondence b etween computed

answer substitutions of the logic program and normal forms of the corresp ond

ing TRS Moreover the transform preserves termination ie a logic program

terminates i the corresp onding TRS terminates This transform is rened in

suchaway that the ab ove results hold for a relevant class of unication free

programs containing SWM the class of Flatly Well Mo ded FWM programs

Note This work was done during an authors stay at CWI Amsterdam

Intro duction

The study of transforms from logic programs into term rewriting systems TRSs

for short is an imp ortant topic Theoreticallyitisinteresting to analyze which

classes of logic programs can b e regarded as TRSs in disguise Practically using an

appropriate transform allows to infer prop erties of a logic program from prop erties

of the corresp onding TRS For instance this metho d has b een successfully applied in

anumberofworks to study termination of logic programs SRK RKS GW

AM using transforms that preserve nontermination

Transforms that compute TRSs which not only preserve nontermination but

are equivalent to the corresp onding logic programs havebeeninvestigated only very

recently Mar AM Equivalence means that the logic program terminates i

the corresp onding TRS terminates and that computed answer substitutions of the

logic program and normal forms of the TRS are in onetoone corresp ondence

The aim of this pap er is to provide a compositional transform which maps a

logic program into an equivalent TRS Comp ositionality is fundamental b ecause

for instance a comp ositional transform allows to transform a logic program into

a TRS in a mo dular way ie clause by clause The classes of logic programs we

consider are contained in the socalled well mo ded programs DM Moreover the

transform can deal with a broader class of programs where typ es instead of mo des

are considered

The contribution of this pap er can b e summarized as follows

First a comp ositional transform Tswm is intro duced which allows to translate

into a class of equivalent TRSs b oth the class of Simply Well Mo ded programs of

AE and a wider class of programs which are not wel l moded namely the Simply

and Well Typ ed programs SWT of AE

It is also shown how with little mo dications Tswm can b e extended to the whole

class of Well Mo ded programs losing equivalence but still providing a relevant

corresp ondence in the sense that if the TRS terminates then the corresp onding

logic program terminates as well

Finallyitisintro duced a second transform called Tfwm that extends Tswm by

translating the class of atly wel l moded programs FWM intro duced in Mar

into equivalent TRSs The FWM class has b een proven to b e lo cally maximal among

the unication free ones Moreover it is the lo cally greatest class of unication free

programs containing SWM Informally a class of logic programs is lo cally maximal

resp greatest among the unication free programs if it is maximal resp greatest

among those classes of unication free programs that can b e characterized bymeans

of clause by clause syntactical criterions see Mar for a formal denition Recall

that a logic program is unication free if unication in the pro cess can

b e p erformed by means of iterated applications of the simpler pattern matching

MK These results can b e roughly summarized in the following slogan

Logic Programs Unication Term Rewriting Systems

Relations with Previous Work

The idea to transform logic programs into TRSs started with SRK and has b een

further rened in RKSGW AM The main stimulus in this eld was the

study of termination of logic programs a transform is dened with the prop erty

that if a logic program do es not terminate then the corresp onding TRS do es not

terminate as well Thus a sucient criterion for proving the termination of a logic

program consists of checking whether the transformed TRS terminates

This approach is attractive b ecause TRSs enjoypowerful techniques to prove

termination as path orderings for instance see Der DJ whereas for logic

programs the situation is far more complicated

In this pap er the ab ove transformational metho dology is taken a step further in

the sense that weintro duce transforms that fully translate a logic program this way

a sucient but also necessary criterion for termination can b e given

A transform treating this kind of corresp ondence was intro duced in AM

However this transform is not comp ositional Also the transform Tfwm here pre

sented is able to transform classes of logic programs that are completely out of scope

from the previous transforms a drawback observed also by the same authors of

GW AM

The pap er is organized as follows Section gives the necessary preliminaries

Section intro duces the concept of transform and a formal setting for its analysis

In Section we sp ecify the languages used b oth for the logic programs and the

corresp onding TRSs and the basic rewrite rules used in the transforms In Section

the transform Tswm is dened and its completeness is proven wrt the class SWM

Section intro duces some techniques to simplify the output of a transform and

illustrates its application to prove the termination of a logic program by means of an

example Section intro duces the transform Tfwmthatisproven complete for the

class FWM The relevance of this transform is illustrated by means of a signicant

example Finally Section concludes with some imp ortant remarks Pro ofs are here

omitted or only sketched they can b e found in the full version of this pap er

Preliminaries

We assume familiarity with the basic notions regarding logic programs Llo

Apt and term rewriting systems Klo

In this pap er we will deal only with socalled LDderivations briey derivations

that is SLDderivations in which the leftmost selection rule is used

With TermF we will denote the set of terms built up from some function set

i

F of terms will b e written in vectorial notation eg t and with t we

will denote the ith term in t

Given a family S of ob jects terms atoms etc VarS is the set of all the

variables contained in it moreover S is said linear i no o ccurs more than

once in it ob jects O O will b e said disjoint i ij n VarO

n i

VarO s U t will mean that the terms s and t are disjoint and uniable With

j

tA t A t we will mean the term obtained by t replacing every o ccurrence

n n

in t of the symbol A with t i n For the sake of simplicitywe will sometimes

i i

omit brackets from the argument of unary functions eg f g X maybewritten

fgX and if t t t the f t ft will b e indicated with f t

n n

Also given two sequencess s s and t t t we will denote bys t

n m

the sequence s s t t

n m

Programs

We call logic program a nite set of Horn clauses and goals If the program has no

goals it is called a prop er program

Let P b e a program P the set of all the goals in P andP P n P Wecall

derivation of P a derivation of P fGgwhen G is in P Analogouslywecall

computed answer of P a computed answer substitution of P fGg

where G is in P The set of all the computed answer substitutions of P is denoted

by cas P

This formalism is well suited when formulating program prop erties since it allows

not to treat separately the clauses and the goal Moreover it is a natural choice when

using regular prop erties see b elow that essentially do not distinguish b etween goals

and clauses

Since we are going to transform logic programs in term rewriting systems the

same convention will b e adopted for TRSs as well reading goal as term clause

as rewrite rule and derivation as reduction hence an extended TRS is a nite

set of rules and a p ossibly innite set of terms The analogous of the set cas P for

a TRS T is the set NF T of its normal forms

The class of these extended logic programs resp TRSs will b e denoted by

LPext resp TRSext

Regularity

Denition A prop erty P is said regular if

B B P START B B P

n n

where START isanewnullary predicate symbol 

Using regularity will prove useful to elegantly shorten the description of the prop er

ties we will use in this pap er likewell mo dedness for example and also the denition

of the transforms since from nowon weidentify a goal with the corresp onding clause

having the sp ecial symbol START in the head and analogously for TRSs we identify

a term with the corresp onding rule having the sp ecial symbol START in the left

hand side cf Subsection for the language setting

Although this will not concern us it is worthwhile to notice that regularityisfar

from b eing merely a syntactic sugaring to ol but has an imp ortance for its own see

Mar

Mo des and Well Mo dedness

Denition A mode for a nary predicate p is a map from fng to fin outg

A moding is a map asso ciating to every predicate p a mo de for it 

An argument p osition of a mo ded predicate is therefore said input output if it is

mapp ed by the mo de into in out For every predicate pthenumb er of its arguments

ie the arity input p ositions and output p ositions will b e denoted resp ectively by

p p and p Moreover we adopt the convention to write ps t to denote

in out

a mo ded atom p having its input p ositions lled in by the sequence of terms s and

its output p ositions lled in by t

Now let us recall the following wellknown prop erty

Denition A program is data driven if every time an atom is selected in a

goal during a derivation its input arguments are ground 

Wenow state the basic notion of well mo ding

Denition A program satises the regular wel l modedness WM prop erty

if for every its clause C p t s p s t p s t it holds

n n n n

i

i n Vars Vart 

i j

j

Well Mo ded programs enjoy the following relevant prop erty

Theorem DM Wel l Modedprograms are data driven

Simply Well Mo dedness

First weintro duce the following class of programs

Denition A program satises the regular Simply Modedness SM prop erty

if for every its clause C p s t p s t p s t itholds

n n n

The sequence t t is linear comp osed only byvariables and disjointwith s

n

S

n

i n Vars Vart 

i j

j i

We will call simply wel l moded SWM a program which is b oth SM and WM

this is the main class proved in AE to b e unication free Interestingly this class

is quite large see for instance the list of programs presented in the pap er just cited

Transforms

Weintro duce here a formal setting for the analysis of fundamental prop erties of

transforms In this pap er S will always denote a class of logic programs GOALS

and SUBST the sets of all the goals and substitutions resp ectively Moreover is

the usual p owerset op erator

Denition A transform is a map LPext TRSext 

Denition A transform is semicomplete resp sound for S if

If P S then P has an innite derivation resp P has an innite

reduction

There is a computable map GOALS TRSext SUBST suchthatfor

every goal G S and every prop er program P S it holds G NF G P

resp cas G P 

Notice that transforms in SRK RKS GW are trivially semicomplete in the

sense that their map is just G NF G P SUBST

Denition A transform is said complete for S if

If P S then P has an innite derivation if and only if P has an innite

reduction

There is a computable map GOALS TRSext SUBST called an

answer map of such that for every goal G S and every prop er program P S

it holds G NF G P cas G P 

Comp ositionality

A transform as dened in Def can b e arbitrarily complex as far as the compu

tational asp ect is concerned A very imp ortant concept in developing a satisfactory

transform is the following

Denition A transform is compositional if for every two programs P P

P P P P 

Thus a comp ositional transform of a program is equal to the union of the trans

forms of its clauses

The advantages of a comp ositional denition are wellknown to compute the

transform of a program the transforms of its clauses can b e executed in parallel

Moreover to understand the relationship b etween logic programs and TRSs it suces

to consider a logic program with one clause Finally in case a new set of clauses

D is added to P obtaining a new program P to calculate the transformed P

we dont have to redo the whole computation again but only to transform D since

P P D P D This may b e of great advantage esp ecially when

the program P is big enough The general case in which P is mo died into P

p ossibly also deleting some of its clauses will b e treated later in Section

Setting the Stage

Languages

A moded language is a pair L m where L is a language and m is a mo ding for L Ev

ery logic program is written in a mo ded language L fp p f f gm

LP

where the p are the relation symbols and the f are the function symbols wecon

i i

sider constants like functions of null arity

For every natural nwe assume that in L there is an nary function such

LP n

functions are used as cartesian pro duct op erators

The functional language L for the TRS that will b e asso ciated with the

TRS

considered programs is describ ed as follows

a function p for every p L withp p

i i LP i in i

a function f for every f L with f f

i i LP i i

functions f f for every f L withf called

i i i LP i

j

f

i

inverse functions

an unary function N

unary functions for every n IN called transition functions

T

n

N is a marker asso ciated to terms t of L N t indicates that the term t is

TRS

in normal form in the considered TRS hence the name N Intuitively this means

that t has already b een computed

Inverse functions are used to select an argument from a term which is marked

thus already computed for instance the function f applied to the term

i

j

N f t t yields the term N t

i n j

Transition functions are used to rewrite a term whichismarked for example

to rewrite a term N X into the term f N X N X the function is used

T

f



where is a placeholder that will b e replaced with N X and f is an

appropriate natural numb er enco ding of f

n

Since the are used to represent cartesian pro ducts the notation is used

n

i

n

usually instead of and t t we to indicate the inverse function

n n n

i

i

will simply write and ht t i resp ectively

i n

The subset ff j f g of L that is the corresp onding of the prop er

i i TRS

functions in L will b e denoted byFunc whereas the set of all terms built on

LP TRS

L ie T L will b e denoted byTerm

TRS TRS TRS

Propagation

The following denition sp ecies the only rules that rewrite terms of the form

f t t with f Func

n TRS

Denition The propagation rules are

f N X N X Nf X X f Func

n n TRS



N f X X NX f Func i n f f

n i TRS

i

The rst schema of rules expresses the fact the N op erator propagates thru

terms in Func

TRS

The second schema is more interesting it shows that to apply an inverse function

wehavetowait for the argument to b e computed marked by N Nave rules of the

form f f X X X wont work intuitively supp ose a term t reduces to

n i

i

two normal forms ie it gives two results say ht t i and hs s i and supp ose that

the rst pro jection as well as the second pro jection are used Then by applying

the nave rules hX Y i X and hX Y iY one can obtain the following two

reductions t ht t it and t hs s is thus violating the

fundamental prop erty of the pro jections ie h t ti t The rules of Denition

allowtoavoid such splitting phenomena we will see later Subsection how

to relax these rules whenever these bad cases do not happ en

Transition

Transition rules will b e used in the transform to enco de a clause of a program by

means of a rewrite rule In the index n enco des the transition that has to b e

T

n

p erformed using the following injective and computable enco ding function

TermL fg IN

TRS

The new symbol is intro duced to represent the argument of the transition function

as will b ecome clear from the following denition

Denition The transition rules are dened as follows

N X tN X t TermL fg

T

TRS



t

will haveascorre so for example instead of For simplicitywe write

T T T

t

f t



sp onding transition rule N X f N X N X

T

f



The Transform Tswm

Weintro duce here the T ransform for s imply w ell mo ded programs called Tswm

Roughly a clause C A B B is transformed into a rewrite rule r where

n

the left hand side lhs of r is a TRS term obtained transforming the head of C

while the right hand side rhs of r is constructed incrementally in a sequence of

n steps where in the ith step i n the partial rhs so far constructed

is mo died using the atom B and in the n th step the atom A is used Tothis

i

end twovariables rhs and V are used where rhs contains the part of the rightend

side of r so far computed and V is a sequence of terms in Term used to store

TRS

the output arguments of the atoms in the clause

First we need the following denition to extract arguments from a term

Denition Given a term t Term and a variable X Vart a suitable

TRS

of X from t notation SE t is dened as SE t where

X X

i SE X uu

X

ii SE f t t uSE t f u if X Vart i n

X n X i i

i

Generalizing in case t s Term and Vart fX X g Vars the suit

TRS n

able expression of s from t is dened as SE ts X SE tX SE t

s X n X

 n

wheres is obtained from s replacing every constant c by N c 

Note that a suitable expression is in general not unique b ecause in p oint ii there

may b e more than one t containing X However this is not a problem since we

i

dont care ab out dierent o ccurrences of the same variable to obtain uniqueness

one can simply change the denition taking the minimum i

and SE t g f Example If t f X ghY i then SE tf

Y X

SE thX f N a Y iXSE tYSE t

X Y

hXf aY i

hX f N a Y iZf Y g f hf fN a g f i 

Denition Transform Tswm

Let the clause b e C pt s p s t p s t

n n n n

The lhs of the rewrite rule is lhs pNht i

The rhs is built iteratively the atoms in the b o dy are transformed in left to right

order from p to p using a sequence V of terms The following sequence of actions

n

is used

rhs NhX X i and V X X where fX X g Vart

k k k

For i to n do rhs rhs V V ht i

T

i

SE hV i

hVp hs ii

i i

If C is not a goal viz p START then let rhs rhs

T

SE hV i

hs i

n

The pro duced rewrite rule is then lhs rhs

This rule has o ccurrences of functions inverse or not and of T op erators for each

of them include the corresp onding propagation and transition rules 

Assumption Since propagation and transition rules can b e inferred from the rule

lhs rhs henceforth we will omit them when writing the TRS obtained from the

transform of a program

Example Take the classical app end program with mo ding apoutoutin

ap XX

apAXYAZ apXYZ

Then its transform is

ap NhX i hN X i

T

hN i





ap NhAjZ i NhA Z i

T T

h j i h aph i i

       

Builtin

As seen the transform Tswm translates each clause C into a nite TRS consisting

of one rule lhs rhs together with the corresp onding propagation and transition

rules Wecalllhs rhs the main rule pro duced by the transform denoted by

Tswm C while the remaining rules are called builtin rules notation Tswm C

M B

hence TswmC Tswm C Tswm C

M B

The intuition is that these builtin rules are just to olb ox accessories for the main

rule

Thus let us say the builtin B is the TRS comp osed by b oth the propagation

and transition rules note this is an innite TRS so not in TRSext

Later on we will improve the transform adding rules to the builtin so we assume

that when translating a clause C its transform is given by the main rules plus

the rules from B that have some symb ol in common with the symb ols in the main

rules This assumption will stayvalid also for the subsequent transform Tfwm of

Section

It is evidentwe could replace Tswm C with B itself without aecting the prop

B

erties of the TRS pro duced by TswmThus we can study B as a whole instead of its

nite subTRSs that are generated by Tswm

A rst relevant prop erty B should enjoyiscompleteness This is not the case

b ecause B is terminating but not conuent consider the term f N f N X inB it

reduces to two dierent normal forms NNX and f NNfX Torecover complete

ness one can add the following rule

NNX NX idempotence rule

that is plausible expressing the fact that marking twice a term by means of N is

the same than marking it once Nevertheless B is complete relatively to the terms

pro duced by Tswm itself

Summing up wehave the following

Lemma B fN N X NX g is complete whereas B is complete only relatively

to the terms producedbythetransform itself

Tohave a theoretically cleaner treatment we include the idemp otence rule in the

builtin b eing aware it do es not play a role op erationallySowe can reformulate the

ab ove lemma saying that B is complete

Another imp ortantproperty the builtin enjoys is the following roughly sp eaking

two TRSs commute if it do es not matter in which order we apply rules from a TRS

or from the other see Klo for a formal denition

Lemma For every program P the builtin commutes with Tswm P

M

Completeness of Tswm

The transform Tswm satises the following fundamental prop erty

Theorem Tswm is a complete compositional transform for SWM

The pro of of the ab ove theorem is not easy and quite long The idea is that wedene

an embeddedsequence from the sequence of goals G G of an LDderivation

G C G C in the following way means normalization wrt the TRS T

T

E G Tswm G

M B

E G E G

i i B

Tswm C

M i

Notice that Lemma guarantees that the normalization pro cess wrt B is well

dened ie it terminates and yields an unique result Moreover Lemma ensures

that normalization wrt B do es not prevent the applicability of the main rules To

showthat is welldened the following two lemmata are used in fact

Tswm C

M i

this normalization pro cess reduces to just a onestep reduction

Lemma Agoal G SWM and a clause C SWM have a resolvent i the rule

Tswm C can be applied to the term Tswm G

M M B

Lemma For every goal G and clause C theruleTswm C can be appliedto

M

the term Tswm G at most in one position that is thereare no ambiguities

M B

Then a map is dened that asso ciates to every member E G oftheembedded

i

sequence the partial answer substitution of G This map when G gives

i i

precisely the answer map of Denition whichisnow dened

Tswm

If the goal is G p s t p s t and W Vart t then

n n n n

the answer map is

Tswm

G NF TswmGP f j hht iht ii t N t NF TswmGP g

W n

Tswm

where j is the restriction of to the variables in W

W

Simplifying the Transform

In this section we showhow by using simple techniques the output pro duced by

the transform can b e greatly simplied providing a more readable and easier to

analyze TRS

Normalization

One great simplication comes from normalizing the rhs of the main rewrite rule

obtained by the transform wrt its builtin This in general proves quite useful

b ecause in Subsection we will increase the p ower of the builtin adding simplifying

rules that enter in action just thanks to this technique that is we will normalize

wrt the extended builtin

Example As a rst example of what is p ossible using this technique consider

the program app end seen previously in Example If the obtained main rules are

normalized wrt the builtin we obtain nally

ap NhX iNhXi



hN A N Z ap NhZ ii ap NhAjZ i

T

h j i

    

Smart Referencing

The transition functions used in the construction of the transform maintain a growing

sequence V of all the output arguments in the b o dy of the clause however all

this information is not needed in case no further references are made to a variable

o ccurring either in some output argument or in the input arguments of the head

of the clause To preventsuchawaste we can rene the transform letting V be

the set of variables really needed in the sequel smart referencing more formally

the dierence with the old transform is that we drop from V the variables no more

present neither in the input arguments of the remaining atoms to b e pro cessed nor

in the output arguments of the head atom

An example will clarify the situation

Example Consider the clause

pXYZ qXWrYZ

with mo ding pininout qinout rinout

Its transform via Tswm gives

phN XN Y i

T T T

NhXY i

h q h i i h i h h ir h ii

         

  

     

hV ihXY hW ihZ ii hV ihXY hW ii hV ihXY i

But note howinX is not needed and in Y and W are not needed as well

so the revised transform gives

phN XN Y i

T T T

NhXY i

h i hr h ii h q h i i

    



     

hV ihhZ ii hV ihYhW ii hV ihXY i

Relaxing the Builtin

Both the propagation and transition rules aim at constraining the calculus forcing

their arguments via the N op erator to b e completely calculated b efore b eing

further utilized

As said it is the p ossibility of splitting that forces to b ehavethisway review

Subsection anyway when this is not the case we can get rid of this imp osed

extra structure

One such case is when unary functions are concerned thus we add the following

rules to the propagation rules that relax their b ehaviour recovering the symmetry

between a function and its inverse that was missing in the original propagation rules

f N X Nf X f Func f

TRS

f fX X

ff X X

Regarding the transition rules instead the following rules are added

t TermL fg with

TRS

X tX

T

only one o ccurrence of

t

This case is in all similar to the previous one since transition functions are mas

querade functions with argument and hence their arity should b e calculated in

terms of o ccurrences of

Adding all these rules to the builtin is useful b ecause as said earlier in Subsec

tion when we normalize wrt the builtin much of the extra structure imp osed

disapp ears simplifying a lot the obtained rules Note also that for this extended

builtin Lemmata and still hold

Example Consider the previous Example

pXYZ qXWrYZ

was transformed using smart referencing into

NhX Y i phN X N Y i

T T T

h i hr h i i h q h ii

    

This rewrite rule normalized wrt the builtin is now

phN X N Y ir h hN Y qNhX iii 

Example Take the clause

pXY qXVrVWZsZY

mo ded pinout qinout rinoutout and sinout

Its transform using the nave Tswm transform gives

pNhX i NhX i

T T T T

h i h h ih ish i i h h ir h ii h q h i i

                 

whereas using smart referencing and the builtin extended with the aforementioned

unary functions rules weget

pNhX is rqNhX i 

Termination of Logic Programs an Example

Semicomplete transforms can b e used to provide a sucient criterion for proving

termination of logic programs a logic program P terminates if its transformed TRS

P terminates Clearlya complete transform provides a sucient and also neces

sary criterion hence strengthening the analysis p ower

Now consider the following program taken from RKS and further cited in

GW AM computing the transitive closure of a relation p

pab tcXY pXY

pbc tcXY pXZtcZY

with mo ding pinout and tcinout

The transform develop ed in RKS transforms this terminating logic program

into a nonterminating TRS whereas the transform Tswm since the ab oveprogram

is SWM obtains a TRS

pNhaiNhbi tcNhX ipNhX i

pNhbiNhci tcNhX itc pNhX i

that can b e proven terminating using standard techniques of term rewriting

Another application of this technique to prove termination of logic programs is

given in Subsection using transform Tfwm

The Transform Tfwm

Transform Tswm is complete for SWM In this section we mo dify Tswm to get a

more p owerful yet more complicated transform which is complete for the broader

class of Flatly Wel l Moded programs FWM after Mar

The idea underlying this sub class of well mo ded programs is that roughlyevery

time an atom is selected to resolve the head of a clause its output arguments are

all lled in with attening expression a attening expression briey FE is either

a variable or a ground term or a socalled steady term where a term t is said to

be steady if t is linear and t f f X X withkandf

k n

f f For example f g hX Y is steady whereas f X X

k k

f g X Y and f g X hY are not

FWM has the remarkable prop erty of b eing not only lo cally maximal among

the unication free classes of programs but even the lo cally greatest one containing

SWM see the Intro duction and see Mar for details

To dene Tfwmwe need the following concepts

Denition The degree of a attening expression is the map

jjtjj if t is ground

jjtjj if t is a variable

jjtjj k if t f f X X

k n



Denition The truncation of a term t at level k Trunc t is

k

Trunc tt

Trunc t s for s an FE jjsjj k s U t 

k

The transform Tfwm acts like the previous Tswm except that it keeps trackof

the structure of the terms o ccurring in the output p ositions of the selected atom in

the goal When the head of the clause is considered a numb er of terms are pro duced

and each of them is used as lhs of a corresp onding rewrite rule These terms are

computed by considering the degrees of all the FEswhich unify with the output

arguments of the head Each of these degrees combined with the head yields the lhs

of a rewrite rule To this end for an atom pt satripleht s di is used where t is

the input argument s is the output argumentandd is the degree of s Notice that

in Tswm only the input argument t is used to dene the lhs of the corresp ondent

rule The rhss of these rules are computed in a similar wayasinthecaseofTswm

only that this time when pro cessing the atoms in the b o dy also the structure of the

current output and its degree is taken into account the starting one is calculated

by means of the clause head

For example consider the clause pXfaY qXY mo ded pin out

qin out pXfaY yields three rules the rst has lhs pNhX f a Y i corre

sp onding to the case that a degree term ie a ground one is present in the output

argument of the atom the clause head unies with and rhs q NhX Y i if f a Y

unies with a ground term then Y is set to a ground term hence a degree FE

as well the second has lhs pNhX Y i corresp onding to the degree case ie a

variable and rhs q NhX Y i if f a Y unies with a variable then Y remains a

variable hence again a degree FE the third has lhs pNhX f Z Y i corre

sp onding to the degree case ie a steady term and rhs q NhX Y i if f a Y

unies with a degree FE the latter has the form f W W and so Y remains a

variable viz a degree FE

The construction pro cess of the main rules is similar to the one in Tswm hence

for space reasons we simply state the lhs and the start rhs

Denition Transform Tfwm For simplicitywe assume that relation sym

b ols have only one output argument the general case b eing in all similar Moreover

we apply at once smart referencing Subsection

So consider a clause pt s p s t p s t we distinguish the fol

n n n n

lowing cases

Vars Vart

n

This means for the data driveness prop erty of WM programs that s is instan

n

tiated to a ground term the transformed rule is then

Transition functions

pNht YZi T s YZNht i

n

likewise Tswm

where T X Y Z W isaT est op erator that checks whether unication b etween

the head and the selected atom in the goal can o ccur and in armative case yields

W This can b e done b ecause wehave the information ie Z ab out the degree of

the attening expression present in the output argument of the goal atom ie Y

T is dened by the following rules

i T f X fY sssZ W T X Y ssZ W f Func deg

TRS

ii T f X X fY Y ssW W f Func deg

n n TRS

iii T X Y sW W deg

iv T X X W W deg

all these T rules are included in the builtin rules in i and ii alb eit in innite

numb er are manageable by the transform since they are needed only when the

corresp onding functions of Func are present and so are treated analogously to

TRS

the propagation and transition rules

Note that the output eventually obtained after the T test has b een passed is just

Nht i and not the full description Nht s i b ecause this extra information is

n

utterly sup eruous variables in s are already contained in t

n

Vars Vart

n

Two cases are p ossible whether the degree of a attening expression unifying with

s is bounded or unbounded

n

A k maxfjjtjj t is an FE t U s g b ounded degree

n

Then the transform pro duces k rewrite rules

Transition functions

pNht Trunc s ss i Nht i i k

i n

z

likewise Tswm

i

where Trunc s is required to b e disjointwith t Again note that just

i n

Nht i is used and not a full description of the output b ecause for prop erties

of the FWM class no references to variables in s are p ossible in this case

n

B maxfjjtjj t is an FE t U s g unb ounded degree

n

This case can only happ en when s is a steady term or a variable so let

n

k jjs jj

n

The transform pro duces k rewrite rules

The rst k are analogously to the previous case A

Transition functions

pNht Trunc s ss i ssi Nht s

i n

n

z z

likewise Tswm

i

k

with i k t and Trunc s disjoint where s is a ground instance

i n

n

of s this is done only to ensure the ab ove rules satisfy the condition of a

n

rewrite rule to have all of the variables in the righthandsidecontained in the

left hand side For instance if s f g X Y and Trunc s f X

n n

to put s in the rhs wewould need to intro duce another variable Y inthe

n

lhs

Acting this way is safe for we are only interested in Trunc s andnotin

k n

s itself and so we can pass to s since Trunc s Trunc s

n k n k

n n

up to a renaming

The k th rule corresp onds to the innite cases in which the degree of the

output part in the goal atom is greater than k the key fact is just that we

can parametrize in a single rewrite rule an otherwise innite sequence of cases

The rule is

Transition functions

Nht s ss Z i Z i pNht s ss

n n

z z

likewise Tswm

k k



The main result is

Theorem The map Tfwm isacomplete compositional transform for FWM

The pro of uses the same techniques employed in proving the analogous Theorem

for Tswm

An example

Consider the following logic program after GW AM

pXgX

pXfY pXgY

with mo ding pin out This program alb eit very simple is completely out of scope

from all the other transforms presented in the literature SRK RKS AM

and even the one in GW transforming logic programs into the more p owerful

conditional TRSs The problem as already noticed in GW is that all these

transforms consider only the input arguments which in this example are xed

and not the output ones Instead since the ab ove program b elongs to FWM its

transform via Tfwm gives

T g X YZNhX i pNhX Y Z i

T

hg i



pNhX f Y i NhX f Y i

T T

 

hfg i ph g f i

   

pNhX Y s i NhX gcss i

T T



ph g i

hfg i

  



NhX gcss i pNhX f Y ssi

T T



hfg i ph g i

   

pNhX f Y sssZ i NhX f Y sssZ i

T T

 

hfg i ph g f i

   

that is a terminating TRS whose termination can b e proved for every goal b elonging

to FWM these include eg pa ground termX and all the goals of the form

pa ground terma flattening expression

The transform Tfwm enhanced with the normalizing technique of Subsection

gives nally

pNhX Y Z ihg T g X YZNhX i i

pNhX f Y ihfg pNhX gY ii

pNhX Y s ihfg pNhX gcssii

pNhX f Y ssihfg pNhX gcss ii

pNhX f Y sssZ ihfg pNhX gY sssZ ii

Nowwe illustrate some reductions in the TRS to clarify how the mimicking of the

logic program is p erformed



 

hpNhaXs ii hhg T g aXsNhai ii

paX



B







hhg Nhaiii

hhfg pNhag cssii i





B B







 



Nhhg a ii

hhfg hg T g ag cssNhaiii i

 

B





 

Nhhf a ii  





 

hpNhaf X ss ii

pafX

hhfg pNhag cssiii









hhg T g af X ssNhai ii

hhfg hg T g ag cssNhaiiii



 

B





hhfg hg Nhaiiii

 

B





 

 

Nhhf a ii





 

hpNhaf a ii

pafa

hhfg pNhag aiii









hhg T g af aNhai ii

hhfg hg T g ag aNhai ii i



 

B





hhfg hg Nhaiiii

 

B





 

Nhhf a ii





 

hpNhaf hXY sssii

pafhXY

hhfg pNhag hXY sssiii







hhfg hg T g ag hXY sssNhaiiii

 

B





hhfg hg T ahXY ssNhaiiii

 



 

hpNhaf hag a ii hhg T g af hag aNhai ii

pafhaga







hhfg pNhag hag a iii



Remarks

Wementioned in Subsection among the advantages of a comp ositional trans

form that in case of mo dications of a program P adding new rules to obtain a new

program P only the transform of the dierence program has to b e calculated

The general case in which from P we pass to an arbitrary P is readily much

more worthwhile in case of practical applications It turns out that for the transform

here develop ed this case can b e cop ed satisfactorily as b efore

For twoTRSsT and T dene T n T as the TRS fr j r T nT r T B g

M

that is we cut from T only its main rules Then it holds stands for Tswm or Tfwm

P P n P n P P n P P n P n P P n P

M

and since P was already calculated only P n P and P n P remain hence we

can generalize the case treated in Subsection saying that when from P wepass

to P to calculate the transform of P once given the one of P only the transforms

of the dierence programs need to b e calculated

In developing the transforms of this pap er we paid attention to completeness only

Nevertheless semicompleteness is imp ortant in deriving sucient criterions for ter

mination It turns out that we can easily extend our transforms to makethem

semicomplete for the broader class of Well Mo ded programs

The problem is that the result of Lemma do es not hold any more since only

the input arguments discriminate the application of a rule to a term To cop e with

this weintro duce conditional op erators dened via the rules

C t t t Term

t TRS

that check whether a given argument has a certain structure provided by t if it

has not the computation stays blo cked that is N cannot propagate

The mo dication of the transform Tswm is simply to put

rhs rhs in place of rhs rhs

T T

SE hV i SE hV i

hVC p hs ii hVp hs ii



i i i i

Nht i

i

viz every time p hs i is computed it is checked whether the result is compatible with

i i

the term structure of t The same technique is used for Tfwm

i

We gain only semicompleteness and not completeness b ecause the check is p er

formed only after the computation of p s is nished and therefore new innite

i i

computations could b e added destroying soundness

So far all the prop osals of transformation from logic programs to TRSs have b een

staying always within well mo ded programs However Transform Tswm has b een

built in suchaway to supp ort a broader class that is the class of Simply Wel l Typed

SWT programs intro duced in AE this is the generalization of the SWM class

to typ es muchlike the class of Well Typ ed programs intro duced in BLRisfor

Well Mo ded programs thanks to the intro duction of typ es it allows nonground

inputs thus giving much more exibility than the SWM class Using almost the

same pro of used for the SWM case it can b e shown that Tswm is complete for SWT

as well

References

AE KR Apt and S Etalle On the unication free Prolog programs In

S Sokolowski editor MFCS LNCS pp Springer

AM G Aguzzi and U Mo digliani Proving termination of logic programs by

transforming them into equivalent term rewriting systems FSTTCS

LNCS Springer

Apt KR Apt Logic programming In J van Leeuwen editor Handbook

of Theoretical Computer Sciencevolume B chapter pp

Elsevier MIT Press

BLR F Bronsard TK Lakshman and US Reddy A framework of direc

tionality for proving termination of logic programs In KR Apt editor

JICSLP pp MIT Press

Der N Dershowitz Termination of rewriting JSC

DJ N Dershowitz and J Jouannaud Rewrite systems In J van Leeuwen

editor Handbook of Theoretical Computer Sciencevolume B chapter

pp Elsevier MIT Press

DM PDembinski and J Maluszynski ANDparallelism with intelligent back

tracking for annotated logic programs ILPS pp

GW H Ganzinger and U Waldmann Termination pro ofs of wellmo ded logic

programs via conditional rewrite systems In M Rusinowitch and JL

Remy editors CTRS pp July

Klo JW Klop Term rewriting systems In S AbramskyDovMGabbay

and TSE Maibaum editors Handbook of Logic in Computer Science

volume chapter pp Clarendon Press Oxford

Llo JW Lloyd Foundations of Logic Programming Springer nd ed

Mar M Marchiori Logic programming matching and term rewriting systems

Masters thesis Dep of Pure and Applied Mathematics Universityof

Padova Italy July In Italian

Mar M Marchiori Lo calizations of unication freedom through matching

directions Submitted March

MK J Maluszynski and HJ Komorowski Unicationfree execution of logic

programs IEEE Symposium on Logic Programming pp

RKS K Rao D Kapur and RK Shyamasundar A transformational metho d

ology for proving termination of logic programs In CSLvolume

of LNCS pp Berlin Springer

SRK RK Shyamasundar K Rao and D Kapur Termination of logic pro

grams Computer science group Tata Institute of Fundamental Research

Bombay India Revised version Rewriting concepts in the study

of termination of logic programs ALPUK