X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Control%2FMonad.hs;h=a1d7d267b12bd3aae60d76cbfd2d0b8ee39ae7ca;hb=45a54fd09e281c1b4eef3b610e90ea04e1def9b2;hp=32f04036f2b685466f663800af74e4c7c5978836;hpb=e4c92ded9108b12c7996b5769fa2ee5914f42f1d;p=ghc-base.git diff --git a/Control/Monad.hs b/Control/Monad.hs index 32f0403..a1d7d26 100644 --- a/Control/Monad.hs +++ b/Control/Monad.hs @@ -1,4 +1,4 @@ -{-# OPTIONS_GHC -fno-implicit-prelude #-} +{-# OPTIONS_GHC -XNoImplicitPrelude #-} ----------------------------------------------------------------------------- -- | -- Module : Control.Monad @@ -20,9 +20,9 @@ module Control.Monad , Monad((>>=), (>>), return, fail) , MonadPlus ( -- class context: Monad - mzero -- :: (MonadPlus m) => m a - , mplus -- :: (MonadPlus m) => m a -> m a -> m a - ) + mzero -- :: (MonadPlus m) => m a + , mplus -- :: (MonadPlus m) => m a -> m a -> m a + ) -- * Functions -- ** Naming conventions @@ -37,6 +37,9 @@ module Control.Monad , sequence -- :: (Monad m) => [m a] -> m [a] , sequence_ -- :: (Monad m) => [m a] -> m () , (=<<) -- :: (Monad m) => (a -> m b) -> m a -> m b + , (>=>) -- :: (Monad m) => (a -> m b) -> (b -> m c) -> (a -> m c) + , (<=<) -- :: (Monad m) => (b -> m c) -> (a -> m b) -> (a -> m c) + , forever -- :: (Monad m) => m a -> m b -- ** Generalisations of list functions @@ -85,15 +88,15 @@ infixr 1 =<< -- | Same as '>>=', but with the arguments interchanged. {-# SPECIALISE (=<<) :: (a -> [b]) -> [a] -> [b] #-} (=<<) :: Monad m => (a -> m b) -> m a -> m b -f =<< x = x >>= f +f =<< x = x >>= f -- | Evaluate each action in the sequence from left to right, -- and collect the results. sequence :: Monad m => [m a] -> m [a] {-# INLINE sequence #-} sequence ms = foldr k (return []) ms - where - k m m' = do { x <- m; xs <- m'; return (x:xs) } + where + k m m' = do { x <- m; xs <- m'; return (x:xs) } -- | Evaluate each action in the sequence from left to right, -- and ignore the results. @@ -123,9 +126,9 @@ class Monad m => MonadPlus m where -- > mzero >>= f = mzero -- > v >> mzero = mzero -- - -- (but the instance for 'System.IO.IO' defined in "Control.Monad.Error" - -- does not satisfy the second one). - mzero :: m a + -- (but the instance for 'System.IO.IO' defined in Control.Monad.Error + -- in the mtl package does not satisfy the second one). + mzero :: m a -- | an associative operation mplus :: m a -> m a -> m a @@ -173,6 +176,20 @@ msum :: MonadPlus m => [m a] -> m a {-# INLINE msum #-} msum = foldr mplus mzero +infixr 1 <=<, >=> + +-- | Left-to-right Kleisli composition of monads. +(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c) +f >=> g = \x -> f x >>= g + +-- | Right-to-left Kleisli composition of monads. '(>=>)', with the arguments flipped +(<=<) :: Monad m => (b -> m c) -> (a -> m b) -> (a -> m c) +(<=<) = flip (>=>) + +-- | @'forever' act@ repeats the action infinitely. +forever :: (Monad m) => m a -> m b +forever a = a >> forever a + -- ----------------------------------------------------------------------------- -- Other monad functions @@ -202,15 +219,15 @@ the list arguments. This could be an issue where '(>>)' and the `folded function' are not commutative. -> foldM f a1 [x1, x2, ..., xm ] +> foldM f a1 [x1, x2, ..., xm ] == -> do -> a2 <- f a1 x1 -> a3 <- f a2 x2 -> ... -> f am xm +> do +> a2 <- f a1 x1 +> a3 <- f a2 x2 +> ... +> f am xm If right-to-left evaluation is required, the input list should be reversed. -} @@ -234,7 +251,7 @@ replicateM_ n x = sequence_ (replicate n x) {- | Conditional execution of monadic expressions. For example, -> when debug (putStr "Debugging\n") +> when debug (putStr "Debugging\n") will output the string @Debugging\\n@ if the Boolean value @debug@ is 'True', and otherwise do nothing. @@ -255,8 +272,8 @@ liftM f m1 = do { x1 <- m1; return (f x1) } -- | Promote a function to a monad, scanning the monadic arguments from -- left to right. For example, -- --- > liftM2 (+) [0,1] [0,2] = [0,2,1,3] --- > liftM2 (+) (Just 1) Nothing = Nothing +-- > liftM2 (+) [0,1] [0,2] = [0,2,1,3] +-- > liftM2 (+) (Just 1) Nothing = Nothing -- liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) } @@ -279,17 +296,18 @@ liftM5 f m1 m2 m3 m4 m5 = do { x1 <- m1; x2 <- m2; x3 <- m3; x4 <- m4; x5 <- m5; {- | In many situations, the 'liftM' operations can be replaced by uses of 'ap', which promotes function application. -> return f `ap` x1 `ap` ... `ap` xn +> return f `ap` x1 `ap` ... `ap` xn is equivalent to -> liftMn f x1 x2 ... xn +> liftMn f x1 x2 ... xn -} ap :: (Monad m) => m (a -> b) -> m a -> m b ap = liftM2 id + {- $naming The functions in this library use the following naming conventions: