instance {Semigroup, Monoid} (Bag a) ?

Ben Gamari ben at
Thu Apr 15 00:19:00 UTC 2021

Richard Eisenberg <rae at> writes:

> Hi devs,
> In the work on simplifying the error-message infrastructure (heavy lifting by Alfredo, in cc), I've been tempted (twice!) to add
>> instance Semigroup (Bag a) where
>>   (<>) = unionBags
>> instance Monoid (Bag a) where
>>   mempty = emptyBag
> to GHC.Data.Bag.
> The downside to writing these is that users might be tempted to write
> e.g. mempty instead of emptyBag, while the latter gives more
> information to readers and induces less manual type inference (to a
> human reader). The upside is that it means Bags work well with
> Monoid-oriented functions, like foldMap.
> I favor adding them, and slipped them into !5509 (a big commit with
> lots of other stuff). Alfredo rightly wondered whether this decision
> deserved more scrutiny, and so I'm asking the question here.
My sense is that adding the instances is a Good Thing. However, I do
think that we probably ought to refrain from using (<>) and mempty where
more specific functions would do. Adding a lint would be one way to
accomplish this. Hiding the functions from GhcPrelude would be another.


- Ben

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: not available
URL: <>

More information about the ghc-devs mailing list