GHC issueshttps://gitlab.haskell.org/ghc/ghc/-/issues2019-07-07T18:03:39Zhttps://gitlab.haskell.org/ghc/ghc/-/issues/15637Ambiguous type variables in GeneralisedNewtypeDeriving2019-07-07T18:03:39Zi-am-tomAmbiguous type variables in GeneralisedNewtypeDerivingWhen deriving the `C` instance in the following code:
```hs
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
module Test where
class C a where f :: String
instance C () where f = "foo"
newtype T = T ()...When deriving the `C` instance in the following code:
```hs
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
module Test where
class C a where f :: String
instance C () where f = "foo"
newtype T = T () deriving C
```
The following error occurs:
```
Test.hs:10:27: error:
• Ambiguous type variable ‘a0’ arising from a use of ‘f’
prevents the constraint ‘(C a0)’ from being solved.
Probable fix: use a type annotation to specify what ‘a0’ should be.
These potential instances exist:
instance C T -- Defined at Test.hs:10:27
instance C () -- Defined at Test.hs:8:10
• In the third argument of ‘GHC.Prim.coerce’, namely ‘f’
In the expression: GHC.Prim.coerce @String @String f :: String
In an equation for ‘f’:
f = GHC.Prim.coerce @String @String f :: String
When typechecking the code for ‘f’
in a derived instance for ‘C T’:
To see the code I am typechecking, use -ddump-deriv
|
10 | newtype T = T () deriving C
| ^
```
1. .. and the following core is produced:
```
==================== Derived instances ====================
Derived class instances:
instance Test.C Test.T where
Test.f
= GHC.Prim.coerce @GHC.Base.String @GHC.Base.String Test.f ::
GHC.Base.String
Derived type family instances:
```
The problem seems to be that the `a` should have been set to `()` within the coerced instance. I've been working round this with a `newtype X a = X String` as the result value so that the `a` is present in the signature, but I think this is a bug; should a more specialised instance be generated? I hope this is enough of an explanation!
Thanks,
Tom
<details><summary>Trac metadata</summary>
| Trac field | Value |
| ---------------------- | ------------ |
| Version | 8.4.3 |
| Type | Bug |
| TypeOfFailure | OtherFailure |
| Priority | normal |
| Resolution | Unresolved |
| Component | Compiler |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | |
| Operating system | |
| Architecture | |
</details>
<!-- {"blocked_by":[],"summary":"Ambiguous type variables in GeneralisedNewtypeDeriving","status":"New","operating_system":"","component":"Compiler","related":[],"milestone":"8.6.1","resolution":"Unresolved","owner":{"tag":"Unowned"},"version":"8.4.3","keywords":["GeneralisedNewtypeDeriving,","GeneralizedNewtypeDeriving"],"differentials":[],"test_case":"","architecture":"","cc":[""],"type":"Bug","description":"When deriving the `C` instance in the following code:\r\n\r\n{{{#!hs\r\n{-# LANGUAGE AllowAmbiguousTypes #-}\r\n{-# LANGUAGE GeneralisedNewtypeDeriving #-}\r\nmodule Test where\r\n\r\nclass C a where f :: String\r\n\r\ninstance C () where f = \"foo\"\r\n\r\nnewtype T = T () deriving C\r\n}}}\r\n\r\nThe following error occurs:\r\n\r\n{{{\r\nTest.hs:10:27: error:\r\n • Ambiguous type variable ‘a0’ arising from a use of ‘f’\r\n prevents the constraint ‘(C a0)’ from being solved.\r\n Probable fix: use a type annotation to specify what ‘a0’ should be.\r\n These potential instances exist:\r\n instance C T -- Defined at Test.hs:10:27\r\n instance C () -- Defined at Test.hs:8:10\r\n • In the third argument of ‘GHC.Prim.coerce’, namely ‘f’\r\n In the expression: GHC.Prim.coerce @String @String f :: String\r\n In an equation for ‘f’:\r\n f = GHC.Prim.coerce @String @String f :: String\r\n When typechecking the code for ‘f’\r\n in a derived instance for ‘C T’:\r\n To see the code I am typechecking, use -ddump-deriv\r\n |\r\n10 | newtype T = T () deriving C\r\n | ^\r\n}}}\r\n\r\n... and the following core is produced:\r\n\r\n{{{\r\n==================== Derived instances ====================\r\nDerived class instances:\r\n instance Test.C Test.T where\r\n Test.f\r\n = GHC.Prim.coerce @GHC.Base.String @GHC.Base.String Test.f ::\r\n GHC.Base.String\r\n\r\n\r\nDerived type family instances:\r\n}}}\r\n\r\nThe problem seems to be that the `a` should have been set to `()` within the coerced instance. I've been working round this with a `newtype X a = X String` as the result value so that the `a` is present in the signature, but I think this is a bug; should a more specialised instance be generated? I hope this is enough of an explanation!\r\n\r\nThanks,\r\nTom","type_of_failure":"OtherFailure","blocking":[]} -->8.8.1