[GHC] #12594: DeriveAnyClass fails to derive some classes

GHC ghc-devs at haskell.org
Tue Sep 13 10:37:57 UTC 2016


#12594: DeriveAnyClass fails to derive some classes
-------------------------------------+-------------------------------------
        Reporter:  ivanm             |                Owner:
            Type:  bug               |               Status:  new
        Priority:  normal            |            Milestone:
       Component:  Compiler          |              Version:  8.0.1
      Resolution:                    |             Keywords:
Operating System:  Unknown/Multiple  |         Architecture:
                                     |  Unknown/Multiple
 Type of failure:  None/Unknown      |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:                    |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------

@@ -15,1 +15,1 @@
-     • No instance for (Database.PostgreSQL.Simple.ToField.ToField Char)
+     • No instance for (ToRow Int)

New description:

 {{{#!hs
 {-# LANGUAGE DeriveAnyClass, DeriveGeneric #-}

 import Database.PostgreSQL.Simple
 import GHC.Generics

 data Foo = Foo { bar :: Int }
   deriving (Generic, ToRow)
 }}}

 This succeeds using `ghci`, but fails when trying to compile it using
 `ghc` with the following error message:

 {{{
     • No instance for (ToRow Int)
         arising from the first field of ‘Foo’ (type ‘Int’)
       Possible fix:
         use a standalone 'deriving instance' declaration,
           so you can specify the instance context yourself
     • When deriving the instance for (ToRow Foo)
 }}}

 However, it works if I use `instance ToRow Foo` instead of relying upon
 `DeriveAnyClass`.

 I've tried this with other types instead of `Int`, using `newtype` instead
 of `data` and having multiple fields in the datatype.

--

Comment (by hsyl20):

 With DeriveAnyClass, GHC tries to infer the context for the instance. From
 compiler/typecheck/TcDeriv.hs:
 {{{
 -- Unfortunately, it is not clear how to determine the context (in case of
 -- standard deriving; in standalone deriving, the user provides the
 context).
 -- GHC uses the same heuristic for figuring out the class context that it
 uses for
 -- Eq in the case of *-kinded classes, and for Functor in the case of
 -- * -> *-kinded classes. That may not be optimal or even wrong. But in
 such
 -- cases, standalone deriving can still be used.
 }}}

 Here it seems to infer
 {{{instance ToRow Int => ToRow Foo }}}, hence the error.

 Maybe we should add the previous comment into the manual.

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12594#comment:2>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list