1
votes

I tested this code out. I solved it logically without the use of prolog and the answer was different. I cannot understand where the mistake is! These are my clues:

Mary Smith put all five wedding gifts from each of her five pairs of aunts and uncles into one stack and only later did she realise that she’d absent-mindedly put all five cards into a single pile on top of the stack and couldn’t remember which aunt and uncle had sent which gift. Each pair of aunt (Amber, Emily, Janet, Maisie and Patsy) and uncle (Bill, Doug, Nick, Tom or Zack) live in a different state (including Iowa) and sent a different item (one is a vase). Can you match each aunt and uncle pair with their respective state and gift?

  1. Aunt Maisie’s husband is Uncle Nick.
  2. Uncle Bill and Aunt Emily and their respective spouses sent, in some order, the salver and the urn.
  3. Aunt Amber and Uncle Doug live in Georgia.
  4. Aunt Patsy and her husband sent the toaster.
  5. The rotissererie came from the Delaware relatives.
  6. Uncle Zack and his wife (who don’t live in Kansas) sent neither the salver nor the toaster.
  7. Aunt Janet and Uncle Bill live in California.

This is My logic reasoning:

Solving the problem logically to check if answers are correct. According to the clues:

Five Aunts: Amber, Emily, Janet, Maisie, Patsy.

Five Uncles: Bill, Doug, Nick, Tom, Zack.

Five States: California, Delaware, Georgia, Iowa, Kansas.

Five Gifts: Rotiseserie, Salver, Urn, Toaster, Vase

According to the first clue, we can join Maisie and Nick together. According to the third, we can also join Amber and Doug together and lvive in Georgia. We can also join Janet and Bill, living in California, according to the seventh clue.

FOUND: Maisie & Nick are together.

FOUND: Amber & Doug are together and live in Georgia.

FOUND: Janet & Bill are together and live in California.

According to clue 5 we can say that Amber and Doug did not give the rotissererie as they live in Georgia, not Delaware. We can also say that for Janet and Bill, who live in California.

FOUND: Rotissererie was not sent by neither Amber & Doug nor Janet & Bill.

We are left with Patsy and Emily. Patsy sent the toaster according to clue 4, so joint with clue 6, she is not Zack’s wife. Then we have found that Patsy is Tom’s wife while Zack is Emily’s husband, as we already know all of the others. We also know, according to clue 6 that Zack does not live in Kansas.

FOUND: Patsy & Tom are together

FOUND: Emily & Zack are together.

Either the Janet couple or the Emily couple sent the salver and the urn by clue two. We know that the Emily couple did not send the salver by the sixth clue, so that gives us another two facts, that the Emily couple sent the urn, while the Janet couple sent the salver.

FOUND: Emily & Zack sent the urn.

FOUND: Janet & Bill sent the salver. [janet, bill, california, salver].

According to clue 4, the Patsy couple sent the toaster. Thus another one ruled out of Delaware.

FOUND: Patsy & Tom sent the Toaster and do not live in Delaware.

Since Amber and Doug live in Georgia, we can say that they have sent the vase.

FOUND: Amber & Doug sent the vase. [amber, doug, georgia, vase].

Now we can say that Maisie and Nick sent the rotissererie as it is the only one left. Therfore, they live in Delaware.

FOUND: Maisie & Nick sent the Rotisserie and live in Delaware. [maisie, nick, delaware, rotissererie].

The sixth clue tells us that the Emily couple does not live in Kansas. So the only one left is Iowa. This also gives us naturally our last answer, that the Patsy couple lives in Kansas.

FOUND: Emily & Zack live in Iowa [emily, zack, iowa, urn].

FOUND: Patsy & Tom live in Kansas [patsy, tom, kansas, toaster].

This is my prolog code:

livesIn(State):-
member(State,[california,georgia,delaware,iowa,kansas]).

sentGift(Gift):-
member(Gift,[rotisserie,salver,toaster,urn,vase]).

isHusband(Husband):-
member(Husband,[bill,doug,nick,tom,zack]).

solve(Z):-

Z=[[amber,Husband1,State1,Gift1],
   [emily,Husband2,State2,Gift2],
   [janet,Husband3,State3,Gift3],
   [maisie,Husband4,State4,Gift4],
   [patsy,Husband5,State5,Gift5]],

