The Reader and Writer monads are possibly the first monads a reader
encounters when working through a Haskell book.
They both depend on a type s and map a type a to
For the Writer monad to truly become a monad, the type s needs to have a monoid
instance (we’ll see why later).
The State monad seems to be closely related when inspecting its definition:
1State s a = s -> (s, a)
This begs the question: Is State the composition of Reader and Writer?
More precisely, is State s a = Reader s (Writer s a)?
A closer look at Reader and Writer as functors
Link to heading
For a mathematician, Reader is more well-known as the Hom-functor
\[
\mathrm{Hom}(s,-)\colon a \mapsto \mathrm{Hom}(s,a)
\]
which may be defined for any (locally small) category.
As for Writer, we need a monoidal category with tensor product $\otimes$ but
we can certainly confine ourselves with the category of sets and take the tensor
product to be the cartesian product of sets if you like.
In any case, Writer s is then the product functor with the object s:
\[
s \otimes -\colon a \mapsto s \otimes a
\]
As for their functor instances, fmap for Reader is given by post-composition
while fmap for Writer is given by applying $f$ to the second factor. More
precisely, for any $f\colon a \to b$, we have
\[
\mathrm{Hom}(s,f) = f_\ast\colon (g\colon s \to a) \mapsto (f \circ g\colon s \to b)
\]
\[
s \otimes f = \mathrm{id}_s \otimes f\colon s \otimes a \mapsto s \otimes f(a)
\]
s for Writer at any point here.
Indeed, the functor instance for Writer doesn’t impose any restrictions on
the object s.And now, in fact, we observe that as functors, State s is the composition of
Reader s and Writer s.
The monad instances for State
Link to heading
The same can’t hold for the monad instances, simply because Writer demands
the object s to be a monoid while the monad instance of State doesn’t restrict
s at all. So, what is going on here?
Let’s take a look at the unit (or the return function). For Reader, it is given
by sending an object to the constant function:
\[
\lambda_R\colon a \to \mathrm{Hom}(s,a) , c \mapsto (y \mapsto c) = \mathrm{const}_c.
\]
As for Writer, here’s where the monoid instance comes into play. If s is a monoid,
we can send any object to the product with the neutral element $e_s$ of the monoid:
\[
\lambda_W\colon a \to s \otimes a , x \mapsto e_s \otimes x.
\]
Now, if State was the composition of Reader and Writer as monads, its return function
would need to be the composition of Reader’s and Writer’s. However,
\[
\lambda_R \circ \lambda_W\colon x \mapsto (y \mapsto e_s \otimes x) = \mathrm{const}_{e_s \otimes x}
\]
while State’s return function is given by
\[
\lambda_S\colon x \mapsto (y \mapsto y \otimes x).
\]
This is not the end of the story. We could still ask where this canonical monad
structure comes from if it doesn’t come from the monad instances of Reader and
Writer. The observation that State is the composition of Reader and Writer
as functors is the key: As it turns out, these two functors are adjoint, i.e. there’s
a natural isomorphism
\[
\mathrm{Hom}(s \otimes a, b) \cong \mathrm{Hom}(a, \mathrm{Hom}(s, b)).
\]
An adjunction always gives rise to a monad, with the monad’s structure maps (unit, join) coming from the natural isomorphism.
Conclusion Link to heading
To sum it up, State is not only the composition of Reader and Writer
as functors; its whole structure as a monad is determined by the adjunction between
Reader and Writer.