Subject: Should partial continuations capture the dynamic environment?

From: Taylan Ulrich Bayirli/Kammer <[email-masked]>
Date: Fri, 29 Aug 2014 20:12:09 +0200

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