home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / arch / 10885 < prev    next >
Encoding:
Internet Message Format  |  1992-11-18  |  4.0 KB

  1. Xref: sparky comp.arch:10885 comp.lang.misc:3767
  2. Newsgroups: comp.arch,comp.lang.misc
  3. Path: sparky!uunet!charon.amdahl.com!pacbell.com!ames!saimiri.primate.wisc.edu!caen!hellgate.utah.edu!lanl!cochiti.lanl.gov!jlg
  4. From: jlg@cochiti.lanl.gov (J. Giles)
  5. Subject: Re: adding new operators on the fly
  6. Message-ID: <1992Nov18.212323.15324@newshost.lanl.gov>
  7. Sender: news@newshost.lanl.gov
  8. Organization: Los Alamos National Laboratory
  9. References: <1992Nov13.155126.3660@linus.mitre.org> <722061125@sheol.UUCP>
  10. Date: Wed, 18 Nov 1992 21:23:23 GMT
  11. Lines: 81
  12.  
  13.  
  14. First: an operator is shorthand for a function call.  So, issues of
  15. type compatibility are settled the same way those issues are handled
  16. by your function protocols.  If these are inadequate, your language
  17. needs work even if you *don't* allow user defined operators.  Further,
  18. this means that operators are expanded inline only if you have such
  19. a mechanism defined for function calls.  The operator's function
  20. can be written in assembly only if your ordinary functions can be.
  21. Etc..  To have an assembly function expanded inline requires that
  22. you have some assembler/processor which makes assembly functions
  23. into intermediate code which is suitable for you compiler to use
  24. (or, you define pseudo-assembly language for each target machine).
  25. This is not all that hard - but it's unconventional - so there's
  26. probably a lot of nay-sayers out there who'll claim it can't be done.
  27.  
  28. Second: the declaration of an operator should state a) what the operator
  29. looks like (what sequence of characters are to be recognized as the
  30. operator); b) what the associativity of the operator is (defaults to
  31. non-associative); c) what the commutativity of the operator is (defaults
  32. to non-commutative); d) what the precedence of the operator is (if the
  33. language allows the user to define precedence - I personally think all
  34. user defined operators should have the same precedence); e) what the
  35. fixity of the operator is: infix, prefix, or postfix (the number of
  36. arguments is decided by the fixity); f) and finally, the name of the
  37. function which performs the operation.
  38.  
  39. Note: the associativity choices are:
  40.  
  41.     Not associative - that is "a @ b @ c" is not even allowed without 
  42.        parenthesis
  43.  
  44.     left associative - "a @ b @ c" is the same as "(a @ b) @ c"
  45.  
  46.     right associative - "a @ b @ c" is the same as "a @ (b @ c)"
  47.  
  48.     associative - the order is left to the compiler's discretion
  49.  
  50.     anti-associative - "(a @ b) @ c" is the same as "-(a @ (b @ c))"
  51.  
  52. Commutativity choices are:
  53.  
  54.     Not commutative - "a @ b" does not equal "b @ a"
  55.  
  56.     commutative - "a @ b" equals "b @ a"
  57.  
  58.     anti-commutative - "a @ b" equals "-(b @ a)"
  59.  
  60. The commutativity choices are handy for declaring overloaded math 
  61. operators.  For example, the mixed mode complex and real multiply
  62. can be defined as (in a pseudo-fortran syntax):
  63.  
  64.       associative commutative infix operator `*' is
  65.       complex function cfmult(complex::a, real::b)
  66.          return complex(real(a)*b, imag(a)*b)
  67.       end cfmult
  68.  
  69. Since the operator is declared commutative, this definition handles
  70. the mixed-mode operation even when the real part goes first.  You don't
  71. have to declare additional functions for each possible order of the
  72. operands.  This is particularly useful with union types:
  73.  
  74.       type scalar_number is union(real, integer)
  75.  
  76.       associative commutative infix operator `*' is
  77.       complex function cfmult(complex::a, scalar_number::b)
  78.          return complex(real(a)*b, imag(a)*b)
  79.       end cfmult
  80.  
  81. This defines mixed-mode multiply for complex with *either* real
  82. or integer numbers in either order of the operands appearing.
  83. In many languages, this would require four separate functions
  84. to be defined which differ only in their declarations.
  85.  
  86. The declaration that `*' is associative means that the compiler
  87. is free to choose evaluation order (if parenthesis are absent).
  88. So, "x * cz * y" where `x' and `y' are real and `cz' is complex
  89. might be done as (using both associativity and commutativity)
  90. "(x * y) * cz" with some possible speedup.
  91.  
  92. -- 
  93. J. Giles
  94.