fix nhc98 build: need a qualified Prelude import
[ghc-base.git] / Control / Arrow.hs
index 2710be6..66c7745 100644 (file)
@@ -25,7 +25,7 @@ module Control.Arrow (
                returnA,
                (^>>), (>>^),
                -- ** Right-to-left variants
-               (<<<), (<<^), (^<<),
+               (<<^), (^<<),
                -- * Monoid operations
                ArrowZero(..), ArrowPlus(..),
                -- * Conditionals
@@ -36,25 +36,27 @@ module Control.Arrow (
                ArrowLoop(..)
        ) where
 
-import Prelude
+import Prelude hiding (id,(.))
+import qualified Prelude
 
 import Control.Monad
 import Control.Monad.Fix
+import Control.Category
 
 infixr 5 <+>
 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),
---   as well as '>>>' and 'first'.  The other combinators have sensible
+--   as well as 'first'.  The other combinators have sensible
 --   default definitions, which may be overridden for efficiency.
 
-class Arrow a where
+class Category a => Arrow a where
 
        -- | Lift a function to an arrow: you must define either this
        --   or 'pure'.
@@ -65,9 +67,6 @@ class Arrow a where
        pure :: (b -> c) -> a b c
        pure = arr
 
-       -- | Left-to-right composition of arrows.
-       (>>>) :: a b c -> a c d -> a b d
-
        -- | 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)
@@ -97,8 +96,10 @@ class Arrow a where
        f &&& g = arr (\b -> (b,b)) >>> f *** g
 
 {-# RULES
+"identity"
+               arr id = id
 "compose/arr"  forall f g .
-               arr f >>> arr g = arr (f >>> g)
+               (arr f) . (arr g) = arr (f . g)
 "first/arr"    forall f .
                first (arr f) = arr (first f)
 "second/arr"   forall f .
@@ -108,16 +109,15 @@ class Arrow a where
 "fanout/arr"   forall f g .
                arr f &&& arr g = arr (f &&& g)
 "compose/first"        forall f g .
-               first f >>> first g = first (f >>> g)
+               (first f) . (first g) = first (f . g)
 "compose/second" forall f g .
-               second f >>> second g = second (f >>> g)
+               (second f) . (second g) = second (f . g)
  #-}
 
 -- Ordinary functions are arrows.
 
 instance Arrow (->) where
        arr f = f
-       f >>> g = g . f
        first f = f *** id
        second f = id *** f
 --     (f *** g) ~(x,y) = (f x, g y)
@@ -128,9 +128,12 @@ instance Arrow (->) where
 
 newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }
 
+instance Monad m => Category (Kleisli m) where
+       id = Kleisli return
+       (Kleisli f) . (Kleisli g) = Kleisli (\b -> g b >>= f)
+
 instance Monad m => Arrow (Kleisli m) where
        arr f = Kleisli (return . f)
-       Kleisli f >>> Kleisli g = Kleisli (\b -> f b >>= g)
        first (Kleisli f) = Kleisli (\ ~(b,d) -> f b >>= \c -> return (c,d))
        second (Kleisli f) = Kleisli (\ ~(d,b) -> f b >>= \c -> return (d,c))
 
@@ -147,10 +150,6 @@ f ^>> a = arr f >>> a
 (>>^) :: 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