Skip to content

Commit

Permalink
Merge pull request #425 from haskellfoundation/ghc-93557-ghc-54540
Browse files Browse the repository at this point in the history
Document GHC-54540 and GHC-93557
  • Loading branch information
BinderDavid authored Jul 9, 2023
2 parents 7e2013c + cc67979 commit 93b0256
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{-# LANGUAGE StandaloneDeriving #-}
module ConstructorsNotInScope where

import System.IO( Handle )

-- It is not possible to derive an instance for the type class in this module.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{-# LANGUAGE StandaloneDeriving #-}
module ConstructorsNotInScope where

import System.IO( Handle )

deriving instance Eq Handle
16 changes: 16 additions & 0 deletions message-index/messages/GHC-54540/constructorsNotInScope/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: Cannot derive an instance for an abstract type
---

The `Handle` type is abstract and does not export its constructors. It is therefore not possible to derive an instance of a typeclass for the `Handle` type in another module. You can only derive an instance of a typeclass in the module where `Handle` is defined.

````
messages/GHC-54540/constructorsNotInScope/before/ConstructorsNotInScope.hs:6:1: error: [GHC-54540]
• Can't make a derived instance of ‘Eq Handle’:
The data constructors of ‘Handle’ are not all in scope
so you cannot derive an instance for it
• In the stand-alone deriving instance for ‘Eq Handle’
|
6 | deriving instance Eq Handle
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
8 changes: 8 additions & 0 deletions message-index/messages/GHC-54540/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Cannot derive instance without constructors in scope
summary: It is not possible to derive a typeclass instance if the constructors of the type are not in scope
severity: error
introduced: 9.6.1
---

Deriving an instance of a typeclass for a type is only possible if the constructors of the type are in scope at the point where you want to derive the instance.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{-# LANGUAGE Haskell2010 #-}
module IllegalSynonymInstance where

data RGB = R | G | B

instance Eq RGB where
R == R = True
G == G = True
B == B = True
_ == _ = False
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{-# LANGUAGE Haskell2010 #-}
module IllegalSynonymInstance where

data RGB = R | G | B

type T = RGB

instance Eq T where
R == R = True
G == G = True
B == B = True
_ == _ = False
19 changes: 19 additions & 0 deletions message-index/messages/GHC-93557/illegalSynonymInstance/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: Cannot implement a typeclass for type synonyms by default
---

The programmer defined a data type `RGB`, and a type synonym `T`, and then tried to implement an instance of the type class `Eq` for `T`.
In the language version Haskell 2010 this is not allowed, since all type class instances have to start with a type constructor.
This problem can be fixed by implementing the typeclass instance directly for the type `RGB` instead of the type synonym `T`.

```
messages/GHC-93557/illegalSynonymInstance/before/IllegalSynonymInstance.hs:8:10: error: [GHC-93557]
• Illegal instance declaration for ‘Eq T’:
All instance types must be of the form (T t1 ... tn)
where T is not a synonym.
• In the instance declaration for ‘Eq T’
Suggested fix: Perhaps you intended to use TypeSynonymInstances
|
8 | instance Eq T where
| ^^^^
```
16 changes: 16 additions & 0 deletions message-index/messages/GHC-93557/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: Illegal typeclass instance
summary: Illegal typeclass instance
severity: error
introduced: 9.8.1
---

Using the language version Haskell 2010, only certain types are allowed as instances of a type class.
Every type class instance in Haskell 2010 has the following form:
```
instance C t where
````
Here, `C` is the name of a type class, such as `Eq`, `Show`, `Read` or `Functor`, and `t` is a type.
But only a subset of types `t` is allowed to appear in typeclass instances.
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.
For example, `Maybe a` is allowed as a type in an instance, because the data type `Maybe` instantiates `T` and `a` is a type variable`, but `Maybe Int` is not allowed, since `Int` is not a type variable.

0 comments on commit 93b0256

Please sign in to comment.