Support for -fwarn-unused-do-bind and -fwarn-wrong-do-bind, as per #3263
[ghc-hetmet.git] / compiler / utils / MonadUtils.hs
1
2 -- | Utilities related to Monad and Applicative classes
3 --   Mostly for backwards compatability.
4
5 module MonadUtils
6         ( Applicative(..)
7         , (<$>)
8         
9         , MonadFix(..)
10         , MonadIO(..)
11         
12         , liftIO1, liftIO2, liftIO3, liftIO4
13
14         , zipWith3M        
15         , mapAndUnzipM, mapAndUnzip3M, mapAndUnzip4M
16         , mapAccumLM
17         , mapSndM
18         , concatMapM
19         , mapMaybeM
20         , anyM, allM
21         , foldlM, foldlM_, foldrM
22         , maybeMapM
23         ) where
24
25 ----------------------------------------------------------------------------------------
26 -- Detection of available libraries
27 ----------------------------------------------------------------------------------------
28
29 -- we don't depend on MTL for now
30 #define HAVE_MTL 0
31
32 ----------------------------------------------------------------------------------------
33 -- Imports
34 ----------------------------------------------------------------------------------------
35
36 import Maybes
37
38 import Control.Applicative
39 #if HAVE_MTL
40 import Control.Monad.Trans
41 #endif
42 import Control.Monad
43 import Control.Monad.Fix
44
45 ----------------------------------------------------------------------------------------
46 -- MTL
47 ----------------------------------------------------------------------------------------
48
49 #if !HAVE_MTL
50
51 class Monad m => MonadIO m where
52     liftIO :: IO a -> m a
53
54 instance MonadIO IO where liftIO = id
55 #endif
56
57 ----------------------------------------------------------------------------------------
58 -- Lift combinators
59 --  These are used throughout the compiler
60 ----------------------------------------------------------------------------------------
61
62 -- | Lift an 'IO' operation with 1 argument into another monad
63 liftIO1 :: MonadIO m => (a -> IO b) -> a -> m b
64 liftIO1 = (.) liftIO
65
66 -- | Lift an 'IO' operation with 2 arguments into another monad
67 liftIO2 :: MonadIO m => (a -> b -> IO c) -> a -> b -> m c
68 liftIO2 = ((.).(.)) liftIO
69
70 -- | Lift an 'IO' operation with 3 arguments into another monad
71 liftIO3 :: MonadIO m => (a -> b -> c -> IO d) -> a -> b -> c -> m d
72 liftIO3 = ((.).((.).(.))) liftIO
73
74 -- | Lift an 'IO' operation with 4 arguments into another monad
75 liftIO4 :: MonadIO m => (a -> b -> c -> d -> IO e) -> a -> b -> c -> d -> m e
76 liftIO4 = (((.).(.)).((.).(.))) liftIO
77
78 ----------------------------------------------------------------------------------------
79 -- Common functions
80 --  These are used throughout the compiler
81 ----------------------------------------------------------------------------------------
82
83 zipWith3M :: Monad m => (a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
84 zipWith3M _ []     _      _      = return []
85 zipWith3M _ _      []     _      = return []
86 zipWith3M _ _      _      []     = return []
87 zipWith3M f (x:xs) (y:ys) (z:zs) 
88   = do { r  <- f x y z
89        ; rs <- zipWith3M f xs ys zs
90        ; return $ r:rs
91        }
92
93 -- | mapAndUnzipM for triples
94 mapAndUnzip3M :: Monad m => (a -> m (b,c,d)) -> [a] -> m ([b],[c],[d])
95 mapAndUnzip3M _ []     = return ([],[],[])
96 mapAndUnzip3M f (x:xs) = do
97     (r1,  r2,  r3)  <- f x
98     (rs1, rs2, rs3) <- mapAndUnzip3M f xs
99     return (r1:rs1, r2:rs2, r3:rs3)
100
101 mapAndUnzip4M :: Monad m => (a -> m (b,c,d,e)) -> [a] -> m ([b],[c],[d],[e])
102 mapAndUnzip4M _ []     = return ([],[],[],[])
103 mapAndUnzip4M f (x:xs) = do
104     (r1,  r2,  r3,  r4)  <- f x
105     (rs1, rs2, rs3, rs4) <- mapAndUnzip4M f xs
106     return (r1:rs1, r2:rs2, r3:rs3, r4:rs4)
107
108 -- | Monadic version of mapAccumL
109 mapAccumLM :: Monad m
110             => (acc -> x -> m (acc, y)) -- ^ combining funcction
111             -> acc                      -- ^ initial state
112             -> [x]                      -- ^ inputs
113             -> m (acc, [y])             -- ^ final state, outputs
114 mapAccumLM _ s []     = return (s, [])
115 mapAccumLM f s (x:xs) = do
116     (s1, x')  <- f s x
117     (s2, xs') <- mapAccumLM f s1 xs
118     return    (s2, x' : xs')
119
120 -- | Monadic version of mapSnd
121 mapSndM :: Monad m => (b -> m c) -> [(a,b)] -> m [(a,c)]
122 mapSndM _ []         = return []
123 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
124
125 -- | Monadic version of concatMap
126 concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
127 concatMapM f xs = liftM concat (mapM f xs)
128
129 -- | Monadic version of mapMaybe
130 mapMaybeM :: (Monad m) => (a -> m (Maybe b)) -> [a] -> m [b]
131 mapMaybeM f = liftM catMaybes . mapM f
132
133 -- | Monadic version of 'any', aborts the computation at the first @True@ value
134 anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
135 anyM _ []     = return False
136 anyM f (x:xs) = do b <- f x
137                    if b then return True 
138                         else anyM f xs
139
140 -- | Monad version of 'all', aborts the computation at the first @False@ value
141 allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
142 allM _ []     = return True
143 allM f (b:bs) = (f b) >>= (\bv -> if bv then allM f bs else return False)
144
145 -- | Monadic version of foldl
146 foldlM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
147 foldlM = foldM
148
149 -- | Monadic version of foldl that discards its result
150 foldlM_ :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m ()
151 foldlM_ = foldM_
152
153 -- | Monadic version of foldr
154 foldrM        :: (Monad m) => (b -> a -> m a) -> a -> [b] -> m a
155 foldrM _ z []     = return z
156 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }
157
158 -- | Monadic version of fmap specialised for Maybe
159 maybeMapM :: Monad m => (a -> m b) -> (Maybe a -> m (Maybe b))
160 maybeMapM _ Nothing  = return Nothing
161 maybeMapM m (Just x) = liftM Just $ m x