Revert "Fix typo in maybeToList documentation"
[ghc.git] / libraries / base / Data / Maybe.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE NoImplicitPrelude #-}
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)
21
22 , maybe
23
24 , isJust
25 , isNothing
26 , fromJust
27 , fromMaybe
28 , listToMaybe
29 , maybeToList
30 , catMaybes
31 , mapMaybe
32 ) where
33
34 import GHC.Base
35 import GHC.Stack.Types ( HasCallStack )
36
37 -- $setup
38 -- Allow the use of some Prelude functions in doctests.
39 -- >>> import Prelude ( (*), odd, show, sum )
40
41 -- ---------------------------------------------------------------------------
42 -- Functions over Maybe
43
44 -- | The 'maybe' function takes a default value, a function, and a 'Maybe'
45 -- value. If the 'Maybe' value is 'Nothing', the function returns the
46 -- default value. Otherwise, it applies the function to the value inside
47 -- the 'Just' and returns the result.
48 --
49 -- ==== __Examples__
50 --
51 -- Basic usage:
52 --
53 -- >>> maybe False odd (Just 3)
54 -- True
55 --
56 -- >>> maybe False odd Nothing
57 -- False
58 --
59 -- Read an integer from a string using 'Text.Read.readMaybe'. If we succeed,
60 -- return twice the integer; that is, apply @(*2)@ to it. If instead
61 -- we fail to parse an integer, return @0@ by default:
62 --
63 -- >>> import Text.Read ( readMaybe )
64 -- >>> maybe 0 (*2) (readMaybe "5")
65 -- 10
66 -- >>> maybe 0 (*2) (readMaybe "")
67 -- 0
68 --
69 -- Apply 'Prelude.show' to a @Maybe Int@. If we have @Just n@, we want to show
70 -- the underlying 'Int' @n@. But if we have 'Nothing', we return the
71 -- empty string instead of (for example) \"Nothing\":
72 --
73 -- >>> maybe "" show (Just 5)
74 -- "5"
75 -- >>> maybe "" show Nothing
76 -- ""
77 --
78 maybe :: b -> (a -> b) -> Maybe a -> b
79 maybe n _ Nothing = n
80 maybe _ f (Just x) = f x
81
82 -- | The 'isJust' function returns 'True' iff its argument is of the
83 -- form @Just _@.
84 --
85 -- ==== __Examples__
86 --
87 -- Basic usage:
88 --
89 -- >>> isJust (Just 3)
90 -- True
91 --
92 -- >>> isJust (Just ())
93 -- True
94 --
95 -- >>> isJust Nothing
96 -- False
97 --
98 -- Only the outer constructor is taken into consideration:
99 --
100 -- >>> isJust (Just Nothing)
101 -- True
102 --
103 isJust :: Maybe a -> Bool
104 isJust Nothing = False
105 isJust _ = True
106
107 -- | The 'isNothing' function returns 'True' iff its argument is 'Nothing'.
108 --
109 -- ==== __Examples__
110 --
111 -- Basic usage:
112 --
113 -- >>> isNothing (Just 3)
114 -- False
115 --
116 -- >>> isNothing (Just ())
117 -- False
118 --
119 -- >>> isNothing Nothing
120 -- True
121 --
122 -- Only the outer constructor is taken into consideration:
123 --
124 -- >>> isNothing (Just Nothing)
125 -- False
126 --
127 isNothing :: Maybe a -> Bool
128 isNothing Nothing = True
129 isNothing _ = False
130
131 -- | The 'fromJust' function extracts the element out of a 'Just' and
132 -- throws an error if its argument is 'Nothing'.
133 --
134 -- ==== __Examples__
135 --
136 -- Basic usage:
137 --
138 -- >>> fromJust (Just 1)
139 -- 1
140 --
141 -- >>> 2 * (fromJust (Just 10))
142 -- 20
143 --
144 -- >>> 2 * (fromJust Nothing)
145 -- *** Exception: Maybe.fromJust: Nothing
146 --
147 fromJust :: HasCallStack => Maybe a -> a
148 fromJust Nothing = error "Maybe.fromJust: Nothing" -- yuck
149 fromJust (Just x) = x
150
151 -- | The 'fromMaybe' function takes a default value and and 'Maybe'
152 -- value. If the 'Maybe' is 'Nothing', it returns the default values;
153 -- otherwise, it returns the value contained in the 'Maybe'.
154 --
155 -- ==== __Examples__
156 --
157 -- Basic usage:
158 --
159 -- >>> fromMaybe "" (Just "Hello, World!")
160 -- "Hello, World!"
161 --
162 -- >>> fromMaybe "" Nothing
163 -- ""
164 --
165 -- Read an integer from a string using 'Text.Read.readMaybe'. If we fail to
166 -- parse an integer, we want to return @0@ by default:
167 --
168 -- >>> import Text.Read ( readMaybe )
169 -- >>> fromMaybe 0 (readMaybe "5")
170 -- 5
171 -- >>> fromMaybe 0 (readMaybe "")
172 -- 0
173 --
174 fromMaybe :: a -> Maybe a -> a
175 fromMaybe d x = case x of {Nothing -> d;Just v -> v}
176
177 -- | The 'maybeToList' function returns an empty list when given
178 -- 'Nothing' or a singleton list when not given 'Nothing'.
179 --
180 -- ==== __Examples__
181 --
182 -- Basic usage:
183 --
184 -- >>> maybeToList (Just 7)
185 -- [7]
186 --
187 -- >>> maybeToList Nothing
188 -- []
189 --
190 -- One can use 'maybeToList' to avoid pattern matching when combined
191 -- with a function that (safely) works on lists:
192 --
193 -- >>> import Text.Read ( readMaybe )
194 -- >>> sum $ maybeToList (readMaybe "3")
195 -- 3
196 -- >>> sum $ maybeToList (readMaybe "")
197 -- 0
198 --
199 maybeToList :: Maybe a -> [a]
200 maybeToList Nothing = []
201 maybeToList (Just x) = [x]
202
203 -- | The 'listToMaybe' function returns 'Nothing' on an empty list
204 -- or @'Just' a@ where @a@ is the first element of the list.
205 --
206 -- ==== __Examples__
207 --
208 -- Basic usage:
209 --
210 -- >>> listToMaybe []
211 -- Nothing
212 --
213 -- >>> listToMaybe [9]
214 -- Just 9
215 --
216 -- >>> listToMaybe [1,2,3]
217 -- Just 1
218 --
219 -- Composing 'maybeToList' with 'listToMaybe' should be the identity
220 -- on singleton/empty lists:
221 --
222 -- >>> maybeToList $ listToMaybe [5]
223 -- [5]
224 -- >>> maybeToList $ listToMaybe []
225 -- []
226 --
227 -- But not on lists with more than one element:
228 --
229 -- >>> maybeToList $ listToMaybe [1,2,3]
230 -- [1]
231 --
232 listToMaybe :: [a] -> Maybe a
233 listToMaybe = foldr (const . Just) Nothing
234 {-# INLINE listToMaybe #-}
235 -- We define listToMaybe using foldr so that it can fuse via the foldr/build
236 -- rule. See #14387
237
238
239 -- | The 'catMaybes' function takes a list of 'Maybe's and returns
240 -- a list of all the 'Just' values.
241 --
242 -- ==== __Examples__
243 --
244 -- Basic usage:
245 --
246 -- >>> catMaybes [Just 1, Nothing, Just 3]
247 -- [1,3]
248 --
249 -- When constructing a list of 'Maybe' values, 'catMaybes' can be used
250 -- to return all of the \"success\" results (if the list is the result
251 -- of a 'map', then 'mapMaybe' would be more appropriate):
252 --
253 -- >>> import Text.Read ( readMaybe )
254 -- >>> [readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ]
255 -- [Just 1,Nothing,Just 3]
256 -- >>> catMaybes $ [readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ]
257 -- [1,3]
258 --
259 catMaybes :: [Maybe a] -> [a]
260 catMaybes ls = [x | Just x <- ls]
261
262 -- | The 'mapMaybe' function is a version of 'map' which can throw
263 -- out elements. In particular, the functional argument returns
264 -- something of type @'Maybe' b@. If this is 'Nothing', no element
265 -- is added on to the result list. If it is @'Just' b@, then @b@ is
266 -- included in the result list.
267 --
268 -- ==== __Examples__
269 --
270 -- Using @'mapMaybe' f x@ is a shortcut for @'catMaybes' $ 'map' f x@
271 -- in most cases:
272 --
273 -- >>> import Text.Read ( readMaybe )
274 -- >>> let readMaybeInt = readMaybe :: String -> Maybe Int
275 -- >>> mapMaybe readMaybeInt ["1", "Foo", "3"]
276 -- [1,3]
277 -- >>> catMaybes $ map readMaybeInt ["1", "Foo", "3"]
278 -- [1,3]
279 --
280 -- If we map the 'Just' constructor, the entire list should be returned:
281 --
282 -- >>> mapMaybe Just [1,2,3]
283 -- [1,2,3]
284 --
285 mapMaybe :: (a -> Maybe b) -> [a] -> [b]
286 mapMaybe _ [] = []
287 mapMaybe f (x:xs) =
288 let rs = mapMaybe f xs in
289 case f x of
290 Nothing -> rs
291 Just r -> r:rs
292 {-# NOINLINE [1] mapMaybe #-}
293
294 {-# RULES
295 "mapMaybe" [~1] forall f xs. mapMaybe f xs
296 = build (\c n -> foldr (mapMaybeFB c f) n xs)
297 "mapMaybeList" [1] forall f. foldr (mapMaybeFB (:) f) [] = mapMaybe f
298 #-}
299
300 {-# INLINE [0] mapMaybeFB #-} -- See Note [Inline FB functions] in GHC.List
301 mapMaybeFB :: (b -> r -> r) -> (a -> Maybe b) -> a -> r -> r
302 mapMaybeFB cons f x next = case f x of
303 Nothing -> next
304 Just r -> cons r next