Skip main navigation

New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £29.99 £19.99. New subscribers only. T&Cs apply

Find out more

Type Classes

Similar types may be related using type classes. Watch Jeremy Singer introduce some existing type classes in Haskell.
5.2
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.
82.1
Let’s have a look at the type of (+).
91.7
Sorry, I need to put (+) inside brackets because it’s an infix operator.
100.4
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.
153.2
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.
169.6
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.
199
The Ord type class means we can order values of such types with relational operators like less than and greater than.
212.8
This is like the IComparable interface in C#.
224.9
For strings, less than implements a lexicographic ordering.
234.1
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.
256.3
If I say show False, I get back the string False.
268.2
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.
317.9
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).
358.7
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.
393.3
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.
413.4
Let’s try comparing SimpleNum values for equality.
421.7
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).
454.3
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.
482.9
Again, there’s a default implementation of ==, which is being used to compare SimpleNums.
491.2
There is a mechanism for defining type class function implementations, but we’ll save this discussion for later on in the course.
502.3
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

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now