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

Is Alternative constraint needed for: Applicative (Cofree f)? #161

Open
Icelandjack opened this issue Aug 24, 2017 · 1 comment
Open

Is Alternative constraint needed for: Applicative (Cofree f)? #161

Icelandjack opened this issue Aug 24, 2017 · 1 comment

Comments

@Icelandjack
Copy link

Icelandjack commented Aug 24, 2017

This rules out deriving the Applicative instance for Moore machines

instance Applicative (Moore a) where
  pure a = r where r = Moore a (const r)

  Moore f ff <*> Moore a fa  = Moore (f a) (\i -> ff i <*> fa i)

So instead of implementing

instance Alternative f => Applicative (Cofree f) where
  pure x = x :< empty

  (<*>) = ap

is this not a more natural definition?

instance Applicative f => Applicative (Cofree f) where
  pure x = r where
    r = x :< pure r

  (f :< ff) <*> (a :< fa) =
    f a :< liftA2 (<*>) ff fa
@Icelandjack
Copy link
Author

Icelandjack commented Aug 24, 2017

It would be a breaking change so it can be attached to a newtype, but it feels a lot more natural to me than the Alternative instance

  pure @(Cofree []) a
= a :< []

-- vs

  pure @(Cofree []) a
= a :< [a :< [a :< [a :< ...

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

1 participant