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.
/cse/courses/misc_lang/axp/prolog/bin
and
is called pl
.
parent
. )
1 ?- [parent]. parent compiled, 0.02 sec, 13,648 bytes.
sibling(richard, A).
query gives
six answers. This is because different search paths are considered
different results.
not
which is used
by the different relation. If your prolog does not have not
, use
the text's implementation (page 469).
halt.
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).
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