Version 15 of continuation

Updated 2005-04-18 22:04:36 by antirez

Abstractly, a continuation is a representation of the state of an execution, and especially its "execution stack" [L1 ].

There's a delightful article explaining continuations at [L2 ].

DKF 17-Apr-2005: Interesting. I just wish it was easy to apply in Tcl, but our openness to extension, especially by C libraries, makes this very tricky indeed.

WHD 17-Apr-2005: Damn. I want this. The idea of User Interface Continuations makes me drool.

jcw - Yeah, me too, same reaction. DKF, yes the C stack can cause trouble, but only when C calls back into Tcl. There are probably many cases when this need not be a show stopper. I think there are ways to get there. Might be something to discuss at the tcl2005e conf - are you going to be there?

NEM 18-Apr-2005: You could sort of get this to work, if we went a similar route to Stackless Python (from what little I know of that project). You'd have to change all the C-API so that instead of re-calling into the interpreter with Tcl_Eval* APIs, they instead return a TCL_CALL or similar code with some code of where to go next in the interpreter result (i.e. trampoline). This is very similar to the recent proposals for tail-call optimisation (which is also very connected to continuations, through continuation-passing style, CPS). Obviously, it'd be an enormous amount of work to restructure the core and many many C extensions to do things this way. Continuations rock. (I should be at tcl2005e, and would love to discuss these ideas with people).

The core is not that far from doing it. At one point I had an experimental patch working, which copied the C stack away and back. Not sure it covered all the cases, but it was definitely very close to full contins across all of Tcl and C. -jcw

WHD: I don't see why the C stack is an issue, based on what the article says. A closure consists of a stack frame and its parent stack frame, and so on recursively, it says; but a continuation, so far as I can tell, is a single stack frame. Suppose we made the rule that you could "yield" only from the body of a normal proc.... Oh. If you do it from within a while loop, say, you've got the C stack involved. Never mind. (Or is there something else I'm missing?)

Will, the stack becomes a tree, because processing (and further nesting) can proceed at any continuation point. Yes, C loops and C re-entering Tcl (as foreach and proc do now when evaluating their bodies) is where things get tricky. -jcw

RHS 17Apr2005 One of the most interesting (and informative) descriptions I've seen of the use of continuations was in the area of web programming. The programmers used them to use web pages as if they were stateful. The "process" that the web pages are involved with work linearly. Each time it needs input from the user it saves its current continuation (which contains everything about where it is in the process) in a hashtable. It then returns a web page with a form to the user, and one of the elements of the form (a hidden one, I presume) contains the key into the hash table for the current continuation. Once the user fills in the form and hits submit, the process can look up the continuation in the hash table and continue right where it left off... Only, now, it has the data it needs to continue, as provided by the form.

JR 18Apr2005 What I think would be a nifty use for continuations would be to unnest vwait. As everyone knows, if you call vwait from inside an event handler then the outer vwait cannot return until the inner one does. This gets messy when things that you may not expect call vwait internally, with the general conclusion of vwait considered harmful. However, it is also very useful in some circumstances which makes this all a pain. If we had continuations, then vwait could simply be reimplemented as something like

   proc vwait {var} {
      uplevel trace variable $var w [info continuation 1]
      return -code return
   }

Where info continuation level returns a command that executes the continuation. Then make the stack frames into Tcl_Objs and increment the reference counts to keep them around and it all works naturally. (if only it were that simple)

SS 18Apr2005' Using setcontext and getcontext calls that are POSIX and AFAIK also available in some form on every platform Tcl supports allows for continuations without a stackless interpreter (basically you save the C stack too). This is how it's in plan to implement continuations into Jim. (this is also how Ruby implements continuations IIRC).


Category Concept