[email protected] (Ludovic Courtès) writes:
> Use of multiple values breaks tail recursion in VM-compiled code:
>
> (let loop ((x 1000000))
> (and (> x 0)
> (call-with-values
> (lambda ()
> (values (1+ x) (1- x)))
> (lambda (next prev)
> (loop prev)))))
Actually no: it works with VM-compiled code, but it breaks when using
Guile-VM with `,o interp #t' (which appears to be the default, except at
the REPL).
In this case, `loop' is an interpreter procedure while
`call-with-values' is a program. It's the implementation of `goto/args'
in this particular case that seems to break tail recursion:
if (!SCM_FALSEP (scm_procedure_p (x)))
{
POP_LIST (nargs);
SYNC_REGISTER ();
sp[-1] = scm_apply (x, sp[0], SCM_EOL);
The `scm_apply ()' call introduces a new stack frame instead of re-using
the current one.
At this point I'm not sure how to fix it. We need better integration of
calls from the VM to subrs/procedures anyway (notably removing
`POP_LIST ()' and argument consing before subr/procedure calls!) so
probably we could postpone this issue until then. Andy?
Thanks,
Ludo'.