comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Surprise in array concatenation
Date: 06 Sep 2005 11:30:07 -0400
Date: 2005-09-06T11:30:07-04:00	[thread overview]
Message-ID: <wccpsrm5i4g.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: dfk57g$urs$1@f04n12.cac.psu.edu

"Bob Spooner" <rls19@psu.edu> writes:

> "Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message
> news:wccoe77nq3l.fsf@shell01.TheWorld.com...
> > tmoran@acm.org writes:
> >
> snip...
> >
> > Slices should slide to the lower bound.  The Ada rule breaks
> > abstraction:
> 
> So if I pass a subprogram a slice of an array indexed by an enumeration type
> then the mapping of the values to the enumeration type should change?

No.  Let me explain more clearly what I meant:

In Ada, for a given array type, you can choose to fix both bounds, or
neither.  You cannot choose to fix the lower bound (for all objects of
the type), but not the upper bound.  I think the programmer should have
all four choices of fixing the bounds for an array type.

Part of the problem here is that "array" is a fairly low-level
abstraction.  When the index is an enumeration type, you're usually
using the array to represent a higher-level concept -- a "mapping" from
enum values to whatever.  When you declare something like String, on the
other hand, you're using the array to represent a different higher-level
concept -- a "sequence of characters".

"Sequence" and "mapping" are different (though related) concepts.

For a sequence, the indices have no inherent meaning -- they just
represent the order in which the elements appear in the sequence.
So I don't have trouble with the idea that slices of strings should
slide the bounds to 1..Length, whereas slices of a "mapping" sort of
array should not.

> >
> >     procedure P(X: String) is
> >     begin
> >         ...
> >     end P;
> >
> >     Y: String := "Hello, world!";
> >
> >     P(Y(3..4));
> >
> > Inside the body of P, X is just a String -- we don't (or shouldn't) know
> > that it's a substring of Y.  So we can't possibly make any sense (inside
> > P) of the fact that X'First = 3.  Index 3 from what?
> >
> > If I ran the circus, X'First would be 1.
> >
> 
> But that would make String a special case of an array if you allow other
> arrays to start with a value other than the first value of the index type.
> Yet another thing we would have to remember...

Yes, you have to remember, for each array type, whether the programmer
who wrote it intended it as a sequence or a mapping.  If a sequence, you
have to remember whether they fixed the lower bound at zero or one (or
they did some other weird thing, presumably for good reason).

> >
> > I wouldn't insist on starting _all_ arrays at 1, but I think it makes
> > sense for _many_ arrays, including String.
> >
> 
> Many other special cases to remember as well?

Yes, String is just one example where it makes sense to fix the lower
bound at 1.  Just like for a linked list, you have to remember whether
it's circular or null-terminated or ....

> snip...
> >
> > - Bob
> 
> In my view, the abstraction that is important here is that of the array
> rather than that of the subprogram seeing a slice as always beginning with
> Index'first. The characteristics of the abstraction of an array should be as
> consistent in Ada as possible.

As consistent as possible, but no more so.  ;-)

Enumerations and integers have much in common (that's why they're both
part of the class of discrete types).  But there are some important
differences, and I don't think you can get away with pretending they're
the same thing.

We should avoid _gratuitous_ inconsistencies.

>... While having the first value of a String and
> whatever types of arrays you think are appropriate always be the first value
> of the index type would save some memory and computation time,...

It would also reduce the number of bugs, I think.  (In particular, it
would prevent the confusion shown by the original example at the start
of this thread.)

We know how to write code that correctly handles all Strings, including
those with bounds 100..101, but Ada should be designed to prevent
accidental errors, even in the presence of fallible programmers.

>... Ada is one of
> the few languages where the array is a powerful and safe abstraction. It is
> therefore much more useful than in other languages, making consistency of
> behavior all the more important and, I think, worth the price in
> computation. To make arrays robust and safe in C type languages,...

I'm comparing Ada to an Ada-like language that is slightly different
(and, I claim, slightly better).  I'm not comparing it to C.  Ada arrays
are obviously far superior to C arrays in many ways!

>... for
> instance, involves adding a lot more baggage than an extra integer value and
> a little bit of computation done transparently by the compiler.

It's not always transparent.  For example, Dmitry pointed out the
example of a procedure that takes two arrays, and wants to perform some
operation on pairs of elements from each.  If you can assume that the
two arrays have equal lower bounds, the source code becomes much simpler
(and therefore less error prone).

I'll grant you that you must then remember which array types allow that
assumption.  But that's already an issue, since you can fix _both_
bounds in Ada (and you have to remember you did that).  For example:

    type Char_Mapping is array(Character) of Character;

    procedure P(X, Y: Character_Mapping) is
    begin
        for I in Character loop -- or, equivalently, Char_Mapping'Range
            Do_Something(X(I), Y(I));

I claim that's perfectly reasonable code to write -- yet it would be
wrong if Char_Mapping had not fixed the bounds (that is, if it said
"range <>").

- Bob



  reply	other threads:[~2005-09-06 15:30 UTC|newest]

Thread overview: 108+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-01  3:16 Surprise in array concatenation Gene
2005-09-01  7:55 ` Dmitry A. Kazakov
2005-09-01  8:02   ` Florian Weimer
2005-09-01 11:48     ` Georg Bauhaus
2005-09-01 12:02       ` Lutz Donnerhacke
2005-09-01 13:01         ` Georg Bauhaus
2005-09-01 15:54       ` Florian Weimer
2005-09-01 16:09     ` Robert A Duff
2005-09-05  8:38       ` Jean-Pierre Rosen
2005-09-05 23:52         ` Robert A Duff
2005-09-06  9:03           ` Jean-Pierre Rosen
2005-09-07 17:57         ` adaworks
2005-09-07 20:01           ` Robert A Duff
2005-09-08  8:08             ` Jacob Sparre Andersen
2005-09-07 22:46           ` Jeffrey Carter
2005-09-08  4:43             ` Simon Wright
2005-09-08 10:36               ` Georg Bauhaus
2005-09-08 13:47                 ` Ed Falis
2005-09-08 17:03                   ` Pascal Obry
2005-09-08 16:45               ` Jeffrey Carter
2005-09-08 19:37                 ` Simon Wright
2005-09-08  6:32             ` adaworks
2005-09-08  9:09               ` Jean-Pierre Rosen
2005-09-08 16:56               ` Jeffrey Carter
2005-09-09 14:04                 ` Bob Spooner
2005-09-09 16:17                 ` adaworks
2005-09-23 23:04               ` Randy Brukardt
2005-09-14  8:57           ` Ole-Hjalmar Kristensen
2005-09-23 23:09           ` Randy Brukardt
2005-09-24 10:49             ` Larry Kilgallen
2005-09-24 20:27             ` Lurker
2005-09-25  0:20             ` Robert A Duff
2005-09-25 17:05             ` adaworks
2005-09-01 11:42   ` Georg Bauhaus
2005-09-01 13:59     ` Dmitry A. Kazakov
2005-09-01 15:36       ` Georg Bauhaus
2005-09-01 18:34         ` Dmitry A. Kazakov
2005-09-02 10:43           ` Georg Bauhaus
2005-09-02 13:11             ` Dmitry A. Kazakov
2005-09-02 14:23               ` Georg Bauhaus
2005-09-02 19:48                 ` Dmitry A. Kazakov
2005-09-02 17:21           ` Björn Persson
2005-09-01 16:04   ` Robert A Duff
2005-09-01 18:06     ` Dmitry A. Kazakov
2005-09-02 10:42       ` Georg Bauhaus
2005-09-02 13:20         ` Dmitry A. Kazakov
2005-09-02 14:14           ` Georg Bauhaus
2005-09-02 19:48             ` Dmitry A. Kazakov
2005-09-03 20:01               ` Georg Bauhaus
2005-09-04 10:13                 ` Dmitry A. Kazakov
2005-09-05 13:22                   ` Georg Bauhaus
2005-09-05 15:50                     ` Dmitry A. Kazakov
2005-09-05 18:20                       ` Georg Bauhaus
2005-09-05 18:31                         ` Georg Bauhaus
2005-09-06  8:20                         ` Dmitry A. Kazakov
2005-09-06 11:52                           ` Georg Bauhaus
2005-09-06 13:46                             ` Dmitry A. Kazakov
2005-09-06 15:51                               ` Georg Bauhaus
2005-09-06 21:32                                 ` Robert A Duff
2005-09-07  9:08                                 ` Dmitry A. Kazakov
2005-09-07 18:20                                   ` Georg Bauhaus
2005-09-07 19:07                                     ` Georg Bauhaus
2005-09-07 21:23                                     ` Dmitry A. Kazakov
2005-09-08 10:27                                       ` Georg Bauhaus
2005-09-08 11:39                                         ` Georg Bauhaus
2005-09-08 13:44                                         ` Dmitry A. Kazakov
2005-09-08 18:18                                           ` Georg Bauhaus
2005-09-09 10:06                                             ` Dmitry A. Kazakov
2005-09-09 12:26                                               ` Georg Bauhaus
2005-09-09 12:29                                               ` Georg Bauhaus
2005-09-01  8:48 ` Jean-Pierre Rosen
2005-09-01 15:57 ` Robert A Duff
2005-09-01 21:42   ` Gene
2005-09-01 22:56     ` tmoran
2005-09-05 15:53       ` Gene
2005-09-05 17:47         ` jimmaureenrogers
2005-09-05 22:13           ` Robert A Duff
2005-09-06  8:24             ` Dmitry A. Kazakov
2005-09-05 19:22         ` Jeffrey R. Carter
2005-09-05 21:54           ` Robert A Duff
2005-09-05 22:50             ` Larry Kilgallen
2005-09-05 23:46               ` Robert A Duff
2005-09-12  3:59                 ` Dave Thompson
2005-09-06 16:02             ` Jeffrey Carter
2005-09-06 21:00               ` Robert A Duff
2005-09-06  5:38         ` Pascal Obry
2005-09-05 21:48       ` Robert A Duff
2005-09-06  5:25         ` tmoran
2005-09-06 14:58           ` Robert A Duff
2005-09-06  9:26         ` Georg Bauhaus
2005-09-06 15:00           ` Robert A Duff
2005-09-07 11:02             ` Thierry Pirot
2005-09-07 20:09               ` Robert A Duff
2005-09-06 13:22         ` Bob Spooner
2005-09-06 15:30           ` Robert A Duff [this message]
2005-09-06 16:12             ` Jeffrey Carter
2005-09-06 21:21               ` Robert A Duff
2005-09-02 20:19     ` Jeffrey R. Carter
2005-09-03 12:51     ` Dr. Adrian Wrigley
2005-09-03 14:08       ` Jacob Sparre Andersen
2005-09-05  8:34         ` Jean-Pierre Rosen
2005-09-05  9:32           ` Arrays indexed by fixed point types (Was: Surprise in array concatenation) Jacob Sparre Andersen
2005-09-05 11:07             ` Jean-Pierre Rosen
2005-09-05 15:12               ` Dr. Adrian Wrigley
2005-09-05 12:14             ` Dmitry A. Kazakov
2005-09-05 13:07               ` Jacob Sparre Andersen
2005-09-05 15:10                 ` Dmitry A. Kazakov
2005-09-05 11:29           ` Surprise in array concatenation Dr. Adrian Wrigley
replies disabled

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