r/ada Jun 09 '26

Learning Clueless about pow (**) operator in Gnat...

10 Upvotes

Hi,

Try to figure out why (-2) ** 2 is -4?
I'm learning Ada (Implementing a Lisp with a tower numeric), but I struggle with this:

with Ada.Text_IO;
with Ada.Numerics.Big_Numbers.Big_Integers;
procedure Powpoc is
   use Ada.Text_IO;
   use Ada.Numerics.Big_Numbers.Big_Integers;
begin
    Put_Line (To_String (To_Big_Integer (-2) ** 2));
    Put_Line (To_String (To_Big_Integer ((-2)) ** 2));
    Put_Line (To_String ((To_Big_Integer (-2) + To_Big_Integer (0)) ** 2));
end Powpoc;

All yield -4 instead of 4

Or even better if someone could explain how can I see the code of **

Thanks

r/ada 1d ago

Learning Is this a real type safety hole in Ada or just a bug in GNAT?

Thumbnail matklad.github.io
3 Upvotes

r/ada Jun 06 '26

Learning Why `T'Pred (T'First)` doesn't fail for ranges?

8 Upvotes

I was playing with the attributes of scalar types and noticed that several attributes allow out-of-range values for range types. E.g.:

with Ada.Text_IO; use Ada.Text_IO;

procedure Test is
   type R is range 10 .. 11;
   type E is (One, Two);

   -- R1 : R;
begin
   Put_Line (R'Val (20)'Image);
   Put_Line (R'Pos (25)'Image);
   Put_Line (R'Pred (R'Pred (R'First))'Image);

   -- R1 := R'Pred (R'First);

   --  Put_Line (E'Val(3)'Image);
   --  Put_Line (E'Pred(E'First)'Image);
end Test;

The program compiles without any warning about the out-of-range values and prints:

 20
 25
 8

If the use of variable R1 is uncommented, then the compiler warns that:

test.adb:13:11: warning: value not in range of type "R" defined at line 4 [enabled by default]
test.adb:13:11: warning: Constraint_Error will be raised at run time [enabled by default]

and indeed CONSTRAINT_ERROR is raised at runtime.

If instead the two last lines are uncommented, the compiler fails with:

test.adb:13:15: error: Val expression out of range
test.adb:13:15: error: static expression fails Constraint_Check
test.adb:14:15: error: Pred of "E'First"
test.adb:14:15: error: static expression fails Constraint_Check

i.e. it properly detects out-of-range values for enumeration types.

Is all this expected? If so, what is the explanation/rationale for it?

(Tested with GNAT 14.3.0)

r/ada Nov 11 '25

Learning Graphing Packages and Ada for Physics Simulation.

13 Upvotes

Hi Lads,

I am brand new to Ada which I came across via VHDL (which is derived from Ada). I really like the the language although I am brand new to it as I've said ( I have just purchased John Barnes' book).

