I'm doing the exercises from Typeclassopedia; in the Applicative
section, I write ZipList
's pure
function, and check whether it follows the Applicative
Laws.
I've checked:
- identity law
- interchange law
- composition law
But when I try to check the "homomorphism" law, I find that GHCi doesn't get the result as an MZipList
.
I think this is because I miss to designate the pure
to my Applicative
type class. How can I run a pure
function without <*>
it to Applicative
immediately?
Here's the MZipList
definition and class instances:
newtype MZipList a = MZipList { getZipList :: [a] }
deriving (Show)
instance Functor MZipList where
fmap gs x = pure gs <*> x
instance Applicative MZipList where
pure a= MZipList (repeat a)
(MZipList gs) <*> (MZipList xs) = MZipList (zipWith ($) gs xs)
When I check the "Interchange" law, for example:
*Main> (MZipList [(*2),(*3),(*4)]) <*> pure (2)
MZipList {getZipList = [4,6,8]}
*Main> pure ($ 2) <*> (MZipList [(*2),(*3),(*4)])
MZipList {getZipList = [4,6,8]}
But when I check the "Homomorphism" law , the MZipList
's pure
is not called:
*Main> pure (*2) <*> pure 2
4
*Main> pure ((*2) 2)
4
*Main>
Why is that?