father(michael,cathy).
father(michael,sharon).
father(charles_gordon,michael).
father(charles_gordon,julie).
father(charles,charles_gordon).
father(jim,melody).
father(jim,crystal).
father(elmo,jim).
father(greg,stephanie).
father(greg,danielle).


mother(melody,cathy).
mother(melody,sharon).
mother(hazel,michael).
mother(hazel,julie).
mother(eleanor,melody).
mother(eleanor,crystal).
mother(crystal,stephanie).
mother(crystal,danielle).

sex(male,michael).
sex(male,charles).
sex(male,charles_gordon).
sex(male,jim).
sex(male,elmo).
sex(male,greg).

sex(female,cathy).
sex(female,sharon).
sex(female,melody).
sex(female,hazel).
sex(female,julie).
sex(female,eleanor).
sex(female,crystal).
sex(female,stephanie).
sex(female,danielle).

married(michael,melody).
married(charles_gordon,hazel).
married(jim,eleanor).
married(greg,crystal).
married(hazel,charles_gordon).
married(eleanor,jim).
married(melody,michael).
married(crystal,greg).

parent(X,Y) :- father(X,Y).
parent(X,Y) :- mother(X,Y).

child(X,Y) :- father(Y,X).
child(X,Y) :- mother(Y,X).

grandparent(X,Y) :-
     parent(Z,Y),
     parent(X,Z).

% both bio parents
sibling(Sib1, Sib2) :-
     mother(Mom, Sib1), father(Dad, Sib1),
     mother(Mom, Sib2), father(Dad, Sib2),
     \+ Sib1==Sib2.
% stepfather version
sibling(Sib1, Sib2) :-
     mother(Mom, Sib1), father(Dad1, Sib1),
     mother(Mom, Sib2), father(Dad2, Sib2),
     \+ Dad1==Dad2, \+ Sib1==Sib2.
% stepmother version
sibling(Sib1, Sib2) :-
     mother(Mom1, Sib1), father(Dad, Sib1),
     mother(Mom2, Sib2), father(Dad, Sib2),
     \+ Mom1==Mom2, \+ Sib1==Sib2.

%sibling(Sib1, Sib2) :-
%     parent(Parent, Sib1),
%     parent(Parent, Sib2),
%     \+ Sib1==Sib2.

uncle(X,Y) :- 
     parent(Z,Y),
     sibling(X,Z),
     sex(male,X).
uncle(X,Y) :- 
     parent(B,Y),
     sibling(A,B),
     sex(female,A),
     married(A,X).

% uncle(X,Y) :-
%      sex(male,X),
%      aunt(A,Y),
%      married(X,A).

aunt(X,Y) :- 
    parent(Z,Y),
    sibling(X,Z),
    sex(female,X).
aunt(X,Y) :- 
    parent(B,Y),
    sibling(A,B),
    sex(male,A),
    married(A,X).

% aunt(X,Y) :-
%     sex(female,X),
%     uncle(U,Y),
%     married(X,U).

nephew(X,Y) :-
      sibling(Y,Z),
      parent(Z,X),
      sex(male,X).
nephew(X,Y) :-
      married(Y,A),
      sibling(Z,A),
      parent(Z,X),
      sex(male,X).

niece(X,Y) :-
     sibling(Y,Z),
     parent(Z,X),
     sex(female,X).
niece(X,Y) :-
     married(Y,A),
     sibling(Z,A),
     parent(Z,X),
     sex(female,X).

mother_in_law(M,S) :-
     sex(female,M),
     married(S,X),
     parent(M,X).

ancestor(X,Y) :-
     parent(X,Y).
ancestor(X,Y) :-
     parent(X,Z),
     ancestor(Z,Y).

% 2.4.4
run_it :-
     write('The ancestors of Cathy are: '),
     ancestor(Y, cathy),
     write(Y),
     write(' '),
     fail.

% 2.5.2
get_ancestors(X) :-
     write('The ancestors of '),
     write(X),
     write(' are: '),
     ancestor(Y, X),
     write(Y),
     write(', '),
     fail.
get_ancestors(_) :-
     write(done).