Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell class Num not contained in Ord?

Tags:

haskell

I was reading LYAH and read that the Num class isn't a subset of Ord. This means that there is a Num type which is not a member of Ord, what could constitute such a type?

Note: Num is not a subclass of Ord. That means that what constitutes for a number doesn't really have to adhere to an ordering. So that's why we have to specify both the Num and Ord class constraints when doing addition or subtraction and also comparison.

Thanks!

like image 725
Guido Avatar asked Mar 22 '12 14:03

Guido


People also ask

What is an Ord in Haskell?

The Ord class is used for totally ordered datatypes. Instances of Ord can be derived for any user-defined datatype whose constituent types are in Ord. The declared order of the constructors in the data declaration determines the ordering in derived Ord instances.

Is Ord a subclass of Eq?

We say that Eq is a superclass of Ord (conversely, Ord is a subclass of Eq), and any type which is an instance of Ord must also be an instance of Eq.

What is instance Haskell?

An instance of a class is an individual object which belongs to that class. In Haskell, the class system is (roughly speaking) a way to group similar types. (This is the reason we call them "type classes"). An instance of a class is an individual type which belongs to that class.


1 Answers

Complex number (Data.Complex.Complex) is one example which is a number (Num) but cannot be ordered (not Ord).

like image 170
kennytm Avatar answered Sep 19 '22 13:09

kennytm