merge GHC HEAD
[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         , ID, runID
13         
14         , liftIO1, liftIO2, liftIO3, liftIO4
15
16         , zipWith3M        
17         , mapAndUnzipM, mapAndUnzip3M, mapAndUnzip4M
18         , mapAccumLM
19         , mapSndM
20         , concatMapM
21         , mapMaybeM
22         , fmapMaybeM, fmapEitherM
23         , anyM, allM
24         , foldlM, foldlM_, foldrM
25         , maybeMapM
26         ) where
27
28 import Outputable 
29
30 -------------------------------------------------------------------------------
31 -- Detection of available libraries
32 -------------------------------------------------------------------------------
33
34 -- we don't depend on MTL for now
35 #define HAVE_MTL 0
36
37 -------------------------------------------------------------------------------
38 -- Imports
39 -------------------------------------------------------------------------------
40
41 import Maybes
42
43 import Control.Applicative
44 #if HAVE_MTL
45 import Control.Monad.Trans
46 #endif
47 import Control.Monad
48 import Control.Monad.Fix
49
50 -------------------------------------------------------------------------------
51 -- The ID monad
52 -------------------------------------------------------------------------------
53
54 newtype ID a = ID a
55 instance Monad ID where
56   return x     = ID x
57   (ID x) >>= f = f x
58   _ >> y       = y
59   fail s       = panic s
60
61 runID :: ID a -> a
62 runID (ID x) = x
63
64 -------------------------------------------------------------------------------
65 -- MTL
66 -------------------------------------------------------------------------------
67
68 #if !HAVE_MTL
69
70 class Monad m => MonadIO m where
71     liftIO :: IO a -> m a
72
73 instance MonadIO IO where liftIO = id
74 #endif
75
76 -------------------------------------------------------------------------------
77 -- Lift combinators
78 --  These are used throughout the compiler
79 -------------------------------------------------------------------------------
80
81 -- | Lift an 'IO' operation with 1 argument into another monad
82 liftIO1 :: MonadIO m => (a -> IO b) -> a -> m b
83 liftIO1 = (.) liftIO
84
85 -- | Lift an 'IO' operation with 2 arguments into another monad
86 liftIO2 :: MonadIO m => (a -> b -> IO c) -> a -> b -> m c
87 liftIO2 = ((.).(.)) liftIO
88
89 -- | Lift an 'IO' operation with 3 arguments into another monad
90 liftIO3 :: MonadIO m => (a -> b -> c -> IO d) -> a -> b -> c -> m d
91 liftIO3 = ((.).((.).(.))) liftIO
92
93 -- | Lift an 'IO' operation with 4 arguments into another monad
94 liftIO4 :: MonadIO m => (a -> b -> c -> d -> IO e) -> a -> b -> c -> d -> m e
95 liftIO4 = (((.).(.)).((.).(.))) liftIO
96
97 -------------------------------------------------------------------------------
98 -- Common functions
99 --  These are used throughout the compiler
100 -------------------------------------------------------------------------------
101
102 zipWith3M :: Monad m => (a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
103 zipWith3M _ []     _      _      = return []
104 zipWith3M _ _      []     _      = return []
105 zipWith3M _ _      _      []     = return []
106 zipWith3M f (x:xs) (y:ys) (z:zs) 
107   = do { r  <- f x y z
108        ; rs <- zipWith3M f xs ys zs
109        ; return $ r:rs
110        }
111
112 -- | mapAndUnzipM for triples
113 mapAndUnzip3M :: Monad m => (a -> m (b,c,d)) -> [a] -> m ([b],[c],[d])
114 mapAndUnzip3M _ []     = return ([],[],[])
115 mapAndUnzip3M f (x:xs) = do
116     (r1,  r2,  r3)  <- f x
117     (rs1, rs2, rs3) <- mapAndUnzip3M f xs
118     return (r1:rs1, r2:rs2, r3:rs3)
119
120 mapAndUnzip4M :: Monad m => (a -> m (b,c,d,e)) -> [a] -> m ([b],[c],[d],[e])
121 mapAndUnzip4M _ []     = return ([],[],[],[])
122 mapAndUnzip4M f (x:xs) = do
123     (r1,  r2,  r3,  r4)  <- f x
124     (rs1, rs2, rs3, rs4) <- mapAndUnzip4M f xs
125     return (r1:rs1, r2:rs2, r3:rs3, r4:rs4)
126
127 -- | Monadic version of mapAccumL
128 mapAccumLM :: Monad m
129             => (acc -> x -> m (acc, y)) -- ^ combining funcction
130             -> acc                      -- ^ initial state
131             -> [x]                      -- ^ inputs
132             -> m (acc, [y])             -- ^ final state, outputs
133 mapAccumLM _ s []     = return (s, [])
134 mapAccumLM f s (x:xs) = do
135     (s1, x')  <- f s x
136     (s2, xs') <- mapAccumLM f s1 xs
137     return    (s2, x' : xs')
138
139 -- | Monadic version of mapSnd
140 mapSndM :: Monad m => (b -> m c) -> [(a,b)] -> m [(a,c)]
141 mapSndM _ []         = return []
142 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
143
144 -- | Monadic version of concatMap
145 concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
146 concatMapM f xs = liftM concat (mapM f xs)
147
148 -- | Monadic version of mapMaybe
149 mapMaybeM :: (Monad m) => (a -> m (Maybe b)) -> [a] -> m [b]
150 mapMaybeM f = liftM catMaybes . mapM f
151
152 -- | Monadic version of fmap
153 fmapMaybeM :: (Monad m) => (a -> m b) -> Maybe a -> m (Maybe b)
154 fmapMaybeM _ Nothing  = return Nothing
155 fmapMaybeM f (Just x) = f x >>= (return . Just)
156
157 -- | Monadic version of fmap
158 fmapEitherM :: Monad m => (a -> m b) -> (c -> m d) -> Either a c -> m (Either b d)
159 fmapEitherM fl _ (Left  a) = fl a >>= (return . Left)
160 fmapEitherM _ fr (Right b) = fr b >>= (return . Right)
161
162 -- | Monadic version of 'any', aborts the computation at the first @True@ value
163 anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
164 anyM _ []     = return False
165 anyM f (x:xs) = do b <- f x
166                    if b then return True 
167                         else anyM f xs
168
169 -- | Monad version of 'all', aborts the computation at the first @False@ value
170 allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
171 allM _ []     = return True
172 allM f (b:bs) = (f b) >>= (\bv -> if bv then allM f bs else return False)
173
174 -- | Monadic version of foldl
175 foldlM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
176 foldlM = foldM
177
178 -- | Monadic version of foldl that discards its result
179 foldlM_ :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m ()
180 foldlM_ = foldM_
181
182 -- | Monadic version of foldr
183 foldrM        :: (Monad m) => (b -> a -> m a) -> a -> [b] -> m a
184 foldrM _ z []     = return z
185 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }
186
187 -- | Monadic version of fmap specialised for Maybe
188 maybeMapM :: Monad m => (a -> m b) -> (Maybe a -> m (Maybe b))
189 maybeMapM _ Nothing  = return Nothing
190 maybeMapM m (Just x) = liftM Just $ m x