770d8502028a956eb59e4a4bcc18674a5945d4c2
[ghc-base.git] / Control / Category.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Control.Category
4 -- Copyright   :  (c) Ashley Yakeley 2007
5 -- License     :  BSD-style (see the LICENSE file in the distribution)
6 --
7 -- Maintainer  :  ashley@semantic.org
8 -- Stability   :  experimental
9 -- Portability :  portable
10
11 -- http://hackage.haskell.org/trac/ghc/ticket/1773
12
13 module Control.Category where
14
15 import Prelude hiding (id,(.))
16 import qualified Prelude
17
18 infixr 9 .
19 infixr 1 >>>, <<<
20
21 -- | A class for categories.
22 --   id and (.) must form a monoid.
23 class Category cat where
24         -- | the identity morphism
25         id :: cat a a
26
27         -- | morphism composition
28         (.) :: cat b c -> cat a b -> cat a c
29
30 {-# RULES
31 "identity/left" forall p .
32                 id . p = p
33 "identity/right"        forall p .
34                 p . id = p
35 "association"   forall p q r .
36                 (p . q) . r = p . (q . r)
37  #-}
38
39 instance Category (->) where
40         id = Prelude.id
41 #ifndef __HADDOCK__
42 -- Haddock 1.x cannot parse this:
43         (.) = (Prelude..)
44 #endif
45
46 -- | Right-to-left composition
47 (<<<) :: Category cat => cat b c -> cat a b -> cat a c
48 (<<<) = (.)
49
50 -- | Left-to-right composition
51 (>>>) :: Category cat => cat a b -> cat b c -> cat a c
52 f >>> g = g . f