Opened 14 months ago

Closed 14 months ago

Last modified 14 months ago

#15551 closed bug (fixed)

TH-reified type classes have redundant tyvars/class constraints on each method

Reported by: RyanGlScott Owned by:
Priority: normal Milestone: 8.8.1
Component: Template Haskell Version: 8.4.3
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D5088
Wiki Page:

Description

If you try to reify a type class in Template Haskell, as in the following example:

{-# LANGUAGE TemplateHaskell #-}
module Bug where

import Language.Haskell.TH

class C a where
  method :: a

$(pure [])

main :: IO ()
main = putStrLn $(reify ''C >>= stringE . pprint)

You'll discover something strange:

$ /opt/ghc/8.4.3/bin/runghc Bug.hs
class Bug.C (a_0 :: *)
    where Bug.method :: forall (a_0 :: *) . Bug.C a_0 => a_0

Notice how method has a completely redundant forall (a_0 :: *) . Bug.C a_0 =>! I find this very strange, and would have expected to simply see Bug.method :: a_0 instead.

Currently, I have to work around this oddity by manually stripping off the forall (a_0 :: *) . Bug.C a_0 => myself, but it would be nice if I didn't have to do so. Does this proposal sound reasonable?

Change History (5)

comment:1 Changed 14 months ago by RyanGlScott

Differential Rev(s): Phab:D5088
Status: newpatch

I decided to be bold and just fix this. See Phab:D5088.

comment:2 Changed 14 months ago by Krzysztof Gogolewski <krz.gogolewski@…>

In 6e765ae/ghc:

Don't reify redundant class method tyvars/contexts

Summary:
Currently, reifying classes produces class methods with
redundant tyvars and class contexts in their type signatures, such
as in the following:

```lang=haskell
class C a where
  method :: forall a. C a => a
```

Fixing this is very straightforward: just apply `tcSplitMethodTy` to
the type of each class method to lop off the redundant parts.

It's possible that this could break some TH code in the wild that
assumes the existence of these tyvars and class contexts, so I'll
advertise this change in the release notes just to be safe.

Test Plan: make test TEST="TH_reifyDecl1 T9064 T10891 T14888"

Reviewers: goldfire, bgamari, simonpj

Reviewed By: simonpj

Subscribers: simonpj, rwbarton, carter

GHC Trac Issues: #15551

Differential Revision: https://phabricator.haskell.org/D5088

comment:3 Changed 14 months ago by monoidal

Milestone: 8.8.18.6.1
Status: patchmerge

If it's not too late, this can be merged to 8.6.

comment:4 Changed 14 months ago by RyanGlScott

Milestone: 8.6.18.8.1
Resolution: fixed
Status: mergeclosed

I'm going to request that this not be merged to GHC 8.6.1, since it has the potential to break code which assumes the existence of these redundant tyvars/class constraints when reified. (I'll update the Migration Guide for 8.8 to address this.)

comment:5 Changed 14 months ago by monoidal

I'm sorry, I updated the wrong ticket. I agree.

Note: See TracTickets for help on using tickets.