[GHC] #14243: GHC doesn't add constraint when deriving

#14243: GHC doesn't add constraint when deriving -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.1 Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- I can't remember if this was intended, but GHC cannot `derive newtype Functor` or `derive stock Functor` for `WGeneric` even if it knows the constraint to add (I have `FlexibleInstances` enabled) {{{#!hs import GHC.Generic -- • Could not deduce (Functor (Rep1 f)) -- arising from the 'deriving' clause of a data type declaration -- from the context: Functor f -- bound by the deriving clause for ‘Functor (WGeneric f)’ -- at /home/baldur/hs/074.hs:63:5-11 -- Possible fix: -- use a standalone 'deriving instance' declaration, -- so you can specify the instance context yourself newtype WGeneric f a = WGeneric (Sum (Rep1 f) f a) deriving newtype Functor }}} but either of these works {{{#!hs deriving newtype instance (Functor f, Functor (Rep1 f)) => Functor (WGeneric f) deriving stock instance (Functor f, Functor (Rep1 f)) => Functor (WGeneric f) }}} Could GHC add the inferred `Functor (Rep1 f)`? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14243 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14243: GHC doesn't add constraint when deriving -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * cc: RyanGlScott (added) Comment: It seems like this may fall in Ryan's territory. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14243#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14243: GHC doesn't add constraint when deriving -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Iceland_jack): I don't remember if GHC rejects constraints with type families / that require `FlexibleContexts`, but here it infers the missing constraint -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14243#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14243: GHC doesn't add constraint when deriving -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.2.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: #11008 | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * related: => #11008 Comment: This is expected behavior. Here's a much simpler demonstration of this phenomenon: {{{#!hs {-# LANGUAGE TypeFamilies #-} type family Foo data Bar = Bar Foo deriving Show }}} {{{ Bug.hs:4:29: error: • No instance for (Show Foo) arising from the first field of ‘Bar’ (type ‘Foo’) Possible fix: use a standalone 'deriving instance' declaration, so you can specify the instance context yourself • When deriving the instance for (Show Bar) | 4 | data Bar = Bar Foo deriving Show | ^^^^ }}} As you noted, GHC could theoretically add this inferred `Show Foo` constraint to the derived instance context, but it chooses not to. The reasoning is outlined in [https://downloads.haskell.org/~ghc/8.2.1/docs/html/users_guide/glasgow_exts.... #inferred-context-for-deriving-clauses this section] of the users' guide. Essentially, GHC has a metric for determining whether an inferred context is "exotic", and if a constraint is considered too exotic, GHC will give up and demand that you write the context yourself using `StandaloneDeriving`. There are a couple of reasons why GHC does this. One reason is that exotic constraints are often not Haskell98/2010, so inferring them would be troublesome. But perhaps a more compelling reason is it can sometimes catch mistakes, like in this code: {{{#!hs data X a b = MkX (a -> b) deriving Eq }}} This fails, complaining about a missing `Eq (a -> b)` instance. This is almost certainly the desired behavior, since you probably didn't mean to use an `Eq` instance for function types in the first place! If you're determined that this is the right thing to do, however, GHC gives you a manual override in the form of `StandaloneDeriving`. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14243#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14243: GHC doesn't add constraint when deriving -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 8.2.1 Resolution: wontfix | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: #11008 | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * status: new => closed * resolution: => wontfix -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14243#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC