{-# 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      :  GHC.Internal.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 GHC.Internal.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 GHC.Internal.Data.Tuple ( fst, snd, uncurry )
import GHC.Internal.Data.Either
import GHC.Internal.Control.Monad.Fix
import GHC.Internal.Control.Category
import GHC.Internal.Base hiding ( (.), id )
import GHC.Internal.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 = (a b c -> a d d -> a (b, d) (c, d)
forall b c b' c'. 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 d d
forall a. a a a
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 a. a a a
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 b c b' c'. 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')
***)

    -- | 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 = a b c -> a (b, b') (c, b')
forall b c d. a b c -> a (b, d) (c, d)
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 b c. (b -> c) -> a 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 b c d. a b c -> a (b, d) (c, d)
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 b c. (b -> c) -> a b 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 ~(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 = (b -> (b, b)) -> a b (b, b)
forall b c. (b -> c) -> a b c
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (\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 b c b' c'. 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 base-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 base-4.14.0.0
deriving instance Generic (Kleisli m a b)

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

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

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

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

-- | @since base-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 = (a -> m b) -> Kleisli m a b
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli ((a -> m b) -> Kleisli m a b) -> (a -> m b) -> Kleisli m a b
forall a b. (a -> b) -> a -> b
$ \a
x -> a -> m a
f a
x m a -> (a -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
a -> Kleisli m a b -> a -> m b
forall (m :: * -> *) a b. Kleisli m a b -> a -> m b
runKleisli (a -> Kleisli m a b
k a
a) a
x
  {-# INLINE (>>=) #-}

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

-- | @since base-3.0
instance Monad m => Category (Kleisli m) where
    id :: forall a. 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 a. a -> m a
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) = (a -> m c) -> Kleisli m a c
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (\a
b -> a -> m b
g a
b m b -> (b -> m c) -> m c
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> m c
f)

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

-- | @since base-4.6.0.0
instance Arrow a => Applicative (ArrowMonad a) where
   pure :: forall a. a -> ArrowMonad a a
pure a
x = a () a -> ArrowMonad a a
forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad ((() -> a) -> a () a
forall b c. (b -> c) -> a b c
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (a -> () -> a
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 = 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 b c c'. a b c -> a b c' -> a b (c, c')
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 b c. (b -> c) -> a b c
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 a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id))

-- | @since base-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 = 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 b c. (b -> c) -> a b 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, ())) 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 b c. a (a b c, b) c
forall (a :: * -> * -> *) b c. ArrowApply a => a (a b c, b) c
app

-- | @since base-4.6.0.0
instance ArrowPlus a => Alternative (ArrowMonad a) where
   empty :: forall a. ArrowMonad a a
empty = a () a -> ArrowMonad a a
forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad a () a
forall b c. a b c
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 = a () a -> ArrowMonad a a
forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad (a () a
x a () a -> a () a -> a () a
forall b c. a b c -> a b c -> a b c
forall (a :: * -> * -> *) b c.
ArrowPlus a =>
a b c -> a b c -> a b c
<+> a () a
y)

-- | @since base-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 = (Either b d -> (a () (Either c d), ()))
-> a (Either b d) (a () (Either c d), ())
forall b c. (b -> c) -> a b c
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr ((\b
b -> ((() -> b) -> a () b
forall b c. (b -> c) -> a b c
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 b c. (b -> c) -> a b c
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 b d c. (b -> d) -> (c -> d) -> Either b c -> d
forall (a :: * -> * -> *) b d c.
ArrowChoice a =>
a b d -> a c d -> a (Either b c) d
|||
             (\d
d -> ((() -> d) -> a () d
forall b c. (b -> c) -> a b c
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 b c. (b -> c) -> a b c
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 b c. a (a b c, b) 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 base-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 base-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) = (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 b c a. (b -> c) -> (a -> b) -> a -> 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 a. (a -> m a) -> m a
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 b c a. (b -> c) -> (a -> b) -> a -> 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, d) -> m (c, d)
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, (a, d) -> d
forall a b. (a, b) -> b
snd (a, d)
y)