Add Applicative instances for MTL types

Ross Paterson ross at soi.city.ac.uk
Wed Jan 14 08:50:54 EST 2009


On Wed, Jan 14, 2009 at 01:54:47PM +0100, Bas van Dijk wrote:
> On Sat, Dec 20, 2008 at 7:09 PM, Bas van Dijk <v.dijk.bas at gmail.com> wrote:
> > In a project of mine I needed an Applicative instance for Identity and
> > noticed it didn't exist. So I decided to add Applicative (and
> > Alternative instances where possible) for all MTL types.
> >
> > When I was about to submit a library proposal I saw there already
> > existed one. So I added my patch to that ticket.
> >
> > My patch I different in that my Applicative instances don't require a
> > Monad constraint. This also means that most Functor instances now also
> > depend on Applicative rather than on Monad.
> >
> > See the ticket for the details:
> >
> > http://hackage.haskell.org/trac/ghc/ticket/2316

The Functor instances could depend on Functor rather than Applicative.

Even though Applicative is not a superclass of Monad, I think we ought to
ensure that the instances are compatible.  That is, if an Applicative
is also a Monad, then we should have pure = return and (<*>) = ap.
This fails for your ErrorT instance: ap runs the second computation
only if the first succeeded, while (<*>) runs them both before checking
for errors.  It needs a Monad constraint (like StateT), though not an
Error constraint.

> * Can we get rid of the Monad and MonadPlus constraints in the
> Applicative and Alternative instances for StateT and RWST?

I don't think so: you need part of the value generated by the first
computation, namely the state (inside the f), to construct the second one.
You can do that in a Monad, but not in an Applicative.

At Henning Thielemann's request, I've recently put up on hackage a
restructuring of the mtl into three packages, to provide three different
interfaces to the same monad transformers:

transformers: a Haskell 98 package with the MonadTrans class, concrete
	monad transformers, operations and liftings.
monads-fd: multi-parameter monad classes using functional dependencies,
	with instances for these transformers.  (Almost backward-compatible
	with the mtl package.)
monads-tf: monad classes using type families, with instances for these
	transformers.

The first one includes Applicative instances like these.


More information about the Libraries mailing list