首页 > 解决方案 > When does Prolog prompts 'yes' and when does it says 'true'

问题描述

I wrote the following knowledge base in Prolog:

likes(yamini,chocolate).
likes(anuj,apple).
likes(yamini,book).
likes(john,book).
likes(john,france).

Now, when I consult the above file and try the following commands:

| ?- likes(anuj,apple).      

(1 ms) yes
| ?- likes(yamini,chocolate).

true ? ;

no

I want to understand when does Prolog replies a 'yes' versus when does it replies 'true'.

标签: prologprolog-toplevelgnu-prolog

解决方案


This is an artefact of the toplevel (Prolog command line) implementation. Apparently you implementation says true when it could prove a goal and it is unsure whether there may be more solutions. If it is sure that there is only one solution, it says yes.

Here is what SWI-Prolog does:

?- likes(anuj,apple). 
true.

Prolog could successfully prove the goal likes(anuj,apple) and it is also sure there are no other ways to prove it otherwise one would see something like this:

?- member(X,[1,2]).
X = 1 ; 
X = 2.

where an X that makes the goal true has been found as 1 but there may be other solutions. And indeed there are, namely 2.

Back to our example:

?- likes(yamini,chocolate).
true.

推荐阅读