Geekcephale

/**
  * Scala developer @Ebiznext,
  * SNUG co-organizer.
  * Exalted about FP and maintaining
  * A list of great ressources !
  */

val me = SoftwareDeveloper(
  firstname = "Martin",
  name      = "Menestret",
  email     = "here",
  twitter   = "@mmenestret"
)


» Blog posts

» Talks

» FP learning resources

16 February 2019

Anatomy of functors and category theory

by Myself

I will try to group here, in an anatomy atlas, basic notions of functional programming that I find myself explaining often lately into a series of articles.

The idea here is to have a place to point people needing explanations and to increase my own understanding of these subjects by trying to explain them the best I can. I’ll try to focus more on making the reader feel an intuition, a feeling about the concepts rather than on the perfect, strict correctness of my explanations.

Introduction

In this article, I’ll try to give you an intuition about what is a functor and what do they look like in Scala. Then the ones being curious about theory can keep on reading because we’ll take a quick glance at category theory and what are functors in category theory terms. Then we’ll try to bridge the gap between category theory and pure FP in Scala and finally take a look back at our functors !

What is a functor ?

There’s a nice answer by Bartosz Milewski on Quora from which I’ll keep some parts:

I like to think of a functor as a generalization of a container. A regular container contains zero or more values of some type. A functor may or may not contain a value or values of some type (…) .

So what can you do with such a container? You might think that, at the minimum, you should be able to retrieve values. But each container has its own interface for accessing values. If you try to specify that interface, you’re Balkanizing containers. You’re splitting them into stacks, queues, smart pointers, futures, etc. So value retrieval is too specific.

It turns out that the most general way of interacting with a container is by modifying its contents using a function.

Let’s try to rephrase that

So, to summarize, a functor is a kind of container that can be mapped over by a function.

But functors have to respect some rules, called functor’s laws…

A quick note about functor / container parallel: the analogy is convenient to get the intuition, but not all functors will not fit into that model, keep it in a corner of you mind so that you’re not taken off guard.

How does it look like in practice

Along the next sections, the examples and code snippets I’ll provide will be in Scala.

Let explore some examples

We’re going to play with concrete containers of Int values to try to grasp the concept.

val halve: Int => Float = x => x.toFloat / 2

Here we defined the function from Int to Float that we are going to use to map over our containers

So we can observe that pattern we described earlier:

A functor, let’s call it F[A], is a structure containing a value of type A and which can be mapped over by a function of type A => B, getting back a functor F[B] containing a value of type B.

How do we abstract and encode that ability ?

Functors are usually represented by a type class.

As a reminder, a type class is a group of types that all provide the same abilities (interface), which make them part of the same class (group, “club”) of same abilities providing types (see my article about type classes here).

This is the functor type class implementation:

trait Functor[F[_]]{
    def map[A, B](fa: F[A], func: A => B): F[B]
}
  1. The types our functor type class abstract over are type constructors (F[_], our container types)
  2. The type class exposes a map function taking a container F[A] of values of type A, a function of type A => B and return a F[B], a container of values of type B: the pattern we just described.

A note about type constructors: A type constructor is a type to which you have to supply an other type to get back a new type. You can think of it just as functions that take values to produce values. And that makes sense, since we have to supply to our container type the type of values it will “hold” !

Most used concrete type constructors are List[_], Option[_], Either[_,_], Map[_, _] and so on.

To illustrate what it means in your Scala code let’s make our UselessContainer a functor:

implicit val ucFunctor = new Functor[UselessContainer] {
    override def map[A, B](fa: UselessContainer[A],
                           func: A => B): UselessContainer[B] =
      UselessContainer(func(fa.innerValue))
}

Be careful, if you attempt to create your own functor, it is not enough. You have to prove that your functor instance respects the functor’s laws we stated earlier (usually via property based tests), hence that:

However, you can safely use functor instances brought to you by Cats or Scalaz because their implementations lawfulness are tested for you.

(You can find the Cats functor laws here and their tests here. They are tested with discipline.)

Now that you know what a functor is and how it’s implemented in Scala, let’s talk a bit about category theory !

An insight about the theory behind functors

During this article, we only talked about the most widely known kind of functors, the co-variant endofunctors. Don’t mind the complicated name, they are all you need to know to begin having fun in functional programming.

