[Haskell-beginners] Monad Transformer Type Signatures

snk_kid korcan_h at hotmail.com
Tue Feb 15 14:12:51 CET 2011

Brent Yorgey <byorgey <at> seas.upenn.edu> writes:

> On Mon, Feb 14, 2011 at 02:10:13PM -0500, Britt Anderson wrote:
> > I am looking at code from a tutorial that has the following type
> > signature for a function:
> > 
> > getScreen :: MonadReader AppConfig m => m Surface
> > 
> > AppConfig is a data structure that was defined previously. My question
> > is how am I to interpret this type signature? Is it any m belonging to
> > the MonadReader AppConfig class? 
> Yes.  And there will be an instance for MonadReader AppConfig m
> exactly when m carries around an implicit "read-only" AppConfig value,
> available for querying with the 'ask' and 'asks' functions.
> > Since I can't see any instance of the MonadReader
> > Class being defined for MonadReader AppConfig I am puzzled.
> Typically what you will see is some concrete monad later being defined
> in terms of ReaderT AppConfig, which will automatically give it an
> appropriate MonadReader instance, allowing users of the monad to query
> the AppConfig.
> > I would appreciate some clarification or a pointer to a prior
> > discussion of this issue. I have tried to look on my own.
> I recommend reading Cale Gibbard's article "How to use monad
> transformers"... except at the moment I can't seem to find it
> anywhere. =(
> -Brent

This looks like my code, just to add this is this requires Flexible Contexts
extension (for which I believe became part of the Haskell 2010 standard if I
remember correctly) I didn't need to write it this way, I could have used the
specific monad transformer stack that I use. The only reason to use a typeclass
is for more generic code, it makes the code decoupled from a specific monad 
stack (any monad type for that matter, any which implements 
MonadReader AppConfig).

Also note that at the time I didn't know much about first-class record libraries
like fclabels which could have reduce repeative boiler-plate code I wrote in
later tutorials.

More information about the Beginners mailing list