[comp.lang.modula2] Defining Operators

BOTCHAIR@UOGUELPH.BITNET (Alex Bewley) (09/30/89)

    I was struck with an urge to think just the other day and came up with the
  following observation.  I was flipping through some Ada books in the library
  ('cause they outnumber Modula-2 books about 20 to 1) and Ada seems to have a
  feature where you can define an operator for non-standard types.  For example
  suppose you want to add two complex numbers.  You have to:

       Result := AddComplex(A, B);

    Which detracts from structure readability (say what?).  Well, what I mean
  is that, for example, when adding CARDINALs you just have:

       Result := A + B;

    Which (algebraically) reads exactly as you mean it.  In Ada you can
  define the '+' operator to be able to handle the Complex number type.

    Would such a feature be warranted in Modula-2?

    Here is some theoretical code:

(* necessary IMPORTs *)

TYPE
  Complex;     (* abstract type *)

OPERATORTYPE
    "+"  : AddComplex(Complex, Complex) : Complex; (* procedures called *)
    "-"  : SubComplex(Complex, Complex) : Complex; (* when operators used *)
    ":=" : DefineComplex(REAL, REAL) : Complex;
VAR
  Result : Complex;
  A, B   : Complex;

BEGIN
  A := 2.0, 2.0;
  B := 4.0, 4.0;
  Result := A + B;
END;

    The compiler or translator could substitute the necessary procedure
  and make sure that the correct types are being used.
    Thus, the compilable code would look like:

BEGIN
  A := DefineComplex(2.0, 2.0);
  B := DefineComplex(4.0, 4.0);
  Result := AddComplex(A, B);
END;

    This kind of operator definability could be used to simplify reading
  of bit shifting operations.  For example:

    EquipList := (BIOSResult SHL 5) XOR OtherBIOSResult;

    Rather than:

    EquipList := Shl(BIOSResult, 5);
    EquipList := Xor(EquipList, OtherBIOSResult);


    Something to think on...

    Alex
    Just this guy...

sludwig@ics.uci.edu (Stefan Ludwig) (09/30/89)

In article <INFO-M2%89093000360807@UCF1VM> Modula2 List <INFO-M2%UCF1VM.BITNET@PSUVM.PSU.EDU> writes:
[stuff deleted]
>    Which (algebraically) reads exactly as you mean it.  In Ada you can
>  define the '+' operator to be able to handle the Complex number type.
>
>    Would such a feature be warranted in Modula-2?
>
>    Here is some theoretical code:
[stuff deleted]
>    The compiler or translator could substitute the necessary procedure
>  and make sure that the correct types are being used.
>    Thus, the compilable code would look like:
[stuff deleted]
I recently suggested operator overloading to Prof. Wirth. His argument is:
the possibility you gain for coding programs does not justify the added
complexity to the compiler. This guy is running for 'the smallest compiler
in the world'-contest. His compilers are *really* small and *fast*.
The Oberon compiler for the Ceres (NS32000 based) is just 39KB object code!
It sure would be a nice feature to have, especially when you have object-
oriented features (like in Object-Oberon). We're thinking about adding that
to our Object-Oberon compiler...We'll see, just stay tuned.
>
>    Alex
>    Just this guy...

	Stefan

Stefan HM Ludwig	"Don't flame my english, it's what
			 they teach us in Swiss schools"

E-MAIL:		sludwig@ics.uci.edu		until October 21st
		ludwig@badile.iis.ethz.ch	later

pepers@cpsc.ucalgary.ca (Bradley Pepers) (10/01/89)

Oberon sounds really interesting. I missed anything talking in detail about
it so where can I find information on it? And also on Object-Oberon.

   Brad Pepers

toma@tekgvs.LABS.TEK.COM (Tom Almy) (10/02/89)

In article <1989Sep30.050044.12442@paris.ics.uci.edu> Stefan Ludwig <sludwig@ics.uci.edu> writes:
>This guy [Wirth] is running for 'the smallest compiler
>in the world'-contest. His compilers are *really* small and *fast*.
>The Oberon compiler for the Ceres (NS32000 based) is just 39KB object code!

Well he should give up on winning the Smallest Compiler contest and instead
go for the "Most Useful Language" contest. He'll never win smallest compiler
since typical Forth language compilers are about 1k byte. As long as the
compiler fits on *my* machine *I* don't care how big or small it is as long
as it saves me programming/debugging time!

Tom Almy
toma@tekgvs.labs.tek.com
Standard Disclaimers Apply

treese@crltrx.crl.dec.com (Win Treese) (10/06/89)

In article <INFO-M2%89093000360807@UCF1VM> Modula2 List <INFO-M2%UCF1VM.BITNET@PSUVM.PSU.EDU> writes:
>
>    I was struck with an urge to think just the other day and came up with the
>  following observation.  I was flipping through some Ada books in the library
>  ('cause they outnumber Modula-2 books about 20 to 1) and Ada seems to have a
>  feature where you can define an operator for non-standard types.  For example
>  suppose you want to add two complex numbers.  You have to:

It's worth noting that CLU does this (and has been around for a long time).
The compiler can do it automatically:

	a + b

is simply syntactic sugar for

	typeof(a)$add(a, b)

The types of a and b must match, of course.

3 + 5 is just int$add(3, 5).

Win Treese						Cambridge Research Lab
treese@crl.dec.com					Digital Equipment Corp.