1 {-# LANGUAGE MultiParamTypeClasses, TypeOperators, FunctionalDependencies, TypeFamilies, FlexibleContexts #-}
2 -----------------------------------------------------------------------------
4 -- Module : Control.GArrow
6 -- License : public domain
8 -- Maintainer : Adam Megacz <megacz@acm.org>
9 -- Stability : experimental
10 -- Portability : portable
12 module Control.GArrow (
22 GArrowLiteral(..), -- should be implemented, but never invoked, by user code
24 GArrowSum(..), ga_inl, ga_inr,
37 GArrowCopyDropSwap(..),
38 GArrowCopyDropSwapLoop(..),
43 import Control.Category hiding ((.))
44 import Prelude hiding (id)
46 ------------------------------------------------------------------------
47 -- The main GArrow class
49 class Category g => GArrow g (**) u | g (**) -> u, g u -> (**) where
51 --comp :: g x y -> g y z -> g x z
52 ga_first :: g x y -> g (x ** z) (y ** z)
53 ga_second :: g x y -> g (z ** x) (z ** y)
54 ga_cancell :: g (u**x) x
55 ga_cancelr :: g (x**u) x
56 ga_uncancell :: g x (u**x)
57 ga_uncancelr :: g x (x**u)
58 ga_assoc :: g ((x** y)**z ) ( x**(y **z))
59 ga_unassoc :: g ( x**(y **z)) ((x** y)**z )
62 ------------------------------------------------------------------------
63 -- The three context-manipulation classes
65 class GArrow g (**) u => GArrowCopy g (**) u where
68 class GArrow g (**) u => GArrowDrop g (**) u where
71 class GArrow g (**) u => GArrowSwap g (**) u where
72 ga_swap :: g (x**y) (y**x)
75 ga_swap >>> ga_first f >>> ga_swap
76 -- implementation of ga_second for GArrowSwap
78 -- http://haskell.org/haskellwiki/Class_system_extension_proposal
79 -- "Allowing superclass methods to be overridden in derived classes";
80 -- if we had this we could do a better job here
86 ------------------------------------------------------------------------
87 -- Products, Coproducts, etc
90 class (GArrowDrop g (<*>) u,
91 GArrowCopy g (<*>) u) =>
94 class GArrow g (<+>) u =>
95 GArrowSum g (<+>) u where
96 ga_merge :: g (x<+>x) x
99 ga_inl :: GArrowSum g (<+>) u => g x (x<+>y)
100 ga_inl = ga_uncancelr >>> ga_second ga_never
102 ga_inr :: GArrowSum g (<+>) u => g x (y<+>x)
103 ga_inr = ga_uncancell >>> ga_first ga_never
106 ------------------------------------------------------------------------
109 class GArrow g (**) u => GArrowLoop g (**) u where
110 ga_loopr :: g (x**z) (y**z) -> g x y
111 ga_loopl :: g (z**x) (z**y) -> g x y
114 ------------------------------------------------------------------------
115 -- Literal. Note that ga_literal should never appear in (unflattened)
116 -- Haskell programs, though the user may wish to write implementations
117 -- of this function (I haven't yet found a way to enforce this
118 -- restriction using exports)
120 class GArrow g (**) u => GArrowLiteral g (**) u t r where
121 ga_literal :: t -> g u r
126 ------------------------------------------------------------------------
127 -- Constant and Run, which are dual to each other
129 class GArrow g (**) u => GArrowEval g (**) u r t where
130 ga_eval :: g u r -> t
132 class GArrow g (**) u => GArrowConstant g (**) u t r where
133 ga_constant :: t -> g u r
139 ------------------------------------------------------------------------
140 -- Reify and Reflect, which are "curried" versions of eval/const
142 -- If you have this for R the identity map on types, you're basically
143 -- a Control.Arrow; you can also define essentially all the other
144 -- methods of GArrow, GArrowDrop, GArrowCopy, etc in terms of this.
145 class GArrow g (**) u => GArrowReify g (**) u x y r q where
146 ga_reify :: (x -> y) -> g r q
148 class GArrow g (**) u => GArrowReflect g (**) u r q x y where
149 ga_reflect :: g r q -> (x -> y)
155 ------------------------------------------------------------------------
158 class GArrow g (**) u => GArrowApply g (**) u (~>) where
159 ga_applyl :: g (x**(x~>y) ) y
160 ga_applyr :: g ( (x~>y)**x) y
162 class GArrow g (**) u => GArrowCurry g (**) u (~>) where
163 ga_curryl :: g (x**y) z -> g x (y~>z)
164 ga_curryr :: g (x**y) z -> g y (x~>z)
170 ------------------------------------------------------------------------
174 -- The GArrow and GArrow{Copy,Drop,Swap} classes brandish their tensor
175 -- and unit types; this is important because we might want to have
176 -- both "instance GArrow g X Y" and "instance GArrow g Z Q" -- in
177 -- fact, this is exactly how sums and pairs are defined.
179 -- However, in daily practice it's a pain to have all those extra type
180 -- variables floating around. If you'd like to hide them, you can use
181 -- the type families below to do so; see the definition of class
182 -- GArrowCopyDropSwap for an example. Keep in mind, however, that any given
183 -- type may only have a single instance declared using the type
187 type family GArrowTensor g :: * -> * -> * -- (**)
188 type family GArrowUnit g :: * -- ()
189 type family GArrowExponent g :: * -> * -> * -- (~>)
194 ------------------------------------------------------------------------
195 -- Commonly Implemented Collections of Classes
197 class (GArrowDrop g (GArrowTensor g) (GArrowUnit g),
198 GArrowCopy g (GArrowTensor g) (GArrowUnit g),
199 GArrowSwap g (GArrowTensor g) (GArrowUnit g)) =>
202 class (GArrowDrop g (GArrowTensor g) (GArrowUnit g),
203 GArrowCopy g (GArrowTensor g) (GArrowUnit g),
204 GArrowSwap g (GArrowTensor g) (GArrowUnit g),
205 GArrowLoop g (GArrowTensor g) (GArrowUnit g)) =>
206 GArrowCopyDropSwapLoop g
208 -- The simply typed LAMBDA calculus
209 class (GArrowDrop g (GArrowTensor g) (GArrowUnit g),
210 GArrowCopy g (GArrowTensor g) (GArrowUnit g),
211 GArrowSwap g (GArrowTensor g) (GArrowUnit g),
212 GArrowCurry g (GArrowTensor g) (GArrowUnit g) (GArrowExponent g),
213 GArrowApply g (GArrowTensor g) (GArrowUnit g) (GArrowExponent g)
217 -- Programming Language for Computable Functions (w/o integers and booleans)
218 class (GArrowDrop g (GArrowTensor g) (GArrowUnit g),
219 GArrowCopy g (GArrowTensor g) (GArrowUnit g),
220 GArrowSwap g (GArrowTensor g) (GArrowUnit g),
221 GArrowCurry g (GArrowTensor g) (GArrowUnit g) (GArrowExponent g),
222 GArrowApply g (GArrowTensor g) (GArrowUnit g) (GArrowExponent g),
223 GArrowLoop g (GArrowTensor g) (GArrowUnit g)
225 GArrowPCF g (**) u (~>)
231 ------------------------------------------------------------------------
232 -- Experimental, Not Yet Exported
234 -- See Lindley, Wadler, and Yallop '08 -- except that here ga_force
235 -- is primitive since there is no "arr" to define it in terms of.
236 class GArrow g (**) u => GArrowStatic g (**) u (~>) where
237 ga_delay :: g a b -> g u (a~>b)
238 ga_force :: g u (a~>b) -> g a b
239 -- "ga_static/force_delay" forall a . force (delay a) = a
240 -- "ga_static/delay_force" forall a . delay (force a) = a