1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
10 -- Portability : portable
12 -- The Maybe type, and associated operations.
14 -----------------------------------------------------------------------------
18 Maybe(Nothing,Just)-- instance of: Eq, Ord, Show, Read,
19 -- Functor, Monad, MonadPlus
21 , maybe -- :: b -> (a -> b) -> Maybe a -> b
23 , isJust -- :: Maybe a -> Bool
24 , isNothing -- :: Maybe a -> Bool
25 , fromJust -- :: Maybe a -> a
26 , fromMaybe -- :: a -> Maybe a -> a
27 , listToMaybe -- :: [a] -> Maybe a
28 , maybeToList -- :: Maybe a -> [a]
29 , catMaybes -- :: [Maybe a] -> [a]
30 , mapMaybe -- :: (a -> Maybe b) -> [a] -> [b]
33 #ifdef __GLASGOW_HASKELL__
34 import {-# SOURCE #-} GHC.Err ( error )
40 import Prelude (Maybe(..), maybe)
54 -- ---------------------------------------------------------------------------
55 -- The Maybe type, and instances
57 -- | The 'Maybe' type encapsulates an optional value. A value of type
58 -- @'Maybe' a@ either contains a value of type @a@ (represented as @'Just' a@),
59 -- or it is empty (represented as 'Nothing'). Using 'Maybe' is a good way to
60 -- deal with errors or exceptional cases without resorting to drastic
61 -- measures such as 'error'.
63 -- The 'Maybe' type is also a monad. It is a simple kind of error
64 -- monad, where all errors are represented by 'Nothing'. A richer
65 -- error monad can be built using the 'Data.Either.Either' type.
67 data Maybe a = Nothing | Just a
70 instance Functor Maybe where
71 fmap _ Nothing = Nothing
72 fmap f (Just a) = Just (f a)
74 instance Monad Maybe where
76 Nothing >>= _ = Nothing
79 Nothing >> _ = Nothing
84 -- ---------------------------------------------------------------------------
85 -- Functions over Maybe
87 -- | The 'maybe' function takes a default value, a function, and a 'Maybe'
88 -- value. If the 'Maybe' value is 'Nothing', the function returns the
89 -- default value. Otherwise, it applies the function to the value inside
90 -- the 'Just' and returns that.
91 maybe :: b -> (a -> b) -> Maybe a -> b
93 maybe _ f (Just x) = f x
96 -- | The 'isJust' function returns 'True' iff its argument is of the
98 isJust :: Maybe a -> Bool
99 isJust Nothing = False
102 -- | The 'isNothing' function returns 'True' iff its argument is 'Nothing'.
103 isNothing :: Maybe a -> Bool
104 isNothing Nothing = True
107 -- | The 'fromJust' function extracts the element out of a 'Just' and
108 -- throws an error if its argument is 'Nothing'.
109 fromJust :: Maybe a -> a
110 fromJust Nothing = error "Maybe.fromJust: Nothing" -- yuck
111 fromJust (Just x) = x
113 -- | The 'fromMaybe' function takes a default value and and 'Maybe'
114 -- value. If the 'Maybe' is 'Nothing', it returns the default values;
115 -- otherwise, it returns the value contained in the 'Maybe'.
116 fromMaybe :: a -> Maybe a -> a
117 fromMaybe d x = case x of {Nothing -> d;Just v -> v}
119 -- | The 'maybeToList' function returns an empty list when given
120 -- 'Nothing' or a singleton list when not given 'Nothing'.
121 maybeToList :: Maybe a -> [a]
122 maybeToList Nothing = []
123 maybeToList (Just x) = [x]
125 -- | The 'listToMaybe' function returns 'Nothing' on an empty list
126 -- or @'Just' a@ where @a@ is the first element of the list.
127 listToMaybe :: [a] -> Maybe a
128 listToMaybe [] = Nothing
129 listToMaybe (a:_) = Just a
131 -- | The 'catMaybes' function takes a list of 'Maybe's and returns
132 -- a list of all the 'Just' values.
133 catMaybes :: [Maybe a] -> [a]
134 catMaybes ls = [x | Just x <- ls]
136 -- | The 'mapMaybe' function is a version of 'map' which can throw
137 -- out elements. In particular, the functional argument returns
138 -- something of type @'Maybe' b@. If this is 'Nothing', no element
139 -- is added on to the result list. If it just @'Just' a@, then @a@ is
140 -- added on to the result.
141 mapMaybe :: (a -> Maybe b) -> [a] -> [b]
144 let rs = mapMaybe f xs in
149 #endif /* else not __NHC__ */