unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#18356: Should partial continuations capture the dynamic environment?
@ 2014-08-29 18:12 Taylan Ulrich Bayirli/Kammer
  2014-08-29 18:26 ` Taylan Ulrich Bayirli/Kammer
  2014-10-14 14:25 ` Taylan Ulrich Bayirli/Kammer
  0 siblings, 2 replies; 5+ messages in thread
From: Taylan Ulrich Bayirli/Kammer @ 2014-08-29 18:12 UTC (permalink / raw)
  To: 18356

On Guile 2.0.11:

scheme@(guile-user)> (define (capture-dynenv)
                       (let ((tag (make-prompt-tag "dynenv-capture")))
                         (call-with-prompt
                          tag
                          (lambda ()
                            ((abort-to-prompt tag)))
                          (lambda (call-in-captured-dynenv)
                            (lambda (proc)
                              (call-in-captured-dynenv proc))))))
scheme@(guile-user)> (define param (make-parameter 0))
scheme@(guile-user)> (define dynenv (parameterize ((param 1))
                                      (capture-dynenv)))
scheme@(guile-user)> (parameterize ((param 2))
                       (dynenv (lambda () (param))))
$9 = 2

In other words, when a partial continuation is called, the dynamic
environment at that call-time is in effect for the continuation, and
not the one from when the continuation was captured.

Is this behavior correct and what I'm trying to do won't work, or
should the code return 1 as I had expected?


For comparison, the following variant that uses call/cc works fine:

(define (capture-dynenv)
  ((call/cc
    (lambda (call-in-captured-dynenv)
      (lambda ()
        (lambda (proc)
          (call/cc
           (lambda (go-back)
             (call-in-captured-dynenv
              (lambda ()
                (call-with-values proc go-back)))))))))))

Taylan





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#18356: Should partial continuations capture the dynamic environment?
  2014-08-29 18:12 bug#18356: Should partial continuations capture the dynamic environment? Taylan Ulrich Bayirli/Kammer
@ 2014-08-29 18:26 ` Taylan Ulrich Bayirli/Kammer
  2014-10-14 14:25 ` Taylan Ulrich Bayirli/Kammer
  1 sibling, 0 replies; 5+ messages in thread
From: Taylan Ulrich Bayirli/Kammer @ 2014-08-29 18:26 UTC (permalink / raw)
  To: 18356

Some more examples; this one works fine:

scheme@(guile-user)> (define (with-captured-dynenv proc)
                       (let ((tag (make-prompt-tag "dynenv-capture")))
                         (call-with-prompt
                          tag
                          (lambda ()
                            (proc (lambda (thunk)
                                    (abort-to-prompt tag thunk))))
                          (lambda (return thunk)
                            (return (thunk))))))
scheme@(guile-user)> (define param (make-parameter 0))
scheme@(guile-user)> (parameterize ((param 1))
                       (with-captured-dynenv
                        (lambda (dynenv)
                          (parameterize ((param 2))
                            (dynenv (lambda () (param)))))))
$2 = 1

But not this simpler one:

scheme@(guile-user)> (define (with-captured-dynenv proc)
                       (let ((tag (make-prompt-tag "dynenv-capture")))
                         (call-with-prompt
                          tag
                          (lambda ()
                            ((abort-to-prompt tag)))
                          (lambda (call-in-captured-dynenv)
                            (proc call-in-captured-dynenv)))))
scheme@(guile-user)> (parameterize ((param 1))
                       (with-captured-dynenv
                        (lambda (dynenv)
                          (parameterize ((param 2))
                            (dynenv (lambda () (param)))))))
$3 = 2

Taylan





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#18356: Should partial continuations capture the dynamic environment?
  2014-08-29 18:12 bug#18356: Should partial continuations capture the dynamic environment? Taylan Ulrich Bayirli/Kammer
  2014-08-29 18:26 ` Taylan Ulrich Bayirli/Kammer
@ 2014-10-14 14:25 ` Taylan Ulrich Bayirli/Kammer
  2014-10-14 16:48   ` Nala Ginrut
  2014-10-15 16:37   ` Mark H Weaver
  1 sibling, 2 replies; 5+ messages in thread
From: Taylan Ulrich Bayirli/Kammer @ 2014-10-14 14:25 UTC (permalink / raw)
  To: 18356

It has been explained on the #guile Freenode channel that this
behavior is indeed desired and not a bug, being argued for by some
authors of papers on delimited continuations:

http://www.cs.utah.edu/plt/publications/icfp07-fyff.pdf

http://okmij.org/ftp/papers/DDBinding.pdf


In short, no: partial continuations in Guile should not (and do not)
capture their dynamic environment.


Thanks to Andy Wingo for the clarification.

Taylan





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#18356: Should partial continuations capture the dynamic environment?
  2014-10-14 14:25 ` Taylan Ulrich Bayirli/Kammer
@ 2014-10-14 16:48   ` Nala Ginrut
  2014-10-15 16:37   ` Mark H Weaver
  1 sibling, 0 replies; 5+ messages in thread
From: Nala Ginrut @ 2014-10-14 16:48 UTC (permalink / raw)
  To: Taylan Ulrich Bayirli/Kammer; +Cc: 18356

Thanks for researching and explaining this issue!
It's helpful for me to think about how to deal with my async server
design based on delimited-continuation.
I think it's fine if it doesn't capture dynamic environment, even
cooler, so that the dynamic state could be well understood and traced
by users more easily.


On Tue, Oct 14, 2014 at 10:25 PM, Taylan Ulrich Bayirli/Kammer
<taylanbayirli@gmail.com> wrote:
> It has been explained on the #guile Freenode channel that this
> behavior is indeed desired and not a bug, being argued for by some
> authors of papers on delimited continuations:
>
> http://www.cs.utah.edu/plt/publications/icfp07-fyff.pdf
>
> http://okmij.org/ftp/papers/DDBinding.pdf
>
>
> In short, no: partial continuations in Guile should not (and do not)
> capture their dynamic environment.
>
>
> Thanks to Andy Wingo for the clarification.
>
> Taylan
>
>
>





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#18356: Should partial continuations capture the dynamic environment?
  2014-10-14 14:25 ` Taylan Ulrich Bayirli/Kammer
  2014-10-14 16:48   ` Nala Ginrut
@ 2014-10-15 16:37   ` Mark H Weaver
  1 sibling, 0 replies; 5+ messages in thread
From: Mark H Weaver @ 2014-10-15 16:37 UTC (permalink / raw)
  To: Taylan Ulrich Bayirli/Kammer; +Cc: 18356, request

tags 18356 + notabug
close 18356
thanks

Taylan Ulrich Bayirli/Kammer <taylanbayirli@gmail.com> writes:
> In short, no: partial continuations in Guile should not (and do not)
> capture their dynamic environment.

I think this requires further explanation, because the above statement
is not quite correct.

Partial continuations do not capture the _entire_ dynamic environment,
but they *do* capture (and later restore) the _part_ of the dynamic
environment that was established between the prompt and abort.

Thinking in terms of 'dynamic-wind', 'abort-to-prompt' unwinds from the
abort to the prompt, and when the partial continuation is later invoked,
it will rewind from the prompt back to the abort before resuming the
computation.

Thinking in terms of dynamic environments, 'abort-to-prompt' captures
the dynamic bindings that were established between the prompt and abort,
and these captured bindings are composed on top of the dynamic
environment of the call site when the partial continuation is later
invoked.

So, for example:

--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> (call-with-prompt
                      'foo
                      (lambda ()
                        (dynamic-wind
                          (lambda () (display "entering\n"))
                          (lambda () (abort-to-prompt 'foo))
                          (lambda () (display "leaving\n"))))
                      (lambda (k) k))
entering
leaving
$1 = #<partial-continuation 10934680>
scheme@(guile-user)> ($1 'hello)
entering
leaving
$2 = hello
--8<---------------cut here---------------end--------------->8---

Above, the 'dynamic-wind' between the prompt and abort is rewound when
invoking the partial continuation.

--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> (dynamic-wind
                       (lambda () (display "entering\n"))
                       (lambda ()
                         (call-with-prompt
                          'foo
                          (lambda () (abort-to-prompt 'foo))
                          (lambda (k) k)))
                       (lambda () (display "leaving\n")))
entering
leaving
$3 = #<partial-continuation 10972810>
scheme@(guile-user)> ($3 'hello)
$4 = hello
--8<---------------cut here---------------end--------------->8---

Above, the 'dynamic-wind' outside of the prompt is _not_ rewound when
invoking the partial continuation.

--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> (define my-param (make-parameter #f))
scheme@(guile-user)> (call-with-prompt
                      'foo
                      (lambda ()
                        (parameterize ((my-param 5))
                          ((abort-to-prompt 'foo))))
                      (lambda (k) k))
$5 = #<partial-continuation 109f9c60>
scheme@(guile-user)> ($5 my-param)
$6 = 5
--8<---------------cut here---------------end--------------->8---

Above, the dynamic binding of 'my-param' to 5 is restored when invoking
the partial continuation, because it was established between the prompt
and abort.

--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> (parameterize ((my-param 5))
                       (call-with-prompt
                        'foo
                        (lambda () ((abort-to-prompt 'foo)))
                        (lambda (k) k)))
$7 = #<partial-continuation 107a37a0>
scheme@(guile-user)> ($7 my-param)
$8 = #f
--8<---------------cut here---------------end--------------->8---

Above, the dynamic-binding of 'my-param' to 5 is _not_ restored, because
it was established outside of the prompt.

      Mark





^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-10-15 16:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-29 18:12 bug#18356: Should partial continuations capture the dynamic environment? Taylan Ulrich Bayirli/Kammer
2014-08-29 18:26 ` Taylan Ulrich Bayirli/Kammer
2014-10-14 14:25 ` Taylan Ulrich Bayirli/Kammer
2014-10-14 16:48   ` Nala Ginrut
2014-10-15 16:37   ` Mark H Weaver

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).