[GHC] #7742: StandaloneDeriving on Read fails for GADTs

#7742: StandaloneDeriving on Read fails for GADTs --------------------------------------+------------------------------------- Reporter: hoffstaetter | Owner: Type: bug | Status: new Priority: normal | Component: Compiler Version: 7.6.2 | Keywords: Os: MacOS X | Architecture: Unknown/Multiple Failure: GHC rejects valid program | Blockedby: Blocking: | Related: --------------------------------------+------------------------------------- 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) }}} -- Ticket URL: http://hackage.haskell.org/trac/ghc/ticket/7742 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#7742: StandaloneDeriving on Read fails for GADTs --------------------------------------+------------------------------------- Reporter: hoffstaetter | Owner: Type: bug | Status: new Priority: normal | Component: Compiler Version: 7.6.2 | Keywords: Os: MacOS X | Architecture: Unknown/Multiple Failure: GHC rejects valid program | Blockedby: Blocking: | Related: --------------------------------------+------------------------------------- Comment(by kosmikus): I think GHC is correct here. Ignoring `readsPrec` for simplicity and just looking at `read`, you are trying to derive a function of `String -> Foo a` here. But in order to produce a `Foo a`, we have to ''construct'' a `Read a` dictionary. And there's no way to come up with one for an arbitrary `a`. That's why you need an external `Read a` constraint. The situation is quite different for `Show`, because then you're provided with a `Foo a`, can extract the `Show` dictionary hidden within and use that to produce a textual representation of the `a`. -- Ticket URL: http://hackage.haskell.org/trac/ghc/ticket/7742#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#7742: StandaloneDeriving on Read fails for GADTs ---------------------------------+------------------------------------------ Reporter: hoffstaetter | Owner: Type: bug | Status: closed Priority: normal | Component: Compiler Version: 7.6.2 | Resolution: invalid Keywords: | Os: MacOS X Architecture: Unknown/Multiple | Failure: GHC rejects valid program Blockedby: | Blocking: Related: | ---------------------------------+------------------------------------------ Changes (by monoidal): * status: new => closed * resolution: => invalid Comment: I agree with kosmikus. {{{instance Show (Foo a)}}} can be written manually, while {{{instance Read (Foo a)}}} cannot. -- Ticket URL: http://hackage.haskell.org/trac/ghc/ticket/7742#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC