comp.lang.ada
 help / color / mirror / Atom feed
* Unconstrained array aggregation..sq. peg into round hole?
@ 1996-03-17  0:00 David Weller
  1996-03-19  0:00 ` Unconstrained array aggregation..sq. peg i John Hutchison x2141
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: David Weller @ 1996-03-17  0:00 UTC (permalink / raw)


Disclaimer: I didn't write this code, but I'm sure puzzled about what
to do to get the "desired" results.  This is easy to do in C, so I'm
sure there's a "simple" way to do it in Ada 95.  Anyway, the problem
is wanting to take a "slice" of a 2-dim unconstrained array and use it
to generate a constant 1-dim array.  The GNAT error message is
"strange.ads:20:50: incorrect number of indices in indexed component",
but I don't see this as a GNAT bug at all.  

I must confess I don't use such strange constnat initializations
typically, so I'm a little rusty on figuring out a solution.

Suggestions are welcome...

package Strange is

   type Real_Array is array (Natural range <>) of Float;

   type Real_Table is array (Natural range <>, Natural range <>) of Float;

   MAX : constant := 3;

   Table          : constant Real_Table :=
  ( (20.0,  40.0,  80.0),
    (20.0,  40.0,  80.0),
    (20.0,  40.0,  80.0),
    (20.0,  40.0,  80.0),
    (60.0, 120.0, 240.0) );

  An_Array : constant Real_Array;

private

  An_Array : constant Real_Array := (0..Max-1 => Table(Max)(0..Max-1));
						-- incorrect # of indices?

end Strange;
-- 
		    GNAT = GNAT is Not an Ada Translator
==Ada 95 Booch Components: www.ocsystems.com/booch or www.dfw.net/~dweller==
Reality: Work, Work, Work, Guitar.         | Plugged: Fender Telecaster Deluxe
Fantasy: Guitar, Guitar, Guitar, Work(ha!) | Unplugged: Yamaha CG-150SA




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Unconstrained array aggregation..sq. peg into round hole?
  1996-03-17  0:00 Unconstrained array aggregation..sq. peg into round hole? David Weller
  1996-03-19  0:00 ` Unconstrained array aggregation..sq. peg i John Hutchison x2141
@ 1996-03-19  0:00 ` Ted Dennison
  1996-03-19  0:00   ` Robert Dewar
  1996-03-20  0:00 ` Jon S Anthony
  1996-03-20  0:00 ` Robert I. Eachus
  3 siblings, 1 reply; 13+ messages in thread
From: Ted Dennison @ 1996-03-19  0:00 UTC (permalink / raw)


David Weller wrote:
> 
> package Strange is
> 
>    type Real_Array is array (Natural range <>) of Float;
> 
>    type Real_Table is array (Natural range <>, Natural range <>) of Float;
> 
...
> 
>   An_Array : constant Real_Array := (0..Max-1 => Table(Max)(0..Max-1));
>                                                 -- incorrect # of indices?
> 
> end Strange;

Well, I don't have an Ada (95) compiler handy, but from my Ada 83 
experience one thing jumps right out at me.

In Ada 83 this would be illegal because Real_Table is an array with
two dimensions, but you are indexing it like an array of arrays. 
This is a syntactic no-no right off the bat (which is what the 
compiler is pointing out).

I don't believe there is a way to "convert" one dimension of a 
doubly dimensioned array into some kind of anonymous singly
dimensioned array type, like this code is attempting to do. I 
don't know about Ada 95, but Ada 83 does not allow slices of 
multi-dimensioned arrays (after all, what would the type be?).

You could try to correct this by defining Real_Table as an array
of Real_Array's, but this won't compile either because you can't
use an unconstrained array in another type declaration.

If you don't mind constraining one index of the table, this will
work (in Ada 83):

package Strange is

   type Real_Array is array (Natural range <>) of Float;

   MAX : constant := 3;

   subtype Real_3_Array is Real_Array (0..Max-1);
   type Real_Table is array (Natural range <>) of Real_3_Array;

   Table          : constant Real_Table :=
  ( (20.0,  40.0,  80.0),
    (20.0,  40.0,  80.0),
    (20.0,  40.0,  80.0),
    (20.0,  40.0,  80.0),
    (60.0, 120.0, 240.0) );

  An_Array : constant Real_Array := Table(Max);

end Strange;

-- 
T.E.D.          
                |  Work - mailto:dennison@escmail.orl.mmc.com  |
                |  Home - mailto:dennison@iag.net              |
                |  URL  - http://www.iag.net/~dennison         |




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Unconstrained array aggregation..sq. peg into round hole?
  1996-03-19  0:00 ` Unconstrained array aggregation..sq. peg into round hole? Ted Dennison
@ 1996-03-19  0:00   ` Robert Dewar
  0 siblings, 0 replies; 13+ messages in thread
From: Robert Dewar @ 1996-03-19  0:00 UTC (permalink / raw)


In C, you represent a 2-D array as an array of arrays. Do the same
in Ada, and you will be able to slice it (at either level). Allowing
direct slicing of 2-D arrays (a la Fortran 90) is a BIG feature with
distributed overhead.





^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Unconstrained array aggregation..sq. peg i
  1996-03-17  0:00 Unconstrained array aggregation..sq. peg into round hole? David Weller
@ 1996-03-19  0:00 ` John Hutchison x2141
  1996-03-19  0:00 ` Unconstrained array aggregation..sq. peg into round hole? Ted Dennison
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 13+ messages in thread
From: John Hutchison x2141 @ 1996-03-19  0:00 UTC (permalink / raw)


The problem is that Table is a 2 dimensional array and needs to be addressed
via Table(e1, e2), not as a vector of vectors. If you want to be able to 
slice the last dimension then you need to declare Real_Table as a vector of
vectors.






^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Unconstrained array aggregation..sq. peg into round hole?
  1996-03-17  0:00 Unconstrained array aggregation..sq. peg into round hole? David Weller
                   ` (2 preceding siblings ...)
  1996-03-20  0:00 ` Jon S Anthony
@ 1996-03-20  0:00 ` Robert I. Eachus
  1996-03-20  0:00   ` Robert A Duff
                     ` (2 more replies)
  3 siblings, 3 replies; 13+ messages in thread
From: Robert I. Eachus @ 1996-03-20  0:00 UTC (permalink / raw)



In article <4ihrvo$hs5@dfw.dfw.net> dweller@dfw.net (David Weller) writes:

  > I must confess I don't use such strange constnat initializations
  > typically, so I'm a little rusty on figuring out a solution.

  > Suggestions are welcome...

   It isn't a GNAT bug, and it is an Ada rule, but one of the weirder
gotchas.  (An element of a one dimensional array is a name not a
slice, so this case only comes up when you have two dimensional to one
dimensional and forget to sprinkle the right syntactic sugar...

 > package Strange is

 >     type Real_Array is array (Natural range <>) of Float;

 >     type Real_Table is array (Natural range <>, Natural range <>) of Float;

--  try: type Real_Table is array (Natural range <>) of Real_Array;

 >     MAX : constant := 3;

 >     Table          : constant Real_Table :=
 >    ( (20.0,  40.0,  80.0),
 >      (20.0,  40.0,  80.0),
 >      (20.0,  40.0,  80.0),
 >      (20.0,  40.0,  80.0),
 >      (60.0, 120.0, 240.0) );

-- the constant is fine... ;-)

 >    An_Array : constant Real_Array;

 >  private

 >    An_Array : constant Real_Array := (0..Max-1 => Table(Max)(0..Max-1));
-- An_Array : constant Real_Array := Table(Max);

 >  end Strange;

  In Ada there is a difference between Table(2)(3) and Table(2,3), in
C there isn't.

--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Unconstrained array aggregation..sq. peg into round hole?
  1996-03-20  0:00 ` Robert I. Eachus
@ 1996-03-20  0:00   ` Robert A Duff
  1996-03-20  0:00     ` Keith Thompson
  1996-03-21  0:00   ` Robert I. Eachus
  1996-03-25  0:00   ` Robert I. Eachus
  2 siblings, 1 reply; 13+ messages in thread
