I'm new to Prolog and royally confused! I keep getting a "singleton variable for [WMAPDY]" warning. I read somewhere that sometimes that warning is useless. I also read that the program will not compile all the clauses because of the warning?
The program I'm trying to do is a crypt-arithmetic puzzle that is supposed to "solve" AM+PM=DAY.
If anyone could help with this error and also wether the singleton variable warning is always important I'd greatly appreciate it!!
Scott
solve([A,M,P,D,Y]):-
select(A,[0,1,2,3,4,5,6,7,8,9],WA), % W means Without
not(A=0),
select(M,WA,WMA),
select(P,WMA,WMAP),
not(P=0),
select(D,WMAP,WMAPD),
not(D=0),
select(Y,WMAPD,WMAPDY),
DAY is 100*D+10*A+Y,
AM is 10*A+M,
PM is 10*P+M,
DAY is AM+PM.
Why not use clpfd?
Building on my previous answer to a very related question, we query:
The warning is generated because of this line:
The program doesn't use the variable
WMAPDY
anywhere else, thus it is useless, and Prolog warns you about it, because it is likely a typo (it isn't in this case). To get rid of the warning you have some possibilities:Use
member/2
instead ofselect/3
, since you aren't interested in the resulting list:member(Y,WMAPD)
.Mark the variable as a singleton. If you start variables with a
_
they wont be checked it they are singletons:select(Y, WMAPD,_WMAPDY)
. Alternatively, you could use the special singleton variable_
:select(Y,WMAPD,_)
. (This description is at least true for SWI Prolog, the underscored variable_WMAPDY
might work with more dialects).Use
:- style_check(-singleton)
in your file. This turns off all singleton variable warnings for the file, I'd rather not use that, because this warning is good for finding typos. (this desciption also is for SWI Prolog, SICStus Prolog may use the optionsingle_var_warnings
, for other systems, check your manual).Here is the relevant section in the SWI-Prolog manual