2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Maybes]{The `Maybe' types and associated utility functions}
8 Maybe2(..), Maybe3(..),
19 mkLookupFun, mkLookupFunDef,
30 #include "HsVersions.h"
32 import Maybe( catMaybes, mapMaybe )
39 %************************************************************************
41 \subsection[Maybe2,3 types]{The @Maybe2@ and @Maybe3@ types}
43 %************************************************************************
46 data Maybe2 a b = Just2 a b | Nothing2 deriving (Eq,Show)
47 data Maybe3 a b c = Just3 a b c | Nothing3 deriving (Eq,Show)
51 %************************************************************************
53 \subsection[Maybe type]{The @Maybe@ type}
55 %************************************************************************
58 maybeToBool :: Maybe a -> Bool
59 maybeToBool Nothing = False
60 maybeToBool (Just x) = True
63 @catMaybes@ takes a list of @Maybe@s and returns a list of
64 the contents of all the @Just@s in it. @allMaybes@ collects
65 a list of @Justs@ into a single @Just@, returning @Nothing@ if there
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)
78 @firstJust@ takes a list of @Maybes@ and returns the
79 first @Just@ if there is one, or @Nothing@ otherwise.
82 firstJust :: [Maybe a] -> Maybe a
83 firstJust [] = Nothing
84 firstJust (Just x : ms) = Just x
85 firstJust (Nothing : ms) = firstJust ms
89 findJust :: (a -> Maybe b) -> [a] -> Maybe b
90 findJust f [] = Nothing
91 findJust f (a:as) = case f a of
92 Nothing -> findJust f as
97 expectJust :: String -> Maybe a -> a
98 {-# INLINE expectJust #-}
99 expectJust err (Just x) = x
100 expectJust err Nothing = error ("expectJust " ++ err)
106 seqMaybe :: Maybe a -> Maybe a -> Maybe a
107 seqMaybe (Just x) _ = Just x
108 seqMaybe Nothing my = my
110 returnMaybe :: a -> Maybe a
116 orElse :: Maybe a -> a -> a
117 (Just x) `orElse` y = x
118 Nothing `orElse` y = y
124 @assocMaybe@ looks up in an assocation list, returning
125 @Nothing@ if it fails.
128 assocMaybe :: (Eq a) => [(a,b)] -> a -> Maybe b
134 lookup ((tv,ty):rest) = if key == tv then Just ty else lookup rest
137 @mkLookupFun eq alist@ is a function which looks up
138 its argument in the association list @alist@, returning a Maybe type.
139 @mkLookupFunDef@ is similar except that it is given a value to return
143 mkLookupFun :: (key -> key -> Bool) -- Equality predicate
144 -> [(key,val)] -- The assoc list
146 -> Maybe val -- The corresponding value
148 mkLookupFun eq alist s
149 = case [a | (s',a) <- alist, s' `eq` s] of
153 mkLookupFunDef :: (key -> key -> Bool) -- Equality predicate
154 -> [(key,val)] -- The assoc list
155 -> val -- Value to return on failure
157 -> val -- The corresponding value
159 mkLookupFunDef eq alist deflt s
160 = case [a | (s',a) <- alist, s' `eq` s] of
165 %************************************************************************
167 \subsection[MaybeErr type]{The @MaybeErr@ type}
169 %************************************************************************
172 data MaybeErr val err = Succeeded val | Failed err
176 thenMaB :: MaybeErr val1 err -> (val1 -> MaybeErr val2 err) -> MaybeErr val2 err
182 returnMaB :: val -> MaybeErr val err
183 returnMaB v = Succeeded v
185 failMaB :: err -> MaybeErr val err