### 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.

### 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 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? ```
``` 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. 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). ```