I have just started learning Prolog, and I'm wondering about the first question of this exercise.
%% Suppose we are working with the following knowledge base:
wizard(ron).
hasWand(harry).
quidditchPlayer(harry).
wizard(X) :- hasBroom(X), hasWand(X).
hasBroom(X) :- quidditchPlayer(X).
How does Prolog respond to the following queries?
wizard(ron). -> true
witch(ron). -> undefined procedure
wizard(hermione). -> false
witch(hermione). -> undefined procedure
wizard(harry). -> true
wizard(Y). -> Y = ron ; Y = harry.
witch(Y). -> undefined procedure
Using swipl
on Ubuntu, importing the knowledge base for this exercise, first of course trying to decipher what Prolog will returns, and finally checking by myself.
Ok pretty boring stuff until now, I have seen a few answer to these exercises over Github (here, here and there), and I don't understand the answer to the first one: %% 1. wizard(ron). -> true
.
First of all the interpreter is complaining about the two definition of what is a wizard
:
Warning: /tmp/prolog/ex15.pl:4:
Clauses of wizard/1 are not together in the source-file
Earlier definition at /tmp/prolog/ex15.pl:1
Current predicate: quidditchPlayer/1
Use :- discontiguous wizard/1. to suppress this message
Secondly, when querying I obtain:
?- wizard(ron).
true ;
false.
The way I get it, first Prolog returns the first fact from the knowledge base, then apply the rule head and find out that ron has neither a broom nor a wand.
All this leading to my question: what subtlety have I missed that makes others writing true
as an answer to this query?
true
given there is at least one path that returnstrue
. – Willem Van Onsemexercise.pl
you point to is poorly structured. It defineswizard(ron).
, a couple of other different facts, thenwizard(X) :- ...
. That means the definition ofwizard
is discontiguous and many Prolog interpreters will ignorewizard(X)
at this point. You need to keep all yourwizard(_)
definitions next to each other. – lurkerwizard(ron)
holds iftrue
orfalse
."); and "wizard(X)
holds ifX = harry
orX = ron
". – Willem Van OnsemthatUsingMixedCaps
is typicallya_lot_less_readable_than
using underscores, which are therefore used in idiomatic Prolog code instead! – mat