I have this pair of functions
(,) <$> length :: Foldable t => t a -> b -> (Int, b)
and,
head :: [a] -> a
I would like to understand the type of
(,) <$> length <*> head
In (<*>) :: Applicative f => f (a -> b) -> f a -> f b type signature,
f :: (->) [a]
a :: b
b :: (Int -> b)
So, the instantiated type would be:
(->) [a] (Int, b)
However, I found out really its type is:
(->) [a] (Int, a)
Two questions, if I may:
- Why is the
bswitched for ana? - What's the step by step process in this type signature calculation ?
Let's keep using the signature
But change
to
so it doesn't get confusing. Clearly
f ~ (->) [a](assuming we're using the list instance of foldable) as you noticed, and thusx -> y ~ b -> (Int, b), sox ~ bandy ~ (Int, b). This is the part you missed, likely due to having confusing naming: the second argument isf xor[a] -> b, and you pass inhead, which is[a] -> a. This forcesbto become the same asa, otherwise the types wouldn't work out. The result isf y, or[a] -> (Int, b), exceptbis nowa, giving you the[a] -> (Int, a)signature.