1 -----------------------------------------------------------------------------
3 -- Module : Control.Arrow
4 -- Copyright : (c) Ross Paterson 2002
5 -- License : BSD-style (see the LICENSE file in the distribution)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : portable
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/>.
21 module Control.Arrow (
23 Arrow(..), Kleisli(..),
24 -- ** Derived combinators
27 -- ** Right-to-left variants
29 -- * Monoid operations
30 ArrowZero(..), ArrowPlus(..),
33 -- * Arrow application
34 ArrowApply(..), ArrowMonad(..), leftApp,
39 import Prelude hiding (id,(.))
40 import qualified Prelude
43 import Control.Monad.Fix
44 import Control.Category
54 -- | The basic arrow class.
55 -- Any instance must define either 'arr' or 'pure' (which are synonyms),
56 -- as well as 'first'. The other combinators have sensible
57 -- default definitions, which may be overridden for efficiency.
59 class Category a => Arrow a where
61 -- | Lift a function to an arrow: you must define either this
63 arr :: (b -> c) -> a b c
66 -- | A synonym for 'arr': you must define one or other of them.
67 pure :: (b -> c) -> a b c
70 -- | Send the first component of the input through the argument
71 -- arrow, and copy the rest unchanged to the output.
72 first :: a b c -> a (b,d) (c,d)
74 -- | A mirror image of 'first'.
76 -- The default definition may be overridden with a more efficient
77 -- version if desired.
78 second :: a b c -> a (d,b) (d,c)
79 second f = arr swap >>> first f >>> arr swap
80 where swap ~(x,y) = (y,x)
82 -- | Split the input between the two argument arrows and combine
83 -- their output. Note that this is in general not a functor.
85 -- The default definition may be overridden with a more efficient
86 -- version if desired.
87 (***) :: a b c -> a b' c' -> a (b,b') (c,c')
88 f *** g = first f >>> second g
90 -- | Fanout: send the input to both argument arrows and combine
93 -- The default definition may be overridden with a more efficient
94 -- version if desired.
95 (&&&) :: a b c -> a b c' -> a b (c,c')
96 f &&& g = arr (\b -> (b,b)) >>> f *** g
101 "compose/arr" forall f g .
102 (arr f) . (arr g) = arr (f . g)
103 "first/arr" forall f .
104 first (arr f) = arr (first f)
105 "second/arr" forall f .
106 second (arr f) = arr (second f)
107 "product/arr" forall f g .
108 arr f *** arr g = arr (f *** g)
109 "fanout/arr" forall f g .
110 arr f &&& arr g = arr (f &&& g)
111 "compose/first" forall f g .
112 (first f) . (first g) = first (f . g)
113 "compose/second" forall f g .
114 (second f) . (second g) = second (f . g)
117 -- Ordinary functions are arrows.
119 instance Arrow (->) where
123 -- (f *** g) ~(x,y) = (f x, g y)
124 -- sorry, although the above defn is fully H'98, nhc98 can't parse it.
125 (***) f g ~(x,y) = (f x, g y)
127 -- | Kleisli arrows of a monad.
129 newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }
131 instance Monad m => Category (Kleisli m) where
133 (Kleisli f) . (Kleisli g) = Kleisli (\b -> g b >>= f)
135 instance Monad m => Arrow (Kleisli m) where
136 arr f = Kleisli (return . f)
137 first (Kleisli f) = Kleisli (\ ~(b,d) -> f b >>= \c -> return (c,d))
138 second (Kleisli f) = Kleisli (\ ~(d,b) -> f b >>= \c -> return (d,c))
140 -- | The identity arrow, which plays the role of 'return' in arrow notation.
142 returnA :: Arrow a => a b b
145 -- | Precomposition with a pure function.
146 (^>>) :: Arrow a => (b -> c) -> a c d -> a b d
147 f ^>> a = arr f >>> a
149 -- | Postcomposition with a pure function.
150 (>>^) :: Arrow a => a b c -> (c -> d) -> a b d
151 a >>^ f = a >>> arr f
153 -- | Precomposition with a pure function (right-to-left variant).
154 (<<^) :: Arrow a => a c d -> (b -> c) -> a b d
155 a <<^ f = a <<< arr f
157 -- | Postcomposition with a pure function (right-to-left variant).
158 (^<<) :: Arrow a => (c -> d) -> a b c -> a b d
159 f ^<< a = arr f <<< a
161 class Arrow a => ArrowZero a where
164 instance MonadPlus m => ArrowZero (Kleisli m) where
165 zeroArrow = Kleisli (\x -> mzero)
167 class ArrowZero a => ArrowPlus a where
168 (<+>) :: a b c -> a b c -> a b c
170 instance MonadPlus m => ArrowPlus (Kleisli m) where
171 Kleisli f <+> Kleisli g = Kleisli (\x -> f x `mplus` g x)
173 -- | Choice, for arrows that support it. This class underlies the
174 -- @if@ and @case@ constructs in arrow notation.
175 -- Any instance must define 'left'. The other combinators have sensible
176 -- default definitions, which may be overridden for efficiency.
178 class Arrow a => ArrowChoice a where
180 -- | Feed marked inputs through the argument arrow, passing the
181 -- rest through unchanged to the output.
182 left :: a b c -> a (Either b d) (Either c d)
184 -- | A mirror image of 'left'.
186 -- The default definition may be overridden with a more efficient
187 -- version if desired.
188 right :: a b c -> a (Either d b) (Either d c)
189 right f = arr mirror >>> left f >>> arr mirror
190 where mirror (Left x) = Right x
191 mirror (Right y) = Left y
193 -- | Split the input between the two argument arrows, retagging
194 -- and merging their outputs.
195 -- Note that this is in general not a functor.
197 -- The default definition may be overridden with a more efficient
198 -- version if desired.
199 (+++) :: a b c -> a b' c' -> a (Either b b') (Either c c')
200 f +++ g = left f >>> right g
202 -- | Fanin: Split the input between the two argument arrows and
203 -- merge their outputs.
205 -- The default definition may be overridden with a more efficient
206 -- version if desired.
207 (|||) :: a b d -> a c d -> a (Either b c) d
208 f ||| g = f +++ g >>> arr untag
209 where untag (Left x) = x
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)
227 instance ArrowChoice (->) where
230 f +++ g = (Left . f) ||| (Right . g)
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)
239 -- | Some arrows allow application of arrow inputs to other inputs.
241 class Arrow a => ArrowApply a where
242 app :: a (a b c, b) c
244 instance ArrowApply (->) where
247 instance Monad m => ArrowApply (Kleisli m) where
248 app = Kleisli (\(Kleisli f, x) -> f x)
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.
253 newtype ArrowApply a => ArrowMonad a b = ArrowMonad (a () b)
255 instance ArrowApply a => Monad (ArrowMonad a) where
256 return x = ArrowMonad (arr (\z -> x))
257 ArrowMonad m >>= f = ArrowMonad (m >>>
258 arr (\x -> let ArrowMonad h = f x in (h, ())) >>>
261 -- | Any instance of 'ArrowApply' can be made into an instance of
262 -- 'ArrowChoice' by defining 'left' = 'leftApp'.
264 leftApp :: ArrowApply a => a b c -> a (Either b d) (Either c d)
265 leftApp f = arr ((\b -> (arr (\() -> b) >>> f >>> arr Left, ())) |||
266 (\d -> (arr (\() -> d) >>> arr Right, ()))) >>> app
268 -- | The 'loop' operator expresses computations in which an output value is
269 -- fed back as input, even though the computation occurs only once.
270 -- It underlies the @rec@ value recursion construct in arrow notation.
272 class Arrow a => ArrowLoop a where
273 loop :: a (b,d) (c,d) -> a b c
275 instance ArrowLoop (->) where
276 loop f b = let (c,d) = f (b,d) in c
278 instance MonadFix m => ArrowLoop (Kleisli m) where
279 loop (Kleisli f) = Kleisli (liftM fst . mfix . f')
280 where f' x y = f (x, snd y)