1 {-# OPTIONS -XRankNTypes -XMultiParamTypeClasses -XNoMonomorphismRestriction -XTypeOperators -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,
38 import Control.Category
40 ------------------------------------------------------------------------
41 -- The main GArrow class
43 class Category g => GArrow g (**) u | (**) -> u, u -> (**) where
45 --comp :: g x y -> g y z -> g x z
46 ga_first :: g x y -> g (x ** z) (y ** z)
47 ga_second :: g x y -> g (z ** x) (z ** y)
48 ga_cancell :: g (u**x) x
49 ga_cancelr :: g (x**u) x
50 ga_uncancell :: g x (u**x)
51 ga_uncancelr :: g x (x**u)
52 ga_assoc :: g ((x** y)**z ) ( x**(y **z))
53 ga_unassoc :: g ( x**(y **z)) ((x** y)**z )
56 ------------------------------------------------------------------------
57 -- The three context-manipulation classes
59 class GArrow g (**) u => GArrowCopy g (**) u where
62 class GArrow g (**) u => GArrowDrop g (**) u where
65 class GArrow g (**) u => GArrowSwap g (**) u where
66 ga_swap :: g (x**y) (y**x)
69 ga_swap >>> ga_first f >>> ga_swap
70 -- implementation of ga_second for GArrowSwap
72 -- http://haskell.org/haskellwiki/Class_system_extension_proposal
73 -- "Allowing superclass methods to be overridden in derived classes";
74 -- if we had this we could do a better job here
78 ------------------------------------------------------------------------
79 -- Products, Coproducts, etc
82 class (GArrowDrop g (<*>) u,
83 GArrowCopy g (<*>) u) =>
86 class GArrow g (<+>) u =>
87 GArrowSum g (<+>) u where
88 ga_merge :: g (x<+>x) x
91 ga_inl :: GArrowSum g (<+>) u => g x (x<+>y)
92 ga_inl = ga_uncancelr >>> ga_second ga_never
94 ga_inr :: GArrowSum g (<+>) u => g x (y<+>x)
95 ga_inr = ga_uncancell >>> ga_first ga_never
98 ------------------------------------------------------------------------
101 class GArrow g (**) u => GArrowLoop g (**) u where
102 ga_loopl :: g (x**z) (y**z) -> g x y
103 ga_loopr :: g (z**x) (z**y) -> g x y
106 ------------------------------------------------------------------------
107 -- Literal. Note that ga_literal should never appear in (unflattened)
108 -- Haskell programs, though the user may wish to write implementations
109 -- of this function (I haven't yet found a way to enforce this
110 -- restriction using exports)
112 class GArrow g (**) u => GArrowLiteral g (**) u t r where
113 ga_literal :: t -> g u r
118 ------------------------------------------------------------------------
119 -- Constant and Run, which are dual to each other
121 class GArrow g (**) u => GArrowEval g (**) u r t where
122 ga_eval :: g u r -> t
124 class GArrow g (**) u => GArrowConstant g (**) u t r where
125 ga_constant :: t -> g u r
129 ------------------------------------------------------------------------
130 -- Reify and Reflect, which are "curried" versions
132 -- If you have this for R the identity map on types, you're basically
133 -- a Control.Arrow; you can also define essentially all the other
134 -- methods of GArrow, GArrowDrop, GArrowCopy, etc in terms of this.
135 class GArrow g (**) u => GArrowReify g (**) u x y r q where
136 ga_reify :: (x -> y) -> g r q
138 class GArrow g (**) u => GArrowReflect g (**) u r q x y where
139 ga_reflect :: g r q -> (x -> y)
144 ------------------------------------------------------------------------
147 class GArrow g (**) u => GArrowApply g (**) u (~>) where
148 ga_applyl :: g (x**(x~>y) ) y
149 ga_applyr :: g ( (x~>y)**x) y
151 class GArrow g (**) u => GArrowCurry g (**) u (~>) where
152 ga_curryl :: g (x**y) z -> g x (y~>z)
153 ga_curryr :: g (x**y) z -> g y (x~>z)
158 ------------------------------------------------------------------------
159 -- Commonly Implemented Collections of Classes
162 -- The simply typed KAPPA calculus; see Hasegawa, __Decomposing Typed
163 -- Lambda Calculus into a Couple of Categorical Programming
164 -- Languages__, http://dx.doi.org/10.1007/3-540-60164-3_28
166 class (GArrowDrop g (**) u,
168 GArrowSwap g (**) u) =>
171 -- The simply typed LAMBDA calculus
172 class (GArrowSTKC g (**) u,
173 GArrowCurry g (**) u (~>),
174 GArrowApply g (**) u (~>)) =>
175 GArrowSTLC g (**) u (~>)
177 -- Programming Language for Computable Functions (w/o integers and booleans)
178 class (GArrowSTLC g (**) u (~>),
179 GArrowLoop g (**) u) =>
180 GArrowPCF g (**) u (~>)