Open Icelandjack opened 7 years ago
Alternative instance based on Alternative
instance Functor f => Monoid (Free f a) where
mempty = empty
mappend = (<|>)
mconcat as = from (as >>= to)
where
to (Plus xs) = xs
to x = [x]
from [x] = x
from xs = Plus xs
It's worth pointing out that in your first post, you gave instances for Cofree
and in the second one you gave instances for Free
. I think the Cofree
instances are clearly correct. In fact, I had just come here to request them as you have written them. (I haven't thought as much about the Free
instance). @ElvishJerricco or @ekmett, would a PR be accepted for the Cofree
instances?
Is there a use for those instances (and similar
Free
instances)?These instances
correspond to
while sadly
Semigroup a => Semigroup (Cofree Maybe a)
does not correspond toSemigroup (NonEmpty a)
.