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