Created
July 27, 2011 11:39
-
-
Save xfire/1109189 to your computer and use it in GitHub Desktop.
lifting Either to ErrorT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad.Error | |
execAna :: Int -> Either String (Int, Int) | |
execAna 0 = Left "ERROR" | |
execAna n = Right (n - 1, n + 1) | |
report :: Either String () -> IO () | |
report (Left err) = putStrLn $ "failed: " ++ err | |
report _ = putStrLn "complete" | |
liftToErrorT :: (Error a, Monad m) => Either a b -> ErrorT a m b | |
liftToErrorT (Left msg) = throwError msg | |
liftToErrorT (Right x) = return x | |
errorRun :: Int -> IO () | |
errorRun n = do | |
err <- runErrorT $ do | |
-- Either a b --return--> IO (Either a b) --ErrorT--> ErrorT a IO b | |
-- phew ;) | |
(a, b) <- ErrorT $ return $ execAna n | |
-- (a, b) <- liftToErrorT $ execAna n | |
liftIO $ putStrLn $ show a | |
liftIO $ putStrLn $ show b | |
report err | |
main :: IO () | |
main = do | |
errorRun 1 -- should work and return 0 and 2 | |
errorRun 0 -- should return an error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment