Provide default MonadIO instance for IO.
[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, allM
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 instance MonadIO IO where liftIO = id
77 #endif
78
79 ----------------------------------------------------------------------------------------
80 -- Common functions
81 --  These are used throught the compiler
82 ----------------------------------------------------------------------------------------
83
84 -- | mapAndUnzipM for triples
85 mapAndUnzip3M :: Monad m => (a -> m (b,c,d)) -> [a] -> m ([b],[c],[d])
86 mapAndUnzip3M _ []     = return ([],[],[])
87 mapAndUnzip3M f (x:xs) = do
88     (r1,  r2,  r3)  <- f x
89     (rs1, rs2, rs3) <- mapAndUnzip3M f xs
90     return (r1:rs1, r2:rs2, r3:rs3)
91
92 mapAndUnzip4M :: Monad m => (a -> m (b,c,d,e)) -> [a] -> m ([b],[c],[d],[e])
93 mapAndUnzip4M _ []     = return ([],[],[],[])
94 mapAndUnzip4M f (x:xs) = do
95     (r1,  r2,  r3,  r4)  <- f x
96     (rs1, rs2, rs3, rs4) <- mapAndUnzip4M f xs
97     return (r1:rs1, r2:rs2, r3:rs3, r4:rs4)
98
99 -- | Monadic version of mapAccumL
100 mapAccumLM :: Monad m
101             => (acc -> x -> m (acc, y)) -- ^ combining funcction
102             -> acc                      -- ^ initial state
103             -> [x]                      -- ^ inputs
104             -> m (acc, [y])             -- ^ final state, outputs
105 mapAccumLM _ s []     = return (s, [])
106 mapAccumLM f s (x:xs) = do
107     (s1, x')  <- f s x
108     (s2, xs') <- mapAccumLM f s1 xs
109     return    (s2, x' : xs')
110
111 -- | Monadic version of mapSnd
112 mapSndM :: Monad m => (b -> m c) -> [(a,b)] -> m [(a,c)]
113 mapSndM _ []         = return []
114 mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
115
116 -- | Monadic version of concatMap
117 concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
118 concatMapM f xs = liftM concat (mapM f xs)
119
120 -- | Monadic version of 'any', aborts the computation at the first @True@ value
121 anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
122 anyM _ []     = return False
123 anyM f (x:xs) = do b <- f x
124                    if b then return True 
125                         else anyM f xs
126
127 -- | Monad version of 'all', aborts the computation at the first @False@ value
128 allM :: Monad m => (a -> m Bool) -> [a] -> m Bool
129 allM _ []     = return True
130 allM f (b:bs) = (f b) >>= (\bv -> if bv then allM f bs else return False)
131
132 -- | Monadic version of foldl
133 foldlM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
134 foldlM = foldM
135
136 -- | Monadic version of foldr
137 foldrM        :: (Monad m) => (b -> a -> m a) -> a -> [b] -> m a
138 foldrM _ z []     = return z
139 foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }