comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Negative float problem
Date: 27 Oct 2005 10:48:24 -0400
Date: 2005-10-27T10:48:24-04:00	[thread overview]
Message-ID: <wccmzkvrp6v.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: NI-dnTOLqY64F_3eRVn-uQ@comcast.com

"Robert I. Eachus" <rieachus@comcast.net> writes:

> Robert A Duff wrote:
> 
> > But I suggest using the first solution.  Really, the "use type"
> > semantics ought to be turned on by default -- you shouldn't have to say
> > anything special to use the "-" operator.  After all, you don't have
> > to say anything special to use "1.0" or ":=" on this type!
> 
> There was an Ada83 AI on this, usually referred to as the "for I in
> 1..-10" problem.  (Yes the loop would be executed zero times, but the
> problem is that an Ada 83 compiler was required to reject the for
> statement.)

A more realistic example is "for I in -10 .. 10".  ;-)

But that's a completely separate issue from what the OP asked about.
The problem here is that Ada makes the type default to
Integer.  That's a kludge.  I would simply require the
user to write "for I in Integer range -10..10", if they
want Integer.  But you almost never want Integer -- you should
be declaring a new integer type to suit your purpose.
We've got a default that makes a poor coding practise easier!

And you almost never want -10..10 -- 'Range is far more common.

>...Tucker came up with some preference rules that handle some
> of the cases, but the case mentioned here is one of the more refractory
> ones.  It could be solved by having a negation symbol that could be part
> of a numeric literal.

Perhaps.  Would "- 10" and "-10" then have different semantics?!
                  ^
                  | That's a blank.

>...But as long as users are allowed to overload
> operator symbols, the visibility rules will have some edge cases like
> this one.
> 
> For example
> 
> package file1 is
> 
> 	type My_Float is digits 6;
>          function "-"(L: My_Float) return My_Float;
> end file1;
> 
> package body file1 is
>          function "-"(L: My_Float) return My_Float is
>          begin return L-1.0; end "-";
> end file1;
> 
> with file1; use file1;
> with Ada.Text_IO; use Ada.Text_IO;
> procedure file2 is
> 	X : file1.My_Float := -1.0;
>          function "-"(L: My_Float) return My_Float is
>          begin return L+1.0; end "-";
>          Y : file1.My_Float := -1.0;
> begin
>          if X /= Y then Put_Line("Correct"); end if;
> end file2;

The above example is a separate issue.  This anomaly would occur in
exactly the same way, if you erase the "use file1", and change the
language as I suggested (i.e. "use type" happens automatically).
And it would occur in exactly the same way if the "-" functions
were called Mumble instead of "-".

Anyway, the above example is extremely confusing and error prone.
If I ran the circus, both calls to "-" would be ambiguous,
and therefore illegal.  And the same would apply if they
were called Mumble.

And besides, the OP had no desire to overload any operators!
It's always annoying when somebody wants to do some simple
thing, and the answer from language lawyers is, "Well,
if you added an upside-down generic discriminated task aggregate
to the program, it would cause so-and-so anomaly."

> Now in one sense this is a pretty silly example.  But it is the way the
> visibility and overloading rules work in Ada for good reasons that have
> nothing to do with this example. ;-)  Obviously it is better to have one
> set of visibility rules for all subprograms than for programmers to try
> to remember which set of visibility rules apply in this case.

I agree that it would complicate the language to have different
visibility rules for operators than for other things.  But it would
make the language more friendly.  And the added complexity would
be _less_ than the complexity we caused when we added "use type"
to the language!

>...(Yes,
> there is a different set of rules for basic operations like numeric
> literals and assignment.  But since these are not overloadable, this
> doesn't cause a problem.)
> 
> Finally, there is actually a third way to make the negation operator
> from file1 visible.  But note that this is direct visibility not use
> visible:
>    function "-"(L: My_Float) return My_Float renames file1."-";

That one is error prone, too.  The problem here is that you don't want
to rename anything, so using "renames" is overkill.  The thing is still
called "-" -- you don't want to rename it, you want to import it with
the same name it always had.  Ada has no such feature.

The above sort of renaming was common for operators in Ada 83,
because there was no "use type".  I remember a bug that looked
something like this:

    function "and"(...) return ... renames "and";
    function "or"(...) return ... renames "or";
    function "xor"(...) return ... renames "and";

I was so annoyed by this bug that I still remember it many years later!

Here's another annoying example:

    while X /= null loop...

Null is visible everywhere, but I have to type in extra mumbo jumbo to
see "/=".

- Bob



  reply	other threads:[~2005-10-27 14:48 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-10-26 18:32 Negative float problem Luke
2005-10-26 19:05 ` Samuel Tardieu
2005-10-26 20:42 ` Robert A Duff
2005-10-27  8:05   ` Robert I. Eachus
2005-10-27 14:48     ` Robert A Duff [this message]
2005-10-27 15:07       ` Maciej Sobczak
2005-10-27 15:47         ` Robert A Duff
2005-10-28  8:34           ` Maciej Sobczak
2005-10-29 23:39             ` Brian May
2005-10-30  9:11             ` Dmitry A. Kazakov
2005-10-31  9:46               ` Maciej Sobczak
2005-10-31 14:20                 ` Dmitry A. Kazakov
2005-11-01 11:06                   ` Maciej Sobczak
2005-11-01 14:06                     ` Robert A Duff
2005-11-01 14:46                       ` Martin Dowie
2005-11-01 16:04                         ` Hyman Rosen
2005-11-01 17:19                           ` Martin Dowie
2005-11-02  0:13                         ` Robert A Duff
2005-11-02  6:59                           ` Martin Dowie
2005-11-02 13:24                             ` Robert A Duff
2005-11-02 15:22                               ` Martin Dowie
2005-11-01 15:12                       ` Maciej Sobczak
2005-11-02  0:28                         ` Robert A Duff
2005-11-02  4:16                           ` Steve Whalen
2005-11-14  7:26                           ` Dave Thompson
2005-11-20  0:19                             ` Robert A Duff
2005-11-20 11:07                               ` Dmitry A. Kazakov
2005-11-01 14:27                     ` Dmitry A. Kazakov
2005-11-01 15:19                       ` Maciej Sobczak
2005-11-01 19:44                         ` Dmitry A. Kazakov
2005-11-02  9:04                           ` Maciej Sobczak
2005-11-02 11:17                             ` Dmitry A. Kazakov
2005-11-02 13:03                               ` Maciej Sobczak
2005-11-02 14:20                                 ` Jean-Pierre Rosen
2005-11-02 20:15                                   ` Jeffrey R. Carter
2005-11-03 13:06                                     ` Jean-Pierre Rosen
2005-11-03 18:32                                       ` Jeffrey R. Carter
2005-11-03  9:51                                   ` Maciej Sobczak
2005-11-03 13:20                                     ` Jean-Pierre Rosen
2005-11-03 15:02                                       ` Maciej Sobczak
2005-11-03 18:55                                         ` Frank J. Lhota
2005-11-04  9:32                                           ` Maciej Sobczak
2005-11-03 20:59                                     ` Simon Wright
2005-11-02 13:32                               ` Robert A Duff
2005-11-02 14:44                                 ` Dmitry A. Kazakov
2005-11-02 13:47                               ` Dmitry A. Kazakov
2005-10-27 18:33       ` Dmitry A. Kazakov
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox