Automatically derived instances

Juan Carlos Arevalo Baeza jcab.lists at JCABs-Rumblings.com
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:

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

    If you try to compile this, you get:

---8<--------------------------------------
    Overlapping instances for Show Int
      arising from the 'deriving' clause of a data type declaration at 
Test.hs:5:8
    Matching instances:
      Test.hs:3:0:
        instance (MyClass a) => Show a
      Imported from GHC.Show: instance Show Int
    When deriving the `Show' instance for type `Type1'
---8<--------------------------------------

   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 
expected.

   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:

---8<--------------------------------------
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)
---8<--------------------------------------

   I guess that's not doable?

JCAB



More information about the Glasgow-haskell-users mailing list