Skip main navigation

Types with Class

Type classes are a way to overload functions or operators by putting constraints on polymorphism

Type classes are a way to overload functions or operators by putting constraints on polymorphism.

For example, we have seen that:

(+) :: a -> a -> a 

is not OK because we want to restrict addition to numbers.

Likewise,

 (<) :: a -> a -> Bool

is not OK because it is not clear a priori how to compare to arbitrary types.

To address this issue Haskell provides type classes. These restrict the polymorphism. For example:

 (+) :: Num a => a -> a -> a 

says that the type a must be a numeric type, and

 (<) :: Ord a => a -> a -> Bool

says that a must be orderable.

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