Typo
[packages/base.git] / 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
36 -- ---------------------------------------------------------------------------
37 -- The Maybe type, and instances
38
39 -- | The 'Maybe' type encapsulates an optional value. A value of type
40 -- @'Maybe' a@ either contains a value of type @a@ (represented as @'Just' a@),
41 -- or it is empty (represented as 'Nothing'). Using 'Maybe' is a good way to
42 -- deal with errors or exceptional cases without resorting to drastic
43 -- measures such as 'error'.
44 --
45 -- The 'Maybe' type is also a monad. It is a simple kind of error
46 -- monad, where all errors are represented by 'Nothing'. A richer
47 -- error monad can be built using the 'Data.Either.Either' type.
48
49 data Maybe a = Nothing | Just a
50 deriving (Eq, Ord)
51
52 instance Functor Maybe where
53 fmap _ Nothing = Nothing
54 fmap f (Just a) = Just (f a)
55
56 instance Monad Maybe where
57 (Just x) >>= k = k x
58 Nothing >>= _ = Nothing
59
60 (Just _) >> k = k
61 Nothing >> _ = Nothing
62
63 return = Just
64 fail _ = Nothing
65
66 -- ---------------------------------------------------------------------------
67 -- Functions over Maybe
68
69 -- | The 'maybe' function takes a default value, a function, and a 'Maybe'
70 -- value. If the 'Maybe' value is 'Nothing', the function returns the
71 -- default value. Otherwise, it applies the function to the value inside
72 -- the 'Just' and returns the result.
73 maybe :: b -> (a -> b) -> Maybe a -> b
74 maybe n _ Nothing = n
75 maybe _ f (Just x) = f x
76
77 -- | The 'isJust' function returns 'True' iff its argument is of the
78 -- form @Just _@.
79 isJust :: Maybe a -> Bool
80 isJust Nothing = False
81 isJust _ = True
82
83 -- | The 'isNothing' function returns 'True' iff its argument is 'Nothing'.
84 isNothing :: Maybe a -> Bool
85 isNothing Nothing = True
86 isNothing _ = False
87
88 -- | The 'fromJust' function extracts the element out of a 'Just' and
89 -- throws an error if its argument is 'Nothing'.
90 fromJust :: Maybe a -> a
91 fromJust Nothing = error "Maybe.fromJust: Nothing" -- yuck
92 fromJust (Just x) = x
93
94 -- | The 'fromMaybe' function takes a default value and and 'Maybe'
95 -- value. If the 'Maybe' is 'Nothing', it returns the default values;
96 -- otherwise, it returns the value contained in the 'Maybe'.
97 fromMaybe :: a -> Maybe a -> a
98 fromMaybe d x = case x of {Nothing -> d;Just v -> v}
99
100 -- | The 'maybeToList' function returns an empty list when given
101 -- 'Nothing' or a singleton list when not given 'Nothing'.
102 maybeToList :: Maybe a -> [a]
103 maybeToList Nothing = []
104 maybeToList (Just x) = [x]
105
106 -- | The 'listToMaybe' function returns 'Nothing' on an empty list
107 -- or @'Just' a@ where @a@ is the first element of the list.
108 listToMaybe :: [a] -> Maybe a
109 listToMaybe [] = Nothing
110 listToMaybe (a:_) = Just a
111
112 -- | The 'catMaybes' function takes a list of 'Maybe's and returns
113 -- a list of all the 'Just' values.
114 catMaybes :: [Maybe a] -> [a]
115 catMaybes ls = [x | Just x <- ls]
116
117 -- | The 'mapMaybe' function is a version of 'map' which can throw
118 -- out elements. In particular, the functional argument returns
119 -- something of type @'Maybe' b@. If this is 'Nothing', no element
120 -- is added on to the result list. If it just @'Just' b@, then @b@ is
121 -- included in the result list.
122 mapMaybe :: (a -> Maybe b) -> [a] -> [b]
123 mapMaybe _ [] = []
124 mapMaybe f (x:xs) =
125 let rs = mapMaybe f xs in
126 case f x of
127 Nothing -> rs
128 Just r -> r:rs
129 {-# NOINLINE [1] mapMaybe #-}
130
131 {-# RULES
132 "mapMaybe" [~1] forall f xs. mapMaybe f xs
133 = build (\c n -> foldr (mapMaybeFB c f) n xs)
134 "mapMaybeList" [1] forall f. foldr (mapMaybeFB (:) f) [] = mapMaybe f
135 #-}
136
137 {-# NOINLINE [0] mapMaybeFB #-}
138 mapMaybeFB :: (b -> r -> r) -> (a -> Maybe b) -> a -> r -> r
139 mapMaybeFB cons f x next = case f x of
140 Nothing -> next
141 Just r -> cons r next