[Haskell-cafe] Correct usage of MonadError, ErrorT?

Daniel dagda at xtra.co.nz
Mon Jan 30 21:00:41 EST 2006


I've got some functions in MonadError with different Error types.  I would 
like to map errors of one Error type onto the other Error type.

It seems that the only facility for doing this is mapErrorT, but that seems to 
force me to work in ErrorT rather than any old instance of MonadError.

Am I barking up the wrong tree trying to keep the functions generalised over 
Is there some other paradigm I should use for passing errors across system 
layers in haskell?

I would like to do something like the following, but I don't know how to 
achieve the 'mapError errMap' part of g.

instance Error Int where
    strMsg s = 0

f :: (MonadError Int m, MonadIO m) => Int -> m String
f 0 = do
    liftIO $ putStrLn "The int is 0, that'll be an error."
    throwError (-27)
f i = do
    liftIO $ putStrLn ("The int is " ++ show i ++ ", we'll do the division.")
    return $ show (100 `div` i)

g :: (MonadError String m, MonadIO m) => Int -> m String
g i = do
    liftIO $ putStrLn ("Will try to divide by " ++ show i)
    mapError errMap (f i)

h :: (MonadIO m) => Int -> m String
h i = (runErrorT (g i)) >>= either
    (\e -> return ("had an error: " ++ e)) 


More information about the Haskell-Cafe mailing list