LOgiC

Programming

~

The Impact

~ ~

Logic Programming on Databases John Grant and Jack Minker

he purpose of this article is to demonstrate the significant impact that logic programming has had on databases. In particular, logic programming has contributed to the understanding of the semantics of a database, has extended the concept of relational databases, and has introduced new techniques in providing useful tools for database users. This section of the article contains some historical background, fundamental concepts from first-order logic and lattice theory, and an axiomatization for deductive databases. At the present time the most commonly used deductive databases are definite. This class is studied in the section "Definite Databases" where the three major semantics: declarative, fixpoint, and procedural, are defined. The handling of negation and implementation issues are also covered in this section. The section entitled "Applications" contains three applications of the use of logic in databases: semantic query optimization, the generation o f cooperative answers, and update validation. Then, three types of deductive databases that are more general than definite databases are considered in the section "General Deductive Databases." Stratified databases allow negation in the body of a statement but not recursion via negation. The wellfounded approach is more general and uses a three-valued logic. Disjunctive databases allow disjunctive

T

COMMUNICATIONSOF THE &¢M/March 1992/Vol.35, No.3

facts and conclusions. T h e article concludes with the section "Summary." Historical Background T h e reader is referred to [30] for an account of the development of deductive databases starting in the 1950s. That paper also includes numerous references which are not included in this article. T h e history is divided there into three parts, each covering approximately 10 years. During the time period 1957-1968 active research on precursors to deductive databases was conducted at several locations in the U.S., including the Rand Corporation, MIT, and Stanford. A system called RDF (relational data file) that had an inferential capability was implemented at the Rand Corporation. The resolution principle was discovered by J.A. Robinson [40] for theorem proving. Green and Raphael [18] at the Stanford Research Institute recognized the application of resolution to the implementation of deduction for databases in a uniform manner. During the second time period, 1969-1978, the concept of logic programming was proposed and then implemented in the language Prolog. Also, the foundations of logic programming were developed by van Emden and Kowalski [45]. Their major results concern definite logic programs and databases and will be sketched in a later section of this article. This work established the equivalence of declara-

tive, fixpoint, and procedural semantics. By 1977 there were several researchers who were applying logic to databases with significant results. Consequently, the Workshop on Logic and Databases was held in November 1977 in Toulouse, France. Some of the early important results about deductive databases were included in the book of papers [14] presented at the Workshop including the results about negation in definite databases that will be given in a later section of this article. The third time period begins in 1979 and leads up to the present. In the early 1980s Reiter [39] proposed formal theories of databases and reinterpreted the conventional model-theoretic perspective on databases in purely proof-theoretic terms. The basic axioms that he defined will be given in a later subsection of this article. In the 1980s the efficient implementation of recursion became an important research topic [44]. T h e handling of negation in rules was clarified with the introduction of stratified databases and the handling of disjunctive information was investigated for disjunctive databases. The Workshop on the Foundations of Deductive Databases and Logic Programming, held in Washington in July 1986, brought together some o f the leading researchers in the field. T h e book [31] of papers described the state-of-the-art in the late 1980s and directions for future research.

67

LOgiC P r o g r a m m i n g Deductive databases represent the convergence o f databases and logic p r o g r a m m i n g . S t a n d a r d relational database systems are used successfully in business, government, and industry. But these systems do not have built-in reasoning capabilities. Logic p r o g r a m m i n g languages typically deal with files as standard p r o g r a m m i n g languages and provide few of the capabilities o f database systems. A deductive database system combines the multiple file handling, concurrency, security, and recovery aspects o f database systems with the logicbased reasoning, in terms o f recursive rules, o f logic p r o g r a m m i n g . T h u s logic p r o g r a m m i n g has had a p r o f o u n d effect on databases both theoretically, providing logical foundations for databases, and practically, by e x t e n d i n g the power o f relational database systems to incorporate logical deduction. Fundamental Concepts This subsection provides some key definitions for logic p r o g r a m m i n g that are i m p o r t a n t in the formalization o f deductive databases. See [16] and o t h e r articles in this issue for additional background. A firstorder language L (for the application o f first-order logic) contains infinitely many variables, propositional connectives, quantifiers, punctuation symbols, constant symbols, function symbols, a n d predicate symbols. A specific language for a deductive database is characterized by its constant and predicate symbols; usually deductive databases contain no function symbols in o r d e r to avoid infinite domains that can lead to an infinite set o f answers. We will assume for this article that there are no function symbols in the language and that the language contains at least one predicate a n d one constant symbol. However, the results in this article apply to the case where there are function symbols, but then a query may have an infinite set o f answers. In particular, function symbols may have to be included for some e x p e r t systems. Terms, atoms (or atomic

68

formulas), and formulas (including sentences) are defined in the stand a r d way. T h e Herbrand universe is the set o f constant symbols o f L; the Herbrand base (HB), is the set o f all atoms that are ground, that is, contain no variables. HB contains all possible facts about the database. T h e semantics o f first-order logic is defined by an interpretation that consists o f a n o n e m p t y domain and which assigns a meaning to each nonlogical symbol: an element o f the d o m a i n to a constant symbol and a predicate on the d o m a i n to a predicate symbol. A n interpretation I is a model for a set o f sentences S if every element o f S is true in I. S logically implies a sentence w (written S I= w) if w is true in all models o f S. A Herbrand interpretation is one whose domain is the H e r b r a n d universe; if it is a model for S, it is called a Herbrand model. A n inference system ( p r o o f theory) consists o f a set o f axiom schemas a n d rules o f inference that are used to prove formulas. T h e sentence w is provable from S (written S I- w) if there is a p r o o f o f w from S. An inference system is sound if S I - w implies S I= w and complete if S I= w implies S I- w. T h e resolution method tries to derive the e m p t y clause from the clausal forms o f S and -qw by using a simple rule called the resolution principle. T h e resolution m e t h o d is sound and complete. A clause has the form "TAlv . . .

v~AnvBlv

...

vBm

where the Ai and Bj are atoms and all variables are universally quantified. T h e equivalent formulation

B1,

• • • , Bm

*--A1

.....

A.

is used with deductive databases. Such a clause may be interpreted in two ways: 1) if AI . . . . , A, are all true, then at ]east one o f B1 . . . . . B m is true; 2) to solve for B1 v . . . v Bin, solve all o f Al . . . . . An. T h e atoms A] . . . . . A, form the body o f the clause; B1 . . . . . Bm form the head. A clause is called rangerestricted if every variable that appears in the head also appears in the body. A clause is recursive if the

same predicate appears both in the head and the body. T h e empty clause, <--, represents a contradiction. A clause with m = 0, that is, an empty head, is a query. For a query Q(xl . . . . . Xk) (with free variables x], .... Xk), a (definite) answer is a k-tuple (al . . . . . ak) such that DB I Q(al . . . . . ak), where DB is the set o f database clauses. Concepts from the mathematical theory o f lattices are i m p o r t a n t for fixpoint semantics. A lattice is a set with a partial o r d e r i n g (-<) relation. For a lattice L and a set X C_ L, a E L is called an upper bound o f X i f x < a for all x ~ X. A least u p p e r b o u n d o f X, lub(X), is unique, if it exists. In a similar but opposite m a n n e r the notions o f lower b o u n d a n d greatest lower b o u n d can be defined. A lattice L is complete if lub(X) and glb(X) exist for every subset X o f L. T h e following definitions are given u n d e r the assumption that L is complete. A m a p p i n g T: L--~ L is monotonic if a -< b implies T(a) < T(b) for all a,b E L, and continuous if T(lub(X)) = lub(T(X)) for every directed subset X o f L. (X is directed if it contains an u p p e r b o u n d for every finite subset.) A n element a E L is called afixpoint o f T if T(a) = a. T h e least fixpoint o f T, if it exists, is written as/fp(T). Powers o f a monotonic m a p p i n g T are defined as follows: T1'0=O (The 0th power o f T is the e m p t y set.) Tl'i+ 1 =T(TTi) (The next power o f T is obtained by applying T to the previous power.) T 1' 0~ = lub {T 1' ili < w} (The a~th power o f T is obtained by taking the lub o f all finite powers.) T h e following two results about lattices will be applied later. 1) I f T is monotonic then lfp(T) exists. 2) I f T is continuous then lfp(T) = TTo~.

AxIomatizatlon A deductive database may be defined as a triple DB = (C, P, I). C is the set o f nonlogical (constant and

March 1992/Vol.35, No.3/COMMUNICATIONS OF THE ACM

predicate) symbols which defines a specific language L. P is a finite set of axioms (the program) in L and may contain metarules, rules not expressible in L, as well. I is a finite set of sentences in L, the integrity constraints, which must be satisfied by the database. T h e integrity constraints will be considered in more detail in the section "Applications." Alternative types of deductive databases differ in the types of statements allowed in P. A basic set of axioms is assumed to be in P for all deductive databases: the domain closure axiom, the unique name axioms, and the equality axiom. T h e domain closure axiom has the form Vx(x = Cl v . . . v x = c,) where cl, • • •, cn are all the constant symbols in C. This axiom assures that the database is finite and all constants can be named. T h e unique name axioms are "-'lC1 = C2 "1Cl =

C3

-'IOn--1 = Cn

where cl . . . . . c, are all the constant symbols in C. These axioms assure that different constant symbols stand for different objects. T h e equality axioms are Vx(x = x) reflexivity, VxVy(x -- y ~ y = x) symmetry, VxVyVz(x = y & y = z ~ x = z) transitivity, Vxl . . . Vy,(R(x] . . . . . Xn) & xl = y] & . . . & x , = y,---~ R(yl . . . . . y,)) substitution for all predicate symbols R. These axioms assure that equality stands for the identity relation. As explained in [15] these axioms are typically not needed for an operational definition of deductive databases where a metarule is given for dealing with negation, but they are useful for u n d e r s t a n d i n g the underlying theory. Assuming from now on that the domain closure, unique name, and equality axioms

COMMUNICATIONS OF THE A C M / M a r c h 1992/Vol.35, No.3

are always in P, we will ignore these basic axioms and let P stand for the r e m a i n d e r of the rules (of a particular deductive database).

Definite Databases I n a definite database P contains only formulas of the form B ~---A1, . . . . A, (n --> 0) where B, A I. . . . . A, are atoms. T h e extensional database is the portion of P that consists of g r o u n d atoms, that is, n = 0 and B contains only constants. T h e extensional database represents the database facts; it is the type of data that can exist in a relational database. T h e intensional database consists of the rest of the formulas of P; it represents the database rules. If there are no recursive rules the intensional database corresponds to views in a relational database. T h e intensional database constitutes the deductive part of the database. Sometimes the terminology Horn database is used instead of definite database because the formulas of P belong to the class of H o r n formulas. Definite databases are also called DATALOG programs. A simple family database will be used to illustrate some concepts about definite databases. T h e following abbreviations are used: P for parent, G for grandparent, A for ancestor, and lower-case letters for names of individuals. P(a,b) P(b,c) P(a,d) ~-P(c,e) P(c,f) G(x,y) ~ P(x,z),P(z,y) A(x,y) ~ P(x,y) A(x,y) ~- P(x,z),A(z,y) I n this case the first five clauses are the extensional database and the last three clauses are the intensional database. T h e last rule is recursive as the atom in the head of the rule appears in the body of the rule. T h e following two queries will be used later. Q I : *-- G(x,y) Q2: ~ A(a,y) This section of the article investi-

gates several issues concerning definite databases. T h e semantics of definite databases is considered first. T h e semantics of a database refers to its meaning. It is important to determine formally the m e a n i n g of a database in order to be certain that the answers obtained for a query are correct. T h e three major semantics for definite databases are considered: declarative semantics, fixpoint semantics, and procedural semantics. Handling negation is another important issue because definite databases contain only positive data. Negation is considered next. At the end of the section, implementation techniques are discussed.

Declarative Semantics T h e concept of interpretation in general, and H e r b r a n d interpretation in particular, was defined earlier in this article as a way to assign m e a n i n g to the nonlogical symbols of a language. Assume now that a specific language has been specified. Declarative semantics is based on H e r b r a n d interpretations. T h e H e r b r a n d Base, HB, is a H e r b r a n d model for every definite database because it satisfies the axioms of P including the fundamental axioms. However, HB is usually not the intended model as it contains all possible facts. T h e usual approach is to look for small H e r b r a n d models in order to make the least n u m b e r of assumptions concerning what is true in the database. T h e following results about definite databases are due to van E m d e n and Kowalski [45]. T h e intersection of every (nonempty) set of H e r b r a n d models for a definite database is a H e r b r a n d model. This result provides a unique smallest H e r b r a n d model, Mp, the minimal model, which is the intersection of all H e r b r a n d models of P. M f is taken to be the intended m e a n i n g of the database. By another theorem, Mp = {A E HBIP I= A}, that is, the minimal model contains exactly the set of atoms logically implied by P, further evidence that it is the intended model. This

69

LOgiC P r o g r a m m i n g model can then be used to answer queries in the following way: (ab . . . . ak) is an answer to the query Q(xl . . . . . Xk) if Q(al . . . . . ak) is true in Mp. In the example given above, Mp = {P(a,b), P(b,c), P(a,d), P(c,e), P(c,f), G(a,c), G(b,e), G(b,f), A(a,b), A(b,c), A(a,d), A(c,e), A(c,f), A(a,c), A(b,e), A(b,f), A(a,e), A(a,f)}. Ano t h e r possible model is Me tO {P(b,a)}, and there are many others. However, all o f the models contain Mp. F u r t h e r m o r e , from the facts and rules there is no reason to conclude that P(b,a) is true, even though it appears in some model. T h e answers to the two queries are given next using the minimal model Mp; for Q]: {(a,c), (b,e), (b,f)}, for Q2: {(b), (d), (c), (e), (f)}. Flxpolnt Semantics Declarative semantics is difficult to apply because it requires the construction o f models a n d then taking their intersection. In fixpoint semantics the intended model is built using H e r b r a n d interpretations in a step-by-step process. Fixpoint semantics for definite databases was also introduced by van E m d e n and Kowalski [45]. Concepts from the theory o f lattices, as explained in an earlier section o f this article, are used in this approach. T h e lattice L is 2 uS, the set o f all subsets o f the H e r b r a n d base, which is also the set o f all H e r b r a n d interpretations, with the subset relation C_ taking the place o f -<. It is a complete lattice. A m a p p i n g T: 2 l-In ~ 2 nB transforms a H e r b r a n d interpretation to a n o t h e r H e r b r a n d interpretation. A special m a p p i n g based on the database axioms P, called Tp, is defined on a H e r b r a n d interpretation I as follows: Tp(I) = {A E HBIA <---AI. . . . . An is a g r o u n d instance of a clause in P and {A1. . . . . An} C_ I}. Tp(I) contains all immediate consequences o f the rules o f P applied to I. For definite databases Tp is continuous. T h e combination o f this t h e o r e m with the results stated e a r -

70

lier about lattices implies that lfp(Tp) -- Tp 1' oJ. Fixpoint semantics picks lfp(Tp) as the meaning o f P. T h e connection between declarative semantics and fixpoint semantics is provided by the following resuit: Mp = lfp(Tp). So the same intended model is obtained by both semantics, but the Tp m a p p i n g provides an algorithm for obtaining that model. T h e Tp o p e r a t o r works in a bottom-up fashion. At the first iteration all atoms in the extensional database are obtained. T h e n , using these facts, the Tp o p e r a t o r finds in one step all atoms that can be derived from that set. This new set is used at the next iteration until no m o r e results are obtained when the Tp o p e r a t o r is used. This is the way answers are f o u n d in relational databases that have views, and is how answers are f o u n d in deductive databases. In the example database,

Tpt0=O Tp ~ 1 = Tt,(Tp T 0) = {P(a,b), P(b,c), P(a,d), P(c,e), P(c,f)} T# 1' 2 = T#(Tp ~ 1)= Tp 1' 1 tO {G(a,c), G(b,e), G(b,f), A(a,b), A(b,c), A(a,d), A(c,e), A(c,f)} T t, ~ 3 = Tp(Tt, T 2)=Tp T 2 U {A(a,c), A(b,e), A(b,f)} Tp ~ 4 = Tp(Tp ~ 3)= Tp 1"3 tO {A(a,e), A(a,f)} TpTn=Tp(Tp~4)= for a l l n - > 4 TpTto = lub {Tp ~' i]i < w} = Tp~'4 Note that Tp T ~0 = Mp. For details concerning fixpoint operators see [3], [25], and [45]. Procedural Semantics Procedural semantics refers to a computational method, usually involving resolution, for obtaining the meaning o f a deductive database. For definite databases linear resolution with selection function based on definite clauses, SLDresolution, is used. In an SLDderivation a database P and a query Q is given. Linear resolution is applied with Q as the top clause. Each time a clause in P is used in the resolution, the clause is given new variables not previously used in the derivation. At each step, an atom,

