Question 1 [11 marks]
(a) [2 marks]
(b) [2 marks]
(c) [2 marks]
ok([prolog, twists, minds], I, Y) and ok([I | R], R, I). p(h(B, Z), B, Y) and p(A, b, f (A)).
bar(A,[i,s | B],B) and bar([c,s | C],C,[f,u,n])
CPSC 312
Midterm Examination 2 — Fall 2017
(a) [3 marks] What two facts about “Foo Inc.” (foo_inc) do the following triples tell us? (Assume that the atoms have the meaning one would expect. Use plain English that someone who knows nothing about computer science or mathematics would understand.)
prop(prolog_is_fun_book, published_by, foo_inc).
prop(published_by, ’rdfs:range’, publisher).
prop(published_by, ’rdfs:domain’, book).
(b) [5 marks] Consider the predicate
exam(Course, Section, Year, Month, Day)
which is true when Section of Course has a midterm on day given by the Year, Month and Day. Translate exam(cpsc312, 101, 2017, december, 13), which means section 101 of CPSC312 in 2017
has a final exam on December 13, into triples where the values are all atomic:
(c) [3 marks] In the semantic web, URI is a “Uniform Resource Identifier”. What is the purpose of a URI?
Question 2 [6 marks]
For each the following pairs of terms, either give their most general unifier or say why no most general unifier exists.
Question 3 [10 marks]
(a) [8 marks] Given the logic program:
mp(L0,L2,O1,C0,C2) :-
reln(L0,L1,O1,O2,C0,C1),
noun(L1,L2,O2,C1,C2).
reln([next,to | L],L,O1,O2,[next_to(O1,O2)|C],C).
noun([X | L],L,X,C,C) :- country(X).
country(brazil).
Consider a derivation for the first answer that Prolog finds for the query:
?- mp([next,to,brazil],R,I,C,[]).
Fill in the blanks to show the sequence of answer clauses in a the first part of derivation
1
yes(R,I,C) :- mp([next,to,brazil],R,I,C,[]).
yes(R,I,C) :- reln([next,to,brazil],L11,____,O12,C,C12),
noun(L11,_____,O12,C12,______).
yes(R,I,___________________________________) :-
noun(_______________,R,O12,C12,________).
yes(_________,I, ___________________________________) :-
country(brazil).
(b) [2 marks] What is the answer that Prolog gives for this query?
Question 4 [10 marks]
Suppose a binary tree is represented using the terms:
• leaf , the empty tree (where the root is a just a leaf) or
• node(LT,Value,RT),non-emptytreewhereValueisthevalueattherootofatreeandLTisthesubtree to the left of the root and RT is the subtree to the right of the root.
For example node(node(leaf , 2, leaf ), 5, leaf ) defines the tree with 5 at the root, and containing 2 in it’s left subtree.
Define the relation to list(Tree, Lst) that is true if Lst is a list of the elements in the tree (they are the root of some subtree), such that for each tree, the list contains the elements the left subtree followed by the root followed by elements of the right subtree. This is often called the inorder traversal of the tree. For example:
?- to_list(node(node(leaf, 1, node(leaf, 4, leaf)),
6, node(leaf, 8, leaf)),L).
L = [1, 4, 6, 8].
The only built-in predicate you may use is append. You may define any helper predicates you like. If you use append in your solution (even if you define it as a helper function and give it a different name), the most you can get is 7/10.
2
Question 5 [5 marks]
Sam wanted to write a program to flatten a list (find the atomic elements of a perhaps nested list) and put the elements in reverse order.
For example, [a,[b],c] should be flattened to [c,b,a] and [a,[b,c,[d,[e],f],[g]]] should be flattened to [g,f,e,d,c,b,a]. Sam wrote:
% revFlat(L,F1,F2) is true if F1-F2 is the reverse of the flattening of L
revFlat([],L,L).
revFlat([H|T],R,R0) :-
atomic(H),
revFlat(T,R,[H|R0]).
revFlat([[A|B]|T],R0,R2) :-
revFlat([A|B],R0,R1),
revFlat(T,R1,R2).
Sam found that it has the following behaviour:
?- revFlat([a,[b],c],R,[]).
R = [b, c, a]
?- revFlat([a,[b,c,[d,[e],f],[g]]],F,[]).
F = [e, f, d, g, c, b, a]
Explain to Sam how to fix the problem. (Do not write a new program, but explain what modifictions are needed to the program above. Feel free to circle or point to parts of the program in your description.)
Question 6 [3 marks]
Complete the following sentences (preferably with reference to the course). (a) I like
(b) I dislike
(c) I wish
3