new Control.Compositor module
[ghc.git] / libraries / base / Control / Arrow.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module : Control.Arrow
4 -- Copyright : (c) Ross Paterson 2002
5 -- License : BSD-style (see the LICENSE file in the distribution)
6 --
7 -- Maintainer : ross@soi.city.ac.uk
8 -- Stability : experimental
9 -- Portability : portable
10 --
11 -- Basic arrow definitions, based on
12 -- /Generalising Monads to Arrows/, by John Hughes,
13 -- /Science of Computer Programming/ 37, pp67-111, May 2000.
14 -- plus a couple of definitions ('returnA' and 'loop') from
15 -- /A New Notation for Arrows/, by Ross Paterson, in /ICFP 2001/,
16 -- Firenze, Italy, pp229-240.
17 -- See these papers for the equations these combinators are expected to
18 -- satisfy. 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 -- ** Right-to-left variants
28 (<<^), (^<<),
29 -- * Monoid operations
30 ArrowZero(..), ArrowPlus(..),
31 -- * Conditionals
32 ArrowChoice(..),
33 -- * Arrow application
34 ArrowApply(..), ArrowMonad(..), leftApp,
35 -- * Feedback
36 ArrowLoop(..)
37 ) where
38
39 import Prelude
40
41 import Control.Monad
42 import Control.Monad.Fix
43 import Control.Compositor
44
45 infixr 5 <+>
46 infixr 3 ***
47 infixr 3 &&&
48 infixr 2 +++
49 infixr 2 |||
50 infixr 1 ^>>, >>^
51 infixr 1 ^<<, <<^
52
53 -- | The basic arrow class.
54 -- Any instance must define either 'arr' or 'pure' (which are synonyms),
55 -- as well as 'first'. The other combinators have sensible
56 -- default definitions, which may be overridden for efficiency.
57
58 class Compositor a => Arrow a where
59
60 -- | Lift a function to an arrow: you must define either this
61 -- or 'pure'.
62 arr :: (b -> c) -> a b c
63 arr = pure
64
65 -- | A synonym for 'arr': you must define one or other of them.
66 pure :: (b -> c) -> a b c
67 pure = arr
68
69 -- | Send the first component of the input through the argument
70 -- arrow, and copy the rest unchanged to the output.
71 first :: a b c -> a (b,d) (c,d)
72
73 -- | A mirror image of 'first'.
74 --
75 -- The default definition may be overridden with a more efficient
76 -- version if desired.
77 second :: a b c -> a (d,b) (d,c)
78 second f = arr swap >>> first f >>> arr swap
79 where 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 "identity"
99 arr id = identity
100 "compose/arr" forall f g .
101 arr f >>> arr g = arr (f >>> g)
102 "first/arr" forall f .
103 first (arr f) = arr (first f)
104 "second/arr" forall f .
105 second (arr f) = arr (second f)
106 "product/arr" forall f g .
107 arr f *** arr g = arr (f *** g)
108 "fanout/arr" forall f g .
109 arr f &&& arr g = arr (f &&& g)
110 "compose/first" forall f g .
111 first f >>> first g = first (f >>> g)
112 "compose/second" forall f g .
113 second f >>> second g = second (f >>> g)
114 #-}
115
116 -- Ordinary functions are arrows.
117
118 instance Arrow (->) where
119 arr f = f
120 first f = f *** id
121 second f = id *** f
122 -- (f *** g) ~(x,y) = (f x, g y)
123 -- sorry, although the above defn is fully H'98, nhc98 can't parse it.
124 (***) f g ~(x,y) = (f x, g y)
125
126 -- | Kleisli arrows of a monad.
127
128 newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }
129
130 instance Monad m => Compositor (Kleisli m) where
131 identity = Kleisli return
132 Kleisli f >>> Kleisli g = Kleisli (\b -> f b >>= g)
133
134 instance Monad m => Arrow (Kleisli m) where
135 arr f = Kleisli (return . f)
136 first (Kleisli f) = Kleisli (\ ~(b,d) -> f b >>= \c -> return (c,d))
137 second (Kleisli f) = Kleisli (\ ~(d,b) -> f b >>= \c -> return (d,c))
138
139 -- | The identity arrow, which plays the role of 'return' in arrow notation.
140
141 returnA :: Arrow a => a b b
142 returnA = arr id
143
144 -- | Precomposition with a pure function.
145 (^>>) :: Arrow a => (b -> c) -> a c d -> a b d
146 f ^>> a = arr f >>> a
147
148 -- | Postcomposition with a pure function.
149 (>>^) :: Arrow a => a b c -> (c -> d) -> a b d
150 a >>^ f = a >>> arr f
151
152 -- | Precomposition with a pure function (right-to-left variant).
153 (<<^) :: Arrow a => a c d -> (b -> c) -> a b d
154 a <<^ f = a <<< arr f
155
156 -- | Postcomposition with a pure function (right-to-left variant).
157 (^<<) :: Arrow a => (c -> d) -> a b c -> a b d
158 f ^<< a = arr f <<< a
159
160 class Arrow a => ArrowZero a where
161 zeroArrow :: a b c
162
163 instance MonadPlus m => ArrowZero (Kleisli m) where
164 zeroArrow = Kleisli (\x -> mzero)
165
166 class ArrowZero a => ArrowPlus a where
167 (<+>) :: a b c -> a b c -> a b c
168
169 instance MonadPlus m => ArrowPlus (Kleisli m) where
170 Kleisli f <+> Kleisli g = Kleisli (\x -> f x `mplus` g x)
171
172 -- | Choice, for arrows that support it. This class underlies the
173 -- @if@ and @case@ constructs in arrow notation.
174 -- Any instance must define 'left'. The other combinators have sensible
175 -- default definitions, which may be overridden for efficiency.
176
177 class Arrow a => ArrowChoice a where
178
179 -- | Feed marked inputs through the argument arrow, passing the
180 -- rest through unchanged to the output.
181 left :: a b c -> a (Either b d) (Either c d)
182
183 -- | A mirror image of 'left'.
184 --
185 -- The default definition may be overridden with a more efficient
186 -- version if desired.
187 right :: a b c -> a (Either d b) (Either d c)
188 right f = arr mirror >>> left f >>> arr mirror
189 where 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 untag (Left x) = x
209 untag (Right y) = y
210
211 {-# RULES
212 "left/arr" forall f .
213 left (arr f) = arr (left f)
214 "right/arr" forall f .
215 right (arr f) = arr (right f)
216 "sum/arr" forall f g .
217 arr f +++ arr g = arr (f +++ g)
218 "fanin/arr" forall f g .
219 arr f ||| arr g = arr (f ||| g)
220 "compose/left" forall f g .
221 left f >>> left g = left (f >>> g)
222 "compose/right" forall f g .
223 right f >>> right g = right (f >>> g)
224 #-}
225
226 instance ArrowChoice (->) where
227 left f = f +++ id
228 right f = id +++ f
229 f +++ g = (Left . f) ||| (Right . g)
230 (|||) = either
231
232 instance Monad m => ArrowChoice (Kleisli m) where
233 left f = f +++ arr id
234 right f = arr id +++ f
235 f +++ g = (f >>> arr Left) ||| (g >>> arr Right)
236 Kleisli f ||| Kleisli g = Kleisli (either f g)
237
238 -- | Some arrows allow application of arrow inputs to other inputs.
239
240 class Arrow a => ArrowApply a where
241 app :: a (a b c, b) c
242
243 instance ArrowApply (->) where
244 app (f,x) = f x
245
246 instance Monad m => ArrowApply (Kleisli m) where
247 app = Kleisli (\(Kleisli f, x) -> f x)
248
249 -- | The 'ArrowApply' class is equivalent to 'Monad': any monad gives rise
250 -- to a 'Kleisli' arrow, and any instance of 'ArrowApply' defines a monad.
251
252 newtype ArrowApply a => ArrowMonad a b = ArrowMonad (a () b)
253
254 instance ArrowApply a => Monad (ArrowMonad a) where
255 return x = ArrowMonad (arr (\z -> x))
256 ArrowMonad m >>= f = ArrowMonad (m >>>
257 arr (\x -> let ArrowMonad h = f x in (h, ())) >>>
258 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)