Remove the (very) old strictness analyser
[ghc-hetmet.git] / compiler / utils / Maybes.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5
6 \begin{code}
7 module Maybes (
8         module Data.Maybe,
9
10         MaybeErr(..), -- Instance of Monad
11         failME, isSuccess,
12
13         fmapM_maybe,
14         orElse,
15         mapCatMaybes,
16         allMaybes,
17         firstJust,
18         expectJust,
19         maybeToBool,
20
21         MaybeT(..)
22     ) where
23
24 import Data.Maybe
25
26 infixr 4 `orElse`
27 \end{code}
28
29 %************************************************************************
30 %*                                                                      *
31 \subsection[Maybe type]{The @Maybe@ type}
32 %*                                                                      *
33 %************************************************************************
34
35 \begin{code}
36 maybeToBool :: Maybe a -> Bool
37 maybeToBool Nothing  = False
38 maybeToBool (Just _) = True
39
40 -- | Collects a list of @Justs@ into a single @Just@, returning @Nothing@ if
41 -- there are any @Nothings@.
42 allMaybes :: [Maybe a] -> Maybe [a]
43 allMaybes [] = Just []
44 allMaybes (Nothing : _)  = Nothing
45 allMaybes (Just x  : ms) = case allMaybes ms of
46                            Nothing -> Nothing
47                            Just xs -> Just (x:xs)
48
49 -- | Takes a list of @Maybes@ and returns the first @Just@ if there is one, or
50 -- @Nothing@ otherwise.
51 firstJust :: [Maybe a] -> Maybe a
52 firstJust [] = Nothing
53 firstJust (Just x  : _)  = Just x
54 firstJust (Nothing : ms) = firstJust ms
55 \end{code}
56
57 \begin{code}
58 expectJust :: String -> Maybe a -> a
59 {-# INLINE expectJust #-}
60 expectJust _   (Just x) = x
61 expectJust err Nothing  = error ("expectJust " ++ err)
62 \end{code}
63
64 \begin{code}
65 mapCatMaybes :: (a -> Maybe b) -> [a] -> [b]
66 mapCatMaybes _ [] = []
67 mapCatMaybes f (x:xs) = case f x of
68                         Just y  -> y : mapCatMaybes f xs
69                         Nothing -> mapCatMaybes f xs
70 \end{code}
71
72 \begin{code}
73 orElse :: Maybe a -> a -> a
74 (Just x) `orElse` _ = x
75 Nothing  `orElse` y = y
76 \end{code}
77
78 \begin{code}
79 fmapM_maybe :: Monad m => (a -> m b) -> Maybe a -> m (Maybe b)
80 fmapM_maybe _ Nothing = return Nothing
81 fmapM_maybe f (Just x) = do
82         x' <- f x
83         return $ Just x'
84 \end{code}
85
86 %************************************************************************
87 %*                                                                      *
88 \subsection[MaybeT type]{The @MaybeT@ monad transformer}
89 %*                                                                      *
90 %************************************************************************
91
92 \begin{code}
93
94 newtype MaybeT m a = MaybeT {runMaybeT :: m (Maybe a)}
95
96 instance Functor m => Functor (MaybeT m) where
97   fmap f x = MaybeT $ fmap (fmap f) $ runMaybeT x
98
99 instance Monad m => Monad (MaybeT m) where
100   return = MaybeT . return . Just
101   x >>= f = MaybeT $ runMaybeT x >>= maybe (return Nothing) (runMaybeT . f)
102   fail _ = MaybeT $ return Nothing
103
104 \end{code}
105
106
107 %************************************************************************
108 %*                                                                      *
109 \subsection[MaybeErr type]{The @MaybeErr@ type}
110 %*                                                                      *
111 %************************************************************************
112
113 \begin{code}
114 data MaybeErr err val = Succeeded val | Failed err
115
116 instance Monad (MaybeErr err) where
117   return v = Succeeded v
118   Succeeded v >>= k = k v
119   Failed e    >>= _ = Failed e
120
121 isSuccess :: MaybeErr err val -> Bool
122 isSuccess (Succeeded {}) = True
123 isSuccess (Failed {})    = False
124
125 failME :: err -> MaybeErr err val
126 failME e = Failed e
127 \end{code}