Ordered Logic Programming with Sets

26

Transcript of Ordered Logic Programming with Sets

Logic Programming With SetsGabriel M. KuperIBM T. J. Watson Research LaboratoryYorktown Heights, New York1 IntroductionIn recent years there has been a large amount of interest in extending therelational model of a database to capture more of the structure of the data,e.g., [HY82] [JS82] [KV84] [OY85] [RR83] [SP82] [SS77]. Most of this workhas focussed on two issues: How to model the physical structure of the data,and how to de�ne query languages on structures that are more general than at relations. One popular model has been non-�rst normal form (non 1NF),or nested, relations. These are relations in which the components of tuplesmay be sets of objects rather than simple, atomic, objects.Another important direction of database research has been in usinglogic programming languages, such as Prolog, as database query languages[BMSU86] [GM78] [HN84] [Kow79] [Nai83] [Rei78] [Ull85]. Such languagesprovide a very natural way to express queries on a relational database. Fur-thermore, by allowing recursion, we get a language that is more powerfulthan the relational algebra of Codd [AU79].There has been very little work, so far, on combining these two ap-proaches [BK86] [TZ86] [BNR*87] [SN87]. By this we mean both extendingthe data model to capture more of the semantics of the data, while at thesame time using a logic programming language as the query language. Inorder to do this, we need to have some way to deal with complex objectsin logic programs. Some types of data structures can be easily representedby Prolog structures. Others, in particular aggregation, i.e., forming sets ofobjects, cannot. Therefore, if we are to use a logic programming languageas a query language for nested relations, the language has to be extendedto handle sets of objects. The subject of this paper is how to extend logicprogramming to handle aggregation. We regard such an extension as an es-sential prerequisite to extending logic programming to non-1NF databases.1

