% The family database in chapter 9 mother(alice, tom). mother(alice, carol). mother(carol, george). mother(carol, heather). mother(susan, hank). father(john, tom). father(john, carol). father(fred, george). father(fred, heather). father(george, hank). parent(X, Y) :- father(X, Y). parent(X, Y) :- mother(X, Y). parents(X, Y, Z) :- father(X, Z), mother(Y, Z). grandmother(X, Y) :- mother(X, Z), parent(Z, Y). grandfather(X, Y) :- father(X, Z), parent(Z, Y). grandparent(X, Y) :- grandfather(X, Y). grandparent(X, Y) :- grandmother(X, Y). ancestor(X, Y) :- parent(X, Y). ancestor(X, Y) :- parent(X, _Z), ancestor(_Z, Y). ancestors(X, Z) :- setof(X, Y^ancestor(X, Y), Z). progeny(Y, Z) :- setof(Y, X^ancestor(X, Y), Z). % auxiliary routines % show shows an enumerated relation, one tuple per line show(Relation) :- enumerate(Relation, List), list(List). % enumerate enumerates a named relation to produce a list enumerate(Relation, List) :- current_predicate(Relation, Form), Form =.. [Relation | Args], (setof(Args, Form, List) ; List = []). % list shows a list, one element per line list([]). list([A | X]) :- write(A), nl, list(X).