2 -- | Utilities related to Monad and Applicative classes
3 -- Mostly for backwards compatability.
12 , mapAndUnzipM, mapAndUnzip3M, mapAndUnzip4M
20 ----------------------------------------------------------------------------------------
21 -- Detection of available libraries
22 ----------------------------------------------------------------------------------------
24 #if __GLASGOW_HASKELL__ >= 606
25 #define HAVE_APPLICATIVE 1
27 #define HAVE_APPLICATIVE 0
29 -- we don't depend on MTL for now
32 ----------------------------------------------------------------------------------------
34 ----------------------------------------------------------------------------------------
37 import Control.Applicative
40 import Control.Monad.Trans
43 import Control.Monad.Fix
45 ----------------------------------------------------------------------------------------
47 ----------------------------------------------------------------------------------------
51 class Functor f => Applicative f where
53 (<*>) :: f (a -> b) -> f a -> f b
55 (<$>) :: Functor f => (a -> b) -> (f a -> f b)
61 instance Applicative IO where
67 ----------------------------------------------------------------------------------------
69 ----------------------------------------------------------------------------------------
73 class Monad m => MonadIO m where
78 ----------------------------------------------------------------------------------------
80 -- These are used throught the compiler
81 ----------------------------------------------------------------------------------------
83 -- | mapAndUnzipM for triples
84 mapAndUnzip3M :: Monad m => (a -> m (b,c,d)) -> [a] -> m ([b],[c],[d])
85 mapAndUnzip3M _ [] = return ([],[],[])
86 mapAndUnzip3M f (x:xs) = do
88 (rs1, rs2, rs3) <- mapAndUnzip3M f xs
89 return (r1:rs1, r2:rs2, r3:rs3)
91 mapAndUnzip4M :: Monad m => (a -> m (b,c,d,e)) -> [a] -> m ([b],[c],[d],[e])
92 mapAndUnzip4M _ [] = return ([],[],[],[])
93 mapAndUnzip4M f (x:xs) = do
94 (r1, r2, r3, r4) <- f x
95 (rs1, rs2, rs3, rs4) <- mapAndUnzip4M f xs
96 return (r1:rs1, r2:rs2, r3:rs3, r4:rs4)
98 -- | Monadic version of mapAccumL
100 => (acc -> x -> m (acc, y)) -- ^ combining funcction
101 -> acc -- ^ initial state
103 -> m (acc, [y]) -- ^ final state, outputs
104 mapAccumLM _ s [] = return (s, [])
105 mapAccumLM f s (x:xs) = do
107 (s2, xs') <- mapAccumLM f s1 xs
108 return (s2, x' : xs')
110 -- | Monadic version of mapSnd
111 mapSndM :: Monad m => (b -> m c) -> [(a,b)] -> m [(a,c)]
112 mapSndM _ [] = return []
113 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
115 -- | Monadic version of concatMap
116 concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
117 concatMapM f xs = liftM concat (mapM f xs)
119 -- | Monadic version of 'any', aborts the computation at the first @True@ value
120 anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
121 anyM _ [] = return False
122 anyM f (x:xs) = do b <- f x
123 if b then return True
126 -- | Monad version of 'all', aborts the computation at the first @False@ value
127 allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
128 allM _ [] = return True
129 allM f (b:bs) = (f b) >>= (\bv -> if bv then allM f bs else return False)
131 -- | Monadic version of foldl
132 foldlM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
135 -- | Monadic version of foldr
136 foldrM :: (Monad m) => (b -> a -> m a) -> a -> [b] -> m a
137 foldrM _ z [] = return z
138 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }