Termination of logic programs with delay declarations

Termination of logic programs with delay declarations

TME ~ r R ~ A L O F ~ ELSEVIER ~MMlbit3 The Journal of Logic Programming 39 (1999) 95- ! 24 T e r m i n a t i o n o f logic p r o g r a m s with d...

2MB Sizes 0 Downloads 65 Views

TME ~ r R ~ A L O F

~ ELSEVIER

~MMlbit3

The Journal of Logic Programming 39 (1999) 95- ! 24

T e r m i n a t i o n o f logic p r o g r a m s with delay declarations E l e n a M a r c h i o r i *, F r a n k T e u s i n k Department o f Computer Science. Leiden l~)tirersio'. P. 0

Box 0512. 23t~9 RA Leiden. ~lte" Netherlands

Received 4 February 1997: received in revis,:.,,i form I September 1998; accepted ! i September 1998

Abstract T h i s p a p e r proposes a m e t h o d for p r o v i n g t e r m i n a t i o n o f logic p r o g r a m s w i t h delay declarations. T h e m e t h o d is based o n the iac~~ion o f r e c u r r e n t logic p r o g r a m , which is used to prove p r o g r a m s t e r m i n a t i n g with respect to a n arbitrz~r-y selection rule. M o s t i m p o r t a n t l y , we use the n o t i o n o f b o u n d e d q u e r y (as p r o p o s e d by M. Bczem) in the definition o f cover, a new n o t i o n which forms the kernel o f o u r a p p r o a c h . W e ifitroduce the class o f d e l a y reczwrent p r o g r a m s a n d prove t h a t p r o g r a m s in this class t e r m i n a t e for all local d e l a y selection rules, provided t h a t the delay c o n d i t i o n s i m p l y b o u n d e d n e s s . T h e c o r r e s p o n d i n g m e t h o d c a n be also used to transf o r m a logic p r o g r a m into a t e r m i n a t i n g logic p r o g r a m with delay declarations. © 1099 Elsevier Science Inc. All rights reserved. K e y w o r d s : Logic p r o g r a m s ; D y n a m i c selection rule; T e r m i n a t i o n , D e l a y d e c l a r a t i o n

1. Introduction D e l a y d e c l a r a t i o n s are used for the d y n a m i c c o n t r o l o f the selection o f a t o m s in a d e r i v a t i o n . T h e i d e a is t h a t , b e s i d e s t h e u s u a l l o g i c c l a u s e s , t h e p r o g r a m c o n t a i n s declarations of the form DELAY p r e d i c a t e UNTIL c o n d i t i o n T h e n , a s e l e c t i o n r u l e is u s e d w h i c h o n l y s e l e c t s a n a t o m f r o m a q u e r y , i f t h a t a t o m is n o t d e l a y e d , i.e. t h e c o n d i t i o n in t h e d e l a y d e c l a r a t i o n f o r t h a i a t o m is s a t i s f i e d . D e l a y d e c l a r a t i o n s a r e e m p l o y e d in m a n y p r o g r a m m i n g s y s t e m s b a s e d o n l o g i c p r o g r a m m i n g , l i k e N U - P r o l o g [25] a n d G 6 d e l [13]. T h e y a r e i m p o r t a i t ! f o r a n u m b e r o f r e a sons: for i n s t a n c e , t h e y c a n be u s e d to e n s u r e t e r m i n a t i o n o f the program~ or to support coroutining. As a consequence, efficient algorithms can be produced from a simple logical specification augmented with suitable delay declarations. This approach reflects t h e idea o f c o n s i d e r i n g a p r o g r a m as c o n s i s t i n g o f t w o parts: logic a n d c o n t r o l .

"Corresponding author. Tel.: +31 71 52771 I2: lax: +31 71 5227 6985: e-mail: [email protected]. 0743-1066/99/5 - see front matter © 1999 Elsevier Science Inc. All rights reserved. PI1:S0743- I 066(98) 10034- 1

96

E. Marchh,'i. F. Teuxhtk I ,I. Logic Programming 39 (1999) 95- 124

In this p a p e r , we i n t r o d u c e a m e t h o d for p r o v i n g t e r m i n a t i o n o f logic p~'ograms with d e l a y d e c l a r a t i o n s . T o i l l u s t r a t e h o w d e l a y d e c l a r a t i o n s m a y affect the t e r m i n a tion b e h a v i o u r o f a p r o g r a m , c o n s i d e r the w e l l - k n o w n append/3 p r o g r a m : app( [xlxs]. ys, [xlzs]) --- app(xs, ys, zs). app([ ]. ys..l~'). a n d the- q u e r y app(xs, [4,5], zs), xs = [! ,2.3]. T h i s q u e r y does not t e r m i n a t e w h e n the l e f l m o s t selection rule is used. H o w e v e r , s u p p o s e we a d d tile f o l l o w i n g d e l a y declar a t i o n for append/3: DELAY app(xs, ys, :s)

UNTIl,

list (xs)

W i t h this d e l a y d e c l a r a t i o n , the l e f t m o s t a t o m in the q u e r y is d e l a y e d . T h e r e f o r e , if we use a d e l a y selection rule, o n l y the s e c o n d a t o m c a n be seicctcd, r e s u l t i n g in the r e s o l v e n t app([I,2,3], [4,5], :s). H e r e the a t o m in the q u e r y is not delayed. M o r e o v e r , this q u e r y is t e r m i n a t i n g . T h e t e r m i n a t i o n b e h a v i o u r o f a logic p r o g r a m with d e l a y d e c l a r a t i o n s is r a t h e r subtle. T h e r e are w~rious aspects, s o m e t i m e s u n e x p e c t e d , that o n e h a s to take into a c c o u n t . A t h o r o u g h d i s c u s s i o n o f these aspects is given b y N a i s h in Ref. [19]. F o r i n s t a n c e , o n e w o u l d expect the d e l a y d e c l a r a t i o n DELAY app(xs, ys, zs) t INT~L noncar(xs) V nom~ar(zs) to e n s u r e the t e r m i n a t i o n o f appendl3. H o w e v e r , as i l l u s t r a t e d b y N a i s h , the q u e r y app([a] T], []. T) satisfies the d e l a y d e c l a r a t i o n , but h,i,~ a n infinite d e r i v a t i o n . T h e fact t h a t t e r m i n a t i o n b e h a v i o u r o f logic p r o g r a m s in the c o n t e x t o f d y n a m i c selection rules is r e l y subtle, is reflected also in the v a r i o u s m e t h o d s that h a v e been i n t r o d u c e d , w h i c h are e i t h e r b a s e d on h e u r i s t i c s (e.g. [14,18]), o r are r a t h e r specialized (e.g. [!]). In tiffs p a p e r we try to tackle the p r o b l e m f r o m a different perspective. T h a t is, we d o not c o n s i d e r g e n e r a l c o r o u t i n i n g , w i t h all its p r o b l e m s , b u t c o n s i d e r the class o f d e l a y selection rules w h i c h are " l o c a l " . Local selection rules h a v e b e e n i n t r o d u c e d in Ref. [27]. I n f o r m a l l y , a local selection rule selects a l w a y s in a q u e r y o n e a t o m a m o n g s t t h o s e that h a v e b e e n m o s t recently i n t r o d u c e d in the c o m p u t a t i o n f r o m the b e g i n o f the e x e c u t i o n till that qu,,ry. In Ref. [27], Vieille investigates the relev a n c e o f local selection rules, w h i c h p r o d u c e S L D - t r e e s with a s i m p l e structure, a n d h e n c e s u p p o r t efficient s e a r c h i n g t e c h n i q u e s . M o r e o v e r , local selection rules beh a v e well with respect to s e m a n t i c i n f o r m a t i o n , in the f o l l o w i n g sense, i f a n a t o m o f a q u e r y in a d e r i v a t i o n is .~,Accted, t h e n the d e r i v a t i o n is c o m m i t t e d to resolve t h a t a t o m , a n d o n l y after that a t o m h a s been c o m p l e t e l y resolved, a n o t h e r a t o m o f the q u e r y can be selected. It is this s e m a n t i c p r o p e r t y o f local selection rules, w h i c h allows us to define a simple, yet p o w e r f u l , m e t h o d for p r o v i n g t e r m i n a t i o n o f logic p r o g r a m s with d e l a y d e c l a r a t i o n s . O u r m e t h o d is b a s e d on a n o v e l n o t i o n , called cover, w h i c h relates a b o d y - a t o m o f a c l a u s e with a set o f s u b q u e r i e s . C o v e r s d e s c r i b e s y n t a c t i c a l l y , by m e a n s o f a m u l t i p r o d u c e r o n e - c o n s u m e r r e l a t i o n b e t w e e n b o d y - a t o m s o f a clause, the i n t e r r e l a t i o n s a m o n g the a t o m s o f a clause, w h i c h c a n be c a u s e d b y the d e l a y d e c l a r a t i o n s , a n d wifich m a y affect the t e r m i n a t i o n b e h a v i o u r o f that a t o m . Intuitively, for a n a t o m A, in a clause H ~--~,t~ . . . . . ,4,, the covers o f A~ are t h o s e s u b q u e r i e s o f Ai . . . . . A, w h o s e e x e c u t i o n g u a r a n t e e s t h a t A~ c a n be safely selected (with respect to te,r m i n a tion). In o r d e r to specify s y n t a c t i c a l l y the t e r m i n a t i o n b e h a v i o u r o f a t o m s in the def-

E. i~larchiorL F.. Teusink I J. Logic Pr~granunb~g 39 (1999) 95-124

97

inition o f cover, we use the n o t i o n o f b o u n d e d a t o m , i n t r o d u c e d by Bezem in Ref. [3] (see also Ref. [6]) to s t u d y t e r m i n a t i o n o f logic p r o g r a m s . U s i n g a c o m b i n a t i o n o f s y n t a c t i c a l (covers) a n d s e m a n t i c a l ( m o d e l ) i n f o r m a t i o n , we define the n o t i o n o f d e l a y r e c u r r e n t p r o g r a m , T h i s n o t i o n is a g e n e r a l i z a t i o n to S L D - r e s o l u t i o n with d e l a y selection rules o f the o n e o f r e c u r r e n t p r o g r a m introduced b y Bezem to s t u d y t e r m i n a t i o n o f logic p r o g r a m s with respect to a n a r b i t r a r y selection rule. W e p ro v e t h a t a d e l a y r e c u r r e n t p r o g r a m t e r m i n a t e s for every local selection rule w h i c h selects o n l y b o u n d e d a t o m s . T h u s , this n o t i o n p r o v i d e s a m e t h ~,d for p r o v i n g t e r m i n a t i o n o f a logic p r o g r a m with d e l a y d e c l a r a t i o n s , w h e n the delay deci~:rations i m p l y b o u n d e d n e s s , i~e. if an a t o m satisfies its d e l a y d e c l a r a t i o n t h e n that a t o m is b o u n d e d . A l t e r n a t i v e l y , this m e t h o d c a n be used to find s u i t a b l e d e l a y dech~rations t h a t e n s u r e t e r m i n a t i o n o f goals for a given p r o g r a m , by c h o o s i n g d e l a y declarations which imply boundedness. Tl-~e benefits o f this a p p r o a c h c a n be s u m m a r i z e d as lbllows: T h e p r o p o s e d m e t h od p r o v i d e s a s i m p l e tool to reas o n a b o u t t e r m i n a t i o n o f logic p r o g r a m s w i t h d e l a y d e c l a r a t i o n s . It c a n be also used to t r a n s f o r m a logic p r o g r a m into a t e r m i n a t i n g logic p r o g r a m wi t h d e l a y d e c l a r a t i o n s . M o r e o v e r , this a p p r o a c h p r o v ; d e s a new i n s i g h t on the role o f the selection rules w h e n r e a s o n i n g a b o u t r u n - t i m e p r o p e r t i e s o f logic p r o g r a m s wi t h d e l a y d e c l a r a t i o n s . In p a r t i c u l a r , it s h o w s tha t the class o f local selection rules is n o t o n l y g o o d b ecau s e it s u p p o r t s efficient s e a r c h i n g t e c h n i q u e s , b u t also b e c a u s e it s u p p o r t s s i m p l e tools for p r o v i n g t e r m i n a t i o n . T h e p a p e r is o r g a n i z e d as follows. A f t e r s o m e p r e l i m i n a r i e s on n o t a t i o n a n d term i n o l o g y , in Section 3 we present o u r m e t h o d a n d investigate its r e l a t i o n s h i p w ith the p r o o f m e t h o d s b as ed o n tile n o t i o n s o f r e c u r r e n c y a n d o f a c c e p t a b i l i t y . Section 4 c o n t a i n s the results on the correct~less. In Section 5 the c o m p l e t e n e s s issue is discussed a n d s o m e e x t e n s i o n s o f o u r m e t h o d are c o n s i d e r e d . Section 6 a n a l y z e s the expressiveness o f the d e l a y d e c l a r a t i o n s we use, a n d h o w d e l a y d e c l a r a t i o n s c a n affect the d e c l a r a t i v e semantics. Section 7 deals with related p r o p o s a l s . F i n a l l y , Section 8 c o n c l u d e s with a d i s c u s s i o n on the c o n t e n t s o f the paper. A p r e l i m i n a r y v ers io n o f this p a p e r a p p e a r e d in Ref. [15].

2. Preliminaries

W e shall use the f o l l o w i n g n o t i o n s a n d t e r m i n o l o g y . A l o g i c p r o g r a m , called for brevity p r o g r a m a n d d e n o t e d by P, is a finite set o f ( u n i v e r s a l l y q u a n t i f i e d ) c l a u s e s H ~-- Q, w h e r e Q is a q u e r y , i.e. a s e q u e n c e o f a t o m s , a n d H is a n a t o m . In the following, the letters A, B i n d i c a t e a t o m s a n d c a clause. Recall that ~ v a r i a b l e free e x p r e s s i o n ( a t o m , que ry, etc.) is also calied g r o u n d . F o r a q u e r y O, define a Q - g r o u n d i n s t a n c e o f a c l a u s e c to be a n y i n s t a n c e o f c w h i c h g r o u n d s all the a t o m s o f Q. F i n a l l y , c.a.s, is used as s h o r t h a n d for c o m p u t e d a n s w e r substitution. A s e q u e n c e o f a t o m s will also be d e n o t e d b y A. A s we a re not interested in the o r d e r o f a t o m s , we will s o m e t i m e s treat s e q u e n c e s o f a t o m s as multisets. M o r e o v e r , we will s o m e t i m e s i m p l i c i t l y t r a n s l a t e a s e q u e n c e o f a t o m s into a set o f a t o m s , in order to be a b l e to refer to elements, subsets, unions, etc. In those cases, m u l t i p l i c i t y o f a t o m s will be ignored, i.e. p , p will be t r a n s l a t e d into {p}. W e o n l y do this w h e r e m u l tiplicity o f a t o m s is n o t a n issue.

E. ,~larchiorL F.. Teushlk / J. Logic Programmblg 39 (1999) 95-124

98

W e shall use m u l t i s e t s a n d the m u l t i s e t o r d e r i n g (see Ref. [9]). Recall t h a t a multiset is a u n o r d e r e d (finite) collection in w h i c h the n u m b e r o f o c c u r r e n c e s o f e a c h ele m e n t is significant. W e shall c o n s i d e r here t h e m u l t i s e t o r d e r i n g o n m u l t i s e t s o f n a t u r a l n u m b e r s . F o r m a l l y , a multiset o f n a t u r a l n u m b e r s is a f u n c t i o n f r o m the n a t u r a l n u m b e r s to itself, g i v i n g t h e m u l t i p l i c i t y o f e a c h n a t u r a l n u m b e r . T h e n , given the s t a n d a r d o r d e r < o n n a t u r a l n u m b e r s , the o r d e r i n g <,,,,t o n m u l t i s e t s is defined as the t r a n s i t i v e c l o s u r e o f the r e p l a c e m e n t o f a n a t u r a l n u m b e r with a n y finite n u m b e r ( p o s s i b l y z e r o ) o f n a t u r a l n u m b e r s t h a t a r e s m a l l e r u n d e r < . Since < is wellf o u n d e d , t h e i n d u c e d o r d e r i n g < .....t is also w e l l - f o u n d e d . F o r simplicity we shall o m i t in the sequel the s u b s c r i p t m u l f r o m < .....~. A d e k o ' d e c l a r a t i o n is d e n o t e d as follows: for a p r e d i c a t e p o f a r i t y n a d e l a y decl a r a t i o n h a s the f o r m DELAY p(x I .....

S,,) UNTIL

C o n d ( x l . . . . . x,,)

