%
-% (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
+% (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
%
\section[Maybes]{The `Maybe' types and associated utility functions}
#endif
module Maybes (
- Maybe(..), MaybeErr(..),
+-- Maybe(..), -- no, it's in 1.3
+ MaybeErr(..),
- allMaybes, -- GHCI only
- assocMaybe,
+ allMaybes,
catMaybes,
- failMaB,
- failMaybe,
firstJust,
- mapMaybe, -- GHCI only
+ expectJust,
maybeToBool,
- mkLookupFun,
+
+ assocMaybe,
+ mkLookupFun, mkLookupFunDef,
+
+ failMaB,
+ failMaybe,
+ seqMaybe,
returnMaB,
- returnMaybe, -- GHCI only
- thenMaB,
- thenMaybe -- GHCI only
+ returnMaybe,
+ thenMaB
#if ! defined(COMPILING_GHC)
, findJust
) where
#if defined(COMPILING_GHC)
-import AbsUniType
-import Id
-import IdInfo
-import Name
-import Outputable
-#if USE_ATTACK_PRAGMAS
-import Util
-#endif
+
+CHK_Ubiq() -- debugging consistency check
+
+import Unique (Unique) -- only for specialising
+
#endif
\end{code}
%************************************************************************
\begin{code}
-#if __HASKELL1__ < 3
-data Maybe a
- = Nothing
- | Just a
-#endif
-\end{code}
-
-\begin{code}
maybeToBool :: Maybe a -> Bool
maybeToBool Nothing = False
maybeToBool (Just x) = True
\end{code}
-@catMaybes@ takes a list of @Maybe@s and returns a list of
+@catMaybes@ takes a list of @Maybe@s and returns a list of
the contents of all the @Just@s in it. @allMaybes@ collects
a list of @Justs@ into a single @Just@, returning @Nothing@ if there
are any @Nothings@.
b -> b
\end{code}
+\begin{code}
+expectJust :: String -> Maybe a -> a
+{-# INLINE expectJust #-}
+expectJust err (Just x) = x
+expectJust err Nothing = error ("expectJust " ++ err)
+\end{code}
+
+The Maybe monad
+~~~~~~~~~~~~~~~
+\begin{code}
+seqMaybe :: Maybe a -> Maybe a -> Maybe a
+seqMaybe (Just x) _ = Just x
+seqMaybe Nothing my = my
+
+returnMaybe :: a -> Maybe a
+returnMaybe = Just
+
+failMaybe :: Maybe a
+failMaybe = Nothing
+\end{code}
+
+Lookup functions
+~~~~~~~~~~~~~~~~
+
@assocMaybe@ looks up in an assocation list, returning
@Nothing@ if it fails.
#if defined(COMPILING_GHC)
{-# SPECIALIZE assocMaybe
- :: [(String, b)] -> String -> Maybe b,
- [(Id, b)] -> Id -> Maybe b,
- [(Class, b)] -> Class -> Maybe b,
- [(Int, b)] -> Int -> Maybe b,
- [(Name, b)] -> Name -> Maybe b,
- [(TyVar, b)] -> TyVar -> Maybe b,
- [(TyVarTemplate, b)] -> TyVarTemplate -> Maybe b
+ :: [(FAST_STRING, b)] -> FAST_STRING -> Maybe b
+ , [(Int, b)] -> Int -> Maybe b
+ , [(Unique, b)] -> Unique -> Maybe b
+ , [(RdrName, b)] -> RdrName -> Maybe b
#-}
#endif
\end{code}
-@mkLookupFun alist s@ is a function which looks up
-@s@ in the association list @alist@, returning a Maybe type.
+@mkLookupFun eq alist@ is a function which looks up
+its argument in the association list @alist@, returning a Maybe type.
+@mkLookupFunDef@ is similar except that it is given a value to return
+on failure.
\begin{code}
mkLookupFun :: (key -> key -> Bool) -- Equality predicate
= case [a | (s',a) <- alist, s' `eq` s] of
[] -> Nothing
(a:_) -> Just a
-\end{code}
-\begin{code}
-#if __HASKELL1__ < 3
-thenMaybe :: Maybe a -> (a -> Maybe b) -> Maybe b
-m `thenMaybe` k = case m of
- Nothing -> Nothing
- Just a -> k a
-#endif
-returnMaybe :: a -> Maybe a
-returnMaybe = Just
+mkLookupFunDef :: (key -> key -> Bool) -- Equality predicate
+ -> [(key,val)] -- The assoc list
+ -> val -- Value to return on failure
+ -> key -- The key
+ -> val -- The corresponding value
-failMaybe :: Maybe a
-failMaybe = Nothing
-
-mapMaybe :: (a -> Maybe b) -> [a] -> Maybe [b]
-mapMaybe f [] = returnMaybe []
-mapMaybe f (x:xs) = f x `thenMaybe` (\ x' ->
- mapMaybe f xs `thenMaybe` (\ xs' ->
- returnMaybe (x':xs') ))
+mkLookupFunDef eq alist deflt s
+ = case [a | (s',a) <- alist, s' `eq` s] of
+ [] -> deflt
+ (a:_) -> a
\end{code}
%************************************************************************
\begin{code}
listMaybeErrs :: [MaybeErr val err] -> MaybeErr [val] [err]
listMaybeErrs
- = foldr combine (Succeeded [])
+ = foldr combine (Succeeded [])
where
combine (Succeeded v) (Succeeded vs) = Succeeded (v:vs)
combine (Failed err) (Succeeded _) = Failed [err]