我正在读《七周七种语言》atm,我对一些 Prolog 查询感到困惑,我不明白对“否”的回答。
The friends.pl
文件看起来像这样:
likes(wallace, cheese).
likes(grommit, cheese).
likes(wendolene, sheep).
friend(X, Y) :- \+(X = Y), likes(X, Z), likes(Y, Z).
我可以对其进行一些简单的查询,例如:
| ?- ['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
这一切都在预料之中。现在,我想在查询中引入一个变量。我的意图是 Prolog 将为我提供华莱士所有朋友的列表。我期待着X = grommit
,但我得到了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}
它甚至没有尝试统一X
(_16
) with grommit
. Why?