linear-base-0.1.0: Standard library for linear types.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Either.Linear

Description

This module contains useful functions for working with Eithers.

Synopsis

Documentation

data Either a b #

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

Expand

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"

Constructors

Left a 
Right b 

Instances

Instances details
Show2 Either

Since: base-4.9.0.0

Instance details

Defined in Data.Functor.Classes

Methods

liftShowsPrec2 :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> (Int -> b -> ShowS) -> ([b] -> ShowS) -> Int -> Either a b -> ShowS #

liftShowList2 :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> (Int -> b -> ShowS) -> ([b] -> ShowS) -> [Either a b] -> ShowS #

Read2 Either

Since: base-4.9.0.0

Instance details

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] #

Ord2 Either

Since: base-4.9.0.0

Instance details

Defined in Data.Functor.Classes

Methods

liftCompare2 :: (a -> b -> Ordering) -> (c -> d -> Ordering) -> Either a c -> Either b d -> Ordering #

Eq2 Either

Since: base-4.9.0.0

Instance details

Defined in Data.Functor.Classes

Methods

liftEq2 :: (a -> b -> Bool) -> (c -> d -> Bool) -> Either a c -> Either b d -> Bool #

Bifunctor Either

Since: base-4.8.0.0

Instance details

Defined in Data.Bifunctor

Methods

bimap :: (a -> b) -> (c -> d) -> Either a c -> Either b d #

first :: (a -> b) -> Either a c -> Either b c #

second :: (b -> c) -> Either a b -> Either a c #

Hashable2 Either 
Instance details

Defined in Data.Hashable.Class

Methods

liftHashWithSalt2 :: (Int -> a -> Int) -> (Int -> b -> Int) -> Int -> Either a b -> Int #

Bifunctor Either Source # 
Instance details

Defined in Data.Bifunctor.Linear.Internal.Bifunctor

Methods

bimap :: (a %1 -> b) -> (c %1 -> d) -> Either a c %1 -> Either b d Source #

first :: (a %1 -> b) -> Either a c %1 -> Either b c Source #

second :: (b %1 -> c) -> Either a b %1 -> Either a c Source #

SymmetricMonoidal Either Void Source # 
Instance details

Defined in Data.Bifunctor.Linear.Internal.SymmetricMonoidal

Methods

rassoc :: Either (Either a b) c %1 -> Either a (Either b c) Source #

lassoc :: Either a (Either b c) %1 -> Either (Either a b) c Source #

swap :: Either a b %1 -> Either b a Source #

Strong Either Void LinearArrow Source # 
Instance details

Defined in Data.Profunctor.Linear

Methods

first :: LinearArrow a b -> LinearArrow (Either a c) (Either b c) Source #

second :: LinearArrow b c -> LinearArrow (Either a b) (Either a c) Source #

Monoidal Either Void LinearArrow Source # 
Instance details

Defined in Data.Profunctor.Linear

Applicative f => Strong Either Void (Kleisli f) Source # 
Instance details

Defined in Data.Profunctor.Linear

Methods

first :: Kleisli f a b -> Kleisli f (Either a c) (Either b c) Source #

second :: Kleisli f b c -> Kleisli f (Either a b) (Either a c) Source #

Strong Either Void (CoKleisli (Const x :: Type -> Type)) Source # 
Instance details

Defined in Data.Profunctor.Kleisli.Linear

Methods

first :: CoKleisli (Const x) a b -> CoKleisli (Const x) (Either a c) (Either b c) Source #

second :: CoKleisli (Const x) b c -> CoKleisli (Const x) (Either a b) (Either a c) Source #

Applicative f => Strong Either Void (Kleisli f) Source # 
Instance details

Defined in Data.Profunctor.Kleisli.Linear

Methods

first :: Kleisli f a b -> Kleisli f (Either a c) (Either b c) Source #

second :: Kleisli f b c -> Kleisli f (Either a b) (Either a c) Source #

Functor f => Monoidal Either Void (Kleisli f) Source # 
Instance details

Defined in Data.Profunctor.Linear

Methods

(***) :: Kleisli f a b -> Kleisli f x y -> Kleisli f (Either a x) (Either b y) Source #

unit :: Kleisli f Void Void Source #

Functor f => Monoidal Either Void (Kleisli f) Source # 
Instance details

Defined in Data.Profunctor.Kleisli.Linear

Methods

(***) :: Kleisli f a b -> Kleisli f x y -> Kleisli f (Either a x) (Either b y) Source #

unit :: Kleisli f Void Void Source #

Strong Either Void (->) Source # 
Instance details

Defined in Data.Profunctor.Linear

Methods

