4

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.

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.
pppery
  • 3,731
  • 22
  • 33
  • 46
NoobCoderChick
  • 617
  • 3
  • 21
  • 40
  • 2
    it's not the warning that's useless, it's the variable ! – CapelliC Dec 09 '14 at 00:56
  • So I should take out the variable? – NoobCoderChick Dec 09 '14 at 01:02
  • 1
    Specifically, in your expression, `select(Y,WMAPD,WMAPDY)`, the `WMAPDY` variable isn't used anywhere else. In other words, your `select` provides a result that you don't use. If you don't need that `select`, then remove it. If you need the result of that `select`, then use it. – lurker Dec 09 '14 at 01:04

2 Answers2

6

The warning is generated because of this line:

select(Y,WMAPD,WMAPDY),

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:

  1. Use member/2 instead of select/3, since you aren't interested in the resulting list: member(Y,WMAPD).

  2. 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).

  3. 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 option single_var_warnings, for other systems, check your manual).

Here is the relevant section in the SWI-Prolog manual

repeat
  • 18,496
  • 4
  • 54
  • 166
Patrick J. S.
  • 2,885
  • 19
  • 26
  • Patrick you are awesome, thanks once again for answering my questions! You are a prolog master! – NoobCoderChick Dec 09 '14 at 06:15
  • Whither variables starting with an underscore (and, of course, excluding the anonymous variable, `_`) are not reported as singletons **depends** on the Prolog system. In some cases, it also depends if the letter following the underscore is lower case or upper case (e.g. SWI-Prolog reports for the `a(_x,_Y,a).` clause `_x` as a singleton). – Paulo Moura Dec 09 '14 at 12:30
  • I'll add that in, I knew from OP's previous question, that he uses SWI. – Patrick J. S. Dec 09 '14 at 16:26
3

Why not use ?

Building on my previous answer to a very related question, we query:

?- 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.
Community
  • 1
  • 1
repeat
  • 18,496
  • 4
  • 54
  • 166