2 -- | Utilities related to Monad and Applicative classes
3 -- Mostly for backwards compatability.
14 , liftIO1, liftIO2, liftIO3, liftIO4
17 , mapAndUnzipM, mapAndUnzip3M, mapAndUnzip4M
23 , foldlM, foldlM_, foldrM
29 ----------------------------------------------------------------------------------------
30 -- Detection of available libraries
31 ----------------------------------------------------------------------------------------
33 -- we don't depend on MTL for now
36 ----------------------------------------------------------------------------------------
38 ----------------------------------------------------------------------------------------
42 import Control.Applicative
44 import Control.Monad.Trans
47 import Control.Monad.Fix
49 ----------------------------------------------------------------------------------------
51 ----------------------------------------------------------------------------------------
54 instance Monad ID where
63 ----------------------------------------------------------------------------------------
65 ----------------------------------------------------------------------------------------
69 class Monad m => MonadIO m where
72 instance MonadIO IO where liftIO = id
75 ----------------------------------------------------------------------------------------
77 -- These are used throughout the compiler
78 ----------------------------------------------------------------------------------------
80 -- | Lift an 'IO' operation with 1 argument into another monad
81 liftIO1 :: MonadIO m => (a -> IO b) -> a -> m b
84 -- | Lift an 'IO' operation with 2 arguments into another monad
85 liftIO2 :: MonadIO m => (a -> b -> IO c) -> a -> b -> m c
86 liftIO2 = ((.).(.)) liftIO
88 -- | Lift an 'IO' operation with 3 arguments into another monad
89 liftIO3 :: MonadIO m => (a -> b -> c -> IO d) -> a -> b -> c -> m d
90 liftIO3 = ((.).((.).(.))) liftIO
92 -- | Lift an 'IO' operation with 4 arguments into another monad
93 liftIO4 :: MonadIO m => (a -> b -> c -> d -> IO e) -> a -> b -> c -> d -> m e
94 liftIO4 = (((.).(.)).((.).(.))) liftIO
96 ----------------------------------------------------------------------------------------
98 -- These are used throughout the compiler
99 ----------------------------------------------------------------------------------------
101 zipWith3M :: Monad m => (a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
102 zipWith3M _ [] _ _ = return []
103 zipWith3M _ _ [] _ = return []
104 zipWith3M _ _ _ [] = return []
105 zipWith3M f (x:xs) (y:ys) (z:zs)
107 ; rs <- zipWith3M f xs ys zs
111 -- | mapAndUnzipM for triples
112 mapAndUnzip3M :: Monad m => (a -> m (b,c,d)) -> [a] -> m ([b],[c],[d])
113 mapAndUnzip3M _ [] = return ([],[],[])
114 mapAndUnzip3M f (x:xs) = do
116 (rs1, rs2, rs3) <- mapAndUnzip3M f xs
117 return (r1:rs1, r2:rs2, r3:rs3)
119 mapAndUnzip4M :: Monad m => (a -> m (b,c,d,e)) -> [a] -> m ([b],[c],[d],[e])
120 mapAndUnzip4M _ [] = return ([],[],[],[])
121 mapAndUnzip4M f (x:xs) = do
122 (r1, r2, r3, r4) <- f x
123 (rs1, rs2, rs3, rs4) <- mapAndUnzip4M f xs
124 return (r1:rs1, r2:rs2, r3:rs3, r4:rs4)
126 -- | Monadic version of mapAccumL
127 mapAccumLM :: Monad m
128 => (acc -> x -> m (acc, y)) -- ^ combining funcction
129 -> acc -- ^ initial state
131 -> m (acc, [y]) -- ^ final state, outputs
132 mapAccumLM _ s [] = return (s, [])
133 mapAccumLM f s (x:xs) = do
135 (s2, xs') <- mapAccumLM f s1 xs
136 return (s2, x' : xs')
138 -- | Monadic version of mapSnd
139 mapSndM :: Monad m => (b -> m c) -> [(a,b)] -> m [(a,c)]
140 mapSndM _ [] = return []
141 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
143 -- | Monadic version of concatMap
144 concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
145 concatMapM f xs = liftM concat (mapM f xs)
147 -- | Monadic version of mapMaybe
148 mapMaybeM :: (Monad m) => (a -> m (Maybe b)) -> [a] -> m [b]
149 mapMaybeM f = liftM catMaybes . mapM f
151 -- | Monadic version of 'any', aborts the computation at the first @True@ value
152 anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
153 anyM _ [] = return False
154 anyM f (x:xs) = do b <- f x
155 if b then return True
158 -- | Monad version of 'all', aborts the computation at the first @False@ value
159 allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
160 allM _ [] = return True
161 allM f (b:bs) = (f b) >>= (\bv -> if bv then allM f bs else return False)
163 -- | Monadic version of foldl
164 foldlM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
167 -- | Monadic version of foldl that discards its result
168 foldlM_ :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m ()
171 -- | Monadic version of foldr
172 foldrM :: (Monad m) => (b -> a -> m a) -> a -> [b] -> m a
173 foldrM _ z [] = return z
174 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }
176 -- | Monadic version of fmap specialised for Maybe
177 maybeMapM :: Monad m => (a -> m b) -> (Maybe a -> m (Maybe b))
178 maybeMapM _ Nothing = return Nothing
179 maybeMapM m (Just x) = liftM Just $ m x