Module header tidyup #2
[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
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 \end{code}
104
105
106 %************************************************************************
107 %*                                                                      *
108 \subsection[MaybeErr type]{The @MaybeErr@ type}
109 %*                                                                      *
110 %************************************************************************
111
112 \begin{code}
113 data MaybeErr err val = Succeeded val | Failed err
114
115 instance Monad (MaybeErr err) where
116   return v = Succeeded v
117   Succeeded v >>= k = k v
118   Failed e    >>= k = Failed e
119
120 isSuccess :: MaybeErr err val -> Bool
121 isSuccess (Succeeded {}) = True
122 isSuccess (Failed {})    = False
123
124 failME :: err -> MaybeErr err val
125 failME e = Failed e
126 \end{code}