Problem with `\ +` in Prolog Requests with Variables

I am reading "Seven Languages ​​in Seven Weeks" and I am at a standstill over some Prolog request that I do not understand the answer is "no."

The file is friends.plas follows:

likes(wallace, cheese).
likes(grommit, cheese).
likes(wendolene, sheep).

friend(X, Y) :- \+(X = Y), likes(X, Z), likes(Y, Z).

I can fulfill some trivial queries, for example:

| ?- ['friends'].
compiling /home/marc/btlang-code/code/prolog/friends.pl for byte code...
/home/marc/btlang-code/code/prolog/friends.pl compiled, 12 lines read - 994 bytes written, 8 ms

yes
| ?- friend(wallace,grommit).

yes
| ?- friend(wallace,wendolene).

no

This is all as expected. Now I want to enter a variable in the query. My intention is that Prolog will give me a list of all Wallace's friends. I expect X = grommit, but I get no:

| ?- trace.
The debugger will first creep -- showing everything (trace)

yes
{trace}
| ?- friend(wallace,X).
      1    1  Call: friend(wallace,_16) ?
      2    2  Call: \+wallace=_16 ?
      3    3  Call: wallace=_16 ?
      3    3  Exit: wallace=wallace ?
      2    2  Fail: \+wallace=_16 ?
      1    1  Fail: friend(wallace,_16) ?

no
{trace}

He doesn't even try to combine X( _16) with grommit. What for?

+5
source share
4 answers

This is a friend’s definition:

friend(X, Y) :- \+(X = Y), likes(X, Z), likes(Y, Z).

, \+(X = Y), :

\+ Goal :- Goal,!,fail

, , , . (, ) , , , . , X Y, .

friend(X, Y) :-  likes(X, Z), likes(Y, Z), \+(X = Y)

, .

, , . " " , . .

+4

JF :

" " , .

: . , , dif/2, Prolog.

+4

friend/2 - \+(X = Y). , X = Y, . =/2 unify/2, . , , , , friend(wallace, gromit), wallace gromit ; ​​ , , X = Y , \+(X = Y) , .

+3

: X ( grommit ). Prolog , , . likes(grommit, Z) Z, , likes. grommit , Prolog . , friend , , .

+1

All Articles