make Control.Category a superclass of GArrow
[ghc-base.git] / GHC / HetMet / GArrow.hs
1 {-# OPTIONS -XRankNTypes -XMultiParamTypeClasses -XNoMonomorphismRestriction -XTypeOperators -XFlexibleInstances -XFunctionalDependencies #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  GHC.HetMet.GArrow
5 -- Copyright   :  none
6 -- License     :  public domain
7 --
8 -- Maintainer  :  Adam Megacz <megacz@acm.org>
9 -- Stability   :  experimental
10 -- Portability :  portable
11
12 module GHC.HetMet.GArrow (
13   GArrow(..),
14   GArrowDrop(..),
15   GArrowCopy(..),
16   GArrowSwap(..),
17   GArrowLoop(..),
18   GArrowReify(..),
19   GArrowReflect(..)
20 ) where
21 import Control.Arrow
22 import Control.Category
23
24 class Category g => GArrow g (**) | g -> (**) where
25   ga_first     :: g x y -> g (x ** z) (y ** z)
26   ga_second    :: g x y -> g (z ** x) (z ** y)
27   ga_cancell   :: g (()**x) x
28   ga_cancelr   :: g (x**()) x
29   ga_uncancell :: g x       (()**x)
30   ga_uncancelr :: g x       (x**())
31   ga_assoc     :: g ((x**y)**z) (x**(y**z))
32   ga_unassoc   :: g (x**(y**z)) ((x**y)**z)
33
34 class GArrow g (**) => GArrowDrop g (**) where
35   ga_drop      :: g x ()
36
37 class GArrow g (**) => GArrowCopy g (**) where
38   ga_copy      :: g x (x**x)
39
40 class GArrow g (**) => GArrowSwap g (**) where
41   ga_swap          :: g (x**y) (y**x)
42
43 -- implementation of ga_second for GArrowSwap instances
44 ga_swap_second f = ga_swap >>> ga_first f >>> ga_swap
45
46 class GArrow g (**) => GArrowLoop g (**) where
47   ga_loop      :: g (x**z) (y**z) -> g x y
48
49 class GArrow g (**) => GArrowLiteral g (**) a where
50   ga_literal   :: a -> g () a
51
52 -- not sure -- subject to change
53 class GArrow g (**) => GArrowReify g (**) where
54   ga_reify     :: (x -> y) -> g x y
55
56 -- not sure -- subject to change
57 class GArrow g (**) => GArrowReflect g (**) where
58   ga_reflect   :: g x y -> (x -> y)
59
60
61
62 ------------------------------------------------------------------------------
63 -- GArrow instances for Control.Arrow
64
65 instance Arrow a => GArrow a (,) where
66   ga_first     =  first
67   ga_second    =  second
68   ga_cancell   =  arr (\((),x) -> x)
69   ga_cancelr   =  arr (\(x,()) -> x)
70   ga_uncancell =  arr (\x -> ((),x))
71   ga_uncancelr =  arr (\x -> (x,()))
72   ga_assoc     =  arr (\((x,y),z) -> (x,(y,z)))
73   ga_unassoc   =  arr (\(x,(y,z)) -> ((x,y),z))
74   
75 instance Arrow a => GArrowDrop a (,) where
76   ga_drop      =  arr (\x -> ())
77
78 instance Arrow a => GArrowCopy a (,) where
79   ga_copy      =  arr (\x -> (x,x))
80
81 instance Arrow a => GArrowSwap a (,) where
82   ga_swap      =  arr (\(x,y) -> (y,x))
83
84 instance Arrow a => GArrowLiteral a (,) b where
85   ga_literal x =  arr (\() -> x)
86
87 instance Arrow a => GArrowReify a (,) where
88   ga_reify     =  arr
89
90 instance ArrowLoop a => GArrowLoop a (,) where
91   ga_loop      =  loop
92
93
94
95