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)
40 #if defined(COMPILING_GHC)
42 CHK_Ubiq() -- debugging consistency check
44 IMPORT_DELOOPER( SpecLoop ) -- Specialisation
45 import Unique (Unique) -- only for specialising
48 import Maybe -- renamer will tell us if there are any conflicts
53 %************************************************************************
55 \subsection[Maybe type]{The @Maybe@ type}
57 %************************************************************************
60 maybeToBool :: Maybe a -> Bool
61 maybeToBool Nothing = False
62 maybeToBool (Just x) = True
65 @catMaybes@ takes a list of @Maybe@s and returns a list of
66 the contents of all the @Just@s in it. @allMaybes@ collects
67 a list of @Justs@ into a single @Just@, returning @Nothing@ if there
72 catMaybes :: [Maybe a] -> [a]
74 catMaybes (Nothing : xs) = catMaybes xs
75 catMaybes (Just x : xs) = (x : catMaybes xs)
78 allMaybes :: [Maybe a] -> Maybe [a]
79 allMaybes [] = Just []
80 allMaybes (Nothing : ms) = Nothing
81 allMaybes (Just x : ms) = case (allMaybes ms) of
83 Just xs -> Just (x:xs)
85 mapMaybe :: (a -> Maybe b) -> [a] -> [b]
87 mapMaybe f (x:xs) = case f x of
88 Just y -> y : mapMaybe f xs
89 Nothing -> mapMaybe f xs
92 @firstJust@ takes a list of @Maybes@ and returns the
93 first @Just@ if there is one, or @Nothing@ otherwise.
96 firstJust :: [Maybe a] -> Maybe a
97 firstJust [] = Nothing
98 firstJust (Just x : ms) = Just x
99 firstJust (Nothing : ms) = firstJust ms
103 findJust :: (a -> Maybe b) -> [a] -> Maybe b
104 findJust f [] = Nothing
105 findJust f (a:as) = case f a of
106 Nothing -> findJust f as
111 expectJust :: String -> Maybe a -> a
112 {-# INLINE expectJust #-}
113 expectJust err (Just x) = x
114 expectJust err Nothing = error ("expectJust " ++ err)
120 seqMaybe :: Maybe a -> Maybe a -> Maybe a
121 seqMaybe (Just x) _ = Just x
122 seqMaybe Nothing my = my
124 returnMaybe :: a -> Maybe a
134 @assocMaybe@ looks up in an assocation list, returning
135 @Nothing@ if it fails.
138 assocMaybe :: (Eq a) => [(a,b)] -> a -> Maybe b
144 lookup ((tv,ty):rest) = if key == tv then Just ty else lookup rest
146 #if defined(COMPILING_GHC)
147 {-# SPECIALIZE assocMaybe
148 :: [(FAST_STRING, b)] -> FAST_STRING -> Maybe b
149 , [(Int, b)] -> Int -> Maybe b
150 , [(Unique, b)] -> Unique -> Maybe b
155 @mkLookupFun eq alist@ is a function which looks up
156 its argument in the association list @alist@, returning a Maybe type.
157 @mkLookupFunDef@ is similar except that it is given a value to return
161 mkLookupFun :: (key -> key -> Bool) -- Equality predicate
162 -> [(key,val)] -- The assoc list
164 -> Maybe val -- The corresponding value
166 mkLookupFun eq alist s
167 = case [a | (s',a) <- alist, s' `eq` s] of
171 mkLookupFunDef :: (key -> key -> Bool) -- Equality predicate
172 -> [(key,val)] -- The assoc list
173 -> val -- Value to return on failure
175 -> val -- The corresponding value
177 mkLookupFunDef eq alist deflt s
178 = case [a | (s',a) <- alist, s' `eq` s] of
183 %************************************************************************
185 \subsection[MaybeErr type]{The @MaybeErr@ type}
187 %************************************************************************
190 data MaybeErr val err = Succeeded val | Failed err
194 thenMaB :: MaybeErr val1 err -> (val1 -> MaybeErr val2 err) -> MaybeErr val2 err
200 returnMaB :: val -> MaybeErr val err
201 returnMaB v = Succeeded v
203 failMaB :: err -> MaybeErr val err
208 @listMaybeErrs@ takes a list of @MaybeErrs@ and, if they all succeed, returns
209 a @Succeeded@ of a list of their values. If any fail, it returns a
210 @Failed@ of the list of all the errors in the list.
213 listMaybeErrs :: [MaybeErr val err] -> MaybeErr [val] [err]
215 = foldr combine (Succeeded [])
217 combine (Succeeded v) (Succeeded vs) = Succeeded (v:vs)
218 combine (Failed err) (Succeeded _) = Failed [err]
219 combine (Succeeded v) (Failed errs) = Failed errs
220 combine (Failed err) (Failed errs) = Failed (err:errs)
223 @foldlMaybeErrs@ works along a list, carrying an accumulator; it
224 applies the given function to the accumulator and the next list item,
225 accumulating any errors that occur.
228 foldlMaybeErrs :: (acc -> input -> MaybeErr acc err)
231 -> MaybeErr acc [err]
233 foldlMaybeErrs k accum ins = do_it [] accum ins
235 do_it [] acc [] = Succeeded acc
236 do_it errs acc [] = Failed errs
237 do_it errs acc (v:vs) = case (k acc v) of
238 Succeeded acc' -> do_it errs acc' vs
239 Failed err -> do_it (err:errs) acc vs