However if you’d like to have a grasp a little bit of theory behind functors, keep on reading.

Functors are structure-preserving mappings between categories.

Tiny crash course into category theory

Category theory is the mathematical field that study how things relate to each others in general and how their relations compose.

A category is composed of:

category

Back to Scala

In the context of purely functional programming in Scala, we can consider that we work in a particular category that we are going to call it S (I won’t go into theoretical compromises implied by that parallel, but there are some !):

Indeed, if we consider the object a (the type A) and the object b (the type B), Scala functions A => B are morphisms between a and b.

Given our morphism from a to b, if it exists an object c (the type C) and a morphism between b and c exists (a function B => C):

Moreover for every object (every type) it exists an identity morphism, the identity function, which is the type parametric function:

We can now grasp how category theory and purely functional programming can relate !

And then back to our functors

Now that you know what a category is, and that you know about the category S we work in when doing functional programming in Scala, re-think about it.

A functor F being a structure-preserving mapping between two categories means that it maps objects from category A to objects from category F(A) (the category which A is mapped to by the functor F) and morphisms from A to morphisms of F(A) while preserving their relations.

Since we always work with types and with functions between types in Scala, a functor in that context is a mapping from and to the same category, between S and S, and that particular kind of functor is called an endofunctor.

Let’s explore how Option behaves (but we could have replaced Option by any functor F):

Objects

Objects in S (types) Objects in F(S)
A Option[A]
Int Option[Int]
String Option[String]

So Option type construtor maps objects (types) in S to other objects (types) in S.

Morphisms

Let’s use our previously defined:

If we partially apply map with a function f of type A => B like so (pseudo-code): map(_, f), then we are left with a new function of type F[A] => F[B].

Using map that way, let’s see how morphisms behave:

Morphisms in S (function between types) Morphisms in F(S)
A => A (identity) Option[A] => Option[A]
A => B Option[A] => Option[B]
Int => Float Option[Int] => Option[Float]
String => String Option[String] => Option[String]

So Option’s map maps morphisms (functions from type to type) in S to other morphisms (functions from type to type) in S.

We won’t go into details but we could have shown how Option functor respects morphism composition and identity laws.

What does it buy us ?

In programming terms, (endo)functors in Scala allow us to move from origin types (A, B, …), to new target types (F[A], F[B], …) while safely allowing us to re-use the origin functions and their compositions on the target types.

To continue with our Option example, Option type constructor “map” our types A and B into Option[A] and Option[B] types while allowing us to re-use functions of type A => B thanks to Optionsmap, turning them into Option[A] => Option[B] and preserving their compositions.

But that is not over ! Let’s leave abstraction world we all love so much and head back to concrete world.

Concrete functors instances enhance our origin types with new capacities. Indeed, functor instances are concrete data structures with particularities (the one we said we did not care about at the beginning of that article), the abilty to represent empty value for Option, the ability to suspend an effectful computation for IO, the ability to hold multiple values for List and so on !

Ok, so, to sum up, why functors are awesome ? The two main reasons I can think of are:

  1. Abstraction, abstraction, abstraction… Code using functors allows you to only care about the fact that what you manipulate is mappable.
    • It increases code reuse since a piece of code using functors can be called with any concrete functor instance
    • And it reduces a lot error risks since you have to deal with less particularities of the concrete, final, data structure your functions will be called with
  2. They add functionnalities to existing types, while allowing to still use functions on them (you would not want to re-write them for every functor instances), and that’s a big deal:
    • Option allow you to bring null into a concrete value, making code a lot healthier (and functions purer)
    • Either allow you to bring computation errors into concrete values, making dealing with computation errors a lot healthier (and functions purer)
    • IO allow you to turn a computations into a values, allowing better compositionality and referential transparency
    • And so on…

I hope I made a bit clearer what functors are in the context of category theory and how that translates to pure FP in Scala !

More material

If you want to keep diving deeper, some interesting stuff can be found on my FP resources list and in particular:

Conclusion

To sum up, we saw:

I’ll try to keep that blog post updated. If there are any additions, imprecision or mistakes that I should correct or if you need more explanations, feel free to contact me on Twitter or by mail !


Edit: Thanks Jules Ivanic for the review :).

tags: Scala - Functional programming