Remove some unnecessary Data.Tuple imports
[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 #ifdef __GLASGOW_HASKELL__
25 import GHC.Base
26 import GHC.Show
27
28 {-
29 -- just for testing
30 import Test.QuickCheck
31 -}
32
33 {-|
34
35 The 'Either' type represents values with two possibilities: a value of
36 type @'Either' a b@ is either @'Left' a@ or @'Right' b@.
37
38 The 'Either' type is sometimes used to represent a value which is
39 either correct or an error; by convention, the 'Left' constructor is
40 used to hold an error value and the 'Right' constructor is used to
41 hold a correct value (mnemonic: \"right\" also means \"correct\").
42 -}
43 data  Either a b  =  Left a | Right b   deriving (Eq, Ord, Show)
44
45 -- | Case analysis for the 'Either' type.
46 -- If the value is @'Left' a@, apply the first function to @a@;
47 -- if it is @'Right' b@, apply the second function to @b@.
48 either                  :: (a -> c) -> (b -> c) -> Either a b -> c
49 either f _ (Left x)     =  f x
50 either _ g (Right y)    =  g y
51 #endif  /* __GLASGOW_HASKELL__ */
52
53 -- | Extracts from a list of 'Either' all the 'Left' elements
54 -- All the 'Left' elements are extracted in order.
55
56 lefts   :: [Either a b] -> [a]
57 lefts x = [a | Left a <- x]
58
59 -- | Extracts from a list of 'Either' all the 'Right' elements
60 -- All the 'Right' elements are extracted in order.
61
62 rights   :: [Either a b] -> [b]
63 rights x = [a | Right a <- x]
64
65 -- | Partitions a list of 'Either' into two lists
66 -- All the 'Left' elements are extracted, in order, to the first
67 -- component of the output.  Similarly the 'Right' elements are extracted
68 -- to the second component of the output.
69
70 partitionEithers :: [Either a b] -> ([a],[b])
71 partitionEithers = foldr (either left right) ([],[])
72  where
73   left  a (l, r) = (a:l, r)
74   right a (l, r) = (l, a:r)
75
76 {-
77 {--------------------------------------------------------------------
78   Testing
79 --------------------------------------------------------------------}
80 prop_partitionEithers :: [Either Int Int] -> Bool
81 prop_partitionEithers x =
82   partitionEithers x == (lefts x, rights x)
83 -}
84