comp.lang.ada
 help / color / mirror / Atom feed
* Loop parameter type
@ 2006-09-25 10:56 Maxim Reznik
  2006-09-25 11:50 ` Stuart
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Maxim Reznik @ 2006-09-25 10:56 UTC (permalink / raw)


Hi, All

Please help me understand Ada name resolution rules for
loop parameter specification.

Consider follow code:

 1: procedure Test is
 2:    type Int is range 0 .. 1000;
 3:    C     : Integer := 0;
 4:    C_Int : Int := 0;
 5: begin
 6:    for J in 1 .. 10 loop
 7:       C := C + J;
 8:    end loop;
 9:   
10:    for K in 1 .. 10 loop
11:       C_Int := C_Int + K;
12:    end loop;
13: end Test;

gcc -c test.adb 
test.adb:11:22: invalid operand types for operator "+"
test.adb:11:22: left operand has type "Int" defined at line 2
test.adb:11:22: right operand has subtype of "Standard.Integer" defined
at line 10

GNAT complains at line 11, but line 7 seems Ok. Why?

I expect range 1 .. 10 is root_integer type, because of preference
root_integer over others integer types. 
ARM states: "There is a preference for the primitive operators (and
ranges) of the root numeric types root_integer and root_real."

In this case both 7 and 11 lines need explicit type conversion.

Why GNAT says type of K is Standard.Integer type?

Thank you
-- 
Maxim Reznik




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

* Re: Loop parameter type
  2006-09-25 10:56 Loop parameter type Maxim Reznik
@ 2006-09-25 11:50 ` Stuart
  2006-09-25 16:32 ` Robert A Duff
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Stuart @ 2006-09-25 11:50 UTC (permalink / raw)


"Maxim Reznik" <reznikmm@front.ru> wrote in message 
news:20060925105627.GA14409@ws.max.zp.ua...
...
> 1: procedure Test is
> 2:    type Int is range 0 .. 1000;
...
> 4:    C_Int : Int := 0;
> 5: begin
...
> 10:    for K in 1 .. 10 loop
> 11:       C_Int := C_Int + K;
> 12:    end loop;
> 13: end Test;

> Why GNAT says type of K is Standard.Integer type?

Why should it think it is anything else?

Try the following for line 10:
   for K in Int range 1..10 loop

-- 
Regards
Stuart 





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

* Re: Loop parameter type
  2006-09-25 10:56 Loop parameter type Maxim Reznik
  2006-09-25 11:50 ` Stuart
