0

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.

Community
  • 1
  • 1
Sajib Acharya
  • 1,666
  • 5
  • 29
  • 54
  • 1
    Ha, "buggy". This must be the laziest attempt at not explaining I have yet heard of. Rather, if you get "singleton" warnings, your code is buggy. But please don't tell this to your teacher :) –  Nov 24 '15 at 21:34
  • @Boris, I lost respect for this teacher the moment he told me that. I am only getting to understand Prolog from learnprolognow.com and I realize he has taught me nothing. I am seriously complaining to higher authorities. – Sajib Acharya Nov 24 '15 at 21:47

2 Answers2

3

All questions you ask are answered in the questions you link to.

See in particular this answer, explaining clearly the reason why SWI emits a warning and different ways to avoid it. It even links to the manual that shows that SWI behaves exactly as documented and intended.

See also this answer to get more information that also applies in this case: Maybe it's time to leave the 80s behind and use more general arithmetic, so that you can use the code in all directions.

In short: As explained in the other answers, mark the variable as singleton to avoid the (typically very useful) warning.

Community
  • 1
  • 1
mat
  • 40,498
  • 3
  • 51
  • 78
3

Technically, you don't have to get rid of the singleton warning if you really intended the code as written. Just like in other languages, it's possible for warnings to be ignored if you really know what you're doing. The warnings are there because the compiler or interpreter is letting you know that something may not be correct. Often, the compiler or interpreter is correct, and it's discovered an error on the part of the programmer (e.g., misspelled variable). Or it could be a sign of brittle code that might work now, but could fail under corner case conditions, or with slight changes around it.

It's highly recommended that you get rid of warnings so that your programming logic intentions are clear for you and for anyone else who may have to read your code and be assured that you did something intentionally and not in error. Ideally, the only time you'd allow a warning to remain is if you can't get rid of it with any clear programming construct. The existence of warnings is a general sign of an untidy or, more extremely, sloppy program.

In this particular case, power(_, 0, 1). much better expresses the logic than power(X, 0, 1).. power(_, 0, 1). says that, Any variable, taken to the power 0, yields 1. Rather than, power(X, 0, 1). which says A variable X, taken to the power 0, yields 1 which begs the question, Is there something specific about X that hasn't been stated here?

Of course, we have been ignoring the undefined case of 0 taken to a 0 power, so really, it should be power(X, 0, 1) :- X =\= 0. :)

lurker
  • 56,987
  • 9
  • 69
  • 103
  • and I am sure my Teacher is really wrong about SWI being buggy? – Sajib Acharya Nov 24 '15 at 17:07
  • @SajibAcharya all software has some bugs, but, no, I would not call SWI "buggy". If your teach says it is buggy, you should ask for specific examples illustrating why that is so. – lurker Nov 24 '15 at 23:14