On Apr 21, 5:19 pm, Eric Hughes <eric....@[EMAIL PROTECTED]
> wrote:
> OK. Somehow I had missed the fact that Ada does overload resolution
> based on assignment context. So I read up on it in more detail.
> Pardon me if I've missed something in the foregoing.
>
> The overload resolution in the present example, though, doesn't seem
> unique. Could not "+" resolve to the "+" of root_integer? It seems
> to pass all of the requirements. To test this, I wrote the following
> three lines of code and ran them through GNAT:
> -- (Example A)
> Z0 : constant := Integer'Last - 1 ;
> type Z is range 0 .. Z0 ;
> B : constant Z := ( Z0 + 1 ) - 1 ;
> GNAT compiled it without a problem and it ran without error. The
> following program, however, did neither:
> -- (Example B)
> Z0 : constant := Integer'Last - 1 ;
> type Z is range 0 .. Z0 ;
> C : constant Z := Z0 + 1 ;
> GNAT gave a warning that a constraint error would be raised and indeed
> running it raised one. Now I raise the upper bound by one:
> -- (Example C)
> Z0 : constant := Integer'Last ;
> type Z is range 0 .. Z0 ;
> B : constant Z := ( Z0 + 1 ) - 1 ;
> GNAT compiles this one fine and runs fine, just like Example A.
> -- (Example D)
> Z0 : constant := Integer'Last ;
> type Z is range 0 .. Z0 ;
> C : constant Z := Z0 + 1 ;
> GNAT says "static expression fails Constraint_Check" and does not
> compile. Let's raise the upper bound one last time:
> -- (Example E)
> Z0 : constant := Integer'Last + 1 ;
> type Z is range 0 .. Z0 ;
> B : constant Z := ( Z0 + 1 ) - 1 ;
> Just fine, as usual.
> -- (Example F)
> Z0 : constant := Integer'Last + 1 ;
> type Z is range 0 .. Z0 ;
> C : constant Z := Z0 + 1 ;
> GNAT gives the same warning as example B.
>
> As an aside, GNAT is clearly doing something odd. Of the three
> declarations of "C", you have two warnings and one error, not the
> same. I'm not even sure that's a defect.
>
> The real point, though, is that it just can't be true that the
> addition and subtraction in examples A and C are those of type Z. If
> this were true, the expression
> ( Z0 + 1 ) - 1
> should translate to something like this:
> Z_subtract( Z_add( Z0, 1 ), 1 )
> If that were the case, then the Z_add expression should raise a
> constraint error. It doesn't. Maybe this is a GNAT defect. It seems
> just as likely that overload resolution is taking the addition and
> subtraction of root_integer.
See 4.9(33). I'm not sure it applies in this case, though, since the
root_integer operations will be preferred (see below). But note that
this compiles and will not cause Constraint_Error to be raised:
A1 : constant Integer := Integer'Last;
A2 : constant Integer := (A1 + 1) - 1;
> And then I had an inspiration:
> -- (Example G)
> Z0 : constant := Long_Long_Integer'Last ;
> type Z is range 0 .. Z0 ;
> B : constant Z := ( Z0 + 1 ) - 1 ;
> -- (Example H)
> Z0 : constant := Long_Long_Integer'Last + 1 ;
> type Z is range 0 .. Z0 ;
> B : constant Z := ( Z0 + 1 ) - 1 ;
> Now we've hit the GNAT implementation limit. Example H, finally,
> fails to compile because "integer type definition bounds out of
> range". (I don't like this message because it's not clear that an
> internal limit has been exceeded.) Example G, however, compiles and
> runs just fine. Example G shows that, at least in this example, it's
> doing arithmetic on universal_integer, because there's no other type
> internally that it could be using.
>
> > Choosing which function to call nondeterministically seems
> > like an Obviously Bad Idea, to me, so perhaps I don't understand
> > what you mean.
>
> All the above examples seem to indicate that GNAT, at least, is
> resolving overloaded addition operators. The overload is between the
> declared type, root_integer (or something like it), and possibly even
> universal_integer. With other types, ones where there is no universal
> type, this would be an illegal ambiguity. So it seems that GNAT is
> nondeterministically resolving them.
There's nothing nondeterministic about this. See 8.6(29).
I can't follow your argument so I have no idea whether these points
affect it or not. But you do seem to have made at least a couple
mistakes about the language.
-- Adam


|