# CS345 Assignment #5

### Problem Set

1. (30 pts.) Consider the global predicates in an integrated information system about books.

author(A,B)
A is the author of book B.
Y is the copyright year of book B.
cost(B,C)
C is the cost of book B.
publisher(P,B)
P is the publisher for book B.
bookstore(S,L)
L is the location of bookstore S.
sells(S,B)
Book B is sold by bookstore S.
phone(L,T)
T is the telephone number associated with location L.

Suppose the following five views are provided:

``` published(P,B,A,Y) :- publisher(P,B) & author(A,B) & copyright(B,Y) bookInfo(B,A,Y,C) :- author(A,B) & copyright(B,Y) & cost(B,C) storeInfo(S,L,T) :- bookstore(S,L) & phone(L,T) stocks(S,B,C) :- sells(S,B) & cost(B,C) ```
```BordersInfo(L,T,B) :- bookstore("Borders",L) & phone(L,T) & sells("Borders",B) ```

``` ```

Express each of the following queries in terms of the global predicates, and find all minimal solutions for each query.

a)
What is the price for the book "Hamlet" at the bookstore Borders?

b)
Which bookstores sell books written by author Edgar Allan Poe?

c)
Who are the publishers for the books sold by bookstore Barnes and Noble?

2. (30 pts.) Suppose that the views from Problem 1 have the following binding patterns:

``` published(P,B,A,Y) - fbff bookInfo(B,A,Y,C) - bfff or fbff storeInfo(S,L,T) - fff stocks(S,B,C) - bff BordersInfo(L,T,B) - bff or ffb ```

Find all minimal solutions (in terms of the adorned views) to each of the following queries:

a)
What is the copyright date for the book "The Catcher in the Rye"?

b)
Where (location) can one find copies of the book "Crime and Punishment"?

c)
At which bookstores, and for what price, can one find copies of the book "The Sun Also Rises"?

3. (20 pts.) Redo the class example on finding all the maternal ancestors for an individual X:

``` ```

``` r1: manc(X,Y) :- m(X,Y) r2: manc(X,Y) :- f(X,Z) & manc(Z,Y) r3: manc(X,Y) :- m(X,Z) & manc(Z,Y) ```

but with the following altered views:

``` v1(X,Y) :- f(X,Z) & m(Z,Y) v2(X,Y) :- f(X,Y) ```

Use the Duschka/Genesereth method to find all possible answers to the query obtainable from the given views.

4. (20 pts.) Consider the following example:

red(X,Y) and green(X,Y) are global predicates. They represent colored arcs, and for both, X is the starting node and Y is the ending node.

There are the following two source views:

``` v1^ff(X,Y) :- red(X,Y) v2^fb(X,Y) :- green(X,Z) red(Z,Y) ```

"v1^ff" means that the v1 is the view, with bindings f and f, and similarly for v2^fb.

Write a Datalog program that uses the views as EDB predicates, in such a way that the views can be queried with their proper binding patterns. Your program must find as many nodes as it can, from which a green arc can eventually be reached (there exists some path starting at that node which contains a green arc). For example, if we have green(1,2), red(2,3), red(3,4), green(4,5), and green(5,6) then the answer should contain nodes 1, 2, 3, and 4. Note that even though there is a green arc (5,6) reachable from node 5, there is no way to determine that fact from the views, so 5 will not be in the result of your program.