%Prolog Example - Geneology %Author - Dan Schlegel %Modified - 11/7/2010 son(S, P) :- man(S), parent(P, S). daughter(D, P) :- woman(D), parent(P, D). childOf(C, P) :- parent(P, C). childOf(C, P1, P2) :- parent(P1, C), parent(P2, C), P1\=P2. sibling(S1, S2) :- parent(Parent, S1), parent(Parent, S2), S1\=S2. aunt(A, P) :- parent(Parent, P), sibling(Parent, A), woman(A). uncle(U, P) :- parent(Parent, P), sibling(Parent, U), man(U). grandparent(GP, P) :- parent(Parent, P), parent(GP, Parent). %You'll note that ancestor is the transitive closure of 'Parent'. ancestor(A, P) :- ancestor(A, P, _). %Bad form in Prolog, you shouldn't do this! ancestor(A, P, L) :- parent(A, P), L is 1. ancestor(A, P, L) :- parent(Parent, P), ancestor(A, Parent, M), L is M+1. descendent(D, P, L) :- ancestor(P, D, L). commonancestor(A, P1, P2) :- ancestor(A, P1, L1), ancestor(A, P2, L2), L1=L2, P1\=P2. cousin(C, P) :- grandparent(GP, P), commonancestor(GP, C, P). man(tom). man(adam). man(alex). woman(betty). woman(mary). woman(kate). parent(tom, mary). parent(tom, adam). parent(betty, mary). parent(betty, adam). parent(adam, alex). parent(mary, kate). %A trap not to fall in to... %childOf(betty, mary, adam). %%%Questions. grandparent(X, kate). aunt(A, kate). uncle(U, kate). descendent(D, tom, _). cousin(X, Y). %Lets add some rules... %nephew %great-granddaughter