在 Prolog 中使用匿名变量和 "normal" 变量时的不同答案

Different answers when using anonymous variable and "normal" variable in Prolog

我有以下数据库:

vegetarian(jose).
vegetarian(james).
vegetable(carrot).
vegetable(egg_plant).
likes(jose,X):-vegetable(X).
loves(Who,egg_plant):-vegetarian(Who).

当我查询 vegetarian(_). 时,我期望得到 _ = jose; _ = james. 但我得到的却是 true; true.

如果我改为 vegetarian(X).,那么我会得到预期的答案 X = jose; X = james. 为什么会有这种差异?

如果您使用的是 SWI-Prolog,您可以使用标志 toplevel_print_anon) 来控制它。但是,由单个下划线 (_) 组成的名称是特殊的,从不打印。

$ swipl dd.pl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.16)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- set_prolog_flag(toplevel_print_anon, true).
true.

?- vegetarian(_X).
_X = jose ;
_X = james.

?- vegetarian(_).
true ;
true.

?- set_prolog_flag(toplevel_print_anon, false).
true.

?- vegetarian(_X).
true ;
true.