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