say A, in the present query is selected, a n d a clause is chosen from P whose head can unify (that is, can be m a d e to match by substituting a variable or a constant for a variable) with A; the new query is obtained by replacing A with the body o f the clause after applying the substitution required for unifcation. An SLD-refutation is an SLDderivation that ends with the e m p t y clause. T h e success set o f P is defined as follows: Succ(P) = {A]A E HB a n d P U {<---A}has an SLD-refutation}. T h e major result o f van E m d e n and Kowalski [45] is that Mp = Succ(P), so that all three semantics yield the same model. Figure 1 shows that A(b,e) E Succ(P). In answering queries, the equivalence o f the declarative and fixpoint semantics assure identical answers by logical implication. For p r o c e d u r a l semantics an answer is obtained by finding an SLD-refutation for the query; the substitutions for the variables provide the answer. Figure 2 shows that (a,c) is an answer for Q1. In those cases where an SLD-refutation does not bind some query variables, all substitutions o f constants in C for the variables are taken as solutions. With this proviso, the three different semantics provide identical answers to queries. Note in particular that this result shows the equivalence o f a forward chaining (fixpoint, which is bottom-up) and a backward chaining (procedural, which is top-down) technique for finding the i n t e n d e d H e r b r a n d model o f a definite database. Unification was defined first by H e r b r a n d and i n c o r p o r a t e d into t h e o r e m proving by Robinson [40]. See [24] for a detailed description o f the history o f the unification algorithm. A simplified form o f the algorithm is used in deductive databases, as they are function-free. SLD-resolution was described by Hill [20], who r e f e r r e d to it as L U S H resolution and r e n a m e d by A p t and van E m d e n [1]. SLD-resolution is a r e f i n e m e n t o f linear r e s -

March 1992/Vol.35, No.3/COMMUNICATiONI OF T H E A C M

olution with selection function developed by Kowalski and K u e h n e r [23]. SL-resolution is a refinement of Loveland's model elimination method [27]. Negation

T h e volume o f negative data is usually much greater than the volume o f positive data. Since the storage o f negative data might overwhelm a database system, databases store positive data only; negation is defined implicitly. T h e r e are three major techniques for dealing with implicit negation: the Closed World Assumption, the Completed Database, and Negation as Finite Failure. T h e Closed World Assumption (CWA), as p r o p o s e d by Reiter [37], is a metarule. Given a database P, the CWA infers a negated g r o u n d atom -vA if it is not the case that P I= A. Intuitively, a g r o u n d atom not implied by the database axioms is assumed to be false. T h e semantics discussed in the previous sections conforms to the CWA. T h e CWA determines Mp, the unique minimal H e r b r a n d model, by subtracting from the H e r b r a n d base the set of atoms whose negation is inferred by the CWA. T h e Completed Database (CDB) a p p r o a c h was introduced by Clark [10]. According to the CDB the axioms o f P provide only the if parts o f the definitions o f predicates; the only if part is implicit. For example, the definition o f the G r a n d p a r e n t predicate G(x,y) *-- P(x,z), P(z,y) indicates that a p a r e n t o f a p a r e n t is a g r a n d p a r e n t , but does not exclude the possibility that G(a,b) is true for some other reason. What is meant is that a g r a n d p a r e n t must be a p a r e n t o f a parent, the only if part:

o f T's definition are satisfied. T h e formal definition o f the CDB is as follows. Each definite clause o f the form

for R rewritten as shown. T h e n the completed database contains, in addition to P, for each T the formula

T(q

T(xI . . . . .

.....

t,) *'--A1 .....

Am

containing the variables Yl . . . . . is rewritten as T(xl

.....

x . ) *-- ( ~ y l )

Yp

• • • (~yp)

(Xl = tl & • • • & x, = t, & A]

. . . & Am)

where Xl, •. •, x, are new variables. For each T, let E1 . . . . . Er be the right-hand sides of the r definitions

<-- A(b,e)

xn)---) E1 v . . . v Er.

(In the special case where there are no definitions for R, the CDB yields VT(xl . . . . . x,).) A negative query can be answered in the CDB by applying a theorem prover. For the example database given earlier, the CDB contains, in addition to the eight clauses in P, the following three formulas:

A(Xl,Yl) <-- P(x1 ,Zl), A(Zl,Yl)

<-- P(b,zl), A(zl,e)

P(b,c) <.--

<-- A(c,e)

A(x2,Y2) <-- P(x2,Y2)

<-- P(c,e)

P(c,e) <--

(---

F i g u r e 1. SLD-refutatlon for a ground atom

<-- G(x,y)

G(xl,yl) <--- P(xl,zl), P(Zl,Yl)

<-,- P(X,Zl), P(Zl,y )

<---- P(b,y)

P(a,b) <----

P(b,c) <--

G(x,y) ~ 3z (P(x,z) & P(z,y)). T h e predicate G is defined by a single clause. For an arbitrary predicate T that is defined by several clauses, according to the CDB T holds if and only if the conditions

COMMUNICATIONS OF THE AOM/March 1992/Vol.35, No.3

F i g u r e 2. SLD-refutatlon with substitution

71

Logi¢ Programming P(Xl,X2) ----)(Xl = a & x2 = b ) v (xl = b & X 2 = C) V (X I = a & X 2 ---- d) v (xl=c&x2=e) v (xx=c&x~=f) G ( X l , X 2 ) ----) 3y (P(xby) & P ( y , x 2 ) ) A(Xl,X2) ~ P(xl,x2) v 3y (P(Xl,y) & A(y,x2)) (Some o f the existential quantifiers a n d equalities were eliminated by simplification.) Negation as Finite Failure (NFF) was also introduced by Clark [10]. N F F is a metarule which states that for any positive g r o u n d atom A, ~ A is proved if every attempt to prove A fails in a finite n u m b e r o f steps. T h e addition o f N F F to SLDresolution is called SLDNF-resolution. In an SLDNF-derivation when ~ A is selected, an attempt is made to solve for A. I f the attempt is successful, then ~ A is considered to have failed; if the attempt fails, then ~ A is considered to have been solved. Figure 3 shows that
12

writing the answer as a disjunction, and taking the negation o f all answers, using inequalities, as the answer to the query. See [43] for a t h o r o u g h study o f negation.

