Skip to content

Instantly share code, notes, and snippets.

@xfire
Created July 27, 2011 11:39
Show Gist options
  • Save xfire/1109189 to your computer and use it in GitHub Desktop.
Save xfire/1109189 to your computer and use it in GitHub Desktop.
lifting Either to ErrorT
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