1

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'.

false
  • 10,264
  • 13
  • 101
  • 209
yamini goel
  • 519
  • 2
  • 10
  • 23

2 Answers2

1

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.
David Tonhofer
  • 14,559
  • 5
  • 55
  • 51
  • Incidentally, the "goal" should really have been called "conjecture" back in the 70s, shouldn't it? We want to make the conjecture a theorem... – David Tonhofer Feb 08 '21 at 18:44
1

That output depends on the toplevel you are using. It seems you are using GProlog. It will output yes when the query succeeds and there are no choice points left (no other possible solutions).

And it will answer true when the query succeeds but there are still other possible solutions to be checked.

gusbro
  • 22,357
  • 35
  • 46
  • 1
    perhaps the implementers thought `yes ?` would seem argumentative. :) – Will Ness Feb 08 '21 at 19:00
  • But `likes(yamini,chocolate).` has just one answer... I mean `chocolate` isn't a variable, then why would prolog think there could be other solutions...? – yamini goel Feb 08 '21 at 23:55
  • @yaminigoel: I believe GProlog is using first argument indexing so when proving the first clause it only "checks" whether there are any remaining clauses with first argument `yamini` which there exists (the third clause), therefore it leaves a choicepoint. If you query `likes(yamini, book)` instead it will answer `yes` because when it proves the third clause there are no remaining clauses with first argument `yamini`. – gusbro Feb 09 '21 at 03:29