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