135040228cebf03e10e6dcee41353f50ce794d49
[packages/base.git] / Data / Maybe.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE CPP, NoImplicitPrelude, DeriveGeneric #-}
3
4 -----------------------------------------------------------------------------
5 -- |
6 -- Module : Data.Maybe
7 -- Copyright : (c) The University of Glasgow 2001
8 -- License : BSD-style (see the file libraries/base/LICENSE)
9 --
10 -- Maintainer : libraries@haskell.org
11 -- Stability : stable
12 -- Portability : portable
13 --
14 -- The Maybe type, and associated operations.
15 --
16 -----------------------------------------------------------------------------
17
18 module Data.Maybe
19 (
20 Maybe(Nothing,Just)-- instance of: Eq, Ord, Show, Read,
21 -- Functor, Monad, MonadPlus
22
23 , maybe -- :: b -> (a -> b) -> Maybe a -> b
24
25 , isJust -- :: Maybe a -> Bool
26 , isNothing -- :: Maybe a -> Bool
27 , fromJust -- :: Maybe a -> a
28 , fromMaybe -- :: a -> Maybe a -> a
29 , listToMaybe -- :: [a] -> Maybe a
30 , maybeToList -- :: Maybe a -> [a]
31 , catMaybes -- :: [Maybe a] -> [a]
32 , mapMaybe -- :: (a -> Maybe b) -> [a] -> [b]
33 ) where
34
35 #ifdef __GLASGOW_HASKELL__
36 import GHC.Base
37 import GHC.Generics (Generic)
38 #endif
39
40 #ifdef __NHC__
41 import Prelude
42 import Prelude (Maybe(..), maybe)
43 import Maybe
44 ( isJust
45 , isNothing
46 , fromJust
47 , fromMaybe
48 , listToMaybe
49 , maybeToList
50 , catMaybes
51 , mapMaybe
52 )
53 #else
54
55 #ifndef __HUGS__
56 -- ---------------------------------------------------------------------------
57 -- The Maybe type, and instances
58
59 -- | The 'Maybe' type encapsulates an optional value. A value of type
60 -- @'Maybe' a@ either contains a value of type @a@ (represented as @'Just' a@),
61 -- or it is empty (represented as 'Nothing'). Using 'Maybe' is a good way to
62 -- deal with errors or exceptional cases without resorting to drastic
63 -- measures such as 'error'.
64 --
65 -- The 'Maybe' type is also a monad. It is a simple kind of error
66 -- monad, where all errors are represented by 'Nothing'. A richer
67 -- error monad can be built using the 'Data.Either.Either' type.
68
69 data Maybe a = Nothing | Just a
70 deriving (Eq, Ord, Generic)
71
72 instance Functor Maybe where
73 fmap _ Nothing = Nothing
74 fmap f (Just a) = Just (f a)
75
76 instance Monad Maybe where
77 (Just x) >>= k = k x
78 Nothing >>= _ = Nothing
79
80 (Just _) >> k = k
81 Nothing >> _ = Nothing
82
83 return = Just
84 fail _ = Nothing
85
86 -- ---------------------------------------------------------------------------
87 -- Functions over Maybe
88
89 -- | The 'maybe' function takes a default value, a function, and a 'Maybe'
90 -- value. If the 'Maybe' value is 'Nothing', the function returns the
91 -- default value. Otherwise, it applies the function to the value inside
92 -- the 'Just' and returns the result.
93 maybe :: b -> (a -> b) -> Maybe a -> b
94 maybe n _ Nothing = n
95 maybe _ f (Just x) = f x
96 #endif /* __HUGS__ */
97
98 -- | The 'isJust' function returns 'True' iff its argument is of the
99 -- form @Just _@.
100 isJust :: Maybe a -> Bool
101 isJust Nothing = False
102 isJust _ = True
103
104 -- | The 'isNothing' function returns 'True' iff its argument is 'Nothing'.
105 isNothing :: Maybe a -> Bool
106 isNothing Nothing = True
107 isNothing _ = False
108
109 -- | The 'fromJust' function extracts the element out of a 'Just' and
110 -- throws an error if its argument is 'Nothing'.
111 fromJust :: Maybe a -> a
112 fromJust Nothing = error "Maybe.fromJust: Nothing" -- yuck
113 fromJust (Just x) = x
114
115 -- | The 'fromMaybe' function takes a default value and and 'Maybe'
116 -- value. If the 'Maybe' is 'Nothing', it returns the default values;
117 -- otherwise, it returns the value contained in the 'Maybe'.
118 fromMaybe :: a -> Maybe a -> a
119 fromMaybe d x = case x of {Nothing -> d;Just v -> v}
120
121 -- | The 'maybeToList' function returns an empty list when given
122 -- 'Nothing' or a singleton list when not given 'Nothing'.
123 maybeToList :: Maybe a -> [a]
124 maybeToList Nothing = []
125 maybeToList (Just x) = [x]
126
127 -- | The 'listToMaybe' function returns 'Nothing' on an empty list
128 -- or @'Just' a@ where @a@ is the first element of the list.
129 listToMaybe :: [a] -> Maybe a
130 listToMaybe [] = Nothing
131 listToMaybe (a:_) = Just a
132
133 -- | The 'catMaybes' function takes a list of 'Maybe's and returns
134 -- a list of all the 'Just' values.
135 catMaybes :: [Maybe a] -> [a]
136 catMaybes ls = [x | Just x <- ls]
137
138 -- | The 'mapMaybe' function is a version of 'map' which can throw
139 -- out elements. In particular, the functional argument returns
140 -- something of type @'Maybe' b@. If this is 'Nothing', no element
141 -- is added on to the result list. If it just @'Just' b@, then @b@ is
142 -- included in the result list.
143 mapMaybe :: (a -> Maybe b) -> [a] -> [b]
144 mapMaybe _ [] = []
145 mapMaybe f (x:xs) =
146 let rs = mapMaybe f xs in
147 case f x of
148 Nothing -> rs
149 Just r -> r:rs
150
151 #endif /* else not __NHC__ */
152