[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Which direction should function arrows point?



At 6:49 PM -0500 3/31/03, Neel Krishnaswami wrote:
>Here's a critical, burning language design question. Why do people
>write function arrows from left to right (eg, "int -> string")
>rather than right to left ("string <- int")?
>
>The reason I ask is that composition is much more readable when you
>put the result on the left. Take the type of the compose function
>
>fun compose(f, g) {
>   fun(x) { f(g(x)) }
>}
>
>The conventional spelling of its type is:
>
>   (a -> b, c -> a) -> (c -> b)
>   ^^^          ^^^
>Notice how the 'a' expression is separated, so that you can no longer
>clearly see the flow of data.
>
>   (a -> b, c -> a) -> (c -> b)
>         ^^^^           ^^^^^^
>See also that the order of the types in the arguments is transposed
>from how they appears in the return value.
>
>Compare this with the right-to-left version:
>
>   (b <- c) <- (b <- a, a <- c)
>
>Both of those defects have been fixed in this version. The 'a's appear
>next to each other, so that they are easy to visually "cancel out",
>and the order of the types in the return value matches the order in
>the argument.
>
>However, I haven't seen this notation in any other programming
>language, and I wonder if there is a deeper reason for the standard
>convention that I have missed.

C, C++:

string (*foo)(int x) ?

When using higher-order types, there is a potential inconvenience 
with type parametrizers being first seen used and then defined, but 
that can be addressed in one of several ways.

     Waldemar