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