Mathematical intuition behind Applicative Functor
Tags: applicative, functional, mathematics, category
Published: 2020-11-14 07:18:00

---
title: Mathematical intuition behind Applicative Functor
published: true
description: 
tags: applicative, functional, mathematics, category
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/ei7qnclj9k3zydn2c333.jpeg
---

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{% katex inline %}
f:X \rightarrow Y
{% endkatex %}and a{% katex inline %}
x \in X
{% endkatex %}then apply{% katex inline %}
x
{% endkatex %}to{% katex inline %}
f
{% endkatex %}then return it.

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

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

{% katex inline %}
\text{apply}(\text{id},x)
{% endkatex %} must always yield {%katex inline%}
x 
{%endkatex%}itself where `id` is a function that returns the input as it is.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tiddq2u9eq8vhlb043jk.jpeg)

### Rule 2 - composition

Follwing must hold

{%katex%}
\text{apply}(f, \text{apply}(g,x)) = \text{apply}( \text{apply}(\text{apply}( \cdot , f), g),x)
{%endkatex%}

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/osp5ajd0spx4re7c684n.jpeg)

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

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 {%katex inline%}\eta{%endkatex%})
{%katex%}
\eta:X \rightarrow AX
{%endkatex%}
and {%katex inline%}
\text{apply}_A
{%endkatex%}which is the same as *apply* but for the types that are lifted by *A*.
{%katex%}
\text{apply}_A : A(X \rightarrow Y) \rightarrow AZ
{%endkatex%}

and any given two composable functions {%katex inline%}
f
{%endkatex%} and {%katex inline%}
g
{%endkatex%} and an element {%katex inline%}
x
{%endkatex%} from the function’s domain will preserve their applicative structure after they are transformed by {% katex inline %}
\eta
{%endkatex%}.
{%katex%}
\text{apply}_A ( A\text{id}, A x) = A x
{%endkatex%}

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/m102eudpb8q45ntiwihv.jpeg)

{%katex%}
\text{apply}_A(Af, \text{apply}_A(Ag,Ax)) = \text{apply}_A( \text{apply}_A(\text{apply}_A( A, Af), Ag),Ax)
{%endkatex%}

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ei7qnclj9k3zydn2c333.jpeg)

It’s all about preserving the structure.

> ###### Note that {%katex inline%}\eta{%endkatex%} is widely known as *pure* or *return* to programmers, {%katex inline%} \text{apply}_A{%endkatex%} as *ap*.

## 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 
{%katex%}+:X\times Y \rightarrow Z{%endkatex%}
and an Applicative Functor {%katex inline%}A{%endkatex%}
We can construct following new binary operator

{%katex%}
+_A:AX \times AY \rightarrow AZ
{%endkatex%}

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.

{%katex%}
+ : X \rightarrow (Y \rightarrow Z)
{%endkatex%}

then
{%katex%}
+’ = \eta(+)
{%endkatex%}
the new object’s type is
{%katex%}
A(X \rightarrow (Y \rightarrow Z))
{%endkatex%}
then apply it to the curried form of applyA
{%katex%}
+’’ = \text{apply}_A (+’)
{%endkatex%}
the new object’s type is
{%katex%}
AX \rightarrow A(Y \rightarrow Z)
{%endkatex%}
You can see it became a function
Since it’s a function now, compose it with {%katex inline%}\text{apply}_A{%endkatex%}.
{%katex%}
+_A = \text{apply}_A \cdot +’’
{%endkatex%}

the new function’s type is
{%katex%}
+_A : AX \rightarrow AY \rightarrow AZ
{%endkatex%}
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.