1 {-# OPTIONS -XRankNTypes -XMultiParamTypeClasses -XNoMonomorphismRestriction -XTypeOperators -XFlexibleInstances -XFunctionalDependencies #-}
2 -----------------------------------------------------------------------------
4 -- Module : GHC.HetMet.GArrow
6 -- License : public domain
8 -- Maintainer : Adam Megacz <megacz@acm.org>
9 -- Stability : experimental
10 -- Portability : portable
12 module GHC.HetMet.GArrow (
22 GArrowLiteral(..), -- should be implemented, but never invoked, by user code
24 GArrowSum(..), ga_inl, ga_inr,
33 import Control.Category
35 ------------------------------------------------------------------------
36 -- The main GArrow class
38 class Category g => GArrow g (**) u | (**) -> u where
40 --comp :: g x y -> g y z -> g x z
41 ga_first :: g x y -> g (x ** z) (y ** z)
42 ga_second :: g x y -> g (z ** x) (z ** y)
43 ga_cancell :: g (u**x) x
44 ga_cancelr :: g (x**u) x
45 ga_uncancell :: g x (u**x)
46 ga_uncancelr :: g x (x**u)
47 ga_assoc :: g ((x** y)**z ) ( x**(y **z))
48 ga_unassoc :: g ( x**(y **z)) ((x** y)**z )
51 ------------------------------------------------------------------------
52 -- The three context-manipulation classes
54 class GArrow g (**) u => GArrowCopy g (**) u where
57 class GArrow g (**) u => GArrowDrop g (**) u where
60 class GArrow g (**) u => GArrowSwap g (**) u where
61 ga_swap :: g (x**y) (y**x)
64 ga_swap >>> ga_first f >>> ga_swap
65 -- implementation of ga_second for GArrowSwap
67 -- http://haskell.org/haskellwiki/Class_system_extension_proposal
68 -- "Allowing superclass methods to be overridden in derived classes";
69 -- if we had this we could do a better job here
73 ------------------------------------------------------------------------
74 -- Products, Coproducts, etc
77 class (GArrow g (**) u,
79 GArrowProd g (**) u (<*>) v where
80 ga_prod_copy :: g x (x<*>x)
83 class (GArrow g (**) u,
85 GArrowSum g (**) u (<+>) v where
86 ga_merge :: g (x<+>x) x
89 ga_inl = ga_uncancelr >>> ga_second ga_never
90 ga_inr = ga_uncancell >>> ga_first ga_never
92 ------------------------------------------------------------------------
95 class GArrow g (**) u => GArrowLoop g (**) u where
96 ga_loopl :: g (x**z) (y**z) -> g x y
97 ga_loopr :: g (z**x) (z**y) -> g x y
100 ------------------------------------------------------------------------
101 -- Literal. Note that ga_literal should never appear in (unflattened)
102 -- Haskell programs, though the user may wish to write implementations
103 -- of this function (I haven't yet found a way to enforce this
104 -- restriction using exports)
106 class GArrow g (**) u => GArrowLiteral g (**) u t r where
107 ga_literal :: t -> g u r
112 ------------------------------------------------------------------------
113 -- Constant and Run, which are dual to each other
115 class GArrow g (**) u => GArrowEval g (**) u r t where
116 ga_eval :: g u r -> t
118 class GArrow g (**) u => GArrowConstant g (**) u t r where
119 ga_constant :: t -> g u r
123 ------------------------------------------------------------------------
124 -- Reify and Reflect, which are "curried" versions
126 -- If you have this for R the identity map on types, you're basically
127 -- a Control.Arrow; you can also define essentially all the other
128 -- methods of GArrow, GArrowDrop, GArrowCopy, etc in terms of this.
129 class GArrow g (**) u => GArrowReify g (**) u x y r q where
130 ga_reify :: (x -> y) -> g r q
132 class GArrow g (**) u => GArrowReflect g (**) u r q x y where
133 ga_reflect :: g r q -> (x -> y)
138 ------------------------------------------------------------------------
141 class GArrow g (**) u => GArrowApply g (**) u (~>) where
142 ga_applyl :: g (x**(x~>y) ) y
143 ga_applyr :: g ( (x~>y)**x) y
145 class GArrow g (**) u => GArrowCurry g (**) u (~>) where
146 ga_curryl :: g (x**y) z -> g x (y~>z)
147 ga_curryr :: g (x**y) z -> g y (x~>z)