Before marking this question as duplicate, please note that I have gone through similar questions here, here at SO but my question is a bit different.
I am new to Prolog and here is a simple code to calculate the result of a number to the power of another. (X^Y)
Here is the code. power.pl
:
power(X,0,1).
power(X,Y,Z):- Y > 0, Y1 is Y-1, power(X,Y1,Z1), Z is X*Z1.
power(X,Y,Z):- Y < 0, Y1 is -1*Y, Y2 is Y1-1, X1 is 1/X, power(X1,Y2,Z1), Z is X*Z1.
While I try to run this using SWI-PROLOG
with the command [power].
it gives me a Singleton variable: [X]
warning.
I found out (from here) that if I use power(_,0,1).
at the first line, then things just work fine.
This code is provided by my Teacher who really does not explain stuff. But I got to learn basic prolog from learnprolognow.org. My teacher says that the warning is not a headache as SWI-PROLOG is buggy and keeps giving such warnings all the time. I really do not buy this. I mean, it is used so widely, can this be so buggy? There must be a reason this warning is shown.
My question is:
1 - power(X,0,1)
is a fact
that I am stating as far as I understand from learnprolognow.org, so, why is this warning given? Does prolog
expect me to use a variable
while stating a fact
?
2 - Should I really be worried about this warning
, or just let this happen. I am not really comfortable when a warning is shown. It is a warning for a reason.
Sorry, if I am missing any basic concepts on this matter. I am new to it. Thank you.