module Cgm.Data.Either (
(:|),
isRight,
mkRight,
mkLeft,
boolEither,
fromRight,
throwErrorT,
mapError
) where
import Cgm.Control.InFunctor
import Cgm.Data.Bool
import Control.Monad.Error
import Control.Exception
import Control.Arrow
infixr 2 :|
type a :| b = Either a b
isRight :: Either a b -> Bool
isRight = either (const False) (const True)
mkRight :: a :>> Either b a
mkRight = uncheckedStrictlyIncreasing Right
mkLeft :: a :>> Either a b
mkLeft = uncheckedStrictlyIncreasing Left
boolEither :: a -> b -> Bool -> Either a b
boolEither a b = bool (Left a) (Right b)
fromRight :: Either a b -> b
fromRight (Left _) = error "fromRight called on Left"
fromRight (Right x) = x
throwErrorT :: (Show e, MonadIO m) => ErrorT e m a -> m a
throwErrorT = (>>= liftIO . evaluate . either (error . show) id) . runErrorT
mapError :: Monad m => (e -> e') -> ErrorT e m a -> ErrorT e' m a
mapError e = mapErrorT $ liftM $ left e