Standard Transformers
Let's look at the standard transformers and look at some simple examples of what
we can do with them. Before doing this, it is useful to review the Identity
functor. When I introduced it a long time ago, I promised you that we would
meet it again in the discussion of monad transformers. The moment has come.
The Identity
monad is the simplest monad we can think of. The decoration it
provides is exactly nothing:
newtype Identity a = Identity { runIdentity :: a }
It simply wraps a plain old value. return
does the wrapping. runIdentity
does the unwrapping. And (>>=)
is plain old function application.
instance Monad Identity where
return = Identity
x >>= f = f $ runIdentity x
All that Identity
does is to allow us to pretend that pure computations
without side effects actually use some monad. We'll see in a second why that's
useful.