transformation-based bottom-up computation of the well ... - CiteSeerX

0 downloads 0 Views 424KB Size Report
our program transformations, is equivalent to the original program under a wide range of semantics. .... One such transformation is the deletion of tautological rules like p p ^q: ..... This start set can be constructed using O(n) derivation steps.
J. LOGIC PROGRAMMING 1999, VOLUME ??:1{36

1

TRANSFORMATION-BASED BOTTOM-UP COMPUTATION OF THE WELL-FOUNDED MODEL

STEFAN BRASS, JU RGEN DIX, BURKHARD FREITAG AND ULRICH ZUKOWSKI .

We present a bottom-up algorithm for the computation of the wellfounded model of non-disjunctive logic programs. Our method is based on the elementary program transformations studied by Brass and Dix [BD94, BD97, BD98a]. However, their \residual program" can grow to exponential size, whereas for function-free programs our \program remainder" is always polynomial in the size of the extensional database (EDB). As in the SLG-resolution of Chen and Warren [CW93, CW96, CSW95], we do not only delay negative but also positive literals if they depend on delayed negative literals. When disregarding goal-directedness, which needs additional concepts, our approach can be seen as a simpli ed bottom-up version of SLG-resolution applicable to range-restricted Datalog programs. Since our approach is also closely related to the alternating xpoint procedure [VG89, VG93], it can possibly serve as a basis for an integration of the resolution-based, xpoint-based, and transformation-based evaluation methods.

Keywords: well-founded semantics, bottom-up evaluation, alternating xpoint, partial evaluation

/

This is a completely revised and extended version of [BZF97] and [ZBF97], originally presented at the post-conference workshop on Non-monotonic Extensions of Logic Programming following JICSLP'96, Bad Honnef, Germany, September 7{8, 1996 and at the conference on Logic Programming and Nonmonotonic Reasoning (LPNMR '97) in Dagstuhl, Germany, July 28{31, 1997. Address correspondence to Stefan Brass, School of Information Sciences, University of Pittsburgh, 135 North Belle eld Ave., Pittsburgh, PA 15260, USA, e-mail: [email protected] and Jurgen Dix, University of Koblenz, Dept. of Computer Science, Rheinau 1, D-56075 Koblenz, Germany, e-mail: [email protected] and Burkhard Freitag, Ulrich Zukowski, Universitat Passau, Fakultat fur Mathematik und Informatik, D-94030 Passau, Germany, e-mail: ffreitag,[email protected] THE JOURNAL OF LOGIC PROGRAMMING

c Elsevier Science Inc., 1994 655 Avenue of the Americas, New York, NY 10010 0743-1066/94/$7.00

2

1. Introduction

The next generation of deductive database systems (see Ramakrishnan's article in [AMTW99]) will probably support the full class of normal programs. It seems also very likely that the well-founded semantics will be chosen by nearly all system designers, because it has a unique model. Furthermore, the time complexity of computing the well-founded model of a given intensional database (IDB) is polynomial in the size of the extensional database (EDB) [VGRS91]. In contrast, it has been shown by Marek and Truszczynski [MT91] that even for a propositional logic program P , determining whether P has a stable model is NP-complete. Extensions of deductive database systems that can deal with the well-founded semantics are already realized in XSB [CW96, SSW94, SS97, CSW95] and announced for LOLA [FSS91, ZF96a, ZF96b, ZF97]. The SLG-resolution of Chen and Warren [CW93, CW96, CSW95], as implemented in the XSB system, is the most prominent top-down method for the computation of the well-founded model of a normal program. In this paper we characterize \good" bottom-up methods in terms of elementary program transformations. Essentially, the bottom-up algorithms that compute the well-founded model of a normal program are:  The alternating xpoint approach, introduced by Van Gelder [VG89, VG93] and further developed by Kemp, Stuckey and Srivastava [KSS95].  The computation of the residual program, suggested by Bry [Bry89, Bry90] and independently by Dung/Kanchanasut [DK89a, DK89b], and extended by Brass and Dix [BD97, BD98b]. The alternating xpoint procedure is known to have eciency problems, since in every iteration many facts have to be recomputed. The residual program approach avoids recomputations of this kind. But it is possible that the residual program can grow to exponential size while for function-free programs the alternating xpoint approach guarantees a number of derived facts that is polynomial in the size, i.e. the number of tuples, of the extensional database (EDB). However, the residual program contains important information which is not provided by the alternating xpoint method. It is natural to ask for an algorithm which combines the advantages of both bottom-up approaches. In this paper, we present a framework for the computation of the well-founded model that is based on the residual program method but guarantees polynomial complexity like the alternating xpoint procedure. The residual program method is based on elementary program transformations and the concept of conditional facts. Conditional facts are ground rules having exactly those negative literals in their bodies that can not (yet) be resolved because their complement is not (yet) known to be true or false. This can be regarded as a non-procedural equivalent of the delay operation needed in SLG-resolution [CW93, CW96, CSW95]. Like in SLG-Resolution, the key idea to avoid the exponential blow-up of the residual program is to delay not only negative literals but also positive literals which depend on delayed negative literals. The transformation-based program remainder method which we propose in this paper can be seen as a bottom-up equivalent of SLG-resolution for the case of range-restricted function-free programs. We feel that the simplicity of our method which is mainly due to its level of abstraction is one of its particularly appealing features. Another advantage is the

3

fact that the causes for one or more atoms being unde ned can be made visible. The \unde ned" truth value is something unusual, often some kind of \localized error message". In a system based on the residual program or program remainder, we can display the dependencies between the delayed facts directly. This information is not available in the alternating xpoint method. We also emphasize that the power of the transformation method lies in its generality: Although the main motivation of this paper is to compute the wellfounded semantics, we will show that the program remainder, i.e., the \result" of our program transformations, is equivalent to the original program under a wide range of semantics. For instance, our transformation preserves stable models. So it can also be used as a preprocessing step for algorithms computing stable models, e.g. [BNNS93, BNNS94]. Interesting applications of stable models have recently been studied in [CMMT95, SZ95, DNK97] (see also [DFN99] which contains an overview). In the context of deductive databases it is customary to have a very simple (\naive") bottom-up procedure, which is later made goal-directed by means of a (suitable form of) magic set transformation (for applications to the wellfounded semantics, we refer to [KSS91, DN95]). This separation of issues makes the approach easier to understand and simpli es the correctness proofs. In this paper, we concentrate on the rst step. In [ZF96a], Zukowski and Freitag propose a mixed top-down and bottom-up procedure for the evaluation of modularly strati ed programs. The results we present in this paper give rise to hope that also for the class of normal programs a exible mixed evaluation method is feasible. The rest of the paper is organized as follows. After introducing preliminaries in Section 2, we recall the alternating xpoint procedure in Section 3 and the residual program approach in Section 4. In Section 5, we introduce our concepts to delay positive and negative literals, and in Section 6 we present a variant of the immediate consequence operator that is appropriate in this context. Section 7 describes an SCC-oriented variant that leads to further optimizations. Section 8 compares our approach to the alternating xpoint procedure. Section 9 concludes the paper with a summary.

2. Preliminaries A rule is of the form A L1 ^  ^ Ln , where the head A is an atom and each body literal Li is a positive literal, i.e. an atom, or a negative literal Li = not B . We treat the rule body as a set of literals and write also A B with B = fL1; : : : ; Lng. Consequently, a fact A is represented by the rule A ;. We also use the notation A B ^ not C with atom sets B and C . A program is a set of rules as introduced above. We consider normal, i.e., nondisjunctive, logic programs without function symbols. We assume that all rules are range-restricted, i.e. that each variable of the rule appears also in a positive body literal. Let P be a program. BASE (P ) denotes the Herbrand base of P , i.e. the set of all ground atoms. It might happen that our transformations eliminate some constants and predicates from the program, but we use the Herbrand base BASE (P ) of the original program. We write ground (P ) for the Herbrand instantiation of a

4

program P . For a ground program P we de ne the following sets: facts(P ) := fA 2 BASE (P ) j (A ;) 2 P g; heads(P ) := fA 2 BASE (P ) j there is a B such that (A B) 2 P g: ?  The complement of a literal L is denoted by  L , i.e.,  B = not B and, conversely, ?   not B = B . For a set S of literals, S denotes the set of the complements of the literals in S . S is consistent if and only if S \ S = ;. For a set S of literals we de ne the following sets: pos(S ) := fA 2 S j A is a positive literal g; neg(S ) := fA j not A 2 S g: Let P be a logic program. A partial interpretation for P is a consistent set I of ground literals such that pos(I ) [ neg(I )  BASE (P ); i.e., its set of atoms is a subset of the Herbrand base of P . A total interpretation for P is a partial interpretation I such that A 2 BASE (P ) =) A 2 I or not A 2 I; i.e., for each atom A of the Herbrand base of P either A or its complement is contained in I . We rely on the de nition of the well-founded partial model WP of P as given in [VGRS91]. An operational characterization of WP will be given in Section 3.

3. The Alternating Fixpoint Procedure Let us recall the de nition of the alternating xpoint procedure. We introduce an extended version of the immediate consequence operator that uses two di erent sets of facts for positive and negative subgoals, respectively. Actually, this is an adaption of the stability transformation found in [VG89, VG93] to our purposes. This operator has been introduced and investigated by Przymusinski in [Prz89, Prz90, Prz91]. De nition 3.1. (Extended Immediate Consequence Operator) Let P be a normal logic program. Let I and J be sets of ground atoms. The set TP;J (I ) of immediate consequences of I w.r.t. P and J is de ned as follows: TP;J (I ) := fA j there is A B 2 ground(P ) with pos(B)  I and neg(B) \ J = ;g: If P is de nite, the set J is not needed and we obtain the standard immediate consequence operator TP by TP (I ) = TP;;(I ).

TP;J checks negative subgoals against the set of possibly true atoms that is supplied as the argument J . This allows the following elegant formulation of the alternating xpoint procedure. De nition 3.2. (Alternating Fixpoint Procedure) Let P be a normal logic program. Let P + denote the subprogram consisting of the de nite rules of P . Then

5

the sequence (Ki ; Ui )i0 with sets Ki of true (known) facts and Ui of possible (unknown) facts is de ned by: K0 := lfp(TP ) U0 := lfp(TP;K ) i > 0 : Ki := lfp(TP;Ui? ); Ui := lfp(TP;Ki ): The computation terminates when the sequence becomes stationary, i.e., when a xpoint is reached in the sense that (Kj ; Uj ) = (Kj+1 ; Uj+1 ): This computation schema is called the Alternating Fixpoint Procedure (AFP). +

0

1

Theorem 3.3 ([VG93]) Let the sequence (Ki; Ui )i0 be de ned as above. Then there is a j  0 such that (Kj ; Uj ) = (Kj+1 ; Uj+1 ): The well-founded model WP of P can be directly derived from the xpoint (Kj ; Uj ), i.e., WP = fL j L is a positive ground literal and L 2 Kj or L is a negative ground literal not A and A 2 BASE (P ) ? Uj g: The following results which follow easily from the anti-monotonicity of TP;J w.r.t. J are also stated in [KSS95]. Lemma 3.4 Let the sequence (Ki; Ui)i0 be de ned as above. Then the following holds for i  0: Ki  Ki+1 ; Ui+1  Ui ; Ki  Ui . Example 3.5. [Even Numbers] Consider the following logic program EvenNum even(0): even(1) not even(0): even(2) not even(1): even(3) not even(2): .. . even(n) not even(n ? 1): It de nes the even numbers between 0 and some xed even number n. We would expect that it is possible to compute the well-founded model of this program in linear time w.r.t. n. The alternating xpoint procedure produces the following sequence: K0 = feven(0)g U0 = feven(0); even(2); even(3); : : :; even(n)g K1 = feven(0); even(2)g U1 = feven(0); even(2); even(4); even(5); : : :; even(n)g K2 = feven(0); even(2); even(4)g U2 = feven(0); even(2); even(4); even(6); even(7); : : :; even(n)g .. . Kn=2 = Un=2 = feven(0); even(2); even(4); : : :; even(n)g: Apparently, the alternating xpoint approach needs n iterations, each deriving a number of facts that is linear in n. So the total time complexity is at least quadratic in n.

6

4. Elementary Program Transformations Brass and Dix [BD94, BD97, BD98a, BD98b] have introduced a framework for

studying and computing negation semantics by means of elementary program transformations. De nition 4.1. (Semantics) A semantics is a mapping S , which assigns to every logic program P a set S (P? ) of (partial)  models of P such that S is \instantiation invariant", i.e. S (P ) = S ground (P ) .

Here, we are mainly interested in the well-founded semantics WFS(P ) := fWP g. However, the stable model semantics, which assigns to every program P the set of stable models, also has the properties required below. Due to the instantiation invariance, a semantics S is completely determined by its values on ground programs. Therefore, we de ne program transformations only on ground programs: De nition 4.2. (Program Transformation) A program transformation is a relation 7! between ground logic programs. A semantics S allows a transformation 7! i S (P1 ) = S (P2 ) for all P1 and P2 with P1 7! P2 .

One such transformation is the deletion of tautological rules like p

p ^ q:

De nition 4.3. (Deletion of Tautologies) Let P1 and P2 be ground programs. Program P2 results from program P1 by deletion of tautologies (P1 7!T P2 ) i there is A B 2 P1 such that A 2 B and P2 = P1 ? fA Bg.

Another very important program transformation is the \unfolding" of a body literal. De nition 4.4. (Unfolding) Let P1 and P2 be ground programs. Program P2 results from program P1 by unfolding (P1 7!U P2 ) i there is a rule A B in P1 and a positive literal B 2 B such that ?   ?  P2 = P1 ? fA Bg [ A (B ? fB g) [ B0 B B0 2 P1 :

Next, if we have a rule like p weaker rule like p q ^ r:

q, it should be possible to delete a logically

De nition 4.5. (Deletion of Nonminimal Rules) Let P1 and P2 be ground programs. Program P2 results from the program P1 by deletion of non-minimal rules (P1 7!M P2 ) i there are rules A B and A B0 in P1 such that B  B0 and P2 = P1 ? fA B0g.

The next two transformations describe the evaluation of negative body literals in trivial cases.

7

De nition 4.6. (Positive Reduction) Let P1 and P2 be ground programs. P2 results from P1 by positive reduction (P1 7!P P2 ) i there is a rule A B in P1 and a negative literal not B 2? B such that there  is no rule about B in P1 , i.e., B 2= heads(P1 ), and P2 = P1 ? fA Bg [ A (B ? fnot B g) . De nition 4.7. (Negative Reduction) Let P1 and P2 be ground programs. P2 results from P1 by negative reduction (P1 7!N P2 ) i there is a rule A B in P1 and a negative literal not B 2 B such that B appears as a fact in P1 , i.e., B 2 facts(P1 ), and P2 = P1 ? fA Bg. De nition 4.8. Let 7!R be the rewriting system consisting of the above ve transformations, i.e. 7!R := 7!T [ 7!U [ 7!M [ 7!P [ 7!N .

As shown in [BD98a, BD97] 7!R has some nice properties (for the non-disjunctive case, see also [Bra96]) which we summarize in the following. De nition 4.9. (Normal form) A program P 0 is a normal form of a program P w.r.t. a transformation 7! i

1. P 7! P 0 , and 2. P 0 is irreducible, i.e. there is no program P 00 with P 0 7! P 00 . For 7!R we have the following strong normal form theorem.

Theorem 4.10 (Residual Program [BD98b, BD98a]) The rewriting system 7!R is

1. terminating, i.e. every ground program P has a normal form P 0 , and 2. con uent, i.e. for all ground programs P; P1 ; P2 with P 7!R P1 and P ! 7 R P2 ,   there is a program P3 with P1 7!R P3 and P2 7!R P3 . Thus, every ground program P has a unique normal form, which is called the residual program res (P ) of P . In fact, as shown in [BD98a] we even have strong termination (i.e. every chain of transformations is terminating) for fair sequences of transformations. As shown in [BD98b, BD94], the well-founded semantics is the weakest semantics which allows the above transformations. We can directly \read o " the well-founded model from the residual program: De nition 4.11. (Known Literals) Let P be a ground program and let S be a set of ground atoms. The set of positive and negative ground literals with atoms in S having an obvious truth value in P is denoted by known S (P ): known S (P ) := fL j L 2 S is a positive ground literal and L 2 S \ facts(P ) or L is a negative ground literal not A and A 2 S ? heads(P )g:

Theorem 4.12 (Computation of WFS [BD97, BD94, BD98b])

The well-founded model WP of a program P consists of those positive and negative

8

ground literals which have an obvious truth value in the residual program, i.e., ?  WP = known BASE (P ) res (P ) : As the following example shows, the residual program needs only a linear number of transformation steps, whereas the alternating xpoint procedure needs a quadratic number of steps. Example 4.13. [Even Numbers revisited] Consider the logic program EvenNum of Example 3.5. With the program transformations de ned above, EvenNum can be transformed into the residual program res (EvenNum) as follows: EvenNum = f even(0); even(1) not even(0); even(2) not even(1); even(3) not even(2);

:::; even(n) 7!N f even(0); even(2) even(3) :::; even(n) 7!P f even(0); even(2); even(3) :::; even(n) .. .

not even(n ? 1) g not even(1); not even(2); not even(n ? 1) g not even(2); not even(n ? 1) g

7!R f even(0); even(2); even(4); : : :; even(n) g:

It is possible to store the rules in such a data structure that each step can be done in constant time ([BD95, BZF97]). Then the residual program res (EvenNum) can be derived in O(n) transformation steps. In contrast, the alternating xpoint procedure (cf. Example 3.5) needs a quadratic number of derivation steps. There are cases where the residual program can grow to exponential size. This problem with the delayed evaluation of negative literals was already noted in [CW96]. Example 4.14. [Exponential Residual Program] Consider the following program Exp. p(0): p(X ) p(Y ) ^ succ (Y; X ) ^ not q(Y ): p(X ) p(Y ) ^ succ (Y; X ) ^ not r(Y ): q(X ) succ (X; ) ^ not q(X ): r(X ) succ (X; ) ^ not r(X ):

9

succ(0; 1): .. .

succ(n ? 1; n): The rules about q and r ensure that the corresponding negative body literals must be delayed. But then the set of delayed literals in the body of a rule about p encodes the derivation path for the p-fact in the head. The following diagram depicts all possible paths on which a p-fact can be derived:

not q(0)

?? p(0) @@

not q(1)

@@ R ?? p(1)  @@ ??

not r(0)

@@R p(2) ??

...

not r(1)

So the residual program contains the following conditional facts about p: p(0): p(1) not q(0): p(1) not r(0): p(2) not q(0) ^ not q(1): p(2) not q(0) ^ not r(1): p(2) not r(0) ^ not q(1): p(2) not r(0) ^ not r(1): .. . It is obvious that every possible path is encoded in the residual program. Each possible fact p(n) is represented by 2n conditional facts.

5. Delaying Positive and Negative Literals As we have seen in Example 4.14, the residual program can grow to exponential size. The main reason for this is the unfolding transformation which may replace a positive body literal by an exponential number of combinations of negative literals. In the rewriting system 7!R (De nition 4.8) only negative body literals are delayed, positive body literals are eliminated by unfolding and deletion of tautologies, even if their truth value is not yet known. In contrast to [BD97, BD98b], we propose not to replace a positive body literal A by the bodies of rules about A, but to delay the processing of positive body literals until their truth value is obvious. This simply means that we do not allow transformations on positive body literals except when they are given as facts (and thus known to be true) or do not occur in any rule head (and thus are known to be false). Thus, we adapt the de nition of a conditional fact from [Bry89, Bry90, BD97, BD98b, BD98a] by allowing also positive literals in the condition:

10

De nition 5.1. (Conditional Fact) A conditional fact A C is a ground rule containing possibly both positive and negative delayed literals in the body.

To evaluate delayed negative literals when their truth value becomes known, we have the two transformations positive reduction 7!P and negative reduction 7!N (cf. De nitions 4.6 and 4.7). Let us now consider positive body literals. If there is a fact B , we obviously should be able to \simplify away" the condition B : De nition 5.2. (Success) Let P1 and P2 be ground programs. P2 results from P1 by success (P1 7!S P2 ) i there is a rule? A B in P1 and a positive literal B 2 B such that B 2 facts(P1 ), and P2 = P1 ?fA Bg [ A (B ?fB g) .

Lemma 5.3 If a semantics S allows unfolding and the deletion of nonminimal

rules, then it also allows success. Proof. Success corresponds to unfolding, when the program contains a fact B ; and B is replaced by ;. However, there might be further rules about B besides this fact. But then the resulting rules are certainly non-minimal, so we can remove them with 7!M . 2 Next, when there is no longer any possibility to prove a literal B , we can remove rule instances depending on B : De nition 5.4. (Failure) Let P1 and P2 be ground programs. P2 results from P1 by failure (P1 7!F P2 ) i there is a rule A B in P1 and a positive literal B 2 B such that there is no rule about B in P1 , i.e., B 2= heads(P1 ), and P2 = P1 ? fA Bg.

Lemma 5.5 If a semantics S allows unfolding, then it also allows failure. Proof. Obviously, the transformation 7!F is a very special case of unfolding, i.e., if there are no rules about the unfolded literal.

2

De nition 5.6. Let 7!PNSF denote the rewriting system which evaluates negative literals by positive and negative reduction and positive literals by success and failure: 7!PNSF := 7!P [ 7!N [ 7!S [ 7!F :

All sensible semantics should allow these transformations: Lemma 5.7 Let S be a semantics which allows unfolding, deletion of nonminimal rules, and positive and negative reduction. Then also P1 7!PNSF P2 =) S (P1 ) = S (P2 ) for all ground programs P1 and P2 . Proof. The lemma follows immediately from the Lemmata 5.3 and 5.5. 2 Example 5.8. [Exponential Residual Program Revisited] Starting from the program of Example 4.14, the rewriting system 7!PNSF yields

11

the following irreducible rules about p: p(0): p(1) not q(0): p(1) not r(0): p(2) p(1) ^ not q(1): p(2) p(1) ^ not r(1): p(3) p(2) ^ not q(2): p(3) p(2) ^ not r(2): .. .

Each possible fact p(n) (for n  1) is represented by exactly 2 rules. If we allow full unfolding of the positive body literals, we immediately get the exponential residual program of Example 4.14. The next theorem states that the four transformations success, failure, positive and negative reduction correspond exactly to the Fitting operator P , which is the procedural counterpart of the 3-valued completion comp3 (we refer to [Fit85, Dix95] for a detailed exposition of P ). Theorem 5.9 (7!PNSF corresponds to lfp(P )) The rewriting system 7!PNSF is con uent and strongly terminating. It therefore induces a normal form fitt(P ) for any program P . The known literals with respect to this normal form constitute exactly the least xpoint lfp(P ) of Fitting's operator P : ?  lfp(P ) = known BASE (P ) fitt(P ) : We also note that this normal form fitt(P ) can be computed in linear time (see Section 7 for a detailed investigation). Proof. See Appendix A. 2 In the approach of [BD97, BD98b], where only negative literals are delayed, the two reductions 7!P and 7!N are sucient to compute the well-founded model. The transformations 7!S and 7!F seem to be the corresponding operations for positive literals. However, they are not strong enough. Whereas the well-founded semantics depends on negative loops to determine unde ned truth values, positive loops have to be detected and removed. Example 5.10. [Positive Loop] Consider the following program Loop:

p: q q r

not p: r: q:

We can apply negative reduction to delete q not p, since not p is obviously false. But 7!PNSF does not allow to delete q r and r q. Apparently our reductions are still too weak. As for the residual program, we

12

want to obtain the well-founded model of a program directly from its normal form w.r.t. the rewriting system used. This is not possible in Example 5.10. In the rewriting system 7!R we could unfold one of the two last rules and delete the resulting tautology by the application of 7!T . Since we do not want to allow full unfolding, we have to introduce a new transformation that detects this kind of tautology but does not imply the risk of an exponential blow-up. In other words: weakening unfolding forces us to strengthen deletion of tautologies. This strengthening is called Loop Detection . De nition 5.11. (Loop Detection) Let P1 and P2 be ground programs. P2 results from P1 by loop detection (P1 7!L P2 ) i there is a set A of ground atoms such that1 1. for each rule A B in P1 , if A 2 A, then B \ A 6= ;, 2. P2 := fA B 2 P1 j A 62 Ag, 3. P1 6= P2 .

Note that because of the rst condition, the deleted rules have an atom from the set A in the head and in the body. In contrast to the deletion of tautologies, which requires exactly the same atom in head and body, any element of A suces (possibly di erent ones in head and body). In order for this to work, we need a more global perspective on the program and ensure that all rules about the atoms in A are tautologies in this more extensive sense. This globality is unfortunately quite expensive, and we will later try to avoid the explicit application of loop detection as far as possible. At rst, it might seem that we have to \guess" an appropriate set A. However, such a set A is nothing else than an unfounded set (cf. [VGRS91]). The greatest unfounded set consists of all positive ground atoms which are not possibly true, i.e., cannot be derived by assuming all negative literals to be true. According to De nition 3.1, the extended immediate consequence operator TP ;; computes the possibly true atoms. Therefore, the greatest unfounded set is given by BASE (P ) ? lfp(TP ;; ). It has the property required for A in De nition 5.11: Lemma 5.12 (Loop Detection) Let P1 be a ground program and let  P2 = (A B) 2 P1 A 2 lfp(TP ;; ) : Then: 1. P1 7!L P2 (unless P2 = P1 ), and 2. P2 is irreducible wrt 7!L . Proof. See Appendix A. 2 As a consequence we see that also the operation of loop detection can be performed in polynomial time w.r.t. the size of the EDB: We compute lfp(TP ;; ) and 1

1

1

1

1 In our earlier papers, we have de ned loop detection such that all rules are deleted with a positive body atom in A. This has the disadvantage, however, that loop detection is not local to SCCs. For instance, in = f g[f g, the old version of loop detection would delete all three rules. Then the useful Lemma 7.1 would not hold. P

p

q; q

p

r

q

13

delete all rules A B with A 62 lfp(TP ;; ). So it is neither necessary to construct all possible sets A to check whether 7!L is applicable, nor it is necessary to explicitly construct the greatest unfounded set A as the complement of lfp(TP ;; ). The following lemma shows that loop detection is a special case of deletion of tautologies and unfolding. Lemma 5.13 If P1 7!L P2 , then also P1 7!TU P2. Thus any semantics S , which allows unfolding and deletion of tautologies, also allows loop detection. Proof. See Appendix A. 2 1

1

Example 5.14. [Loop Detection] Consider again the program Loop of Example 5.10:

p: q q r

not p: r: q:

After deleting the second rule by negative reduction, the resulting program Loop0 is irreducible w.r.t. 7!PNSF . Now loop detection has to be applied. First, the set fpg of possible facts is computed by iterating the TLoop0;; operator. We have A = fp; q; rg ? fpg. According to De nition 5.11 the last two rules are deleted because their heads q and r are contained in A. Note that in operational terms it would have been sucient to test whether q and r are elements of fpg = lfp(TLoop0;; ). It is not necessary to explicitly compute the complement. Example 5.15. [Repeated Loop Detection] Unfortunately, a single application of the (still) expensive loop detection is not always sucient for computing the wellfounded model:

p1 : q1 q1 r1 p2 q2 q2 r2

not p1: r1 : q1 :

not q1: not p2: r2 : q2 :

First, negative reduction eliminates the second rule because not p1 is obviously false. In the next step, we need to apply loop detection in order to get rid of the rules q1 r1 and r1 q1 . After that we can evaluate not q1 to true and apply positive reduction and then not p2 to false and apply negative reduction. Now loop detection is again necessary to delete the rules q2 r2 and r2 q2 . Example 5.15 shows that we must iterate all ve transformations. Obviously, loop detection ist the most expensive transformation. Later, we will determine classes of programs for which no or only a limited number of applications of loop detection is

14

necessary. De nition 5.16. Let 7!X denote our nal rewriting system: 7!X := 7!P [ 7!N [ 7!S [ 7!F [ 7!L:

Thus 7!X can be seen as the extension of Fittings least xpoint operator P by Loop-Detection (see Theorem 5.9). A normal form w.r.t. the rewriting system 7!X is called a program remainder: De nition 5.17. (Program Remainder) Let P be a program. Let the program Pb satisfy 1. ground (P ) 7!X Pb,

2. Pb is irreducible w.r.t. 7!X , i.e. there is no P 0 with Pb 7!X P 0 . Then Pb is called a program remainder of P . We will later see that every program has exactly one program remainder and write rem (P ) for the remainder Pb of P .

Lemma 5.18 Every program P has a program remainder, i.e. the rewriting system 7!X is terminating. Proof. All our transformations strictly reduce the total number of occurring literals. So if P0 := ground (P ) and Pi is any program with Pi?1 7!X Pi , i  1, then we must reach a program Pn in which no further transformation can be applied.

This is a program remainder of P . 2 Note that only polynomially many transformations are needed to reach the program remainder, and each of our transformations is computable in polynomial time w.r.t. the size of the EDB, i.e. the number of tuples. As in [VGRS91] we exploit the fact that if the IDB, i.e. the set of rules with non-empty bodies, is assumed to be xed, the size of the Herbrand base and also the size of the Herbrand instantiation of the program are polynomial in the size of the EDB. Further, all transformations operate on subsets of the Herbrand base (unconditional facts) or on programs derived from subsets of the Herbrand instantiation (conditional facts). A program remainder is equivalent to the original program under the wellfounded and stable model semantics, and, in fact, under a large class of semantics which allow the elementary transformations introduced in Section 4: Theorem 5.19 (Equivalence of Program Remainder) Let S be any semantics which allows unfolding, deletion of tautologies, positive and negative reduction, and the deletion of nonminimal rules. Then S (Pb) = S (P ) for every program P with remainder Pb. Proof. This follows immediately from the Lemmata 5.7 and 5.13. 2 The program remainder behaves like the residual program of [BD98b, BD98a]. A literal A is true in the well-founded model i A is contained as a fact in the program remainder, A 2 facts(Pb). Conversely, the well-founded model satis es not A i A does not appear in any rule head of the program remainder, i.e., A 2= heads(Pb).

15

In other words, in the well-founded model exactly the literals of known BASE (P ) (Pb) (cf. De nition 4.11) have a de nite truth value. All other literals are unde ned in the well-founded model. Theorem 5.20 (Computation of Well-Founded Semantics) For every program P and remainder Pb, the well-founded model WP of P satis es exactly those positive and negative literals which are immediately obvious in Pb, i.e. WP = known BASE (P ) (Pb): Proof. See Appendix A. 2 From this it is also simple to derive an explicit characterization of the program remainder: From the Herbrand instantiation of the program we obtain the program remainder by 1. deleting every rule instance with a body literal which is false in the wellfounded model, and 2. removing from the remaining rule instances the body literals which are true in the well-founded model. This is a kind of Gelfond/Lifschitz-transformation [GL88]. Theorem 5.21 (Program Remainder) Let Pb be a remainder of a program P . The following holds:  Pb = A (B ? WP ) A B 2 ground (P ) and for every B 2 B: B 62 WP : Thus the remainder of a program is uniquely determined and we will write in the following rem (P ) for the program remainder of P . Proof. See Appendix A. 2

Corollary 5.22 The rewriting system 7!X is con uent, strongly terminating and the program remainder Pb is the unique normal form of ground (P ) w.r.t. 7!X . 6. Immediate Consequences with Delayed Literals

In the preceding sections, we applied program transformations to ground (P ) to derive the program remainder from which we could directly determine the wellfounded model. However, the construction of the ground instantiation of P is a very costly operation. Example 6.1. [Non-Ground Even Numbers] Consider the following logic program EvenNum2 which de nes again the even numbers between 0 and some xed even number n: even(0): even(X ) succ(Y; X ) ^ not even(Y ): succ(0; 1): .. . succ(n ? 1; n):

16

The alternating xpoint procedure produces the same sequence of sets Ki and Ui as in Example 3.5, except that here these sets contain also the given facts about succ. Again, a quadratic number of derivations is needed to compute the well-founded model. In this example, size of the ground instantiation of the program would also be quadratic in n (if we replace the variables X and Y independently by all numbers between 0 and n). Therefore, a quadratic number of transformation steps were needed, and we would lose the advantage of the transformation-based approach. In other examples, the e ort needed for the explicit grounding might even be worse. Our aim now is to construct an ecient algorithm which is based on our transformation system, by 1. xing the order of application of our transformations (due to the con uence we have maximal freedom), 2. choosing appropriate data structures to represent the current set of conditional facts, 3. avoiding to compute the entire ground instantiation of the program before applying the transformations. With respect to the order of the transformations, we choose to apply loop detection rst, and then to apply failure as long as possible. However, we can compute the result of this initial transformations without explicitly constructing the ground instantiation. In a sense, we simply have chosen the right representation of the current set of conditional facts: We represent the ground instantiation of P by the original program P . Of course, this is only possible because there is an algorithm which implements our transformations for this representation. The idea is to use a version of the TP -operator which keeps the bodies of the applied rule instances and assumes that all negative body literals are possibly true. In this way, we never touch rules in ground (P ) which contain positive body literals which are obviously false: We can start with rule instances having no positive body literals. The heads of these rule instances are possibly true, thus further rule instances having these atoms as positive body literals can be derived successively. De nition 6.2. (Immediate Consequences with Delayed Literals) Let P be a program. Given a set S of conditional facts the operator TP computes the following set of conditional facts:  TP (S ) := A B 2 ground(P ) pos(B)  heads(S ) :

Because all our rules are range-restricted, the condition pos(B)  heads(S ) binds all the variables and we never have to do \blind instantiation". For instance, in the above example, we would never consider the rule instance even(2) succ(0; 2) ^ not even(0): We use the existing succ-facts in order to instantiate the rule, and there is no fact succ(0; 2). The following propositions about the least xpoint of this operator are obvious:

17

1. lfp(TP )  ground (P ). ?  2. heads lfp(TP ) = lfp(TP;; ). (The operators are very similar, only TP;; \forgets" the bodies of the applied rule instances.) 3. lfp(TP ) contains only constants appearing in P . (This can be easily proven by an induction on the number of derivation steps.) 4. lfp(TP ) can be computed in time polynomial with respect to the size of the EDB. The following lemma embeds this kind of \intelligent grounding" into our transformation-based approach: Lemma 6.3 Let P be a normal program (not necessarily ground), TP be de ned as above, and let 7!LF := 7!L [ 7!F , i.e. the combination of loop detection and failure. Then we have ground (P ) 7!LF lfp(TP ): By the con uence of 7!X , this immediately implies lfp(TP ) 7!X Pb, so we can start the computation of the program remainder with lfp(TP ). Proof. See Appendix B. 2 Example 6.4. [Non-Ground Even Numbers Revisited] Consider again the program EvenNum2 of Example 6.1. Following Lemma 6.3, we can start the transformation process at lfp(TEvenNum2 ) instead of the complete ground instantiation: lfp(TEvenNum2 ) = f succ(0; 1);

:::; succ(n ? 1; n); even(0); even(1) succ(0; 1) ^ not even(0); :::; even(n) succ(n ? 1; n) ^ not even(n ? 1)g: This start set can be constructed using O(n) derivation steps. The following O(n) transformation steps transform the start set into the program remainder of EvenNum2. First we apply 7!S (Success) in order to evaluate the positive body literals and get:

EvenNum3 := f succ(0; 1); :::; succ(n ? 1; n); even(0); even(1) not even(0); :::; even(n) not even(n ? 1)g: Except the additional succ-facts, this is exactly our program EvenNum from Example 3.5. So with alternating applications of 7!N and 7!P , we get the well-founded model: fsucc(0; 1); : : : ; succ(n ? 1; n); even(0); even(2); : : : ; even(n)g:

18

In summary, O(n) derivation/transformation steps were needed.

7. SCC-oriented Evaluation In any practical implementation, one would not apply the transformations to the entire set of rules but partition the set of program rules according to the strongly connected components (SCCs) of its static dependency graph. This induces an order for applying our transformations which allows to delay less body literals, because often their truthvalue is already known. Also the intelligent grounding explained in Section 6 becomes even more selective when the information about lower SCCs is already available. As we will see, this has also advantages for the expensive loop check: Often the loop check performed implicitly by the intelligent grounding is already sucient, when the grounding is done component-wise. For many programs, no explict loop check is needed. As shown in [Tar72], the SCC's can be computed in linear time (see also [AHU87]). Thus, the additional e ort for the SCC-computation cannot make the overall complexity worse. With this additional optimization, we will get the algorithm of Berman, Schlipf and J. Franco [BSF95] from our transformation framework. This algorithm is probably the best method currently known for computing the well-founded model. By being able to derive this algorithm from our transformation framework, we demonstrate the practical impact of our approach. Furthermore, we believe that our presentation is easier to understand than the original reference. This is due to the fact that our program transformations operate only on the source program level. Also I. Niemela and P. Simons use the SCC-structure of the program in their smodels-system ([NS96], see also [DFN99] for a detailed description of the underlying computation). This system computes the stable semantics, but is based on an algorithm for computing the WFS. The following lemma is the reason why an SCC-wise application of our transformations is possible: Lemma 7.1 Let P1, P2, and P be ground programs such that P1 and P2 contain no atoms from heads(P ). Then

P1 7!X P2 () P1 [ P 7!X P2 [ P:

Proof. See Appendix C.

2

This means that we can ignore higher SCCs when we apply our transformations. When a transformation is applicable without considering the rules about higher SCCs (P1 7!X P2 ), it remains valid when we later add this rules (P1 [ P 7!X P2 [ P ). On the other hand, by adding the rules no new transformations become applicable. So once P1 is irreducible (i.e. there is no P2 with P1 7!X P2 ), we do not need to try later again to apply our transformations to its rules. The above lemma only ensures that in this case any transformation must necessarily change one of the new rules in P . But in fact, the irreducible P1 remains unchanged:

19

Lemma 7.2 Let P1 and P be ground programs, such that P1 is irreducible wrt 7!X and contains no atoms from heads(P ). If P1 [ P 7!X P 0 for some ground program P 0 , then P1  P 0 . Proof. For positive and negative reduction, success and failure this is a corol-

lary of Lemma 7.1, because these transformations change only a single rule and Lemma 7.1 implies that P must be changed. Now let P1 [ P 7!L P 0 , let A be the set of ground atoms as in De nition 5.11, and let A0 := A \ heads(P1 ). If A0 = ;, P1  P 0 immediately follows. Otherwise, A0 demonstrates that P1 is not irreducible: Loop detection can be applied to P1 based on A0 . 2 With these lemmas, it is clear that our transformations can be applied locally to the single program components. For this, we need to determine any evaluation sequence (based on the predicate dependency graph).

De nition 7.3. (Evaluation Sequence) Let P be a program (not necessarily ground). A sequence P (1) ; : : : ; P (m) of subsets of P is called an evaluation sequence for P i

1. P = P (1) [    [ P (m) , 2. P (i) \ P (j) = ; for i 6= j , 3. all predicates occurring in P (i) , 1  i  m, do not occur in the heads of P (j) for j > i. Of course, the single components P (i) are usually chosen to be as small as possible, i.e. either all rules about a single non-recursive predicate or all rules about a set of mutually recursive predicates. This is, however, not necessary for our theorems. Now we can compute the program remainder component-wise in the order given by any evaluation sequence: Corollary 7.4 Let P (1); : : : ; P (m) be an evaluation sequence for P , and let the ground programs R0 ; : : : ; Rm be de ned as follows:

 R0 := ;,  Ri is the program remainder of Ri?1 [ ground (P (i) ). Then Rm is the remainder of P . Furthermore, Ri?1  Ri for i := 1; : : : ; m.

We now can combine this component-wise approach with the intelligent grounding introduced in Section 6. Since when the program component P (i) is processed, the remainder Ri?1 of the lower program components is already known, a smaller set of ground instances can be computed. Especially, we do not need to generate a rule instance if a negative body literal is known to be false since the corresonding positive atom was already derived as a fact. Already with this simple optimization, the implicit loop detection performed by the grounding is sucient for many program components (see below): De nition 7.5. Let P be a program and R be a ground program. Given a set S of conditional facts, the operator T^P;R computes the following set of conditional

20

facts: T^P;R (S ) := fA

B 2 ground(P ) j pos(B)  heads(R) [ heads(S ) and neg(B) \ facts(R) = ;g:

If R is empty, we obtain the operator TP of De nition 6.2 by TP (S ) = T^P;;(S ). Of course, in a practical implementation, one would not only use the negative knowledge to create as few as possible ground instances, but also positive knowledge, and remove body literals which are already known to be true. So we could already apply success and positive reduction during the grounding phase. But it is theoretically simpler to separate the issues. The following lemma shows that computing the least xed point of this operator is only a more ecient implementation of certain transformations (this generalizes Lemma 6.3): Lemma 7.6 Let P be a program and R be a ground program. Then R [ ground (P ) 7!X R [ lfp(T^P;R ): Furthermore, if R is irreducible wrt 7!L , then R [ lfp(T^P;R ) is irreducible wrt 7!L . Proof. See Appendix C. 2 So the next re nement of our algorithm is: Corollary 7.7 Let P (1); : : : ; P (m) be an evaluation sequence for P , and let the ground programs R0 ; : : : ; Rm be de ned as follows:  R0 := ;,  Ri is the program remainder of Ri?1 [ lfp(T^P i ;Ri? ). Then Rm is the remainder of P . Furthermore, Ri?1  Ri for i := 1; : : : ; m. Our next goal is to avoid as much as possible the costly loop detection. First, loop detection is obviously only needed if a program component contains positive recursion. For instance, in the above examples, even calls itself only through negation. In this case, loop detection is not needed: Lemma 7.8 Let P and R be ground programs such that R contains no atoms from heads(P ). Let further R be irreducible wrt 7!X and P contain no predicate which depends positively on itself. If (R [ P ) 7!L P 0 , then (R [ P ) 7!F P 0 . Proof. See Appendix C. 2 So all applications of loop detection can in this case be replaced by failure | there are no \proper" applications of loop detection. This means that in the above algorithm, we can compute the program remainder Ri of Ri?1 [ lfp(T^P i ;Ri? ) by using only 7!PNSF . However, this is not the only case where this much more ecient computation is possible. If there is positive recursion, but not also negative recursion in the same program component, only a single application of loop detection suces. This is based on the fact that once a program is irreducible wrt loop detection, it can only become applicable again if we use negative reduction and thereby delete a possible support for a ground atom: ( )

1

( )

1

21

Lemma 7.9 Let P1 and P2 be ground programs with P1 7!PSF P2 (where 7!PSF is the union of positive reduction, success and failure). If P1 is irreducible wrt 7!L , then also P2 is irreducible wrt 7!L. Proof. See Appendix C.

2

But if all negative literals in a program component have a predicate from a lower component, we can rst do all applications of negative reduction and then the loop check based on the greatest unfounded set. After this, negative reduction and the loop check do not become applicable again. So a single application of the loop check suces, and, even better, this loop check is already done implictly by the intelligent grounding. Corollary 7.10 Let P be a program and R be a ground program which contains no predicates appearing in the heads of rules from P . Let further R be irreducible wrt 7!X and P contain no predicate which depends on itself both, positively and negatively (i.e. through negation). Then the program remainder of R [ P can be computed without explicit application of the loop check: R [ lfp(T^P;R ) 7!PNSF rem (R [ P ): We believe that programs containing positive and negative recursion in a single program component are very rare in practice. So although the explicit loop check is needed in the most general case, it will be used only very seldomly. We do not know a single sensible propositional program where positive and negative recursion occur in the same program component. In non-ground programs where the program components are based only on the predicates, it might happen, although rarely. In such a case, we propose to do the intelligent grounding as described above, and then to determine program components again. Since this can be done in linear time, the costs are most probably less than the time saved. And in almost all cases, now the ner program components will not contain positive and negative recursion together.

8. Relation to Alternating Fixpoint The computation of the set of all possibly true facts that is needed for the loop detection 7!L transformation (cf. De nition 5.11) is very similar to the computation of possible facts performed in each iteration step of the alternating xpoint procedure (c.f. De nition 3.2). There is an even closer correspondence as we will show in this section. Our main result is that a particular ordering of our transformations corresponds exactly to the alternating xpoint procedure. To characterize the computation performed by the alternating xpoint procedure we de ne a sequence (K i ; Ui )i0 where K i and Ui are constructed by the program transformations de ned in this paper and that is closely related to the sequence (Ki ; Ui )i0 computed by the alternating xpoint procedure (cf. De nition 3.2). De nition 8.1. (Alternating Fixpoint by Program Transformations) Let P be a normal logic program. We de ne the sequence (K i ; Ui )i0 of sets K i

22

and Ui of conditional facts as follows: K 0 is the normal form of ground (P ) w.r.t. 7!LFS , i.e., ground (P ) 7! lfp(TP ) 7! K 0, U0 is the normal formLFof K 0 w.r.t. S7!NLF , i > 0 : K i is the normal form of Ui?1 w.r.t. 7!PS , i > 0 : Ui is the normal form of K i w.r.t. 7!NLF . The computation terminates when the sequence becomes stationary, i.e., when a xpoint is reached in the sense that (K j ; Uj ) = (K j+1 ; Uj+1 ):

Lemma 8.2 (Con uence of 7!PS and 7!NLF ) In De nition 8.1, the normal form of Ui?1 w.r.t. 7!PS and the normal form of K i w.r.t. 7!NLF are uniquely

determined for the following reasons: 1. As one possible application of 7!PS never interferes with another possible application of 7!PS , the transformations 7!P and 7!S can be applied in any order and always produce the same result. In particular, it is legal to apply rst all possible 7!P transformations and then all possible 7!S transformations, because 7!S does not derive any new negative information, i.e., it does not delete any rule instances. 2. Analogously, to construct the normal form w.r.t. 7!NLF it is legal to apply all possible 7!N transformations rst before applying 7!LF , since loop detection does not derive any new facts, i.e., it only deletes rule instances. All applicable 7!N transformations can be applied in any order because they do not interfere and always delete the same set of rule instances. Since the greatest unfounded set of a program is the union of all unfounded sets, the normal form w.r.t. 7!L can be constructed by one application of 7!L w.r.t. this greatest unfounded set (cf. Lemma 5.12). Finally, by applying 7!F rule instances with positive body literals known to be false are deleted. In this phase, no new negative information is derived. If for an atom A all rule instances having A as head atom can be deleted by 7!F then fAg is an unfounded set and the corresponding rules have already been deleted by 7!L . Thus all possible applications of 7!F can be performed in any order. Comment 8.8.1. Due to Lemma 6.3 the sequence (K i ; Ui )i0 of De nition 8.1 reaches a xpoint (K j ; Uj ) and computes the program remainder Pb = K j = Uj .

In the rst step of the alternating xpoint procedure, de nitely true facts are derived by K0 = lfp(TP ). The same facts can be derived by applying the success transformation. For the computation of Ui , i  0, those possible facts that are still derivable are recomputed. Rule instances with negative subgoals whose complements are known to be true, i.e. that are contained in Ki , are not used. As a byproduct of this recomputation, the generation of any positive cycles is avoided. Disregarding rule instances with false negative subgoals corresponds to the negative reduction 7!N . +

23

However, by this reduction, positive cycles may be produced. Thus, loop detection 7!L has to be applied. Finally, 7!F is applied to remove rule instances with positive body literals known to be false, but without deriving new information. For the computation of Ki , i > 0, the alternating xpoint procedure derives more de nitely true facts by regarding those negative subgoals as true whose complements are not contained in Ui?1 . Newly derived true facts are used to derive more true facts by rule applications. At the program transformation side, this corresponds to the positive reduction 7!P that deletes negative conditions whose complements are known to be false, i.e. not contained in heads(Ui?1 ). Facts obtained this way can be used to derive further facts by the success 7!S transformation that deletes positive conditions that are now known to be true. Example 8.3. [Even Numbers by Alternating Program Transformation] Consider the program EvenNum2 of Examples 6.1 and 6.4. The sequence (K i ; Ui )i0 as de ned in De nition 8.1 is computed as follows: K 0 = f even(0); succ(0; 1); : : :; succ(n ? 1; n); even(1) not even(0); : : : ; even(n) not even(n ? 1) g U0 = f even(0); succ(0; 1); : : :; succ(n ? 1; n); even(2) not even(1); : : : ; even(n) not even(n ? 1) g K 1 = f even(0); succ(0; 1); : : :; succ(n ? 1; n); even(2); even(3) not even(2); : : : ; even(n) not even(n ? 1) g U1 = f even(0); succ(0; 1); : : :; succ(n ? 1; n); even(2); even(4) not even(3); : : : ; even(n) not even(n ? 1) g .. . K n=2 = Un=2 = feven(0); succ(0; 1); : : :; succ(n ? 1; n); even(2); even(4); even(6); : : :; even(n) g The set K 0 is computed by xpoint computation performing O(n) derivation steps. Each set K i , for i > 0, is generated from Ui?1 by one single application of positive reduction: Ui?1 7!P K i : Each set Ui , for i  0, can be constructed from K i by one application of negative reduction as shown in Example 6.4: K i 7!N Ui : However, since Ui is de ned to be the normal form of K i w.r.t. 7!NLF , loop detection has to be applied, if possible. Following Lemma 5.12, the set poss := lfp(TUi ;; ) = K 0 [ feven(2); even(4); : : :; even(2i); even(2i + 1); : : : ; even(n)g serves as a reference set for loop detection. Since Ui does not contain any rules with positive body literals, the application of loop detection has no e ect. However, for each set Ui , i > 0, the set poss has to be actually computed, where each computation needs O(n) derivation steps. In summary, using this order of transformations that simulates the computation of the alternating xpoint procedure, the computation of the well-founded model of EvenNum2 needs O(n2 ) derivation/transformation

24

steps. This corresponds to the costs of the original alternating xpoint procedure as shown in Example 6.1. In contrast, by choosing another strategy, the time complexity can be reduced to O(n) as shown in Example 6.4. The relation between the alternating xpoint computation and the sequence of program transformations de ned above is described by the following theorem. Theorem 8.4 Let P be a normal logic program. Let (Ki; Ui)i0 and (K i ; Ui)i0 be the sequences given by De nitions 3.2 and 8.1, respectively. Then the following holds: Ki = facts(K i ) and Ui = heads(Ui ). Proof. See Appendix D. 2 Theorem 8.4 shows that the program transformation approach will never do more work than the alternating xpoint procedure. Although the set U0 contains more elements than U0 , the same number of derivation steps is needed to compute U0 . Consider the case that one possible fact A 2 U0 is represented by more than one extended conditional facts A C . These conditional facts are the rule instances used to derive the possible fact A. Then during the computation of U0 , the fact A is derived by the same number of rule instances. Only due to duplicate elimination, the cardinality of U0 is less than the number of derivation steps needed to compute U0 . For the computation of the set K i , i > 0, the same argument holds. Whenever a fact can be derived by the deletion of the body literals of several extended conditional facts, the fact is repeatedly derived in Ki by several rule instances, and the duplicates are eliminated afterwards. Of course, for the derivation of one fact in K i , several transformation steps are needed. But analogously, during the iteration of the TP;Ui? operator, also several body literals have to be checked before the head of one rule instance can be derived. As proposed in [KSS95], the sets Ki do not have to be recomputed in each iteration step, but can be maintained in terms of increments. This optimization is automatically performed by the transformation approach, since extended conditional facts that were transformed into facts by the deletion of their body literals are never changed afterwards. The most interesting part is the recomputation of the possible facts. Since the alternating xpoint procedure does not memorize by which rule instances the possible facts in Ui?1 have been derived, in each iteration step the set Ui has to be recomputed. In the transformation approach, possible facts do not always have to be recomputed. Possible facts that become false can rather be deleted directly if one of their body literals has become false. However, by this reduction, positive cycles can occur and have to be eliminated by the more costly transformation loop detection. Although, for many programs the number of applications of loop detection can be reduced, we have chosen the worst case for this comparison in the sense that in each iteration step the loop detection is applied. For loop detection, those facts have to be computed that are still possibly true. Negative reduction prevents us from using rule instances with negative body literals already known to be false. Extended conditional facts having positive body literals not derivable anymore are then deleted. Exactly the same is done by the alternating xpoint procedure by the computation Ui := lfp(TP;Ki ). Again, rule instances with negative body literals whose complements are in Ki are excluded. 1

25

This comparison illustrates that the computation performed by the alternating xpoint procedure corresponds to the alternating application of the transformations 7!PS and 7!NLF . However, loop detection is a costly operation since it needs a recomputation of all possible facts as it is done by the alternating xpoint procedure. Instead, the cheaper transformation failure should be preferred as long as no loop detection is needed. One possibility is to apply loop detection only if no application of 7!PNSF is possible. Due to Theorem 5.9 this corresponds to computing the least xpoint of Fitting's P operator. This way, many programs can be evaluated more eciently than by the alternating xpoint procedure, as illustrated in Examples 3.5, 6.1, 4.13, and 6.4. In the transformation approach all rule instances that are still relevant are explicitly stored. The alternating xpoint procedure needs to consider the same rule instances, but generates them on demand without storing them. Thus, while having a (sometimes) better time complexity, the transformation approach is expected to have a space complexity worse than the alternating xpoint approach. Note that the results of this section show that the alternating xpoint procedure can be seen as one particular implementation of our approach (given by choosing a xed ordering of applying the transformations).

9. Conclusions We have presented an algorithm for computing the well-founded semantics of function-free programs which is based on the set of elementary program transformations studied by Brass and Dix [BD94, BD97, BD98a, BD98b]. The time complexity of our algorithm is polynomial in the size of the EDB. If we would unfold all positive body literals in the program remainder (and delete tautologies), we would get the residual program plus some non-minimal conditional facts. So the program remainder can be seen as an ecient encoding of the residual program. It is equivalent to the original program under a wide range of semantics. In fact, our rewriting system 7!X is even weaker than the transformations used in [BD94, BD97, BD98a, BD98b] because full unfolding is not needed to compute the program remainder. While the computational complexity of the program remainder is decisively lower than that of the residual program, the \intellectual complexity" seems to be higher. One of the strengths of the residual program was its simplicity, and it seems that this has to be partially sacri ced, since we now have to cope with positive loops. But our method still operates on the source program level, and can be understood as a sequence of elementary program transformations. Obviously, our approach owes much to SLG-resolution developed by Chen and Warren [CW93, CW96, RSS+ 97, CSW95]. On the other hand we have obtained an important simpli cation by separating the discussion of the overall evaluation method from the issue of goal-directedness. The latter will have to be investigated in future extensions of our approach. We have shown that our method never does more work than the alternating xpoint approach. However, there are examples (such as Example 6.4), where our method is superior to the alternating xpoint approach.

26

The generality of our method gives rise to hope that it can serve as a basis for an integration of the resolution-based, xpoint-based, and transformation-based evaluation methods.

REFERENCES [AHU87.]

Alfred V. Aho, John E. Hopcroft, and Je rey D. Ullman. Data Structures and Algorithms. Addison-Wesley, 1987. [AMTW99.] K. R. Apt, V. Marek, M. Truszczynski, and D. S. Warren, editors. The Logic Programming Paradigm: Current Trends and Future Directions, LNAI, Berlin, 1999. Springer. [BD94.] Stefan Brass and Jurgen Dix. A disjunctive semantics based on unfolding and bottom-up evaluation. In Bernd Wol nger, editor, Innovationen bei Rechen- und Kommunikationssystemen, (IFIP '94-Congress, Workshop FG2: Disjunctive Logic Programming and Disjunctive Databases), pages 83{91, Berlin, 1994. Springer. [BD95.] Stefan Brass and Jurgen Dix. A General Approach to Bottom-Up Computation of Disjunctive Semantics. In J. Dix, L. Pereira, and T. Przymusinski, editors, Nonmonotonic Extensions of Logic Programming, LNAI 927, pages 127{155. Springer, Berlin, 1995. [BD97.] Stefan Brass and Jurgen Dix. Characterizations of the Disjunctive Stable Semantics by Partial Evaluation. Journal of Logic Programming, 32(3):207{ 228, 1997. (Extended abstract appeared in: Characterizations of the Stable Semantics by Partial Evaluation LPNMR, Proceedings of the Third International Conference, Kentucky , pages 85{98, 1995. LNCS 928, Springer.). [BD98a.] Stefan Brass and Jurgen Dix. Characterizations of the Disjunctive Wellfounded Semantics: Con uent Calculi and Iterated GCWA. Journal of Automated Reasoning, 20(1):143{165, 1998. (Extended abstract appeared in: Characterizing D-WFS: Con uence and Iterated GCWA. Logics in Arti cial Intelligence, JELIA '96 , pages 268{283, 1996. Springer, LNCS 1126.). [BD98b.] Stefan Brass and Jurgen Dix. Semantics of (Disjunctive) Logic Programs Based on Partial Evaluation. Journal of Logic Programming, accepted for publication, 1998. (Extended abstract appeared in: Disjunctive Semantics Based upon Partial and Bottom-Up Evaluation, Proceedings of the 12-th International Logic Programming Conference, Tokyo , pages 199{213, 1995. MIT Press.). [BNNS93.] Colin Bell, Anil Nerode, Raymond Ng, and V. S. Subrahmanian. Implementing stable semantics by linear programming. In Lus Moniz Pereira and Anil Nerode, editors, Logic Programming and Non-monotonic Reasoning, Proc. of the Second Int. Workshop (LPNMR'93), pages 23{42. MIT Press, 1993. [BNNS94.] Colin Bell, Anil Nerode, Raymond T. Ng, and V. S. Subrahmanian. Mixed Integer Programming Methods for Computing Non-Monotonic Deductive Databases. Journal of the ACM, 41(6):1178{1215, November 1994. [Bra96.] Stefan Brass. Bottom-up query evaluation in extended deductive databases. Habilitationsschrift, Universitat Hannover, Fachbereich Mathematik, 1996. http://www.informatik.uni-hannover.de/~sb/habil.html.

27 [Bry89.]

Francois Bry. Logic programming as constructivism: A formalization and its application to databases. In Proc. of the 8th ACM SIGACT-SIGMODSIGART Symposium on Principles of Database Systems (PODS'89), pages 34{50, 1989. [Bry90.] Francois Bry. Negation in logic programming: A formalization in constructive logic. In Dimitris Karagiannis, editor, Information Systems and Arti cial Intelligence: Integration Aspects, number 474 in LNCS, pages 30{46. Springer, 1990. [BSF95.] Kenneth A. Berman, John S. Schlipf, and John V. Franco. Computing the Well-Founded Semantics Faster. In A. Nerode, W. Marek, and M. Truszczynski, editors, Logic Programming and Non-Monotonic Reasoning, Proceedings of the Third International Conference, LNCS 928, pages 113{126, Berlin, June 1995. Springer. [BZF97.] Stefan Brass, Ulrich Zukowski, and Burkhard Freitag. Transformationbased bottom-up computation of the well-founded model. In Jurgen Dix, Luis Moniz Pereira, and Teodor C. Przymusinski, editors, Non-Monotonic Extensions of Logic Programming (NMELP'96), number 1216 in LNAI, pages 171{201. Springer, 1997. [CMMT95.] Pawel Cholewinski, V. Wiktor Marek, Artur Mikitiuk, and Miroslaw Truszczynski. Experimenting with nonmonotonic reasoning. In Leon Sterling, editor, Logic Programming, Proc. of the Twelfth Int. Conf. on Logic Programming (ICLP'95), pages 267{281. MIT Press, 1995. [CSW95.] Weidong Chen, Terrance Swift, and David S. Warren. Ecient Top-Down Computation of Queries under the Well-Founded Semantics. Journal of Logic Programming, 24(3):219{245, 1995. [CW93.] Weidong Chen and David S. Warren. Query-evaluation under the well founded semantics. In Proc. of the Twelfth ACM SIGACT-SIGMODSIGART Symposium on Principles of Database Systems (PODS'93), pages 168{179, 1993. [CW96.] Weidong Chen and David S. Warren. Tabled evaluation with delaying for general logic programs. Journal of the ACM, 43(1):20{74, 1996. [DFN99.] Jurgen Dix, Ulrich Furbach, and Ilkka Niemela. Nonmonotonic Reasoning: Towards Ecient Calculi and Implementations. In Andrei Voronkov and Alan Robinson, editors, Handbook of Automated Reasoning. ElsevierScience-Press, to appear 1999. [Dix95.] Jurgen Dix. A Classi cation-Theory of Semantics of Normal Logic Programs: I. Strong Properties. Fundamenta Informaticae, XXII(3):227{255, 1995. [DK89a.] Phan Minh Dung and Kanchana Kanchansut. A xpoint approach to declarative semantics of logic programs. In Proc. North American Conference on Logic Programming (NACLP'89), pages 604{625, 1989. [DK89b.] Phan Minh Dung and Kanchana Kanchansut. A natural semantics of logic programs with negation. In Proc. of the Ninth Conf. on Foundations of Software Technology and Theoretical Computer Science, pages 70{80, 1989. [DN95.] Lars Degerstedt and Ulf Nilsson. Magic Computation of Well-founded Semantics. In J. Dix, L. Pereira, and T. Przymusinski, editors, Nonmonotonic Extensions of Logic Programming, LNAI 927, pages 181{204. Springer, Berlin, 1995.

28 [DNK97.] [Fit85.] [FSS91.] [GL88.] [KSS91.] [KSS95.] [MT91.] [NS96.] [Prz89.] [Prz90.] [Prz91.]

[RSS+97.]

[SS97.] [SSW94.]

Y. Dimopoulos, B. Nebel, and J. Koehler. Encoding planning problems in non-monotonic logic programs. In Proceedings of the Fourth European Conference on Planning, Toulouse, France, September 1997. Springer-Verlag. To appear. Melvin C. Fitting. A Kripke-Kleene Semantics of logic Programs. Journal of Logic Programming, 4:295{312, 1985. Burkhard Freitag, Heribert Schutz, and Gunther Specht. LOLA - a logic language for deductive databases and its implementation. In Proc. 2nd International Symposium on Database Systems for Advanced Applications (DASFAA '91), Tokyo, Japan, April 2{4, 1991, pages 216{225, 1991. Michael Gelfond and Vladimir Lifschitz. The stable model semantics for logic programming. In Robert A. Kowalski and Kenneth A. Bowen, editors, Logic Programming, Proc. of the 5th Int. Conf. and Symp., pages 1070{ 1080, Cambridge, Mass., 1988. MIT Press. David B. Kemp, Peter J. Stuckey, and Divesh Srivastava. Magic Sets and Bottom-Up Evaluation of Well-Founded Models. In Vijay Saraswat and Kazunori Ueda, editors, Proceedings of the 1991 Int. Symposium on Logic Programming, pages 337{351. MIT, June 1991. David B. Kemp, Divesh Srivastava, and Peter J. Stuckey. Bottom-up evaluation and query optimization of well-founded models. Theoretical Computer Science, 146:145{184, 1995. Wiktor Marek and Miroslaw Truszczynski. Autoepistemic logic. Journal of the ACM, 38(3):588{619, 1991. Ilkka Niemela and Patrik Simons. Ecient Implementation of the Wellfounded and Stable Model Semantics. In M. Maher, editor, Proceedings of the Joint International Conference and Symposium on Logic Programming, pages 289{303, Bonn, Germany, September 1996. The MIT Press. Teodor Przymusinski. On the declarative and procedural Semantics of logic Programs. Journal of Automated Reasoning, 5:167{205, 1989. Teodor Przymusinski. Well-founded Semantics Coincides With ThreeValued Stable Semantics. Fundamenta Informaticae, XIII:445{463, 1990. Teodor Przymusinski. Three-valued non-monotonic formalisms and semantics of logic programs. Arti cial Intelligence, 49:309{343, 1991. (Extended abstract appeared in: Three-valued non-monotonic formalisms and logic programming. Proceedings of the First International Conference on Principles of Knowledge Representation and Reasoning (KR'89), Toronto, Canada, pages 341{348, Morgan Kaufmann, 1989.). Prasad Rao, K. Sagonas, T. Swift, D. S. Warren, and J. Freire. XSB: A System for Eciently Computing Well-Founded Semantics. In J. Dix, U. Furbach, and A. Nerode, editors, Logic Programming and Non-Monotonic Reasoning, Proceedings of the Fourth International Conference, LNAI 1265, pages 430{440, Berlin, June 1997. Springer. Chiaki Sakama and Hirohisa Seki. Partial Deduction in Disjunctive Logic Programming. Journal of Logic Programming, 32(3):229{245, 1997. Konstantinos Sagonas, Terrance Swift, and David S. Warren. XSB as an ecient deductive database engine. In Richard T. Snodgrass and Marianne Winslett, editors, Proc. of the 1994 ACM SIGMOD Int. Conf. on Management of Data (SIGMOD'94), pages 442{453, 1994.

29 [SZ95.]

V. S. Subrahmanian and Carlo Zaniolo. Relating stable models and AI planning domains. In Leon Sterling, editor, Logic Programming, Proc. of the Twelfth Int. Conf. on Logic Programming (ICLP'95), pages 233{247. MIT Press, 1995. [Tar72.] R. Tarjan. Depth rst search and linear graph algorithms. SIAM Journal on Computing, 1:146{160, 1972. [VG89.] Allen Van Gelder. The alternating xpoint of logic programs with negation. In Proc. of the Eighth ACM SIGACT-SIGMOD-SIGART Symposium on Principles of Database Systems (PODS'89), pages 1{10, 1989. [VG93.] Allen Van Gelder. The alternating xpoint of logic programs with negation. Journal of Computer and System Sciences, 47(1):185{221, 1993. [VGRS91.] Allen Van Gelder, Kenneth A. Ross, and John S. Schlipf. The well-founded semantics for general logic programs. Journal of the Association for Computing Machinary (JACM), 38:620{650, 1991. [ZBF97.] Ulrich Zukowski, Stefan Brass, and Burkhard Freitag. Improving the alternating xpoint: The transformation approach. In J. Dix, U. Furbach, and A. Nerode, editors, Proc. of the 4th Int. Conf. on Logic Programming and Non-Monotonic Reasoning (LPNMR'97), number 1265 in LNAI, pages 40{59. Springer, 1997. [ZF96a.] Ulrich Zukowski and Burkhard Freitag. Adding exibility to query evaluation for modularly strati ed databases. In Proc. of the 1996 Joint International Conference and Symposium on Logic Programming (JICSLP'96). September 2 { 6, 1996, Bonn, Germany, pages 304{319. MIT Press, 1996. [ZF96b.] Ulrich Zukowski and Burkhard Freitag. The di erential xpoint of general logic programs. In Dimitri Boulanger, Ulrich Geske, Fosca Gianotti, and Dietmar Seipel, editors, Proc. of the Workshop DDLP'96 on Deductive Databases and Logic Programming. 4th Workshop in conjunction with JICSLP'96. Bonn, Germany, September 2 { 6, 1996, volume 295 of GMDStudien, pages 45{56, St. Augustin, Germany, 1996. GMD. [ZF97.] Ulrich Zukowski and Burkhard Freitag. The Deductive Database System LOLA. In J. Dix, U. Furbach, and A. Nerode, editors, Nonmonotonic Extensions of Logic Programming, LNAI 1265, pages 375{386. Springer, Berlin, 1997.

A. Proofs of Section 5 Proof. (of Lemma 5.9) Strong termination is obvious, because all transformations

strictly reduce the number of literals occurring in the transformed program. To show con uence, we rst note that success commutes with all other three transformations: If P 7!S P1 , and P1 7!tr P 0 then P 7!tr P2 7!S P 0 and If P 7!tr P1 , and P1 7!S P 0 then P 7!S P2 7!tr P 0 for tr 2 ffailure; negative reduction; positive reductiong. This commutativity property does not hold between the other transformations: consider the program P

p q: r

not q: p:

30

If we rst apply negative reduction to remove the rst clause, we can then apply positive reduction to simplify the third rule of P . However, this simpli cation of the third rule is not possible if we start with P . To show con uence, it suces (using general results about con uent systems) to prove If P 7!X P1 and P 7!P N F P2 then P1 7!P N F P 0 and P2 7!P N F P 0 , for X 2 ffailure; negative reduction; positive reductiong = fF; N; P g. This follows immediately by checking that the following diagrams commute: 7 P N F P ?! ????! P2 ? ?

P? y

? yP

0 P1 ?7! ????  ! P PNF

7 P N F P ?! ????! P2 ? ?

N? y

? yN

0 P1 ?7! ????  ! P PNF

7 P N F P ?! ????! P2 ? ?

F? y

? yF

0 P1 ?7! ????  ! P PNF

It remains to show the relation to Fitting's least xpoint of P . This operator can be best explained if we view it as a mapping between 3-valued interpretations. If we denote the set of truthvalues by 3k , then P : 3k BP 7?! 3k BP ; I 7?! P (I ) is de ned as follows: (A is a ground atom) 8 > > > > > > > > > > > > > > > >
> > > > > > > > > > > > > > > :

t, if there is a clause A L1; : : : ; Ln in P with: | 8i  n we have:  I (Li ) = t. f, if for all clauses A L1; : : : ; Ln 2 P we have: | 9i  n with:  I (Li ) = f. u, otherwise.

Note that we start with the set h;; ;i and iterate P until a xpoint is reached. We will identify lfp(P ) with a three-valued model resp. with a set of literals. To determine the exact relationship between the iterations of P and the applications of our transformations, we de ne step1 (P ) := normal form of P after applying 7!S F and 7!P N stepi+1 (P ) := normal form of stepi (P ) after applying 7!S F and 7!P N Obviously (by inspection of the de nition of P ) we have the following result: P "i = known BASE (P ) (stepi (P )): Clearly, P reaches a xpoint if and only if the program P is irreducible with respect to 7!S F P N . The computation of lfp(P ) therefore corresponds to a speci c ordering of our transformations. 2 Proof. (of Lemma 5.12) 1. First we have to show that A = BASE (P ) ? lfp(TP ;; ) satis es the rst requirement in De nition 5.11, namely that for every rule A B in P1 , if A 2 A, then B \A 6= ;. Suppose that this were not the case. Then P1 would 1

31

contain a rule A B with pos(B)  lfp(TP ;; ) and A 62 lfp(TP ;; ). But this is impossible, since if pos(B) is already derived by TP ;; , then A will be derived in the next step. So loop detection can indeed be applied with this set A, and we get 1

1

1







B) 2 P1 A 2 lfp(TP ;; ) :

P2 = (A

1

2. The second part is to show that P2 is irreducible wrt 7!L . Suppose that loop detection were applicable to P2 based on some set A0 of ground atoms. Because of the second and third condition in De nition 5.11, an atom from A0 must appear in at least on rule head of P2 . By the construction of P2 , this atom is contained in lfp(TP ;; ). But this is impossible: We show by induction on i that A0 \ (TP ;; " i) = ;. For i = 0 this is trivial. Let now A B be applied in the i-th iteration. By the induction hypothesis, we know that pos(B) \ A0 = ; and thus B \ A0 = ;. Since A 2 (TP ;; " i), the rule is also contained in P2 . But then the rst condition in De nition 5.11 implies that A 62 A0 . 2 1

1

1

A be a set of ground atoms such that for all rules B in P1 , if A 2 A, then B \ A 6= ;. Let P2 := fA B 2 P1 j A 62 Ag. We have to show that P1 ? P2 , i.e. the rules with an atom from A in the head, can be deleted by unfolding and elimination of tautologies. We will show P1 7!TU P2 by induction on the number n of atoms from A, which actually appear in rule bodies of P1 ? P2 . If n = 0, no atom of A appears in a body of a rule from P1 ? P2 . But since each

Proof. (of Lemma 5.13) Let

A

of the deleted rules must have such an atom in the body, this means P1 = P2 , and thus P1 7!TU P2 trivially holds. Let now n > 0 and A0 2 A be an atom which appears in at least one body of a rule from P1 ? P2 . Then we rst eliminate all tautological rules containing A0 in head and body. These rules are contained in P1 ? P2 , so we are allowed to remove them. After that, we unfold all remaining occurrences of A0 in bodies of rules which also contain an atom from A in their head. Let the resulting program be called P10 . Then we have the following:

 After these operations, A0 does not appear in any rule body with an atom from A in the head (Since we have eliminated the tautologies rst, the rules about A0 do not contain A0 in their bodies.).

 Furthermore, no new atoms appear in the bodies of rules with a head from A (since A0 2 A), so the number n has decreased.  The rules which were deleted by the unfolding steps contain an element of A in the head, so they are contained in P1 ? P2 , and we are really allowed

to delete them. The rules which were generated by the unfolding step also contain an atom from A in the head (since the head is not changed by unfolding). Thus, P2 = fA B 2 P10 j A 62 Ag. The inductive hypothesis then gives P10 7!TU P2 , and thus together we have P1 7!TU P10 7!TU P2 . 2

32 Proof. (of Theorem 5.20) By Theorem 5.19 and since the well-founded semantics allows the transformations of the rewriting system 7!X , P and Pb have the same well-founded model, i.e., WP = WPb . Thus it suces to show that WPb = known BASE (P ) (Pb). We do this by executing the the alternating xpoint

procedure on Pb. 1. K0 = facts(Pb): It is clear that at least these facts are contained in K0 , i.e. facts(Pb)  K0 . To show equality assume that K0 ? facts(Pb) 6= ;. Let A L1 ^    ^ Ln be the rst rule with non-empty body applied by the alternating xpoint procedure. By De nition 3.2 the Li are all positive and contained in facts(Pb). But then the success transformation is applicable to Pb. This contradicts the irreducibility of Pb. 2. U0 = heads(Pb): The inclusion U0  heads(Pb) is trivial. Now assume that U0  heads(Pb) holds. Let A := BASE (P ) ? U0 and let A B be an arbitrary rule of Pb with A 2 A (such a rule exists because U0 6= heads(Pb)). If B \ A 6= ;, loop detection were applicable, contradicting the irreducability of Pb. Thus we have B \ A = ;, and therefore pos(B)  U0 . But then, since A 2 A, i.e., A 2= U0 , there must be a negative literal not B 2 B with B 2 K0 . In this case negative reduction would be applicable to Pb which is impossible because Pb is irreducible. 3. K1 = facts(Pb): The inclusion facts(Pb)  K1 is trivial, because the xpoint b K1 := lfp(TP;U b ) (cf. De nition 3.2) of course contains all facts of P . To show equality assume that K1 ? facts(Pb) 6= ;: Let A L1 ^  ^ Ln 2 Pb be the rst rule with non-empty body applied during the computation of K1 . If it contains a positive body literal Li , Li 2 facts(Pb) must hold. But then, success is applicable, contradicting the irreducibility of Pb. If it contains a negative body literal L1 = not B , B 2= U0 = heads(Pb) must hold. But then, positive reduction is applicable, contradicting again the irreducibility of Pb. 4. Since K1 = K0 , we also have U1 = U0 , so the xpoint is reached. 2 0

Proof. (of Theorem 5.21) Let us temporarily call the transformation on the right hand side wred , i.e. for every program P 0 let  wred (P 0 ) := A (B ? WP 0 ) A B 2 ground (P 0 ) and for every B 2 B: B 62 WP 0 :

1. We rst show that P1 7!X P2 =) wred (P1 ) = wred (P2 ): (a) P1 7!P P2 : Positive reduction removes a negative body literal not B from some rule A B, if B 2= heads(P1 ). Positive reduction does not change the well-founded model. Furthermore true in ? not B is certainly  the well-founded model. Thus B ? WP = B ? fnot B g ? WP . (b) P1 7!N P2 : Negative reduction deletes a rule A B where B contains a negative literal not B such that B 2 facts(P1 ). But then B is true in the well-founded model of P1 and P2 , so the wred -transformation will delete this rule anyway. 1

2

33

(c) P1 7!S P2 : Success removes a positive body literal B from some rule A B, if B 2 facts(P1 ). Success does not change the well-founded model, and furthermore B is certainly true in this well-founded model. ? Thus we can conclude B ? WP = B ? fB g ? WP . (d) P1 7!F P2 : Failure deletes a rule A B where B contains a positive literal B such that B 2= heads(P1 ). But then not B is true in the wellfounded model of P1 and P2 , so the wred -transformation will delete this rule anyway. (e) P1 7!L P2 : Loop detection w.r.t. to an unfounded set A deletes all rules A B where A 2 A. By the rst condition of De nition 5.11, this also means that B contains a positive body literal B with B 2 A. But since all atoms in the unfounded set A are false in the well-founded model of P1 and P2 , the wred -transformation will delete these rules anyway. 1

2

2. Since ground (P ) 7!X Pb, we have wred (ground (P )) = wred (Pb): Because the well-founded semantics is instantiation-invariant (cf. De nition 4.1) the de nition of wred implies wred (P ) = wred (Pb): We now only have to show b Assume that wred (Pb) 6= P: b Then there is a rule A wred (Pb) = P: B 2 Pb such that there is B 2 B and either A B has been modi ed by wred because B 2 WPb or entirely deleted because B 2 WPb . But we know by Theorem 5.20 that

WPb = known BASE (P ) (Pb): We can distinguish the following four cases: (a) B 2 WPb . i. B is positive. It follows that B 2 facts(Pb): But then success is applicable, contradicting the irreducibility of Pb. ii. B = not C is negative. It follows that C 2= heads(Pb): But then positive reduction is applicable, again contradicting the irreducibility of Pb. (b) B 2 WPb . i. B is positive. It follows that not B 2 WPb and thus B 2= heads(Pb): But then failure is applicable, contradicting the irreducibility of Pb. ii. B = not C is negative. It follows that C 2 WPb and thus C 2 facts(Pb): But then negative reduction is applicable, again contradicting the irreducibility of Pb. 2

B. Proofs of Section 6

Proof. (of Lemma 6.3) Following Lemma 5.12, A := BASE (P ) ? lfp(TP;; ) is a valid reference set for loop detection. Let P 0 be the result of applying loop detection

w.r.t. this set A to ground (P ). It follows that P 0 = fA B 2 ground (P ) j A 2 lfp(TP;;)g:

34

Thus heads(P 0 )  lfp(TP;; ), and we can use failure to remove also the rules which contain a positive body literal B with B 62 lfp(TP;; ). Then the result is P 00 = fA B 2 ground (P ) j fAg [ pos(B)  lfp(TP;; )g: As mentioned above, it is obvious that lfp(TP;; ) = heads(lfp(TP )). To prove P 00 = lfp(TP ), we show that P 00  lfp(TP ) and P 00  lfp(TP ): 1. It is clear that A B 2 ground (P ) with pos(B)  lfp(TP;; ), thus pos(B)  heads(lfp(TP )) must also be contained in lfp(TP ). 2. If A B 2 ground (P ) is derived by TP , this means that pos(B)  heads(lfp(TP )) = lfp(TP;;) and A 2 heads(lfp(TP )) = lfp(TP;;).

2

C. Proofs of Section 7 Proof. (of Lemma 7.1)

Let A B be the rule a ected (i.e. deleted or modi ed) by one of the transformations positive and negative reduction, success and failure (7!PNSF ). Then the applicability of this transformation depends only on rules having a ground atom from pos(B) [ neg(B) in the head. So the rules in P have no in uence on the applicability of the transformation: If P1 7!PNSF P2 , then P1 [ P 7!PNSF P2 [ P , and if P1 67!PNSF P2 , then P1 [ P 67!PNSF P2 [ P . The same is true for loop detection: Let P1 7!L P2 and let A be the set of ground atoms used. Then loop detection gives the same result if we use the atom set A0 := fA 2 A j A occurs in P1 g instead of A. But with this unfounded set it is clear that the addition of the rules P has no in uence on the applicability of loop detection and on the set of deleted rules. Conversely, suppose that P1 [ P 7!L P2 [ P . Removing rules can never prevent the applicability of loop detection, except when the last deleted rule is removed (since the main condition is a \for all rules" condition). But in our case, the rules in P are anyway not e ected by loop detection. So we immediately get that loop detection is applicable to P1 with the same set A. Then we get of course the same result: P1 7!L P2 . 2 Proof. (of Lemma 7.6) So we start with R [ ground (P ). We rst apply negative reduction to get R [ fA B j neg(B) \ facts(R) = ;g. Now we apply loop detection based on the set ?  A := BASE (P ) ? heads R [ lfp(T^P;R ) : Of course, if it should happen that no elements from this set appear as rule heads, loop detection is not needed. Otherwise we have to show that the condition of De nition 5.11 is satis ed. Suppose that there were a rule A B such that the head A is contained in A, but B \ A = ;. Since heads(R) \ A = ;, it is clear that such a rule?must be from ground (P ). Furthermore, the construction of A entails ?  A 62 heads lfp(T^P;R ) and pos(B)  heads R [ lfp(T^P;R ) . But this is a contradiction since in this case the operator T^P;R would have derived the rule A B:

35 ?  lfp(T^P;R ) = T^P;R lfp(T^P;R ) is  ?  A B 2 ground(P ) pos(B) 2 heads(R) [ heads lfp(T^P;R ) and neg(B) \ facts(R) = ; : But in this case we would? have a rule A B which is not? contained in lfp(T^P;R ) (since A 62 heads lfp(T^P;R ) ), although pos(B)  heads R [ lfp(T^P;R ) and neg(B) \ facts(R) = ; (since otherwise the rule would have been deleted by negative reduction). This application of loop detection gives  ?  R [ A B 2 ground(P ) A 2 heads(R) [ heads lfp(T^P;R ) and neg(B) \ facts(R) = ; : (R is unchanged because A\ heads(R) = ; by construction.) Now we apply Failure to get  ?  R [ A B 2 ground(P ) A 2 heads(R) [ heads lfp(T?^P;R ) and pos(B) 2 heads(R) [ heads lfp(T^P;R ) and neg(B) \ facts(R) = ; : But as we have just seen, if pos(B) 2 heads(R) [ heads(lfp(T^P;R )) and furthermore ?  neg(B) \ facts(R) = ;, for a rule A B in ground (P ), then A 2 heads lfp(T^P;R ) . Thus, the formula can be simpli ed to  ?  R [ A B 2 ground(P ) pos(B) 2 heads(R) [ heads lfp(T^P;R ) and neg(B) \ facts(R) = ; ; ?  which is R [ T^P;R lfp(T^P;R ) , i.e. R [ lfp(T^P;R ). This completes the proof of the rst part of the theorem (R [ ground (P ) 7!X R [ lfp(T^P;R )). Now let us prove the second part of the theorem, namely that the resulting set R [ lfp(T^P;R ) is irreducible wrt loop detection. Suppose it were not. Then there would be a set A which satis es the conditions of De nition 5.11. This especially means that there is a rule A B with A 2 A. Because R was assumed to be irreducible, this rule cannot be contained in R (otherwise loop detection could be applied with the same set A to R). However, we now show by an induction on i that T^P;R " i also cannot contain any rules which loop detection would delete. For i = 0 this is trivial. Suppose that A B is generated by T^P;R in the i + 1-st iteration. This means that pos(B)  heads(R) [ heads(T^P;R " i). By the induction hypothesis, heads(T^P;R " i) \ A = ;, and by the irreducibility of R, heads(R) \ A = ;. But then B \ A = ;, and thus the rst condition in De nition 5.11 entails A 62 A. 2 Proof. (of Lemma 7.8) Let A be the unfounded set on which this application of loop detection is based. Since R is irreducible, the elements of A cannot appear in heads of R. Now let A be partitioned into subsets A1 ; : : : ; An such that P contains no rules with an element of Ai in the head, of Aj as a positive body literal, and i  j . Since P contains no positive recursion, such a partition exists. Loop detection deletes all rules with an element of A in the head. We now show by induction on i that we can eliminate all rules with an element from Ai in the head by 7!F .

 The elements of A1 cannot appear in rule heads, since such a rule must

36

contain an element of A in the body (violating the acyclicity condition).  Now suppose that the atoms from A1 [    [ Ai?1 already do not appear in rule heads. All rules containing an element from Ai in the head must contain an element from A in the body, and by the acyclicity condition, this element is from A1 [    [ Ai?1 . But now it is obvious that such rules can be deleted by failure. 2 Proof. (of Lemma 7.9)

Positive reduction and success are simple: If loop detection were applicable in P2 based on some unfounded set A, it would be applicable in P1 also based on the same set A. Failure is slightly more dicult, because in this case a rule A B is deleted, and it might be that only this rule prevented the application of loop detection based on some set A. However, loop detection is nevertheless applicable in P1 based on the set A[fB g, where B 2 B is the positive body literal which was responsible for the deletion of this rule because it did not appear in any rule head.

2

D. Proofs of Section 8 Lemma D.1 Let P1 be any ground program and let P2 be the normal form of P1 wrt 7!S (success). Then:  P2 = A (B ? facts(P2 )) ^ not C A B ^ not C 2 P1 :

Proof. This is obvious: Every rule in P2 results from a rule in P1 by removing

positive body literals. Only positive body literals can be removed which are known as facts, and the set of facts can only increase during this process. Since P2 is irreducible wrt 7!S , atoms in facts(P2 ) cannot occur any longer as positive body literals. 2

Lemma D.2 Let P1 be any ground program and let P2 be the normal form of P1 wrt 7!N (negative reduction). Then:  P2 = A B ^ not C 2 P1 C \ facts(P1 ) = ; : Proof. This is trivial: Negative reduction deletes the rules which have a negative body literal con icting with a fact. The set of facts is not changed during this process, i.e. facts(P1 ) = facts(P2 ). 2 Lemma D.3 Let P1 be any ground program and let P2 be the normal form of P1 wrt 7!P (positive reduction). Then:  P2 = A B ^ not(C \ heads(P1 )) A B ^ not C 2 P1 : Proof. This is trivial: Positive reduction removes the negative body literals for which the corresponding positive atom does not appear as a head literal. The set of head literals is not changed during this process, i.e. heads(P1 ) = heads(P2 ). 2 Lemma D.4 Let P1 be any ground program and let P2 be the normal form of P1 wrt 7!L F (loop detection and failure). Then: P2 = lfp(TP ). 1

37 Proof.

1. P1 7!LF lfp(TP ) has already been proven in Lemma 6.3. 2. We have only to prove that lfp(TP ) is irreducible wrt 7!LF . Let us suppose that 7!L is applicable. Let A as in the de nition of 7!L (De nition 5.11). Consider the rst iteration in which a rule instance A B^ not C is derived with B \ A 6= ;. There must be such a rule instance since loop detection would otherwise not be applicable. Suppose that B 2 B \ A. Then a rule instance B B0 ^ not C was derived in a previous iteration. This means that B0 \ A = ;. But this contradicts the rst condition in De nition 5.11. Now let us suppose that 7!F is applicable. Then there is a rule instance A B ^ not C and an atom B 2 B that is not in heads(lfp(TP )). But this 2 contradicts the iterative construction of lfp(TP ). 1

1

1

1

Lemma D.5 Let P1 be any ground program and let P2 be the normal form of P wrt ! 7 (loop detection 1 LF and failre). Then: P2 = A B ^ not C 2 P1 B  heads(P2 ) . Proof. This follows immediately from Lemma D.4: Since P2 is a xpoint of TP , every rule A B ^ not C 2 P1 with B  heads(P2 ) must be contained in P2 . On the other hand, it follows from the iterative construction of lfp(TP ) that any rule 2 A B ^ not C contained in lfp(TP ) must satisfy B  heads(lfp(TP )). 1

1

1

1

Proof. (of Theorem 8.4) We do not only show facts(K i) = Ki and heads(Ui ) = Ui but also the following stronger characterizations of K i and Ui : 

K 0 = A



(B ? K0) ^ not C

B ^ not C 2 ground (P ); B  lfp( TP;; )  A U0 = A (B ? K0) ^ not C B ^ not C 2 ground (P ); B  U ; C \ K = ; 0 0  K i = A (B ? Ki ) ^ not(C \ Ui?1 ) A B ^ not C 2 ground (P ); B  Ui?1 ; C \ Ki?1 = ;  Ui = A (B ? Ki ) ^ not(C \ Ui?1 ) A B ^ not C 2 ground (P ); B  Ui ; C \ Ki = ; : A

The proof is by induction on i. 1. Proof for K 0 : We already know that ground (P ) 7!LF lfp(TP ). It is easy to show by induction on the number of derivation steps that  lfp(TP ) = A B ^ not C A B ^ not C 2 ground (P ); B  TP;;

(because heads(TP " j ) = TP;; " j ). Now K 0 results from lfp(TP ) by applying 7!S until irreducibility w.r.t. 7!S . We show that facts(K 0) = K0 : (a) K0  facts(K 0 ): We show TP " j  facts(K 0) by induction on j : The case j = 0 is trivial since TP " 0 = ;. Now suppose that A was derived in step j by applying A B 2 ground (P + ). Then B  TP " (j ? 1) and therefore B  facts(K 0 ) by the induction hypothesis. Furthermore we know that TP " (j ? 1)  lfp(TP )  lfp(TP;;) and therefore A B 2 lfp(TP ). But since B  facts(K 0 ) and K 0 is the normal form of lfp(TP ) wrt 7!S , it follows that A 2 K 0 . +

+

+

+

+

38

(b) facts(K 0)  K0 : The proof is by induction on the number of applications of 7!S . All facts contained in lfp(TP ) are also contained in ground (P + ) and thus in K0 . Suppose that A is derived from the rule A B 2 lfp(TP ) by j applications of 7!S . Then the atoms in B are derived in fewer steps, and thus B  K0 by the induction hypothesis. Since K0 is a xpoint of TP and A B 2 ground (P + ) it follows that A 2 K0 . From facts(K 0) = K0 our characterization now follows by Lemma D.1. 2. Proof for U0 : In order to construct U0 from K 0 , we rst compute the normal form wrt 7!N . By Lemma D.2, it is: +



A



(B ? K0) ^ not C A

B ^ not C 2 ground (P ); B  lfp(TP;; ); C \ K0 = ; :

Let us call this set N0 . Let U0 be the normal form of N0 w.r.t. 7!LF . We rst prove heads(U0 ) = U0 : (a) U0  heads(U0 ): Because we have U0 = lfp(TP;K ), we prove by induction on j : TP;K " j  heads(U0 ). The case j = 0 is trivial. Let now A 2 TP;K " j be derived by using the rule instance A B ^ not C . Then B  TP;K " (j ? 1) and C \ K0 = ;. Also TP;K " (j ? 1)  lfp(TP;K )  lfp(TP;; ), thus A (B ? K0) ^ not C is contained in N0 . By the induction hypothesis, we have B  heads(U0). By Lemma D.5 this implies that A (B ? K0) ^ not C is not deleted by loop detection or failure. Thus A 2 heads(U0 ). (b) heads(U0 )  U0 : By Lemma D.4, U0 = lfp(TN ). Therefore, we show heads(TN " j )  U0 by induction on j . The case j = 0 is again trivial. Now suppose that A (B ? K0 ) ^ not C is derived in step j . Then (B ? K0)  heads(TN " (j ? 1)) and therefore (B ? K0 )  U0 by the induction hypothesis. But K0  U0 also holds by Lemma 3.4, thus B  U0 . We also have C \ K0 = ;, otherwise the rule instance would not be contained in N0 . But since U0 = lfp(TP;K ), it follows that A 2 U0 . Our characterization of U0 follows from heads(U0 ) = U0 by Lemma D.5. 3. Proof for K i , i  1: In order to construct K i , we rst apply positive reduction to Ui?1 until irreducibility w.r.t. 7!P . By Lemma D.3 and since heads(Ui?1 ) = Ui?1 and Ui?1  Ui?2 (Lemma 3.4), the normal form is: 0

0

0

0

0

0

0

0

0

0



A



(B ? Ki?1 ) ^ not(C \ Ui?1 ) A

B ^ not C 2 ground (P ); B  Ui?1 ; C \ Ki?1 = ; :

Let us call this set Ni . Note that this also works for i = 1, although U0 di ers a bit from the general formula for Ui?1 . Now we apply 7!S until irreducibility w.r.t. 7!S , the result is K i . We rst prove facts(K i ) = Ki : (a) facts(K i )  Ki : The proof is by induction on the number of applications of 7!S . Every A 2 facts(K i ) results from a rule A (B ? Ki?1 ) ^ not(C \ Ui?1 )

39

in Ni where C \ Ui?1 = ; and B ? Ki?1 can be derived as facts with fewer applications of 7!S . Then the inductive hypothesis gives (B? Ki?1 )  Ki . Furthermore Ki?1  Ki by Lemma 3.4. Thus B  Ki . But then the rule instance A B ^ not C is applicable in TP;Ui? , and we get A 2 Ki . (b) Ki  facts(K i): Because of the equality Ki = lfp(TP;Ui? ), we show (TP;Ui? " j )  facts(K i ) by induction on j : The case j = 0 is again trivial. Now suppose that  step j by applying ? A was derived in A B ^ not C . Then B  TP;Ui? " (j ? 1) and C \ Ui?1 = ;. The rst part gives us B  Ki and together with Lemma 3.4: B  Ki  Ui  Ui?1 . The second part (C \ Ui?1 = ;) implies, again using Lemma 3.4, that C\ Ui?1 = ;. From this together we can conclude that A (B ? Ki?1 ) ^ not(C \ Ui?1 ) is indeed contained in Ni . Moreover, C \ Ui?1 = ; shows that there are in fact ?no negative body literals, so the rule is A (B ? Ki?1 ). Next, B  TP;Ui? " (j ? 1) together with the inductive hypothesis gives us B  facts(K i ). But since K i is irreducible under 7!S , and A (B ? Ki?1 ) was contained in Ni , we can nally conclude A 2 K i . From facts(K i ) = Ki our characterization now follows by Lemma D.1 (using also K i?1  K i , Lemma 3.4). 4. Proof for Ui , i  1: In order to construct Ui from K i , we rst compute the normal form wrt 7!N . By Lemma D.2, it is: 1

1

1

1

1



A



(B ? Ki ) ^ not(C \ Ui?1 ) A

B ^ not C 2 ground (P ); B  Ui?1 ; C \ Ki = ; :

Let us call this set Ni . Let Ui be the normal form Ni w.r.t. 7!LF . We rst prove heads(Ui ) = Ui : (a) heads(Ui )  Ui : By Lemma D.4, Ui = lfp(TNi ). Therefore, we show (TNi " j )  Ui by induction on j . The case j = 0 is trivial. Now suppose that A (B ? Ki ) ^ not(C \ Ui?1 ) is derived in step j . Then (B? Ki )  heads(TNi " (j ? 1)) and therefore (B ? Ki )  Ui by the induction hypothesis. But Ki  Ui also holds by Lemma 3.4, thus B  Ui . We also have C\ Ki = ;, otherwise the derived ground rule would not be contained in Ni . But Ui = lfp(TP;Ki ), and we can apply A B ^ not C in TP;Ki , so A 2 Ui . (b) Ui  heads(Ui ): Since Ui = lfp(TP;Ki ), we prove (TP;Ki " j )  heads(Ui ) by induction on j . The case j = 0 is the rule intrivial. Let now A 2 (TP;Ki " j ) be? derived by applying  stance A B ^ not C . Then B  TP;Ki " (j ? 1) and C \ Ki = ;. Since TP;Ki " (j ? 1)  lfp(TP;Ki ) = Ui and Ui  Ui?1 by Lemma 3.4, we get B  Ui?1 . Therefore, the ground rule A (B ? Ki ) ^ not(C \ Ui?1 )

40  ? is really contained in Ni . From B  TP;Ki " (j ? 1) the inductive hypothesis gives us B  heads(Ui ). But then A 2 heads(Ui ) follows from Lemma D.5. Our characterization of Ui follows from heads(Ui ) = Ui by Lemma D.5 and using Ui  Ui?1 (Lemma 3.4). 2

Suggest Documents