Connecting...

W1siziisimnvbxbpbgvkx3rozw1lx2fzc2v0cy9zawduawz5lxrly2hub2xvz3kvanbnl2jhbm5lci1kzwzhdwx0lmpwzyjdxq

Anatomy of functors and category theory by Martin Menestret

W1siziisijiwmtkvmdivmtgvmtyvmjgvmjivnza3l3blegvscy1wag90by0zntywnzkuanblzyjdlfsiccisinrodw1iiiwiotawedkwmfx1mdazzsjdxq

What is a functor? What does it look like in Scala?

Data Engineer, Martin Menestret can give you an answer! You will bridge the gap between category theory and pure Functional Programming in Scala.

 

'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 to 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.

  • Part 1: Anatomy of functional programming
  • Part 2: Anatomy of an algebra
  • Part 3: Anatomy of a type class
  • Part 4: Anatomy of semi groups and monoids
  • Part 5: Anatomy of functors and category theory
  • Part 6: Anatomy of the tagless final encoding - Yet to come!

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

  • Functors represent containers (or I like to describe them as “contexts”)
  • For now, we won’t care about their particularities, all we need to know is that, at some point, they will maybe hold a value or values “inside” (but keep in mind that every container have particularities, I’ll refer to that at the end)
  • Defining an generic interface about how to access values inside a container does not make any sense since some containers’ values would be accessed by index (arrays for example), others only by taking the first element (stacks for example), other by taking the value only if it exists (optionals), etc.
  • However, we can define an interface defining how the value(s) inside containers is modified by a function despite being in a container

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…

  • Identity:functor mapped over by the identity function is the same as the original functor (the container and its content remain unchanged)
  • Composition:functor mapped over the composition of two functions is the same as the functor mapped over the first function and then mapped over the second one

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.

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

  • Our first guinea pig is 'Option[Int]', which is a container of (0 or 1) 'Int'.

We can see that an Option[Int] turns into an Option[Float], the inner value of the container being modified from Int to Float when mapped over with a function from Int to Float

  • Our second guinea pig is List[Int], which is a container of (0 or more) Int.

We can see that an List[Int] turns into a List[Float], the inner values of the container are modified from Int to Float when mapped over with a function from Int to Float

  • Our third is a hand made UselessContainer[Int], which is a container of exactly 1 Int.
 final case class UselessContainer[A](innerValue: A)
  val intContainer: UselessContainer[Int] = UselessContainer(99)
  val mappedResult3: UselessContainer[Float] = intContainer.map(halve)

We can see that an UselessContainer[Int] turns into an UselessContainer[Float], the inner value of the container being modified from Int to Float when mapped over with a function from Int to Float… (I’ve deliberately hidden an implementation detail here for clarity, I’ll cover it later)

So we can observe that pattern we described earlier:

functor, let’s call it F[A], is a structure containing a value of type Aand 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).

Here the types of our functor type class are higher kinded types, type constructors F[_], containers, and the type class exposes a map function that takes a container of type F[A], a function A => B that will return a F[B]: the pattern we just described.

That way, for a container F[_] to be a functor, it must be an instance of the functor type class, hence, provide an instance of Functor.

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:

  • For all values uc of type UselessContainer:

  • For all values uc of type UselessContainer and for any two functions f of type A => B and g of type B => C:
  ucFunctor.map(uc, g compose f) == ucFunctor.map(ucFunctor.map(uc, f), g)

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.

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:

  • Objects (view it as something purely abstract, absolutely anything, points for example)
  • Arrows or morphisms (which are the ways to go from one object to another)
  • And two fundamental properties:

Composition: A way to compose these arrows associatively. It means that if it exists an arrow from an object a to an object band an arrow from the object b to an object c, it exists an arrow that goes from a to c and the order of composition does not matter (given 3 morphisms that are composable fgh then (g) . f) == h . (g . f))

Identity: There is an identity arrow for every object in the category which is the arrow which goes from that object to itself

 

  • A, B, C are this category’s objects
  • and are its arrows or morphisms
  • g . f is f and g composition since f goes from A to B and g goes from B to C (and it MUST exist to satisfy composition law, since f and g exist)
  • 1A, 1B and 1C are the identity arrows of A, B and C

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 !):

  • S objects are Scala’s types
  • S morphisms are Scala’s functions

Composition between morphisms is then function composition

Identity morphisms for S objects is the identity function

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):

  • Then it must exist a morphism from a to c which is the composition of the two. And it does ! It is (pseudo code):

For g: B => C and f: A => B, g compose f

  • And that composition is associative:

(h compose g) compose f is the same as h compose (g compose f)

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

  • def id[A](a: A) = a

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 functional programming in Scala, re-think about it.

functor F being a structure-preserving mapping between two categories means that it maps objects from category A to objects of the 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

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

Morphisms

Let’s use our previously defined:

  • def map[A, B](fa: F[A], func: A => B): F[B].

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:

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?

Ok, so, to sum up:

  • Functors are mappings between two categories
  • functor, due to its theorical nature, preserves the morphisms and their relations between the two categories it maps
  • When programming in pure FP, we are in S, the category of Scala types, functions and under function composition and the functors we use are then endofunctors (from S to S) because they map Scala types and functions between them to other Scala types and functions between them

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 into Option[A] and Option[B] types while allowing us to re-use functions of type A => B thanks to Options’ map, 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!

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:

  • That a functor is a kind of container that can be mapped over by a function and the laws it has to respect
  • Some examples and identified a common pattern
  • How we abstract over and encode that pattern in Scala as a type class of type constructors
  • We had a modest overview about category theory, what functors are in category theory, and how both relates to pure FP in Scala

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!

 

This article was written by Martin Menestret and posted originally on geekocephale.com