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 CHK_Ubiq() -- debugging consistency check
41 import Unique (Unique) -- only for specialising
45 %************************************************************************
47 \subsection[Maybe type]{The @Maybe@ type}
49 %************************************************************************
52 maybeToBool :: Maybe a -> Bool
53 maybeToBool Nothing = False
54 maybeToBool (Just x) = True
57 @catMaybes@ takes a list of @Maybe@s and returns a list of
58 the contents of all the @Just@s in it. @allMaybes@ collects
59 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)
70 allMaybes :: [Maybe a] -> Maybe [a]
71 allMaybes [] = Just []
72 allMaybes (Nothing : ms) = Nothing
73 allMaybes (Just x : ms) = case (allMaybes ms) of
75 Just xs -> Just (x:xs)
77 mapMaybe :: (a -> Maybe b) -> [a] -> [b]
79 mapMaybe f (x:xs) = case f x of
80 Just y -> y : mapMaybe f xs
81 Nothing -> mapMaybe f xs
84 @firstJust@ takes a list of @Maybes@ and returns the
85 first @Just@ if there is one, or @Nothing@ otherwise.
88 firstJust :: [Maybe a] -> Maybe a
89 firstJust [] = Nothing
90 firstJust (Just x : ms) = Just x
91 firstJust (Nothing : ms) = firstJust ms
95 findJust :: (a -> Maybe b) -> [a] -> Maybe b
96 findJust f [] = Nothing
97 findJust f (a:as) = case f a of
98 Nothing -> findJust f as
103 expectJust :: String -> Maybe a -> a
104 {-# INLINE expectJust #-}
105 expectJust err (Just x) = x
106 expectJust err Nothing = error ("expectJust " ++ err)
112 seqMaybe :: Maybe a -> Maybe a -> Maybe a
113 seqMaybe (Just x) _ = Just x
114 seqMaybe Nothing my = my
116 returnMaybe :: a -> Maybe a
126 @assocMaybe@ looks up in an assocation list, returning
127 @Nothing@ if it fails.
130 assocMaybe :: (Eq a) => [(a,b)] -> a -> Maybe b
136 lookup ((tv,ty):rest) = if key == tv then Just ty else lookup rest
138 #if defined(COMPILING_GHC)
139 {-# SPECIALIZE assocMaybe
140 :: [(FAST_STRING, b)] -> FAST_STRING -> Maybe b
141 , [(Int, b)] -> Int -> Maybe b
142 , [(Unique, b)] -> Unique -> Maybe b
147 @mkLookupFun eq alist@ is a function which looks up
148 its argument in the association list @alist@, returning a Maybe type.
149 @mkLookupFunDef@ is similar except that it is given a value to return
153 mkLookupFun :: (key -> key -> Bool) -- Equality predicate
154 -> [(key,val)] -- The assoc list
156 -> Maybe val -- The corresponding value
158 mkLookupFun eq alist s
159 = case [a | (s',a) <- alist, s' `eq` s] of
163 mkLookupFunDef :: (key -> key -> Bool) -- Equality predicate
164 -> [(key,val)] -- The assoc list
165 -> val -- Value to return on failure
167 -> val -- The corresponding value
169 mkLookupFunDef eq alist deflt s
170 = case [a | (s',a) <- alist, s' `eq` s] of
175 %************************************************************************
177 \subsection[MaybeErr type]{The @MaybeErr@ type}
179 %************************************************************************
182 data MaybeErr val err = Succeeded val | Failed err
186 thenMaB :: MaybeErr val1 err -> (val1 -> MaybeErr val2 err) -> MaybeErr val2 err
192 returnMaB :: val -> MaybeErr val err
193 returnMaB v = Succeeded v
195 failMaB :: err -> MaybeErr val err
200 @listMaybeErrs@ takes a list of @MaybeErrs@ and, if they all succeed, returns
201 a @Succeeded@ of a list of their values. If any fail, it returns a
202 @Failed@ of the list of all the errors in the list.
205 listMaybeErrs :: [MaybeErr val err] -> MaybeErr [val] [err]
207 = foldr combine (Succeeded [])
209 combine (Succeeded v) (Succeeded vs) = Succeeded (v:vs)
210 combine (Failed err) (Succeeded _) = Failed [err]
211 combine (Succeeded v) (Failed errs) = Failed errs
212 combine (Failed err) (Failed errs) = Failed (err:errs)
215 @foldlMaybeErrs@ works along a list, carrying an accumulator; it
216 applies the given function to the accumulator and the next list item,
217 accumulating any errors that occur.
220 foldlMaybeErrs :: (acc -> input -> MaybeErr acc err)
223 -> MaybeErr acc [err]
225 foldlMaybeErrs k accum ins = do_it [] accum ins
227 do_it [] acc [] = Succeeded acc
228 do_it errs acc [] = Failed errs
229 do_it errs acc (v:vs) = case (k acc v) of
230 Succeeded acc' -> do_it errs acc' vs
231 Failed err -> do_it (err:errs) acc vs