From: Robert A Duff @ 1996-03-20  0:00 UTC (permalink / raw)


In article <EACHUS.96Mar19201632@spectre.mitre.org>,
Robert I. Eachus <eachus@spectre.mitre.org> wrote:
>--  try: type Real_Table is array (Natural range <>) of Real_Array;

Right, except you have to give a constraint.  Array-of-unconstrained
array is illegal.

>  In Ada there is a difference between Table(2)(3) and Table(2,3), in
>C there isn't.

Right.

- Bob




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Unconstrained array aggregation..sq. peg into round hole?
  1996-03-17  0:00 Unconstrained array aggregation..sq. peg into round hole? David Weller
  1996-03-19  0:00 ` Unconstrained array aggregation..sq. peg i John Hutchison x2141
  1996-03-19  0:00 ` Unconstrained array aggregation..sq. peg into round hole? Ted Dennison
@ 1996-03-20  0:00 ` Jon S Anthony
  1996-03-20  0:00   ` David Weller
  1996-03-20  0:00 ` Robert I. Eachus
  3 siblings, 1 reply; 13+ messages in thread
From: Jon S Anthony @ 1996-03-20  0:00 UTC (permalink / raw)


In article <4ihrvo$hs5@dfw.dfw.net> dweller@dfw.net (David Weller) writes:

> package Strange is
> 
>    type Real_Array is array (Natural range <>) of Float;
> 
>    type Real_Table is array (Natural range <>, Natural range <>) of Float;
> 
>    MAX : constant := 3;
> 
>    Table          : constant Real_Table :=
>   ( (20.0,  40.0,  80.0),
>     (20.0,  40.0,  80.0),
>     (20.0,  40.0,  80.0),
>     (20.0,  40.0,  80.0),
>     (60.0, 120.0, 240.0) );
> 
>   An_Array : constant Real_Array;
> 
> private
> 
>   An_Array : constant Real_Array := (0..Max-1 => Table(Max)(0..Max-1));
> 						-- incorrect # of indices?
> 
> end Strange;

Table is a 2 dimensional array with float components.  The error line
shows an attempt to only specify one index of the two required.  Two
dimensional arrays do not have Pascal array of array semantics.  You
have to write that if it is what you want.  Of course here, you can't
quite do that because you can't have an indefinite type be a component
of an array (the potential Real_Array part).  The typical trick is to
use an array of pointers to unconstrained array type.

see 3.6(10, 12, 13)

/Jon
-- 
Jon Anthony
Organon Motives, Inc.
1 Williston Road, Suite 4
Belmont, MA 02178

617.484.3383
jsa@organon.com





^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Unconstrained array aggregation..sq. peg into round hole?
  1996-03-20  0:00 ` Jon S Anthony
@ 1996-03-20  0:00   ` David Weller
  0 siblings, 0 replies; 13+ messages in thread
From: David Weller @ 1996-03-20  0:00 UTC (permalink / raw)


In article <JSA.96Mar19204126@organon.com>,
Jon S Anthony <jsa@organon.com> wrote:
>In article <4ihrvo$hs5@dfw.dfw.net> dweller@dfw.net (David Weller) writes:
>
>>[example code deleted]
>Table is a 2 dimensional array with float components.  The error line
>shows an attempt to only specify one index of the two required.  Two
>dimensional arrays do not have Pascal array of array semantics.  You
>have to write that if it is what you want.  Of course here, you can't
>quite do that because you can't have an indefinite type be a component
>of an array (the potential Real_Array part).  The typical trick is to
>use an array of pointers to unconstrained array type.
>
>see 3.6(10, 12, 13)
>

Yup.  Actually the solution was to create something like a
"Load_Vector_From_Table" function.  That worked out pretty well.  I
knew about the "ragged array" solution, but I was looking something
with a more C-like flavor to it.  All things considered, the call to
the function actually turned out to be more readable for the target
audience (most of whom are highly Ada-illiterate).

I thank everybody for their comments.  In the end, things were just as
I had expected, but I thought maybe, just maybe, there was something
about Ada 95 that I missed :-)

-- 
		    GNAT = GNAT is Not an Ada Translator
==Ada 95 Booch Components: www.ocsystems.com/booch or www.dfw.net/~dweller==
Reality: Work, Work, Work, Guitar.         | Plugged: Fender Telecaster Deluxe
Fantasy: Guitar, Guitar, Guitar, Work(ha!) | Unplugged: Yamaha CG-150SA




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Unconstrained array aggregation..sq. peg into round hole?
  1996-03-20  0:00   ` Robert A Duff
