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