[Haskell-beginners] understanding MTL

Karl Voelker karl at karlv.net
Sun Mar 30 04:19:14 UTC 2014


On Sat, Mar 29, 2014, at 03:32 AM, Dennis Raddle wrote:

He then said I don't want to put StateT and ErrorT in the same newtype
declaration, so it would be something like
newtype Er m a = Er { runEr :: StateT ErState m a }

On this point I'm confused. I don't know how to get from a line like
the above to eventually making a type that combines error handling and
state. Furthermore I need to define instances for things like
MonadError.


I don't think there was really anything wrong with your prior approach.
It seems like the advice you got was to make the Er type more generic,
which may or may not be appropriate in the context of your application.
At this point, Er is now a monad transformer. It's not really a monad
until the "m" type parameter is instantiated. For example:

type ErActuallyAMonad a = Er Error a

Notice that the value inside the newtype will then be a "StateT ErState
Error a", which is almost the same as the "ErrorT (State ErState) a"
you had originally.

He got me started with the line

instance MonadError e m => MonadError e (Er m) where
... define throwError and catchError which I have no idea how to do ...


An instance declaration like this is saying that if you have a
MonadError monad m, and you transform it with the transformer Er, then
the resulting monad will also be a MonadError monad. These instance
declarations are pretty common in the MTL.

-Karl
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140329/33f22a88/attachment.html>


More information about the Beginners mailing list