Re: [GHC] #12047: Users Guide: GeneralizedNewtypeDeriving derives “instance Num Int => Num Dollars”

GHC ghc-devs at haskell.org
Thu May 12 06:02:39 UTC 2016


#12047: Users Guide: GeneralizedNewtypeDeriving derives “instance Num Int => Num
Dollars”
-------------------------------------+-------------------------------------
        Reporter:  Iceland_jack      |                Owner:
            Type:  task              |               Status:  new
        Priority:  normal            |            Milestone:
       Component:  Compiler          |              Version:  7.10.3
      Resolution:                    |             Keywords:
Operating System:  Unknown/Multiple  |         Architecture:
 Type of failure:  Documentation     |  Unknown/Multiple
  bug                                |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:                    |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------
Description changed by Iceland_jack:

@@ -54,0 +54,2 @@
+ }}}
+ {{{
@@ -61,0 +63,2 @@
+ }}}
+ {{{
@@ -86,1 +90,1 @@
- that type signature is correct
+ that instance declaration is correct

New description:

 In
 [http://downloads.haskell.org/~ghc/8.0.1-rc4/docs/html/users_guide/glasgow_exts.html
 #generalising-the-deriving-clause 9.6.9.1. Generalising the deriving
 clause]

 > Notionally, the compiler derives an instance declaration of the form
 >
 > {{{#!hs
 > instance Num Int => Num Dollars
 > }}}
 >
 > which just adds or removes the newtype constructor according to the
 type.

 which I find weird, I so I decided to check the actual derived instance

 {{{
 $ ghci -ignore-dot-ghci
 GHCi, version 8.1.20160503: http://www.haskell.org/ghc/  :? for help
 Prelude> :set -ddump-deriv
 Prelude> :set -XGeneralizedNewtypeDeriving
 Prelude> newtype Dollar = Dollar Int deriving Num

 ==================== Derived instances ====================
 Derived instances:
   instance GHC.Num.Num Ghci1.Dollar where
     [...]
 }}}

 and neither found it there nor in the `:info`

 {{{
 Prelude> :info Num
 class Num a where
   [...]
 instance [safe] Num Dollar -- Defined at <interactive>:3:38
 }}}

 and while actually defining `instance Num Int => Num Dollar` gives you
 this awkward encounter

 {{{
 $ ghci -ignore-dot-ghci
 GHCi, version 8.1.20160503: http://www.haskell.org/ghc/  :? for help
 Prelude> data Dollar
 Prelude> instance Num Int => Num Dollar

 <interactive>:2:10: error:
     • Non type-variable argument in the constraint: Num Int
       (Use FlexibleContexts to permit this)
     • In the context: Num Int
       While checking an instance declaration
       In the instance declaration for ‘Num Dollar’
 }}}
 {{{
 Prelude> :set -XFlexibleContexts
 Prelude> instance Num Int => Num Dollar

 <interactive>:4:10: error:
     • The constraint ‘Num Int’ is no smaller than the instance head
       (Use UndecidableInstances to permit this)
     • In the instance declaration for ‘Num Dollar’
 }}}
 {{{
 Prelude> :set -XUndecidableInstances
 Prelude> instance NumPrelude Int => Num Dollar

 <interactive>:6:10: warning: [-Wsimplifiable-class-constraints]
     The constraint ‘Num Int’ matches an instance declaration
     instance Num Int -- Defined in ‘GHC.Num’
     This makes type inference very fragile;
       try simplifying it using the instance

 <interactive>:6:10: warning: [-Wmissing-methods]
     • No explicit implementation for
         ‘+’, ‘*’, ‘abs’, ‘signum’, ‘fromInteger’, and (either ‘negate’
                                                               or
                                                               ‘-’)
     • In the instance declaration for ‘Num Dollar’
 }}}

 {{{
 Prelude> :info Dollar
 data Dollar     -- Defined at <interactive>:1:1
 instance [safe] Num Int => Num Dollar
   -- Defined at <interactive>:6:10
 }}}

 Note that it appears with the context in `:info` so I wonder in what way
 that instance declaration is correct

--

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12047#comment:2>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list