compilation of tail recursive functions



Am 15 Feb 2006 um 18:55 hat van.Nek at gmx.net geschrieben:

> Hello all,
> I have recognized, that at Maxima-level the compiler does not make a tail recursive call 
> optimization, at Lisp-level the compiler does.
> Is there a switch I can set to aim a tail recursive call optimization when compiling?
> (I use Windows-Maxima with GCL)
> Thanks
> Volker van Nek


No answer? 	OK, I try to be more precise:

(C1) n_sum_1(n,res):=
  if n=0 then res
  else n_sum_1(n-1,n+res)$
(C2) n_sum_1(1000,0);	--->  Bind stack overflow.

(C3) compile_file( "C:\\home\\maxima\\test.mc");

(C4) load( "C:\\home\\maxima\\test.o");

(C6) n_sum_1(10000,0); 	--->  Bind stack overflow.

That means: Compiling doesn't help. No tail call optimization occurred.

I looked at the translated code:

(PROGN
  (DEFPROP $N_SUM_1 T TRANSLATED)
  (ADD2LNC '$N_SUM_1 $PROPS)
  (DEFMTRFUN ($N_SUM_1 $ANY MDEFINE NIL NIL) ($N $RES)
      (DECLARE (SPECIAL $RES $N))
      (COND
        ((LIKE $N 0) $RES)
        (T ;(SIMPLIFY		<---  I commented out  SIMPLIFY
               (MFUNCTION-CALL $N_SUM_1 (ADD* $N -1) (ADD* $N $RES))))));)
 
( NOW this is a tail recursive function again )

and compiled again --->     Tail-recursive call of $N_SUM_1 was replaced by iteration.
               
loaded again

(C12) n_sum_1(100000000,0);	---> no problem
(D12)                 5000000050000000

any ideas??

what is the need of SIMPLIFY	there?
is there any possibility to aim tail recursion optimization?

Thanks
Volker van Nek