Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use unary function instead of binary in `flip`?

The type of the Prelude function flip is:

flip :: (a -> b -> c) -> b -> a -> c

I.e., it takes one binary function and two arguments.

The type of the Prelude function id is:

id :: a -> a

But the type of flip id is:

flip id :: a -> (a -> b) -> b

How is it possible to apply flip to id when id is a unary function and flip requires binary function for the first arg?

btw. flip id is similar to \ x f -> f x

like image 443
Tom Pažourek Avatar asked Nov 11 '09 14:11

Tom Pažourek


2 Answers

Haskell makes id fit the type of the first argument to flip by setting a = b -> c. So:

flip :: ( a       -> b -> c) -> b ->  a       -> c
flip :: ((b -> c) -> b -> c) -> b -> (b -> c) -> c
flip id ::                      b -> (b -> c) -> c

where id is taken to be of type

id :: (b -> c) ->  b -> c

which is equivalent to

id :: (b -> c) -> (b -> c)

i.e. a specialisation of id that only applies to unary functions.

Edit: I think I might rephrase my first line as:
Haskell deduces that id fits the type of the first argument to flip if a = b -> c.
In case that's any clearer.

like image 120
Nefrubyr Avatar answered Dec 19 '22 11:12

Nefrubyr


Nefrubyr explains it very well.
Another way to (hopefully) make this a bit more intuitive is to think of the function application operator ($).

($) is a specialized form of id:

($) :: (a -> b) -> (a -> b)
($) = id

I've seen the definition (#) = flip ($), such that you can write the argument before the function its applied to: obj # show.

Obviously, since ($) is just a specialized form of id, you could also write: (#) = flip id

like image 26
Tom Lokhorst Avatar answered Dec 19 '22 11:12

Tom Lokhorst