From f794b53658a22a1464db7168f9821c194bcf5268 Mon Sep 17 00:00:00 2001 From: Ashley Yakeley Date: Mon, 29 Oct 2007 02:25:26 +0000 Subject: [PATCH] new Control.Category, ghc ticket #1773 --- Control/Applicative.hs | 6 ++---- Control/Arrow.hs | 20 ++++++++++---------- Control/Category.hs | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ Control/Compositor.hs | 37 ------------------------------------ base.cabal | 2 +- 5 files changed, 62 insertions(+), 52 deletions(-) create mode 100644 Control/Category.hs delete mode 100644 Control/Compositor.hs diff --git a/Control/Applicative.hs b/Control/Applicative.hs index cb76bcc..c54a346 100644 --- a/Control/Applicative.hs +++ b/Control/Applicative.hs @@ -35,11 +35,9 @@ module Control.Applicative ( optional, some, many ) where -#ifdef __HADDOCK__ -import Prelude -#endif +import Prelude hiding (id,(.)) -import Control.Compositor +import Control.Category import Control.Arrow (Arrow(arr, (&&&)), ArrowZero(zeroArrow), ArrowPlus((<+>))) import Control.Monad (liftM, ap, MonadPlus(..)) diff --git a/Control/Arrow.hs b/Control/Arrow.hs index a339adf..76c0212 100644 --- a/Control/Arrow.hs +++ b/Control/Arrow.hs @@ -36,11 +36,11 @@ module Control.Arrow ( ArrowLoop(..) ) where -import Prelude +import Prelude hiding (id,(.)) import Control.Monad import Control.Monad.Fix -import Control.Compositor +import Control.Category infixr 5 <+> infixr 3 *** @@ -55,7 +55,7 @@ infixr 1 ^<<, <<^ -- as well as 'first'. The other combinators have sensible -- default definitions, which may be overridden for efficiency. -class Compositor a => Arrow a where +class Category a => Arrow a where -- | Lift a function to an arrow: you must define either this -- or 'pure'. @@ -96,9 +96,9 @@ class Compositor a => Arrow a where {-# RULES "identity" - arr id = 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,9 +108,9 @@ class Compositor a => 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. @@ -127,9 +127,9 @@ instance Arrow (->) where newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b } -instance Monad m => Compositor (Kleisli m) where - identity = Kleisli return - Kleisli f >>> Kleisli g = Kleisli (\b -> f b >>= g) +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) diff --git a/Control/Category.hs b/Control/Category.hs new file mode 100644 index 0000000..13fba06 --- /dev/null +++ b/Control/Category.hs @@ -0,0 +1,49 @@ +----------------------------------------------------------------------------- +-- | +-- Module : Control.Category +-- Copyright : (c) Ashley Yakeley 2007 +-- License : BSD-style (see the LICENSE file in the distribution) +-- +-- Maintainer : ashley@semantic.org +-- Stability : experimental +-- Portability : portable + +-- http://hackage.haskell.org/trac/ghc/ticket/1773 + +module Control.Category where + +import Prelude hiding (id,(.)) +import qualified Prelude + +infixr 9 . +infixr 1 >>>, <<< + +-- | A class for categories. +-- id and (.) must form a monoid. +class Category cat where + -- | the identity morphism + id :: cat a a + + -- | morphism composition + (.) :: cat b c -> cat a b -> cat a c + +{-# RULES +"identity/left" forall p . + id . p = p +"identity/right" forall p . + p . id = p +"association" forall p q r . + (p . q) . r = p . (q . r) + #-} + +instance Category (->) where + id = Prelude.id + (.) = (Prelude..) + +-- | Right-to-left composition +(<<<) :: Category cat => cat b c -> cat a b -> cat a c +(<<<) = (.) + +-- | Left-to-right composition +(>>>) :: Category cat => cat a b -> cat b c -> cat a c +f >>> g = g . f diff --git a/Control/Compositor.hs b/Control/Compositor.hs deleted file mode 100644 index 7cc6381..0000000 --- a/Control/Compositor.hs +++ /dev/null @@ -1,37 +0,0 @@ ------------------------------------------------------------------------------ --- | --- Module : Control.Compositor --- Copyright : (c) Ashley Yakeley 2007 --- License : BSD-style (see the LICENSE file in the distribution) --- --- Maintainer : ashley@semantic.org --- Stability : experimental --- Portability : portable - -module Control.Compositor where - -infixr 1 >>>, <<< - -class Compositor comp where - identity :: comp a a - - -- | Left-to-right composition - (>>>) :: comp a b -> comp b c -> comp a c - -{-# RULES -"identity/left" forall p . - identity >>> p = p -"identity/right" forall p . - p >>> identity = p -"association" forall p q r . - (p >>> q) >>> r = p >>> (q >>> r) - #-} - -instance Compositor (->) where - identity = id - p >>> q = q . p - --- | Right-to-left composition -(<<<) :: Compositor comp => comp b c -> comp a b -> comp a c -f <<< g = g >>> f - diff --git a/base.cabal b/base.cabal index 14bcf08..376c15b 100644 --- a/base.cabal +++ b/base.cabal @@ -69,7 +69,7 @@ Library { exposed-modules: Control.Applicative, Control.Arrow, - Control.Compositor, + Control.Category, Control.Concurrent, Control.Concurrent.Chan, Control.Concurrent.MVar, -- 1.7.10.4