For Programmers: Free Programming Magazines  


Home > Archive > Prolog > July 2006 > Liars puzzle









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Liars puzzle
sbrown

2006-07-29, 8:00 am

Hello,

I have tried writing a program in SWI-Prolog to solve the Liars puzzle
from chapter 4 of SICP,
but it doesn't work. Could anyone please tell me what is wrong with
this program?

% Liars puzzle from SICP chapter 4
:- use_module(library('bounds')).
xor(X,Y) :- X, not(Y);not(X),Y.
liars([Betty,Ethel,Joan,Kitty,Mary]) :-
Girls = [Betty,Ethel,Joan,Kitty,Mary],
Girls in 1..5,
all_different(Girls),
xor(Kitty = 2,Betty = 3),
xor(Ethel = 1,Joan = 2),
xor(Joan = 3,Ethel = 5),
xor(Kitty = 2,Mary = 4),
xor(Mary = 4,Betty = 1),
label(Girls).

Markus Triska

2006-07-29, 6:59 pm

Hi,

"sbrown" <doolagarl2002@yahoo.com.au> writes:

> but it doesn't work. Could anyone please tell me what is wrong with
> this program?


\+/1 ("not/1") is not negation from logic.

You can generate ground instances and place the tests afterwards
("generate and test"):


xor(X, Y) :- (X, \+ Y) ; (\+ X, Y).

liars(Girls) :-
Girls = [Betty,Ethel,Joan,Kitty,Mary],
Girls in 1..5,
all_different(Girls),
label(Girls),
xor(Kitty = 2, Betty = 3),
xor(Ethel = 1, Joan = 2),
xor(Joan = 3, Ethel = 5),
xor(Kitty = 2, Mary = 4),
xor(Mary = 4, Betty = 1).


or, and preferably, use the inequality constraint #\=/2 from the
FD-solver:


xor(A = B, C = D) :- A #= B, C #\= D.
xor(A = B, C = D) :- A #\= B, C #= D.


liars(Girls) :
Girls = [Betty,Ethel,Joan,Kitty,Mary],
Girls in 1..5,
all_different(Girls),
xor(Kitty = 2, Betty = 3),
xor(Ethel = 1, Joan = 2),
xor(Joan = 3, Ethel = 5),
xor(Kitty = 2, Mary = 4),
xor(Mary = 4, Betty = 1),
label(Girls).


?- liars(Ls).

Ls = [3, 5, 2, 1, 4]


All the best,
Markus Triska.
sbrown

2006-07-29, 6:59 pm

Okay, it's working now, thank you.

Markus Triska wrote:
> Hi,
>
> "sbrown" <doolagarl2002@yahoo.com.au> writes:
>
>
> \+/1 ("not/1") is not negation from logic.
>
> You can generate ground instances and place the tests afterwards
> ("generate and test"):
>
>
> xor(X, Y) :- (X, \+ Y) ; (\+ X, Y).
>
> liars(Girls) :-
> Girls = [Betty,Ethel,Joan,Kitty,Mary],
> Girls in 1..5,
> all_different(Girls),
> label(Girls),
> xor(Kitty = 2, Betty = 3),
> xor(Ethel = 1, Joan = 2),
> xor(Joan = 3, Ethel = 5),
> xor(Kitty = 2, Mary = 4),
> xor(Mary = 4, Betty = 1).
>
>
> or, and preferably, use the inequality constraint #\=/2 from the
> FD-solver:
>
>
> xor(A = B, C = D) :- A #= B, C #\= D.
> xor(A = B, C = D) :- A #\= B, C #= D.
>
>
> liars(Girls) :
> Girls = [Betty,Ethel,Joan,Kitty,Mary],
> Girls in 1..5,
> all_different(Girls),
> xor(Kitty = 2, Betty = 3),
> xor(Ethel = 1, Joan = 2),
> xor(Joan = 3, Ethel = 5),
> xor(Kitty = 2, Mary = 4),
> xor(Mary = 4, Betty = 1),
> label(Girls).
>
>
> ?- liars(Ls).
>
> Ls = [3, 5, 2, 1, 4]
>
>
> All the best,
> Markus Triska.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com