[racket] Saved continuations

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue Sep 4 15:33:40 EDT 2012

At Tue, 4 Sep 2012 15:25:32 -0400, Harry Spier wrote:
> I did this experiment with saved continuations in DrRacket
> --------------------------------------------------
> > (define saved-k #f)
> (define (save-it) (call-with-composable-continuation
>            (lambda (k)
>            (set! saved-k k)
>             0)))
> 
> > (define x 10)
> (call-with-continuation-prompt
>  (lambda ()
>    (+ x (save-it)  ))) ; continuation is (+ x []))
> 
> (saved-k 0)
> (set! x 100)
> (saved-k 0)
> -------------------------------------
> GIVES RESULTS
> 10
> 10
> 
> 
> -------------------------------------------
> 
> 
> (define x 10)
> (call-with-continuation-prompt
>  (lambda()
>    (+ (save-it) x  ))) ; continuation is (+  [] x))
> 
> (saved-k 0)
> (set! x 100)
> (saved-k 0)
> 
> ---------------------------
> GIVES RESULTS
> 10
> 100
> 
> Is this a querk of Dr. Racket or does this mean that if within a saved
> continuation, a variable has not yet been reduced to a value at the
> time the continuation was saved, that variable can be mutated changing
> the behavior of the saved continuation?

"Yes" to the latter.

I'd phrase it this way: the example illustrates left-to-right
evaluation within a function call.

Where you write

  ; continuation is (+ x []))

the continuation is actually

 (+ 10 [])

because the `x' has already been evaluated when the continuation is
captured. In contrast, where you write

  ; continuation is (+ [] x)

the comment is more precisely correct, because the `x' has not been
evaluated, yet.

In the latter case, `x' is not evaluated until the continuation is
applied, and that's why it can "see" mutation of `x'.


Posted on the users mailing list.