w h e r e xl . . . . . x,, d e n o t e the a r g u m e n t s o f p, a n d Cond(x~ . . . . . x,,) is a f o r m u l a in s o m e a s s e r t i o n l a n g u a g e . W'c ~huil ~aot fix the s y n t a x o f t h a t a s s e r t i o n l a n g u a g e , as it is n o t r e l e v a n t for the sequel o f the p a p e r . T h e m e a n i n g o f s u c h a d e l a y d e c l a r a t i o n is t h a t in a q u e r y a n a t o m p ( h . . . . . t,,) c a n o n l y be selected if the c o n d i t i o n C o n d ( h . . . . . t,,) is satisfied. W e shall a s s u m e t h a t i f an a t o m is" s e l e c t a b l e then aH its i n s t a n c e s are s e l e c t a b l e too. T h i s c o n d i t i o n is satisfied b y a l m o s t all the logic p r o g r a m m i n g s y s t e m s w h i c h use delay d e c l a r a t i o n s . Its i m p o r t a n c e in the s t u d y o f t e r m i n a t i o n is crucial, a n d all the a p p r o a c h e s we a r e a w a r e of, f o r t h e s t u d y o f p r o p e r t i e s o f logic p r o g r a m s w i t h d e l a y d e c l a r a t i o n s , use this a s s u m p t i o n . T h e d e l a y d e c l a r a t i o n s in a p r o g r a m define a class o f selection rules, called d e l a y s e l e c t i o n rules. A d e l a y s e l e c t i o n rule selects a n a t o m f r o m a q u e r y , a m o n g t h o s e ato m s w h i c h satisfy t h e i r d e l a y d e c l a r a t i o n s . I f the q u e r y is n o n - e m p t y a n d n o s u c h a~o m exists, n o a t o m is selected a n d t h e q u e r y is called d e a d l o c k e d . W h e n u s i n g d,elay d e c l a r a t i o n s , we a r e o n l y i n t e r e s t e d in S L D - d e r i v a t i o n s t h a t a r e c o n s t r u c t e d u s i n g a d e l a y selection rule. W e call these d e r i v a t i o n s d e l a y S L D - d e r i v a tions.

3. Delay recurrent programs T h e a i m o f this p a p e r is to define a class o f p r o g r a m s t h a t b e h a v e nicely with respect to t e r m i n a t i o n . First, we i n t r o d u c e the n o t i o n o f d e l a y r e c u r r e n t p r o g r a m . T h e n , we p r o v e t h a t , for a s u i t a b l e d e l a y d e c l a r a t i o n a n d a b r o a d class o f d e l a y selection rules, e v e r y q u e r y m a d e l a y r e c u r r e n t p r o g r a m h a s o n l y finite d e r i v a t i o n s . T o this e n d , we use t h e n o t i o n s o f level m a p p i n g a n d o f b o u n d e d q u e r y , i n t r o d u c e d in Refs. [3,6]. Definition 3.1 ,,h:rei m a p p i n g ) . Let P be a p r o g r a m . A h'vel m a p p i n g j b r P is a f u n c t i o n I[ : .~P -~ ~ f r o m the H e r b r a n d ba~c ['or P to the set o f n a t u r a l n u m b e r s . T h u s , ] I is o n l y defined f o r g r o u n d a t o m s . H o w e v e r , o n e c a n a s s o c i a t e to a n o n g r o u n d a t o m , the i m a g e o f its set o f g r o u n d i n s t a n c e s w i t h respect to I t: dt f

Ilhtl = (1~'1 ] .4'is a g r o u n d i n s t a n c e o f A } . U s i n g this set. we define the n o t i o n o f b o u n d e d a t o m s a n d queries.

E. ~l,larchiorL F- Teushtk I J. Logic Programmit~g 39 (1999) 95-124

99

