Download presentation
Presentation is loading. Please wait.
Published byThomasina Strickland Modified over 9 years ago
1
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 24 A First Course in Database Systems
2
Deductive Databases Conjunctive Queries and shared variables in logic programming: An important extension to the queries discussed so far is conjunctive queries. Conjunctive queries are a conjunction of goals posed as a query. Example: father(terach, X), father(X,Y)? Goal1 AND Goal2 ?
3
Deductive Databases We will see this again when we discuss Datalog!! Conjunctive queries are a conjunction of goals posed as a query. Example: father(terach, X), father(X,Y)? Goal1 AND Goal2 ? In general father(terach, X), father(X,Y)? ==> Q 1, Q 2, ……., Q n ? Simple queries are a special case of conjunctive queries when there is a single goal.
4
Deductive Databases Q 1, Q 2, ……., Q n ? => Q 1 AND Q 2 AND …….AND Q n ? Conjunctive Query In the simplest conjunctive queries all the goals are ground, for example, father(abraham, isaac), male(lot)? The answer to this query is clearly yes as both goals in the query are facts in the program. ==> goals are ground!
5
Deductive Databases In general, the query Q 1, Q 2, ……., Q n ?, Q 2, ……., Q n ? Where each Q i is a ground goal is answered yes with respect to a parogram P if each Q i is implied by P. Hence, ground conjunctive queries are not very interesting!!! Conjunctive queries are interesting when there are one or more shared variables, variables that occur in two different goals of a query.
6
Deductive Databases Shared variables example: Query: father(haran, X), male(X)? The scope of a variable in a conjunctive query is the whole conjunction. Query: p(X),q(X)? Reads: “Is there an X such that both p(X) and q(X)?
7
Deductive Databases Like in simple queries, variables in conjunctive queries are implicitly existentially quantified. Shared variables are used as a means of constraining a simple query by restricting the range variable. We have already seen as example with the query plus(X,X,4)? where the solution of numbers adding up to 4 was restricted to the numbers being the same.
8
Deductive Databases Consider the query father(haran,X), male (X)? Here solutions to the query father(haran,X)? are restricted to children that are male. Based on our family database, there is only one solution, {X=lot}. Alternatively, this query can be viewed as restricting solutions to the query male(X)? to individuals who have Haran for a father.
9
Deductive Databases A slightly different use of a shared variable can be seen in the query father(terach, X),father(X,Y)? On the one hand, restricts the sons of terach to those who are themselves fathers. On the other hand, it considers individuals Y, whose fathers are sons of terach.
10
The Deductive Databases The query father(terach, X),father(X,Y)? has several solutions: 1){X=abraham, Y=isaac} 2){X=haran, Y=lot}
11
Deductive Databases Conjunctive Query: A conjunctive query is a logical consequence of a program P if all goals in the conjunction are consequences of P, where shared variables are instantiated to the same values in different goals. This instance then deduces the conjuncts in the query via generalization.
12
Deductive Databases Conjunctive Queries: The restriction to ground instances is unnecessary, and will be lifted in later discussions. Operationally, to solve conjunctive query A 1, A 2, ……., A n ? Using a program P, find a substitution such that A 1 ... A n are ground instances of facts in P. The same substitution applied to all the goals ensures that instances of variables are common throughout the query.
13
Deductive Databases Example: father(haran,X), male(X)? Applying the substitution {X=lot} to the query gives the ground instance father(haran, lot), male (lot)? Which is a consequence of the program P.
14
Deductive Databases RULES: Interesting conjunctive queries are defining relationships in their own right!!! The query father(haran,X), male(X)? Asking for a son of Haran.
15
Deductive Databases RULES: The query father(terach, X),father(X,Y)? Asking about grandchildren of Terach.
16
Deductive Databases This brings us to the third and most important statement in logic programming, A RULE, which enables us to define new relationships in terms of existing relationships. Rules are statements of the form: A B 1, B 2, ……., B n where n 0.
17
Deductive Databases A B 1, B 2, ……., B n Horn clause A is the head of the rule, and the B i ’ s are its body. Both A and the B i ’ s are goals. Rules, facts and queries are also called Horn clauses, or clauses for short.
18
Deductive Databases A B 1, B 2, ……., B n Horn clause NOTE: A fact is just a special case of a rule when n=0. Facts are also called unit clauses. There is a special name for clauses with one goalin the body, namely when n=1.
19
Deductive Databases A clause with one goal in the body is called a iterative clause. As for facts, variables appearing in rules are universally quantified, and their scope is the whole rule.
20
Deductive Databases RULES: son(X,Y) father(Y, X), male (X). Expresses the son relationship daughter(X,Y) father(Y, X), female (X). Expresses the daughter relationship grandfather(X, Z) father (X, Y), father (Y, Z). Expresses the grandfather relationship
21
Deductive Databases RULES: Rules can be viewed in two ways. 1)A means of expressing new or complex queries in terms of simple queries. A query son(X, haran)? to the program that contain the rule son(X,Y) father(Y, X), male (X). is translated to the query father(haran, X), male(X)? according to the rule, and solved as before.
22
Deductive Databases A new query about the son relationship has been built from simple queries involving father and male relationships. Interpreting rules in this way is their procedural reading. Procedural reading for the grandfather rule: “To answer a query is X the grandfather of Y, answer the conjunctive query is X the father of Z and Z the father of Y.”
23
Deductive Databases grandfather(X, Z) father (X, Y), father (Y, Z). Procedural reading for the grandfather rule: “To answer a query is X the grandfather of Y, answer the conjunctive query is X the father of Z and Z the father of Y.”
24
Deductive Databases RULES: 2)The second view of rules come from interpreting the rule as a logical axiom. The backward arrow ( ) is used to denote logical implication
25
Deductive Databases RULES: son(X,Y) father(Y, X), male (X). The son rule reads:”X is a son of Y if Y is the father of X and X is male.” The associated reading of this rule is known as the declarative reading.
26
Deductive Databases Rule: The declarative reading of the grandfather rule is: “For all X, Y, and Z, X is the grandfather of Z if X is the father of Y and Y is the father of Z.” (X,Y,Z)(grandfather(X, Z) father (X, Y), father (Y, Z)).
27
Deductive Databases Next Lecture MORE Deductive Databases
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.