1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
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__
39 import Prelude (Maybe(..), maybe)
53 -- ---------------------------------------------------------------------------
54 -- The Maybe type, and instances
56 -- | The 'Maybe' type encapsulates an optional value. A value of type
57 -- @'Maybe' a@ either contains a value of type @a@ (represented as @'Just' a@),
58 -- or it is empty (represented as 'Nothing'). Using 'Maybe' is a good way to
59 -- deal with errors or exceptional cases without resorting to drastic
60 -- measures such as 'error'.
62 -- The 'Maybe' type is also a monad. It is a simple kind of error
63 -- monad, where all errors are represented by 'Nothing'. A richer
64 -- error monad can be built using the 'Data.Either.Either' type.
66 data Maybe a = Nothing | Just a
69 instance Functor Maybe where
70 fmap _ Nothing = Nothing
71 fmap f (Just a) = Just (f a)
73 instance Monad Maybe where
75 Nothing >>= _ = Nothing
78 Nothing >> _ = Nothing
83 -- ---------------------------------------------------------------------------
84 -- Functions over Maybe
86 -- | The 'maybe' function takes a default value, a function, and a 'Maybe'
87 -- value. If the 'Maybe' value is 'Nothing', the function returns the
88 -- default value. Otherwise, it applies the function to the value inside
89 -- the 'Just' and returns the result.
90 maybe :: b -> (a -> b) -> Maybe a -> b
92 maybe _ f (Just x) = f x
95 -- | The 'isJust' function returns 'True' iff its argument is of the
97 isJust :: Maybe a -> Bool
98 isJust Nothing = False
101 -- | The 'isNothing' function returns 'True' iff its argument is 'Nothing'.
102 isNothing :: Maybe a -> Bool
103 isNothing Nothing = True
106 -- | The 'fromJust' function extracts the element out of a 'Just' and
107 -- throws an error if its argument is 'Nothing'.
108 fromJust :: Maybe a -> a
109 fromJust Nothing = error "Maybe.fromJust: Nothing" -- yuck
110 fromJust (Just x) = x
112 -- | The 'fromMaybe' function takes a default value and and 'Maybe'
113 -- value. If the 'Maybe' is 'Nothing', it returns the default values;
114 -- otherwise, it returns the value contained in the 'Maybe'.
115 fromMaybe :: a -> Maybe a -> a
116 fromMaybe d x = case x of {Nothing -> d;Just v -> v}
118 -- | The 'maybeToList' function returns an empty list when given
119 -- 'Nothing' or a singleton list when not given 'Nothing'.
120 maybeToList :: Maybe a -> [a]
121 maybeToList Nothing = []
122 maybeToList (Just x) = [x]
124 -- | The 'listToMaybe' function returns 'Nothing' on an empty list
125 -- or @'Just' a@ where @a@ is the first element of the list.
126 listToMaybe :: [a] -> Maybe a
127 listToMaybe [] = Nothing
128 listToMaybe (a:_) = Just a
130 -- | The 'catMaybes' function takes a list of 'Maybe's and returns
131 -- a list of all the 'Just' values.
132 catMaybes :: [Maybe a] -> [a]
133 catMaybes ls = [x | Just x <- ls]
135 -- | The 'mapMaybe' function is a version of 'map' which can throw
136 -- out elements. In particular, the functional argument returns
137 -- something of type @'Maybe' b@. If this is 'Nothing', no element
138 -- is added on to the result list. If it just @'Just' b@, then @b@ is
139 -- included in the result list.
140 mapMaybe :: (a -> Maybe b) -> [a] -> [b]
143 let rs = mapMaybe f xs in
148 #endif /* else not __NHC__ */