Skip to content

Commit 324dd42

Browse files
committed
Add GHC-22705 - NonCanonicalMonad
1 parent cc1f169 commit 324dd42

File tree

7 files changed

+81
-0
lines changed

7 files changed

+81
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: Noncanonical ‘return’ definition detected
3+
summary: "'return', or '>>' not defined in terms of 'pure' or '*>'."
4+
severity: warning
5+
flag: -Wnoncanonical-monad-instances
6+
introduced: 9.6.1
7+
---
8+
9+
Since Applicative became a superclass of Monad, the 'return', and '>>' functions of law-abiding
10+
Monad instances are assumed to be identical to the 'pure' and '*>' functions.
11+
12+
This warning is caused by a definition of 'return' that isn't equal to 'pure', or a definition of
13+
'(>>)' that isn't equal to '(*>)'.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Return where
2+
3+
data P a = P
4+
5+
instance Functor P where
6+
fmap _ _ = P
7+
8+
instance Applicative P where
9+
pure _ = P
10+
_ <*> _ = P
11+
12+
instance Monad P where
13+
_ >>= _ = P
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Return where
2+
3+
data P a = P
4+
5+
instance Functor P where
6+
fmap _ _ = P
7+
8+
instance Applicative P where
9+
pure _ = P
10+
_ <*> _ = P
11+
12+
instance Monad P where
13+
_ >>= _ = P
14+
return _ = P
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: Non-canonical 'return' defined
3+
---
4+
5+
In this example, 'return' is defined in terms of 'P', which doesn't directly use Applicative's 'pure'.
6+
7+
To fix this, you can define 'return' as 'pure', or leave the definition empty, to use the default value.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Shortfish where
2+
3+
data P a = P
4+
5+
instance Functor P where
6+
fmap _ _ = P
7+
8+
instance Applicative P where
9+
pure _ = P
10+
_ <*> _ = P
11+
12+
instance Monad P where
13+
_ >>= _ = P
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Shortfish where
2+
3+
data P a = P
4+
5+
instance Functor P where
6+
fmap _ _ = P
7+
8+
instance Applicative P where
9+
pure _ = P
10+
_ <*> _ = P
11+
12+
instance Monad P where
13+
_ >>= _ = P
14+
_ >> _ = P
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: Non-canonical '>>' defined
3+
---
4+
5+
In this example, '>>' is defined in terms of 'P', which doesn't directly use Applicative's '*>'.
6+
7+
To fix this, you can define '>>' as '*>', or leave the definition empty, to use the default value.

0 commit comments

Comments
 (0)