{-# language CPP #-}
module Data.Either.Combinators
( isLeft
, isRight
, fromLeft
, fromRight
, fromLeft'
, fromRight'
, mapBoth
, mapLeft
, mapRight
, whenLeft
, whenRight
, unlessLeft
, unlessRight
, leftToMaybe
, rightToMaybe
, maybeToLeft
, maybeToRight
, eitherToError
, swapEither
) where
#if __GLASGOW_HASKELL__ < 710
import Control.Applicative
#endif
import Control.Monad.Error.Class ( MonadError(throwError) )
isLeft :: Either a b -> Bool
isLeft :: forall a b. Either a b -> Bool
isLeft (Left a
_) = Bool
True
isLeft Either a b
_ = Bool
False
isRight :: Either a b -> Bool
isRight :: forall a b. Either a b -> Bool
isRight (Right b
_) = Bool
True
isRight Either a b
_ = Bool
False
fromLeft' :: Either a b -> a
fromLeft' :: forall a b. Either a b -> a
fromLeft' (Right b
_) = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"Data.Either.Combinators.fromLeft' encountered a value of form 'Right _', consider using Data.Either.Combinators.fromLeft with a default value."
fromLeft' (Left a
x) = a
x
fromRight' :: Either a b -> b
fromRight' :: forall a b. Either a b -> b
fromRight' (Left a
_) = [Char] -> b
forall a. HasCallStack => [Char] -> a
error [Char]
"Data.Either.Combinators.fromRight' encountered a value of form 'Left _', consider using Data.Either.Combinators.fromRight with a default value."
fromRight' (Right b
x) = b
x
mapBoth :: (a -> c) -> (b -> d) -> Either a b -> Either c d
mapBoth :: forall a c b d. (a -> c) -> (b -> d) -> Either a b -> Either c d
mapBoth a -> c
f b -> d
_ (Left a
x) = c -> Either c d
forall a b. a -> Either a b
Left (a -> c
f a
x)
mapBoth a -> c
_ b -> d
f (Right b
x) = d -> Either c d
forall a b. b -> Either a b
Right (b -> d
f b
x)
mapLeft :: (a -> c) -> Either a b -> Either c b
mapLeft :: forall a c b. (a -> c) -> Either a b -> Either c b
mapLeft a -> c
f = (a -> c) -> (b -> b) -> Either a b -> Either c b
forall a c b d. (a -> c) -> (b -> d) -> Either a b -> Either c d
mapBoth a -> c
f b -> b
forall a. a -> a
id
mapRight :: (b -> c) -> Either a b -> Either a c
mapRight :: forall b c a. (b -> c) -> Either a b -> Either a c
mapRight = (a -> a) -> (b -> c) -> Either a b -> Either a c
forall a c b d. (a -> c) -> (b -> d) -> Either a b -> Either c d
mapBoth a -> a
forall a. a -> a
id
whenLeft :: Applicative m => Either a b -> (a -> m ()) -> m ()
whenLeft :: forall (m :: * -> *) a b.
Applicative m =>
Either a b -> (a -> m ()) -> m ()
whenLeft (Left a
x) a -> m ()
f = a -> m ()
f a
x
whenLeft Either a b
_ a -> m ()
_ = () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
whenRight :: Applicative m => Either a b -> (b -> m ()) -> m ()
whenRight :: forall (m :: * -> *) a b.
Applicative m =>
Either a b -> (b -> m ()) -> m ()
whenRight (Right b
x) b -> m ()
f = b -> m ()
f b
x
whenRight Either a b
_ b -> m ()
_ = () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
unlessLeft :: Applicative m => Either a b -> (b -> m ()) -> m ()
unlessLeft :: forall (m :: * -> *) a b.
Applicative m =>
Either a b -> (b -> m ()) -> m ()
unlessLeft = Either a b -> (b -> m ()) -> m ()
forall (m :: * -> *) a b.
Applicative m =>
Either a b -> (b -> m ()) -> m ()
whenRight
unlessRight :: Applicative m => Either a b -> (a -> m ()) -> m ()
unlessRight :: forall (m :: * -> *) a b.
Applicative m =>
Either a b -> (a -> m ()) -> m ()
unlessRight = Either a b -> (a -> m ()) -> m ()
forall (m :: * -> *) a b.
Applicative m =>
Either a b -> (a -> m ()) -> m ()
whenLeft
fromLeft :: a -> Either a b -> a
fromLeft :: forall a b. a -> Either a b -> a
fromLeft a
_ (Left a
x) = a
x
fromLeft a
x Either a b
_ = a
x
fromRight :: b -> Either a b -> b
fromRight :: forall b a. b -> Either a b -> b
fromRight b
_ (Right b
x) = b
x
fromRight b
x Either a b
_ = b
x
leftToMaybe :: Either a b -> Maybe a
leftToMaybe :: forall a b. Either a b -> Maybe a
leftToMaybe = (a -> Maybe a) -> (b -> Maybe a) -> Either a b -> Maybe a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> Maybe a
forall a. a -> Maybe a
Just (Maybe a -> b -> Maybe a
forall a b. a -> b -> a
const Maybe a
forall a. Maybe a
Nothing)
rightToMaybe :: Either a b -> Maybe b
rightToMaybe :: forall a b. Either a b -> Maybe b
rightToMaybe = (a -> Maybe b) -> (b -> Maybe b) -> Either a b -> Maybe b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe b -> a -> Maybe b
forall a b. a -> b -> a
const Maybe b
forall a. Maybe a
Nothing) b -> Maybe b
forall a. a -> Maybe a
Just
maybeToLeft :: b -> Maybe a -> Either a b
maybeToLeft :: forall b a. b -> Maybe a -> Either a b
maybeToLeft b
_ (Just a
x) = a -> Either a b
forall a b. a -> Either a b
Left a
x
maybeToLeft b
y Maybe a
Nothing = b -> Either a b
forall a b. b -> Either a b
Right b
y
maybeToRight :: b -> Maybe a -> Either b a
maybeToRight :: forall b a. b -> Maybe a -> Either b a
maybeToRight b
_ (Just a
x) = a -> Either b a
forall a b. b -> Either a b
Right a
x
maybeToRight b
y Maybe a
Nothing = b -> Either b a
forall a b. a -> Either a b
Left b
y
eitherToError :: (MonadError e m) => Either e a -> m a
eitherToError :: forall e (m :: * -> *) a. MonadError e m => Either e a -> m a
eitherToError = (e -> m a) -> (a -> m a) -> Either e a -> m a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either e -> m a
forall a. e -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
swapEither :: Either e a -> Either a e
swapEither :: forall e a. Either e a -> Either a e
swapEither = (e -> Either a e) -> (a -> Either a e) -> Either e a -> Either a e
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either e -> Either a e
forall a b. b -> Either a b
Right a -> Either a e
forall a b. a -> Either a b
Left
{-# INLINE swapEither #-}