Skip to content

Commit c503cf9

Browse files
VeryMilkyJoeBodigrim
authored andcommitted
Document GHC-72771 "Mismatched default method signature"
1 parent 8821db3 commit c503cf9

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{-# LANGUAGE DefaultSignatures #-}
2+
{-# LANGUAGE TypeOperators #-}
3+
4+
module Lib where
5+
6+
import GHC.Generics
7+
8+
class Enumerate a where
9+
enumerate :: [a]
10+
default enumerate :: (Int ~ a) => [a]
11+
enumerate = [1 ..]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{-# LANGUAGE DefaultSignatures #-}
2+
{-# LANGUAGE TypeOperators #-}
3+
4+
module Lib where
5+
6+
import GHC.Generics
7+
8+
class Enumerate a where
9+
enumerate :: [a]
10+
default enumerate :: (Int ~ a) => a
11+
enumerate = [1 ..]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Default type signature does not match
3+
---
4+
5+
## Error Message
6+
```
7+
Lib.hs:10:11: error: [GHC-72771]
8+
• The default type signature for enumerate: (Int ~ a) => a
9+
does not match its corresponding non-default type signature
10+
• When checking the class method:
11+
enumerate :: forall a. Enumerate a => [a]
12+
In the class declaration for ‘Enumerate’
13+
|
14+
10 | default enumerate :: (Int ~ a) => a
15+
| ^^^^^^^^^
16+
```
17+
18+
## Explanation
19+
20+
Here, we have defined an `Enumerate` typeclass over some type `a`, its instance `enumerate` simply has the signature `[a]`.
21+
At the same time, we define a default method for `enumerate`, which is applied in case the `a` is of type `Int`.
22+
But, this default method has the type signature `a`, which is not equivalent to `[a]`, therefore this error is thrown.
23+
24+
To resolve this error, we need to define the default method signature to match the method signature.
25+
Therefore, we define the default method signature to produce `[a]` instead of `a`.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: Mismatched default method signature
3+
summary: The provided default method implementation's type signature does not match the corresponding method type signature.
4+
severity: error
5+
introduced: 9.8.1
6+
---
7+
8+
This error means that there is a type class with some method `m` for which a [default signature](https://downloads.haskell.org/ghc/9.12.1/docs/users_guide/exts/default_signatures.html) was defined, but that default signature does not match the type signature of `m`.

0 commit comments

Comments
 (0)