[Haskell-cafe] Unary functions and infix notation
Johannes Emerich
johannes at emerich.de
Fri Sep 6 17:04:12 CEST 2013
As is well known, any binary function f can be turned into an infix operator by surrounding it with backticks:
f a b -- prefix application
a `f` b -- infix application
It is then possible to take left and right sections, i.e. partially applying f:
(a `f`) -- equivalent to \b -> a `f` b
(`f` b) -- equivalent to \a -> a `f` b
This extends relatively naturally to functions of arity greater than two, where usage of a function in infix notation produces a binary operator that returns a function of arity n-2.
Weirdly, however, infix notation can also be used for unary functions with polymorphic types, as the following ghci session shows:
Prelude> :t (`id` 1)
(`id` 1) :: Num a => (a -> t) -> t
Prelude> (`id` 1) (\y -> show y ++ ".what")
"1.what"
Desugaring of an equivalent source file shows that id is applied to the anonymous function, which is then applied to 1.
The following example of a function that is not polymorphic in its return type behaves closer to what I would have expected: It does not work.
Prelude> let z = (\y -> True) :: a -> Bool
Prelude> :t (`z` True)
<interactive>:1:2:
The operator `z' takes two arguments,
but its type `a0 -> Bool' has only one
In the expression: (`z` True)
What is the purpose/reason for this behaviour?
Thank you,
--Johannes
More information about the Haskell-Cafe
mailing list