Blocks, scoping, gensym() and compiling



  On 8/14/2010 12:20 PM, Sergio Callegari wrote:
> Hi,
>
> I have recently asked on the ml about blocks and modules.
>
> I have received from Richard Fateman the suggestion to use gensym(), 
> as in
>
> foo(y):=block([x:?gensym()],find_root(exp(x)+x=y,x,0,1));
>
> [now that gensym is a first class citizen, the "?" can be omitted too!]
>
> Unfortunately, I am now experiencing problems when trying to compile 
> the above:
>
> foo(y):=block([x:?gensym()],mode_declare(x,float),find_root(exp(x)+x=y,x,0,1)); 
>
>
> compile(foo)
>
> This complains about the fact that x (declared as float) is assigned 
> the gensym stuff (of type any).
>
> What is the best way to fix this issue?
1. Don't compile it, since it will not run any faster after compiling.  
It is, after all, just a call to find_root.
2. You have, indeed, tried to declare the wrong thing, namely "x",  and 
not the gensym associated with x, so
the declaration is not meaningful, and is in fact wrong as the error 
message says.  So remove the declaration,
if you insist on compiling it.
3. Even if you left x alone, e.g.   
foo(y):=find_root(exp(x)+x=y,x,0,1),  I think  x would STILL not be a 
float, but a symbol,
as far as I can tell.

Compilation is helpful when you have a loop or recursion calling 
programs in the maxima language doing
floating-point arithmetic.  Otherwise, it is not going to make much 
difference.  the evaluator, simplifier, find_root
etc, run at the same speed whether called from interpreted or compiled code.

>
> Would the compilation be fine with the scoping construct (blex) 
> suggested by Robert Dodier?
You could probably find more productive ways of spending your time than 
trying to compile this.
RJF

>
> Thanks in advance,
>
> Sergio
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima