views:

1153

answers:

5

Can continuations be said to be monads? Are they a subset of monads or are they simply a way of implementing monads?

Edit: Or maybe I got it wrong and monads is a more abstract concept than continuations? (So I'm really comparing apples to oranges here)

+4  A: 

They can be, although they don't need to be. I'd reverse your question a little bit and say instead that monads are a way of implementing continuations. But you can implement continuations in many ways -- you can do a modest but constrained facsimile of CPS in C# without too much effort, for example. Have a look at The Continuation Monad from the Haskell site for a very thorough treatment.

John Feminella
I got a bit excited, but that's not CPS in C#. It's a utility function which takes a function and returns a the value returned by that function to the caller. Nothing to do with CPS.
Pete Kirkham
Oops, wrong link. Fixed. As noted, this is only an approximation to CPS, not real CPS (I don't think that's possible in C#, but I'd have to think about it a little more).
John Feminella
+7  A: 

Briefly, since the 'bind' of a monad takes an effective continuation (a lambda of the 'rest of the computation') as an argument, monads are continuations in that sense. On the flip side, continuation-passing style can be effectively implemented in a non-CPS language using monadic syntax sugars, as suggested by a number of misc links below.

From the 'all about monads' tutorial in Haskell:

http://www.haskell.org/all_about_monads/html/contmonad.html

An F# continuation monad, used to implement 'break' and 'continue' for for-style-loops

http://cs.hubfs.net/forums/thread/9311.aspx

And example of applying a continuation monad to a problem in F#:

http://lorgonblog.spaces.live.com/blog/cns!701679AD17B6D310!256.entry

Brian
+7  A: 

Not only are continuations monads, but they are a sort of universal monad, in the sense that if you have continuations and state, you can simulate any functional monad. This impressive but highly technical result comes from the impressive and highly technical mind of Andrzej Filinski, who wrote in 1994 or thereabouts:

We show that any monad whose unit and extension operations are expressible as purely functional terms can be embedded in a call-by-value language with “composable continuations”.

Norman Ramsey
I agree the result is impressive. Let me underscore though that this embedding is into a language with "delimited" (or as Filinski says "composable") continuations. These are strictly more powerful than the continuation values you get handed from call/cc in Scheme.
profjim
@profjim: actually, Filinski also showed how to implement delimited continuations using ordinary continuations and state. He implemented the whole thing in SML/NJ using callcc.
RD1
+3  A: 

A very nice article on that topic: http://blog.sigfpe.com/2008/12/mother-of-all-monads.html

sdcvvc
+1 for citing sigfpe's blog. What a beautiful treasure trove of awesomeness. The author manages to explain category theory and other far-off concepts in a detailed, enlightening, yet down to earth way. The smartest people, in my book, are the best teachers.
Jared Updike
A: 

A continuation is a particular function in a program. Monads are type constructors.

A type constructor Cont<T> for continuations taking type T would not be a monad.

However, Cont<Cont<T>> is a monad, and this is what is commonly called "the continuation monad".

(Having callcc in a language is equivalent to being able to convert from Cont<Cont<T>> to T.)

RD1