-4

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
duplode
  • 33,731
  • 7
  • 79
  • 150
beHappy
  • 15
  • 3
  • 2
    Could you please be more precise about what you don't understand? Are you okay with folding, `Functor` and `Applicative` already? It is hard to know how much one would need to write in order to answer your question in its current form. – duplode Oct 28 '16 at 10:57
  • yes, i kown a little about Functor and Applicative. how can (:) <$> f a <*> b ::f [b]? (:) :: a->[a]->[a], (<$>):: a->b->f a->f b, should (:) <$> be f (a->[a])->f [a]? – beHappy Oct 29 '16 at 06:22
  • You should edit your question to add what you just said to it. That way, it will be much less likely that it ends up ignored, or closed, for being unclear or too broad. – duplode Oct 29 '16 at 12:27
  • Note that the five downvotes you got would have been entirely avoidable, if only you had explained clearly what you wanted to ask in your original question. Keep that in mind for future questions. – duplode Oct 31 '16 at 13:39
  • Thank you very much – beHappy Oct 31 '16 at 15:01

1 Answers1

0

what's wrong with the following derivation?

The problem is with this passage:

(:) <$> f :: ((->) a) ([a] -> [a])

Function application always has precedence over any operator, and so (:) <$> f a is not ((:) <$> f) a, as in your derivation, but (:) <$> (f a). The rest follows smoothly (you may want to try finishing it yourself before reading the solution below):

(:) <$>     :: F a -> F ([a] -> [a]) -- The third line of your derivation.
f           :: a -> A b
f a         :: A b
(:) <$> f a :: A ([b] -> [b])
(<*>)             :: A (a -> b) -> A a -> A b
(:) <$> f a       :: A ([b] -> [b])
(:) <$> f a <*>   :: A [b] -> A [b]
b                 :: A [b]
(:) <$> f a <*> b :: A [b]
duplode
  • 33,731
  • 7
  • 79
  • 150