[Stackless] On continuations

Andy Sy andy at nospam.com
Tue Mar 16 16:45:26 CET 2004

Anyone aware that Ruby now has continuations?


Anyway, I thought I might revisit the old Stackless with continuations
since I never really got my understanding down pat (although I felt I had
at least partially understood how call/cc worked under Scheme, and could've
sworn I had been able to wrap my brain somewhat around Nathaniel Gray's

Revising the original SPC paper reveals that the culprit lies with the
very first example in the original SPC paper:

def looptest(n):
     this = continuation.current()
     k = this.update(n)
       # continuation at "="
     if k:
         # resuming at "="
         del this.link

The problem is that this.update(), this() and this.link
were never really explained properly when they were presented.
Further, I believe the choice of how n and k were used confuses
instead of enlightens the reader.

I think the first example for an absolute beginner should go something
like the following:

"A continuation is like a bookmark."

def example1():
   bookmark=continuation.current()  # create a bookmark object, i.e. a 'current continuation'
   print "1"
   print "2"
   bookmark.update()  # update the bookmark to this position
   print "3"
   print "4"
   bookmark()         # jump to the bookmark, this will
                      # result in an endless loop printing out "3" and "4"

def example2():
   print "Before setting bookmark"
   print bm.update("value when bookmark was set")
                         # bm.update(xxx) will update (or save, if you will) the
                         # bookmark position and return a value xxx
   print "stuff after bookmark"
                         # calling the bookmark bm(xxx) with a value xxx will
                         # also return xxx at the point where it was last updated
                         # Thus, this will result in an endless loop with
                         # bm("newval") constantly jumping back to the saved
                         # bookmark position returning the value "newval"

                         # Note that bm.update(xxx), the 'bookmark setting code',
                         # did not seem to get executed (since we did not get to
                         # see its parameter printed)

def example3():
   bma=continuation.current()  # the initial setting of continuation.current()
   print "Bookmark A"          # seems to already set up a 'bookmark'
   print "Bookmark B"
   print "Bookmark C"
   print bmb
   bmb()                       # however the value will not be renewed as when
                               # the code comes to this point the 2nd time,
                               # bmb will now contain None

def example4():
   print "Bookmark A"
   print "Bookmark B"
   print "Bookmark C"
   print bmb
   bmb()                       # this will loop endlessly however...

In example1(), a continuation is shown as operating like just a simple goto,
in example2() it is like a goto except it can return a value.  While I'm sure
that the rudimentary examples above are nowhere near anything that continuations
can do, I hope the understanding is at least correct.

In example3() how come one can only jump back to bmb() only once as opposed to
example4()?  Is it because the position saved is right before the call to
continuation.current(), but also right after the '=' sign so the assignment
never takes place?

Now to move on and try to understand the more sophisticated examples all over

Stackless mailing list
Stackless at stackless.com

More information about the Stackless mailing list