Another type of fu hitch

trying to understand how the final signature is displayed:

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')should be unified with f (a -> b), so fit probably has 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 anyone explain how to get the final type?

Thank.

+5
source share
1 answer

The problem you are facing is associated with proper associativity ->. Consider the type <*>:

<*> :: (Applicative f) => f (a -> b) -> f a -> f b

With f aequal r -> a, we have

<*> :: f (a -> b) -> f a -> f b
    :: (r -> (a -> b)) -> (r -> a) -> (r -> b)
    :: (r -> a -> b) -> (r -> a) -> (r -> b) -- This is the key line

Note that it went from (r -> (a -> b)) -> other stuffto (r -> a -> b) -> other stuff, not r -> (a -> b) -> other stuff. We can remove the internal brackets since they are to the right of the arrow, but we cannot remove the external brackets because they are to the left of the arrow.

(+) :: (Num a) => a -> a -> a. <*>, r a, b, . ,

(<*>) (+) :: (Num a) => (a -> a) -> (a -> a)
          :: (Num a) => (a -> a) -> a -> a

, , .

+12

All Articles