Furthermore, the extension we propose may also be useful in other applica-tions of logic programming besides database query languages.To illustrate our approach, let us see how a programmer would normallydeal with a set of objects in Prolog. Usually a set would be representedas a list. Predicates involving sets would then be de�ned by iteration onthe elements of the list. For example, the membership predicate could bede�ned as follows. member(x; [xjL]) :{ :member(x; [yjL]) :{member(x; L)When a predicate involves more than one set, the rules can become quitecomplicated and unintuitive. For example, one way to de�ne a predicate thatholds when two sets are disjoint is as follows.disj(L; []) :{ :disj([]; L) :{ :disj([xjL1]; [yjL2]) :{ x 6= y;disj([xjL1]; L2); disj(L1; [yjL2])Even if we replaced these rules by a more e�cient de�nition, such anapproach would still be contrary to the general philosophy of logic program-ming. Whenever possible, the programmer should not have to deal with thecontrol structures in the program. In examples such as these, the logicalde�nitions of the predicates are fairly simple. Despite this, the programmerhas to specify a lot of details about the implementation, such as how toiterate over the sets.We propose an extension to logic programming called LPS (Logic Pro-gramming with Sets). The LPS language has two types of objects: Individ-ual objects, and sets whose elements are individual objects. We �rst consideronly one level of set nesting in order to concentrate on the key problems thatarise, in as simple a framework as possible. The rules in the LPS languageare fairly similar to the Horn clauses of logic programming. The main dif-ference between LPS rules and Horn clauses is that the right-hand side of anLPS rule may be preceded by restricted universal quanti�ers. This meansthat an LPS rule has the formA :{ (8x1 2 X1) � � �(8xn 2 Xn)(B1 ^ � � � ^ Bm)This is a fairly conservative extension of Horn clause logic, since wheneverthe setsX1, : : : ,Xn have known values, the body can be reduced to a normal2

Horn clause, i.e., the conjunction of the body (without the quanti�ers) overall the elements of the sets. We shall see that our extension of Horn clauselogic, unlike extensions that allow arbitrary quanti�cation on the right-handside [LT84] [BNR*87], preserves the semantics of Horn clause logic.Example 1: The disj predicate could be de�ned by the ruledisj(X; Y ) :{ (8x 2 X)(8y 2 Y )(x 6= y)(x 6= y could be de�ned as :(x = y); negation will be discussed later on.)Example 2: LPS contains the membership predicate as a primitive. Usingthis predicate, we can de�ne the subset relationship by the rulesubset(X; Y ) :{ (8x 2 X)(x 2 Y )Example 3: The predicate union(X; Y; Z) means that Z is the union ofX and Y . It is de�ned by the ruleunion(X; Y; Z) :{ subset(X;Z)^subset(Y; Z)^ (8z 2 Z)(z 2 X_z 2 Y )(Note that because this rule uses disjunction it is not really an LPS ruleas de�ned above. In Section 4 we show how to convert this a rule into anequivalent set of LPS rules.Example 4: Let R(x; Y ) be a non-�rst normal-form relation. The unnestoperation of [JS82] can be de�ned by the ruleS(x; y) :{ R(x; Y ) ^ y 2 YExample 5: Suppose that X is a set of numbers, and that we want tocompute their sum. Let disj-union(X; Y; Z) mean that Z is the disjointunion of X and Y , i.e.,disj-union(X; Y; Z) :{ union(X; Y; Z)^ disj(X; Y )Then sum(Z; k) can be de�ned by the recursive rulesum(Z; k) :{ disj-union(X; Y; Z)^ sum(X;m)^ sum(Y; n)^m+ n = kwith the base case sum(X; n) :{ X = fng3

Example 6: Let parts(x; Y ) be a non 1NF relation that means that objectx is built from the component parts in set Y . Let cost(x; n) be a relationthat gives the costs of the component parts. Then the cost of object x canbe computed by the rules.obj-cost(x; n) :{ parts(x; Y ) ^ sum-costs(Y; n)sum-costs(Z; n) :{ disj-union(X; Y; Z)^ sum-costs(X;m)^ sum-costs(Y; n)^m+ n = ksum-costs(X; n) :{ X = fpg ^ cost(p; n)In the next section we de�ne the LPS language formally. In Section 3we discuss the formal semantics of LPS, and show that the language hasmodel-theoretic and �xed point semantics similar to those of [vEK76]. InSection 4 we investigate the expressive power of the LPS language. Weshow that we can allow disjunction on the right-hand side of an LPS rulewithout increasing the power of the language, but, on the other hand, LPSis not powerful enough to construct the set of all objects that satisfy a givenpredicate. In Section 5 we show how to extend the LPS language to arbitrary�nite sets, and �nally, in Section 6 we compare our approach to other waysof adding sets to logic programming, such as that of [BNR*87].2 The LPS Language2.1 The Underlying LogicThe LPS language is based on a two-sorted logic. The two sorts, which bebe denoted by a and s, correspond to atomic objects and sets of atoms. Thelanguage contains function symbols, which, with one exception are from sorta to sort a1. This exception is for building sets, for which we have termssuch as fx1; x2; x3g, where the variables x1, x2 and x3 are of sort a. Theseterms are de�ned using special function symbols fn, where fn(x1; : : : ; xn)represents the set fx1; : : : ; xng.De�nition 1: An LPS language L consists of:1. Predicate symbols p�ii , i = 1, : : : , n1, where �i is a string of a's and s'sof length � 0 that speci�es the sort of the predicate. L also containsthree special \built-in" binary predicate symbols: =aa, =ss, and 2as.1In Example 8 we explain why uninterpreted function symbols with other sorts are notallowed. 4

2. Function symbols fmii , i = 1, : : : , n2. fmii is from sort ami to sort a.L also contains the special function symbols fn for all n � 0. fn isfrom sort an to sort s.3. Constants cai , i = 1, : : : , n3, all of sort a.4. Variables x�ii , i = 1, : : : , n4. x�ii is of sort �i.In practice, the sorts of the functions and predicates will often be clearfrom the context, and then we shall not mention the sort explicitly. Inparticular, we shall usually not distinguish between the two types of equality.We use the convention that lower case letters near the end of the alphabet(x, y, z) represent variables of sort a, upper case letters (X , Y , Z) representvariables of sort s, and we often assume that predicate symbols are writtenwith all the variables of sort a preceding those of sort s. Normally, when werefer to predicate or function symbols in a language, we include the special\built-in" ones, unless we explicitly state otherwise.De�nition 2: A term t over an LPS language L is one of:1. A constant ci. The sort of such a term is a.2. A variable xi. The sort of such a term is the sort of the variable.3. f(t1; : : : ; tk), where f is a function symbol, and each ti is of sort a.The sort of the term is s if f is one of the fn's, and is a otherwise.We write ft1; : : : ; tng for the term fn(t1; : : : ; tn), and ; for the term f0.We then de�ne atomic formulas and well-formed formulas in the usual way.An LPS model is a �rst-order model of the logic, that satis�es several ad-ditional constraints. These constraints require that the model interpret sorts as sets of elements of sort a, and that it interprets the special function andpredicate symbols correctly. In other words, the equality and membershippredicates are interpreted as identity and membership over the correspond-ing domains, and the function symbol fn is interpreted as a set constructor.De�nition 3: Let L be an LPS language. An LPS model M of an LPSlanguage L consists of1. Two sets D and D�. D is the interpretation of sort a, and D� is theinterpretation of sort s. Because sort s corresponds to sets of objectsof sort a, we require that D� be a subset of P�n(D).5

2. Interpretations of the predicate symbols.(a) If p�ii is a non-special predicate symbol of sort �i = kz }| {a � � �a lz }| {s � � �s,then M(p�ii ) is a subset ofkz }| {D � � � � �D� lz }| {D� � � � � �D�(b) M(=aa) is the equality relation on D �D(c) M(=ss) is the equality relation on D� �D�(d) M(2) is the membership relationship between elements of D andD�3. Interpretations of the function symbols.(a) If fni is a n-ary function symbol other than fn, then M(fi) is afunction from nz }| {D � � � � �D to D(b) M(fn) is de�ned as M(fn)(d1; : : : ; dn) = fd1; : : : ; dng, for all el-ements d1, : : : , dn of D4. An interpretation M(ci) 2 D of each constant.Interpretations of terms and formulas are de�ned as usual. Note that theinterpretations of quanti�ers (8x) and (8X) are overD andD�, respectively.Free variables, closed formulas, validity, the symbol :{ , etc., are also de�nedas usual.2.2 LPS ProgramsDe�nition 4: A restricted universal quanti�er is a quanti�er of the form(8x 2 X).The formula (8x 2 X)� is an abbreviation for (8x)(x 2 X ) �). Notethat (8x 2 X)� is true whenever X is the empty set.De�nition 5: An LPS clause over L is a formula of the formA :{ (8x1 2 X1) � � �(8xn 2 Xn)(B1 ^ � � � ^ Bm)6

where each Bi is an atomic formula, each xi is a variable of sort a, each Xiis a variable of sort s, and A is a non-special atomic formula2. The casen = 0 is allowed, in which the clause is an ordinary Horn clause.This clause in an abbreviation for the closure of the formula(8x1 2 X1) � � �(8xn 2 Xn)(B1 ^ � � � ^Bm)) AA has to be a non-special atomic formula, since otherwise we could write aclause that rede�nes equality or membership, and this could cause problemslater on. Goals, empty clauses, substitutions, etc., are de�ned in the usualway.De�nition 6: An LPS Program P is a �nite set of LPS clauses.3 Semantics3.1 Minimal Model SemanticsWe �rst extend the notion of a Herbrand model to the LPS language.De�nition 7: The Herbrand universe has two components:1. UaL, consisting of(a) All constants ci.(b) All terms fiu1 � � �un where u1, : : : , un are in UaL.2. UsL = P�n(UaL), i.e., all �nite sets of elements of UaL.A signi�cant di�erence between our de�nition and the standard de�-nition of a Herbrand model is that terms of the form fn(x1; : : : :xn) areinterpreted as �nite setsof elements of UaL, rather than by the term that wewould get by concatenating the function symbol fn to its arguments. Wedo this to ensure that a Herbrand model is also an LPS model.De�nition 8: The Herbrand base is the set of all atomic formulas of theform1. p�ii (u1; : : : ; uk; u�1; : : : ; u�l ), where �i = kz }| {a � � �a lz }| {s � � �s.2i.e., one whose predicate symbol is not an equality or membership predicate.7

2. u1 =aa u23. u�1 =ss u�24. u1 2 u�1where u1, : : : , uk are in UaL and u�1, : : : , u�l are in UsL.De�nition 9: A Herbrand model of L is an LPS model of L that satis�es1. The domains of the sorts a and s are UaL and UsL, respectively.2. The interpretation of a constant is the constant itself.3. The interpretation of a non-special function symbol is the functionthat consists of concatenating the function symbol to its arguments.Lemma 1: LetM1 andM2 be Herbrand models of L, and let � be a groundsubstitution for the formula (x 2 X). ThenM1 j= (x 2 X)�,M2 j= (x 2 X)�Proof: The substitution � replaces x by some ground term u in UaL, andreplaces X by some U in UsL. By the de�nition of UsL, U is a �nite setfu1; : : : ; ung of ground terms of sort a. But then, for any Herbrand modelM (in fact, any LPS model), M satis�es (x 2 X)� i� u is equal to one ofthe terms u1, : : : , un.A key property of Horn clauses is that any set of Horn clauses that has amodel must have a Herbrand model. This result also holds for LPS clauses.Lemma 2: Let P be a set of LPS clauses, and let M be an LPS model ofP . Then P has a Herbrand model M�.Proof: In order to de�ne M�, we have to de�ne the interpretations of thenon-special predicate symbols. M�(p�ii ) is de�ned as the set of all tuples(u1; : : : ; uk; u�1; : : : ; u�l ) such that each ui is in UaL, each u�j is in UsL, and suchthat �M(u1); : : : ;M(uk);M(u�1); : : : ;M(u�l )� 2M(p�ii )Whenever A� is a ground instance of an atomic formula, we haveM� j= A� , M j= A�8

We claim that any LPS clause that is satis�ed byM is also satis�ed by M�.To show this, assume that the clauseA :{ (8x1 2 X1) � � �(8xn 2 Xn)(B1 ^ � � � ^ Bm)is not satis�ed by M�. Then there is a ground substitution � for whichM� 6j= A� butM� j= �(8x1 2 X1) � � �(8xn 2 Xn)(B1 ^ � � � ^ Bm)��Since M 6j= A�, we will complete the proof by showing thatM j= �(8x1 2 X1) � � �(8xn 2 Xn)(B1 ^ � � � ^Bm)��Let d1, : : : , dn be in the interpretation of sort a in M with the propertythat M j= (xi 2 Xi)�fx1=d1; : : : ; xn=dng for all i from 1 to n. We have toshow that M j= (B1 ^ � � � ^Bm)�fx1=d1; : : : ; xn=dngLet u�i be the term that � substitutes for the variable Xi. Since u�i isground, it must be of the form fu1i ; : : : ; ukii g, and thereforeM(u�i ) is equal tofM(u1i ); : : : ;M(ukii )g. But then M j= fxi 2 Xi)�(x1=d1; : : : ; xn=dng holdsi� di is equal to M(ujii ) for some ji. This implies thatM� j= fxi 2 Xi)�(x1=uj11 ; : : : ; xn=ujnn gholds for all i, which in turn impliesM� j= (B1 ^ � � � ^ Bm)�fx1=uj11 ; : : : ; xn=ujnn gTherefore, since di is equal to M(ujii ),M j= (B1 ^ � � � ^Bm)�fx1=d1; : : : ; xn=dngExample 7: It is well known that �rst-order formulas may have �rst-ordermodels but not have Herbrand models. An example of this is the formulap(a)^ (9x):p(x) in a language with the single individual constant a. Since(9x):p(x) is the same as the Horn clause :{ (8x)p(x), we might think thatthe LPS clauses p(a) and :{ (8x 2 X)p(x) would have LPS models butnot Herbrand models, contradicting the Lemma. The reason this does nothappen is that :{ (8x 2 X)p(x) has no LPS models. :{ (8x 2 X)p(x) isequivalent to the closed formula (8X)(9x 2 X):p(x), and (9x 2 X):p(x)is always false when X = ;. 9

Example 8: The requirement that the non-special function symbols alwaysbe of sort a is essential for If we were to allow function symbols from sort ato sort s, and if f were such a function symbol, then the clausesf :{ A(f(a)); A(X) :{ (8x 2 X)B(x)gwould has an LPS model, since we could interpret f(a) by the set fag, andmake B(a) false. However, these clauses do not have a Herbrand model,since x 2 f(a) is false in every Herbrand, forcing A(f(a)) to be true in sucha model.De�nition 10: Let P be a set of LPS clauses. The least Herbrand modelof P is de�ned as the intersection MP of all the Herbrand models of P .The next Theorem can be proved in the same way as for Horn clauses.Lemma 2 is the key result that makes use of the fact that our clauses are ofa more general form.Theorem 3:1. MP is a Herbrand model of P .2. MP consists of those formulas in the Herbrand base that are logicalconsequences of P .3.2 Fixpoint SemanticsLemma 4: Every ground instance of an LPS clause is logically equivalentto a ground instance of some Horn clause.Proof: Let � be a ground substitution for the LPS clauseA :{ (8x1 2 X1) � � �(8xn 2 Xn)(B1 ^ � � � ^ Bm)The ground term that � substitutes for Xi must be of the form fu1i ; : : : ; ujii g.Since xi 2 fu1i ; : : : ; ujii g is logically equivalent to xi = u1i _ � � � _ xi = ujii , itfollows that the ground instance of this LPS clause is logically equivalent tothe Horn clauseA :{ ^(k1;:::;kn)1�ki�ji (B1 ^ � � � ^Bm)�fx1=uk11 ; : : : ; xn=uknn g10

De�nition 11: Let P be an LPS program. We de�ne a mapping TP fromHerbrand interpretations to Herbrand interpretations as follows. Tp(M) isthe set of all formulas A in the Herbrand base for which there is a groundinstance A :{ B1 ^ � � � ^ Bmof a clause in P , with the property that all the Bi's are in M.Using Lemma 4, we can modify the standard proof to show.Theorem 5: Let P be an LPS program. ThenMP = lfp(TP ) = TP " !.The standard procedural semantics can also be extended to LPS. How-ever, to do so, we have to use arbitrary uni�ers, rather than the most speci�cone. For this reason, it is no longer a practical decision procedure.4 The Expressive Power of the Language4.1 Using Disjunction in LPS RulesSome of the examples in the introduction to this paper use clause that donot �t the formal de�nition of LPS. For example, we de�ned union by theclauseunion(X; Y; Z) :{ (8x 2 X)(x 2 Z) ^ (8y 2 Y )(y 2 Z)^(8z 2 Z)(z 2 X _ z 2 Y )This is is not an LPS rule since the clause contains disjunction, and universalquanti�ers were allowed only in front of the entire right-hand side.The second point might not seem to make a di�erence|after all, theformula (8x)(A^B) is equivalent to A^ (8x)B whenever x does not appearfree in A. With restricted quanti�ers, however, (8x 2 X)(A ^ B) is not, ingeneral, equivalent to A ^ (8x 2 X)B, since when X is the empty set, the�rst formula is always true, while the second is equivalent to A.Ignoring this problem for a moment, and considering only disjunction,we might try to handle it in a similar way to Horn clauses. The Horn clauseA :{ B _ C is equivalent to the pair of clauses A :{ B and A :{ C. If wetry the same thing with the union predicate, we get (using X � Y as anabbreviation for (8x 2 X)(x 2 Y ))union(X; Y; Z) :{ X � Z ^ Y � Z ^ (8z 2 Z)(z 2 X)union(X; Y; Z) :{ X � Z ^ Y � Z ^ (8z 2 Z)(z 2 Y )11

But this is equivalent to(X = Z ^ Y � Z) _ (Y = Z ^X � Z)which is not what we wanted. However, if we allow the use of additional,auxiliary, predicates, we can then express the union predicate in LPS bythe clauses.union(X; Y; Z) :{ t3(X;Z)^ t3(Y; Z)^ t1(X; Y; Z)t1(X; Y; Z) :{ (8z 2 Z)t2(X; Y; z)t2(X; Y; z) :{ z 2 Xt2(X; Y; z) :{ z 2 Yt3(X; Y ) :{ (8x 2 X)(x 2 Y )We now show that this construction generalizes to any positive formula.De�nition 12: Positive formulas are de�ned by induction as follows.1. An atomic formula.2. � _ and � ^ , where � and are positive formulas.3. (8x 2 X)� and (9x 2 X)� where � is a positive formula, and x andX are variables of sorts a and s, respectively.Theorem 6: Let P be a set of clauses of the form Ai :{ Bi, where each Aiis an atomic formula and each Bi is a positive formula over L. Then there isan extension L� of L and a program P � over L� such that for every formula� over L, P j= �, P � j= �Proof: We assume that P consists of the single clause A :{ B, as theconstruction can easily be extended to programs with more than one clause.We de�ne the program P � = f(A :{ B) by induction on the size of B asfollows.1. B is an atomic formula. Then f(A :{ B) consists of the clause A :{B.2. B is C1 ^ C2. Let x1, : : : , xn and y1, : : : , ym be the free variablesin C1 and C2, respectively. Let N1 (n-ary) and N2 (m-ary) be newpredicates.f(A :{ B) = f(N1(x1; : : : ; xn) :{ C1)[ f(N2(y1; : : : ; ym) :{ C2)[fA :{ N1(x1; : : : ; xn) ^N2(y1; : : : ; ym)g12

3. B is C1 _ C2. Let x1, : : : , xn and y1, : : : , ym be the free variablesin C1 and C2, respectively. Let N1 (n-ary) and N2 (m-ary) be newpredicates.f(A :{ B) = f(N1(x1; : : : ; xn) :{ C1)[ f(N2(y1; : : : ; ym) :{ C2)[fA :{ N1(x1; : : : ; xn)g [ fA :{ N2(y1; : : : ; ym)g4. B is (9x 2 X)C. Let x1, : : : , xn, x be the free variables in C. Let Nbe a new (n+ 1)-ary predicate.f(A :{ B) = f(N(x1; : : : ; xn) :{ C)[fA :{ N(x1; : : : ; xn; x)^ x 2 Xg5. B is (8x 2 X)C. Let x1, : : : , xn, x be the free variables in C. Let Nbe a new (n+ 1)-ary predicate.f(A :{ B) = f(N(x1; : : : ; xn) :{ C)[fA :{ (8x 2 X)N(x1; : : : ; xn; x)gObviously, P � is an LPS program. By induction on the size of B, we showthat A :{ B is equivalent to the program f(A :{ B). The base case ofthe induction is when B is atomic, in which case f(A :{ B) consists of thesingle clause A :{ B. For the general case, we show how to deal with thecase A :{ C1 _ C2. The other cases are similar.The de�nition of f(A :{ C1 _ C2) isf(N1(x1; : : : ; xn) :{ C1) [ f(N2(y1; : : : ; ym) :{ C2)[fA :{ N1(x1; : : : ; xn)g [ fA :{ N2(y1; : : : ; ym)gBy the inductive hypothesis, the program f(N1(x1; : : : ; xn) :{ C1) is equiv-alent to the rule N1(x1; : : : ; xn) :{ C1, and similarly for N2 and C2. Sinceall the new predicates that were introduced by our construction are distinct,it follows that any formula � over L does not contain any of the Ni's, andhence that � is a consequence offA :{ N1; A :{ N2; N1 :{ C1; N2 :{ C2gif and only if � is a consequence of A :{ C1 _ C2.13

Example 9: The de�nition of union in LPS that we gave above is some-what simpler that the de�nition that we get from the general constructionin the proof. The proof gives us the programunion(X; Y; Z) :{ N1(X;Z)^N2(X; Y; Z)N1(X;Z) :{ (8x 2 X)N3(x; Z)N2(X; Y; Z) :{ N4(Y; Z)^N5(X; Y; Z)N3(x; Z) :{ x 2 ZN4(Y; Z) :{ (8y 2 Y )N6(y; Z)N5(X; Y; Z) :{ (8z 2 Z)N7(X; Y; z)N6(y; Z) :{ y 2 ZN7(X; Y; z) :{ N8(z; Z)N7(X; Y; z) :{ N9(z; Y )N8(z; Z) :{ z 2 ZN9(z; Y ) :{ z 2 YIt turns out that auxiliary predicates are essential to the proof of thisTheorem, since the union predicate cannot be de�ned without them.Theorem 7: Let L be a language whose only non-special predicate is aternary predicate p. There is no LPS program P with the property that forall sets A, B and C in UsL,MP j= p(A;B;C), A [B = CProof: See Appendix A.4.2 Construction of SetsSuppose we are given a predicate A(x) and we want to construct the setS = fx j A(x)g, i.e., the set of all those x's that satisfy the predicate A(x).Since LPS is a nonprocedural, language, we have no mechanism to actuallyconstruct such a set, but could instead ask whether we can de�ne a predicateB(X) that is satis�ed exactly when X = S. If we try to de�ne B(X) by theclause B(X) :{ (8x 2 X)A(x)B(S) would indeed hold, but B(X) would also hold for all subsets X of S.We now show that such a predicate B(X) cannot be de�ned in LPS. The14

statement of the proof is rather complicated, since we have to be careful notto allow the de�nition of B(X) to rede�ne A(x), or vice versa. The proofshows that such a predicate cannot be de�ned in any language with minimalmodel semantics like LPS.Theorem 8: Let A(x) be a unary predicate and let P be a program thatdoes not contain the predicate B(X). There does not exist an LPS programP � such that MP[P � j= B(U) if and only if U is equal to the set of allelements u in the Herbrand universe for which MP j= A(u).Proof: Assume that such a program P � exists. Let c1 and c2 be twoconstants in L, let P1 be the program fA(c1)g, and let P2 be the programfA(c1); A(c2)g. Then MP1 j= A(x) i� x = c1, and therefore MP1[P � shouldsatisfy B(fc1g) but should not satisfy B(X) for any other set X . On theother hand, MP2 j= A(x) whenever x = c1 or c2, and therefore MP2[P �should satisfy B(fc1; c2g) but should not satisfy B(X) for any other set X .In particular MP2[P � 6j= B(fc1g). Since every model of P2 is a model of P1,since MP2[P � is a Herbrand model of P2 and of P �, it follows that MP2[P �is a Herbrand model of P1 [ P �. But then B(fc1g) is not satis�ed in atleast one Herbrand model of P1 [ P �, and hence is not satis�ed in the leastHerbrand model MP1[P � , a contradiction.In order to construct the set fx j A(x)g we have to know, for each x,whether or not A(x) is true. This is closely related to making the closedworld assumption, since we need negative information (which x's do notsatisfy A(x)) as well as positive information (which x's do). Since LPS issimilar to Horn clause logic it only provides positive information, and notthe negative information needed for set construction.The relation between set construction and negation is very close. Wecan negation to LPS in a straightforward way, losing of course the minimalmodel semantics. Extending the notion of a strati�ed program [ABW86] toLPS is also straightforward. We could then de�ne the predicate B(X) bythe clauses C(X) :{ X � Y ^ (8y 2 Y )A(y)B(X) :{ (8x 2 X)A(x)^ :C(X)where X � Y is de�ned byX � Y :{ (8x 2 X)(x 2 Y ) ^ z 2 Y ^ :z 2 XInformally, C(X) says that there is some set Y that is larger than X , allof whose elements satisfy A(x). B(X) says that all of X 's elements satisfy15

A, but that there is no larger set with this property, which is equivalent tosaying that X = fx j A(x)g.5 Extending LPS to Arbitrary SetsWe outline how LPS can be extended to arbitrary �nite sets, i.e., how toallow more than one level of set nesting. There are two ways we could dothis. One way is to use a fully typed language, i.e., to have a type n for allsets of nesting depth n. The other way is to use an untyped language. Wechoose the second approach, primarily to facilitate comparing our results tothose of LDL in the next section. In an untyped language we must still becareful to avoid letting the values of function symbols contain any elements,since then the minimal model semantics would no longer hold. We thereforeuse a model of set theory that has atoms as well as sets.We call the new language ELPS (Extended LPS). An ELPS languageL is an untyped �rst-order logic with equality, with special function sym-bols fn and 2. An ELPS model M is a model of L over a domain D thatinterprets fn and 2 as the corresponding set-theoretic constructors and re-lations. We require that if f is a function symbol in L, the range of M(f)must consist entirely of atoms, i.e., objects in D that themselves have noelements. Similarly, constants must also be interpreted as atoms.De�nition 13: The Herbrand universe UL for L is the smallest set thatcontains1. All the constants ci in L,2. All terms of the form fiu1 � � �un where fi is a function symbol in L,and u1, : : : , un are in UL, and3. All �nite subsets of UL.The Herbrand base and Herbrand models are de�ned in the same wayas before. Our previous results then hold in ELPS.Theorem 9: Let P be an ELPS program.1. There exists a minimal model MP that is a Herbrand model of P ,and consists of those formulas in the Herbrand base that are logicalconsequences of P . 16

2. MP = lfp(TP ) = TP " !Theorems 6 and 8 can also be generalized to ELPS.6 Comparison with Other Ways of Adding Setsto Logic ProgrammingWe compare our approach to other ways of adding sets to logic programs,speci�cally that of LDL [BNR*87]. The LDL language has several typeof clauses, and built-in predicates, but for our purposes an LDL programwill consist of Horn clauses and grouping rules (de�ned below). Other LDLprimitives, such as scons, will be treated separately. We assume that allthe languages contain the special predicates of ELPS.De�nition 14: An LDL grouping clause [BNR*87] over L is a formula ofthe form A(x1; : : : ; xn; hxi) :{ B1 ^ � � � ^BmThe meaning of this clause is that the left-hand side is satis�ed by atuple (d1; : : : ; dn; d) i� d is equal to the set of those values of x for whichthe body of the clause holds.De�nition 15: Let L be a �rst-order logic1. L+ union consists of L with the additional predicate union(x; y; z).Every model Munion of L + union is required to satisfyMunion j= union(d1; d2; d3), d3 = d1 [ d22. L+ scons consists of L with the additional predicate scons(x; y; z).Every model Mscons of L + scons is required to satisfyMscons j= scons(d1; d2; d3), d3 = d1 [ fd2gWe always assume that the union and scons predicates are not pred-icates of L. Our scons predicate is essentially the same as the scons op-eration of [BNR*87], but for technical reasons we prefer to de�ne it as apredicate rather than as a function symbol. We further require that clausesnever have the predicates union or scons in the head.17

There are obvious mappings between models of any one of these lan-guages to models of any other, and we can use these mappings to de�neequivalences between theories over L and over L + union or L + scons.We shall assume that each language also has some auxiliary predicates notshared by the other language. Equivalence will be relative only to the pred-icates that the languages have in common.De�nition 16: An LDL program over L is a �nite set of Horn and LDLgrouping clauses over L.6.1 Languages Without NegationWe �rst look at languages without negation, introduced either directly orindirectly (e.g., via a grouping operation). All of these languages have min-imal model and least �xpoint semantics similar to ELPS.Theorem 10: The following are equivalent1. ELPS programs over L2. Horn programs over L + union3. Horn programs over L + sconsProof:1. Any Horn clause over L + union can be converted into a set of ELPSclauses over L by replacing all occurrences of the union predicate bya new predicate p(x; y; z) that does not occur in the original program,and adding the clausep(x; y; z) :{ (8w 2 z)(w 2 x _ w 2 y)^(8w 2 x)(w 2 z)^ (8w 2 y)(w 2 z)Note that we have to use Theorem 6 to eliminate the disjunction, andthis construction introduces additional auxiliary predicates.2. A Horn clause over L + scons can be converted into a set of ELPSclauses by replacing scons by a new predicate r(x; y; z), and addingthe clauser(x; y; z) :{ (8w 2 x)(w 2 z) ^ y 2 z ^ (8w 2 z)(w 2 x _ w = y)18

3. Let A :{ (8x1 2 y1) � � �(8xn 2 yn)(B1 ^ � � � ^Bm)be an ELPS clause over L. This rule is equivalent to the ruleA :{ union(y01; y001 ; y1) ^ Afy1=y01g ^Afy1=y001gwith the \base case"A :{ x1 = fy1g ^ (8x2 2 y2) � � �(8xn 2 yn)(B1 ^ � � � ^Bm)Repeating this construction n times converts the original rule into aset of Horn clauses over L+ union.4. The same technique converts this clause into a set of Horn clauses overL+ scons, i.e.,A :{ scons(y01; x1; y1) ^Afy1=y01g ^ (8x2 2 y2) � � �(8xn 2 yn)(B1 ^ � � � ^Bm)with the base caseA :{ scons(;; x1; y1) ^ (8x2 2 y2) � � �(8xn 2 yn)(B1 ^ � � � ^Bm)6.2 Languages with NegationWe �rst look at unstrati�ed negation. Since such programs do not haveunique minimal, or even preferred, models, equivalence will be with respectto the class of all the models of the program.Theorem 11: The following are equivalent1. ELPS programs with unstrati�ed negation over L2. Horn programs with unstrati�ed negation over L+ union3. Horn programs with unstrati�ed negation over L+ scons4. LDL programs over LProof: The equivalences 1{3 follow easily from the proof of Theorem 10.We can convert a Horn program over L + union into an LDL program overL by replacing the union predicate by a new predicate q(x; y; z) de�ned by:19

q(x; y; hzi) :{ p(x; y; z)p(x; y; z) :{ z 2 xp(x; y; z) :{ z 2 yThe resulting program may still use negation, but we can use the construc-tion in [BNR*87] to express the negation in terms of grouping.3Finally, we can convert an LDL program into an ELPS program byconverting the LDL grouping clauseA(x1; : : : ; xn; hxi) :{ B1 ^ � � � ^Bminto the ELPS clauses with negationq(x; y) :{ (8z 2 x)(z 2 y) ^ w 2 y ^ :w 2 xp(x1; : : : ; xn; y) :{ q(y; z)^ (8x 2 z)(B1 ^ � � � ^Bm)and A(x1; : : : ; xn; y) :{ (8x 2 y)(B1 ^ � � � ^ Bm)^ :p(x1; : : : ; xn; y)This is essentially the same technique used to construct sets at the end ofSection 4.2. q(x; y) holds when x is a proper subset of y, p(x1; : : : ; xn; y)holds when there is some proper superset of y all of whose elements satisfythe right-hand side of the original rule. Finally the last rule says that all ofy's elements satisfy the right-hand side of the original rule, and that thereis no larger set with the same property.Most of these equivalences also hold for strati�ed programs, sincethecorresponding proofs map strati�ed programs into strati�ed ones.Theorem 12: The following are equivalent1. Strati�ed ELPS programs with negation over L2. Strati�ed Horn programs with negation over L+ union3. Strati�ed Horn programs with negation over L+ scons3We could have used instead the proof of [BNR*87] to convert an ELPS programs intoan LDL program. However, the proof we have given is much simpler, and does not dependon the existence of suitable function symbols.20

Furthermore, all these languages are at least as powerful as strati�ed LDLprograms over L.The question whether every strati�ed ELPS program is equivalent tosome strati�ed LDL program remains open.A Proof of Theorem 7Theorem 7: Let L be a language whose only non-special predicate is aternary predicate p. There is no LPS program P with the property that forall sets A, B and C in UsL,MP j= p(A;B;C), A [B = CProof: Assume that such a program P exists. We �rst show that wheneverthe head of a rule in P is of the form p(t1; t2; Z), where t1 and t2 are termsof sort s that are di�erent from the variable Z, we may assume that the rulecontains no quanti�ers in the body.1. If the body contains a quanti�er of the from (8w 2 W ), and thevariable does not appear in the head, then the body is always true forW = ;, and therefore the head is always true. This clause is thereforeequivalent to the quanti�er-free clauseunion(t1; t2; Z):In the remaining cases, we assume that the sets over which quanti�ersin the body range, actually appear in the head.2. t1 and t2 are of the form fx1; : : : ; xng and fy1; : : : ; ymg. We can thenreplace the clause byp(fx1; : : : ; xng; fy1; : : : ; ymg; fx1; : : : ; xn; y1; : : : ; ymg):Here we use the fact that P is assumed to de�ne a union predicate.Therefore, if our clause implies anything not covered by the fact above,our assumption is false. If, on the other hand, it does not imply allthese facts, some other clause in P must imply them, and adding themhere as well does no harm. 21

3. t1 = fx1; : : : ; xng and t2 = Y (or t1 = X and t2 = fy1; : : : ; ymg). If aquanti�er in the body if the clause ranges over the elements of Z, thenp(fx1; : : : ; xng; Y; ;)must hold for all Y , a contradiction. If a quanti�erin the body ranges over the elements of Y then p(fx1; : : : ; xng; ;; Z)must for all Z, once more a contradiction.4. t1 = X and t2 = Y . If a quanti�er in the body ranges over the elementsof Z, then p(X; Y; ;) must hold for all X and Y , a contradiction. Ifa quanti�er in the body ranges over the elements of X (or Y ), thenp(;; Y; Z) must for all Y (or union(X; ;; Z) for all X) and Z, oncemore a contradiction.5. t1 = t2 = X . If a quanti�er in the body ranges over the elements of Z,then p(X;X; ;) must hold for all X . If a quanti�er in the body rangesover the elements of X , then p(;; ;; Z) must hold for all Z, in bothcases a contradiction.Now, let A, B and C be three sets in UsL that satisfy1. A [B = C, A 6= C and B 6= C2. C has more than 2N elements, where N is the largest number forwhich the function symbol fN is used in the program P .3. There exists a derivation of p(A;B;C) from P , and there exists no setsA0, B0 and C 0 satisfying 1 and 2, such that p(A0; B0; C 0) has a shorterderivation from P .Let � be an element of UaL that is not a member of C. We shall show thatMP j= p(A;B;C [ f�g). Since A [ B 6= C [ f�g, this will contradict thede�nition of P and complete the proof.The last step of the derivation of union(A;B;C) has to use a groundinstance of the rule p(t1; t2; Z) :{ B1 ^ � � � ^Bkto derive p(A;B;C), after having already derived the ground instances ofB1, : : : , Bk . The last step must use such a rule for the following reason.Since C has more than N elements, the third argument of p must be avariable. Since A 6= C and B 6= C, neither t1 nor t2 can be equal to thevariable Z. As shown above, we may therefore assume that the rule has noquanti�ers in the body. 22

Let the free variables in this rule bew1; : : : ; wn;W1; : : : ;WmThe last step of the derivation of p(A;B;C) from P uses this rule to derivep(A;B;C), having already derived (B1 ^ � � � ^Bk)�, where the substitution� is of the form� = fw1=u1; : : : ; wn=un;W1=U1; : : : ;Wm=UmgFor any set U in UsL, let U� be U [ f�g whenever C � U , and let U� beU otherwise. We claim that (B1 ^ � � � ^ Bk)�� can also be derived from P ,where �� = fu1=u1; : : : ; wn=un;W1=U�1 ; : : : ;Wm=U�mgThis implies that p(A�; B�; C�) can be derived from P . Since A and B areboth proper subsets of C, A� = A, B� = B, and C� = C [ f�g, completingthe proof.We therefore have to show that each Bi�� can be derived from P . Wedo this by enumeration of the possible forms of Bi.1. wi 2 Wj . (wi 2 Wj)� is equal to ui 2 Uj , Uj is always a subset of U�j ,and therefore ui 2 U�j , i.e., (wi 2 Wj)��.2. wi = wj . In this case (wi = wj)� is identical to (wi = wj)��.3. Wi = Wj . Since Ui = Uj always implies U�i = U�j , (Wi =Wj)� implies(Wi = Wj)��.4. p(t1; t2; t3). There are six cases to consider.(a) t1, t2 and t3 are the variables Wi, Wj and Wk. Since the deriva-tion of p(Ui; Uj ; Uk) is part of the derivation of p(A;B;C), it mustbe shorter. But then, Ui, Uj and Uk cannot satisfy both condi-tions 1 and 2 in the de�nition of A, B and C. Since P satis�esp(Ui; Uj ; Uk), Ui [ Uj must be equal to Uk, and condition 1 canbe violated only if Ui = Uk (or if Uj = Uk). But Ui = Uk impliesU�i = U�k , and since Ui [ Uj = Uk , we have U�1 [ U�2 = U�3 , whichimplies that P must imply p(U�1 ; U�2 ; U�3 ). If condition 2 is vio-lated, then Uk must have � 2N elements. Since C has more that2N elements, C cannot be a subset of Uk, and therefore U�i = Ui,U�j = Uj and U�k = Uk . 23

(b) t1 is the term fwi1; : : : ; wiag, t2 is the variable Wj and t3 is thevariable Wk. Then p(fui1 ; : : : ; uiag; Uj; Uk) must have a shorterderivation than p(A;B;C). As before, one of conditions 1 and 2must be violated. If fui1 ; : : : ; uiag = Uk, then C is too large to bea subset of Uk, and therefore Uk = U�k which implies the result.If Uj = Uk, or if Uk has � 2N elements, the same argument as incase (a) applies.(c) t3 is the term fwk1 ; : : : ; wkcg, t1 is the variable Wi and tj is thethe variable Wj . In this case the sets fuk1 ; : : : ; ukcg and hence Uiand Uj have � N elements and therefore U�i = Ui and U�j = Uj .(d) t1 is the term fwi1; : : : ; wiag, t2 is the term fwj1 ; : : : ; wjbg, andt3 is the variable Wk. In this case Uk has � 2N elements, andtherefore U�k = Uk.(e) t1 is the term fwi1 ; : : : ; wiag, t3 is the term fwk1 ; : : : ; wkcg, andt2 is the variable Uj . In this case Uj has � N elements, andtherefore U�j = Uj .(f) t1 is the term fwi1 ; : : : ; wiag, t2 the term fwj1 ; : : : ; wjbg, t3 is theterm fwk1 ; : : : ; wkcg. In this case there are no set variables toreplace.References[ABW86] Krzysztof R. Apt, Howard Blair, and Adrian Walker. Towardsa Theory of Declarative Logic. Technical Report RJ11681, IBM,Watson Research Center, 1986.[AU79] A. V. Aho and J. D. Ullman. Universality of data retrieval lan-guages. In Conference Record of the Sixth Annual ACM Sympo-sium on Principles of Programming Languages, pages 110{120,1979.[BK86] F. Bancilhon and S. Khosha�an. A calculus for complex ob-jects. In Proc. Fifth Annual ACM Symposium on Principles ofDatabase Systems, pages 53{59, ACM, Cambridge, Mass., 1986.[BMSU86] F. Bancilhon, D. Maier, Y. Sagiv, and J. D. Ullman. Magicsets and other strange ways to implement logic programs. InProc. Fifth Annual ACM Symposium on Principles of DatabaseSystems, pages 1{15, ACM, Cambridge, Mass., 1986.24

[BNR*87] Catriel Beeri, Shamim Naqvi, Raghu Ramakrishnan, OdedShmueli, and Shalom Tsur. Sets and negation in a logic databaselanguage (LDL1). In Proc. Sixth Annual ACM Symposium onPrinciples of Database Systems, pages 21{37, ACM, 1987.[GM78] H. Gallaire and J. Minker. Logic and Databases. Plenum Press,NY, 1978.[HN84] L. J. Henschen and S. A. Naqvi. On compiling queries in re-cursive �rst-order databases. Journal of the ACM, 31(1):47{85,1984.[HY82] R. Hull and C. K. Yap. The format model: A theory of databaseorganization. In Proc. First Annual ACM Symposium on Prin-ciples of Database Systems, pages 205{211, ACM, Los Angeles,CA, 1982.[JS82] G. Jaeschke and H.-J. Schek. Remarks on the algebra of non �rstnormal form relations. In Proc. First Annual ACM Symposiumon Principles of Database Systems, pages 124{138, ACM, LosAngeles, CA, 1982.[Kow79] R. Kowalski. Logic for Problem Solving. North Holland, Ams-terdam, 1979.[Kup87a] G. M. Kuper. An Extension of LPS to Arbitrary Sets. TechnicalReport to appear, IBM, Watson Research Center, 1987.[Kup87b] G. M. Kuper. Logic programming with sets. In Proc. Sixth An-nual ACM Symposium on Principles of Database Systems, SanDiego, CA, 1987.[Kup87c] G. M. Kuper. LPS: A Logic Programming Language for NestedRelations. Technical Report RC 12624, IBM, Watson ResearchCenter, 1987.[KV84] G. M. Kuper and M. Y. Vardi. A new approach to databaselogic. In Proc. Third Annual ACM Symposium on Principles ofDatabase Systems, pages 86{96, ACM, Waterloo, Ontario, 1984.[LT84] J. W. Lloyd and R. W. Topor. Making PROLOG more expres-sive. Journal of Logic Programming, 1(3):225{240, October 1984.25

[Nai83] L. Naish. Automatic Generation of Control for Logic Programs.Technical Report TR 83/6, University of Melbourne, 1983.[OY85] Z. M. Ozsoyoglu and L.-Y. Yuan. A normal form for nested re-lations. In Proc. Fourth Annual ACM Symposium on Principlesof Database Systems, pages 251{260, ACM, Portland, OR, 1985.[Rei78] R. Reiter. Deductive question answering in relational databases.In H. Gallaire and J. Minker, editors, Logic and Databases,pages 147{177, Plenum Press, 1978.[RR83] M. Rafanelli and F. L. Ricci. A Data De�nition Language for aStatistical Database. Technical Report TR-62, IASI-CNR, July1983.[SN87] Oded Shmueli and Shamim Naqvi. Set grouping and layering inHorn clause programs. In Proc. Fourth International Conferenceon Logic Programming, pages 152{177, 1987.[SP82] H.-J. Scheck and P. Pistor. Data structures for an integrated database management and information retrieval system. In Proc.Fourth Intl. Conf. on Very Large Data Bases, IEEE, 1982.[SS77] J. M. Smith and D. C. P. Smith. Database abstractions: Ag-gregation and generalization. ACM Transactions on DatabaseSystems, 2(2):105{133, 1977.[TZ86] S. Tsur and C. Zaniolo. LDL: A logic-based data-language. InProc. Twelfth Intl. Conf. on Very Large Data Bases, pages 33{41, IEEE, Kyoto, Japan, 1986.[Ull85] J. D. Ullman. Implementation of logical query languages fordatabases. In Proc. ACM Int'l Conf. on Management of Data,ACM, Austin, TX, 1985.[vEK76] M. H. van Emden and R. A. Kowalski. The semantics of pred-icate logic as a programming language. Journal of the ACM,23(4):733{742, 1976. 26