We use cookies and other tracking technologies to improve your browsing experience on our site, analyze site traffic, and understand where our audience is coming from. To find out more, please read our privacy policy.

By choosing 'I Accept', you consent to our use of cookies and other tracking technologies.

We use cookies and other tracking technologies to improve your browsing experience on our site, analyze site traffic, and understand where our audience is coming from. To find out more, please read our privacy policy.

By choosing 'I Accept', you consent to our use of cookies and other tracking technologies. Less

We use cookies and other tracking technologies... More

# Login or registerto boost this post!

Show some love to the author of this blog by giving their post some rocket fuel đźš€.

# Login to see the application

Engineers who find a new job through Functional Works average a 15% increase in salary đźš€

# Functors and Category Theory Introductory

Alex Bakic 10 April, 2018 | 6 min read

## Functors and category theory

I think the best way to start learning functors is to look at where they came from. The founders of category theory , Samuel Eilenberg and Saunders Mac Lane (1945) introduced categories as preparation for what they called â€śfunctorsâ€ť and â€śnatural transformationsâ€ť.

Ah ha !

Then thatâ€™s where weâ€™ll start.

This is another one of those topics where you could read a whole book on the subject , but Iâ€™m only going to teach you category theory relevant to understanding functors. One resource that I highly recommend that explains category theory is the Stanford Encyclopedia page on it. Iâ€™ll be making references to them throughout.

So , what is category theory ?

To put it bluntly , it is the study of categories. A category is a structure comprised of objects and their morphisms.

Note:

## Objects

â†’ are the elements in a category

## Morphisms

â†’ are the functions applied on an element that return a new one. Morphisms connect objects to each other and form the category structure.

This is a basic representation of what a category looks like :

##### reference: Wikipedia

It was noted in the early days that â€śobjects play a secondary role and could be entirely omitted from definitionâ€ť. But over time it became accepted to refer to categories using sets of objects, in this case the category C would be defined as

``C =  {X ,  Y,  Z} ``

which in my opinion is much easier to understand.

Given our category C , to be a category , it must satisfy the following conditions:

Morphisms:
For every pair X, Y of objects, there is a set {X, Y}, called the morphisms from X to Y in C. If f is a morphism from X to Y, we write f: X â†’ Y.
This means that there is a function that takes an object X and returns a new object Y. This pairing happens with all objects in the category.

In our category there are the morphisms :

f : X â†’ Y , allowing the pairing {X , Y}
g : Y â†’ Z , allowing the pairing {Y , Z}
g â—‹ f : X â†’ Z , allowing the pairing {X , Z}

Identityâ€‰:
For every object X, there exists a morphism idX in {X,X}, called the identity on X. This means there is a function that returns the object it was given . The point of this morphism is to preserve identity. If the function returns something other than the object given, that means the category itself has been altered and hence identity lost.

Therefore we need to add the following identity morphisms into C so we can withhold identity.

idX: X â†’ X , allowing the pairing {X, X}
idY: Y â†’ Y , allowing the pairing {Y, Y}
idZ : Z â†’ Z , allowing the pairing {Z, Z}

Composition :
For every triple X, Y and Z of objects, there exists a composition of morphisms in C. If fâ€‰: X â†’ Y and gâ€‰: Y â†’ Z, the composition of f and g is notated (g â—‹ fâ€‰)â€‰: X â†’ Z . This rule must hold as otherwise the category wouldn't be an interconnected structure.

Alright , whatâ€™s this got to with functors I hear you ask?

Weâ€™re almost there , in this next section weâ€™re going to learn what makes a functor a functor . Iâ€™m going to provide you another extract from Eilenberg and Mac Laneâ€™s work in which they talk about the role of a functor in category theory:

The idea of a category is required only by the precept that every function should have a definite class as domain and a definite class as range, for the categories are provided as the domains and ranges of functors. Thus one could drop the category concept altogether and adopt an even more intuitive standpoint, in which a functor such as â€śHomâ€ť is not defined over the category of â€śallâ€ť groups, but for each particular pair of groups which may be given.

That was probably a lot to take in. But donâ€™t despair !

This kind of links back to what was said about objects being a secondary role. The extract states we could drop objects and categories all together. Instead we define a functor called â€śHomâ€ť and that there is a set of inputs (domain) , lets call X , and a set of outputs (range) , lets call Y, for which they create the mapping {Xi , Yi} for a particular grouping of domain and range.

And there in lies the definition.

A functor is a mapping between categories.

In this case the different categories are omitted as domain and range , but hopefully you get the picture.

OK , I think itâ€™s time for a recap on all this new knowledge.

Categories are structures that are made from objects and morphisms that connect them. Morphisms are functions which takes one object and return a new one . The interconnectivity builds a category which can be used to model real world structures. Functors have the ability to map one category to another , without altering the morphisms between the category itself to do so. Functors satisfy the condition that each object must have itâ€™s own identity.

Quick Q & A time â†’

### Q:

Is a functor a function ?

### A:

Yes , one that takes a category and maps it to another.

### Q:

A functor is the mapping between structures , is the functor itself mappable ?

### A:

This is where all the confusion and arguments start . What I think will help us here is this section on the composition of functors (pg.20). Basically, a functor does allow another functor as an argument, making it a higher order function but it does not make it mappable. Think of it this way, if we assume that functors are mappable we could associate things like a vector as being a functor, because a vector is mappable. But that would be ridiculous. A functor is the mapping between categories , and I donâ€™t know about you but I havenâ€™t seen any vectors that can map categories.

If we look at the word "functor" it is similar to "operator" and an operator will perform operations on data. This is what a functor does. It takes operands (categories) and links them. To say the functor is mappable would assume the functor is the operand, which isn't the case. A succinct explanation can be found here.

Example :

Letâ€™s define a category C as a linked list

``(2 4 8 16 32) ``

If this were a category we could say the morphism is to double the given element and return the new element.

Now another category D as a linked list

``(4 16 64 256 1024) ``

If this were a category we could say the morphism is to multiply the given element by 4 and return the new element.

Now, what would our functor need to do to map the following categories?

We could write

``(map #(* % %)  C)``

which would be suitable and allow us to map each object in C to each object in D.

But this isnâ€™t perfect . The clojure definition of map returns a list , what if we had used vectors?

So

``C = [ 2 4 8 16 32] ``

and

``D = [4 16 64 256 1024]``

Then our mapping of C would be different to the category D , as we would have had

`` (4 16 64 256 1024)``

which would have been wrong in this case.

The â€śfmapâ€ť functor for mapping would return the type it is given. Then it would be suitable as we always map to D and not some other category of a different type.

But there is another layer to this, look at this example :

What if I wanted to map these two categories?

``````C = (2 4 8 16 32)
D = [4 16 64 256 1024]``````

We canâ€™t use fmap , do you know what we would have to write ?

All we need to do is utilise the vec functor to map categories like C , to categories like D.

``(vec (map #(* % %)  C))``

And that my friends about wraps this article up ! I hope you have enjoyed , here some other pieces on functors and category theory that you may find useful.

References :

Alex Bakic
FP Enthusiast

## Related Issues

cosmos / gaia
• Started
• 0
• 7
• Intermediate
• Go
cosmos / gaia
• Started
• 0
• 3
• Intermediate
• Go
cosmos / ibc
• Started
• 0
• 2
• Intermediate
• TeX
cosmos / ibc
• Open
• 0
• 0
• Intermediate
• TeX

### Get hired!

#### Sign up now and apply for roles at companies that interest you.

Engineers who find a new job through Functional Works average a 15% increase in salary.