981d028d42443c95fec1bb053b9e8c3215a4c2d1
[ghc-base.git] / Data / Maybe.hs
1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Data.Maybe
5 -- Copyright   :  (c) The University of Glasgow 2001
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  stable
10 -- Portability :  portable
11 --
12 -- The Maybe type, and associated operations.
13 --
14 -----------------------------------------------------------------------------
15
16 module Data.Maybe
17    (
18      Maybe(Nothing,Just)-- instance of: Eq, Ord, Show, Read,
19                         --              Functor, Monad, MonadPlus
20
21    , maybe              -- :: b -> (a -> b) -> Maybe a -> b
22
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]
31    ) where
32
33 #ifdef __GLASGOW_HASKELL__
34 import GHC.Base
35 #endif
36
37 #ifdef __NHC__
38 import Prelude
39 import Prelude (Maybe(..), maybe)
40 import Maybe
41     ( isJust
42     , isNothing
43     , fromJust
44     , fromMaybe
45     , listToMaybe
46     , maybeToList
47     , catMaybes
48     , mapMaybe
49     )
50 #else
51
52 #ifndef __HUGS__
53 -- ---------------------------------------------------------------------------
54 -- The Maybe type, and instances
55
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'.
61 --
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.
65
66 data  Maybe a  =  Nothing | Just a
67   deriving (Eq, Ord)
68
69 instance  Functor Maybe  where
70     fmap _ Nothing       = Nothing
71     fmap f (Just a)      = Just (f a)
72
73 instance  Monad Maybe  where
74     (Just x) >>= k      = k x
75     Nothing  >>= _      = Nothing
76
77     (Just _) >>  k      = k
78     Nothing  >>  _      = Nothing
79
80     return              = Just
81     fail _              = Nothing
82
83 -- ---------------------------------------------------------------------------
84 -- Functions over Maybe
85
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
91 maybe n _ Nothing  = n
92 maybe _ f (Just x) = f x
93 #endif  /* __HUGS__ */
94
95 -- | The 'isJust' function returns 'True' iff its argument is of the
96 -- form @Just _@.
97 isJust         :: Maybe a -> Bool
98 isJust Nothing = False
99 isJust _       = True
100
101 -- | The 'isNothing' function returns 'True' iff its argument is 'Nothing'.
102 isNothing         :: Maybe a -> Bool
103 isNothing Nothing = True
104 isNothing _       = False
105
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
111
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}
117
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]
123
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
129
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]
134
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]
141 mapMaybe _ []     = []
142 mapMaybe f (x:xs) =
143  let rs = mapMaybe f xs in
144  case f x of
145   Nothing -> rs
146   Just r  -> r:rs
147
148 #endif /* else not __NHC__ */