Users Guide: GeneralizedNewtypeDeriving derives “instance Num Int => Num Dollars”
In 9.6.9.1. Generalising the deriving clause
Notionally, the compiler derives an instance declaration of the form
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