Add voidM to Control.Monad

Hi all, I would like to propose adding the following function to Control.Monad to complement the `void` function: voidM :: Monad m => m a -> m () voidM m = m >> return () This function would be used to prevent warnings like Warning: A do-notation statement discarded a result of type Int. Suppress this warning by saying "_ <- bar", or by using the flag -fno-warn-unused-do-bind The (rather contrived) code below was used to generate the warning above: foo :: IO () foo = do bar return () bar :: IO Int bar = return 1 Using `voidM`, we can rewrite `foo` as such: foo' :: IO () foo' = do voidM bar return () after which no warning will be generated by GHC. Jurriën

voidM :: Monad m => m a -> m () voidM m = m >> return ()
Is voidM meant to be used with instance of Monad that are no monads, and hence have no Functor instance (like HtmlM[1])? Cheers, Simon [1] http://hackage.haskell.org/packages/archive/blaze-html/0.4.3.1/doc/html/Text...

2012/1/16 Simon Hengel
voidM :: Monad m => m a -> m () voidM m = m >> return ()
Is voidM meant to be used with instance of Monad that are no monads, and hence have no Functor instance (like HtmlM[1])?
You could easily implement a Functor instance for this type, since it would mirror the instance for `Const a` [2], i.e. only change the type parameter, and otherwise be the identity function. Erik [1] http://hackage.haskell.org/packages/archive/blaze-html/0.4.3.1/doc/html/Text... [2] http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-App...

It is indeed intended for monads that have no Functor instance. While one could argue that every monad should have a Functor instance, this is not the case in practice. If, on the other hand, Monad was defined as class Functor m => Monad m then all would be fine and we would be able to use the current implementation for `void`. A Functor constraint on Monad is an entirely different discussion though. Jurriën On 16 Jan 2012, at 16:22, Erik Hesselink wrote:
2012/1/16 Simon Hengel
: voidM :: Monad m => m a -> m () voidM m = m >> return ()
Is voidM meant to be used with instance of Monad that are no monads, and hence have no Functor instance (like HtmlM[1])?
You could easily implement a Functor instance for this type, since it would mirror the instance for `Const a` [2], i.e. only change the type parameter, and otherwise be the identity function.
Erik
[1] http://hackage.haskell.org/packages/archive/blaze-html/0.4.3.1/doc/html/Text... [2] http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-App...

2012/1/16 Jurriën Stutterheim
It is indeed intended for monads that have no Functor instance. While one could argue that every monad should have a Functor instance, this is not the case in practice. If, on the other hand, Monad was defined as
class Functor m => Monad m
then all would be fine and we would be able to use the current implementation for `void`. A Functor constraint on Monad is an entirely different discussion though.
We would be better off adding Functor instances for the Monads in question. Then readers wouldn't have to wonder about the difference between coid and voidM and why one was chosen over the other in a particular context &c. Antoine
Jurriën
On 16 Jan 2012, at 16:22, Erik Hesselink wrote:
2012/1/16 Simon Hengel
: voidM :: Monad m => m a -> m () voidM m = m >> return ()
Is voidM meant to be used with instance of Monad that are no monads, and hence have no Functor instance (like HtmlM[1])?
You could easily implement a Functor instance for this type, since it would mirror the instance for `Const a` [2], i.e. only change the type parameter, and otherwise be the identity function.
Erik
[1] http://hackage.haskell.org/packages/archive/blaze-html/0.4.3.1/doc/html/Text... [2] http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-App...
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries

On Mon, Jan 16, 2012 at 10:05:25AM -0600, Antoine Latter wrote:
2012/1/16 Jurriën Stutterheim
: It is indeed intended for monads that have no Functor instance. While one could argue that every monad should have a Functor instance, this is not the case in practice. If, on the other hand, Monad was defined as
class Functor m => Monad m
then all would be fine and we would be able to use the current implementation for `void`. A Functor constraint on Monad is an entirely different discussion though.
We would be better off adding Functor instances for the Monads in question.
IIRC, Functor rather than Monad was chosen for void's constraint as all Monads could/should also have Functor instances. The ticket was http://hackage.haskell.org/trac/ghc/ticket/3292 and it links to the discussion. Thanks Ian

We would be better off adding Functor instances for the Monads in question.
In general I agree. In the particular case of HtmlM I'm not sure if that would make things better.** Cheers, Simon ** I'm not arguing for voidM here. I, personally, would just use (_ <- foo) or -fno-warn-unused-do-bind in that particular case.

voidM :: Monad m => m a -> m () voidM m = m >> return ()
Is voidM meant to be used with instance of Monad that are no monads, and hence have no Functor instance (like HtmlM[1])?
You could easily implement a Functor instance for this type, since it would mirror the instance for `Const a` [2], i.e. only change the type parameter, and otherwise be the identity function.
Yes, you are right. As long as we do not use bind we are fine. Cheers, Simon

Jurriën Stutterheim schrieb:
Hi all,
I would like to propose adding the following function to Control.Monad to complement the `void` function:
voidM :: Monad m => m a -> m () voidM m = m >> return ()
In the case of the IO monad, the already existing 'void' function would work, too, since IO is also in Functor class.
participants (6)
-
Antoine Latter
-
Erik Hesselink
-
Henning Thielemann
-
Ian Lynagh
-
Jurriën Stutterheim
-
Simon Hengel