[racket] Saved continuations

From: Harry Spier (vasishtha.spier at gmail.com)
Date: Tue Sep 4 21:11:56 EDT 2012

Is it possible in Racket to make a kind of copy of a saved
continuation with its environment at the time it was saved, so that no
mutation of variables between the time the copy of the saved
continuation was made and the time the copy is invoked can possible
have any affect on the values the copy of the saved continuation
produces?

I thought this would work but it didn't, it still captured the
mutation of x and returned:
10
100
--------------------------------------
#lang web-server
(require racket/serialize)
(define saved-k #f)

(define x 10)
(call-with-web-prompt
 (λ()
   (+ (call-with-serializable-current-continuation
           (lambda (k)
           (set! saved-k k)
            0)) x  ))) ; continuation is (+  [] x)

(define serialized-k (serialize saved-k))

(call-with-web-prompt (thunk (saved-k 0)))
(set! x 100)
(call-with-web-prompt (thunk ((deserialize serialized-k) 0)))
----------------------
RETURNS:
10
100


On Tue, Sep 4, 2012 at 3:33 PM, Matthew Flatt <mflatt at cs.utah.edu> wrote:
> 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()
>>    (+ (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?
>

>   ; continuation is (+ [] x)
>
>   `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.