[Haskell-cafe] Commutative monads vs Applicative functors

David Menendez dave at zednenem.com
Wed May 14 12:59:11 EDT 2008

On Tue, May 13, 2008 at 9:06 PM, Ronald Guida <oddron at gmail.com> wrote:
> I have a few questions about commutative monads and applicative functors.
>  >From what I have read about applicative functors, they are weaker than
>  monads because with a monad, I can use the results of a computation to
>  select between alternative future computations and their side effects,
>  whereas with an applicative functor, I can only select between the
>  results of computations, while the structure of those computations and
>  their side effects are fixed in advance.
>  But then there are commutative monads.  I'm not exactly sure what a
>  commutative monad is, but my understanding is that in a commutative
>  monad the order of side effects does not matter.
>  This leads me to wonder, are commutative monads still stronger than
>  applicative functors, or are they equivalent?
>  And by the way, what exactly is a commutative monad?

A monad is commutative if the expression "a >>= \x -> b >>= \y -> f x
y" is equivalent to "b >>= \y -> a >>= \x -> f x y". The identity,
state reader, and maybe monads are commutative, for example.

To put it another way, a monad is commutative if

    liftM2 f a b = liftM2 (flip f) b a

Since liftA2 generalizes liftM2, we can also say that an applicative
functor is commutative if

    liftA2 f a b = liftA2 (flip f) b a

Or, put another way, if

    a <*> b = flip ($) <$> b <*> a

If w is a commutative monoid (that is, if mappend w1 w2 == mappend w2
w1), then Const w is a commutative applicative functor.

To summarize: some applicative functors are commutative, some
applicative functors are monads, and the ones that are both are
commutative monads.

Dave Menendez <dave at zednenem.com>

More information about the Haskell-Cafe mailing list