There are four baskets, and each with a unique color. I write a prolog program to tell the color oder based on some facts and rules. This is the .pl file:
iright(L, R, [L | [R | _]]).
iright(L, R, [_ | Rest]) :- iright(L, R, Rest).
nextto(L, R, List) :- iright(L, R, List).
nextto(L, R, List) :- iright(R, L, List).
myprogram(Data) :- =(Data, [_,red,_,_]),
\+nextto(red,blue,Data), % blue is not next to red
iright(red,green,Data), %green is right to red
member(red,Data),
member(blue,Data),
member(green,Data),
member(yellow,Data).
The iright and nextto predicates are correct. My query is myprogram(Data)
, and I expect the result should be
Data = [yellow,red, green, blue]?
yes
but actually the prompt show that
| ?- myprogram(Data).
no
I know the problem is the negation, but idk how and why. Plz help.
When I use trace.
1 1 Call: myprogram(_16) ?
2 2 Call: \+nextto(red,blue,[_46,red,_50,_52]) ?
3 3 Call: nextto(red,blue,[_46,red,_50,_52]) ?
4 4 Call: iright(red,blue,[_46,red,_50,_52]) ?
5 5 Call: iright(red,blue,[red,_50,_52]) ?
5 5 Exit: iright(red,blue,[red,blue,_52]) ?
4 4 Exit: iright(red,blue,[_46,red,blue,_52]) ?
3 3 Exit: nextto(red,blue,[_46,red,blue,_52]) ?
2 2 Fail: \+nextto(red,blue,[_46,red,_50,_52]) ?
1 1 Fail: myprogram(_16) ?
(2 ms) no
trace.
at the Prolog prompt. – Fred Foo