Major Overhaul of Pattern Match Checking (Fixes #595)
[ghc.git] / compiler / utils / MonadUtils.hs
1
2 -- | Utilities related to Monad and Applicative classes
3 -- Mostly for backwards compatability.
4
5 module MonadUtils
6 ( Applicative(..)
7 , (<$>)
8
9 , MonadFix(..)
10 , MonadIO(..)
11
12 , liftIO1, liftIO2, liftIO3, liftIO4
13
14 , zipWith3M, zipWith3M_, zipWithAndUnzipM
15 , mapAndUnzipM, mapAndUnzip3M, mapAndUnzip4M
16 , mapAccumLM
17 , mapSndM
18 , concatMapM
19 , mapMaybeM
20 , fmapMaybeM, fmapEitherM
21 , anyM, allM, orM
22 , foldlM, foldlM_, foldrM
23 , maybeMapM
24 , whenM
25 ) where
26
27 -------------------------------------------------------------------------------
28 -- Imports
29 -------------------------------------------------------------------------------
30
31 import Maybes
32
33 import Control.Applicative
34 import Control.Monad
35 import Control.Monad.Fix
36 import Control.Monad.IO.Class
37 import Prelude -- avoid redundant import warning due to AMP
38
39 -------------------------------------------------------------------------------
40 -- Lift combinators
41 -- These are used throughout the compiler
42 -------------------------------------------------------------------------------
43
44 -- | Lift an 'IO' operation with 1 argument into another monad
45 liftIO1 :: MonadIO m => (a -> IO b) -> a -> m b
46 liftIO1 = (.) liftIO
47
48 -- | Lift an 'IO' operation with 2 arguments into another monad
49 liftIO2 :: MonadIO m => (a -> b -> IO c) -> a -> b -> m c
50 liftIO2 = ((.).(.)) liftIO
51
52 -- | Lift an 'IO' operation with 3 arguments into another monad
53 liftIO3 :: MonadIO m => (a -> b -> c -> IO d) -> a -> b -> c -> m d
54 liftIO3 = ((.).((.).(.))) liftIO
55
56 -- | Lift an 'IO' operation with 4 arguments into another monad
57 liftIO4 :: MonadIO m => (a -> b -> c -> d -> IO e) -> a -> b -> c -> d -> m e
58 liftIO4 = (((.).(.)).((.).(.))) liftIO
59
60 -------------------------------------------------------------------------------
61 -- Common functions
62 -- These are used throughout the compiler
63 -------------------------------------------------------------------------------
64
65 zipWith3M :: Monad m => (a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
66 zipWith3M _ [] _ _ = return []
67 zipWith3M _ _ [] _ = return []
68 zipWith3M _ _ _ [] = return []
69 zipWith3M f (x:xs) (y:ys) (z:zs)
70 = do { r <- f x y z
71 ; rs <- zipWith3M f xs ys zs
72 ; return $ r:rs
73 }
74
75 zipWith3M_ :: Monad m => (a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m ()
76 zipWith3M_ f as bs cs = do { _ <- zipWith3M f as bs cs
77 ; return () }
78
79 zipWithAndUnzipM :: Monad m
80 => (a -> b -> m (c, d)) -> [a] -> [b] -> m ([c], [d])
81 {-# INLINE zipWithAndUnzipM #-}
82 -- See Note [flatten_many performance] in TcFlatten for why this
83 -- pragma is essential.
84 zipWithAndUnzipM f (x:xs) (y:ys)
85 = do { (c, d) <- f x y
86 ; (cs, ds) <- zipWithAndUnzipM f xs ys
87 ; return (c:cs, d:ds) }
88 zipWithAndUnzipM _ _ _ = return ([], [])
89
90 -- | mapAndUnzipM for triples
91 mapAndUnzip3M :: Monad m => (a -> m (b,c,d)) -> [a] -> m ([b],[c],[d])
92 mapAndUnzip3M _ [] = return ([],[],[])
93 mapAndUnzip3M f (x:xs) = do
94 (r1, r2, r3) <- f x
95 (rs1, rs2, rs3) <- mapAndUnzip3M f xs
96 return (r1:rs1, r2:rs2, r3:rs3)
97
98 mapAndUnzip4M :: Monad m => (a -> m (b,c,d,e)) -> [a] -> m ([b],[c],[d],[e])
99 mapAndUnzip4M _ [] = return ([],[],[],[])
100 mapAndUnzip4M f (x:xs) = do
101 (r1, r2, r3, r4) <- f x
102 (rs1, rs2, rs3, rs4) <- mapAndUnzip4M f xs
103 return (r1:rs1, r2:rs2, r3:rs3, r4:rs4)
104
105 -- | Monadic version of mapAccumL
106 mapAccumLM :: Monad m
107 => (acc -> x -> m (acc, y)) -- ^ combining funcction
108 -> acc -- ^ initial state
109 -> [x] -- ^ inputs
110 -> m (acc, [y]) -- ^ final state, outputs
111 mapAccumLM _ s [] = return (s, [])
112 mapAccumLM f s (x:xs) = do
113 (s1, x') <- f s x
114 (s2, xs') <- mapAccumLM f s1 xs
115 return (s2, x' : xs')
116
117 -- | Monadic version of mapSnd
118 mapSndM :: Monad m => (b -> m c) -> [(a,b)] -> m [(a,c)]
119 mapSndM _ [] = return []
120 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
121
122 -- | Monadic version of concatMap
123 concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
124 concatMapM f xs = liftM concat (mapM f xs)
125
126 -- | Monadic version of mapMaybe
127 mapMaybeM :: (Monad m) => (a -> m (Maybe b)) -> [a] -> m [b]
128 mapMaybeM f = liftM catMaybes . mapM f
129
130 -- | Monadic version of fmap
131 fmapMaybeM :: (Monad m) => (a -> m b) -> Maybe a -> m (Maybe b)
132 fmapMaybeM _ Nothing = return Nothing
133 fmapMaybeM f (Just x) = f x >>= (return . Just)
134
135 -- | Monadic version of fmap
136 fmapEitherM :: Monad m => (a -> m b) -> (c -> m d) -> Either a c -> m (Either b d)
137 fmapEitherM fl _ (Left a) = fl a >>= (return . Left)
138 fmapEitherM _ fr (Right b) = fr b >>= (return . Right)
139
140 -- | Monadic version of 'any', aborts the computation at the first @True@ value
141 anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
142 anyM _ [] = return False
143 anyM f (x:xs) = do b <- f x
144 if b then return True
145 else anyM f xs
146
147 -- | Monad version of 'all', aborts the computation at the first @False@ value
148 allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
149 allM _ [] = return True
150 allM f (b:bs) = (f b) >>= (\bv -> if bv then allM f bs else return False)
151
152 -- | Monadic version of or
153 orM :: Monad m => m Bool -> m Bool -> m Bool
154 orM m1 m2 = m1 >>= \x -> if x then return True else m2
155
156 -- | Monadic version of foldl
157 foldlM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
158 foldlM = foldM
159
160 -- | Monadic version of foldl that discards its result
161 foldlM_ :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m ()
162 foldlM_ = foldM_
163
164 -- | Monadic version of foldr
165 foldrM :: (Monad m) => (b -> a -> m a) -> a -> [b] -> m a
166 foldrM _ z [] = return z
167 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }
168
169 -- | Monadic version of fmap specialised for Maybe
170 maybeMapM :: Monad m => (a -> m b) -> (Maybe a -> m (Maybe b))
171 maybeMapM _ Nothing = return Nothing
172 maybeMapM m (Just x) = liftM Just $ m x
173
174 -- | Monadic version of @when@, taking the condition in the monad
175 whenM :: Monad m => m Bool -> m () -> m ()
176 whenM mb thing = do { b <- mb
177 ; when b thing }