CS345A PROBLEM SET #1
SOLUTIONS
4 Questions, 100 pts.

Problem 1 (10 pts.)

Consider the rule

p(X) :- q(X,Y) & q(Y,X) & r(Y).
Suppose the relation Q for q contains the (binary) tuples {(1,2), (2,1), (3,3), (1,3), (3,2)} and the relation R for r contains the (unary) tuples {(1), (3), (4)}. What is the value of the relation P for p? For each tuple of P, give a substitution of constants for variables that explains why that tuple is in P.

Solution

The value of P is {(2), (3)}. Substituting 2 for X and 1 for Y explains why (2) is in P. Substituting 3 for X and 3 for Y explains why (3) is in P.

Problem 2 (30pts.)

a)
Consider the rules:

p(X) :- r(X,Y) & s(Y,X).
p(X) :- q(Y,X).
q(X,Y) :- p(X) & s(Y,X).

Suppose the EDB relation R for r contains the tuples {ab,ac,bc,ad,dd} and the EDB relation S for s contains the tuples {ba,ac,cb,dd}. Execute the seminaive evaluation (SNE) algorithm on these rules. What are the final values of IDB relations for p and q? To demonstrate your understanding of the process, show the DELTA-IDB after each round of the SNE algorithm. You need not show the new IDB itself after each round.

b)
Consider the "food chain" rules:

chain(X,Y) :- eats(X,Y).
chain(X,Y) :- chain(X,Z) & chain(Z,Y).

Prove that for any (possibly infinite) EDB eats there is a minimal model for the rules such that chain(X,Y) is true if and only if there is a sequence of Z1,...Zn, where Z1=X, Zn=Y, and eats(Zi,Z{i+1}) for all i.

Hint: Use induction on n in the ``only-if'' direction. For the ``if'' direction, do an induction on the round of naive or seminaive evaluation that results in a pair being added to the chain relation.

c)
Give an example of an infinite EDB relation and rules defining IDB relation(s) such that the SNE algorithm does not converge in any finite number of rounds. What is the minimal model?

Solution

a)
The final values of the IDB relations for p and q are {a,b,c,d} and {ab,bc,ca,dd} respectively. The following table shows the DELTA-IDB after each round of the SNE algorithm.
Round Number DELTA-P DELTA-Q
Round 0 {a,b,d} {}
Round 1 {} {ab,bc,dd}
Round 2 {c} {}
Round 3 {} {ca}
Round 4 {} {}
end

b)
First we note that for any (possibly infinite) EDB there is a unique minimal model for the "food chain" rules because the rules have no negation. We prove the if part first.
Assertion:
If there is a sequence of Z1,...Zn, where eats(Zi,Z{i+1}) for all i, then chain(Z1,Zn) is in the unique minimal model.
Basis:
For n = 2, chain(Z1,Z2) must be in the unique minimal model because otherwise we can make rule 1 false; chain(Z1,Z2) :- eats(Z1,Z2) becomes F :- T..
Inductive step:
Let the assertion hold for all n < k. Consider any sequence Z1,...Zk, such that eats(Zi,Z{i+1}) for all i < k. Then chain(Z1,Z{k-1}) is true because of the inductive hypothesis for n=k-1. Similarly chain(Z{k-1},Zk) is true because of the inductive hypothesis for n=2. Then chain(Z1,Zk) must be true because otherwise we can make rule 2 false; chain(Z1,Zk) :- chain(Z1,Z{k-1}) & chain(Z{k-1},Zk) becomes F :- T & T.
Now we prove only-if part.
Assertion:
If chain(X,Y) is true than there exists a sequence of Z1,...Zn, where eats(Zi,Z{i+1}) for all i, and Z1=X and Zn=Y.
Basis:
If chain(X,Y) is first "discovered" after round 0 of the SNE algorithm then rule 1 is used and thus eats(X,Y) is true so the assertion holds.
Inductive step:
Let the assertion hold for all chain(X,Y) first "discovered" after round n, n < k, of the SNE algorithm. Consider any chain(X,Y) first "discovered" after round k of the SNE algorithm. Since rule 1 does not yield new tuples after round 0, rule 2 must have been used. Therefore, there is Z such that chain(X,Z) and chain(Z,Y) are "discovered" before round k. Then, by the inductive hypothesis we have that there is a sequences of V1,...Vm, where eats(Vi,V{i+1}) for all i, and V1=X and Vm=Z and there is a sequences of W1,...Wj, where eats(Wi,W{i+1}) for all i, and W1=Z and Wj=Y. Thus, we have a sequence X=V1,...Vm=Z=W1,...Wj=Y that proves the assertion.