@ 1996-03-20  0:00     ` Keith Thompson
  0 siblings, 0 replies; 13+ messages in thread
From: Keith Thompson @ 1996-03-20  0:00 UTC (permalink / raw)


In <DoJqI0.51n@world.std.com> bobduff@world.std.com (Robert A Duff) writes:
[...]
> >  In Ada there is a difference between Table(2)(3) and Table(2,3), in
> >C there isn't.
> 
> Right.

Um, not quite.  In C, if Table is declared as a two-dimensional array, then

    Table[2][3]

is a single element of that array.  C's syntax doesn't allow more than
one index expression between a pair of brackets.  The expression

    Table[2,3]

is syntactically legal, but the expression between the brackets includes
a comma operator, so it's equivalent to

    Table[3]

I *think* that gives you a one-dimensional array, the fourth (zero-based
arrays, remember?) row of Table, which in most expression contexts
becomes a pointer to the first element of that row.

-- 
Keith Thompson (The_Other_Keith) kst@thomsoft.com
TeleSoft^H^H^H^H^H^H^H^H Alsys^H^H^H^H^H Thomson Software Products
10251 Vista Sorrento Parkway, Suite 300, San Diego, CA, USA, 92121-2718
This sig uses the word "Exon" in violation of the Communications Decency Act.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Unconstrained array aggregation..sq. peg into round hole?
  1996-03-20  0:00 ` Robert I. Eachus
  1996-03-20  0:00   ` Robert A Duff
@ 1996-03-21  0:00   ` Robert I. Eachus
  1996-03-22  0:00     ` Robert Dewar
  1996-03-25  0:00   ` Robert I. Eachus
  2 siblings, 1 reply; 13+ messages in thread
From: Robert I. Eachus @ 1996-03-21  0:00 UTC (permalink / raw)


In article <DoJqI0.51n@world.std.com> bobduff@world.std.com (Robert A Duff) writes:

  > Right, except you have to give a constraint.  Array-of-unconstrained
  > array is illegal.

   Not really, but you have to put two records in the middle to make
it work:

   subtype Limited_Subscripts is Natural range 1..1000;

   type Real_Array is array (Limited_Subscripts range <>) of Float;

   type Inner_Wrapper is (First: Integer := 1; Last: Integer := 0) is record
     B: Real_Array(First..Last);
   end record;

   type Outer_Wrapper is 
     A: Inner_Wrapper;
   end record;
 
   type Ragged_Table is array (Natural range <>) of Outer_Wrapper;

   I never understood why, if this useful and used feature was in Ada
83 and Ada 95, you have to go through all this semantic sugar to create
the ragged array type.  To keep novice users from trying it?   

   (The Limited_Subscripts subtype is so that this will work on all
implementations including those that "allocate the maximum" for this
type of subcomponent.)

--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Unconstrained array aggregation..sq. peg into round hole?
  1996-03-21  0:00   ` Robert I. Eachus
@ 1996-03-22  0:00     ` Robert Dewar
  0 siblings, 0 replies; 13+ messages in thread
From: Robert Dewar @ 1996-03-22  0:00 UTC (permalink / raw)


Robert Eachus said:

"   I never understood why, if this useful and used feature was in Ada
83 and Ada 95, you have to go through all this semantic sugar to create
the ragged array type.  To keep novice users from trying it?"

it is not really a ragged array at all. If an implementation uses hidden
heap pointers (very few implementations do in this case, e.g. the old
Alsys technology, which sometimes uses hidden pointers, does not do so,
and the last time I used RR it warned of storage leaks here), then all
you have is an array of pointers, which is more easily declared
directly.

If the implementation allocates the maximum, fine, but that is not what
people call a ragged array!

THere is nothing odd about the syntax here, it is a natural combination
of features. If novices don't undrstand it, they certainly should not
be using it!

As for allowing directly the notion of array of unconstrained array, this
is no different from allowing e.g.

	X : String;

in the first place. Although this has definable semantics, it represents
a level of dynamic behavior that is inconsistent with the rest of the
Ada design (in particular, this would definitely require hidden heap
allocation, and probably only makes sense in a garbage collected regime).





^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Unconstrained array aggregation..sq. peg into round hole?
  1996-03-20  0:00 ` Robert I. Eachus
  1996-03-20  0:00   ` Robert A Duff
  1996-03-21  0:00   ` Robert I. Eachus
@ 1996-03-25  0:00   ` Robert I. Eachus
  1996-03-25  0:00     ` Robert Dewar
  2 siblings, 1 reply; 13+ messages in thread
From: Robert I. Eachus @ 1996-03-25  0:00 UTC (permalink / raw)


In article <dewar.827504035@schonberg> dewar@cs.nyu.edu (Robert Dewar) writes:

  > As for allowing directly the notion of array of unconstrained array, this
  > is no different from allowing e.g.

  >	   X : String;

  > in the first place. Although this has definable semantics, it represents
  > a level of dynamic behavior that is inconsistent with the rest of the
  > Ada design (in particular, this would definitely require hidden heap
  > allocation, and probably only makes sense in a garbage collected regime).

   Exactly.  Allowing objects of record types with discriminants that
have defaults requires exactly the same thing.  I think I confused
Robert Dewar slightly.  The sucrose overload is in creating a variable
string type with a hidden discriminant--where Alsys and others handle
the allocations just fine--so that the top level type has no
discriminants and can appear in an array.

   In Ada 95 this exact sugar appears in the private part of
Ada.Strings.Bounded and Ada.Strings.Unbounded, so the user doesn't
have to rewrite it, but it always seemed to me that the requirement
that array components have the same subtype could have been relaxed
slightly with no damage.  I agree that creating ragged arrays "under
the covers" would not be in the spirit of Ada, but two of the
necessary type declarations are only there to hide the discriminant
from direct view.  That seems excessive.

--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Unconstrained array aggregation..sq. peg into round hole?
  1996-03-25  0:00   ` Robert I. Eachus
@ 1996-03-25  0:00     ` Robert Dewar
  0 siblings, 0 replies; 13+ messages in thread
From: Robert Dewar @ 1996-03-25  0:00 UTC (permalink / raw)


"   Exactly.  Allowing objects of record types with discriminants that
have defaults requires exactly the same thing.  I think I confused
Robert Dewar slightly.  The sucrose overload is in creating a variable
string type with a hidden discriminant--where Alsys and others handle
the allocations just fine--so that the top level type has no
discriminants and can appear in an array."

Alsys does not handle the allocatoins just fine, it works only at the
outer level, and is not composable. Th alternatie would be to create
non-contiguous types, which are a real mess.





^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~1996-03-25  0:00 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-03-17  0:00 Unconstrained array aggregation..sq. peg into round hole? David Weller
1996-03-19  0:00 ` Unconstrained array aggregation..sq. peg i John Hutchison x2141
1996-03-19  0:00 ` Unconstrained array aggregation..sq. peg into round hole? Ted Dennison
1996-03-19  0:00   ` Robert Dewar
1996-03-20  0:00 ` Jon S Anthony
1996-03-20  0:00   ` David Weller
1996-03-20  0:00 ` Robert I. Eachus
1996-03-20  0:00   ` Robert A Duff
1996-03-20  0:00     ` Keith Thompson
1996-03-21  0:00   ` Robert I. Eachus
1996-03-22  0:00     ` Robert Dewar
1996-03-25  0:00   ` Robert I. Eachus
1996-03-25  0:00     ` Robert Dewar

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