# CS345 Solutions #4

1. (25 pts.)
Q2 contains Q1:

Partition Canonical DB
1 {X},{Y},{Z} {q(0,1), q(0,2), r(1,2)}
2 {X,Y},{Z} {q(0,0), q(0,1), r(10,1)}
3 {X},{Y,Z} {q(0,1), r(1,1)}
4 {X,Z},{Y} {q(0,1), q(0,0), r(1,0)}
5 {X,Y,Z} {q(0,0), r(0,0)}

For 3, 4, and 5, Q1(D) does not contain the frozen head.

For 1: Q1(D) contains its own frozen head, p(0). Q2(D) contains the frozen head of Q1: B->0, A->2, C->1, D->2. For the extended database D', adding tuples consisting of 0's, 1's, and 2's cannot change things as long as we don't add q(2,0), which is a frozen negative subgoal of Q1. Q2(D') contains Q1's frozen head.

For 2: Q1(D) contains its own frozen head, p(0). Q2(D) contains the frozen head of Q1: B->0, A->1, C->0, D->1. For the extended database D', adding tuples consisting of 0's and 1's cannot change things as long as we don't add q(1,0), which is a frozen negative subgoal of Q1. Q2(D') contains Q1's frozen head.

Q1 doesn't contain Q2:

As a counterexample, take the partion: {A},{B},{C},{D}. The canonical DB is {q(1,0), q(1,2), r(3,4)}. The frozen head of Q1 is p(1). Q2(D) = {}, so Q2(D) does not contain the frozen head of Q1. (The heads force X to map to 1; Y can map to 0 or 2, but there is no r(0,?) or r(2,?).)

2. (25 pts.)
Q2 contains Q1:

Partition Canonical DB Q2(D) contains the head of Q1? Mapping
{X},{Y},{Z} D1 = {q(0,1), q(1,2), q(2,0)} Yes A->0, B->1, C->2
{X},{Z},{Y} D2 = {q(0,2), q(2,1), q(1,0)} Yes A->0, B->2, C->1
{Y},{X},{Z} D2 = {q(0,2), q(2,1), q(1,0)} Yes A->0, B->2, C->1
{Y},{Z},{X} D1 = {q(0,1), q(1,2), q(2,0)} Yes A->0, B->1, C->2
{Z},{X},{Y} D1 = {q(0,1), q(1,2), q(2,0)} Yes A->0, B->1, C->2
{Z},{Y},{X} D2 = {q(0,2), q(2,1), q(1,0)} Yes A->0, B->2, C->1
{X},{Y,Z} D3 = {q(0,1), q(1,1), q(1,0)} Yes A->0, B->1, C->1
{X,Y},{Z} D4 = {q(0,0), q(0,1), q(1,0)} Yes A->0, B->0, C->1
{X,Z},{Y} D4 = {q(0,0), q(0,1), q(1,0)} Yes A->0, B->0, C->1
{Y},{X,Z} D3 = {q(0,1), q(1,1), q(1,0)} Yes A->0, B->1, C->1
{Y,Z},{X} D4 = {q(0,0), q(0,1), q(1,0)} Yes A->0, B->0, C->1
{Z},{X,Y} D3 = {q(0,1), q(1,1), q(1,0)} Yes A->0, B->1, C->1
{X,Y,Z} D5 = {q(0,0)} Yes A->0, B->0, C->0

Q1 contains it's own head for all the canonical databases above; there are no negated subgoals or arithmetic subgoals. Since there are no negated subgoals in Q2, when we check to see if Q2(D) contains the frozen head of Q1, we don't need to extend the database. Since for all canonical databases, Q2(D) contains the frozen head of Q1 (p), Q2 contains Q1.

Q1 does not contain Q1:

As a counterexample, take the partition: {A},{B},{C}. D = {q(0,1), q(1,2)}. Q1 contains its own frozen head, p. However, Q2(D) = {}, so Q2(D) doesn't contain the frozen head of Q1.

3. (20 pts.)
Rectify Q1 and Q2:

``` Q1: p(X,Y) :- q(X1,Y1) & q(Y2,X2) & X>=0 & Y>=0 & X1=X &Y1=Y & X2=X & Y2=Y ```

``` Q2: p(A,B) :- q(A1,B1) & q(B2,C) & A+B>=C & A1=A & B1=B & B2=B ```

Four containment mappings:

m1: A1->X1, B1->Y1, B2->X1, C->Y1 & A->X & B->Y
m2: A1->X1, B1->Y1, B2->Y2, C->X2 & A->X & B->Y
m3: A1->Y2, B1->X2, B2->X1, C->Y1 & A->X & B->Y
m4: A1->Y2, B1->X2, B2->Y2, C->X2 & A->X & B->Y

For Q2 to contain Q1, the following implication must hold:

(X>=0 & Y>=0 & X1=X &Y1=Y & X2=X & Y2=Y)

=> m1(A+B>=C & A1=A & B1=B & B2=B) |

m2(A+B>=C & A1=A & B1=B & B2=B) |
m3(A+B>=C & A1=A & B1=B & B2=B) |
m4(A+B>=C & A1=A & B1=B & B2=B)

=> (X+Y>=Y1 & X1=X & Y1=Y & X1=Y) |

(X+Y>=X2 & X1=X & Y1=Y & Y2=Y) |
(X+Y>=Y1 & Y2=X & X2=Y & X1=Y) |
(X+Y>=X2 & Y2=X & X2=Y & Y2=Y)

We know that Y=Y is alway true.

(X>=0) => X+Y>=Y

(X>0 & Y1= Y) = Y => X+Y>Y1

Thus, (X>=0 & Y>=0 & X1=X &Y1=Y & X2=X & Y2=Y) => (X+Y>=Y1 & X1=X & Y1=Y & X1=Y).

Therefore, the implication holds. Q1 is contained in Q2.

4. (30 pts.)
a)
A counterexample is:

``` Q: p(X) :- e(X) & m(X,X,X) R1: p(X) :- e(X) & m(X,X,1) R2: p(X) :- e(X) & m(X,X,0) ```

Q contains p(0) and p(1). R1 contains p(1). R2 contains p(0).

Q1 is contained by the union of R1 and R2. However, Q is not contained by R1, and is not contained by R2.

b)
The two queries are already rectified:

``` Q1: panic :- e(X) & m(X,X,X) Q2: panic :- e(X) & e(Y) & m(X,Y,Y) ```

Q1 contained in Q2:

There is one possible containment mapping from Q2 to Q1:

m1: X->X, Y->Y

Check:

m(X,X,X) => m1(m(X,Y,Y))
=> m(X,X,X)
This is obviously true. Thus, Q1 is contained in Q2.

Q2 is contained in Q1:

There are two containment mappings from Q2 to Q1:

m1:X->X
m2:X->Y

Check:
m(X,Y,Y) => m1(m(X,X,X)) | m2(m(X,X,X))
=> m(X,X,X) | m(Y,Y,Y)

By the rules of arithmetic, m(X,Y,Y) => X=1 | Y=0. And, X=1 | Y=0 => m(X,X,X) | m(Y,Y,Y).

Thus, the implication holds. Therefore, Q2 is contained in Q1.