[Haskell-cafe] State monad is missing Applicative instance

Denis Bueno dbueno at gmail.com
Fri Mar 13 00:09:57 EDT 2009


2009/3/12 Peter Verswyvelen <bugfact at gmail.com>:
> I think. Or is it defined in some other package?

Note that you can get an Applicative instance for "free" by using
"WrapMonad" in Control.Applicative.  For example, just today I was
writing a quickcheck Arbitrary instance, and the Gen monad doesn't
have an Applicative instance.  No problem:

>    instance Gen MyDataType where
>       arbitrary = MyDataConstructor <$> arbitrary <*> arbitrary

becomes

>    instance Gen MyDataType where
>       arbitrary = unWrapMonad (MyDataConstructor <$> WrapMonad arbitrary <*> WrapMonad arbitrary)

This works because every monad induces an Applicative instance in a
way I've ingested just enough wine to forget. =]


                                       Denis


More information about the Haskell-Cafe mailing list