Karim BELABAS on Tue, 25 Feb 2003 11:50:25 +0100 (MET)


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

Re: gp: variables


On Fri, 21 Feb 2003, Michael Somos wrote:
> ? test(v)=x=v[1];y=v[2];0
> ? for(x=1,1,for(y=3,4,print(x," ",y," ",[x,y]," ",test([x,y]))));
> 1 3 [1, 3] 0
> 4 4 [1, 4] 0
> ? x=1;y=3;print(x," ",y," ",[x,y]," ",test([x,y]));
> 3 3 [1, 3] 0
[...]
> As a further test along the lines I tried the following :
>
> ? test(v)= x=v[1]; y=v[2]; y=(y); fordiv(y/2,X,); return("abcdef");
> ? forstep(x=1,1,2,forstep(y=10,12,2,print(x" "y" ",[x,y]," ",test([x,y]))))
> 1 10 [1, 10] abcdef
> abcdef 12 [1, 12] abcdef
>
> Seems like a bug to me.

Calling a function as f(x, x = y) is syntactically correct, but should not be
used [ this is implicitly what you do, through side effects of subroutines ].
Basically, it depends on the order in which the arguments are evaluated.
The result is undefined [ actually you could get a SEGV here ].
Of course, _defining_ a function in this way to provide default arguments is
OK.


There _is_ a bug in PARI here, in that variables may have too short a
lifespan: variables values are passed as reference to a cloned "durable"
object, not as copies. If this object is affected, so are the values. This
is OK in x = y, since the assignment makes a copy of the y-value into x, but
it definitely is not in function arguments.

And anyway the possibility of SEGV is annoying...

I do not know how to correct this without a major slowdown [ always make full
copies, whenever referencing a variable value ]. Adding reference counts
wouldn't help, since objects components are modified, without the parent
object being able to tell about it.

Cheers,

    Karim.
-- 
Karim Belabas                    Tel: (+33) (0)1 69 15 57 48
Dép. de Mathématiques, Bât. 425  Fax: (+33) (0)1 69 15 60 19
Université Paris-Sud             Email: Karim.Belabas@math.u-psud.fr
F-91405 Orsay (France)           http://www.math.u-psud.fr/~belabas/
--
PARI/GP Home Page: http://www.parigp-home.de/