Opened 16 months ago

Last modified 9 months ago

#15167 new bug

DerivClause list is not populated for (TyConI (DataD ...))

Reported by: 0xd34df00d Owned by:
Priority: normal Milestone: 8.10.1
Component: Template Haskell Version: 8.4.2
Keywords: Cc: newcomer
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


% cat Test.hs
{-# LANGUAGE LambdaCase #-}

module Test where

import Language.Haskell.TH

test :: Name -> Q [Dec]
test name = reify name >>= \case
    TyConI dec -> do
        runIO $ print dec
        pure []
    _ -> pure []

% cat Run.hs
{-# LANGUAGE TemplateHaskell #-}

import Test

data Foo = Foo deriving (Eq, Ord, Show)

test ''Foo

% ghc Run.hs
[2 of 2] Compiling Main             ( Run.hs, Run.o )
DataD [] Main.Foo [] Nothing [NormalC Main.Foo []] []

One might expect the DataD to mention Eq, Ord, Show in the DerivClause list, but it doesn't.

This behavior manifests with every ghc version I tried: 8.0.2, 8.2.2, 8.4.2. I also asked a question whether it's intended behaviour on #haskell, and I've been advised to open a bug report, so here it is.

Change History (6)

comment:1 Changed 16 months ago by RyanGlScott

Cc: newcomer added

Indeed, this is expected. Reified data declarations never have derived instances attached to them, as they're only used when splicing a TH-constructed data type into source code. (If you want to know a data type's instances via TH, you should use reifyInstances.)

The Haddocks in template-haskell currently don't state that reified data types lack derived instance information, however, so it would be good to update the documentation to reflect this.

comment:2 Changed 16 months ago by 0xd34df00d

Thanks for getting to my report!

Seems like reifyInstances's documentation could also be slightly improved: it's not entirely obvious from it right now that it can/should be used to get the list of instances for a data type. Indeed, for the above example, how does one use it to get something about Eq, Ord, Show without knowing a priori that those are the classes that Foo implements?

comment:3 Changed 16 months ago by bgamari

Unfortunately I don't think we currently have a good way to get this information. When we are asked to reify the tycon the original Haskell AST (which would contain the deriving information) that we typechecked to produce the type is gone.

comment:4 Changed 15 months ago by bgamari


These won't be addressed for GHC 8.6.

comment:5 Changed 12 months ago by Ryan Scott <…>

In 85376570/ghc:

Documentation fixes in 'template-haskell'

 * Clarify the non-presence of derived classes in reified decls (#15167)
 * Clarify the shallowness of "reifyInstances" (#7066)
 * Mention that 'Typeable' instances are not found by reifyInstances (#11251)
 * Various Haddock markup issues fixed

Reviewers: goldfire, bgamari

Reviewed By: bgamari

Subscribers: rwbarton, carter

GHC Trac Issues: #15167, #7066, #11251

Differential Revision:

comment:6 Changed 9 months ago by osa1


Bumping milestones of low-priority tickets.

Note: See TracTickets for help on using tickets.