Meaning of (\x y z -> x y z) in Haskell

Viewed 306

In understand the meaning and the type of (\x y z -> x+y+z) for example. The type of (\x y z -> x y z)is (t1 -> t2 -> t3) -> t1 -> t2 -> t3. But what does that mean and why are now round brackets in the type?

1 Answers

In Haskell, something of the form a b c (i.e., juxtaposition of letter-names without infix symbols between them) generally means a is a function and being applied to b and c.
In this case, x y z is basically what in other languages would be written x(y,z). In Haskell, you can also write it

\x y z -> x(y)(z)

or in fact (x)(y)(z) or ((x)y)z, but parentheses are discouraged unless needed.

So knowing that, it's clear that the type of the first argument of that lambda, i.e. of x, needs to be a function type. In this case, a function taking two arguments of whatever type y and z happen to be, respectively. I.e.

(\x y z -> x y z) :: (typeofy -> typeofz -> resulttype) -> ...
                     └───────── type of x ────────────┘

Now say y :: t1 and z :: t2 and the final result should be t3, and we have

(\x y z -> x y z) :: (t1 -> t2 -> t3) -> (t1 -> t2 -> t3)
                     └─ type of x ──┘     └y    └z    └(x y z)

Function-arrows are right-associative, i.e. the parentheses on the right side of the type can be omitted:

(\x y z -> x y z) :: (t1 -> t2 -> t3) -> t1 -> t2 -> t3
                     └─ type of x ──┘    └y    └z    └(x y z)

The parentheses on the left can not be omitted: t1->t2->t3 -> t1->t2->t3 would actually parse as t1->(t2->(t3->(t1->(t2->t3)))). That would be instead the type of

(\w x y w' x' -> y) :: t1 -> t2 -> t3 -> t1 -> t2 -> t3
                       └w    └x    └y    └w'   └x'   └y

...preferrably written (\_ _ y _ _ -> y).

Related