Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
GHC
GHC
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 4,326
    • Issues 4,326
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
    • Iterations
  • Merge Requests 390
    • Merge Requests 390
  • Requirements
    • Requirements
    • List
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
  • Security & Compliance
    • Security & Compliance
    • Dependency List
    • License Compliance
  • Operations
    • Operations
    • Incidents
    • Environments
  • Analytics
    • Analytics
    • CI / CD
    • Code Review
    • Insights
    • Issue
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Glasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #12047

Closed
Open
Opened May 12, 2016 by Icelandjack@IcelandjackReporter

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

Edited Mar 10, 2019 by Icelandjack
Assignee
Assign to
None
Milestone
None
Assign milestone
Time tracking
None
Due date
None
Reference: ghc/ghc#12047