2 -- | Utilities related to Monad and Applicative classes
3 -- Mostly for backwards compatability.
6 ( Applicative(..)
7 , (<\$>)
12 , liftIO1, liftIO2, liftIO3, liftIO4
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
27 -------------------------------------------------------------------------------
28 -- Imports
29 -------------------------------------------------------------------------------
31 import Maybes
33 import Control.Applicative
37 import Prelude -- avoid redundant import warning due to AMP
39 -------------------------------------------------------------------------------
40 -- Lift combinators
41 -- These are used throughout the compiler
42 -------------------------------------------------------------------------------
44 -- | Lift an 'IO' operation with 1 argument into another monad
45 liftIO1 :: MonadIO m => (a -> IO b) -> a -> m b
46 liftIO1 = (.) liftIO
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
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
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
60 -------------------------------------------------------------------------------
61 -- Common functions
62 -- These are used throughout the compiler
63 -------------------------------------------------------------------------------
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 }
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 () }
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 ([], [])
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)
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)
105 -- | Monadic version of mapAccumL
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')
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) }
122 -- | Monadic version of concatMap
123 concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
124 concatMapM f xs = liftM concat (mapM f xs)
126 -- | Monadic version of mapMaybe
127 mapMaybeM :: (Monad m) => (a -> m (Maybe b)) -> [a] -> m [b]
128 mapMaybeM f = liftM catMaybes . mapM f
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)
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)
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
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)
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
156 -- | Monadic version of foldl
157 foldlM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
158 foldlM = foldM
161 foldlM_ :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m ()
162 foldlM_ = foldM_
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 }
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
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 }