comp.lang.ada
 help / color / mirror / Atom feed
* Q: discrete_subtype_definition: static only cases?
@ 2020-06-07  9:27 gautier_niouzes
  2020-06-07 10:40 ` J-P. Rosen
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: gautier_niouzes @ 2020-06-07  9:27 UTC (permalink / raw)


Hi,

Are there cases in the Ada syntax where a discrete_subtype_definition (RM 3.6 (6)) is expected to be determined at compile-time?

For instance, for array indices or "for" loops, it can be static or dynamic.
Same for subtypes.

procedure SD is
  A : array (1 .. 5) of Integer;
  type XYZ is (X, Y, Z);
begin
  for I in 1 .. 3 loop
    declare
      B : array (1 .. I) of Integer;  --  I is a variable
    begin
      for J in 1 .. I loop            --  I is a variable
        B (J) := J;
      end loop;
    end;
  end loop;
  for E in XYZ loop
    declare
      subtype S is XYZ range X .. E;  --  E is a variable
    begin
      null;
    end;
  end loop;
end;
_________________________ 
Gautier's Ada programming 
https://hacadacompiler.sourceforge.io/
NB: follow the above link for a valid e-mail address 

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

* Re: Q: discrete_subtype_definition: static only cases?
  2020-06-07  9:27 Q: discrete_subtype_definition: static only cases? gautier_niouzes
@ 2020-06-07 10:40 ` J-P. Rosen
  2020-06-07 11:36   ` Niklas Holsti
  2020-06-07 12:13   ` gautier_niouzes
  2020-06-07 13:00 ` Jeffrey R. Carter
  2020-06-11  5:36 ` reinert
  2 siblings, 2 replies; 13+ messages in thread
From: J-P. Rosen @ 2020-06-07 10:40 UTC (permalink / raw)


Le 07/06/2020 à 11:27, gautier_niouzes@hotmail.com a écrit :
> Are there cases in the Ada syntax where a discrete_subtype_definition (RM 3.6 (6)) is expected to be determined at compile-time?
> 
> For instance, for array indices or "for" loops, it can be static or dynamic.
> Same for subtypes.

No. The basic principle is that types are static (they determine the
underlying representation) and subtypes are dynamic.

But nothing prevents a compiler from optimizing cases where the subtype
is actually static...

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr

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

* Re: Q: discrete_subtype_definition: static only cases?
  2020-06-07 10:40 ` J-P. Rosen
@ 2020-06-07 11:36   ` Niklas Holsti
  2020-06-07 12:05     ` Niklas Holsti
  2020-06-07 21:02     ` J-P. Rosen
  2020-06-07 12:13   ` gautier_niouzes
  1 sibling, 2 replies; 13+ messages in thread
From: Niklas Holsti @ 2020-06-07 11:36 UTC (permalink / raw)


On 2020-06-07 13:40, J-P. Rosen wrote:
> Le 07/06/2020 à 11:27, gautier_niouzes@hotmail.com a écrit :
>> Are there cases in the Ada syntax where a discrete_subtype_definition (RM 3.6 (6)) is expected to be determined at compile-time?
>>
>> For instance, for array indices or "for" loops, it can be static or dynamic.
>> Same for subtypes.
> 
> No. The basic principle is that types are static (they determine the
> underlying representation) and subtypes are dynamic.

Subtype indications in case statements and record type declarations with 
variant parts must be static.

But AFAIK subtypes used as array indices are never required to be static.

-- 
Niklas Holsti

niklas holsti tidorum fi
       .      @       .

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

* Re: Q: discrete_subtype_definition: static only cases?
  2020-06-07 11:36   ` Niklas Holsti
@ 2020-06-07 12:05     ` Niklas Holsti
  2020-06-07 21:02     ` J-P. Rosen
  1 sibling, 0 replies; 13+ messages in thread
From: Niklas Holsti @ 2020-06-07 12:05 UTC (permalink / raw)


