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