September 7, 2026

Haskell for Hobos-- Type Classes

Haskell has types and type classes. I mention this up front because it is important to be clear on the difference.

Type classes have nothing to do with classes and object oriented programming. Get that out of your head entirely if it was trying to jump in there.

The game with type classes is that a given type may "belong" to several (or none) type classes. Belonging to a typeclass (we'll just coin a new word) means that the type implements certain behavior, which generally means that certain operators (functions) can be used with that type.

An example is the "Ord" class, which means that the type can be ordered. In particular various forms of the inequality operators may be applied to two objects of that type. We can write expressions like "a < b" that return a boolean

There is a separate "Eq" typeclass that supports the == and /= operators. That's right, /= is how you test for "not equal". Some types can be compared for equality but cannot be ordered.

There is a "Show" typeclass that indicates that a type supports the "show" function to convert it to a string for display. Notice that the typeclass names begin with a capital letter.

Last (in my discussion here), but not least is the "Monad" typeclass. We aren't ready yet to talk about Monads, but I want to emphasize that when we say that something is a Monad, we are talking about its behavior as a Monad. Monad is not a type, but many times things get talked about as though they were, and this (for me) led to confusion. A list is a Monad for example (who knew?), yet its type is a list. (We never write "list" or "List", we just start using square brackets.)

Finally for numbers there are the typeclasses Num and Real. If something is a Num (like and Int type) we can use + and -. If something is a Real, it can represent a rational number.


Have any comments? Questions? Drop me a line!

Tom's software pages / tom@mmto.org