Either -is:module

The Either type represents values with two possibilities: a value of type Either a b is either Left a or 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 Either String Int is the type of values which can be either a String 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

>>> 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 Ints.
>>> :{
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"
The Either type represents values with two possibilities: a value of type Either a b is either Left a or 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 Either String Int is the type of values which can be either a String 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

>>> 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 Ints.
>>> :{
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"
The strict choice type.
The strict choice type.
Case analysis for the Either type. If the value is Left a, apply the first function to a; if it is Right b, apply the second function to b.

Examples

We create two values of type Either String Int, one using the Left constructor and another using the Right constructor. Then we apply "either" the length function (if we have a String) or the "times-two" function (if we have an Int):
>>> let s = Left "foo" :: Either String Int

>>> let n = Right 3 :: Either String Int

>>> either length (*2) s
3

>>> either length (*2) n
6
Generates either an a or a b. As the size grows, this generator generates Rights more often than Lefts.
Combinators for working with sums Combinators for working with sums.
Case analysis: if the value is Left a, apply the first function to a; if it is Right b, apply the second function to b.
Combine two folds into a fold over inputs for either of them.
Case analysis for the Either type. If the value is Left a, apply the first function to a; if it is Right b, apply the second function to b.

Examples

We create two values of type Either String Int, one using the Left constructor and another using the Right constructor. Then we apply "either" the length function (if we have a String) or the "times-two" function (if we have an Int):
>>> let s = Left "foo" :: Either String Int

>>> let n = Right 3 :: Either String Int

>>> either length (*2) s
3

>>> either length (*2) n
6
Map an Either returning function on the next element in the stream. If the function returns 'Left err', the parser fails with the error message err otherwise returns the Right value. Pre-release
Choose left or right unfold based on an either input. Pre-release
Case analysis for symbolic Eithers. If the value isLeft, apply the first function; if it isRight, apply the second function.
>>> either (*2) (*3) (sLeft (3 :: SInteger))
6 :: SInteger

>>> either (*2) (*3) (sRight (3 :: SInteger))
9 :: SInteger

>>> let f = uninterpret "f" :: SInteger -> SInteger

>>> let g = uninterpret "g" :: SInteger -> SInteger

>>> prove $ \x -> either f g (sLeft x) .== f x
Q.E.D.

>>> prove $ \x -> either f g (sRight x) .== g x
Q.E.D.
Case analysis for the Either type. If the value is Left a, apply the first function to a; if it is Right b, apply the second function to b.

Examples

We create two values of type Either String Int, one using the Left constructor and another using the Right constructor. Then we apply "either" the length function (if we have a String) or the "times-two" function (if we have an Int):
>>> let s = Left "foo" :: Either String Int

>>> let n = Right 3 :: Either String Int

>>> either length (*2) s
3

>>> either length (*2) n
6
Try a decoder and get back a 'Right a' if it succeeds and a 'Left String' if it fails. In other words, this decoder always succeeds with an 'Either String a' value.
>>> decode (either string) "42"
Just (Left "expected String, but encountered Number")

>>> decode (either int) "42"
Just (Right 42)
If "Either e r" is the error monad, then "EitherR r e" is the corresponding success monad, where:
Tag type for Either to use it as a DSum.