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 -- $Id: Arrow.hs,v 1.1 2002/02/26 18:19:17 ross Exp $
13 -- Basic arrow definitions, based on
15 -- "Generalising Monads to Arrows", by John Hughes, Science of
16 -- Computer Programming 37, pp67-111, May 2000.
18 -- plus a couple of definitions (returnA and loop) from
20 -- "A New Notation for Arrows", by Ross Paterson, in ICFP 2001,
21 -- Firenze, Italy, pp229-240.
23 -- See these papers for the equations these combinators are expected to
24 -- satisfy. These papers and more information on arrows can be found at
26 -- http://www.soi.city.ac.uk/~ross/arrows/
28 -----------------------------------------------------------------------------
30 module Control.Arrow where
35 import Control.Monad.Fix
45 -----------------------------------------------------------------------------
49 arr :: (b -> c) -> a b c
50 (>>>) :: a b c -> a c d -> a b d
51 first :: a b c -> a (b,d) (c,d)
53 -- The following combinators are placed in the class so that they
54 -- can be overridden with more efficient versions if required.
55 -- Any replacements should satisfy these equations.
57 second :: a b c -> a (d,b) (d,c)
58 second f = arr swap >>> first f >>> arr swap
59 where swap ~(x,y) = (y,x)
61 (***) :: a b c -> a b' c' -> a (b,b') (c,c')
62 f *** g = first f >>> second g
64 (&&&) :: a b c -> a b c' -> a b (c,c')
65 f &&& g = arr (\b -> (b,b)) >>> f *** g
67 -- Some people prefer the name pure to arr, so both are allowed,
68 -- but you must define one of them:
70 pure :: (b -> c) -> a b c
74 -----------------------------------------------------------------------------
75 -- Derived combinators
77 -- The counterpart of return in arrow notation:
79 returnA :: Arrow a => a b b
82 -- Mirror image of >>>, for a better fit with arrow notation:
84 (<<<) :: Arrow a => a c d -> a b c -> a b d
87 -----------------------------------------------------------------------------
90 class Arrow a => ArrowZero a where
93 class ArrowZero a => ArrowPlus a where
94 (<+>) :: a b c -> a b c -> a b c
96 -----------------------------------------------------------------------------
99 class Arrow a => ArrowChoice a where
100 left :: a b c -> a (Either b d) (Either c d)
102 -- The following combinators are placed in the class so that they
103 -- can be overridden with more efficient versions if required.
104 -- Any replacements should satisfy these equations.
106 right :: a b c -> a (Either d b) (Either d c)
107 right f = arr mirror >>> left f >>> arr mirror
108 where mirror (Left x) = Right x
109 mirror (Right y) = Left y
111 (+++) :: a b c -> a b' c' -> a (Either b b') (Either c c')
112 f +++ g = left f >>> right g
114 (|||) :: a b d -> a c d -> a (Either b c) d
115 f ||| g = f +++ g >>> arr untag
116 where untag (Left x) = x
119 -----------------------------------------------------------------------------
122 class Arrow a => ArrowApply a where
123 app :: a (a b c, b) c
125 -- Any instance of ArrowApply can be made into an instance if ArrowChoice
126 -- by defining left = leftApp, where
128 leftApp :: ArrowApply a => a b c -> a (Either b d) (Either c d)
129 leftApp f = arr ((\b -> (arr (\() -> b) >>> f >>> arr Left, ())) |||
130 (\d -> (arr (\() -> d) >>> arr Right, ()))) >>> app
132 -- The ArrowApply class is equivalent to Monad: any monad gives rise to
133 -- a Kliesli arrow (see below), and any instance of ArrowApply defines
136 newtype ArrowApply a => ArrowMonad a b = ArrowMonad (a () b)
138 instance ArrowApply a => Monad (ArrowMonad a) where
139 return x = ArrowMonad (arr (\z -> x))
140 ArrowMonad m >>= f = ArrowMonad (m >>>
141 arr (\x -> let ArrowMonad h = f x in (h, ())) >>>
144 -----------------------------------------------------------------------------
147 -- The following operator expresses computations in which a value is
148 -- recursively defined through the computation, even though the computation
151 class Arrow a => ArrowLoop a where
152 loop :: a (b,d) (c,d) -> a b c
154 -----------------------------------------------------------------------------
157 -- Ordinary functions are arrows.
159 instance Arrow (->) where
164 (f *** g) ~(x,y) = (f x, g y)
166 instance ArrowChoice (->) where
169 f +++ g = (Left . f) ||| (Right . g)
172 instance ArrowApply (->) where
175 instance ArrowLoop (->) where
176 loop f b = let (c,d) = f (b,d) in c
178 -----------------------------------------------------------------------------
179 -- Kleisli arrows of a monad
181 newtype Kleisli m a b = Kleisli (a -> m b)
183 instance Monad m => Arrow (Kleisli m) where
184 arr f = Kleisli (return . f)
185 Kleisli f >>> Kleisli g = Kleisli (\b -> f b >>= g)
186 first (Kleisli f) = Kleisli (\ ~(b,d) -> f b >>= \c -> return (c,d))
187 second (Kleisli f) = Kleisli (\ ~(d,b) -> f b >>= \c -> return (d,c))
189 instance MonadPlus m => ArrowZero (Kleisli m) where
190 zeroArrow = Kleisli (\x -> mzero)
192 instance MonadPlus m => ArrowPlus (Kleisli m) where
193 Kleisli f <+> Kleisli g = Kleisli (\x -> f x `mplus` g x)
195 instance Monad m => ArrowChoice (Kleisli m) where
196 left f = f +++ arr id
197 right f = arr id +++ f
198 f +++ g = (f >>> arr Left) ||| (g >>> arr Right)
199 Kleisli f ||| Kleisli g = Kleisli (either f g)
201 instance Monad m => ArrowApply (Kleisli m) where
202 app = Kleisli (\(Kleisli f, x) -> f x)
204 instance MonadFix m => ArrowLoop (Kleisli m) where
205 loop (Kleisli f) = Kleisli (liftM fst . mfix . f')
206 where f' x y = f (x, snd y)