[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: So, what the heck is a continuation anyway?

Dan Sugalski wrote:

> >At some point, while my program is running, I look at the stack and
> >think, "Hey, this is a useful computation -- maybe I'd like to come
> >back and do this later".  I "capture" it.  Think of that as making a
> >copy of the stack.
> Sounds like a warped closure. (Or a closure's a warped continuation. Or both)

I don't know what you mean.  A continuation is a closure, in that it
captures its lexical environment, but not its store.

> >Scheme asks, why use it only once?  Use it as many times as you want.
> >So each time I use it, I have to copy it again.
> Is the copying fully required, and does anything that happens after the 
> snapshot's taken affect the snapshot?

The *semantics* requires a full copy.  Clever implementations (eg,
Dybvig's in Chez Scheme) don't do it all the way.

The only thing that can affect the snapshot is modifications of the
store.  The stack and environment stay the same, but you can
side-effect a variable.  As a result, invoking a continuation a second 
time doesn't have to yield exactly the same computation as invoking it 
the first time.

(Hope that's clear ...)

> For example, if I have:
>     {
>        int i = 0;
>        make_continuation();
>        print i;
>        i = 3;
>     }
> Will that print 0 or 3 when the continuation is invoked? And if I invoke 
> that continuation a second time will it print 0 or 3?

I don't know that this program makes a lot of sense.  In Scheme, at
least, continuations are VALUES (like lists, closures, etc), so the
corresponding correct program would be a lot more complex.  But I see
what you're getting at, and it's what I'm getting at above: it will
print 3.