add GArrow instance for Control.Arrow
[ghc-base.git] / GHC / HetMet / GArrow.hs
1 {-# OPTIONS -XRankNTypes -XMultiParamTypeClasses -XNoMonomorphismRestriction -XTypeOperators -XFlexibleInstances #-}
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
23 class GArrow g (**) where
24   ga_id        :: g x x
25   ga_comp      :: g x y -> g y z -> g x z
26   ga_first     :: g x y -> g (x ** z) (y ** z)
27   ga_second    :: g x y -> g (z ** x) (z ** y)
28   ga_cancell   :: g (()**x) x
29   ga_cancelr   :: g (x**()) x
30   ga_uncancell :: g x       (()**x)
31   ga_uncancelr :: g x       (x**())
32   ga_assoc     :: g ((x**y)**z) (x**(y**z))
33   ga_unassoc   :: g (x**(y**z)) ((x**y)**z)
34
35 class GArrow g (**) => GArrowDrop g (**) where
36   ga_drop      :: g x ()
37
38 class GArrow g (**) => GArrowCopy g (**) where
39   ga_copy      :: g x (x**x)
40
41 class GArrow g (**) => GArrowSwap g (**) where
42   ga_swap          :: g (x**y) (y**x)
43
44 -- implementation of ga_second for GArrowSwap instances
45 ga_swap_second f = ga_comp (ga_comp ga_swap (ga_first f)) ga_swap
46
47 class GArrow g (**) => GArrowLoop g (**) where
48   ga_loop      :: g (x**z) (y**z) -> g x y
49
50 class GArrow g (**) => GArrowLiteral g (**) a where
51   ga_literal   :: a -> g () a
52
53 -- not sure -- subject to change
54 class GArrow g (**) => GArrowReify g (**) where
55   ga_reify     :: (x -> y) -> g x y
56
57 -- not sure -- subject to change
58 class GArrow g (**) => GArrowReflect g (**) where
59   ga_reflect   :: g x y -> (x -> y)
60
61
62
63 ------------------------------------------------------------------------------
64 -- GArrow instances for Control.Arrow
65
66 instance Arrow a => GArrow a (,) where
67   ga_id        =  arr Prelude.id
68   ga_comp      =  (>>>)
69   ga_first     =  first
70   ga_second    =  second
71   ga_cancell   =  arr (\((),x) -> x)
72   ga_cancelr   =  arr (\(x,()) -> x)
73   ga_uncancell =  arr (\x -> ((),x))
74   ga_uncancelr =  arr (\x -> (x,()))
75   ga_assoc     =  arr (\((x,y),z) -> (x,(y,z)))
76   ga_unassoc   =  arr (\(x,(y,z)) -> ((x,y),z))
77   
78 instance Arrow a => GArrowDrop a (,) where
79   ga_drop      =  arr (\x -> ())
80
81 instance Arrow a => GArrowCopy a (,) where
82   ga_copy      =  arr (\x -> (x,x))
83
84 instance Arrow a => GArrowSwap a (,) where
85   ga_swap      =  arr (\(x,y) -> (y,x))
86
87 instance Arrow a => GArrowLiteral a (,) where
88   ga_literal x =  arr (\() -> x)
89
90 instance Arrow a => GArrowReify a (,) where
91   ga_reify     =  arr
92
93 instance ArrowLoop a => GArrowLoop a (,) where
94   ga_loop      =  loop
95
96