Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we interact with 2 MonadError constraints having 2 different error types?

Say I have a function

function1 :: (MonadError String m, MonadIO m) => m Int

function2 :: (MonadError Int m, MonadIO m) => m Int

and a function to transform Int into a String

renderException :: Int -> String

Is there a way to implement function3, which is reusing all 3 functions?

function3 :: (MonadError String m, MonadIO m) => m Int
-- add the results from function1, function2 and 
-- transform function2 error into a String
like image 679
Eric Avatar asked Oct 25 '17 11:10

Eric


1 Answers

It turns out that I can use runExceptT:

-- runExceptT :: ExceptT e m a -> m (Either e a)
function3 :: (MonadError String m, MonadIO m) => m Int
function3 =
  do ei <- runExceptT function1
     a  <- either (throwError . show) pure ei
     b  <- function2
     return (a + b)

So there's a way out of the MonadError e "trap". On the other hand I don't know how to encapsulate this runExcept / either throwError pattern.

like image 104
Eric Avatar answered Nov 14 '22 09:11

Eric