CPSC 312 Functional and Logic Programming January-April 2021
Assignment Four: Logic Programming Introduction
Solution
Question One
(a) The query is ?- assignment(A, march, D). Here are the results from the query:
$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.2.4)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- [cs312_2021].
true.
?- assignment(A, march, D).
A = as4,
D = 11 ;
A = as5,
D = 18. ?-
(b) Query is ?- ta(cs312,2021,P), email(P,E).
Just ask of the KB to get the answer. Note that this provides extra information, namely
P.
(c) There can’t be a conjunctive query, because it needs to check if the two variables refer to different people. We could write a rule such as the following where dif(X,Y) is true if X and Y refer to diferent people.
?- office_hour(P1,D1,S1,F1), office_hour(P2,D2,S2,F2),
nextday(D1,D2), dif(P1, P2),
pname(P1,FN1), pname(P2,FN2).
(Without the dif, it could return the same person who has office hours on consecutive days. I used dif which is a built-in predicate that true when the symbols are different – which, in general, does not imply the individuals are different). You could write your own facts about dif for the given constants.
1
(d) You can’t define this given the language defined so far. There may be a TA called sam who is not listed and doesn’t have any office hours. All of the given facts would be true, and yet the answer would be different. You could define a predicate such as ta with no office hours(sam) to indicate that sam is a TA with no office hours.
(e) You need to know:
• When classes start
• What days of the week classes are on • Which dates are holidays
• When classes end
Question Two
See http://www.cs.ubc.ca/~poole/cs312/2021/as4/plumbing_ws.pl for a solution.
Figure 1 shows the interpretation for the symbols in (d).
p3
sink
d3
p4
shower
t3 t6
floor
bath
t2
t5
p2
hws
p5 t4
t1 on t2 on t3 off t4 off t5 off t6 on
d2
t1
p1
d1
Question Three
Figure 1: The Plumbing Domain
2
(a) The following table gives the atoms added and the clause responsible for one possible sequence of clauses selected.
Atom Added
outgrabe manxome toves vorpal gyre gimble wabe slithy
Clause
outgrabe .
manxome .
toves ← manxome .
vorpal ← manxome .
gyre ← manxome .
gimble ← outgrabe .
wabe.
slithy ← gyre , gimble , wabe .
(b) For the query ? − slithy. here is one failing derivation:
Answer clause
yes ← slithy
yes ← toves, brillig.
yes ← outgrabe, vorpal, brillig. yes ← vorpal,brillig.
yes ← manxome, brillig.
yes ← brillig.
yes ← jubjub.
Clause to resolve
slithy ← toves, brillig. toves ← outgrabe,vorpal. outgrabe .
vorpal ← manxome. manxome .
brillig ← jubjub.
FAIL
(c) For the query ? − slithy. Here is one successful derivation:
Answer clause
yes ← slithy
yes ← gyre, gimble, wabe.
yes ← manxome, gimble, wabe. yes ← gimble, wabe.
yes ← outgrabe, wabe.
yes ← wabe.
yes ← .
here is part of the trace
Clause to resolve
slithy : −gyre, gimble, wabe. gyre : −manxome. manxome .
gimble : −outgrabe. outgrabe .
wabe.
SUCCESS!
Call: (8) brillig ? creep
Call: (9) jubjub ? creep (*)
Fail: (9) jubjub ? creep (**)
Fail: (8) brillig ? creep
Redo: (8) toves ? creep (***)
Call: (9) manxome ? creep
Exit: (9) manxome ? creep (****)
(*) it is calling jujube after brillig because of the clause brillig : −jubjub.
(**) jujube has no clauses so it fails. And then brillig fails as all of its clauses fail.
(***) toves needs to find another proof, so it calls the second one. SWI Prolog is reporting
redo when it executes the down arrow (from fail to call). (****) maxome succeeds as there is a fact for it.
(d) Figure
3
call
exit
redo
toves brillig
fail gyre gimble wabe
(e) There are 4 answers. There are 2 proofs for gyre, and 2 proofs for gimble. There is a proof for slithy for each combination of these.
Question Four
It should not have taken more than a few hours. Most of this should have been in understanding the material, not in doing busy work. I hope it was reasonable, and you learned something.
This question took about 3 minutes.
4