CSc 8710 Deductive Databases and Logic Programming
Fall 1998
Practice Problems for Exam #1
Due: Monday, October 12, 1998 (will count for 20% of the exam)
r(X,[],0). r(X,[X|L],N) :- r(X,L,M), N is M + 1. r(X,[Y|L],N) :- r(X,L,N).How will Prolog answer the following queries?
The first relation indicates the bars a drinker visits; the second relation tells what beer each bar sells, and the last relation indicates which beer each drinker likes to drink. Write Prolog programs to answer the following queries:
supplies(X,Z) :- supplies(X,Y), subpart(Z,Y). supplies(X,Y) :- d_supplies(X,Y). supplies(foobar,X) :- widget(X). d_supplies(acme,p1). d_supplies(aaa,w3). d_supplies(aaa,w4). subpart(X,Y) :- d_subpart(X,Y). subpart(X,Y) :- d_subpart(X,Z), subpart(Z,Y). d_subpart(p2,p1). d_subpart(p3,p2). d_subpart(w1,p1). d_subpart(w2,w1). widget(w1). widget(w2). widget(w3). widget(w4).
I1 = EDB U { supplies(aaa, w3), supplies(aaa, w4), supplies(acme, p1), supplies(acme, p2), supplies(acme, w1), supplies(acme, w2), supplies(foobar, w1), supplies(foobar, w2), supplies(foobar, w3), supplies(foobar, w4), subpart(p2, p1), subpart(p3, p2), subpart(w1, p1), subpart(w2, p1), subpart(w2, w1) } I2 = EDB U { supplies(aaa, w3), supplies(aaa, w4), supplies(acme, p1), supplies(acme, p2), supplies(acme, p3), supplies(acme, w1), supplies(acme, w2), supplies(foobar, w1), supplies(foobar, w2), supplies(foobar, w3), supplies(foobar, w4), subpart(p2, p1), subpart(p3, p1), subpart(p3, p2), subpart(w1, p1), subpart(w2, p1), subpart(w2, w1) } I3 = EDB U { supplies(aaa, w3), supplies(aaa, w4), supplies(acme, p1), supplies(acme, p2), supplies(acme, p3), supplies(acme, w1), supplies(acme, w2), supplies(foobar, w1), supplies(foobar, w2), supplies(foobar, w3), supplies(foobar, w4), supplies(foobar,p1), subpart(p2, p1), subpart(p3, p1), subpart(p3, p2), subpart(w1, p1), subpart(w2, p1), subpart(w2, w1), subpart(w1,p3) }