I'm having trouble to understand the following example from the NICTA course:
instance Traversable List where
traverse ::
Applicative f =>
(a -> f b)
-> List a
-> f (List b)
traverse f =
foldRight (\a b -> (:.) <$> f a <*> b) (pure Nil)
In what follows, I will replace List
with []
, so we have:
instance Traversable [] where
traverse ::
Applicative f =>
(a -> f b)
-> [a]
-> f ([b])
traverse f =
foldRight (\a b -> (:) <$> f a <*> b) []
In particular, what's wrong with the following derivation of the type of (<*>)
?
(:) :: a -> [a] -> [a]
(<$>) :: a -> b -> F a -> F b, F for Functor
(therefore...)
(:) <$> :: F a -> F ([a] -> [a])
f :: a -> A b :: ((->) a) (A b), A for Applicative
(therefore...)
(:) <$> f :: ((->) a) ([a] -> [a])
(:) <$> f a :: [a] -> [a]
(<*>) :: A (a -> b) -> A a -> A b