add laws to class documentation
[ghc.git] / libraries / base / Control / Arrow.hs
1 {-# LANGUAGE Trustworthy #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module : Control.Arrow
5 -- Copyright : (c) Ross Paterson 2002
6 -- License : BSD-style (see the LICENSE file in the distribution)
7 --
8 -- Maintainer : libraries@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
11 --
12 -- Basic arrow definitions, based on
13 -- * /Generalising Monads to Arrows/, by John Hughes,
14 -- /Science of Computer Programming/ 37, pp67-111, May 2000.
15 -- plus a couple of definitions ('returnA' and 'loop') from
16 -- * /A New Notation for Arrows/, by Ross Paterson, in /ICFP 2001/,
17 -- Firenze, Italy, pp229-240.
18 -- These papers and more information on arrows can be found at
19 -- <http://www.haskell.org/arrows/>.
20
21 module Control.Arrow (
22 -- * Arrows
23 Arrow(..), Kleisli(..),
24 -- ** Derived combinators
25 returnA,
26 (^>>), (>>^),
27 (>>>), (<<<), -- reexported
28 -- ** Right-to-left variants
29 (<<^), (^<<),
30 -- * Monoid operations
31 ArrowZero(..), ArrowPlus(..),
32 -- * Conditionals
33 ArrowChoice(..),
34 -- * Arrow application
35 ArrowApply(..), ArrowMonad(..), leftApp,
36 -- * Feedback
37 ArrowLoop(..)
38 ) where
39
40 import Prelude hiding (id,(.))
41
42 import Control.Monad
43 import Control.Monad.Fix
44 import Control.Category
45
46 infixr 5 <+>
47 infixr 3 ***
48 infixr 3 &&&
49 infixr 2 +++
50 infixr 2 |||
51 infixr 1 ^>>, >>^
52 infixr 1 ^<<, <<^
53
54 -- | The basic arrow class.
55 --
56 -- Minimal complete definition: 'arr' and 'first', satisfying the laws
57 --
58 -- * @'arr' id = 'id'@
59 --
60 -- * @'arr' (f >>> g) = 'arr' f >>> 'arr' g@
61 --
62 -- * @'first' ('arr' f) = 'arr' ('first' f)@
63 --
64 -- * @'first' (f >>> g) = 'first' f >>> 'first' g@
65 --
66 -- * @'first' f >>> 'arr' 'fst' = 'arr' 'fst' >>> f@
67 --
68 -- * @'first' f >>> 'arr' ('id' *** g) = 'arr' ('id' *** g) >>> 'first' f@
69 --
70 -- * @'first' ('first' f) >>> 'arr' 'assoc' = 'arr' 'assoc' >>> 'first' f@
71 --
72 -- where
73 --
74 -- > assoc ((a,b),c) = (a,(b,c))
75 --
76 -- The other combinators have sensible default definitions,
77 -- which may be overridden for efficiency.
78
79 class Category a => Arrow a where
80
81 -- | Lift a function to an arrow.
82 arr :: (b -> c) -> a b c
83
84 -- | Send the first component of the input through the argument
85 -- arrow, and copy the rest unchanged to the output.
86 first :: a b c -> a (b,d) (c,d)
87
88 -- | A mirror image of 'first'.
89 --
90 -- The default definition may be overridden with a more efficient
91 -- version if desired.
92 second :: a b c -> a (d,b) (d,c)
93 second f = arr swap >>> first f >>> arr swap
94 where
95 swap :: (x,y) -> (y,x)
96 swap ~(x,y) = (y,x)
97
98 -- | Split the input between the two argument arrows and combine
99 -- their output. Note that this is in general not a functor.
100 --
101 -- The default definition may be overridden with a more efficient
102 -- version if desired.
103 (***) :: a b c -> a b' c' -> a (b,b') (c,c')
104 f *** g = first f >>> second g
105
106 -- | Fanout: send the input to both argument arrows and combine
107 -- their output.
108 --
109 -- The default definition may be overridden with a more efficient
110 -- version if desired.
111 (&&&) :: a b c -> a b c' -> a b (c,c')
112 f &&& g = arr (\b -> (b,b)) >>> f *** g
113
114 {-# RULES
115 "compose/arr" forall f g .
116 (arr f) . (arr g) = arr (f . g)
117 "first/arr" forall f .
118 first (arr f) = arr (first f)
119 "second/arr" forall f .
120 second (arr f) = arr (second f)
121 "product/arr" forall f g .
122 arr f *** arr g = arr (f *** g)
123 "fanout/arr" forall f g .
124 arr f &&& arr g = arr (f &&& g)
125 "compose/first" forall f g .
126 (first f) . (first g) = first (f . g)
127 "compose/second" forall f g .
128 (second f) . (second g) = second (f . g)
129 #-}
130
131 -- Ordinary functions are arrows.
132
133 instance Arrow (->) where
134 arr f = f
135 first f = f *** id
136 second f = id *** f
137 -- (f *** g) ~(x,y) = (f x, g y)
138 -- sorry, although the above defn is fully H'98, nhc98 can't parse it.
139 (***) f g ~(x,y) = (f x, g y)
140
141 -- | Kleisli arrows of a monad.
142 newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }
143
144 instance Monad m => Category (Kleisli m) where
145 id = Kleisli return
146 (Kleisli f) . (Kleisli g) = Kleisli (\b -> g b >>= f)
147
148 instance Monad m => Arrow (Kleisli m) where
149 arr f = Kleisli (return . f)
150 first (Kleisli f) = Kleisli (\ ~(b,d) -> f b >>= \c -> return (c,d))
151 second (Kleisli f) = Kleisli (\ ~(d,b) -> f b >>= \c -> return (d,c))
152
153 -- | The identity arrow, which plays the role of 'return' in arrow notation.
154 returnA :: Arrow a => a b b
155 returnA = arr id
156
157 -- | Precomposition with a pure function.
158 (^>>) :: Arrow a => (b -> c) -> a c d -> a b d
159 f ^>> a = arr f >>> a
160
161 -- | Postcomposition with a pure function.
162 (>>^) :: Arrow a => a b c -> (c -> d) -> a b d
163 a >>^ f = a >>> arr f
164
165 -- | Precomposition with a pure function (right-to-left variant).
166 (<<^) :: Arrow a => a c d -> (b -> c) -> a b d
167 a <<^ f = a <<< arr f
168
169 -- | Postcomposition with a pure function (right-to-left variant).
170 (^<<) :: Arrow a => (c -> d) -> a b c -> a b d
171 f ^<< a = arr f <<< a
172
173 class Arrow a => ArrowZero a where
174 zeroArrow :: a b c
175
176 instance MonadPlus m => ArrowZero (Kleisli m) where
177 zeroArrow = Kleisli (\_ -> mzero)
178
179 -- | A monoid on arrows.
180 class ArrowZero a => ArrowPlus a where
181 -- | An associative operation with identity 'zeroArrow'.
182 (<+>) :: a b c -> a b c -> a b c
183
184 instance MonadPlus m => ArrowPlus (Kleisli m) where
185 Kleisli f <+> Kleisli g = Kleisli (\x -> f x `mplus` g x)
186
187 -- | Choice, for arrows that support it. This class underlies the
188 -- @if@ and @case@ constructs in arrow notation.
189 -- Minimal complete definition: 'left', satisfying the laws
190 --
191 -- * @'left' ('arr' f) = 'arr' ('left' f)@
192 --
193 -- * @'left' (f >>> g) = 'left' f >>> 'left' g@
194 --
195 -- * @'left' f >>> 'arr' 'Left' = 'arr' 'Left' >>> f@
196 --
197 -- * @'left' f >>> 'arr' ('id' +++ g) = 'arr' ('id' +++ g) >>> 'left' f@
198 --
199 -- * @'left' ('left' f) >>> 'arr' 'assocsum' = 'arr' 'assocsum' >>> 'left' f@
200 --
201 -- where
202 --
203 -- > assocsum (Left (Left x)) = Left x
204 -- > assocsum (Left (Right y)) = Right (Left y)
205 -- > assocsum (Right z) = Right (Right z)
206 --
207 -- The other combinators have sensible default definitions, which may
208 -- be overridden for efficiency.
209
210 class Arrow a => ArrowChoice a where
211
212 -- | Feed marked inputs through the argument arrow, passing the
213 -- rest through unchanged to the output.
214 left :: a b c -> a (Either b d) (Either c d)
215
216 -- | A mirror image of 'left'.
217 --
218 -- The default definition may be overridden with a more efficient
219 -- version if desired.
220 right :: a b c -> a (Either d b) (Either d c)
221 right f = arr mirror >>> left f >>> arr mirror
222 where
223 mirror :: Either x y -> Either y x
224 mirror (Left x) = Right x
225 mirror (Right y) = Left y
226
227 -- | Split the input between the two argument arrows, retagging
228 -- and merging their outputs.
229 -- Note that this is in general not a functor.
230 --
231 -- The default definition may be overridden with a more efficient
232 -- version if desired.
233 (+++) :: a b c -> a b' c' -> a (Either b b') (Either c c')
234 f +++ g = left f >>> right g
235
236 -- | Fanin: Split the input between the two argument arrows and
237 -- merge their outputs.
238 --
239 -- The default definition may be overridden with a more efficient
240 -- version if desired.
241 (|||) :: a b d -> a c d -> a (Either b c) d
242 f ||| g = f +++ g >>> arr untag
243 where
244 untag (Left x) = x
245 untag (Right y) = y
246
247 {-# RULES
248 "left/arr" forall f .
249 left (arr f) = arr (left f)
250 "right/arr" forall f .
251 right (arr f) = arr (right f)
252 "sum/arr" forall f g .
253 arr f +++ arr g = arr (f +++ g)
254 "fanin/arr" forall f g .
255 arr f ||| arr g = arr (f ||| g)
256 "compose/left" forall f g .
257 left f . left g = left (f . g)
258 "compose/right" forall f g .
259 right f . right g = right (f . g)
260 #-}
261
262 instance ArrowChoice (->) where
263 left f = f +++ id
264 right f = id +++ f
265 f +++ g = (Left . f) ||| (Right . g)
266 (|||) = either
267
268 instance Monad m => ArrowChoice (Kleisli m) where
269 left f = f +++ arr id
270 right f = arr id +++ f
271 f +++ g = (f >>> arr Left) ||| (g >>> arr Right)
272 Kleisli f ||| Kleisli g = Kleisli (either f g)
273
274 -- | Some arrows allow application of arrow inputs to other inputs.
275 -- Instances should satisfy the following laws:
276 --
277 -- * @'first' ('arr' (\\x -> 'arr' (\\y -> (x,y)))) >>> 'app' = 'id'@
278 --
279 -- * @'first' ('arr' (g >>>)) >>> 'app' = 'second' g >>> 'app'@
280 --
281 -- * @'first' ('arr' (>>> h)) >>> 'app' = 'app' >>> h@
282 --
283 -- Such arrows are equivalent to monads (see 'ArrowMonad').
284
285 class Arrow a => ArrowApply a where
286 app :: a (a b c, b) c
287
288 instance ArrowApply (->) where
289 app (f,x) = f x
290
291 instance Monad m => ArrowApply (Kleisli m) where
292 app = Kleisli (\(Kleisli f, x) -> f x)
293
294 -- | The 'ArrowApply' class is equivalent to 'Monad': any monad gives rise
295 -- to a 'Kleisli' arrow, and any instance of 'ArrowApply' defines a monad.
296
297 newtype ArrowMonad a b = ArrowMonad (a () b)
298
299 instance ArrowApply a => Monad (ArrowMonad a) where
300 return x = ArrowMonad (arr (\_ -> x))
301 ArrowMonad m >>= f = ArrowMonad $
302 m >>> arr (\x -> let ArrowMonad h = f x in (h, ())) >>> app
303
304 -- | Any instance of 'ArrowApply' can be made into an instance of
305 -- 'ArrowChoice' by defining 'left' = 'leftApp'.
306
307 leftApp :: ArrowApply a => a b c -> a (Either b d) (Either c d)
308 leftApp f = arr ((\b -> (arr (\() -> b) >>> f >>> arr Left, ())) |||
309 (\d -> (arr (\() -> d) >>> arr Right, ()))) >>> app
310
311 -- | The 'loop' operator expresses computations in which an output value
312 -- is fed back as input, although the computation occurs only once.
313 -- It underlies the @rec@ value recursion construct in arrow notation.
314 -- 'loop' should satisfy the following laws:
315 --
316 -- [/extension/]
317 -- @'loop' ('arr' f) = 'arr' (\\ b -> 'fst' ('fix' (\\ (c,d) -> f (b,d))))@
318 --
319 -- [/left tightening/]
320 -- @'loop' ('first' h >>> f) = h >>> 'loop' f@
321 --
322 -- [/right tightening/]
323 -- @'loop' (f >>> 'first' h) = 'loop' f >>> h@
324 --
325 -- [/sliding/]
326 -- @'loop' (f >>> 'arr' ('id' *** k)) = 'loop' ('arr' ('id' *** k) >>> f)@
327 --
328 -- [/vanishing/]
329 -- @'loop' ('loop' f) = 'loop' ('arr' unassoc >>> f >>> 'arr' assoc)@
330 --
331 -- [/superposing/]
332 -- @'second' ('loop' f) = 'loop' ('arr' assoc >>> 'second' f >>> 'arr' unassoc)@
333 --
334 -- where
335 --
336 -- > assoc ((a,b),c) = (a,(b,c))
337 -- > unassoc (a,(b,c)) = ((a,b),c)
338 --
339 class Arrow a => ArrowLoop a where
340 loop :: a (b,d) (c,d) -> a b c
341
342 instance ArrowLoop (->) where
343 loop f b = let (c,d) = f (b,d) in c
344
345 -- | Beware that for many monads (those for which the '>>=' operation
346 -- is strict) this instance will /not/ satisfy the right-tightening law
347 -- required by the 'ArrowLoop' class.
348 instance MonadFix m => ArrowLoop (Kleisli m) where
349 loop (Kleisli f) = Kleisli (liftM fst . mfix . f')
350 where f' x y = f (x, snd y)