❌ About FreshRSS

Reading view

There are new articles available, click to refresh the page.

Is there a good way to handle exceptioins in the declarative region of a function

While working on day 6 of AoC, I found myself breaking a larger calculation into parts and saving them as constants in the declarative region of the function. This was super clean for reading and let me document all my steps nicely. I also knew that if any of the steps failed, then my result should be 0. So I figured I would put an exception handler in the function and return 0 (Note that the case where exceptions happen would be pretty rare based on some really edge case inputs that weren’t practical to the problem):

The problem I ran into is if the early steps raised an exception, then the exception handler wouldn’t catch them as they were in the declarative region. See mock up below:

function Calculate(Inputs : Input_Type) return Integer is
   Step_1 : constant Integer := Some_Function(Stuff);
   Step_2 : constant Integer := Some_Other_Function(Stuff);
   return Final_Function(Step_1, Step_2);
   when others => return 0;  -- doesn't catch Step_1 or Step_2 exceptions!!!
end Calculate;

Any suggestions on a clean way to catch those declarative exceptions? I don’t like putting them in a declare block in the function body because that just indents everything needlessly. Currently I renamed my original function to old_name_unhandled and call it a new wrapper function named the original name, but that does feel kinda silly.

I was hoping there is an aspect or pragma maybe?

6 posts - 5 participants

Read full topic

Day 5: If You Give A Seed A Fertilizer

@JeremyGrosser hasn’t posted on today’s puzzle yet; I hope he won’t mind if I do.

There’s always at least one puzzle with ginormous numbers, though I don’t recall their appearance in as early as Day 5!

The first speed bump I encountered is that gnat defaults (?) to 32-bit integers for Natural, so my machine choked, though nearly 12 hours later I can’t remember whether it choked on the input proper or during the tracing. Very well, then: spoiler

Is 32-bits a fixed standard for Ada’s Natural? Since 64-bit numbers are not uncommon in these puzzles (at least 2 or 3 puzzles will go there), should I by default define a numeric type and use that? (Isn’t that arguably the Ada way anyway? I’ve always wished the puzzle master would be more specific about the data we might encounter in these puzzles.)

The second speed bump is that solving Part 2 the natural, “brute-force” way takes the computer while, and I mean a while. It is doable! It took my machine “only” about 15 minutes. (I’ve paid for those gigahertz; might as well use them, right?)

So, while I was able to solve Part 2 via brute force – not usually possible when we start to see 6-digit numbers, let alone 9-digit ones! – I decided to spoiler.

My solution is here. I’d be interested to know if anyone took a different approach.

17 posts - 5 participants

Read full topic

Volatile records containing an array

svd2ada generates volatile arrays.

e.g. IMR_MR_Field_Array

There are two other options that I believe will work easily with spark. The .Val Hal.UInt32 or a record of booleans with different names instead of the array.

The array makes for simpler code. So before I switch to the value. Could anyone suggest how the Spark compatibility error may be avoided. Editing the svds isn’t ideal but possible. A function or procedure local instantiation would be ideal. Any ideas?

IMR1.MR.Arr(Var) := True;

error: volatile object cannot appear in this context. Spark RM 7.1.3 (10).


2 posts - 1 participant

Read full topic

AdaCV - OpenCV but Ada

Hi everyone,

I’ve been a long time lurker on the Ada subreddit and was directed here by Lucretia to share my plan/idea. After a few years in Python and short, unhappy tread through the chaos of C++, I’m learning Ada, while I’m still new to the language, I have a project idea. I want to make sure I don’t reinvent the wheel and that I engage with anyone else who is interested.

Are you familiar with OpenCV? If not, it’s a very good computer vision library in C++ and Python.

Well I have a several years experience with it in both C++ and Python (and the science/math directly). My interpretation is that, while the basics can be easy to use, the more complicated functions (Stereo Calibration, triangulation, really anything with photogrammetry) are nuanced and somewhat unforgiving. A lot of it is driven by poor examples and the poor documentation on what you’re actually getting. For example, it doesn’t talk about what units is a particular return value is in? Distance units or a some normalized unit? What’s expected as the input? A vector of vectors of a custom cv::Point2f two dimension float type. Stack overflow is filled with questions where people don’t get much help and their answers are met more with theory photogrammetry and I never see actual usage help or answers. It’s just like the documentation: theory heavy, usage thin, typing vague. A more… user friendly library with thorough usage documentation would be very popular if it was genuinely easy to use.

I’m sure you see where this is going but please let me finish:

Ada is the language of reliability and safety. Look at the popular and booming Tech industries, two relevant highlights are Autonomous Vehicles and Augmented Reality. Both use imaging processing and photogrammetric techniques. If an ADA based package or library that was easily usable and accurate, while having the reliability and safety of ADA, it could bring a lot of new people, companies, and industries to the language.

So anyways, the idea is AdaCV. A potentially slimmer but more easily usable and user friendly OpenCV in Ada.

Anyone working on that? Anyone finished it? Thoughts? Objections?

7 posts - 4 participants

Read full topic

