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
b
switched 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 ~ b
andy ~ (Int, b)
. This is the part you missed, likely due to having confusing naming: the second argument isf x
or[a] -> b
, and you pass inhead
, which is[a] -> a
. This forcesb
to become the same asa
, otherwise the types wouldn't work out. The result isf y
, or[a] -> (Int, b)
, exceptb
is nowa
, giving you the[a] -> (Int, a)
signature.