bitemyapp / fp-course

Fork of the original Data61 course to be more Stack friendly

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Test for List Applicative instance is ambigious

mark-schultz opened this issue · comments

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).

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