@@ -2,34 +2,25 @@ module Control.IOExcept
3-- An IO monad with exception handling
3-- An IO monad with exception handling
5−data IOExcept : Type -> Type -> Type where
5+record IOExcept err a where
6− IOM : IO (Either err a) -> IOExcept err a
7+ runIOExcept : IO (Either err a)
8instance Functor (IOExcept e) where
9instance Functor (IOExcept e) where
9 map f (IOM fn) = IOM (map (map f) fn)
10 map f (IOM fn) = IOM (map (map f) fn)
11instance Applicative (IOExcept e) where
12instance Applicative (IOExcept e) where
12 pure x = IOM (pure (pure x))
13 pure x = IOM (pure (pure x))
13− (IOM f) <*> (IOM a) = IOM (do f' <- f; a' <- a
14+ (IOM f) <*> (IOM a) = IOM [| f <*> a |]
16instance Monad (IOExcept e) where
16instance Monad (IOExcept e) where
17− (IOM x) >>= k = IOM (do x' <- x;
17+ (IOM x) >>= f = IOM $ x >>= either (pure . Left) (runIOExcept . f)
19− Right a => let (IOM ka) = k a in
21− Left err => return (Left err))
23ioe_lift : IO a -> IOExcept err a
19ioe_lift : IO a -> IOExcept err a
24−ioe_lift op = IOM (do op' <- op
20+ioe_lift op = IOM $ map Right op
27ioe_fail : err -> IOExcept err a
22ioe_fail : err -> IOExcept err a
28−ioe_fail e = IOM (return (Left e))
23+ioe_fail e = IOM $ pure (Left e)
30ioe_run : IOExcept err a -> (err -> IO b) -> (a -> IO b) -> IO b
25ioe_run : IOExcept err a -> (err -> IO b) -> (a -> IO b) -> IO b
31−ioe_run (IOM act) err ok = do act' <- act
26+ioe_run (IOM act) err ok = either err ok !act