Control.Applicative (original) (raw)

Contents

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[>>=](Control-Monad.html#v:-62--62--61-), but

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, seeApplicative Programming with Effects, by Conor McBride and Ross Paterson.

Synopsis

Applicative functors

class Functor f => Applicative f where Source

A functor with application, providing operations to

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

identity

[pure](Control-Applicative.html#v:pure) [id](Control-Category.html#v:id) [<*>](Control-Applicative.html#v:-60--42--62-) v = v

composition

[pure](Control-Applicative.html#v:pure) (.) [<*>](Control-Applicative.html#v:-60--42--62-) u [<*>](Control-Applicative.html#v:-60--42--62-) v [<*>](Control-Applicative.html#v:-60--42--62-) w = u [<*>](Control-Applicative.html#v:-60--42--62-) (v [<*>](Control-Applicative.html#v:-60--42--62-) w)

homomorphism

[pure](Control-Applicative.html#v:pure) f [<*>](Control-Applicative.html#v:-60--42--62-) [pure](Control-Applicative.html#v:pure) x = [pure](Control-Applicative.html#v:pure) (f x)

interchange

u [<*>](Control-Applicative.html#v:-60--42--62-) [pure](Control-Applicative.html#v:pure) y = [pure](Control-Applicative.html#v:pure) ([$](Prelude.html#v:-36-) y) [<*>](Control-Applicative.html#v:-60--42--62-) u

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

As a consequence of these laws, the [Functor](Control-Monad.html#t:Functor) instance for f will satisfy

If f is also a [Monad](Control-Monad.html#t:Monad), it should satisfy

(which implies that [pure](Control-Applicative.html#v:pure) and [<*>](Control-Applicative.html#v:-60--42--62-) satisfy the applicative functor laws).

Methods

pure :: a -> f a Source

Lift a value.

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

Sequential application.

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

Sequence actions, discarding the value of the first argument.

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

Sequence actions, discarding the value of the second argument.

Alternatives

Instances

Utility functions

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

Replace all locations in the input with the same value. The default definition is `[fmap](Control-Monad.html#v:fmap)` . `[const](Prelude.html#v:const)`, but this may be overridden with a more efficient version.

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

Lift a ternary function to actions.