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 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
45 IMPORT_DELOOPER( SpecLoop ) -- Specialisation
47 import {-# SOURCE #-} Name
50 import Unique (Unique) -- only for specialising
53 import Maybe -- renamer will tell us if there are any conflicts
58 %************************************************************************
60 \subsection[Maybe type]{The @Maybe@ type}
62 %************************************************************************
65 maybeToBool :: Maybe a -> Bool
66 maybeToBool Nothing = False
67 maybeToBool (Just x) = True
70 @catMaybes@ takes a list of @Maybe@s and returns a list of
71 the contents of all the @Just@s in it. @allMaybes@ collects
72 a list of @Justs@ into a single @Just@, returning @Nothing@ if there
77 catMaybes :: [Maybe a] -> [a]
79 catMaybes (Nothing : xs) = catMaybes xs
80 catMaybes (Just x : xs) = (x : catMaybes xs)
83 allMaybes :: [Maybe a] -> Maybe [a]
84 allMaybes [] = Just []
85 allMaybes (Nothing : ms) = Nothing
86 allMaybes (Just x : ms) = case (allMaybes ms) of
88 Just xs -> Just (x:xs)
90 mapMaybe :: (a -> Maybe b) -> [a] -> [b]
92 mapMaybe f (x:xs) = case f x of
93 Just y -> y : mapMaybe f xs
94 Nothing -> mapMaybe f xs
97 @firstJust@ takes a list of @Maybes@ and returns the
98 first @Just@ if there is one, or @Nothing@ otherwise.
101 firstJust :: [Maybe a] -> Maybe a
102 firstJust [] = Nothing
103 firstJust (Just x : ms) = Just x
104 firstJust (Nothing : ms) = firstJust ms
108 findJust :: (a -> Maybe b) -> [a] -> Maybe b
109 findJust f [] = Nothing
110 findJust f (a:as) = case f a of
111 Nothing -> findJust f as
116 expectJust :: String -> Maybe a -> a
117 {-# INLINE expectJust #-}
118 expectJust err (Just x) = x
119 expectJust err Nothing = error ("expectJust " ++ err)
125 seqMaybe :: Maybe a -> Maybe a -> Maybe a
126 seqMaybe (Just x) _ = Just x
127 seqMaybe Nothing my = my
129 returnMaybe :: a -> Maybe a
139 @assocMaybe@ looks up in an assocation list, returning
140 @Nothing@ if it fails.
143 assocMaybe :: (Eq a) => [(a,b)] -> a -> Maybe b
149 lookup ((tv,ty):rest) = if key == tv then Just ty else lookup rest
151 #if defined(COMPILING_GHC)
152 {-# SPECIALIZE assocMaybe
153 :: [(FAST_STRING, b)] -> FAST_STRING -> Maybe b
154 , [(Int, b)] -> Int -> Maybe b
155 , [(Unique, b)] -> Unique -> Maybe b
160 @mkLookupFun eq alist@ is a function which looks up
161 its argument in the association list @alist@, returning a Maybe type.
162 @mkLookupFunDef@ is similar except that it is given a value to return
166 mkLookupFun :: (key -> key -> Bool) -- Equality predicate
167 -> [(key,val)] -- The assoc list
169 -> Maybe val -- The corresponding value
171 mkLookupFun eq alist s
172 = case [a | (s',a) <- alist, s' `eq` s] of
176 mkLookupFunDef :: (key -> key -> Bool) -- Equality predicate
177 -> [(key,val)] -- The assoc list
178 -> val -- Value to return on failure
180 -> val -- The corresponding value
182 mkLookupFunDef eq alist deflt s
183 = case [a | (s',a) <- alist, s' `eq` s] of
188 %************************************************************************
190 \subsection[MaybeErr type]{The @MaybeErr@ type}
192 %************************************************************************
195 data MaybeErr val err = Succeeded val | Failed err
199 thenMaB :: MaybeErr val1 err -> (val1 -> MaybeErr val2 err) -> MaybeErr val2 err
205 returnMaB :: val -> MaybeErr val err
206 returnMaB v = Succeeded v
208 failMaB :: err -> MaybeErr val err
213 @listMaybeErrs@ takes a list of @MaybeErrs@ and, if they all succeed, returns
214 a @Succeeded@ of a list of their values. If any fail, it returns a
215 @Failed@ of the list of all the errors in the list.
218 listMaybeErrs :: [MaybeErr val err] -> MaybeErr [val] [err]
220 = foldr combine (Succeeded [])
222 combine (Succeeded v) (Succeeded vs) = Succeeded (v:vs)
223 combine (Failed err) (Succeeded _) = Failed [err]
224 combine (Succeeded v) (Failed errs) = Failed errs
225 combine (Failed err) (Failed errs) = Failed (err:errs)
228 @foldlMaybeErrs@ works along a list, carrying an accumulator; it
229 applies the given function to the accumulator and the next list item,
230 accumulating any errors that occur.
233 foldlMaybeErrs :: (acc -> input -> MaybeErr acc err)
236 -> MaybeErr acc [err]
238 foldlMaybeErrs k accum ins = do_it [] accum ins
240 do_it [] acc [] = Succeeded acc
241 do_it errs acc [] = Failed errs
242 do_it errs acc (v:vs) = case (k acc v) of
243 Succeeded acc' -> do_it errs acc' vs
244 Failed err -> do_it (err:errs) acc vs