@ 2006-09-25 16:32 ` Robert A Duff
  2006-09-25 19:08 ` Jeffrey R. Carter
  2006-09-26  0:12 ` Adam Beneschan
  3 siblings, 0 replies; 5+ messages in thread
From: Robert A Duff @ 2006-09-25 16:32 UTC (permalink / raw)


Maxim Reznik <reznikmm@front.ru> writes:

> Hi, All
> 
> Please help me understand Ada name resolution rules for
> loop parameter specification.
> 
> Consider follow code:
> 
>  1: procedure Test is
>  2:    type Int is range 0 .. 1000;
>  3:    C     : Integer := 0;
>  4:    C_Int : Int := 0;
>  5: begin
>  6:    for J in 1 .. 10 loop
>  7:       C := C + J;
>  8:    end loop;
>  9:   
> 10:    for K in 1 .. 10 loop
> 11:       C_Int := C_Int + K;
> 12:    end loop;
> 13: end Test;
> 
> gcc -c test.adb 
> test.adb:11:22: invalid operand types for operator "+"
> test.adb:11:22: left operand has type "Int" defined at line 2
> test.adb:11:22: right operand has subtype of "Standard.Integer" defined
> at line 10
> 
> GNAT complains at line 11, but line 7 seems Ok. Why?

The rule is in RM-3.6(18).  J and K above are both of type Integer, not Int.

IMHO it's a kludge, and you should not rely on it.  Say "for I in
Integer range 1..10" or "for I in Int range 1..10" or whatever.

- Bob



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

* Re: Loop parameter type
  2006-09-25 10:56 Loop parameter type Maxim Reznik
  2006-09-25 11:50 ` Stuart
  2006-09-25 16:32 ` Robert A Duff
@ 2006-09-25 19:08 ` Jeffrey R. Carter
  2006-09-26  0:12 ` Adam Beneschan
  3 siblings, 0 replies; 5+ messages in thread
From: Jeffrey R. Carter @ 2006-09-25 19:08 UTC (permalink / raw)


Maxim Reznik wrote:
> 
> Please help me understand Ada name resolution rules for
> loop parameter specification.
> 
> Consider follow code:
> 
>  1: procedure Test is
>  2:    type Int is range 0 .. 1000;
>  3:    C     : Integer := 0;
>  4:    C_Int : Int := 0;
>  5: begin
>  6:    for J in 1 .. 10 loop
>  7:       C := C + J;
>  8:    end loop;
>  9:   
> 10:    for K in 1 .. 10 loop
> 11:       C_Int := C_Int + K;
> 12:    end loop;
> 13: end Test;

When the subtype in a for loop is an explicit range, with the parent 
subtype not specified, and the values are universal integers (literals 
or named numbers), the parent type is Standard.Integer. See ARM 3.6. 
That's sort of an odd place for it to be defined, but that's the way it is.

You can specify the parent subtype in a for loop:

for I in Int range 1 .. 10 loop

-- 
Jeff Carter
"Don't knock masturbation. It's sex with someone I love."
Annie Hall
45



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

* Re: Loop parameter type
  2006-09-25 10:56 Loop parameter type Maxim Reznik
                   ` (2 preceding siblings ...)
  2006-09-25 19:08 ` Jeffrey R. Carter
@ 2006-09-26  0:12 ` Adam Beneschan
  3 siblings, 0 replies; 5+ messages in thread
From: Adam Beneschan @ 2006-09-26  0:12 UTC (permalink / raw)


Maxim Reznik wrote:
> Hi, All
>
> Please help me understand Ada name resolution rules for
> loop parameter specification.
>
> Consider follow code:
>
>  1: procedure Test is
>  2:    type Int is range 0 .. 1000;
>  3:    C     : Integer := 0;
>  4:    C_Int : Int := 0;
>  5: begin
>  6:    for J in 1 .. 10 loop
>  7:       C := C + J;
>  8:    end loop;
>  9:
> 10:    for K in 1 .. 10 loop
> 11:       C_Int := C_Int + K;
> 12:    end loop;
> 13: end Test;
>
> gcc -c test.adb
> test.adb:11:22: invalid operand types for operator "+"
> test.adb:11:22: left operand has type "Int" defined at line 2
> test.adb:11:22: right operand has subtype of "Standard.Integer" defined
> at line 10
>
> GNAT complains at line 11, but line 7 seems Ok. Why?
>
> I expect range 1 .. 10 is root_integer type, because of preference
> root_integer over others integer types.
> ARM states: "There is a preference for the primitive operators (and
> ranges) of the root numeric types root_integer and root_real."

Bob and Jeff have already explained why the types of J and K are of
type Standard.Integer and not root_integer.  You should also be aware
that if J and K *were* root_integer, then you'd get two errors instead
of one, because there is no predefined + operation whose left operand
is Standard.Integer or Int, and whose right operand is root_integer.
You're probably confusing this with universal integers.  A universal
integer can appear any place that *any* integer type is expected.  But
a value of type root_integer is *not* a universal integer and doesn't
have that property.

                          -- Adam




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

end of thread, other threads:[~2006-09-26  0:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-09-25 10:56 Loop parameter type Maxim Reznik
2006-09-25 11:50 ` Stuart
2006-09-25 16:32 ` Robert A Duff
2006-09-25 19:08 ` Jeffrey R. Carter
2006-09-26  0:12 ` Adam Beneschan

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