You could test your instance using the checkers package on hackage (has quickcheck properties for common typeclasses) to see if it fulfills the applicative laws.

But I'm not sure if it is acceptable to define applicative instances that don't match the monad instance. 
Does anyone know of libraries that depend on applicative instances matching their corresponding monad instance?

I've  often wanted an applicative instance for a datatype that didn't match the monad instance.
For example, I find the "zipping" applicative list instance more useful than the current "choice" applicative list instance
instance Applicative [] where
  pure x = repeat x
  fs <*> xs = zipWith ($) fs xs

This actually also has a corresponding Monad instance (with a couple restrictions). It would be nice if there was a way to hide instances so that they could be redefined.

- Job



On Wed, Aug 26, 2009 at 12:04 PM, Martijn van Steenbergen <martijn@van.steenbergen.nl> wrote:
Jeremy Shaw wrote:
What I would prefer is:

instance (Monad f, Applicative f) => Applicative (ReaderT r f) where
   pure a = ReaderT $ const (pure a)
   f <*> a = ReaderT $ \r ->              ((runReaderT f r) <*> (runReaderT a r))

Right. This doesn't only go for ReaderT, it already goes for Either, too: you don't want the 'ap' implementation for <*> there either.

These are beautiful examples of how applicative style gives the caller less power, but the callee more information, allowing more information to be retained. In this case it allows you to concatenate errors using mappend.

Another example is parsing: I believe Doaitse's parsers allow more optimization if they are only used in applicative style (but I'm not sure of this).

This shows there can be several sensible implementations of a type class. You ask which instance is right--that depends entirely on what you want it to do! Setting (<*>) = ap is just one of them, one you happen to get for free if your functor is already a monad.

Hope this helps,

Martijn.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe