2 -- | Utilities related to Monad and Applicative classes
3 -- Mostly for backwards compatability.
12 , liftIO1, liftIO2, liftIO3, liftIO4
14 , mapAndUnzipM, mapAndUnzip3M, mapAndUnzip4M
23 ----------------------------------------------------------------------------------------
24 -- Detection of available libraries
25 ----------------------------------------------------------------------------------------
27 #if __GLASGOW_HASKELL__ >= 606
28 #define HAVE_APPLICATIVE 1
30 #define HAVE_APPLICATIVE 0
32 -- we don't depend on MTL for now
35 ----------------------------------------------------------------------------------------
37 ----------------------------------------------------------------------------------------
42 import Control.Applicative
45 import Control.Monad.Trans
48 import Control.Monad.Fix
50 ----------------------------------------------------------------------------------------
52 ----------------------------------------------------------------------------------------
56 class Functor f => Applicative f where
58 (<*>) :: f (a -> b) -> f a -> f b
60 (<$>) :: Functor f => (a -> b) -> (f a -> f b)
66 instance Applicative IO where
72 ----------------------------------------------------------------------------------------
74 ----------------------------------------------------------------------------------------
78 class Monad m => MonadIO m where
81 instance MonadIO IO where liftIO = id
84 ----------------------------------------------------------------------------------------
86 -- These are used throughout the compiler
87 ----------------------------------------------------------------------------------------
89 -- | Lift an 'IO' operation with 1 argument into another monad
90 liftIO1 :: MonadIO m => (a -> IO b) -> a -> m b
93 -- | Lift an 'IO' operation with 2 arguments into another monad
94 liftIO2 :: MonadIO m => (a -> b -> IO c) -> a -> b -> m c
95 liftIO2 = ((.).(.)) liftIO
97 -- | Lift an 'IO' operation with 3 arguments into another monad
98 liftIO3 :: MonadIO m => (a -> b -> c -> IO d) -> a -> b -> c -> m d
99 liftIO3 = ((.).((.).(.))) liftIO
101 -- | Lift an 'IO' operation with 4 arguments into another monad
102 liftIO4 :: MonadIO m => (a -> b -> c -> d -> IO e) -> a -> b -> c -> d -> m e
103 liftIO4 = (((.).(.)).((.).(.))) liftIO
105 ----------------------------------------------------------------------------------------
107 -- These are used throughout the compiler
108 ----------------------------------------------------------------------------------------
110 -- | mapAndUnzipM for triples
111 mapAndUnzip3M :: Monad m => (a -> m (b,c,d)) -> [a] -> m ([b],[c],[d])
112 mapAndUnzip3M _ [] = return ([],[],[])
113 mapAndUnzip3M f (x:xs) = do
115 (rs1, rs2, rs3) <- mapAndUnzip3M f xs
116 return (r1:rs1, r2:rs2, r3:rs3)
118 mapAndUnzip4M :: Monad m => (a -> m (b,c,d,e)) -> [a] -> m ([b],[c],[d],[e])
119 mapAndUnzip4M _ [] = return ([],[],[],[])
120 mapAndUnzip4M f (x:xs) = do
121 (r1, r2, r3, r4) <- f x
122 (rs1, rs2, rs3, rs4) <- mapAndUnzip4M f xs
123 return (r1:rs1, r2:rs2, r3:rs3, r4:rs4)
125 -- | Monadic version of mapAccumL
126 mapAccumLM :: Monad m
127 => (acc -> x -> m (acc, y)) -- ^ combining funcction
128 -> acc -- ^ initial state
130 -> m (acc, [y]) -- ^ final state, outputs
131 mapAccumLM _ s [] = return (s, [])
132 mapAccumLM f s (x:xs) = do
134 (s2, xs') <- mapAccumLM f s1 xs
135 return (s2, x' : xs')
137 -- | Monadic version of mapSnd
138 mapSndM :: Monad m => (b -> m c) -> [(a,b)] -> m [(a,c)]
139 mapSndM _ [] = return []
140 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
142 -- | Monadic version of concatMap
143 concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
144 concatMapM f xs = liftM concat (mapM f xs)
146 -- | Monadic version of mapMaybe
147 mapMaybeM :: (Monad m) => (a -> m (Maybe b)) -> [a] -> m [b]
148 mapMaybeM f = liftM catMaybes . mapM f
150 -- | Monadic version of 'any', aborts the computation at the first @True@ value
151 anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
152 anyM _ [] = return False
153 anyM f (x:xs) = do b <- f x
154 if b then return True
157 -- | Monad version of 'all', aborts the computation at the first @False@ value
158 allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
159 allM _ [] = return True
160 allM f (b:bs) = (f b) >>= (\bv -> if bv then allM f bs else return False)
162 -- | Monadic version of foldl
163 foldlM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
166 -- | Monadic version of foldr
167 foldrM :: (Monad m) => (b -> a -> m a) -> a -> [b] -> m a
168 foldrM _ z [] = return z
169 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }