Ticket #2721 (new feature request)

Opened 5 years ago

Last modified 8 months ago

Newtype deriving doesn't work with type families

Reported by: rl Owned by:
Priority: lowest Milestone: 7.6.2
Component: Compiler Version: 6.10.1
Keywords: Cc: tom.schrijvers@…, illissius@…, acfoltzer@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
Test Case: deriving/should_fail/T2721 Blocked By:
Blocking: Related Tickets:

Description (last modified by simonpj) (diff)

This assumes -XTypeFamiles -XGeneralizedNewtypeDeriving. Example:

class C a where
  type T a
  foo :: a -> T a

instance C Int where
  type T Int = Int
  foo = id

newtype N = N Int deriving(C)

This happily produces an instance C N but no type instance T N. It should either (preferably) generate

type instance T N = Int

or fail. The example also compiles if T is a data family (the Int instance needs to be change accordingly). It should probably fail in this case.

BTW, this also compiles fine, with rather dramatic consequences:

type family T a
class C a where
  foo :: a -> T a

type instance T Int = Int
instance C Int where
  foo = id

type instance T N = Double
newtype N = N Int deriving(C)

I guess this last example is the same bug as #1496. I wonder if the deriving clause could generate something like:

instance T Int ~ T N => C Int

Change History

Changed 5 years ago by igloo

  • difficulty set to Unknown
  • milestone set to 6.10.2

Changed 4 years ago by simonpj

  • testcase set to deriving/should_fail/T2721
  • description modified (diff)

Good point. For now I'm just going to make it fail. It's quite a bit more work to make it generate the extra instance, and the fact that it does not work at all for 'data' convinced me.

It's annoying that you therefore cannot do newtype-deriving for a class with an associated type. An alternative is to allow top-level declarations for associated types, and check that these exist when doing the instance decl. So this would be valid:

class C a where
  type T a
  op :: a -> T a

type instance T Int = Bool
instance C Int where
  op x = True

So I'll leave this open to record the idea, but meanwhile I'll push a patch to make it fail uniformly.

Simon

Changed 4 years ago by simonpj

  • owner set to igloo
  • type changed from bug to merge

This is the patch

Wed Dec 31 16:43:00 GMT 2008  simonpj@microsoft.com
  * Fix Trac #2721: reject newtype deriving if the class has associated types

Pls merge; then leave the ticket open as feature request for 6.12

Simon

Changed 4 years ago by igloo

  • type changed from merge to feature request
  • milestone changed from 6.10.2 to 6.12 branch

Merged.

Changed 4 years ago by igloo

  • owner igloo deleted

Changed 4 years ago by test

  • cc tom.schrijvers@… added

Changed 3 years ago by igloo

  • milestone changed from 6.12 branch to 6.12.3

Changed 3 years ago by igloo

  • priority changed from normal to low
  • milestone changed from 6.12.3 to 6.14.1

Changed 2 years ago by igloo

  • milestone changed from 7.0.1 to 7.0.2

Changed 2 years ago by igloo

  • milestone changed from 7.0.2 to 7.2.1

Changed 2 years ago by illissius

  • cc illissius@… added
  • failure set to None/Unknown

Changed 20 months ago by igloo

  • milestone changed from 7.2.1 to 7.4.1

Changed 15 months ago by igloo

  • priority changed from low to lowest
  • milestone changed from 7.4.1 to 7.6.1

Changed 13 months ago by acfoltzer

  • cc acfoltzer@… added

Changed 8 months ago by igloo

  • milestone changed from 7.6.1 to 7.6.2
Note: See TracTickets for help on using tickets.