[project @ 2002-04-26 13:34:05 by simonmar]
[ghc-base.git] / Control / Arrow.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Control.Arrow
4 -- Copyright   :  (c) Ross Paterson 2002
5 -- License     :  BSD-style (see the LICENSE file in the distribution)
6 --
7 -- Maintainer  :  ross@soi.city.ac.uk
8 -- Stability   :  experimental
9 -- Portability :  portable
10 --
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.soi.city.ac.uk/~ross/arrows/>.
20
21 module Control.Arrow where
22
23 import Control.Monad
24 import Control.Monad.Fix
25
26 infixr 5 <+>
27 infixr 3 ***
28 infixr 3 &&&
29 infixr 2 +++
30 infixr 2 |||
31 infixr 1 >>>
32 infixr 1 <<<
33
34 -----------------------------------------------------------------------------
35 -- * Arrows
36
37 -- | The basic arrow class.
38 --   Any instance must define either 'arr' or 'pure' (which are synonyms),
39 --   as well as '>>>' and 'first'.  The other combinators have sensible
40 --   default definitions, which may be overridden for efficiency.
41
42 class Arrow a where
43
44         -- | Lift a function to an arrow: you must define either this
45         --   or 'pure'.
46         arr :: (b -> c) -> a b c
47         arr = pure
48
49         -- | A synonym for 'arr': you must define one or other of them.
50         pure :: (b -> c) -> a b c
51         pure = arr
52
53         -- | Left-to-right composition of arrows.
54         (>>>) :: a b c -> a c d -> a b d
55
56         -- | Send the first component of the input through the argument
57         --   arrow, and copy the rest unchanged to the output.
58         first :: a b c -> a (b,d) (c,d)
59
60         -- | A mirror image of 'first'.
61         --
62         --   The default definition may be overridden with a more efficient
63         --   version if desired.
64         second :: a b c -> a (d,b) (d,c)
65         second f = arr swap >>> first f >>> arr swap
66                         where   swap ~(x,y) = (y,x)
67
68         -- | Split the input between the two argument arrows and combine
69         --   their output.  Note that this is in general not a functor.
70         --
71         --   The default definition may be overridden with a more efficient
72         --   version if desired.
73         (***) :: a b c -> a b' c' -> a (b,b') (c,c')
74         f *** g = first f >>> second g
75
76         -- | Fanout: send the input to both argument arrows and combine
77         --   their output.
78         --
79         --   The default definition may be overridden with a more efficient
80         --   version if desired.
81         (&&&) :: a b c -> a b c' -> a b (c,c')
82         f &&& g = arr (\b -> (b,b)) >>> f *** g
83
84 -- Ordinary functions are arrows.
85
86 instance Arrow (->) where
87         arr f = f
88         f >>> g = g . f
89         first f = f *** id
90         second f = id *** f
91         (f *** g) ~(x,y) = (f x, g y)
92
93 -- | Kleisli arrows of a monad.
94
95 newtype Kleisli m a b = Kleisli (a -> m b)
96
97 instance Monad m => Arrow (Kleisli m) where
98         arr f = Kleisli (return . f)
99         Kleisli f >>> Kleisli g = Kleisli (\b -> f b >>= g)
100         first (Kleisli f) = Kleisli (\ ~(b,d) -> f b >>= \c -> return (c,d))
101         second (Kleisli f) = Kleisli (\ ~(d,b) -> f b >>= \c -> return (d,c))
102
103 -----------------------------------------------------------------------------
104 -- ** Derived combinators
105
106 -- | The identity arrow, which plays the role of 'return' in arrow notation.
107
108 returnA :: Arrow a => a b b
109 returnA = arr id
110
111 -- | Right-to-left composition, for a better fit with arrow notation.
112
113 (<<<) :: Arrow a => a c d -> a b c -> a b d
114 f <<< g = g >>> f
115
116 -----------------------------------------------------------------------------
117 -- * Monoid operations
118
119 class Arrow a => ArrowZero a where
120         zeroArrow :: a b c
121
122 instance MonadPlus m => ArrowZero (Kleisli m) where
123         zeroArrow = Kleisli (\x -> mzero)
124
125 class ArrowZero a => ArrowPlus a where
126         (<+>) :: a b c -> a b c -> a b c
127
128 instance MonadPlus m => ArrowPlus (Kleisli m) where
129         Kleisli f <+> Kleisli g = Kleisli (\x -> f x `mplus` g x)
130
131 -----------------------------------------------------------------------------
132 -- * Conditionals
133
134 -- | Choice, for arrows that support it.  This class underlies the
135 --   [if] and [case] constructs in arrow notation.
136 --   Any instance must define 'left'.  The other combinators have sensible
137 --   default definitions, which may be overridden for efficiency.
138
139 class Arrow a => ArrowChoice a where
140
141         -- | Feed marked inputs through the argument arrow, passing the
142         --   rest through unchanged to the output.
143         left :: a b c -> a (Either b d) (Either c d)
144
145         -- | A mirror image of 'left'.
146         --
147         --   The default definition may be overridden with a more efficient
148         --   version if desired.
149         right :: a b c -> a (Either d b) (Either d c)
150         right f = arr mirror >>> left f >>> arr mirror
151                         where   mirror (Left x) = Right x
152                                 mirror (Right y) = Left y
153
154         -- | Split the input between the two argument arrows, retagging
155         --   and merging their outputs.
156         --   Note that this is in general not a functor.
157         --
158         --   The default definition may be overridden with a more efficient
159         --   version if desired.
160         (+++) :: a b c -> a b' c' -> a (Either b b') (Either c c')
161         f +++ g = left f >>> right g
162
163         -- | Fanin: Split the input between the two argument arrows and
164         --   merge their outputs.
165         --
166         --   The default definition may be overridden with a more efficient
167         --   version if desired.
168         (|||) :: a b d -> a c d -> a (Either b c) d
169         f ||| g = f +++ g >>> arr untag
170                         where   untag (Left x) = x
171                                 untag (Right y) = y
172
173 instance ArrowChoice (->) where
174         left f = f +++ id
175         right f = id +++ f
176         f +++ g = (Left . f) ||| (Right . g)
177         (|||) = either
178
179 instance Monad m => ArrowChoice (Kleisli m) where
180         left f = f +++ arr id
181         right f = arr id +++ f
182         f +++ g = (f >>> arr Left) ||| (g >>> arr Right)
183         Kleisli f ||| Kleisli g = Kleisli (either f g)
184
185 -----------------------------------------------------------------------------
186 -- * Arrow application
187
188 -- | Some arrows allow application of arrow inputs to other inputs.
189
190 class Arrow a => ArrowApply a where
191         app :: a (a b c, b) c
192
193 instance ArrowApply (->) where
194         app (f,x) = f x
195
196 instance Monad m => ArrowApply (Kleisli m) where
197         app = Kleisli (\(Kleisli f, x) -> f x)
198
199 -- | The 'ArrowApply' class is equivalent to 'Monad': any monad gives rise
200 --   to a 'Kleisli' arrow, and any instance of 'ArrowApply' defines a monad.
201
202 newtype ArrowApply a => ArrowMonad a b = ArrowMonad (a () b)
203
204 instance ArrowApply a => Monad (ArrowMonad a) where
205         return x = ArrowMonad (arr (\z -> x))
206         ArrowMonad m >>= f = ArrowMonad (m >>>
207                         arr (\x -> let ArrowMonad h = f x in (h, ())) >>>
208                         app)
209
210 -- | Any instance of 'ArrowApply' can be made into an instance of
211 --   'ArrowChoice' by defining 'left' = 'leftApp'.
212
213 leftApp :: ArrowApply a => a b c -> a (Either b d) (Either c d)
214 leftApp f = arr ((\b -> (arr (\() -> b) >>> f >>> arr Left, ())) |||
215                  (\d -> (arr (\() -> d) >>> arr Right, ()))) >>> app
216
217 -----------------------------------------------------------------------------
218 -- * Feedback
219
220 -- | The 'loop' operator expresses computations in which an output value is
221 --   fed back as input, even though the computation occurs only once.
222 --   It underlies the [rec] value recursion construct in arrow notation.
223
224 class Arrow a => ArrowLoop a where
225         loop :: a (b,d) (c,d) -> a b c
226
227 instance ArrowLoop (->) where
228         loop f b = let (c,d) = f (b,d) in c
229
230 instance MonadFix m => ArrowLoop (Kleisli m) where
231         loop (Kleisli f) = Kleisli (liftM fst . mfix . f')
232                 where   f' x y = f (x, snd y)