Skip main navigation

Type Classes

Similar types may be related using type classes. Watch Jeremy Singer introduce some existing type classes in Haskell.
JEREMY: Hi. In any high-level programming language, there are various number like types. In Haskell, for instance, we have seen Int so far. There are also float and infinite precision integers, like BigIntegers in Java to name a few more types. Now, here is a key insight. Many arithmetic functions can be applied to values from any number like type, for example, addition or multiplication. Here’s 2 + 2, which makes 4. Here is 2 + pi, which makes 5.1415, and so on. Here’s 2 * 2, which makes 4. And here’s 2 * pi, which makes 6.2831, so on. This is where the notion of type classes comes in handy. A type class specifies a family of types that provide implementations for common functions.
Let’s have a look at the type of (+).
Sorry, I need to put (+) inside brackets because it’s an infix operator.
This says, given a type a– some people pronounce this as alpha– given a type a belonging to the Num type class, then the + function takes two a parameters and returns an a result. a is a type variable. The part of the type before the double arrow here is called the context of the type. This expresses type class membership for type variables like a. So a type class is a generalized family of similar types.
Let’s think about some other the type classes. The Eq type class means we can compare values of such types equality with the (==) operator.
We see that given the context of the type a that is an instance of the Eq type class, then the (==) function takes two parameters of type a and return to Boolean result. So we can see that ints are an instance of the Eq type class.
The Ord type class means we can order values of such types with relational operators like less than and greater than.
This is like the IComparable interface in C#.
For strings, less than implements a lexicographic ordering.
The Show type class means we can generate string values that represent values of such types, like toString() in Java. So if I say, show 42 integer value, then I get back the string 42.
If I say show False, I get back the string False.
The Read type class means we can generate values of such types from string values. So I’m going to say read “1” so string. Now, this doesn’t work at the moment because I need to say that the thing I’m reading should be of type Int. Now, it works. Again, I could say read “True,” which should be of type Bool, and I get back a Boolean type– rather, a value of a Boolean type.
To specify that a type belongs to a type class, for now, we will use the deriving clause in the type definition. Let’s go back to our SimpleNum class. Remember, we can only count values One, Two, and Many. Data SimpleNum = One, Two, Many. And this type is deriving (Show, Read).
The set of type classes that this type is an instance of are in parentheses in the deriving clause. Now, this means we can convert SimpleNum values into strings using the show function, and vice versa using the read function. Let’s say show One, show Two, show Many. And in each case, we return a string.
Let’s try to read “One”. We get an error because read doesn’t know what the type of One to be– the string One to be– when we read it. So let’s say we want this to be of type SimpleNum, and how it works.
Let’s try comparing SimpleNum values for equality.
Does One == One? No. Look at the error message. SimpleNum types are not instances of the Eq type class. So let’s add the Eq type class to the deriving clause in the SimpleNum type definition. Look, now, we have three type classes (Show, Read, Eq).
Does One == One now? Yes, it does. Does One == Two? No, it doesn’t. Does One == “One”? No, because SimpleNums are of a different type two strings, which a character lists.
Again, there’s a default implementation of ==, which is being used to compare SimpleNums.
There is a mechanism for defining type class function implementations, but we’ll save this discussion for later on in the course.
Note that type classes were one of the early innovations of the Haskell programming language. The type class constrains member types to provide functions that conform to certain type signatures effectively, API constraints. This is a little like object-oriented programming. Type classes are like interfaces in C# and Java. Types in the type class are like concrete implementations of the interface. Type classes provide a neat mechanism to enable operator overloading in the Haskell language. We’ve seen this with functions like ==, and relational operators, and the show and read functions. We’ll explore type classes in more detail later on in the course. But for now, goodbye.

Inevitably, we have glossed over some of the more complex parts of Haskell while we have been introducing the language. Now it’s time to explore one of the ideas we have bumped into on several occasions already — type classes.

What’s the type of 42? Either type 42 into the online interpreter or enter

:t 42

in GHCi. Either way, you should get back Num a => a — whereas you might have thought the answer should be Int. The Num is a type class, which specifies a family of types including integer and floating-point types.

We also anticipated type classes when we used the deriving Show clause to print out our custom data types. Now it’s time to explain type classes in more detail. Please watch the video and think about the concepts. Do type classes remind you of features in other languages? Leave your thoughts in the comments section.

This article is from the free online

Functional Programming in Haskell: Supercharge Your Coding

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education