{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wno-inline-rule-shadowing #-}
    -- The RULES for the methods of class Arrow may never fire
    -- e.g. compose/arr;  see Trac #10528

-----------------------------------------------------------------------------
-- |
-- Module      :  Control.Arrow
-- Copyright   :  (c) Ross Paterson 2002
-- License     :  BSD-style (see the LICENSE file in the distribution)
--
-- Maintainer  :  libraries@haskell.org
-- Stability   :  provisional
-- Portability :  portable
--
-- Basic arrow definitions, based on
--
--  * /Generalising Monads to Arrows/, by John Hughes,
--    /Science of Computer Programming/ 37, pp67-111, May 2000.
--
-- plus a couple of definitions ('returnA' and 'loop') from
--
--  * /A New Notation for Arrows/, by Ross Paterson, in /ICFP 2001/,
--    Firenze, Italy, pp229-240.
--
-- These papers and more information on arrows can be found at
-- <http://www.haskell.org/arrows/>.

module Control.Arrow (
    -- * Arrows
    Arrow(..), Kleisli(..),
    -- ** Derived combinators
    returnA,
    (^>>), (>>^),
    (>>>), (<<<), -- reexported
    -- ** Right-to-left variants
    (<<^), (^<<),
    -- * Monoid operations
    ArrowZero(..), ArrowPlus(..),
    -- * Conditionals
    ArrowChoice(..),
    -- * Arrow application
    ArrowApply(..), ArrowMonad(..), leftApp,
    -- * Feedback
    ArrowLoop(..)
    ) where

import Data.Tuple ( fst, snd, uncurry )
import Data.Either
import Control.Monad.Fix
import Control.Category
import GHC.Base hiding ( (.), id )

infixr 5 <+>
infixr 3 ***
infixr 3 &&&
infixr 2 +++
infixr 2 |||
infixr 1 ^>>, >>^
infixr 1 ^<<, <<^

-- | The basic arrow class.
--
-- Instances should satisfy the following laws:
--
--  * @'arr' id = 'id'@
--
--  * @'arr' (f >>> g) = 'arr' f >>> 'arr' g@
--
--  * @'first' ('arr' f) = 'arr' ('first' f)@
--
--  * @'first' (f >>> g) = 'first' f >>> 'first' g@
--
--  * @'first' f >>> 'arr' 'fst' = 'arr' 'fst' >>> f@
--
--  * @'first' f >>> 'arr' ('id' *** g) = 'arr' ('id' *** g) >>> 'first' f@
--
--  * @'first' ('first' f) >>> 'arr' assoc = 'arr' assoc >>> 'first' f@
--
-- where
--
-- > assoc ((a,b),c) = (a,(b,c))
--
-- The other combinators have sensible default definitions,
-- which may be overridden for efficiency.

class Category a => Arrow a where
    {-# MINIMAL arr, (first | (***)) #-}

    -- | Lift a function to an arrow.
    arr :: (b -> c) -> a b c

    -- | Send the first component of the input through the argument
    --   arrow, and copy the rest unchanged to the output.
    first :: a b c -> a (b,d) (c,d)
    first = (a b c -> a d d -> a (b, d) (c, d)
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** a d d
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id)

    -- | A mirror image of 'first'.
    --
    --   The default definition may be overridden with a more efficient
    --   version if desired.
    second :: a b c -> a (d,b) (d,c)
    second = (a d d
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id a d d -> a b c -> a (d, b) (d, c)
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
***)

    -- | Split the input between the two argument arrows and combine
    --   their output.  Note that this is in general not a functor.
    --
    --   The default definition may be overridden with a more efficient
    --   version if desired.
    (***) :: a b c -> a b' c' -> a (b,b') (c,c')
    f :: a b c
f *** g :: a b' c'
g = a b c -> a (b, b') (c, b')
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first a b c
f a (b, b') (c, b') -> a (c, b') (c, c') -> a (b, b') (c, c')
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ((c, b') -> (b', c)) -> a (c, b') (b', c)
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (c, b') -> (b', c)
forall b a. (b, a) -> (a, b)
swap a (c, b') (b', c) -> a (b', c) (c, c') -> a (c, b') (c, c')
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a b' c' -> a (b', c) (c', c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first a b' c'
g a (b', c) (c', c) -> a (c', c) (c, c') -> a (b', c) (c, c')
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ((c', c) -> (c, c')) -> a (c', c) (c, c')
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (c', c) -> (c, c')
forall b a. (b, a) -> (a, b)
swap
      where swap :: (b, a) -> (a, b)
swap ~(x :: b
x,y :: a
y) = (a
y,b
x)

    -- | Fanout: send the input to both argument arrows and combine
    --   their output.
    --
    --   The default definition may be overridden with a more efficient
    --   version if desired.
    (&&&) :: a b c -> a b c' -> a b (c,c')
    f :: a b c
f &&& g :: a b c'
g = (b -> (b, b)) -> a b (b, b)
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (\b :: b
b -> (b
b,b
b)) a b (b, b) -> a (b, b) (c, c') -> a b (c, c')
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a b c
f a b c -> a b c' -> a (b, b) (c, c')
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** a b c'
g

{-# RULES
"compose/arr"   forall f g .
                (arr f) . (arr g) = arr (f . g)
"first/arr"     forall f .
                first (arr f) = arr (first f)
"second/arr"    forall f .
                second (arr f) = arr (second f)
"product/arr"   forall f g .
                arr f *** arr g = arr (f *** g)
"fanout/arr"    forall f g .
                arr f &&& arr g = arr (f &&& g)
"compose/first" forall f g .
                (first f) . (first g) = first (f . g)
"compose/second" forall f g .
                (second f) . (second g) = second (f . g)
 #-}

-- Ordinary functions are arrows.

-- | @since 2.01
instance Arrow (->) where
    arr :: (b -> c) -> b -> c
arr f :: b -> c
f = b -> c
f
--  (f *** g) ~(x,y) = (f x, g y)
--  sorry, although the above defn is fully H'98, nhc98 can't parse it.
    *** :: (b -> c) -> (b' -> c') -> (b, b') -> (c, c')
(***) f :: b -> c
f g :: b' -> c'
g ~(x :: b
x,y :: b'
y) = (b -> c
f b
x, b' -> c'
g b'
y)

-- | Kleisli arrows of a monad.
newtype Kleisli m a b = Kleisli { Kleisli m a b -> a -> m b
runKleisli :: a -> m b }

-- | @since 3.0
instance Monad m => Category (Kleisli m) where
    id :: Kleisli m a a
id = (a -> m a) -> Kleisli m a a
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
    (Kleisli f :: b -> m c
f) . :: Kleisli m b c -> Kleisli m a b -> Kleisli m a c
. (Kleisli g :: a -> m b
g) = (a -> m c) -> Kleisli m a c
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (\b :: a
b -> a -> m b
g a
b m b -> (b -> m c) -> m c
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> m c
f)

-- | @since 2.01
instance Monad m => Arrow (Kleisli m) where
    arr :: (b -> c) -> Kleisli m b c
arr f :: b -> c
f = (b -> m c) -> Kleisli m b c
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (c -> m c
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> m c) -> (b -> c) -> b -> m c
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. b -> c
f)
    first :: Kleisli m b c -> Kleisli m (b, d) (c, d)
first (Kleisli f :: b -> m c
f) = ((b, d) -> m (c, d)) -> Kleisli m (b, d) (c, d)
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (\ ~(b :: b
b,d :: d
d) -> b -> m c
f b
b m c -> (c -> m (c, d)) -> m (c, d)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \c :: c
c -> (c, d) -> m (c, d)
forall (m :: * -> *) a. Monad m => a -> m a
return (c
c,d
d))
    second :: Kleisli m b c -> Kleisli m (d, b) (d, c)
second (Kleisli f :: b -> m c
f) = ((d, b) -> m (d, c)) -> Kleisli m (d, b) (d, c)
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (\ ~(d :: d
d,b :: b
b) -> b -> m c
f b
b m c -> (c -> m (d, c)) -> m (d, c)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \c :: c
c -> (d, c) -> m (d, c)
forall (m :: * -> *) a. Monad m => a -> m a
return (d
d,c
c))

-- | The identity arrow, which plays the role of 'return' in arrow notation.
returnA :: Arrow a => a b b
returnA :: a b b
returnA = (b -> b) -> a b b
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr b -> b
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

-- | Precomposition with a pure function.
(^>>) :: Arrow a => (b -> c) -> a c d -> a b d
f :: b -> c
f ^>> :: (b -> c) -> a c d -> a b d
^>> a :: a c d
a = (b -> c) -> a b c
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr b -> c
f a b c -> a c d -> a b d
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a c d
a

-- | Postcomposition with a pure function.
(>>^) :: Arrow a => a b c -> (c -> d) -> a b d
a :: a b c
a >>^ :: a b c -> (c -> d) -> a b d
>>^ f :: c -> d
f = a b c
a a b c -> a c d -> a b d
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (c -> d) -> a c d
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr c -> d
f

-- | Precomposition with a pure function (right-to-left variant).
(<<^) :: Arrow a => a c d -> (b -> c) -> a b d
a :: a c d
a <<^ :: a c d -> (b -> c) -> a b d
<<^ f :: b -> c
f = a c d
a a c d -> a b c -> a b d
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
<<< (b -> c) -> a b c
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr b -> c
f

-- | Postcomposition with a pure function (right-to-left variant).
(^<<) :: Arrow a => (c -> d) -> a b c -> a b d
f :: c -> d
f ^<< :: (c -> d) -> a b c -> a b d
^<< a :: a b c
a = (c -> d) -> a c d
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr c -> d
f a c d -> a b c -> a b d
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
<<< a b c
a

class Arrow a => ArrowZero a where
    zeroArrow :: a b c

-- | @since 2.01
instance MonadPlus m => ArrowZero (Kleisli m) where
    zeroArrow :: Kleisli m b c
zeroArrow = (b -> m c) -> Kleisli m b c
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (\_ -> m c
forall (m :: * -> *) a. MonadPlus m => m a
mzero)

-- | A monoid on arrows.
class ArrowZero a => ArrowPlus a where
    -- | An associative operation with identity 'zeroArrow'.
    (<+>) :: a b c -> a b c -> a b c

-- | @since 2.01
instance MonadPlus m => ArrowPlus (Kleisli m) where
    Kleisli f :: b -> m c
f <+> :: Kleisli m b c -> Kleisli m b c -> Kleisli m b c
<+> Kleisli g :: b -> m c
g = (b -> m c) -> Kleisli m b c
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (\x :: b
x -> b -> m c
f b
x m c -> m c -> m c
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` b -> m c
g b
x)

-- | Choice, for arrows that support it.  This class underlies the
-- @if@ and @case@ constructs in arrow notation.
--
-- Instances should satisfy the following laws:
--
--  * @'left' ('arr' f) = 'arr' ('left' f)@
--
--  * @'left' (f >>> g) = 'left' f >>> 'left' g@
--
--  * @f >>> 'arr' 'Left' = 'arr' 'Left' >>> 'left' f@
--
--  * @'left' f >>> 'arr' ('id' +++ g) = 'arr' ('id' +++ g) >>> 'left' f@
--
--  * @'left' ('left' f) >>> 'arr' assocsum = 'arr' assocsum >>> 'left' f@
--
-- where
--
-- > assocsum (Left (Left x)) = Left x
-- > assocsum (Left (Right y)) = Right (Left y)
-- > assocsum (Right z) = Right (Right z)
--
-- The other combinators have sensible default definitions, which may
-- be overridden for efficiency.

class Arrow a => ArrowChoice a where
    {-# MINIMAL (left | (+++)) #-}

    -- | Feed marked inputs through the argument arrow, passing the
    --   rest through unchanged to the output.
    left :: a b c -> a (Either b d) (Either c d)
    left = (a b c -> a d d -> a (Either b d) (Either c d)
forall (a :: * -> * -> *) b c b' c'.
ArrowChoice a =>
a b c -> a b' c' -> a (Either b b') (Either c c')
+++ a d d
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id)

    -- | A mirror image of 'left'.
    --
    --   The default definition may be overridden with a more efficient
    --   version if desired.
    right :: a b c -> a (Either d b) (Either d c)
    right = (a d d
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id a d d -> a b c -> a (Either d b) (Either d c)
forall (a :: * -> * -> *) b c b' c'.
ArrowChoice a =>
a b c -> a b' c' -> a (Either b b') (Either c c')
+++)

    -- | Split the input between the two argument arrows, retagging
    --   and merging their outputs.
    --   Note that this is in general not a functor.
    --
    --   The default definition may be overridden with a more efficient
    --   version if desired.
    (+++) :: a b c -> a b' c' -> a (Either b b') (Either c c')
    f :: a b c
f +++ g :: a b' c'
g = a b c -> a (Either b b') (Either c b')
forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either b d) (Either c d)
left a b c
f a (Either b b') (Either c b')
-> a (Either c b') (Either c c') -> a (Either b b') (Either c c')
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (Either c b' -> Either b' c) -> a (Either c b') (Either b' c)
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr Either c b' -> Either b' c
forall x y. Either x y -> Either y x
mirror a (Either c b') (Either b' c)
-> a (Either b' c) (Either c c') -> a (Either c b') (Either c c')
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a b' c' -> a (Either b' c) (Either c' c)
forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either b d) (Either c d)
left a b' c'
g a (Either b' c) (Either c' c)
-> a (Either c' c) (Either c c') -> a (Either b' c) (Either c c')
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (Either c' c -> Either c c') -> a (Either c' c) (Either c c')
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr Either c' c -> Either c c'
forall x y. Either x y -> Either y x
mirror
      where
        mirror :: Either x y -> Either y x
        mirror :: Either x y -> Either y x
mirror (Left x :: x
x) = x -> Either y x
forall a b. b -> Either a b
Right x
x
        mirror (Right y :: y
y) = y -> Either y x
forall a b. a -> Either a b
Left y
y

    -- | Fanin: Split the input between the two argument arrows and
    --   merge their outputs.
    --
    --   The default definition may be overridden with a more efficient
    --   version if desired.
    (|||) :: a b d -> a c d -> a (Either b c) d
    f :: a b d
f ||| g :: a c d
g = a b d
f a b d -> a c d -> a (Either b c) (Either d d)
forall (a :: * -> * -> *) b c b' c'.
ArrowChoice a =>
a b c -> a b' c' -> a (Either b b') (Either c c')
+++ a c d
g a (Either b c) (Either d d) -> a (Either d d) d -> a (Either b c) d
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (Either d d -> d) -> a (Either d d) d
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr Either d d -> d
forall p. Either p p -> p
untag
      where
        untag :: Either p p -> p
untag (Left x :: p
x) = p
x
        untag (Right y :: p
y) = p
y

{-# RULES
"left/arr"      forall f .
                left (arr f) = arr (left f)
"right/arr"     forall f .
                right (arr f) = arr (right f)
"sum/arr"       forall f g .
                arr f +++ arr g = arr (f +++ g)
"fanin/arr"     forall f g .
                arr f ||| arr g = arr (f ||| g)
"compose/left"  forall f g .
                left f . left g = left (f . g)
"compose/right" forall f g .
                right f . right g = right (f . g)
 #-}

-- | @since 2.01
instance ArrowChoice (->) where
    left :: (b -> c) -> Either b d -> Either c d
left f :: b -> c
f = b -> c
f (b -> c) -> (d -> d) -> Either b d -> Either c d
forall (a :: * -> * -> *) b c b' c'.
ArrowChoice a =>
a b c -> a b' c' -> a (Either b b') (Either c c')
+++ d -> d
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
    right :: (b -> c) -> Either d b -> Either d c
right f :: b -> c
f = d -> d
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id (d -> d) -> (b -> c) -> Either d b -> Either d c
forall (a :: * -> * -> *) b c b' c'.
ArrowChoice a =>
a b c -> a b' c' -> a (Either b b') (Either c c')
+++ b -> c
f
    f :: b -> c
f +++ :: (b -> c) -> (b' -> c') -> Either b b' -> Either c c'
+++ g :: b' -> c'
g = (c -> Either c c'
forall a b. a -> Either a b
Left (c -> Either c c') -> (b -> c) -> b -> Either c c'
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. b -> c
f) (b -> Either c c')
-> (b' -> Either c c') -> Either b b' -> Either c c'
forall (a :: * -> * -> *) b d c.
ArrowChoice a =>
a b d -> a c d -> a (Either b c) d
||| (c' -> Either c c'
forall a b. b -> Either a b
Right (c' -> Either c c') -> (b' -> c') -> b' -> Either c c'
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. b' -> c'
g)
    ||| :: (b -> d) -> (c -> d) -> Either b c -> d
(|||) = (b -> d) -> (c -> d) -> Either b c -> d
forall b d c. (b -> d) -> (c -> d) -> Either b c -> d
either

-- | @since 2.01
instance Monad m => ArrowChoice (Kleisli m) where
    left :: Kleisli m b c -> Kleisli m (Either b d) (Either c d)
left f :: Kleisli m b c
f = Kleisli m b c
f Kleisli m b c
-> Kleisli m d d -> Kleisli m (Either b d) (Either c d)
forall (a :: * -> * -> *) b c b' c'.
ArrowChoice a =>
a b c -> a b' c' -> a (Either b b') (Either c c')
+++ (d -> d) -> Kleisli m d d
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr d -> d
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
    right :: Kleisli m b c -> Kleisli m (Either d b) (Either d c)
right f :: Kleisli m b c
f = (d -> d) -> Kleisli m d d
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr d -> d
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id Kleisli m d d
-> Kleisli m b c -> Kleisli m (Either d b) (Either d c)
forall (a :: * -> * -> *) b c b' c'.
ArrowChoice a =>
a b c -> a b' c' -> a (Either b b') (Either c c')
+++ Kleisli m b c
f
    f :: Kleisli m b c
f +++ :: Kleisli m b c
-> Kleisli m b' c' -> Kleisli m (Either b b') (Either c c')
+++ g :: Kleisli m b' c'
g = (Kleisli m b c
f Kleisli m b c
-> Kleisli m c (Either c c') -> Kleisli m b (Either c c')
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (c -> Either c c') -> Kleisli m c (Either c c')
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr c -> Either c c'
forall a b. a -> Either a b
Left) Kleisli m b (Either c c')
-> Kleisli m b' (Either c c')
-> Kleisli m (Either b b') (Either c c')
forall (a :: * -> * -> *) b d c.
ArrowChoice a =>
a b d -> a c d -> a (Either b c) d
||| (Kleisli m b' c'
g Kleisli m b' c'
-> Kleisli m c' (Either c c') -> Kleisli m b' (Either c c')
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (c' -> Either c c') -> Kleisli m c' (Either c c')
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr c' -> Either c c'
forall a b. b -> Either a b
Right)
    Kleisli f :: b -> m d
f ||| :: Kleisli m b d -> Kleisli m c d -> Kleisli m (Either b c) d
||| Kleisli g :: c -> m d
g = (Either b c -> m d) -> Kleisli m (Either b c) d
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli ((b -> m d) -> (c -> m d) -> Either b c -> m d
forall b d c. (b -> d) -> (c -> d) -> Either b c -> d
either b -> m d
f c -> m d
g)

-- | Some arrows allow application of arrow inputs to other inputs.
-- Instances should satisfy the following laws:
--
--  * @'first' ('arr' (\\x -> 'arr' (\\y -> (x,y)))) >>> 'app' = 'id'@
--
--  * @'first' ('arr' (g >>>)) >>> 'app' = 'second' g >>> 'app'@
--
--  * @'first' ('arr' (>>> h)) >>> 'app' = 'app' >>> h@
--
-- Such arrows are equivalent to monads (see 'ArrowMonad').

class Arrow a => ArrowApply a where
    app :: a (a b c, b) c

-- | @since 2.01
instance ArrowApply (->) where
    app :: (b -> c, b) -> c
app (f :: b -> c
f,x :: b
x) = b -> c
f b
x

-- | @since 2.01
instance Monad m => ArrowApply (Kleisli m) where
    app :: Kleisli m (Kleisli m b c, b) c
app = ((Kleisli m b c, b) -> m c) -> Kleisli m (Kleisli m b c, b) c
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (\(Kleisli f :: b -> m c
f, x :: b
x) -> b -> m c
f b
x)

-- | The 'ArrowApply' class is equivalent to 'Monad': any monad gives rise
--   to a 'Kleisli' arrow, and any instance of 'ArrowApply' defines a monad.

newtype ArrowMonad a b = ArrowMonad (a () b)

-- | @since 4.6.0.0
instance Arrow a => Functor (ArrowMonad a) where
    fmap :: (a -> b) -> ArrowMonad a a -> ArrowMonad a b
fmap f :: a -> b
f (ArrowMonad m :: a () a
m) = a () b -> ArrowMonad a b
forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad (a () b -> ArrowMonad a b) -> a () b -> ArrowMonad a b
forall a b. (a -> b) -> a -> b
$ a () a
m a () a -> a a b -> a () b
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (a -> b) -> a a b
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr a -> b
f

-- | @since 4.6.0.0
instance Arrow a => Applicative (ArrowMonad a) where
   pure :: a -> ArrowMonad a a
pure x :: a
x = a () a -> ArrowMonad a a
forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad ((() -> a) -> a () a
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (a -> () -> a
forall a b. a -> b -> a
const a
x))
   ArrowMonad f :: a () (a -> b)
f <*> :: ArrowMonad a (a -> b) -> ArrowMonad a a -> ArrowMonad a b
<*> ArrowMonad x :: a () a
x = a () b -> ArrowMonad a b
forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad (a () (a -> b)
f a () (a -> b) -> a () a -> a () (a -> b, a)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& a () a
x a () (a -> b, a) -> a (a -> b, a) b -> a () b
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ((a -> b, a) -> b) -> a (a -> b, a) b
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (((a -> b) -> a -> b) -> (a -> b, a) -> b
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (a -> b) -> a -> b
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id))

-- | @since 2.01
instance ArrowApply a => Monad (ArrowMonad a) where
    ArrowMonad m :: a () a
m >>= :: ArrowMonad a a -> (a -> ArrowMonad a b) -> ArrowMonad a b
>>= f :: a -> ArrowMonad a b
f = a () b -> ArrowMonad a b
forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad (a () b -> ArrowMonad a b) -> a () b -> ArrowMonad a b
forall a b. (a -> b) -> a -> b
$
        a () a
m a () a -> a a b -> a () b
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (a -> (a () b, ())) -> a a (a () b, ())
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (\x :: a
x -> let ArrowMonad h :: a () b
h = a -> ArrowMonad a b
f a
x in (a () b
h, ())) a a (a () b, ()) -> a (a () b, ()) b -> a a b
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a (a () b, ()) b
forall (a :: * -> * -> *) b c. ArrowApply a => a (a b c, b) c
app

-- | @since 4.6.0.0
instance ArrowPlus a => Alternative (ArrowMonad a) where
   empty :: ArrowMonad a a
empty = a () a -> ArrowMonad a a
forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad a () a
forall (a :: * -> * -> *) b c. ArrowZero a => a b c
zeroArrow
   ArrowMonad x :: a () a
x <|> :: ArrowMonad a a -> ArrowMonad a a -> ArrowMonad a a
<|> ArrowMonad y :: a () a
y = a () a -> ArrowMonad a a
forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad (a () a
x a () a -> a () a -> a () a
forall (a :: * -> * -> *) b c.
ArrowPlus a =>
a b c -> a b c -> a b c
<+> a () a
y)

-- | @since 4.6.0.0
instance (ArrowApply a, ArrowPlus a) => MonadPlus (ArrowMonad a)

-- | Any instance of 'ArrowApply' can be made into an instance of
--   'ArrowChoice' by defining 'left' = 'leftApp'.

leftApp :: ArrowApply a => a b c -> a (Either b d) (Either c d)
leftApp :: a b c -> a (Either b d) (Either c d)
leftApp f :: a b c
f = (Either b d -> (a () (Either c d), ()))
-> a (Either b d) (a () (Either c d), ())
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr ((\b :: b
b -> ((() -> b) -> a () b
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (\() -> b
b) a () b -> a b (Either c d) -> a () (Either c d)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a b c
f a b c -> a c (Either c d) -> a b (Either c d)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (c -> Either c d) -> a c (Either c d)
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr c -> Either c d
forall a b. a -> Either a b
Left, ())) (b -> (a () (Either c d), ()))
-> (d -> (a () (Either c d), ()))
-> Either b d
-> (a () (Either c d), ())
forall (a :: * -> * -> *) b d c.
ArrowChoice a =>
a b d -> a c d -> a (Either b c) d
|||
             (\d :: d
d -> ((() -> d) -> a () d
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (\() -> d
d) a () d -> a d (Either c d) -> a () (Either c d)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (d -> Either c d) -> a d (Either c d)
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr d -> Either c d
forall a b. b -> Either a b
Right, ()))) a (Either b d) (a () (Either c d), ())
-> a (a () (Either c d), ()) (Either c d)
-> a (Either b d) (Either c d)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a (a () (Either c d), ()) (Either c d)
forall (a :: * -> * -> *) b c. ArrowApply a => a (a b c, b) c
app

-- | The 'loop' operator expresses computations in which an output value
-- is fed back as input, although the computation occurs only once.
-- It underlies the @rec@ value recursion construct in arrow notation.
-- 'loop' should satisfy the following laws:
--
-- [/extension/]
--      @'loop' ('arr' f) = 'arr' (\\ b -> 'fst' ('fix' (\\ (c,d) -> f (b,d))))@
--
-- [/left tightening/]
--      @'loop' ('first' h >>> f) = h >>> 'loop' f@
--
-- [/right tightening/]
--      @'loop' (f >>> 'first' h) = 'loop' f >>> h@
--
-- [/sliding/]
--      @'loop' (f >>> 'arr' ('id' *** k)) = 'loop' ('arr' ('id' *** k) >>> f)@
--
-- [/vanishing/]
--      @'loop' ('loop' f) = 'loop' ('arr' unassoc >>> f >>> 'arr' assoc)@
--
-- [/superposing/]
--      @'second' ('loop' f) = 'loop' ('arr' assoc >>> 'second' f >>> 'arr' unassoc)@
--
-- where
--
-- > assoc ((a,b),c) = (a,(b,c))
-- > unassoc (a,(b,c)) = ((a,b),c)
--
class Arrow a => ArrowLoop a where
    loop :: a (b,d) (c,d) -> a b c

-- | @since 2.01
instance ArrowLoop (->) where
    loop :: ((b, d) -> (c, d)) -> b -> c
loop f :: (b, d) -> (c, d)
f b :: b
b = let (c :: c
c,d :: d
d) = (b, d) -> (c, d)
f (b
b,d
d) in c
c

-- | Beware that for many monads (those for which the '>>=' operation
-- is strict) this instance will /not/ satisfy the right-tightening law
-- required by the 'ArrowLoop' class.
--
-- @since 2.01
instance MonadFix m => ArrowLoop (Kleisli m) where
    loop :: Kleisli m (b, d) (c, d) -> Kleisli m b c
loop (Kleisli f :: (b, d) -> m (c, d)
f) = (b -> m c) -> Kleisli m b c
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (((c, d) -> c) -> m (c, d) -> m c
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (c, d) -> c
forall a b. (a, b) -> a
fst (m (c, d) -> m c) -> (b -> m (c, d)) -> b -> m c
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((c, d) -> m (c, d)) -> m (c, d)
forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (((c, d) -> m (c, d)) -> m (c, d))
-> (b -> (c, d) -> m (c, d)) -> b -> m (c, d)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. b -> (c, d) -> m (c, d)
forall a. b -> (a, d) -> m (c, d)
f')
      where f' :: b -> (a, d) -> m (c, d)
f' x :: b
x y :: (a, d)
y = (b, d) -> m (c, d)
f (b
x, (a, d) -> d
forall a b. (a, b) -> b
snd (a, d)
y)