papa-0.7.0: src/Papa/Base/Control/Arrow.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | Arrows.
--
-- The 'Arrow' methods @first@ and @second@ are exported as 'firstA' and
-- 'secondA', and the 'ArrowChoice' methods @left@ and @right@ as 'leftA' and
-- 'rightA', so that this module can be imported with "Papa", whose @first@
-- and @second@ are those of 'Data.Bifunctor.Bifunctor', and whose @left@
-- and @right@ are the names of the sides of a sum.
module Papa.Base.Control.Arrow (
module P,
firstA,
secondA,
leftA,
rightA,
) where
import Control.Arrow as P (
Arrow (arr, (&&&), (***)),
ArrowApply (app),
ArrowChoice ((+++), (|||)),
ArrowLoop (loop),
ArrowPlus ((<+>)),
ArrowZero (zeroArrow),
Kleisli (Kleisli, runKleisli),
)
import qualified Control.Arrow as A
import Data.Either (Either)
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import Papa.Base.Control.Arrow
-- >>> import Hedgehog
-- >>> import Hedgehog.Function (fn, apply)
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genInt = Gen.int (Range.linear (-100) 100)
-- | Send the first component of the input through the arrow, and the
-- second through unchanged. @Arrow@'s @first@.
--
-- ==== __Examples__
--
-- >>> firstA (+ 1) (1 :: Int, 'x')
-- (2,'x')
--
-- >>> check $ withTests 100 $ property $ do f <- forAll (fn genInt); a <- forAll genInt; c <- forAll Gen.alpha; firstA (apply f) (a, c) === first (apply f) (a, c)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
firstA :: (Arrow a) => a b c -> a (b, d) (c, d)
firstA =
A.first
{-# INLINE firstA #-}
-- | Send the second component of the input through the arrow, and the
-- first through unchanged. @Arrow@'s @second@.
--
-- ==== __Examples__
--
-- >>> secondA (+ 1) ('x', 1 :: Int)
-- ('x',2)
--
-- >>> check $ withTests 100 $ property $ do f <- forAll (fn genInt); a <- forAll genInt; c <- forAll Gen.alpha; secondA (apply f) (c, a) === second (apply f) (c, a)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
secondA :: (Arrow a) => a b c -> a (d, b) (d, c)
secondA =
A.second
{-# INLINE secondA #-}
-- | Send a 'Data.Either.Left' input through the arrow, and a 'Data.Either.Right' input through
-- unchanged. @ArrowChoice@'s @left@.
--
-- ==== __Examples__
--
-- >>> leftA (+ 1) (Left 1 :: Either Int Char)
-- Left 2
--
-- >>> check $ withTests 100 $ property $ do f <- forAll (fn genInt); e <- forAll (Gen.either genInt Gen.alpha); leftA (apply f) e === first (apply f) e
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
leftA :: (ArrowChoice a) => a b c -> a (Either b d) (Either c d)
leftA =
A.left
{-# INLINE leftA #-}
-- | Send a 'Data.Either.Right' input through the arrow, and a 'Data.Either.Left' input through
-- unchanged. @ArrowChoice@'s @right@.
--
-- ==== __Examples__
--
-- >>> rightA (+ 1) (Right 1 :: Either Char Int)
-- Right 2
--
-- >>> check $ withTests 100 $ property $ do f <- forAll (fn genInt); e <- forAll (Gen.either Gen.alpha genInt); rightA (apply f) e === second (apply f) e
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
rightA :: (ArrowChoice a) => a b c -> a (Either d b) (Either d c)
rightA =
A.right
{-# INLINE rightA #-}