Implementation This subsection briefly discusses some implementation issues for query processing in definite databases. Relational databases, that is, definite databases that are purely extensional, are not considered here. Otherwise, a definite database either involves recursion o r it does not. T h e nonrecursive case is considered first. In a hierarchic database each predicate symbol may be given a level, a non-negative integer, in such a way that for every rule o f the intensional database the level o f each predicate symbol in the body is less than the level o f the predicate symbol in the head. T h u s recursive definitions are precluded. In particular, the example given earlier is not hierarchic because of the A predicate. Now suppose that the example is modified by omitting the last two rules and a d d i n g the rule R(x,y) (-- P(x,z), G(z,y) where the R predicate stands for G r e a t g r a n d p a r e n t . In this case the level m a p p i n g is l(P) = 0, l ( G ) = l, I ( R ) = 2. T h e interpretive method and the compiled m e t h o d are the two major approaches for query processing in hierarchic databases. T h e y were first described by Reiter [38].Consider the query (--R(a,x) for the modified database. T h e interpretive m e t h o d interleaves a top-down pattern-matching proced u r e with a search o f the extensional database. For this query it would use the definition o f R at execution time to transform the original query to (--P(a,z), G(z,x), breaking the problem into two sub-

problems, and then continue with the processing the same way. T h e compiled a p p r o a c h compiles all the rules once before execution time using their definitions; the compiled database contains only rules whose body contains extensional predicates only. For example, the rule for R is compiled to R(x,y) ~ P(x,z), P(z,u), P(u,y), so the query becomes (---P(a,z), P(z,u), P(u,x) in one step, and the p r o b l e m is reduced to a relational database search. For databases involving recursive rules many different approaches have been p r o p o s e d for query processing. T h e two basic methods are bottom-up (forward chaining) a n d top-down (backward chaining). A bottom-up m e t h o d starts with the database facts a n d applies the rules to obtain new facts until the query is generated. A top-down m e t h o d starts with the query and verifies the premises (body) o f the a p p r o priate rules in o r d e r to make the query true. Typically, b o t t o m - u p methods deal with a set o f tuples at a time, while top-down methods work m o r e naturally one tuple at a time. However, b o t t o m - u p methods may be inefficient if they generate many facts not related to the query, while top-down methods deal with facts that are related to the query. O n the other hand, top-down approaches may not terminate in a finite n u m b e r o f steps while b o t t o m - u p methods are g u a r a n t e e d to terminate. A f u n d a m e n t a l bottom-up technique is called naive evaluation. This is essentially the fixpoint semantics using the Te operator, as explained earlier. W h e n lfp(Te) is reached, the answer is taken to be the tuples that make the query true in the model. Naive evaluation tends to be inefficient; one reason for this is that the same tuples may be generated many times d u r i n g the iteration. Some o f the r e d u n d a n c y may be eliminated by the use o f seminaive evaluation. This m e t h o d ap-

March 1992/Vol.35, No.3/COMMUNICATIONSOFTHEACM

~i~i+~'~i ~ i~~/~' i ~f

plies the Tp o p e r a t o r at the i + 1st step not to all of Tp T i but only to a portion o f Tp 1' i. For linear recursion, where no rule contains more than one predicate in its body that is mutually recursive with the predicate in the head, it is possible to replace Tp ~ i by Tp 1' i - Te T (i I) when Te is applied at the i + 1st step. For nonlinear recursion it is more difficult to find the a p p r o p r i ate subset o f Tp "~i. As bottom-up techniques, both naive evaluation and semi-naive evaluation generate many tuples unrelated to the query. Several rewriting methods have been proposed to eliminate useless derivations by using knowledge about the constants in the query, for instance. T h e magic sets method uses the query to transform the original database into a larger database containing an additional predicate called Magic, as well as some of the old clauses, some new clauses, and some modifications of the old clauses. After the transformation the semi-naive evaluation can be used on the new database to process the query. In a sense magic sets simulate the top-down method in a bottom-up framework. Magic sets and other bottom-up techniques work with a d o r n e d predicates, where an adornment gives a binding pattern, a sequence o f b's (for bound, or a constant) and f's (for free, or a variable) to arguments o f a predicate. For the query Q2, for instance, the a d o r n e d goal is A ¥ indicating that the first a r g u m e n t (a) is b o u n d (b) and the second arg u m e n t (y) is free (f). A fundamental top-down technique is the procedural semantics SLD-resolution presented earlier in this article. T h e inefficiency o f SLD-resolution is due partly to its tuple at a time approach. T h e Prolog evaluation m e t h o d is based on SLD-resolution, but its depth-first a p p r o a c h can lead to infinite loops. T h e Query/Subquery (QSQ) method is a more efficient top-down technique. T h e atoms in the body of a rule defining a predicate in the query provide the subqueries. Two

COMMUNICATIONS OF THE ACM/March 1992/Vol.35, No.3

sets are maintained: 1) a set o f answer tuples containing answers to the query and some subqueries, and 2) a set o f current subqueries. T h e process generates new answers and new subqueries at certain steps. While the Q S Q method is topdown, it works with a set o f tuples at a time; and is also complete. It proceeds in a breadth-first manner. See [44] for details on some o f these methods and [4] for an analysis o f a n u m b e r o f recursive methods.

subsumption is applied to integrity constraints facilitating applications to query optimization, informative answer generation, and update validation [ 17]. To illustrate the applications o f integrity constraints, this section introduces a simple knowledge base involving student, course, and teacher information to serve as example. T h e predicates are Sch(edule), Reg(istration), Cat(alog), Ins(tructor), and Tea(cherof) where in Sch(x,y,z), x is the teacher, y is the d e p a r t m e n t , and z is the course; in Reg(x,y,z), x is the student, y is the d e p a r t m e n t , and z is the course; in Cat(x,y,z), x is the d e p a r t m e n t , y is the course, and z is the credits; in Ins(x), x is the teacher; and in Tea(x,y), x is the teacher and y is the student.

Applications T h e previous section on deductive databases dealt only with facts and rules and their expression and meaning within first-order logic. However, true knowledge-based systems have another important c o m p o n e n t that has not yet been discussed, the integrity constraints. T h e inclusion of integrity constraints provides semantic information about the data in the database. As is the case with facts and rules, integrity constraints are also expressed as formulas in first-order logic. T h e integrity constraints are restricted to contain only extensional predicates and are rangerestricted formulas. T h e theme o f this section is the interaction of integrity constraints with the facts and rules o f the knowledge base. A uniform technique called partial

T h e extensional database consists o f the a p p r o p r i a t e tuples, not listed here, for the Sch, Reg, Cat, and Ins predicates. T h e r e is one rule in the intensional database: Tea(x,y) ~ Sch(x,u,v), Reg(y,u,v) stating that x is a teacher o f y if x is the teacher o f a course and y is a student in that course. T h e r e are 4 integrity constraints:

<-- A(a,y),-,P(b,y)

A(xlIYl) +-- P(xl,Yl)

<-- P(a,y), -,P(b,y)

P(a,d) <--

<--- -,P(b,d)

P(b,d)

I

succeeds

fails

Figure 3. SLDNF-refutatlon

73

LOgiC P r o g r a m m i n g IC1. All course numbers in the cosc d e p a r t m e n t are less than 700. y < 700 ~-- Cat(cosc,y,z) IC2. No one is a student and a teacher for the same course. *--Reg(x,y,z), Sch(x,y,z) IC3. Smith teaches only math courses. y -- math <-- Sch(smith,y,z) IC4. Every teacher in Sch appears in Ins. Ins(x) ~ Sch(x,y,z) First the concepts o f partial subsumption and residues are defined and illustrated on the example knowledge base. These residues are then used for transforming a query into a semantically equivalent query that is easier to process: this techn i q u e is called semantic query optimization. T h e n residues are shown to help a knowledge-based system in providing the users with cooperative answers instead of answers that may be technically correct and yet misunderstood. A n o t h e r application o f residues occurs in using / them to simplify the integrity constraints that need to be checked d u r i n g the validation o f updates. Partial Subsumption and Residues This subsection informally describes a technique called partial

subsumption. T h e formal definitions, including proofs o f theorems asserting the correctness o f this technique may be f o u n d in [7]. Partial subsumption yields fragments o f integrity constraints, called residues, which are attached to predicates and used d u r i n g query processing. T h e whole process o f partial subsumption, the generation o f residues, and the attachm e n t o f residues to predicates is called semantic compilation. Semantic compilation is p e r f o r m e d only once, before the processing o f queries and updates, unless a change to the intensional database or the integrity constraints forces a semantic recompilation. Partial subsumption is based on the concept o f subsumption, which is a relationship between two clauses: a clause C subsumes a clause D if there is a substitution ~ such that Ccr, the application o f ~ to C, is a subclause o f D. For example, if C = P(x,y) ~ R(x,z), S(z,y,a), a n d D = P(a,u) ~ R(a,v), R(b,b), S(v,u,a), then C subsumes D by the substitution a for x and u for y, written as {a/x,u/y}. T h e standard algorithm that tests two clauses for subsumption is now illustrated by p e r f o r m ing the test on the C a n d D given above. T h e first step is the transformation o f D to a g r o u n d clause by a substitution 0 that uses constants k l,

P ~ P ( a ' k l ) (-.- R(a,z), S(z,k1,a) (--- S(k2,kl,a)

F i g u r e 4. Subsumptlon

74

R(a,k2)(--S(k2,kl,a) (---

. . . . kn not present in C or D. H e r e 0 = {kl/u,k2/v}, and DO = P(a,kl) ~-- R(a,k2), R(b,b), S(kz,kl,a). T h e n DO is negated; ~ D 0 is a set o f literals. In this case 7 D 0 = {~-- P(a,kl), R(a,k2) ~---, R(b,b) *--, S(k2,kz,a) *--} Next, the algorithm attempts to construct a linear refutation tree with C as the root, using at each step an element o f ~ D 0 for resolution. It can be proved that C subsumes D if and only if at least one such refutation tree ends with the null clause. T h e tree for this example is given in Figure 4. Partial subsumption is essentially the application o f the subsumption algorithm with C as the integrity constraint and D as the body o f a rule that defines a predicate. Since partial subsumption is p e r f o r m e d both for intensional and extensional predicates and there are no rules for extensional predicates, the d u m m y rule R * - R is a d d e d for each extensional predicate R. In general, the subsumption algorithm is not expected to yield the null clause because that would imply that the integrity constraint subsumes the body o f the axiom, meaning essentially that there is a conflict between the rule and the integrity constraint. However, there might still be a linear resolution tree that ends with a clause that is simpler than the integrity constraint. Such a f r a g m e n t is called a residue. T h e rules may have to be modified to a logically equivalent form in o r d e r to obtain useful residues. T h e details can be found in

[7]. Semantic compilation applies partial subsumption for each rule with each integrity constraint. T h e nontrivial residues are attached in braces to each rule. Semantic compilation is d e m o n s t r a t e d now for the knowledge base example given earlier. T h e r e is a semantically compiled rule for each rule o f the intensional database as well as for

March 1992/Vol.35, NO.3/COMMUNIOATIONSOFTHEACM

IIII

each predicate (dummy rule) of the extensional database. T h e first rule, the semantically compiled (dummy) rule for Sch is explained in detail. SCR1. Sch(x,y,z) *-- Sch(x,y,z) {~--Reg(x,y,z); y = math ~ x = smith; Ins(x) ~ } T h r e e residues are obtained in this example, one each from IC2, IC3, and IC4. Consider the meaning o f the residues. Let (x,y,z) be a tuple in the Sch relation. It is not possible for (x,y,z) to be a tuple in Reg. I f x = smith then y = math. T h e Ins relation contains x. Consider now the derivation o f the second residue. T h e integrity constraint IC3 (C in the subsumption algorithm) is first modified to IC3': y = math ~ Sch(x,y,z), x = smith. T h e body o f the rule (D in the subsumption algorithm) is transf o r m e d to the g r o u n d clause *--Sch(kx,k2,ks) by the substitution 0 = {kJx,k2/y,kJz}. T h e n ~DO = {Sch(kbk~,k~) ~-- }. T h e resolution of IC3' and 'qD0 yields k2 = math ~ kl = smith. At this point the reverse substitution 0 -1 = {x/kx,y/k2,z/k3} is applied to obtain y = math ,~- x = smith SCR2. Reg(x,y,z) ,~- Reg(x,y,z) {',-- Sch(x,y,z)} SCR3. Cat(x,y,z) ~-- Cat(x,y,z) {y < 700 ~-- x =cosc} SCR4. Ins(x) ~ Ins(x) { } (no residues)

I

I

m e t h o d of partial subsumption and residues can be applied to semantic query optimization. T h e semantic compilation o f rules, as given above, is assumed. T h e residues are used to transform a query to a semantically equivalent query that is easier to process; two queries are semantically equivalent if their answers are the same for all knowledge bases with the same intensional database and integrity constraints. O u t p u t variables are starred. 1. Print the name of every teacher in the math d e p a r t m e n t whose name appears in Ins. Q1. ~- Sch(x*,math,z), Ins(x*) Without semantic query optimization the processing o f this query requires p e r f o r m i n g a join. T h e first step is to obtain the corresponding semantically compiled query by including the instantiated residue for each relation using the semantically compiled rules. In this case SCR1 and SCR4 are used. SCQ1. ~ - Sch(x*,math,z) {~--- Reg(x*,math,z); math = math ~ x* = smith; Ins(x*) ,~- }, Ins(x*) { } T h e first residue states that a certain tuple cannot be in the Reg relation. This is not a useful residue. T h e second residue is a valid statement; that is also not useful. However, the third residue is useful; it states that x* must a p p e a r in the Ins relation. Consequently, the Ins predicate can be eliminated from the query. Hence the query Q I ' is semantically equivalent to Q I and can be processed instead of Q1. Q I ' ~-- Sch(x*,math,z)

SCRS. Tea(x,y) ~-- Sch(x,u,v), Reg(y,u,v){*-- x = y; u = math ~ x = smith; Ins(x) ~ }

2. Print the course numbers for all c o m p u t e r science courses in the Cat relation.

Query OPtimization

By SCR3,

Q2. ~-- Cat(cosc,y*,z)

T h e use of integrity constraints for query optimization is called semantic query optimization because it involves semantic knowledge about the knowledge base. This subsection shows by four examples how the

COMMUt4|~TIONS OF THm ~M/March

1992/Vol.35, No,3

SCQ2. ~-- Cat(cosc,y*,z) {y* < 700 *-- cosc - c o s c } I n the residue the body is true, hence it can be omitted to obtain y* < 700 ~---. Now

Q2' ~ Cat(cosc,y*,z), y* < 700 This version o f the query is useful if the Cat relation is sorted on course number, because it limits the search to y* values less than 700. It introduces a selection criterion into the query. 3. Print all d e p a r t m e n t s in which smith teaches. Q3. ~ Sch(smith,y*,z) By SCR1, SCQ3. *-- Sch(smith,y*,z) {~--- Reg(smith,y*,z); y* = math ~ smith = smith; Ins(smith) ~ } In this case the first and third residues are not useful. In the second residue the body may be eliminated because it is true. T h e r e f o r e the answer, if it exists, is unique. Here Q3' ~ Sch(smith,y*,z), y* = math T h e a p p e a r a n c e of a tuple in the Sch relation with first element smith must still be checked before concluding math as the unique answer. 4. Print the names o f all individuals who are their own teachers in a course. Q4. ~ Tea(x*,x*) By SCR5, SCQ4. *-- Sch(x*,u,v), Reg(x*,u,v) {<-'- X* = X*;

u = math <-- x* = smith; Ins(x*) <-- } Since the first residue is false, this query cannot have any answers. In this case Q4' ,is the null query. Note that without

semantic query optimization the query processor would execute a potentially large join. T h e first papers on semantic query optimization were by McSkimin and Minker [28], Hammer and Zdonik [19], and King [22]. T h e formalization o f the approach and d e v e l o p m e n t o f the

~

Logic Programming concept of partial subsumption is due to Chakravarthy, Fishman, and Minker [6].

Informative Answers T h e previous subsection showed how residues may be used for query optimization. T h e answer to a query was a set of tuples in each case. Such an answer, while technically correct, may be misleading to the user. An intelligent knowledge base system should provide more informative answers whenever that is possible. T h e purpose of this subsection is to illustrate the application of residues to the generation of informative answers on the knowledge base example of this section. Such an informative answer would be provided in a natural language. We now reconsider the four example queries for query optimization from the previous subsection. T h e first query, Q1, asks for the name of every teacher in the math d e p a r t m e n t whose name appears in Ins. Using the residue, it is possible to eliminate the join of Sch with Ins in processing this query. However, note that the user must have had a misconception about the Ins relation in formulating the query. That is, the user apparently did not know that every teacher's name appears in Ins. T h e system should point this out. So in addition to the list of teachers in the math d e p a r t m e n t it could print the statement of IC4, "Every teacher in Sch appears in Ins." T h e second query, Q2, requests the course n u m b e r s for all cosc courses in the Cat relation. T h e residue y* < 700 ~- was used here for query optimization. T h e additional information provided by the residue is the statement of IC1, "All course n u m b e r s in the cosc department are less than 700." However, it is not clear that such a statement would be helpful to the questioner. T h e system should be helpful and informative but not verbose. T h e content of IC1 need not be given here. However, suppose the second query were changed slightly to request all course n u m b e r s that are

76

less than 700 for all cosc courses in the Cat relation. In this case a portion of the query is identical to the residue. So the user probably thinks that there are cosc course n u m b e r s that are at least 700. I n this case the statement of IC1 would be helpful to correct the mistaken assumption. Heuristic rules are used to select the appropriate response as described in Gal and Minker [13]. T h e third query, Q3, asks for all departments in which smith teaches. T h e residue y* = math *-(after its true body is omitted) indicates that the answer is unique, if it exists. Assuming that smith actually teaches at least one course, the answer is {math}. This answer, by itself, does not exclude the possibility that at other times smith may teach courses in other departments. T h e statement of IC3, "Smith teaches only math courses." provides additional information to the person who formulated Q3. Finally, Q4 requests the names of all individuals who are their own teachers in a course. Semantic query optimization is very helpful here in obtaining the empty set as the answer. But merely providing the empty set as the answer is not necessarily informative. It does not rule out the possibility that at another time a person may be his/her own teacher in a course. T h e statement of IC2, "No one is a student and a teacher for the same course." gives the reason for the answer to the query. T h e formalization of the use of integrity constraints to provide informative answers is due to Gal and Minker [ 13]. They use earlier work by Kaplan [21] and others.

updates T h e previous subsections dealt with queries; this subsection considers updates. T h e integrity constraints of a knowledge base must remain true after updates. A n update which results in a violation of an integrity constraint must not be allowed to execute. Therefore an update should be validated to make sure that it does not change the

truth of any integrity constraint. This subsection shows by examples how the method of partial subsumption and residues helps in this process. Essentially, the method involves checking the truth of instantiated residues (the instantiation determined by the update) instead of the original integrity constraints. As the instantiated residues are usually simpler formulas than the integrity constraints, checking the former is faster than checking the latter. T h u s this technique optimizes the update validation process. It is assumed that only extensional relations are explicitly updated. For insertions the semantically compiled versions of the d u m m y rules are used. For the example of this section those are SCR1-SCR4. Consider SCR1 first and suppose that a tuple (a,b,c) is to be inserted into Sch. T h e instantiation uses the substitution {a/x,b/y, c/z} and the residues become Reg(a,b,c); b = math <---a = smith; Ins(a) ~---. T h e following actions must be taken to check the residues: 1) T h e Reg relation must not contain (a,b,c), 2) I f a = smith then b = math, 3) T h e Ins relation must contain the element a. Suppose now that (a,b,c) is to be inserted into Reg. T h e residue becomes *--Sch(a,b,c); so for this insertion the Sch relation must not contain (a,b,c). Next, for the insertion of (a,b,c) into Cat it suffices to check that if a = cosc then b < 700. Finally, an insertion into Ins does not violate any integrity constraints. If the update is a deletion, different d u m m y rules must be used: not R ~ not R, for each extensional relation R. I n the partial subsumption algorithm the effect of this change is the movement in 7 D 0 of R(kl . . . . . k,) from the left of the arrow to the right. I n this example there are no residues for not Sch, not Reg, and not Cat. Hence deletion from Sch, Reg, a n d Cat cannot violate any integrity constraints. T h e only residue is ~--Sch(x,y,z) for

March 1992/Vol.35, No.3/COMMUNICATIONS OF T H E A C M

not Ins. This means that if the element a is to be deleted from Ins then the Sch relation should not contain a tuple whose first element is a. Often an update is part o f a transaction. In that case the whole transaction, rather than an individual update, must be validated. An integrity constraint is allowed to become false d u r i n g the transaction, but all integrity constraints must be true at the end o f the transaction. T h e r e f o r e the truths o f the residues for each u p d a t e must be checked on the new relations that result from p e r f o r m i n g all the updates o f the transaction. Consider the transaction Trl: Insert
General Deductive Databases Definite databases have a straightforward semantics and are applicable in many typical database contexts. But definite databases have some limitations in their expressiveness. Recall that the rules have the form B ~---Al. . . . .

A, (n >- 0)

A n are all atoms. In some cases it is useful to allow negated atoms in the body o f a rule;

where B, A l . . . . .

C O M M U N I C A T I O N S OF THE ACM/March

normal databases have this capacity. T h e rules of a normal database have the form B *---L1 . . . . .

Lm (m > 0)

where B is an atom, L], • • •, Lm are literals (that is, atoms or negations o f atoms). T h e semantics o f normal databases can be problematic when recursion is combined with negation. Stratified databases have a more manageable structure as they do not allow such a combination. For databases that are not stratified, the well-founded a p p r o a c h provides a reasonable semantics via three-valued logic. Finally, disjunctive databases allow disjunction in the head o f a rule. T h e following subsections contain the definitions as well as some examples and comments about the fixpoint semantics for stratified databases, normal databases using the well-founded approach, and disjunctive databases.

Stratified Databases Normal databases allow rules whose body contains negated atoms. Suppose, for instance, that a supplier wishes to write a rule to backorder items that are not in the warehouse. A natural way to write the rule is as Backorder(x) *-- Item(x), -7Warehouse(x). This rule is allowed in a normal database, but not in a definite database. However, the semantics o f a normal database is not always clear. Consider the use o f the following two rules:

mal database P is stratified if there is a level m a p p i n g r for all predicate symbols such that for every rule o f the form B *--Lx . . . . . Lm for all i, 1 - i-< m, if Li is an atom then r(Li)-< r(B) and if Li is a negated atom then r(Li) < r(B). T h e rules o f P are placed into strata Pi with each Pi containing the definitions o f the predicates o f level i. T h e example above with Female and Male is not stratified because the definition would require r(Male) < r(Female) and r ( F e m a l e ) < r(Male). A stratified database may have many possible stratifications; it is convenient to assume that the levels are 1. . . . . n. Consider now the following stratified database about a g r a p h where the following abbreviations are used: E for Edge, P for Path, and A for Acyclicpath. E(a,b) E(b,c) ~-E(c,b) E(c,e) E(c,f) E(f,c) P(x,y) ~ E(x,y) P(x,y) ~ E(x,z), P(z,y) A(x,y) ~ P(x,y),'7(p(y,x) T h e level m a p p i n g r(E) = 1, r(P) = 1, r(A) = 2 provides a stratification. Fixpoint semantics for stratified databases is similar to fixpoint semantics for definite databases. However, the Tp o p e r a t o r need not be monotonic for stratified databases. For instance, in the earlier example with the predicates Female and Male, if the database contains only the first rule

Female(pat) *-- 7Male(pat) Male(pat) *- ~Female(pat)

Female(pat) *-- TMale(pat),

It is certainly not clear whether Female(pat) or Male(pat) should be taken as true. Stratification disallows the combination o f recursion wi~h negation that may obscure the semantics o f the database. Stratification involves the assignm e n t o f a level or rank, r(R), for each predicate symbol R. T h e level r(R) is a positive integer. T h e level o f a literal is the level of the corresponding predicate symbol. A nor-

Tp({Male(pat)}) = O.

1992/Vol.35, No.3

Tp(O) = {Female(pat)},

To avoid this problem, cumulative powers o f an o p e r a t o r T (in this case Tp) are used for stratified databases as follows: T ~' 0(I) = I T 1' (i + 1)(I) = T ( T 1' i(I)) t.J T T i(I) T 1' ~o(I) = U{T 1' i(I)li < ~o} T h e definition o f fixpoint semantics also proceeds t h r o u g h the levels

17

Logic Programming 1..... n via the strata P[ . . . . . of the database

Pn

m i = TPi ~ t o ( m / _ [ ) ,

where M0 = 0 . In particular, for the graph example, m l = {E(a,b), E(b,c), E(c,b), E(c,e), E(c,f), E(f,c), P(a,b), P(b,c), P(c,b), P(c,e), P(c,f), P(f,c), P(a,c), P(b,b), P(c,c), P(f,f), P(b,e), P(b,f), P(f,b), P(a,e), P(a,f)} M2 = M[ U {A(a,b), A(c,e), A(a,c), A(b,e), A(a,e), A(a,f)} T h e concept of stratification was discussed first by C h a n d r a and Harel [9]. Apt, Blair, and Walker [2] formalized the fixpoint operator described previously. Van Gelder [46] and also Naqvi [33] had the idea of stratification at the same time as Apt, Blair, and Walker. Przymusinski [35] showed that an alternative approach based on a preference relation yields the same model, called the perfect model, as the one obtained by the fixpoint semantics.

Well-founded Approach T h e minimal model defined in the previous section for stratified databases appears to be the best model intuitively. But there are normal databases that are not stratified and yet have a model that clearly seems to be appropriate. Consider the non-stratified database of the following three rules: R(a) *-- 7R(b), "TR(c) R(b) ~-- 7R(a) R(c) ~Intuitively there is an appropriate minimal model: {R(b),R(c)}. As will be shown later in this subsection, this is the model produced by the well-founded approach, which defines semantics for all normal databases. T h e well-founded approach generalizes the semantics of stratified databases in the sense that its semantics coincides with the description given in the previous subsection for stratified databases and it provides intuitively reasonable

78

semantics for non-stratified databases. T h e well-founded approach uses a three-valued logic with the truth values true (1), undefined (1/2), and false (0). A three-valued interpretation assigns one of the three truth values to all the atoms of the H e r b r a n d base, I:HB--> {1,V2,0}. A n interpretation is often written as I = (T,F), where T = {A E HB I I(A) = 1} and F = {a E HB[ I(A) = 0}. Here, T stands for the true atoms and F for the false atoms. An interpretation is twovalued if T U F = H B . I is extended to formulas by the following equations: I(-7A) = 1 - I(A) I(A & B) = min(I(A),I(B)) I(A v B) = max(I(A),I(B)) I(B *-- A) = 1 if I(B) > I(A), = 0 otherwise An interpretation I is a model of a normal database P if I ( S ) = 1 for every S E P. Interpretations are ordered by using the numerical values of the truth-values: I -< I' if I(A) -< I'(A) for all A G HB. A minimal model I is one such that there is no model I' with I' < I. Intuitively, a minimal model minimizes T and maximizes F. This generalizes the concept of minimal models for definite databases where the logic is two-valued and the set of true atoms is minimized. T h e wellf o u n d e d approach provides a minimal three-valued model for every database. For stratified databases this model is two-valued a n d is the one described in the previous subsection. Let I = (T,F) be an interpretation, T C_ HB, F C_ HB. Define T/(T) = {A E HB I there is a g r o u n d instance of a rule in P, A <--LI . . . . , Lm, such that for every i, 1--
FI(F) = {A E HB I for every g r o u n d instance of a rule in P, A<--L1 . . . . . Lm, there is an i, 1 -< i -< m, such that either I(Li) = 0 or Li E F}. Intuitively, I = (T,F) represents the facts currently known to be true or

false, TI(T) contains immediately derivable new true facts and F/(T) contains immediately derivable new false facts. T h e operations 1"1 and F1 are monotonic and can be iterated to a fixpoint as follows: T1 ~' 0 = O, FI ~, 0 = HB r l l ' n + 1 = Tl(r~'~ n), F / ~ , n + 1 = Ft(FI~, n), T1 = U{TI'r n I n < t0}, FI = n{Ft ~ n I n < t0}. Basically, 7"1 contains the new atomic facts derivable from I and F1 contains the new atomic facts whose falsity can be assumed by the Closed World Assumption. Define Tp a s T p ( I ) = I O ( T I , F I ) . T h e models for the database P are defined as follows: Mo = ( 0 , 0 ) Mi = Tp(Mi-1)

T h e well-founded model of P, Mp, is the least fixed point of Tp. In the case of the example of the nonstratified database given at the beg i n n i n g of this subsection, M0 = (O,O), M} = ({R(c)},O), M2 = ({R(c),R(b)},{R(a)}) is the well-founded model. T h e well-founded approach was developed by Van Gelder, Schlipf, and Ross [47]. Przymusinski [36] elaborated on their result.

Disjunctive Databases T h e rules of a definite database have a single atom in the head. Indefinite information cannot be stored in a definite database. I n a disjunctive database the head may contain a disjunction of atoms: the rules have the form BI v . . . vBm

<--A1 .....

A,

where B1 . . . . . Bin, A1. . . . , A, are all atoms. (The case where the body may contain negated atoms is not considered here.) I n particular, disjunctive facts of the form B1 v . . . V Bm are allowed. Consider the following disjunctive database about a graph where E abbreviates Edge a n d P abbreviates Path.

March 1992/Vol.35, No.3/COMMUNICATIONSOFT H E

ACM

i

E(a,b) E(b,c) P(x,y) P(x,y)

*-v E(b,d) ~ E(x,y) ~ E(x,z), P(z,y)

T h e disjunctive fact indicates that either (b,c) is an edge or (b,d) is an edge. Consider now the query ~ E(x,y). Clearly, (a,b) is an answer, but so is (b,c) or (b,d). Such an answer has not been considered until this subsection, however. So the notion o f an answer to a query needs to be redefined, because the previous definition was for definite answers only. For a query Q(xl . . . . . Xm) an (indefinite) answer has the form (all, ....

aim) (akl .....

+ (a21 . . . . . akin) s u c h

P t= Q(all . . . . .

a2m) +''"

T h e rule e m b o d i e d in the ~e operator is actually an instance o f the hyperresolution rule developed by Robinson [41]. See also Robinson [42]. T h e results of the ~ o p e r a t o r on the above example, (omitting subsumed clauses) are: 1' 1 = {E(a,b), E(b,c) v E(b,d)} T/e I' 2 = T/e 1' 1 tO {P(a,b), E(b,c) v P(b,d), P(b,c) v E(b,d)} T~e 1' 3 = T/e I' 2 tO {P(a,c) v P(b,d), E(b,c) v P(a,d), P(a,c) v E(b,d), P(b,c) v P(a,d), P(b,c) v P(b,d)} T 4 = ~ T 3 to {P(a,c) v P(a,d)} ~ ~' 60 = ~ ~'4.

+

T h e set ~ I' o~ is the set o f all minimal disjunctions implied by the database P. T h e methods given for negation in definite databases, CWA, CDB, and NFF, are not a p p r o p r i a t e for disjunctive databases. Consider that in the example disjunctive database, the CWA would a d d ~E(b,c) and ~E(b,d), thereby making the database inconsistent. All three methods can be generalized, however, to disjunctive databases. Only the GCWA (Generalized Closed World Assumption), introduced by Minker [29], is discussed here briefly. T h e definition is as follows: I f A is a g r o u n d atom, then ~ A is inferred from the database P by the GCWA if A is not a m e m b e r o f T = {B I B i s a g r o u n d a t o m , P I- B v K, K is a positive (or null) clause and P] K}. For the query

that

alm) v

Q(a21 .....

a2m ) V . . . V

Q(akl . . . . .

akm).

T h u s an answer is a sum o f tuples such that in every model o f P at least one o f Q(ail . . . . . aim) is true. A minimal indefinite answer is one none o f whose subsums is also an answer. Usually, one is interested only in minimal indefinite answers, so that only those will be listed. In particular, the two answers for *-- E(x,y) are (a,b) and (b,c) + (b,d). Fixpoint semantics for definite databases is developed via the H e r b r a n d base. For disjunctive databases the H e r b r a n d base must be e x t e n d e d to include disjunctions; the Disjunctive H e r b r a n d base, DHB, consists o f the set o f all finite disjunctions o f different atoms in the H e r b r a n d base. T h e closure o p e r a t o r is defined on states, where a state S is a subset o f DHB. Intuitively, a state is a set o f true disjuncts. T h e o p e r a t o r T/ is defined as follows: T/(S) = {E ~ E H B c [ E' *-- A1, A2, • . . , A. is a g r o u n d instance o f a rule in P, A1 v E l . . . . . A. vE. are in S (any o f E1 . . . . . En may be null), E" = E' vE1 v . . . v E n , and E is the smallest factor o f En; the A1 . . . . , An are atoms; E1 . . . . . En, E, E', E" are disjunctive facts}.

COMMUNICATIONS OF THE

*--'nE(x,y) the GCWA answer is {(a,a), (a,c), (a,d), (b,a), (b,b), (c,a),

(c,b), (c,c), (c,d), (d,a), (d,b), (d,c), (d,d)}. For definite databases the GCWA reduces to the CWA. T h e concept o f an answer to a query was formalized first by Reiter [37]. T h e fixpoint o p e r a t o r for disjunctive logic programs described above is due to Minker and Rajasekar [32]. A comprehensive

ACM/March 1992/Vol.35, No.3

account o f the foundations o f disjunctive logic p r o g r a m m i n g , including two chapters on disjunctive deductive databases, is contained in [26].

Summary Logic p r o g r a m m i n g has had a profound impact on databases• T h e theory o f logic p r o g r a m m i n g has been used to provide a solid theoretical foundations for databases• F u r t h e r m o r e , these theoretical concepts have been applied to practical database problems• This article started by sketching the historical b a c k g r o u n d and by presenting some fundamental logical concepts including the basic axioms for databases• This article emphasized definite deductive databases, databases that may contain facts and definite (Horn) rules• T h e three major types o f semantics, declarative, fixpoint, and procedural, were explained as well as the three major techniques for negation, the Closed World Assumption, the Completed Database, and Negation as Finite Failure. Aspects o f deductive database implementation were also considered. T h r e e application areas were considered next, query optimization, informative answer generation, and u p d a t e validation; all three by using a u n i f o r m method called partial subsumption. Finally, some generalizations o f definite databases, defined by allowing certain classes o f logical formulas as rules, were presented: stratified databases, the well-founded approach to normal databases, and disjunctive databases• Deductive databases are useful for e x p e r t systems and knowledgebased systems• Several prototypes have been u n d e r d e v e l o p m e n t at ECRC, MCC, Stanford University, and the University o f Melbourne• Small deductive databases may be i m p l e m e n t e d rapidly using the logic p r o g r a m m i n g language Prolog. Many practical implementation issues have to be addressed before the database types described in the latter part o f the article are implem e n t e d efficiently. [ ]

19

i¸i!!¸¸~i

Logic Programming References

1. Apt, K.R. and van Emden, M.H. Contributions to the theory of Logic Programming. J. ACM 29, (1982), 841-862. 2. Apt, K.R., Blair, H.A., and Walker, A. "Towards a Theory of Declarative Knowledge." In Foundations of

Deductive Databases and Logic Programming, J. Minker, Ed. Morgan Kaufmann, 1988, 89-148. 3. Apt, K.R. "Logic Programming." In

Handbook of Theoretical Computer Science, Vol. B, J. van Leeuwen, Ed., Elsevier, 1990, 493-574. 4. Bancilhon, F. and Ramakrishnan, R. Performance evaluation of data intensive logic programs." In Foun-

dations of Deductive Databases and Logic Programming, J. Minker, Ed. Morgan Kaufmann, 1988, 439517. 5. Blaustein, B.T. "Enforcing Database Assertions: Techniques and Applications", Ph.D. dissertation, Computer Science Department, Harvard University, 1981. 6. Chakravarthy, U.S., Fishman, D. H., and Minker, J. "Semantic Query Optimization in Expert Systems and Database Systems." In Expert Database Systems, L. Kerschberg, Ed. Benjamin-Cummings, 1985, 659675. 7. Chakravarthy, U.S., Grant, J., and Minker, J. "Foundations of Semantic Query Optimization for Deductive Databases." In Foundations of

Deductive Databases and Logic Programming, J. Minker, Ed. Morgan Kaufmann, 1988, 243-273. 8. Chan, C. Constructive negation based on the completed database. In Logic Programming Proceedings of

the 5th International Conference and Symposium, R. A Kowalski and K.A. 9.

10.

11.

12.

Bowen, Eds. The MIT Press, (1988) pp. 111-125. Chandra, A. and Harel, D. Horn clause queries and generalizations. J. Logic Program. 2 (1985), 1-15. Clark, K.L., "Negation as Failure". In Logic and Databases, H. Gallaire and J. Minker, Eds. Plenum, 1978, 293-322. Colmerauer, A. "Theoretical Model of Prolog II". In Logic Programming and its Application, M. Van Caneghem and D. Warren, Eds., Ablex, 1986, 3-31. Fagin, R., Ullman, J., and Vardi, M. On the semantics of updates in databases. In Proceedings of the Sec-

ond ACM Symposium on the Principles

0

of Database Systems, (1983) 352-365. 13. Gal, A. and Minker, J. "Informative and Cooperative Answers in Databases Using Integrity Constraints." In Natural Language Understanding and Logic, P. Saint-Dizier and V. Dahl, Eds. North-Holland, 1988. 14. Gallaire, H. and Minker, J., Eds. Logic and Databases. Plenum, 1978. 15. Gallaire, H., Minker, J., and Nicolas, J. Logic and databases: A deductive approach. ACM Comput. Surv. 16 (1984), 153-185. 16. Grant, J. and Minker, J. Deductive database theories. Knowled. Eng. Rev. 4, (1989), 267-304. 17. Grant, J. and Minker, J., "Integrity Constraints in Knowledge Based Systems." In Knowledge Engineering Vol H, Applications. H. Adeli, Ed., McGraw-Hill, 1990. 18. Green, C.C. and Raphael, B. The use of theorem-proving techniques to question-answering systems. In Proceedings of the 23rd National Conference of the ACM (1968), 169-181. 19. Hammer, M.M. and Zdonik, S.B. Knowledge based query processing. In Proceedings of the 6th VLDB Conference (1980), 137-147. 20. Hill, R. "LUSH Resolution and Its Completeness." DCS Memo No. 78, University of Edinburgh, School of Artificial Intelligence, 1974. 21. Kaplan, J. "Cooperative Responses from a Portable Natural Language Data Base Query System." P h . D . dissertation, Department of Computer and Information Sciences, University of Pennsylvania, 1979. 22. King, J.J. "Query Optimization by Semantic Reasoning." Ph.D. dissertation, Department of Computer Science, Stanford University, 1981. 23. Kowalski, R.A. and Kuehner, D. Linear resolution with selection function. Artific. InteU., 2 (1971), 227-260. 24. Lassez, J.-L., Maher, M.J., and Marriott, K. "Unification Revisited." In

Eds., Elsevier North-Holland, 1969, 73-86. 28. McSkimin, J.R. and Minker, J. The use of a semantic network in a deductive query answering system. In Proceedings of the 5th IJCAI, (1977), pp. 50-58. 29. Minker, J. On indefinite databases and the closed world assumption. In Proceedings of the 6th CADE, D. Loveland, Ed. Springer-Verlag Lecture Notes in Computer Science 138, (1982), 292-308. 30. Minker, J. Perspectives in deductive databases. J. Logic Prog. 5 (1988), 33-60. 31. Minker, J. Ed. Foundations of Deduc-

tive Databases and Logic Programming. Morgan Kaufmann, 1988. 32. Minker, J. and Rajasekar, A. A fixpoint semantics for disjunctive logic programs. J. Logic Prog. 9 (1990), 45-74. 33. Naqvi, S.A. A logic for negation in database systems. In Proceedings of

the Workshop on Foundations of Deductive Databases and Logic Programming, J. Minker, Ed. (1986), 378-387. 34. Nicolas, J.M. Logic for improving

integrity checking in relational data bases. Acta Informatica 18 (1982), 227-253. 35. Przymusinski, T.C. "On the Declarative Semantics of Deductive Databases and Logic Programs." In

Foundations of Deductive Databases and Logic Programming, J. Minker, Ed. Morgan Kaufmann, 1988, 193216. 36. Przymusinski, T. C. Every logic program has a natural stratification and an iterated fixed point model. In Proceedings of the 8th Symposium on Principles of Database Systems, (1989), 11-21. 37. Reiter, R. "On Closed World Data-

38.

Foundations of Deductive Databases and Logic Programming, J. Minker, Ed. Morgan Kaufmann, 1988, 587625. 25. Lloyd, J.W. Foundations of Logic Pro-

39.

gramming, Second Extended Edition. Springer-Verlag, 1987. 26. Lobo, J., Minker, J., and Rajasekar,

A. Foundations of Disjunctive Logic Programming. MIT Press, 1992. 27. Loveland, D. "Theorem Provers Combining Model Elimination and Resolution." In Machine Intelligence, Vol. 4. B. Meltzer and D. Michie,

40.

41.

bases". In Logic and Databases, H. Gallaire and J. Minker, Eds. Plenum, 1978, 56-76. Reiter, R., "Deductive QuestionAnswering on Relational Databases." In Logic and Databases, H. Gallaire and J. Minker, Eds. Plenum, 1978, 149-178. Reiter, R. "Towards a Logical Reconstruction of Relational Database Theory." In On Conceptual Modelling, M.L. Brodie, J. Mylopoulos, and J.W. Schmidt, Eds. SpringerVerlag, 1984, 191-233. Robinson, J.A. A machine-oriented logic based on the resolution principle."J. ACM 12 (1965), 23-41. Robinson, J.A. Automatic deduc-

March 1992/Vol.35, No.3/COMMUNICATIONS OF THE ACM

tion with hyper-resolution. Int. J. Comput. Math. 1 (1965), 227-234. 42. Robinson, J.A. Logic and Logic Programming. Commun. ACM 35, 3(Mar. 1992). 43. Shepherdson, J.C. "Negation in Logic Programming." In Founda-

tions of Deductive Databases and Logic Programming, J. Minker, Ed. Morgan Kaufmann, 1988, 19-88. 44. Ullman, J.D. Principles of Database

search interests include artificial intelligence, deductive databases, logic programming, and nonmonotonic reasoning. Author's Present Address: Department of Computer Science, University of Maryland, College Park, MD 20742; email: [email protected] Research supported by the National Science Foundation under grant numbers IRI8921951 and IRI-8916059

Permission to copy without fee all or part of this material is granted provided that the copies are not made or distributed for direct commercial advantage, the ACM copyright notice and the title of the publication and its date appear, and notice is given that copying is by permission of the Association for Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission. © ACM 0002-0782/92/0300-066 $1.50

and Knowledge-BaseSystems, Volumes1 and H. Computer Science Press, 1988. 45. van Emden, M.H. and Kowalski, R. The semantics of predicate logic as a programming language. J. ACM 23 (1976), 733-742. 46. Van Gelder, A., "Negation as Failure Using Tight Derivations for General Logic Programs." In Foun-

dations of Deductive Databases and Logic Programming, J. Minker, Ed. Morgan-Kaufmann, 1988, 149176. 47. Van Gelder, A., Ross, K.A., and Schlipf, J.S. The well-founded semantics for general logic programs. J. ACM 38, (1991), 620-650. CR Categories and Subject Descriptors: F.4.1 [Mathematical Logic and Formal Languages]: Mathematical Logic--Logic Programming; H.2.0 [Information Systems]: Database Manage-

ment--General General Terms: Algorithms, Languages, Management, Theory Additional Key Words and Phrases: Cooperative answers, database semantics, disjunctive databases, semantic query optimization, stratified databases, update validation, well-founded approach About the Authors: J O H N GRANT is a professor of Computer and Information Sciences at Towson State University and a professor at the University of Maryland's Institute for Advanced Computer Studies (UMAICS). His current research interests include logic programming and knowledge bases. Author's Present Address: Department of Computer and Information Sciences, Towson State University, Towson, MD 21204; emaih [email protected] JACK MINKER is a professor of Computer Science in the Department of Computer Science and the Institute for Advanced Computer Studies at the University of Maryland. His current re-

COMMUNICATIONSOFTHE ACM/March 1992/Vo1.35,No.3

The ACM Member Services Department wants you to have a l l t h e FACTS. F A C T # t : If your m a i l i n g a d d r e s s is c h a n g i n g , contact us directly by telephone, fax, email or simply m a i l y o u r request to: A C M Member Services Department 1515 Broadway NY, N Y 10036 TELEPHONE: 212. 626. 0500~FAX." 212. 944.1318/EMAIL: A CMCOA @A CM VM. BI T N E T D o n ' t f o r g e t - - I n c l u d e y o u r A C M M e m b e r # on all correspondence! T h e C h a n g e of Address request you m a k e will cover A L L of y o u r subscription services. FACT # 2 " I f you have any questions c o n c e r n i n g y o u r A C M m e m b e r s h i p and/or s u b s c r i p t i o n services or w o u l d like further details c o n c e r n i n g the benefits of b e i n g an A C M M e m b e r , contact us at:

A C M Member Services Department 1M5 Broadway NY, N Y 10036 TELEPHONE." 212. 626. 0500~FAX." 212. 944.1318/EMAIL: [email protected] FACT # 3 : I f you would like to o r d e r S i n g l e C o p y items, i n c l u d i n g conference proceedings, individual j o u r n a l s and S I G newsletters and o t h e r special publications, contact:

A C M Order Department 1.800. 342. 6626 (Credit Card Orders) 1.301.528. 4261 (AK, M D and Outside US and~or Customer Service Inquiries and Orders) FAX: 301.528. 8596/EMAIL: A C M P U B S @ A C M V M . B I T N E T For p r e - p a i d and p u r c h a s e orders, mail y o u r o r d e r and p a y m e n t to:

A C M Order Department PO Box 64145 Baltimore, M D 21264 For SIC, G R A P H V i d e o R e v i e w call First Priority at 1.800.223.5503 or 708.250.0807; fax: 708.250.0038.

81

Logic Programming

shop on Logic and Databases was ..... database system, databases store positive data only ...... directly by telephone, fax, email or simply mail your request to:.

2MB Sizes 2 Downloads 199 Views

Recommend Documents

Programmable Logic Controllers: Programming ...
switches simultaneously that are separated by a distance such that both switches ..... This complicates learning to program PLC's in general since one language ...

Logic Programming with Graded Introspection
School of Computer Science and Engineering,. Southeast University ... (3) Tom has 1 or 2 full years experiences in Java programming. (4) Tom has 3 or 4 full ...

[PDF BOOK] Inductive Logic Programming
Online PDF Inductive Logic Programming: From Machine Learning to Software Engineering, Read PDF Inductive Logic Programming: From Machine Learning to Software Engineering, Full PDF Inductive .... The text includes a definition of the basic ILP proble