Removed unused Maybe functions, use the standard Maybe monad instead
[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         orElse,
14         mapCatMaybes,
15         allMaybes,
16         firstJust,
17         expectJust,
18         maybeToBool,
19
20     ) where
21
22 import Data.Maybe
23
24 infixr 4 `orElse`
25 \end{code}
26
27 %************************************************************************
28 %*                                                                      *
29 \subsection[Maybe type]{The @Maybe@ type}
30 %*                                                                      *
31 %************************************************************************
32
33 \begin{code}
34 maybeToBool :: Maybe a -> Bool
35 maybeToBool Nothing  = False
36 maybeToBool (Just _) = True
37 \end{code}
38
39 @catMaybes@ takes a list of @Maybe@s and returns a list of
40 the contents of all the @Just@s in it. @allMaybes@ collects
41 a list of @Justs@ into a single @Just@, returning @Nothing@ if there
42 are any @Nothings@.
43
44 \begin{code}
45 allMaybes :: [Maybe a] -> Maybe [a]
46 allMaybes [] = Just []
47 allMaybes (Nothing : _)  = Nothing
48 allMaybes (Just x  : ms) = case allMaybes ms of
49                            Nothing -> Nothing
50                            Just xs -> Just (x:xs)
51
52 \end{code}
53
54 @firstJust@ takes a list of @Maybes@ and returns the
55 first @Just@ if there is one, or @Nothing@ otherwise.
56
57 \begin{code}
58 firstJust :: [Maybe a] -> Maybe a
59 firstJust [] = Nothing
60 firstJust (Just x  : _)  = Just x
61 firstJust (Nothing : ms) = firstJust ms
62 \end{code}
63
64 \begin{code}
65 expectJust :: String -> Maybe a -> a
66 {-# INLINE expectJust #-}
67 expectJust _   (Just x) = x
68 expectJust err Nothing  = error ("expectJust " ++ err)
69 \end{code}
70
71 \begin{code}
72 mapCatMaybes :: (a -> Maybe b) -> [a] -> [b]
73 mapCatMaybes _ [] = []
74 mapCatMaybes f (x:xs) = case f x of
75                         Just y  -> y : mapCatMaybes f xs
76                         Nothing -> mapCatMaybes f xs
77 \end{code}
78
79 \begin{code}
80 orElse :: Maybe a -> a -> a
81 (Just x) `orElse` _ = x
82 Nothing  `orElse` y = y
83 \end{code}
84
85
86 %************************************************************************
87 %*                                                                      *
88 \subsection[MaybeErr type]{The @MaybeErr@ type}
89 %*                                                                      *
90 %************************************************************************
91
92 \begin{code}
93 data MaybeErr err val = Succeeded val | Failed err
94
95 instance Monad (MaybeErr err) where
96   return v = Succeeded v
97   Succeeded v >>= k = k v
98   Failed e    >>= _ = Failed e
99
100 isSuccess :: MaybeErr err val -> Bool
101 isSuccess (Succeeded {}) = True
102 isSuccess (Failed {})    = False
103
104 failME :: err -> MaybeErr err val
105 failME e = Failed e
106 \end{code}