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