#2734: Newtype deriving over phantom types broke between GHC 6.6.1 and GHC 6.8.1
--------------------------------------------+-------------------------------
 Reporter:  DavidA                          |          Owner:         
     Type:  bug                             |         Status:  closed 
 Priority:  normal                          |      Milestone:         
Component:  Compiler                        |        Version:  6.8.3  
 Severity:  normal                          |     Resolution:  invalid
 Keywords:  newtype deriving, phantom type  |     Difficulty:  Unknown
 Testcase:                                  |   Architecture:  x86    
       Os:  Windows                         |  
--------------------------------------------+-------------------------------
Changes (by simonpj):

  * status:  new => closed
  * difficulty:  => Unknown
  * resolution:  => invalid

Old description:

> Consider the following code:
>
> {-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-}
>
> data Lex
>
> data Glex
>
> newtype Monomial ord = M [Int] deriving (Eq,Show)
>
> instance Ord (Monomial Lex) where
>     compare (M xs) (M ys) = compare xs ys
>
> instance Ord (Monomial Glex) where
>     compare (M xs) (M ys) = compare (sum xs, xs) (sum ys, ys)
>
> -- newtype Polynomial ord = P [Monomial ord] deriving (Eq,Show,Ord)
>
> newtype Polynomial ord = P [Monomial ord] deriving (Eq,Show)
>
> instance Ord (Monomial ord) => Ord (Polynomial ord) where
>     compare (P ts) (P us) = compare ts us
>
> In 6.6.1, it was permissible to derive the Ord instance for Polynomial
> ord from the ord instance for Monomial ord - the commented out code would
> compile. In 6.8.1-3, the commented out code doesn't compile, so you have
> to do the derivation by hand, as shown.

New description:

 Consider the following code:
 {{{
 {-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-}

 data Lex

 data Glex

 newtype Monomial ord = M [Int] deriving (Eq,Show)

 instance Ord (Monomial Lex) where
     compare (M xs) (M ys) = compare xs ys

 instance Ord (Monomial Glex) where
     compare (M xs) (M ys) = compare (sum xs, xs) (sum ys, ys)

 -- newtype Polynomial ord = P [Monomial ord] deriving (Eq,Show,Ord)

 newtype Polynomial ord = P [Monomial ord] deriving (Eq,Show)

 instance Ord (Monomial ord) => Ord (Polynomial ord) where
     compare (P ts) (P us) = compare ts us
 }}}
 In 6.6.1, it was permissible to derive the Ord instance for Polynomial ord
 from the ord instance for Monomial ord - the commented out code would
 compile. In 6.8.1-3, the commented out code doesn't compile, so you have
 to do the derivation by hand, as shown.

Comment:

 I'm afraid this is by design: the 6.6 deriving mechanism could all-too-
 easily infer a stupid context for the derived instance declaration.

 However 6.10 lets you ''specify'' the context for the derived instance
 declaration (rather than having it inferred) thus:
 {{{
  newtype Polynomial ord = P [Monomial ord] deriving (Eq,Show)

  deriving instance Ord (Monomial ord) => Ord (Polynomial ord)
 }}}
 The `deriving instance` is half way between `deriving(Ord)` and giving a
 full instance declaration, which you didn't want to do.  It's documented
 under "standalone deriving" in the user manual.

 If the documentation could be improved, I'd welcome concrete suggestions.
 Meanwhile I'm closing the bug.

 Simon

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/2734#comment:1>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
_______________________________________________
Glasgow-haskell-bugs mailing list
Glasgow-haskell-bugs@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to