Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- see Control.Monad.Except
- action :: (MonadExcept String m, MonadWriter String m) =>
- m ()
- action = do tell "a"
- tell "b"
- throwError "c"
- tell "d"
- -- run action and massage it into your format
- yourOutput :: (Maybe String, String)
- yourOutput = runWriter $ fmap (either Just (const Nothing)) $ runExceptT actions
- action :: MonadWriter String m => m ()
- action = do tell "a"
- tell "b"
- error "c"
- tell "d"
- runWriter action =
- ( ()
- , "a" ++ "b" ++ (case error "c" of (_, c) -> c) ++ "d"
- )
- -- can also be seen as Free (a,) () -> Free (a,) (Maybe e)
- -- essentially, that type encodes the intuition that a list may end in [] (nil)
- -- or in an error
- salvageList :: Exception e => [a] -> IO ([a], Maybe e)
- salvageList xs = catch (do xs' <- evaluate xs
- case xs' of
- [] -> return ([], Nothing)
- (x : tl) -> do (tl', e) <- salvageList tl
- return (x : tl', e)
- ) (e -> return ([], Just e))
- -- we get the return value, too! that makes me feel... surprisingly weirded out!
- yourOutputPlus :: IO ((), Maybe String, String)
- yourOutputPlus = do let (val, log) = runWriter action
- (realLog, error) <- salvageList log
- return (val, fmap ((ErrorCall msg) -> msg) error, realLog)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement