At 05:07 PM 6/11/2002 +0100, Dave Mitchell wrote: >On Tue, Jun 11, 2002 at 11:31:37AM -0400, Dan Sugalski wrote: > > We'll find out with A6 whether we do coroutines and continuations as > > part of the core perl. If not, well, python does the first and ruby > > the second, so it's all good in there. > >Does anyone feel like giving a 1 paragraph potted summary of what >coroutines and continuations are (from a Perl persepective), for the >benefit those ignoramousses here (like me, who wasn't taught anything at >all useful during a 3 year CS degree course - unless you count Pascal as >useful :-)
I wasn't taught about continuations in school either. Co-routines are virtually identical to normal subroutines, except while subroutine's execute from start to finish, and return, co-routines may suspend themselves, (or be suspended asynchronously if the language permits) and resume later. You can implement things like "factories" with co-routines. If the co-routine never returns, everytime you call it it "resumes". Continuations take a while to sink in, and I'm still not finished grasping their full usage. You can think of continuations as an execution "context". This context incudes everything, not just stack. It is a snapshot in time. You may think this is just like setjmp/lonjmp, except that longjmp'ing only works "down" the stack. Jumping "up" the stack (ie back to a frame that has returned) is bad for longjmp. Continuations work either way. You can do 2 important things with continuations: 1) Create and pass a continuation object to subroutines, who may recursively pass that object up the call chain. Finally the continuation can be called/executed/ taken to handle the final computation or return value. This is pretty much tail recursion. 2) Continuations can be taken at some call depth of arbitrary recursive level. This freezes the call chain at that point in time. If you save that continuation object off into a variable, later you can reinstate the complete context, even if you have returned from it. This allows neat things like backtracking that you can't really do in conventional stacked languages. From what I gather from Dan, we are doing continuations with tree-based stacks, so this may help you get a handle on how you can freeze an execution context, return from it, and then restore it later. I apologize if my layman's view is not the proper academic description, feel free to chime in and correct me. -Melvin PS: A context is a context is a context. You can do anything that continuations can do with threads, however, academics claim fast-continuations outperform system threads due to the fact that system level threads usually are almost as heavy as a fork().