Remove uses of Data.Traversable to fix stage1 on pre ghc-6.6 systems
[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,      -- Re-export all of 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         thenMaybe, seqMaybe, returnMaybe, failMaybe, fmapMMaybe
21     ) where
22
23 #include "HsVersions.h"
24
25 import Data.Maybe
26
27 infixr 4 `orElse`
28 \end{code}
29
30 %************************************************************************
31 %*                                                                      *
32 \subsection[Maybe type]{The @Maybe@ type}
33 %*                                                                      *
34 %************************************************************************
35
36 \begin{code}
37 maybeToBool :: Maybe a -> Bool
38 maybeToBool Nothing  = False
39 maybeToBool (Just x) = True
40 \end{code}
41
42 @catMaybes@ takes a list of @Maybe@s and returns a list of
43 the contents of all the @Just@s in it.  @allMaybes@ collects
44 a list of @Justs@ into a single @Just@, returning @Nothing@ if there
45 are any @Nothings@.
46
47 \begin{code}
48 allMaybes :: [Maybe a] -> Maybe [a]
49 allMaybes [] = Just []
50 allMaybes (Nothing : ms) = Nothing
51 allMaybes (Just x  : ms) = case (allMaybes ms) of
52                              Nothing -> Nothing
53                              Just xs -> Just (x:xs)
54
55 \end{code}
56
57 @firstJust@ takes a list of @Maybes@ and returns the
58 first @Just@ if there is one, or @Nothing@ otherwise.
59
60 \begin{code}
61 firstJust :: [Maybe a] -> Maybe a
62 firstJust [] = Nothing
63 firstJust (Just x  : ms) = Just x
64 firstJust (Nothing : ms) = firstJust ms
65 \end{code}
66
67 \begin{code}
68 expectJust :: String -> Maybe a -> a
69 {-# INLINE expectJust #-}
70 expectJust err (Just x) = x
71 expectJust err Nothing  = error ("expectJust " ++ err)
72 \end{code}
73
74 \begin{code}
75 mapCatMaybes :: (a -> Maybe b) -> [a] -> [b]
76 mapCatMaybes f [] = []
77 mapCatMaybes f (x:xs) = case f x of
78                           Just y  -> y : mapCatMaybes f xs
79                           Nothing -> mapCatMaybes f xs
80 \end{code}
81
82 The Maybe monad
83 ~~~~~~~~~~~~~~~
84 \begin{code}
85 seqMaybe :: Maybe a -> Maybe a -> Maybe a
86 seqMaybe (Just x) _  = Just x
87 seqMaybe Nothing  my = my
88
89 thenMaybe :: Maybe a -> (a -> Maybe b) -> Maybe b
90 thenMaybe ma mb = case ma of
91                     Just x  -> mb x
92                     Nothing -> Nothing
93
94 returnMaybe :: a -> Maybe a
95 returnMaybe = Just
96
97 failMaybe :: Maybe a
98 failMaybe = Nothing
99
100 orElse :: Maybe a -> a -> a
101 (Just x) `orElse` y = x
102 Nothing  `orElse` y = y
103
104 fmapMMaybe :: Monad m => (a -> m b) -> Maybe a -> m (Maybe b)
105 fmapMMaybe f Nothing  = return Nothing
106 fmapMMaybe f (Just x) = f x >>= \x' -> return (Just x')
107
108 \end{code}
109
110
111 %************************************************************************
112 %*                                                                      *
113 \subsection[MaybeErr type]{The @MaybeErr@ type}
114 %*                                                                      *
115 %************************************************************************
116
117 \begin{code}
118 data MaybeErr err val = Succeeded val | Failed err
119
120 instance Monad (MaybeErr err) where
121   return v = Succeeded v
122   Succeeded v >>= k = k v
123   Failed e    >>= k = Failed e
124
125 isSuccess :: MaybeErr err val -> Bool
126 isSuccess (Succeeded {}) = True
127 isSuccess (Failed {})    = False
128
129 failME :: err -> MaybeErr err val
130 failME e = Failed e
131 \end{code}