Reorganisation of the source tree
[ghc-hetmet.git] / compiler / utils / Maybes.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[Maybes]{The `Maybe' types and associated utility functions}
5
6 \begin{code}
7 module Maybes (
8         module Maybe,           -- Re-export all of Maybe
9
10         MaybeErr(..),   -- Instance of Monad
11         failME,
12
13         orElse, 
14         mapCatMaybes,
15         allMaybes,
16         firstJust,
17         expectJust,
18         maybeToBool,
19
20         thenMaybe, seqMaybe, returnMaybe, failMaybe
21     ) where
22
23 #include "HsVersions.h"
24
25 import Maybe
26
27
28 infixr 4 `orElse`
29 \end{code}
30
31 %************************************************************************
32 %*                                                                      *
33 \subsection[Maybe type]{The @Maybe@ type}
34 %*                                                                      *
35 %************************************************************************
36
37 \begin{code}
38 maybeToBool :: Maybe a -> Bool
39 maybeToBool Nothing  = False
40 maybeToBool (Just x) = True
41 \end{code}
42
43 @catMaybes@ takes a list of @Maybe@s and returns a list of
44 the contents of all the @Just@s in it.  @allMaybes@ collects
45 a list of @Justs@ into a single @Just@, returning @Nothing@ if there
46 are any @Nothings@.
47
48 \begin{code}
49 allMaybes :: [Maybe a] -> Maybe [a]
50 allMaybes [] = Just []
51 allMaybes (Nothing : ms) = Nothing
52 allMaybes (Just x  : ms) = case (allMaybes ms) of
53                              Nothing -> Nothing
54                              Just xs -> Just (x:xs)
55
56 \end{code}
57
58 @firstJust@ takes a list of @Maybes@ and returns the
59 first @Just@ if there is one, or @Nothing@ otherwise.
60
61 \begin{code}
62 firstJust :: [Maybe a] -> Maybe a
63 firstJust [] = Nothing
64 firstJust (Just x  : ms) = Just x
65 firstJust (Nothing : ms) = firstJust ms
66 \end{code}
67
68 \begin{code}
69 expectJust :: String -> Maybe a -> a
70 {-# INLINE expectJust #-}
71 expectJust err (Just x) = x
72 expectJust err Nothing  = error ("expectJust " ++ err)
73 \end{code}
74
75 \begin{code}
76 mapCatMaybes :: (a -> Maybe b) -> [a] -> [b]
77 mapCatMaybes f [] = []
78 mapCatMaybes f (x:xs) = case f x of
79                           Just y  -> y : mapCatMaybes f xs
80                           Nothing -> mapCatMaybes f xs
81 \end{code}
82
83 The Maybe monad
84 ~~~~~~~~~~~~~~~
85 \begin{code}
86 seqMaybe :: Maybe a -> Maybe a -> Maybe a
87 seqMaybe (Just x) _  = Just x
88 seqMaybe Nothing  my = my
89
90 thenMaybe :: Maybe a -> (a -> Maybe b) -> Maybe b
91 thenMaybe ma mb = case ma of
92                     Just x  -> mb x
93                     Nothing -> Nothing
94
95 returnMaybe :: a -> Maybe a
96 returnMaybe = Just
97
98 failMaybe :: Maybe a
99 failMaybe = Nothing
100
101 orElse :: Maybe a -> a -> a
102 (Just x) `orElse` y = x
103 Nothing  `orElse` y = y
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    >>= k = Failed e
120
121 failME :: err -> MaybeErr err val
122 failME e = Failed e
123 \end{code}