From: Andy Wingo <wingo@pobox.com>
To: ludo@gnu.org (Ludovic Courtès)
Cc: guile-devel@gnu.org
Subject: Re: [VM] Tail recursion and multiple values
Date: Sun, 01 Mar 2009 21:31:35 +0100 [thread overview]
Message-ID: <m363itkms8.fsf@pobox.com> (raw)
In-Reply-To: <8763iuhb7k.fsf@gnu.org> ("Ludovic Courtès"'s message of "Sat, 28 Feb 2009 15:45:19 +0100")
Hey Ludo!
On Sat 28 Feb 2009 15:45, ludo@gnu.org (Ludovic Courtès) writes:
> ludo@gnu.org (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).
This is a misunderstanding.
Last things first: code is not run through the VM unless it is compiled.
The REPL in the vm branch compiles expressions by default, though it has
an option to use the interpreter instead (,option interp #t).
So if you are running this code via e.g. guile -s foo.scm, the code in
foo.scm is evaluated with the interpreter. Sometimes this is faster, in
that the compiler doesn't have to be loaded up -- see the recent numbers
that I posted. It depends on what foo.scm does.
> In this case, `loop' is an interpreter procedure while
> `call-with-values' is a program.
Just as you cannot have tail recursion between interpreted code and
primitive (C-compiled) code, you cannot have tail recursion between
VM and interpreted (or primitive) code.
Multiple values actually doesn't have anything to do with this, except
for one thing -- r4rs.scm defines call-with-values like this:
(define (call-with-values producer consumer)
(@call-with-values producer consumer))
@call-with-values is a primitive understood to the interpreter. In this
way the interpreter preserves tail recursion, not only for calls to
call-with-values, but also (apply call-with-values ...). Indeed, call/cc
and even `apply' have similar definitions in this file.
So what I really mean to say is:
1) It is expected that you don't have tail recursion between
interpreted and VM code.
2) This particular problem manifests itself in that call-with-values
is VM code (when r5rs.scm is compiled).
3) The strategy used by r5rs.scm is actually not bad, as it handles
the `apply' case well.
If we really want to preserve tail recursion in this case, we could add
hacks to the interpreter, e.g. to recognize VM programs that are eq? to
call-with-values as being the same as @call-with-values; but the
interpreter already has enough hacks. Better to make loading the
compiler faster, so we can just compile by default.
Cheers,
Andy
--
http://wingolog.org/
next prev parent reply other threads:[~2009-03-01 20:31 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-02-28 14:27 [VM] Tail recursion and multiple values Ludovic Courtès
2009-02-28 14:45 ` Ludovic Courtès
2009-03-01 20:31 ` Andy Wingo [this message]
2009-03-01 23:48 ` Ludovic Courtès
2009-03-02 18:03 ` Andy Wingo
2009-03-02 21:55 ` Ludovic Courtès
2009-03-02 23:15 ` Andy Wingo
2009-03-02 23:33 ` Andreas Rottmann
2009-03-02 23:43 ` Eduardo Cavazos
2009-03-03 23:45 ` Ludovic Courtès
2009-03-04 22:11 ` Clinton Ebadi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m363itkms8.fsf@pobox.com \
--to=wingo@pobox.com \
--cc=guile-devel@gnu.org \
--cc=ludo@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).