[Haskell-cafe] instance Applicative f => Applicative (StateT s f)

Ryan Ingram ryani.spam at gmail.com
Fri Dec 5 13:26:22 EST 2008


> This makes me wonder: does that mean there is no such thing as an
> applicative transformer?

Applicative functors compose much more simply than monads, so you
don't need transformers.

> newtype Compose f g a = O (f (g a))
> unO (O x) = x

> instance (Functor f, Functor g) => Functor (Compose f g) where
>     fmap f o = O $ fmap (fmap f) $ unO o

> instance (Applicative f, Applicative g) => Applicative (Compose f g) where
>    pure x = O $ pure $ pure x
>
>    -- unO of :: f (g (a -> b))
>    -- unO ox :: f (g a)
>    -- to get result :: f (g b)
>    -- we need to lift of to f (g a -> g b)
>    -- which we do via (fmap (<*>)) :: f (g (a -> b)) -> f (g a -> g b)
>    of <*> ox = O ((<*>) <$> unO of <*> unO ox)

Prettier implementations of this are available in the TypeCompose library.

  -- ryan


More information about the Haskell-Cafe mailing list