[project @ 2005-03-15 17:19:09 by ross]
[ghc-base.git] / Control / Arrow.hs
index f6ee713..2710be6 100644 (file)
@@ -22,7 +22,10 @@ module Control.Arrow (
                -- * Arrows
                Arrow(..), Kleisli(..),
                -- ** Derived combinators
-               returnA, (<<<),
+               returnA,
+               (^>>), (>>^),
+               -- ** Right-to-left variants
+               (<<<), (<<^), (^<<),
                -- * Monoid operations
                ArrowZero(..), ArrowPlus(..),
                -- * Conditionals
@@ -43,8 +46,8 @@ infixr 3 ***
 infixr 3 &&&
 infixr 2 +++
 infixr 2 |||
-infixr 1 >>>
-infixr 1 <<<
+infixr 1 >>>, ^>>, >>^
+infixr 1 <<<, ^<<, <<^
 
 -- | The basic arrow class.
 --   Any instance must define either 'arr' or 'pure' (which are synonyms),
@@ -93,6 +96,23 @@ class Arrow a where
        (&&&) :: a b c -> a b c' -> a b (c,c')
        f &&& g = arr (\b -> (b,b)) >>> f *** 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.
 
 instance Arrow (->) where
@@ -106,7 +126,7 @@ instance Arrow (->) where
 
 -- | Kleisli arrows of a monad.
 
-newtype Kleisli m a b = Kleisli (a -> m b)
+newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }
 
 instance Monad m => Arrow (Kleisli m) where
        arr f = Kleisli (return . f)
@@ -119,11 +139,26 @@ instance Monad m => Arrow (Kleisli m) where
 returnA :: Arrow a => a b b
 returnA = arr id
 
--- | Right-to-left composition, for a better fit with arrow notation.
+-- | Precomposition with a pure function.
+(^>>) :: Arrow a => (b -> c) -> a c d -> a b d
+f ^>> a = arr f >>> a
 
+-- | Postcomposition with a pure function.
+(>>^) :: Arrow a => a b c -> (c -> d) -> a b d
+a >>^ f = a >>> arr f
+
+-- | Right-to-left composition, for a better fit with arrow notation.
 (<<<) :: Arrow a => a c d -> a b c -> a b d
 f <<< g = g >>> f
 
+-- | Precomposition with a pure function (right-to-left variant).
+(<<^) :: Arrow a => a c d -> (b -> c) -> a b d
+a <<^ f = a <<< arr f
+
+-- | Postcomposition with a pure function (right-to-left variant).
+(^<<) :: Arrow a => (c -> d) -> a b c -> a b d
+f ^<< a = arr f <<< a
+
 class Arrow a => ArrowZero a where
        zeroArrow :: a b c
 
@@ -175,6 +210,21 @@ class Arrow a => ArrowChoice a where
                        where   untag (Left x) = x
                                untag (Right y) = 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 f = f +++ id
        right f = id +++ f