isHusband(Husband1), isHusband(Husband2), isHusband(Husband3), isHusband(Husband4), isHusband(Husband5),
Husband1 \== Husband2, Husband1 \== Husband3, Husband1 \== Husband4, Husband1 \== Husband5,
Husband2 \== Husband1, Husband2 \== Husband3, Husband2 \== Husband4, Husband2 \== Husband5,
Husband3 \== Husband1, Husband3 \== Husband2, Husband3 \== Husband4, Husband3 \== Husband5,
Husband4 \== Husband1, Husband4 \== Husband2, Husband4 \== Husband3, Husband4 \== Husband5,
Husband5 \== Husband1, Husband5 \== Husband2, Husband5 \== Husband3, Husband5 \== Husband4,

livesIn(State1), livesIn(State2), livesIn(State3), livesIn(State4), livesIn(State5),
State1 \== State2, State1 \== State3, State1 \== State4, State1 \== State5,
State2 \== State1, State2 \== State3, State2 \== State4, State2 \== State5,
State3 \== State1, State3 \== State2, State3 \== State4, State3 \== State5,
State4 \== State1, State4 \== State2, State4 \== State3, State4 \== State5,
State5 \== State1, State5 \== State2, State5 \== State3, State5 \== State4,

sentGift(Gift1), sentGift(Gift2), sentGift(Gift3), sentGift(Gift4), sentGift(Gift5),
Gift1 \== Gift2, Gift1 \== Gift3, Gift1 \== Gift4, Gift1 \== Gift5,
Gift2 \== Gift1, Gift2 \== Gift3, Gift2 \== Gift4, Gift2 \== Gift5,
Gift3 \== Gift1, Gift3 \== Gift2, Gift3 \== Gift4, Gift3 \== Gift5,
Gift4 \== Gift1, Gift4 \== Gift2, Gift4 \== Gift3, Gift4 \== Gift5,
Gift5 \== Gift1, Gift5 \== Gift2, Gift5 \== Gift3, Gift5 \== Gift4,

%Aunt Maisie's Husband is Uncle Nick

Husband4 = nick,

%Uncle Bill and Aunt Emily and their respective spouses sent the salver and the urn (in some order)


member([_, bill, _, salver], Z); member([_, bill, _, urn], Z),
Gift2 = salver; Gift2 = urn,

%Aunt Amber and Uncle Doug live in Georgia

Husband1 = doug,
State1 = georgia,

%Aunt Patsy and her husband sent a toaster

Gift5 = toaster,

%The rotisserie came from the Delaware relatives

member([_, _, delaware, rotisserie], Z),

%Uncle Zack and his wife(don't live in Kansas) sent neither the salver nor the toaster

\+ member([_, zack, kansas, _], Z),
\+ member([_, zack, _, salver], Z),
\+ member([_, zack, _, toaster], Z),

%Aunt Janet and Uncle Bill live in California

Husband3 = bill,
State3 = california.
1
There must a mistake in the logic somewhere...user1812457
I am quite sure I answered your question here. stackoverflow.com/a/16206695/1400768nhahtdh

1 Answers

0
votes

You need parenthesis around disjunctions, because (;)/2 precedence is higher that (,)/2

%Uncle Bill and Aunt Emily and their respective spouses sent the salver and the urn (in some order)
( member([_, bill, _, salver], Z); member([_, bill, _, urn], Z) ),
( Gift2 = salver; Gift2 = urn ),

but the logic is not correct: I would try instead

( member([BillWife, bill, _, BillGift], Z),
  member([emily, EmilyHusband, _, EmilyGift], Z),
  ( BillGift = salver, EmilyGift = urn
  ; BillGift = urn, EmilyGift = salver),
  BillWife \= emily, EmilyHusband \= bill
),

I think this condition isn't what is intended from the clue. I would write

%Uncle Zack and his wife(don't live in Kansas) sent neither the salver nor the toaster
member([_, zack, ZackState, _], Z), ZackState \= kansas,
\+ member([_, zack, ZackState, salver], Z),
\+ member([_, zack, ZackState, toaster], Z),

Style note: these code chunks

isHusband(Husband1), isHusband(Husband2), isHusband(Husband3), isHusband(Husband4), isHusband(Husband5),
Husband1 \== Husband2, Husband1 \== Husband3, Husband1 \== Husband4, Husband1 \== Husband5,
...

are really unreadable, therefore there is high probability to put a mistake. The functionality required is all_different/1 from CLP(FD), but equivalently - except for performance - you can do with

...
permutation([bill,doug,nick,tom,zack],[Husband1,Husband2,Husband3,Husband4,Husband5]),
...

HTH