% Solution to the Five Sisters logic puzzle.

% Facts
month(february).
month(march).
month(june).
month(july).
month(december).

sister(abigail).
sister(brenda).
sister(mary).
sister(paula).
sister(tara).

day(sunday).
day(monday).
day(wednesday).
day(friday).
day(saturday).

solve :-
	 month(AbigailMonth), month(BrendaMonth), month(MaryMonth),
    month(PaulaMonth), month(TaraMonth),
    all_different([AbigailMonth, BrendaMonth, MaryMonth, PaulaMonth, TaraMonth]),
    
    day(AbigailDay), day(BrendaDay), day(MaryDay), day(PaulaDay), day(TaraDay),
    all_different([AbigailDay, BrendaDay, MaryDay, PaulaDay, TaraDay]),
    
    
            
    Birthdays = [ birthday(abigail, AbigailMonth, AbigailDay),
              birthday(brenda, BrendaMonth, BrendaDay),
              birthday(mary, MaryMonth, MaryDay),
              birthday(paula, PaulaMonth, PaulaDay), 
              birthday(tara, TaraMonth, TaraDay) ],

% 1. Paula was born in March but not on Saturday. 
%    Abigail's birthday was not on Friday or Wednesday.
   member(birthday(paula, march, _), Birthdays),
\+ member(birthday(paula, _, saturday), Birthdays),
\+ member(birthday(abigail, _, friday), Birthdays),
\+ member(birthday(abigail, _, wednesday), Birthdays),

% 2. The girl whose birthday is on Monday was born earlier in the year than Brenda and Mary.
%  ?

% 3. Tara wasn't born in February and her birthday was on the weekend.
( \+ member(birthday(tara, february, _), Birthdays),
   ( member(birthday(tara, _, sunday), Birthdays) ) ;
   ( member(birthday(tara, _, saturday), Birthdays) ) ),

% 4. Mary was not born in December nor was her birthday on a weekday. 
%    The girl whose birthday was in June was born on Sunday.
\+ member(birthday(mary, december, _), Birthdays),
\+ member(birthday(mary, _, monday), Birthdays),
\+ member(birthday(mary, _, wednesday), Birthdays),
\+ member(birthday(mary, _, friday), Birthdays), 
   member(birthday(_, june, sunday), Birthdays),

% 5. Tara was born before Brenda, whose birthday wasn't on Friday. Mary wasn't born in July
\+ member(birthday(tara, _, friday), Birthdays),
% ???

tell(birthday(abigail, AbigailMonth, AbigailDay),
     birthday(brenda, BrendaMonth, BrendaDay),
     birthday(mary, MaryMonth, MaryDay),
     birthday(paula, PaulaMonth, PaulaDay), 
     birthday(tara, TaraMonth, TaraDay.
    
tell(Sister, Month, Day) :- write(Sister), write('s '), write('birthday was during the month of '), write(Month),
    write(', on the '), write(Day), write('th'), write('.'), nl.
    
% If the head of the list is in the tail, fail.
all_different([Head|Tail]) :- member(Head, Tail), !, fail.
% Otherwise, check the tail of the list.
all_different([_|Tail]) :- all_different(Tail).
% If there's only one thing in the list, it's fine.
all_different([_]).