Skip to content

Commit 77fccb1

Browse files
GHC-81995: add error message and example using sequence (#526)
* GHC-81995: add error message and example using `sequence` * [GHC-81995] add pure example with list monad * Add error message output to examples --------- Co-authored-by: David Binder <[email protected]>
1 parent dc7a43c commit 77fccb1

File tree

7 files changed

+74
-0
lines changed

7 files changed

+74
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Unused "do" bind
3+
summary: A return value of a monadic action is ignored
4+
severity: warning
5+
flag: -Wunused-do-bind
6+
introduced: 9.6.1.
7+
---
8+
9+
Many monadic actions perform side effects before returning a value.
10+
11+
Sometimes, we are only interested in the side effect of a monadic action, and
12+
not in the return value. In these cases, we should use appropriate functions
13+
indicating that we purposefully ignore the return value.
14+
15+
Even more, in monadic code without side effects such as the list monad, this
16+
warning may indicate a bug.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module List where
2+
3+
list :: [Int]
4+
-- list = do return 2
5+
list = [2]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module List where
2+
3+
-- Evaluates to [2].
4+
list :: [Int]
5+
list = do
6+
return 1 -- This action has no side effects and is completely ignored.
7+
return 2
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: Action without side effect is completely ignored in pure monadic code
3+
---
4+
5+
# Error message
6+
7+
```
8+
list/before/List.hs:6:3: warning: [GHC-81995] [-Wunused-do-bind]
9+
A do-notation statement discarded a result of type ‘Integer’
10+
Suggested fix: Suppress this warning by saying ‘_ <- return 1’
11+
|
12+
6 | return 1 -- This action has no side effects and is completely ignored.
13+
| ^^^^^^^^
14+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module GetLine where
2+
3+
main :: IO ()
4+
main = do
5+
sequence_ [putStrLn "First line", putStrLn "Second line"]
6+
putStrLn "Done."
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module GetLine where
2+
3+
main :: IO ()
4+
main = do
5+
sequence [putStrLn "First line", putStrLn "Second line"]
6+
putStrLn "Done."
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: Fix warning about unused "do" bind using `sequence_`
3+
---
4+
5+
For example, `sequence :: (Traversable t, Monad m) => t (m a) -> m (t a)`
6+
performs all actions in a traversable container and returns the container with
7+
the return values. In a similar manner, `sequence_` performs all actions but
8+
indicates that the we want to ignore the return value.
9+
10+
# Error message
11+
```
12+
sequence/before/GetLine.hs:5:3: warning: [GHC-81995] [-Wunused-do-bind]
13+
A do-notation statement discarded a result of type ‘[()]’
14+
Suggested fix:
15+
Suppress this warning by saying
16+
‘_ <- sequence [putStrLn "First line", putStrLn "Second line"]’
17+
|
18+
5 | sequence [putStrLn "First line", putStrLn "Second line"]
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
```

0 commit comments

Comments
 (0)