
father(tom,bryan).
father(tom,marcus).
father(john,tom).
father(john,otelia).

mother(stephanie,bryan).
mother(stephanie,marcus).
mother(mary,stephanie).
mother(mary,ted).
mother(mary,boobie).
mother(mary,nolansr).

male(bryan).
male(tom).
male(marcus).
male(john).
male(ted).
male(boobie).
male(nolan).

female(stephanie).
female(mary).
female(otelia).



is_mother(X) :- mother(X,Y).

is_father(X) :- father(X,Y).

is_son(X) :- father(Y,X), male(X).

sister_of(X,Y) :- sibling(X,Y), female(X).

grandpa_of(X,Y) :- is_father(Z,Y), is_father(X,Z); is_mother(Z,Y), 
is_father(X,Z).

/* Or:
grandpa_of(X,Y) :- is_father(Z,Y), is_father(X,Z).
grandpa_of(X,Y) :- is_mother(Z,Y), is_father(X,Z).
*/

grandma_of(X,Y) :- is_mother(Z,Y), is_mother(X,Z); is_father(Z,Y), 
is_mother(X,Z).

sibling(X,Y) :- is_mother(M,X), is_mother(M,Y), X\==Y; is_father(F,X), 
is_father(F,Y), X\==Y.

/* Or, better:
sibling(X,Y) :- is_mother(M,X), is_mother(M,Y), is_father(F,X), is_father(F,Y),
X\==Y.
*/

/*
Error here!
aunt(X,Aunt) :- is_mother(M,X), sister_of(M,Aunt), female(Aunt); 
is_father(F,X), sister_of(F,Aunt), female(Aunt).
Use the following instead:*/

aunt(X,Y) :- female(X), sibling(X,Z), parent(Z,Y).


% del( Element, List1, List2) if List2 is List1 with one occurrence of
% Element deleted

del( X,[X|T],T).
del( X,[Y|T1],[Y|T2]) :- del( X,T1,T2).

sort1( X,Y) :- permutation( X,Y), sorted(Y).

sorted( []).
sorted( [_]).
sorted( [X | [Y | Ys]]) :- X =< Y, sorted( [Y|Ys]). 

permutation( [],[]).
permutation( X,[Y|Ys]) :- del(Y,X,Z), permutation(Z,Ys).




























