Num does not inherit Show and Eq in new versions of GHC

82 views
Skip to first unread message

Petar Kovačević

unread,
Oct 14, 2017, 3:13:59 PM10/14/17
to Haskell-FER
I got the error "Could not deduce (Show a) arising from a use of ‘show’ from the context: Num a" and found the solution here (I'm on GHC 8.2.1)

I was wondering - is this just a GHC thing or is it a new language standard? 
What other type changes have there been (i.e. what else is different from this graph)?

The main reason for these questions is that on a lot of places online I still find "class (Eq a, Show a) => Num a" (including the preview when searching "Num" on Hoogle) but, if the Stack Overflow comment is correct, Num has been independent of Show and Eq in GHC since 2012.

Luka Hadžiegrić

unread,
Oct 15, 2017, 5:41:13 AM10/15/17
to Haskell-FER
Yes, there were some changes in GHC 8.x when compared to GHC 7.x. I don't know if there is a practical graph somewhere but you can usually find out all of useful information about a certain type class, as well as what it inherits from by typing `:i <name_of_typeclass>` in your REPL.

For example, if you check for `Real` type class, you can see that it "inherits" from `Num` and `Ord`, and if you check a concrete type like `Int` than you can see it has instance of `Show`.

Prelude> :i Real
class (Num a, Ord a) => Real a where
  toRational
:: a -> Rational
 
{-# MINIMAL toRational #-}
       
-- Defined in `GHC.Real'
instance Real Word -- Defined in `
GHC.Real'
instance Real Integer -- Defined in `GHC.Real'

instance
Real Int -- Defined in `GHC.Real'
instance Real Float -- Defined in `
GHC.Float'
instance Real Double -- Defined in `GHC.Float'


Prelude> :i Int
data
Int = GHC.Types.I# GHC.Prim.Int#   -- Defined in `GHC.Types'
instance
Bounded Int -- Defined in `GHC.Enum'
instance Enum Int -- Defined in `
GHC.Enum'
instance Eq Int -- Defined in `GHC.Classes'

instance
Integral Int -- Defined in `GHC.Real'
instance Num Int -- Defined in `
GHC.Num'
instance Ord Int -- Defined in `GHC.Classes'

instance
Read Int -- Defined in `GHC.Read'
instance Real Int -- Defined in `
GHC.Real'
instance Show Int -- Defined in `GHC.Show'

Luka Hadžiegrić

unread,
Oct 15, 2017, 5:44:31 AM10/15/17
to Haskell-FER
Although, graph you found is from Haskell 98, current standard is Haskell 2010 so that might be it.


On Saturday, October 14, 2017 at 9:13:59 PM UTC+2, Petar Kovačević wrote:
Reply all
Reply to author
Forward
0 new messages