I’m using: SWI-Prolog version 7.6.4
I want the code to: output all solutions.
But what I’m getting is: duplicates and wrong solutions
My code looks like this:
%
% 8 friends are sitting at a round table.
%
seating(Friends) :-
length(Friends, 8),
member(bruce, Friends), % One of the friends is Bruce
opposite(alice, david, Friends), % Alice is sitting directly opposite of David
inbetween(greta, henry, eugene, Friends), % Henry is sitting between Greta and Eugene.
inbetween(greta, X, claire, Friends),
member(X, Friends), % There is one person between Greta and Claire.
leftof(eugene, david, Friends), % Eugene is sitting immediately to David's left.
next(franny, Y, Friends), % Franny is not next to Alice or David.
dif(alice, Y), dif(david, Y).
opposite(X, Y, Friends) :- Friends = [X, _, _, _, Y, _, _, _].
opposite(X, Y, Friends) :- Friends = [_, X, _, _, _, Y, _, _].
opposite(X, Y, Friends) :- Friends = [_, _, X, _, _, _, Y, _].
opposite(X, Y, Friends) :- Friends = [_, _, _, X, _, _, _, Y].
opposite(Y, X, Friends) :- Friends = [X, _, _, _, Y, _, _, _].
opposite(Y, X, Friends) :- Friends = [_, X, _, _, _, Y, _, _].
opposite(Y, X, Friends) :- Friends = [_, _, X, _, _, _, Y, _].
opposite(Y, X, Friends) :- Friends = [_, _, _, X, _, _, _, Y].
inbetween(L, M, R, Friends) :- leftof(L, M, Friends), leftof(M, R, Friends).
inbetween(L, M, R, Friends) :- leftof(R, M, Friends), leftof(M, L, Friends).
next(X, Y, Friends) :- leftof(X, Y, Friends).
next(X, Y, Friends) :- leftof(Y, X, Friends).
leftof(X, Y, Friends) :- nextto(X, Y, Friends).
leftof(X, Y, Friends) :- Friends = [Y|_], last(X, Friends).
I’m getting:
$ prolog seating.pl
Welcome to SWI-Prolog (threaded, 64 bits, version 7.6.4)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit http://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- seating(H).
H = [bruce, franny, claire, alice, greta, henry, eugene, david] ;
H = [bruce, franny, claire, alice, greta, henry, eugene, david] ;
H = [franny, bruce, claire, alice, greta, henry, eugene, david] ;
H = [franny, bruce, claire, alice, greta, henry, eugene, david] ;
H = [franny, bruce, claire, alice, greta, henry, eugene, david] ;
H = [franny, bruce, claire, alice, greta, henry, eugene, david]
...
The second solution is wrong since franny is sitting next to David (it’s a round table).
My Prolog is very rusty - I haven’t done it in some 20 years. I would appreciate any help.
The constraints of the puzzle are in comments.
I’m particularly confused by the dif/2
predicate, but even without it, it’s not properly backtracking through all solutions.