0

I have tried using a code similar to this link:

Solving a textual logic puzzle in Prolog - Find birthday and month

The problem I'm trying to solve is this(Telephone Conversations). http://www.cis.upenn.edu/~matuszek/cis554-2012/Assignments/prolog-01-logic-puzzle.html

My code:

dated(Date):-
member(Date,[1928,1929,1932,1935]).
exchanged(Exchange):-
member(Exchange,[al,be,pe,sl]).

solve(X):-
X=[[gertie,Exchange1,Date1],
   [herbert,Exchange2,Date2],
   [miriam,Exchange3,Date3],
   [wallace,Exchange4,Date4]],

exchanged(Exchange1), exchanged(Exchange2), exchanged(Exchange3), exchanged(Exchange4),
Exchange1 \== Exchange2, Exchange1 \== Exchange3, Exchange1 \== Exchange4,
Exchange2 \== Exchange1, Exchange2 \== Exchange3, Exchange2 \== Exchange4,
Exchange3 \== Exchange1, Exchange3 \== Exchange2, Exchange3 \== Exchange4,
Exchange4 \== Exchange1, Exchange4 \== Exchange2, Exchange4 \== Exchange3,

dated(Date1), dated(Date2), dated(Date3), dated(Date4),
Date1 \== Date2, Date1 \== Date3, Date1 \== Date4,
Date2 \== Date1, Date2 \== Date3, Date2 \== Date4,
Date3 \== Date1, Date3 \== Date2, Date3 \== Date4,
Date4 \== Date1, Date4 \== Date2, Date4 \== Date3,

%Herbet's first exchange wasn't for BE
Exchange2 \== be,

%The Person whose first exchange was SL wasn't Getie or Herbert
Exchange1 \== sl,
Exchange2 \== sl,

%The person whose first exchange was BE didn't get the phone in 1935
member([_,be, \+1935], X),

%The person who got the first phone in 1932 didn't have an exchange for AL or BE
member([_, \+al, 1932], X),
member([_, \+be, 1932],X),

%The person who got the first phone in 1928 had an exchange for PE
member([_,pe,1929], X),

%Wallace first exchange was AL
Exchange4 == al.

My problem is this:

?- solve(X).
 false.
Community
  • 1
  • 1

2 Answers2

1

So your problem is, your solve predicate doesn't find any solutions. This means, one of the prerequisites for finding a solution fails for all possible paths in the solution tree.

Did you actually try to search which one it is? Surely not, otherwise you would have noticed that this:

member([_,be,\+1935],X)

always fails. Why? What is \+/1? "\+ :Goal is true if Goal cannot be proven". In other words, you cannot use \+ for matching. Instead, you could write:

\+ member([_,be,1935),X).

So with all the corrections:

?- solve(X).
X = [[gertie, be, 1928], [herbert, pe, 1929], [miriam, sl, 1932], [wallace, al, 1935]] ;
false.

Assuming that the rest of the program is correct.

It is really bad to use stackoverflow as an alternative to debugging your code.

  • Excuse me for not knowing how to exactly debug using SWI prolog. I am totally new to declarative programming! Thanks for your reply - Yes I tried searching it also – Alex Cutajar Apr 06 '13 at 23:05
  • @AlexCutajar I tried to show the way I found what was wrong with your program, so that you can do it yourself. And really, no matter the language, spotting problems in your programs is something you just have to learn how to do. –  Apr 07 '13 at 01:11
0

Instead of

exchanged(Exchange1), exchanged(Exchange2), exchanged(Exchange3), exchanged(Exchange4),
Exchange1 \== Exchange2, Exchange1 \== Exchange3, Exchange1 \== Exchange4,
Exchange2 \== Exchange1, Exchange2 \== Exchange3, Exchange2 \== Exchange4,
Exchange3 \== Exchange1, Exchange3 \== Exchange2, Exchange3 \== Exchange4,
Exchange4 \== Exchange1, Exchange4 \== Exchange2, Exchange4 \== Exchange3,

dated(Date1), dated(Date2), dated(Date3), dated(Date4),
Date1 \== Date2, Date1 \== Date3, Date1 \== Date4,
Date2 \== Date1, Date2 \== Date3, Date2 \== Date4,
Date3 \== Date1, Date3 \== Date2, Date3 \== Date4,
Date4 \== Date1, Date4 \== Date2, Date4 \== Date3,

you can write

permutation([al,be,pe,sl], [Exchange1, Exchange2, Exchange3, Exchange4]),
permutation([1928,1929,1932,1935], [Date1, Date2, Date3, Date4]),
joel76
  • 5,565
  • 1
  • 18
  • 22