Proposal: add isEmpty to Monoid

It's often useful to have a generalized null/unit test, e.g. to check if a monadic stream `m a` reaches EOF. A workaround is to use `Eq a, Monoid a` constraint and use `== mempty` test. But this is no only inefficient for some type, but also impossible for some monoids, such as Builders. I propose add this test `isEmpty :: a -> Bool` to `Monoid`, to provide the ability to check if a value is the unit value. Cheers Han Dong~

On Sat, 25 May 2019, Dr.Koster wrote:
It's often useful to have a generalized null/unit test, e.g. to check if a monadic stream `m a` reaches EOF. A workaround is to use `Eq a, Monoid a` constraint and use `== mempty` test. But this is no only inefficient for some type, but also impossible for some monoids, such as Builders. I propose add this test `isEmpty :: a -> Bool` to `Monoid`, to provide the ability to check if a value is the unit value.
This will not work. You cannot always inspect what you have constructed via Monoid. E.g. you will not be able to implement isEmpty for the Endo monoid. The Eq constraint imposed by (== mempty) is really required. If you want isEmpty as method, you need a Monoid sub-class.

The monoid-subclasses packages provides a MonoidNull class that you could
use:
http://hackage.haskell.org/package/monoid-subclasses-0.4.6.1/docs/Data-Monoi...
Am Sa., 25. Mai 2019 um 07:56 Uhr schrieb Dr.Koster
It's often useful to have a generalized null/unit test, e.g. to check if a monadic stream `m a` reaches EOF. A workaround is to use `Eq a, Monoid a` constraint and use `== mempty` test. But this is no only inefficient for some type, but also impossible for some monoids, such as Builders.
I propose add this test `isEmpty :: a -> Bool` to `Monoid`, to provide the ability to check if a value is the unit value.
Cheers Han Dong~ _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

I see, Thanks!
发自我的iPhone
------------------ Original ------------------
From: Simon Jakobi
participants (3)
-
Dr.Koster
-
Henning Thielemann
-
Simon Jakobi