fix nhc98 build: need a qualified Prelude import
[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.haskell.org/arrows/>.
20
21 module Control.Arrow (
22                 -- * Arrows
23                 Arrow(..), Kleisli(..),
24                 -- ** Derived combinators
25                 returnA,
26                 (^>>), (>>^),
27                 -- ** Right-to-left variants
28                 (<<^), (^<<),
29                 -- * Monoid operations
30                 ArrowZero(..), ArrowPlus(..),
31                 -- * Conditionals
32                 ArrowChoice(..),
33                 -- * Arrow application
34                 ArrowApply(..), ArrowMonad(..), leftApp,
35                 -- * Feedback
36                 ArrowLoop(..)
37         ) where
38
39 import Prelude hiding (id,(.))
40 import qualified Prelude
41
42 import Control.Monad
43 import Control.Monad.Fix
44 import Control.Category
45
46 infixr 5 <+>
47 infixr 3 ***
48 infixr 3 &&&
49 infixr 2 +++
50 infixr 2 |||
51 infixr 1 ^>>, >>^
52 infixr 1 ^<<, <<^
53
54 -- | The basic arrow class.
55 --   Any instance must define either 'arr' or 'pure' (which are synonyms),
56 --   as well as 'first'.  The other combinators have sensible
57 --   default definitions, which may be overridden for efficiency.
58
59 class Category a => Arrow a where
60
61         -- | Lift a function to an arrow: you must define either this
62         --   or 'pure'.
63         arr :: (b -> c) -> a b c
64         arr = pure
65
66         -- | A synonym for 'arr': you must define one or other of them.
67         pure :: (b -> c) -> a b c
68         pure = arr
69
70         -- | Send the first component of the input through the argument
71         --   arrow, and copy the rest unchanged to the output.
72         first :: a b c -> a (b,d) (c,d)
73
74         -- | A mirror image of 'first'.
75         --
76         --   The default definition may be overridden with a more efficient
77         --   version if desired.
78         second :: a b c -> a (d,b) (d,c)
79         second f = arr swap >>> first f >>> arr swap
80                         where   swap ~(x,y) = (y,x)
81
82         -- | Split the input between the two argument arrows and combine
83         --   their output.  Note that this is in general not a functor.
84         --
85         --   The default definition may be overridden with a more efficient
86         --   version if desired.
87         (***) :: a b c -> a b' c' -> a (b,b') (c,c')
88         f *** g = first f >>> second g
89
90         -- | Fanout: send the input to both argument arrows and combine
91         --   their output.
92         --
93         --   The default definition may be overridden with a more efficient
94         --   version if desired.
95         (&&&) :: a b c -> a b c' -> a b (c,c')
96         f &&& g = arr (\b -> (b,b)) >>> f *** g
97
98 {-# RULES
99 "identity"
100                 arr id = id
101 "compose/arr"   forall f g .
102                 (arr f) . (arr g) = arr (f . g)
103 "first/arr"     forall f .
104                 first (arr f) = arr (first f)
105 "second/arr"    forall f .
106                 second (arr f) = arr (second f)
107 "product/arr"   forall f g .
108                 arr f *** arr g = arr (f *** g)
109 "fanout/arr"    forall f g .
110                 arr f &&& arr g = arr (f &&& g)
111 "compose/first" forall f g .
112                 (first f) . (first g) = first (f . g)
113 "compose/second" forall f g .
114                 (second f) . (second g) = second (f . g)
115  #-}
116
117 -- Ordinary functions are arrows.
118
119 instance Arrow (->) where
120         arr f = f
121         first f = f *** id
122         second f = id *** f
123 --      (f *** g) ~(x,y) = (f x, g y)
124 --      sorry, although the above defn is fully H'98, nhc98 can't parse it.
125         (***) f g ~(x,y) = (f x, g y)
126
127 -- | Kleisli arrows of a monad.
128
129 newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }
130
131 instance Monad m => Category (Kleisli m) where
132         id = Kleisli return
133         (Kleisli f) . (Kleisli g) = Kleisli (\b -> g b >>= f)
134
135 instance Monad m => Arrow (Kleisli m) where
136         arr f = Kleisli (return . f)
137         first (Kleisli f) = Kleisli (\ ~(b,d) -> f b >>= \c -> return (c,d))
138         second (Kleisli f) = Kleisli (\ ~(d,b) -> f b >>= \c -> return (d,c))
139
140 -- | The identity arrow, which plays the role of 'return' in arrow notation.
141
142 returnA :: Arrow a => a b b
143 returnA = arr id
144
145 -- | Precomposition with a pure function.
146 (^>>) :: Arrow a => (b -> c) -> a c d -> a b d
147 f ^>> a = arr f >>> a
148
149 -- | Postcomposition with a pure function.
150 (>>^) :: Arrow a => a b c -> (c -> d) -> a b d
151 a >>^ f = a >>> arr f
152
153 -- | Precomposition with a pure function (right-to-left variant).
154 (<<^) :: Arrow a => a c d -> (b -> c) -> a b d
155 a <<^ f = a <<< arr f
156
157 -- | Postcomposition with a pure function (right-to-left variant).
158 (^<<) :: Arrow a => (c -> d) -> a b c -> a b d
159 f ^<< a = arr f <<< a
160
161 class Arrow a => ArrowZero a where
162         zeroArrow :: a b c
163
164 instance MonadPlus m => ArrowZero (Kleisli m) where
165         zeroArrow = Kleisli (\x -> mzero)
166
167 class ArrowZero a => ArrowPlus a where
168         (<+>) :: a b c -> a b c -> a b c
169
170 instance MonadPlus m => ArrowPlus (Kleisli m) where
171         Kleisli f <+> Kleisli g = Kleisli (\x -> f x `mplus` g x)
172
173 -- | Choice, for arrows that support it.  This class underlies the
174 --   @if@ and @case@ constructs in arrow notation.
175 --   Any instance must define 'left'.  The other combinators have sensible
176 --   default definitions, which may be overridden for efficiency.
177
178 class Arrow a => ArrowChoice a where
179
180         -- | Feed marked inputs through the argument arrow, passing the
181         --   rest through unchanged to the output.
182         left :: a b c -> a (Either b d) (Either c d)
183
184         -- | A mirror image of 'left'.
185         --
186         --   The default definition may be overridden with a more efficient
187         --   version if desired.
188         right :: a b c -> a (Either d b) (Either d c)
189         right f = arr mirror >>> left f >>> arr mirror
190                         where   mirror (Left x) = Right x
191                                 mirror (Right y) = Left y
192
193         -- | Split the input between the two argument arrows, retagging
194         --   and merging their outputs.
195         --   Note that this is in general not a functor.
196         --
197         --   The default definition may be overridden with a more efficient
198         --   version if desired.
199         (+++) :: a b c -> a b' c' -> a (Either b b') (Either c c')
200         f +++ g = left f >>> right g
201
202         -- | Fanin: Split the input between the two argument arrows and
203         --   merge their outputs.
204         --
205         --   The default definition may be overridden with a more efficient
206         --   version if desired.
207         (|||) :: a b d -> a c d -> a (Either b c) d
208         f ||| g = f +++ g >>> arr untag
209                         where   untag (Left x) = x
210                                 untag (Right y) = y
211
212 {-# RULES
213 "left/arr"      forall f .
214                 left (arr f) = arr (left f)
215 "right/arr"     forall f .
216                 right (arr f) = arr (right f)
217 "sum/arr"       forall f g .
218                 arr f +++ arr g = arr (f +++ g)
219 "fanin/arr"     forall f g .
220                 arr f ||| arr g = arr (f ||| g)
221 "compose/left"  forall f g .
222                 left f >>> left g = left (f >>> g)
223 "compose/right" forall f g .
224                 right f >>> right g = right (f >>> g)
225  #-}
226
227 instance ArrowChoice (->) where
228         left f = f +++ id
229         right f = id +++ f
230         f +++ g = (Left . f) ||| (Right . g)
231         (|||) = either
232
233 instance Monad m => ArrowChoice (Kleisli m) where
234         left f = f +++ arr id
235         right f = arr id +++ f
236         f +++ g = (f >>> arr Left) ||| (g >>> arr Right)
237         Kleisli f ||| Kleisli g = Kleisli (either f g)
238
239 -- | Some arrows allow application of arrow inputs to other inputs.
240
241 class Arrow a => ArrowApply a where
242         app :: a (a b c, b) c
243
244 instance ArrowApply (->) where
245         app (f,x) = f x
246
247 instance Monad m => ArrowApply (Kleisli m) where
248         app = Kleisli (\(Kleisli f, x) -> f x)
249
250 -- | The 'ArrowApply' class is equivalent to 'Monad': any monad gives rise
251 --   to a 'Kleisli' arrow, and any instance of 'ArrowApply' defines a monad.
252
253 newtype ArrowApply a => ArrowMonad a b = ArrowMonad (a () b)
254
255 instance ArrowApply a => Monad (ArrowMonad a) where
256         return x = ArrowMonad (arr (\z -> x))
257         ArrowMonad m >>= f = ArrowMonad (m >>>
258                         arr (\x -> let ArrowMonad h = f x in (h, ())) >>>
259                         app)
260
261 -- | Any instance of 'ArrowApply' can be made into an instance of
262 --   'ArrowChoice' by defining 'left' = 'leftApp'.
263
264 leftApp :: ArrowApply a => a b c -> a (Either b d) (Either c d)
265 leftApp f = arr ((\b -> (arr (\() -> b) >>> f >>> arr Left, ())) |||
266                  (\d -> (arr (\() -> d) >>> arr Right, ()))) >>> app
267
268 -- | The 'loop' operator expresses computations in which an output value is
269 --   fed back as input, even though the computation occurs only once.
270 --   It underlies the @rec@ value recursion construct in arrow notation.
271
272 class Arrow a => ArrowLoop a where
273         loop :: a (b,d) (c,d) -> a b c
274
275 instance ArrowLoop (->) where
276         loop f b = let (c,d) = f (b,d) in c
277
278 instance MonadFix m => ArrowLoop (Kleisli m) where
279         loop (Kleisli f) = Kleisli (liftM fst . mfix . f')
280                 where   f' x y = f (x, snd y)