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