Reader
Since ask
and asks
are defined for ReaderT
instead of Reader
, how
come they work also for Reader
? The reason is that Reader
isn't defined as
newtype Reader r a = Reader { runReader :: r -> a }
at all. It only behaves as if it was. The real definition is
type Reader r a = ReaderT r Identity a
runReader :: Reader r a -> r -> a
runReader f = runIdentiry . runReader f
The Reader
monad simply stacks the decoration provided by the ReaderT
monad
transformer on top of the Identity
monad. But as we discussed before, the
Identity
monad doesn't provide any decoration at all. It's merely a wrapper
for pure values. Thus, Reader
decorates pure values with a context in the same
way that ReaderT
decorates computations in some monad m
with some context.