Normal view

There are new articles available, click to refresh the page.
Before yesterdayAda Forum - Latest topics

2022 Day 20: Grove Positioning System

By: cantanima
20 December 2022 at 14:52

(edited after remembering another bad idea, whoops)

I had one very good idea, one good idea, and quite a few bad ideas, including one very bad idea that held me up a long time.

  • The very good idea was spoiler As a result, the solution to both parts takes only less than two seconds (using full optimization and inlining).
  • The good idea I don’t quite remember, sorry, but I had it in mind at one point. :grin:
  • One bad idea was spoiler. Fortunately, I caught that one quickly.
  • Another bad idea was spoiler Rather naughty of the example not to have a value that would illustrate that.
  • The very bad idea came while testing my code on the example. spoiler I wasted over an hour on that stupid mistake…

8 posts - 4 participants

Read full topic

2022 Day 19: Not Enough Minerals

By: cantanima
19 December 2022 at 13:43

Nope. I’ve been at this for hours.

  • I wasted hours just trying to get my code to run the example in a reasonable time, and when I gave up I discovered that the input takes not much longer to run than the example.

    So there’s a hint that doesn’t require a spoiler: don’t bother trying to get your program to work on the example in seconds; the input isn’t that much harder! Just try to get the program to work on the example, period!

  • Unfortunately, that’s not enough. The example doesn’t really illuminate the problem. Your code can run correctly on the example and run incorrectly on the problem input. This happens from time to time, but on a problem like today it’s ruinous.

  • To help me debug my program, I downloaded and ran several people’s solutions posted online (in other languages). I haven’t tried any of the solutions, but there’s a morbid satisfaction in the fact that different “solutions” produce different results on my input.

  • Some authors admit that they guessed at optimizations and got lucky; the optimizations probably don’t work in general. I’ve tried a few, and nope, they don’t work on my input (unless I’m implementing them wrong).

  • Some authors used parallelism. I may yet try that, since the problem is parallelizable, and otherwise I have to wait 5-10 minutes per attempt.

In short, I haven’t finished it yet. IMHO the puzzle is terribly flawed, for the reasons given above. And, yeah, sour grapes, too. :grin:

8 posts - 3 participants

Read full topic

Am I doing something wrong here?

By: cantanima
19 December 2022 at 00:38

I use Ada for my personal projects, largely out of admiration for the language, but I’m entirely self-taught in Ada, and the literature on its usage is… well, somewhat scant in comparison to, say, C++.

Not every language is perfect, but some things in the standard Ada library seem far too cumbersome. So much so, that I have to wonder if I’m doing things right. I’ve looked at the ARM, perhaps not well, and rosetta code is down at the moment, so I thought I’d inquire here.


Setting up a queue seems to involve way more effort than it should:

  • You have to include two separate packages:
    • Containers.Synchronized_Queue_Interfaces
    • Containers.Unbounded_Synchronized_Queues or one of the other three variants
  • You have to use all type Ada.Containers.Count_Type if you want to check whether it’s empty.

The payoff? For all the effort you put into creating it, the resulting queue offers less functionality than other languages’. All you can do is Enqueue, Dequeue, and check usage. You can’t iterate through the queue to see what’s there and possibly modify it, nor search through it, let alone split the queue. There isn’t even an .Is_Empty method to tell you when it’s exhausted.

Queues are pretty common, and I’ve never seen one so bare bones. Is there something more functional than this in the standard library? If not, do people recommend a different library (e.g,. the Simple Components library) or do you typically roll your own?

(I realize there may be good reasons for the library to do things this way; I’m just wondering what people do about it.)

Iteration through maps

Suppose I set up a map M and want to iterate through it.

  • for E of M iterates through the map’s elements, not through its keys. I wish they had returned the keys instead, since often that’s what I want, and it’s easy to obtain the elements from the keys, and not so much the converse. But this does make sense.
  • for C in M.Iterate iterates a cursor through the key/pair combinations. This makes sense, too. But…
  • Using Cursor is, again, cumbersome. As far as I can tell, I can’t do this: C.Key or C.Element. Instead, I have to with all type Map_Type.Cursor to get Key(C) and Element(C), or else qualify the functions fully: Map_Type.Key(C) or Map_Type.Element(C).

