PLS Lab

Applicative functors

An applicative functor is a design pattern in functional programming. It is often used when there is a functor f that we wish to program 'under.' Applicative functors are also known as sequences or idioms.

In Haskell

The type class of applicatives is given by

class Applicative f where
  pure  :: a -> f a
  (<*>) :: f (a -> b) -> f a -> f b

These primitives must satisfy the following laws.

  1. Identity: pure id <*> v = v
  2. Composition: pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
  3. Homomorphism: pure f <*> pure x = pure (f x)
  4. Interchange: u <*> pure y = pure ($ y) <*> u

Applicative functors are evidently functors, using the definition

fmap :: Applicative f => (a -> b) -> f a -> f b
fmap f u = pure f <*> u

Moreover, any Haskell monad is an applicative.

Unlike monads, two applicatives compose to an applicative.

Categorically

Categorically, an applicative functor seems to be an endofunctor F : \mathcal{C} \to \mathcal{C} which has

  1. a unit' natural transformation $\eta : 1 \Rightarrow F`$
  2. a natural transformation m : F(-) \times F(-) \Rightarrow F(-)

While \eta corresponds to pure, the <*> combinator seems to be defined by constructing the composite

  F(B^A) \times F(A)
\xrightarrow{\textsf{st}_{B^A, A}}
  F(B^A \times A)
\xrightarrow{F(\textsf{ev})}
  F(B)

and taking the \lambda-transpose of that to obtain F(B^A) \to F(B)^{F(A)}. Note that B^A is the exponential, and \textsf{ev} : B^A \times A \to B is the evaluation morphism that comes with it.

The four equations given above seem to suggest that F is at the very least a lax monoidal functor. However, because we are working internally in Haskell - i.e. everything is written as a Haskell function - implies that we also need a tensorial strength. Some more discussion of this is found in section 7 of McBride and Patterson 2008.

There is also some discussion on the nLab.

In LaTeX

When typesetting in LaTeX, the operator <*> is often rendered as \circledast (\circledast).

See also

References

McBride, Conor, and Ross Paterson. 2008. ‘Applicative Programming with Effects’. Journal of Functional Programming 18 (01). https://doi.org/10.1017/S0956796807006326. pdf
@article{mcbride_2008,
	title = {Applicative programming with effects},
	volume = {18},
	doi = {10.1017/S0956796807006326},
	number = {01},
	journal = {Journal of Functional Programming},
	author = {McBride, Conor and Paterson, Ross},
	year = {2008}
}