[project @ 1996-07-25 20:43:49 by partain]
[ghc-hetmet.git] / ghc / compiler / utils / Maybes.lhs
index 66c1279..1f17679 100644 (file)
@@ -1,5 +1,5 @@
 %
-% (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}
 
@@ -9,23 +9,27 @@
 #endif
 
 module Maybes (
-       Maybe(..), MaybeErr(..),
+--     Maybe(..), -- no, it's in 1.3
+       MaybeErr(..),
+
+       allMaybes,
+       firstJust,
+       expectJust,
+       maybeToBool,
 
-       allMaybes,      -- GHCI only
        assocMaybe,
-       catMaybes,
+       mkLookupFun, mkLookupFunDef,
+
        failMaB,
        failMaybe,
-       firstJust,
-       mapMaybe,       -- GHCI only
-       maybeToBool,
-       mkLookupFun,
+       seqMaybe,
        returnMaB,
-       returnMaybe,    -- GHCI only
-       thenMaB,
-       thenMaybe       -- GHCI only
+       returnMaybe,
+       thenMaB
 
-#if ! defined(COMPILING_GHC)
+#if defined(COMPILING_GHC)
+       , catMaybes
+#else
        , findJust
        , foldlMaybeErrs
        , listMaybeErrs
@@ -33,14 +37,13 @@ module Maybes (
     ) 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
+
+#else
+import Maybe -- renamer will tell us if there are any conflicts
 #endif
 \end{code}
 
@@ -52,29 +55,23 @@ import Util
 %************************************************************************
 
 \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@.
 
 \begin{code}
+#ifdef COMPILING_GHC
 catMaybes :: [Maybe a] -> [a]
 catMaybes []               = []
 catMaybes (Nothing : xs)   = catMaybes xs
 catMaybes (Just x : xs)           = (x : catMaybes xs)
+#endif
 
 allMaybes :: [Maybe a] -> Maybe [a]
 allMaybes [] = Just []
@@ -102,6 +99,30 @@ findJust f (a:as) = case f a of
                      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.
 
@@ -116,19 +137,18 @@ assocMaybe alist key
 
 #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
@@ -140,26 +160,17 @@ mkLookupFun eq alist s
   = 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}
 
 %************************************************************************
@@ -194,7 +205,7 @@ a @Succeeded@ of a list of their values.  If any fail, it returns a
 \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]