
Eight years later, I stumbled across this thread. Here's a cute way to express commutativity of an applicative functor:
forall f. flip (liftA2 f) == liftA2 (flip f)
Cuter yet,
flip . liftA2 == liftA2 . flip
-- Conal
On Wed, May 14, 2008 at 9:59 AM, David Menendez
On Tue, May 13, 2008 at 9:06 PM, Ronald Guida
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
http://www.eyrie.org/~zednenem/ _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe