fix a discarded exception in hClose
[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                                 swap ~(x,y) = (y,x)
79
80         -- | Split the input between the two argument arrows and combine
81         --   their output.  Note that this is in general not a functor.
82         --
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
87
88         -- | Fanout: send the input to both argument arrows and combine
89         --   their output.
90         --
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
95
96 {-# RULES
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)
111  #-}
112
113 -- Ordinary functions are arrows.
114
115 instance Arrow (->) where
116         arr f = f
117         first f = f *** id
118         second f = id *** f
119 --      (f *** g) ~(x,y) = (f x, g y)
120 --      sorry, although the above defn is fully H'98, nhc98 can't parse it.
121         (***) f g ~(x,y) = (f x, g y)
122
123 -- | Kleisli arrows of a monad.
124
125 newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }
126
127 instance Monad m => Category (Kleisli m) where
128         id = Kleisli return
129         (Kleisli f) . (Kleisli g) = Kleisli (\b -> g b >>= f)
130
131 instance Monad m => Arrow (Kleisli m) where
132         arr f = Kleisli (return . f)
133         first (Kleisli f) = Kleisli (\ ~(b,d) -> f b >>= \c -> return (c,d))
134         second (Kleisli f) = Kleisli (\ ~(d,b) -> f b >>= \c -> return (d,c))
135
136 -- | The identity arrow, which plays the role of 'return' in arrow notation.
137
138 returnA :: Arrow a => a b b
139 returnA = arr id
140
141 -- | Precomposition with a pure function.
142 (^>>) :: Arrow a => (b -> c) -> a c d -> a b d
143 f ^>> a = arr f >>> a
144
145 -- | Postcomposition with a pure function.
146 (>>^) :: Arrow a => a b c -> (c -> d) -> a b d
147 a >>^ f = a >>> arr f
148
149 -- | Precomposition with a pure function (right-to-left variant).
150 (<<^) :: Arrow a => a c d -> (b -> c) -> a b d
151 a <<^ f = a <<< arr f
152
153 -- | Postcomposition with a pure function (right-to-left variant).
154 (^<<) :: Arrow a => (c -> d) -> a b c -> a b d
155 f ^<< a = arr f <<< a
156
157 class Arrow a => ArrowZero a where
158         zeroArrow :: a b c
159
160 instance MonadPlus m => ArrowZero (Kleisli m) where
161         zeroArrow = Kleisli (\_ -> mzero)
162
163 class ArrowZero a => ArrowPlus a where
164         (<+>) :: a b c -> a b c -> a b c
165
166 instance MonadPlus m => ArrowPlus (Kleisli m) where
167         Kleisli f <+> Kleisli g = Kleisli (\x -> f x `mplus` g x)
168
169 -- | Choice, for arrows that support it.  This class underlies the
170 --   @if@ and @case@ constructs in arrow notation.
171 --   Any instance must define 'left'.  The other combinators have sensible
172 --   default definitions, which may be overridden for efficiency.
173
174 class Arrow a => ArrowChoice a where
175
176         -- | Feed marked inputs through the argument arrow, passing the
177         --   rest through unchanged to the output.
178         left :: a b c -> a (Either b d) (Either c d)
179
180         -- | A mirror image of 'left'.
181         --
182         --   The default definition may be overridden with a more efficient
183         --   version if desired.
184         right :: a b c -> a (Either d b) (Either d c)
185         right f = arr mirror >>> left f >>> arr mirror
186                         where   mirror :: Either x y -> Either y x
187                                 mirror (Left x) = Right x
188                                 mirror (Right y) = Left y
189
190         -- | Split the input between the two argument arrows, retagging
191         --   and merging their outputs.
192         --   Note that this is in general not a functor.
193         --
194         --   The default definition may be overridden with a more efficient
195         --   version if desired.
196         (+++) :: a b c -> a b' c' -> a (Either b b') (Either c c')
197         f +++ g = left f >>> right g
198
199         -- | Fanin: Split the input between the two argument arrows and
200         --   merge their outputs.
201         --
202         --   The default definition may be overridden with a more efficient
203         --   version if desired.
204         (|||) :: a b d -> a c d -> a (Either b c) d
205         f ||| g = f +++ g >>> arr untag
206                         where   untag (Left x) = x
207                                 untag (Right y) = y
208
209 {-# RULES
210 "left/arr"      forall f .
211                 left (arr f) = arr (left f)
212 "right/arr"     forall f .
213                 right (arr f) = arr (right f)
214 "sum/arr"       forall f g .
215                 arr f +++ arr g = arr (f +++ g)
216 "fanin/arr"     forall f g .
217                 arr f ||| arr g = arr (f ||| g)
218 "compose/left"  forall f g .
219                 left f . left g = left (f . g)
220 "compose/right" forall f g .
221                 right f . right g = right (f . g)
222  #-}
223
224 instance ArrowChoice (->) where
225         left f = f +++ id
226         right f = id +++ f
227         f +++ g = (Left . f) ||| (Right . g)
228         (|||) = either
229
230 instance Monad m => ArrowChoice (Kleisli m) where
231         left f = f +++ arr id
232         right f = arr id +++ f
233         f +++ g = (f >>> arr Left) ||| (g >>> arr Right)
234         Kleisli f ||| Kleisli g = Kleisli (either f g)
235
236 -- | Some arrows allow application of arrow inputs to other inputs.
237
238 class Arrow a => ArrowApply a where
239         app :: a (a b c, b) c
240
241 instance ArrowApply (->) where
242         app (f,x) = f x
243
244 instance Monad m => ArrowApply (Kleisli m) where
245         app = Kleisli (\(Kleisli f, x) -> f x)
246
247 -- | The 'ArrowApply' class is equivalent to 'Monad': any monad gives rise
248 --   to a 'Kleisli' arrow, and any instance of 'ArrowApply' defines a monad.
249
250 newtype ArrowApply a => ArrowMonad a b = ArrowMonad (a () b)
251
252 instance ArrowApply a => Monad (ArrowMonad a) where
253         return x = ArrowMonad (arr (\_ -> x))
254         ArrowMonad m >>= f = ArrowMonad (m >>>
255                         arr (\x -> let ArrowMonad h = f x in (h, ())) >>>
256                         app)
257
258 -- | Any instance of 'ArrowApply' can be made into an instance of
259 --   'ArrowChoice' by defining 'left' = 'leftApp'.
260
261 leftApp :: ArrowApply a => a b c -> a (Either b d) (Either c d)
262 leftApp f = arr ((\b -> (arr (\() -> b) >>> f >>> arr Left, ())) |||
263                  (\d -> (arr (\() -> d) >>> arr Right, ()))) >>> app
264
265 -- | The 'loop' operator expresses computations in which an output value is
266 --   fed back as input, even though the computation occurs only once.
267 --   It underlies the @rec@ value recursion construct in arrow notation.
268
269 class Arrow a => ArrowLoop a where
270         loop :: a (b,d) (c,d) -> a b c
271
272 instance ArrowLoop (->) where
273         loop f b = let (c,d) = f (b,d) in c
274
275 instance MonadFix m => ArrowLoop (Kleisli m) where
276         loop (Kleisli f) = Kleisli (liftM fst . mfix . f')
277                 where   f' x y = f (x, snd y)