Currently I'm exercising the Prolog chapter of seven languages in seven weeks. I tried to change the coloring example, in order to not write down every valid color combination.
different(red, green).
different(green, red).
different(red, blue).
different(blue, red).
different(blue, green).
different(green, blue).
coloring(A, M, G, T, F) :-
different(M, T),
different(M, A),
different(A, T),
different(A, M),
different(A, G),
different(A, F),
different(G, F),
different(G, T).
I changed the file to make different a functionpredicate:
color(red).
color(blue).
color(green).
different(X, Y) :- color(X), color(Y), \+(X=Y).
different_fail(X, Y) :- \+(X=Y), color(X), color(Y).
coloring(A, M, G, T, F) :-
different(M, T),
different(M, A),
different(A, T),
different(A, M),
different(A, G),
different(A, F),
different(G, F),
different(G, T).
What I don't get is that different
gives me all combinations of different colors, but different_fail
does only check if they are really different.
GNU Prolog 1.4.5 (32 bits)
Compiled Feb 23 2015, 08:36:31 with gcc
By Daniel Diaz
Copyright (C) 1999-2015 Daniel Diaz
| ?- ['color'].
compiling /home/rudi/7langs/prolog/color.pl for byte code...
/home/rudi/7langs/prolog/color.pl compiled, 17 lines read - 2206 bytes written, 7 ms
| ?- different(red, red).
no
| ?- different(red, blue).
yes
| ?- different_fail(red, red).
no
| ?- different_fail(red, blue).
Up to here everything works as expected.
yes
| ?- different(red, A).
A = blue ? ;
A = green
yes
| ?- different_fail(red, A).
no
But here I would expect that different_fail
yields the exact results as different
.
What is the difference in these functions, which cause my different_fail
to fail?