Control.Applicative

base-4.8.2.0: Basic libraries

Copyright | Conor McBride and Ross Paterson 2005
License | BSD-style (see the LICENSE file in the distribution)
Maintainer | libraries@haskell.org
Stability | experimental
Portability | portable
Safe Haskell | Trustworthy
Language | Haskell2010

Control.Applicative

Contents

- Applicative functors
- Alternatives
- Instances
- Utility functions

Description

This module describes a structure intermediate between a functor and a monad (technically, a strong lax monoidal functor). Compared with monads, this interface lacks the full power of the binding operation `>>=`, but

- it has more instances.
- it is sufficient for many uses, e.g. context-free parsing, or the `Traversable` class.
- instances can perform analysis of computations before they are executed, and thus produce shared optimizations.

This interface was introduced for parsers by Niklas Röjemo, because it admits more sharing than the monadic interface. The names here are mostly based on parsing work by Doaitse Swierstra.

For more details, see Applicative Programming with Effects, by Conor McBride and Ross Paterson.

Synopsis

- class Functor f => Applicative f where
  - pure :: a -> f a
  - (<*>) :: f (a -> b) -> f a -> f b
  - (*>) :: f a -> f b -> f b
  - (<*) :: f a -> f b -> f a
- class Applicative f => Alternative f where
  - empty :: f a
  - (<|>) :: f a -> f a -> f a
  - some :: f a -> f [a]
  - many :: f a -> f [a]
- newtype Const a b = Const {
  - getConst :: a
  }
- newtype WrappedMonad m a = WrapMonad {
  - unwrapMonad :: m a
  }
- newtype WrappedArrow a b c = WrapArrow {
  - unwrapArrow :: a b c
  }
- newtype ZipList a = ZipList {
  - getZipList :: [a]
  }
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- (<$) :: Functor f => a -> f b -> f a
- (<**>) :: Applicative f => f a -> f (a -> b) -> f b
- liftA :: Applicative f => (a -> b) -> f a -> f b
- liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
- liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
- optional :: Alternative f => f a -> f (Maybe a)

# Applicative functors

class Functor f => Applicative f where

A functor with application, providing operations to

- embed pure expressions (`pure`), and
- sequence computations and combine their results (`<*>`).

A minimal complete definition must include implementations of these functions satisfying the following laws:

identity

      pure id <*> v = v

composition

      pure (.) <*> u <*> v <*> w = u <*> (v <*> w)

homomorphism

      pure f <*> pure x = pure (f x)

interchange

      u <*> pure y = pure ($ y) <*> u

The other methods have the following default definitions, which may be overridden with equivalent specialized implementations:

-     u *> v = pure (const id) <*> u <*> v

-     u <* v = pure const <*> u <*> v

As a consequence of these laws, the `Functor` instance for `f` will satisfy

-     fmap f x = pure f <*> x

If `f` is also a `Monad`, it should satisfy

-     pure = return

-     (<*>) = ap

(which implies that `pure` and `<*>` satisfy the applicative functor laws).

Minimal complete definition

pure, (<*>)

Methods

pure :: a -> f a

Lift a value.

(<*>) :: f (a -> b) -> f a -> f b infixl 4

Sequential application.

(*>) :: f a -> f b -> f b infixl 4

Sequence actions, discarding the value of the first argument.

(<*) :: f a -> f b -> f a infixl 4

Sequence actions, discarding the value of the second argument.

Instances

Applicative [] |
Applicative IO |
Applicative Maybe |
Applicative ReadP |
Applicative ReadPrec |
Applicative Last |
Applicative First |
Applicative STM |
Applicative ZipList |
Applicative Identity |
Applicative ((->) a) |
Applicative (Either e) |
Monoid a => Applicative ((,) a) |
Applicative (ST s) |
Applicative (Proxy *) |
Arrow a => Applicative (ArrowMonad a) |
Monad m => Applicative (WrappedMonad m) |
Monoid m => Applicative (Const m) |
Applicative (ST s) |
Applicative f => Applicative (Alt * f) |
Arrow a => Applicative (WrappedArrow a b) |

# Alternatives

class Applicative f => Alternative f where

A monoid on applicative functors.

If defined, `some` and `many` should be the least solutions of the equations:

-     some v = (:) <$> v <*> many v

-     many v = some v <|> pure []

Minimal complete definition

empty, (<|>)

Methods

empty :: f a

The identity of `<|>`

(<|>) :: f a -> f a -> f a infixl 3

An associative binary operation

some :: f a -> f [a]

One or more.

many :: f a -> f [a]

Zero or more.

Instances

Alternative [] |
Alternative Maybe |
Alternative ReadP |
Alternative ReadPrec |
Alternative STM |
ArrowPlus a => Alternative (ArrowMonad a) |
MonadPlus m => Alternative (WrappedMonad m) |
Alternative f => Alternative (Alt * f) |
(ArrowZero a, ArrowPlus a) => Alternative (WrappedArrow a b) |

# Instances

newtype Const a b

Constructors

Const |
Fields getConst :: a

Instances

Bifunctor Const |
Functor (Const m) |
Monoid m => Applicative (Const m) |
Foldable (Const m) |
Traversable (Const m) |
Generic1 (Const a) |
Eq a => Eq (Const a b) |
Ord a => Ord (Const a b) |
Read a => Read (Const a b) |
Show a => Show (Const a b) |
Generic (Const a b) |
Monoid a => Monoid (Const a b) |
type Rep1 (Const a) |
type Rep (Const a b) |

newtype WrappedMonad m a

Constructors

WrapMonad |
Fields unwrapMonad :: m a

Instances

Monad m => Monad (WrappedMonad m) |
Monad m => Functor (WrappedMonad m) |
Monad m => Applicative (WrappedMonad m) |
Generic1 (WrappedMonad m) |
MonadPlus m => Alternative (WrappedMonad m) |
Generic (WrappedMonad m a) |
type Rep1 (WrappedMonad m) |
type Rep (WrappedMonad m a) |

newtype WrappedArrow a b c

Constructors

WrapArrow |
Fields unwrapArrow :: a b c

Instances

Arrow a => Functor (WrappedArrow a b) |
Arrow a => Applicative (WrappedArrow a b) |
Generic1 (WrappedArrow a b) |
(ArrowZero a, ArrowPlus a) => Alternative (WrappedArrow a b) |
Generic (WrappedArrow a b c) |
type Rep1 (WrappedArrow a b) |
type Rep (WrappedArrow a b c) |

newtype ZipList a

Lists, but with an `Applicative` functor based on zipping, so that

    f <$> ZipList xs1 <*> ... <*> ZipList xsn = ZipList (zipWithn f xs1 ... xsn)

Constructors

ZipList |
Fields getZipList :: [a]

Instances

Functor ZipList |
Applicative ZipList |
Generic1 ZipList |
Eq a => Eq (ZipList a) |
Ord a => Ord (ZipList a) |
Read a => Read (ZipList a) |
Show a => Show (ZipList a) |
Generic (ZipList a) |
type Rep1 ZipList |
type Rep (ZipList a) |

# Utility functions

(<$>) :: Functor f => (a -> b) -> f a -> f b infixl 4

An infix synonym for `fmap`.

#### Examples

Convert from a ``Maybe` `Int`` to a ``Maybe` `String`` using `show`:

    >>> show <$> Nothing
    Nothing
    >>> show <$> Just 3
    Just "3"

Convert from an ``Either` `Int` `Int`` to an ``Either` `Int`` `String` using `show`:

    >>> show <$> Left 17
    Left 17
    >>> show <$> Right 17
    Right "17"

Double each element of a list:

    >>> (*2) <$> [1,2,3]
    [2,4,6]

Apply `even` to the second element of a pair:

    >>> even <$> (2,2)
    (2,True)

(<$) :: Functor f => a -> f b -> f a

Replace all locations in the input with the same value. The default definition is ``fmap` . `const``, but this may be overridden with a more efficient version.

(<**>) :: Applicative f => f a -> f (a -> b) -> f b infixl 4

A variant of `<*>` with the arguments reversed.

liftA :: Applicative f => (a -> b) -> f a -> f b

Lift a function to actions. This function may be used as a value for `fmap` in a `Functor` instance.

liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c

Lift a binary function to actions.

liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d

Lift a ternary function to actions.

optional :: Alternative f => f a -> f (Maybe a)

One or none.