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.
58 -- Minimal complete definition: 'arr' and 'first'.
60 -- The other combinators have sensible default definitions,
61 -- which may be overridden for efficiency.
63 class Category a => Arrow a where
65 -- | Lift a function to an arrow.
66 arr :: (b -> c) -> a b c
68 -- | Send the first component of the input through the argument
69 -- arrow, and copy the rest unchanged to the output.
70 first :: a b c -> a (b,d) (c,d)
72 -- | A mirror image of 'first'.
74 -- The default definition may be overridden with a more efficient
75 -- version if desired.
76 second :: a b c -> a (d,b) (d,c)
77 second f = arr swap >>> first f >>> arr swap
78 where swap ~(x,y) = (y,x)
80 -- | Split the input between the two argument arrows and combine
81 -- their output. Note that this is in general not a functor.
83 -- The default definition may be overridden with a more efficient
84 -- version if desired.
85 (***) :: a b c -> a b' c' -> a (b,b') (c,c')
86 f *** g = first f >>> second g
88 -- | Fanout: send the input to both argument arrows and combine
91 -- The default definition may be overridden with a more efficient
92 -- version if desired.
93 (&&&) :: a b c -> a b c' -> a b (c,c')
94 f &&& g = arr (\b -> (b,b)) >>> f *** g
99 "compose/arr" forall f g .
100 (arr f) . (arr g) = arr (f . g)
101 "first/arr" forall f .
102 first (arr f) = arr (first f)
103 "second/arr" forall f .
104 second (arr f) = arr (second f)
105 "product/arr" forall f g .
106 arr f *** arr g = arr (f *** g)
107 "fanout/arr" forall f g .
108 arr f &&& arr g = arr (f &&& g)
109 "compose/first" forall f g .
110 (first f) . (first g) = first (f . g)
111 "compose/second" forall f g .
112 (second f) . (second g) = second (f . g)
115 -- Ordinary functions are arrows.
117 instance Arrow (->) where
121 -- (f *** g) ~(x,y) = (f x, g y)
122 -- sorry, although the above defn is fully H'98, nhc98 can't parse it.
123 (***) f g ~(x,y) = (f x, g y)
125 -- | Kleisli arrows of a monad.
127 newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }
129 instance Monad m => Category (Kleisli m) where
131 (Kleisli f) . (Kleisli g) = Kleisli (\b -> g b >>= f)
133 instance Monad m => Arrow (Kleisli m) where
134 arr f = Kleisli (return . f)
135 first (Kleisli f) = Kleisli (\ ~(b,d) -> f b >>= \c -> return (c,d))
136 second (Kleisli f) = Kleisli (\ ~(d,b) -> f b >>= \c -> return (d,c))
138 -- | The identity arrow, which plays the role of 'return' in arrow notation.
140 returnA :: Arrow a => a b b
143 -- | Precomposition with a pure function.
144 (^>>) :: Arrow a => (b -> c) -> a c d -> a b d
145 f ^>> a = arr f >>> a
147 -- | Postcomposition with a pure function.
148 (>>^) :: Arrow a => a b c -> (c -> d) -> a b d
149 a >>^ f = a >>> arr f
151 -- | Precomposition with a pure function (right-to-left variant).
152 (<<^) :: Arrow a => a c d -> (b -> c) -> a b d
153 a <<^ f = a <<< arr f
155 -- | Postcomposition with a pure function (right-to-left variant).
156 (^<<) :: Arrow a => (c -> d) -> a b c -> a b d
157 f ^<< a = arr f <<< a
159 class Arrow a => ArrowZero a where
162 instance MonadPlus m => ArrowZero (Kleisli m) where
163 zeroArrow = Kleisli (\_ -> mzero)
165 class ArrowZero a => ArrowPlus a where
166 (<+>) :: a b c -> a b c -> a b c
168 instance MonadPlus m => ArrowPlus (Kleisli m) where
169 Kleisli f <+> Kleisli g = Kleisli (\x -> f x `mplus` g x)
171 -- | Choice, for arrows that support it. This class underlies the
172 -- @if@ and @case@ constructs in arrow notation.
173 -- Any instance must define 'left'. The other combinators have sensible
174 -- default definitions, which may be overridden for efficiency.
176 class Arrow a => ArrowChoice a where
178 -- | Feed marked inputs through the argument arrow, passing the
179 -- rest through unchanged to the output.
180 left :: a b c -> a (Either b d) (Either c d)
182 -- | A mirror image of 'left'.
184 -- The default definition may be overridden with a more efficient
185 -- version if desired.
186 right :: a b c -> a (Either d b) (Either d c)
187 right f = arr mirror >>> left f >>> arr mirror
188 where mirror (Left x) = Right x
189 mirror (Right y) = Left y
191 -- | Split the input between the two argument arrows, retagging
192 -- and merging their outputs.
193 -- Note that this is in general not a functor.
195 -- The default definition may be overridden with a more efficient
196 -- version if desired.
197 (+++) :: a b c -> a b' c' -> a (Either b b') (Either c c')
198 f +++ g = left f >>> right g
200 -- | Fanin: Split the input between the two argument arrows and
201 -- merge their outputs.
203 -- The default definition may be overridden with a more efficient
204 -- version if desired.
205 (|||) :: a b d -> a c d -> a (Either b c) d
206 f ||| g = f +++ g >>> arr untag
207 where untag (Left x) = x
211 "left/arr" forall f .
212 left (arr f) = arr (left f)
213 "right/arr" forall f .
214 right (arr f) = arr (right f)
215 "sum/arr" forall f g .
216 arr f +++ arr g = arr (f +++ g)
217 "fanin/arr" forall f g .
218 arr f ||| arr g = arr (f ||| g)
219 "compose/left" forall f g .
220 left f >>> left g = left (f >>> g)
221 "compose/right" forall f g .
222 right f >>> right g = right (f >>> g)
225 instance ArrowChoice (->) where
228 f +++ g = (Left . f) ||| (Right . g)
231 instance Monad m => ArrowChoice (Kleisli m) where
232 left f = f +++ arr id
233 right f = arr id +++ f
234 f +++ g = (f >>> arr Left) ||| (g >>> arr Right)
235 Kleisli f ||| Kleisli g = Kleisli (either f g)
237 -- | Some arrows allow application of arrow inputs to other inputs.
239 class Arrow a => ArrowApply a where
240 app :: a (a b c, b) c
242 instance ArrowApply (->) where
245 instance Monad m => ArrowApply (Kleisli m) where
246 app = Kleisli (\(Kleisli f, x) -> f x)
248 -- | The 'ArrowApply' class is equivalent to 'Monad': any monad gives rise
249 -- to a 'Kleisli' arrow, and any instance of 'ArrowApply' defines a monad.
251 newtype ArrowApply a => ArrowMonad a b = ArrowMonad (a () b)
253 instance ArrowApply a => Monad (ArrowMonad a) where
254 return x = ArrowMonad (arr (\_ -> x))
255 ArrowMonad m >>= f = ArrowMonad (m >>>
256 arr (\x -> let ArrowMonad h = f x in (h, ())) >>>
259 -- | Any instance of 'ArrowApply' can be made into an instance of
260 -- 'ArrowChoice' by defining 'left' = 'leftApp'.
262 leftApp :: ArrowApply a => a b c -> a (Either b d) (Either c d)
263 leftApp f = arr ((\b -> (arr (\() -> b) >>> f >>> arr Left, ())) |||
264 (\d -> (arr (\() -> d) >>> arr Right, ()))) >>> app
266 -- | The 'loop' operator expresses computations in which an output value is
267 -- fed back as input, even though the computation occurs only once.
268 -- It underlies the @rec@ value recursion construct in arrow notation.
270 class Arrow a => ArrowLoop a where
271 loop :: a (b,d) (c,d) -> a b c
273 instance ArrowLoop (->) where
274 loop f b = let (c,d) = f (b,d) in c
276 instance MonadFix m => ArrowLoop (Kleisli m) where
277 loop (Kleisli f) = Kleisli (liftM fst . mfix . f')
278 where f' x y = f (x, snd y)