So that’s my first time using Ada alongside with my main language Rust to make some project, I heard that Ada gives you more safety so I’m into it
Which is indeed true. You might want to read the Strongly typed language and Design by contracts chapters.
Since you are from Rust memory management will be important to you. Ada memory management is stack based. That is very different from the heap based memory management from C style languages have. You might want to read The Secondary Stack. Ada does have a heap but it’s used sparsely.
In that case then you might want to use Ada SPARK Bronze mode to start but perhaps you should get used to Ada first (debatable as Rod Chapman advised me to start with SPARK enabled but I didn’t and some say get to know Ada first
).
My tip for spark would be to use Gnat Studio and alr with gnatprove. Then in the SPARK menu (after alr edit to run gnat studio) start with Examine all and click do not report warnings but this may not be necessary on a small example project. Then move to higher modes as you desire with Bronze being a good default.
Here’s my Golden Rule for using Ada: Use the type-system to describe your problem-space, then use that to solve your problem.
IOW: (in general) don’t cater to the machine, cater to the problem.
100% If it feels like you’re fighting the language, the language is probably telling you “this is the wrong direction, try another one.” It’s been right for me in this respect almost every time.
Something that tripped me up is that if you want full SPARK verification, make sure your function bodies are also SPARK.
Otherwise spark can just ignore it and assume that you took care of it, which can lead to many suprises if your code actually breaks your contract when you run it for real.
Start by learning why data types can’t be mixed. For example:
String and Character, even though a string is an array of characters, you can’t mix them up.
```
S : String := "A";
L : Character := 'A';
if "A" = 'A' then
null;
end if;
because L is a Standard.Character and S is a Standard.String.
If you write programs that take user input, I don’t know this already, but I learned it the hard way: don’t use Skip_Line on what’s left in the buffer when using Get_Line.
For example [although it depends on how you take the input, but if you do it this way, be careful with this]:
procedure main ... etc.
S : String (1 .. 100) := (others => '0');
L : Natural range 0 .. S'length := 0;
Ada.text_IO.Get_Line(Item => S, Last => L);
If the user typed more than S'length, the extra text will remain floating in the buffer. Therefore, if you don’t clear the buffer with Skip_Line, it will stay there. Then, when you use Ada.text_IO.Get_Line(Item => S, Last => L); again, the first thing read will be what’s already in the buffer, which can lead to unexpected behavior.
I would recommend doing it like this:
S: String(1..101) := (others => '0'); -- 101 -> to have that extra value to know if there are any extra lines
L: Natural range 0.. S'length := 0;
Max: constant Positive := S'Length - 1;
and inside the begin block:
Ada.text_IO.Get_Line(Item => S, Last => L);
if L > Max then
Skip_Line;
end if;
This way, Ada clears the buffer. By “clear,” I mean that any floating data waiting for a line will no longer be there; it doesn’t clear RAM or anything like that.
So when you use Get_Line again, it’s already clean, with no more waiting data.
Not clearing the buffer further can affect you if you use this method in a loop with retries.
Learn about subtypes; it’s not difficult, but it helps a lot with future checks, especially if you’re using Spark.
Be careful with slices, not just string slices:
- 'First /= 1
- 'Last /= 'Length
Learn to separate .ads and .adb, and see how the visibility of what you use within them works.
This will help you understand what might raise an exception and where to separate certain code.
If you use Spark, it depends on your level. Remember that in regular Ada, you can say something and it won’t fail, but when working with Spark, you must know how to prove it by writing in a way that Spark can validate.
This includes loop invariants, dynamic and static predicates, etc.
Remember that working with verification in Spark is only effective if it doesn’t take any input from the user, or even if it doesn’t.
And if you have functions or precedence with unrestricted types, the opposite can be ideal: 100% verification with a proven. But if you compile without Assertion_policy(chec) or -gnata, those contracts disappear. So, even if it’s verified, without restricted types and without active flags, something unexpected could slip through and violate the contract, resulting in unexpected behavior.
well… Welcome to Ada! ![]()
Read the Style Guides and Rationales for Ada 1983, 1995, 2005 and 2012. Unfortunately, there appears to be no version of these documents for Ada 2022. Both documents are quite easy reading and very helpful.
A decent book would also be helpful, of course. I believe Cohen and Barnes are quite good, tho I have not myself read them (yet). Others can probably give you better recommendations, as per books.
Finally, there is the Awesome Ada website, which gives a curated list of Ada resources. Oh, and there is also Ada Wikibook.
You’re in for a treat ![]()
Regards.
- Download Richard Riehle’s Ada Distilled and read it as an introduction.
- Be aware of the Ada Reference Manual, especially the library annexes [a], which you are likely to use extensively.
- Ignore everything in the language and the library that’s prefixed with “Wide_” or “Wide_Wide_” (assume it doesn’t exist).
- Ada is a very good didactic language, and can be used to teach and learn all sorts of computer science subjecs such as data structures and algorithms, object-oriented programming, concurrency, real-time systems and even formal verification (with the SPARK language). For older books, which can usually be bought pre-owned cheaply, check the old Ada Home FAQ [b].
[a] The Standard Libraries
[b] Ada FAQ: Learning the Language
For Ada 2022 there indeed isn’t a Rationale, but there’s a smaller similar “Ada 2022 Overview” document by Jeff Coussins. See Ada 2022 Documents - Ada Resource Association.
Please do not refer to this hopelessly outdated and unmaintained website.
For learning material, including books, see the “Education” section of the Awesome Ada list, direct link: GitHub - ohenley/awesome-ada: A curated list of awesome resources related to the Ada and SPARK programming language · GitHub .
Please fix your formatting.
For an inline code use one backtick ('`'), for a block of code use three (each set of backticks, in this case, should be on its own line).
I will, thanks for letting me know! ![]()
![]()
Speak for yourself. I had good success with those. Especially in the realm of text transformation.
Texts should be processed not transformed. But even then “Wide_Wide_” stuff is not required. Algorithmically there is no advantage of having UCS-4 encoding. If you need random access to string code points, then you are probably doing something wrong.
But for a newcomer more important is to keep it understandable. String and character literals do not work as in other languages. Though Ada rules are perfectly consistent they conflict with de-facto standard UTF-8 and very notion Unicode, because they were drafted for handling multiple coding systems long before Unicode.
Understanding this in full and facing all insane consequences of related GNAT switches is too much to ask. While without “Wide_” / “Wide_Wide_” everything works just like in other languages.
This is an advice for a beginner and it’s a great and actionable advice.
This thread is not about you.
Do not derail from the OP with such pointless attacks.
Sometimes reading text is not enough. Sometimes you have the change text as well.
Yes, but not in a way where random access to low-level representation were relevant. Basic search, matching, parsing, substitution algorithms are invariant between UTF-8 and UCS-4. Of course they work for you, but they would also work for UTF-8 without additional complexity of conversion from UTF-8 to UCS-4 and back with quirks of -gnat8 switch on top. All this might be no problem for an experienced Ada programmer, but for a beginner it an unnecessary source of frustration.
I couldn’t agree more, Dmitry. You couldn’t have said it better, without saying too much:)
Ada.Strings.Wide_Wide_Maps.To_Set ("ÄÖÜäöüß");
AdaCL.Wide_Wide_Strings.Regexp.Compile("[ÄÖÜäöüß]");
AdaCL.Wide_Wide_Strings.Spitbol.Patterns.Any ("ÄÖÜäöüß");
The complexity from conversion is minute when compared to the complexity from not having characters maps any more. The difference is so large that I put in the effort of developing a wide string regular expression and Spitbol pattern matcher.