add final newlines
[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 -- we don't depend on MTL for now
28 #define HAVE_MTL 0
29
30 ----------------------------------------------------------------------------------------
31 -- Imports
32 ----------------------------------------------------------------------------------------
33
34 import Maybes
35
36 import Control.Applicative
37 #if HAVE_MTL
38 import Control.Monad.Trans
39 #endif
40 import Control.Monad
41 import Control.Monad.Fix
42
43 ----------------------------------------------------------------------------------------
44 -- MTL
45 ----------------------------------------------------------------------------------------
46
47 #if !HAVE_MTL
48
49 class Monad m => MonadIO m where
50     liftIO :: IO a -> m a
51
52 instance MonadIO IO where liftIO = id
53 #endif
54
55 ----------------------------------------------------------------------------------------
56 -- Lift combinators
57 --  These are used throughout the compiler
58 ----------------------------------------------------------------------------------------
59
60 -- | Lift an 'IO' operation with 1 argument into another monad
61 liftIO1 :: MonadIO m => (a -> IO b) -> a -> m b
62 liftIO1 = (.) liftIO
63
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
67
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
71
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
75
76 ----------------------------------------------------------------------------------------
77 -- Common functions
78 --  These are used throughout the compiler
79 ----------------------------------------------------------------------------------------
80
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
85     (r1,  r2,  r3)  <- f x
86     (rs1, rs2, rs3) <- mapAndUnzip3M f xs
87     return (r1:rs1, r2:rs2, r3:rs3)
88
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)
95
96 -- | Monadic version of mapAccumL
97 mapAccumLM :: Monad m
98             => (acc -> x -> m (acc, y)) -- ^ combining funcction
99             -> acc                      -- ^ initial state
100             -> [x]                      -- ^ inputs
101             -> m (acc, [y])             -- ^ final state, outputs
102 mapAccumLM _ s []     = return (s, [])
103 mapAccumLM f s (x:xs) = do
104     (s1, x')  <- f s x
105     (s2, xs') <- mapAccumLM f s1 xs
106     return    (s2, x' : xs')
107
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) }
112
113 -- | Monadic version of concatMap
114 concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
115 concatMapM f xs = liftM concat (mapM f xs)
116
117 -- | Monadic version of mapMaybe
118 mapMaybeM :: (Monad m) => (a -> m (Maybe b)) -> [a] -> m [b]
119 mapMaybeM f = liftM catMaybes . mapM f
120
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 
126                         else anyM f xs
127
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)
132
133 -- | Monadic version of foldl
134 foldlM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
135 foldlM = foldM
136
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 }