1parent(tom, bob).
2parent(tom, liz).
3parent(bob, ann).
4parent(bob, pat).
5parent(pat, jim).
6parent(pat, eve).
7
8female(liz).
9female(ann).
10female(pat).
11female(eve).
12male(tom).
13male(bob).
14male(jim).
15
16mother(X, Y) :- parent(X, Y), female(X).
17father(X, Y) :- parent(X, Y), male(X).
18
19sibling(X, Y) :-
20 parent(Z, X),
21 parent(Z, Y),
22 X \= Y.
23
24grandparent(X, Z) :-
25 parent(X, Y),
26 parent(Y, Z).
27
28ancestor(X, Y) :- parent(X, Y).
29ancestor(X, Y) :-
30 parent(X, Z),
31 ancestor(Z, Y).
32
33descendant(X, Y) :- ancestor(Y, X).
34
35cousin(X, Y) :-
36 parent(PX, X),
37 parent(PY, Y),
38 sibling(PX, PY).
39
40uncle(X, Y) :-
41 male(X),
42 sibling(X, P),
43 parent(P, Y).
44
45generation(X, X, 0).
46generation(X, Y, N) :-
47 parent(X, Z),
48 generation(Z, Y, N1),
49 N is N1 + 1.
50
51common_ancestor(X, Y, A) :-
52 ancestor(A, X),
53 ancestor(A, Y).
54
55list_length([], 0).
56list_length([_|T], N) :-
57 list_length(T, N1),
58 N is N1 + 1.
59
60list_member(X, [X|_]).
61list_member(X, [_|T]) :- list_member(X, T).
62
63list_append([], L, L).
64list_append([H|T], L, [H|R]) :-
65 list_append(T, L, R).
66
67list_reverse([], []).
68list_reverse([H|T], R) :-
69 list_reverse(T, Rev),
70 list_append(Rev, [H], R).