6 Sep 17:04 2013

## Unary functions and infix notation

Johannes Emerich <johannes <at> emerich.de>

2013-09-06 15:04:12 GMT

2013-09-06 15:04:12 GMT

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)(Continue reading)