comp.lang.ada
 help / color / mirror / Atom feed
* Re: null arrays
@ 1992-11-18 13:22 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!apl
  0 siblings, 0 replies; 3+ messages in thread
From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!apl @ 1992-11-18 13:22 UTC (permalink / raw)


In <BxvtCA.LM1@dale.cts.com> jhb@dale.cts.com (John Bollenbacher) writes:

>Can somebody shed some light on the symptoms of the following program.  In 

>-------------------cut here-----------------------------------------------
>procedure TEST is

>  subtype T is NATURAL range 0 .. 10;

>  type ARR is array (T range <>) of BOOLEAN;
>  
>  type A(N : T := 0) is record 
>    DATA : ARR(1..N);
>  end record;  
>  I : INTEGER;
>  O : A := (3, (TRUE, FALSE, TRUE));
>  
>  N1 : constant ARR := O.DATA(1..0) & O.DATA(1..0);
>  N2 : constant ARR := O.DATA(1..0) & O.DATA(3..2);

-- The result of concatenation of a null array and another array is
-- an array with the range of the second array.  Thus N1 has a range
-- of 1..0 and N2 has a range of 3..2.  

>begin
>  I := N1'LENGTH; -- I = 0 
>  I := N2'LENGTH; -- I = 0
>  O := (0, N1);   -- does not raise constraint
>  O := (0, N2);   -- raises constraint

-- Given the above, this is the common sliding error.  An aggregate assignment
-- does not "slide" the indices to match the constraints of the array in your
-- record.  Since N1'First is 1, no problem.  N2'First is 3 so it does
-- not match 1..N.

>end TEST; 

--Thor
collard@capsrv.jhuapl.edu
dlc@ddsdx2.jhuapl.edu

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

* Re: null arrays
@ 1992-11-18 14:48 Douglas N. Surber
  0 siblings, 0 replies; 3+ messages in thread
From: Douglas N. Surber @ 1992-11-18 14:48 UTC (permalink / raw)


In <BxvtCA.LM1@dale.cts.com> jhb@dale.cts.com (John Bollenbacher) writes:

>procedure TEST is

>  subtype T is NATURAL range 0 .. 10;

>  type ARR is array (T range <>) of BOOLEAN;
>  
>  type A(N : T := 0) is record 
>    DATA : ARR(1..N);
>  end record;  
>  I : INTEGER;
>  O : A := (3, (TRUE, FALSE, TRUE));
>  
>  N1 : constant ARR := O.DATA(1..0) & O.DATA(1..0);
>  N2 : constant ARR := O.DATA(1..0) & O.DATA(3..2);

N2'first = 3 (LRM 4.5.3 4)

>begin
>  I := N1'LENGTH; -- I = 0 
>  I := N2'LENGTH; -- I = 0
>  O := (0, N1);   -- does not raise constraint
>  O := (0, N2);   -- raises constraint
 
O.Data'first /= N2'first thus raises constraint error (LRM 4.3.1 3, 3.3 4)

>end TEST; 

The trick here is that the constraint error is raised in forming the
aggregate, not in the assignment.  LRM 4.3.1 3 says "A check is made that
the value of each subcomponent of the aggregate _belongs_ to the subtype
of this component."  The magic word is "belongs".  LRM 3.3 4 says "a value
is said to belong to a subtype of a given type if it belongs to the type
and satisfies the constraint." The bounds of N2, i.e. 3 and 2, do not
satisfy the constraint of the aggregate, i.e. in the range 1 .. 0.  If this
were an array assignment rather than an aggregate, it would work.

Douglas Surber
Lockheed
Houston, TX

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

* Re: null arrays
@ 1992-11-20 20:38 Tucker Taft
  0 siblings, 0 replies; 3+ messages in thread
From: Tucker Taft @ 1992-11-20 20:38 UTC (permalink / raw)


In article <dnsurber.722098125@node_26400> 
  dnsurber@lescsse.jsc.nasa.gov (Douglas N. Surber) writes:

>In <BxvtCA.LM1@dale.cts.com> jhb@dale.cts.com (John Bollenbacher) writes:
>
>>procedure TEST is
>
>>  subtype T is NATURAL range 0 .. 10;
>
>>  type ARR is array (T range <>) of BOOLEAN;
>>  
>>  type A(N : T := 0) is record 
>>    DATA : ARR(1..N);
>>  end record;  
>>  I : INTEGER;
>>  O : A := (3, (TRUE, FALSE, TRUE));
>>  
>>  N1 : constant ARR := O.DATA(1..0) & O.DATA(1..0);
>>  N2 : constant ARR := O.DATA(1..0) & O.DATA(3..2);
>
>N2'first = 3 (LRM 4.5.3 4)
>
>>begin
>>  I := N1'LENGTH; -- I = 0 
>>  I := N2'LENGTH; -- I = 0
>>  O := (0, N1);   -- does not raise constraint
>>  O := (0, N2);   -- raises constraint
> 
>O.Data'first /= N2'first thus raises constraint error (LRM 4.3.1 3, 3.3 4)
>
>>end TEST; 
>
>The trick here is that the constraint error is raised in forming the
>aggregate, not in the assignment.  LRM 4.3.1 3 says "A check is made that
>the value of each subcomponent of the aggregate _belongs_ to the subtype
>of this component."  The magic word is "belongs".  LRM 3.3 4 says "a value
>is said to belong to a subtype of a given type if it belongs to the type
>and satisfies the constraint." The bounds of N2, i.e. 3 and 2, do not
>satisfy the constraint of the aggregate, i.e. in the range 1 .. 0.  If this
>were an array assignment rather than an aggregate, it would work.

This problem is alleviated in Ada 9X by providing "sliding" (implicit
array subtype conversion) in essentially all contexts where
it might be useful.  In particular, in Ada 9X, the bounds of
N2 (i.e. 3..2) would "slide" to match the bounds of the DATA
component (i.e. 1..0) as part of constructing the aggregate.

Until you convince your implementor to provide a "-9X" mode
in their compiler ;-), you might want to define a function that "slides"
its parameter and then returns it.  For example, here is a function
that slides its parameter to have a low bound of 1 (if needed, you
could define one that took the low bound desired as an additional
parameter):

   function Slide(A : ARR) return ARR is
       subtype S is ARR(1..A'Length);
   begin
       return S(A);  -- Explicit array subtype conversion
   end Slide;

A "pragma INLINE" might be appropriate here.

>Douglas Surber
>Lockheed
>Houston, TX

S. Tucker Taft    stt@inmet.com
Ada 9X Mapping/Revision Team
Intermetrics, Inc.
Cambridge, MA  02138

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

end of thread, other threads:[~1992-11-20 20:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-11-18 14:48 null arrays Douglas N. Surber
  -- strict thread matches above, loose matches on Subject: below --
1992-11-20 20:38 Tucker Taft
1992-11-18 13:22 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!apl

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