Update Trac ticket URLs to point to GitLab
[ghc.git] / libraries / base / Control / Category.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE GADTs #-}
3 {-# LANGUAGE NoImplicitPrelude #-}
4 {-# LANGUAGE PolyKinds #-}
5 {-# OPTIONS_GHC -Wno-inline-rule-shadowing #-}
6 -- The RULES for the methods of class Category may never fire
7 -- e.g. identity/left, identity/right, association; see #10528
8
9 -----------------------------------------------------------------------------
10 -- |
11 -- Module : Control.Category
12 -- Copyright : (c) Ashley Yakeley 2007
13 -- License : BSD-style (see the LICENSE file in the distribution)
14 --
15 -- Maintainer : ashley@semantic.org
16 -- Stability : experimental
17 -- Portability : portable
18
19 -- https://gitlab.haskell.org/ghc/ghc/issues/1773
20
21 module Control.Category where
22
23 import qualified GHC.Base (id,(.))
24 import Data.Type.Coercion
25 import Data.Type.Equality
26 import GHC.Prim (coerce)
27
28 infixr 9 .
29 infixr 1 >>>, <<<
30
31 -- | A class for categories. Instances should satisfy the laws
32 --
33 -- [Right identity] @f '.' 'id' = f@
34 -- [Left identity] @'id' '.' f = f@
35 -- [Associativity] @f '.' (g '.' h) = (f '.' g) '.' h@
36 --
37 class Category cat where
38 -- | the identity morphism
39 id :: cat a a
40
41 -- | morphism composition
42 (.) :: cat b c -> cat a b -> cat a c
43
44 {-# RULES
45 "identity/left" forall p .
46 id . p = p
47 "identity/right" forall p .
48 p . id = p
49 "association" forall p q r .
50 (p . q) . r = p . (q . r)
51 #-}
52
53 -- | @since 3.0
54 instance Category (->) where
55 id = GHC.Base.id
56 (.) = (GHC.Base..)
57
58 -- | @since 4.7.0.0
59 instance Category (:~:) where
60 id = Refl
61 Refl . Refl = Refl
62
63 -- | @since 4.10.0.0
64 instance Category (:~~:) where
65 id = HRefl
66 HRefl . HRefl = HRefl
67
68 -- | @since 4.7.0.0
69 instance Category Coercion where
70 id = Coercion
71 (.) Coercion = coerce
72
73 -- | Right-to-left composition
74 (<<<) :: Category cat => cat b c -> cat a b -> cat a c
75 (<<<) = (.)
76
77 -- | Left-to-right composition
78 (>>>) :: Category cat => cat a b -> cat b c -> cat a c
79 f >>> g = g . f