{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Control.Monad
(
Functor(fmap)
, Monad((>>=), (>>), return)
, MonadFail(fail)
, MonadPlus(mzero, mplus)
, mapM
, mapM_
, forM
, forM_
, sequence
, sequence_
, (=<<)
, (>=>)
, (<=<)
, forever
, void
, join
, msum
, mfilter
, filterM
, mapAndUnzipM
, zipWithM
, zipWithM_
, foldM
, foldM_
, replicateM
, replicateM_
, guard
, when
, unless
, liftM
, liftM2
, liftM3
, liftM4
, liftM5
, ap
, (<$!>)
) where
import Control.Monad.Fail ( MonadFail(fail) )
import Data.Foldable ( Foldable, sequence_, sequenceA_, msum, mapM_, foldlM, forM_ )
import Data.Functor ( void, (<$>) )
import Data.Traversable ( forM, mapM, traverse, sequence, sequenceA )
import GHC.Base hiding ( mapM, sequence )
import GHC.List ( zipWith, unzip )
import GHC.Num ( (-) )
guard :: (Alternative f) => Bool -> f ()
guard :: Bool -> f ()
guard True = () -> f ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
guard False = f ()
forall (f :: * -> *) a. Alternative f => f a
empty
{-# INLINE filterM #-}
filterM :: (Applicative m) => (a -> m Bool) -> [a] -> m [a]
filterM :: (a -> m Bool) -> [a] -> m [a]
filterM p :: a -> m Bool
p = (a -> m [a] -> m [a]) -> m [a] -> [a] -> m [a]
forall a b. (a -> b -> b) -> b -> [a] -> b
foldr (\ x :: a
x -> (Bool -> [a] -> [a]) -> m Bool -> m [a] -> m [a]
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (\ flg :: Bool
flg -> if Bool
flg then (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) else [a] -> [a]
forall a. a -> a
id) (a -> m Bool
p a
x)) ([a] -> m [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [])
infixr 1 <=<, >=>
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)
f :: a -> m b
f >=> :: (a -> m b) -> (b -> m c) -> a -> m c
>=> g :: b -> m c
g = \x :: a
x -> a -> m b
f a
x m b -> (b -> m c) -> m c
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> m c
g
(<=<) :: Monad m => (b -> m c) -> (a -> m b) -> (a -> m c)
<=< :: (b -> m c) -> (a -> m b) -> a -> m c
(<=<) = ((a -> m b) -> (b -> m c) -> a -> m c)
-> (b -> m c) -> (a -> m b) -> a -> m c
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> m b) -> (b -> m c) -> a -> m c
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
(>=>)
forever :: (Applicative f) => f a -> f b
{-# INLINE forever #-}
forever :: f a -> f b
forever a :: f a
a = let a' :: f b
a' = f a
a f a -> f b -> f b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> f b
a' in f b
forall b. f b
a'
mapAndUnzipM :: (Applicative m) => (a -> m (b,c)) -> [a] -> m ([b], [c])
{-# INLINE mapAndUnzipM #-}
mapAndUnzipM :: (a -> m (b, c)) -> [a] -> m ([b], [c])
mapAndUnzipM f :: a -> m (b, c)
f xs :: [a]
xs = [(b, c)] -> ([b], [c])
forall a b. [(a, b)] -> ([a], [b])
unzip ([(b, c)] -> ([b], [c])) -> m [(b, c)] -> m ([b], [c])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> m (b, c)) -> [a] -> m [(b, c)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> m (b, c)
f [a]
xs
zipWithM :: (Applicative m) => (a -> b -> m c) -> [a] -> [b] -> m [c]
{-# INLINE zipWithM #-}
zipWithM :: (a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM f :: a -> b -> m c
f xs :: [a]
xs ys :: [b]
ys = [m c] -> m [c]
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
sequenceA ((a -> b -> m c) -> [a] -> [b] -> [m c]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith a -> b -> m c
f [a]
xs [b]
ys)
zipWithM_ :: (Applicative m) => (a -> b -> m c) -> [a] -> [b] -> m ()
{-# INLINE zipWithM_ #-}
zipWithM_ :: (a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ f :: a -> b -> m c
f xs :: [a]
xs ys :: [b]
ys = [m c] -> m ()
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Applicative f) =>
t (f a) -> f ()
sequenceA_ ((a -> b -> m c) -> [a] -> [b] -> [m c]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith a -> b -> m c
f [a]
xs [b]
ys)
foldM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b
{-# INLINABLE foldM #-}
{-# SPECIALISE foldM :: (a -> b -> IO a) -> a -> [b] -> IO a #-}
{-# SPECIALISE foldM :: (a -> b -> Maybe a) -> a -> [b] -> Maybe a #-}
foldM :: (b -> a -> m b) -> b -> t a -> m b
foldM = (b -> a -> m b) -> b -> t a -> m b
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldlM
foldM_ :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m ()
{-# INLINABLE foldM_ #-}
{-# SPECIALISE foldM_ :: (a -> b -> IO a) -> a -> [b] -> IO () #-}
{-# SPECIALISE foldM_ :: (a -> b -> Maybe a) -> a -> [b] -> Maybe () #-}
foldM_ :: (b -> a -> m b) -> b -> t a -> m ()
foldM_ f :: b -> a -> m b
f a :: b
a xs :: t a
xs = (b -> a -> m b) -> b -> t a -> m b
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldlM b -> a -> m b
f b
a t a
xs m b -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
replicateM :: (Applicative m) => Int -> m a -> m [a]
{-# INLINABLE replicateM #-}
{-# SPECIALISE replicateM :: Int -> IO a -> IO [a] #-}
{-# SPECIALISE replicateM :: Int -> Maybe a -> Maybe [a] #-}
replicateM :: Int -> m a -> m [a]
replicateM cnt0 :: Int
cnt0 f :: m a
f =
Int -> m [a]
forall t. (Ord t, Num t) => t -> m [a]
loop Int
cnt0
where
loop :: t -> m [a]
loop cnt :: t
cnt
| t
cnt t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= 0 = [a] -> m [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
| Bool
otherwise = (a -> [a] -> [a]) -> m a -> m [a] -> m [a]
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:) m a
f (t -> m [a]
loop (t
cnt t -> t -> t
forall a. Num a => a -> a -> a
- 1))
replicateM_ :: (Applicative m) => Int -> m a -> m ()
{-# INLINABLE replicateM_ #-}
{-# SPECIALISE replicateM_ :: Int -> IO a -> IO () #-}
{-# SPECIALISE replicateM_ :: Int -> Maybe a -> Maybe () #-}
replicateM_ :: Int -> m a -> m ()
replicateM_ cnt0 :: Int
cnt0 f :: m a
f =
Int -> m ()
forall t. (Ord t, Num t) => t -> m ()
loop Int
cnt0
where
loop :: t -> m ()
loop cnt :: t
cnt
| t
cnt t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= 0 = () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Bool
otherwise = m a
f m a -> m () -> m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> t -> m ()
loop (t
cnt t -> t -> t
forall a. Num a => a -> a -> a
- 1)
unless :: (Applicative f) => Bool -> f () -> f ()
{-# INLINABLE unless #-}
{-# SPECIALISE unless :: Bool -> IO () -> IO () #-}
{-# SPECIALISE unless :: Bool -> Maybe () -> Maybe () #-}
unless :: Bool -> f () -> f ()
unless p :: Bool
p s :: f ()
s = if Bool
p then () -> f ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure () else f ()
s
infixl 4 <$!>
(<$!>) :: Monad m => (a -> b) -> m a -> m b
{-# INLINE (<$!>) #-}
f :: a -> b
f <$!> :: (a -> b) -> m a -> m b
<$!> m :: m a
m = do
a
x <- m a
m
let z :: b
z = a -> b
f a
x
b
z b -> m b -> m b
forall a b. a -> b -> b
`seq` b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
z
mfilter :: (MonadPlus m) => (a -> Bool) -> m a -> m a
{-# INLINABLE mfilter #-}
mfilter :: (a -> Bool) -> m a -> m a
mfilter p :: a -> Bool
p ma :: m a
ma = do
a
a <- m a
ma
if a -> Bool
p a
a then a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a else m a
forall (m :: * -> *) a. MonadPlus m => m a
mzero