Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ghci outputs (Num a) => a for :t 4 and not (Ord a) => a?

When I enter :t 4 in ghci I get

Prelude> :t 4
4 :: Num t => t

I understand why 4 is not only an Int or an Integer and that it is infered bottom up but I do not understand why 4 is not shown as an Ord t => t or even more correct something like this:

4 :: (Ord t || Num t) => t

Because 4 is both an Ord and a Num but Ord and Num have no connection...

So why does :t 4 output only Num?

like image 781
Databyte Avatar asked Jan 05 '23 03:01

Databyte


1 Answers

Not all types with instances for Num also have instances for Ord, and you only need the fromInteger part of Num to have the overloaded numeric literals Haskell has. For example, Complex from Data.Complex has a Num instance but no Ord. In this case, 4 is not an Ord.

ghci> import Data.Complex
ghci> let x = 1 :: Complex Double
ghci> let y = 2 :: Complex Double
ghci> x < y
<interactive>
    * No instance for (Ord (complex Double)) arising from use of `<'
    * In the expression: x < y
      In the equation for `it': it = x < y
ghci>

As @Lee commented, this is the behavior outlined in the report.

like image 79
Alec Avatar answered Jan 10 '23 13:01

Alec