unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Andy Wingo <wingo@pobox.com>
To: Mark H Weaver <mhw@netris.org>
Cc: guile-devel@gnu.org
Subject: Re: Eval, tail calls, (current-module), and backward compatibility
Date: Wed, 18 Jan 2012 22:52:05 +0100	[thread overview]
Message-ID: <87boq0fzlm.fsf@pobox.com> (raw)
In-Reply-To: <87zkdkye86.fsf@netris.org> (Mark H. Weaver's message of "Wed, 18 Jan 2012 14:58:49 -0500")

On Wed 18 Jan 2012 20:58, Mark H Weaver <mhw@netris.org> writes:

> Andy Wingo <wingo@pobox.com> writes:
>
>> "continuation marks".  We might be able to reuse their strategy in our
>> with-fluids implementation.
>
> I don't see how continuation marks could solve this problem.  They avoid
> adding more frames to the stack, but that's not enough.  The R5RS says:
>
>   A Scheme implementation is properly tail-recursive if it supports an
>   unbounded number of active tail calls.  A call is _active_ if the
>   called procedure may still return.
>
> Therefore, even if you save the old value of (current-module) cleverly
> somewhere other than the stack, these old values would still in general
> use O(n) space, where N is the number of active calls to `eval'.

There's the rub!  I believe that the way this works is to consider every
continuation as having "marks" associated with them.  For example in

  (+ (eval '(eval 'bar mod2) mod1))

the continuation may be written as

  (+ [ ])

where [ ] signifies a hole.  (Consider it to be a previous frame
pointer, coupled with a return address, if you like.)

(current-module) looks at the 'module continuation mark (or equivalent)
on a continuation.  Call-with-continuation-marks does create a new
continuation to pop marks, but only if marks are not set on that
continuation already.  If the continuation already has marks,
call-with-continuation-marks just adds/sets marks on the existing
continuation, instead of creating a new one.

So to translate it back to our world, something like

  (with-fluids ((a 2)) (with-fluids ((a 3)) (tail)))
                       ^ this doesn't create a new continuation, it just
                       overrides the marks on the previous one

It seems to be a dynamic construct (i.e., at runtime, not compile-time),
but it works for the purposes of proper tail calls.

AIUI anyway :)

Andy
-- 
http://wingolog.org/



  reply	other threads:[~2012-01-18 21:52 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-17  3:28 Eval, tail calls, (current-module), and backward compatibility Mark H Weaver
2012-01-17 11:02 ` David Kastrup
2012-01-17 21:02   ` Mark H Weaver
2012-01-18  9:36     ` Andy Wingo
2012-01-18 19:58       ` Mark H Weaver
2012-01-18 21:52         ` Andy Wingo [this message]
2012-01-18 21:18 ` Ludovic Courtès
2012-01-18 22:01   ` Andy Wingo
2012-01-18 22:21     ` Ludovic Courtès
2012-01-18 22:27       ` Andy Wingo
2012-01-18 22:47         ` Ludovic Courtès
2012-01-18 22:56           ` Andy Wingo
2012-01-21 15:59 ` David Kastrup
2012-01-21 18:28   ` Mark H Weaver
2012-01-21 18:33     ` David Kastrup
2012-01-21 19:06       ` Mark H Weaver
2012-01-23 10:41 ` Andy Wingo

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=87boq0fzlm.fsf@pobox.com \
    --to=wingo@pobox.com \
    --cc=guile-devel@gnu.org \
    --cc=mhw@netris.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).