2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[Maybes]{The `Maybe' types and associated utility functions}
7 #if defined(COMPILING_GHC)
8 #include "HsVersions.h"
12 -- Maybe(..), -- no, it's in 1.3
22 mkLookupFun, mkLookupFunDef,
33 #if ! defined(COMPILING_GHC)
40 #if defined(COMPILING_GHC)
42 CHK_Ubiq() -- debugging consistency check
48 %************************************************************************
50 \subsection[Maybe type]{The @Maybe@ type}
52 %************************************************************************
63 maybeToBool :: Maybe a -> Bool
64 maybeToBool Nothing = False
65 maybeToBool (Just x) = True
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
74 catMaybes :: [Maybe a] -> [a]
76 catMaybes (Nothing : xs) = catMaybes xs
77 catMaybes (Just x : xs) = (x : catMaybes xs)
79 allMaybes :: [Maybe a] -> Maybe [a]
80 allMaybes [] = Just []
81 allMaybes (Nothing : ms) = Nothing
82 allMaybes (Just x : ms) = case (allMaybes ms) of
84 Just xs -> Just (x:xs)
87 @firstJust@ takes a list of @Maybes@ and returns the
88 first @Just@ if there is one, or @Nothing@ otherwise.
91 firstJust :: [Maybe a] -> Maybe a
92 firstJust [] = Nothing
93 firstJust (Just x : ms) = Just x
94 firstJust (Nothing : ms) = firstJust ms
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
106 expectJust :: String -> Maybe a -> a
107 {-# INLINE expectJust #-}
108 expectJust err (Just x) = x
109 expectJust err Nothing = error ("expectJust " ++ err)
116 thenMaybe :: Maybe a -> (a -> Maybe b) -> Maybe b
117 m `thenMaybe` k = case m of
122 seqMaybe :: Maybe a -> Maybe a -> Maybe a
123 seqMaybe (Just x) _ = Just x
124 seqMaybe Nothing my = my
126 returnMaybe :: a -> Maybe a
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' ->
142 @assocMaybe@ looks up in an assocation list, returning
143 @Nothing@ if it fails.
146 assocMaybe :: (Eq a) => [(a,b)] -> a -> Maybe b
152 lookup ((tv,ty):rest) = if key == tv then Just ty else lookup rest
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
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
173 mkLookupFun :: (key -> key -> Bool) -- Equality predicate
174 -> [(key,val)] -- The assoc list
176 -> Maybe val -- The corresponding value
178 mkLookupFun eq alist s
179 = case [a | (s',a) <- alist, s' `eq` s] of
183 mkLookupFunDef :: (key -> key -> Bool) -- Equality predicate
184 -> [(key,val)] -- The assoc list
185 -> val -- Value to return on failure
187 -> val -- The corresponding value
189 mkLookupFunDef eq alist deflt s
190 = case [a | (s',a) <- alist, s' `eq` s] of
195 %************************************************************************
197 \subsection[MaybeErr type]{The @MaybeErr@ type}
199 %************************************************************************
202 data MaybeErr val err = Succeeded val | Failed err
206 thenMaB :: MaybeErr val1 err -> (val1 -> MaybeErr val2 err) -> MaybeErr val2 err
212 returnMaB :: val -> MaybeErr val err
213 returnMaB v = Succeeded v
215 failMaB :: err -> MaybeErr val err
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.
225 listMaybeErrs :: [MaybeErr val err] -> MaybeErr [val] [err]
227 = foldr combine (Succeeded [])
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)
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.
240 foldlMaybeErrs :: (acc -> input -> MaybeErr acc err)
243 -> MaybeErr acc [err]
245 foldlMaybeErrs k accum ins = do_it [] accum ins
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