Cont



> import Control.Monad.Cont



> ex1 = do

> a <- return 1

> b <- return 10

> return $ a+b





[11]

Maybe

Just 11

Cont



> test1 = runCont ex1 show





ex1

show

"11"

Cont

runCont

Cont

test1

show

Cont

fred



ex1 = do

a b ...)

return $ a+b



...

fred

Cont

runCont

fred



> ex2 = do

> a <- return 1

> b <- Cont (\fred -> fred 10)

> return $ a+b



> test2 = runCont ex2 show





"11"

return

b

fred



> ex3 = do

> a <- return 1

> b <- Cont (\fred -> "escape")

> return $ a+b



> test3 = runCont ex3 show





fred

show

"escape"

Maybe

Maybe



> ex4 = do

> a <- return 1

> b <- Cont (\fred -> fred 10 ++ fred 20)

> return $ a+b



> test4 = runCont ex4 show





fred



> test5 = do

> a <- return 1

> b <- [10,20]

> return $ a+b







> ex6 = do

> a <- return 1

> b <- Cont (\fred -> fred 10 ++ fred 20)

> return $ a+b



> test6 = runCont ex6 (\x -> [x])





++

concat



> ex7 = do

> a <- return 1

> b <- Cont (\fred -> concat [fred 10,fred 20])

> return $ a+b



> test7 = runCont ex7 (\x -> [x])







> ex8 = do

> a <- return 1

> b <- Cont (\fred -> [10,20] >>= fred)

> return $ a+b



> test8 = runCont ex8 return







> i x = Cont (\fred -> x >>= fred)

> run m = runCont m return







> test9 = run $ do

> a <- i [1,2]

> b <- i [10,20]

> return $ a+b





i x

x

>>=



> test10 = run $ do

> i $ print "What is your name?"

> name <- i getLine

> i $ print $ "Merry Xmas " ++ name





Cont

call-with-current-continuation

yield

Suppose someone stole all the monads but one, which monad would you want it to be? If you're a Haskell programmer you wouldn't be too bothered, you could just roll your own monads using nothing more than functions.But suppose someone stole do-notation leaving you with a version that only supported one type of monad. Which one would you choose? Rolling your own Haskell syntax is hard so you really want to choose wisely. Is there a universal monad that encompasses the functionality of all other monads?I often find I learn more computer science by trying to decode random isolated sentences than from reading entire papers. About a year ago I must have skimmed this post because the line "the continuation monad is in some sense the mother of all monads" became stuck in my head. So maybeis the monad we should choose. This post is my investigation of why exactly it's the best choice. Along the way I'll also try to give some insight into how you can make practical use the continuation monad. I'm deliberately going to avoid discussing the underlying mechanism that makes continuations work.So let's start with this simple piece of codeI haven't specified the monad but in almost every case we'd expect the result to have something to do with the number 11. For the list monad we get, for themonad we getand so on. For themonad we get something that takes a function, and applies it to 11. Here's an example of its use:is just a function that takes as argumentand applies it to 11 to give the stringandare just wrapping and unwrapping functions that we can mostly ignore.We could have done that without continuations. So what exactly does themonad give us here? Well let's make a 'hole' in the code above:Whatever integer we place in the hole, the value ofwill be the result of adding one and applying. So we can think of that picture as being a function whose argument we shove in the hole. Now Haskell is a functional programming language so we expect that we can somehow reify that function and get our hands on it. That's exactly what the continuation monaddoes. Let's call the function we're talking about by the name. How can we get our hands on it? It's with this piece code:Theis a context in whichrepresents "the entire surrounding computation". Such a computaton is known as a "continuation". It's a bit hard to get your head around but themonad allows you to write subexpressions that are able to "capture" the entirety of the code around them, as far as the function provided to. To show that this is the case let's applyto the number 10:The entire computation is applied to 10 and we get. Now you know whatdoes in this monad. But that's a convoluted way of doing things. What other advantages do we get? Well the expression forcan do whatever it wants withas long as it returns the same type, ie. a string. So we can write this:is completely ignored. The entire computation is thrown away and instead of applyingto a number, we simply return. In other words, we have a mechanism for throwing values out of a computation. So continuations provide, among other things, an exception handling mechanism. But that's curious, because that's exactly what themonad provides. It looks like we might be able to simulatethis way. But rather than do that, let's do something even more radical.We've usedtwice. We've made the code around our "hole" run twice, each time executing with a different starting value. Continuations allow mere subexpressions to take complete control of the expressions within which they lie. That should remind you of something. It's just like the list monad. The above code is a lot likeSo can we emulate the list monad? Well instead of converting our integer to a string at the end we want to convert it to a list. So this will work:We can avoid thoseoperators by usingBut now you may notice we can remove almost every depepndence on the list type to get:Note, we're using monad related functions, but when we do so we're not using do-notation. We can now do one last thing to tidy this up:And now we have something close to do-notation for the list monad at our disposal again:I hope you can see how this works.says that the continuation should be applied to, not as an ordinary function, but with. But that's just business as usual for monads. So the above should work for any monad.The Grinch has been foiled and we see that the continuation monad really is the mother of all monads.There are some interesting consequences of this beyond Haskell. Many languages with support for continuations should be extensible to support monads. In particular, if there is an elegant notation for continuations, there should be one for monads too. This is why I didn't want to talk about the underlying mechanism of themonad. Different languages can implement continuations in different ways. An extreme example is (non-portable) C where you can reify continuations by literally flushing out all registers to memory and grabbing the stack. In fact, I've used this to implement something like the list monad for searching in C. (Just for fun, not for real work.) Scheme haswhich can be used similarly. And even Python'sdoes something a little like reifying a continuation and might be usable this way. (Is that's what's going on here ? I haven't read that yet.).This post was also inspired by this paper by Filinski. I haven't followed the details yet (it's tricky) but the gist is similar. I was actually looking at Filinski's paper because of something I'll mention in my next post.