[erlang-questions] Infix function and user-defined operators

Richard O'Keefe <>
Sun Jan 20 13:25:56 CET 2019


It's ironic that you mention C++, because C++ is *the* example of
operator abuse.  I mean, *really*, using "<<" for both left shift
and output?  That's almost as evil as using "+" for string concatenation.
The problem with C++ is that it allows completely ad hoc overloading
BUT does not allow operator *introduction* so that there is enormous
pressure on the same few operators that happen to be built in.

The example of X^Y is an interesting one, because you don't, you really
don't, want to go around writing exp(Y*log(X)) all the time.  Not only
does it involve three operations, when only one is intended, but in
many cases they are the *wrong* operations.  (Notably, but not only,
when Y is an integer.)  The other thing is the notation itself.  There
is no such operator in high-school maths.  And the reason ^ is used is
that in ASCII-63 (as implemented on the Model 33 teletype, for one
popular example), that codepoint was used for ↑ so people were actually
writing X↑Y.  ASCII-67 replaced ↑ with ^.  And there were two operators
ASCII meant to give us that we seldom get.  The \ character was added
to ASCII for the specific purpose of letting you write /\ (and, min,
meet) and \/ (or, max, join).  How many programming languages let you
use _those_ very well established operators?

There are many things that might reasonably be taken as infix
operators:
   List includes Element
   Long begins_with Short
   X <=> Y                 -- increasingly popular 3-way comparison
(these examples were chosen to be at the same level as >=)
   Mat1 %*% Mat2           -- matrix multiplication in R
   Set1 union: Set2        -- set operation in Smalltalk
and of course assorted parser combinators and other combinators.
I count 28 operators in SML.  (Proper handling of IEEE comparison
needs more than six "relational operators", and SML has some but
not all of the extras.)

Here's an interesting question.
The designers of Ada thought that letting people mix up 'and' and 'or'
without parentheses was dangerous.  So you can have
   e0 and e1 and ... and en
and you can have
   e0 or e1 or ... or en
but you cannot have
   e0 and e1 or e2
So are 'and' and 'or' operators in Ada, or should they be thought of as
special multi-operator forms?  If the latter, how would you declare such
things?

My own impression, and it is no more than that,
is that infix operators help a lot when you are treating something
as a quasi-algebraic expression that you are interested in rearranging
and simplifying in a mathematical way.  I don't think it is a coincidence
that the Bird-Meertens approach does that, and that specification languages
often have lots of operators.  In particular, when you are transforming
expressions by hand, you can tolerate a fair bit of precedence fuzziness
and even outright ambiguity.
On the other hand, bulkier, clunkier, but more obvious syntax seems to
work better for code that you want other people to read.

Just an impression, for what it is worth.

>
>
>
>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190121/17af9465/attachment.html>


More information about the erlang-questions mailing list