Operational Semantics for SLG Evaluation. Part 1 ... - CiteSeerX

1 downloads 0 Views 417KB Size Report
May 11, 1994 - Terrance Swift. Weidong Chen ..... exists a three-valued stable model M of P such that MjHB = M0. ...... Then the SLGO automaton marks a.
Operational Semantics for SLG Evaluation. Part 1: Modularly Strati ed Programs Draft Terrance Swift

Weidong Chen

David S. Warren

May 11, 1994

Abstract

SLG evaluation has received attention not only because of its theoretical properties | it computes well-founded and three-valued stable models for general logic programs | but also because it can be eciently implemented. The SLG-WAM of XSB has proven an order of magnitude or more faster than similar systems when executing modularly strati ed programs. This paper proposes an operational semantics, SLGO , which bridges a gap between the general description of SLG in [2] and the SLG-WAM. As a result, SLGO , which shares soundness and completeness properties with SLG, formalizes the scheduling strategy of the SLG-WAM, and models SLG-WAM operations such as the table cut and existential negation [7]. Within the framework of SLGO , algorithms for incremental completion of sets of subgoals can be proven correct, along with an evaluation strategy which avoids delay for modularly strati ed programs.

1 Introduction SLG [1, 2] uses memoing to evaluate general logic programs. Speci cally, SLG keeps a table of answers to subgoals, and resolves repeated subgoals against answer clauses in the table rather than against program clauses. By this use of answer clause resolution, SLG avoids non-terminating evaluations for programs with nite models (including datalog programs) and as well as avoiding redundant subcomputations. This paper seeks to de ne a version of SLG, SLGO , which adds operational information to SLG. The formulation of SLG does not take into account the order in which transformations are applied nor does it make explicit its algorithms for completion of a SLG system (evaluation). By rectifying these omissions, we clarify our implementation SLG in the SLG-WAM. Another new feature of SLGO is its explicit mixture of SLG and SLD resolution. Predicates (and goals) that are evaluated using SLD are sometimes referred to as non-tabled predicates (goals), and we will use the terms SLG and tabled interchangeably, along with the terms SLD and non-tabled. The next few sections build successive formulations of SLGO , rst for de nite programs, and later for modularly strati ed programs. These formulations will allow proofs of correctness of several algorithms upon which the SLG-WAM is based as well as motivating many of its the data structures. Before developing SLGO we present some preliminary information about SLG and the partial stable model theory on which it is based.

1

