CS345 Solutions #5

  1. (30 pts.)
    a)
    Query: ans(C) :- cost("Hamlet",C) & sells("Borders","Hamlet)

    Minimal solutions:

      ans(C) :- stocks("Borders","Hamlet",C)
      ans(C) :- BordersInfo(L,T,"Hamlet") & bookInfo("Hamlet",A,Y,C)
      ans(C) :- stocks("Borders","Hamlet",C2) & stocks(S,"Hamlet",C)
      ans(C) :- stocks("Borders","Hamlet",C2) & bookInfo("Hamlet,A,Y,C)

    Answers giving the first two solutions received full credit. The last two solutions are non-intuitive; the cost is associated with the book, and not with the bookstore.

    b)
    Query: ans(S) :- author("Edgar Allan Poe",B) & sells(S,B)

    Minimal solutions:

      ans(S) :- stocks(S,B,C) & published(P,B,"Edgar Allan Poe",Y)
      ans(S) :- stocks(S,B,C1) & bookInfo(B,"Edgar Allan Poe",Y,C2)
      ans("Borders") :- BordersInfo(L,T,B) & published(P,B,"Edgar Allan Poe",Y)
      ans("Borders") :- BordersInfo(L,T,B) & bookInfo(B,"Edgar Allan Poe",Y,C)

    Note that for the second solution, the cost variable in stocks and bookInfo must be given different names: C1 and C2. We don't want to try to match the cost.

    c)
    Query: ans(P) :- publisher(P,B) & sells("Barnes and Noble",B)

    Minimal solutions:

      ans(P) :- stocks("Barnes and Noble",B,C) & published(P,B,A,Y)


  2. (30 pts.)
    a)
    Query: ans(Y) :- copyright("Catcher in the Rye",Y)

    Minimal solutions:

      ans^f(Y) :- published^fbff(P,"Catcher in the Rye",A,Y)
      ans^f(Y) :- bookInfo^bfff("Catcher in the Rye",A,Y,C)

    b)
    Query: ans(P) :- bookstore(S,L) & sells(S,"Crime and Punishment")

    Minimal solutions:

      ans^f(L) :- storeInfo^fff(S,L,T) & stocks^bff(S,"Crime and Punishment",C)
      ans^f(L) :- BordersInfo^ffb(L,T,"Crime and Punishment")

    c)
    Query: ans(S,C) :- cost("The Sun Also Rises",C) & sells(S,"The Sun Also Rises")

    Minimal solutions:

      ans^ff(S,C) :- storeInfo^fff(S,L,T) & stocks^bff(S,"The Sun Also Rises",C)
      ans^bf("Borders",C) :- BordersInfo^ffb(L,T,"The Sun Also Rises) & bookInfo^bfff("The Sun Also Rises",A,Y,C)

    Other minimal solutions are possible, if one assumes that the cost isn't related to the store.


  3. (20 pts.)
    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)

    Views:

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

    Invert the views:

    r4: f(X,g(X,Y)) :- v1(X,Y)
    r5: m(g(X,Y),Y) :- v1(X,Y)
    r6: f(X,Y) :- v2(X,Y)

    Use any pattern with function symbols tat appears in a head in subgoals with which that patter can be unified:

    r7: manc(X,Y) :- f(X,g(A,B)) & manc(g(A,B),Y)
    r8: manc(g(C,D),Y) :- m(g(C,D),Y)
    r9: manc(g(C,D),Y) :- m(g(C,D),Z) & manc(Z,Y)
    r10: manc(X,Y) :- m(X,g(E,F)) & manc(g(E,F),Y)
    r11: manc(g(C,D),Y) :- m(g(C,D),g(E,F)) & manc(g(E,F),Y)

    Use manc1(X,Y,Z) in place of manc(g(X,Y),Z), and substitute for the global predicate in terms of the views:

    r2: manc(X,Y) :- v2(X,Z) & manc(Z,Y)
    r7: manc(X,Y) :- v1(X,B) & manc1(X,B,Y)
    r8: manc1(C,Y,Y) :- v1(C,Y)
    r9: manc1(C,Z,Y) :- v1(C,Z) & manc(Z,Y)

    Answer:

    manc(X,Y) :- v2(X,Z) & manc(Z,Y)
    manc(X,Y) :- v1(X,B) &manc(B,Y)
    manc(X,Y) :- v1(X,Y)

    The last answer can be found by substituting manc1 from r8 into r7.


  4. (20 pts.)
    Query:

    ans(X) :- green(X,Y)
    ans(X) :- red(X,Y) & ans(X,Y)

    Views:

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

    Invert the views:

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

    Introduce the domain predicate node:

    node(X) :- red(X,Y)
    node(Y) :- red(X,Y)
    node(X) :- green(X,Y)
    node(Y) :- green(X,Y)

    Use node to satisfy bindings:

    red(X,Y) :- v1^ff(X,Y)
    red(g(X,Y),Y) :- node(Y) & v2^fb(X,Y)
    green(X,g(X,Y)) :- node(Y) & v2^fb(X,Y)

    Use any pattern with function symbols tat appears in a head in subgoals with which that patter can be unified:

    ans(X) :- green(X,g(A,B))
    node(X) :- green(X,g(A,B))
    node(g(A,B)) :- green(X,g(A,B))

    ans(g(C,D)) :- red(g(C,D),Y) & ans(Y)
    node(g(C,D)) :- red(g(C,D),Y)
    node(Y) :- red(g(C,D),Y)

    ans(Z) :- red(Z,g(E,F)) & ans(g(E,F))
    ans(g(E,F)) :- green(g(E,F),g(A,B))

    Substituting in views:

    node^f(X) :- v1^ff(X,Y)
    node^f(Y) :- v1^ff(X,Y)
    node^f(X) :- node(Y) & v2^fb(X,Y)
    node^f(Y) :- node(Y) & v2(fb(X,Y)

    ans^f(X) :- v1^ff(X,Y) & ans(Y)
    ans^f(X) :- node(Y) & v2^fb(X,Y)

    ans1^f(X,Y) :- node(Y) & v2^fb(X,Y) & ans(Y)

    Answer:

    node^f(X) :- v1^ff(X,Y)
    node^f(Y) :- v1^ff(X,Y)
    node^f(X) :- node(Y) & v2^fb(X,Y)
    ans^f(X) :- v1^ff(X,Y) & ans(Y)
    ans^f(X) :- v2^fb(X,Y) & node(Y)

    It isn't valid to assume that v1 and v2 use the same database and contain complete information (v2 may know about some red arcs that v1 doesn't know about). To get all the answers, it's necessary to introduce a node predicate, which can be used in the binding for v2.