[GHC] #12047: Users Guide:

#12047: Users Guide: -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: task | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 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: -------------------------------------+------------------------------------- In [http://downloads.haskell.org/~ghc/8.0.1-rc4/docs/html/users_guide/glasgow_ex... #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 type signature is correct -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12047 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#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: | -------------------------------------+------------------------------------- -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12047#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#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_ex... #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

#12047: Users Guide: GeneralizedNewtypeDeriving derives “instance Num Int => Num Dollars” -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: task | Status: new Priority: normal | Milestone: Component: Documentation | 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: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * component: Compiler => Documentation Comment: 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. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12047#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

I agree that the wording in that section is a bit vague (it uses the
#12047: Users Guide: GeneralizedNewtypeDeriving derives “instance Num Int => Num Dollars” -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: task | Status: new Priority: normal | Milestone: Component: Documentation | 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: | -------------------------------------+------------------------------------- Comment (by simonpj): phrase "Notationally" without going into much detail about what it really entails) and could stand to be clarified. Agreed. Patch welcome! Simon -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12047#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12047: Users Guide: GeneralizedNewtypeDeriving derives “instance Num Int => Num Dollars” -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: task | Status: patch Priority: normal | Milestone: Component: Documentation | 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): Phab:D2273 Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * status: new => patch * differential: => Phab:D2273 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12047#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12047: Users Guide: GeneralizedNewtypeDeriving derives “instance Num Int => Num
Dollars”
-------------------------------------+-------------------------------------
Reporter: Iceland_jack | Owner:
Type: task | Status: patch
Priority: normal | Milestone:
Component: Documentation | 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): Phab:D2273
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by Ryan Scott

#12047: Users Guide: GeneralizedNewtypeDeriving derives “instance Num Int => Num Dollars” -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: task | Status: closed Priority: normal | Milestone: Component: Documentation | Version: 7.10.3 Resolution: fixed | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Documentation | Unknown/Multiple bug | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2273 Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * status: patch => closed * resolution: => fixed -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12047#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12047: Users Guide: GeneralizedNewtypeDeriving derives “instance Num Int => Num Dollars” -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: task | Status: closed Priority: normal | Milestone: Component: Documentation | Version: 7.10.3 Resolution: fixed | Keywords: deriving Operating System: Unknown/Multiple | Architecture: Type of failure: Documentation | Unknown/Multiple bug | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D2273 Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * keywords: => deriving -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12047#comment:8 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC