
girl(abigail).
girl(brenda).
girl(mary).
girl(paula).
girl(tera).

month(february).
month(march).
month(june).
month(july).
month(december).

day(sunday).
day(monday).
day(wensday).
day(friday).
day(saturday).

solve :-
	month(Abigail_month),month(Brenda_month),month(Mary_month),month(Paula_month),month(Tera_month),

	all_different([Abigail_month,Brenda_month,Mary_month,Paula_month,Tera_month]),

	day(Abigail_day),day(Brenda_day),day(Mary_day),day(Paula_day),day(Tera_day),

	all_different([Abigail_day,Brenda_day,Mary_day,Paula_day,Tera_day]),

	Birthdays = [birthday(abigail,Abigail_month,Abigail_day),birthday(brenda,Brenda_month,Brenda_day),birthday(mary,Mary_month,Mary_day),birthday(paula,Paula_month,Paula_day),birthday(tera,Tera_month,Tera_day)],
% Rule 1
	member(birthday(paula,march,_),Birthdays),
	\+member(birthday(paula,_,saturday),Birthdays),
	\+member(birthday(abigail,_,friday),Birthdays),
	\+member(birthday(abigail,_,wensday),Birthdays),

% Rule 2
	\+member(birthday(brenda,february,_),Birthdays),
	\+member(birthday(brenda,_,monday),Birthdays),
	\+member(birthday(mary,february,_),Birthdays),
	\+member(birthday(mary,_,monday),Birthdays),

% Rule 3
	\+member(birthday(tera,february,_),Birthdays),
	((member(birthday(tera,_,saturday),Birthdays));(member(birthday(tera,_,sunday),Birthdays))),

% Rule 4
	\+member(birthday(mary,december,_),Birthdays),
	\+member(birthday(mary,_,monday),Birthdays),
	\+member(birthday(mary,_,wensday),Birthdays),
	\+member(birthday(mary,_,friday),Birthdays),
	member(birthday(_,june,sunday),Birthdays),

% Rule 5
	\+member(birthday(tera,december,_),Birthdays),
	\+member(birthday(brenda,_,friday),Birthdays),
	\+member(birthday(mary,july,_),Birthdays),

% print results

	tell(abigail,Abigail_month,Abigail_day),
	tell(brenda,Brenda_month,Brenda_day),
	tell(mary,Mary_month,Mary_day),
	tell(paula,Paula_month,Paula_day),
	tell(tera,Tera_month,Tera_day).

	tell(Girl, Month, Day) :- write(Girl), write(' was born in '), write(Month),
    write(' on '), 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([_]).


