[plt-scheme] Understanding continuations

From: Anton van Straaten (anton at appsolutions.com)
Date: Wed Jul 23 06:50:08 EDT 2008

Filipe Cabecinhas wrote:
> 
> On 19 Jul, 2008, at 23:08, Anton van Straaten wrote:
> 
>> Grant Rettke wrote:
>>> I'm trying to visualize and understand continuations (this part of the
>>> work in another thread [Confusing continuation behavior (a question
>>> about)]).
>>> If I write:
>>> #lang scheme
>>> (define cont #f)
>>> (define (foo)
>>>  (call/cc
>>>   (lambda (arg-cont)
>>>     (set! cont arg-cont)))
>>>  13)
>>> (+ 1 2 3 (foo))
>>> Could I visualize the continuation as?
>>> (lambda (argument)
>>>  (+ 1 2 3 13))
>>
>> You could, but you should be aware that this is an optimized version 
>> of the continuation.  The questions you've asked below might be 
>> answered more easily and precisely if you had a more detailed 
>> representation of the continuation, which at the very least should 
>> include a reference to 'argument' (other than its binding).  Where 
>> does that reference belong?
> 
> So, I suppose the continuation should be something like:
> (lambda (argument)  ;; Continuation that just returns to the toplevel
>   ((lambda (other-argument)  ;; Call to plus with the result of foo
>      (+ 1 2 3 other-argument))
>    ((lambda (inner-argument)  ;; the rest of the foo function
>       inner-argument
>       13))))
> 
> effectively turning the program inside out. Or am I misunderstanding 
> what you want to say?

That's about what I had in mind, yes.  The main thing I wanted to get at 
was the reference to inner-argument on your second-last line of code, 
because that helps clarify the issue of how this version of the function 
ignores its argument and returns 13, whereas the other version (which 
had the 13 inside the call/cc expression) just returns its argument. 
Expressed in the above form, there's no mystery about this.

As for the rest, yes, I think that modeling the whole thing in CPS is 
essential if you really want to understand what's going on when learning 
about this stuff.

Anton



Posted on the users mailing list.