Skip to content

Commit e22a9ac

Browse files
authored
Merge pull request #445 from jhrcek/GHC-97044
Document GHC-97044
2 parents 96d8c9c + 250bf8f commit e22a9ac

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Main where
2+
3+
import Data.Coerce
4+
import Data.Type.Equality
5+
import Type.Reflection
6+
7+
newtype Foo = Foo { unFoo :: () }
8+
9+
main :: IO ()
10+
main = do
11+
let foo = Foo ()
12+
print (coerce foo:: ())
13+
print (typeOf foo)
14+
print (Refl :: Foo :~: Foo)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Main where
2+
3+
import Data.Coerce (Coercible, coerce)
4+
import Type.Reflection (Typeable(..))
5+
import Data.Type.Equality
6+
7+
newtype Foo = Foo { unFoo :: () }
8+
9+
instance Coercible Foo ()
10+
11+
instance Typeable Foo
12+
13+
instance Foo ~ Foo
14+
15+
main :: IO ()
16+
main = do
17+
let foo = Foo ()
18+
print (coerce foo:: ())
19+
print (typeOf foo)
20+
print (Refl :: Foo :~: Foo)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: Attempts to create hand-written instances for special type classes
3+
---
4+
5+
Since these type classes don't allow hand-written instances, you'll get the following errors if you try to implement them.
6+
To fix the errors, just remove the instance declarations - GHC will provide them for you automatically.
7+
8+
## Error Message
9+
```
10+
UserSpecifiedInstances.hs:9:10: error: [GHC-97044]
11+
• Class ‘Coercible’ does not support user-specified instances.
12+
• In the instance declaration for ‘Coercible Foo ()’
13+
|
14+
9 | instance Coercible Foo ()
15+
| ^^^^^^^^^^^^^^^^
16+
17+
UserSpecifiedInstances.hs:11:10: error: [GHC-97044]
18+
• Class ‘Typeable’ does not support user-specified instances.
19+
• In the instance declaration for ‘Typeable Foo’
20+
|
21+
11 | instance Typeable Foo
22+
| ^^^^^^^^^^^^
23+
24+
UserSpecifiedInstances.hs:13:10: error: [GHC-97044]
25+
• Class ‘~’ does not support user-specified instances.
26+
• In the instance declaration for ‘Foo ~ Foo’
27+
|
28+
13 | instance Foo ~ Foo
29+
| ^^^^^^^^^
30+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: User-specified instance is not allowed
3+
summary: Type class does not allow user-specified instances
4+
severity: error
5+
introduced: 9.6.1
6+
---
7+
8+
There are several special type classes in [base](https://hackage.haskell.org/package/base) which require special treatment from the compiler. For this reason they don't permit user-specified instances.
9+
10+
An attempt to provide an explicit instance declaration in your library / application code, e.g.
11+
```haskell
12+
instance Coercible A B where ...
13+
```
14+
will lead to this error being reported.
15+
16+
The instances for these type classes are automatically created by GHC on an as-needed basis.
17+
18+
This restriction applies to the following type classes:
19+
20+
- [Coercible](https://hackage.haskell.org/package/base/docs/Data-Coerce.html#t:Coercible) - This class does not have regular instances; instead they are created on-the-fly during type-checking.
21+
- [Typeable](https://hackage.haskell.org/package/base/docs/Type-Reflection.html#t:Typeable) - Since GHC 7.10, all types automatically have `Typeable` instances derived. This is in contrast to previous releases where Typeable had to be explicitly derived using the DeriveDataTypeable language extension.
22+
- Type equality classes `~` and `~~` defined in [Data.Type.Equality](https://hackage.haskell.org/package/base/docs/Data-Type-Equality.html)
23+
- [WithDict](https://hackage.haskell.org/package/base/docs/GHC-Exts.html#t:WithDict) - is used to create dictionaries for classes with a single method. It is used to implement a primitive that we cannot define in Haskell but we can write in Core. Some details can be found in a [Note in GHC library](https://hackage.haskell.org/package/ghc-9.6.1/docs/src/GHC.Tc.Instance.Class.html#line-493)
24+
- [KnownChar](https://hackage.haskell.org/package/base/docs/GHC-TypeLits.html#t:KnownChar) - This class gives the Char associated with a type-level Char literal. There are instances of the class for every concrete literal: 'a', 'b', 'c' etc. But instances for user-defined types are not permitted.
25+
- [KnownNat](https://hackage.haskell.org/package/base/docs/GHC-TypeNats.html#t:KnownNat) - This class gives the integer associated with a type-level natural. There are instances of the class for every concrete literal: 0, 1, 2, etc. But instances for user-defined types are not permitted.
26+
- [KnownSymbol](https://hackage.haskell.org/package/base/docs/GHC-TypeLits.html#t:KnownSymbol) - This class gives the string associated with a type-level symbol. There are instances of the class for every concrete literal: "hello", etc. But instances for user-defined types are not permitted.
27+
- [Generic](https://hackage.haskell.org/package/base/docs/GHC-Generics.html#t:Generic) - This class is one of the key pieces of `GHC.Generics` and you usually want to derive it using [DeriveGeneric](https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/generics.html#extension-DeriveGeneric) extension. Hand-written instances of Generic instances are allowed by default, but disallowed when [SafeHaskell](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/safe_haskell.html) extension is enabled.
28+

0 commit comments

Comments
 (0)