%=============================================================================
%----Write a program that stores information about your family, and will 
%----answer queries about relationships. Information about individual people 
%----must be stored as facts containing the persons name, sex and parents' 
%----names, e.g. person(geoff,male,bob,margaret).

%----Here are the queries you should be able to answer ...

%?-sibling(Person,Sibling).       Algorithm : Siblings have the same parents.
%?-brother(Person,Brother).       Algorithm : A brother is a male sibling.
%?-sister(Person,Sister).         Algorithm : A sister is a female sibling.
%?-father(Person,Father).
%?-mother(Person,Mother).
%?-parent(Person,Parent).         Algorithm : Either a mother or father
%?-offspring(Person,Offspring).   Algorithm : The inverse of parent.
%?-son(Person,Son).               Algorithm : A son is a male offspring.
%?-daughter(Person,Daughter).     Algorithm : A daughter is a female offspring.
%?-aunt(Person,Aunt).             Algorithm : An aunt is a parent's sister.
%?-uncle(Person,Uncle).           Algorithm : An uncle is a parent's brother.
%?-nephew(Person,Nephew).         Algorithm : A nephew is a sibling's son.
%?-niece(Person,Niece).           Algorithm : A niece is a sibling's daughter.
%?-descendant(Person,Descendant). Algorithm : A descendant is an offspring or 
%                                             a descendant's offspring.
%?-ancestor(Person,Ancestor).	  Algorithm : The inverse of descendant.
%?-relation(Person,Relation).     Algorithm : Descendants of most distant
%                                             ancestor.
%=============================================================================
%----The Sutcliffe family tree
%----Marriage indicated by +
%----
%----             unknown1+unknown2    unknown3+unknown4
%----                     |                    |
%----unknown7+unknown8    -------bob+maggie-----    unknown9+unknow0
%----        |                     /|\                      |
%----      chris+jill-------------- | ---------------geoff+jade
%----          /                    |
%----         |   unknown5+unknown6 |
%----       jimi          |         |
%----                     sharon + jim
%----                           / \
%----                     gareth   anthony
%----
%-----------------------------------------------------------------------------
%----Facts about people in the family tree. These facts have the
%----semantics person(<mother>,<father>,<name>,<sex>).
person(bob,male,unknown1,unknown2).
person(maggie,female,unknown3,unknown4).
person(jill,female,bob,maggie).
person(jim,male,bob,maggie).
person(geoff,male,bob,maggie).
person(chris,male,unknown7,unknown8).
person(sharon,female,unknown5,unknowm6).
person(jade,female,unknown9,unknown0).
person(jimi,male,chris,jill).
person(gareth,male,jim,sharon).
person(anthony,male,jim,sharon).
%-----------------------------------------------------------------------------
%----To find the siblings of a person. Same parents.
sibling(Person,Sibling):-
    person(Person,_,Father,Mother),
    person(Sibling,_,Father,Mother),
    Person \== Sibling.
%-----------------------------------------------------------------------------
%----To find the brothers of a person. Male sibling
brother(Person,Brother):-
    sibling(Person,Brother),
    person(Brother,male,_,_).
%-----------------------------------------------------------------------------
%----To find the sisters of a person. Female sibling
sister(Person,Sister):-
    sibling(Person,Sister),
    person(Sister,female,_,_).
%-----------------------------------------------------------------------------
%----To find the father of a person.
father(Person,Father):-
    person(Person,_,Father,_).
%-----------------------------------------------------------------------------
%----To find the mother of a person.
mother(Person,Mother):-
    person(Person,_,_,Mother).
%-----------------------------------------------------------------------------
%----Parent is either mother or father
parent(Person,Parent):-
    father(Person,Parent).

parent(Person,Parent):-
    mother(Person,Parent).
%-----------------------------------------------------------------------------
%----To find the offspring of a person. Reverse parent.
offspring(Person,Offspring):-
    parent(Offspring,Person).
%-----------------------------------------------------------------------------
%----To find the sons of a person. A male offspring
son(Person,Son):-
    offspring(Person,Son),
    person(Son,male,_,_).
%-----------------------------------------------------------------------------
%----To find the daughters of a person. A female offspring
daughter(Person,Daughter):-
    offspring(Person,Daughter),
    person(Daughter,female,_,_).
%-----------------------------------------------------------------------------
%----To find the aunts of a person. The sister of a parent.
aunt(Person,Aunt):-
    parent(Person,Parent),
    sister(Parent,Aunt).
%-----------------------------------------------------------------------------
%----To find the uncles of a person. The brother of a parent.
uncle(Person,Uncle):-
    parent(Person,Parent),
    brother(Parent,Uncle).
%-----------------------------------------------------------------------------
%----To find the nephews of a person. Sons of siblings
nephew(Person,Nephew):-
    sibling(Person,Sibling),
    son(Sibling,Nephew).
%-----------------------------------------------------------------------------
%----To find the nieces of a person. Daughters of siblings
niece(Person,Niece):-
    sibling(Person,Sibling),
    daughter(Sibling,Niece).
%-----------------------------------------------------------------------------
%----To find the descendants of a person. First case is for immediate 
%----descendants, second is for descendants of the immediate descendants.
descendant(Person,Descendant):-
    offspring(Person,Descendant).

descendant(Person,Descendant):-
    offspring(Person,Offspring),
    descendant(Offspring,Descendant).
%-----------------------------------------------------------------------------
%----To find the ancestors of a person. Reverse descendant.
ancestor(Person,Ancestor):-
    descendant(Ancestor,Person).
%-----------------------------------------------------------------------------
%----To find the relations of a person, find the descendants of the
%----most distant ancestors. Note, unless you have a hermaphrodite at the
%----top, and not inter-breeding, some people will be listed multiple times.
relation(Person,Relation):-
    most_distant_ancestor(Person,Relation).

relation(Person,Relation):-
    most_distant_ancestor(Person,MostDistantAncestor),
    descendant(MostDistantAncestor,Relation),
    Person \== Relation.
%-----------------------------------------------------------------------------
%----Find any most distant ancestor
most_distant_ancestor(Person,MostDistantAncestor):-
    parent(Person,Parent),
    !,
    most_distant_ancestor(Parent,MostDistantAncestor).

most_distant_ancestor(Person,Person).
%-----------------------------------------------------------------------------
%=============================================================================
