CSE 341, Prolog Non Assignment

This is not to be turned in.

This is a practice assignment for prolog - I recommend very strongly that you gain some familiarity with prolog by playing with the interpreter. Do this soon - preferably before Monday's lecture. It will make the assignment much easier, and should also make the lecture material clearer.

The assignment is to implement a "family database", and to define basic relations. Below is a database I put together for my family, and a script of a brief run.

  1. You can use whatever prolog system you want. I have had a public domain version installed on the instructional alphas. The executable is located in /cse/courses/misc_lang/axp/prolog/bin and is called pl.
  2. You load a file into the interpreter by enclosing it in [ ]'s. (I had difficulty using a filename with an extension - so I just called the file parent. )
    1 ?- [parent].
    parent compiled, 0.02 sec, 13,648 bytes.
    
  3. I ran through some basic queries. After you get an answer, return ends the query, and a semi-colon gives the next answer.

  4. Note that the sibling(richard, A). query gives six answers. This is because different search paths are considered different results.

  5. The prolog I used has a built in not which is used by the different relation. If your prolog does not have not, use the text's implementation (page 469).

  6. When you are done, type halt.

My database file


different(X,Y) :-
  not(X = Y).

parent(norman, richard). parent(norman, judith). parent(norman, susan).
parent(norman, karen). parent(margaret, richard). parent(margaret, judith).
parent(margaret, susan). parent(margaret, karen). parent(richard, casey).
parent(richard, jena). parent(nancy, casey). parent(nancy, jena).
parent(karen, fiona). parent(bruce, fiona). parent(judith, alex).
parent(judith, chris). parent(bob, chris). parent(bob, alex).
parent(susan, conner). parent(susan, cameron). parent(barry, conner).
parent(barry, cameron). parent(kathleen, norman). parent(herbert, norman).
parent(john, margaret). parent(madge, margaret).
male(richard). male(norman).  male(casey). male(chris). male(alex). 
male(cameron). male(conner). male(barry). male(bruce). male(bob).
male(john). male(herbert).
female(margaret). female(susan). female(karen). female(judith). female(jena).
female(nancy). female(fiona). female(kathleen). female(madge).

grandparent(X,Y) :- parent(X,Z), parent(Z,Y).
grandfather(X,Y) :- male(X), grandparent(X,Y).
grandmother(X,Y) :- female(X), grandparent(X,Y).
father(X,Y) :- male(X), parent(X,Y).
mother(X,Y) :- female(X), parent(X,Y).
child(X,Y) :- parent(Y,X).
son(X,Y) :- male(X), child(X,Y).
daughter(X,Y) :- female(X), child(X,Y).
sibling(X,Y) :- child(X, Z), child(Y, Z), different(X, Y).
cousin(X, Y) :- child(X, A), sibling(A, B), child(Y, B).
brother(X, Y) :- male(X), sibling(X, Y).
sister(X, Y) :- female(X), sibling(X, Y).
uncle(X, Y) :- brother(X, Z), parent(Z, Y).
aunt(X, Y) :- sister(X, Z), parent(Z, Y).
ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :- parent(X,Z), ancestor(Z, Y).
descendent(X,Y) :- ancestor(Y,X).

Transcript


Script started on Fri Oct 31 15:41:52 1997
orcas% prolog
Welcome to SWI-Prolog (Version 2.9.5)
Copyright (c) 1993-1997 University of Amsterdam.  All rights reserved.

For help, use ?- help(Topic). or ?- apropos(Word).

1 ?- [parent].
parent compiled, 0.02 sec, 13,648 bytes.

Yes
2 ?- parent(richard, A).

A = casey ;

A = jena 

Yes
3 ?- parent(A, richard).

A = norman ;

A = margaret 

Yes
4 ?- ancestor(A, richard).

A = norman ;

A = margaret ;

A = kathleen ;

A = herbert ;

A = john ;

A = madge ;

No

6 ?- sibling(richard, A).

A = judith ;

A = susan ;

A = karen ;

A = judith ;

A = susan ;

A = karen ;

No
7 ?- brother(A, B).

A = richard
B = judith ;

A = richard
B = susan ;

A = richard
B = karen ;

A = richard
B = judith ;

 . . . quite a few answers deleted . . .

No
8 ?- male(X), ancestor(X, richard).

X = norman ;

X = john ;

X = herbert ;

No
9 ?- halt.
orcas%
script done on Fri Oct 31 15:46:58 1997