module Monad
( module Monad.Maybe
, module Monad.Either
, module Monad.Trans
, Monad ((>>=), (>>), return)
, MonadFail (fail)
, MonadPlus (..)
, (=<<)
, (>=>)
, (<=<)
, forever
, join
, mfilter
, filterM
, mapAndUnzipM
, zipWithM
, zipWithM_
, foldM
, foldM_
, replicateM
, replicateM_
, concatMapM
, concatForM
, guard
, when
, unless
, allM
, anyM
, andM
, orM
, liftM
, liftM2
, liftM3
, liftM4
, liftM5
, ap
, (<$!>)
) where
import Monad.Either
import Monad.Maybe
import Monad.Trans
import Base (IO, seq)
import Control.Applicative (Applicative (pure))
import Data.Function ((.))
import Data.Functor (fmap)
import Data.Traversable (Traversable (traverse))
import Prelude (Bool (..), flip)
#if __GLASGOW_HASKELL__ >= 710
import Control.Monad hiding (fail, (<$!>))
#else
import Control.Monad hiding (fail)
#endif
#if __GLASGOW_HASKELL__ >= 800
import Control.Monad.Fail (MonadFail (..))
#else
import Prelude (Maybe (Nothing), String)
import qualified Prelude as P (fail)
import Text.ParserCombinators.ReadP (ReadP)
import Text.ParserCombinators.ReadPrec (ReadPrec)
#endif
import Containers (Element, NontrivialContainer, toList)
concatMapM :: (Applicative q, Monad m, Traversable m)
=> (a -> q (m b))
-> m a
-> q (m b)
concatMapM f = fmap join . traverse f
concatForM :: (Applicative q, Monad m, Traversable m)
=> m a
-> (a -> q (m b))
-> q (m b)
concatForM = flip concatMapM
(<$!>) :: Monad m => (a -> b) -> m a -> m b
f <$!> m = do
x <- m
let z = f x
z `seq` return z
andM :: (NontrivialContainer f, Element f ~ m Bool, Monad m) => f -> m Bool
andM = go . toList
where
go [] = pure True
go (p:ps) = do
q <- p
if q then go ps else pure False
orM :: (NontrivialContainer f, Element f ~ m Bool, Monad m) => f -> m Bool
orM = go . toList
where
go [] = pure False
go (p:ps) = do
q <- p
if q then pure True else go ps
allM :: (NontrivialContainer f, Monad m) => (Element f -> m Bool) -> f -> m Bool
allM p = go . toList
where
go [] = pure True
go (x:xs) = do
q <- p x
if q then go xs else pure False
anyM :: (NontrivialContainer f, Monad m) => (Element f -> m Bool) -> f -> m Bool
anyM p = go . toList
where
go [] = pure False
go (x:xs) = do
q <- p x
if q then pure True else go xs
#if __GLASGOW_HASKELL__ < 800
class Monad m => MonadFail m where
fail :: String -> m a
instance MonadFail Maybe where
fail _ = Nothing
instance MonadFail [] where
fail _ = []
instance MonadFail IO where
fail = P.fail
instance MonadFail ReadPrec where
fail = P.fail
instance MonadFail ReadP where
fail = P.fail
#endif