Opened 9 years ago

Closed 9 months ago

#2721 closed feature request (fixed)

Newtype deriving doesn't work with type families

Reported by: rl Owned by:
Priority: lowest Milestone: 8.2.1
Component: Compiler (Type checker) Version: 6.10.1
Keywords: TypeFamilies Cc: tom.schrijvers@…, illissius@…, acfoltzer@…, jkarni@…, RyanGlScott
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case: deriving/should_fail/T2721
Blocked By: Blocking:
Related Tickets: #8165 Differential Rev(s): Phab:D2636
Wiki Page:

Description (last modified by simonpj)

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 (27)

comment:1 Changed 9 years ago by igloo

difficulty: Unknown
Milestone: 6.10.2

comment:2 Changed 9 years ago by simonpj

Description: modified (diff)
Test Case: deriving/should_fail/T2721

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

comment:3 Changed 9 years ago by simonpj

Owner: set to igloo
Type: bugmerge

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

comment:4 Changed 9 years ago by igloo

Milestone: 6.10.26.12 branch
Type: mergefeature request

Merged.

comment:5 Changed 9 years ago by igloo

Owner: igloo deleted

comment:6 Changed 8 years ago by test

Cc: tom.schrijvers@… added

comment:7 Changed 7 years ago by igloo

Milestone: 6.12 branch6.12.3

comment:8 Changed 7 years ago by igloo

Milestone: 6.12.36.14.1
Priority: normallow

comment:9 Changed 7 years ago by igloo

Milestone: 7.0.17.0.2

comment:10 Changed 6 years ago by igloo

Milestone: 7.0.27.2.1

comment:11 Changed 6 years ago by illissius

Cc: illissius@… added
Type of failure: None/Unknown

comment:12 Changed 6 years ago by igloo

Milestone: 7.2.17.4.1

comment:13 Changed 6 years ago by igloo

Milestone: 7.4.17.6.1
Priority: lowlowest

comment:14 Changed 5 years ago by acfoltzer

Cc: acfoltzer@… added

comment:15 Changed 5 years ago by igloo

Milestone: 7.6.17.6.2

comment:16 Changed 3 years ago by thoughtpolice

Milestone: 7.6.27.10.1

Moving to 7.10.1.

comment:17 Changed 3 years ago by thomie

Component: CompilerCompiler (Type checker)

comment:18 Changed 3 years ago by thoughtpolice

Milestone: 7.10.17.12.1

Moving to 7.12.1 milestone; if you feel this is an error and should be addressed sooner, please move it back to the 7.10.1 milestone.

comment:19 Changed 3 years ago by thoughtpolice

Moving to 7.12.1 milestone; if you feel this is an error and should be addressed sooner, please move it back to the 7.10.1 milestone.

comment:20 Changed 2 years ago by jkarni

Cc: jkarni@… added

comment:21 Changed 2 years ago by thoughtpolice

Milestone: 7.12.18.0.1

Milestone renamed

comment:22 Changed 19 months ago by thomie

Keywords: TypeFamilies added

comment:23 Changed 19 months ago by thomie

Milestone: 8.0.1

comment:24 Changed 10 months ago by RyanGlScott

Cc: RyanGlScott added

comment:25 Changed 10 months ago by RyanGlScott

Differential Rev(s): Phab:D2636
Milestone: 8.2.1
Status: newpatch

comment:26 Changed 9 months ago by Ryan Scott <ryan.gl.scott@…>

In 630d8817/ghc:

Allow GeneralizedNewtypeDeriving for classes with associated type families

Summary:
This implements the ability to derive associated type family instances
for newtypes automatically using `GeneralizedNewtypeDeriving`. Refer to the
users' guide additions for how this works; I essentially follow the pattern
laid out in https://ghc.haskell.org/trac/ghc/ticket/8165#comment:18.

Fixes #2721 and #8165.

Test Plan: ./validate

Reviewers: simonpj, goldfire, austin, bgamari

Reviewed By: simonpj

Subscribers: mpickering, thomie

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

GHC Trac Issues: #2721, #8165

comment:27 Changed 9 months ago by RyanGlScott

Resolution: fixed
Status: patchclosed
Note: See TracTickets for help on using tickets.