Skip to content

Commit f1d773f

Browse files
author
David Binder
committed
Document GHC-93557
1 parent aaf7fb2 commit f1d773f

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
module IllegalSynonymInstance where
3+
4+
data RGB = R | G | B
5+
6+
instance Eq RGB where
7+
R == R = True
8+
G == G = True
9+
B == B = True
10+
_ == _ = False
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
module IllegalSynonymInstance where
3+
4+
data RGB = R | G | B
5+
6+
type T = RGB
7+
8+
instance Eq T where
9+
R == R = True
10+
G == G = True
11+
B == B = True
12+
_ == _ = False
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Cannot implement a typeclass for type synonyms by default
3+
---
4+
5+
The programmer defined a data type `RGB`, and a type synonym `T`, and then tried to implement an instance of the typeclass `Eq` for `T`.
6+
In the language version Haskell2010 this is not allowed, since all typeclass instances have to start with a type constructor.
7+
This problem can be fixed by implementing the typeclass instance directly for the type `RGB` instead of the type synonym `T`.
8+
9+
```
10+
messages/GHC-93557/illegalSynonymInstance/before/IllegalSynonymInstance.hs:8:10: error: [GHC-93557]
11+
• Illegal instance declaration for ‘Eq T’:
12+
All instance types must be of the form (T t1 ... tn)
13+
where T is not a synonym.
14+
• In the instance declaration for ‘Eq T’
15+
Suggested fix: Perhaps you intended to use TypeSynonymInstances
16+
|
17+
8 | instance Eq T where
18+
| ^^^^
19+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Illegal typeclass instance
3+
summary: Illegal typeclass instance
4+
severity: error
5+
introduced: 9.8.1
6+
---
7+
8+
Using the language version Haskell2010, only certain types are allowed as instances of a typeclass.
9+
Every typeclass instance in Haskell2010 has the following form:
10+
```
11+
instance C t where
12+
````
13+
Here, `C` is the name of a typeclass, such as `Eq`, `Show` or `Read`, and `t` is a type.
14+
But only a subset of types `t` is allowed to appear in typeclass instances.
15+
These types must all have the form `T` or `T a ... b`, where `T` has to be the name of a type introduced by a data or newtype declaration, and the arguments `a` to `b` all have to be type variables.

0 commit comments

Comments
 (0)