Opened 3 years ago

Closed 3 years ago

Last modified 2 years ago

#12047 closed task (fixed)

Users Guide: GeneralizedNewtypeDeriving derives “instance Num Int => Num Dollars”

Reported by: Iceland_jack Owned by:
Priority: normal Milestone:
Component: Documentation Version: 7.10.3
Keywords: deriving Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Documentation bug Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D2273
Wiki Page:

Description (last modified by Iceland_jack)

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

Change History (8)

comment:1 Changed 3 years ago by Iceland_jack

Summary: Users Guide:Users Guide: GeneralizedNewtypeDeriving derives “instance Num Int => Num Dollars”

comment:2 Changed 3 years ago by Iceland_jack

Description: modified (diff)

comment:3 Changed 3 years ago by RyanGlScott

Component: CompilerDocumentation

That section isn't saying that newtype Dollars = Dollars Int deriving Num literally derives an instance of the form instance Num Int => Num Dollars, but rather it generates an instance Num Dollars by starting with Num Int as the context, then simplifying as much as possible. In that example, since a Num Int instance exists, we can discharge that constraint and be left with (), giving us the instance Num Dollars that you actually get when you compile with -ddump-deriv.

I agree that the wording in that section is a bit vague (it uses the phrase "Notationally" without going into much detail about what it really entails) and could stand to be clarified.

comment:4 in reply to:  3 Changed 3 years ago by simonpj

I agree that the wording in that section is a bit vague (it uses the phrase "Notationally" without going into much detail about what it really entails) and could stand to be clarified.

Agreed. Patch welcome!

Simon

comment:5 Changed 3 years ago by RyanGlScott

Differential Rev(s): Phab:D2273
Status: newpatch

comment:6 Changed 3 years ago by Ryan Scott <ryan.gl.scott@…>

In d0dd572b/ghc:

Clarify users' guide section on GeneralizedNewtypeDeriving

Summary:
It seemed to imply that GHC was generating infelicitous code when it
actually wasn't. Fixes #12047.

Reviewers: hvr, bgamari, austin

Reviewed By: austin

Subscribers: thomie, Iceland_jack

Differential Revision: https://phabricator.haskell.org/D2273

GHC Trac Issues: #12047

comment:7 Changed 3 years ago by RyanGlScott

Resolution: fixed
Status: patchclosed

comment:8 Changed 2 years ago by RyanGlScott

Keywords: deriving added
Note: See TracTickets for help on using tickets.