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 -- we don't depend on MTL for now
30 ----------------------------------------------------------------------------------------
32 ----------------------------------------------------------------------------------------
36 import Control.Applicative
38 import Control.Monad.Trans
41 import Control.Monad.Fix
43 ----------------------------------------------------------------------------------------
45 ----------------------------------------------------------------------------------------
49 class Monad m => MonadIO m where
52 instance MonadIO IO where liftIO = id
55 ----------------------------------------------------------------------------------------
57 -- These are used throughout the compiler
58 ----------------------------------------------------------------------------------------
60 -- | Lift an 'IO' operation with 1 argument into another monad
61 liftIO1 :: MonadIO m => (a -> IO b) -> a -> m b
64 -- | Lift an 'IO' operation with 2 arguments into another monad
65 liftIO2 :: MonadIO m => (a -> b -> IO c) -> a -> b -> m c
66 liftIO2 = ((.).(.)) liftIO
68 -- | Lift an 'IO' operation with 3 arguments into another monad
69 liftIO3 :: MonadIO m => (a -> b -> c -> IO d) -> a -> b -> c -> m d
70 liftIO3 = ((.).((.).(.))) liftIO
72 -- | Lift an 'IO' operation with 4 arguments into another monad
73 liftIO4 :: MonadIO m => (a -> b -> c -> d -> IO e) -> a -> b -> c -> d -> m e
74 liftIO4 = (((.).(.)).((.).(.))) liftIO
76 ----------------------------------------------------------------------------------------
78 -- These are used throughout the compiler
79 ----------------------------------------------------------------------------------------
81 -- | mapAndUnzipM for triples
82 mapAndUnzip3M :: Monad m => (a -> m (b,c,d)) -> [a] -> m ([b],[c],[d])
83 mapAndUnzip3M _ [] = return ([],[],[])
84 mapAndUnzip3M f (x:xs) = do
86 (rs1, rs2, rs3) <- mapAndUnzip3M f xs
87 return (r1:rs1, r2:rs2, r3:rs3)
89 mapAndUnzip4M :: Monad m => (a -> m (b,c,d,e)) -> [a] -> m ([b],[c],[d],[e])
90 mapAndUnzip4M _ [] = return ([],[],[],[])
91 mapAndUnzip4M f (x:xs) = do
92 (r1, r2, r3, r4) <- f x
93 (rs1, rs2, rs3, rs4) <- mapAndUnzip4M f xs
94 return (r1:rs1, r2:rs2, r3:rs3, r4:rs4)
96 -- | Monadic version of mapAccumL
98 => (acc -> x -> m (acc, y)) -- ^ combining funcction
99 -> acc -- ^ initial state
101 -> m (acc, [y]) -- ^ final state, outputs
102 mapAccumLM _ s [] = return (s, [])
103 mapAccumLM f s (x:xs) = do
105 (s2, xs') <- mapAccumLM f s1 xs
106 return (s2, x' : xs')
108 -- | Monadic version of mapSnd
109 mapSndM :: Monad m => (b -> m c) -> [(a,b)] -> m [(a,c)]
110 mapSndM _ [] = return []
111 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
113 -- | Monadic version of concatMap
114 concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
115 concatMapM f xs = liftM concat (mapM f xs)
117 -- | Monadic version of mapMaybe
118 mapMaybeM :: (Monad m) => (a -> m (Maybe b)) -> [a] -> m [b]
119 mapMaybeM f = liftM catMaybes . mapM f
121 -- | Monadic version of 'any', aborts the computation at the first @True@ value
122 anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
123 anyM _ [] = return False
124 anyM f (x:xs) = do b <- f x
125 if b then return True
128 -- | Monad version of 'all', aborts the computation at the first @False@ value
129 allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
130 allM _ [] = return True
131 allM f (b:bs) = (f b) >>= (\bv -> if bv then allM f bs else return False)
133 -- | Monadic version of foldl
134 foldlM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
137 -- | Monadic version of foldr
138 foldrM :: (Monad m) => (b -> a -> m a) -> a -> [b] -> m a
139 foldrM _ z [] = return z
140 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }