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