Types And Typeclasses

:: Learning

By: Onorio Catenacci

In some ways typeclasses and types are the Haskell implementation of the idea of “It’s turtles all the way down!” I post this on my blog in the sure and certain knowledge that if I’ve misunderstood something someone will quickly comment and set me straight!

Learning About Haskell

Since it’s relatively easy for those curious on the subject to find reference I’m not going to bother to give a long spiel about what Haskell is. I’ve been taking some time to really dig in to Haskell more deeply lately. While my heart is with Elixir and has been for quite some time, giving up strong typing has always felt like more than a little sacrifice. Strong typing is an area in which Haskell excels. Haskell introduces a rather interesting concept called the “Type Class” and it’s that that I’m going to discuss here. However we need some preliminaries in order to make the discussion of type classes make some sense.

What Is A Type?

It’s easiest for me to define type in mathematical terms. Just bear with me; I hope this will make some sense.

When mankind started developing math one of the first ideas that he/she developed was the idea of a natural number. Natural number arise (as one may guess) by simply looking at nature. If I look at my fingers, I’m lucky enough to have ten of them. I start at one and count to ten. I don’t need any additional context—mother nature presents these ten digits to me. Mathematicians identify the set of natural numbers as being 1 (or maybe zero depending on who you ask) and all numbers succeeding 1 with 1 added (i. e. 2, 3, 4 etc.)

Mathematically you’ll see expressions of this sort:

1 ∈ ℕ

This can be read as “1 is a member of the class of Natural Numbers” The funny looking e is “member of” and the interesting N is the standard notation for Natural Numbers (capital N in what’s called a blackboard bold font). This assertion is true, of course. We can also assert that a negative is true. For example:

–1 ∉ ℕ

Which simply says that –1 is not a natural number. And of course that’s true as well. –1 is not a natural number. However both –1 and 1 are integers. In this case integers are represented by the special symbol ℤ which is a blackboard bold font Z. So these are all true:

  • 1 is a member of the set of Natural Numbers.

  • –1 is not a member of the set of Natural Numbers.

  • 1 is a member of the set of Integers.

  • –1 is a member of the set of Integers.

We can say that 1 is of type Natural Number or more familiarly we can say that it’s an integer. Some languages have untyped integers and this corresponds to natural numbers.

Type Classes

Now what about if we want to define operations which are applicable to both integers and natural numbers? Addition applies to both natural numbers and integers but it might not apply in a straightforward way to other types (for example, if I have a department type, what does it mean to add departments)?

Haskell’s type classes give me a mechanism to isolate and define behavior common to several types without having to lose the specific properties of the types. For example I can have subtraction on natural numbers but if I subtract 2 from 1 I’m no longer in the set of natural numbers. So it may make some sense to define an Integer typeclass and say that the result of subtraction of two integers is an integer. But the result of subtraction of two natural numbers may be undefined. Type classes give us a mechanism for capturing commonality among types without having to create types so generic as to be useless.

You would not be wrong to think of type classes as being types made up of types. In fact the saying about “turtles all the way down” comes to mind.