On 2020-06-07 14:36, Niklas Holsti wrote:
> On 2020-06-07 13:40, J-P. Rosen wrote:
>> Le 07/06/2020 à 11:27, gautier_niouzes@hotmail.com a écrit :
>>> Are there cases in the Ada syntax where a discrete_subtype_definition 
>>> (RM 3.6 (6)) is expected to be determined at compile-time?
>>>
>>> For instance, for array indices or "for" loops, it can be static or 
>>> dynamic.
>>> Same for subtypes.
>>
>> No. The basic principle is that types are static (they determine the
>> underlying representation) and subtypes are dynamic.
> 
> Subtype indications in case statements and record type declarations with 
> variant parts must be static.
> 
> But AFAIK subtypes used as array indices are never required to be static.

However, RM 4.9.1 defines when two ranges statically match, and refers 
to the use that concept in RM 12.5.3, Formal Array Types. This may be a 
case where some index subtypes should be static for things to work as 
the programmer wants.

-- 
Niklas Holsti

niklas holsti tidorum fi
       .      @       .

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

* Re: Q: discrete_subtype_definition: static only cases?
  2020-06-07 10:40 ` J-P. Rosen
  2020-06-07 11:36   ` Niklas Holsti
@ 2020-06-07 12:13   ` gautier_niouzes
  2020-06-07 13:02     ` Jeffrey R. Carter
  1 sibling, 1 reply; 13+ messages in thread
From: gautier_niouzes @ 2020-06-07 12:13 UTC (permalink / raw)


> No. The basic principle is that types are static (they determine the
> underlying representation) and subtypes are dynamic.

Well, there are obviously exceptions to that basic principle:

procedure SD2 is
begin
  for I in 1 .. 3 loop
    declare
      type T is array (1 .. I) of Integer;  --  I is a variable
      B: T;
    begin
      null;
    end;
  end loop;
end;

:-)

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

* Re: Q: discrete_subtype_definition: static only cases?
  2020-06-07  9:27 Q: discrete_subtype_definition: static only cases? gautier_niouzes
  2020-06-07 10:40 ` J-P. Rosen
@ 2020-06-07 13:00 ` Jeffrey R. Carter
  2020-06-11  5:36 ` reinert
  2 siblings, 0 replies; 13+ messages in thread
From: Jeffrey R. Carter @ 2020-06-07 13:00 UTC (permalink / raw)


On 6/7/20 11:27 AM, gautier_niouzes@hotmail.com wrote:
> 
> procedure SD is
>    A : array (1 .. 5) of Integer;
>    type XYZ is (X, Y, Z);
> begin
>    for I in 1 .. 3 loop
>      declare
>        B : array (1 .. I) of Integer;  --  I is a variable
>      begin
>        for J in 1 .. I loop            --  I is a variable
>          B (J) := J;
>        end loop;
>      end;
>    end loop;
>    for E in XYZ loop
>      declare
>        subtype S is XYZ range X .. E;  --  E is a variable
>      begin
>        null;
>      end;
>    end loop;
> end;

The things you have marked as variables are all constants. They're not static, 
but you can't assign to them.

-- 
Jeff Carter
"How'd you like to hide the egg and gurgitate
a few saucers of mocha java?"
Never Give a Sucker an Even Break
101

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

* Re: Q: discrete_subtype_definition: static only cases?
  2020-06-07 12:13   ` gautier_niouzes
@ 2020-06-07 13:02     ` Jeffrey R. Carter
  2020-06-07 13:14       ` gautier_niouzes
  0 siblings, 1 reply; 13+ messages in thread
From: Jeffrey R. Carter @ 2020-06-07 13:02 UTC (permalink / raw)


On 6/7/20 2:13 PM, gautier_niouzes@hotmail.com wrote:
>> No. The basic principle is that types are static (they determine the
>> underlying representation) and subtypes are dynamic.
> 
> Well, there are obviously exceptions to that basic principle:
> 
> procedure SD2 is
> begin
>    for I in 1 .. 3 loop
>      declare
>        type T is array (1 .. I) of Integer;  --  I is a variable
>        B: T;
>      begin
>        null;
>      end;
>    end loop;
> end;

Conceptually, this declaration is equivalent to

type /anon/ is array (Integer range <>) of Integer;
subtype T is /anon/ (1 .. I);

So the (anonymous) type is static, but the (first-named) subtype is not.

-- 
Jeff Carter
"How'd you like to hide the egg and gurgitate
a few saucers of mocha java?"
Never Give a Sucker an Even Break
101

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

* Re: Q: discrete_subtype_definition: static only cases?
  2020-06-07 13:02     ` Jeffrey R. Carter
@ 2020-06-07 13:14       ` gautier_niouzes
  2020-06-07 13:50         ` AdaMagica
  0 siblings, 1 reply; 13+ messages in thread
From: gautier_niouzes @ 2020-06-07 13:14 UTC (permalink / raw)


On Sunday, June 7, 2020 at 3:02:56 PM UTC+2, Jeffrey R. Carter wrote:
> On 6/7/20 2:13 PM, gautier_niouzes@hotmail.com wrote:
> >> No. The basic principle is that types are static (they determine the
> >> underlying representation) and subtypes are dynamic.
> > 
> > Well, there are obviously exceptions to that basic principle:
> > 
> > procedure SD2 is
> > begin
> >    for I in 1 .. 3 loop
> >      declare
> >        type T is array (1 .. I) of Integer;  --  I is a variable
> >        B: T;
> >      begin
> >        null;
> >      end;
> >    end loop;
> > end;
> 
> Conceptually, this declaration is equivalent to
> 
> type /anon/ is array (Integer range <>) of Integer;
> subtype T is /anon/ (1 .. I);
> 
> So the (anonymous) type is static, but the (first-named) subtype is not.

...plus, the keyword "type" is used and not "subtype". And /anon/ could itself be dynamic (an array of a named dynamic (sub)type).
So basically types are static, except when they aren't ;-)

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

* Re: Q: discrete_subtype_definition: static only cases?
  2020-06-07 13:14       ` gautier_niouzes
@ 2020-06-07 13:50         ` AdaMagica
  2020-06-07 14:11           ` gautier_niouzes
  0 siblings, 1 reply; 13+ messages in thread
From: AdaMagica @ 2020-06-07 13:50 UTC (permalink / raw)


Am Sonntag, 7. Juni 2020 15:14:49 UTC+2 schrieb gautier...@hotmail.com:
> ...plus, the keyword "type" is used and not "subtype". And /anon/ could itself be dynamic (an array of a named dynamic (sub)type).
> So basically types are static, except when they aren't ;-)

Nonono, types in Ada are always anonymous. What you name in a *type declaration* is the *first subtype*.

type My_Int is range 1 .. 10;

The anonymous type of this type declaration comprises the complete set of mathematical integers (only restricted by storage). Named numbers make use of this fact:

Big: constant := My_Int'Base'Last + 10**100;

Here, the "+" operator is that of My_Int'Base.

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

* Re: Q: discrete_subtype_definition: static only cases?
  2020-06-07 13:50         ` AdaMagica
@ 2020-06-07 14:11           ` gautier_niouzes
  0 siblings, 0 replies; 13+ messages in thread
From: gautier_niouzes @ 2020-06-07 14:11 UTC (permalink / raw)


> Nonono, types in Ada are always anonymous. What you name in a *type declaration* is the *first subtype*.

OK, found the reference (3.2.1 (6)). Thanks.
So, what is declared following the keyword "type" is actually a "first subtype", right ?
Then, to summarize, whether you use the keyword "type" or "subtype", it is actually a subtype and can be dynamic (not too wysiwig, but there are surely some good reasons behind that concept).
Back to the question, the purely static cases of discrete_subtype_definition seem to be quite restricted (Niklas listed 2 cases - thanks!).

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

* Re: Q: discrete_subtype_definition: static only cases?
  2020-06-07 11:36   ` Niklas Holsti
  2020-06-07 12:05     ` Niklas Holsti
@ 2020-06-07 21:02     ` J-P. Rosen
  2020-06-08  4:55       ` gautier_niouzes
  1 sibling, 1 reply; 13+ messages in thread
