Remove commented types in module export lists
[packages/base.git] / Data / Either.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE CPP, NoImplicitPrelude #-}
3 #ifdef __GLASGOW_HASKELL__
4 {-# LANGUAGE DeriveDataTypeable, 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,
24 lefts,
25 rights,
26 partitionEithers,
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
39 #ifdef __GLASGOW_HASKELL__
40 {-
41 -- just for testing
42 import Test.QuickCheck
43 -}
44
45 {-|
46
47 The 'Either' type represents values with two possibilities: a value of
48 type @'Either' a b@ is either @'Left' a@ or @'Right' b@.
49
50 The 'Either' type is sometimes used to represent a value which is
51 either correct or an error; by convention, the 'Left' constructor is
52 used to hold an error value and the 'Right' constructor is used to
53 hold a correct value (mnemonic: \"right\" also means \"correct\").
54 -}
55 data Either a b = Left a | Right b
56 deriving (Eq, Ord, Read, Show)
57
58 instance Functor (Either a) where
59 fmap _ (Left x) = Left x
60 fmap f (Right y) = Right (f y)
61
62 instance Monad (Either e) where
63 return = Right
64 Left l >>= _ = Left l
65 Right r >>= k = k r
66
67 -- | Case analysis for the 'Either' type.
68 -- If the value is @'Left' a@, apply the first function to @a@;
69 -- if it is @'Right' b@, apply the second function to @b@.
70 either :: (a -> c) -> (b -> c) -> Either a b -> c
71 either f _ (Left x) = f x
72 either _ g (Right y) = g y
73 #endif /* __GLASGOW_HASKELL__ */
74
75 INSTANCE_TYPEABLE2(Either,eitherTc,"Either")
76
77 -- | Extracts from a list of 'Either' all the 'Left' elements
78 -- All the 'Left' elements are extracted in order.
79
80 lefts :: [Either a b] -> [a]
81 lefts x = [a | Left a <- x]
82
83 -- | Extracts from a list of 'Either' all the 'Right' elements
84 -- All the 'Right' elements are extracted in order.
85
86 rights :: [Either a b] -> [b]
87 rights x = [a | Right a <- x]
88
89 -- | Partitions a list of 'Either' into two lists
90 -- All the 'Left' elements are extracted, in order, to the first
91 -- component of the output. Similarly the 'Right' elements are extracted
92 -- to the second component of the output.
93
94 partitionEithers :: [Either a b] -> ([a],[b])
95 partitionEithers = foldr (either left right) ([],[])
96 where
97 left a ~(l, r) = (a:l, r)
98 right a ~(l, r) = (l, a:r)
99
100 {-
101 {--------------------------------------------------------------------
102 Testing
103 --------------------------------------------------------------------}
104 prop_partitionEithers :: [Either Int Int] -> Bool
105 prop_partitionEithers x =
106 partitionEithers x == (lefts x, rights x)
107 -}
108