Definition 3.2 (boumled querr). A n a t o m A is bounded ( w i t h resnect to I I) if [[A[[ is finite. A q u e r y Q is b o u n d e d if all t h e a t o m s in it a r e b o u n d e d . A b o u n d e d q u e r y Q = A~ . . . . . t,,,, c a n be u s e d to deflate t h e m u l t i s e t del"

i[Q]I-- ~ma-~:l[,4 , Jl . . . . . maxtlA,,I !~ w h e r e marllAll d e n o t e s t h e m a x i m u m o f [ l A It. In t h e sequel, w e sbal! olden r e f e r to I[Q]I a s t h e lerel m a p p i n g o./' Q. T h e i d e a o f u s i n g a levei , n a p p i n g to p r o v e t e r m i n a t i o n is t h a t o n e p r o v e s t h a t . in a d e r i v a t i o n , selected a t o m s a r e a l w a y s b o u n d e d a n d t h a t t h e level m a p p i n g s o f t h e q u e r i e s d e c r e a s e . In this w a y o n e c a n a p p l y the s t a n d a r d a p p r o a c h u s e d in t h e t e r m i n a t i o n c r i t e r i a , w h e r e c o m p u t a t i o n s a r e m a p p e d i n t o d e c r e a s i n g c h a i n s in a well f o u n d e d set ( h e r e t h e m u l t i s e t o f n a t u r a l n u m b e r s ~ , h e n c e t h e y a r e finite. W e c a n use d e l a y d e c l a r a t i o n s to e n s u r e t h a t o n l y b o u n d e d a t o m s a r e selected, i.e. that the delay declarations imply boundedness. Definition 3.3 ( s ~ , delar declaration). A d e l a y d e c l a r a t i o n is sail" with respect to Ii if f o r e v e r y a t o m A , i f . 4 satisfies its d e l a y d e c l a r a t i o n t h e n A is b o u n d e d w i t h r e s p e c t

toil. T h u s u s i n g s a f e d e l a y d e c l a r a t i o n s g u a r a n t e e s t h a t selected a t o m s a r e b o u n d e d . N o w , we p r o v i d e a m e t h o d t h a t e n s u r e s t h a t t h e level m a p p i n g a l s o d e c r e a s e s . F o r this, w e use t h e i n f o r m a t i o n t h a t selected a t o m s a r e b o u n d e d , t o g e t h e r w i t h t h e a d d i t i o n a l i n f o r m a t i o n p r o v i d e d by a m o d e l o f t h e p r o g r a m . In o r d e r f o r a n a t o m to be b o u n d e d , c e r t a i n o t h e r a t o m s t h a t o r i g i n a t e f r o m t h e s a m e b o d y m u s t h a v e b e e n ( p a r t i a l l y ) r e s o l v e d . W e call t h e s e sets o f a t o m s corers. A c o n s t r u c t i v e d e f i n i t i o n o f t h e s e sets c a n be g i v e n as follows. F i r s t , we d e f i n e direct covers. I n f o r m a l l y , a d i r e c t c o v e r o f a n a t o m B in a q u e r y is a s u b s e t L" o f t h a t q u e r y , s u c h t h a t f o r s o m e i n s t a n t i a t i o n 0 o f t h e v a r i a b l e s in C, BO is b o u n d ed. T h i s m e a n s t h a t C is a p o t e n t i a l a c t i v a t o r o f B: i f t h e r e s o l u t i o n o f ~" i n s t a n t i a t e s its v a r i a b l e s w i t h a s u b s t i t u t i o n t h a t is a n i n s t a n c e o f 0 t h e n B b e c o m e s selectable. T h e n t h e i d e a is to collect all t h e a t o m s t h a t a r e o b t a i n e d b y a p p l y i n g this o p e r a t i o n to t h e a t o m s o f t~, a n d i t e r a t i v e l y to all t h e o t h e r a t o m s i n t r o d u c e d . T h e final set o f a t o m s will r e p r e s e n t t h e p o t e n t i a l a c t i v a t o r s o f B t h a t a r e c o n t a i n e d in t h a t c l a u s e . T h i s set will be filtered u s i n g a m o d e l o f t h e c l a u s e , a n d it will be u s e d to d e f i n e a c o n d i t i o n a n a l o g o u s to t h e o n e u s e d in t h e o r i g i n a l d e f i n i t i o n o f recu rreney. O b s e r v e t h a t , in o r d e r f o r this p r o c e d u r e to t e r m i n a t e , it is sufficient t h a t e v e r y step o f the p r o c e d u r e does not introduce a t o m s a l r e a d y considered. W e i n c o r p o r a t e this r e q u i r e m e n t in t h e d e f i n i t i o n o f ( d i r e c t ) c o v e r . Definition 3.4 (direct coeer). Let [ t be a level m a p p i n g . Let Q b e a q u e r y , let B be a n a t o m in O a n d let ~" be a s u b s e t o f Q s u c h t h a t B ¢ C. W e s a y t h a t C is a direct corer Jbr B with respect to Q and I i, if t h e r e exists a s u b s t i t u t i o n 0 s u c h t h a t OO is b o u n d e d w i t h r e s p e c t to I i a n d Dora(O) C Var(~'). Let H be a n a t o m . W e say t h a t E" is a direct cover f o r B with respect to H .--- Q a n d ] ], if t h e r e exists a s u b s t i t u t i o n 0 s u c h t h a t BO

E. ~larchiori, F. Teuaink I J. Logic Programm#lg 39 (1999) 95-124

100

is b o u n d e d w i t h respect to [ I a n d Dora(O) C_ Vat(H, C). F i n a l l y , a d i r e c t c o v e r C o f is minimal i f n o p r o p e r s u b s e t o f C" is a d i r e c t c o v e r for B. O n e s h o u l d n o t e t h a t a b o d y a t o m B c a n h a v e zero, o n e , o r m o r e ( m i n i m a l ) d i r e c t covers.

F o r i n s t a n c e . w h e n , for B t,~ b e c o m e b o u n d e d , it is n e c e s s a r y to i n s t a n t i a t e a varia b l e o f B w h i c l i d o e s n o t occ,ar a n y w h e r e else in t h e c l a u s e , B will h a v e n o d i r e c t c o v ers. T h i s is i l l u s t r a t e d b y the f o l l o w i n g s i m p l e e x a m p l e . C o n s i d e r t h e c l a u s e p ( x ) ~-- p(y). with delay declaration DELAYp(x) UNTIL list(x) T h e b o d y a t o m P 0 ' ) h a s n o d i r e c t c o v e r s w i t h respect to the level t h a t i f t is a list t h e n tp(t)[ is t h e n u m b e r o f its e l e m e n t s , o t h e r w i s e O n t h e o t h e r h a n d , i f B is b o u n d e d w h e n e v e r H is b o u n d e d , t h e n o n e m i n i m a l d i r e c t c o v e r , n a m e l y t h e e m p t y set. T h i s is i l l u s t r a t e d

mapping [ [ such it is zero. t h e r e exists o n l y b y the c l a u s e

app( [xlxs], ~ , [x]z's]) ~-- app(xs, ys, zs). with delay declaration DELAY app(xs,)~', :s) UNT,L Iist(xs) T h e b o d y a t o m app(xs,ys, zs) h a s the e m p t y cover with respect to the level m a p p i n g [ ! such that if r is a list then lapp(r, s, t) l is the n u m b e r o f its elements, otherwise it is zero. O b s e r v e t h a t t h e d i r e c t c o v e r s o f a n a t o m d e p e n d o n t h e level m a p p i n g o n e chooses. For instance, consider again the clause

p(x) ~ p ( y ) . a n d t h e t w o level m a p p i n g s [ [i a n d [ [2 s u c h t h a t ]p(s)]l is e q u a l to its l e n g t h i f s is a list. o t h e r w i s e it is e q u a l to O; a n d ho(s)[_, is e q u a l to 0 t b r e v e r y s. Then PO') h a s n o d i r e c t c o v e r w i t h r e s p e c t to [ [i, w h i l e it h a s 0 as d i r e c t c o v e r w i t h respect to [ [,. F i n a l l y , we w o u l d like to e m p h a s i z e t h a t d i r e c t c o v e r s c a n b e "cyclic', in t h e s e n s e t h a t t w o a t o m s c a n h a v e e a c h o t h e r in t h e i r d i r e c t c o v e r s . T a k e f o r i n s t a n c e t h e query p ( x ) , q(x) a n d a level m a p p i n g ] I in w h i c h b o u n d e d n e s s o f p ( x ) a n d q(x) d e p e n d o n x. T h e n , p(x) will h a v e d i r e c t c o v e r {q(x)} a n d q(x) will h a v e d i r e c t c o v e r {p(x)}. In t h e d e f i n i t i o n o f c o v e r , w e t a k e a k i n d o f transitive closure o f the direct cover relation: w'e c a n n o t use the s t a n d a r d transitive closure because a direct cover is not in general a cover, a n d because the direct cover relation is between a n a t o m a n d a set o f atoms. F o r m a l l y . we h a v e t h e f o l l o w i n g d e f i n i t i o n o f c o v e r . D e f i n i t i o n 3.5 (cover). Let Q b e a q u e r y a n d let I I b e a level m a p p i n g . Let Cot,ers(Q) b e a set o f p a i r s in Q x .~(Q) i n d u c t i v e l y d e f i n e d a s follows: C o n s i d e r t h e set C6(Q) o f p a i r s in Q x . ~ ( Q ) s a t i s f y i n g t h e t w o f o l l o w i n g c o n d i t i o n s : 1. for e v e r y a t o m B, i f B h a s t h e e m p t y set as m i n i m a l d i r e c t c o v e r t h e n (B, 0) ~ c6~(Q); 2. for e v e r y a t o m B ~ n d q u e r y C, i f B ~ t~, a n d C" is o f t h e f o r m

{C~ . . . . . c~} ob~ u . . . o b ~ s u c h t h a t {Ci . . . . . ~"~.} is a m i n i m a l d i r e c t c o v e r o f B in Q, a n d (Ci, Di} E ~6(Q) f o r i E [1..k], t h e n (B, C) E Cg(Q).

I:~ .lhtrchiori. F. Tcu.~'#tk / J. Logic P r o g r w m n i n g 30 ( t¢~q~; > ~S-.t24

101

C o v e r s ( Q ) is o b t a i n e d f r o m cg (O) b y d e l e t i n g e a c h e l e m e n t o f t h e f o r m (B. C') if t h e r e exists a n o t h e r e l e m e n t o f <¢;(Q) o t the f o r m (B, C") s u c h t h a t C' C C'. L e t B be a n a t o m in Q a n d let C be a s u b s e t o f Q. W e s a y t h a t C is a c o r e r o . f B :,'ith respect to Q a n d [ I if (B, C') is a n e l e m e n t o f Corer,~(Q). T h e n o t i o n o f c o v e r o f a n a t o m w i t h r e s p e c t t o a c l a u s e is d e f i n e d a n a l o g o u s l y . Let us i l l u s t r a t e t h e ~eneral i a t u i t i o n b e h i n d t h e n o t i o n o f c o v e r . C o n s i d e r a ~,en e r i c c l a u s e o f t h e p r o g r a m , s a y c -----H ~ O. T h e p r o p e r t i e s w e a r e i n t e r e s t e d in. rel a t e e a c h a t o m .4 o f t h e b o d y Q o f <" w i t h a set o f s u b q u e r i e s (7 o f Q. E a c h o f t h e s e s u b q u e r i e s ~" ~s s u p p o s e d to p r o d u c e s u i t a b l e i n l b r m a t i o n w h i c h g u a r a n t e e s t h a t a c o n d i t i o n o n ,4. n a m e l y b o u n d e d n e s s , is satisfied. In o t h e r w o r d s , t h e :,elation bet w e e n a t o m s a n d s u b q u e r i e s o f Q is a m u l t i - p r o d u c e r s o n e - c o n s u m e r rel::fion. S o f a r . f o r t h e stud), o f p r o p e r t i e s o f logic p r o g r a m s , like m o d e - a n a l y s i s a n d t e r m i n a t i o n [22,21]. o n l y a o n e - p r o d u c e r o n e - c o n s u m e r r e l a t i o n w a s used. I f a s u b q u e r y C is a p r o d u c e r o f a n a t o m .4, t h e n t~ is a cove_, o f .4. T h e constr:~cl i o n o f a c o v e r is i n c r e m e n t a l : first, o n e h a s to find a s u b q u e r y , s a y b , w h i c h is directly r e l a t e d w i t h .4 w i t h r e s p e c t to t h e p r o p e r t y o f interest, t h a t is b h a s to b e a direct c o v e r o f A. T h e n . f o r ever): 0 t o m B o f / ) . a c o v e r o f B h a s to be a d d e d to t h e set s o f a r c o n s t r u c t e d . W e c o n s i d e r t h e s i t u a t i o n in w h i c h t h e r e c a n t;~- m o r e t h a n one direct cover. Then. an atom can have many covers. The covers of an atom A can be g r a p h i c a l l y r e p r e s e n t e d b y m e a n s o f a n A N D - O R tree. in t h e style o f N i l s s o n [20]. T h e r o o t o f t h e tree is .4. N o d e s a r e l a b e l e d by sets o f a t o m s . N o d e s l a b e l e d by sets c o n t a i n i n g m o r e t h a n o n e e l e m e n t h a v e sets o f s u c c e s s o r n o d e s e a c h l a b e l e d b y o n e o f t h e e l e m e n t s . T h e s e s u c c e s s o r n o d e s a r e c a l l e d A N D n o d e s b e c a u s e in o r d e r to c o m p u t e a c o v e r o f .4. o n e c o v e r o f e a c h o f t h e e l e m e n t s o f t h e set o f a t o m s h a s to be c o m p u t e d . N o d e s l a b e l e d by a set c o n t a i n i n g o n e e l e m e n t , h a v e sets o f s u c c e s s o r s e a c h l a b e l e d b y o n e d i r e c t c o v e r o f t h a t a t o m . F o r a n o d e n o f a n A N D - O R tree 7". a p a t h T' s t a r t i n g a t n is a s u b t r e e o f T s u c h t h a t : i f n is in T' a n d n is n o t a l e a f o f T t h e n : ~a) i f n is a~l A N D - n o d e t h e n all t h e s u c c e s s o r s o f n in T a r e in T': o t h e r w i s e (n is tin O R - n o d e ) e::~tctli¢ o n e s u c c e s s o r o f n in T is in T'. T h e n . t h e c o v e r s o f ,4 a r e t h e sets o f n o d e s o f t h o s e path.,~ in t h e A N D - O R t r e e h a v i n g l e a v e n o d e s e q u a l to 0. F o r instance, consider the clause:

H --- ,41. A2..43. A4. S u p p o s e t h a t t h e d i r e c t c o v e r s o f this c l a u s e a r e g i v e n in Fig. 1. T h e n t h e c o v e r s o f a n a t o m . s a y .4 I. c a n be c o m p u t e d u s i n g t h e A N D - O R tree l b r A l o f Fig. [. W e use here Nilsson notation, and indicatc an AND-node by a circular mark linking their i n c o m i n g arcs. In t h e p i c t u r e d tree t h e r e a r e t w o p a t h s , y i e l d i n g t h e c o l l e c t i o n s o f n o d e s c o n s i s t i n g o f t h e sets I A 2 . A 3 } . a n d 1.42. A 3 . . 4 4 ~ . N o t i c e t h a t all a t o m s o f t h e first p a t h o c c u r a l s o in t h e s e c o n d o n e . Since w e a r e i n t e r e s t e d in t h e m i n i m a l set o f p r o d u c e r s , we c a n d i s c a r d t h e s e c o n d p a t h . T h u s . we c o n s i d e r {A2. A3} to be the o n l y c o v e r o f ,4 i. O b s e r v e t h a t t h e n o t i o n o f c o v e r c a n be used f o r d e f i n i n g p r o o f m e t h o d s f o r p r o p erties o t h e r t h a n t e r m i n a t i o n . F o r i n s t a n c e , in R e l : [16] c o v e r s a r e used f o r d e f i n i n g a m e t h o d f o r p r o v i n g d e a d l o c k f r e e d o m o f logic p r o g r a m s w i t h d e l a y d e c l a r a t i o n s . T h e o n l y d i f f e r e n c e is t h e d e f i n i t i o n o f d i r e c t c o v e r , w h e r e t h e p r o p e r t y o f b o u n d e d ness is r e p l a c e d b y a p r o p e r t y u s e f u l f o r p r o v i n g d e a d l o c k f r e e d o m .

17. Marchiori. F. Teusink I Z Logic P r o g r a m m i n g 39 ( 1 9 9 9 ) 95-124

102

~ m

3 4

d i r e c t cover {A2, A3}, {A2, A4} {Aa}

0

Fig. t. D i r e c t c o v e r s o f , 4 t

.....

A4

a n d A N D - O R tree f o r A,.

T h e f~2!owin,Z simple e x a m p l e illustrates h o w covers are built. E x a m p l e 3.1. C o n s i d e r the q u e r y Q = p ( x ) , q ( x ) , r(x). A s s u m e that the usual level m a p p i n g 1 [ is given such t h a t [p(t)[ is equal to the n u m b e r o f elements o f t if t is a list, an-~" [p(t)l is zero otherwise; Iq(t)[ is equal to [Pf;)I, while ir(t)[ is zero, for every t. First, we build the set r~(Q). I. {r(x), 0) is in ~,(Q) since 0 is a direct c o v e r -~f r(x). 2. By p o i n t 2 o f Definition 3.5 we have t h a t (q(r), {r(x)}) is in r(, (Q). In fact, q(x) is n o t in {r(x)}. M o r e o v e r , {r(x) } is o f the form {r(x) } U 0 w h e r e {,'(x) } is a m i n i m a l direct c o v e r o f q(x) a n d (r(x), ~) is in r~(Q). O n e can s h o w in a similar w a y that (p(x), {r(x)}) is in ~ ( Q ) . 3. By p o i n t 2 o f Definition 3.5 we have t h a t (p(x), { q ( x ) , r ( x ) } ) is in r~(Q). In fact, p(x) is n o t in { q ( x ) , r ( x ) } . M o r e o v e r , {q(x),,'(x)} is o f the f o r m {q(x)} u {r(x)} w h e r e {q(x)} is a m i n i m a l direct cover o f p ( x ) a n d (q(x), {r(x)}) is in reT(Q). A n a l o g o u s l y , o n e c a n p r o v e t h a t (q(x), { p ( x ) , r ( x ) } ) is in r~(Q). N o w Covers(Q) is o b t a i n e d f r o m r6-(Q) by deleting the two pairs (q(x), {p(x), r(x)} ) a n d (p(x), { q ( x ) , r ( x ) } ) , hence ~ ( Q ) -- {(r(x), ~), (q(x), {r(x)}), (p(x), {r(x)})}. C o v e r s satisfy the following useful properties, t h a t will be needed in t t~e i~r~of o f the results given in the sequel. L e m m a 3.1. Let C be a covet" "~,["A with respect ~o Q and 1[. 7hen the jollowing properties hoM: 1. For every atom D C C" there exists a cover b o l D such that b c C'. 2. A subset o f CO is a cover o f AO with respect to QO, ./'or every substitution O. Proof. W e p r o v e p r o p e r t y 1 by i n d u c t i o n on the n u m b e r o f e l e m e n t s o f C?. T h e result follows trivially if C is e m p t y . Otherwise, let B be an a t o m o f C. By definition o f c o v e r we h a v e t h a t C is o f the f o r m {Ci . . . . ,Ck} t3/)! t _ J . . . u / ) k such t h a t { C ! , . . . ,Ck} is a m i n i m a l direct c o v e r o f A, a n d (C~,/),) ~ c6,(Q) for i E [i..k]_ T h e n the c o n c l u s i o n follows directly if B = C~ for s o m e i. O t h e r w i s e B is in s o m e D~. W e c a n a p p l y the i n d u c t i v e h y p o t h e s i s to (Ci, b~) ap.d c o n c l u d e t h a t B has a c o v e r /~ which is a p r o p e r subset o f h i , hence B C t~. P r o p e r t y 2 is a direct c o n s e q u e n c e o f the definition o f direct c o v e r a n d o f the fact t h a t in the definition o f c o v e r m i n i m a l direct covers are considered. []

E. Marchiori, F. Teusink ! J. Logic Programming 39 (1999) 95-124

103

O b s e r v e t h a t t h e first p r o p e r t y i m p l i e s t h a t i f ,4 h a s a n o n - e m p t y c o v e r C" tLc~ every a t o m o f C" h a s at least o n e c o v e r w h i c h d o e s n o t c o n t a i n .4. M o r e o v e r , t h e first p r o p e r t y i m p l i e s t h a t i f ,4 h a s a n o n - e m p t y c o v e r C t h e n t h e r e exists a n a t o m B in w h i c h h a s t h e e m p t y cover. U s i n g t h e n o t i o n o f c o v e r , we c a n d e f i n e t h e class o f delay recurrent p r o g r a m s . Ess e n t i a l l y , the i d e a is t h e o n e t h a t h a s b e e n u s e d in Ref. [2] in o r d e r t o e x t e n d t h e n o t i o n o f r e c u r r e n c y to P r o l o g p r o g r a m s , w h e r e t h e l e f t - t o - r i g h t s e l e c t i o n rule is used: t h e r e a n i n t e r p r e t a t i o n I ( n o t n e c e s s a r i l y H e r b r a n d ) , t o g e t h e r w i t h t h e l e f t m o s t select i o n rule a r e used, for p r o v i d i n g the e x t r a i n f o r m a t i o n t h a t e n a b l e s o n e to p r o v e term i n a t i o n . W i t h d e l a y r e c u r r e n t p r o g r a m s , we d o n o t rely o n the l e f t m o s t s e l e c t i o n rule. I n s t e a d , we a s s u m e t h a t b e f o r e a n a t o m is selected, o n e o f its c o v e r s h a s b e c o m e bound. D e f i n i t i o n 3.6 (delay recurrent progr,:,~O. Let I [ b e a level m a p p i n g a n d I a m o d e l o f the p r o g r a m P. 1. A c l a u s e c : H ~-- Q o f P is delay recurrent ,vith respect to [ [ a n d I i f for e v e r y a t o m B in Q, for e v e r y c o v e r c" for B, a n d for e v e r y (H, C ) - g r o u n d i n s t a n c e HO ~-- QO o f c s u c h t h a t I ~ C?0, we h a v e t h a t

[HOl > I[B0]l. 2. P is d e l a y r e c u r r e n t w i t h respect to [ [ a n d I i f e a c h o f its c l a u s e s is d e l a y r e c u r r e n t w i t h r e s p e c t to ]l a n d / . O b s e r v e t h a t a n e q u i v a l e n t d e f i n i t i o n c a n b e o b t a i n e d b y considerii~g in p o i n t I. ' g r o u n d i n s t a n c e s o f c" i n s t e a d o f ' ( H , C ) - g r o u n d i n s t a n c e s o f c'. H o w e v e r , w e h a v e p r e f e r to a d o p t t h e l a t t e r f o r m u l a t i o n in o r d e r to e m p h a s i z e the use o f c o v e r s in t h e instantiation process of the body atoms. D e f i n i t i o n 3.6 is u s e d to p r o v e t e r m i n a t i o n o f p r o g r a m s f o r e a c h q u e r y w i t h respect to a specific class o f s e l e c t i o n rules, u n d e r t h e a s s u m p t i o n t h a t t h e d e l a y decl a r a t i o n s i m p l y b o u n d e d n e s s . I n t u i t i v e l y , t h e w a y t h e m o d e l I is used in t h e a b o v e d e f i n i t i o n restricts the k i n d o f s e l e c t i o n r u l e s to t h o s e w h e r e o n l y a t o m s a m o n g s t t h o s e t h a t h a v e b e e n m o r e r e c e n t l y i n t r o d u c e d c a n b e selected. T h i s p h e n o m e n o n y i e l d s the results c o n t a i n e d in the f o l l o w i n g section. W e c o n c l u d e t h i s s e c t i o n w i t h a d i s c u s s i o n o n t h e r e l a t i o n s h i p b e t w e e n the n o t i o n o f d e l a y r e c u r r e n c y a n d the o n e s o f r e c u r r e n c y (cf. Ref. [4]) a n d a c c e p t a b i l i t y (cf. Ref. [2]). It is e a s y to s h o w t h a t the n o t i o n o f d e l a y r e c u r r e n t p r o g r a m is a g e n e r a l i z a t i o n o f t h e n o t i o n o f recurrent program. R e c a l l t h a t a p r o g r a m P is r e c u r r e n t i f f o r s o m e level m a p p i n g l I, e v e r y g r o u n d i n s t a n c e H ~ A i . . . . . A,, o f a c l a u s e o f P ~atisfies t h e test

ItIf > ra,I for e v e r y i E [I..n]. T h e n we h a v e the f o l l o w i n g result. L e m m a 3.2. l f P is recurrent with respect to [ I then P is delay recurrent with respect to II a n d L f o r any m o d e l I o f P. Proof. C o n s i d e r t h e test JH'[ > [[A'][. W e h a v e t h a t IH'[ > [A"[ for e v e r y g r o u n d i n s t a n c e A" o f A', b e c a u s e P is r e c u r r e n t w i t h respect to [ ]. H e n c e tH'[ > ][A']I. []

E. ~lhtrchhn'i. F. Teushtk I .L Logh" Programmh;g 39 ¢ 1999) 95--124

104

W e t u r n n o w to t h e s t u d y o f the r e l a t i o n s h i p b e t w e e n d e l a y r e c u r r e n c y a n d acc c p t a b i l i t y . R e c a l l t h a t a p r o g r a r n P is a c c e p t a b l e if t h e r e ex:st a level m a p p i n g t [ a n d a m o d e l I f o r P s u c h t h a t e a c h g r o u n d i n s t a n c e H ~-- At . . . . . A, o f a c l a u s e o f P satisfies t h e test t l t l > 1A,I for e v e r y i E [! ..n] s u c h t h a t I ~ A z. . . . . . 4,_1. A c c e p t a b i l i t y e n s u r e s t e r m i n a t i o n w i t h r e s p e c t to t h e P r o l o g left to r i g h t selection rule for all b o u n d e d q u e r i e s . T h e d e f i n i t i o n o f d e l a y r e c u r r e n c y is r a t h e r s i m i l a r to t h e o n e o f a c c e p t a b i l i t y , b u t n o t e q u i v a l e n t , as i l ! u s t r a t e d in the l b l l o w i n g e x a m p l e .

E x a m p l e 3.2. C o n s i d e r o n c e a g a i n the p r o g r a m P: ptx) ~- ply).

C l e a r l y , P is n o t a c c e p t a b l e . H o w e v e r . P is d e l a y r e c u r r e n t w i t h respect to t h e level m a p p i n g 1 ] s u c h t h a t tp(s)[ is t h e n u m b e r o f e l e m e n t s o r s i f s is a list, a n d it is z e r o o t h e r w i s e . In fact. we h a v e a l r e a d y seen t h a t p l y ) h a s n o covc,-s, h e n c e t h e c o n d i t i o n o f d e l a y r e c u r r e n c y is v a c u o t l s l y satisfied. I n d e e d , if o n e c o n s i d e r s a d e l a y d e c l a r a t i o n w h i c h is s a f e w i t h r e s p e c t to [ [ t h e n e v e r y q u e r y will h a v e o n l y finite d e r i v a t i o n s t e n d ing e i t h e r in d e a d l o c k o r in failure). N e v e r t h e l e s s , the r e l a t i o n s h i p b e t w e e n d e l a y r e c u r r e n c y a n d a c c e p t a b i l i t y is r a t h e r s t r o n g , in the f o l l o w i n g sense. I f e v e r y a t o m o f a p r o g r a m P h a s at least o n e c o v e r t h e n p r o v i n g d e l a y r e c u r r e n c y o f P a m o u n t s to p r o v e a c c e p t a b i l i t y c f t h e p r o g r a m o b t a i n e d by a p p l y i n g a s u i t a b l e t r a n s f o r n a a t i o n to t h e c l a u s e s o f P. T h i s is p o s s i b l e because covers d e t e r m i n e static reorderings of the bod3 a t o m s o f a clause, that can be a p p l i e d to t h e b o d i e s o f t h e p r o g r a m c l a u s e s in o r d e r to o b t a i n a n a c c e p t a b l e p r o g r a m . Let us tirst e x p l a i n this r e l a t i o n s h i p i n f o r m a l l y by m e a n s o f a n e x a m p l e . E x a m p l e 3.3. C o n s i d e r

the p r o g r a m

p( [.v t.r.~]. [x I:..,-] ) ~ q 0~,. :., ). ,-(--,,). p(.,-.,-, y.,.). p((].[]l. q([xlxs]. [.r!vs]) - - qCx-s.ys).

q([].f]). ,.([.rlx.,.])

-

,.(x.,.).

a n d a s s u m e t h a t . w i t h r e s p e c t to a g i v e n level m a p p i n g 1 ], w e c o v e r {q(ys,2s).p(xs,.vs)}. q(3~-.z.s) h a s the c o v e r {p(.x-s, ys)}, e m p t y c o v e r . M o r e o v e r . a s s u m e that the p r o g r a m is d e l a y to [t a n d a m o d e l I. T h e n it is n o t difficult to c h e c k that the

p( Ix !.,.,1. [.,Lv.,] ) -- t,(x.,, v.,. ~. q (y.,.. =s ). ,.(-.,- ). p(I].[]). q([., Ix.,-]. [.,l.r.,l) ~- q(-,-,. ), ). q([].[]~"([-','l-~-"l)

"- ,'(.r.,').

is a c c e p t a b l e w i t h r e s p e c t to [l a n d I.

h a v e that r(zs) h a s t h e a n d p ( x s , y s ) h a s the r e c u r r e n t w i t h respect program

E. ,~larchiori. E Teusink i J. Logic Programming 39 (1999) 95-124

105

T h u s , if a p r o g r a m P is delay r e c u r r e n t w i t h respect to j J a n d I a n d if every bod-" a t o m h a s at least o n e cover, then we c a n a p p l y a static r e o r d e r i n g to the a t o m s in the bodies o f the clauses o f the p r o g r a m in o r d e r to o b t a i n an a c c e p t a b l e p r o g r a m with respect to I J a n d L In general, if s o m e b o d y a t o m h a s m o r e t h a n o n e cover, one h a s to t a k e into a c c o u n t all the different static r e o r d e r i n g s o f the b o d y a t o m s which a r e i n d u c e d by the covers. F o r m a l l y , we i n t r o d u c e the following p r o g r a m t r a n s f o r m a t i o n . A s s u m e t h a t we are given a p r o g r a m P a n d a level m a p p i n g J t for P. First, we define the n o t i o n o f safe o r d e r i n g o f a q u e r y Q. Definition 3.7. Let tr be a p e r m u t a t i o n o f [1 ..n]. W e say t h a t (2' = A~I~ . . . . . A¢,, 0 is a safe reordering o f Q (with respect to j J) if for every i E [l..n] we h a v e t h a t A ~ I . . . . . A,m-i~ c o n t a i n s a c o v e r ofAolj ! with respect to J JFirst o f all, we p r o v e t h a t covers can be safely r e o r d e r e d . .Lemma 3.3. L e t C be a corer o f Q. Thep! there e_vists a safe reordering C~ o f C. Proof. W e p r o v e the claim by i n d u c t i o n o f the n u m b e r n o f a t o m s o f C. T h e result is trivial if n = 0, i.e.. if C is e m p t y . O t h e r w i s e (n > 0) let Co be a n a t o m e f C. By P r o p o s i t i o n 3. ! ( p r o p e r t y 1) applied to Co it follows t h a t Co has a c o v e r ~0 C ~'. By the inductive h y p o t h e s i s a p p l i e d to ~.0 we h a v e t h a t there exists a safe r e o r d e r i n g o f ~,0. N o w if (7 \ C-~* is not e m p t y , then we c o n s i d e r a~i a t o m CI in C \ (7°. A g a i n , by P r o p o s i t i o n 3.1 ( p r o p e r t y 1) a p p l i e d to Ci it follows t h a t C~ has a c o v e r C7' c (7. M o r e o v e r . by the i n d u c t i v e h y p o t h e s i s a p p l i e d to C" we h a v e t h a t there exists a safe r e o r d e r i n g C', o f C'. Let C~ (C'~) be the q u e r y o b t a i n e d f r o m C' (~7") by deleting the a t o m s t h a t o c c u r a l r e a d y in C-~. T h e n it is e a s y to check t h a t the q u e r y C~, C~ is a safe r e o r d e r i n g o f ~o0. ~ t . By r e p e a t i n g the a b o v e r e a s o n i n g a n u m b e r k o f times such t h a t ~ t.J . . . u C'~ = one o b t a i n s a safe r e o r d e r i n g ~ . . . . . C~ o f C'. E N o w , s u p p o s e that every b o d y a t o m o f P has at least o n e cover. C o n s i d e r a clause c = p(st . . . . . s,,) ,--- Q o f P, with Q - A i . . . . . A,. I.et Q,~t . . . . . Q,,, be all the safe reorderings o f Q, w h e r e o-~ . . . . . tr~ are the c o r r e s p o n d i n g p e r m u t a t i o n s . C o n s i d e r the t r a n s f o r m a t i o n F which replaces the clause c by the following clauses: p(~

.....

p(s, .....

~.,) .-- Q,~,.

s.,) ~

Q~,.

T h e t r a n s f o r m a t i o n F applie,', to each clause o f P yields a new p r o g r a m , say F ( P ) . T h e n we b a r e the following result. T h e o r e m 3.1. S u p p o s e that every b o d y a t o m o f P has at least one cover with respect to J ]- Then P is d e l a y recurrent with respect to i J a n d ! ( l a n d only i f F ( P ) is a~x'eptable with respect to j [ a n d I.

106

E. hlarchiorL F. T,,usink I J. Logic P r o g r a m m i n g 39 ( 1 9 9 9 ) 95-124

Proof. S u p p o s e t h a t P is d e l a y r e c u r r e n t w i t h r e s p e c t to I I a n d L C o n s i d e r a c l a u s e . . . . . s , , ) ~--Q,~ o f F ( P ) . w i t h Q ~ = A ~ I I 1. . . . . A,~I~ ). C o n s i d e r a g r o u n d i n s t a n c e p ( s l . . . . . . ~,,,)0 ~-- Q~O o f c: s u p p o s e t h a t ~: L (A~il~ . . . . . A~t,__11)O. W e h a v e to s h o w t h a t Ip(st . . . . . s,,,)OI > I A ~ 0 1 . Since Q~ is a safe r e o r d e r i n g o f Q = Ai . . . . . 4 , . t h e n A~tl) . . . . . A,t~_I t c o n t a i n s a c o v e r , s a y C" o f A ~ . C o n s i d e r t h e specific ( H . C ' ) - g r o u n d i n s t a n c e p(s~ . . . . . s , , ) O .-- Q ~ o f p(s~ . . . . . s,,,) - - Q s u c h t h a t QO is a n i n s t a n c e o f Q 6 . T h e n CO = ~'6 h e n c e 1 ~ t~6. S o by t h e h y p o t h e s i s t h a t y' is d e l a y r e c u r r e n t we h a v e t h a t lp(s~ . . . . . s,,,)Ot > I[A~,¢~)~]I, h e n c e tp(s~ . . . . . s,,)OI > c=p(s~

C o n v e r s e l y . s u p p o s e t h a t F ( P ) is a c c e p t a b l e w i t h respect to I I a n d I. C o n s i d e r a c l a u s e c =- p ( s l . . . . . s,,,) ~-- Q o f p . w i t h Q = Ai . . . . . A,,. Le, t:" be a c o v e r o f a b o d y a t o m ~i. L e t p ( s l . . . . . s,,,)6 ~-- Q 6 be a ( p ( s l . . . . . s,,,). C ) - g r o u n . t i n s t a n c e o f c a n d ass u m e t h a t I ~ ~'6. W e h a v e to s h o w t h a t Ip(sl . . . . . s,,,)6[ > I[A:5]I. By P r o p o s i t i o n 3.3 o n e c a n find a s a f e r e o r d e r i n g , s a y ~'~.. o f t~. T h e n C,..4~ is a safe reordering o f C.A/. N o w if Q \ ~7 is n o t e m p t y , t h e n we c o n s i d e r a n a t o m D~ in t2 \ ~'. By h y p o t h e s i s Di h a s at least o n e c o v e r , s a y /)'. A g a i n . b~ P r o p o s i t i o n 3.3 o n e c a n find a safe re+' o f O'. Let b L (/3~) be t h e q u e r y o b t a i n e d f r o m /~Y ( D ' ) b y d e l e t i n g t h e o r d e r i n g D~ a t o m s t h a t o c c u r a l r e a d y in C. T h e n it is e a s y to c h e c k t h a t t h e q u e r y C~. Ai. C~1, . D~ is a s a f e r e o r d e r i n g o f ~'.A~. ~'~. D i . By r e p e a t i n g the a b o v e reasoning a number k o f t i m e s such that ( C. Ai ) tJ ( CI . DI ) U . . . U ( C k . D~ ) = ~_ one obtains a safe reordering, say Q~. = t~',~.Aj. b o f Q. M o r e o v e r . / ~ C~6 b e c a u s e I ~ C6. T h e n by t h e h y p o t h e s i s t h a t F ( P ) is a c c e p t a b l e we h a v e t h a t Ip(sr . . . . . . ~,,,)61 > 1.4,01. f o r e v e r y g r o u n d i n s t a n c e A,0 o f A / L H e n c e [p(sl . . . . . . v,,,)61 > 1[.4,6]]. [] T h e a b o v e r e s u l t s h o w s t h a t in m o s t c a s e s o n e c a n use t h e n o t i o n o f a c c e p t a b i l ity t o g e t h e r w i t h a p r o g r a m t r a n s f o r m a t i o n in o r d e r to p r o v e t h a t a p r o g r a m is d e l a y r e c u r r e n t . H o w e v e r . t h e use o f c o v e r s h-:s a n u m b e r o f a d v a n t a g e s o v e r this a l t e r n a t i v e m e t h o d o l o g y . F i r s t o f all. w i t h c - v e r s we h a v e a s y s t e m a t i c a p p r o a c h f o r f i n d i n g s t a t i c r e o r d e r i n g s t h a t e n s u r e t e r m i n a t i o n , w h i c h is m o r e efficient t h a n s i m p l y c h e c k i n g all p e r m u t a t i o n s o f b o d y a t o m s . S e c o n d l y . o u r m e t h o d d o e s n o t i m p o s e a n o r d e r o n b o d y a t o m s , i f o n e fixes t h e o r d e r o f b o d y a t o m s in o r d e r to e n s u r e t e r m i n a t i o n , o n e l o o s e s t h e f r e e d o m to let a c o m p i l e r o r o p t i m l z e r fix s o m e o r d e r . I n s t e a d . t h e c o v e r s c o m p u t e d in o u r m e t h o d f o r m a c o n c i s e r c p c e s e n ration of all reorderings of body atoms thai ensure termination. This information c a n be fed to a c o m p i l e r o r o p t i m i z e r , as a c o n s t r a i n t o n t h e o r d e r i n g s o f b o d i e s it may choose.

4. Results In this s e c t i o n we c h a r a c t e r i z e o p e r a t i o n a l l y h a v i o r ) : h e class o f d e l a y r e c u r r e n t p r o g r a m s . Knox~.i~g t h a t a selected a t o m is b o u n d e d is its c o v e r s h a s b e e n p a r t i a l l y resolved. H o w e v e r . in ttae d e f i n i t i o n o f d e l a y r e c u r r e n c y is s t r o n g e r ,

( w i t h respect to t h e i r t e r m i n a t i o n beuseful, b e c a u s e it implies t h a t o n e o f t h e s e m a n t i c i n f o r m a t i o n t h a t we use b e c a u s e it i m p l i e s t h a t a c o v e r o f t h e

E. ~larchiori. F. Teushzk I J. Logic Programming 39 [ 1999) 95-124

107

selected a t o m h a s b e e n r e s o l v e d c o m p l e t e l y , In o r d e r to be a b l e to e n s u r e this o p e r a t i o n a l l y , we h a v e to use a local selection rule. L o c a l selection rules h a v e b e e n e x t e n sively s t u d i e d b y Vieille in R e f . [27]. Definition 4,1 (local selection rule). Let Q be a q u e r y in a a t o m s A a n d B, T h e n A is i n t r o d u c e d m o r e recently t h a n step introducing A comes before the SLD-derivation step i n t r o d u c e d m o s t recently, if n o a t o m B is i n t r o d u c e d m o r e A local seh, ction rule is a selection rule t h a t o n l y selects atoms.

d e r i v a t i o n Pl, c o n t a i n i n g B. if t h e S L D - d e r i v a t i o n i n t r o d u c i n g B, in q. A is r e c e n t l y t h a n A, most recently introduced

N o t e t h a t , if in a q u e r y Q n o n e o f t h e m o s t r e c e n t l y i n t r o d u c e d a t o m s satisfies its d e l a y d e c l a r a t i o n , t h e n a local d e l a y selection rule s h o u l d d e a d l o c k o n Q, e v e n if t h e query contains some other a t o m s that satisfy their delay declaration. Thus the gene r a l i z a t i o n o f t h e P r o l o g selection rule to p r o g r a m s w i t h d e l a y d e c l a r a t i o n s u s e d f o r i n s t a n c e in G/Sdei, w h e r e t h e l e f t m o s t non-0ei;~.y~d a t o m o f a q u e r y is selected, is n o t in g e n e r a l a local d e l a y selection rule. U s i n g local selection rules, we c a n d e f i n e t h e f o l l o w i n g n o t i o n o f t e r m i n a t i o n . Definition 4.2 (local d e h t v t e r m h u t t i o n ) . A q u e r y Q is h w a l d e k t v t e r m i m l t h t g if e v e r y d e l a y S L D - d e r i v a t i o n f o r Q w h i c h uses a local selection rule "s finite. I f e v e r y q u e r y Q is local ~h'ktv t e r m h u t t # t g ther~ P is h~cal d e h t v termhtating. T h e n we h a v e t h e follov¢ing result. T h e o r e m 4,1. L e t P he a h~gic p r o g r a m with d e k t v declarations. m a p p h t g a n d let I be a m o d e / o f P . S u p p o s e that: 1. P is dela.v recto'rent with respect to !1 a n d 1, a n d 2. the delay declarations are sa]~, with re,wect to t l. Then p is local delay terminating.

L e t j] he a level

T h e p r o o f o f T h e o r e m 4.1 is r a t h e r long, a n d r e q u i r e s a n u m b e r o f p r e l i m i n a r y results, w h i c h will fill a l m o s t all the rest o f this section, a n d w h i c h will yield this theo r e m in t h e f o r m o f C o r o l l a r y 4.1. B e z e m p r o v e d t h a t f o r a r e c u r r e n t p r o g r a m , all b o u n d e d q u e r i e s will t e r m i n a t e f o r all selection rules. In essence, t h e t e r m i n a t i o n p r o o f f o r r e c u r r e n t p r o g r a m s p r o v e s t h a t , in e v e r y d e r i v a t i o n f o r a b o u n d e d g o a l , t h e level m a p p i n g o f a g o a l is g r e a t e r t h a n t h e level m a p p i n g o f its r e s o l v e n t . A s t h e o r d e r o n m u l t i s e t s is w e l l - f o u n d e d , it f o l l o w s t h a t t h e d e r i v a t i o n h a s to be finite. T h e t e r m i n a t i o n p r o o f f o r d e l a y r e c u r rent p r o g r a m s is s i m i l a r to t h e o n e f o r r e c u r r e n t p r o g r a m s , e x c e p t t h a t we ,Jse t h e a d d i t i o n a l i n f o r m a t i o n p r o v i d e d b y a m o d e l o f t h e p r o g r a m , as in [2]. M o r e o v e r , w e use t h e fact t h a t t h e selected a t o m is a l w a y s b o u n d e d . R e c a l l t h a t w e a r e w o r k i n g u n d e r t h e f o l l o w i n g t w o a,vstttnptioi:s: !. t h e d e l a y d e c l a r a t i o n s a r e safe, a n d 2. t h e selection rules a r e local. F i r s L we s t a t e a u s e l u l p e r s i s t e n c e p r o p e r t y o f b e i n g dela.~ r e c u r r e n t . C a l l a n ins t a n c e HO .-- QO o f a c l a u s e c =~ H *--- Q h e a d - i n s t a n c e ~ ' c if Dora(O) c: Var(H).

E. hlart'hiori, F. Teusink I J. Logic Programm#~g 39 (1999) 95-124

108

L e m m a 4.1. A h e a d - i n s t a n c e o/" tt dehtv recto'rent obtuse with respect to ][ is d e l a y recto'rent with respect to !1. P r o o f . T h e result is a d i r e c t c o n s e q u e n c e o f p o i n t 2 o f L e m m a definition of delay recurrency. []

3.I a n d o f t h e

N o t e t h a t t h e a b o v e r e s u l t d, es n o t h o l d w h e n a g e n e r i c i n s t a n c e is c o n s i d e r e d . In fact. it c o u l d h a p p e n t h a t a n u n b o u n d e d a t o m o c c u r r i n g in t h e b o d y o f t h e c l a u s e b e c o m e s b o u n d e d b y i n s t a n t i a t i n g s o m e v a r i a b l e s o c c u r r i n g o n l y in t h a t a t o m . F o r i n s t a n c e , c o n s i d e r t h e f a m i l i a r c l a u s e c = p(x) ~ - - p ( y ) a n d t h e level m a p p i n g s u c h t h a t Ip(t)l is e q u a l t o t h e n u m b e r o f e l e m e n t s o f t i f t is a list, a n d is e q u a l t o z e r o o t h e r w i s e . T h e n c is d e l a y r e c u r r e n t (PO') h a s n o c o v e r s ) b u t its i n s t a n c e c = p( [a] ) --- p( [a] ) is n o t d e l a y r e c u r r e n t , b e c a u s e n o w p([a]) h a s t h e e m p t y c o v e r b u t tP([a})l > Ip([a])l d o e s n o t h o l d . N e x t , we fix s o m e t e r m i n o l o g y in o r d e r to be a b l e t o s p e a k a b o u t t h e q u e r i e s a n d a t o m s in a d e l a y S L D - d e r i v a t i o n . S u p p o s e we h a v e a d e l a y S L D - d e r i v a t i o n c o n t a i n i n g a q u e r y Q = A,N~ . . . . . N~. a n d its r e s o l v e n t Q' = (Bn . . . . . B~. Ni "\)0. Then: . A n a t o m N~ in Q is t h e p a r e n t o f t h e a t o m N,0 in O' ( o r vice versa, N,0 is a child o f N,), f o r i C [1..k]. . Q is c a l l e d t h e p a r e n t o f Q' ( a n d Q' the c h i l d o f O). . ,4 is c a l l e d t h e direct g e n e r a t o r o f the a t o m s (BI . . . . . Bt)O in _O'. o T h e t r a n s i t i v e c l o s u r e o f t h e p a r e n t (resp. c h i l d ) r e l a t i o n is c a l l e d p r e d e c e s s o r (resp. ~h'scendent) r e l a t i o n ( o n a t o m s / q u e r i e s ) . * A n a t o m A (in a q u e r y Q) is t h e g e n e r a t o r o f a n a t o m ,:' (in a q u e r y O'), if A is t h e d i r e c t g e n e r a t o r o f A', o r - A : A' a n d Q = Q' is the first n o d e o f t h e d e r i v a t i o n , o r .4 is t h e d i r e c t g e n e r a t o r o f a n a t o m ,4" in t h e r e s o l v e n t o f Q. a n d .4" is a pred e c e s s o r o f A'. G i v e n a n a t o m A, w e use g e n ( A ) t o d e n o t e t h e g e n e r a t o r o f ,4. B e f o r e we e v e n b e g i n w i t h t h e t e r T , , i n a t i o n p r o o f , let us first s t a t e s o m e p r o p e r t i e s o f l o c a l s e l e c t i o n rules w h i c h wi!i pLove to be e s s e n t i a l in t h e t e r m i n a t i o n p r o o f . -

-

L e m m a 4.2. L e t tl be a derivation us#lg a local selection rule. L e t Q a n d Q' be queries in !1 such that Q is a p r e d e c e s s o r o f Q ' , atut let .4 a n d A' be theh" respective s e l e c t e d a t o m s . L e t Q,e a n d ¢~, be the queries that contain g e n ( A ) a n d gen(~i'), respectirely. I j ' ~ g is a p r e d e c e s s o r o f Q then ~ is a predecessor o f Q~. P r o o f . B e c a u s e t h e l o c a l s e l e c t i o n rule is used, t h e se!ected a t o m s a r e a l w a y s t h o s e t h a t " ' h a v e b e e n i n t r o d u c e d in t h e d e r i v a t i o n m o s t r e c e n t l y " . T h a t is e q u i v a l e n t to s a y i n g t h a t t h e i r g e n e r a t o r s a r e closest. [] T h u s a t o m s a r e selected a c c o r d i n g to a L I F O (last in, first o u t ) s c h e m e . N e x t , we introduce the notions of segment, which describes a partial derivation, and of comp l e t e s e g m e n t , w h i c h d e s c r i b e s a p a r t i a l d e r i v a ~ ! o n w h e r e e a c h selected a t o m h a s been con~pletely resolved.

E. .Warchiori. F. Tettsink I J. Logic Progromming 39 : 1999 ~ 95 -124

109

D e f i n i t i o n 4.3. L e t q b e ' d e r i v a t i o n u s i n g a l o c a l s e l e c t i o n r u l e . w i t h q u e r i e s Q0, Q ~ , . . . , i n p u t c l a u s e s H o ~ .~1o. H~ .--- .4~ . . . . a n d m g u ' s 0o. th . . . . . A s e g m e ~ t t q~ o f ~i c o n s i s t s o f q u e r i e s Q~ . . . . . Q~. i n p u t c l a u s e s H~ ~ A~ . . . . . H i - ~ ~ ,?li ~ a n d m g u ' s 0 ; , . . . . 0/_~. W e d e f i n e O~ = 0 ~ . . . 0 i _ ~ . W e c a l l a s e g m e n t ,l~ c o m p l e t e if: 1. i < j , 2. f o r all q u e r i e s Q~ w i t h k /> j , f o r all a t o m s A' in Q~. g e n ( A ' ) is n o t c o n t a i n e d ~n a q u e r y in Qi . . . . . Q i - ~ , a n d 3. f o r all q u e r i e s Q~ w i t h i -< k < j . t h e g e n e r a t o r o f t h e s e l e c t e d a t o m in Q~ is c o n t a i n e d in a q u e r y in Q, . . . . . ~.)i lw e s h o w n o w t h a t e a c h a t o m o c c u r r i n g in a c o m p l e t e s e g m e n t is c o m p l e t e l y res o l v e d in t h a t s e g m e n t . L e m m a 4.3. L e t q h e a d e r i r c t t i o , u s i t l g a h ~ c u i s e l e c t i o n r u b ' . L e t ~l~ &" a o o m p h ' r e segme,'~t o/" q. L e " A h e t h e s e l e c t e d a t o m i . O i _ i . T h e t t 0~!~,,.~_~; is a c.a.s, f o r el. P r o o f . W e p r o v e t h e c l a i m b y i n d u c t i o n o n t h e l e n g t h I = (.j -- i) ~ I o f q ~ . N o t e . t h a t t h e l e n g t h o f a c o m p l e t e s e g m e n t is at least 2. F o r I = 2. ,:'e h a v e _that 0~; = 0~. C o n s i d e r t h e i n p u t c l a u s e Hi ~ ,4~. B e c a u s e q~. is a c o m p l e t e s e g m e n t . A/_ ~ m u s t b e the empty query. But then. from i =.j1 it f o l l o w s t h a t .4; is t h e e m p t y query-. B e c a u s e A0; -- H O ; . w e h a v e t h a t .4 h a s t h e e m p t y q u e r y as r e s o l v e n t , u s i n g Hi "--- .4i as i n p u t c l a u s e , a n d 0, as m g u . It f o l l o w s t h a t 0, is a c.a.s, f o r A. N o w . a s s u m e t h a t t h e c l a i m h o l d s f o r sill s e g m e n t s o f l e n g t h less t h a n /. W e h a v e t o p r o v e t h e c l a i m f o r s e g m e n t s o f l e n , , l h /. C o n s i d e r t h e i n p u t c l a u s e Hi +-- .4, f o r O,~. L e t .4~ = .41 . . . . . . . . 1.... B e c a u s e q uses a l o c a l s e l e c t i o n rule. q~-I c a n b e d i v i d e d i n t o m c o m p l e t e s e g m e n t s , o n e f o r e v e r y .4k. t b r k c [I..m]. W i t h o u t l o s s o f g e n e r a l i t y , let us a s s u m e t h a t . f o r k E [l..m]. t h e c o m p l e t e s e g m e n t f o r ..I~ is t h e k t h c o m p l e t e s e g m e n t in t1~:~. Let, f o r k ~_ [l..m]. I~ b e t h e c o m p o s i t i o n o f t h e m g u ' s o f t h e k t h c o m p l e t e s e g m e n t . By i n d u c t i o n h y p o t h e s i s , lt~ ]h,,.~ J~, is a e.a.s, f o r A~. B u t t h e n . (lt: . . . . . lt,,,)Ij.,,,_,m~ ...... ~...~ is a c.a.s, f o r t h e q u e r y .41 . . . . . . 4 .... B e c a u s e we h a v e t h a t AO~ = H d t , . it f o l l o w s t h a t t i ii[~,,~ :~ --= (',tdt I . . . 1~,,, ) j~~,,,.,~ is a c o m p u t e d a n s w e r s u b s t i t u t i o n f o r A. [] In t h e s e q u e l , w e a s s u m e tha~ 01 = e i f i 7> j . W e use t h e p r e v i o u s l e m m a t o p r o v e a more general result on segments.

L e m m a 4.4. L e t q h e a d e r i r a t i o n

rising a h ~ c a l s e l e c t i o , rule. w i t h q , e r i e s Q o . Q i . . . . . i n p u t c k t l t s e s H o *--- ,4o. H I .-- ,4 ! . . . . o n d m g u ' s 0 . . O! . . . . L e t q~ h e a s e g m e n ~ t o f q s u c h that the generator o f the' s e l e c t e d a t o m #t Q i is t h e s e l e c t e d a t o m itz Qi. L e t It = (~+ i ]f~,~i.),~, ~" T h e , . i t is u c . a . s . . l o r a s u b q u e r v o f 74i Oi.

Proof. W e p r o v e t h e c l a i m by i n d u c t i o n o n t h e l e n g t h / = ( j - i) + 1 o f q~. F o r / = i. w e h a v e t h a t t!)+l = e, a n d t h e r e f o r e t h e c l a i m h o l d s , i f w e t a k e t h e s u b q u e r y t o b e t h e e m p t y q u e r y . N o w ' , c o n s i d e r / > 1 a n d a s s u m e t h a t t h e c l a i m h o l d s f o r all s e g m e n t s s m a l l e r t h a n /. F i r s t o f all, a p r e f i x o f ll'i c a n b e h a n d l e d d i r e c t l y u s i n g i n d u c t i o n . S u p p o s e t h a t f o r s o m e O in Qj_ i . . . . . Qj._ ~. t h e g e n e r a t o r o f t h e s e l e c t e d a t o m in Q is Q~. T h e n . let k b e t h e g r e a t e s t n u m b e r in [i + l . . j - 1] s u c h t h a t t h e g e n e r a t o r o f t h e s e l e c t e d a t o m in _Ok

E. MarchiorL F. Teusink I J. Logic Programmhlg 39 (1999) 95-124

110

is Q~. O t h e r w i s e , let k = i. N o w , let i t ' = (0,-+~ ...O,_t)lVar(ft~O~)). By i n d u c t i o n hypothesis, It' is a c.a.s, o f a s u b q u e r y of,4~0~. W e n o w deal w i t h the r e m a i n d e r o f ~ , i.e. ~ . As the g e n e r a t o r o f the selected ato m o f Qs- is in Q~, we k n o w t h a t the selected a t o m in Q, is o f the f o r m Ale', w h e r e A is an e l e m e n t o f A~O~. M o r e o v e r , the g e n e r a t o r o f the selected a t o m in Qi is also in Q~. B u t then, f r o m L e m m a 4.2, it follows t h a t ~ is a c o m p l e t e s e g m e n t a n d therefore, by L e m m a 4.3, ~]~,-(m,') is a c.a.s, for AI,'. Let I t " = ~l,~,~<,~,,,)C o m p o s i n g / ~ ' a n d p" we h a v e t h a t (p t! t" )]r,,.(~,0,) -- ~Ilt" is a c.a.s, for a s u b q u e r y o f AiOi •

[]

H a v i n g s t a t e d s o m e results on local selection rules, we a r e n o w able to p r o v e termination of delay recurrent programs. T h e o r e m 4.2. L e t P b e a p r o g r a m that is d e l a y r e c u r r e n t with respect to 11 a n d L L e t be a d e l a y S L D - t r e e f o r P ( w i t h respect to [ l) h a v i n g an a t o m as root. T h e n z is f i n i t e . T h e t e r m i n a t i o n p r o o f for d e l a y r e c u r r e n t p r o g r a m s will p r o c e e d as follows. First, we label every q u e r y Q in the S L D - t r e e with a m u l t i s e t [[Q] la- T h e n , we s h o w t h a t for every q u e r y Q in t h e tree, a n d every resolvent R o f Q, [[R] [a is s m a l l e r t h a n [[Q] la in the m u l t i s e t o r d e r i n g . So, let us define the p r o c e d u r e to label the n o d e s o f a local d e l a y S L D - t r e e . W i t h o u t loss o f g e n e r a l i t y , we a s s u m e in the sequel t h a t level m a p p i n g s h a v e values which are strictly g r e a t e r t h a n zero. T h i s a s s u m p t i o n g u a r a n t e e s t h a t i[Q]I,, is g r e a t e r t h a n o r e q u a l to zero. Definit[en 4.4 (t[Q]fa). Let I I be a level m a p p i n g for P. Let z be a d e l a y S L D - t r e e f o r P h a v i n g a b o u n d e d a t o m as root. Let A be a n a t o m in s o m e q u e r y in ~. T h e n the f u n c t i o n I[]td m a p s A i n t o a nature,! n u m b e r , a n d is defined as follows:

I[A]I

I[A]l,

=

t[gen(a)]l-

if A

I

:

,Ten(A),

otherwise.

Let Q = A~ . . . . . A, be a q u e r y in r. T h e n ,

I[Q]l,t =

~l[a,][,~,..., I[A,,IL,t].

T h e idea b e h i n d the label I[Q]la is t h a t it is, in s o m e sense, a "safe' e s t i m a t i o n o f W e h a v e to use such a n e s t i m a t i o n b e c a u s e t h e a t o m s in an S L D tree are n o t necessarily b o u n d e d . T h e r e f o r e I[Q]I is n o t defined f o r all queries in the S L D tree. T o solve this p r o b l e m , we define j[A]la in t e r m s o f I[B]I, w h e r e B is the g e n e r a t o r o f A. In the case o f d e l a y S L D trees, I[B][ is defined, b e c a u s e in a d e l a y S L D - t r e e the selected a t o m o f a q u e r y is g u a r a n t e e d to be b o u n d e d . In t h e f o l l o w i n g l e m m a , we p r o v e t h a t l[a]la is "safe', in the sense t h a t l[Alla >i [[A]I for all selected a t o m s in the queries of a delay SLD-tree.

I[Q]I.

Lernma 4.5. L e t P b e a p r o g r a m that is d e l a y recurrent w i t h respect to It a n d I. L e t T be a d e l a y S L D - t r e e f o r P (with respect to [ ]) h a v i n g an a t o m as root. L e t Q be a q u e r y o f z a n d let A be its s e l e c t e d a t o m . T h e n , 1[,411a >~ f[-411• Proof. I f the g e n e r a t o r o f A is A itself, then the claim follows i m m e d i a t e l y f r o m D e f i n i t i o n 4.4.

E. ~WarchiorL E Teusink ! J. Logic Programming 39 (1999) 95-124

Ill

O t h e r w i s e , let tl be t h e b r a n c h o f z t h a t c o n t a i n s Q, a n d let Q be t h e f l h q u e r y in (i.e. Q = Qj). Let B be the g e n e r a t o r o f A, a n d let Q~ be t h e r, o d e t h a t c o n t a i n s B. N o w , c o n s i d e r t h e s e g m e n t r/~. o f ~/, a n d let it = (P~'[~r(.~,0~" By L e m m a 4.4, we h a v e t h a t tt is a c.a.s, for a s u b q u e r y , say/~, of.4~J~. T h i s i m p l i e s t h a t I ~ V(Bp). Let A' be the a t o m of,),0~ s u c h t h a t A is t h e d e s c e n d a n t o f A'. T h e n A is a n i n s t a n c e o f A' w h i c h is o b t a i n e d b y i n s t a n t i a t i n g s o m e a t o m s o f B. Since A is b o u n d e d , t h e n a s u b q u e r y , say C', o f A' is a c o v e r f o r A'. Let fl = Pl,~,r~')" T h e n we h a v e t h a t I ~ Cflp, for e v e r y p w h i c h g r o u n d s Cfl. By the h y p o t h e s i s t h a t P is d e l a y r e c u r r e n t a n d b y L e m m a 4.1 ~.t follows t h a t HO~ ~ A~O~ is d e l a y r e c u r r e n t . T h e n :

I[HO,Fp]I > I[A'Fp][.

(l)

But t h e n , b e c a u s e B is b o u n d e d a n d HO~ = BO~, it follows t h a t

I[B]t >~ I[B0,]t- I[H0;]I >i [[HO,Fp]I > I[A'Fp]! for every p. F r o m A'fl b o u n d e d it follows t h a t I[B]I > I[A'F] l- F i n a l l y , t h e claim folI:~ws fi'~m the fact t h a t A =A'fl'; for s o m e 7 a n d [[B][ = i[A]I~. [] H a v i n g p r o v e n t h a t ][A] [a is a g o o d a p p r o x i m a t i o n for 1[,4]1, we n o w p r o v e t h a t t h e label o f a q u e r y is g r e a t e r t h a n the label o f its resolvent. T o this e n d , we use the foll o w i n g result b y Bezem. P r o p o s i t i o n 4.1. L e t Q be a bounded ques T and let 0 be a substitution. Then: I. QO is bounded,

2. I[Q]I i> I[Q0]l. L e m m a 4.6. Let P be a program that is delay recurrent with respect to [[ and L Let ~ be a delay S L D - t r e e j b r P (with respect to I I) having an atom as root. Let Q be a query o f and let R be a direct descendant o f Q. Then I[g][ a is smaller than [[Q][a m the multiset ordering. P r o o f . S u p p o s e for s i m p l i c i t y t h a t t h e l e f t m o s t a t o m o f Q is selected. T h e n , Q = A, a n d R = ( C , B ) O , with H ~- C i n p u t c l a u s e a n d 0 = m g u ( H , A ) . F o r e v e r y B in B we h a v e t h a t [[B0][a -- l[B] [a- M o r e o v e r , for e v e r y C in C?0, we h a v e f r o m D e f i n i t i o n 4.4 t h a t I[C][,~ = [[H0][ -- I. T h u s , to p r o v e the claim, it is sufficient to s h o w t h a t I[A]Ia > I[H0]I - 1. W e h a v e t h a t :

I[A][,, t> I[A]I from L e m m a 4.5 i> t[,4011 from P r o p o s i t i o n 4. I = [[HO][

f r o m 0---- m g u ( H , A )

> I [ H 0 ] i - I.

[]

N o w we c a n p r o v e T h e o r e m 4.2. P r o o f o f T h e o r e m 4.2. F r o m L e m m a 4.6 (recall we a s s u m e local selection rules a n d sail: ,~clay d e c l a r a t i o n s ) a n d t h e fact t h a t t h e m u l t i s e t o l d e r o f D e f i n i t i o n 4.4 is wellfounded. []

T h e n we o b t a i n T h e o r e m 4.1 in t h e f o r m o f the f o l l o w i n g c o r o l l a r y .

E. Alarchiori, F Tettshtk I J. Logic Programming 39 (1999) 95-124

i!2

CoroJllary 4.1. L e t P he a p r o g r a m t h a t is de~a)' recto'rent n i t h respect to he tt d e k t v S i , D-ti'ee l~,r P (w;th re.sT~e¢'t to [ ]). Then z is f i n i t e .

II c;nd I.

Let z

P r o o f . Let Q = A t . . . . . A,, be the r o o t o f z. W e p r o v e the result b y i n d u c t i o n o n n. I f n = 1 then the result follows by T h e o r e m 4.2. S u p p o s e n o w t, > 1. If e v e r y a t o m o f Q is n o t b o u n d e d t h e n the result follows i m m e d i a t e . O t h e r w i s e let A~ be the selected a t o m . T h e n f r o m T h e o r e m 4.2 we h a v e t h a t the d e l a y S L D - t r e e for A~ is finite. Let Ot . . . . . 0,,, be its c o m p u t e d a n s w e r s . T h e n f r o m the a s s u m p t i o n t h a t the selection rule is local, it follows t h a t z c a n be split into m + 1 d e l a y S L D - t r e e s , zl f o r A~ a n d the o t h e r s r~ for ( Q - {Ai})Ok, f o r k E [l..m]. T h e n the result follows by ~.he a p p l i c a t i o n o f the i n d u c t i o n h y p o t h e s i s to the r~'s. [] 4. I. A n Exa:tq~ie

The definition of delay recurrency provides a m e t h o d for proving termination of logic p r o g r a m s with d e l a y d e c l a r a t i o n s : o n e h a s to End a le,,el m a p p i n g a n d a m o d e l o f the p r o g r a m a n d to p r o v e t h a t the p r o g r a m is d e l a y recu~ J'ent with respect to t h a t level m a p p i n g a n d m o d e l . In this section, we illustrate the a p p l i c a t i o n o f such a m e t h o d by m e a n s o f a n exa m p l e . T o h e l p the r e a d e r to focus m o r e on the a p p r o a c h ~han ol~ the e x a m p l e , we h a v e c h o s e n the w e l l - k n o w n p r o g r a m qtticksorti2, defined by the f o l l o w i n g set o f clauses: q~,.( [.,-tx.,] . y., ) ~

p,,,-t (.~.,.. x. /.,.. b.,. ). q.,.( /.,.. .,. /.,. ) .

qs( t~s. shs ), app( sls, [x]sbs], ys).

q.,-([ ]. [ 1). pa,-t([.,lx.,l..v.

[xl/.~]./,.,')

p-,'~([-,'t.,'x],y./.,'.

~

x <~.v. p, rtC.x'~,'..v./.,'. I,.~).

[-,'lh.,']) --- -, > ~'. p,,'t(.,-~'.y.

,r.,../,.,.).

p,~,,',,([ j..,.. [], []). a u g m e n t e d with the c l a u s e s for a p p e n d l 3 given in the I n t r o d u c t i o n . U s u a l l y , the int e n d e d use o f the p r e d i c a t e qs is t h a t o f g i v i n g it a list as first a r g u m e n t , in o r d e r to get a sorteta p e r m u t a t i o n o f t h a t list as o u t p u t in the s e c o n d a r g u m e n t . T h i s u s a g e o f q u i c k s o r t l 2 v :s ' .rouen to be safe (with respect to t e r m i n a t i o n ) e.g. in Ref. [!], w h e r e a p r o p e r d e l a y d e c l a r a t i o n is c h o s e n . H e r e we will s h o w t h a t , o n e c a n also use safely the p r o g r a m in its reverse, i.e. give qs a s o r t e d list in its s e c o n d a r g u m e n t , a n d it will p r o d u c e all p e r m u t a t i o n s o f t h a t list in its first a r g u m e n t . O b s e r v e t h a t w h e n the P r o log selection rule is used. this a l t e r n a t i v e u s a g e o f the p r o g r a m yields n o n - t e r m i n a tion. T h i s is the m a i n r e a s o n w h y the a p p r o a c h o f A p t a n d Luitjes c a n n o t d e a l with this case. W e n o w give a level m a p p i n g f o r the p r e d i c a t e s in the p r o g r a m , a n d a m o d e l . It w o u l d g o t o o far to give a d e t a i l e d a c c o u n t o f the w a y we a r r i v e d at this specific level m a p p i n g . F o r t h o s e w h o a r e i n t e r e s t e d in tech~aiques f o r finding level m a p p i n g s , we refer e.g. to [8]. Let tl . . . . . t4 be g r o u n d terms. T h e n :

E. i~larchiori. F. Tt,ushtk I J. Logic Programmb~g 39 [ 1099) 95-124

113

Iqs(tl, t2)l - tsi=e(t2) + l, I p a r t ( t l , t2, t3, t4)] --= tsize( t3 ) + tsize( t4 ) , [app(tl, t2, t3)l = tsize(t3), Itl > t 2 i = 0 , Itl <~ t21 = 0, where

tsize(t) =

the length o f t 0

if t is a list, otherwise.

M o r e o v e r , c o n s i d e r t h e f o l l o w i n g i n t e r p r e t a t i o n I:

I = { q s ( t l , t2) I tsize(tl) ---- tsize(t2)} u { p a r t ( t l , t2. t3, ~'4) [ tsize(tl ) = tsize(t3) + tsize(t4)} u { a p p ( t l , t2. t3) I tsize(t3) = tsize(tl ) + tsize(t2)}. It is e a s y to c h e c k t h a t I is a m o d e l o f qtth'ksortl2. W e h a v e to p r o v e t h a t t h e c l a u s e s o f qtdcksort/2 a r e d e l a y r e c u r r e n t w i t h r e s p e c t t o this level m a p p i n g a n d this m o d e l . F o r tli)p a n d part, this is eas.v to c h e c k , b e c a u s e t h e y a r e r e c u r r e n t w i t h r e s p e c t to the g i v e n level m a p p i n g . H e n c e t h e result f o l l o w s f r o m L e m m a 3.2. So, to p r o v e t h e p r o g r a m d e l a y r e c u r r e n t , we h a v e to z h e c k t h e t w o c l a u s e s f o r qs. T h e s e c o n d c l a u s e is trivial, b e c a u s e it is a fact. T o c h e c k t h e first c l a u s e , w e a c t u a l l y h a v e to d o s o m e w o r k . F i r s t . w e c o m p u t e t h e m i n i m a l d i r e c t c ~ v e r s a n d c o v e r s f o r t h e a t o m s in t h e b o d y . T h e s e a r e g i v e n in Fig. 2. A s we see, in this c a s e e v e r y a t o m h a s a single m i n i m a l d i r e c t c o v e r a n d a single c o v e r . H a v i n g f o u n d the c o v e r s , we c a n p r o v e t h a t t h e c l a u s e is d e l a y r e c u r r e n t . F i r s t o f all, c o n s i d e r app(sls, [xlsbs],)~). A q s ( [ x l x s ] , ) ~ ) - g r o u n d i n s t a n c e o f t h e c l a u s e b i n d s x, x s a n d y s to g r o u n d t e r m s , s a y t l, t2 a n d t3. It f o l l o w s d i r e c t l y f r o m t h e level m a p p i n g s o f qs a n d app t h a t :

Iqs([tllt2],t3)l = tsize(t3) + 1 > tsize(t3) -- I[app(s!s, [tllsbs],t3)]l. S e c o n d l y , qs(ls, sls) h a s B - ~ {app(sL~,[xlsbs],)~)} a s c o v e r . A (B. qs([xlxs],3~'))g r o u n d i n s t a n c e o f t h e c l a u s e b i n d s xs, jw, x, sis, sbs to g r o u n d t e r m s , s a y tl . . . . . t5, r e s p e c t i v e l y . S u p p o s e t h a t I ~ app(t4, [t31t5],t2). T h e n tsize(t2) > tsize(t4). B u t t h e n , w e h a v e t h a t

lqs([t3[tl],t2)l = tsize(t2) + 1 > t s = e ( t 4 ) + I --- ilqs(ls, t4)] I. T h e p r o o f f o r qs(bs, sbs) is similar. F i n a l l y , part(xs, x, L~, bs ) h a s c o v e r b = { qs( ls, sis), qs( bs. sb~ ). app( sls, [xlsbs], ys) }. A (/), qs([xlxs],ys))-ground i n s t a n c e o f t h e c l a u s e b i n d s .rs,3~', x, sis, sbs, Is, bs to g r o u n d t e r m s , s a y tl . . . . . t7, r e s p e c t i v e l y . S u p p o s e t h a t

I ~ qs(t6, t4), qs(t7, iS). app(t4, [t31tS], t2). T h e n tsize(t2) > tsize(t6) +- tsi:e(t7). But t h e n we h a v e t h a t

tqs([t3ltl],t2)l = tsi:e(t2) + I > tsize(t6) + tsi-e(t7) -- [ ~ a r t ( t l . t 3 , t6, t7)][

114

E. AlarchiorL F. Teus#tk I J. Logic Programming 39 (1999) 95-124

m i n i m a l direct

atom

cover

part(x~, x, Is, bs) qs(ls, sis) qs (bs, sbs) app(sls, [xlsbs], ys)

{ qs(ls, sis), qs(bs, sbs) } { app(31s, [xlsbs], ys) } { app(sls, [xlsbs], ys) } 0 Fig. 2. C o m p u t i n g

cover

qs(ls, sis), qs(bs, sbs), } app( ~ls, [xlsbs], ys) } { app(sls, [xlsbs], y s) } { app(sls, [xlsbs], ys) } 0

c o v e r s f o r qs.

SO. we have proven that quicksortl2 is delay recurrent with respect to I ] and L As a result, we have that all queries wilt terminate, provided that a local d~lay selection rule is used and the delay declarations are safe. Thus, we now have to translate the b o u n d e d n e s s i n f o r m a t i o n given by the level m a p p i n g into delay declarations, i.e. find deiay declarations for qs. /:ari and app such that if an a t o m is not delayed, it is b o u n d e d . F o r this, the foliowing delay declarations suffice: DELAY q s ( x s . y s ) UNTIL l i s t ( y Q

DELAYpart(.vs,

y. ls. bs) UNTIL list(Is) A list(bs)

DELAY app(.vS.):~. =v) t:NTIL list(ys) 5. O n the expressive power o f the method In this section we consider ~o~,~,.- issues concerning the expressive power o f the p r o o f metbc, d 8¢~ ived from the notion of delay recurrency. First, we discuss the completeness issue, and show that the m e t h o d is not in general complete. Next, we discuss how the m e t h o d can be extended in order to deal with sets of queries. Finally. we investigate the role oC local selection rules in ensuring delay termination and discuss a generalization o f the notion o f delay recurrency, recently introduced in Ref. [17], which permits coroutining.

5.1. Completeness We have seen that the notion o f delay recurrency subsumes the n o t i o n o f recurrency and is strongly related to the notion o f acceptability. P r o o f m e t h o d s for proving t e r m i n a t i o n based on acceptability and recurrency have been generally expressed in te~ms o f necessary and sufficient conditions (cf. Ref. [23]). However, a similal" t r e a t m e n t is not possible for o u r method. Delay recurrency and safe delay declarations imply local delay termination. However, the vice versa does not hold, namely t~ere are some local delay t e r m i n a t i n g p r o g r a m s which c a n n o t at the same time be delay recurrent and have a safe delay declaration. The following examples illustrate tlSs p h e n o m e n o n .

E. zlgarchiori, F Teushsk I J. Logic Progratmhhlg 39 (1999) 95-124

115

Example 5.1. Consider the p r o g r a m P:

p(x) *-- q(z),p(z). q(z). with delay declaration DELAY

p(x)

UNTIL

ground(x)

It is not difficult to see that P is (local-)delay terminating. In fact, every delay S L D - d e r i v a t i o n o f p ( t ) terminates in deadlock: this h a p p e n s ina~aediately if t is n o t ground; otherwise, the first clause o f P can be selected and p(t) is reduced to q(z), p(.:). In this query, only q(-) can be selected; however, the resulting resolvent p ( : ) contains no selectable atoms. M o r e o v e r , every delay S L D - d e r i v a t i o n o f q(t) is finite. N o w , for every level m a p p i n g [ I we have that P c a n n o t at the same time be delay recurrent a n d have a safe delay declaration. In fact, in o r d e r to have a safe delay declaration, q(:) has to be b o u n d e d . F r o m this it follows that q(-_) has the e m p t y cover. C o n s i d e r the first clause p(x) ,-- q(z),p(:-). Since {q(:)} is a cover o f p(:), then the test Ip(s)l > Ip(t)l would have to be satisfied for all g r o u n d terms s, t where t is a list. But this is impossible. The p r o g r a m in the a b o v e example has d e a d l o c k e d derivations. The following example shows that even if we assume that every a t o m which satisfies its delay declaration has no finite derivation ending in deadlock, that i:i if the p r o g r a m is deadlock free (see Section 6.2), the m e t h o d remains incomplete. Example 5.2. C o n s i d e r the p r o g r a m P:

p(x) ~-- q(z),s(=),r(y),s(y). q(x).

s([xlxs])- s(xs).

s([]). with delay declarations DELAY

p(x)

UNTIL

glt'Oll;ld(x)

DELAY

S(X)

UNTIL

groulld(.v)

It is easy to see that P is (local-)delay terminating. In fact, every, delay S L D - d e r ivation o f q(t) is finite. M o r e o v e r , every delay S L D - d e r i v a t i o n o f s(t) is finite, a n d it terminates in deadlock only if s(t) does not satisfy its delay declaration, that is if t is not g r o u n d . Finally, every delay S L D - d e r i v a t i o n o f p ( t ) terminates: this h a p p e n s immediately if t is not g r o u n d : otherwise, the first clause o f P can be selected and p(t) is reduced to q(z), s(,:), r(y). s(y). In this query, only q(:) a n d rO') can be selected. T h e selection o f r 0 ' ) yields failure. While the selection o f q(:) yields the q u e r y s(z), r(y), s(y), where only r(y) can be selected, so also this derivation ends in failure. O n e can easily check tht:t P is deadlock free. However, for every level m a p p i n g [ I, we have that P c a n n o t at the same time be delay recurrent and have a safe delay declaration. In fact. in order to have a safe delay declaration, q(z) has to be bounded. F r o m this it follows that q(z) has the e m p t y cover. By applying the condition o f delay recurrency to the clause s([xlxs]) ~-- s(xs), one obtains that the test Is([t: ]t2])[ > !s(t_,)l would have to be satisfied for all g r o u n d

116

E. MarchiorL F. Teushtk / J. Logw Progr.amnt;~.g 39 (1999) 95-124

t e r m s tj. t~,, H e n c e , the set {Is(t)) [ t is g r o u n d } h a s n o m a x i m u m . C o n s i d e r n o w the first c l a u s e p(x) .--- q(z), s(z), t O ' ) , s(y). T h e n {q(z)} is a c o v e r o f s(z), h e n c e the test )P([])I > [s(t)J w o u l d h a v e to be satisfied for all g r o u n d t e r m s t. But this is impossible. W e c o n c l u d e with a n a n a l y s i s o f the r e a s o n s w h i c h p r e v e n t us to use s i m i l a r a r g u m e n t s a s t h o s e e m p l o y e d in the c o m p l e t e n e s s p r o o f s o f t h e m e t h o d s b a s e d o n the n o tion o f a c c e p t a b i l i t y a n d r e c u r r e n c y . In these p r o o f s , a level m a p p i n g If is c h o s e n t h a t a s s i g n s to a g r o u n d a t o m the n u m b e r o f n o d e s o f all t h e ( S ) L D - t r e e s for t h a t a t o m . F o r i n s t a n c e , in Re['. [2] the Lifting L e m m a is used for p r o v i n g the f o l l o w i n g p r o p e r t y : if a q u e r y Q h a s a finite L D - t r e e x , ~ : i f o r all s u b s t i t u t i o n s 0, QO h a s a n L I ) - t r e e c o n t a i n i n g tess o r e q u a l n u m b e r o f n o d e s . T h e c o m p l e t e n e s s p r o o f p r o c e e d s by o b s e r v i n g t h a t the n u m b e r o f ~lodes o f all the ( S ) L D - t r e e s f o r a g r o u n d a t o m is less t h a n t h e n u m b e r o f n o d e s o f all the ( S ) L D - t r e e s o f a n y o f the r e s o l v e n t s o f t h a t a t o m . M o r e o v e r , the a b o v e m e n t i o n e d p r o p e r t y e n s u r e s t h a t this i n e q u a l i t y is preserved w h e n a g r o u n d i n s t a n c e o f the r e s o l v e n t is c o n s i d e r e d . T h i s a l l o w s to c o n c l u d e t h a t t h e test o f a c c e p t a b i l i t y ( r e c u r r e n c y ) is satisfied. W e c a n n o t a p p l y a s i m i l a r a r g u m e n t in c a s e o f local d e l a y t e r m i n a t i n g p r o g r a m s , b e c a u s e the p r o p e r t y a b o v e m e n t i o n e d d o e s not h o l d for p r o g r a m s with d e l a y declarati¢~ns. F o r i n s t a n c e , in the p r o g r a m P given in E x a m p l e 5.2, the q u e r y s([a]) h a s a d e l a y S L D - t r e e c o n s i s t i n g o f t h r e e n o d e s , w h i k its m o r e g e n e r a l i n s t a n c e s(x) h a s a d e l a y S L D - t r e e c o n s i s t i n g o f j u s t o n e n o d e , d u e to the s u s p e n s i o n effect c a u s e d by the d e l a y d e c l a r a t i o n .

5.2. Termination with re.~7~ect to a class o f queries In T h e o r e m 4. ! we h a v e p r o v e n t h a t if a p r o g r a m is d e l a y r e c u r r e n t a n d if the delay d e c l a r a t i o n s a r e safe t h e n the p r o g r a m is local d e l a y t e r m i n a t i n g . A c c o r d i n g to D e f i n i t i o n 4.2 this mea,~,,, t h a t aH the queries h a v e o n ! y finite local d e l a y S L D - d e r i r a t i o n s . T h i s is n o t s u r p r i s i n g , since the a s s u m p t i o n t h a t t h e d e l a y d e c l a r a t i o n s be safe is r a t h e r s t r o n g . In f'ac~, this a s s u m p t i o n e n s u r e s t h a t d u r i n g the e x e c u t i o n , n o n - b o u n d e d a t o m s r e m a i n s u s p e n d e d . H e n c e n o n - t e r m i n a t i o n c a n o n l y be c a u s e d b y the b o u n d e d a t o m s o c c u r r i n g in the d e r i v a t i o n s . T h e n o t i o n o f d e l a y r e c u r r e n c y t a k e s c a r e o f these a t o m s a n d it g u a r a n t e e s t h a t t h e y d o not g e n e r a t e infinite d e r i v a tions. C l e a r l y if o n e d i s c a r d s the c o n d i t i o n o f safe d e l a y d e c l a r a t i o n s then it is n o t a n y m o r e t r u e t h a t a d e l a y r e c u r r e n t p r o g r a m is local d e l a y t e r m i n a t i n g . N e v e r t h e l e s s , if we a r e o n l y i n t e r e s t e d in p r o v i n g local d e l a y t e r m i n a t i o n o f a specific set o f queries, t h e n we c a n use the n o t i o n o f d e l a y r e c u r r e n c y , t o g e t h e r w i t h the a s s u m p t i o n t h a t e a c h a t o m w h i c h is selected in s o m e local d e l a y S I , D - d e r i v a t i o n o f a q u e r y in t h a t set is b o u n d e d . In Ref. [24], a m e t h o d is introduceci for p r o v i n g t e r m i n a t i o n a n d l e f t - t e r m i n a t i o n with respect to a set o f queries. T h e a u t h o r s i n t r o d u c e t h e n o t i o n o f call set w h i c h is used to define the n o t i o n o f r e c u r r e n c y a n d o f a c c e p t a b i l i t y o f a p r o g r a m with respect to a set o f queries.

i Recall that an LD-tree is an SLD-tree obtained using the Prolog left-to-right selection rule.

E. ,i.Iarchiori. F. Teusink ! J. Logic Pr¢&gramming 39 (1990) 95-1.74

117

W e c a n use t h e a p p r o a c h o f Ref. [24] f o r e x t e n d i n g o u r m e t h o d t o c o p e w i t h specific classes o f i n p u t q u e r i e s . D e f i n i t i o n 5.1 ( c a l l set). T h e c a l l s e t C a l i ( . ~ ) o f a set . ~ o f q u e r i e s is t h e set o f all a t o m s ( m o d u l o v a r i a n c e ) ,4 s u c h t h a t A is a s e l e c t e d a t o m in s o m e l o c a l d e l a y S L D d e r i v a t i o n o f a q u e r y Q in ,~'. T h e call set o f : / is used in t h e f o l l o w i n g d e f i n i t i o n o f d e l a y rectarrency w i t h respect t o -~'. D e f i n i t i o n 5.2 ( d e k o ' r e c u r r e n t p r o g r a m w i t h r e s p e c t to .
IH0] > I[A0]I. A p r o g r a m P is d e l a y r e c u r r e n t w i t h r e s p e c t t o I ], I a n d : f if e a c h o f its c l a u s e s is d e l a y r e c u r r e n t w i t h r e s p e c t t o 1], I a n d : / ' . T h e n we h a v e t h e f o l l o w i n g result.

T h e o r e m 5.1. S u p p o s e t h a t P is deho" r e c u r r e n t w i t h r e s p e c t to ! I. I a n d :/'. A l o r e o v e r . a s s u m e t h a t a l l a t o m s in C a l l ( . f / ' ) a r e h o t m d e t L T h e n e r e r v quer!" h~ .'/' is h~cai d e l a y te~vnh~ath~g. T h e p r o o f o f this r e s u l t is a n a l o g o u s t o t h e p r o o f o f Theorems, 4. ! N o t i c e t h a t in o r d e r to p r o v e l o c a l d e l a y t e r m i n a t i o n w i t h r e s p e c t t o .'/' we r e q u i r e i n f o r m a t i o n o n p r o p e r t i e s o f t h e a t o m s o c c u i r i n g in t h e d e r i v a t i o n s o f .~'. O n e ca,...-:use g l o b a l a n a l y s i s t e c h n i q u e s in o r d e r t o i n f e r o r to v e r i f y s u c h p r o p e r ties. F o r p r o g r a m s w i t h d e l a y d e c l a r a t i o n s , t h e call set o f a set o f q u e r i e s c a n be a p p r o x i m a t e d b y u s i n g t e c h n i q u e s b a s e d o n a b s t r a c t i n t e r p r e t a t i o n (cf. Ref. [12]L H o w e v e r , t h e s e t e c h n i q u e s a r e in g e n e r a l d e f i n e d f o r p r o g r a m s w h i c h a r e s u p r g s e d t o be e x e c u t e d u n d e r t h e l e f t - t o - r i g h t s e l e c t i o n rule, w h e r e t h e leftmt.-:t a t o m o f a q u e r y is selected, a m o n g s t t h o s e t h a t s a t i s f y t h e i r d e l a y d e c l a r a t i o n s . In o r d e r ~o a p ply t h e s e t e c h n i q u e s d i r e c t l y to logic p r o g r a m s w i t h l o c a l d e l a y s e l e c t i o n rides, o n e h a s first t o t r a n s f o r m t h e p r o g r a m u s i n g t h e t r a n s f o r m a t i o n F in~;roduced in Sect i o n 3 a f t e r L e m m a 3.3. H o w e v e r , F is d e f i n e d o n l y f o r c l a u s e s w h o s e b o d y a t o m s h a v e a t least o n e c o v e r , h e n c e it is n o t a l w a y s a p p l i c a b l e . W e a r e n o t a w a r e o f t e c h niques based on static analysis that are designed for p r o g r a m s which are executed u n d e r l o c a l d e l a y s e l e c t i o n rules. 5.3. E x t e n s i o n to n o n - l o c a l d e l a y s e l e c t i o n r u l e s

In t h e s o u n d n e s s result o f o u r m e t h o d { T h e o r e m 4.1 ), we r e s t r i c t o u r s e l v e s to l o c a l s e l e c t i o n rules. T h e r e a s o n f o r t h i s is t h a t w~ w a n t t o use t h e s e m a n t i c i n f o r m a t i o n p r o v i d e d by t h e m o d e l L In the p r o o f o f T h e , ~ r e m 4.1, we use t h i s s e m a n t i c i n f o r m a t i o n as f o l l o w s . F i r s t we o b s e r v e t h a t , w h e n a n a t o m A b e c o m e s s e l e c t a b l e , s o m e

118

E. Marchiori. F. Teusink I J. Logic Programnling 39 (1999) 95-124

c o v e r B o f A in the i n p u t c l a u s e t h a t i n t r o d u c e d A h a s b e e n p a r t i a l l y i n s t a n t i a t e d . By u s i n g t h e fact t h a t a l o c a l s e l e c t i o n rule is used, we c a n c o n c l u d e t h a t this c o v e r B h a s b e e n r e s o l v e d c o m p l e t e l y . A s a result, we h a v e t h a t I ~ V/~0, w h e r e 0 is the c o m p o s i t i o n o f s u b s t i t u t i o n s b e t w e e n the n o d e w h e r e (a g e n e r a l i z a t i o n of) A w a s i n t r o d u c e d a n d t h e n o d e w h e r e A is selected. F i n a l l y , we use this fact to p r o v e t h a t t h e level m a p p i n g o f A is strictly s m a l l e r t h a t t h e level m a p p i n g o f t h e selected a t o m in t h e resol u t i o n s t e p t h a t i n t r o d u c e d (a g e n e r a l i z a t i o n of) A. We illustrate now by means of an example that a delay recurrent program does not t e r m i n a t e w i t h respect to e v e r y safe s e l e c t i o n rule. C o n s i d e r the f o l l o w i n g p r o g r a m :

p([xlxs], [xlys]) ,-- p(xs, ys). p( [xlxs] , Ix, x[xs]) +--.fail.

o([],[]). q( [xlxs ], [xlzs]) ~-- p(xs, ys), q(ys, zs).

q([],[]). w i t h the d e l a y d e c l a r a t i o n s : DELAY p(XS, y s ) UNTIL DELAY Q ( x s , y s )

UNTIL

list(xs) list(xs)

T h e p r e d i c a t e p s i m p l y c o p i e s the list in the first a r g u m e n t to the s e c o n d a r g u m e n t . H o w e v e r , i~ ~as a w e i r d a d d i t i o n a l c l a u s e ( c l a u s e 2) t h a t a l w a y s fails. T h e p r e d i c a t e q a l s o c o p i e s t h e list in t h e first a r g u m e n t to t h e s e c o n d a r g u m e n t . H o w e v e r , it uses p in its b o d y , to c o p y x s to ys. W e c a n p r o v e this p r o g r a m d e l a y r e c u r r e n t , u s i n g

It] =

the l e n g t h o f t 0

if t is a ( g r o u n d ) list, otherwise

lpts, t)[ = Is1 lq(s,t)l = Isl + 1 Ifaill = 0 a n d I = {p(s,t) I lsl = Ill} tj { q ( s , t ) I I-~]= It]}. H o w e v e r , t h i s p r o g r a m d o e s n o t t e r m i n a t e for all d e l a y s e l e c t i o n rules. T o see this, c o n s i d e r the f o l l o w i n g d e l a y S L D - t r e e for the q u e r y q([2, 2],rs). q([2, 2]. cs)

[311 {x/2.xs/[2],

vs/[2]cs]}

p( [2] , )~") , qO's, zs )

[i.211'.i-c,,,:)_.:-s/..ys/[2, ,., 21} i

faii, q( [2, 2l, zs)

I°,311

17. Marchiori, F_ Teusink ! J. Logic Programming 39 (1999) 95-124

il9

W e see t h a t the t h i r d q u e r y c o n t a i n s (a v a r i a n t oO the first que ry. T h u s , the re exist infinite d e l a y S L D - d e r i v a t i o n s fo r this goal o b t a i n e d us ing a d e l a y selection rule, T h e r e a s o n for the fact that d e l a y r e c u r r e n t p r o g r a m s are n o t t e r m i n a t i n g for all d e l a y selection rules is th at the test for decrease o f level m a p p i n g f r o m h e a d a t o m HO to b o d y a t o m AiO is c o n d i t i o n a l , In the case o f d e l a y r e c u r r e n t p r o g r a m s this c o n d i t i o n is t h a t for s o m e c o v e r B for Ai, we h a v e t h a t I ~ B0. T r a n s l a t e d to S L D - t r e e s , this i m p lies th at the m u i t i s e t s a s s i g n e d to the n o d e s o f tha t tree are o n l y g u a r a n t e e d to decrease, i f for every selected a t o m in the tree, we k n o w t h a t s o m e c o v e r o f t h a t a t o m is true in L In o u r e x a m p l e , {p(xs,)s)} is the o n l y c o v e r for qOs, zs) in the b o d y o f the first clause for q. In the S L D - t r e e , how e ve r, we select q([2, 2], zs) w i t h o u t first p r o v i n g its cover, w h i c h in this case w o u l d m e a n first selecti n g f a i l . By u s i n g a local selection rule, we e n s u r e that, b e f o r e selecting a n a t o m A, we first resolve a c ov er o f th at a t o m . T h u s , we need to restrict ourselves to the local selection rule in o r d e r to c o n c l u d e that I ~ k/B0, w h i c h a l l o w s us to use the s e m a n t i c i n f o r m a t i o n c o n t a i n e d in L T h i s i m p l i e s t h a t o u r m e t h o d c a n n o t be used directly e.g. with G/Sdel. In fact, the (36del selection rule selects the l e f t m o s t a t o m s o f a q ue ry, a m o n g t h o s e w h i c h satisfy their d e l a y d e c l a r a t i o n , even if this a t o m is n o t m o s t recently i n t r o d u c e d . O n e c a n a r g u e t h a t the use o f local selection rules d o e s not a llow a n y f o r m o f c o r o u t i n i n g , a n d h en ce it is a severe restriction. T h i s is true if the m e t h o d is used to p r o v e t e r m i n a t i o n o f logic p r o g r a m s w h i c h are m e a n t to s u p p o r t c o r o u t i n i n g . H o w e v e r , o b s e r v e that p r o g r a m s w h e r e c o r o u t i n i n g is essential for p r o d u c i n g c o m p u t e d a n s w e r s s u b s t i t u t i o n s are g e n e r a l l y d e s i g n e d for b e i n g not t e r m i n a t i n g . A typical e x a m p l e o f such p r o g r a m s are th o se i m p l e m e n t i n g static n e t w o r k s o f s t r e a m t r a n s d u c e r s . A p o p u l a r i n s t a n c e o f this k i n d o f p r o g r a m s is the one for s o l v i n g the so-called H a m m i n g p r o b l e m : g e n e r a t e a n o r d e r e d s t r e a m o f all n u m bers o f the f o r m 2i3J5 k w i t h o u t repetitions. T o this e nd, five processes a re used, t h a t i n t e r l e a v e their execution, thus g iv in g rise to a n u m b e r o f different s c he dulings. In [26], v a n E m d e n a n d d e L u c e n a p r o p o s e d a s o l u t i o n ba s e d on the incorp o r a t i o n o f c o r o u t i n i n g in the e x e c u t i o n o f a logic p r o g r a m . In o r d e r to b e h a v e in a c c o r d a n c e wi t h the i n t e n d e d m e a n i n g o f the p r o g r a m , i n t e r e s t i n g queries are not g l o b a l l y t e r m i n a t i n g , b e c a u s e their ex ecu tio n p r o d u c e s a s t r e a m o f c o m p u t e d a nswer s u b s t i t u t i o n s . P r o p e r t i e s o f relevance c o n c e r n i n g the o p e r a t i o n a l b e h a v i o u r o f these k i n d o f prog r a m s are existential t e r m i n a t i o n a n d d e a d l o c k f r e e d o m . Existential t e r m i n a t i o n i.,~a r a t h e r difficult issue (cf. [23]). P r o o f m e t h o d s for d e a d l o c k f r e e d o m include the eleg a n t t r a n s f o r m a t i o n a l a p p r o a c h i n t r o d u c e d in Ref. [11]. T h u s , o u r m e t h o d is m o r e s u i t a b l e as a tool for f i n d i n g d e l a y d e c l a r a t i o n s t h a t g u a r a n t e e t e r m i n a t i o n o f logic p r o g r a m s . T h i s topic h a s been c o n s i d e r e d in a recent p a p e r b y M a r t i n a n d K i n g ([17]), w h i c h d e a l s with the g e n e r a t i o n o f efficient, termin a t i n g logic p r o g r a m s . T h e a u t h o r s use d e l a y d e c l a r a t i o n s c o m b i n e d with a s u i t a b l e p r o g r a m t r a n s f o r m a t i o n in o r d e r to g e n e r a t e p r o g r a m s w h i c h a re g u a r a n t e e d to term i n a t e a n d are also efficient. T o this end, the n o t i o n o f semi delay recurrency is int r o d u c e d , w h i c h is a strict g e n e r a l i z a t i o n o f d e l a y r e c u r r e n c y tha t a l l o w s o n e to prove t e r m i n a t i o n w;_h respect to a larger class o f selection rules, w h i c h a d m i t c o r o u t i n i n g . T h e idea is to p e r f o r m the test IHI > i[A~]I o n l y for those a t o m s As such t h a t A~ a n d H are m u t u a l l y d e p e n d e n t . T h e d e p e n d e n c y relat ion they used is the o n e i n t r o d u c e d i r [2], a n d it is o b t a i n e d b y p e r f o r m i n g the t r a n s i t i v e closure o f the re la tion directly

120

I:: Marcl.,it)r/. F. Tt,tts#d," I J. Logic Pro.~rtmsm#lg 39 ("999) 9.5-124

d e p e n d s oll d e f i n e d on the p r o g r a m predicates, such t h a t p directly d e p e n d s on q if there is a c l a u se in the p r o g r a m h a v i n g p as p r e d i c a t e s y m b o l o f the h e a d a n d q as predicate symbol of some body atom. T h e n o / i o n o f semi d e l a y r e c u r r e n c y allows one to p r o v e t e r m i n a t i o n w ith respect to a class o f selection rules w h i c h p e r m i t c o r o u t i n i n g . In fact, for semi d e l a y recurrent p r o g r a m s , it is o n l y n e c e s s a r y for the covers o f those a t o m s w h i c h are m u t u a l l y d e p e n d e n t with the h e a d o f the clause to be resolved c o m p l e t e l y . T h u s , while these covers are resolved, an a r b i t r a r y a m o u n t o f c o r o u t i n i n g m a y take place a m o n g s t the r e m a i n i n g a t o m s o f the clause.

6. O n delay declarations In this section we discuss s o m e aspects o f d e l a y d e c l a r a t i o n s . M o r e precisely, the class "~f d e l a y d e c l a r a t i o n s th at can be expressed using o u r m e t h o d , a n d w h e n the delay d e c l a r a t i o n s d o not affect the d e c l a r a t i v e s e m a n t i c s o f the p r o g r a m . 6.1. O n expres.s'it:eness o/' d e h O' d e c h o ' a t h m s

In G/Sdel, o n e c a n use the p r e d i c a t e nom~ar in d e l a y d e c l a r a t i o n s . F o r instance, the f o l l o w i n g d e l a y d e c l a r a t i o n is used for the p r e d i c a t e ,tpp defined by the p r o g r a m given in Section 1: DELAY app(xs,3:~, ~'s) UNTIL n o n r a r ( x s ) \/nom'ar(-_~) W h e n this d e l a y d e c l a r a t i o n is used, an a t o m a p p ( s , t, u) is not selected until e ithe r s or u is a n o n - v a r i a b l e term. W e c a n n o t deal w ith these k i n d s o f d e l a y d e c l a r a t i o n s . T h e r e a s o n is t h a t in o u r d e f i n i t i o n o f d e l a y recu rren t p r o g r a m s , the n o t i o n o f level m a p p i n g we use is the one used in the d e f i n i t i o n o f r e c u r r e n t p r o g r a m s . In this definition, the level m a p p i n g 1,41 for g r o u n d a t o m s A is defined b y a (total) f u n c t i o n f r o m .~p to ~ , w h e r e a s the level m a p p i n g I[B]L for n o n - g r o u n d a t o m s B is defined as the m a x i m u m o f the level m a p p i n g s o f all its g r o u n d instances. T h u s I[ ]l is a p a r t i a l f u n c t i o n , b e c a u s e the set o f level m a p p i n g s o f g r o u n d i n s t a n c e s c a n be u n b o u n d e d . As a c o n s e q u e n c e , w h e n t a k i n g the level m a p p i n g o f a n a t o m p ( I ) to be the len g th o f list i, the a t o m p([xlx.v]) c o n t a i n s a n o n - v a r i a b l e t e r m , b u t Ibo(blxs])ll is u n d e f i n e d b e c a u s e x s c a n be i n s t a n t i a t e d w ith a n a r b i t r a r y large g r o u n d list. T h u s , an a t o m app([xlxs].)~,-_s ) is not b o u n d e d , while it satisfies the c o n d i t i o n o f the d e l a y d e c l a r a t i o n . T e r m s w h i c h b e h a v e well w ith respect to level m a p p i n g s h a v c been s tu d ied for i n s t a n c e in Ref. [5], w he re they are called rigid. As the a p p e n d l 3 e x a m p l e given in the I n t r o d u c t i o n shows, the t e r m i n a t i o n b e h a v i o u r o f " d e l a y until n o n v a r ' " is p o o r l y u n d e r s t o o d . As far as we c a n see now, a m e t h od h a n d l i n g the n o n c a r d e l a y p r e d i c a t e w o u l d also be s ignific a ntly m o r e c o m p l e x (or, a l t e r n a t i v e l y , weaker), t h a n o u r m e t h o d . All in all, the p r o b l e m s with the nomrar delay p r e d i c a t e were e n o u g h for us to decide not to deal with it at this point. A s a final r e m a r k we w o u l d like to n o te that, if one b r o w s e s t h r o u g h the G 6 d e l m a n u a l , it seems that o u r m e t h o d is severely h a n d i c a p p e d by not b e i n g able to h a n d l e n o m , ar, b e c a u s e m o s t d e l a y d e c l a r a t i o n s in e x a m p l e p r o g r a m s use nont'ar. O n e s h o u l d note

E. Marchiori. F. Teu.v#rk I J. Logic Pro~rmllmil~¢ 39 t'1999) 95-124

121

however, t h a t these p r o g r a m s are not g u a r a n t e e d to t e r m i n a t e for all goals (not even when the leftmost undelayed selection rule is used). T o be fair, the (3iSdel m a n u a l only states t h a t the delay declarations can be used to assist termination. On the o t h e r hand, o u r m e t h o d g u a r a n t e e s termination, be it that the delay declarations will be m o r e restrictive.

6.2. On completeness o.f delay declarations We have seen how delay declarations can be used to ensure t e r m i n a t i o n o f a logic p r o g r a m . O n e could choose strong delay declarations, like for instance DELAY p(.~) U N T I L f a l s e which certainly imply termination. However, the resulting p r o g r a m would not be very interesting, since it yields no c.a.s.'s. T o ensure that the delay declaration is not too strong, one has to g u a r a n t e e that the declarative semantics o f the p r o g r a m is preserved, This is specified in the following definition.

Definition 6.1 (complete delay declaration). Let P be a p r o g r a m and let ~ be the set o f delay declarations for P. Let Me be the least H e r b r a n d model for P. We say t h a t ~ is complete if every a t o m in Me has a successful local delay S L D - d e r i v a t i o n in P. A sufficient condition for the completeness o f the delay declarations o f P is that every g r o u n d a t o m which is in Mp is deadlock free. A query is deadlock free if all its finite local delay S L D - d e r i v a t i o n s do not end in a n o n - e m p t y query which contains only a t o m s that do not satisfy their delay declarations. A p r o g r a m is deadlock free if all its atomic queries which satisfy their delay declarations are deadlock free. Lemma 6.1. L e t P be a p r o g r a m a n d let Me be the least H e r b r a n d m o d e / f o r P. Let be a set o f deko' declarations J o t P. Suppose that ecerv ~:tom A o[" Me is deadlock-free. Then ~ is complete with respect to P. Recently, the topic o f d e a d l o c k - f r e e d o m o f p r o g r a m s with delay declarations has been studied in a n u m b e r o f papers [I,7,10,1 I, 16]. The m e t h o d s there introduced can be used for proving that the hypothesis o f d e a d l o c k - f r e e d o m in L e m m a 6.1 is satisfied.

7. Related work

Let us now relate o u r a p p r o a c h to o t h e r w o r k on termination with respect to dynamic selection rules. The p a p e r which helped us to u n d e r s t a n d the problems in reasoning a b o u t the term i n a t i o n o f logic p r o g r a m s with delay declarations, is Ref. [19]. In this paper. Naish investigates how termination o f a conjunction o f queries can be established, u n d e r the hypothesis that the execution o f each q u e r y does terminate. However, he does not propose ready to use methods for proving p r o g r a m s terminating. In his paper, Naish argues that the use o f modes is crucial to reasoning a b o u t termination. T o

122

E. MarchiorL 1;'. Tetmink ! J. Logic Programsp~'l~g 39 (1999) 95-124

s u p p o r t this claim, he gives a n u m b e r o f useful observations on the t e r m i n a t i o n beh a v i o u r o f a p r o g r a m with delay declarations, which emphasize how subtle is this behaviour, and how difficult it is to prove t e r m i n a t i o n , when dealing with general coroutining. T o w a r d s the end o f the paper, N a i s h suggests t h a t the existence o f "speculative o u t p u t bindings' are an i m p o r t a n t complicating factor when reasoning a b o u t termination in the presence o f recursion, m speculative o u t p m binding occur when an o u t p u t variable is b o u n d e d before it is k n o w n that a call will succeed. This binding allows a recursive call to proceed. It might be the case that in absence o f these speculative bindings, we can generalize o u r m e t h o d to non-local delay selection rules. A n o t h e r c o n t r i b u t i o n to the subject o f t e r m i n a t i o n with respect to delay declarations, is R e f [14]. In this paper, L i i t t r i n g h a u s - K a p p e l discusses a non-deterministic scheme for finding delay declarations that ensure termination. First, he presents an algebra o f ' w h e n " declarations. This algebra is m o r e expressive t h a n the class o f d , . : a y declarations we can handle, basically because we c a n n o t handle n o n v a r predicates. The scheme itself is very general; it is m e a n t as a basis for practical implementations, using heuristics a n d partial evaluation to replace non-deterministic choices. T [ ~ results o f an existing i m p l e m e n t a t i o n look quite promising. On the o t h e r hand, as the scheme is very general, it does not give m u c h insight in the problem ,~f t e r m i n a t i o n itself. A n o t h e r p r o b l e m is t h a t one has to prove that a prograrrt is "safe" (not the notion used in dlis paper), which is quite difficult, the m o r e because there are no methods for d o i n g this. A p a p e r by A p t and Luitjes [1] stimulated us to w o r k on o u r a p p r o a c h . In this paper, they discuss verification o f logic p r o g r a m s with respect to d y n a m i c selection rules. In one section they discuss the p r o b l e m o f termination. The ap~:'oach they take is m o r e general t h a n ours, in the sense that they do not restrict to local selection rules. As a consequence, they need to impose strong restrictions on the class o f prog r a m s they consider. One restriction in this w o r k is that the t e r m i n a t i o n results are stated in terms o f t e r m i n a t i o n with respect to LD-resolution. Thus, it can only discuss t e r m i n a t i o n with respect to d y n a m i c selection rule o f p r o g r a m s which are k n o w n to t e r m i n a t e with respect to leftmost selection rule. Finally, a recent p a p e r by King and M a r t i n [17] deals with the generation o f efficient, t e r m i n a t i n g logic p r o g r a m s . The a u t h o r s introduce the notion o f semi delay recurrency, an extension o f delay recurrency, which is used to prove t e r m i n a t i o n o f a larger class o f p r o g r a m s which a d m i t coroutining. Their m e t h o d c o m b i n e the n o t i o n o f semi delay recurrency with a p r o g r a m t r a n s f o r m a t i o n in order to generate t e r m i n a t i n g logic p r o g r a m s with delay declarations starting from their logical specification.

8. C o n c l u s i o n

In this paper we have introduced a simple m e t h o d for pro,~.ng termination o f logic p r o g r a m s with delay declarations. T h e m e t h o d is b~;,sed on a novel notion, called cover, which is used to describe the interrelation a m o n g the a t o m s o f a clause that can be caused by the delay declarations. Covers are used to define the class o f delay recurrent p r o g r a m s . We proved that all derivations o f a delay recurrent p r o g r a m are finite, if the delay declarations are safe a n d the selection rule is local delay, i.e. it selects at each resolution step one a t o m which satisfies its delay

E, Mart'hiori. F. Teusink i J. Logk- P r ~ r a m m i n g 39 (1999) 95-124

123

declaration, among those atoms most recently introduced. We have discussed adv a n t a g e s a n d l i m i t a t i o n s o f t h i s i,~st c o n d i t i o n o n t h e s e l e c t i o n r u l e , a s w e l l a s p o s s:,ble e x t e n s i o n s . It s e e m s t h a t o u r m e t h o d c a n b e e a s i l y e x t e n d e d t o d e a l w i t h l o g i c p r o g r a m s w i t h r+.egat~,on. W e s k e t c h b r i e f l y h o w t h i s c o u l d b e d o n e . O n e c a n e x t e n d t h e p r o c e d u r e for resolving negated atoms to the case of delay selection rules, simply considering a f o r m o f ( a b n o r m a l ) t e r m i n a t i o n , w h i c h a r i s e s w h e n a t r e e f o r ~A is f i n i t e b u t c o n t a i n s a t l e a s t o n e l e a f c o n s i s t i n g o f d e l a y e d l i t e r a l s . I n s u c h a c a s e --,,4 h a s n o r e s o l v e n t ; it e n d s in d e a d l o c k . T h e n t h e d e f i n i t i o n o f l e v e l m a p p i n g c a n b e e x t e n d e d t o n e g a t e d a t o m s , s i m p l y b y d e f i n i n g 1~,41 = IAI. F i n a l l y , i n D e f i n i t i o n 3.6 o f d e l a y rec u r r e n t p r o g r a m , t h e m o d e l I s h o u l d b e r e p l a c e d b y a m o d e l c o n t a i n i n g s u i t a b l e semantic information.

Acknowledgements We would like to thank the anonymous referees, whose comments and suggest i o n s h a v e i m p r o v e d b o t h t h e c o n t e n t s a n d t h e p r e s e n t a t i o n o~ t h e p a p e r . T h a n k s also to Krszysztof Apt for helpful discussions on the subject of the delay declarations.

References [!] K.R. Ap,, 1, Luitjes, Verification of logic: programs with delay declarations, in: Invited Lecture, Proceedings of the Fourth International Conl~rence on Algebraic Methodology and Software Technology (AMAST'95), Lecture Notes in Computer Science 936, Springer, Berlin, 1995. pp. 66-90. [2] K.R. Apt, D. Pedreschi, Reasoning about termination of pure Prolog programs, Information and Computation 106 (1) I 1993) 109-157. 13] M. Be:,em, Characterizing termination of logic programs with level mappings, in: E.L. Lusk, R.A. Overbeek (Eds.), Proceedings of the North Americ-~n Conference on Logic Programming, MIT Press, Cambridge, MA, 1989, pp. 69-80. [4] M. Bezem, Strong termination of logic programs. Journal of Logic Programming ! -2 {15)( 1993} 7997. I5| A. Bossi, N. Cocco, M. Fabris, Norms on terms and their use in proving universal termination of logic programs, Theoretical Computer Science 124 (1994) 297-328. [6] L. Cavedon, Continuity, consistency, and completeness properties for logic programs, in: G. Levi. M. Martelli (Eds.), Proceedings of the International Conference on Logic Programming, MIT Press, Cambridge, MA, 1989, pp. 571-584. 171 P. Chambre, P. Deransart, Towards a proof method of non-suspension of Concurrent Constraint Logic Programs. in: F. de Boer. M. Gabbrieili (Eds.), Proceedings of the W2 Post-Conference Workshop, Internationa! Conference on Logic Programming, Free University, Amsterdam, 1994. 18] S. Decorte+ D, De Schreye, M, Fabris, Automatic inference of norms: a rvi,+sing link in automatic termination analysis, in: Proceedings of the International Logic Programming Symposium, MIT Press, Cambridge, MA. 1993, pp. 420-436. [9] N. Dershowitz, Termination of rewriting, Journal of Symbolic Computation 8 11987) 69--116. [10] S. Etalle, M+ Gabbrielli, Layered Modes, in: F. de Boer, M. Gabbrielli (Eds.), Proceedings of the JICSLP Post-conference Workshop on Verification and Analysis of Logic Programs, University of Pisa, 1996. [1 I] S. Etalle, M. Gabbrielli, E. Marcbiori. A transformation system for CLP with dynamic scheduling and CCP, ~n: Proceedings of the ~-,97 ACM-SIGPLAN Symposium on Partial Evaluation and Semantic Based Program Manipulation, ACM Press, New York, 1997.

124

E. Mm'chiori. F Teusink i J. Logic Programmh~g 39 (1999) 95-124

[! 2] A. H e a t o n , P. Hill, A. King, Analysis o f Logic Programs with Delay, in: Logic P r o g r a m Synthesis and T r a n s f o r m a t i o n , Springer, Berlin, 1997. [13] P.M. Hill, J.W. Lloyd, T h e G6del P r o g r a m m i n g Language, M I T Press, Cambridge, M A , 1994. [14] S. Liittringhaus-Kappel, Control generation for logic programs, in: Proceedings o f the International Conference on Logic P r o g r a m m i n g , M I T Press, C a m b r i d g e , MA, 1993, ~p. 478-495. [! 5] E. Marchiori, F. Teusink, Proving termination o f logic p r o g r a m s with delay declarations, in: J. Lloyd (Ed.), Proceedings o f the 1995 International S y m p o s i u m on Logic P r o g r a m m i n g , M I T Press, Cambridge, MA, 1995, pp. 447-464. [16] E, Marehiori. F. Teusink. O n deadlock freedom o f logic p r o g r a m s with d y n a m i c schedulin, in: F. de Boer, M. Gabbrielli (Eds.), Proceedings o f the J I C S L P post-conference w o r k s h o p on Verification and Analysis o f Logic Programs, University o f Pisa, 1996. [17] J.M. Martin. A.M. King, Generating efficient, terminating logic programs, in: T A P S O F T 97: Proceedings, Springer, Berlin, 1997. [I 8] L. Naish. A u t o m a t i n g control o f logic programs, Journal o f Logic P r o g r a m m i n g 2 (3) l 1985) 167-! 83. [19] L. Naisl~, C'ocoutining and the construction o f terminating logic p r o g r a m s , Technical R e p o r t 92/5, D e p a r t m e n t o f C ~ m p u t e r Science, University of Melbourne, 1992. [20] N.J. Nilsson, Principles o f Artificial Intelligence, Springer, Berlin, 1982, [21] R . K . K . Rao, T e r m i n a t i o n Characteristics o f Logic Programs. Ph.D. Thesis, University o f Bombay, 1993. [221 U.S. Reddy. O n the relationship between logic and functional languages, in: D. D e G r o o t , G. L i n d s t r o m (Eds.), Functional and Logic P r o g r a m m i n g , Prentice-Hall, Englewood Cliffs, NJ, 1986, pp. 3--36. [23] D. De Sehreye, S. Decorte, T e r m i n a t i o n of logic programs: T h e never ending story, Journal o f Logic P r o g r a m m i n g 19 (20) (1994) 199-260. [24] D. De Schreye, K. Verschaetse, M. Bruynooghe, A f r a m e w o r k for the analysis a n d termination o f definite logic p r o g r a m s with respect to call patterns, in: Proceedings o f the International Conference on Fifth G e n e r a t i o n C o m p u t e r Systems, ICOT, Tokio, 1992. [251 J. Thorn, J. Zobel, N U - p r o l o g reference m a n u a l , Technical R e p o r t 86tl. D e p a r t m e n t o f C o m p u t e r Science. University o f Melbourne, 1986. [26] M.H. van Emden, G.J. de Lucena, Predicate logic as a language for parallel p r o g r a m m i n g , in: K,L. Clark, S.-A. T/irnlund (Eds.), Logic Programming, Academic Press, L o n d o n , 1982. [27] L. X'ieille, Recursi,,e query processing: the power o f logic, Theoretical C o m p u t e r Science 69 ( i ) (1989) 1-53.