From a64c2b5ae075922828a702826d4dbc0ce7c3c7d0 Mon Sep 17 00:00:00 2001 From: ross Date: Wed, 24 Apr 2002 17:57:55 +0000 Subject: [PATCH] [project @ 2002-04-24 17:57:55 by ross] haddock food. --- Control/Arrow.hs | 218 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 123 insertions(+), 95 deletions(-) diff --git a/Control/Arrow.hs b/Control/Arrow.hs index 237c37f..34783e0 100644 --- a/Control/Arrow.hs +++ b/Control/Arrow.hs @@ -1,5 +1,5 @@ ----------------------------------------------------------------------------- --- +-- | -- Module : Control.Arrow -- Copyright : (c) Ross Paterson 2002 -- License : BSD-style (see the LICENSE file in the distribution) @@ -8,29 +8,20 @@ -- Stability : experimental -- Portability : portable -- --- $Id: Arrow.hs,v 1.1 2002/02/26 18:19:17 ross Exp $ +-- $Id: Arrow.hs,v 1.2 2002/04/24 17:57:55 ross Exp $ -- -- Basic arrow definitions, based on --- --- "Generalising Monads to Arrows", by John Hughes, Science of --- Computer Programming 37, pp67-111, May 2000. --- --- plus a couple of definitions (returnA and loop) from --- --- "A New Notation for Arrows", by Ross Paterson, in ICFP 2001, +-- /Generalising Monads to Arrows/, by John Hughes, +-- /Science of Computer Programming/ 37, pp67-111, May 2000. +-- plus a couple of definitions ('returnA' and 'loop') from +-- /A New Notation for Arrows/, by Ross Paterson, in /ICFP 2001/, -- Firenze, Italy, pp229-240. --- -- See these papers for the equations these combinators are expected to -- satisfy. These papers and more information on arrows can be found at --- --- http://www.soi.city.ac.uk/~ross/arrows/ --- ------------------------------------------------------------------------------ +-- . module Control.Arrow where -import Prelude - import Control.Monad import Control.Monad.Fix @@ -43,95 +34,172 @@ infixr 1 >>> infixr 1 <<< ----------------------------------------------------------------------------- --- Arrow classes +-- * Arrows + +-- | The basic arrow class. +-- Any instance must define either 'arr' or 'pure' (which are synonyms), +-- as well as '>>>' and 'first'. The other combinators have sensible +-- default definitions, which may be overridden for efficiency. class Arrow a where + + -- | Lift a function to an arrow: you must define either this + -- or 'pure'. arr :: (b -> c) -> a b c + arr = pure + + -- | A synonym for 'arr': you must define one or other of them. + pure :: (b -> c) -> a b c + pure = arr + + -- | Left-to-right composition of arrows. (>>>) :: a b c -> a c d -> a b d - first :: a b c -> a (b,d) (c,d) - -- The following combinators are placed in the class so that they - -- can be overridden with more efficient versions if required. - -- Any replacements should satisfy these equations. + -- | Send the first component of the input through the argument + -- arrow, and copy the rest unchanged to the output. + first :: a b c -> a (b,d) (c,d) + -- | A mirror image of 'first'. + -- + -- The default definition may be overridden with a more efficient + -- version if desired. second :: a b c -> a (d,b) (d,c) second f = arr swap >>> first f >>> arr swap where swap ~(x,y) = (y,x) + -- | Split the input between the two argument arrows and combine + -- their output. Note that this is in general not a functor. + -- + -- The default definition may be overridden with a more efficient + -- version if desired. (***) :: a b c -> a b' c' -> a (b,b') (c,c') f *** g = first f >>> second g + -- | Fanout: send the input to both argument arrows and combine + -- their output. + -- + -- The default definition may be overridden with a more efficient + -- version if desired. (&&&) :: a b c -> a b c' -> a b (c,c') f &&& g = arr (\b -> (b,b)) >>> f *** g - -- Some people prefer the name pure to arr, so both are allowed, - -- but you must define one of them: +-- Ordinary functions are arrows. - pure :: (b -> c) -> a b c - pure = arr - arr = pure +instance Arrow (->) where + arr f = f + f >>> g = g . f + first f = f *** id + second f = id *** f + (f *** g) ~(x,y) = (f x, g y) + +-- | Kleisli arrows of a monad. + +newtype Kleisli m a b = Kleisli (a -> m b) + +instance Monad m => Arrow (Kleisli m) where + arr f = Kleisli (return . f) + Kleisli f >>> Kleisli g = Kleisli (\b -> f b >>= g) + first (Kleisli f) = Kleisli (\ ~(b,d) -> f b >>= \c -> return (c,d)) + second (Kleisli f) = Kleisli (\ ~(d,b) -> f b >>= \c -> return (d,c)) ----------------------------------------------------------------------------- --- Derived combinators +-- ** Derived combinators --- The counterpart of return in arrow notation: +-- | The identity arrow, which plays the role of 'return' in arrow notation. returnA :: Arrow a => a b b returnA = arr id --- Mirror image of >>>, for a better fit with arrow notation: +-- | Right-to-left composition, for a better fit with arrow notation. (<<<) :: Arrow a => a c d -> a b c -> a b d f <<< g = g >>> f ----------------------------------------------------------------------------- --- Monoid operations +-- * Monoid operations class Arrow a => ArrowZero a where zeroArrow :: a b c +instance MonadPlus m => ArrowZero (Kleisli m) where + zeroArrow = Kleisli (\x -> mzero) + class ArrowZero a => ArrowPlus a where (<+>) :: a b c -> a b c -> a b c +instance MonadPlus m => ArrowPlus (Kleisli m) where + Kleisli f <+> Kleisli g = Kleisli (\x -> f x `mplus` g x) + ----------------------------------------------------------------------------- --- Conditionals +-- * Conditionals + +-- | Choice, for arrows that support it. This class underlies the +-- [if] and [case] constructs in arrow notation. +-- Any instance must define 'left'. The other combinators have sensible +-- default definitions, which may be overridden for efficiency. class Arrow a => ArrowChoice a where - left :: a b c -> a (Either b d) (Either c d) - -- The following combinators are placed in the class so that they - -- can be overridden with more efficient versions if required. - -- Any replacements should satisfy these equations. + -- | Feed marked inputs through the argument arrow, passing the + -- rest through unchanged to the output. + left :: a b c -> a (Either b d) (Either c d) + -- | A mirror image of 'left'. + -- + -- The default definition may be overridden with a more efficient + -- version if desired. right :: a b c -> a (Either d b) (Either d c) right f = arr mirror >>> left f >>> arr mirror where mirror (Left x) = Right x mirror (Right y) = Left y + -- | Split the input between the two argument arrows, retagging + -- and merging their outputs. + -- Note that this is in general not a functor. + -- + -- The default definition may be overridden with a more efficient + -- version if desired. (+++) :: a b c -> a b' c' -> a (Either b b') (Either c c') f +++ g = left f >>> right g + -- | Fanin: Split the input between the two argument arrows and + -- merge their outputs. + -- + -- The default definition may be overridden with a more efficient + -- version if desired. (|||) :: a b d -> a c d -> a (Either b c) d f ||| g = f +++ g >>> arr untag where untag (Left x) = x untag (Right y) = y +instance ArrowChoice (->) where + left f = f +++ id + right f = id +++ f + f +++ g = (Left . f) ||| (Right . g) + (|||) = either + +instance Monad m => ArrowChoice (Kleisli m) where + left f = f +++ arr id + right f = arr id +++ f + f +++ g = (f >>> arr Left) ||| (g >>> arr Right) + Kleisli f ||| Kleisli g = Kleisli (either f g) + ----------------------------------------------------------------------------- --- Arrow application +-- * Arrow application + +-- | Some arrows allow application of arrow inputs to other inputs. class Arrow a => ArrowApply a where app :: a (a b c, b) c --- Any instance of ArrowApply can be made into an instance if ArrowChoice --- by defining left = leftApp, where +instance ArrowApply (->) where + app (f,x) = f x -leftApp :: ArrowApply a => a b c -> a (Either b d) (Either c d) -leftApp f = arr ((\b -> (arr (\() -> b) >>> f >>> arr Left, ())) ||| - (\d -> (arr (\() -> d) >>> arr Right, ()))) >>> app +instance Monad m => ArrowApply (Kleisli m) where + app = Kleisli (\(Kleisli f, x) -> f x) --- The ArrowApply class is equivalent to Monad: any monad gives rise to --- a Kliesli arrow (see below), and any instance of ArrowApply defines --- a monad: +-- | The 'ArrowApply' class is equivalent to 'Monad': any monad gives rise +-- to a 'Kleisli' arrow, and any instance of 'ArrowApply' defines a monad. newtype ArrowApply a => ArrowMonad a b = ArrowMonad (a () b) @@ -141,66 +209,26 @@ instance ArrowApply a => Monad (ArrowMonad a) where arr (\x -> let ArrowMonad h = f x in (h, ())) >>> app) ------------------------------------------------------------------------------ --- Feedback - --- The following operator expresses computations in which a value is --- recursively defined through the computation, even though the computation --- occurs only once: +-- | Any instance of 'ArrowApply' can be made into an instance of +-- 'ArrowChoice' by defining 'left' = 'leftApp'. -class Arrow a => ArrowLoop a where - loop :: a (b,d) (c,d) -> a b c +leftApp :: ArrowApply a => a b c -> a (Either b d) (Either c d) +leftApp f = arr ((\b -> (arr (\() -> b) >>> f >>> arr Left, ())) ||| + (\d -> (arr (\() -> d) >>> arr Right, ()))) >>> app ----------------------------------------------------------------------------- --- Arrow instances +-- * Feedback --- Ordinary functions are arrows. - -instance Arrow (->) where - arr f = f - f >>> g = g . f - first f = f *** id - second f = id *** f - (f *** g) ~(x,y) = (f x, g y) - -instance ArrowChoice (->) where - left f = f +++ id - right f = id +++ f - f +++ g = (Left . f) ||| (Right . g) - (|||) = either +-- | The 'loop' operator expresses computations in which an output value is +-- fed back as input, even though the computation occurs only once. +-- It underlies the [rec] value recursion construct in arrow notation. -instance ArrowApply (->) where - app (f,x) = f x +class Arrow a => ArrowLoop a where + loop :: a (b,d) (c,d) -> a b c instance ArrowLoop (->) where loop f b = let (c,d) = f (b,d) in c ------------------------------------------------------------------------------ --- Kleisli arrows of a monad - -newtype Kleisli m a b = Kleisli (a -> m b) - -instance Monad m => Arrow (Kleisli m) where - arr f = Kleisli (return . f) - Kleisli f >>> Kleisli g = Kleisli (\b -> f b >>= g) - first (Kleisli f) = Kleisli (\ ~(b,d) -> f b >>= \c -> return (c,d)) - second (Kleisli f) = Kleisli (\ ~(d,b) -> f b >>= \c -> return (d,c)) - -instance MonadPlus m => ArrowZero (Kleisli m) where - zeroArrow = Kleisli (\x -> mzero) - -instance MonadPlus m => ArrowPlus (Kleisli m) where - Kleisli f <+> Kleisli g = Kleisli (\x -> f x `mplus` g x) - -instance Monad m => ArrowChoice (Kleisli m) where - left f = f +++ arr id - right f = arr id +++ f - f +++ g = (f >>> arr Left) ||| (g >>> arr Right) - Kleisli f ||| Kleisli g = Kleisli (either f g) - -instance Monad m => ArrowApply (Kleisli m) where - app = Kleisli (\(Kleisli f, x) -> f x) - instance MonadFix m => ArrowLoop (Kleisli m) where loop (Kleisli f) = Kleisli (liftM fst . mfix . f') where f' x y = f (x, snd y) -- 1.7.10.4