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