first :: (a -> b) -> Either a c -> Either b c Source #

second :: (b -> c) -> Either a b -> Either a c Source #

Strong Either Void (Market a b) Source # 
Instance details

Defined in Data.Profunctor.Linear

Methods

first :: Market a b a0 b0 -> Market a b (Either a0 c) (Either b0 c) Source #

second :: Market a b b0 c -> Market a b (Either a0 b0) (Either a0 c) Source #

Monoidal Either Void (->) Source # 
Instance details

Defined in Data.Profunctor.Linear

Methods

(***) :: (a -> b) -> (x -> y) -> Either a x -> Either b y Source #

unit :: Void -> Void Source #

Generic1 (Either a :: Type -> Type) 
Instance details

Defined in GHC.Generics

Associated Types

type Rep1 (Either a) :: k -> Type #

Methods

from1 :: forall (a0 :: k). Either a a0 -> Rep1 (Either a) a0 #

to1 :: forall (a0 :: k). Rep1 (Either a) a0 -> Either a a0 #

(Lift a, Lift b) => Lift (Either a b :: Type) 
Instance details

Defined in Language.Haskell.TH.Syntax

Methods

lift :: Quote m => Either a b -> m Exp #

liftTyped :: forall (m :: Type -> Type). Quote m => Either a b -> Code m (Either a b) #

Monad (Either e)

Since: base-4.4.0.0

Instance details

Defined in Data.Either

Methods

(>>=) :: Either e a -> (a -> Either e b) -> Either e b #

(>>) :: Either e a -> Either e b -> Either e b #

return :: a -> Either e a #

Functor (Either a)

Since: base-3.0

Instance details

Defined in Data.Either

Methods

fmap :: (a0 -> b) -> Either a a0 -> Either a b #

(<$) :: a0 -> Either a b -> Either a a0 #

Applicative (Either e)

Since: base-3.0

Instance details

Defined in Data.Either

Methods

pure :: a -> Either e a #

(<*>) :: Either e (a -> b) -> Either e a -> Either e b #

liftA2 :: (a -> b -> c) -> Either e a -> Either e b -> Either e c #

(*>) :: Either e a -> Either e b -> Either e b #

(<*) :: Either e a -> Either e b -> Either e a #

Foldable (Either a)

Since: base-4.7.0.0

Instance details

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] #

null :: Either a a0 -> Bool #

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 #

sum :: Num a0 => Either a a0 -> a0 #

product :: Num a0 => Either a a0 -> a0 #

Traversable (Either a)

Since: base-4.7.0.0

Instance details

Defined in Data.Traversable

Methods

traverse :: Applicative f => (a0 -> f b) -> Either a a0 -> f (Either a b) #

sequenceA :: Applicative f => Either a (f a0) -> f (Either a a0) #

mapM :: Monad m => (a0 -> m b) -> Either a a0 -> m (Either a b) #

sequence :: Monad m => Either a (m a0) -> m (Either a a0) #

Show a => Show1 (Either a)

Since: base-4.9.0.0

Instance details

Defined in Data.Functor.Classes

Methods

liftShowsPrec :: (Int -> a0 -> ShowS) -> ([a0] -> ShowS) -> Int -> Either a a0 -> ShowS #

liftShowList :: (Int -> a0 -> ShowS) -> ([a0] -> ShowS) -> [Either a a0] -> ShowS #

Read a => Read1 (Either a)

Since: base-4.9.0.0

Instance details

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] #

Ord a => Ord1 (Either a)

Since: base-4.9.0.0

Instance details

Defined in Data.Functor.Classes

Methods

liftCompare :: (a0 -> b -> Ordering) -> Either a a0 -> Either a b -> Ordering #

Eq a => Eq1 (Either a)

Since: base-4.9.0.0

Instance details

Defined in Data.Functor.Classes

Methods

liftEq :: (a0 -> b -> Bool) -> Either a a0 -> Either a b -> Bool #

Hashable a => Hashable1 (Either a) 
Instance details

Defined in Data.Hashable.Class

Methods

liftHashWithSalt :: (Int -> a0 -> Int) -> Int -> Either a a0 -> Int #

Functor (Either e) Source # 
Instance details

Defined in Data.Functor.Linear.Internal.Functor

Methods

fmap :: (a %1 -> b) -> Either e a %1 -> Either e b Source #

Traversable (Either a) Source # 
Instance details

Defined in Data.Functor.Linear.Internal.Traversable

Methods

traverse :: Applicative f => (a0 %1 -> f b) -> Either a a0 %1 -> f (Either a b) Source #

sequence :: Applicative f => Either a (f a0) %1 -> f (Either a a0) Source #

