Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are continuations monads?

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)

like image 835
troelskn Avatar asked Mar 20 '09 13:03

troelskn


People also ask

What is a monad type?

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 ).

Are monads functions?

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.

What is a monad in mapping?

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.

Why are monads used?

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.


2 Answers

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”.

like image 176
Norman Ramsey Avatar answered Sep 20 '22 15:09

Norman Ramsey


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

like image 33
Brian Avatar answered Sep 20 '22 15:09

Brian