Merge branch 'master' into type-nats
[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 : experimental
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 -- See these papers for the equations these combinators are expected to
19 -- satisfy. These papers and more information on arrows can be found at
20 -- <http://www.haskell.org/arrows/>.
21
22 module Control.Arrow (
23 -- * Arrows
24 Arrow(..), Kleisli(..),
25 -- ** Derived combinators
26 returnA,
27 (^>>), (>>^),
28 (>>>), (<<<), -- reexported
29 -- ** Right-to-left variants
30 (<<^), (^<<),
31 -- * Monoid operations
32 ArrowZero(..), ArrowPlus(..),
33 -- * Conditionals
34 ArrowChoice(..),
35 -- * Arrow application
36 ArrowApply(..), ArrowMonad(..), leftApp,
37 -- * Feedback
38 ArrowLoop(..)
39 ) where
40
41 import Prelude hiding (id,(.))
42
43 import Control.Monad
44 import Control.Monad.Fix
45 import Control.Category
46
47 infixr 5 <+>
48 infixr 3 ***
49 infixr 3 &&&
50 infixr 2 +++
51 infixr 2 |||
52 infixr 1 ^>>, >>^
53 infixr 1 ^<<, <<^
54
55 -- | The basic arrow class.
56 --
57 -- Minimal complete definition: 'arr' and 'first'.
58 --
59 -- The other combinators have sensible default definitions,
60 -- which may be overridden for efficiency.
61
62 class Category a => Arrow a where
63
64 -- | Lift a function to an arrow.
65 arr :: (b -> c) -> a b c
66
67 -- | Send the first component of the input through the argument
68 -- arrow, and copy the rest unchanged to the output.
69 first :: a b c -> a (b,d) (c,d)
70
71 -- | A mirror image of 'first'.
72 --
73 -- The default definition may be overridden with a more efficient
74 -- version if desired.
75 second :: a b c -> a (d,b) (d,c)
76 second f = arr swap >>> first f >>> arr swap
77 where
78 swap :: (x,y) -> (y,x)
79 swap ~(x,y) = (y,x)
80
81 -- | Split the input between the two argument arrows and combine
82 -- their output. Note that this is in general not a functor.
83 --
84 -- The default definition may be overridden with a more efficient
85 -- version if desired.
86 (***) :: a b c -> a b' c' -> a (b,b') (c,c')
87 f *** g = first f >>> second g
88
89 -- | Fanout: send the input to both argument arrows and combine
90 -- their output.
91 --
92 -- The default definition may be overridden with a more efficient
93 -- version if desired.
94 (&&&) :: a b c -> a b c' -> a b (c,c')
95 f &&& g = arr (\b -> (b,b)) >>> f *** g
96
97 {-# RULES
98 "compose/arr" forall f g .
99 (arr f) . (arr g) = arr (f . g)
100 "first/arr" forall f .
101 first (arr f) = arr (first f)
102 "second/arr" forall f .
103 second (arr f) = arr (second f)
104 "product/arr" forall f g .
105 arr f *** arr g = arr (f *** g)
106 "fanout/arr" forall f g .
107 arr f &&& arr g = arr (f &&& g)
108 "compose/first" forall f g .
109 (first f) . (first g) = first (f . g)
110 "compose/second" forall f g .
111 (second f) . (second g) = second (f . g)
112 #-}
113
114 -- Ordinary functions are arrows.
115
116 instance Arrow (->) where
117 arr f = f
118 first f = f *** id
119 second f = id *** f
120 -- (f *** g) ~(x,y) = (f x, g y)
121 -- sorry, although the above defn is fully H'98, nhc98 can't parse it.
122 (***) f g ~(x,y) = (f x, g y)
123
124 -- | Kleisli arrows of a monad.
125
126 newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }
127
128 instance Monad m => Category (Kleisli m) where
129 id = Kleisli return
130 (Kleisli f) . (Kleisli g) = Kleisli (\b -> g b >>= f)
131
132 instance Monad m => Arrow (Kleisli m) where
133 arr f = Kleisli (return . f)
134 first (Kleisli f) = Kleisli (\ ~(b,d) -> f b >>= \c -> return (c,d))
135 second (Kleisli f) = Kleisli (\ ~(d,b) -> f b >>= \c -> return (d,c))
136
137 -- | The identity arrow, which plays the role of 'return' in arrow notation.
138
139 returnA :: Arrow a => a b b
140 returnA = arr id
141
142 -- | Precomposition with a pure function.
143 (^>>) :: Arrow a => (b -> c) -> a c d -> a b d
144 f ^>> a = arr f >>> a
145
146 -- | Postcomposition with a pure function.
147 (>>^) :: Arrow a => a b c -> (c -> d) -> a b d
148 a >>^ f = a >>> arr f
149
150 -- | Precomposition with a pure function (right-to-left variant).
151 (<<^) :: Arrow a => a c d -> (b -> c) -> a b d
152 a <<^ f = a <<< arr f
153
154 -- | Postcomposition with a pure function (right-to-left variant).
155 (^<<) :: Arrow a => (c -> d) -> a b c -> a b d
156 f ^<< a = arr f <<< a
157
158 class Arrow a => ArrowZero a where
159 zeroArrow :: a b c
160
161 instance MonadPlus m => ArrowZero (Kleisli m) where
162 zeroArrow = Kleisli (\_ -> mzero)
163
164 class ArrowZero a => ArrowPlus a where
165 (<+>) :: a b c -> a b c -> a b c
166
167 instance MonadPlus m => ArrowPlus (Kleisli m) where
168 Kleisli f <+> Kleisli g = Kleisli (\x -> f x `mplus` g x)
169
170 -- | Choice, for arrows that support it. This class underlies the
171 -- @if@ and @case@ constructs in arrow notation.
172 -- Any instance must define 'left'. The other combinators have sensible
173 -- default definitions, which may be overridden for efficiency.
174
175 class Arrow a => ArrowChoice a where
176
177 -- | Feed marked inputs through the argument arrow, passing the
178 -- rest through unchanged to the output.
179 left :: a b c -> a (Either b d) (Either c d)
180
181 -- | A mirror image of 'left'.
182 --
183 -- The default definition may be overridden with a more efficient
184 -- version if desired.
185 right :: a b c -> a (Either d b) (Either d c)
186 right f = arr mirror >>> left f >>> arr mirror
187 where
188 mirror :: Either x y -> Either y x
189 mirror (Left x) = Right x
190 mirror (Right y) = Left y
191
192 -- | Split the input between the two argument arrows, retagging
193 -- and merging their outputs.
194 -- Note that this is in general not a functor.
195 --
196 -- The default definition may be overridden with a more efficient
197 -- version if desired.
198 (+++) :: a b c -> a b' c' -> a (Either b b') (Either c c')
199 f +++ g = left f >>> right g
200
201 -- | Fanin: Split the input between the two argument arrows and
202 -- merge their outputs.
203 --
204 -- The default definition may be overridden with a more efficient
205 -- version if desired.
206 (|||) :: a b d -> a c d -> a (Either b c) d
207 f ||| g = f +++ g >>> arr untag
208 where
209 untag (Left x) = x
210 untag (Right y) = y
211
212 {-# RULES
213 "left/arr" forall f .
214 left (arr f) = arr (left f)
215 "right/arr" forall f .
216 right (arr f) = arr (right f)
217 "sum/arr" forall f g .
218 arr f +++ arr g = arr (f +++ g)
219 "fanin/arr" forall f g .
220 arr f ||| arr g = arr (f ||| g)
221 "compose/left" forall f g .
222 left f . left g = left (f . g)
223 "compose/right" forall f g .
224 right f . right g = right (f . g)
225 #-}
226
227 instance ArrowChoice (->) where
228 left f = f +++ id
229 right f = id +++ f
230 f +++ g = (Left . f) ||| (Right . g)
231 (|||) = either
232
233 instance Monad m => ArrowChoice (Kleisli m) where
234 left f = f +++ arr id
235 right f = arr id +++ f
236 f +++ g = (f >>> arr Left) ||| (g >>> arr Right)
237 Kleisli f ||| Kleisli g = Kleisli (either f g)
238
239 -- | Some arrows allow application of arrow inputs to other inputs.
240
241 class Arrow a => ArrowApply a where
242 app :: a (a b c, b) c
243
244 instance ArrowApply (->) where
245 app (f,x) = f x
246
247 instance Monad m => ArrowApply (Kleisli m) where
248 app = Kleisli (\(Kleisli f, x) -> f x)
249
250 -- | The 'ArrowApply' class is equivalent to 'Monad': any monad gives rise
251 -- to a 'Kleisli' arrow, and any instance of 'ArrowApply' defines a monad.
252
253 newtype ArrowMonad a b = ArrowMonad (a () b)
254
255 instance ArrowApply a => Monad (ArrowMonad a) where
256 return x = ArrowMonad (arr (\_ -> x))
257 ArrowMonad m >>= f = ArrowMonad $
258 m >>> arr (\x -> let ArrowMonad h = f x in (h, ())) >>> app
259
260 -- | Any instance of 'ArrowApply' can be made into an instance of
261 -- 'ArrowChoice' by defining 'left' = 'leftApp'.
262
263 leftApp :: ArrowApply a => a b c -> a (Either b d) (Either c d)
264 leftApp f = arr ((\b -> (arr (\() -> b) >>> f >>> arr Left, ())) |||
265 (\d -> (arr (\() -> d) >>> arr Right, ()))) >>> app
266
267 -- | The 'loop' operator expresses computations in which an output value is
268 -- fed back as input, even though the computation occurs only once.
269 -- It underlies the @rec@ value recursion construct in arrow notation.
270
271 class Arrow a => ArrowLoop a where
272 loop :: a (b,d) (c,d) -> a b c
273
274 instance ArrowLoop (->) where
275 loop f b = let (c,d) = f (b,d) in c
276
277 instance MonadFix m => ArrowLoop (Kleisli m) where
278 loop (Kleisli f) = Kleisli (liftM fst . mfix . f')
279 where f' x y = f (x, snd y)