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)
In Haskell a monad is represented as a type constructor (call it m ), a function that builds values of that type ( a -> m a ), and a function that combines values of that type with computations that produce values of that type to produce a new computation for values of that type ( m a -> (a -> m b) -> m b ).
In functional programming, a monad is a software design pattern with a structure that combines program fragments (functions) and wraps their return values in a type with additional computation.
A monad is a way of composing functions that require context in addition to the return value, such as computation, branching, or I/O. Monads type lift, flatten and map so that the types line up for lifting functions a => M(b) , making them composable.
monads are used to address the more general problem of computations (involving state, input/output, backtracking, ...) returning values: they do not solve any input/output-problems directly but rather provide an elegant and flexible abstraction of many solutions to related problems.
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”.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With