-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Semigroup and Monoid instances for GenT that lift the inner Monoid #156
Conversation
cc @chessai |
I needed this at work the other day, in the situation @andrewthad described. Also, thanks for doing this, @andrewthad. I meant to do so when I got over the flu. |
No problem. I figured that when you're running a giant fever, there are probably more pressing things on your mind. |
@@ -522,6 +524,16 @@ instance (MonadGen m, Monoid w) => MonadGen (Strict.RWST r w s m) where | |||
------------------------------------------------------------------------ | |||
-- GenT instances | |||
|
|||
-- technically, the Monoid constraint on @a@ could be weakened |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: technically should start with an upper-case T, so it looks sane in Haddock docs on Hackage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment does not currently show up in haddock docs, but I can make it show up there since that might actually be mildly useful information for users of the library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can just leave this unannotated after adopting George's feedback
-- technically, the Monoid constraint on @a@ could be weakened | ||
-- to Semigroup, but this would be annoying for older GHCs | ||
-- where Semigroup is not a superclass of Monoid. | ||
instance (Monad m, Monoid a) => Semigroup (GenT m a) where |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer that you weaken the constraint to Semigroup a
and delete the comment.
this would be annoying for older GHCs
Not if the user depends on the semigroups
package, as I've written about. It provides Semigroup
compatibility at least as far back as GHC 7.2.
On the flip-side, the current approach would be unhelpful to any user whose type is a Semigroup
but not a Monoid
.
I have made this change on my fork here if you'd like to use that patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, George's version looks right to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. I had originally thought this wouldn't work with pre-SMP base
, but it totally does. This is certainly the best option. I merged it into the PR.
Thanks for the PR! This seems like the most sensible monoid for GenT. Anyone object? |
I've merged @gwils changes into the PR. |
I've just had a look at the definition of I'm not sure whether that's a reasonable thing to have on Don't let this concern hold up the PR, but I thought I'd mention it. |
I would argue that this is always the most appropriate
But yeah, I agree that it would be better to discuss that on a separate PR. |
Tree is internal, so unless there's an internal motivation it's not important either way. This change looks fine as is.
Agree, the Alternative-based instance doesn't seem too compelling. Will wait for @jystic to weigh in before merging, but LGTM. Thanks for the contribution! Hope you're getting value out of Hedgehog. |
Does @jystic have any additional thoughts on this? |
Thanks again for the PR! |
@thumphries thank you |
This uses the technique suggested in Equational Reasoning at Scale to provide a
Monoid
instance forGenT
. This was motivated by more than just theoretical concerns. At work, I was recently in a situation where I needed to flatten a list of generators like this:With the instances provided by this PR,
flatten
is justmconcat
. I've written these instances so that they should be compatible with all phases Semigroup-Monoid-Proposal that are represented by GHC 8.4 and lower.