Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document GHC-54540 and GHC-93557 #425

Merged
merged 3 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
BinderDavid marked this conversation as resolved.
Show resolved Hide resolved
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.