Discussion:
Singleton variable in branch: right warning message?
Parker Jones
2014-03-17 12:32:01 UTC
Permalink
This code gives the warning "Singleton variable in branch: A", but shouldn't it be a regular singleton warning?


p(X) :-
(X > 0 ->
A=1
;
A=0
).
I spent time trying to figure out
which branch was to blame (in more complex code), when in fact the branches are fine, it's just
that the variable isn't used afterwards.


p(X) :-
(X > 0 ->
A=1
;
A=0
),
write(A). % use variable A and the warning goes away.

Cheers,
Parker

-------------- next part --------------
HTML attachment scrubbed and removed
Michael Hendricks
2014-03-17 14:43:07 UTC
Permalink
On Mon, Mar 17, 2014 at 6:32 AM, Parker Jones <***@hotmail.com> wrote:

> This code gives the warning "Singleton variable in branch: A", but
> shouldn't it be a regular singleton warning?
>
>
> p(X) :-
> (X > 0 ->
> A=1
> ;
> A=0
> ).


It's not a regular singleton warning because the variable A appears twice
in this clause. This code has two branches: X > 0 and X =< 0. Simplifying
you have roughly:

P(X) :- A=1. % when X > 0
P(X) :- A=0. % when X =< 0

You can see that A is a singleton (appears only once) in each of those
clauses (each representing a branch).

By adding write(A), you add a second occurrence of the variable A to each
branch, so it's no longer a singleton.

--
Michael
-------------- next part --------------
HTML attachment scrubbed and removed
Parker Jones
2014-03-17 15:27:13 UTC
Permalink
Thank you for the explanation, Michael. It makes sense now.

> Date: Mon, 17 Mar 2014 08:43:07 -0600
> From: ***@ndrix.org
> To: ***@hotmail.com
> CC: swi-***@lists.iai.uni-bonn.de
> Subject: Re: [SWIPL] Singleton variable in branch: right warning message?
>
> On Mon, Mar 17, 2014 at 6:32 AM, Parker Jones <***@hotmail.com> wrote:
>
> > This code gives the warning "Singleton variable in branch: A", but
> > shouldn't it be a regular singleton warning?
> >
> >
> > p(X) :-
> > (X > 0 ->
> > A=1
> > ;
> > A=0
> > ).
>
>
> It's not a regular singleton warning because the variable A appears twice
> in this clause. This code has two branches: X > 0 and X =< 0. Simplifying
> you have roughly:
>
> P(X) :- A=1. % when X > 0
> P(X) :- A=0. % when X =< 0
>
> You can see that A is a singleton (appears only once) in each of those
> clauses (each representing a branch).
>
> By adding write(A), you add a second occurrence of the variable A to each
> branch, so it's no longer a singleton.
>
> --
> Michael
> -------------- next part --------------
> HTML attachment scrubbed and removed
> _______________________________________________
> SWI-Prolog mailing list
> SWI-***@lists.iai.uni-bonn.de
> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

-------------- next part --------------
HTML attachment scrubbed and removed
Loading...