2 Preliminaries In order to make this paper self-contained we o er a brief overview of some of the de nitions and results of SLG which are used later in the paper. The de nitions and theorems in this section are largely taken from [2]. The Herbrand universe of a program P is the set of all ground terms that may be constructed from the constants and function symbols appearing in P . An arbitrary constant is added if no constant occurs in P . The Herbrand instantiation of P is the (possibly in nite) set of all ground clauses obtained by substituting terms in the Herbrand universe for variables in clauses in P . The Herbrand base of P , denoted by BP , is the set of all ground atoms with predicates occurring in P whose arguments are in the Herbrand universe of P . Let f ; u; t be truth values ordered by f < u < t. A partial Herbrand interpretation over BP (or just interpretation) I is a mapping from BP to ff ; u; tg. An interpretation I can be represented as a partition of the Herbrand base Pos(I ) [ Und(I ) [ Neg(I ), where Pos(I ) (respectively, Und(I ), Neg(I )) is the set of ground atoms A such that I (A) is t (respectively u, f). Any two of these sets will uniquely determine I . An atom is of the form p(t1; :::; tn), where p is an n-ary predicate symbol and t1 ; :::; tn are terms. A literal is either an atom A or its negation A. A clause is of the form A L1 ; :::; Ln: where A, the head of the clause, is an atom, and L1 ; :::; Ln(n  0), the body of the clause, are literals. A program is a nite set of clauses. A ground atom (literal, clause) is one that is variable-free. Atoms (literals, clauses) that are variants of each other are considered syntactically identical1.

2.1 Three-valued Stable Models De nition 2.1 ([5]) Let I be an interpretation, A be a ground atom and L be a literal. Then

 A (A) is true (false) in I if A 2 Pos(I ), and is false (true) in I if A 2 Neg(I ), and is unde ned if A 2 Und(I );

An interpretation I is a model of a program P if and only if for all ground instances of its clauses A L1; :::; Ln if all Li 's are true in I then A is true in I and if A is false in I then at least one of the Li 's is also false in I . De nition 2.2 ([5]) Let I and J be interpretations. There are two natural orderings between interpretations, namely the truth ordering  and the information ordering , where  I  J if Pos(I )  Pos(J ) and Neg(I )  Neg(J );  I  J if Pos(I )  Pos(J ) and Neg(I )  Neg(J ). Models that are least in the sense of the truth ordering  are called least models. Models that are smallest in the sense of information ordering  are called smallest models. 1 One atom is a variant of another if they are the same up to variable renaming. Tabling systems may perform either variant checks as does SLG, or subsumption checks, where an atom A1 subsumes an atom A2 if there is some substitution  which uni es A1 and A2 such that the domain of  includes only variables of A1 .

2

Associated with each program there is a mapping T over (three-valued) interpretations. It is a generalization of the well-known immediate consequence operator T in [10]. De nition 2.3 ([5]) Let P be a program and I be an interpretation. Then TP (I ) is an interpretation such that:  A 2 Pos(TP (I )) if and only if there is a clause A L1; :::; Ln in the Herbrand instantiation of P and all Li 's are true in I ;  A 2 Neg(TP (I )) if and only if for every clause A L1; :::; Ln in the Herbrand instantiation of P , some Li is false in I . Let ; be the least interpretation (in which all ground atoms are false). The powers of TP are de ned as follows: TP " 0 = ; TP " n = TP (TP " (n ? 1)) if n is a successor ordinal = tfTP " k : k < ng if n is a limit ordinal where t is the least upper bound operation of interpretations with respect to the truth ordering . We assume that there is a special ground atom u. Atom u is always unde ned (u 2 Und(I )). It can appear only in the body of a clause in a program. A non-negative program is a nite set of clauses whose bodies do not contain any negative literals, but may contain atom u. Theorem 2.1 ([5]) Let P be a non-negative program. Then P has a unique least three-valued model, denoted by LPM (P ). Furthermore, TP has a least xed point, which coincides with TP " ! and LPM (P ). De nition 2.4 ([5]) Let P be a program and I be an interpretation. The quotient of P modulo I, denoted by PI , is the non-negative program obtained from the Herbrand instantiation of P by  deleting every clause with a negative literal in the body that is false in I ; and  deleting every negative literal in the body of a clause that is true in I ; and  replacing every negative literal with u in the body of a clause that is unde ned in I . I is a three-valued stable model of P if I is the least three-valued model LPM ( PI ). The set of all three-valued stable models of P is denoted by M(P ). Theorem 2.2 ([5]) Let P be a program, and WF (P ) be the well founded partial model of P . Then WF (P ) is the smallest three-valued stable model of P . Stable models as de ned by Gelfond and Lifschitz coincide with two-valued stable models.

2.2 SLG Resolution

In order to compute three-valued models of non-strati ed programs, SLG delays evaluation of those literals whose truth value may be unde ned. Delayed literals are represented explicitly in X-clauses. De nition 2.5 An X-clause G is a clause of the form A DjB where A is an atom, D is a sequence of ground negative literals and atoms, and B is a sequence of literals. If B is empty, an X-clause is also called an answer clause. 3

Because X-clauses contain delay lists, a special de nition of resolution is required along with a de nition of factoring. De nition 2.6 (SLG Resolution) Let G be an X-clause A D j L1; :::; Ln, where n > 0 and Li be the selected atom. Let C be an X-clause, and C 0, of the form A0 j L01; :::; L0m, be a variant of C with variables renamed so that G and C 0 have no variables in common. G is SLG resolvable with C if Li and A0 are uni able. The clause (A D j L1 ; :::; Li?1; L01; :::; L0m; Li+1; :::; Ln) is the SLG resolvent of G with C , where  is a most general uni er of Li and A0. SLG resolution is used for resolution with a clause in a program or with an answer X-clause that has an empty sequence of delayed literals (on the left of j ). For answer X-clauses that have a non-empty sequence of delayed literals, relevant variable bindings in the head of an answer X-clause are propagated by SLG Factoring. De nition 2.7 (SLG Factoring) Let G be an X-clause A D j L1; :::; Ln, where n > 0 and Li be the selected atom. Let C be an X-clause, and C 0 , of the form A0 D0 j , be a variant of C with variables renamed so that G and C 0 have no variables in common. If D0 is not empty and Li and A0 are uni able with a most general uni er , then the SLG factor of G with C is (A D; Li j L1; :::; Li?1; Li+1; :::; Ln) SLG is modelled as a partial deduction technique which begins with a program P and applies a series of transformations each of which builds or modi es a dual program P . After each transformation, P consists of a system of subgoals and of X-clauses with various statuses. This system models the state of an SLG evaluation, and we will sometimes use the two terms interchangibly. De nition 2.8 A subgoal is an atom. A system S is a set of subgoals. Each subgoal A in S has an associated set of X-clauses, denoted by S (A). Each X-clause in S (A) has an associated status, which can be new, answer, active, disposed, or oundered. An active X-clause has an associated set of atoms. A subgoal A 2 S is oundered if S (A) contains an X-clause that is oundered, and is completed if all clauses in S (A) are either answer or disposed. A system S is oundered if some subgoal A 2 S is oundered, and is completed if all subgoals in S are completed. An evaluation E is floundered it contains the oundering transformation. A subgoal A is successful (in S ) if S (A) contains an answer clause of the form H , and is failed (in S ) if A is completed and S (A) contains no answer clauses. Evaluation of negation in SLG is based on the property of being completely evaluated. De nition 2.9 Let S be an SLG system and A be a subset of S such that no subgoal in A is completed, We say that A is completely evaluated if it satis es the following properties:  For every subgoal A 2 A, S (A) does not contain any new, clauses.  For every active clause G in S (A), the selected literal L of G must be an atom that is either completed or in A, and the associated set of used answer clauses of G is exactly the set of all atoms occurring in the head of some answer clause in S (L). 4

disposed

active

floundered

answer

new

Figure 1: SLG Status Relations The transformations of SLG are shown in Figure 2. SLG evaluations are de ned in the following manner. De nition 2.10 Let P be a program, R be an arbitrary but xed computation rule, and A be a set of atoms. (In most cases, we start with a single atom for the initial query.) An SLG derivation (or evaluation) for A is a sequence of systems (states) S0; S1; :::; S such that  S0 is the initial system for A;  for each successor ordinal + 1  , S +1 is obtained from S by an application of one of transformations in Figure 2;  for each limit ordinal  other than 0, S is such that { the set S of subgoals is the union of all Si(i < ); and { for each subgoal A 2 S , S (A) is the union of Si(A) for all i < and A 2 Si; and { for each X-clause G in S (A), the status of G is the maximum (with respect to Figure 1) of all the statuses of G in Si(A) for all i < such that G 2 Si (A); and { for each active X-clause G in S , the associated set of atoms is the union of the associated set of atoms of G in Si for all i < such that A 2 Si. If no transformation is applicable to S , S is called a nal system of A. A is oundered (with respect to R) if some nal system of A (with respect a computation rule R) is oundered. SLG resolution is the process of constructing an SLG derivation for a set A of atoms with respect to a program P under a computation rule R. We note some facts about SLG computation in passing. Unlike other resolution strategies such as SLS, SLG works on a xed computation strategy, a feature which lends itself to ecient implementation. Also, the de nition of an SLG evaluation re ects the fact that computing threevalued stable models may require trans nite induction. Since SLG is a goal-directed resolution strategy, it does not compute the entire model of a program, but only those atoms of the model which are needed to support an initial query. To re ect this property, statements about its correctness are made with respect to the restriction of a model. De nition 2.11 Let P be a program and B be the Herbrand base of P . Let HB be a subset of B and I be an interpretation over B. Then the restriction of I to HB, denoted by I jHB, is an interpretation over HB such that Pos(I jHB ) = Pos(I ) \ HB , Neg (I jHB) = Neg (I ) \ HB . The central property of SLG lies in the following theorem. 5

new answer floundering

A : new : H D j A : answer : H D j

A : new : G with a non-ground negative literal selected A : floundered : G new active A : new : G with an atom B or a ground negative literal B selected A : active : G and B : new : C for each SLG resolvent C of B B with a clause in KB if B is not previously a subgoal answer return A : active : G and L : answer : B D j and B is not in the associated set of atoms of G and G is the SLG resolvent or SLG factor of G with B D j on L A : active : G and A : new : G and B is in the associated set of atoms of G negation failure-R A : active : G with B selected and subgoal B is successful A : disposed : G negation success-R A : active : G with B selected and subgoal B is failed A : disposed : G and A : new : G where G is G with B deleted delaying A : active : G with B selected and B is neither successful nor failed A : disposed : G and A : new : G where G is G with B moved to the left of j 0

0

0

0

0

0

completion

A a non-empty set of subgoals that is completely evaluated all active clauses of subgoals in A are changed to disposed negation failure-L A : St : G with B selected and subgoal B is successful, and St = 6 disposed

A : disposed : G A : St : G with B selected and subgoal B is failed, and St 6= disposed A : disposed : G and A : St : G where G is G with B deleted atom failure-L A : St : G with B on the left of j and atom B is failed, and St 6= disposed A : disposed : G atom success-L A : St : G with B on the left of j and atom B is successful, and St 6= disposed A : disposed : G and A : St : G where G is G with B deleted negation success-L

0

0

0

0

Figure 2: Transformations of SLG Resolution

6

Theorem 2.3 (Partial Correctness of SLG Resolution) Let P be a program, R be an arbitrary but xed computation rule, A be a set of atoms that is not oundered, and S be a nal system for A. Let HB be the set of ground instances of all subgoals in S . Then (a) for every three-valued stable model M of P , M jHB is a three-valued stable model of P (A); (b) for every three-valued stable model M0 of P (A), which is an interpretation over HB , there exists a three-valued stable model M of P such that M jHB = M0 .

2.3 Restrictions of SLG

[2] also provides useful restrictions of SLG for de nite programs and for modularly strati ed programs. For de nite programs, SLG reduces to SLD with tabling. The following theorem states the properties of SLG for de nite programs. Theorem 2.4 Let P be a de nite program, MP be the least model of P , R be an arbitrary but xed computation rule, A be a set of atoms. Then  there exists an SLG derivation for A, S0; S1; :::; S , such that only the new active, new answer, answer resolution and completion transformations in Figure 2 are used and S is completed; and  for every such derivation, let HB be the set of ground instances of all subgoals in S . Then MP jHB is the least model of P (A), where P (A) is the program obtained from P (S ) by replacing each newly introduced predicate p with p. Proof: The proof follows directly from Theorem 2.3 and the fact that MP is the only (three-valued) stable model of P . 2 Note that the SLG evaluation computing MP may require an in nite number of transformations. De nition 2.12 A predicate dependency graph P , is the minimal graph, G such that  The nodes in G are the predicates in P .  There is a directed link from p to q whenever q appears in the body of p. If q appears in a negative literal, the link is negative. Otherwise the link is positive. We say that p depends positively on q is there is a path from p to q in G which traverses an even number of negative links, and that p depends negatively on q if there is a path with an odd number of links. p depends on q if it depends positively or negatively on q . A component is a subset of nodes of G. A recursive predicate is a predicate whose node in G contains a path to itself. A recursive component C is a component such that if C contains a recursive predicate p, C contains all predicates on all paths from p to itself. Given a recursive component C , if there is a path from a predicate p to a predicate r, but C does not contain r, then r is used by C . Modularly strati ed programs[6] are de ned as a re nement of strati ed and of locally strati ed programs. De nition 2.13 (Modular Strati cation[6]) Let P be a program and  be the partial ordering over recursive components of P . P is modularly strati ed if, for every recursive component F of P , 7

 There is a total well-founded model M for the union of all components F 0  F , and  The quotient of F modulo M , MF , is locally strati ed.

For modularly strati ed programs, the following theorem holds about restricted SLG. We will later characterize some programs as left-to-right modularly strati ed. This additional condition requires the set of all rule pre xes to be modularly strati ed. Theorem 2.5 ([2]) Let P be a modularly strati ed program, MP be the well founded model of P , R be a modularly ordered computation rule, and A be a non- oundered set of atoms. Then  there exists an SLG derivation for A, S0; :::; S , such that only the transformations new answer, floundering, new active, answer resolution, negation-failure-R negation success-R, and completion are used;  S is completed;  for every such derivation, let HB be the set of ground instances of all subgoals in S . Then MP jHB is the well founded model of P (A), where P (A) is the program obtained from P (S ) by replacing each newly introduced predicate p with p. Recursive components form the basis of strati cation in the framework of modularly strati ed programs. (They were called complete components in [6]). There is a natural partial ordering  over components, where F1  F2 if some predicates in F2 calls directly or indirectly some predicates in F1 .

3 Combining SLG and SLD This section introduces a variant of SLG, SLGT which combines SLG and SLD resolution. For illustrative purposes we restrict ourselves to de nite programs where the general actions of SLG should not be familiar to readers conversant with other top-down approaches such as OLDT [9], SLD-AL [12] or Extension Tables [4]. In particular, de nite programs do not need the delaying transformation that SLG uses to evaluate programs with non-strati ed negation. As a result all X-clauses have empty delay lists, and the X-clauses can be understood as clauses in the traditional logical sense. Furthermore, the de nition of SLG resolution reduces to the de nition of clause resolution, and that of SLG factoring reduces to the de nition of resolution of a clause against a unit (answer) clause. De nition 3.1 An SLGT program is denoted PT and consists of a set of clauses plusP along with a set of table declarations, T . An SLGT system is a forest of SLG trees, along with an SLG table. The SLG table is a set of ordered pairs. The rst component of each pair is an atom, called a subgoal; the second, a set of other atoms called answer clauses or answers. Let (S; Set) be a pair in the table, and A 2 Set; we say answer A is associated with S . Nodes of SLG trees are O-clauses. De nition 3.2 An O-clause has the form DFN : Table goal : Status : Answer Template < ?Subgoals : Resolvents Each O-clause has assigned to it a unique depth- rst number, or DFN . The Table goal is identical for all nodes in the same SLG tree. The Status of the node can take the values new, interior, 8

generator, active, answer, disposed, oundered, or suspended. The Answer Template is an atom,

and Subgoals is a sequence of atoms. Resolvents consists of a sequence of program clauses if the status of the node is generator or interior, and of two sequences of answer clauses if the status is active. If the status is answer, new or disposed the Resolvents eld is empty; in these cases we will generally omit it in our notation. Given a tabled subgoal A, the nodes in an SLGT tree with root A can be denoted as S (A), just as are the clauses associated with A in SLG. Nodes are rst created with the status new and their status later transformed into:  active or generator if the selected literal of a node is tabled;  interior if the selected literal is non-tabled;  answer if Subgoals is empty.  oundered if the selected literal is negative and non-ground.  suspended if the selected literal is negative, and has not been completed.. Bacause floundered and suspended nodes arise only when the selected literal is negative. When it is determined that no further answers can be obtained from active, generator, or interior nodes, they assume the status disposed. For an active clause, the sequences in Resolvents represent in ord er, a Used set of resolved-upon answer clauses and an Unused set of yet-to-be-resolved-upon answer clauses. In the case of generator and interior nodes, which resolve against a static set of program calsuses, Resolvents represents yet-to-be-resolved-upon program clauses. Because of the new values for SLGT statuses, the de nition of completely evaluated must be extended. De nition 3.3 Let S be an SLG system and A be a subset of S such that no subgoal in A is completed, We say that A is completely evaluated if it satis es the following properties:  For every subgoal A 2 A, S (A) does not contain any new, interior, generator or suspended clauses.  For every active clause G in S (A), the selected literal L of G must be an atom that is either completed or in A, and the associated set of used answer clauses of G is exactly the set of all atoms occurring in the head of some answer clause in S (L). SLG trees are built through SLGT derivations. The de nition of an SLGT derivation is given below. Because the focus of this paper is on an operational semantics, our de nition does not incorporate trans nite induction. De nition 3.4 Given a de nite program PT , a set of tabled goals G, and a computation rule R, an SLGT derivation (or evaluation) E foris a sequence of forests of SLG trees and tables S0; S1; :::; S such that  S0 is the forest consisting of the single tree G : new : G < ?G and the empty table;  for each nite ordinal +1, S +1 is obtained from S by an application of one of the operations in De nition 3.5 below; 9

If no operation is applicable to S , S is called a nal system of A. When an evaluation produces a nal system, it is said to terminate. Both the nodes of an SLG tree and the answers in an SLG table have DFN corresponding to the order of their creation in the evaluation. SLGT operations for modularly strati ed programs are summarized in de nition 3.5. We assume for facility of exposition, a left-to-right computation rule throughout the rest of this section. De nition 3.5  new active. Given a node N = A : new : G < ?S , with tabled atom B selected, change the status of N to active, and add an empty used set and an unused set consisting of associated answers for B , if any (B may not be in the table). If B is not in the table add the pair fB; fgg to the table; also, create a new tree with root B : generator : B < ?B : Res, where Res includes the set of program clauses that unify with B.  new interior. Given a node N = A : new : G < ?S , with non-tabled atom B selected, change the node to A : interior : G < ?S : Res, where Res includes the set of program clauses that unify with B .  new answer. Given a node N = A : new : G < ?, change N into A : answer : G < ?. Let (A0; Set) be the pair in Table such that A0 is a variant of A. If Set does not contain a variant of G, add G to Set.  retry generator. Let node N = A : generator : G < ?S : Res, with atom B selected and Cl be the rst element of Res. Remove Cl from Res, and produce a child N1 of N where N1 = A : new : (G0 < ?S 0) and (G0 < ?S 0 ) is the resolvent of G < ?S and Cl on B .  retry interior. Let node N = A : interior : G < ?S : Res, with atom B selected and Cl be the rst element of Res. Remove Cl from Res, and produce a child N1 of N where N1 = A : new : (G0 < ?S 0 ) and (G0 < ?S 0) is the resolvent of G < ?S and Cl on B.  retry active. Let N = A : active : G < ?S : Used : Cl :: Unused, be a node, with atom B selected. Move Cl from unused to used, and produce a child N1 of N where N1 = A : new : (G0 < ?S 0 ) where (G0 < ?S 0) is the resolvent of G < ?S and Cl on B.  answer return. Let N = A : active : G < ?S : Resolvents, be a node, with atom B selected and let Cl be an associated answer clause with DFN (Cl) > DFN (N ) such that Cl is not in the used set of Resolvents. Add Cl to Used, and produce a child N1 of N where N1 = A : new : (G0 < ?S 0 ) where (G0 < ?S 0) is the resolvent of G < ?S and Cl on B.  negation failure-R. Given a node N = A : active : G : Resolvents with B selected where B is ground and successful, change the status of N to disposed.  negation success-R. Given a node N = A : active : G : Resolvents with B selected where B is ground and failed, create a child N1 of N , A : new : G0 , where G0 is G with B deleted  negation suspension. Given a node N = A : active : G : Resolvents with B selected where B is ground but neither successful nor failed. change the status of N to suspended.  floundering. Given a node N = A : new : G with a non-ground negative literal selected change the status of N to floundered. 10



completion. A set S of mutually dependant SLG trees which are completely evaluated,

dispose of these trees, and change to active the status of all suspended nodes whose selected literal is the root of a tree of S . The floundering operation of de nition 3.5 is crude: delay can be used so that some classes of program will not ounder, but the analysis and details of such delay are beyond the scope of this paper. De nition 3.5 does not indicate any order in which the SLGO operations should occur. To model scheduling explicitly, section 4 describes an SLGO automaton which replaces the forest of SLG trees with a scheduling or (choice point) stack. The scheduling stack consists of nodes for which SLGO operations are possible: generator and interior nodes for which program clause resolution is not complete; or active nodes for which answer clause resolution is not complete. The stack-based scheduling of an SLGO automaton is depth- rst with one signi cant exception. When a new answer operation produces an answer, there may be active subgoals with which the answer needs to be resolved. In the SLGO automaton, these active subgoals are placed on the stack to be scheduled for answer return operations, and these answer return operations may break the depth- rst search of the evaluation. The scheduling, and the SLGT operations themselves can be illustrated through a concrete example. Example 3.1 Assume we have the program: :- table a/2. p(a,b). p(a,d). p(b,c). q(b). q(c). a(X,Z) :- a(X,Y),a(Y,Z). a(X,Z) :- p(X,Z),q(Z). and query a(a,Z). The declaration table a/2 indicates that SLG resolution is to be

used for a/2. The forest of SLG trees for this example along with its table is depicted at the end of the evaluation in Figure 3. In this, and future examples, we will assume a leftmost computation rule. Let us examine elements of the forest in detail. New trees are added to the forest whenever a call to a tabled subgoal is made that is not a variant of a previous tabled subgoal. This is performed by the new active operation in De nition 3.5. Thus, in Example 3.1, the evaluation of a(a,Z) begins by creating a new tree a(a,Z) and using a retry generator operation to resolve the program clause a(X,Y):-a(X,Z),a(Z,Y) against the goal, creating node 1 with status new. In node 1, the selected literal a(a,Z) is tabled, so the new active operation applies. Since a(a,Z) is a variant of a previous tabled subgoal no new tree is created; the node's status is changed to active as it appears in Figure 3. If answers for the subgoal were present, retry active operations would backtrack through them. Instead, since there are no answers, the only operation that applies is to node 0, and a second retry generator operation is executed. This resolves the program clause a(X,Y):-p(X,Y),q(Y) against the goal a(a,Z), producing node 2 with status new. Since the selected literal for node 2 is not tabled, a new interior operation applies and performs Prologstyle resolution. Depth- rst resolution of non-tabled nodes continues until node 4 (with status new) is produced, containing no further subgoals to resolve. The new answer operation changes the status of node 4 to answer, and the bindings in node 4's answer template form a solution to the goal a(a,Z). Since the answer is not a variant of any other answer node for a(a,Z), it will be inserted into the table and scheduled to be returned, via the answer return operation, to all active nodes whose selected subgoal is a variant of a(a,b). In our example, the only such active 11

node is node 1. Answer clause resolution thus creates node 5, which in its turn creates other new answers. Eventually, the evaluation gives rise to two other tabled subgoals, a(b,Z), and a(c,Z), each of which forms the root of its own tree. The DFN of the nodes in Figure 3 corresponds to the order of evaluation of the SLGO automaton (and of the SLG-WAM). It can be seen that the evaluation is largely depth- rst, although the two children of node 2 | nodes 3 and 17 | clearly form an exception, caused by the answer return operation returning the answer a(a,b) to node 1. The process of expanding nodes, adding answers and returning answers, continues until all possible resolutions have been made for a mutually dependent set of subgoals or component. If no further resolution steps are possible for goals in a component, the goals are completely evaluated. In program 1 there are no mutual dependencies, and so there are three singleton components fa(a,Z)g, fa(b,Z)g, and fa(c,Z)g. Using incremental completion (not shown in Figure 3), trees for a component can be disposed as soon as they are completely evaluated. The latter two components could be completed after node 14 is produced. After node 16 has been produced, fa(a,Z)g is also completely evaluated, and its tree could be disposed. The tree in Figure 3 was constructed using the SLGO operations as follows.  new active. Used to produce generator nodes 0, 6, and 12 and to change the status of nodes 1, 5, 7, 11, 13,and 16 from new to active.  new interior. Used to change the status of nodes 2, 3, 8, 9, 14 and 17 from new to interior.  retry generator. Used to produce nodes 1, 2, 7, 8, 13 and 14 (with status new).  retry active. Not used in this example.  retry interior. Used to produce nodes 3, 4, 9, 10, and 17 (with status new).  new answer. Used to transform nodes 4, 10, and 15 from status new to answer.  answer return. Used to produce nodes 5, 11, 15 and 16 (with status new).  completion. Can be used incrementally three times to dispose of the trees for the components fa(a,Z)g, fa(b,Z)g, fa(c,Z)g. A call dependency graph will aid in determining when a system of subgoals is completely evaluated. De nition 3.6 Let S be an SLGT system. A Call Dependency Graph or CDG for a S is the minimal graph, G such that  Nodes(S ) is the set of non-completed subgoals of S.  Links(S ) consists of pairs G1 R G2 where G2 is the selected goal for an active, interior, suspended or generator clause in S (G1), and G2 has not been completed. Given an SLGT evaluation, E , the limit call dependancy graph LCDG of E is the union of the CDGs for all systems which are states of E .

12

0. a(a,Z):gen:a(a,Z)

1. a(a,Z):act:a(a,Z)

Suggest Documents