Bringing back Monad Comprehensions (in style)

Max Bolingbroke batterseapower at hotmail.com
Thu Oct 7 12:58:52 EDT 2010


On 7 October 2010 12:04, Sebastiaan Visser <haskell at fvisser.nl> wrote:
> What exactly are the benefits of Monad comprehensions over, for example, the do-notation or idioms?

List comprehensions are just a specialisation of the do-notation for lists.
Monad comprehensions are a generalisation for arbitrary monads of this
specialisation :-)

I don't think there are major benefits to this. The major change is
that they "look like" lists (which might be important if you are
writing a SQL library) and the final "return" is hoisted into the head
of the comprehension [HERE | with, the, usual, do, notation, sequence,
here].

> I'm not fully aware of what Monad comprehensions would offer in general, but aren't most comprehensions directly translatable to applicative style?

Monadic style, not applicative style.

> For example:
>
>  [(x, y) | x <- xs | y <- ys]  -- Comprehension.

This computes a zip of xs and ys.

>  (,) <$> xs <*> ys             -- Applicative style.

This actually computes a cartesian product instead.

You are right in spirit because:

  [e | qs] ==> do { qs; return e }

For example:

  [(x, y) | x <- xs, y <- ys] ==> do { x <- xs; y <- ys; return (x, y) }

Cheers,
Max


More information about the Glasgow-haskell-users mailing list