[Haskell-cafe] Really Simple explanation of Continuations Needed

wren ng thornton wren at freegeek.org
Sun Oct 2 02:49:04 CEST 2011


Hello,

Oleg has a introduction for delimited continuations which he presented 
during ICFP:

     http://okmij.org/ftp/continuations/index.html#tutorial

Of course, it's worth mentioning that the Cont monad is actually doing 
delimited continuations, cf.:

     http://okmij.org/ftp/continuations/ContExample.hs


Ultimately, the idea is simple, which may be part of the reason why it's 
so hard to explain (cf., monads). The short version is that at any point 
in the execution of a program we have some part of the program which has 
already run, and some part which has yet to run; the latter is the 
"continuation", which is also often called the calling "context". All 
the hoopla about continuations comes from the tricksy things we can do 
once we realize that programs have these two parts instead of only 
thinking about the history of the execution.

Operationally, the way the Cont monad works is that we explicitly build 
up the continuation as a function to be called, and then runCont 
actually applies that function in order to yield a result. What use is 
this? Well, it gives us a version of the goto statement, namely call/cc.

Another part of the problem (other than the simplicity) is that the term 
"continuation" has many different meanings in computer science. On the 
one hand we have the call/cc notion of continuations, which is what's 
captured by Scheme's call/cc and by the Cont monad. On the other hand we 
have the CPS transformation that many compilers use when optimizing 
code. But the topics of discussion for call/cc and CPS are very 
different, and so it's easy to get confused if you try to think of them 
as the same thing. They are closely related, but they're different 
enough that you should keep them separate until you have some 
understanding of what they're about.

-- 
Live well,
~wren



More information about the Haskell-Cafe mailing list