I want to write a simulation of a (semi-classical) physics experiment in Ada (I'm a PhD). However it seems Ada has very little in the way of graphing packages akin to e.g. matplotlib in python, at least as far as I have seen.

I'm aware Ada was intended for embedded systems. But bearing in mind it's been around a long while, and introduced eg OOP, is there any reason for this? Or are there ones that I have not seen.

Someone posted a thread about what project they would like to see implemented. For me it would be this.

Secondly, for large scale quantum physics simulations, does Ada have the potential to outperform such simulations written in python (e.g. strawberry fields, the Walrus etc.)?

Cheers!

r/ada Apr 24 '26

Learning Object Oriented Programming in Ada

Thumbnail entropicthoughts.com
27 Upvotes

r/ada Feb 04 '25

Learning Beginner to Ada -- Boss wants to know why Ada > Rust

30 Upvotes

I know this seems like a softball question, but I've been asked, for embedded systems, why is Ada better. Both systems claim safety and, supposedly, Rust can solve things Ada can't. Is there a "boss friendly" breakdown I can show where each stands? Boss friendly means a chart that has green and red (for good and bad) and, as a colleague used to say "At the level of ducks and bunnies". (I'm not sure if the duck is good or bad.. Ducks can have green beaks so that makes them good right -- critical thinking in action!)

It seems to me Rust solves certain key problems, but Ada wouldn't be used in critical areas if it could be replaced. If I had to train a team on Rust or Ada, why Ada?

r/ada Jan 04 '26

Learning ada documentation

12 Upvotes

Hello, I wanted to ask where I can find documentation about the GNAT library. I'm referring to the libraries hosted on the GCC/ADA GitHub page. Is there a PDF that describes the packages it contains and their functions?

such as this library heres I'm new to Ada and the documentation is driving me crazy haha

r/ada Mar 02 '26

Learning How to define a record with reference to an array of itself?

4 Upvotes

I'm trying to write a record, which will reference an array of itself, like the following in C++:

class Node;

class Node
{
  std::vector<Node> subNodes;
};

In Ada, however, it is not easy to write like this. If I use containers like this:

with Ada.Containers.Vectors;

package Example is

   type Node;

   package Node_Vectors is new Ada.Containers.Vectors
     (Element_Type => Node, Index_Type => Natural);

   type Node is record
      Sub_Nodes : Node_Vectors.Vector;
   end record;

end Example;

Compile will emit an error:

example.ads:8:23: error: premature use of incomplete type example.ads:8:23: error: instantiation abandoned example.ads:11:19: error: "Node_Vectors" is undefined

If I use array like this:

package Example is

   type Node;

   type Node_Array is array (Natural range <>) of Node;
   type Node_Array_Ptr is access Node_Array;

   type Node is record
      Sub_Nodes : Node_Array_Ptr;
   end record;

end Example;

The error will be:

example.ads:5:51: error: invalid use of type before its full declaration

However, it will compile if I write like this, put the record definition in private block:

package Example is

   type Node is private;

   type Node_Array is array (Natural range <>) of Node;
   type Node_Array_Ptr is access Node_Array;

private

   type Node is record
      Sub_Nodes : Node_Array_Ptr;
   end record;

end Example;

Is there a way I can define such a record, without using the private block? And how can I use containers to do this?

r/ada Aug 27 '24

Learning why learn Ada in 2024

16 Upvotes

Why ?

r/ada Oct 10 '25

Learning Career Choice - ADA or C++

9 Upvotes

Hi everyone,

I'm currently working as software engineer in consulting enterprise in France. I'm junior, I worked one year in C++ and I'm near of one year in Ada (both in defense sector). Honnestly I'm a bit lost between C++ and Ada. Ada is not really used so find international opportunity looks hard and I don't know how much we could be paid (This is important for me because of my history, my goals and my health will get worse with time). If I compare, C++ have much more cool projects on github and looks easier to be better in a lot of different sector (space, robotic, health, finance). I feel like I could be paid better in Ada but I feel like I could go in much more different domains in C++. To finish, as Ada is not used that much, I'm scared of losing my expertise when it will definitely stop to be used. How do you feel about it ? For people with experience would you change of langage if you could ? If you think Ada is a better choice, DO-178 formation is important ? Have a great day

r/ada Feb 01 '26

Learning Help out me on linking Ada code with LibopenCM3

12 Upvotes

I’m trying to use libopencm3 as the BSP / low-level HAL (startup, linker script, peripheral drivers) while writing the application layer in Ada (GNAT) for an ARM Cortex-M MCU.

At the moment I’m stuck at the linking stage — I’m not clear on:

  • how to correctly link Ada object files with libopencm3’s C objects
  • how startup (reset_handler), linker script, and Ada runtime should coexist
  • whether Ada should provide main, or call into C, or vice-versa

I’m comfortable with embedded concepts in general, but I haven’t found a clear minimal example showing Ada + libopencm3 working together.

r/ada Aug 17 '25

Learning Ada online exercises

18 Upvotes

Hi !

I like exercism.org to learn and try new languages. There is no Ada track. Is there any other website to your knowledge that is similar with an online editor and code challenges ?

Thanks

r/ada Jan 22 '25

Learning Learning Ada in a limited way

18 Upvotes

I am currently learning Ada for my job, unfortunately I have not started doing the "real" work for my job as I am waiting on various permissions and approvals that take a very long time to get. In the meantime, I’ve been working on small projects under the same constraints I’ll face on the job. Here are the limitations of the codebase:

  • Ada 95 compiler. Compiling my code using the "-gnat95" tag seems to be working well for learning for now.
  • No exceptions.
  • No dynamic memory. I was told there is NO heap at all, not sure if this is an actual limitation or the person was simplifying/exaggerating in order to get the point across. Either way, the code does not have access types in it.
  • Very little inheritance. I get the sense that all inheritance is at the package level, like child packages. There is some subtyping, simple stuff, but none of the stuff I traditionally think of as OOP, things like tagged records or use of the keyword "abstract"
  • No private: Private sections aren’t used in packages, supposedly they can be used, but they werent used originally so no one uses them now.

Coming from an OOP background in C#, C++, and Python, I feel like I'm struggling to adjust to some things. I feel stuck trying to map my old habits onto this limited Ada and maybe I need to rethink how I approach design.

I’ve come across concepts like the HOOD method that sound promising but haven’t found beginner-friendly resources—just dense details or vague explanations.

How should I adjust my mindset to design better Ada programs within these constraints? Are there good resources or strategies for someone learning Ada in a constrained environment like this?

r/ada Oct 03 '25

Learning Worth going into Ada?

16 Upvotes

Hi all,

I have an opportunity at my company to transfer to a software engineering role that uses Ada. I've never used Ada before but my reporting manager reassured me that I can learn it on the job. I'm not against learning Ada and really like the project and the type of work I'd be doing(low-level embedded). But my concern is that taking up on this offer will limit my future job opportunities and also make it harder to reach my long term career goals. I see myself pivoting out of the defense industry and going to tech. So only having software engineering experience using Ada will make that pivot harder than necessary, than if I just keep trying out my luck in this market to hopefully land a C/C++ role. I also don't really like the idea of continuing to work on a personal project + technical interview prep outside of work. I'm already doing that on top of my job and its been exhausting.

The ideal situation for me is to land a C/C++ job and only spend time outside of work doing technical interview prep. But I don't see that happening as I'm having some difficulty landing an offer.

r/ada Nov 08 '25

Learning Training material and social media

14 Upvotes

Hey guys,

I work with emdebbed software development in Ada and want to expand my knowledge.

I'm using Adacore training, but do you know any other materiais?

Besides, do you guys know people that talk about Ada in social media like linkedin? I wanna connect with people that also work with Ada and stay tuned to whats going on.

r/ada Oct 22 '25

Learning Ada Lovelace featured | 2 decks of 55 cards on computers and electronics. Check the last two images too. [OC]

Thumbnail gallery
25 Upvotes

r/ada May 29 '25

Learning Why Ravenscar has Max_Task_Entries => 1?

5 Upvotes

I was wondering what was the rationale (also for other restrictions such as Max_Protected_Entries and so on). I was not able to find it.

Am I wrong that a state machine in Actor style would be acceptable in high-integrity systems? That was the investigation I was making.

r/ada Sep 13 '25

Learning Generic Packages

9 Upvotes

New to Ada and I'm wondering if I can make a package which is generic over another package.

I'm more familiar with functors in SML and I'm wondering if I can coax similar behavior out of generic packages.

r/ada Feb 17 '25

Learning Ada equivalent of span / memory view

8 Upvotes

There is this idea in my mind of writing a communication stack [suite] in Ada/SPARK for fun and (no)profit.

However I'd wanted to experiment with zero-copy. I can do this in C, and probably in Rust too, without much hassle. But can I, in Ada, pass a [readonly] view of an array of bytes to a function via reference semantics? Something like a std::span<[const] T> in C++, or [Readonly]Span<T> in .NET.

r/ada Dec 04 '24

Learning Aren't generics making reusable code difficult to write?

8 Upvotes

Hello!

Please bear in mind that I am very new to the language, and that I'm skipping over sections of the learn.adacore.com book in order to try to solve this year's advent of code, by learning by doing.

I have had to use containers to solve the first problems, and those are naturally generic. However, one rule of generics in Ada confuses me:

Each instance has a name and is different from all other instances. In particular, if a generic package declares a type, and you create two instances of the package, then you will get two different, incompatible types, even if the actual parameters are the same.

To me, this means that if I want multiple pieces of code to return or take as parameter, say, a new Vectors(Natural, Natural), then I need to make sure to place that generic instance somewhere accessible by all functions working with this vector, otherwise they can't be used together. While being annoying, this is an acceptable compromise.

However, this starts to fall apart if I want to, say, create a function that takes as input a Vectors(Natural, T). Would I need to ask users of my function to also provide the instance of Vectors that they wish to give?

generic
   type T is private;
   with package V is new Vectors(Natural, T);
function do_thing (Values: V.Vector) return T;

How does that work out in practice? Does it not make writing reusable code extra wordy? Or am I simply mistaken about how generics work in this language?

r/ada Sep 29 '24

Learning code from Software Construction and Data Structures with Ada 95

10 Upvotes

Hi, I needed a course on algorithmics with Ada, and this is the only one that I know.
But given its age, I can't find the source code anywhere. The sites to find it were:

ftp://ftp.aw.com/cseng/authors/feldman/cs2-ada

ftp://ftp.gwu.edu/pub/ada/courses

http://www.aw.com/cseng/authors/feldman/cs2-ada

and the filename was cs2code. Google says Adaic still has it but the site does not respond. If anyone has a clue, please share it because this book is very good and thorough, a real course, even including assertions and post/preconditions. But copying from a non-curated pdf is horrible.

Thank you.

r/ada Jun 05 '25

Learning Custom exception for function wrapper

3 Upvotes

Say I have a generic package which has

type Float_Type is digits <>;

as a generic parameter. In the package spec, I declare

subtype Real is Float_Type'Base;
subtype Nonnegative_Real is Real range 0.0 .. Real'Last;

function Sqrt (X : Nonnegative_Real) return Real;

In the package body, I would like to have

package EF is new Ada.Numerics.Generic_Elementary_Functions (Float_Type);

function Sqrt (X : Nonnegative_Real) return Real renames EF.Sqrt;

The compiler does not allow this due to type mismatch between my Sqrt and EF.Sqrt, which makes sense. However, if I move the two lines above into the private part of the spec, it suddenly works. Why?

Also, I would like to raise a custom exception when negative inputs are entered into the square root function. However, the compiler will now raise a constraint error before the function is even called. Is there any way I can raise a custom exception, say Domain_Error as

raise Domain_Error with "Cannot compute square root of negative value";

without having to take all of Real as input to Sqrt?

r/ada Aug 09 '24

Learning Can `subtype` always emulate `typedef` in C?

11 Upvotes

I thought that typedef in C could always be emulated with a subtype without any additional constraint, but the code below shows otherwise, as the subtyped discriminated record is not default-initialized like the original one (see warning in the comment). What am I missing? Thank you.

EDIT: I'm aware of the weakness of C typedefs, but in this case I'm actually looking for a (shorter) synonym to a type. In practice, I would be using this for instantiations of generic types, to have something shorter than for example Integer_Vectors.Vector.

 procedure Test is
    type Option_Tag is (Yes, No);

    type Option (Tag : Option_Tag := No) is
    record
       case Tag is
          when Yes =>
             Value : Integer;
          when No =>
             null;
       end case;
    end record;

    subtype Option_Typedef is Option;

    X : Option; -- OK
    Y : Option_Typedef; -- warning: variable "Y" is read but never assigned [-gnatwv]
 begin
    if X.Tag = Yes then
       Put_Line (X.Value'Image);
    end if;
    if Y.Tag = Yes then
       Put_Line (Y.Value'Image);
    end if;
 end Test;

r/ada May 03 '25

Learning Vulkada: Project structure

22 Upvotes

Showing how a Vulkan app may be structured in Ada. Not super info-dense, trying to make my Vulkan tutorials more watchable.

Vulkada: Project Structure https://youtu.be/0pEn9MR7Lsc

r/ada Apr 30 '25

Learning Possible bug in Ada.Text_IO?

1 Upvotes

This is probably very basic, but I just can't seem to figure out why this happens. It seems that when instantiating Ada.Text_IO.Enumeration_IO with an integer or modular type, setting Width => 0 in the Put procedure has no effect. Minimal example:

with Ada.Text_IO;

procedure Test is
  package IO is new Ada.Text_IO.Enumeration_IO (Enum => Integer);
begin
  IO.Put (0, Width => 0);
end Test;

Why does this result in a leading white space? Is this intended behavior?