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.
- 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
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
- a
unit' natural transformation $\eta : 1 \Rightarrow F`$ - a natural transformation
m : F(-) \times F(-) \Rightarrow F(-)
While 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
The four equations given above seem to suggest that
There is also some discussion on the nLab.
In LaTeX
When typesetting in LaTeX, the operator <*> is often rendered as \circledast).
See also
References
- The abstraction of applicative functors was introduced by McBride and Patterson in
@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.