













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














Logical Query Languages
Motivatio n:
Datalog Example
Likes(drinker , beer ) Sells(bar , beer , price) Frequents(drinker , bar)
Happy(d) <- Frequents(d,bar) AND Likes(d,beer) AND Sells(bar,beer,p )
Ab ove is a rule.
Left side = head.
Right side = body = AND of subgoals.
Head and subgoals are atoms.
F Atom = predicate and arguments. F Predicate = relation name or arithmetic predicate, e.g. <. F Arguments are variables or constants.
Subgoals (not head) may optionally b e negated by NOT.
Evaluation of Rules
Two, dual, approaches:
Example: Variable-Based Assignment
S(x,y) <- R(x,z) AND R(z,y) AND NOT R(x,y)
R = A B
1 2 2 3
Only assignments that make rst subgoal true:
In case (1), y! 3 makes second subgoal true. Since (1; 3) is not in R , the third subgoal is also true. F Thus, add (x; y ) = (1; 3) to relation S.
In case (2), no value of y makes the second subgoal true. Thus, S =
A B 1 3
Safety
A rule can make no sense if variables app ear in funny ways.
Examples
S(x) <- R(y)
S(x) <- NOT R(x)
S(x) <- R(y) AND x < y
In each of these cases, the result is in nite, even if the relation R is nite.
To make sense as a database op eration, we need to require three things of a variable x (= de nition of safety). If x app ears in either
then x must also app ear in a nonnegated, \ordinary" (relational) subgoal of the b o dy.
We insist that rules b e safe, henceforth.
Datalog Programs
A collect i on of rules is a Datalog program.
Predicates/relati ons divide into two classes:
F EDB = extensional database = relation stored in DB.
F IDB = intensional database = relation de ned by one or more rules.
A predicate must b e IDB or EDB, not b oth.
F Thus, an IDB predicate can app ear in the b o dy or head of a rule; EDB only in the b o dy.
Expressive Power of Datalog
Nonrecursive Datalog = (classical ) relational algebra. F See discussion in text.
Datalog simulates SQL select-from-where without aggregation and grouping.
Recursive Datalog expresses queries that cannot b e expressed in SQL.
But none of these languages have full expressive p ower (Turing completeness).
Recursion
IDB predicate P depends on predicate Q if there is a rule with P in the head and Q in a subgoal.
Draw a graph: no des = IDB predicates, arc P! Q means P dep ends on Q.
Cycles i recursive.
Recursive Example
Sib(x,y) <- Par(x,p) AND Par(y,p) AND x <> y Cousin(x,y) <- Sib(x,y) Cousin(x,y) <- Par(x,xp) AND Par(y,yp) AND Cousin(xp,yp)
Example
EDB Par =
a d
b c e
f g
j k i
h
Note, b ecause of symmetry, Sib and Cousin facts app ear in pairs, so we shall mention only (x; y ) when b oth (x; y ) and (y ; x) are meant.
Sib Cousin
Initial ; ;
Round 1 (b; c); (c; e) ; add: (g ; h); (j; k )
Round 2 (b; c); (c; e) add: (g ; h); (j; k )
Round 3 (f ; g ); (f ; h) add: (g ; i); (h; i) (i; k )
Round 4 (k ; k ) add: (i; j )
Problem with Recursive Negation
Consider:
P(x) <- Q(x) AND NOT P(x)
Q = EDB = f 1 ; 2 g.
Compute IDB P iterativel y?
F Initial ly, P = ;. F Round 1: P = f 1 ; 2 g. F Round 2: P = ;, etc., etc.
Strata
Intuitively : stratum of an IDB predicate = maximum numb er of negations you can pass through on the way to an EDB predicate.
Must not b e 1 in \strati ed" rules.
De ne stratum graph:
F No des = IDB predicates. F Arc P! Q if Q app ears in the b o dy of a rule with head P. F Lab el that arc if Q is in a negated subgoal.
Example
P(x) <- Q(x) AND NOT P(x)