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 #define HAVE_APPLICATIVE 1
25 -- we don't depend on MTL for now
28 ----------------------------------------------------------------------------------------
30 ----------------------------------------------------------------------------------------
33 import Control.Applicative
36 import Control.Monad.Trans
39 import Control.Monad.Fix
41 ----------------------------------------------------------------------------------------
43 ----------------------------------------------------------------------------------------
47 class Functor f => Applicative f where
49 (<*>) :: f (a -> b) -> f a -> f b
51 (<$>) :: Functor f => (a -> b) -> (f a -> f b)
59 ----------------------------------------------------------------------------------------
61 ----------------------------------------------------------------------------------------
65 class Monad m => MonadIO m where
70 ----------------------------------------------------------------------------------------
72 -- These are used throught the compiler
73 ----------------------------------------------------------------------------------------
75 -- | mapAndUnzipM for triples
76 mapAndUnzip3M :: Monad m => (a -> m (b,c,d)) -> [a] -> m ([b],[c],[d])
77 mapAndUnzip3M _ [] = return ([],[],[])
78 mapAndUnzip3M f (x:xs) = do
80 (rs1, rs2, rs3) <- mapAndUnzip3M f xs
81 return (r1:rs1, r2:rs2, r3:rs3)
83 mapAndUnzip4M :: Monad m => (a -> m (b,c,d,e)) -> [a] -> m ([b],[c],[d],[e])
84 mapAndUnzip4M _ [] = return ([],[],[],[])
85 mapAndUnzip4M f (x:xs) = do
86 (r1, r2, r3, r4) <- f x
87 (rs1, rs2, rs3, rs4) <- mapAndUnzip4M f xs
88 return (r1:rs1, r2:rs2, r3:rs3, r4:rs4)
90 -- | Monadic version of mapAccumL
92 => (acc -> x -> m (acc, y)) -- ^ combining funcction
93 -> acc -- ^ initial state
95 -> m (acc, [y]) -- ^ final state, outputs
96 mapAccumLM _ s [] = return (s, [])
97 mapAccumLM f s (x:xs) = do
99 (s2, xs') <- mapAccumLM f s1 xs
100 return (s2, x' : xs')
102 -- | Monadic version of mapSnd
103 mapSndM :: Monad m => (b -> m c) -> [(a,b)] -> m [(a,c)]
104 mapSndM _ [] = return []
105 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
107 -- | Monadic version of concatMap
108 concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
109 concatMapM f xs = liftM concat (mapM f xs)
111 -- | Monadic version of 'any', aborts the computation at the first False value
112 anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
113 anyM _ [] = return False
114 anyM f (x:xs) = do b <- f x
115 if b then return True
118 -- | Monadic version of foldl
119 foldlM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
122 -- | Monadic version of foldr
123 foldrM :: (Monad m) => (b -> a -> m a) -> a -> [b] -> m a
124 foldrM _ z [] = return z
125 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }