Skip to content

Associated data type doesn't infer context

The following code fails to compile:

class Eq a => Bar a where
  bar :: a -> Int

class Bar (Baz a) => Foo a where
  data Baz a :: *
  foo :: Baz a -> Baz a

data Bang a = Bang (Baz a) deriving Eq

The error message is

test.hs:8:36:
    No instance for (Eq (Baz a))
      arising from the 'deriving' clause of a data type declaration
                   at test.hs:8:36-37
    Possible fix: add an instance declaration for (Eq (Baz a))
    When deriving the instance for (Eq (Bang a))

If I add a context of Foo a to Bang, it compiles, but since Baz is only defined on instances of Foo, shouldn't GHC be able to figure out that Baz a always has an Eq instance?

In any case, the error message is misleading, since there already is a context for Baz a

Trac metadata
Trac field Value
Version 6.8.3
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information