1 -----------------------------------------------------------------------------
3 -- Module : Control.Arrow
4 -- Copyright : (c) Ross Paterson 2002
5 -- License : BSD-style (see the LICENSE file in the distribution)
7 -- Maintainer : ross@soi.city.ac.uk
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
26 -- * Monoid operations
27 ArrowZero(..), ArrowPlus(..),
30 -- * Arrow application
31 ArrowApply(..), ArrowMonad(..), leftApp,
39 import Control.Monad.Fix
49 -- | The basic arrow class.
50 -- Any instance must define either 'arr' or 'pure' (which are synonyms),
51 -- as well as '>>>' and 'first'. The other combinators have sensible
52 -- default definitions, which may be overridden for efficiency.
56 -- | Lift a function to an arrow: you must define either this
58 arr :: (b -> c) -> a b c
61 -- | A synonym for 'arr': you must define one or other of them.
62 pure :: (b -> c) -> a b c
65 -- | Left-to-right composition of arrows.
66 (>>>) :: a b c -> a c d -> a b d
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
97 "compose/arr" forall f g .
98 arr f >>> arr g = arr (f >>> g)
99 "first/arr" forall f .
100 first (arr f) = arr (first f)
101 "second/arr" forall f .
102 second (arr f) = arr (second f)
103 "product/arr" forall f g .
104 arr f *** arr g = arr (f *** g)
105 "fanout/arr" forall f g .
106 arr f &&& arr g = arr (f &&& g)
107 "compose/first" forall f g .
108 first f >>> first g = first (f >>> g)
109 "compose/second" forall f g .
110 second f >>> second g = second (f >>> g)
113 -- Ordinary functions are arrows.
115 instance Arrow (->) where
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)
124 -- | Kleisli arrows of a monad.
126 newtype Kleisli m a b = Kleisli (a -> m b)
128 instance Monad m => Arrow (Kleisli m) where
129 arr f = Kleisli (return . f)
130 Kleisli f >>> Kleisli g = Kleisli (\b -> f b >>= g)
131 first (Kleisli f) = Kleisli (\ ~(b,d) -> f b >>= \c -> return (c,d))
132 second (Kleisli f) = Kleisli (\ ~(d,b) -> f b >>= \c -> return (d,c))
134 -- | The identity arrow, which plays the role of 'return' in arrow notation.
136 returnA :: Arrow a => a b b
139 -- | Right-to-left composition, for a better fit with arrow notation.
141 (<<<) :: Arrow a => a c d -> a b c -> a b d
144 class Arrow a => ArrowZero a where
147 instance MonadPlus m => ArrowZero (Kleisli m) where
148 zeroArrow = Kleisli (\x -> mzero)
150 class ArrowZero a => ArrowPlus a where
151 (<+>) :: a b c -> a b c -> a b c
153 instance MonadPlus m => ArrowPlus (Kleisli m) where
154 Kleisli f <+> Kleisli g = Kleisli (\x -> f x `mplus` g x)
156 -- | Choice, for arrows that support it. This class underlies the
157 -- @if@ and @case@ constructs in arrow notation.
158 -- Any instance must define 'left'. The other combinators have sensible
159 -- default definitions, which may be overridden for efficiency.
161 class Arrow a => ArrowChoice a where
163 -- | Feed marked inputs through the argument arrow, passing the
164 -- rest through unchanged to the output.
165 left :: a b c -> a (Either b d) (Either c d)
167 -- | A mirror image of 'left'.
169 -- The default definition may be overridden with a more efficient
170 -- version if desired.
171 right :: a b c -> a (Either d b) (Either d c)
172 right f = arr mirror >>> left f >>> arr mirror
173 where mirror (Left x) = Right x
174 mirror (Right y) = Left y
176 -- | Split the input between the two argument arrows, retagging
177 -- and merging their outputs.
178 -- Note that this is in general not a functor.
180 -- The default definition may be overridden with a more efficient
181 -- version if desired.
182 (+++) :: a b c -> a b' c' -> a (Either b b') (Either c c')
183 f +++ g = left f >>> right g
185 -- | Fanin: Split the input between the two argument arrows and
186 -- merge their outputs.
188 -- The default definition may be overridden with a more efficient
189 -- version if desired.
190 (|||) :: a b d -> a c d -> a (Either b c) d
191 f ||| g = f +++ g >>> arr untag
192 where untag (Left x) = x
196 "left/arr" forall f .
197 left (arr f) = arr (left f)
198 "right/arr" forall f .
199 right (arr f) = arr (right f)
200 "sum/arr" forall f g .
201 arr f +++ arr g = arr (f +++ g)
202 "fanin/arr" forall f g .
203 arr f ||| arr g = arr (f ||| g)
204 "compose/left" forall f g .
205 left f >>> left g = left (f >>> g)
206 "compose/right" forall f g .
207 right f >>> right g = right (f >>> g)
210 instance ArrowChoice (->) where
213 f +++ g = (Left . f) ||| (Right . g)
216 instance Monad m => ArrowChoice (Kleisli m) where
217 left f = f +++ arr id
218 right f = arr id +++ f
219 f +++ g = (f >>> arr Left) ||| (g >>> arr Right)
220 Kleisli f ||| Kleisli g = Kleisli (either f g)
222 -- | Some arrows allow application of arrow inputs to other inputs.
224 class Arrow a => ArrowApply a where
225 app :: a (a b c, b) c
227 instance ArrowApply (->) where
230 instance Monad m => ArrowApply (Kleisli m) where
231 app = Kleisli (\(Kleisli f, x) -> f x)
233 -- | The 'ArrowApply' class is equivalent to 'Monad': any monad gives rise
234 -- to a 'Kleisli' arrow, and any instance of 'ArrowApply' defines a monad.
236 newtype ArrowApply a => ArrowMonad a b = ArrowMonad (a () b)
238 instance ArrowApply a => Monad (ArrowMonad a) where
239 return x = ArrowMonad (arr (\z -> x))
240 ArrowMonad m >>= f = ArrowMonad (m >>>
241 arr (\x -> let ArrowMonad h = f x in (h, ())) >>>
244 -- | Any instance of 'ArrowApply' can be made into an instance of
245 -- 'ArrowChoice' by defining 'left' = 'leftApp'.
247 leftApp :: ArrowApply a => a b c -> a (Either b d) (Either c d)
248 leftApp f = arr ((\b -> (arr (\() -> b) >>> f >>> arr Left, ())) |||
249 (\d -> (arr (\() -> d) >>> arr Right, ()))) >>> app
251 -- | The 'loop' operator expresses computations in which an output value is
252 -- fed back as input, even though the computation occurs only once.
253 -- It underlies the @rec@ value recursion construct in arrow notation.
255 class Arrow a => ArrowLoop a where
256 loop :: a (b,d) (c,d) -> a b c
258 instance ArrowLoop (->) where
259 loop f b = let (c,d) = f (b,d) in c
261 instance MonadFix m => ArrowLoop (Kleisli m) where
262 loop (Kleisli f) = Kleisli (liftM fst . mfix . f')
263 where f' x y = f (x, snd y)