CSI 2120

Lab 4: Prolog

Exercise 1. Books on Prolog

Consider a small database that represents books, readers and loans.

% --------
% book( Title, Authors, Publisher, Year, CallNumber )
% --------

book(
  'The craft of Prolog',
  'R. A. O''Keefe',
  'MIT Press',
  1990,
  'QA 76.73 .P76 O38 1990'
).
book(
  'Programming in Prolog',
  'W. F. Clocksin, C. S. Mellish',
  'Springer',
  2003,
  'QA 76.73 .P76 C57 2003'
).
book(
  'Prolog for programmers',
  'F. Kluzniak, S. Szpakowicz',
  'Academic Press',
  1985,
  'QA 76.73 .P76 K58 1985'
).
book(
  'Prolog programming for artificial intelligence',
  'I. Bratko',
  'Addison-Wesley',
  2001,
  'Q 336 .B74 2001'
).

% --------
% reader( CardNumber, Name )
% --------

reader( 1234567, 'James Brown' ).
reader( 2345678, 'Jacques Brun' ).
reader( 3456789, 'Giacomo Bruno' ).

% --------
% checkedOut( CardNumber, CallNumber )
% --------

checkedOut( 1234567, 'QA 76.73 .P76 C57 2003' ).
checkedOut( 3456789, 'Q 336 .B74 2001' ).

Write the queries that find (1) all books published by Springer, (2) all books published after 1990, (3) all books checked out by James Brown. Next, edit the file to add a few useful entries to each of the three predicates, and run more queries to see that you have not spoiled anything.

Exercise 2: Relationships

Given the following database of relationship:

parent(jack,joe).
parent(jack,karl).
parent(marie,anne).
parent(joe,anne).
parent(marie,paul).
parent(joe,paul).
parent(marie,sylvie).
parent(bruno,sylvie).
parent(anne,zach).
parent(tim,zach).
parent(sam,tim).
parent(emma,tim).
parent(josee,sam).
parent(gilles,sam).
female(marie).
female(sylvie).
female(anne).
female(emma).
female(josee).
male(karl).
male(jack).
male(joe).
male(bruno).
male(paul).
male(tim).
male(zach).
male(sam).
male(gilles).
Complete the predicate gmp replacing the ? with the appropriate variable. We want to identify the grandmother of Y on the paternal (father's side) from the facts listed above.

gmp(X,Y) :- parent(?,?), 
	    male(?), 
	    parent(?,?), 
	    female(?).
Test your predicate with the following queries:

1 ?- gmp(emma,zach).
true.

2 ?- gmp(marie,paul).
false.

3 ?- gmp(X,tim).
X = josee ;
false.

4 ?- gmp(X,anne).
false.

Exercise 3: Rules and Quiz

Please hand-in the answer to this question on Virtual Campus during your lab session but at the latest by Friday 6:00pm! Remember, your submission will only count if you have signed the lab attendance sheet. Given the following database:

% Decision to go skiing

% trail conditions
condition( skyline, green ).
condition( burma, green ).
condition( fortune, green ).
condition( mcclosky, red ).
    
% no time tuesday thursday CSI2120
available( monday ).
available( wednesday ).
available( friday ).
    
% high temperatures
temperature( mild, monday ).
temperature( mild, tuesday ).
temperature( verycold, wednesday ).
temperature( cold, thursday ).
temperature( warm, friday ).

% available wax  
wax( green, verycold ).
wax( blueExtra, mild ).
wax( red, warm ).

route( Col ) :- condition( skyline, Col ),
		condition( burma, Col ),
		condition( fortune, Col ).

% hard-wired lights
connected(L) :- fuse(L, F),
		ok(F).

% plugged in lights
connected(L) :- plug(L,S),
                fuse(S,F),
                ok(F).
Write a predicate goSki that returns true for a day D that you are available and have a wax of the right colour according the above database. You must also make sure that your preferred route skyline to burma to fortune is green. You do not want to ski if it is warm or very cold.

?- goSki(D,W).
D = monday,
W = blueExtra ;
false.