[GHC] #8599: Deriving in associated data families

#8599: Deriving in associated data families ------------------------------------+------------------------------------- Reporter: mojojojo | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Keywords: | Operating System: Unknown/Multiple Architecture: Unknown/Multiple | Type of failure: None/Unknown Difficulty: Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | ------------------------------------+------------------------------------- This code {{{ class Class a where data DF a data family DF' a instance (Eq (DF' a)) => Class a where newtype DF a = DF (DF' a) deriving (Eq) }}} fails with {{{ No instance for (Eq (DF' a)) arising from the 'deriving' clause of a data type declaration Possible fix: add an instance declaration for (Eq (DF' a)) or use a standalone 'deriving instance' declaration, so you can specify the instance context yourself When deriving the instance for (Eq (DF a)) }}} The following way it compiles fine, however {{{ class Class a where data DF a data family DF' a instance Class a where newtype DF a = DF (DF' a) deriving instance (Eq (DF' a)) => Eq (DF a) }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8599 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8599: Deriving in associated data families ignores instance's constraints -------------------------------------+------------------------------------ Reporter: mojojojo | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8599#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8599: Deriving in associated data families ignores instance's constraints -------------------------------------+------------------------------------ Reporter: mojojojo | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Documentation | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Changes (by goldfire): * component: Compiler => Documentation Comment: This is by design, though quite non-trivially. The problem is caused by the confluence of two design decisions: 1. Associated data/newtype instances do not interact with any specified context to the enclosing class instance. 2. As the manual says in section 7.5.1, "each constraint in the inferred instance context must consist only of type variables, with no repetitions." In the case at hand, the inferred context would be `Eq (DF' a)`, which does not meet this criterion. The fact in (1) does not seem to be documented anywhere, which is why I'm relabeling this as a documentation bug. Changing this fact seems hard: do we want the context to be inherited by all data constructors of an associated data instance? How can the context interact with an associated type instance (without substantially changing the way type families work)? To me, the only place that the context should possibly be inherited is in derived instances, as the original report suggests. But, even here, its role would be unclear: is the context "fully-specified", like in a standalone-deriving instance? What if a larger context is necessary? What if a smaller one were sufficient? There are consistent points in this design space, but choosing one and articulating it might be hard, and somewhat arbitrary. Thoughts? If someone agrees with relabeling this as a documentation bug, I can update the stuff on data families when I return from holiday (Jan. 6). -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8599#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8599: Deriving in associated data families ignores instance's constraints -------------------------------------+------------------------------------ Reporter: mojojojo | Owner: goldfire Type: bug | Status: new Priority: normal | Milestone: Component: Documentation | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Changes (by simonpj): * owner: => goldfire Comment: I agree with Richard here. The primary goal of allowing you to declare a `type`/`data` family within a `class` declaration is to make the link explicit and to remind you if you forget to give the instance in an `instance` declaration of the class. I also love the duality: * in a class declaration you give the type signature for any value methods; in the instance you give the actual method. * in a class declaration you give the kind signature for any associated types; in the instance you give the actual type definitions. But really it's not much more than syntactic sugar. You could equally well give the associated type family definitions outside the class, and likewise the instances. However, as syntactic sugar goes, it's pretty heavy. GHC has a lot of code that deals with associated types! In any case, that's why I agree with Richard's analysis. I think it'd be a mistake to conflate the context for the class instance declaration with the context for a derived equality instance. Do clarify the documentation, thank you Richard. Simon -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8599#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8599: Deriving in associated data families ignores instance's constraints
-------------------------------------+------------------------------------
Reporter: mojojojo | Owner: goldfire
Type: bug | Status: new
Priority: normal | Milestone:
Component: Documentation | Version: 7.6.3
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture: Unknown/Multiple
Type of failure: None/Unknown | Difficulty: Unknown
Test Case: | Blocked By:
Blocking: | Related Tickets:
-------------------------------------+------------------------------------
Comment (by Richard Eisenberg

#8599: Deriving in associated data families ignores instance's constraints -------------------------------------+------------------------------------ Reporter: mojojojo | Owner: goldfire Type: bug | Status: closed Priority: normal | Milestone: Component: Documentation | Version: 7.6.3 Resolution: fixed | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Changes (by goldfire): * status: new => closed * resolution: => fixed -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8599#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC