Proposal: add isEmpty to Monoid

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Proposal: add isEmpty to Monoid

winter
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add isEmpty to Monoid

Henning Thielemann

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.
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add isEmpty to Monoid

Haskell - Libraries mailing list
In reply to this post by winter
The monoid-subclasses packages provides a MonoidNull class that you could use:


Am Sa., 25. Mai 2019 um 07:56 Uhr schrieb Dr.Koster <[hidden email]>:
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: add isEmpty to Monoid

winter
I see, Thanks!



------------------ Original ------------------
From: Simon Jakobi <[hidden email]>
Date: Sat,May 25,2019 2:45 PM
To: Dr.Koster <[hidden email]>
Cc: libraries <[hidden email]>
Subject: Re: Proposal: add isEmpty to Monoid

The monoid-subclasses packages provides a MonoidNull class that you could use:


Am Sa., 25. Mai 2019 um 07:56 Uhr schrieb Dr.Koster <[hidden email]>:
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries