base-4.10.1.0: Basic libraries

Copyright | (c) Ross Paterson 2002
License | BSD-style (see the LICENSE file in the distribution)
Maintainer | libraries@haskell.org
Stability | provisional
Portability | portable
Safe Haskell | Trustworthy
Language | Haskell2010

Control.Arrow

Contents

- Arrows
  - Derived combinators
  - Right-to-left variants
- Monoid operations
- Conditionals
- Arrow application
- Feedback

Description

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, Firenze, Italy, pp229-240.

These papers and more information on arrows can be found at http://www.haskell.org/arrows/.

Synopsis

- class Category a => Arrow a where
- newtype Kleisli m a b = Kleisli {
  - runKleisli :: a -> m b
  }
- returnA :: Arrow a => a b b
- (^>>) :: Arrow a => (b -> c) -> a c d -> a b d
- (>>^) :: Arrow a => a b c -> (c -> d) -> a b d
- (>>>) :: Category cat => cat a b -> cat b c -> cat a c
- (<<<) :: Category cat => cat b c -> cat a b -> cat a c
- (<<^) :: Arrow a => a c d -> (b -> c) -> a b d
- (^<<) :: Arrow a => (c -> d) -> a b c -> a b d
- class Arrow a => ArrowZero a where
- class ArrowZero a => ArrowPlus a where
- class Arrow a => ArrowChoice a where
- class Arrow a => ArrowApply a where
- newtype ArrowMonad a b = ArrowMonad (a () b)
- leftApp :: ArrowApply a => a b c -> a (Either b d) (Either c d)
- class Arrow a => ArrowLoop a where

# Arrows

class Category a => Arrow a where #

The basic arrow class.

Instances should satisfy the following laws:

-     arr id = id

-     arr (f >>> g) = arr f >>> arr g

-     first (arr f) = arr (first f)

-     first (f >>> g) = first f >>> first g

-     first f >>> arr fst = arr fst >>> f

-     first f >>> arr (id *** g) = arr (id *** g) >>> first f

-     first (first f) >>> arr assoc = arr assoc >>> first f

where

    assoc ((a,b),c) = (a,(b,c))

The other combinators have sensible default definitions, which may be overridden for efficiency.

Minimal complete definition

arr, (first | (***))

Methods

arr :: (b -> c) -> a b c #

Lift a function to an arrow.

first :: a b c -> a (b, d) (c, d) #

Send the first component of the input through the argument arrow, and copy the rest unchanged to the output.

second :: a b c -> a (d, b) (d, c) #

A mirror image of `first`.

The default definition may be overridden with a more efficient version if desired.

(***) :: a b c -> a b' c' -> a (b, b') (c, c') infixr 3 #

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 (c, c') infixr 3 #

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.

Instances

Monad m => Arrow (Kleisli m) # | Since: 2.1
Methods arr :: (b -> c) -> Kleisli m b c # first :: Kleisli m b c -> Kleisli m (b, d) (c, d) # second :: Kleisli m b c -> Kleisli m (d, b) (d, c) # (***) :: Kleisli m b c -> Kleisli m b' c' -> Kleisli m (b, b') (c, c') # (&&&) :: Kleisli m b c -> Kleisli m b c' -> Kleisli m b (c, c') #
Arrow ((->) LiftedRep LiftedRep) # | Since: 2.1

Methods

arr :: (b -> c) -> (LiftedRep -> LiftedRep) b c #

first :: (LiftedRep -> LiftedRep) b c -> (LiftedRep -> LiftedRep) (b, d) (c, d) #

second :: (LiftedRep -> LiftedRep) b c -> (LiftedRep -> LiftedRep) (d, b) (d, c) #

(***) :: (LiftedRep -> LiftedRep) b c -> (LiftedRep -> LiftedRep) b' c' -> (LiftedRep -> LiftedRep) (b, b') (c, c') #

(&&&) :: (LiftedRep -> LiftedRep) b c -> (LiftedRep -> LiftedRep) b c' -> (LiftedRep -> LiftedRep) b (c, c') #

newtype Kleisli m a b #

Kleisli arrows of a monad.

Constructors

Kleisli |

Fields

- runKleisli :: a -> m b

Instances