c)
Consider the infinite EDB relation for next where next(i,i+1) is true for all positive integers i. Consider also the "comparison" rules:

lessThan(X,Y) :- next(X,Y).
lessThan(X,Y) :- next(X,Z) & lessThan(Y,Z).

It is easy to show that lessThan(n,n+m), where m and n are positive integers, is first discovered at the end of round m-1 of the SNE algorithm. Since n can be arbitrarily large the SNE algorithm does not converge in a finite number of rounds. The minimal model is lessThan(n,n+m) for all positive integers n and m

Problem 3 (40pts.)

Suppose we have EDB relations:
supports(Fan,Team)
wears(Team,Color)
likes(Fan,Color)
The first indicates the teams a fan supports; the second tells what colors each team wears, and the last indicates which colors each fan likes. Define the following predicates using safe Datalog rules, with negated subgoals permitted.
a)
happy(F) is true if fan F supports at least one team that wears a color that F likes.

b)
shouldSupport(F,T) if fan F likes all colors that team T wears. You may assume that each team wears at least one color; i.e., every team appears in the first component of the wears relation at least once.
Write each of the following queries in (i) domain relational calculus and (ii) tuple relational calculus. (It is not necessary to write them in Datalog.)
c)
veryHappy(F) if every team that fan F supports wears at least one color that F likes. You may assume that every fan supports at least one team; i.e., each fan appears in the first component of likes.

d)
grumpy(F) if fan F supports no team that wears a color that F likes. Again, you may assume that all fans appear in likes.

Solution

a)
happy(F) :- supports(F,T) & wears(T,C) & likes(F,C)

b)
conflictWithTeam(F,T) :- wears(T,C) & likes(F,D) & NOT likes(F,C)
shouldSupport(F,T) :- wears(T,C) & likes(F,C) & NOT conflictWithTeam(F,T)

c)
In DRC: {F | likes(F,D) AND NOT ((exists T) (supports(F,T) AND NOT ((exists C) (wears(T,C) AND likes(F,C)))))}.

In TRC: {x(1) | like(x) AND NOT ((exists y) (supports(y) AND y[1]=x[1] AND NOT ((exist z,w) (wears(z) AND likes(w) AND z[2] = w[2] AND w[1] = y[1] AND y[2] = z[1]))))}.

d)
In DRC: {F | likes(F,D) AND NOT ((exist T,C) (supports(F,T) AND wears(T,C) AND likes(F,C)))}

In TRC: {x(1) | likes(x) AND NOT ((exist y,z,w) (supports(y) AND wears(z) AND likes(w) AND (x[1]=y[1]=w[1]) AND y[2]=z[1] AND z[2]=w[2]))}

Problem 4 (20pts.)

Consider the rules:
p(X,Y) :- q(X,Y) & NOT r(X).
r(X) :- s(X,Y) & NOT t(Y).
r(X) :- s(X,Y) & r(Y).
a)
Determine the stratum of each predicate. Is the program stratified?

b)
Suppose the relations for the EDB predicates s, t, and q are, respectively, S = {ab,bc,ca}, T = {a,b,c}, and Q = {ab,bc,cd,de}. Find the stratified (perfect) model for the IDB predicates p and r, given this EDB.

c)
Find another minimal model for the rules and the EDB given in (b).

Solution

a)
The program is stratified. The stratum of the EDB predicates s,t, and q is 0; the stratum of r is 1; and the stratum of p is 2.

b)
The stratified (perfect) model for the IDB predicates p and r and the given EDB is {ab,bc,cd,de} and {}.

c)
Another minimal model for the IDB predicates p and r and the given EDB is {de} and {a,b,c}.