7 ms·
Continuations: One Control Flow Construct to Rule Them All
- Vivtek 16y agoSigh. I'm 43 years old and only really grasped closures this January. It's going to take another ten years for my continuation epiphany. This is what happens when you start with BASIC.
- PieSquared 16y agoTruth is, the idea isn't difficult, it's just very difficult to explain well. I would suggest that you learn some basic Scheme, download an interpreter, and just play around with it. Try a few things with call-with-current-continuation. Try to figure out the examples on Wikipedia. It'll might take a few hours of toying with it over a few days, but you'll get it. Don't be discouraged :)
- Vivtek 16y agoAs I said elsewhere, that's not enough. I wrote a Scheme compiler in a class with Kent Dybvig way back in the stone age. I used Scheme in my AI classes in the same degree program. What I need - and what I suspect will take a lot longer - is an actual situation where continuations solve some problem I've encountered. That's when I'll get them, but not earlier, in my experience.
- pg 16y agoThink of a continuation as a copy of the stack (saved at the point the continuation was created) that you can invoke like a function. When you call it with an argument x, what happens is what would have happened if x was the return value of the expression at the point where the continuation was created.
- Vivtek 16y agoOh, I understand it at that level. I did a compilers course in Scheme with Kent Dybvig; I know how continuations are implemented (the whole "there is no single stack" thing) but my programming mind, the not-really-verbal part that actually does the coding, just doesn't grok them. Just like closures; I knew more or less what they were - but this January I ran into my first situation where I really, truly saw how they made a particular situation easier - like, easier in a way that nothing else did - and since then I can't live without closures. That insight suddenly reorganized everything I thought I knew about programming. I'm still waiting for that to happen with continuations.
- pg 16y agoIt may never happen. You might never need to use raw continuations when writing user applications. They'd be something you more often used through some library that depended on them. E.g. I never explicitly use continuations in HN.
- silentbicycle 16y agoIn other words, continuations provide necessary infrastructure for adding various constructs to a language, but they're sufficiently general that you're unlikely to use them directly. Also, the first edition of _Essentials of Programming Languages_ has a great chapter on turning an interpreter into a compiler using continuations (Chapter 12). I hear it got cut from later editions, though.
- Vivtek 16y agoYeah, hence my ten-year estimate. Sooner or later I'll end up designing my own language for fun; continuations will probably click then. (Actually, I'm already designing my own language for fun, but it compiles to Perl, so continuations are still not really on the table.)
- silentbicycle 16y agoContinuations sound confusing because they give you an explicit handle for something that most languages don't let you reference directly: who gets the result of the current computation. Are you familiar with setjmp/longjmp in C? Continuations are similar, but allow longjmp-ing to the same point multiple times. As you might expect, this gives you a lot of flexibility in defining control constructs. You can implement exceptions easily via continuations - pass along an alternative continuation to receive errors. Continuations are more general than exceptions, backtracking, co-routining, etc., but this generality can make them seem overly abstract - it can be hard to see what those constructs have in common. Hey, and don't worry - I started with BASIC too. (I was five or six.) You gotta start somewhere. After you feel comfortable with continuations, try learning about unification. :)
- eru 16y ago> Continuations sound confusing because they give you an explicit handle for something that most languages don't let you reference directly: who gets the result of the current computation. Yes, unfortunately continuations don't play nice with lazy evaluation.
- cousin_it 16y agoHow about this? http://www.haskell.org/all_about_monads/html/contmonad.html http://www.haskell.org/all_about_monads/html/contmonad.html
- eru 16y agoYes, I know about the continuation monad. But that continuations have to be encapsulated in monads is telling. I have found a discussion about this http://lambda-the-ultimate.org/classic/message10560.html http://lambda-the-ultimate.org/classic/message10560.html
- Vivtek 16y agoNow, unification (and matching in general) I get. That's magic on a level my mind can accept. Also: I envy you your starting at 5 or 6. My parents unfortunately couldn't afford the PDP-11 that would have required in my case.
- blasdel 16y agoConditional Jump: Another Control Flow Construct to Rule Them All
- fab13n 16y agoNot another, pretty much the same. If you add to "goto" the required stuff to play nice with closures, you get a continuation.
- blasdel 16y agoWas that Functional Completeness not obvious in my joke? Except that in this case, they're not quite as equivalent as NAND / NOR are — continuations assume a lot more "required stuff" (as you put it) than just a basic memory space, a means to test values, and the ability to set the program counter.
- eru 16y agoThat is if you approach the requirements from the implementation point of view. In theory continuations aren't really that much more complex than Gotos.