Unary operators, was Re: Definable operators

Tony Finch <fanf@lspace.org>
22 Apr 1997 21:15:57 -0400

          From comp.compilers

Related articles
Problems with Hardware, Languages, and Compilers hrubin@stat.purdue.edu (1997-03-07)
Re: Definable operators dlester@cs.man.ac.uk (1997-04-16)
Re: Definable operators burley@tweedledumb.cygnus.com (Craig Burley) (1997-04-18)
Re: Definable operators genew@vip.net (1997-04-20)
Unary operators, was Re: Definable operators fanf@lspace.org (Tony Finch) (1997-04-22)
Re: Unary operators, was Re: Definable operators mfinney@lynchburg.net (1997-05-04)
| List of all articles for this month |
From: Tony Finch <fanf@lspace.org>
Newsgroups: comp.compilers
Date: 22 Apr 1997 21:15:57 -0400
Organization: Coll: SS. et Indiv: Trin: Cant:
References: 97-03-037 97-04-095 97-04-113 97-04-134
Keywords: syntax, design, C, algol68

genew@vip.net (Gene Wirchenko) wrote:
>
> While the discussion is on the evils of overloading operators,
>how about dealing with unary negation? It's horribly evil: with the
>"-" operator possibly being unary or binary depending on context, the
>potential confusion is intolerable. How this could lead to the
>downfall of civilization is left as an exercise to the advanced
>reader.


The extent of the problem really depends on how flexible you want your
parser to be. There's no real problem at the human end, as experience
with C shows. When you allow user defined operators it starts to get
hairy.


Algol 68 has very complete provision for user defined operators:
operators can be spelt with letters or symbols; they can be monadic or
dyadic with a specified priority; and they can be overloaded,
including allowing the same spelling for a monadic and dyadic operator
in the same scope.


Contrast this with a more modern language: Haskell also allows user
defined operators, but the spellings are distinguished. Operators
spelt with letters are prefix and are called functions; operators
spelt with punctuation are infix (with definable associativity as well
as precedence, something that Algol 68 lacks). These rtles may be
reversed with a sort of stropping: I can write (plus a b) or (a `plus`
b), or (a + b) or ((+) a b). Haskell deals with unary minus by adding
rules to the syntax especially for it -- grotty but pragmatic.


Other languages only allow a symbol to refer do one thing, and their
solution to the ambiguity is to write unary minus with a different
character (e.g. Unix's dc uses _).


I don't really see a big problem, so long as the restrictions imposed
by the language designers are sensible and understandable, and the
programmers don't go overboard with their freedom and define prefix
plus to convert its argument to an IP address and launch Netscape.


Tony.
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.