Skip to content

StandaloneDeriving on Read fails for GADTs

Consider the following code that derives a Read instance for a GADT datatype:

data Foo a where
    Foo :: Show a => a -> Foo a
deriving instance Show (Foo a)

The above code works correctly. However, changing the "Show" constraint to "Read" will cause GHC to fail:

data Foo a where
    Foo :: Read a => a -> Foo a
deriving instance Read (Foo a)

with the error message:

    No instance for (Read a)
      arising from a use of `GHC.Read.readPrec'
    In the first argument of `Text.ParserCombinators.ReadPrec.step', namely
      `GHC.Read.readPrec'
    In a stmt of a 'do' block:
      a1 <- Text.ParserCombinators.ReadPrec.step GHC.Read.readPrec
    In the second argument of `Text.ParserCombinators.ReadPrec.prec', namely
      `do { Text.Read.Lex.Ident "Foo" <- GHC.Read.lexP;
            a1 <- Text.ParserCombinators.ReadPrec.step GHC.Read.readPrec;
            return (Foo a1) }'
    When typechecking the code for  `GHC.Read.readPrec'
      in a standalone derived instance for `Read (Foo a)':
      To see the code I am typechecking, use -ddump-deriv

A workaround is to place an additional Read constraint on the deriving clause:

data Foo a where
    Foo :: Read a => a -> Foo a
deriving instance Read a => Read (Foo a)
Trac metadata
Trac field Value
Version 7.6.2
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