comp.lang.ada
 help / color / mirror / Atom feed
* Question about subtype of loop index variable
@ 2002-10-10 19:05 Paul Graham
  2002-10-10 20:09 ` Jeffrey Carter
  2002-10-10 21:01 ` Robert A Duff
  0 siblings, 2 replies; 3+ messages in thread
From: Paul Graham @ 2002-10-10 19:05 UTC (permalink / raw)


I have a question about the type and subtype of a loop index variable. 
I'll just
give an example:

with Text_Io; use Text_Io;
procedure Test_Range is
   package Int_Io is new Integer_Io(Integer); use Int_Io;
   type T1 is range 0 .. 7;
   type T2 is array(T1 range <>) of Integer;
   X : T2(0 .. 3);
   Y : Integer;
begin
   Y := 0;
   for I in X'First - 1 .. X'Last + 1 loop
      Y := Y + 1;
   end loop;
   Put("Y = ");
   Put(Y);
   New_Line;
end;

My question is, what is the type (and subtype) of loop index I?  Since
the
expressions X'First - 1 and X'Last + 1 are of type T1, then
I is also of type T1.  But the values X'First - 1 and X'Last + 1 lie
outside
the bounds of T1, so I would think that a range constraint error would
occur
as I is assigned these values.  Yet gnat 3.13 compiles and executes this
code 
without error.  Am I misunderstanding something?

Paul



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

* Re: Question about subtype of loop index variable
  2002-10-10 19:05 Question about subtype of loop index variable Paul Graham
@ 2002-10-10 20:09 ` Jeffrey Carter
  2002-10-10 21:01 ` Robert A Duff
  1 sibling, 0 replies; 3+ messages in thread
From: Jeffrey Carter @ 2002-10-10 20:09 UTC (permalink / raw)


Paul Graham wrote:
> with Text_Io; use Text_Io;
> procedure Test_Range is
>    package Int_Io is new Integer_Io(Integer); use Int_Io;
>    type T1 is range 0 .. 7;
>    type T2 is array(T1 range <>) of Integer;
>    X : T2(0 .. 3);
>    Y : Integer;
> begin
>    Y := 0;
>    for I in X'First - 1 .. X'Last + 1 loop
>       Y := Y + 1;
>    end loop;
>    Put("Y = ");
>    Put(Y);
>    New_Line;
> end;
> 
> My question is, what is the type (and subtype) of loop index I?  Since
> the
> expressions X'First - 1 and X'Last + 1 are of type T1, then
> I is also of type T1.  But the values X'First - 1 and X'Last + 1 lie
> outside
> the bounds of T1, so I would think that a range constraint error would
> occur
> as I is assigned these values.  Yet gnat 3.13 compiles and executes this
> code 
> without error.  Am I misunderstanding something?

I think so. The expressions X'First - 1 and X'Last + 1 are evaluated 
using the base type of the subtype. In this case the subtype is T1, 
because types are always anonymous and the name in a type statement is 
the "first named subtype". The base type of T1 is T1'Base. T1'Base has a 
range of at least -7 .. 7 because signed integer types are symmetric 
about 0 but may have an additional negative value. The compiler almost 
certainly used an 8-bit base type here, so the real range is probably 
-128 .. 127; you can check by printing out T1'Base'First and T1'Base'Last.

Of course, it would be easier and quicker to calculate Y as

Y := X'Length + 2;

-- 
Jeff Carter
"You've got the brain of a four-year-old boy,
and I bet he was glad to get rid of it."
Horse Feathers




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

* Re: Question about subtype of loop index variable
  2002-10-10 19:05 Question about subtype of loop index variable Paul Graham
  2002-10-10 20:09 ` Jeffrey Carter
@ 2002-10-10 21:01 ` Robert A Duff
  1 sibling, 0 replies; 3+ messages in thread
From: Robert A Duff @ 2002-10-10 21:01 UTC (permalink / raw)


Paul Graham <pgraham@cadence.com> writes:

> I have a question about the type and subtype of a loop index variable. 
> I'll just
> give an example:
> 
> with Text_Io; use Text_Io;
> procedure Test_Range is
>    package Int_Io is new Integer_Io(Integer); use Int_Io;
>    type T1 is range 0 .. 7;
>    type T2 is array(T1 range <>) of Integer;
>    X : T2(0 .. 3);
>    Y : Integer;
> begin
>    Y := 0;
>    for I in X'First - 1 .. X'Last + 1 loop
>       Y := Y + 1;
>    end loop;
>    Put("Y = ");
>    Put(Y);
>    New_Line;
> end;
> 
> My question is, what is the type (and subtype) of loop index I?  Since
> the
> expressions X'First - 1 and X'Last + 1 are of type T1, then
> I is also of type T1.

Right.  (Or, more precisely: T1 is a "first subtype", and the type of I
is the type of T1.  Types themselves never have names in Ada -- we
informally refer to them by the name of their first subtype, but
strictly speaking the name in a type declaration names a subtype.
Strange, but true.)

The subtype of I is "T1'Base range X'First - 1 .. X'Last + 1".
The implementation gets to choose the bounds of T1'Base,
but the bounds must include 0..7, and must be symmetric about zero
(or symmetric but for an extra negative value).  So the smallest range
that T1'Base could have is -7..7.  So the "X'Last + 1" *could* raise
C_E.

However, compilers normally choose the base range to match some hardware
registers.  So the base range is likely one of -128..127, or
-2**31..2**31-1, or some such.  So the above is unlikely to raise C_E.

>...  But the values X'First - 1 and X'Last + 1 lie
> outside
> the bounds of T1, so I would think that a range constraint error would
> occur
> as I is assigned these values.

The subtype of I comes from the bounds given, so the assignments into I
can never raise C_E.  The only possible C_E comes from the "X'Last + 1",
and as I said, that's unlikely.  If you want to be sure, do this:

    type T0 is range 0..8;
    subtype T1 is T0 range 0..7;

Now you're guaranteed that T1'Base is -8..8 (or bigger).

- Bob



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

end of thread, other threads:[~2002-10-10 21:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-10 19:05 Question about subtype of loop index variable Paul Graham
2002-10-10 20:09 ` Jeffrey Carter
2002-10-10 21:01 ` Robert A Duff

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