MonadFix m => ArrowLoop (Kleisli m) # | Beware that for many monads (those for which the `>>=` operation is strict) this instance will not satisfy the right-tightening law required by the `ArrowLoop` class. Since: 2.1
Methods loop :: Kleisli m (b, d) (c, d) -> Kleisli m b c #
Monad m => ArrowApply (Kleisli m) # | Since: 2.1
Methods app :: Kleisli m (Kleisli m b c, b) c #
Monad m => ArrowChoice (Kleisli m) # | Since: 2.1
Methods left :: Kleisli m b c -> Kleisli m (Either b d) (Either c d) # right :: Kleisli m b c -> Kleisli m (Either d b) (Either d c) # (+++) :: Kleisli m b c -> Kleisli m b' c' -> Kleisli m (Either b b') (Either c c') # (|||) :: Kleisli m b d -> Kleisli m c d -> Kleisli m (Either b c) d #
MonadPlus m => ArrowPlus (Kleisli m) # | Since: 2.1
Methods (<+>) :: Kleisli m b c -> Kleisli m b c -> Kleisli m b c #
MonadPlus m => ArrowZero (Kleisli m) # | Since: 2.1
Methods zeroArrow :: Kleisli m b c #
Monad m => Arrow (Kleisli m) # | Since: 2.1
Methods arr :: (b -> c) -> Kleisli m b c # first :: Kleisli m b c -> Kleisli m (b, d) (c, d) # second :: Kleisli m b c -> Kleisli m (d, b) (d, c) # (***) :: Kleisli m b c -> Kleisli m b' c' -> Kleisli m (b, b') (c, c') # (&&&) :: Kleisli m b c -> Kleisli m b c' -> Kleisli m b (c, c') #
Monad m => Category * (Kleisli m) # | Since: 3.0
Methods id :: cat a a # (.) :: cat b c -> cat a b -> cat a c #

## Derived combinators

returnA :: Arrow a => a b b #

The identity arrow, which plays the role of `return` in arrow notation.

(^>>) :: Arrow a => (b -> c) -> a c d -> a b d infixr 1 #

Precomposition with a pure function.

(>>^) :: Arrow a => a b c -> (c -> d) -> a b d infixr 1 #

Postcomposition with a pure function.

(>>>) :: Category cat => cat a b -> cat b c -> cat a c infixr 1 #

Left-to-right composition

(<<<) :: Category cat => cat b c -> cat a b -> cat a c infixr 1 #

Right-to-left composition

## Right-to-left variants

(<<^) :: Arrow a => a c d -> (b -> c) -> a b d infixr 1 #

Precomposition with a pure function (right-to-left variant).

(^<<) :: Arrow a => (c -> d) -> a b c -> a b d infixr 1 #

Postcomposition with a pure function (right-to-left variant).

# Monoid operations

class Arrow a => ArrowZero a where #

Minimal complete definition

zeroArrow

Methods

zeroArrow :: a b c #

Instances

MonadPlus m => ArrowZero (Kleisli m) # | Since: 2.1
Methods zeroArrow :: Kleisli m b c #

class ArrowZero a => ArrowPlus a where #

A monoid on arrows.

Minimal complete definition

(<+>)

Methods

(<+>) :: a b c -> a b c -> a b c infixr 5 #

An associative operation with identity `zeroArrow`.

Instances

MonadPlus m => ArrowPlus (Kleisli m) # | Since: 2.1
Methods (<+>) :: Kleisli m b c -> Kleisli m b c -> Kleisli m b c #

# Conditionals

class Arrow a => ArrowChoice a where #

Choice, for arrows that support it. This class underlies the `if` and `case` constructs in arrow notation.

Instances should satisfy the following laws:

-     left (arr f) = arr (left f)

-     left (f >>> g) = left f >>> left g

-     f >>> arr Left = arr Left >>> left f

-     left f >>> arr (id +++ g) = arr (id +++ g) >>> left f

-     left (left f) >>> arr assocsum = arr assocsum >>> left f

where

    assocsum (Left (Left x)) = Left x
    assocsum (Left (Right y)) = Right (Left y)
    assocsum (Right z) = Right (Right z)

The other combinators have sensible default definitions, which may be overridden for efficiency.

Methods

left :: a b c -> a (Either b d) (Either c d) #

Feed marked inputs through the argument arrow, passing the rest through unchanged to the output.

right :: a b c -> a (Either d b) (Either d c) #

A mirror image of `left`.

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') infixr 2 #

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 d -> a c d -> a (Either b c) d infixr 2 #

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.

Instances

Monad m => ArrowChoice (Kleisli m) # | Since: 2.1
Methods left :: Kleisli m b c -> Kleisli m (Either b d) (Either c d) # right :: Kleisli m b c -> Kleisli m (Either d b) (Either d c) # (+++) :: Kleisli m b c -> Kleisli m b' c' -> Kleisli m (Either b b') (Either c c') # (|||) :: Kleisli m b d -> Kleisli m c d -> Kleisli m (Either b c) d #
ArrowChoice ((->) LiftedRep LiftedRep) # | Since: 2.1

Methods

left :: (LiftedRep -> LiftedRep) b c -> (LiftedRep -> LiftedRep) (Either b d) (Either c d) #

right :: (LiftedRep -> LiftedRep) b c -> (LiftedRep -> LiftedRep) (Either d b) (Either d c) #

(+++) :: (LiftedRep -> LiftedRep) b c -> (LiftedRep -> LiftedRep) b' c' -> (LiftedRep -> LiftedRep) (Either b b') (Either c c') #

