I am new in Prolog and I was just thinking that why this rule giving me false result after one true.
likes(1,banana).
likes(1,mango).
test :- likes(1,banana),likes(1,mango).
?- test.
true;
false.
I want to know the reason behind this false.
The way prolog works is by evaluating queries until a fail by negation.
Here, you've established two facts:
likes(1, banana).
which says "1 likes banana"likes(1, mango).
which says "1 likes mango"Then you have established a rule, which basically evaluates as:
left_hand_side :- right_hand_side.
left_hand_side
ifright_hand_side
Evaluation of the rule as a query tries to match facts and returns
true
if it can, and false if it cannot match. One important thing to note is that, if specified, prolog will continue to match facts as long as rules evaluate totrue
.So let's step through
test :- likes(1,banana),likes(1,mango).
If
test
is run as a query, prolog first trieslikes(1,banana)
which is a previously established fact, and is true. Then, it moves on tolikes(1,mango)
which, again, is a fact, and is true. Prolog has then reached the end of the rule, and outputstrue
.At this point, if you're not searching for more matches you can cut the query short and just have true. However, if you're looking for more (all) matches, prolog backtracks and tries to evaluate the rule again, searching for more matches.
However, since your rule is only matching "likes banana and likes mango" and we already matched
likes(1,banana)
, when prolog backtracks and tries evaluatinglikes(1,banana)
again, since we already matched it before, this time there is not another fact (in other words, 1 cannot "like" banana more than once, unless that has been defined) to match. So that's where thefalse
comes from.In your prolog interpreter you may be able to trace the execution of your program by typing
trace.
then running your query. My trace is given below:One last thing to note: If, instead of pressing
;
at thetrue ?
prompt, had I pressed<ENTER>
, the script would have finished with only thetrue
.I'm glad you asked this question 'cause it allowed me a tiny refresher on prolog, which I really like but haven't used in a long time.