(I think Ada 2022 introduces functionality to let me treat a cursor the way I want, but if so, I have to update my gnat: version 20210519-103.)

So what am I doing wrong?

Have I missed something, or is this indeed the standard way of doing these things in Ada? I would be grateful for any direction, because I don’t use Ada often enough to remember these two things, and the compiler’s help error messages are not the greatest.


I apologize if it comes across as a complaint. I realize there’s no perfect language, and just want to know if I’m missing something, because Ada does so many things right. I can make much, much longer lists of my complaints with C++, and a somewhat longer list of complaints with Rust, than I can with Ada.

9 posts - 4 participants

Read full topic

2022 Day 18: Boiling Boulders

By: wutka
18 December 2022 at 16:52

It was nice to get an easy one after the past two days. The only hard thing was that gnatprove was either hanging or crashing, and I guessed that it didn’t like all the nested-nested-nested loops. I rewrote it so I generated an array of all coordinates and looped through that instead of the nested loops and I was able to get gnatprove to finish.

In case anyone needs help with part B:

8 posts - 4 participants

Read full topic

2022 Day 17: Pyroclastic Flow

By: cantanima
17 December 2022 at 12:16

Part 1 is pretty straightforward, I think.

Part 2 requires some mathematics, or at least that’s what got me to solve it quite quickly I did one other thing, too.

The solution I uploaded for part 2 will probably not work on the example program, because the example’s sequences aren’t quite as well-behaved as the actual puzzle, which made the problem a little harder. But the same technique really does work!

2 posts - 2 participants

Read full topic

Ada Drivers Library

By: ptihanyi
15 December 2022 at 15:02

Could you help me how I can build an arbitrary stm32f429-discovery example project from Ada_Drivers_Library?
I am using Ubuntu linux, installed on Ali and can create/build native projects.
I installed the gnat-arm-elf toolchain with alr.
I tried the following:
I cloned the repository, and created a library project in-place with alr in the repo directory. Then I selected gnat-arm-elf toolchain.
Then I issued this:
alr exec – ./scripts/

The process stops with the following error:

ROOT_DIR :/home/ptihanyi/adawsp/Ada_Drivers_Library
/home/ptihanyi/adawsp/Ada_Drivers_Library/bb-runtimes already cloned
Running build command:
$ /usr/bin/python3 /home/ptihanyi/adawsp/Ada_Drivers_Library/bb-runtimes/ --arch=arm-eabi --prefix=/home/ptihanyi/.config/alire/cache/dependencies/gnat_arm_elf_12.2.1_9be2ca0e/bin/../arm-eabi/lib/gnat
install runtime sources for stm32f4

