17

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)

troelskn
  • 115,121
  • 27
  • 131
  • 155
  • 1
    Continuations are everything. Continuations can implement data structures; continuations can implement classes and objects; continuations can implement monads. I'm don't see what this question has to do with Haskell, though, aside from having both continuations and monads... – ephemient Mar 21 '09 at 02:04
  • Me neither. I didn't add the Haskell tag in the first place and frankly I'm more interested in an explanation in a different context. – troelskn Mar 21 '09 at 14:52
  • 1
    @troelskn: I'd agree with your edit; continuations are a different beast than monads. It's a bit like asking whether wooden planks are a house. They *could* be, if put together as such. But they could also be lots of other things. – John Feminella Mar 21 '09 at 16:46
  • 1
    Another way of saying this is: the continuation monad and continuations are equivalent. There are monads that are less general than continuations (identity monad, Maybe monad) so monads are a "more abstract" concept. – Jared Updike Jul 29 '09 at 15:21

4 Answers4

24

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
  • 198,648
  • 61
  • 360
  • 533
  • 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. – dubiousjim Aug 01 '10 at 23:37
  • 2
    @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 Aug 05 '10 at 07:15
18

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:

https://www.haskell.org/haskellwiki/All_About_Monads#The_Continuation_monad

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

pasja
  • 365
  • 4
  • 10
Brian
  • 117,631
  • 17
  • 236
  • 300
6

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.

pasja
  • 365
  • 4
  • 10
John Feminella
  • 303,634
  • 46
  • 339
  • 357
  • 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 Mar 20 '09 at 13:40
  • 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 Mar 20 '09 at 14:06
1

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
  • 3,305
  • 19
  • 28