Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Fixed array lower bound #38

Merged
merged 3 commits into from
Mar 3, 2020
Merged

[RFC] Fixed array lower bound #38

merged 3 commits into from
Mar 3, 2020

Conversation

QuentinOchem
Copy link
Contributor

@QuentinOchem QuentinOchem commented Feb 28, 2020

This is an initial proposal allowing to fix the lower bound of an array type, in an attempt at fixing some Ada-specific performance footprint.

Link to text: https://github.com/QuentinOchem/ada-spark-rfcs/blob/master/considered/rfc-lower-bound.rst

@QuentinOchem QuentinOchem changed the title Initial proposal Fixed array lower bound Feb 28, 2020

.. code-block:: ada

V : A (10);
Copy link
Contributor

@yakobowski yakobowski Feb 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This syntax is intrinsically ambiguous. A(n) could mean either A (<lower bound> .. N) (as you probably intended) or "an array of N elements", i.e. A (<lower bound> .. <lower bound> + N -1). I'm not sure it is worth it to introduce another syntax oddity, and I would suggest dropping it altogether.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this part of the proposal might need to be removed or a different syntax used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - I agree with @yakobowski and @jeremiahbreeden - I'm not to happy with that bit either. I'll move this to the "Future possibilities" section.

@kanigsson
Copy link

There are in fact two ways (I know of) to force a fixed lower bound:

   type My_String is array (Integer range <>) of Character;

   type String_Based_Zero (Len : Integer) is record
      Data : My_String (0 .. Len);
   end record;

and

   type My_String is array (Integer range <>) of Character
   with Predicate => My_String'First = 0;

Maybe the compiler could be made smarter to optimize code in both cases. I think your proposed change is very heavy in comparison to the expected performance gain.

@jere-software
Copy link

jere-software commented Mar 2, 2020

There are things to consider here though:

type My_String is array (Integer range <>) of Character;

type String_Based_Zero (Len : Integer) is record
Data : My_String (0 .. Len);
end record;


I do this one a lot, but I will say this is one of the few cases where I feel like Ada pushes me into a bad or unfortunate design. I'm not using a record because my software problem is better solved by a record. I'm using a record because Ada can't do this any better way. This adds noise to my designs and makes it harder to understand for the average and novice reader (I've experienced trying to explain what this is first hand).

   type My_String is array (Integer range <>) of Character
   with Predicate => My_String'First = 0;

I would like this if the compiler could enforce it statically when possible (and ideally without assertions enabled), but this appears to be a dynamic only predicate, which is unfortunate. But that means Ada would need better compile-time support defined in the language spec. Sure a compiler vendor could do it themselves, but not being able to rely on the languages rules for stuff like is an awful place to be. One of the best things C++ did (at least for the embedded landscape) was the constexpr rules. It gave a specific set of rules for when things "must" be evaluated at compile time by the compiler but the flexibility to let the same code be used dynamically if the inputs were not compile-time.

@QuentinOchem
Copy link
Contributor Author

There are in fact two ways (I know of) to force a fixed lower bound:

   type My_String is array (Integer range <>) of Character;

   type String_Based_Zero (Len : Integer) is record
      Data : My_String (0 .. Len);
   end record;

This one is actually slightly misleading as Len is really the Last element of the array, not its length. But yes, and documented in the proposal.

and

   type My_String is array (Integer range <>) of Character
   with Predicate => My_String'First = 0;

Maybe the compiler could be made smarter to optimize code in both cases. I think your proposed change is very heavy in comparison to the expected performance gain.

I think this one will open a whole bunch of difficult questions. What if you want more than this condition in the predicate? Do you have another predicate? Overall, I would argue that relying of arbitrary predicate analysis for structural decision is a bit weak.

@yannickmoy yannickmoy changed the title Fixed array lower bound [RFC] Fixed array lower bound Mar 3, 2020
@yannickmoy yannickmoy added RFC enhancement New feature or request labels Mar 3, 2020
@sttaft
Copy link
Contributor

sttaft commented Mar 3, 2020 via email

@raph-amiard raph-amiard merged commit fd049a4 into AdaCore:master Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request RFC
Projects
Development

Successfully merging this pull request may close these issues.

7 participants