Dependency install command error (returned 1):
Traceback (most recent call last):
  File "/home/ptihanyi/adawsp/Ada_Drivers_Library/bb-runtimes/./", line 317, in <module>
  File "/home/ptihanyi/adawsp/Ada_Drivers_Library/bb-runtimes/./", line 293, in main
    projects += installer.install(
  File "/home/ptihanyi/adawsp/Ada_Drivers_Library/bb-runtimes/support/bsp_sources/", line 168, in install
    runtime_sources = self._find_rts_sources(destination, rts_descriptor)
  File "/home/ptihanyi/adawsp/Ada_Drivers_Library/bb-runtimes/support/bsp_sources/", line 145, in _find_rts_sources
    assert ret is not None, "Cannot find %s" % rts_json_file
AssertionError: Cannot find rts-sources.json
Traceback (most recent call last):
  File "/home/ptihanyi/adawsp/Ada_Drivers_Library/bb-runtimes/", line 74, in <module>
  File "/home/ptihanyi/adawsp/Ada_Drivers_Library/bb-runtimes/", line 70, in main
  File "/usr/lib/python3.10/", line 369, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/usr/bin/python3', './', '--build', '--force', '--output=/home/ptihanyi/.config/alire/cache/dependencies/gnat_arm_elf_12.2.1_9be2ca0e/arm-eabi/lib/gnat', 'stm32f4', 'nucleo_f401re', 'stm32f429disco', 'stm32f469disco', 'stm32f746disco', 'stm32756geval', 'stm32f769disco', 'samg55', 'sam4s', 'samv71', 'openmv2', 'rpi2', 'feather_stm32f405', 'stm32f051r8-hsi', 'nrf52832', 'nrf52840', 'cortex-m0', 'cortex-m0p', 'cortex-m1', 'cortex-m3', 'cortex-m4', 'cortex-m4f', 'cortex-m7f', 'cortex-m7df', 'cortex-m23', 'cortex-m33f', 'cortex-m33df', 'rpi-pico', 'rpi-pico-smp']' returned non-zero exit status 1.

Many thanks!

9 posts - 3 participants

Read full topic

Treesitter grammar for Ada

By: ebriot
15 December 2022 at 08:15

I have recently completed an Ada grammar for TreeSitter.

TreeSitter is meant to be a fast parser used by editors (currently Emacs and neovim, that I know of) or various platforms when they syntax-highlight code (github uses treesitter I believe). The tree-sitter-ada page above gives some hints on how things should be setup in neovim, but I have no recent experience with Emacs.

I am in the process of submitted that to treesitter-nvim and treesitter projects, to make the whole setup easier.

The grammar is strongly based on the one in the Ada Reference Manual, but also draws some ideas from the similar grammar that Stephen Leak has written for the Emacs ada-mode.

There is a reasonable testsuite in place, and I am able to parse all sources from the GNAT runtime and the 3 million lines of Ada code at my company without errors (which of course doesn’t mean that I get a meaningful tree in all cases).

TreeSitter is actually a multi-staged process:

  • we need a grammar to create a tree (which is supposed to be much simpler than an AST like a compiler or libadalang would use, and could even have ambiguities).
  • each editor then needs various modules to take advantage of treesitter, like supporting syntax highlighting, code folding, jumping to various constructs, indentation,…
  • each language need to provide the actual queries. My github repository provides support for syntax-highlighting, block folding and jumping, but not indentation yet.

Syntax-highlighting is a nice example: compared to regex-based highlighting, we can perform more advanced things, like highlighting subprogram specs, underlining parameter names, italicizing gnatprep if-statements, …

Compared to the current vim ada mode, treesitter provides much better support for code folding (“za”), since it knows the bounds for a subprogram or package body, if-statement and other loops.


5 posts - 4 participants

Read full topic

2022 Day 15: Beacon Exclusion Zone

By: cantanima
15 December 2022 at 07:42

Only 10 days left!

My solution to Part 1 was quite slow, perhaps because I used an OrderedSet. It might be worth retrying with a HashedSet at some point.

So I was really worried that Part 2 would be one of those problems whose obvious solution is to apply Part 1, but whoses complexity explodes on you, and as I first began to read it, my heart filled with dread. But then I realized that spoiler It still takes a few moments, but it worked! So I will be interested in seeing if anyone manages to solve it in a fraction of a second, and if so, then how.

I’m also amazed that Part 2 can even be a problem; the fact that he pulled off such a puzzle impresses me. Maybe it won’t impress me if I think about it enough, but even after a couple of glances, I’m still impressed. Maybe I just need some :sleeping_bed:

8 posts - 4 participants

Read full topic

Ada Behind A Historical Milestone

13 December 2022 at 18:30

Many of you have probably heard the news that Lawrence Livermore’s National Ignition Facility (NIF) has achieved net-energy production fusion (ignition):

Well it turns out that, at a minimum, the control system of the giant laser that makes NIF possible was written in Ada.

So looks like Ada software has now had a key role in what could be a new era in human achievement!

Very cool stuff.

12 posts - 6 participants

Read full topic

2022 Day 13: Distress Signal

By: cantanima
13 December 2022 at 09:01

We’re past halfway! :grin: :dark_sunglasses: :grinning: :sunglasses: :fireworks: :sparkler: :partying_face:

(The downside is that the problems have suddenly stepped up in difficulty!)

For the second day in a row, writing a program that solves the example wasn’t enough. Twice I ran it and got solutions that were rejected as too large. I had to comb through some of the pairs that my program claimed were legitimate. In both cases, it was the case where I had to compare a number and a list. The instructions are to create a new list that includes the number, then compare the lists, but it seemed equivalent to me simply to compare the number with the first element of the list. This produced false positives. I haven’t quite figured out why, though.

Curiously, in part 2 I was able to make it work simply by spoiler

5 posts - 5 participants

Read full topic

Various questions about Ada

By: HonkiTonk
10 December 2022 at 16:38

I have a few Ada questions that I’ll just summarize in one post.

If I always have to completely rewrite a file, then it makes no difference whether I use Open or Create. Is that correct or did I miss something?

Currently I use Stream_IO to write files, the files contain different data types. Do other writing methods, such as Sequential_IO, produce smaller files that justify the extra effort to use them? Or are they practically all equally efficient?

I use Pure, Preelaborate and Elaborate_Body whenever possible. However, I have some files that don’t allow Pure/Preelaborate and since they don’t have a body, Elaborate_Body isn’t possible either. However, I would still like to know if there is a circular dependency. Are there any other options besides giving all files a body?

Is there a standard function that returns the number of elements in an enum subtype, or do I have to write it myself?

6 posts - 4 participants

Read full topic

Aggregates as initial value of an array

10 December 2022 at 10:41

I’m stuck with this array construct.
Can’t find the syntax clue to initialize an array of records, with the particularity I aim to pass the array as an in out variable, without needing to pass en extra index (ID) info.

(for K_ID in Knot_ID => ^
Compile error: missing “,” at ^

even with -gnat2022 and -gnatX switches, or pragma Ada_2022;

   subtype Knot_ID is Integer range Head_Knot .. Tail_Knot;
   type Grid_Position is record 
      X : X_Dimension;
      Y : Y_Dimension; 
   end record;

   type Knot_record is record
      ID : Knot_ID;
      Pos : Grid_Position;
   end record;

   type Rope_array is array (Knot_ID'Range) of Knot_record;


   Rope_Positions, Previous_Rope_Positions : Rope_array := 
--    (others => (ID => Head_Knot, Pos => (0,0)));  --:FIXME:  ID => Knot_ID'Range
      (for K_ID in Knot_ID => (ID => K_ID => (ID => K_ID, Pos => (0,0))));

see Ada 2012/2012 Array Aggregates
see Overview of Ada 2022
Compiler is FSF gcc-gnat 12.2.0


3 posts - 2 participants

Read full topic


9 December 2022 at 23:51

I’ve been playing with optimizations for an instance of Ada.Containers.Hashed_Sets for the Advent of Code Day 9 puzzle.

RM A.18.1 The Package Containers says:

type Hash_Type is mod implementation-defined;

With the Implementation advice:

Hash_Type'Modulus should be at least 2**32.

The annotated RM expands upon this:

This is not a requirement so that these types can be declared properly on machines with native sizes that are not 32 bits. For instance, a 24-bit target could use 2**24 for Hash_Type'Modulus.

In GNAT’s, the definition is simply:

type Hash_Type is mod 2**32;

Would it make sense to use System.Max_Binary_Modulus instead, so that 64-bit systems can benefit from a larger hash space?

type Hash_Type is mod System.Max_Binary_Modulus;

If we want to retain the minimum 32-bit size, a conditional could be added:

type Hash_Type is mod (if System.Max_Binary_Modulus >= 32 then System.Max_Binary_Modulus else 32);

1 post - 1 participant

Read full topic

2022 Day 9: Rope Bridge

9 December 2022 at 06:32

I think the problem statement was intentionally difficult to parse this time. Those conditionals are a lot simpler than it makes them sound.

I kept the visited positions in a Hashed_Set and coming up with a good Hash function for a pair of Integers was new for me. I ended up making a lot of assumptions (Integer'Size = 32 and Hash_Type'Size = 64) and just shifted one of the integers left 32 bits. A cursory Google search turned up the Cantor Pairing function, but I wasn’t able to make this work with negative integers in a short amount of time. Clearly I need to do more reading.

Part 2 caught me off guard, I hadn’t anticipated I’d need to deal with more elements.

Note: I’m going to stop using the spoiler blur on the daily posts. If you’ve gotten this far, I assume you know that these posts contain spoilers.

13 posts - 8 participants

Read full topic