| Copyright | (c) Erich Gut |
|---|---|
| License | BSD3 |
| Maintainer | zerich.gut@gmail.com |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
OAlg.Data.Either
Description
disjoint union of data.
Either
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 IntString or an Int. The Left constructor can be used only on
Strings, and the Right constructor can be used only on Ints:
>>>let s = Left "foo" :: Either String Int>>>sLeft "foo">>>let n = Right 3 :: Either String Int>>>nRight 3>>>:type ss :: Either String Int>>>:type nn :: 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) sLeft "foo">>>fmap (*2) nRight 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 Ints.
>>>:{let parseMultiple :: Either String Int parseMultiple = do x <- parseEither '1' y <- parseEither '2' return (x + y)>>>:}
>>>parseMultipleRight 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)>>>:}
>>>parseMultipleLeft "parse error"
Instances
| 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 Methods liftReadsPrec2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> Int -> ReadS (Either a b) # liftReadList2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> ReadS [Either a b] # liftReadPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec (Either a b) # liftReadListPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec [Either a b] # | |
| Show2 Either | Since: base-4.9.0.0 |
| NFData2 Either | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
| Generic1 (Either a :: Type -> Type) | |
| (Lift a, Lift b) => Lift (Either a b :: Type) | |
| Foldable (Either a) | Since: base-4.7.0.0 |
Defined in Data.Foldable Methods fold :: Monoid m => Either a m -> m # foldMap :: Monoid m => (a0 -> m) -> Either a a0 -> m # foldMap' :: Monoid m => (a0 -> m) -> Either a a0 -> m # foldr :: (a0 -> b -> b) -> b -> Either a a0 -> b # foldr' :: (a0 -> b -> b) -> b -> Either a a0 -> b # foldl :: (b -> a0 -> b) -> b -> Either a a0 -> b # foldl' :: (b -> a0 -> b) -> b -> Either a a0 -> b # foldr1 :: (a0 -> a0 -> a0) -> Either a a0 -> a0 # foldl1 :: (a0 -> a0 -> a0) -> Either a a0 -> a0 # toList :: Either a a0 -> [a0] # length :: Either a a0 -> Int # elem :: Eq a0 => a0 -> Either a a0 -> Bool # maximum :: Ord a0 => Either a a0 -> a0 # minimum :: Ord a0 => Either a a0 -> a0 # | |
| 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 Methods liftReadsPrec :: (Int -> ReadS a0) -> ReadS [a0] -> Int -> ReadS (Either a a0) # liftReadList :: (Int -> ReadS a0) -> ReadS [a0] -> ReadS [Either a a0] # liftReadPrec :: ReadPrec a0 -> ReadPrec [a0] -> ReadPrec (Either a a0) # liftReadListPrec :: ReadPrec a0 -> ReadPrec [a0] -> ReadPrec [Either a a0] # | |
| Show a => Show1 (Either a) | Since: base-4.9.0.0 |
| Traversable (Either a) | Since: base-4.7.0.0 |
Defined in Data.Traversable | |
| Applicative (Either e) | Since: base-3.0 |
| Functor (Either a) | Since: base-3.0 |
| Monad (Either e) | Since: base-4.4.0.0 |
| NFData a => NFData1 (Either a) | Since: deepseq-1.4.3.0 |
Defined in Control.DeepSeq | |
| Semigroup (Either a b) | Since: base-4.9.0.0 |
| Generic (Either a b) | |
| (Read a, Read b) => Read (Either a b) | Since: base-3.0 |
| (Show a, Show b) => Show (Either a b) | Since: base-3.0 |
| (NFData a, NFData b) => NFData (Either a b) | |
Defined in Control.DeepSeq | |
| (Eq a, Eq b) => Eq (Either a b) | Since: base-2.1 |
| (Ord a, Ord b) => Ord (Either a b) | Since: base-2.1 |
| (Validable a, Validable b) => Validable (Either a b) Source # | |
| type Rep1 (Either a :: Type -> Type) | Since: base-4.6.0.0 |
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) | Since: base-4.6.0.0 |
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))) | |
Either2
data Either2 f g a b where Source #
disjoint union of two parameterized data types.
Instances
| (Applicative f, Applicative g) => Applicative (Either2 f g) Source # | |
Defined in OAlg.Category.Applicative | |
| (Morphism f, Morphism g, ObjectClass f ~ ObjectClass g) => Morphism (Either2 f g) Source # | |
Defined in OAlg.Category.Definition Associated Types type ObjectClass (Either2 f g) Source # Methods homomorphous :: Either2 f g x y -> Homomorphous (ObjectClass (Either2 f g)) x y Source # domain :: Either2 f g x y -> Struct (ObjectClass (Either2 f g)) x Source # range :: Either2 f g x y -> Struct (ObjectClass (Either2 f g)) y Source # | |
| (Eq2 f, Eq2 g) => Eq2 (Either2 f g) Source # | |
| (Show2 f, Show2 g) => Show2 (Either2 f g) Source # | |
| (Validable2 f, Validable2 g) => Validable2 (Either2 f g) Source # | |
| (Entity2 f, Entity2 g) => Entity2 (Either2 f g) Source # | |
Defined in OAlg.Entity.Definition | |
| (Show2 f, Show2 g) => Show (Either2 f g x y) Source # | |
| type ObjectClass (Either2 f g) Source # | |
Defined in OAlg.Category.Definition | |