2 -- | Utilities related to Monad and Applicative classes
3 -- Mostly for backwards compatability.
12 , liftIO1, liftIO2, liftIO3, liftIO4
15 , mapAndUnzipM, mapAndUnzip3M, mapAndUnzip4M
25 ----------------------------------------------------------------------------------------
26 -- Detection of available libraries
27 ----------------------------------------------------------------------------------------
29 -- we don't depend on MTL for now
32 ----------------------------------------------------------------------------------------
34 ----------------------------------------------------------------------------------------
38 import Control.Applicative
40 import Control.Monad.Trans
43 import Control.Monad.Fix
45 ----------------------------------------------------------------------------------------
47 ----------------------------------------------------------------------------------------
51 class Monad m => MonadIO m where
54 instance MonadIO IO where liftIO = id
57 ----------------------------------------------------------------------------------------
59 -- These are used throughout the compiler
60 ----------------------------------------------------------------------------------------
62 -- | Lift an 'IO' operation with 1 argument into another monad
63 liftIO1 :: MonadIO m => (a -> IO b) -> a -> m b
66 -- | Lift an 'IO' operation with 2 arguments into another monad
67 liftIO2 :: MonadIO m => (a -> b -> IO c) -> a -> b -> m c
68 liftIO2 = ((.).(.)) liftIO
70 -- | Lift an 'IO' operation with 3 arguments into another monad
71 liftIO3 :: MonadIO m => (a -> b -> c -> IO d) -> a -> b -> c -> m d
72 liftIO3 = ((.).((.).(.))) liftIO
74 -- | Lift an 'IO' operation with 4 arguments into another monad
75 liftIO4 :: MonadIO m => (a -> b -> c -> d -> IO e) -> a -> b -> c -> d -> m e
76 liftIO4 = (((.).(.)).((.).(.))) liftIO
78 ----------------------------------------------------------------------------------------
80 -- These are used throughout the compiler
81 ----------------------------------------------------------------------------------------
83 zipWith3M :: Monad m => (a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
84 zipWith3M _ [] _ _ = return []
85 zipWith3M _ _ [] _ = return []
86 zipWith3M _ _ _ [] = return []
87 zipWith3M f (x:xs) (y:ys) (z:zs)
89 ; rs <- zipWith3M f xs ys zs
93 -- | mapAndUnzipM for triples
94 mapAndUnzip3M :: Monad m => (a -> m (b,c,d)) -> [a] -> m ([b],[c],[d])
95 mapAndUnzip3M _ [] = return ([],[],[])
96 mapAndUnzip3M f (x:xs) = do
98 (rs1, rs2, rs3) <- mapAndUnzip3M f xs
99 return (r1:rs1, r2:rs2, r3:rs3)
101 mapAndUnzip4M :: Monad m => (a -> m (b,c,d,e)) -> [a] -> m ([b],[c],[d],[e])
102 mapAndUnzip4M _ [] = return ([],[],[],[])
103 mapAndUnzip4M f (x:xs) = do
104 (r1, r2, r3, r4) <- f x
105 (rs1, rs2, rs3, rs4) <- mapAndUnzip4M f xs
106 return (r1:rs1, r2:rs2, r3:rs3, r4:rs4)
108 -- | Monadic version of mapAccumL
109 mapAccumLM :: Monad m
110 => (acc -> x -> m (acc, y)) -- ^ combining funcction
111 -> acc -- ^ initial state
113 -> m (acc, [y]) -- ^ final state, outputs
114 mapAccumLM _ s [] = return (s, [])
115 mapAccumLM f s (x:xs) = do
117 (s2, xs') <- mapAccumLM f s1 xs
118 return (s2, x' : xs')
120 -- | Monadic version of mapSnd
121 mapSndM :: Monad m => (b -> m c) -> [(a,b)] -> m [(a,c)]
122 mapSndM _ [] = return []
123 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
125 -- | Monadic version of concatMap
126 concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
127 concatMapM f xs = liftM concat (mapM f xs)
129 -- | Monadic version of mapMaybe
130 mapMaybeM :: (Monad m) => (a -> m (Maybe b)) -> [a] -> m [b]
131 mapMaybeM f = liftM catMaybes . mapM f
133 -- | Monadic version of 'any', aborts the computation at the first @True@ value
134 anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
135 anyM _ [] = return False
136 anyM f (x:xs) = do b <- f x
137 if b then return True
140 -- | Monad version of 'all', aborts the computation at the first @False@ value
141 allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
142 allM _ [] = return True
143 allM f (b:bs) = (f b) >>= (\bv -> if bv then allM f bs else return False)
145 -- | Monadic version of foldl
146 foldlM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
149 -- | Monadic version of foldr
150 foldrM :: (Monad m) => (b -> a -> m a) -> a -> [b] -> m a
151 foldrM _ z [] = return z
152 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }
154 -- | Monadic version of fmap specialised for Maybe
155 maybeMapM :: Monad m => (a -> m b) -> (Maybe a -> m (Maybe b))
156 maybeMapM _ Nothing = return Nothing
157 maybeMapM m (Just x) = liftM Just $ m x