yes/no return in PROLOG query
Asked Answered
D

2

7

I'm just learning PROLOG and I'm having a difficult time understanding why the queries that I'm performing result in an ending of 'yes/no'. Here's my KB:

male(albert).
male(bob).
male(bill).
male(carl).
male(charlie).
male(dan).
male(edward).

female(alice).
female(betsy).
female(diana).

parent(albert, bob).
parent(albert, betsy).
parent(albert, bill).

parent(alice, bob).
parent(alice, betsy).
parent(alice, bill).

parent(bob, carl).
parent(bob, Charlie).

I then perform the following query:

 ?-female(X).

which returns:

X = alice ? ; 
X = betsy ? ;
X = diana
yes

When I query:

 ?-parent(X, bob).

I'm returned:

 X = albert ?;
 X = alice ?;
 no

Why does one result in 'yes', and the other in 'no'?

Debus answered 14/4, 2018 at 18:7 Comment(1)
In Prolog you have predicates which are not functions. That means when you call one, it is a query which will succeed (possibly multiple times with more than one solution) or fail (no solutions). In this case 'yes' means success and 'no' means fail. When Prolog prompts with ? after a solution, that means it has more options to explore, which may or may not succeed. If you press ;, Prolog attempts the option and, if falis to find additional solutions, says "no". If you don't get the ? prompt, that means there was no choice point and Prolog just responds 'yes'.Venitavenite
M
0

It depends on the Prolog system how the top level shows answer substitution. Especially whether the toplevel can detect deterministic query success. The later means a query succeeded and did not leave some choice points.

For example GNU-Prolog, SWI-Prolog, Jekejeke Prolog can all detect deterministic query success, and wont prompt the end-user whether he wants further answer substitutions. Since when there are no choice points, then there will be only a further answer no.

With deterministic query success detection:

?- female(X).
X = alice ;
X = betsy ;
X = diana.

Without deterministic query success detection:

?- female(X).
X = alice ;
X = betsy ;
X = diana ;
no

There is actually no Prolog system that would show a yes as in you show in your post after the last semicolon. This is impossible and I would consider the Prolog system broken.

Morley answered 4/11, 2018 at 17:27 Comment(1)
“There is actually no Prolog system that would show a yes as in you show in your post after the last semicolon.” This system exists and is called “GNU Prolog”. | ?- female(X). X = diana (1 ms) yes | ?- Harping
R
0

As lurker already explained, the question is whether the Prolog system knows that there is no further answer or it does not know (yet).

  • If the Prolog system knows that there is no further answer, and there was at least one answer, it prints yes after the last answer and does not wait for you to decide whether you would like another answer (since there is none).
  • If the Prolog system does not know whether there are more answers, you can press ";" to let Prolog search for more answers. If it does not find one, it will print "no", because it failed to satisfy your modified query ("are there more answers?").

In the case of your query female(X), the program clauses were exhausted: There are no more clauses about female. Therefore, it was clear that there cannot be more answers.

In the case of the query parent(X, bob), there are more facts about parent when the system found the answer X=alice. The Prolog system just stopped in the proof execution at this point. It does NOT first compute all answers and then shows them one by one.

When you pressed ";", Prolog continues with the next fact parent(alice, betsy). However, the second argument of the predicate does not match, therefore it tries the following fact and so on. In the end, all facts about parent are exhausted, without any other answer found. Therefore Prolog reports failure by printing "no".

Most Prolog systems have index structures to speed up search. Therefore, you might notice that not always all rules are tried. For instance, you might check parent(alice, X). Depending on your Prolog system, it might be that Prolog immediately stops after the three answers. This would show that it has an index on the (outermost functor of the) first argument of every predicate, which is quite common.

Rodriques answered 10/11, 2021 at 21:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.