[project @ 1996-05-01 18:36:59 by partain]
[ghc-hetmet.git] / ghc / compiler / utils / Maybes.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[Maybes]{The `Maybe' types and associated utility functions}
5
6 \begin{code}
7 #if defined(COMPILING_GHC)
8 #include "HsVersions.h"
9 #endif
10
11 module Maybes (
12 --      Maybe(..), -- no, it's in 1.3
13         MaybeErr(..),
14
15         allMaybes,
16         catMaybes,
17         firstJust,
18         expectJust,
19         maybeToBool,
20
21         assocMaybe,
22         mkLookupFun, mkLookupFunDef,
23
24         failMaB,
25         failMaybe,
26         seqMaybe,
27         mapMaybe,
28         returnMaB,
29         returnMaybe,
30         thenMaB,
31         thenMaybe
32
33 #if ! defined(COMPILING_GHC)
34         , findJust
35         , foldlMaybeErrs
36         , listMaybeErrs
37 #endif
38     ) where
39
40 #if defined(COMPILING_GHC)
41
42 CHK_Ubiq() -- debugging consistency check
43
44 #endif
45 \end{code}
46
47
48 %************************************************************************
49 %*                                                                      *
50 \subsection[Maybe type]{The @Maybe@ type}
51 %*                                                                      *
52 %************************************************************************
53
54 \begin{code}
55 #if __HASKELL1__ < 3
56 data Maybe a
57   = Nothing
58   | Just a
59 #endif
60 \end{code}
61
62 \begin{code}
63 maybeToBool :: Maybe a -> Bool
64 maybeToBool Nothing  = False
65 maybeToBool (Just x) = True
66 \end{code}
67
68 @catMaybes@ takes a list of @Maybe@s and returns a list of
69 the contents of all the @Just@s in it.  @allMaybes@ collects
70 a list of @Justs@ into a single @Just@, returning @Nothing@ if there
71 are any @Nothings@.
72
73 \begin{code}
74 catMaybes :: [Maybe a] -> [a]
75 catMaybes []                = []
76 catMaybes (Nothing : xs)   = catMaybes xs
77 catMaybes (Just x : xs)    = (x : catMaybes xs)
78
79 allMaybes :: [Maybe a] -> Maybe [a]
80 allMaybes [] = Just []
81 allMaybes (Nothing : ms) = Nothing
82 allMaybes (Just x  : ms) = case (allMaybes ms) of
83                              Nothing -> Nothing
84                              Just xs -> Just (x:xs)
85 \end{code}
86
87 @firstJust@ takes a list of @Maybes@ and returns the
88 first @Just@ if there is one, or @Nothing@ otherwise.
89
90 \begin{code}
91 firstJust :: [Maybe a] -> Maybe a
92 firstJust [] = Nothing
93 firstJust (Just x  : ms) = Just x
94 firstJust (Nothing : ms) = firstJust ms
95 \end{code}
96
97 \begin{code}
98 findJust :: (a -> Maybe b) -> [a] -> Maybe b
99 findJust f []     = Nothing
100 findJust f (a:as) = case f a of
101                       Nothing -> findJust f as
102                       b  -> b
103 \end{code}
104
105 \begin{code}
106 expectJust :: String -> Maybe a -> a
107 {-# INLINE expectJust #-}
108 expectJust err (Just x) = x
109 expectJust err Nothing  = error ("expectJust " ++ err)
110 \end{code}
111
112 The Maybe monad
113 ~~~~~~~~~~~~~~~
114 \begin{code}
115 #if __HASKELL1__ < 3
116 thenMaybe :: Maybe a -> (a -> Maybe b) -> Maybe b
117 m `thenMaybe` k = case m of
118                   Nothing -> Nothing
119                   Just a  -> k a
120 #endif
121
122 seqMaybe :: Maybe a -> Maybe a -> Maybe a
123 seqMaybe (Just x) _  = Just x
124 seqMaybe Nothing  my = my
125
126 returnMaybe :: a -> Maybe a
127 returnMaybe = Just
128
129 failMaybe :: Maybe a
130 failMaybe = Nothing
131
132 mapMaybe :: (a -> Maybe b) -> [a] -> Maybe [b]
133 mapMaybe f []     = returnMaybe []
134 mapMaybe f (x:xs) = f x                 `thenMaybe` \ x' ->
135                     mapMaybe f xs       `thenMaybe` \ xs' ->
136                     returnMaybe (x':xs')
137 \end{code}
138
139 Lookup functions
140 ~~~~~~~~~~~~~~~~
141
142 @assocMaybe@ looks up in an assocation list, returning
143 @Nothing@ if it fails.
144
145 \begin{code}
146 assocMaybe :: (Eq a) => [(a,b)] -> a -> Maybe b
147
148 assocMaybe alist key
149   = lookup alist
150   where
151     lookup []             = Nothing
152     lookup ((tv,ty):rest) = if key == tv then Just ty else lookup rest
153
154 #if defined(COMPILING_GHC)
155 {-? SPECIALIZE assocMaybe
156         :: [(String,        b)] -> String        -> Maybe b,
157            [(Id,            b)] -> Id            -> Maybe b,
158            [(Class,         b)] -> Class         -> Maybe b,
159            [(Int,           b)] -> Int           -> Maybe b,
160            [(Name,          b)] -> Name          -> Maybe b,
161            [(TyVar,         b)] -> TyVar         -> Maybe b,
162            [(TyVarTemplate, b)] -> TyVarTemplate -> Maybe b
163   #-}
164 #endif
165 \end{code}
166
167 @mkLookupFun eq alist@ is a function which looks up
168 its argument in the association list @alist@, returning a Maybe type.
169 @mkLookupFunDef@ is similar except that it is given a value to return
170 on failure.
171
172 \begin{code}
173 mkLookupFun :: (key -> key -> Bool)     -- Equality predicate
174             -> [(key,val)]              -- The assoc list
175             -> key                      -- The key
176             -> Maybe val                -- The corresponding value
177
178 mkLookupFun eq alist s
179   = case [a | (s',a) <- alist, s' `eq` s] of
180       []    -> Nothing
181       (a:_) -> Just a
182
183 mkLookupFunDef :: (key -> key -> Bool)  -- Equality predicate
184                -> [(key,val)]           -- The assoc list
185                -> val                   -- Value to return on failure
186                -> key                   -- The key
187                -> val                   -- The corresponding value
188
189 mkLookupFunDef eq alist deflt s
190   = case [a | (s',a) <- alist, s' `eq` s] of
191       []    -> deflt
192       (a:_) -> a
193 \end{code}
194
195 %************************************************************************
196 %*                                                                      *
197 \subsection[MaybeErr type]{The @MaybeErr@ type}
198 %*                                                                      *
199 %************************************************************************
200
201 \begin{code}
202 data MaybeErr val err = Succeeded val | Failed err
203 \end{code}
204
205 \begin{code}
206 thenMaB :: MaybeErr val1 err -> (val1 -> MaybeErr val2 err) -> MaybeErr val2 err
207 thenMaB m k
208   = case m of
209       Succeeded v -> k v
210       Failed e    -> Failed e
211
212 returnMaB :: val -> MaybeErr val err
213 returnMaB v = Succeeded v
214
215 failMaB :: err -> MaybeErr val err
216 failMaB e = Failed e
217 \end{code}
218
219
220 @listMaybeErrs@ takes a list of @MaybeErrs@ and, if they all succeed, returns
221 a @Succeeded@ of a list of their values.  If any fail, it returns a
222 @Failed@ of the list of all the errors in the list.
223
224 \begin{code}
225 listMaybeErrs :: [MaybeErr val err] -> MaybeErr [val] [err]
226 listMaybeErrs
227   = foldr combine (Succeeded [])
228   where
229     combine (Succeeded v) (Succeeded vs) = Succeeded (v:vs)
230     combine (Failed err)  (Succeeded _)  = Failed [err]
231     combine (Succeeded v) (Failed errs)  = Failed errs
232     combine (Failed err)  (Failed errs)  = Failed (err:errs)
233 \end{code}
234
235 @foldlMaybeErrs@ works along a list, carrying an accumulator; it
236 applies the given function to the accumulator and the next list item,
237 accumulating any errors that occur.
238
239 \begin{code}
240 foldlMaybeErrs :: (acc -> input -> MaybeErr acc err)
241                -> acc
242                -> [input]
243                -> MaybeErr acc [err]
244
245 foldlMaybeErrs k accum ins = do_it [] accum ins
246   where
247     do_it []   acc []     = Succeeded acc
248     do_it errs acc []     = Failed errs
249     do_it errs acc (v:vs) = case (k acc v) of
250                               Succeeded acc' -> do_it errs       acc' vs
251                               Failed err     -> do_it (err:errs) acc  vs
252 \end{code}