Safe Haskell | None |
---|---|
Language | Haskell2010 |
Documentation
The Either
type represents values with two possibilities: a value of
type
is either Either
a b
or Left
a
.Right
b
The Either
type is sometimes used to represent a value which is
either correct or an error; by convention, the Left
constructor is
used to hold an error value and the Right
constructor is used to
hold a correct value (mnemonic: "right" also means "correct").
Examples
The type
is the type of values which can be either
a Either
String
Int
String
or an Int
. The Left
constructor can be used only on
String
s, and the Right
constructor can be used only on Int
s:
>>>
let s = Left "foo" :: Either String Int
>>>
s
Left "foo">>>
let n = Right 3 :: Either String Int
>>>
n
Right 3>>>
:type s
s :: Either String Int>>>
:type n
n :: Either String Int
The fmap
from our Functor
instance will ignore Left
values, but
will apply the supplied function to values contained in a Right
:
>>>
let s = Left "foo" :: Either String Int
>>>
let n = Right 3 :: Either String Int
>>>
fmap (*2) s
Left "foo">>>
fmap (*2) n
Right 6
The Monad
instance for Either
allows us to chain together multiple
actions which may fail, and fail overall if any of the individual
steps failed. First we'll write a function that can either parse an
Int
from a Char
, or fail.
>>>
import Data.Char ( digitToInt, isDigit )
>>>
:{
let parseEither :: Char -> Either String Int parseEither c | isDigit c = Right (digitToInt c) | otherwise = Left "parse error">>>
:}
The following should work, since both '1'
and '2'
can be
parsed as Int
s.
>>>
:{
let parseMultiple :: Either String Int parseMultiple = do x <- parseEither '1' y <- parseEither '2' return (x + y)>>>
:}
>>>
parseMultiple
Right 3
But the following should fail overall, since the first operation where
we attempt to parse 'm'
as an Int
will fail:
>>>
:{
let parseMultiple :: Either String Int parseMultiple = do x <- parseEither 'm' y <- parseEither '2' return (x + y)>>>
:}
>>>
parseMultiple
Left "parse error"
Instances
Bifoldable Either | Since: base-4.10.0.0 |
Bifunctor Either | Since: base-4.8.0.0 |
Eq2 Either | Since: base-4.9.0.0 |
Ord2 Either | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes | |
Read2 Either | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes liftReadsPrec2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> Int -> ReadS (Either a b) Source # liftReadList2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> ReadS [Either a b] Source # liftReadPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec (Either a b) Source # liftReadListPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec [Either a b] Source # | |
Show2 Either | Since: base-4.9.0.0 |
NFData2 Either | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
FromJSON2 Either | |
Defined in Data.Aeson.Types.FromJSON liftParseJSON2 :: (Value -> Parser a) -> (Value -> Parser [a]) -> (Value -> Parser b) -> (Value -> Parser [b]) -> Value -> Parser (Either a b) liftParseJSONList2 :: (Value -> Parser a) -> (Value -> Parser [a]) -> (Value -> Parser b) -> (Value -> Parser [b]) -> Value -> Parser [Either a b] | |
ToJSON2 Either | |
Defined in Data.Aeson.Types.ToJSON liftToJSON2 :: (a -> Value) -> ([a] -> Value) -> (b -> Value) -> ([b] -> Value) -> Either a b -> Value liftToJSONList2 :: (a -> Value) -> ([a] -> Value) -> (b -> Value) -> ([b] -> Value) -> [Either a b] -> Value liftToEncoding2 :: (a -> Encoding) -> ([a] -> Encoding) -> (b -> Encoding) -> ([b] -> Encoding) -> Either a b -> Encoding liftToEncodingList2 :: (a -> Encoding) -> ([a] -> Encoding) -> (b -> Encoding) -> ([b] -> Encoding) -> [Either a b] -> Encoding | |
Hashable2 Either | |
Defined in Data.Hashable.Class | |
Bitraversable1 Either | |
Defined in Data.Semigroup.Traversable.Class bitraverse1 :: Apply f => (a -> f b) -> (c -> f d) -> Either a c -> f (Either b d) bisequence1 :: Apply f => Either (f a) (f b) -> f (Either a b) | |
MonadError e (Either e) | |
Defined in Control.Monad.Error.Class throwError :: e -> Either e a catchError :: Either e a -> (e -> Either e a) -> Either e a | |
(Lift a, Lift b) => Lift (Either a b :: Type) | |
(Typeable DefaultUni a, Typeable DefaultUni b, Lift DefaultUni a, Lift DefaultUni b) => Lift DefaultUni (Either a b) Source # | |
Monad (Either e) | Since: base-4.4.0.0 |
Functor (Either a) | Since: base-3.0 |
MonadFix (Either e) | Since: base-4.3.0.0 |
Applicative (Either e) | Since: base-3.0 |
Defined in Data.Either | |
Foldable (Either a) | Since: base-4.7.0.0 |
Defined in Data.Foldable fold :: Monoid m => Either a m -> m Source # foldMap :: Monoid m => (a0 -> m) -> Either a a0 -> m Source # foldMap' :: Monoid m => (a0 -> m) -> Either a a0 -> m Source # foldr :: (a0 -> b -> b) -> b -> Either a a0 -> b Source # foldr' :: (a0 -> b -> b) -> b -> Either a a0 -> b Source # foldl :: (b -> a0 -> b) -> b -> Either a a0 -> b Source # foldl' :: (b -> a0 -> b) -> b -> Either a a0 -> b Source # foldr1 :: (a0 -> a0 -> a0) -> Either a a0 -> a0 Source # foldl1 :: (a0 -> a0 -> a0) -> Either a a0 -> a0 Source # toList :: Either a a0 -> [a0] Source # null :: Either a a0 -> Bool Source # length :: Either a a0 -> Int Source # elem :: Eq a0 => a0 -> Either a a0 -> Bool Source # maximum :: Ord a0 => Either a a0 -> a0 Source # minimum :: Ord a0 => Either a a0 -> a0 Source # | |
Traversable (Either a) | Since: base-4.7.0.0 |
Defined in Data.Traversable | |
Eq a => Eq1 (Either a) | Since: base-4.9.0.0 |
Ord a => Ord1 (Either a) | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes | |
Read a => Read1 (Either a) | Since: base-4.9.0.0 |
Defined in Data.Functor.Classes liftReadsPrec :: (Int -> ReadS a0) -> ReadS [a0] -> Int -> ReadS (Either a a0) Source # liftReadList :: (Int -> ReadS a0) -> ReadS [a0] -> ReadS [Either a a0] Source # liftReadPrec :: ReadPrec a0 -> ReadPrec [a0] -> ReadPrec (Either a a0) Source # liftReadListPrec :: ReadPrec a0 -> ReadPrec [a0] -> ReadPrec [Either a a0] Source # | |
Show a => Show1 (Either a) | Since: base-4.9.0.0 |
NFData a => NFData1 (Either a) | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
FromJSON a => FromJSON1 (Either a) | |
Defined in Data.Aeson.Types.FromJSON liftParseJSON :: (Value -> Parser a0) -> (Value -> Parser [a0]) -> Value -> Parser (Either a a0) liftParseJSONList :: (Value -> Parser a0) -> (Value -> Parser [a0]) -> Value -> Parser [Either a a0] | |
ToJSON a => ToJSON1 (Either a) | |
Defined in Data.Aeson.Types.ToJSON liftToJSON :: (a0 -> Value) -> ([a0] -> Value) -> Either a a0 -> Value liftToJSONList :: (a0 -> Value) -> ([a0] -> Value) -> [Either a a0] -> Value liftToEncoding :: (a0 -> Encoding) -> ([a0] -> Encoding) -> Either a a0 -> Encoding liftToEncodingList :: (a0 -> Encoding) -> ([a0] -> Encoding) -> [Either a a0] -> Encoding | |
Hashable a => Hashable1 (Either a) | |
Defined in Data.Hashable.Class | |
Functor (Either c) Source # | |
MonadFailure (Either a) | |
Applicative (Either a) Source # | |
Foldable (Either c) Source # | |
Traversable (Either c) Source # | |
Defined in PlutusTx.Traversable | |
Generic1 (Either a :: Type -> Type) | Since: base-4.6.0.0 |
MonadBaseControl (Either e) (Either e) | |
Defined in Control.Monad.Trans.Control type StM (Either e) a | |
(Eq a, Eq b) => Eq (Either a b) | Since: base-2.1 |
(Data a, Data b) => Data (Either a b) | Since: base-4.0.0.0 |
Defined in Data.Data gfoldl :: (forall d b0. Data d => c (d -> b0) -> d -> c b0) -> (forall g. g -> c g) -> Either a b -> c (Either a b) Source # gunfold :: (forall b0 r. Data b0 => c (b0 -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Either a b) Source # toConstr :: Either a b -> Constr Source # dataTypeOf :: Either a b -> DataType Source # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Either a b)) Source # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Either a b)) Source # gmapT :: (forall b0. Data b0 => b0 -> b0) -> Either a b -> Either a b Source # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Either a b -> r Source # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Either a b -> r Source # gmapQ :: (forall d. Data d => d -> u) -> Either a b -> [u] Source # gmapQi :: Int -> (forall d. Data d => d -> u) -> Either a b -> u Source # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Either a b -> m (Either a b) Source # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Either a b -> m (Either a b) Source # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Either a b -> m (Either a b) Source # | |
(Ord a, Ord b) => Ord (Either a b) | Since: base-2.1 |
Defined in Data.Either compare :: Either a b -> Either a b -> Ordering Source # (<) :: Either a b -> Either a b -> Bool Source # (<=) :: Either a b -> Either a b -> Bool Source # (>) :: Either a b -> Either a b -> Bool Source # (>=) :: Either a b -> Either a b -> Bool Source # | |
(Read a, Read b) => Read (Either a b) | Since: base-3.0 |
(Show a, Show b) => Show (Either a b) | Since: base-3.0 |
Generic (Either a b) | Since: base-4.6.0.0 |
Semigroup (Either a b) | Since: base-4.9.0.0 |
(NFData a, NFData b) => NFData (Either a b) | |
Defined in Control.DeepSeq | |
(Serialise a, Serialise b) => Serialise (Either a b) | |
Defined in Codec.Serialise.Class encode :: Either a b -> Encoding decode :: Decoder s (Either a b) encodeList :: [Either a b] -> Encoding decodeList :: Decoder s [Either a b] | |
(FromJSON a, FromJSON b) => FromJSON (Either a b) | |
Defined in Data.Aeson.Types.FromJSON parseJSON :: Value -> Parser (Either a b) parseJSONList :: Value -> Parser [Either a b] | |
(ToJSON a, ToJSON b) => ToJSON (Either a b) | |
Defined in Data.Aeson.Types.ToJSON toEncoding :: Either a b -> Encoding toJSONList :: [Either a b] -> Value toEncodingList :: [Either a b] -> Encoding | |
Corecursive (Either a b) | |
Defined in Data.Functor.Foldable embed :: Base (Either a b) (Either a b) -> Either a b ana :: (a0 -> Base (Either a b) a0) -> a0 -> Either a b apo :: (a0 -> Base (Either a b) (Either (Either a b) a0)) -> a0 -> Either a b postpro :: Recursive (Either a b) => (forall b0. Base (Either a b) b0 -> Base (Either a b) b0) -> (a0 -> Base (Either a b) a0) -> a0 -> Either a b gpostpro :: (Recursive (Either a b), Monad m) => (forall b0. m (Base (Either a b) b0) -> Base (Either a b) (m b0)) -> (forall c. Base (Either a b) c -> Base (Either a b) c) -> (a0 -> Base (Either a b) (m a0)) -> a0 -> Either a b | |
Recursive (Either a b) | |
Defined in Data.Functor.Foldable project :: Either a b -> Base (Either a b) (Either a b) cata :: (Base (Either a b) a0 -> a0) -> Either a b -> a0 para :: (Base (Either a b) (Either a b, a0) -> a0) -> Either a b -> a0 gpara :: (Corecursive (Either a b), Comonad w) => (forall b0. Base (Either a b) (w b0) -> w (Base (Either a b) b0)) -> (Base (Either a b) (EnvT (Either a b) w a0) -> a0) -> Either a b -> a0 prepro :: Corecursive (Either a b) => (forall b0. Base (Either a b) b0 -> Base (Either a b) b0) -> (Base (Either a b) a0 -> a0) -> Either a b -> a0 gprepro :: (Corecursive (Either a b), Comonad w) => (forall b0. Base (Either a b) (w b0) -> w (Base (Either a b) b0)) -> (forall c. Base (Either a b) c -> Base (Either a b) c) -> (Base (Either a b) (w a0) -> a0) -> Either a b -> a0 | |
(Hashable a, Hashable b) => Hashable (Either a b) | |
Defined in Data.Hashable.Class | |
MonoFoldable (Either a b) | |
Defined in Data.MonoTraversable ofoldMap :: Monoid m => (Element (Either a b) -> m) -> Either a b -> m ofoldr :: (Element (Either a b) -> b0 -> b0) -> b0 -> Either a b -> b0 ofoldl' :: (a0 -> Element (Either a b) -> a0) -> a0 -> Either a b -> a0 otoList :: Either a b -> [Element (Either a b)] oall :: (Element (Either a b) -> Bool) -> Either a b -> Bool oany :: (Element (Either a b) -> Bool) -> Either a b -> Bool olength64 :: Either a b -> Int64 ocompareLength :: Integral i => Either a b -> i -> Ordering otraverse_ :: Applicative f => (Element (Either a b) -> f b0) -> Either a b -> f () ofor_ :: Applicative f => Either a b -> (Element (Either a b) -> f b0) -> f () omapM_ :: Applicative m => (Element (Either a b) -> m ()) -> Either a b -> m () oforM_ :: Applicative m => Either a b -> (Element (Either a b) -> m ()) -> m () ofoldlM :: Monad m => (a0 -> Element (Either a b) -> m a0) -> a0 -> Either a b -> m a0 ofoldMap1Ex :: Semigroup m => (Element (Either a b) -> m) -> Either a b -> m ofoldr1Ex :: (Element (Either a b) -> Element (Either a b) -> Element (Either a b)) -> Either a b -> Element (Either a b) ofoldl1Ex' :: (Element (Either a b) -> Element (Either a b) -> Element (Either a b)) -> Either a b -> Element (Either a b) headEx :: Either a b -> Element (Either a b) lastEx :: Either a b -> Element (Either a b) unsafeHead :: Either a b -> Element (Either a b) unsafeLast :: Either a b -> Element (Either a b) maximumByEx :: (Element (Either a b) -> Element (Either a b) -> Ordering) -> Either a b -> Element (Either a b) minimumByEx :: (Element (Either a b) -> Element (Either a b) -> Ordering) -> Either a b -> Element (Either a b) | |
MonoTraversable (Either a b) | |
Defined in Data.MonoTraversable | |
MonoFunctor (Either a b) | |
MonoPointed (Either a b) | |
Defined in Data.MonoTraversable | |
(Eq a, Eq b) => Eq (Either a b) Source # | |
(Ord a, Ord b) => Ord (Either a b) Source # | |
Defined in PlutusTx.Ord compare :: Either a b -> Either a b -> Ordering Source # (<) :: Either a b -> Either a b -> Bool Source # (<=) :: Either a b -> Either a b -> Bool Source # (>) :: Either a b -> Either a b -> Bool Source # (>=) :: Either a b -> Either a b -> Bool Source # | |
(UnsafeFromData a, UnsafeFromData b) => UnsafeFromData (Either a b) Source # | |
Defined in PlutusTx.IsData.Instances unsafeFromBuiltinData :: BuiltinData -> Either a b Source # | |
(FromData a, FromData b) => FromData (Either a b) Source # | |
Defined in PlutusTx.IsData.Instances fromBuiltinData :: BuiltinData -> Maybe (Either a b) Source # | |
(ToData a, ToData b) => ToData (Either a b) Source # | |
Defined in PlutusTx.IsData.Instances toBuiltinData :: Either a b -> BuiltinData Source # | |
Typeable DefaultUni Either Source # | |
(a ~ a', b ~ b') => Each (Either a a') (Either b b') a b | |
Defined in Control.Lens.Each | |
(a ~ a', b ~ b') => Each (Either a a') (Either b b') a b | |
Defined in Lens.Micro.Internal | |
(FoldableWithIndex i f, FoldableWithIndex j g) => FoldableWithIndex (Either i j) (Sum f g) | |
Defined in WithIndex ifoldMap :: Monoid m => (Either i j -> a -> m) -> Sum f g a -> m ifoldMap' :: Monoid m => (Either i j -> a -> m) -> Sum f g a -> m ifoldr :: (Either i j -> a -> b -> b) -> b -> Sum f g a -> b ifoldl :: (Either i j -> b -> a -> b) -> b -> Sum f g a -> b ifoldr' :: (Either i j -> a -> b -> b) -> b -> Sum f g a -> b ifoldl' :: (Either i j -> b -> a -> b) -> b -> Sum f g a -> b | |
(FoldableWithIndex i f, FoldableWithIndex j g) => FoldableWithIndex (Either i j) (Product f g) | |
Defined in WithIndex ifoldMap :: Monoid m => (Either i j -> a -> m) -> Product f g a -> m ifoldMap' :: Monoid m => (Either i j -> a -> m) -> Product f g a -> m ifoldr :: (Either i j -> a -> b -> b) -> b -> Product f g a -> b ifoldl :: (Either i j -> b -> a -> b) -> b -> Product f g a -> b ifoldr' :: (Either i j -> a -> b -> b) -> b -> Product f g a -> b ifoldl' :: (Either i j -> b -> a -> b) -> b -> Product f g a -> b | |
(FoldableWithIndex i f, FoldableWithIndex j g) => FoldableWithIndex (Either i j) (f :+: g) | |
Defined in WithIndex ifoldMap :: Monoid m => (Either i j -> a -> m) -> (f :+: g) a -> m ifoldMap' :: Monoid m => (Either i j -> a -> m) -> (f :+: g) a -> m ifoldr :: (Either i j -> a -> b -> b) -> b -> (f :+: g) a -> b ifoldl :: (Either i j -> b -> a -> b) -> b -> (f :+: g) a -> b ifoldr' :: (Either i j -> a -> b -> b) -> b -> (f :+: g) a -> b ifoldl' :: (Either i j -> b -> a -> b) -> b -> (f :+: g) a -> b | |
(FoldableWithIndex i f, FoldableWithIndex j g) => FoldableWithIndex (Either i j) (f :*: g) | |
Defined in WithIndex ifoldMap :: Monoid m => (Either i j -> a -> m) -> (f :*: g) a -> m ifoldMap' :: Monoid m => (Either i j -> a -> m) -> (f :*: g) a -> m ifoldr :: (Either i j -> a -> b -> b) -> b -> (f :*: g) a -> b ifoldl :: (Either i j -> b -> a -> b) -> b -> (f :*: g) a -> b ifoldr' :: (Either i j -> a -> b -> b) -> b -> (f :*: g) a -> b ifoldl' :: (Either i j -> b -> a -> b) -> b -> (f :*: g) a -> b | |
(FunctorWithIndex i f, FunctorWithIndex j g) => FunctorWithIndex (Either i j) (Sum f g) | |
(FunctorWithIndex i f, FunctorWithIndex j g) => FunctorWithIndex (Either i j) (Product f g) | |
(FunctorWithIndex i f, FunctorWithIndex j g) => FunctorWithIndex (Either i j) (f :+: g) | |
(FunctorWithIndex i f, FunctorWithIndex j g) => FunctorWithIndex (Either i j) (f :*: g) | |
(TraversableWithIndex i f, TraversableWithIndex j g) => TraversableWithIndex (Either i j) (Sum f g) | |
(TraversableWithIndex i f, TraversableWithIndex j g) => TraversableWithIndex (Either i j) (Product f g) | |
(TraversableWithIndex i f, TraversableWithIndex j g) => TraversableWithIndex (Either i j) (f :+: g) | |
(TraversableWithIndex i f, TraversableWithIndex j g) => TraversableWithIndex (Either i j) (f :*: g) | |
type Failure (Either a) | |
Defined in Basement.Monad type Failure (Either a) = a | |
type StM (Either e) a | |
Defined in Control.Monad.Trans.Control type StM (Either e) a = a | |
type Rep1 (Either a :: Type -> Type) | |
Defined in GHC.Generics type Rep1 (Either a :: Type -> Type) = D1 ('MetaData "Either" "Data.Either" "base" 'False) (C1 ('MetaCons "Left" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)) :+: C1 ('MetaCons "Right" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1)) | |
type Rep (Either a b) | |
Defined in GHC.Generics type Rep (Either a b) = D1 ('MetaData "Either" "Data.Either" "base" 'False) (C1 ('MetaCons "Left" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)) :+: C1 ('MetaCons "Right" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 b))) | |
type Base (Either a b) | |
type Element (Either a b) | |
Defined in Data.MonoTraversable type Element (Either a b) = b |