trying to understand how is the final signature inferred:
GHCi> :t (+)
(+) :: Num a => a -> a -> a
GHCi> :t (<*>)
(<*>) :: Applicative f => f (a -> b) -> f a -> f b
GHCi> :t (<*>) (+)
(<*>) (+) :: Num b => (b -> b) -> b -> b
(a' -> a' -> a') has to be unified with f (a -> b), so f is probably of type ((->) r):
(<*>) :: Applicative ((->) r) => r -> (a -> b) -> (r -> a) -> (r -> b)
(<*>) (+) ~ a' -> (a' -> a') -> (a' -> a') -> (a' -> a')
(<*>) (+) :: (a' -> a') -> (a' -> a') -- ^^^ got stuck here
Can anybody explain how to get the final type ?
Thanks.
The problem you are having is with the right associativity of
->. Consider the type of<*>:With
f aequal tor -> a, we haveNotice that it went from
(r -> (a -> b)) -> other stuffto(r -> a -> b) -> other stuff, notr -> (a -> b) -> other stuff. We can remove the inner parentheses because they are on the right of an arrow, but we cannot remove the outer parentheses, as they are on the left of an arrow.Now,
(+) :: (Num a) => a -> a -> a. This fits perfectly with the first argument of<*>whenris the same asais the same asband they all are numbers. All together, we get thatNote again that I am removing parentheses on the right, but not on the left of the arrow.