2023 Day 3: Gear Ratios

Got stuck on part 1 because I had my unit test script using yesterday’s input!
Part 2 went okay, just required some careful reading.

Nothing about my solution feels optimal in terms of CPU cycles. Looking forward to seeing how everyone else decides to model this one.

5 posts - 3 participants

Read full topic

[Charity] Advent of Ada/SPARK 2023 Submissions

Hello here,

Today AdaCore announced a second edition of the Advent of Ada/SPARK charity event. For each person completing one of the Advent of Code challenges using the Ada programming language, AdaCore will donate $10 to the Ada Developers Academy, up to a total of $5,000. And for those willing to go an extra mile, AdaCore will donate $20 if the solution is implemented in SPARK with at least proof of absence of run-time errors (a.k.a. Silver level).

To submit your solutions, reply to this post with a line of the following format:
[<pseudonym>][<day>][<Ada or SPARK>]<link to solution source code>

For instance, if I solve the 3rd day problem using Ada:
[Fabien-Chouteau][3][Ada] https://github.com/Fabien-Chouteau/advent-of-ada/

And the 5th day using SPARK:
[Fabien-Chouteau][5][SPARK] https://github.com/Fabien-Choutea/advent-of-ada/

Happy hacking!

58 posts - 13 participants

Read full topic

Elaboration circularity detected

Hi, I am new in this forum.
I have a problem porting ada code from Red Hat 8 (gnat 8.5.0) to Red Hat 9 (gnat 11.3.1).
The linking phase on RHEL9 fails for “Elaboration circularity detected”.
I let the ada compiler to choose the module compilation order using the following instruction:
gnatmake -c
The reason is: “unit depends on its own elaboration”
The circularity detected is: "unit invokes a construct of unit at elaboration time.
What shall I do to find the exact location of the problem?
And why this problem was not detected before using other gnat versions?
Thanks a lot

7 posts - 4 participants

Read full topic

Possible compiler bug?

I have some code that is running fine for me at home using gnat/gcc 12.2. I decided to test it out on various versions of gnat, so I fired up godbolt and tried a few. I found that starting with version 13, my code throws an exception when the program ends:

raised PROGRAM_ERROR : example.adb:4 finalize/adjust raised exception

I wanted to double check and make sure I am not doing anything illegal and 13 catches it vs 13 just having a bug. I had to make a kinda convoluted example since the original code was from a much larger code base, so apologies if the generic formals and code don’t look too practical. I spent a few hours whittling it down from the original. Full code on godbolt is here:

Code is below:


with Ada.Text_IO; use Ada.Text_IO;
with Test;

procedure Example is

    package B is 
        type Instance is limited interface;
        function Make return Instance is abstract;
    end B;

    package C is
        type Instance is new B.Instance with null record;
        function Make return Instance is (null record);
    end C; use C;

    package T is new Test(B.Instance, C.Instance, C.Make);

    Thing : B.Instance'Class := T.Make(2);

    Put_Line("Hello World");
end Example;


    type First(<>) is abstract tagged limited private;
    type Second(<>) is new First with private;
    with function Make return Second is <>;
package Test is

    function Make(Key : Integer) return First'Class;


    type Some_Access is not null access function return First'Class;

    function Make_Delegate return First'Class;

    Thing_Access : constant Some_Access := Make_Delegate'Access;

end Test;


with Ada.Containers.Indefinite_Ordered_Maps;

package body Test is

    package Maps is new Ada.Containers.Indefinite_Ordered_Maps
        (Key_Type     => Integer,
         Element_Type => Some_Access);

    Map : Maps.Map;

    function Make(Key : Integer) return First'Class is
        return Map(Key).all;
    end Make;

    function Make_Delegate return First'Class is
        return Make;
    end Make_Delegate;

end Test;

It’s essentially a map of function pointers (only one in this example). I had to use a wrapper function (Make_Delegate) in order to have correct accessibility to it within the generic.

7 posts - 3 participants

Read full topic

AEiC 2024 - Ada-Europe conference - 2nd Call for Contributions

The 28th Ada-Europe International Conference on Reliable Software Technologies (AEiC 2024) will take place in Barcelona, Spain from 11 to 14 June, and comprises different tracks and co-located events.

Submission deadlines: 15 January for journal track papers; 26 February for industrial track and work-in-progress track papers, tutorial and workshop proposals. Submit early: tutorial/workshop proposals will be evaluated ASAP, with decisions from 1 January 2024!

More information on the conference site, including an extensive list of topics, and details on the call for contributions for the various tracks.


#AEiC2024 #AdaEurope #AdaProgramming

1 post - 1 participant

Read full topic

Gnatd.v elaboration

I wonder. Is gnatd.v a better option than
pragma preelaborate

Is preelaborate more compiler portable? Though I do not use the features that it protects against such as dynamic dispatch, so it is simply an unneeded restriction for me with Gnat. However gnatd.v seems to offer more such as in regard to uninitialised variables? Assuming it works as I had no compile issues :joy:.

see 7.7


2 posts - 2 participants

Read full topic