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