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