From: Stefan Monnier <monnier@iro.umontreal.ca>
To: emacs-devel@gnu.org
Subject: Re: Delimited continuations
Date: Mon, 11 Dec 2017 11:47:01 -0500 [thread overview]
Message-ID: <jwvefo1nssc.fsf-monnier+gmane.emacs.devel@gnu.org> (raw)
In-Reply-To: 87r2s2cyoj.fsf@web.de
[ For some reason, while I had no trouble getting an intuitive
understanding of continuations and can happily write and reason
about CPS style code, I must admit that I still haven't managed to
really wrap my head around delimited continuations.
You've been warned. ]
> (reset (lambda (p)
> (+ (+ 1 2)
> (shift p (lambda (k) (funcall k 1))))))
[...]
> (+ 1 2) is executed twice. I think this isn't necessarily so?
Right, but since (+ 1 2) is pure the problem is minor (and also because
whether (+ 1 2) was already computed or not depends on evaluation order
of arguments, which is defined in Elisp but would still be within the
scope of "acceptable quirks" in my book if it's not 100% obeyed).
Things get more interesting with side-effects:
(defvar fwdc nil)
(reset (λ (p)
(if fwdl 3
(setq fwdc t)
(shift p (λ (k) (k 5))))))
since we end up re-computing the `if` test and hence not using the `5`
provided to `k`. I think this case is a clear "bug" (because, IIUC,
(shift _ (λ (k) (k v))) should be equivalent to just `v`).
BTW, John:
> (defun shift (k entry)
> (if (eq (nth 0 k) 'outer)
> (throw (nth 1 k)
> (funcall entry #'(lambda (val)
> (funcall (nth 2 k)
> (list 'inner val)))))
> (nth 1 k)))
It means that `entry` is evaluated within the dynamic context of `shift`
rather than within the dynamic context of `reset` (because you call
`entry` before calling `throw`).
This has a clear downside: we use extra stack space during evaluation of
`entry` (the extra stack space is equal to the stack space used between
`reset` and `shift`). But I guess this is not too terrible.
It also means that dynamic-bindings (and other unwind-protects such as
with-current-buffer) applied within `k` up to now will still be active
during evaluation of `entry`. Whether that's The Right Thing or not is
a good question but is beyond what my poor brain can take right now.
Stefan
next prev parent reply other threads:[~2017-12-11 16:47 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-12-09 9:06 Delimited continuations John Wiegley
2017-12-10 16:53 ` Michael Heerdegen
2017-12-10 19:59 ` John Wiegley
2017-12-12 14:27 ` Michael Heerdegen
2017-12-11 16:47 ` Stefan Monnier [this message]
2017-12-12 14:17 ` Michael Heerdegen
2017-12-12 22:24 ` John Wiegley
2018-01-02 18:39 ` Christopher Lemmer Webber
2018-01-02 22:29 ` John Wiegley
2018-01-03 16:01 ` Michael Heerdegen
2018-01-03 20:12 ` Stefan Monnier
2017-12-10 17:39 ` Real continuations (was: Delimited continuations) Michael Heerdegen
2017-12-11 16:16 ` Real continuations Stefan Monnier
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/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=jwvefo1nssc.fsf-monnier+gmane.emacs.devel@gnu.org \
--to=monnier@iro.umontreal.ca \
--cc=emacs-devel@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.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
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).