]> Git — Sourcephile - haskell/symantic.git/blob - TFHOE/Lib/Control/Monad.hs
init
[haskell/symantic.git] / TFHOE / Lib / Control / Monad.hs
1 module TFHOE.Lib.Control.Monad where
2
3 import Control.Applicative (Applicative(..))
4 import Control.Monad (Monad(..), (=<<), liftM2, liftM3, liftM4, join)
5 import Data.Bool
6 import Data.Maybe (Maybe(..), maybe)
7
8 -- * 'Monad'ic utilities
9
10 -- | Perform some operation on 'Just', given the field inside the 'Just'.
11 whenJust :: Applicative m => Maybe a -> (a -> m ()) -> m ()
12 whenJust mg f = maybe (pure ()) f mg
13
14 -- | Like 'when', but where the test can be 'Monad'-ic.
15 whenM :: Monad m => m Bool -> m () -> m ()
16 whenM b t = ifM b t (return ())
17
18 -- | Like 'unless', but where the test can be 'Monad'-ic.
19 unlessM :: Monad m => m Bool -> m () -> m ()
20 unlessM b = ifM b (return ())
21
22 -- | Like @if@, but where the test can be 'Monad'-ic.
23 ifM :: Monad m => m Bool -> m a -> m a -> m a
24 ifM b t f = do b' <- b; if b' then t else f
25
26 -- | Like 'liftM' but 'join' the result of the lifted function.
27 liftMJoin :: Monad m => (a -> m b) -> m a -> m b
28 liftMJoin = (=<<)
29
30 -- | Like 'liftM2' but 'join' the result of the lifted function.
31 liftM2Join :: Monad m => (a -> b -> m c) -> m a -> m b -> m c
32 liftM2Join f ma mb = join (liftM2 f ma mb)
33
34 -- | Like 'liftM3' but 'join' the result of the lifted function.
35 liftM3Join :: Monad m => (a -> b -> c -> m d) -> m a -> m b -> m c -> m d
36 liftM3Join f ma mb mc = join (liftM3 f ma mb mc)
37
38 -- | Like 'liftM3' but 'join' the result of the lifted function.
39 liftM4Join :: Monad m => (a -> b -> c -> d -> m e) -> m a -> m b -> m c -> m d -> m e
40 liftM4Join f ma mb mc md = join (liftM4 f ma mb mc md)