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.
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.
pure id <*> v = v
pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
pure f <*> pure x = pure (f x)
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, an applicative functor seems to be an endofunctor which has
While corresponds to pure
, the <*>
combinator seems to be defined by constructing the composite
and taking the -transpose of that to obtain . Note that is the exponential, and is the evaluation morphism that comes with it.
The four equations given above seem to suggest that 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.
When typesetting in LaTeX, the operator <*>
is often rendered as (\circledast
).
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}
}
Applicative functors on the Haskell wikibook.
Applicative functors on Wikipedia.
Applicative functors on the Haskell wiki.