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
76 instance MonadIO IO where liftIO = id
79 ----------------------------------------------------------------------------------------
81 -- These are used throught the compiler
82 ----------------------------------------------------------------------------------------
84 -- | mapAndUnzipM for triples
85 mapAndUnzip3M :: Monad m => (a -> m (b,c,d)) -> [a] -> m ([b],[c],[d])
86 mapAndUnzip3M _ [] = return ([],[],[])
87 mapAndUnzip3M f (x:xs) = do
89 (rs1, rs2, rs3) <- mapAndUnzip3M f xs
90 return (r1:rs1, r2:rs2, r3:rs3)
92 mapAndUnzip4M :: Monad m => (a -> m (b,c,d,e)) -> [a] -> m ([b],[c],[d],[e])
93 mapAndUnzip4M _ [] = return ([],[],[],[])
94 mapAndUnzip4M f (x:xs) = do
95 (r1, r2, r3, r4) <- f x
96 (rs1, rs2, rs3, rs4) <- mapAndUnzip4M f xs
97 return (r1:rs1, r2:rs2, r3:rs3, r4:rs4)
99 -- | Monadic version of mapAccumL
100 mapAccumLM :: Monad m
101 => (acc -> x -> m (acc, y)) -- ^ combining funcction
102 -> acc -- ^ initial state
104 -> m (acc, [y]) -- ^ final state, outputs
105 mapAccumLM _ s [] = return (s, [])
106 mapAccumLM f s (x:xs) = do
108 (s2, xs') <- mapAccumLM f s1 xs
109 return (s2, x' : xs')
111 -- | Monadic version of mapSnd
112 mapSndM :: Monad m => (b -> m c) -> [(a,b)] -> m [(a,c)]
113 mapSndM _ [] = return []
114 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
116 -- | Monadic version of concatMap
117 concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
118 concatMapM f xs = liftM concat (mapM f xs)
120 -- | Monadic version of 'any', aborts the computation at the first @True@ value
121 anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
122 anyM _ [] = return False
123 anyM f (x:xs) = do b <- f x
124 if b then return True
127 -- | Monad version of 'all', aborts the computation at the first @False@ value
128 allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
129 allM _ [] = return True
130 allM f (b:bs) = (f b) >>= (\bv -> if bv then allM f bs else return False)
132 -- | Monadic version of foldl
133 foldlM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
136 -- | Monadic version of foldr
137 foldrM :: (Monad m) => (b -> a -> m a) -> a -> [b] -> m a
138 foldrM _ z [] = return z
139 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }