#4815: Instance constraints should be used when deriving on associated data 
types
---------------------------------+------------------------------------------
    Reporter:  batterseapower    |       Owner:              
        Type:  feature request   |      Status:  new         
    Priority:  normal            |   Component:  Compiler    
     Version:  6.12.3            |    Keywords:              
    Testcase:                    |   Blockedby:              
          Os:  Unknown/Multiple  |    Blocking:              
Architecture:  Unknown/Multiple  |     Failure:  None/Unknown
---------------------------------+------------------------------------------
 Consider this program:

 {{{
 {-# LANGUAGE TypeFamilies, FlexibleContexts #-}

 class Eq (Associated a) => Foo a where
     data Associated a

 instance Foo a => Foo (Maybe a) where
     data Associated (Maybe a) = AssociatedMaybe (Associated a)
                               deriving (Eq)
 }}}

 This does not compile, giving this error message:

 {{{

 /Users/mbolingbroke/Junk/Repro.hs:9:40:
     No instance for (Eq (Associated a))
       arising from the 'deriving' clause of a data type declaration
                    at /Users/mbolingbroke/Junk/Repro.hs:9:40-41
     Possible fix:
       add an instance declaration for (Eq (Associated a))
       or use a standalone 'deriving instance' declaration instead,
          so you can specify the instance context yourself
     When deriving the instance for (Eq (Associated (Maybe a)))
 }}}

 However, this is surprising because I clearly state that a is Foo, and
 hence (Associated a) has an Eq instance by the superclass constraint on
 Foo.

 If I point this out explicitly using standalone deriving it works:

 {{{
 {-# LANGUAGE TypeFamilies, FlexibleContexts #-}
 {-# LANGUAGE StandaloneDeriving, FlexibleInstances #-}

 class Eq (Associated a) => Foo a where
     data Associated a

 instance Foo a => Foo (Maybe a) where
     data Associated (Maybe a) = AssociatedMaybe (Associated a)
 --                              deriving (Eq)

 deriving instance Foo a => Eq (Associated (Maybe a))
 }}}

 So I think the default behaviour for "deriving" on an associated data
 family should be to include the constraints from the enclosing instance.
 For now the workaround is just to use standalone deriving.

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/4815>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to