continuations

Avi Bryant avi at beta4.com
Fri Feb 1 19:23:23 UTC 2002


On Fri, 1 Feb 2002, Anthony Hannan wrote:

> We could add a method to BlockClosure called #copyStack that will copy
> its home context call stack.  This way everytime you want to use it as a
> continuation that may be used again you call this first.  We can even
> have an #asContinuation method that would wrap the block closure in a
> Continuation object that when invoked would send #copyStack to the block
> closure first.

Please do!

I should point out that there are two possible semantics for stack-copying
continuations in smalltalk.  One is to follow "true" continuations more
closely, and only copy the stack once, making sure that every time the
continuation is invoked that same stack is restored - this means that each
invocation of the continuation shares the same temp slots for each
context.  So, for example,

|x continuation|
x := 0.
continuation := [:cc | cc] callCC.
x < 2 ifTrue:
  [x := x + 1.
   Transcript show: x.
   continuation value: continuation]

would print "12" to the transcript and then exit.  This is how Vassili's
implementation posted earlier today works.

The other is to copy the stack again each time the continuation is
invoked, so that each invocation has an independent set of temps.  The
above example would just loop forever printing "1" to the Transcript.
This is more of a backtracking facility than a pure continuation, and I use it
quite heavily in Seaside/IOWA.  You run into some problems with this
approach and block closures, because of the Var objects - these need
to be copied whenever their stack frame is copied.

Ideally, both would be available.  Of course, if I were going to choose
one, I'd pick the latter (it's also simpler to implement), but I imagine
the needs of others will be different.

Avi




More information about the Squeak-dev mailing list