6

Type Classes

Like most other programming languages, Haskell features a number of overloaded operators and functions, such as (+), that work at different types. For instance, (+) has the following four types:

(+) :: Int -> Int -> Int
(+) :: Integer -> Integer -> Integer
(+) :: Float -> Float -> Float
(+) :: Double -> Double -> Double

Overloading is not the same as (parametric) polymorphism: (+) does not have the type a -> a -> a and does not work on all possible types. Overloading means that the operator only works for a specific set of types and that the operator’s behavior is different for each type.

In many languages, overloading is ad hoc: it exists for a fixed number of operators and functions and a fixed set of types. Moreover, a function ...

Get Soar with Haskell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.