Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|cite|improve this question

migrated from Aug 18 '14 at 15:01

This question came from our site for professional and enthusiast programmers.

@Raphael may I ask what's the reason for removing lang tag? – Grzegorz Sławecki Aug 21 '14 at 21:47
Since we care about concepts, we try to keep most things language-agnostic here. I don't know what removing F# does to the question (I think if it's important that it's F# then the question is offtopic here, but it's a borderline case) so I only remove the F# tag. Rule of thumb: F# is not a CS concept, so it does not need a tag. (Yes, I'm aware of other PL tags and I don't like them either. For some questions, community has decided that they warrant these tags.) – Raphael Aug 22 '14 at 5:57
@Raphael I believe the question is indeed a borderline case. Someone decided to migrate it here but it seems that it's slightly off topic here as well. The question itself is kind of computer since, but in the same time both answers and the question are specifically related to f#. I understand Your rule of thumb, thank You for clarification. – Grzegorz Sławecki Aug 22 '14 at 7:27
up vote 18 down vote accepted

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

share|cite|improve this answer
Monad transformers are a generic way to convert one monad into another monad - F# computation expressions only really support directly implementing the /result/ of that transformation, rather than the transformation itself. – Ganesh Sittampalam Aug 18 '14 at 16:53
@GaneshSittampalam - Yes, you are correct. My attempt to simplify was not all that useful here :-). Computation expressions can give you a syntax for working with a computation that is the result of applying a monad transformer (with potentially different syntax for the underlying monad and for the composed monad) – Tomas Petricek Aug 18 '14 at 17:06

You can use computation expressions to express monads. There is an example here. Also, as you noted, you can use computation expressions for a lot more than just monads. There is an extended explanation about how they are different here. There isn't space here to explain the difference properly, but computation expressions are different from monads in that they reuse normal F# syntax and have the ability to add additional abstractions. A limitation is that its non-idiomatic (and difficult) to write a computation expression which is polymorphic over the type of computation.

share|cite|improve this answer
Please make your answer more self-contained by at least summarizing the material on the pages you link to. Your current answer will be completely meaningless if the two links stop working. – David Richerby Aug 18 '14 at 16:09
I'm not sure what you're talking about. I explicitly answered the questions and then gave links to more information if the asker is interested. The links are not needed to answer the question. – mydogisbox Aug 18 '14 at 17:54
I cannot even imagine that the two links could ever stop working :-) – Tomas Petricek Aug 22 '14 at 20:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.