2 -- | Utilities related to Monad and Applicative classes
3 -- Mostly for backwards compatability.
14 , liftIO1, liftIO2, liftIO3, liftIO4
17 , mapAndUnzipM, mapAndUnzip3M, mapAndUnzip4M
22 , fmapMaybeM, fmapEitherM
24 , foldlM, foldlM_, foldrM
30 -------------------------------------------------------------------------------
31 -- Detection of available libraries
32 -------------------------------------------------------------------------------
34 -- we don't depend on MTL for now
37 -------------------------------------------------------------------------------
39 -------------------------------------------------------------------------------
43 import Control.Applicative
45 import Control.Monad.Trans
48 import Control.Monad.Fix
50 -------------------------------------------------------------------------------
52 -------------------------------------------------------------------------------
55 instance Monad ID where
64 -------------------------------------------------------------------------------
66 -------------------------------------------------------------------------------
70 class Monad m => MonadIO m where
73 instance MonadIO IO where liftIO = id
76 -------------------------------------------------------------------------------
78 -- These are used throughout the compiler
79 -------------------------------------------------------------------------------
81 -- | Lift an 'IO' operation with 1 argument into another monad
82 liftIO1 :: MonadIO m => (a -> IO b) -> a -> m b
85 -- | Lift an 'IO' operation with 2 arguments into another monad
86 liftIO2 :: MonadIO m => (a -> b -> IO c) -> a -> b -> m c
87 liftIO2 = ((.).(.)) liftIO
89 -- | Lift an 'IO' operation with 3 arguments into another monad
90 liftIO3 :: MonadIO m => (a -> b -> c -> IO d) -> a -> b -> c -> m d
91 liftIO3 = ((.).((.).(.))) liftIO
93 -- | Lift an 'IO' operation with 4 arguments into another monad
94 liftIO4 :: MonadIO m => (a -> b -> c -> d -> IO e) -> a -> b -> c -> d -> m e
95 liftIO4 = (((.).(.)).((.).(.))) liftIO
97 -------------------------------------------------------------------------------
99 -- These are used throughout the compiler
100 -------------------------------------------------------------------------------
102 zipWith3M :: Monad m => (a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
103 zipWith3M _ [] _ _ = return []
104 zipWith3M _ _ [] _ = return []
105 zipWith3M _ _ _ [] = return []
106 zipWith3M f (x:xs) (y:ys) (z:zs)
108 ; rs <- zipWith3M f xs ys zs
112 -- | mapAndUnzipM for triples
113 mapAndUnzip3M :: Monad m => (a -> m (b,c,d)) -> [a] -> m ([b],[c],[d])
114 mapAndUnzip3M _ [] = return ([],[],[])
115 mapAndUnzip3M f (x:xs) = do
117 (rs1, rs2, rs3) <- mapAndUnzip3M f xs
118 return (r1:rs1, r2:rs2, r3:rs3)
120 mapAndUnzip4M :: Monad m => (a -> m (b,c,d,e)) -> [a] -> m ([b],[c],[d],[e])
121 mapAndUnzip4M _ [] = return ([],[],[],[])
122 mapAndUnzip4M f (x:xs) = do
123 (r1, r2, r3, r4) <- f x
124 (rs1, rs2, rs3, rs4) <- mapAndUnzip4M f xs
125 return (r1:rs1, r2:rs2, r3:rs3, r4:rs4)
127 -- | Monadic version of mapAccumL
128 mapAccumLM :: Monad m
129 => (acc -> x -> m (acc, y)) -- ^ combining funcction
130 -> acc -- ^ initial state
132 -> m (acc, [y]) -- ^ final state, outputs
133 mapAccumLM _ s [] = return (s, [])
134 mapAccumLM f s (x:xs) = do
136 (s2, xs') <- mapAccumLM f s1 xs
137 return (s2, x' : xs')
139 -- | Monadic version of mapSnd
140 mapSndM :: Monad m => (b -> m c) -> [(a,b)] -> m [(a,c)]
141 mapSndM _ [] = return []
142 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
144 -- | Monadic version of concatMap
145 concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
146 concatMapM f xs = liftM concat (mapM f xs)
148 -- | Monadic version of mapMaybe
149 mapMaybeM :: (Monad m) => (a -> m (Maybe b)) -> [a] -> m [b]
150 mapMaybeM f = liftM catMaybes . mapM f
152 -- | Monadic version of fmap
153 fmapMaybeM :: (Monad m) => (a -> m b) -> Maybe a -> m (Maybe b)
154 fmapMaybeM _ Nothing = return Nothing
155 fmapMaybeM f (Just x) = f x >>= (return . Just)
157 -- | Monadic version of fmap
158 fmapEitherM :: Monad m => (a -> m b) -> (c -> m d) -> Either a c -> m (Either b d)
159 fmapEitherM fl _ (Left a) = fl a >>= (return . Left)
160 fmapEitherM _ fr (Right b) = fr b >>= (return . Right)
162 -- | Monadic version of 'any', aborts the computation at the first @True@ value
163 anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
164 anyM _ [] = return False
165 anyM f (x:xs) = do b <- f x
166 if b then return True
169 -- | Monad version of 'all', aborts the computation at the first @False@ value
170 allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
171 allM _ [] = return True
172 allM f (b:bs) = (f b) >>= (\bv -> if bv then allM f bs else return False)
174 -- | Monadic version of foldl
175 foldlM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
178 -- | Monadic version of foldl that discards its result
179 foldlM_ :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m ()
182 -- | Monadic version of foldr
183 foldrM :: (Monad m) => (b -> a -> m a) -> a -> [b] -> m a
184 foldrM _ z [] = return z
185 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }
187 -- | Monadic version of fmap specialised for Maybe
188 maybeMapM :: Monad m => (a -> m b) -> (Maybe a -> m (Maybe b))
189 maybeMapM _ Nothing = return Nothing
190 maybeMapM m (Just x) = liftM Just $ m x