### Problem 1 (20 pts.)

Consider the conjunctive queries with negation P and Q.
``` P: p(X) :- q(X,Y) & q(Y,X) & r(Y,Y) & NOT q(X,X) & NOT r(Y,X). Q: ```
``` p(B) :- q(A,B) & r(A,A) & NOT r(A,B). ```
Use the Levy-Sagiv test to show that P is contained in Q and Q is not contained in P.

### Solution

First, we show that P is contained in Q.
• There are two partitions of the variables in P: {XY} and {X} {Y}.
• The basic canonical database for the first partition is D = {q(0,0), r(0,0)}. Since P(D) is empty we don't need to check if Q(D) contains the frozen head of P.
• The basic canonical database for the second partition is D = {q(0,1), q(1,0), r(1,1)}. Then we have P(D) = Q(D) = {p(0)}. We also have to consider all extended canonical databases for P. Let D' be an extended canonical database for P. Since r(1,0) is a frozen negated subgoal of P then it is not in D'. Then we have that the frozen head of P, p(0), is in Q(D') because of the instantiation A->1, B->0.
Then we show that Q is not contained in P.
• Consider the partition {B} {A} of the variables in Q.
• The basic canonical database for this partition is D = {q(1,0), r(1,1)}. Q(D) = {p(0)} but P(D) is empty.

### Problem 2 (25 pts.)

Consider the conjunctive queries (with arithmetic subgoals) P and Q.
``` P: panic :- q(X,Y,Z) & q(X,Z,Y) & q(Y,Z,X). Q: ```
``` panic :- q(X,Y,Z) & X<=Z & Y<=Z. ```
Use the Klug-Levy-Sagiv test to show that P is contained in Q.

### Solution

Consider the canonical database D, constructed from P for any partition of the variables in P and any order for that partition. Let the constants assigned to X, Y, and Z be a, b, and c respectively. Thus, D = {q(a,b,c), q(a,c,b), q(b,c,a)}. Note that a, b, and c are not necessarily different. We will consider all 3 possible cases.
• Let max(a,b,c) = a. Then Q(D) contains the the frozen head of P, panic, because of the instantiation X->b, Y->c, Z->a.
• Let max(a,b,c) = b. Then Q(D) contains the the frozen head of P, panic, because of the instantiation X->a, Y->c, Z->b.
• Let max(a,b,c) = c. Then Q(D) contains the the frozen head of P, panic, because of the instantiation X->a, Y->b, Z->c
Therefore P is contained in Q.

### Problem 3 (30 pts.)

This problem involves a "mystery" predicate m. There are two things known about m. ("panic" is a generic 0-ary predicate or, equivalently, a propositional variable, that we may use as the head of a CQ that represents a constraint.)
• The conjunctive query

``` ```
``` panic :- p(X,Y) & m(Y,X) ```

contains the conjunctive query

``` ```
``` panic :- p(X,Y) & p(Y,X). ```

• If both m(X,Y) and m(Y,Z) are true than m(Z,X) is true.
Prove that the conjunctive query

``` ```
``` panic :- q(X,Y,Z) & m(Y,X) & m(Y,Z) ```

contains the conjunctive query

``` ```
``` panic :- q(X,Y,X) & q(Y,Z,Y) & q(X,Z,X). ```

### Solution

• First, we apply the Gupta test for the first pair of queries. Since m is an interpreted predicate, and not an ordinary subgoal, we don't need to eliminate X and Y from m. There are 2 containment mappings, M1: {X->X, Y->Y} and M2: {X->Y, Y->X}. The logical expression that we get from the Gupta test is: ``` => M1(m(X,Y)) OR M2(m(X,Y))```. After simplifying it, we get ``` m(X,Y) OR m(Y,X)```. Since we are given that the first query contains the second, the logical expression must be always true.

• Then we apply the Gupta test to the second pair of queries. There are 3 containment mappings, M1: {X->X, Y->Y, Z->X}, M2: {X->Y, Y->Z, Z->Y}, and M3: {X->X, Y->Z, Z->X}. The logical expression we get is: ``` => M1(m(Y,X) & m(Y,Z)) OR M2(m(Y,X) & m(Y,Z)) OR M3(m(Y,X) & m(Y,Z))```. After simplifying it, we get ```m(Y,X) OR m(Z,Y) OR m(Z,X)```. We prove that this expression is always true by contradiction.

• Suppose `m(b,a) OR m(c,b) OR m(c,a)` is false for some constants a, b ,and c. Then, from the logical expression we derived from the first pair of queries we get that `m(a,b)` and `m(b,c)` are true because both `m(a,b) OR m(b,a)` and ` m(b,c) OR m(c,b)` must be true. Then, by the given fact about m we get that `m(c,a)` must be true. This is, however, a contradiction of our assumptions.
Therefore, by the Gupta test, ```panic :- q(X,Y,Z) & m(Y,X) & m(Y,Z)``` contains
```panic :- q(X,Y,X) & q(Y,Z,Y) & q(X,Z,X)```.

### Problem 4 (25 pts.)

Determine whether each of the conjunctive queries
``` P: p(X,Y) :- arc(X,A) & arc(A,Y) & arc(X,B) & arc(B,Y) & X<A & B<Y. Q: ```
``` p(X,Y) :- arc(X,Z) & arc(Z,Y) & X<Z & Z<Y. ```
is contained in the other. If not, give a counterexample database.

Hint: The Gupta test is messy here, since there are a lot of containment mappings after we replace duplicate occurrences of variables. However, in general, and here in particular, when there is implication of the OR over all containment mappings, we can usually identify one or a few of the containment mappings that are really involved in the implication. Try first to understand what the CQ's are saying, and the focus on the containment mappings you really need.

### Solution

First we show that P is not contained in Q.
• Consider the database D = {arc(0,2), arc(1,0), arc(1,3), arc(3,2)}. P(D) = {p(1,2)} but Q(D) is empty.

Then we show that Q is contained in P.
• First we eliminate duplicate variables from the ordinary subgoals in P. The resulting query is: ```p(X,Y) :- arc(X,A) & arc(A1,Y) & arc(X1,B) & arc(B1,Y1) & X<A & B<Y & A1=A & X1=X & B1=B & Y1=Y```.

• Consider the containment mapping from P to Q, M: {X->X, Y->Y, X1->X, Y1-Y, A-Z, A1->Z, B->Z, B1->Z}.

• M(`X<A & B<Y & A1=A & X1=X & B1=B & Y1=Y`) = `X<Z & Z<Y`. From the Gupta test we have to test whether `X<Z & Z<Y` (the interpreted subgoals in Q) logically imply the OR, over all containment mappings from P to Q, of the interpreted subgoals in P. Thus, we have that the implication is always true since M of the interpreted subgoals in P is the same as the interpreted subgoals in Q.