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,
31 #if ! defined(COMPILING_GHC)
38 #if defined(COMPILING_GHC)
40 CHK_Ubiq() -- debugging consistency check
46 %************************************************************************
48 \subsection[Maybe type]{The @Maybe@ type}
50 %************************************************************************
53 maybeToBool :: Maybe a -> Bool
54 maybeToBool Nothing = False
55 maybeToBool (Just x) = True
58 @catMaybes@ takes a list of @Maybe@s and returns a list of
59 the contents of all the @Just@s in it. @allMaybes@ collects
60 a list of @Justs@ into a single @Just@, returning @Nothing@ if there
64 catMaybes :: [Maybe a] -> [a]
66 catMaybes (Nothing : xs) = catMaybes xs
67 catMaybes (Just x : xs) = (x : catMaybes xs)
69 allMaybes :: [Maybe a] -> Maybe [a]
70 allMaybes [] = Just []
71 allMaybes (Nothing : ms) = Nothing
72 allMaybes (Just x : ms) = case (allMaybes ms) of
74 Just xs -> Just (x:xs)
77 @firstJust@ takes a list of @Maybes@ and returns the
78 first @Just@ if there is one, or @Nothing@ otherwise.
81 firstJust :: [Maybe a] -> Maybe a
82 firstJust [] = Nothing
83 firstJust (Just x : ms) = Just x
84 firstJust (Nothing : ms) = firstJust ms
88 findJust :: (a -> Maybe b) -> [a] -> Maybe b
89 findJust f [] = Nothing
90 findJust f (a:as) = case f a of
91 Nothing -> findJust f as
96 expectJust :: String -> Maybe a -> a
97 {-# INLINE expectJust #-}
98 expectJust err (Just x) = x
99 expectJust err Nothing = error ("expectJust " ++ err)
105 seqMaybe :: Maybe a -> Maybe a -> Maybe a
106 seqMaybe (Just x) _ = Just x
107 seqMaybe Nothing my = my
109 returnMaybe :: a -> Maybe a
119 @assocMaybe@ looks up in an assocation list, returning
120 @Nothing@ if it fails.
123 assocMaybe :: (Eq a) => [(a,b)] -> a -> Maybe b
129 lookup ((tv,ty):rest) = if key == tv then Just ty else lookup rest
131 #if defined(COMPILING_GHC)
132 {-? SPECIALIZE assocMaybe
133 :: [(String, b)] -> String -> Maybe b,
134 [(Id, b)] -> Id -> Maybe b,
135 [(Class, b)] -> Class -> Maybe b,
136 [(Int, b)] -> Int -> Maybe b,
137 [(Name, b)] -> Name -> Maybe b,
138 [(TyVar, b)] -> TyVar -> Maybe b,
139 [(TyVarTemplate, b)] -> TyVarTemplate -> Maybe b
144 @mkLookupFun eq alist@ is a function which looks up
145 its argument in the association list @alist@, returning a Maybe type.
146 @mkLookupFunDef@ is similar except that it is given a value to return
150 mkLookupFun :: (key -> key -> Bool) -- Equality predicate
151 -> [(key,val)] -- The assoc list
153 -> Maybe val -- The corresponding value
155 mkLookupFun eq alist s
156 = case [a | (s',a) <- alist, s' `eq` s] of
160 mkLookupFunDef :: (key -> key -> Bool) -- Equality predicate
161 -> [(key,val)] -- The assoc list
162 -> val -- Value to return on failure
164 -> val -- The corresponding value
166 mkLookupFunDef eq alist deflt s
167 = case [a | (s',a) <- alist, s' `eq` s] of
172 %************************************************************************
174 \subsection[MaybeErr type]{The @MaybeErr@ type}
176 %************************************************************************
179 data MaybeErr val err = Succeeded val | Failed err
183 thenMaB :: MaybeErr val1 err -> (val1 -> MaybeErr val2 err) -> MaybeErr val2 err
189 returnMaB :: val -> MaybeErr val err
190 returnMaB v = Succeeded v
192 failMaB :: err -> MaybeErr val err
197 @listMaybeErrs@ takes a list of @MaybeErrs@ and, if they all succeed, returns
198 a @Succeeded@ of a list of their values. If any fail, it returns a
199 @Failed@ of the list of all the errors in the list.
202 listMaybeErrs :: [MaybeErr val err] -> MaybeErr [val] [err]
204 = foldr combine (Succeeded [])
206 combine (Succeeded v) (Succeeded vs) = Succeeded (v:vs)
207 combine (Failed err) (Succeeded _) = Failed [err]
208 combine (Succeeded v) (Failed errs) = Failed errs
209 combine (Failed err) (Failed errs) = Failed (err:errs)
212 @foldlMaybeErrs@ works along a list, carrying an accumulator; it
213 applies the given function to the accumulator and the next list item,
214 accumulating any errors that occur.
217 foldlMaybeErrs :: (acc -> input -> MaybeErr acc err)
220 -> MaybeErr acc [err]
222 foldlMaybeErrs k accum ins = do_it [] accum ins
224 do_it [] acc [] = Succeeded acc
225 do_it errs acc [] = Failed errs
226 do_it errs acc (v:vs) = case (k acc v) of
227 Succeeded acc' -> do_it errs acc' vs
228 Failed err -> do_it (err:errs) acc vs