[GHC] #12863: Associated data families don't use superclasses when deriving instances

#12863: Associated data families don't use superclasses when deriving instances -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.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: -------------------------------------+------------------------------------- {{{#!hs class Functor (TotalMap key) => Key key where data TotalMap key :: Type -> Type (¡) :: TotalMap key val -> key -> val }}} a constraint `Key key` entails a `Functor (TotalMap key)` constraint, using the notation of [https://hackage.haskell.org/package/constraints constraints] {{{#!hs prf1 :: Key key :- Functor (TotalMap key) prf1 = Sub Dict prf2 :: (Key key1, Key key2) :- (Functor (TotalMap key1), Functor (TotalMap key2)) prf2 = prf1 *** prf1 }}} Yet these proofs are not used in {{{#!hs -- tzLp.hs:28:89-95: error: … -- • No instance for (Functor (TotalMap key1)) -- arising from the first field of ‘PairMap’ -- (type ‘TotalMap key1 (TotalMap key2 val)’) -- Possible fix: -- use a standalone 'deriving instance' declaration, -- so you can specify the instance context yourself -- • When deriving the instance for (Functor (TotalMap (key1, key2))) -- Compilation failed. instance (Key key1, Key key2) => Key (key1, key2) where data TotalMap (key1, key2) val = PairMap (TotalMap key1 (TotalMap key2 val)) deriving Functor (¡) :: TotalMap (key1, key2) val -> ((key1, key2) -> val) PairMap keys ¡ (k1, k2) = keys ¡ k1 ¡ k2 }}} I would expect it to work like {{{#!hs deriving instance (Key key1, Key key2) => Functor (TotalMap (key1, key2)) }}} Same problem occurs with only a single constraint {{{#!hs -- tzLp.hs:33:14-20: error: … -- • No instance for (Functor (TotalMap key)) -- arising from the first field of ‘Id’ (type ‘TotalMap key val’) -- Possible fix: -- use a standalone 'deriving instance' declaration, -- so you can specify the instance context yourself -- • When deriving the instance for (Functor -- (TotalMap (Identity key))) -- Compilation failed. instance Key key => Key (Identity key) where data TotalMap (Identity key) val = Id (TotalMap key val) deriving Functor }}} which should work like {{{#!hs deriving instance Key key => Functor (TotalMap (Identity key)) -- or deriving instance Functor (TotalMap key) => Functor (TotalMap (Identity key)) }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12863 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12863: Associated data families don't use superclasses when deriving instances -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.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): Even adding them to the context doesn't make a difference {{{#!hs instance (Key key, Functor (TotalMap key)) => Key (Identity key) instance (Key key1, Key key2, Functor (TotalMap key1), Functor (TotalMap key2)) => Key (key1, key2) }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12863#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12863: Associated data families don't use superclasses when deriving instances -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.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 simonpj): This is just what I'd expect. The `deriving( Functor )` clause is trying to infer a suitable context `???` for {{{ instance ??? => Functor (TotalMap (Identity key)) }}} Let's call this "deriving context inference" (DCI). To do that it needs `Functor (TotalMap key)`. But DCI insists on a simple context with a class applied to type variables, otherwise it could infer stupid context like `Num (Tree a)`. So it insists on being able to simplify `Functor (TotalMap key)` and it can't. It's true that `Key key` is also OK for `???`, since `Functor (TotalMap key)` is a superclass, but DCI will never guess that. Apart from anything else it might be too restrictive. Now I think you are pointing out that DCI for an ''associated'' data type could include the context of the parent instance declaration. It would add a bit more code and complexity (to the compiler and the user manual). And it might be much more restrictive than necessary. E.g. {{{ instance (Key key) => Key (Identity key) where data TotalMap (Identity key) val = Id val deriving( Functor ) }}} I suppose we ''could'' specify that the derived instance is {{{ instance (Key key) => Functor (TotalMap (Identity key)) }}} but today we'll get the superior {{{ instance Functor (TotalMap (Identity key)) }}} But it would be a valid alternative design. My suggestion: use a standalone deriving clause to specify the context yourself. As you point out, it's not hard. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12863#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12863: Associated data families don't use superclasses when deriving instances -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 Resolution: duplicate | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: #4815 | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * status: new => closed * resolution: => duplicate * related: => #4815 Comment: I'm closing this as a duplicate of #4815, which is essentially the same thing. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12863#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC