packages feed

papa-0.7.0: src/Papa/Base/Control/Monad.hs

{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}

-- | Monads.
--
-- 'MonadFail' is exported without its method @fail@, which throws in
-- 'Prelude.IO'. A failing pattern in @do@ notation still uses it.
--
-- 'MonadFix' is exported without its method @mfix@, which throws for
-- base's 'Maybe', list and 'Data.Either.Either' instances, and for free's,
-- where the function forces its own result. @mdo@ and @rec@ notation
-- (@RecursiveDo@) still use it.
module Papa.Base.Control.Monad (
  Functor (fmap),
  Monad ((>>=), return, (>>)),
  MonadFail,
  MonadFix,
  MonadIO (liftIO),
  MonadPlus (mzero, mplus),
  MonadTrans (lift),
  MonadZip (mzip, mzipWith, munzip),
  ap,
  forever,
  liftM,
  liftM2,
  msum,
  mfilter,
  filterM,
  foldM,
  foldM_,
  replicateM,
  replicateM_,
  guard,
  when,
  unless,
  zipWithM,
  zipWithM_,
  (<$!>),
  (>=>),
  (<=<),
  anyM,
  allM,
  findM,
) where

import Control.Monad (
  Functor (fmap),
  Monad (return, (>>), (>>=)),
  MonadFail,
  MonadPlus (mplus, mzero),
  ap,
  filterM,
  foldM,
  foldM_,
  forever,
  guard,
  liftM,
  liftM2,
  mfilter,
  msum,
  replicateM,
  replicateM_,
  unless,
  when,
  zipWithM,
  zipWithM_,
  (<$!>),
  (<=<),
  (>=>),
 )
import Control.Monad.Fix (
  MonadFix,
 )
import Control.Monad.IO.Class (
  MonadIO (liftIO),
 )
import Control.Monad.Trans.Class (
  MonadTrans (lift),
 )
import Control.Monad.Zip (
  MonadZip (munzip, mzip, mzipWith),
 )
import Data.Bool (Bool (False, True), bool)
import Data.Maybe (Maybe (Just, Nothing))

-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import Hedgehog
-- >>> import Hedgehog.Function (Fn, fn, apply)
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genInts = Gen.list (Range.linear 0 20) (Gen.int (Range.linear (-100) 100))
-- >>> let genP = fn Gen.bool :: Gen (Fn Int Bool)

-- | Whether any element satisfies a monadic predicate. Stops at the first
-- element that does.
--
-- ==== __Examples__
--
-- >>> anyM (\x -> Just (x > 2)) [1, 2, 3 :: Int]
-- Just True
--
-- >>> anyM (\x -> if x > 2 then Nothing else Just False) [1, 2 :: Int]
-- Just False
--
-- >>> anyM (\_ -> Nothing) ([] :: [Int])
-- Just False
--
-- >>> check $ withTests 100 $ property $ do xs <- forAll genInts; p <- forAll genP; anyM (Just . apply p) xs === Just (any (apply p) xs)
-- ...passed 100 tests.
-- True
--
-- @since 0.1.0
anyM :: (Monad m) => (a -> m Bool) -> [a] -> m Bool
anyM _ [] =
  return False
anyM f (a : as) =
  f a >>= bool (anyM f as) (return True)
{-# INLINEABLE anyM #-}

-- | Whether every element satisfies a monadic predicate. Stops at the first
-- element that does not.
--
-- ==== __Examples__
--
-- >>> allM (\x -> Just (x > 0)) [1, 2, 3 :: Int]
-- Just True
--
-- >>> allM (\_ -> Nothing) ([] :: [Int])
-- Just True
--
-- >>> check $ withTests 100 $ property $ do xs <- forAll genInts; p <- forAll genP; allM (Just . apply p) xs === Just (all (apply p) xs)
-- ...passed 100 tests.
-- True
--
-- @since 0.1.0
allM :: (Monad m) => (a -> m Bool) -> [a] -> m Bool
allM _ [] =
  return True
allM f (a : as) =
  f a >>= bool (return False) (allM f as)
{-# INLINEABLE allM #-}

-- | The first element that satisfies a monadic predicate. Stops there.
--
-- ==== __Examples__
--
-- >>> findM (\x -> Just (x > 1)) [1, 2, 3 :: Int]
-- Just (Just 2)
--
-- >>> findM (\_ -> Just False) [1, 2, 3 :: Int]
-- Just Nothing
--
-- >>> check $ withTests 100 $ property $ do xs <- forAll genInts; p <- forAll genP; findM (Just . apply p) xs === Just (find (apply p) xs)
-- ...passed 100 tests.
-- True
--
-- @since 0.1.0
findM :: (Monad m) => (a -> m Bool) -> [a] -> m (Maybe a)
findM _ [] =
  return Nothing
findM f (x : xs) =
  f x >>= bool (findM f xs) (return (Just x))
{-# INLINEABLE findM #-}