Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This example seems to break the type sig for $, and it works [duplicate]

If the type for ($) is (a -> b) -> a -> b, then why are you allowed to curry it as ($2)? 2 is not of type (a -> b). See below example.

map ($2)[(+1),(+2)]

This is legit, awesome and intuitively makes sense. Please tell me how it is consistent with the type system rules?

Cheers

like image 705
TheIronKnuckle Avatar asked Dec 21 '22 03:12

TheIronKnuckle


1 Answers

The behavior you are observing is due to how partial application works for infix operators. This is often called "section application" and you are applying 2 as the "right section" which would be the second argument. So you have:

($) :: (a -> b) -> a -> b
                   ^
                   |
                  This is the type variable for the argument '2'

And you can confirm this via:

ghci
> :t ($2)
($2) :: Num a => (a -> b) -> b

You can likely find this info hidden somewhere in most decently complete tutorials or you can see the Haskell report section on sections.

like image 78
Thomas M. DuBuisson Avatar answered Mar 09 '23 00:03

Thomas M. DuBuisson