HiveBrain v1.2.0
Get Started
← Back to all entries
patternMajor

Is computation expression the same as monad?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
expressionsamethecomputationmonad

Problem

I'm still learning functional programming (with f#) and I recently started reading about computation expressions. I still don't fully understand the concept and one thing that keeps me unsure when reading all the articles regarding monads (most of them are written basing on Haskell) is the relation between computation expressions and monads.

Having written all that, here's my question (two questions actually):

Is every F# computation expression a monad? Can every monad be expressed with F# computation expression?

I've read this post of Tomas Petricek and if I understand it well, it states that computation expressions are more than monads, but I'm not sure if I interpret this correctly.

Solution

First of all, computation expressions are a language feature, while monads are mathematical abstractions, so from this point of view, they are completely different things.

But that would not be a very useful answer :-). Computation expressions are a language feature that gives you a syntax which can be used for programming with computations (or data types) that have the monadic structure, but they can be also used with other structures. You can read my F# computation expression zoo paper for more details, but computation expressions can be used with:

  • Monads, but also additive monads (what Haskellers call MonadPlus or MonadOr)



  • Composed computations (what Haskellers call monad transformers)



  • Computations that are monadic, but support other F# constructs like exception handling



  • Monoids (and a couple of variations without monadic bind)



  • Applicative functors (though this is only implemented in a research extension)



So, computation expressions are certainly closely linked to monads, but they are not linked to them that closely. This is in contrast e.g. with Haskell's do notation, which is much more closely linked to monads (although even that can be used with computations that are not strictly mathematically monads).

Context

StackExchange Computer Science Q#29210, answer score: 24

Revisions (0)

No revisions yet.