Opened 8 years ago

Closed 8 years ago

Last modified 3 months ago

#902 closed bug (fixed)

Deriving for H98 types using GADT syntax fails

Reported by: bringert@… Owned by:
Priority: normal Milestone:
Component: Compiler Version: 6.5
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

This example from the GADT section of the user manual:

data Maybe1 a where {
      Nothing1 :: Maybe1 a ;
      Just1    :: a -> Maybe1 a
    } deriving (Eq,Ord)

Fails with:

Test_show.hs:28:5:
    Can't derive instances where the instance context mentions
    type variables that are not data type parameters
      Offending constraint: Eq a
    When deriving the instance for `Eq (Maybe1 a)'

Test_show.hs:28:5:
    Variable occurs more often in a constraint than in the instance head
      in the constraint: Eq a
    (Use -fallow-undecidable-instances to permit this)
    In the derived instance declaration
      instance (Eq a) => Eq (Maybe1 a) 

-dppr-debug gives this:

Test_show.hs:28:5:
    Can't derive instances where the instance context mentions
    type variables that are not data type parameters
      Offending constraint: base:GHC.Base.Eq{tc 23} a{tv adf} [tv]
    When deriving the instance for `base:GHC.Base.Eq{tc 23} (main:Test_show.Maybe1{tc ray} a{tv adg} [tv])'

Test_show.hs:28:5:
    Variable occurs more often in a constraint than in the instance head
      in the constraint: base:GHC.Base.Eq{tc 23} a{tv adf} [tv]
    (Use -fallow-undecidable-instances to permit this)
    In the derived instance declaration
      instance (base:GHC.Base.Eq{tc 23} a{tv adf} [tv]) =>
               base:GHC.Base.Eq{tc 23} (main:Test_show.Maybe1{tc ray} a{tv adg} [tv])

The two "a":s appear to be different, "a{tv adf}" and "a{tv adg}".

Change History (6)

comment:1 Changed 8 years ago by bringert@…

In compiler/typecheck/TcDeriv.lhs, mkDataTypeEqn, ordinary_constraints, constraints are added for all argument types of all data constructors. However, with GADTs, type variables which are data constructor arguments are distinct from the type constructor arguments. simonpj suggested fixing this by using dataConInstOrigArgTys to instantiate the type variables.

comment:2 Changed 8 years ago by bringert@…

Added regression test drv012, testsuite/tests/ghc-regress/deriving/should_compile/drv012.hs.

comment:3 Changed 8 years ago by bringert@…

  • Resolution set to fixed
  • Status changed from new to closed

Fixed by patch "Instantiate data constructor type variables with the type constructor type variables in constraints for derived class instances. Fixes instance deriving for GADTs which declar H98 types, closing ticket #902."

comment:4 Changed 6 years ago by simonmar

  • Architecture changed from Unknown to Unknown/Multiple

comment:5 Changed 6 years ago by simonmar

  • Operating System changed from Unknown to Unknown/Multiple

comment:6 Changed 3 months ago by bjorn <bjorn@…>

In d378a01f454f0645459903d838fde091791c6e35/ghc:

Added test for ticket #902, deriving for GADTs which declare H98 types fails.
Note: See TracTickets for help on using tickets.