Automatically derived instances

Juan Carlos Arevalo Baeza jcab.lists at
Sun Aug 28 18:18:00 EDT 2005

   (BCC'ing the GHC bugs list)

   It seems like there's something very funky going on with GHC (6.4) 
and automatically deriving instances. Consider this code:

1: class MyClass a
3:instance MyClass a => Show a
5:newtype Type1 = Type1 { unType1 :: Int } deriving (Show)
7:main = putStrLn $ show $ Type1 4

    If you try to compile this, you get:

    Overlapping instances for Show Int
      arising from the 'deriving' clause of a data type declaration at 
    Matching instances:
        instance (MyClass a) => Show a
      Imported from GHC.Show: instance Show Int
    When deriving the `Show' instance for type `Type1'

   Weird error. I haven't even declared an instance of MyClass!

   If I change line 5 to "deriving (MyClass), I get:

*** Exception: stack overflow

   Sounds like a compiler bug to me.

   If I comment out line 3 and leave "deriving (Show)", it all works as 

   So... what's exactly the purpose of omitting the where-part of an 
instance declaration? It is supported, and it is even shown in code in 
the Haskell 98 report, but I failed to find any place in the whole 
document where it was described. Same thing for GHC's documentation.

   My purpose here was to do group classes for deriving like so:

class MyNum a

instance MyNum a => Eq a
instance MyNum a => Ord a
instance MyNum a => Show a
instance MyNum a => Enum a
instance MyNum a => Num a
instance MyNum a => Real a
instance MyNum a => Integral a
instance MyNum a => Bits a
instance MyNum a => Storable a

-- Too long?... newtype ID = ID { unID :: WORD } deriving (Eq, Ord, 
Show, Enum, Num, Real, Integral, Bits, Storable)
newtype ID = ID { unID :: Int } deriving (MyNum)

   I guess that's not doable?


More information about the Glasgow-haskell-users mailing list