FIX BUILD with GHC 6.4.x
[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         , mapAndUnzipM, mapAndUnzip3M, mapAndUnzip4M
13         , mapAccumLM
14         , mapSndM
15         , concatMapM
16         , anyM
17         , foldlM, foldrM
18         ) where
19
20 ----------------------------------------------------------------------------------------
21 -- Detection of available libraries
22 ----------------------------------------------------------------------------------------
23
24 #if __GLASGOW_HASKELL__ >= 606
25 #define HAVE_APPLICATIVE 1
26 #else
27 #define HAVE_APPLICATIVE 0
28 #endif
29 -- we don't depend on MTL for now
30 #define HAVE_MTL 0
31
32 ----------------------------------------------------------------------------------------
33 -- Imports
34 ----------------------------------------------------------------------------------------
35
36 #if HAVE_APPLICATIVE
37 import Control.Applicative
38 #endif
39 #if HAVE_MTL
40 import Control.Monad.Trans
41 #endif
42 import Control.Monad
43 import Control.Monad.Fix
44
45 ----------------------------------------------------------------------------------------
46 -- Applicative
47 ----------------------------------------------------------------------------------------
48
49 #if !HAVE_APPLICATIVE
50
51 class Functor f => Applicative f where
52     pure  :: a -> f a
53     (<*>) :: f (a -> b) -> f a -> f b
54
55 (<$>) :: Functor f => (a -> b) -> (f a -> f b)
56 (<$>) = fmap
57
58 infixl 4 <$>
59 infixl 4 <*>
60
61 instance Applicative IO where
62         pure = return
63         (<*>) = ap
64
65 #endif
66
67 ----------------------------------------------------------------------------------------
68 -- MTL
69 ----------------------------------------------------------------------------------------
70
71 #if !HAVE_MTL
72
73 class Monad m => MonadIO m where
74     liftIO :: IO a -> m a
75
76 #endif
77
78 ----------------------------------------------------------------------------------------
79 -- Common functions
80 --  These are used throught the compiler
81 ----------------------------------------------------------------------------------------
82
83 -- | mapAndUnzipM for triples
84 mapAndUnzip3M :: Monad m => (a -> m (b,c,d)) -> [a] -> m ([b],[c],[d])
85 mapAndUnzip3M _ []     = return ([],[],[])
86 mapAndUnzip3M f (x:xs) = do
87     (r1,  r2,  r3)  <- f x
88     (rs1, rs2, rs3) <- mapAndUnzip3M f xs
89     return (r1:rs1, r2:rs2, r3:rs3)
90
91 mapAndUnzip4M :: Monad m => (a -> m (b,c,d,e)) -> [a] -> m ([b],[c],[d],[e])
92 mapAndUnzip4M _ []     = return ([],[],[],[])
93 mapAndUnzip4M f (x:xs) = do
94     (r1,  r2,  r3,  r4)  <- f x
95     (rs1, rs2, rs3, rs4) <- mapAndUnzip4M f xs
96     return (r1:rs1, r2:rs2, r3:rs3, r4:rs4)
97
98 -- | Monadic version of mapAccumL
99 mapAccumLM :: Monad m
100             => (acc -> x -> m (acc, y)) -- ^ combining funcction
101             -> acc                      -- ^ initial state
102             -> [x]                      -- ^ inputs
103             -> m (acc, [y])             -- ^ final state, outputs
104 mapAccumLM _ s []     = return (s, [])
105 mapAccumLM f s (x:xs) = do
106     (s1, x')  <- f s x
107     (s2, xs') <- mapAccumLM f s1 xs
108     return    (s2, x' : xs')
109
110 -- | Monadic version of mapSnd
111 mapSndM :: Monad m => (b -> m c) -> [(a,b)] -> m [(a,c)]
112 mapSndM _ []         = return []
113 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
114
115 -- | Monadic version of concatMap
116 concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
117 concatMapM f xs = liftM concat (mapM f xs)
118
119 -- | Monadic version of 'any', aborts the computation at the first False value
120 anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
121 anyM _ []     = return False
122 anyM f (x:xs) = do b <- f x
123                    if b then return True 
124                         else anyM f xs
125
126 -- | Monadic version of foldl
127 foldlM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
128 foldlM = foldM
129
130 -- | Monadic version of foldr
131 foldrM        :: (Monad m) => (b -> a -> m a) -> a -> [b] -> m a
132 foldrM _ z []     = return z
133 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }