[Haskell-cafe] Using MonadError within other Monads

Karl Grapone kgrapone at gmail.com
Mon Dec 19 04:21:36 EST 2005


On 12/19/05, Andrew Pimlott <andrew at pimlott.net> wrote:
> [It is best to post questions only to haskell-cafe.]

Roger.

>
> On Mon, Dec 19, 2005 at 03:53:53PM +1300, Karl Grapone wrote:
> > I'm having trouble making use of MonadError within another Monad, in
> > this case IO.
snip
> Looking at the signature of f,
>
> > f :: IO (Either String String)
>
> "Either String String" is just an ordinary value produced in the IO
> monad--the monad structure of IO and "Either String" are completely
> independent.
snip
> The outer do is a computation in the IO monad, the inner do is a
> computation in the "Either String" monad, and the net effect is an IO
> computation returning an "Either String" computation--which is just what
> the type signature says.  I had to change your code in 3 other places to
> make it type-check; hopefully you can now find them. ;-)

No problem, with a little shove in the right direction :).
Not entirely sure why I didn't arrive at it myself.  I'm using StateT
successfully elsewhere and was overeager in using similar methods
which, as you point out below, is a different situation.
I still find monadic signatures a little confusing, I believe ghci
was, at some point, deriving types for f or g that had MonadError in
place of Either String, so I'll have to think carefully about why it
is so different from the ErrorT type you show below.

>
> When people speak of nesting monads, they often mean using monad
> transformers.  If you were using the ErrorT monad transformers, your
> signature would look like
>
>     f :: ErrorT String IO String
>
> You might want to try rewriting your code that way, but I would suggest
> making it work with the current type signatures first.

At one point I was playing with types like 'Either String (IO
String)', which to me seems very similar to the ErrorT type. 
Intuitively they don't seem like they'd be correct... I feel like
determining that there is an error involves interaction with the
outside world, consequently the error part should be inside the IO
monad.

Now that you've cleared up the simpler case for me I'll try out the ErrorT case.

Thanks for the help.
Karl


More information about the Haskell-Cafe mailing list