Haskell/The Functor class

In this chapter, we will introduce the important  type class.

Motivation
In Other data structures, we saw operations that apply to all elements of some grouped value. The prime example is  which works on lists. Another example we worked through was the following Tree datatype:

The map function we wrote for Tree was:

As discussed before, we can conceivably define a map-style function for any arbitrary data structure.

When we first introduced  in Lists II, we went through the process of taking a very specific function for list elements and generalizing to show how   combines any appropriate function with all sorts of lists. Now, we will generalize still further. Instead of map-for-lists and map-for-trees and other distinct maps, how about a general concept of maps for all sorts of mappable types?

Introducing Functor
is a Prelude class for types which can be mapped over. It has a single method, called. The class is defined as follows:

The usage of the type variable  can look a little strange at first. Here,  is a parametrized data type; in the signature of ,   takes   as a type parameter in one of its appearances and   in the other. Let's consider an instance of : By replacing   with   we get the following signature for  ...

... which fits the natural definition:

(Incidentally, this definition is in Prelude; so we didn't really need to implement  for that example in the "Other data structures" chapter.)

The  instance for lists (also in Prelude) is simple:

... and if we replace  with   in the   signature, we get the familiar type of.

So,  is a generalization of   for any parametrized data type.

Naturally, we can provide  instances for our own data types. In particular,  can be promptly relocated to an instance:

Here's a quick demo of  in action with the instances above (to reproduce it, you only need to load the   and   declarations for  ; the others are already in Prelude):

The functor laws
When providing a new instance of, you should ensure it satisfies the two functor laws. There is nothing mysterious about these laws; their role is to guarantee  behaves sanely and actually performs a mapping operation (as opposed to some other nonsense). The first law is:

is the identity function, which returns its argument unaltered. The first law states that mapping  over a functorial value must return the functorial value unchanged.

Next, the second law:

It states that it should not matter whether we map a composed function or first map one function and then the other (assuming the application order remains the same in both cases).

What did we gain?
At this point, we can ask what benefit we get from the extra layer of generalization brought by the  class. There are two significant advantages:


 * The availability of the  method relieves us from having to recall, read, and write a plethora of differently named mapping methods (,  ,  , ad infinitum). As a consequence, code becomes both cleaner and easier to understand. On spotting a use of  , we instantly have a general idea of what is going on. Thanks to the guarantees given by the functor laws, this general idea is surprisingly precise.


 * Using the type class system, we can write -based algorithms which work out of the box with any functor - be it ,  ,   or whichever you need. Indeed, a number of useful classes in the core libraries inherit from.

Type classes make it possible to create general solutions to whole categories of problems. Depending on what you use Haskell for, you may not need to define new classes often, but you will certainly be using type classes all the time. Many of the most powerful features and sophisticated capabilities of Haskell rely on type classes (residing either in the standard libraries or elsewhere). From this point on, classes will be a prominent presence in our studies.