Single Prolog Variable Warning
I'm new to Prolog and confused! I keep getting "singleton variable for warning [WMAPDY]". I read somewhere that sometimes this warning is useless. I also read that the program won't compile all articles because of the warning?
The program I'm trying to make is a crypto-arithmetic puzzle that should "solve" AM + PM = DAY.
If anyone can help with this error, and also that the singlon change warning is always important, I would really 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.
source to share
A warning is generated because of this line:
select(Y,WMAPD,WMAPDY),
The program does not use the variable WMAPDY
anywhere else, so it is useless and Prolog warns you about it because it is most likely a typo (it is not). To get rid of the warning, you have some options:
-
Use
member/2
insteadselect/3
, since you are not interested in the result list:member(Y,WMAPD)
. -
Mark the variable as singleton. If you start with the variables
_
, they are not checked, they are odnotochiyami:select(Y, WMAPD,_WMAPDY)
. In addition, you can use a special variable The singleton_
:select(Y,WMAPD,_)
. (This description is at least true for SWI Prolog, the overridden variable_WMAPDY
can work with a lot of dialects). -
Use
:- style_check(-singleton)
in your file. This disables all singleton variable warnings for the file, I would rather not use this because this warning is useful for finding typos. (this description also applies to SWI Prolog, SICStus Prolog can use the optionsingle_var_warnings
, for other systems, check your manual).
source to share
Why not use clpfd?
Building on my previous answer to a very closely related question , we are asking:
?- solve_n_dump([A,M] + [P,M] #= [D,A,Y]).
Eq = ([2,5]+[9,5]#=[1,2,0]), Zs = [2,5,9,1,0].
Eq = ([2,7]+[9,7]#=[1,2,4]), Zs = [2,7,9,1,4].
Eq = ([2,8]+[9,8]#=[1,2,6]), Zs = [2,8,9,1,6].
Eq = ([3,5]+[9,5]#=[1,3,0]), Zs = [3,5,9,1,0].
Eq = ([3,6]+[9,6]#=[1,3,2]), Zs = [3,6,9,1,2].
Eq = ([3,7]+[9,7]#=[1,3,4]), Zs = [3,7,9,1,4].
Eq = ([3,8]+[9,8]#=[1,3,6]), Zs = [3,8,9,1,6].
Eq = ([4,5]+[9,5]#=[1,4,0]), Zs = [4,5,9,1,0].
Eq = ([4,6]+[9,6]#=[1,4,2]), Zs = [4,6,9,1,2].
Eq = ([4,8]+[9,8]#=[1,4,6]), Zs = [4,8,9,1,6].
Eq = ([5,6]+[9,6]#=[1,5,2]), Zs = [5,6,9,1,2].
Eq = ([5,7]+[9,7]#=[1,5,4]), Zs = [5,7,9,1,4].
Eq = ([5,8]+[9,8]#=[1,5,6]), Zs = [5,8,9,1,6].
Eq = ([6,5]+[9,5]#=[1,6,0]), Zs = [6,5,9,1,0].
Eq = ([6,7]+[9,7]#=[1,6,4]), Zs = [6,7,9,1,4].
Eq = ([7,5]+[9,5]#=[1,7,0]), Zs = [7,5,9,1,0].
Eq = ([7,6]+[9,6]#=[1,7,2]), Zs = [7,6,9,1,2].
Eq = ([7,8]+[9,8]#=[1,7,6]), Zs = [7,8,9,1,6].
Eq = ([8,5]+[9,5]#=[1,8,0]), Zs = [8,5,9,1,0].
Eq = ([8,6]+[9,6]#=[1,8,2]), Zs = [8,6,9,1,2].
Eq = ([8,7]+[9,7]#=[1,8,4]), Zs = [8,7,9,1,4].
true.
source to share