Karim BELABAS on Wed, 8 Nov 2000 20:05:34 +0100 (MET)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: grammar question ?


[Nils Skoruppa:]
> I was not sure whether the following should be posted
> here or in pari-user, so please be patient if you feel
> this is not the right place.

It's definitely the right place.

> When meditating about the grammar (and the implementation) of gp
> I had the feeling that it is quite inconsistent that constructs
> like
> 
> 	for( v=[];j=0, 10, v=concat(v,j^2))
> 
> (i.e. constructs like
> 	for( x=seq1, seq2, seq3))
> with seq* beeing a syntactically correct gp expression)
> are not accepted by the parser.
> Is there any deeper reason for this ?

It's not a generic assignment: it's actually deferred until the loop body
is evaluated. And it works so that the loop index is automatically local to
the loop, i.e is restored to its former value when the loop ends. After

  x = 0; for (x=1, 10, print(x))

the value of x is 0.

It's probably a bad design idea. I myself really would prefer x to be equal
to 11 at the end of the above loop. Unfortunately it's a major compatibility
issue: changing it could break many scripts depending on that (documented)
behaviour. Eg:

  f() = for(i=1,, g())
  g() = for(i=1,, ...)

IF i is declared local to g, everything is fine. Otherwise...

So we're stuck with this delayed assignment scheme. On the other hand it is
quite easy, to modify the way the iterators are implemented so as to accept

  for(a seq of assignments, MAX, loop body)

so that the assignments are all local to the loop. It looks like a good
idea since local() declarations can currently only occur right at the start
of a function declaration and one may need temporary variables for the sake
of a single loop. Internally the parsing code would become IGI: Input
position1 = assignment block, GEN = max value of FIRST (say) defined
variable, Input position2 = loop body.

Unfortunately your example:

 	for( v=[];j=0, 10, v=concat(v,j^2))

still wouldn't work as intended since v would be reset upon termination of
the loop... After all what's wrong with

        v = []
 	for(j=0, 10, v=concat(v,j^2))

??? [besides the fact that vector(11, j, (j-1)^2) would be much more
efficient]

Cheers,

    Karim.

P.S: For the sake of easier library programming, the iterators are going to
be modified so as to use generic C functions and not character strings (in
version 2.2.0.alpha :-). I could also include the (trivial) modification
described above since it wouldn't break any existing script. It doesn't
look _that_ useful, but it would be slightly more consistent...
-- 
Karim Belabas                    email: Karim.Belabas@math.u-psud.fr
Dep. de Mathematiques, Bat. 425
Universite Paris-Sud             Tel: (00 33) 1 69 15 57 48
F-91405 Orsay (France)           Fax: (00 33) 1 69 15 60 19
--
PARI/GP Home Page: http://www.parigp-home.de/