From: J-P. Rosen @ 2020-06-07 21:02 UTC (permalink / raw)


Le 07/06/2020 à 13:36, Niklas Holsti a écrit :
> On 2020-06-07 13:40, J-P. Rosen wrote:
>> Le 07/06/2020 à 11:27, gautier_niouzes@hotmail.com a écrit :
>>> Are there cases in the Ada syntax where a discrete_subtype_definition
>>> (RM 3.6 (6)) is expected to be determined at compile-time?
>>>
>>> For instance, for array indices or "for" loops, it can be static or
>>> dynamic.
>>> Same for subtypes.
>>
>> No. The basic principle is that types are static (they determine the
>> underlying representation) and subtypes are dynamic.
> 
> Subtype indications in case statements and record type declarations with
> variant parts must be static.
> 
But these are subtype indications, not subtype definitions.


-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr

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

* Re: Q: discrete_subtype_definition: static only cases?
  2020-06-07 21:02     ` J-P. Rosen
@ 2020-06-08  4:55       ` gautier_niouzes
  0 siblings, 0 replies; 13+ messages in thread
From: gautier_niouzes @ 2020-06-08  4:55 UTC (permalink / raw)


On Sunday, June 7, 2020 at 11:02:29 PM UTC+2, J-P. Rosen wrote:

> But these are subtype indications, not subtype definitions.

RM 3.6 (6)
discrete_subtype_definition ::= discrete_subtype_indication | range

http://www.ada-auth.org/standards/2xrm/html/RM-3-6.html#p6

I'm afraid, Niklas has won this time :-) .

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

* Re: Q: discrete_subtype_definition: static only cases?
  2020-06-07  9:27 Q: discrete_subtype_definition: static only cases? gautier_niouzes
  2020-06-07 10:40 ` J-P. Rosen
  2020-06-07 13:00 ` Jeffrey R. Carter
@ 2020-06-11  5:36 ` reinert
  2 siblings, 0 replies; 13+ messages in thread
From: reinert @ 2020-06-11  5:36 UTC (permalink / raw)


I have made myself somehow similar questions, and found workarounds using subtype predicates. 

http://www.ada-auth.org/standards/12rat/html/Rat12-2-5.html

reinert


søndag 7. juni 2020 11.27.33 UTC+2 skrev gautier...@hotmail.com følgende:
> Hi,
> 
> Are there cases in the Ada syntax where a discrete_subtype_definition (RM 3.6 (6)) is expected to be determined at compile-time?
> 
> For instance, for array indices or "for" loops, it can be static or dynamic.
> Same for subtypes.
> 
> procedure SD is
>   A : array (1 .. 5) of Integer;
>   type XYZ is (X, Y, Z);
> begin
>   for I in 1 .. 3 loop
>     declare
>       B : array (1 .. I) of Integer;  --  I is a variable
>     begin
>       for J in 1 .. I loop            --  I is a variable
>         B (J) := J;
>       end loop;
>     end;
>   end loop;
>   for E in XYZ loop
>     declare
>       subtype S is XYZ range X .. E;  --  E is a variable
>     begin
>       null;
>     end;
>   end loop;
> end;
> _________________________ 
> Gautier's Ada programming 
> https://hacadacompiler.sourceforge.io/
> NB: follow the above link for a valid e-mail address

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

end of thread, other threads:[~2020-06-11  5:36 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-07  9:27 Q: discrete_subtype_definition: static only cases? gautier_niouzes
2020-06-07 10:40 ` J-P. Rosen
2020-06-07 11:36   ` Niklas Holsti
2020-06-07 12:05     ` Niklas Holsti
2020-06-07 21:02     ` J-P. Rosen
2020-06-08  4:55       ` gautier_niouzes
2020-06-07 12:13   ` gautier_niouzes
2020-06-07 13:02     ` Jeffrey R. Carter
2020-06-07 13:14       ` gautier_niouzes
2020-06-07 13:50         ` AdaMagica
2020-06-07 14:11           ` gautier_niouzes
2020-06-07 13:00 ` Jeffrey R. Carter
2020-06-11  5:36 ` reinert

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