For example, MaybeT is defined as:
newtype MaybeT m a =
MaybeT { runMaybeT :: m (Maybe a)}
But not:
newtype MaybeT m a =
MaybeT { runMaybeT :: Maybe (m a) }
Why is this?
For example, MaybeT is defined as:
newtype MaybeT m a =
MaybeT { runMaybeT :: m (Maybe a)}
But not:
newtype MaybeT m a =
MaybeT { runMaybeT :: Maybe (m a) }
Why is this?
On
Looking at StateT might be instructive:
newtype StateT s m a = StateT { runStateT :: s -> m (a,s) }
Here the state is neither "inner" nor "outer", but its type is interleaved with the monad it is transforming, some bits inside, some outside. And indeed
newtype ReaderT r m a = ReaderT { runReaderT :: r -> m a }
is all "outer". So it depends on what transformer it is. There's probably some category theory that at least partially explains this interleaving, I'm curious to know about it (intelligentsia?).
Contrast with applicative functors, for which
newtype Compose f g a = Compose { getCompose :: f (g a) }
is an applicative as well, so there is always a clear "inner/outer" relationship. You could make an applicative-only StateT, and finding its structure by Compose (State s):
ApplicativeStateT s f a = s -> (s, f a)
In fact, there's another one if you compose on the right:
ApplicativeStateT' s f a = f (s -> (s,a))
But monads have no such regularity.
After expanding newtypes, we have
join :: Monad m => m (Maybe (m (Maybe a))) -> m (Maybe a)in the first case andjoin :: Monad m => Maybe (m (Maybe (m a))) -> Maybe (m a)in the second.To implement the first
joinyou need a way to distributeMaybeoverm:dist1 :: Monad m => Maybe (m a) -> m (Maybe a):To implement the second
joinyou need the opposite distributive lawdist2 :: Monad m => m (Maybe a) -> Maybe (m a)dist1is easy to implement (I'll leave proving the monad transformer laws to you):dist2is not so easy. It can't be done for an arbitraryMonad. As a counterexample, let's pickmto be the "reader" monad(->) r:Since you don't have access to an
r, the only implementation ofdist2that'll typecheck isconst Nothing, which clearly won't satisfy the monad laws.