[comp.lang.ada] Operator Overloading Resolution

rds@moss.ATT.COM (08/05/88)

The following code segment was compiled using Telesoft 3.15.
According to LRM 6.6(3) "A call to an overloaded subprogram is
ambiguous (and therefore illegal) if...<the parameters>...are
not sufficient to determine exactly one (overloaded) subprogram
specification."

LRM 4.6(15) also states that a conversion can be applied
"if and only if the innermost complete context (see 8.7)
determines a unique (numeric) target type for the implicit
conversion." 


 WORD	: constant := 16;
 
 type INT_SCALAR is range -(2**15) .. (2**15)-1;
 for INT_SCALAR'size use WORD;
  
 left	: INTEGER range -127..128;
 result	: BOOLEAN;
 
 function "-"( right  : in INT_SCALAR) return INT_SCALAR is
     begin
 	return INT_SCALAR(-INTEGER( right ));
     end;
 
 begin
    result := left < -15;
                   -
>>>    Incompatible operand types or operator not visible <4.5 8.3 8.7>

--------------------------------------------------------------------
The expression -15 is evaluated to be of type INT_SCALAR.

Shouldn't this be an ERROR?

DECAda compiles this code without warning, implying the expression -15
is an INTEGER type.

My impression is NEITHER compiler follows the LRM.
Any comments on what is correct, or what other compilers do?

Richard DeSimine        (201) 386-2059
AT&T Bell Laboratories, Whippany, New Jersey 07054

wayne@inmet.UUCP (08/08/88)

> /* Written  1:59 pm  Aug  4, 1988 by rds@moss.ATT.COM in inmet:comp.lang.ada */
> /* ---------- "Operator Overloading Resolution" ---------- */
> The following code segment was compiled using Telesoft 3.15.
 
  ...
> The expression -15 is evaluated to be of type INT_SCALAR.

> Shouldn't this be an ERROR?

No. DEC-Ada is correct, Telesoft is in error.  The expression "15"
is of course of type UNIVERSAL_INTEGER.  According to LRM 4.10 and 
LRM 4.16:15 the subexpression "-15" is therefore also of universal
integer, regardless of a function definition.  It is then implicitly
converted to INTEGER (LRM 4.6:15) when evaluating the rest of the
expression because "left" is defined to be of INTEGER and a "<" is
available with two INTEGER parameters which returns BOOLEAN.

It can be assumed that if it possible for a subexpression to be of
type universal expression, it will be of type universal expression.
If you wish to use your definition of the negation operator, you
must qualifiy the 15 to be of type INT_SCALAR , such as in

     result := left < - INT_SCALAR'(15);

then it will be ambiguous, because there is no "<" defined to take
both an INTEGER and an INT_SCALAR.  Don't forget to initialize 
"left" or you may get an exception at run_time.     

Wayne Wylupski
Intermetrics, Inc.
Cambridge, MA

hmj@tut.fi (Matti J{rvinen) (08/09/88)

In article <30740@clyde.ATT.COM> rds@clyde.ATT.COM (Richard DeSimine) writes:
>    result := left < -15;
>--------------------------------------------------------------------
>The expression -15 is evaluated to be of type INT_SCALAR.
>
>Shouldn't this be an ERROR?

Yes, it is.

>DECAda compiles this code without warning, implying the expression -15
>is an INTEGER type.

No, it does NOT imply expression -15 to be integer. The expression is of
type universal_integer, which DECAda correctly transforms to the same type than
variable left.



-- 
Hannu-Matti Jarvinen, Tampere University of Technology, Finland
hmj@tut.fi, hmj@tut.uucp, hmj@tut.funet (tut.ARPA is not the same computer).

rds@moss.ATT.COM (08/10/88)

Hannu-Matti Jarvinen writes:

>>    result := left < -15;
>>--------------------------------------------------------------------
>>The expression -15 is evaluated to be of type INT_SCALAR.
>>
>>Shouldn't this be an ERROR?
>
>Yes, it is.
>
>>DECAda compiles this code without warning, implying the expression -15
>>is an INTEGER type.
>
>No, it does NOT imply expression -15 to be integer. The expression is of
>type universal_integer, which DECAda correctly transforms to the same type than
>variable left.
>
, Tampere University of Technology, Finland
>hmj@tut.fi, hmj@tut.uucp, hmj@tut.funet (tut.ARPA is not the same computer).

INTEGER or universal_integer, it it still ambiguous and in its context
could be construed as an INT_SCALAR or a universal_integer and is,
according to the LRM, illegal. Looking back at the original example,
where the unary operator "-" is overloaded, this would imply that
there is a precedence associated with a predefined and overloaded 
operators. If this is legal, then predefined operators have precedence
over programmer defined operators.

Consider the case where the variable "left" is of type INT_SCALAR and
not of type INTEGER. A compiler should evaluate the expression -15
before resolving the arguments passed to the relational operator "<".

The question really is: should the compiler evaluate the "-" operator
as the predefined unary "-" or the overloaded unary "-".  I say the
expression is ambiguous, and should be an error.

Richard DeSimine (201) 386-2059
AT&T Bell Laboratories, Whippany, New Jersey 07981