male(tom).
male(jim).
male(alex).
female(marry).
female(betty).
female(dorothy).
female(alice).
parent(tom,jim).
parent(betty,jim).
parent(tom,alex).
parent(marry,alex).
parent(alice, marry).
parent(dorothy, tom).
child(X, Y) :- parent(Y, X).
mother(X, Y) :- parent(X, Y), female(X).
allparents(X, Set) :- setof(P, parent(P, X), Set).
sibling(X, Y) :- parent(P, Y), parent(P, X), X \= Y.
half_sibling(X,Y) :- parent(P1,X), parent(P1,Y), parent(P2,X), parent(P3,Y),
P1 \= P2, P1 \= P3, P2 \= P3.
sister(X, Y) :- sibling(X, Y), female(X).
grandparent(X, Y) :- parent(P, Y), parent(X, P).
greatgrandparent(X, Y) :- parent(P, Y), grandparent(X, P).
uncle(U, N) :- parent(P, N), sibling(U, P), male(U).
cousin(X, Y) :- grandparent(G, X), grandparent(G, Y), X \= Y, \+sibling(X, Y).
cousinonceremoved(X, Y) :- cousin(Z, Y), child(X, Z).
cousin2(Child1,Child2) :- greatgrandparent(Z, Child1), greatgrandparent(Z, Child2),
\+sibling(Child1, Child2), \+cousin(Child1, Child2), Child1 \= Child2.
cousin2onceremoved(H, G) :- parent(F, H), cousin2(F, G).
% ancestor(X, Y) :- parent(X, Y).
% ancestor(X, Y) :- parent(P, Y), ancestor(X, P).
ancestor(X, Y, Level) :- parent(X, Y), Level is 1.
ancestor(X, Y, Level) :- parent(P, Y), ancestor(X, P, L), Level is L + 1.