CSc 8710 Deductive Databases and Logic Programming
Fall 1998
Exam #1
Monday, October 12, 1998
tree(X,LeftSubTree,RightSubTree)where X is the element at any node and LeftSubTree and RightSubTree are the left sub-tree and the right sub-tree for the node respectively. The term nil is used when there is no left sub-tree or right-tree. For example, the following function term:
tree(20,tree(10,nil,nil),tree(40,tree(30,nil,nil),nil))would represent the following binary (search) tree:
Recall that a binary search tree is a binary tree in which all the elements in the left sub-tree of a node are less than the element at the node and all the elements in the right sub-tree of a node are greater than the element at the node. Assume that the binary search tree is being used to represent a set of elements (i.e. no duplicates).
Write Prolog programs for the following predicates:
mystery1([]). mystery1([X|L]) :- mystery2(L). mystery2([X|L]) :- mystery1(L).How will Prolog respond to the following queries?
?- mystery1([a,b,c,d,e]). ?- mystery1([a,b,c,d]). ?- mystery2([a,b,c]). ?- mystery2([a,b]).What do the relations mystery1 and mystery2 do?
(a) [[the,Y]|Z] and [[X,hare],[is,here]] (b) [golden|T] and [golden,norfolk] (c) [X,Y|Z] and [mary,likes,wine] (d) A(X,3) and A(Y,Y) (e) [] and [X|Y] (f) [X|[Y|Z]] and [a,b,c,d,e] (g) append([b],[c,d],L) and append([X|Xs],Ys,[X|Zs])
MOVIES(TITLE,Director) ACTORS(TITLE,ACTOR) THEATERS(TNAME,Address,Phone) PLAYS(TNAME,TITLE,STARTTIME)
The key attributes are shown in all capital letters. The MOVIES table records information about current movies, the ACTORS table records information about the different actors acting in each movie, the THEATERS table records information about the various theaters in a city and the PLAYS table records information about which movie is being played in which theater and at what time.
Write Datalog programs to answer the following queries. Note: You should use only Datalog constructs, i.e., you should not use any Prolog features that are not available in Datalog such as lists, setof predicate, function terms etc.
P(X,Y) :- Q(X,Y). P(X,Y) :- Q(X,Z), P(Z,Y). Q(a,b). Q(b,c). Q(c,d). Q(d,e).
I1 = EDB U { P(a,b), P(b,c), P(c,d), P(d,e) } I2 = EDB U { P(a,b), P(b,c), P(c,d), P(d,e), P(a,c), P(b,d), P(c,e) } I3 = EDB U { P(a,b), P(b,c), P(c,d), P(d,e), P(a,c), P(b,d), P(c,e), P(a,d), P(b,e) } I4 = EDB U { P(a,b), P(b,c), P(c,d), P(d,e), P(a,c), P(b,d), P(c,e), P(a,d), P(b,e), P(a,e) }