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