(|||) :: (LiftedRep -> LiftedRep) b d -> (LiftedRep -> LiftedRep) c d -> (LiftedRep -> LiftedRep) (Either b c) d #

# Arrow application

class Arrow a => ArrowApply a where #

Some arrows allow application of arrow inputs to other inputs. Instances should satisfy the following laws:

-     first (arr (\x -> arr (\y -> (x,y)))) >>> app = id

-     first (arr (g >>>)) >>> app = second g >>> app

-     first (arr (>>> h)) >>> app = app >>> h

Such arrows are equivalent to monads (see `ArrowMonad`).

Minimal complete definition

app

Methods

app :: a (a b c, b) c #

Instances

Monad m => ArrowApply (Kleisli m) # | Since: 2.1
Methods app :: Kleisli m (Kleisli m b c, b) c #
ArrowApply ((->) LiftedRep LiftedRep) # | Since: 2.1
Methods app :: (LiftedRep -> LiftedRep) ((LiftedRep -> LiftedRep) b c, b) c #

newtype ArrowMonad a b #

The `ArrowApply` class is equivalent to `Monad`: any monad gives rise to a `Kleisli` arrow, and any instance of `ArrowApply` defines a monad.

Constructors

ArrowMonad (a () b) |

Instances

ArrowApply a => Monad (ArrowMonad a) # | Since: 2.1
Methods (>>=) :: ArrowMonad a a -> (a -> ArrowMonad a b) -> ArrowMonad a b # (>>) :: ArrowMonad a a -> ArrowMonad a b -> ArrowMonad a b # return :: a -> ArrowMonad a a # fail :: String -> ArrowMonad a a #
Arrow a => Functor (ArrowMonad a) # | Since: 4.6.0.0
Methods fmap :: (a -> b) -> ArrowMonad a a -> ArrowMonad a b # (<$) :: a -> ArrowMonad a b -> ArrowMonad a a #
Arrow a => Applicative (ArrowMonad a) # | Since: 4.6.0.0
Methods pure :: a -> ArrowMonad a a # (<*>) :: ArrowMonad a (a -> b) -> ArrowMonad a a -> ArrowMonad a b # liftA2 :: (a -> b -> c) -> ArrowMonad a a -> ArrowMonad a b -> ArrowMonad a c # (*>) :: ArrowMonad a a -> ArrowMonad a b -> ArrowMonad a b # (<*) :: ArrowMonad a a -> ArrowMonad a b -> ArrowMonad a a #
(ArrowApply a, ArrowPlus a) => MonadPlus (ArrowMonad a) # | Since: 4.6.0.0
Methods mzero :: ArrowMonad a a # mplus :: ArrowMonad a a -> ArrowMonad a a -> ArrowMonad a a #
ArrowPlus a => Alternative (ArrowMonad a) # | Since: 4.6.0.0
Methods empty :: ArrowMonad a a # (<|>) :: ArrowMonad a a -> ArrowMonad a a -> ArrowMonad a a # some :: ArrowMonad a a -> ArrowMonad a [a] # many :: ArrowMonad a a -> ArrowMonad a [a] #

leftApp :: ArrowApply a => a b c -> a (Either b d) (Either c d) #

Any instance of `ArrowApply` can be made into an instance of `ArrowChoice` by defining `left` = `leftApp`.

# Feedback

class Arrow a => ArrowLoop a where #

The `loop` operator expresses computations in which an output value is fed back as input, although the computation occurs only once. It underlies the `rec` value recursion construct in arrow notation. `loop` should satisfy the following laws:

extension
  ``loop` (`arr` f) = `arr` (\ b -> `fst` (`fix` (\ (c,d) -> f (b,d))))`
left tightening
  ``loop` (`first` h >>> f) = h >>> `loop` f`
right tightening
  ``loop` (f >>> `first` h) = `loop` f >>> h`
sliding
  ``loop` (f >>> `arr` (`id` *** k)) = `loop` (`arr` (`id` *** k) >>> f)`
vanishing
  ``loop` (`loop` f) = `loop` (`arr` unassoc >>> f >>> `arr` assoc)`
superposing
  ``second` (`loop` f) = `loop` (`arr` assoc >>> `second` f >>> `arr` unassoc)`

where

    assoc ((a,b),c) = (a,(b,c))
    unassoc (a,(b,c)) = ((a,b),c)

Minimal complete definition

loop

Methods

loop :: a (b, d) (c, d) -> a b c #

Instances

MonadFix m => ArrowLoop (Kleisli m) # | Beware that for many monads (those for which the `>>=` operation is strict) this instance will not satisfy the right-tightening law required by the `ArrowLoop` class. Since: 2.1
Methods loop :: Kleisli m (b, d) (c, d) -> Kleisli m b c #
ArrowLoop ((->) LiftedRep LiftedRep) # | Since: 2.1
Methods loop :: (LiftedRep -> LiftedRep) (b, d) (c, d) -> (LiftedRep -> LiftedRep) b c #