(Eq a, Eq b) => Eq (Either a b)

Since: base-2.1

Instance details

Defined in Data.Either

Methods

(==) :: Either a b -> Either a b -> Bool #

(/=) :: Either a b -> Either a b -> Bool #

(Ord a, Ord b) => Ord (Either a b)

Since: base-2.1

Instance details

Defined in Data.Either

Methods

compare :: Either a b -> Either a b -> Ordering #

(<) :: Either a b -> Either a b -> Bool #

(<=) :: Either a b -> Either a b -> Bool #

(>) :: Either a b -> Either a b -> Bool #

(>=) :: Either a b -> Either a b -> Bool #

max :: Either a b -> Either a b -> Either a b #

min :: Either a b -> Either a b -> Either a b #

(Read a, Read b) => Read (Either a b)

Since: base-3.0

Instance details

Defined in Data.Either

(Show a, Show b) => Show (Either a b)

Since: base-3.0

Instance details

Defined in Data.Either

Methods

showsPrec :: Int -> Either a b -> ShowS #

show :: Either a b -> String #

showList :: [Either a b] -> ShowS #

Generic (Either a b) 
Instance details

Defined in GHC.Generics

Associated Types

type Rep (Either a b) :: Type -> Type #

Methods

from :: Either a b -> Rep (Either a b) x #

to :: Rep (Either a b) x -> Either a b #

Semigroup (Either a b)

Since: base-4.9.0.0

Instance details

Defined in Data.Either

Methods

(<>) :: Either a b -> Either a b -> Either a b #

sconcat :: NonEmpty (Either a b) -> Either a b #

stimes :: Integral b0 => b0 -> Either a b -> Either a b #

(Hashable a, Hashable b) => Hashable (Either a b) 
Instance details

Defined in Data.Hashable.Class

Methods

hashWithSalt :: Int -> Either a b -> Int #

hash :: Either a b -> Int #

(Consumable a, Consumable b) => Consumable (Either a b) Source # 
Instance details

Defined in Data.Unrestricted.Internal.Instances

Methods

consume :: Either a b %1 -> () Source #

(Dupable a, Dupable b) => Dupable (Either a b) Source # 
Instance details

Defined in Data.Unrestricted.Internal.Instances

Methods

dupV :: forall (n :: Nat). KnownNat n => Either a b %1 -> V n (Either a b) Source #

dup2 :: Either a b %1 -> (Either a b, Either a b) Source #

(Movable a, Movable b) => Movable (Either a b) Source # 
Instance details

Defined in Data.Unrestricted.Internal.Instances

Methods

move :: Either a b %1 -> Ur (Either a b) Source #

(Consumable a, Consumable b, Eq a, Eq b) => Eq (Either a b) Source # 
Instance details

Defined in Data.Ord.Linear.Internal.Eq

Methods

(==) :: Either a b %1 -> Either a b %1 -> Bool Source #

(/=) :: Either a b %1 -> Either a b %1 -> Bool Source #

(Consumable a, Consumable b, Ord a, Ord b) => Ord (Either a b) Source # 
Instance details

Defined in Data.Ord.Linear.Internal.Ord

Methods

compare :: Either a b %1 -> Either a b %1 -> Ordering Source #

(<=) :: Either a b %1 -> Either a b %1 -> Bool Source #

(<) :: Either a b %1 -> Either a b %1 -> Bool Source #

(>) :: Either a b %1 -> Either a b %1 -> Bool Source #

(>=) :: Either a b %1 -> Either a b %1 -> Bool Source #

type Rep1 (Either a :: Type -> Type)

Since: base-4.6.0.0

Instance details

Defined in GHC.Generics

type Rep (Either a b)

Since: base-4.6.0.0

Instance details

Defined in GHC.Generics

either :: (a %1 -> c) -> (b %1 -> c) -> Either a b %1 -> c Source #

Linearly consume an Either by applying the first linear function on a value constructed with Left and the second linear function on a value constructed with Right.

lefts :: Consumable b => [Either a b] %1 -> [a] Source #

Get all the left elements in order, and consume the right ones.

rights :: Consumable a => [Either a b] %1 -> [b] Source #

Get all the right elements in order, and consume the left ones.

fromLeft :: (Consumable a, Consumable b) => a %1 -> Either a b %1 -> a Source #

Get the left element of a consumable Either with a default

fromRight :: (Consumable a, Consumable b) => b %1 -> Either a b %1 -> b Source #

Get the right element of a consumable Either with a default

partitionEithers :: [Either a b] %1 -> ([a], [b]) Source #

Partition and consume a list of Eithers into two lists with all the lefts in one and the rights in the second, in the order they appeared in the initial list.