[project @ 1996-05-17 16:02:43 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 maybeToBool :: Maybe a -> Bool
56 maybeToBool Nothing  = False
57 maybeToBool (Just x) = True
58 \end{code}
59
60 @catMaybes@ takes a list of @Maybe@s and returns a list of
61 the contents of all the @Just@s in it.  @allMaybes@ collects
62 a list of @Justs@ into a single @Just@, returning @Nothing@ if there
63 are any @Nothings@.
64
65 \begin{code}
66 catMaybes :: [Maybe a] -> [a]
67 catMaybes []                = []
68 catMaybes (Nothing : xs)   = catMaybes xs
69 catMaybes (Just x : xs)    = (x : catMaybes xs)
70
71 allMaybes :: [Maybe a] -> Maybe [a]
72 allMaybes [] = Just []
73 allMaybes (Nothing : ms) = Nothing
74 allMaybes (Just x  : ms) = case (allMaybes ms) of
75                              Nothing -> Nothing
76                              Just xs -> Just (x:xs)
77 \end{code}
78
79 @firstJust@ takes a list of @Maybes@ and returns the
80 first @Just@ if there is one, or @Nothing@ otherwise.
81
82 \begin{code}
83 firstJust :: [Maybe a] -> Maybe a
84 firstJust [] = Nothing
85 firstJust (Just x  : ms) = Just x
86 firstJust (Nothing : ms) = firstJust ms
87 \end{code}
88
89 \begin{code}
90 findJust :: (a -> Maybe b) -> [a] -> Maybe b
91 findJust f []     = Nothing
92 findJust f (a:as) = case f a of
93                       Nothing -> findJust f as
94                       b  -> b
95 \end{code}
96
97 \begin{code}
98 expectJust :: String -> Maybe a -> a
99 {-# INLINE expectJust #-}
100 expectJust err (Just x) = x
101 expectJust err Nothing  = error ("expectJust " ++ err)
102 \end{code}
103
104 The Maybe monad
105 ~~~~~~~~~~~~~~~
106 \begin{code}
107 seqMaybe :: Maybe a -> Maybe a -> Maybe a
108 seqMaybe (Just x) _  = Just x
109 seqMaybe Nothing  my = my
110
111 returnMaybe :: a -> Maybe a
112 returnMaybe = Just
113
114 failMaybe :: Maybe a
115 failMaybe = Nothing
116
117 mapMaybe :: (a -> Maybe b) -> [a] -> Maybe [b]
118 mapMaybe f []     = returnMaybe []
119 mapMaybe f (x:xs) = f x                 `thenMaybe` \ x' ->
120                     mapMaybe f xs       `thenMaybe` \ xs' ->
121                     returnMaybe (x':xs')
122 \end{code}
123
124 Lookup functions
125 ~~~~~~~~~~~~~~~~
126
127 @assocMaybe@ looks up in an assocation list, returning
128 @Nothing@ if it fails.
129
130 \begin{code}
131 assocMaybe :: (Eq a) => [(a,b)] -> a -> Maybe b
132
133 assocMaybe alist key
134   = lookup alist
135   where
136     lookup []             = Nothing
137     lookup ((tv,ty):rest) = if key == tv then Just ty else lookup rest
138
139 #if defined(COMPILING_GHC)
140 {-? SPECIALIZE assocMaybe
141         :: [(String,        b)] -> String        -> Maybe b,
142            [(Id,            b)] -> Id            -> Maybe b,
143            [(Class,         b)] -> Class         -> Maybe b,
144            [(Int,           b)] -> Int           -> Maybe b,
145            [(Name,          b)] -> Name          -> Maybe b,
146            [(TyVar,         b)] -> TyVar         -> Maybe b,
147            [(TyVarTemplate, b)] -> TyVarTemplate -> Maybe b
148   #-}
149 #endif
150 \end{code}
151
152 @mkLookupFun eq alist@ is a function which looks up
153 its argument in the association list @alist@, returning a Maybe type.
154 @mkLookupFunDef@ is similar except that it is given a value to return
155 on failure.
156
157 \begin{code}
158 mkLookupFun :: (key -> key -> Bool)     -- Equality predicate
159             -> [(key,val)]              -- The assoc list
160             -> key                      -- The key
161             -> Maybe val                -- The corresponding value
162
163 mkLookupFun eq alist s
164   = case [a | (s',a) <- alist, s' `eq` s] of
165       []    -> Nothing
166       (a:_) -> Just a
167
168 mkLookupFunDef :: (key -> key -> Bool)  -- Equality predicate
169                -> [(key,val)]           -- The assoc list
170                -> val                   -- Value to return on failure
171                -> key                   -- The key
172                -> val                   -- The corresponding value
173
174 mkLookupFunDef eq alist deflt s
175   = case [a | (s',a) <- alist, s' `eq` s] of
176       []    -> deflt
177       (a:_) -> a
178 \end{code}
179
180 %************************************************************************
181 %*                                                                      *
182 \subsection[MaybeErr type]{The @MaybeErr@ type}
183 %*                                                                      *
184 %************************************************************************
185
186 \begin{code}
187 data MaybeErr val err = Succeeded val | Failed err
188 \end{code}
189
190 \begin{code}
191 thenMaB :: MaybeErr val1 err -> (val1 -> MaybeErr val2 err) -> MaybeErr val2 err
192 thenMaB m k
193   = case m of
194       Succeeded v -> k v
195       Failed e    -> Failed e
196
197 returnMaB :: val -> MaybeErr val err
198 returnMaB v = Succeeded v
199
200 failMaB :: err -> MaybeErr val err
201 failMaB e = Failed e
202 \end{code}
203
204
205 @listMaybeErrs@ takes a list of @MaybeErrs@ and, if they all succeed, returns
206 a @Succeeded@ of a list of their values.  If any fail, it returns a
207 @Failed@ of the list of all the errors in the list.
208
209 \begin{code}
210 listMaybeErrs :: [MaybeErr val err] -> MaybeErr [val] [err]
211 listMaybeErrs
212   = foldr combine (Succeeded [])
213   where
214     combine (Succeeded v) (Succeeded vs) = Succeeded (v:vs)
215     combine (Failed err)  (Succeeded _)  = Failed [err]
216     combine (Succeeded v) (Failed errs)  = Failed errs
217     combine (Failed err)  (Failed errs)  = Failed (err:errs)
218 \end{code}
219
220 @foldlMaybeErrs@ works along a list, carrying an accumulator; it
221 applies the given function to the accumulator and the next list item,
222 accumulating any errors that occur.
223
224 \begin{code}
225 foldlMaybeErrs :: (acc -> input -> MaybeErr acc err)
226                -> acc
227                -> [input]
228                -> MaybeErr acc [err]
229
230 foldlMaybeErrs k accum ins = do_it [] accum ins
231   where
232     do_it []   acc []     = Succeeded acc
233     do_it errs acc []     = Failed errs
234     do_it errs acc (v:vs) = case (k acc v) of
235                               Succeeded acc' -> do_it errs       acc' vs
236                               Failed err     -> do_it (err:errs) acc  vs
237 \end{code}