Compiler warnings: Singleton variable in branch

405 views
Skip to first unread message

Steve Moyle

unread,
Jul 10, 2017, 4:22:39 PM7/10/17
to SWI-Prolog
Hi

I am trying to make sense of the compiler warnings for singleton variables in branches.

Here is my simple example (tmp.pl):

a(Term):-
format('Start~n', [Term]),
        (   Term= [A, B, default]
        ->  format('If~n')
        ;   Term = [A, B],
            format('Else~n')
        ).



And here are the warnings.

$ swipl -s tmp.pl
Warning: /home/sam/gits/BroDuce/trunk/src/aleph/library/db_proving/tmp.pl:1:
Singleton variable in branch: A
Singleton variable in branch: B
Welcome to SWI-Prolog (threaded, 64 bits, version 7.4.2)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit http://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- 

Is there something particularly idiomatic in the style of using the branch in tmp.pl or can I make the warnings go away with some special spell? 


Many thanks,

Steve

Barb Knox

unread,
Jul 10, 2017, 7:38:03 PM7/10/17
to SWI-Prolog, Steve Moyle
On 11 Jul 2017, at 08:22, Steve Moyle <katara...@gmail.com> wrote:

Hi

I am trying to make sense of the compiler warnings for singleton variables in branches.

Here is my simple example (tmp.pl):

a(Term):-
format('Start~n', [Term]),
        (   Term= [A, B, default]
        ->  format('If~n')
        ;   Term = [A, B],
            format('Else~n')
        ).


And here are the warnings.

$ swipl -s tmp.pl
Warning: /home/sam/gits/BroDuce/trunk/src/aleph/library/db_proving/tmp.pl:1:
Singleton variable in branch: A
Singleton variable in branch: B
[...]
Is there something particularly idiomatic in the style of using the branch in tmp.pl

It's not idiomatic but structural.  In each branch, both A and B only occur once.

or can I make the warnings go away with some special spell? 

You can replace A and B with _ or with _A and _B respectively.


Many thanks,

Steve


-- 
---------------------------
|  BBB                b    \    Barbara at LivingHistory stop co stop uk
|  B  B   aa     rrr  b     |
|  BBB   a  a   r     bbb   |   ,008015L080180,022036,029037
|  B  B  a  a   r     b  b  |   ,047045,L014114L4.
|  BBB    aa a  r     bbb   |
-----------------------------


Jan Wielemaker

unread,
Jul 11, 2017, 2:44:15 AM7/11/17
to Barb Knox, SWI-Prolog, Steve Moyle
On 07/11/2017 01:37 AM, Barb Knox wrote:
>
> It's not idiomatic but structural. In each branch, both A and B only
> occur once.
>
>> or can I make the warnings go away with some special spell?
>
> You can replace A and B with _ or with _A and _B respectively.

Unfortunately, _A doesn't work. That is because there are two separate
analysers. read_term/2,3 does syntactic analysis and reports two _A
in a term as a warning. The compiler sees an A that doesn't communicate
information because the other A is in a different branch as something to
warn again.

So, _ works fine. There is one other escape: _<digit>... are variables
that are not subject to singleton checking (to avoid reporting on such
generated variable names). So, you can write _0A or something like
that. I also use that in some cases for debug/3 statements like this:

x(...., A),
debug(channel, 'Nice ~p', [A]).

That code raises a warning when compiled using -O as that removes
the debug statement. If you use _A you also get a warning. Using
_0A is the only `solution'.

Cheers --- Jan
Reply all
Reply to author
Forward
0 new messages