% 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 :-
    day(AbigailDay), day(BrendaDay), day(MaryDay), day(PaulaDay), day(TaraDay),
    all_different([AbigailDay, BrendaDay, MaryDay, PaulaDay, TaraDay]),
    
    month(AbigailMonth), month(BrendaMonth), month(MaryMonth), month(PaulaMonth), month(TaraMonth),
    all_different([AbigailMonth, BrendaMonth, MaryMonth, PaulaMonth, TaraMonth]),
    
    
            
    Dates = [ date(abigail, AbigailDay, AbigailMonth),
              date(brenda, BrendaDay, BrendaMonth),
              date(mary, MaryDay, MaryMonth),
              date(paula, PaulaDay, PaulaMonth),
              date(tara, TaraDay, TaraMonth) ], 

    % 1. Paula was born in March but not on Saturday. Abigail's birthday was not on Friday or Wednesday.
    member(date(paula, _, march), Dates),
    \+ member(date(paula, sunday, _), Dates),
    \+ member(date(abigail, friday, _), Dates),
    \+ member(date(abigail, wednesday, _), Dates),
    
    % 2. The girl whose birthday is on Monday was born earlier in the year than Brenda and Mary.
    member(date(_, monday, _), Dates),
    \+ member(date(brenda, _, july), Dates),

    % 3. Tara wasn't born in February and her birthday was on the weekend.
    \+ member(date(tara, _, february), Dates),
    (member(date(tara, saturday, _), Dates); member(date(tara, sunday, _), Dates)),
    
    % 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(date(mary, _, december), Dates),
    (member(date(mary, saturday, _), Dates); member(date(mary, sunday, _), Dates)),
    member(date(_, sunday, june), Dates),
    
    % 5. Tara was born before Brenda, whose birthday wasn't on Friday. Mary wasn't born in July.
    \+ member(date(brenda, friday, _), Dates),
    \+ member(date(mary, _, july), Dates),


    tell(abigail, AbigailDay, AbigailMonth),
    tell(brenda, BrendaDay, BrendaMonth),
    tell(mary, MaryDay, MaryMonth),
    tell(paula, PaulaDay, PaulaMonth),
    tell(tara, TaraDay, TaraMonth).
    
tell(Person, Day, Month) :- write(Person), write('\'s birthday is in '), write(Month),
    write(' on a '), write(Day), 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([_]).


% #2 and #5!