Skip to content

Commit 415e77c

Browse files
authored
Add noncanonical mappend (#497)
* Add noncanonical mappend * Add noncanonical mappend error message
1 parent e26b665 commit 415e77c

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Noncanonical `mappend` definition
3+
summary: A Monoid instance uses a custom `mappend` implementation rather than re-using the default `(<>)` operation from its Semigroup instance.
4+
severity: warning
5+
flag: -Wnoncanonical-monoid-instances
6+
introduced: 9.8.1
7+
---
8+
9+
As part of the Semigroup Monoid proposal, the Semigroup class is now a
10+
superclass of Monoid. Now, the `mappend` function is redundant and should
11+
always do exactly the same as the `(<>)` function of the Semigroup instance.
12+
In the future, the `mappend` function may even be removed completely.
13+
14+
So, you should adapt to these changes by either:
15+
16+
* Changing the definition of `mappend` to use `(<>)`. This is backwards compatible, but may break in the future.
17+
* Removing the definition of `mappend` and rely on the default instance which uses `(<>)`. This is forwards compatible, but breaks your code for GHC 8.2 or earlier.
18+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module NoncanonicalMappend where
2+
3+
data A = A
4+
5+
instance Semigroup A where
6+
A <> A = A
7+
8+
instance Monoid A where
9+
mempty = A
10+
mappend = (<>)
11+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module NoncanonicalMappend where
2+
3+
data A = A
4+
5+
instance Semigroup A where
6+
A <> A = A
7+
8+
instance Monoid A where
9+
mempty = A
10+
mappend A A = A
11+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Noncanonical `mappend` definition.
3+
---
4+
5+
## Error Message
6+
7+
```
8+
NoncanonicalMappend.hs:10:3: warning: [GHC-50928] [-Wnoncanonical-monoid-instances]
9+
Noncanonical ‘mappend’ definition detected
10+
in the instance declaration for ‘Monoid A’.
11+
‘mappend’ will eventually be removed in favour of ‘(<>)’
12+
Suggested fix:
13+
Either remove definition for ‘mappend’ (recommended) or define as ‘mappend = (<>)’
14+
See also: https://gitlab.haskell.org/ghc/ghc/-/wikis/proposal/semigroup-monoid
15+
|
16+
10 | mappend A A = A
17+
| ^^^^^^^^^^^^^^^
18+
```

0 commit comments

Comments
 (0)