[Haskell-cafe] Unary functions and infix notation

John Lato jwlato at gmail.com
Fri Sep 6 17:43:01 CEST 2013


The observation that this only applies to functions with a polymorphic
return type is key.

  id :: a -> a

This can be instantiated at

  id' :: (a->b) -> (a->b)
  id' :: (a->b) -> a -> b    -- these are the same

What this means is that id is a function with arity-2 whenever the first
argument is arity-1, and generally id is a function of arity x+1 where x is
the argument arity.  Incidentally, this is exactly the same as the ($)
operator.

John L.


On Fri, Sep 6, 2013 at 10:04 AM, Johannes Emerich <johannes at emerich.de>wrote:

> 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
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130906/df2c9a10/attachment.htm>


More information about the Haskell-Cafe mailing list