Automatically derived instances

David Menendez zednenem at psualum.com
Sun Aug 28 21:10:01 EDT 2005


Juan Carlos Arevalo Baeza writes:

>    (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<--------------------------------------

Your problem is the instance in line 3. The way Haskell type classes
work, the overlap is determined without looking at the context, so "Show
a" will overlap with every possible instance for Show, including Show
Int, which is predefined.

I'm not sure what the official justification for that is, but reason is
to avoid situations like this:

    class A t where a :: t
    class B t where b :: t
    class C t where c :: t
    
    instance A t => C t where c = a
    instance B t => C t where c = b
    
    instance A Char where a = 'a'
    instance B Char where b = 'b'
    
What should c :: Char evaluate to?
-- 
David Menendez <zednenem at psualum.com> <http://www.eyrie.org/~zednenem/>


More information about the Glasgow-haskell-users mailing list