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,
38 (>>>), (<<<) -- reexported
41 import Prelude hiding (id,(.))
42 import qualified Prelude
45 import Control.Monad.Fix
46 import Control.Category
56 -- | The basic arrow class.
57 -- Any instance must define either 'arr' or 'pure' (which are synonyms),
58 -- as well as 'first'. The other combinators have sensible
59 -- default definitions, which may be overridden for efficiency.
61 class Category a => Arrow a where
63 -- | Lift a function to an arrow: you must define either this
65 arr :: (b -> c) -> a b c
68 -- | A synonym for 'arr': you must define one or other of them.
69 pure :: (b -> c) -> a b c
72 -- | Send the first component of the input through the argument
73 -- arrow, and copy the rest unchanged to the output.
74 first :: a b c -> a (b,d) (c,d)
76 -- | A mirror image of 'first'.
78 -- The default definition may be overridden with a more efficient
79 -- version if desired.
80 second :: a b c -> a (d,b) (d,c)
81 second f = arr swap >>> first f >>> arr swap
82 where swap ~(x,y) = (y,x)
84 -- | Split the input between the two argument arrows and combine
85 -- their output. Note that this is in general not a functor.
87 -- The default definition may be overridden with a more efficient
88 -- version if desired.
89 (***) :: a b c -> a b' c' -> a (b,b') (c,c')
90 f *** g = first f >>> second g
92 -- | Fanout: send the input to both argument arrows and combine
95 -- The default definition may be overridden with a more efficient
96 -- version if desired.
97 (&&&) :: a b c -> a b c' -> a b (c,c')
98 f &&& g = arr (\b -> (b,b)) >>> f *** g
103 "compose/arr" forall f g .
104 (arr f) . (arr g) = arr (f . g)
105 "first/arr" forall f .
106 first (arr f) = arr (first f)
107 "second/arr" forall f .
108 second (arr f) = arr (second f)
109 "product/arr" forall f g .
110 arr f *** arr g = arr (f *** g)
111 "fanout/arr" forall f g .
112 arr f &&& arr g = arr (f &&& g)
113 "compose/first" forall f g .
114 (first f) . (first g) = first (f . g)
115 "compose/second" forall f g .
116 (second f) . (second g) = second (f . g)
119 -- Ordinary functions are arrows.
121 instance Arrow (->) where
125 -- (f *** g) ~(x,y) = (f x, g y)
126 -- sorry, although the above defn is fully H'98, nhc98 can't parse it.
127 (***) f g ~(x,y) = (f x, g y)
129 -- | Kleisli arrows of a monad.
131 newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }
133 instance Monad m => Category (Kleisli m) where
135 (Kleisli f) . (Kleisli g) = Kleisli (\b -> g b >>= f)
137 instance Monad m => Arrow (Kleisli m) where
138 arr f = Kleisli (return . f)
139 first (Kleisli f) = Kleisli (\ ~(b,d) -> f b >>= \c -> return (c,d))
140 second (Kleisli f) = Kleisli (\ ~(d,b) -> f b >>= \c -> return (d,c))
142 -- | The identity arrow, which plays the role of 'return' in arrow notation.
144 returnA :: Arrow a => a b b
147 -- | Precomposition with a pure function.
148 (^>>) :: Arrow a => (b -> c) -> a c d -> a b d
149 f ^>> a = arr f >>> a
151 -- | Postcomposition with a pure function.
152 (>>^) :: Arrow a => a b c -> (c -> d) -> a b d
153 a >>^ f = a >>> arr f
155 -- | Precomposition with a pure function (right-to-left variant).
156 (<<^) :: Arrow a => a c d -> (b -> c) -> a b d
157 a <<^ f = a <<< arr f
159 -- | Postcomposition with a pure function (right-to-left variant).
160 (^<<) :: Arrow a => (c -> d) -> a b c -> a b d
161 f ^<< a = arr f <<< a
163 class Arrow a => ArrowZero a where
166 instance MonadPlus m => ArrowZero (Kleisli m) where
167 zeroArrow = Kleisli (\_ -> mzero)
169 class ArrowZero a => ArrowPlus a where
170 (<+>) :: a b c -> a b c -> a b c
172 instance MonadPlus m => ArrowPlus (Kleisli m) where
173 Kleisli f <+> Kleisli g = Kleisli (\x -> f x `mplus` g x)
175 -- | Choice, for arrows that support it. This class underlies the
176 -- @if@ and @case@ constructs in arrow notation.
177 -- Any instance must define 'left'. The other combinators have sensible
178 -- default definitions, which may be overridden for efficiency.
180 class Arrow a => ArrowChoice a where
182 -- | Feed marked inputs through the argument arrow, passing the
183 -- rest through unchanged to the output.
184 left :: a b c -> a (Either b d) (Either c d)
186 -- | A mirror image of 'left'.
188 -- The default definition may be overridden with a more efficient
189 -- version if desired.
190 right :: a b c -> a (Either d b) (Either d c)
191 right f = arr mirror >>> left f >>> arr mirror
192 where mirror (Left x) = Right x
193 mirror (Right y) = Left y
195 -- | Split the input between the two argument arrows, retagging
196 -- and merging their outputs.
197 -- Note that this is in general not a functor.
199 -- The default definition may be overridden with a more efficient
200 -- version if desired.
201 (+++) :: a b c -> a b' c' -> a (Either b b') (Either c c')
202 f +++ g = left f >>> right g
204 -- | Fanin: Split the input between the two argument arrows and
205 -- merge their outputs.
207 -- The default definition may be overridden with a more efficient
208 -- version if desired.
209 (|||) :: a b d -> a c d -> a (Either b c) d
210 f ||| g = f +++ g >>> arr untag
211 where untag (Left x) = x
215 "left/arr" forall f .
216 left (arr f) = arr (left f)
217 "right/arr" forall f .
218 right (arr f) = arr (right f)
219 "sum/arr" forall f g .
220 arr f +++ arr g = arr (f +++ g)
221 "fanin/arr" forall f g .
222 arr f ||| arr g = arr (f ||| g)
223 "compose/left" forall f g .
224 left f >>> left g = left (f >>> g)
225 "compose/right" forall f g .
226 right f >>> right g = right (f >>> g)
229 instance ArrowChoice (->) where
232 f +++ g = (Left . f) ||| (Right . g)
235 instance Monad m => ArrowChoice (Kleisli m) where
236 left f = f +++ arr id
237 right f = arr id +++ f
238 f +++ g = (f >>> arr Left) ||| (g >>> arr Right)
239 Kleisli f ||| Kleisli g = Kleisli (either f g)
241 -- | Some arrows allow application of arrow inputs to other inputs.
243 class Arrow a => ArrowApply a where
244 app :: a (a b c, b) c
246 instance ArrowApply (->) where
249 instance Monad m => ArrowApply (Kleisli m) where
250 app = Kleisli (\(Kleisli f, x) -> f x)
252 -- | The 'ArrowApply' class is equivalent to 'Monad': any monad gives rise
253 -- to a 'Kleisli' arrow, and any instance of 'ArrowApply' defines a monad.
255 newtype ArrowApply a => ArrowMonad a b = ArrowMonad (a () b)
257 instance ArrowApply a => Monad (ArrowMonad a) where
258 return x = ArrowMonad (arr (\_ -> x))
259 ArrowMonad m >>= f = ArrowMonad (m >>>
260 arr (\x -> let ArrowMonad h = f x in (h, ())) >>>
263 -- | Any instance of 'ArrowApply' can be made into an instance of
264 -- 'ArrowChoice' by defining 'left' = 'leftApp'.
266 leftApp :: ArrowApply a => a b c -> a (Either b d) (Either c d)
267 leftApp f = arr ((\b -> (arr (\() -> b) >>> f >>> arr Left, ())) |||
268 (\d -> (arr (\() -> d) >>> arr Right, ()))) >>> app
270 -- | The 'loop' operator expresses computations in which an output value is
271 -- fed back as input, even though the computation occurs only once.
272 -- It underlies the @rec@ value recursion construct in arrow notation.
274 class Arrow a => ArrowLoop a where
275 loop :: a (b,d) (c,d) -> a b c
277 instance ArrowLoop (->) where
278 loop f b = let (c,d) = f (b,d) in c
280 instance MonadFix m => ArrowLoop (Kleisli m) where
281 loop (Kleisli f) = Kleisli (liftM fst . mfix . f')
282 where f' x y = f (x, snd y)