Typo
[packages/base.git] / Data / Either.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE NoImplicitPrelude #-}
3 {-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
4 {-# LANGUAGE PolyKinds, DataKinds, TypeFamilies, TypeOperators, UndecidableInstances #-}
5
6 -----------------------------------------------------------------------------
7 -- |
8 -- Module : Data.Either
9 -- Copyright : (c) The University of Glasgow 2001
10 -- License : BSD-style (see the file libraries/base/LICENSE)
11 --
12 -- Maintainer : libraries@haskell.org
13 -- Stability : experimental
14 -- Portability : portable
15 --
16 -- The Either type, and associated operations.
17 --
18 -----------------------------------------------------------------------------
19
20 module Data.Either (
21 Either(..),
22 either,
23 lefts,
24 rights,
25 isLeft,
26 isRight,
27 partitionEithers,
28 ) where
29
30 import GHC.Base
31 import GHC.Show
32 import GHC.Read
33
34 import Data.Typeable
35 import Data.Type.Equality
36
37 {-
38 -- just for testing
39 import Test.QuickCheck
40 -}
41
42 {-|
43
44 The 'Either' type represents values with two possibilities: a value of
45 type @'Either' a b@ is either @'Left' a@ or @'Right' b@.
46
47 The 'Either' type is sometimes used to represent a value which is
48 either correct or an error; by convention, the 'Left' constructor is
49 used to hold an error value and the 'Right' constructor is used to
50 hold a correct value (mnemonic: \"right\" also means \"correct\").
51 -}
52 data Either a b = Left a | Right b
53 deriving (Eq, Ord, Read, Show, Typeable)
54
55 instance Functor (Either a) where
56 fmap _ (Left x) = Left x
57 fmap f (Right y) = Right (f y)
58
59 instance Monad (Either e) where
60 return = Right
61 Left l >>= _ = Left l
62 Right r >>= k = k r
63
64 -- | Case analysis for the 'Either' type.
65 -- If the value is @'Left' a@, apply the first function to @a@;
66 -- if it is @'Right' b@, apply the second function to @b@.
67 either :: (a -> c) -> (b -> c) -> Either a b -> c
68 either f _ (Left x) = f x
69 either _ g (Right y) = g y
70
71 -- | Extracts from a list of 'Either' all the 'Left' elements
72 -- All the 'Left' elements are extracted in order.
73
74 lefts :: [Either a b] -> [a]
75 lefts x = [a | Left a <- x]
76
77 -- | Extracts from a list of 'Either' all the 'Right' elements
78 -- All the 'Right' elements are extracted in order.
79
80 rights :: [Either a b] -> [b]
81 rights x = [a | Right a <- x]
82
83 -- | Partitions a list of 'Either' into two lists
84 -- All the 'Left' elements are extracted, in order, to the first
85 -- component of the output. Similarly the 'Right' elements are extracted
86 -- to the second component of the output.
87
88 partitionEithers :: [Either a b] -> ([a],[b])
89 partitionEithers = foldr (either left right) ([],[])
90 where
91 left a ~(l, r) = (a:l, r)
92 right a ~(l, r) = (l, a:r)
93
94 -- | Return `True` if the given value is a `Left`-value, `False` otherwise.
95 --
96 -- /Since: 4.7.0.0/
97 isLeft :: Either a b -> Bool
98 isLeft (Left _) = True
99 isLeft (Right _) = False
100
101 -- | Return `True` if the given value is a `Right`-value, `False` otherwise.
102 --
103 -- /Since: 4.7.0.0/
104 isRight :: Either a b -> Bool
105 isRight (Left _) = False
106 isRight (Right _) = True
107
108 -- instance for the == Boolean type-level equality operator
109 type family EqEither a b where
110 EqEither (Left x) (Left y) = x == y
111 EqEither (Right x) (Right y) = x == y
112 EqEither a b = False
113 type instance a == b = EqEither a b
114
115 {-
116 {--------------------------------------------------------------------
117 Testing
118 --------------------------------------------------------------------}
119 prop_partitionEithers :: [Either Int Int] -> Bool
120 prop_partitionEithers x =
121 partitionEithers x == (lefts x, rights x)
122 -}
123