User-Defined Operators

Andrew J Bromage ajb@spamcop.net
Fri, 18 Jul 2003 11:30:29 +1000


G'day all.

On Thu, Jul 17, 2003 at 05:21:47PM +0200, Christian Maeder wrote:

> Why do you outrule other useful libraries (see above). In fact ($) is 
> quite cryptic (for a non-Haskeller).

Actually this gives me a perfect opportunity to rant a bit. :-)

($) is a wart, even for a Haskeller.  It has the correct meaning and
the correct precedence, but it has the wrong associativity.  "Normal"
application is left-associative.  There's no reason why ($) shouldn't
be either.

Of course, sometimes you want right-associative apply, but there's
already a simple way to do this.  If you want to write:

	f (g (h (i x)))

you can use this:

	f . g . h . i $ x

But there is no way to write this parenthesis-free:

	f (g x) (h y) (i z)

Now it's arguable that this is clearer with the parentheses, and I
would agree with that.  However, consider the situation with ($!).

When you use strict-apply, you intend that one or more of the arguments
to some function is/are to be strictly evaluated.  Making ($!)
right-associative only gives you _exactly_ one, and it's always the
rightmost one, which gives it a 1-in-n chance of being right for a
function of n arguments.

In the above example, for instance:

	f (g x) (h y) (i z)

Suppose you want (h y) to be strictly evaluated.  I argue that some
variation on this:

	f (g x) $! (h y) $ (i z)

even with the parentheses is far more readable than the current
alternatives.

While it makes sense that ($!) should have the same associativity as
($), I can't for the life of me figure out why ($) is right-associative.

There's probably a terribly good reason.  Does anyone know what it is?

Cheers,
Andrew Bromage