[GAP Forum] an inconsistent syntax error

Frank Lübeck frank.luebeck at math.rwth-aachen.de
Mon Mar 2 11:01:28 GMT 2009


On Wed, Feb 11, 2009 at 02:19:23PM +0600, Anvita wrote:
> I am getting a syntax error when copy-and-paste
> this simple program in a newly opened GAP session.
> 
> ----------------------------
> q:=true;
> while q do
>  m:=2;
>  q:=First(Primes,s->s=m)<>2;
> od;
> ----------------------------
> 
> The error is as follows:
> 
> ---------------------------------------------
> gap> q:=true;
> true
> gap> while q do
> >  m:=2;
> >  q:=First(Primes,s->s=m)<>2;
> Syntax error: warning: unbound global variable
>  q:=First(Primes,s->s=m)<>2;
>                        ^
> > od;
> ----------------------------------------------
> 
> Yet "m" appears to be a global variable afterwards:
> 
> -------
> gap> m;
> 2
> gap>
> -------
> 
> On the other hand, a similar program (in which the variable "m"
> is also defined for the first time within the while loop) runs fine:
> 
> ---------------
> gap> q:=true;
> true
> gap> while q do
> >  m:=2;
> >  q:=m<>2;
> > od;
> gap>
> ---------------
> 
> Could this be another bug?
> 

Dear Anvita, dear Forum,

GAP issues the warning
     Syntax error: warning: unbound global variable
when it parses a function and finds a reference to an unbound global
variable. This is often a useful warning, namely when a user has forgotten
to declare that variable as local to the function. The warning can be
ignored when that global variable was meant and is actually bound when 
the parsed function is called.

Of course, sometimes one wants to refer to a global variable; maybe the most
common case is to use another function which will be defined later
(programers can use separate declaration and implementation files to avoid
warnings in this case). 

Now, what has this to do with the examples above? To understand this you
must know what GAP does when it parses a for- or while-loop. It (almost)
handles it as if there where a 'function()' and 'end' around that code, parses
this function, and then calls it. 

Ok, but why do we *not* see in a new GAP session the following 
unpleasant behaviour:

gap> for i in [1..10] do od;
Syntax error: warning: unbound global variable
for i in [1..10] do od;
       ^
gap>

I said '(almost)' above because there is an exception: That is, as long as the 
parser is not inside a literal function definition the warning is suppressed.

And this does now explain the difference between the two examples you have 
sent:  You see the warning for the occurence of 'm' in the literal function 
definition 's->s=m'.

Sorry, it is a bit complicated. 

Best regards,

   Frank

PS.: In some future GAP version it will be possible for a user to switch 
off these warnings.

-- 
///  Dr. Frank Lübeck, Lehrstuhl D für Mathematik, Templergraben 64,  ///
\\\                    52062 Aachen, Germany                          \\\
///  E-mail: Frank.Luebeck at Math.RWTH-Aachen.De                        ///
\\\  WWW:    http://www.math.rwth-aachen.de/~Frank.Luebeck/           \\\



More information about the Forum mailing list