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

Test for List Applicative instance is ambigious #8

Open
mark-schultz-wu opened this issue May 13, 2018 · 1 comment
Open

Test for List Applicative instance is ambigious #8

mark-schultz-wu opened this issue May 13, 2018 · 1 comment

Comments

@mark-schultz-wu
Copy link

The test suite for Applicative.hs accepts the following code:

(<*>) (f :. fs) xs = (f <$> xs) ++ (fs <*> xs)
(<*>) _ _ = Nil

and rejects the following code:

(<*>) (f :. fs) (x :. xs) = (f x) :. (fs <*> xs)
(<*>) _ _ = Nil

I'm under the impression that these are both valid applicative instances (I think the second one is essentially the ZipList applicative instance, but I'm not actually sure).
If so, it might be useful to somehow alert the students that, while the second instance is valid, they should search for an entirely different instance (and not stare at that one and try to get it to work).

@parsonsmatt
Copy link
Collaborator

You are correct that the second instance is the ZipList Applicative. When a type has both a Monad and an Applicative instance, it's necessary for them to agree. That is, given ap,

ap :: Monad m => m (a -> b) -> m a -> m b
ap mf ma = do
  f <- mf
  a <- ma
  pure (f a)

then for any values, f `ap` a == f <*> a. Since ZipList does not have a Monad, we give the "cross-product-y" one to List itself.

Adding a comment to indicate two possible instances is a good idea :) Thanks

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