Ord and Enum type classes: what’s the difference?

Wednesday, 15th May, 2013

Learn you a Haskell describes these two type classes like this:

Ord is for types that have an ordering.

Enum members are sequentially ordered types.

I didn’t find this especially clear.

Real World Haskell failed (yet again). It [the printed book] gives no definition of Enum, although it seems to think it does (p. 472).

Mostly for my own benefit, this is what I’ve found:

The GHC documentation describes the two type classes like this (emphasis added):

The Ord class is used for totally ordered datatypes.

Class Enum defines operations on sequentially ordered types.

So both classes represent ordered types (both are instances of the Ordering data type). The difference is in the kind of ordering.

A clue is in the methods provided: Ord provides >, <, max and min; Enum provides succ and pred (and, interestingly, nothing similar to max and min).

Finally, the best explanation I found was in Chapter 8: Standard Haskell Classes of A Gentle Introduction to Haskell:

8.2 The Enumeration Class

Class Enum has a set of operations that underlie the syntactic sugar of arithmetic sequences; for example, the arithmetic sequence expression [1,3..] stands for enumFromThen 1 3. We can now see that arithmetic sequence expressions can be used to generate lists of any type that is an instance of Enum. This includes not only most numeric types, but also Char, so that, for instance, [‘a’..’z’] denotes the list of lower-case letters in alphabetical order. Furthermore, user-defined enumerated types like Color can easily be given Enum instance declarations. If so:

[Red..Violet] => [Red, Green, Blue, Indigo, Violet]

Note that such a sequence is arithmetic in the sense that the increment between values is constant, even though the values are not numbers. Most types in Enum can be mapped onto integers; for these, the fromEnum and toEnum convert between Int and a type in Enum.

Note however, that for some reason the Ord type class includes “All Prelude types except IO, (->), and IOError”, so a simple mathematical interpretation won’t do.


So! Actually I’m going to ignore that last caveat.

I think the comfiest way for me to understand these type classes is using the available methods: Ord types are those for which it makes sense to talk about relationships like “greater than” and “less than”; Enum types are those for which it makes sense to talk about relationships like “next” and “previous”. Days of the week would be Enum; Dates would be Ord.

6 Responses to “Ord and Enum type classes: what’s the difference?”

  1. yonseiazza Says:

    I just think Enum for elements those in a specific directed sequence(a rule) without their scale and Ord for elements those can be projected on a specific undirected scale.

  2. Pat Says:

    Awesome!! I’m on the Types and Typeclasses chapter of Learn You a Haskell, and the same thing had been bugging me. I’m still confused, but this does shed some light. Maybe it’ll make more sense when we learn more Haskell. I’m adding this page to my bookmarks.

    • llaisdy Says:

      Thanks for your comment! I enjoyed Learn You a Haskell a lot, but I did have to do a lot of digging around myself (the same went for Real World Haskell and Beginning Haskell, but at least LYAH was funny). I haven’t looked at Haskell much since these posts but please feel free to report back or post links here.

Leave a Reply to Pat Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: