{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# OPTIONS_GHC -Wno-inline-rule-shadowing #-}
    -- The RULES for the methods of class Arrow may never fire
    -- e.g. compose/arr;  see #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 )
import GHC.Generics (Generic, Generic1)

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 = (forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** 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 = (forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id 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')
    a b c
f *** a b' c'
g = forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first a b c
f forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall {b} {a}. (b, a) -> (a, b)
swap forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first a b' c'
g forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall {b} {a}. (b, a) -> (a, b)
swap
      where swap :: (b, a) -> (a, b)
swap ~(b
x,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')
    a b c
f &&& a b c'
g = forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (\b
b -> (b
b,b
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 c
f 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 :: forall b c. (b -> c) -> b -> c
arr 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.
    *** :: forall b c b' c'. (b -> c) -> (b' -> c') -> (b, b') -> (c, c')
(***) b -> c
f b' -> c'
g ~(b
x,b'
y) = (b -> c
f b
x, b' -> c'
g b'
y)

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

-- | @since 4.14.0.0
deriving instance Generic (Kleisli m a b)

-- | @since 4.14.0.0
deriving instance Generic1 (Kleisli m a)

-- | @since 4.14.0.0
deriving instance Functor m => Functor (Kleisli m a)

-- | @since 4.14.0.0
instance Applicative m => Applicative (Kleisli m a) where
  pure :: forall a. a -> Kleisli m a a
pure = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a b. a -> b -> a
const forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure
  {-# INLINE pure #-}
  Kleisli a -> m (a -> b)
f <*> :: forall a b. Kleisli m a (a -> b) -> Kleisli m a a -> Kleisli m a b
<*> Kleisli a -> m a
g = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli forall a b. (a -> b) -> a -> b
$ \a
x -> a -> m (a -> b)
f a
x forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> m a
g a
x
  {-# INLINE (<*>) #-}
  Kleisli a -> m a
f *> :: forall a b. Kleisli m a a -> Kleisli m a b -> Kleisli m a b
*> Kleisli a -> m b
g = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli forall a b. (a -> b) -> a -> b
$ \a
x -> a -> m a
f a
x forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> a -> m b
g a
x
  {-# INLINE (*>) #-}
  Kleisli a -> m a
f <* :: forall a b. Kleisli m a a -> Kleisli m a b -> Kleisli m a a
<* Kleisli a -> m b
g = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli forall a b. (a -> b) -> a -> b
$ \a
x -> a -> m a
f a
x forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* a -> m b
g a
x
  {-# INLINE (<*) #-}

-- | @since 4.14.0.0
instance Alternative m => Alternative (Kleisli m a) where
  empty :: forall a. Kleisli m a a
empty = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const forall (f :: * -> *) a. Alternative f => f a
empty
  {-# INLINE empty #-}
  Kleisli a -> m a
f <|> :: forall a. Kleisli m a a -> Kleisli m a a -> Kleisli m a a
<|> Kleisli a -> m a
g = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli forall a b. (a -> b) -> a -> b
$ \a
x -> a -> m a
f a
x forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> a -> m a
g a
x
  {-# INLINE (<|>) #-}

-- | @since 4.14.0.0
instance Monad m => Monad (Kleisli m a) where
  Kleisli a -> m a
f >>= :: forall a b. Kleisli m a a -> (a -> Kleisli m a b) -> Kleisli m a b
>>= a -> Kleisli m a b
k = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli forall a b. (a -> b) -> a -> b
$ \a
x -> a -> m a
f a
x forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
a -> forall (m :: * -> *) a b. Kleisli m a b -> a -> m b
runKleisli (a -> Kleisli m a b
k a
a) a
x
  {-# INLINE (>>=) #-}

-- | @since 4.14.0.0
instance MonadPlus m => MonadPlus (Kleisli m a) where
  mzero :: forall a. Kleisli m a a
mzero = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const forall (m :: * -> *) a. MonadPlus m => m a
mzero
  {-# INLINE mzero #-}
  Kleisli a -> m a
f mplus :: forall a. Kleisli m a a -> Kleisli m a a -> Kleisli m a a
`mplus` Kleisli a -> m a
g = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli forall a b. (a -> b) -> a -> b
$ \a
x -> a -> m a
f a
x forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` a -> m a
g a
x
  {-# INLINE mplus #-}

-- | @since 3.0
instance Monad m => Category (Kleisli m) where
    id :: forall a. Kleisli m a a
id = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli forall (m :: * -> *) a. Monad m => a -> m a
return
    (Kleisli b -> m c
f) . :: forall b c a. Kleisli m b c -> Kleisli m a b -> Kleisli m a c
. (Kleisli a -> m b
g) = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (\a
b -> a -> m b
g a
b 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 :: forall b c. (b -> c) -> Kleisli m b c
arr b -> c
f = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (forall (m :: * -> *) a. Monad m => a -> m a
return 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 :: forall b c d. Kleisli m b c -> Kleisli m (b, d) (c, d)
first (Kleisli b -> m c
f) = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (\ ~(b
b,d
d) -> b -> m c
f b
b forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \c
c -> forall (m :: * -> *) a. Monad m => a -> m a
return (c
c,d
d))
    second :: forall b c d. Kleisli m b c -> Kleisli m (d, b) (d, c)
second (Kleisli b -> m c
f) = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (\ ~(d
d,b
b) -> b -> m c
f b
b forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \c
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 :: forall (a :: * -> * -> *) b. Arrow a => a b b
returnA = forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr 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
b -> c
f ^>> :: forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> a c d
a = forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr b -> c
f 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 b c
a >>^ :: forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ c -> d
f = a b c
a forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> 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 c d
a <<^ :: forall (a :: * -> * -> *) c d b.
Arrow a =>
a c d -> (b -> c) -> a b d
<<^ b -> c
f = a c d
a forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a 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
c -> d
f ^<< :: forall (a :: * -> * -> *) c d b.
Arrow a =>
(c -> d) -> a b c -> a b d
^<< a b c
a = forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr c -> d
f 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 :: forall b c. Kleisli m b c
zeroArrow = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (\b
_ -> 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 b -> m c
f <+> :: forall b c. Kleisli m b c -> Kleisli m b c -> Kleisli m b c
<+> Kleisli b -> m c
g = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (\b
x -> b -> m c
f b
x 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 = (forall (a :: * -> * -> *) b c b' c'.
ArrowChoice a =>
a b c -> a b' c' -> a (Either b b') (Either c c')
+++ 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 = (forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id 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')
    a b c
f +++ a b' c'
g = forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either b d) (Either c d)
left a b c
f forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall x y. Either x y -> Either y x
mirror forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c d.
ArrowChoice a =>
a b c -> a (Either b d) (Either c d)
left a b' c'
g forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall x y. Either x y -> Either y x
mirror
      where
        mirror :: Either x y -> Either y x
        mirror :: forall x y. Either x y -> Either y x
mirror (Left x
x) = forall a b. b -> Either a b
Right x
x
        mirror (Right y
y) = 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
    a b d
f ||| a c d
g = a b d
f 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 forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall {a}. Either a a -> a
untag
      where
        untag :: Either a a -> a
untag (Left a
x) = a
x
        untag (Right a
y) = a
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 :: forall b c d. (b -> c) -> Either b d -> Either c d
left b -> c
f = b -> c
f forall (a :: * -> * -> *) b c b' c'.
ArrowChoice a =>
a b c -> a b' c' -> a (Either b b') (Either c c')
+++ forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
    right :: forall b c d. (b -> c) -> Either d b -> Either d c
right b -> c
f = forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id forall (a :: * -> * -> *) b c b' c'.
ArrowChoice a =>
a b c -> a b' c' -> a (Either b b') (Either c c')
+++ b -> c
f
    b -> c
f +++ :: forall b c b' c'.
(b -> c) -> (b' -> c') -> Either b b' -> Either c c'
+++ b' -> c'
g = (forall a b. a -> Either a b
Left 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) forall (a :: * -> * -> *) b d c.
ArrowChoice a =>
a b d -> a c d -> a (Either b c) d
||| (forall a b. b -> Either a b
Right 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)
    ||| :: forall b d c. (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 :: forall b c d. Kleisli m b c -> Kleisli m (Either b d) (Either c d)
left Kleisli m b c
f = Kleisli m b c
f forall (a :: * -> * -> *) b c b' c'.
ArrowChoice a =>
a b c -> a b' c' -> a (Either b b') (Either c c')
+++ forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
    right :: forall b c d. Kleisli m b c -> Kleisli m (Either d b) (Either d c)
right Kleisli m b c
f = forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id 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
    Kleisli m b c
f +++ :: forall b c b' c'.
Kleisli m b c
-> Kleisli m b' c' -> Kleisli m (Either b b') (Either c c')
+++ Kleisli m b' c'
g = (Kleisli m b c
f forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall a b. a -> Either a b
Left) forall (a :: * -> * -> *) b d c.
ArrowChoice a =>
a b d -> a c d -> a (Either b c) d
||| (Kleisli m b' c'
g forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall a b. b -> Either a b
Right)
    Kleisli b -> m d
f ||| :: forall b d c.
Kleisli m b d -> Kleisli m c d -> Kleisli m (Either b c) d
||| Kleisli c -> m d
g = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (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 :: forall b c. (b -> c, b) -> c
app (b -> c
f,b
x) = b -> c
f b
x

-- | @since 2.01
instance Monad m => ArrowApply (Kleisli m) where
    app :: forall b c. Kleisli m (Kleisli m b c, b) c
app = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (\(Kleisli b -> m c
f, 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 :: forall a b. (a -> b) -> ArrowMonad a a -> ArrowMonad a b
fmap a -> b
f (ArrowMonad a () a
m) = forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad forall a b. (a -> b) -> a -> b
$ a () a
m forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> 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 :: forall a. a -> ArrowMonad a a
pure a
x = forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad (forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (forall a b. a -> b -> a
const a
x))
   ArrowMonad a () (a -> b)
f <*> :: forall a b.
ArrowMonad a (a -> b) -> ArrowMonad a a -> ArrowMonad a b
<*> ArrowMonad a () a
x = forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad (a () (a -> b)
f forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& a () a
x forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id))

-- | @since 2.01
instance ArrowApply a => Monad (ArrowMonad a) where
    ArrowMonad a () a
m >>= :: forall a b.
ArrowMonad a a -> (a -> ArrowMonad a b) -> ArrowMonad a b
>>= a -> ArrowMonad a b
f = forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad forall a b. (a -> b) -> a -> b
$
        a () a
m forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (\a
x -> let ArrowMonad a () b
h = a -> ArrowMonad a b
f a
x in (a () b
h, ())) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> 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 :: forall a. ArrowMonad a a
empty = forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad forall (a :: * -> * -> *) b c. ArrowZero a => a b c
zeroArrow
   ArrowMonad a () a
x <|> :: forall a. ArrowMonad a a -> ArrowMonad a a -> ArrowMonad a a
<|> ArrowMonad a () a
y = forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad (a () a
x 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 :: forall (a :: * -> * -> *) b c d.
ArrowApply a =>
a b c -> a (Either b d) (Either c d)
leftApp a b c
f = forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr ((\b
b -> (forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (\() -> b
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 c
f forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall a b. a -> Either a b
Left, ())) forall (a :: * -> * -> *) b d c.
ArrowChoice a =>
a b d -> a c d -> a (Either b c) d
|||
             (\d
d -> (forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (\() -> d
d) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall a b. b -> Either a b
Right, ()))) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> 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 :: forall b d c. ((b, d) -> (c, d)) -> b -> c
loop (b, d) -> (c, d)
f b
b = let (c
c,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 :: forall b d c. Kleisli m (b, d) (c, d) -> Kleisli m b c
loop (Kleisli (b, d) -> m (c, d)
f) = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall a b. (a, b) -> a
fst forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall {a}. b -> (a, d) -> m (c, d)
f')
      where f' :: b -> (a, d) -> m (c, d)
f' b
x (a, d)
y = (b, d) -> m (c, d)
f (b
x, forall a b. (a, b) -> b
snd (a, d)
y)