Make Applicative a superclass of Monad
[ghc.git] / libraries / base / Data / Either.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE NoImplicitPrelude #-}
3 {-# LANGUAGE AutoDeriveTypeable, 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 Applicative (Either e) where
60 pure = Right
61 Left e <*> _ = Left e
62 Right f <*> r = fmap f r
63
64 instance Monad (Either e) where
65 return = Right
66 Left l >>= _ = Left l
67 Right r >>= k = k r
68
69 -- | Case analysis for the 'Either' type.
70 -- If the value is @'Left' a@, apply the first function to @a@;
71 -- if it is @'Right' b@, apply the second function to @b@.
72 either :: (a -> c) -> (b -> c) -> Either a b -> c
73 either f _ (Left x) = f x
74 either _ g (Right y) = g y
75
76 -- | Extracts from a list of 'Either' all the 'Left' elements
77 -- All the 'Left' elements are extracted in order.
78
79 lefts :: [Either a b] -> [a]
80 lefts x = [a | Left a <- x]
81
82 -- | Extracts from a list of 'Either' all the 'Right' elements
83 -- All the 'Right' elements are extracted in order.
84
85 rights :: [Either a b] -> [b]
86 rights x = [a | Right a <- x]
87
88 -- | Partitions a list of 'Either' into two lists
89 -- All the 'Left' elements are extracted, in order, to the first
90 -- component of the output. Similarly the 'Right' elements are extracted
91 -- to the second component of the output.
92
93 partitionEithers :: [Either a b] -> ([a],[b])
94 partitionEithers = foldr (either left right) ([],[])
95 where
96 left a ~(l, r) = (a:l, r)
97 right a ~(l, r) = (l, a:r)
98
99 -- | Return `True` if the given value is a `Left`-value, `False` otherwise.
100 --
101 -- /Since: 4.7.0.0/
102 isLeft :: Either a b -> Bool
103 isLeft (Left _) = True
104 isLeft (Right _) = False
105
106 -- | Return `True` if the given value is a `Right`-value, `False` otherwise.
107 --
108 -- /Since: 4.7.0.0/
109 isRight :: Either a b -> Bool
110 isRight (Left _) = False
111 isRight (Right _) = True
112
113 -- instance for the == Boolean type-level equality operator
114 type family EqEither a b where
115 EqEither (Left x) (Left y) = x == y
116 EqEither (Right x) (Right y) = x == y
117 EqEither a b = False
118 type instance a == b = EqEither a b
119
120 {-
121 {--------------------------------------------------------------------
122 Testing
123 --------------------------------------------------------------------}
124 prop_partitionEithers :: [Either Int Int] -> Bool
125 prop_partitionEithers x =
126 partitionEithers x == (lefts x, rights x)
127 -}
128