Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Ord a allow me to use "=="?

I was doing some test functions to understand a little more about Haskell, and I did this:

constroiMSet :: Ord a => [a] -> [(a,Int)] 
constroiMSet( x:x1:xs) |x1==x =[(x,2)]

The thing I don't get is why this function works. This function has Ord a but it uses == to compare a. Isn't Ord a only for >=, <=, <, >?

like image 367
Joao Parente Avatar asked Dec 11 '22 09:12

Joao Parente


1 Answers

If we look at the definition of the Ord class, we see:

class Eq a => Ord a where
  compare :: a -> a -> Ordering
  (<) :: a -> a -> Bool
  (<=) :: a -> a -> Bool
  (>) :: a -> a -> Bool
  (>=) :: a -> a -> Bool
  max :: a -> a -> a
  min :: a -> a -> a

So this means that every type a where Ord a, also means Eq a. So an a that supports Ord, also supports Eq and thus (==) and (/=).

like image 182
Willem Van Onsem Avatar answered Jan 07 '23 15:01

Willem Van Onsem