module Language.KURE.Combinators.Arrow
(
result
, argument
, toFst
, toSnd
, swap
, fork
, forkFirst
, forkSecond
, constant
, serialise
, parallelise
) where
import Prelude hiding (id, foldr)
import Control.Category hiding ((.))
import Control.Arrow
import Data.Foldable
result :: Arrow bi => (b -> c) -> bi a b -> bi a c
result :: (b -> c) -> bi a b -> bi a c
result b -> c
f bi a b
a = bi a b
a bi a b -> (b -> c) -> bi a c
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ b -> c
f
{-# INLINE result #-}
argument :: Arrow bi => (a -> b) -> bi b c -> bi a c
argument :: (a -> b) -> bi b c -> bi a c
argument a -> b
f bi b c
a = a -> b
f (a -> b) -> bi b c -> bi a c
forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> bi b c
a
{-# INLINE argument #-}
toFst :: Arrow bi => bi a b -> bi (a,x) b
toFst :: bi a b -> bi (a, x) b
toFst bi a b
f = (a, x) -> a
forall a b. (a, b) -> a
fst ((a, x) -> a) -> bi a b -> bi (a, x) b
forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> bi a b
f
{-# INLINE toFst #-}
toSnd :: Arrow bi => bi a b -> bi (x,a) b
toSnd :: bi a b -> bi (x, a) b
toSnd bi a b
f = (x, a) -> a
forall a b. (a, b) -> b
snd ((x, a) -> a) -> bi a b -> bi (x, a) b
forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> bi a b
f
{-# INLINE toSnd #-}
swap :: Arrow bi => bi (a,b) (b,a)
swap :: bi (a, b) (b, a)
swap = ((a, b) -> (b, a)) -> bi (a, b) (b, a)
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (\(a
a,b
b) -> (b
b,a
a))
{-# INLINE swap #-}
fork :: Arrow bi => bi a (a,a)
fork :: bi a (a, a)
fork = (a -> (a, a)) -> bi a (a, a)
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (\a
a -> (a
a,a
a))
{-# INLINE fork #-}
forkFirst :: Arrow bi => bi a b -> bi a (b,a)
forkFirst :: bi a b -> bi a (b, a)
forkFirst bi a b
sf = bi a (a, a)
forall (bi :: * -> * -> *) a. Arrow bi => bi a (a, a)
fork bi a (a, a) -> bi (a, a) (b, a) -> bi a (b, a)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> bi a b -> bi (a, a) (b, a)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first bi a b
sf
{-# INLINE forkFirst #-}
forkSecond :: Arrow bi => bi a b -> bi a (a,b)
forkSecond :: bi a b -> bi a (a, b)
forkSecond bi a b
sf = bi a (a, a)
forall (bi :: * -> * -> *) a. Arrow bi => bi a (a, a)
fork bi a (a, a) -> bi (a, a) (a, b) -> bi 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
>>> bi a b -> bi (a, a) (a, b)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second bi a b
sf
{-# INLINE forkSecond #-}
constant :: Arrow bi => b -> bi a b
constant :: b -> bi a b
constant = (a -> b) -> bi a b
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr ((a -> b) -> bi a b) -> (b -> a -> b) -> b -> bi a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a -> b
forall a b. a -> b -> a
const
{-# INLINE constant #-}
serialise :: (Foldable f, Category bi) => f (bi a a) -> bi a a
serialise :: f (bi a a) -> bi a a
serialise = (bi a a -> bi a a -> bi a a) -> bi a a -> f (bi a a) -> bi a a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr bi a a -> bi a a -> bi a a
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
(>>>) bi a a
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
{-# INLINE serialise #-}
parallelise :: (Foldable f, Arrow bi, Monoid b) => f (bi a b) -> bi a b
parallelise :: f (bi a b) -> bi a b
parallelise = (bi a b -> bi a b -> bi a b) -> bi a b -> f (bi a b) -> bi a b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\ bi a b
f bi a b
g -> (bi a b
f bi a b -> bi a b -> bi a (b, b)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& bi a b
g) bi a (b, b) -> ((b, b) -> b) -> bi a b
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ (b -> b -> b) -> (b, b) -> b
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry b -> b -> b
forall a. Monoid a => a -> a -> a
mappend) (b -> bi a b
forall (bi :: * -> * -> *) b a. Arrow bi => b -> bi a b
constant b
forall a. Monoid a => a
mempty)
{-# INLINE parallelise #-}