[plt-scheme] Confusing continuation behavior (a question about)

From: Jos Koot (jos.koot at telefonica.net)
Date: Thu Jul 17 23:34:25 EDT 2008

Does procedure toggle give you a hint?
There are in fact two continuations to be taken care of, that of the process 
and that of the program calling the process.
When the program runs, the continuation of the process must be kept safe. In 
this case the program knows its own continuation.
When the process runs,  the continuation of the calling program must be kept 
safe. In this case the process knows its own continuation.
Therefore one variable is enough for saving both. This is done in variable 
state.
Procedure toggle exchanges the continuation of the process with that of the 
calling program and reversely.
When the program calls toggle, the continuation of the progam is stored and 
that of the the process is called.
When the process calls toggle, the continuation of the process is stored and 
that of the program is called.
May be you like to read some of my essay 
http://www.telefonica.net/web2/koot/coroutines-essay.doc
You probably like to skip the first 7 pages and start at the section called 
"Self-replacing procedures".
In my essay I start with two separate variables for the two continuations 
and subsequently show that at any given time only one of them is relevant.
Good luck, Jos

BTW, the diagnosis of the problem is correct: There are in fact two 
continuations to be taken care of.



----- Original Message ----- 
From: "Grant Rettke" <grettke at acm.org>
To: "PLT-list" <plt-scheme at list.cs.brown.edu>
Sent: Friday, July 18, 2008 4:57 AM
Subject: Re: [plt-scheme] Confusing continuation behavior (a question about)


> On Thu, Jul 17, 2008 at 6:36 PM, Anton van Straaten
> <anton at appsolutions.com> wrote:
>>> 1 -> 2 -> 3 -> 1 -> 2 -> 3 -> ...
>>>
>>> 1. (a-process)
>>> 2. (set! cond1 #t)
>>> 3. (a-process)
>>> 4. (set! cond2 #t)
>>
>> Here's a clue: after line 3 invokes the continuation that re-enters 
>> 'impl',
>> what is the value of 'return'?
>
> Here are my thoughts. I am still studying continuations. Honestly, I
> have a hard time visualizing them, but from stepping through with the
> debugger I think I've got a visual of what is happening along with my
> rudimentary practice of converting simple functions to CPS style. Are
> these thoughts correct?
>
>
> The first time 'a-process' gets called at line 1, the the value bound
> to 'return' is a continuation that looks like this:
>
> (lambda (ignored)
>  (begin
>    (a-process)
>    (set! cond1 #t)
>    (a-process)
>    rest of program...))
>
> The second time 'a-process' gets called at line 3, the value of
> 'return' is still the same as above because when impl was rebound,
> that was the value bound to return. After the call at line 3, we
> really want return to be bound to this:
>
> (lambda (ignored)
>  (begin
>    ;;;;; (a-process)
>    ;;;;; (set! cond1 #t)
>    (a-process)
>    rest of program...))
>
> So the question is, when we pass control to impl that second time
> after binding cond1 to #t, how do we access the *that* continuation
> and bind it to return. I'm going to do some reading.
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme 



Posted on the users mailing list.