{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# OPTIONS_GHC -Wno-inline-rule-shadowing #-}
module Control.Arrow (
Arrow(..), Kleisli(..),
returnA,
(^>>), (>>^),
(>>>), (<<<),
(<<^), (^<<),
ArrowZero(..), ArrowPlus(..),
ArrowChoice(..),
ArrowApply(..), ArrowMonad(..), leftApp,
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 ^<<, <<^
class Category a => Arrow a where
{-# MINIMAL arr, (first | (***)) #-}
arr :: (b -> c) -> a b c
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)
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')
***)
(***) :: 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)
(&&&) :: 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)
#-}
instance Arrow (->) where
arr :: forall b c. (b -> c) -> b -> c
arr b -> c
f = b -> c
f
*** :: 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)
newtype Kleisli m a b = Kleisli { forall (m :: * -> *) a b. Kleisli m a b -> a -> m b
runKleisli :: a -> m b }
deriving instance Generic (Kleisli m a b)
deriving instance Generic1 (Kleisli m a)
deriving instance Functor m => Functor (Kleisli m a)
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 (<*) #-}
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 (<|>) #-}
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 (>>=) #-}
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 #-}
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)
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))
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
(^>>) :: 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
(>>^) :: 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
(<<^) :: 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
(^<<) :: 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
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)
class ArrowZero a => ArrowPlus a where
(<+>) :: a b c -> a b c -> a b c
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)
class Arrow a => ArrowChoice a where
{-# MINIMAL (left | (+++)) #-}
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)
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')
+++)
(+++) :: 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
(|||) :: 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)
#-}
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
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)
class Arrow a => ArrowApply a where
app :: a (a b c, b) c
instance ArrowApply (->) where
app :: forall b c. (b -> c, b) -> c
app (b -> c
f,b
x) = b -> c
f b
x
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)
newtype ArrowMonad a b = ArrowMonad (a () b)
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
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))
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
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)
instance (ArrowApply a, ArrowPlus a) => MonadPlus (ArrowMonad a)
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
class Arrow a => ArrowLoop a where
loop :: a (b,d) (c,d) -> a b c
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
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)