Opened 3 years ago

Closed 3 years ago

#5007 closed bug (invalid)

"deriving" seems to ignore class context for a type family

Reported by: jkff Owned by: simonpj
Priority: high Milestone: 7.2.1
Component: Compiler (Type checker) Version: 7.0.2
Keywords: type families, datatype contexts, type classes, deriving Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: GHC rejects valid program Difficulty:
Test Case: Blocked By:
Blocking: Related Tickets:

Description

class Foo a where
  data Bar a :: *

class (Show (Bar a)) => Qux a
data (Qux a) => Xyzzy a = Xyzzy (Bar a) deriving Show

Here I get the following error:

    No instance for (Show (Bar a))
      arising from the 'deriving' clause of a data type declaration
    Possible fix:
      add an instance declaration for (Show (Bar a))
      or use a standalone 'deriving instance' declaration,
           so you can specify the instance context yourself
    When deriving the instance for (Show (Xyzzy a))

Curiously, I get the same error even if I add ", Show (Bar a)" to the context of "Xyzzy".

Change History (2)

comment:1 Changed 3 years ago by igloo

  • Milestone set to 7.2.1
  • Owner set to simonpj
  • Priority changed from normal to high

Simon, is this the expected behaviour?

comment:2 Changed 3 years ago by simonpj

  • Resolution set to invalid
  • Status changed from new to closed

The error message is quite right.

Using a context on a data type declaration, as you are doing, is a mis-feature of Haskell. If you read its specification carefully you'll see that it is practically useless. Any program using it is suspicious. Certainly, it has absolutely no effect on 'deriving' declarations.

You can follow the advice in the error message and use a standalone deriving declaration, thus

{-# LANGUAGE StandaloneDeriving, FlexibleContexts, UndecidableInstances, TypeFamilies #-}

module T5007 where

class Foo a where
   data Bar a :: *

class (Show (Bar a)) => Qux a
data Xyzzy a = Xyzzy (Bar a) 

deriving instance Show (Bar a) => Show (Xyzzy a)
Note: See TracTickets for help on using tickets.