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
42 import Unique (Unique) -- only for specialising
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
121 @assocMaybe@ looks up in an assocation list, returning
122 @Nothing@ if it fails.
125 assocMaybe :: (Eq a) => [(a,b)] -> a -> Maybe b
131 lookup ((tv,ty):rest) = if key == tv then Just ty else lookup rest
133 #if defined(COMPILING_GHC)
134 {-# SPECIALIZE assocMaybe
135 :: [(FAST_STRING, b)] -> FAST_STRING -> Maybe b
136 , [(Int, b)] -> Int -> Maybe b
137 , [(Unique, b)] -> Unique -> Maybe b
138 , [(RdrName, b)] -> RdrName -> Maybe b
143 @mkLookupFun eq alist@ is a function which looks up
144 its argument in the association list @alist@, returning a Maybe type.
145 @mkLookupFunDef@ is similar except that it is given a value to return
149 mkLookupFun :: (key -> key -> Bool) -- Equality predicate
150 -> [(key,val)] -- The assoc list
152 -> Maybe val -- The corresponding value
154 mkLookupFun eq alist s
155 = case [a | (s',a) <- alist, s' `eq` s] of
159 mkLookupFunDef :: (key -> key -> Bool) -- Equality predicate
160 -> [(key,val)] -- The assoc list
161 -> val -- Value to return on failure
163 -> val -- The corresponding value
165 mkLookupFunDef eq alist deflt s
166 = case [a | (s',a) <- alist, s' `eq` s] of
171 %************************************************************************
173 \subsection[MaybeErr type]{The @MaybeErr@ type}
175 %************************************************************************
178 data MaybeErr val err = Succeeded val | Failed err
182 thenMaB :: MaybeErr val1 err -> (val1 -> MaybeErr val2 err) -> MaybeErr val2 err
188 returnMaB :: val -> MaybeErr val err
189 returnMaB v = Succeeded v
191 failMaB :: err -> MaybeErr val err
196 @listMaybeErrs@ takes a list of @MaybeErrs@ and, if they all succeed, returns
197 a @Succeeded@ of a list of their values. If any fail, it returns a
198 @Failed@ of the list of all the errors in the list.
201 listMaybeErrs :: [MaybeErr val err] -> MaybeErr [val] [err]
203 = foldr combine (Succeeded [])
205 combine (Succeeded v) (Succeeded vs) = Succeeded (v:vs)
206 combine (Failed err) (Succeeded _) = Failed [err]
207 combine (Succeeded v) (Failed errs) = Failed errs
208 combine (Failed err) (Failed errs) = Failed (err:errs)
211 @foldlMaybeErrs@ works along a list, carrying an accumulator; it
212 applies the given function to the accumulator and the next list item,
213 accumulating any errors that occur.
216 foldlMaybeErrs :: (acc -> input -> MaybeErr acc err)
219 -> MaybeErr acc [err]
221 foldlMaybeErrs k accum ins = do_it [] accum ins
223 do_it [] acc [] = Succeeded acc
224 do_it errs acc [] = Failed errs
225 do_it errs acc (v:vs) = case (k acc v) of
226 Succeeded acc' -> do_it errs acc' vs
227 Failed err -> do_it (err:errs) acc vs