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 %************************************************************************
55 maybeToBool :: Maybe a -> Bool
56 maybeToBool Nothing = False
57 maybeToBool (Just x) = True
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
66 catMaybes :: [Maybe a] -> [a]
68 catMaybes (Nothing : xs) = catMaybes xs
69 catMaybes (Just x : xs) = (x : catMaybes xs)
71 allMaybes :: [Maybe a] -> Maybe [a]
72 allMaybes [] = Just []
73 allMaybes (Nothing : ms) = Nothing
74 allMaybes (Just x : ms) = case (allMaybes ms) of
76 Just xs -> Just (x:xs)
79 @firstJust@ takes a list of @Maybes@ and returns the
80 first @Just@ if there is one, or @Nothing@ otherwise.
83 firstJust :: [Maybe a] -> Maybe a
84 firstJust [] = Nothing
85 firstJust (Just x : ms) = Just x
86 firstJust (Nothing : ms) = firstJust ms
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
98 expectJust :: String -> Maybe a -> a
99 {-# INLINE expectJust #-}
100 expectJust err (Just x) = x
101 expectJust err Nothing = error ("expectJust " ++ err)
107 seqMaybe :: Maybe a -> Maybe a -> Maybe a
108 seqMaybe (Just x) _ = Just x
109 seqMaybe Nothing my = my
111 returnMaybe :: a -> Maybe a
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' ->
127 @assocMaybe@ looks up in an assocation list, returning
128 @Nothing@ if it fails.
131 assocMaybe :: (Eq a) => [(a,b)] -> a -> Maybe b
137 lookup ((tv,ty):rest) = if key == tv then Just ty else lookup rest
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
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
158 mkLookupFun :: (key -> key -> Bool) -- Equality predicate
159 -> [(key,val)] -- The assoc list
161 -> Maybe val -- The corresponding value
163 mkLookupFun eq alist s
164 = case [a | (s',a) <- alist, s' `eq` s] of
168 mkLookupFunDef :: (key -> key -> Bool) -- Equality predicate
169 -> [(key,val)] -- The assoc list
170 -> val -- Value to return on failure
172 -> val -- The corresponding value
174 mkLookupFunDef eq alist deflt s
175 = case [a | (s',a) <- alist, s' `eq` s] of
180 %************************************************************************
182 \subsection[MaybeErr type]{The @MaybeErr@ type}
184 %************************************************************************
187 data MaybeErr val err = Succeeded val | Failed err
191 thenMaB :: MaybeErr val1 err -> (val1 -> MaybeErr val2 err) -> MaybeErr val2 err
197 returnMaB :: val -> MaybeErr val err
198 returnMaB v = Succeeded v
200 failMaB :: err -> MaybeErr val err
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.
210 listMaybeErrs :: [MaybeErr val err] -> MaybeErr [val] [err]
212 = foldr combine (Succeeded [])
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)
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.
225 foldlMaybeErrs :: (acc -> input -> MaybeErr acc err)
228 -> MaybeErr acc [err]
230 foldlMaybeErrs k accum ins = do_it [] accum ins
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