This is what I think is the best way of explaining Applicative Functor.

The objective of this post is to give anyone, who is not neccessarily mathematically trained, the mathematical intuition behind Applicative Functor.

It all begins with the simplest possible mathematical structure called Apply.

## What is Apply?

You may think that you are not familiar with it but trust me, you already are an expert. apply is a binary function that takes a function $f:X \rightarrow Y$ and a $x \in X$ then apply $x$ to $f$ then return it.

Basically it’s the name for the notation f(x) that you have been using all the time in programming and mathematics. You probably didn’t even bother to learn it’s name because you have been taking it for granted.

Although it looks so trivial, it has rules, namely Applicative rules.

### Rule 1 - identity

$\text{apply}(\text{id},x)$
must always yield
$x$
itself where `id`

is a function that returns the input as it is.

### Rule 2 - composition

Follwing must hold

## Note that $\text{apply}( \cdot , f)$ results in a curried form of composition $\cdot_f(g) = f \cdot g$ .

Those seemingly trivial rules keep mathematics and programming making sense.

## So what is applicative functor?

Preserving mathematical structure is a fundamental concept of mathematics. You can find it everywhere. For example, matrix preserves linearity, homomorphism preserves algebraic structure, continuous function preserves topological structure, functor preserves Categorical structure etc.

Applicative Functor preserves Applicative rules that we have just mentioned in addition to Categorical structure. In other words, Applicative functor will provide a universal construction(means it will work on any elements x of any type X) (call it
$\eta$
)

and $\text{apply}_A$ which is the same as

*apply*but for the types that are lifted by

*A*.

and any given two composable functions
$f$
and
$g$
and an element
$x$
from the function’s domain will preserve their applicative structure after they are transformed by
$\eta$
.

It’s all about preserving the structure.

## Note that $\eta$ is widely known as

pureorreturnto programmers, $\text{apply}_A$ asap.

## Applicative Functor as “Functor for binary operators”

I know many people think Applicative Functor as “Functor for binary operators”. It’s because preserving Applicative rules gives rise to preserving binary operators, and it’s very practical and ubiquitous. Let’s see how it works.

In other words, lets see how given a binary operator

and an Applicative Functor $A$

We can construct following new binary operator

And it preserves all the original operator’s structure, e.g associativity, identity, commutativity etc.

## Thinking of why Functor alone can’t do the job can gives you lots of insight through it. If you try to juggle with Functor alone to achieve the construction, you will soon realise it’s impossible. The reason is that a function as an object pops up and Functor alone is too weak to apply an object to a function as an object. Because Functor can only apply an object to a function as a morphism.

Here’s the construction.

think of + as the curried form.

then

the new object’s type is

then apply it to the curried form of applyA

the new object’s type is

You can see it became a function

Since it’s a function now, compose it with $\text{apply}_A$ .

the new function’s type is

Boom! construction is done.

## Conclusion

Applicative functor is nothing but a preservation of one of the easiest mathematical structure *apply*. There’s nothing to overthink about it.

## Top comments (0)