Skip to content
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

instance {Monoid,Semigroup} ({,Co}Free f a) #152

Open
Icelandjack opened this issue May 17, 2017 · 2 comments
Open

instance {Monoid,Semigroup} ({,Co}Free f a) #152

Icelandjack opened this issue May 17, 2017 · 2 comments

Comments

@Icelandjack
Copy link

Is there a use for those instances (and similar Free instances)?

instance (Semigroup a, Semigroup (f (Cofree f a))) => Semigroup (Cofree f a) where
  (a :< as) <> (b :< bs) = (a <> b) :< (as <> bs)

instance (Monoid a, Monoid (f (Cofree f a))) => Monoid (Cofree f a) where
  mempty = mempty :< mempty

  (a :< as) `mappend` (b :< bs) = (a `mappend` b) :< (as `mappend` bs)

These instances

instance (Monoid a, Monoid b) => Monoid (Cofree (Const a) b)
instance (Monoid a)           => Monoid (Cofree Identity  a)

correspond to

instance (Monoid a, Monoid b) => Monoid (a, b)
instance Monoid a             => Monoid (Stream a)
--                                  ^
-- https://github.com/Frege/frege/blob/master/frege/data/Stream.fr#L84

while sadly Semigroup a => Semigroup (Cofree Maybe a) does not correspond to Semigroup (NonEmpty a).

@Icelandjack
Copy link
Author

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

@andrewthad
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants