Oleg Kiselyov: Delimited continuations library for native-code OCaml
groups.google.com/group/fa.caml/browse_thread...Posted Aug 23, 2010, 10:57 AM UTC
Posted Aug 23, 2010, 10:57 AM UTC
Now also ported to OCaml 3.12.0 on x86_32 and x86_64 (aka amd64) platforms.
What does it do in layman's terms?
I read it, still have no idea.
You know how, in most popular languages these days, when an exception gets thrown/raised, you get a stack trace, and the stack trace represents both how you got to where the exception occurred and what would have happened if the exception hadn't occurred? Well, a "continuation" is just that stack trace handed to you as a function whenever you want it. If the stack trace goes all the way up to the beginning of your program, it's a "full continuation," and if it only goes up to some delimiter that you set in your code, it's a "delimited continuation." Continuations close over all of the local variables in their respective stack frames and can be invoked as many times as you want, so they're particularly powerful for implementing non-deterministic computation and other exotic forms of control flow. In web applications, they're used a bit more prosaically: since, in some sense, continuations invert the flow of control, they can encapsulate the request/response cycle of web applications and present an API to developers that allows us to code in a more traditional linear style, hiding the non-linear user flow (revisiting arbitrary URLs, using the back button, etc.) behind that API.
Wow, this is by far the best explanation on continuations I have ever come across.
Thanks, psnively.
I'm glad you found it helpful! :-)
This used to be touted as the technology for easily making web pages, but with the advent of Javascript-heavy pages, it's become a bit less interesting. You just don't have to sludge through eight pages of forms that often anymore.
That's true, as far as it goes. What's interesting, though, is that event-driven programming is amenable to an emerging approach called FRP, for "Functional Reactive Programming," and it turns out that FRP implementation also benefits a lot from delimited continuations. See FlapJax for an implementation of the concepts in JavaScript, and Deprecating the Observer Pattern (PDF) for more general details about implementing interactive software by taking advantage of continuations.
I'll second psnively here, and note that a web app that takes advantage of continuations is very easy to add AJAX to, and is but a hair's breadth from being RESTful. Continuations take you a long way in the right direction.
My brain hurts every time this guy writes something ;P (in a good way)
If you understand what he said please explain. I'm a programmer, but not a russian programmer (they stopped playing chess all day and got into coding to pass the year while it's frozen outside, they are scary).
The Wikipedia article on delimited continuations is a bit small. I recently tried to get into Seaside, where this article about continuations was quite helpful to refresh my memory.
I don't know exactly what's brand new in his implementation, as I thought that Ocsigen already uses partial continuations.
Ocsigen's continuation implementation is buried within Ocsigen, and it isn't clear (to me, anyway) how general it is. Oleg's implementation is completely general and standalone, and can be compared directly with implementations in other languages. His site also has examples, e.g. of a very lightweight web framework using them--essentially CGI scripting, but with linear control flow from the programmer's point of view. It's a good example, both of how to use delimited continuations, and of how they can help with real-world tasks.
Lwt (lib used by ocsigen) uses monadic bind and return to chain continuations/operations together and comes with a asynch IO layer allowing concurrent code (ie for networking) to be written in a straightforward linear/procedural fashion. Oleg's continuation library is baked into his mods for the interpreter - and this release apparently adds support for the native code compiler. I think you can do more with the raw continuation support (wikipedia mentions coroutines/ easy implementation of backtracking inference algorithms) - but I don't think you could do continuation based networking, without also rewriting code to get select() or epoll() etc to dispatch to the continuations.
I have an article I wrote a few years ago on partial continuations and the differences between full continuations.