## DEV Community Ingun 전인건

Posted on • Updated on

# Mathematical intuition behind Applicative Functor

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.

\begin{aligned} &\text{apply}:Y^X \times X \rightarrow Y \newline \\ &\text{apply}(f,x) = f(x) \end{aligned}

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

$\text{apply}(f, \text{apply}(g,x)) = \text{apply}( \text{apply}(\text{apply}( \cdot , f), g),x)$ ###### 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$ )

$\eta:X \rightarrow AX$

and $\text{apply}_A$ which is the same as apply but for the types that are lifted by A.
$\text{apply}_A : A(X \rightarrow Y) \rightarrow AZ$

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$ .

$\text{apply}_A ( A\text{id}, A x) = A x$ $\text{apply}_A(Af, \text{apply}_A(Ag,Ax)) = \text{apply}_A( \text{apply}_A(\text{apply}_A( A, Af), Ag),Ax)$ It’s all about preserving the structure.

## 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

$+:X\times Y \rightarrow Z$

and an Applicative Functor $A$
We can construct following new binary operator
$+_A:AX \times AY \rightarrow AZ$

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.

$: X \rightarrow (Y \rightarrow Z)$

then

$+’ = \eta(+)$

the new object’s type is
$A(X \rightarrow (Y \rightarrow Z))$

then apply it to the curried form of applyA
$+’’ = \text{apply}_A (+’)$

the new object’s type is
$AX \rightarrow A(Y \rightarrow Z)$

You can see it became a function
Since it’s a function now, compose it with $\text{apply}_A$ .
$+_A = \text{apply}_A \cdot +’’$

the new function’s type is

$+_A : AX \rightarrow AY \rightarrow AZ$

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.