<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Riccardo Bernardini</title>
    <description>The latest articles on DEV Community by Riccardo Bernardini (@pinotattari).</description>
    <link>https://dev.to/pinotattari</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F46163%2F62b3f2b5-1ee3-4145-9cad-5acf097ed4e4.png</url>
      <title>DEV Community: Riccardo Bernardini</title>
      <link>https://dev.to/pinotattari</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pinotattari"/>
    <language>en</language>
    <item>
      <title>Announcement: Ada Developer Room at FOSDEM 2025</title>
      <dc:creator>Riccardo Bernardini</dc:creator>
      <pubDate>Fri, 20 Dec 2024 10:49:52 +0000</pubDate>
      <link>https://dev.to/pinotattari/announcement-ada-developer-room-at-fosdem-2025-59n6</link>
      <guid>https://dev.to/pinotattari/announcement-ada-developer-room-at-fosdem-2025-59n6</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhnax2qknq878golvyjc7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhnax2qknq878golvyjc7.png" alt="Ada Dev Room Manifesto" width="332" height="508"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If on Sunday 2nd of February 2025 you are at Brussels, it could be an interesting idea to spend the morning by visiting the &lt;a href="https://fosdem.org/2025/schedule/track/ada/" rel="noopener noreferrer"&gt;Ada developer room at FOSDEM&lt;/a&gt;.  You'll learn about this non-mainstream language that keeps itself young with feature that are not commonly found in other languages. &lt;/p&gt;

&lt;p&gt;Program overview:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Welcome to the Ada DevRoom&lt;/strong&gt;,
by Fernando Oleo Blanco, Spain, and Dirk Craeynest, Belgium&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Updates on the Ada Ecosystem&lt;/strong&gt;,
by Fernando Oleo Blanco, Spain&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Get started with Ada in 2 minutes or less!&lt;/strong&gt;,
by A.J., USA&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Advent of Compression: writing a working BZip2 encoder in Ada
from scratch in a few days&lt;/strong&gt;,
by Gautier de Montmollin, Switzerland&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ada and Mini-Ada: a solution to the two-language problem&lt;/strong&gt;,
by Gautier de Montmollin, Switzerland&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Understanding liquid types, contracts and formal verification
with Ada/SPARK&lt;/strong&gt;,
by Fernando Oleo Blanco, Spain&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The state of Rust trying to catch up with Ada&lt;/strong&gt;,
by Oli Scherer, Germany&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cryptography in SPARK: building the foundation with
constant-time bigints&lt;/strong&gt;,
by César Sagaert and Fabien Chouteau, France&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiword Arithmetic and Parallel Computing&lt;/strong&gt;,
by Jan Verschelde, USA&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Developing device drivers for Ironclad using Ada&lt;/strong&gt;,
by streaksu&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AdaBots - programmable minetest bots&lt;/strong&gt;,
by Tama McGlinn and Rudolf Batke, the Netherlands &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Credits: cover image from this &lt;a href="https://people.cs.kuleuven.be/~dirk.craeynest/ada-belgium/events/25/250202-fosdem.html" rel="noopener noreferrer"&gt;announcement&lt;/a&gt; &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Attaching notes to git branches</title>
      <dc:creator>Riccardo Bernardini</dc:creator>
      <pubDate>Thu, 08 Aug 2024 15:25:26 +0000</pubDate>
      <link>https://dev.to/pinotattari/attaching-notes-to-git-branches-503k</link>
      <guid>https://dev.to/pinotattari/attaching-notes-to-git-branches-503k</guid>
      <description>&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;Beside my coding activity for work, I have several personal projects active.  Clearly, I work on them in my free time and since they are personal projects, it can happen that for some reason I stop working on them for a long stretch of time.&lt;/p&gt;

&lt;p&gt;The problem is that when I decide to resume a personal project, more often than not I do not remember what I was doing when I stopped working.  Maybe there are a couple of branches open and I wonder if there is still work to do on them or if they are ready to be merged on the main branch, or... &lt;/p&gt;

&lt;p&gt;I thought that it would be nice if I could attach to every branch a note about what that branch is about, what I did so far and what still need to be done.  Since I work on several computers, I would like those notes to be managed as they were tracked files, allowing me to access the same information wherever I work.&lt;/p&gt;

&lt;h2&gt;
  
  
  The research
&lt;/h2&gt;

&lt;p&gt;I looked around for some solution, but I couldn't find anything that suited me.  First I found &lt;a href="https://stackoverflow.com/questions/2108405/branch-descriptions-in-git" rel="noopener noreferrer"&gt;&lt;code&gt;git branch --edit-description&lt;/code&gt;&lt;/a&gt;, but the description is written in the config file and it is not possible to push it to other repositories.  Also, I considered for a while &lt;a href="https://git-scm.com/docs/git-notes" rel="noopener noreferrer"&gt;&lt;code&gt;git notes&lt;/code&gt;&lt;/a&gt;, but it has two drawbacks for me.  First, you can attach a note to a &lt;em&gt;git object&lt;/em&gt; (blob, commit, tree), but branches are not git objects; sure, you could attach the note to the commit where the branch was created, but this would require to search for the branching point every time you want to read the note; moreover, while pushing notes is possible, it is not really straightforward.  Therefore &lt;code&gt;git notes&lt;/code&gt; could be a solution, but definitively not the ideal one.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;Therefore, I decided to bake my own solution in the form of &lt;a href="https://gitlab.com/mockturtle/git-branchnotes" rel="noopener noreferrer"&gt;&lt;code&gt;branchnotes&lt;/code&gt;&lt;/a&gt;, a Ruby script with a CLI similar to git (that can be run as a git external command) that allows you to add notes to a branch and manage them.  &lt;/p&gt;

&lt;p&gt;The idea is that a branch has a &lt;em&gt;motivation&lt;/em&gt; (why did you create the branch? Sure, a descriptive name helps, but sometimes you need more) and an &lt;em&gt;history&lt;/em&gt; of &lt;em&gt;events&lt;/em&gt; (that is, the milestones that you reached so far).  The most recent event is the &lt;em&gt;current status&lt;/em&gt; of the branch.&lt;br&gt;
The notes for branch &lt;code&gt;foo&lt;/code&gt; are stored in JSON format in &lt;code&gt;.branchnotes/foo&lt;/code&gt; under the repository root.  The note files are tracked and pushed using normal git commands.&lt;/p&gt;

&lt;p&gt;A brief overview of the implemented commands follows. See &lt;a href="https://gitlab.com/mockturtle/git-branchnotes/-/blob/master/README.md" rel="noopener noreferrer"&gt;the README.md file&lt;/a&gt; for more information.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;git branchnotes init&lt;/code&gt;  Create the note for the current branch, specifying the motivation for the branch&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git branchnotes update&lt;/code&gt;  Add a new milestone to the history&lt;/li&gt;
&lt;li&gt;'git branchnotes show [ reason | status | history ]` Print the reason/current state/history of the branch&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git branchnotes edit&lt;/code&gt;  Edit the full JSON description&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git branchnotes print&lt;/code&gt;  Print the full JSON description to  the standard output.  Potentially useful for filters.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git branchnotes read&lt;/code&gt;  Read the full JSON description from the standard input.  Potentially useful for filters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Current version (2024-08-08) is still 1.0.0 and it can be a bit rough around the edges, but it should be fairly usable.&lt;/p&gt;

</description>
      <category>git</category>
      <category>development</category>
      <category>documentation</category>
    </item>
    <item>
      <title>YaCaC (Yet another Comment about Comments)</title>
      <dc:creator>Riccardo Bernardini</dc:creator>
      <pubDate>Fri, 26 Jul 2024 15:40:48 +0000</pubDate>
      <link>https://dev.to/pinotattari/yacac-yet-another-comment-about-comments-42jm</link>
      <guid>https://dev.to/pinotattari/yacac-yet-another-comment-about-comments-42jm</guid>
      <description>&lt;p&gt;Recently I read few commentaries about comments in code, the most recent one &lt;a href="https://dev.to/grantdotdev/code-comments-are-not-evil-20o9?context=digest"&gt;just here on dev.to&lt;/a&gt;, so I guessed that I had to write mine too 😜&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A bit of background about me: my main programming language is Ada, therefore I will refer to the Ada program structure and my examples will be in Ada too, but it should not be too difficult to adapt what I write to a different context.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Comments split the developer community in two: those who believe that comments are evil and that the code should be self-documenting and those who agree that self-documenting code is nice, but you need comments nevertheless.&lt;br&gt;&lt;br&gt;
The &lt;em&gt;no-comment&lt;/em&gt; side usually says that comments can be misleading if they are not correct or out-of-date, to that the &lt;em&gt;pro-comment&lt;/em&gt; side replies that if you are a professional, you update your comments and to that the no-commenter replies that "update your comments" is a &lt;em&gt;good fellow policy&lt;/em&gt; that is, a policy that works only if everyone is a good fellow.&lt;/p&gt;

&lt;p&gt;Personally, I am more on the no-comment side, but keep reading because the issue is nuanced and even if you are a pro-commenter, you'll find that maybe our positions are not so far apart.&lt;br&gt;
My position is similar to the one taken by the &lt;a href="https://en.wikibooks.org/wiki/Ada_Style_Guide/Readability#Comments" rel="noopener noreferrer"&gt;Ada Style Guide&lt;/a&gt; (yes!  There is such a thing...).  Let me quote an excerpt&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Comments in source text are a controversial issue. There are arguments both for and against the view that comments enhance readability. In practice, the biggest problem with comments is that people often fail to update them when the associated source text is changed, thereby making the commentary misleading.&lt;br&gt;
...&lt;br&gt;
If possible, source text should use self-explanatory names for objects and program units, and it should use simple, understandable program structures so that little additional commentary is needed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Definitively a no-comment position, although not an extreme one (&lt;em&gt;If possible...&lt;/em&gt;). Few lines below there is a sentence that summarize my position &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Use comments to state the intent of the code. Comments that provide an overview of the code &lt;em&gt;help the maintenance programmer see the forest for the trees&lt;/em&gt;. (emphasis mine) &lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Four tiers of comments
&lt;/h2&gt;

&lt;p&gt;With time I developed this idea where I  think software documentation as organized in four &lt;em&gt;tiers&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tier 0&lt;/strong&gt; Overall description of the program, specs, architecture.  This tier is more about &lt;em&gt;documentation&lt;/em&gt; than &lt;em&gt;comments&lt;/em&gt; since I expect this kind of information to be in some PDF files rather than in the source code.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tier 1&lt;/strong&gt; Package level documentation.  This information is often found at the beginning of the package interface specs (&lt;code&gt;.ads&lt;/code&gt; for Ada, &lt;code&gt;.h&lt;/code&gt; or &lt;code&gt;.H&lt;/code&gt; for C and C++, and so on), maybe after a license blurb.  Here I describe the goal of the package, why the package is necessary and I give an &lt;em&gt;abstract&lt;/em&gt; description of the resources defined there.
To understand what I mean with &lt;em&gt;abstract description&lt;/em&gt;, consider the following example of a package that defines a symbol table for some kind of interpreter
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- This package defines a *symbol table* type used &lt;/span&gt;
&lt;span class="c1"&gt;-- by the interpreter to store information about the symbols&lt;/span&gt;
&lt;span class="c1"&gt;-- defined by the programmer.&lt;/span&gt;
&lt;span class="c1"&gt;--&lt;/span&gt;
&lt;span class="c1"&gt;-- A symbol table maps a *symbol name* to a *description* and &lt;/span&gt;
&lt;span class="c1"&gt;-- it must be able to&lt;/span&gt;
&lt;span class="c1"&gt;-- * Query the table for a symbol&lt;/span&gt;
&lt;span class="c1"&gt;-- * Define a new symbol&lt;/span&gt;
&lt;span class="c1"&gt;-- * Update the information associated to a symbol&lt;/span&gt;
&lt;span class="c1"&gt;-- * Push a new symbol table (when a new block is open)&lt;/span&gt;
&lt;span class="c1"&gt;-- * Pop the top symbol table &lt;/span&gt;
&lt;span class="c1"&gt;--&lt;/span&gt;
&lt;span class="kd"&gt;package&lt;/span&gt; &lt;span class="nf"&gt;Symbol_Tables&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; 
&lt;span class="c1"&gt;-- Add stuff here&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Symbol_Tables&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Note how the details of the API are not described, this is a description that applies more or less to anything you could want to call &lt;em&gt;symbol table&lt;/em&gt;.  Because of this, you can expect this description to be fairly "stable," that is, it does not change frequently.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tier 2&lt;/strong&gt;  API. At this level we describe the intent of each resource (types/functions/procedures) defined in the package. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With a good Tier 1 documentation and descriptive names for the resources, the intent of most functions/procedures should be fairly clear.  Nevertheless, a paragraph in human language does not hurt, especially to clarify details of the behavior that may be not evident from the interface (e.g., what happens if I query the symbol table with a symbol that is not defined).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Contracts&lt;/em&gt; (that is, pre- and post-conditions, in those languages that allow them) are a great way to write Tier 2 documentation since they are formal (they do not have the ambiguity of natural language) and cannot be out-of-sync with the code since otherwise an exception would is raised.  Integrate the contract formal description with a sentence in English and you have the perfect mix for Tier 2 documentation.&lt;/p&gt;

&lt;p&gt;For example, still with the case of the symbol table one could have&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="c1"&gt;--  Return True if Table contains a symbol with name Name&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Contains&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Symbol_Table&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                   &lt;span class="n"&gt;Name&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="c1"&gt;-- Get the description of the given symbol.  Raises Symbol_Not_Found&lt;/span&gt;
&lt;span class="c1"&gt;-- if Name is not in Table&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Description_Of&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Symbol_Table&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                         &lt;span class="n"&gt;Name&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Symbol_Info&lt;/span&gt;
&lt;span class="kn"&gt;with&lt;/span&gt;
  &lt;span class="nn"&gt;Pre&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- Define a new symbol.  Raise Duplicate_Name if Name is &lt;/span&gt;
&lt;span class="c1"&gt;-- already in Table&lt;/span&gt;
&lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;Insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;Symbol_Table&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                 &lt;span class="n"&gt;Name&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                 &lt;span class="n"&gt;Info&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Symbol_Info&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kn"&gt;with&lt;/span&gt; 
  &lt;span class="nn"&gt;Pre&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;Contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="n"&gt;Post&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
          &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="n"&gt;Info&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Description_Of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note how the pre- and post-condition already describe to a fair detail the behavior of the functions.  For example, from the pre-condition of &lt;code&gt;Insert&lt;/code&gt; I can see that I cannot call it if &lt;code&gt;Name&lt;/code&gt; is already in &lt;code&gt;Table&lt;/code&gt;; also, I can see that when &lt;code&gt;Insert&lt;/code&gt; returns &lt;code&gt;Name&lt;/code&gt; will be in &lt;code&gt;Table&lt;/code&gt; and its associated description will be &lt;code&gt;Info&lt;/code&gt;. This is no surprise, I agree, but it is nice to have it formally stated in a contract.  Also, if &lt;code&gt;Insert&lt;/code&gt; has a bug and does not insert properly &lt;code&gt;Name&lt;/code&gt; and &lt;code&gt;Info&lt;/code&gt;, at the exit an exception will be raised.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tier 3&lt;/strong&gt;  Implementation comments.  This is the tier where the &lt;em&gt;self-documenting&lt;/em&gt; code should replace the comments.  At this level you describe the implementation details (algorithm used, data structures, ...) and because of this, comments at this level can be quite &lt;em&gt;volatile&lt;/em&gt;, therefore increasing the probability of having a misleading comment. At this level I prefer to follow the self-documenting approach, adding comments only in those cases where the algorithm is fairly involved (e.g., the Euclidean algorithm for GCD).
Often I prefer to use assertion like &lt;code&gt;pragma Assert&lt;/code&gt;, &lt;code&gt;pragma Loop_Invariant&lt;/code&gt; or &lt;code&gt;pragma Loop_Variant&lt;/code&gt; to have the equivalent of a contract, but for Tier 3.
For example, in a loop for a binary search one could want to write a comment like
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="c1"&gt;--  Here Data(Top) &amp;gt;= X and X &amp;gt; Data(Bottom)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but I prefer to replace that with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Top&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As another example, in the loop of the Euclidean algorithm for the computation of GCD, one could want to write&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- The value of Smaller decrease at each iteration and since&lt;/span&gt;
&lt;span class="c1"&gt;-- Smaller is non-negative the loop will end sooner or later&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to explain why the loop will end; however,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Loop_Variant&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Decreases&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Smaller&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;describes the same fact, but in a more formal way that can be checked at run-time and used, eventually, to formally prove the correctness of the code.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Summarizing...
&lt;/h2&gt;

&lt;p&gt;At this point my position should be clear: Tiers 0, 1, and 2 are the maps (at increasing level of detail) that help us to navigate the forest.  The information gets more "stable" as the tier level decreases, reducing the risk of having out-of-date comments. Here comments are useful and definitively cannot be replaced by self-documenting code.&lt;/p&gt;

&lt;p&gt;Comments at tiers 3  are more tightly connected with the actual implementation and the risk of volatility is greater.  In this case I prefer to follow the self-documenting approach, using them sparingly and only in those cases where I think that a clarification is in order.&lt;/p&gt;

&lt;p&gt;It should also be clear now why my position is not so far away from a typical pro-commenter: the claims used by pro-commenters are fairly often related to lower tier information (e.g., *"Without comments you must read all the code to understand what a function does," this is a Tier 2 issue), while the risk of out-of-date comments, the position of no-commenters, is more related to Tier 3.&lt;/p&gt;

</description>
      <category>documentation</category>
      <category>readability</category>
      <category>development</category>
    </item>
    <item>
      <title>FOSDEM 2022</title>
      <dc:creator>Riccardo Bernardini</dc:creator>
      <pubDate>Fri, 04 Feb 2022 07:41:33 +0000</pubDate>
      <link>https://dev.to/pinotattari/fosdem-2022-46ip</link>
      <guid>https://dev.to/pinotattari/fosdem-2022-46ip</guid>
      <description>&lt;p&gt;&lt;a href="https://fosdem.org/2022/"&gt;FOSDEM 2022&lt;/a&gt; (Free and Open Source Developer European Meeting) is coming (5 &amp;amp; 6 February 2022) with its 8000+ participants and an &lt;a href="https://fosdem.org/2022/schedule/track/ada/"&gt;Ada DevRoom&lt;/a&gt;.  This year FOSDEM is online only.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://fosdem.org/2022/schedule/track/ada/"&gt;Ada DevRoom&lt;/a&gt; has Lots of interesting stuff, both for expert and for people who knows nothing about Ada. &lt;/p&gt;

&lt;p&gt;A talk that I find quite intriguing is &lt;a href="https://fosdem.org/2022/schedule/event/ada_adawebpack/"&gt;Getting Started with AdaWebPack&lt;/a&gt; that introduces an &lt;em&gt;Ada to WebAssembly&lt;/em&gt; compiler that will allow you to do web development in Ada, replacing the frail JS.&lt;/p&gt;

&lt;p&gt;Also, &lt;a href="https://fosdem.org/2022/schedule/event/ada_adagui/"&gt;Overview of Ada GUI&lt;/a&gt; presents a new approach to GUI programming.&lt;/p&gt;

</description>
      <category>fosdem</category>
      <category>webdev</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Reasons for loving Ada: Type invariants (because bugs shouldn't sleep...)</title>
      <dc:creator>Riccardo Bernardini</dc:creator>
      <pubDate>Sun, 19 Jul 2020 14:30:05 +0000</pubDate>
      <link>https://dev.to/pinotattari/reasons-for-loving-ada-type-invariants-because-bugs-shouldn-t-sleep-1082</link>
      <guid>https://dev.to/pinotattari/reasons-for-loving-ada-type-invariants-because-bugs-shouldn-t-sleep-1082</guid>
      <description>&lt;p&gt;This tutorial talks about a powerful &lt;em&gt;bug trap&lt;/em&gt; that was introduced in Ada 2012: the &lt;code&gt;Type_invariant&lt;/code&gt; attribute. &lt;/p&gt;

&lt;h1&gt;
  
  
  Type invariant? What's that?
&lt;/h1&gt;

&lt;p&gt;The idea of &lt;em&gt;type invariant&lt;/em&gt; is pretty simple. Sometimes data structure are quite simple, just an aggregation of values, but often they are quite complex with an internal structure that needs to be preserved.  &lt;/p&gt;

&lt;p&gt;A &lt;em&gt;type invariant&lt;/em&gt; associated to a given type is a Boolean expression that is true for every valid value of the type.  Ada allows the association of a type invariant to a private type; the compiler — if instructed so — will insert code that checks that the invariant remains satisfied; if not, an exception will be raised. &lt;/p&gt;

&lt;h1&gt;
  
  
  An example: a table with two keys
&lt;/h1&gt;

&lt;p&gt;Let's do a simple example. Suppose that &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We have a set of &lt;em&gt;users&lt;/em&gt; &lt;/li&gt;
&lt;li&gt;Every user has a &lt;code&gt;Serial_ID&lt;/code&gt; (a 6 digit number) and a &lt;code&gt;SSN&lt;/code&gt; (Social Security Number). We will assume the Italian convention where the SSN is a 16 character string.&lt;/li&gt;
&lt;li&gt;Both the &lt;code&gt;Serial_ID&lt;/code&gt; and &lt;code&gt;SSN&lt;/code&gt; identify uniquely the user, that is, there are no two users with the same &lt;code&gt;Serial_ID&lt;/code&gt; nor two users with the same &lt;code&gt;SSN&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We want to build a data structure &lt;code&gt;User_Directory&lt;/code&gt; that will allow us to get (&lt;em&gt;efficiently&lt;/em&gt;) an user both by &lt;code&gt;Serial_ID&lt;/code&gt; and &lt;code&gt;SSN&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A possible solution is shown in the figure&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mgNFJSdf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/em3jf1qtr6cx0ob0ddgl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mgNFJSdf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/em3jf1qtr6cx0ob0ddgl.png" alt="Two table pointing to the same record"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The data structure is made of two &lt;code&gt;Ordered_Maps&lt;/code&gt;: one map is indexed by the &lt;code&gt;Serial_ID&lt;/code&gt;, the other one by the &lt;code&gt;SSN&lt;/code&gt; and both maps store pointers (&lt;em&gt;access values&lt;/em&gt; in Ada jargon) to a &lt;code&gt;User_Record&lt;/code&gt;, a record that contains the user information.  This structure clearly introduces some (quite obvious) redundancy &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;SSN&lt;/code&gt; stored in a &lt;code&gt;User_Record&lt;/code&gt; must be equal to the corresponding key in the &lt;code&gt;SSN_Map&lt;/code&gt;, the same for the &lt;code&gt;Serial_ID&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The entries of the two maps corresponding to the same user must map to the same record&lt;/li&gt;
&lt;li&gt;The two maps have the same number of entries.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The spec file
&lt;/h2&gt;

&lt;p&gt;This is an excerpt of the &lt;em&gt;public&lt;/em&gt; part of the &lt;em&gt;spec&lt;/em&gt; file (roughly the &lt;code&gt;*.h&lt;/code&gt; of C)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;   &lt;span class="kd"&gt;subtype&lt;/span&gt; &lt;span class="nf"&gt;ID_Char&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nb"&gt;Character&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="sc"&gt;'0'&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="sc"&gt;'9'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Serial_ID&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;ID_Char&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;User_SSN&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;User_Directory&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;Add_User&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dir&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;User_Directory&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                       &lt;span class="n"&gt;ID&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;Serial_ID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                       &lt;span class="n"&gt;SSN&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;User_SSN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="k"&gt;with&lt;/span&gt;
       &lt;span class="n"&gt;Pre&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;Contains&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;Contains&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Ssn&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
     &lt;span class="n"&gt;Post&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;  &lt;span class="n"&gt;Contains&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;Contains&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Ssn&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


   &lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;Delete&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dir&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;User_Directory&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                     &lt;span class="n"&gt;ID&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;Serial_ID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="k"&gt;with&lt;/span&gt;
       &lt;span class="n"&gt;Pre&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Contains&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
     &lt;span class="n"&gt;Post&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;  &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;Contains&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here we define &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The types &lt;code&gt;Serial_ID&lt;/code&gt; and &lt;code&gt;User_SSN&lt;/code&gt; as fixed lenght strings.  Note that &lt;code&gt;Serial_ID&lt;/code&gt; can contain only digits.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;User_Directory&lt;/code&gt; is the data structure we are defining.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Add_User&lt;/code&gt; and &lt;code&gt;Delete&lt;/code&gt; should be quite obvious.  Note the use of preconditions (after &lt;code&gt;Pre =&amp;gt;&lt;/code&gt;) and postconditions (after &lt;code&gt;Post =&amp;gt;&lt;/code&gt;) that document the behavior of  &lt;code&gt;Add_User&lt;/code&gt; and &lt;code&gt;Delete&lt;/code&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The actual definition (in the &lt;code&gt;private&lt;/code&gt; part) of &lt;code&gt;User_Directory&lt;/code&gt; is basically the translation in Ada of the scheme shown above&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;User_Record&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;
      &lt;span class="k"&gt;record&lt;/span&gt;
         &lt;span class="n"&gt;ID&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Serial_ID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="n"&gt;SSN&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;User_SSN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;User_Record_Pt&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;access&lt;/span&gt; &lt;span class="n"&gt;User_Record&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;package&lt;/span&gt; &lt;span class="nf"&gt;ID_To_User_Maps&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;
     &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Ada&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Containers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Ordered_Maps&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Key_Type&lt;/span&gt;     &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Serial_ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                      &lt;span class="n"&gt;Element_Type&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;User_Record_Pt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="kd"&gt;package&lt;/span&gt; &lt;span class="nf"&gt;SSN_To_User_Maps&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;
     &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Ada&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Containers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Ordered_Maps&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Key_Type&lt;/span&gt;     &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;User_SSN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                      &lt;span class="n"&gt;Element_Type&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;User_Record_Pt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;User_Directory&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;
      &lt;span class="k"&gt;record&lt;/span&gt;
         &lt;span class="n"&gt;ID_To_User&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ID_To_User_Maps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="n"&gt;SSN_To_User&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;SSN_To_User_Maps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We have &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The record &lt;code&gt;User_Record&lt;/code&gt; holding the user information and its access  &lt;code&gt;User_Record_Pt&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;A package &lt;code&gt;ID_To_User_Maps&lt;/code&gt; (obtained by specializing the generic standard package &lt;code&gt;Ordered_Maps&lt;/code&gt;) that implements maps that associate &lt;code&gt;User_ID&lt;/code&gt; to access to &lt;code&gt;User_Record&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;A similar package &lt;code&gt;SSN_To_User_Maps&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;Finally, we have the &lt;code&gt;User_Directory&lt;/code&gt; that simply contains the two required maps. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The implementation
&lt;/h2&gt;

&lt;p&gt;Let's look at the implementation of the package.  Let's start with &lt;code&gt;Add_User&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt; &lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;Add_User&lt;/span&gt;
     &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dir&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;User_Directory&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
      &lt;span class="n"&gt;ID&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;Serial_ID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
      &lt;span class="n"&gt;SSN&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;User_SSN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;is&lt;/span&gt;
      &lt;span class="n"&gt;New_User&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;User_Record_Pt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;begin&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ID_To_User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Contains&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
         &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nb"&gt;Constraint_Error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="n"&gt;New_User&lt;/span&gt;  &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;User_Record&lt;/span&gt;&lt;span class="p"&gt;'(&lt;/span&gt;&lt;span class="n"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SSN&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ID_To_User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Include&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Key&lt;/span&gt;      &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                              &lt;span class="n"&gt;New_Item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;New_User&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


      &lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SSN_To_User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Include&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Key&lt;/span&gt;      &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;SSN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                               &lt;span class="n"&gt;New_Item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;New_User&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Add_User&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The implementation of &lt;code&gt;Add_User&lt;/code&gt; is quite straightforward: with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="n"&gt;New_User&lt;/span&gt;  &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;User_Record&lt;/span&gt;&lt;span class="p"&gt;'(&lt;/span&gt;&lt;span class="n"&gt;ID&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SSN&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;SSN&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;we allocate dynamically a record with the user information and with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;   &lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ID_To_User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Include&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Key&lt;/span&gt;      &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                           &lt;span class="n"&gt;New_Item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;New_User&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


   &lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SSN_To_User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Include&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Key&lt;/span&gt;      &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;SSN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                            &lt;span class="n"&gt;New_Item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;New_User&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;we update the two internal tables by associating the given &lt;code&gt;ID&lt;/code&gt; and &lt;code&gt;SSN&lt;/code&gt; with the address of the newly allocated record. &lt;/p&gt;

&lt;p&gt;This is our first tentative to the implementation of &lt;code&gt;Delete&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;Delete&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dir&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;User_Directory&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;ID&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;Serial_ID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;
      &lt;span class="n"&gt;To_Be_Removed&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;User_Record_Pt&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ID_To_User&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="k"&gt;begin&lt;/span&gt;
      &lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ID_To_User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Delete&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="n"&gt;Free&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;To_Be_Removed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Delete&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This implementation has a bug 🐛, as it is clear from this figure that pictures the behavior of &lt;code&gt;Delete&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MBFajiNk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sptmeiwy5hm6lu0dwv6w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MBFajiNk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sptmeiwy5hm6lu0dwv6w.png" alt="Entry deleted from only one table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The dashed line means that the memory area of the user record now has gone back to the heap.  It is clear that we forgot to remove the entry from &lt;code&gt;Dir.SSN_To_User&lt;/code&gt; and now we have a &lt;em&gt;dangling pointer&lt;/em&gt; referring to the old user record.&lt;/p&gt;

&lt;p&gt;This is a &lt;strong&gt;nasty&lt;/strong&gt; bug.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YLRakKhM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zcvhxxe0s92eya0272n2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YLRakKhM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zcvhxxe0s92eya0272n2.png" alt="Gollum with a scolopendra saying it is a nasty bug"&gt;&lt;/a&gt;&lt;br&gt;
Oh, yes, believe me.  I have already seen this movie, actually I was the main character.  What could go wrong?  Well, for example, later you could  want to update the entries of &lt;code&gt;Dir.SSN_To_User&lt;/code&gt; in some way.  However, the memory that was assigned to the user record now belongs to some other structure that will be corrupted by your update.&lt;/p&gt;

&lt;p&gt;Depending on your code, the bug can remain dormant for long time, then, suddenly,  one day, if you are &lt;em&gt;lucky&lt;/em&gt;, you get a &lt;a href="https://en.wikipedia.org/wiki/Segmentation_fault"&gt;SEGFAULT&lt;/a&gt; when you try to use the corrupted structure again. If you are unlucky you'll get funny random behaviors, possibly difficult to replicate.  &lt;/p&gt;

&lt;p&gt;Even if you are lucky (so to say...) you'll get the SEGFAULT in a place &lt;em&gt;totally unrelated&lt;/em&gt; with the real bug in &lt;code&gt;Delete&lt;/code&gt;. Believe me, finding this bug can take &lt;strong&gt;&lt;em&gt;days&lt;/em&gt;&lt;/strong&gt; of stumbling around in the dark.  Not even an army of rubber ducks can save you.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dnjvK_PK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jphkmppxi00vc0099mxg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dnjvK_PK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jphkmppxi00vc0099mxg.png" alt="Army of rubber ducks"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although this is a specific example, this kind of &lt;em&gt;time bomb&lt;/em&gt; 💣 (or &lt;em&gt;Sword of Damocles&lt;/em&gt;) behavior is typical of bugs that cause a loss of internal coherence in some complex structure. The bug will not manifest itself when the structure is corrupted, but later as a delayed consequence of the loss of coherence, making bug hunting difficult.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Remember:&lt;/em&gt; there is one thing worse than a bug that causes a crash: &lt;a href="https://blog.codinghorror.com/whats-worse-than-crashing/"&gt;a bug that causes the program to continue as nothing happened&lt;/a&gt;. &lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  The solution: type invariant
&lt;/h2&gt;

&lt;p&gt;Fear not, my friends and colleagues.  Ada comes to the rescue with the &lt;code&gt;Type_Invariant&lt;/code&gt;.  It suffices to add it to the type definition as in&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;User_Directory&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;
      &lt;span class="k"&gt;record&lt;/span&gt;
         &lt;span class="n"&gt;ID_To_User&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ID_To_User_Maps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="n"&gt;SSN_To_User&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;SSN_To_User_Maps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;Type_Invariant&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Is_Coherent&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;User_Directory&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;where &lt;code&gt;Is_Coherent&lt;/code&gt; is a function that checks that the whole structure is coherent, that is, that the two maps have the same number of entries and that the data in the user record match the corresponding key in the tables.  The source code of the (fairly long) function is included, for reference, at the end of this post.&lt;/p&gt;

&lt;p&gt;Now if we run the following test program&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kn"&gt;with&lt;/span&gt; &lt;span class="nn"&gt;User_Directories&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;User_Directories&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;
   &lt;span class="n"&gt;Dir&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;User_Directory&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;begin&lt;/span&gt;
   &lt;span class="n"&gt;Add_User&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dir&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="n"&gt;ID&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"123456"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="n"&gt;SSN&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"ABCDEF64X12Q456R"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="n"&gt;Add_User&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dir&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="n"&gt;ID&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"654321"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
             &lt;span class="n"&gt;SSN&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"XBCDEF64X12Q456R"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="n"&gt;Delete&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"123456"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;we get the following results&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Execution of obj/main terminated by unhandled exception
raised SYSTEM.ASSERTIONS.ASSERT_FAILURE : 
  failed invariant from user_directories.ads:65
Call stack traceback locations:
⋯/s-assert.adb:46
⋯/adainclude/a-coorma.adb:1561
⋯/user_directories.adb:52
⋯/user_directories.adb:59
⋯/main.adb:18
⋯
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;where, for the sake of readability I replaced part of the dump text with &lt;code&gt;⋯&lt;/code&gt;.  The line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;failed invariant from user_directories.ads:65
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;refers to the line where type &lt;code&gt;User_Directory&lt;/code&gt; is declared. In the stack dump the first two lines refer to some system files, we are interested in the third line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;⋯/user_directories.adb:52
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;that refers to the first line of procedure &lt;code&gt;Delete&lt;/code&gt;. &lt;br&gt;
Summarizing,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Before calling &lt;code&gt;Delete&lt;/code&gt; user directory &lt;code&gt;Dir&lt;/code&gt; was fine&lt;/li&gt;
&lt;li&gt;After calling &lt;code&gt;Delete&lt;/code&gt; it is not fine anymore,
who are you gonna blame? &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Ghostbusters!&lt;/em&gt;  No, sorry, wrong citation... 😊😛 (this is also a hint to my age... 😊)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_U-IX4o5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ejlmpd17zfnc9j6yzigw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_U-IX4o5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ejlmpd17zfnc9j6yzigw.png" alt="Judge finds Delete guilty of data structure damage"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After a brief inspection of &lt;code&gt;Delete&lt;/code&gt; we discover the problem and we fix it quite easily&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;Delete&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dir&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;User_Directory&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;ID&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;Serial_ID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;
      &lt;span class="n"&gt;To_Be_Removed&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;User_Record_Pt&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ID_To_User&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="k"&gt;begin&lt;/span&gt;
      &lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SSN_To_User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Delete&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;To_Be_Removed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SSN&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;-- New line&lt;/span&gt;
      &lt;span class="c1"&gt;-- and here???&lt;/span&gt;
      &lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ID_To_User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Delete&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="n"&gt;Free&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;To_Be_Removed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Delete&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Wait a minute...
&lt;/h3&gt;

&lt;p&gt;Someone maybe could observe that at the line &lt;code&gt;-- and here???&lt;/code&gt; the variable &lt;code&gt;Dir&lt;/code&gt; is in a non coherent state since we updated one table, but not the other.  The same happen in the &lt;code&gt;Add_User&lt;/code&gt; procedure.  Wouldn't this raise an exception?&lt;/p&gt;

&lt;p&gt;Well, no. The details are a bit complex, but the basic idea is that since &lt;code&gt;Add_User&lt;/code&gt; and &lt;code&gt;Delete&lt;/code&gt; are declared in the same package of &lt;code&gt;User_Directory&lt;/code&gt;, they are able to operate on the internal structure of the type and it is acceptable that during this manipulation the internal structure is not coherent for a moment.  The type invariant will be checked only when the procedures end; &lt;br&gt;
see the &lt;a href="https://www.adaic.org/resources/add_content/standards/12rm/html/RM-7-3-2.html#I3612"&gt;Reference manual&lt;/a&gt; if you want every single, gory, detail...&lt;/p&gt;

&lt;h1&gt;
  
  
  Appendix
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Implementation of &lt;code&gt;Is_Coherent&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Is_Coherent&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dir&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;User_Directory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Boolean&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;
      &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;Ada&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Containers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;ID_To_User_Maps&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;SSN_To_User_Maps&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;begin&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ID_To_User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SSN_To_User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;False&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;Pos&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ID_To_User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Iterate&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;
         &lt;span class="k"&gt;declare&lt;/span&gt;
            &lt;span class="n"&gt;Usr_Entry&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;constant&lt;/span&gt; &lt;span class="n"&gt;User_Record_Pt&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Element&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Pos&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
         &lt;span class="k"&gt;begin&lt;/span&gt; 
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;Usr_Entry&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="n"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SSN_To_User&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Usr_Entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Ssn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
               &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;False&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;Key&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Pos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="n"&gt;Usr_Entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
               &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;False&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         
      &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;True&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
   &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Is_Coherent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The image of Gollum is a modified version of the &lt;a href="https://commons.wikimedia.org/wiki/File:Gollum_at_Wellington_Airport.jpg"&gt;wikipedia image&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;The scolopendra image is available with license CC BY 4.0 &lt;a href="https://www.researchgate.net/publication/266085751_The_Evolutionary_History_of_the_Rediscovered_Austrian_Population_of_the_Giant_Centipede_Scolopendra_cingulata_Latreille_1829_Chilopoda_Scolopendromorpha/figures?lo=1"&gt;as figure of this paper&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>typeinvariant</category>
      <category>strongtyping</category>
      <category>tutorial</category>
      <category>ada</category>
    </item>
    <item>
      <title>Proving the correctness of a binary search procedure with SPARK/Ada </title>
      <dc:creator>Riccardo Bernardini</dc:creator>
      <pubDate>Thu, 09 Jul 2020 13:55:33 +0000</pubDate>
      <link>https://dev.to/pinotattari/proving-the-correctness-of-a-binary-search-procedure-with-spark-ada-34id</link>
      <guid>https://dev.to/pinotattari/proving-the-correctness-of-a-binary-search-procedure-with-spark-ada-34id</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/SPARK_(programming_language)" rel="noopener noreferrer"&gt;SPARK/Ada&lt;/a&gt; is a language derived from &lt;a href="https://en.wikipedia.org/wiki/Ada_(programming_language)" rel="noopener noreferrer"&gt;Ada&lt;/a&gt; that allows for a &lt;strong&gt;formal checking&lt;/strong&gt; (i.e., mathematically prove the correctness) of the software.  Several types of checks can be done: from the absence of runtime exceptions to no use of uninitialized variables, up to the formal proof that a given procedure/function fulfills its &lt;em&gt;contract&lt;/em&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I like formal checking because it can actually &lt;em&gt;prove&lt;/em&gt; that your program is correct (or that, at least, some kind of errors are absent), something that is usually not possible with testing. Of course, formal checking cannot applied to every program, but when it is possible it is a very powerful tool.  &lt;/p&gt;

&lt;p&gt;SPARK/Ada is definitively on my list of stuff that I want to learn.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Recently I had to write a procedure for a binary search in an ordered array.   I thought that it could be an interesting exercise to write it in SPARK/Ada in order to have it formally verified.  This is a brief summary of this experience, written with a tutorial spirit.&lt;/p&gt;

&lt;h1&gt;
  
  
  The requirements
&lt;/h1&gt;

&lt;p&gt;The problem I want to solve is the following&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;INPUTS&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt; &lt;code&gt;Table&lt;/code&gt;: an array of &lt;code&gt;Float&lt;/code&gt; sorted in &lt;em&gt;strictly increasing order&lt;/em&gt; (that is, &lt;code&gt;Table(n+1) &amp;gt; Table(n)&lt;/code&gt; for every &lt;code&gt;n&lt;/code&gt;). &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;What&lt;/code&gt; :  a &lt;code&gt;Float&lt;/code&gt; such that 

&lt;ul&gt;
&lt;li&gt;it is included between the array extremes, that is, &lt;code&gt;Table(Table'First)  ≤ What ≤ Table(Table'Last)&lt;/code&gt;, but &lt;/li&gt;
&lt;li&gt;it is  not necessarily in &lt;code&gt;Table&lt;/code&gt;, that is it is &lt;strong&gt;not required&lt;/strong&gt; that there is &lt;code&gt;n&lt;/code&gt; such that &lt;code&gt;Table(n) = What&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;OUTPUTS&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The unique index &lt;code&gt;n&lt;/code&gt; such that
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;≤&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  The solution
&lt;/h1&gt;

&lt;h2&gt;
  
  
  The &lt;em&gt;spec&lt;/em&gt; file
&lt;/h2&gt;

&lt;h3&gt;
  
  
  First basic draft of specs
&lt;/h3&gt;

&lt;p&gt;Let's first flesh out the spec file (roughly [very roughly] similar to a &lt;code&gt;*.h&lt;/code&gt; file for you C people).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;SPARK_Mode&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;On&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;package&lt;/span&gt; &lt;span class="nf"&gt;Searching&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;
   &lt;span class="kd"&gt;subtype&lt;/span&gt; &lt;span class="nf"&gt;Element_Type&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nb"&gt;Float&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="kd"&gt;subtype&lt;/span&gt; &lt;span class="nf"&gt;Index_Type&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nb"&gt;Natural&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Array_Type&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; 
      &lt;span class="k"&gt;array&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Index_Type&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;Element_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Find&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;What&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Element_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                  &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Array_Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Index_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Searching&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Entering SPARK Mode
&lt;/h3&gt;

&lt;p&gt;The first line we encounter is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;SPARK_Mode&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;On&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This claims that this package will be compatible with SPARK restrictions and conventions.  The lines&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;   &lt;span class="kd"&gt;subtype&lt;/span&gt; &lt;span class="nf"&gt;Element_Type&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nb"&gt;Float&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="kd"&gt;subtype&lt;/span&gt; &lt;span class="nf"&gt;Index_Type&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nb"&gt;Natural&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;define &lt;code&gt;Element_Type&lt;/code&gt; and &lt;code&gt;Index_Type&lt;/code&gt; as synominous of &lt;code&gt;Float&lt;/code&gt; and &lt;code&gt;Index_Type&lt;/code&gt;, respectively.  This is not strictly necessary, but it can make it easier to change the types in a future &lt;/p&gt;

&lt;p&gt;Function declaration&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Find&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;What&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Element_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Array_Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Index_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;should be clear enough.&lt;/p&gt;

&lt;h3&gt;
  
  
  Introducing contracts
&lt;/h3&gt;

&lt;p&gt;In order for SPARK to be able to &lt;em&gt;proof&lt;/em&gt; that our implementation of &lt;code&gt;Find&lt;/code&gt; is correct, we need to describe to SPARK what we expect &lt;code&gt;Find&lt;/code&gt; to do. This is done by means of a &lt;strong&gt;contract&lt;/strong&gt;. Like a normal contract between people, a &lt;em&gt;function contract&lt;/em&gt; usually has two parts: &lt;em&gt;preconditions&lt;/em&gt; and &lt;em&gt;postconditions&lt;/em&gt;.  &lt;/p&gt;

&lt;p&gt;The idea is that if  you (the caller) do your part (i.e. you meet the preconditions when you call me), then I, the function, promise to do my part, that is, that the result will satisfy the post-conditions.  &lt;/p&gt;

&lt;p&gt;If a contract is given, I can ask SPARK to prove that the post-conditions follow from the pre-conditions. If SPARK succeeds, I know that the code I wrote is &lt;strong&gt;correct&lt;/strong&gt; (in the sense that it respects the contract) &lt;em&gt;without doing &lt;strong&gt;any&lt;/strong&gt; test&lt;/em&gt;: I have a mathematical proof for it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;BTW, contracts are very useful even if you are not using SPARK.  First, they are a wonderful "bug trap." By using the right options, you can ask the compiler to insert code that checks the pre-conditions when the function is called and the post-conditions when the function ends.  If pre/post-conditions are not met, an exception is raised. &lt;/p&gt;

&lt;p&gt;Moreover, contracts document in a formal and unambiguous way the behavior of the function and, differently from usual comment-based documentation, cannot go out of sync with the code. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;OK, so contracts are cool. How do we write the contract for our function?  Well, let's start with the precondition and let's check the specs.  It is said that (i) &lt;code&gt;Table&lt;/code&gt; must be sorted and (ii) &lt;code&gt;What&lt;/code&gt; must be between &lt;code&gt;Table&lt;/code&gt; extrema; we just translate that in Ada in this way&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Find&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;What&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Element_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                  &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Array_Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Index_Type&lt;/span&gt;
     &lt;span class="k"&gt;with&lt;/span&gt;
       &lt;span class="n"&gt;Pre&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
          &lt;span class="n"&gt;Is_Sorted&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt;
          &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;First&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;code&gt;Is_Sorted&lt;/code&gt; is a function that checks if its argument is sorted and defined as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Is_Sorted&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Array_Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Boolean&lt;/span&gt;
   &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;all&lt;/span&gt; &lt;span class="n"&gt;L&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Range&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;all&lt;/span&gt; &lt;span class="n"&gt;M&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Range&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;L&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;M&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;L&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;
   &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;Ghost&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The body of the function (this form is called &lt;em&gt;expression function&lt;/em&gt;) is the translation in Ada of the definition of monotonic array. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note the &lt;code&gt;with Ghost&lt;/code&gt; in the definition.  This says that &lt;code&gt;Is_Sorted&lt;/code&gt; is a &lt;em&gt;ghost function&lt;/em&gt;, that is, a function that can be used only in contracts or assertions; if used in other places ("real code") an error is raised.  (I &lt;strong&gt;love&lt;/strong&gt; this kind of protections ♥)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The preconditions look right and they represent the actual specs, but  if we try to run SPARK we get an error&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;medium: array index check might fail&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;at line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;      &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;First&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means that &lt;code&gt;Table'First&lt;/code&gt;, that is the first index of the array, can be ... outside array bounds?  The first index is by definition within the bounds, right?  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3ngv8lp42n84e73hcgln.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3ngv8lp42n84e73hcgln.png" alt="Perplexed woman"&gt;&lt;/a&gt;&lt;br&gt;
Go home SPARK, you're drunk...&lt;/p&gt;

&lt;p&gt;Well...  Actually, SPARK is right.  If you ask for a counterexample you get&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;e.g. when Table'First=1 and Table'Last=0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, when in the world can be possible that the last index is smaller than the first one?!?  Well, if &lt;code&gt;Table&lt;/code&gt; is empty... Empty arrays in Ada have the last index smaller than the first one.  Therefore, trying to access &lt;code&gt;Table (Table'First)&lt;/code&gt; would cause an error.&lt;br&gt;
Well, SPARK, good catch... &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzbd8ff1181171xu3g7ju.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzbd8ff1181171xu3g7ju.png" alt="Old man asking why someone would want to search an empty table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, I agree (although it could happen because of an error), but SPARK does not know it.  To make SPARK happy it suffices to add &lt;code&gt;Table'Length &amp;gt; 0&lt;/code&gt; to the precondition to get&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Find&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;What&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Element_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                  &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Array_Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Index_Type&lt;/span&gt;
     &lt;span class="k"&gt;with&lt;/span&gt;
       &lt;span class="n"&gt;Pre&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
              &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Is_Sorted&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt;
                        &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;First&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now SPARK is happy and  even the casual user sees that you cannot call the function with an empty table.&lt;/p&gt;

&lt;h4&gt;
  
  
  The postcondition
&lt;/h4&gt;

&lt;p&gt;Also for the post-conditions we translate directly the requirement from English to Ada.  It is required that &lt;code&gt;What&lt;/code&gt; is between &lt;code&gt;Table(n)&lt;/code&gt; and &lt;code&gt;Table(n+1)&lt;/code&gt; where &lt;code&gt;n&lt;/code&gt; is the value returned by the function.  In Ada we get&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
             &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt;
          &lt;span class="k"&gt;else&lt;/span&gt;
             &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; 
         &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Result&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that &lt;code&gt;Find'Result&lt;/code&gt; is the value returned by &lt;code&gt;Find&lt;/code&gt; and that we consider as special case &lt;code&gt;Find'Result = Table'Last&lt;/code&gt; since in this case there is no "following entry" in &lt;code&gt;Table&lt;/code&gt;.  Overall, the function declaration with the full contract is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;   &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Find&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;What&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Element_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                  &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Array_Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Index_Type&lt;/span&gt;
     &lt;span class="k"&gt;with&lt;/span&gt;
       &lt;span class="n"&gt;Pre&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
          &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
          &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Is_Sorted&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt;
                    &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;First&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
       &lt;span class="n"&gt;Post&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
             &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt;
          &lt;span class="k"&gt;else&lt;/span&gt;
             &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; 
         &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Result&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The &lt;em&gt;body&lt;/em&gt; file (implementation)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The algorithm
&lt;/h3&gt;

&lt;p&gt;The algorithm for a binary search is well known and exemplified in the following picture&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F57dwap78cxlxl7l2qsfa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F57dwap78cxlxl7l2qsfa.png" alt="Example of binary search over a length 8 array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically, we keep two "cursors" in the table: &lt;code&gt;Bottom&lt;/code&gt; and &lt;code&gt;Top&lt;/code&gt; with the condition that it must always be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;≤&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;Top&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We get the &lt;code&gt;Middle&lt;/code&gt; point between &lt;code&gt;Top&lt;/code&gt; and &lt;code&gt;Bottom&lt;/code&gt; and if &lt;code&gt;Table(Middle)&lt;/code&gt; is too large we move &lt;code&gt;Top&lt;/code&gt; to &lt;code&gt;Middle&lt;/code&gt;, otherwise we move &lt;code&gt;Bottom&lt;/code&gt;.  We iterate this until on of the following two conditions holds&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Table(Bottom) = What&lt;/code&gt;, that is, we actually found &lt;code&gt;What&lt;/code&gt; in &lt;code&gt;Table&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Top = Bottom+1&lt;/code&gt; there are no intermediate entries between &lt;code&gt;Top&lt;/code&gt; and &lt;code&gt;Bottom&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In both cases  we return &lt;code&gt;Bottom&lt;/code&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  The actual code
&lt;/h3&gt;

&lt;p&gt;Let's start with a basic skeleton of the procedure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Find&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Element_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Array_Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Index_Type&lt;/span&gt;
   &lt;span class="k"&gt;is&lt;/span&gt;
      &lt;span class="n"&gt;Bottom&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Index_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;Top&lt;/span&gt;    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Index_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;Middle&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Index_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;begin&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="n"&gt;Bottom&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;First&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;Top&lt;/span&gt;    &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Assert&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Top&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

      &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;Top&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Bottom&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;
         &lt;span class="n"&gt;Middle&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;Top&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

         &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Middle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
            &lt;span class="n"&gt;Top&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Middle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="k"&gt;else&lt;/span&gt;
            &lt;span class="n"&gt;Bottom&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Middle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is just a translation of the algorithm informally described above.  Note how the special case &lt;code&gt;Table (Table'Last) = What&lt;/code&gt; is handled separately; in this way we know that&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Top&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;holds (see the &lt;code&gt;pragma Assert&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Now, in order to make it easier for SPARK to prove the correctness of the code we insert in the code some &lt;code&gt;pragma Assert&lt;/code&gt; claiming properties that hold true in different points of the code. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Automatic theorem proving is not easy and some hint to the prover can help.  Moreover, I love spreading generously my code with assertion since they help understanding what the code does and which properties I expect to be true.  They are also formidable &lt;em&gt;"bug traps"&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The function body with all the assertions looks like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Find&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Element_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Array_Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Index_Type&lt;/span&gt;
   &lt;span class="k"&gt;is&lt;/span&gt;
      &lt;span class="n"&gt;Bottom&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Index_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;Top&lt;/span&gt;    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Index_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;Middle&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Index_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;begin&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Assert&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="n"&gt;Bottom&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;First&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;Top&lt;/span&gt;    &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Assert&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;Top&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Assert&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Top&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

      &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;Top&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Bottom&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;

         &lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Loop_Invariant&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;First&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
         &lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Loop_Invariant&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Top&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
         &lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Loop_Invariant&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Top&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
         &lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Loop_Invariant&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Top&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
         &lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Loop_Variant&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Decreases&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Top&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

         &lt;span class="n"&gt;Middle&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;Top&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Assert&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;Middle&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;Middle&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;Top&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

         &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Middle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
            &lt;span class="n"&gt;Top&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Middle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="k"&gt;else&lt;/span&gt;
            &lt;span class="n"&gt;Bottom&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Middle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Assert&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Find&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are just two points worth describing; the first is this sequence of &lt;code&gt;pragma&lt;/code&gt;s inside the loop&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Loop_Invariant&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;First&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Loop_Invariant&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Top&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Loop_Invariant&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Top&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Loop_Invariant&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; 
                       &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Top&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Loop_Variant&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Decreases&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Top&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;pragma&lt;/code&gt;s &lt;code&gt;Loop_Invariant&lt;/code&gt; and &lt;code&gt;Loop_Variant&lt;/code&gt; are specific to SPARK.  A loop invariant is a condition that is true at every iteration and you can see that this loop invariant is a formalization of what we said before: at every iteration &lt;code&gt;What&lt;/code&gt; is between &lt;code&gt;Top&lt;/code&gt; and &lt;code&gt;Bottom&lt;/code&gt;.  Loop invariants are important in the proof of correctness of &lt;code&gt;while&lt;/code&gt; loops.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;while&lt;/code&gt; loop can potentially go on forever; a technique that allows us to prove that the loop will terminate is to search for some value that always increases (or decreases) at every iteration; if we know a bound for this value (e.g., it is always positive and it always decreases) we can deduce that the loop will terminate.  The &lt;code&gt;pragma&lt;/code&gt; &lt;code&gt;Loop_Variant&lt;/code&gt; allows us to declare said value.  In this case the distance between &lt;code&gt;Top&lt;/code&gt; and &lt;code&gt;Bottom&lt;/code&gt; is halved at every iteration, therefore it is a good variant.  Since &lt;code&gt;Top-Bottom&lt;/code&gt; cannot be smaller than 2 (see condition in the while), we deduce that the loop will terminate.&lt;/p&gt;

&lt;p&gt;A second observation is about seemly innocuous line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt; &lt;span class="n"&gt;Middle&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Top&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;Bottom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here SPARK complains with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;medium: overflow check might fail 
(e.g. when Bottom = Index_Type'Last-4 and Top = Index_Type'Last-2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good catch!  Here SPARK is observing that although theoretically &lt;code&gt;Middle&lt;/code&gt; should be in the range of representable integers if &lt;code&gt;Top&lt;/code&gt; and &lt;code&gt;Bottom&lt;/code&gt; are, there is a possibility of an overflow while doing the sum. Since on my PC &lt;code&gt;Index_Type'Last = 2^63-1&lt;/code&gt;, it is unlikely that one would work with tables so big, nevertheless...&lt;/p&gt;

&lt;p&gt;We have two solutions: (i) allow a smaller range of integers (that is, up to &lt;code&gt;Index_Type'Last/2&lt;/code&gt;) or (ii) compute the mean with function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Mean&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Lo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Hi&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Index_Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Index_Type&lt;/span&gt;
&lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Lo&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Hi&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Lo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;with&lt;/span&gt;
     &lt;span class="n"&gt;Pre&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Lo&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;Index_Type&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="n"&gt;Hi&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Lo&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="n"&gt;Post&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Hi&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Mean&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Result&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;Mean&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Result&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Lo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;that is guaranteed to have no overflow.  Note that in the precondition we require that &lt;code&gt;Hi &amp;gt; Lo + 1&lt;/code&gt;, this is guaranteed by the loop condition and it is necessary in order to guarantee that the post conditions hold which in turn guarantees that the loop variant decreases at every iteration.&lt;/p&gt;

&lt;h1&gt;
  
  
  Finally...
&lt;/h1&gt;

&lt;p&gt;OK, now if we run SPARK we get&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Summary of SPARK analysis
=========================

-------------------------------------------------------------------------------------------------------
SPARK Analysis results        Total      Flow   Interval   CodePeer      Provers   Justified   Unproved
-------------------------------------------------------------------------------------------------------
Data Dependencies                 .         .          .          .            .           .          .
Flow Dependencies                 .         .          .          .            .           .          .
Initialization                    3         3          .          .            .           .          .
Non-Aliasing                      .         .          .          .            .           .          .
Run-time Checks                  29         .          .          .    29 (CVC4)           .          .
Assertions                       14         .          .          .    14 (CVC4)           .          .
Functional Contracts              3         .          .          .     3 (CVC4)           .          .
LSP Verification                  .         .          .          .            .           .          .
-------------------------------------------------------------------------------------------------------
Total                            49    3 (6%)          .          .     46 (94%)           .          .

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do you see on the extreme right the column &lt;code&gt;Unproved&lt;/code&gt; with no entries?  That is what I want to see...  SPARK was able to prove everything, so now you can kick back, relax and enjoy your binary search function with confidence: you made &lt;em&gt;terra bruciata&lt;/em&gt; (scorched earth) around it and no bug can survive.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0ivzzc21hpw3zdpnb0if.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0ivzzc21hpw3zdpnb0if.png" alt="Castle with flag labelled "&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>spark</category>
      <category>ada</category>
      <category>formalchecking</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Watchdoging in Ada</title>
      <dc:creator>Riccardo Bernardini</dc:creator>
      <pubDate>Fri, 28 Feb 2020 16:46:10 +0000</pubDate>
      <link>https://dev.to/pinotattari/watchdoging-in-ada-4m1k</link>
      <guid>https://dev.to/pinotattari/watchdoging-in-ada-4m1k</guid>
      <description>&lt;p&gt;This project was inspired by &lt;a href="https://italiancoders.it/toto-peppino-e-il-watchdog-come-scrivere-un-watchdog-in-c-c-e-go/"&gt;an article about how to write a thread watchdog in &lt;code&gt;C&lt;/code&gt;&lt;/a&gt;. After reading it I thought "this would be a nice Ada project!" &lt;/p&gt;

&lt;p&gt;So, &lt;a href="https://gitlab.com/mockturtle/ada-watchdog"&gt;here it is&lt;/a&gt;.  This post is about my experience in writing it.  My main motivation was to do an "exercise" in programming, but maybe it can be useful somewhere.&lt;/p&gt;

&lt;h1&gt;
  
  
  Task watchdog and how I did it
&lt;/h1&gt;

&lt;p&gt;The problem is to monitor different tasks in a multi-task program and raise an alarm if a task stops working. A task proves that it is still alive by calling a specific function &lt;code&gt;I_Am_Alive&lt;/code&gt;.  If it fails to call it regularly, it is considered dead and an alarm is raised.&lt;/p&gt;

&lt;p&gt;Three ingredients are involved in this&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;em&gt;watcher&lt;/em&gt; itself, that is, the task that check regularly if the other tasks are still alive.&lt;/li&gt;
&lt;li&gt;A &lt;em&gt;connection&lt;/em&gt; to the watchdog used by the controlled task to tell the watchdog &lt;em&gt;Ehi, I am still alive!&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;An &lt;em&gt;alarm handler&lt;/em&gt; that does something when the watchdog raises an alarm.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's examine the three ingredients&lt;/p&gt;

&lt;h2&gt;
  
  
  The watcher
&lt;/h2&gt;

&lt;p&gt;Let's check the package interface.  The key ingredient is the &lt;em&gt;watcher&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;Watchdogs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Connections&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;

   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- Type representing the watcher, that is, the object that wakes up&lt;/span&gt;
   &lt;span class="c1"&gt;-- and checks if its tasks are still alive.&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Watcher_Type&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- Create a new watcher type specifying an alarm handler and a sampling&lt;/span&gt;
   &lt;span class="c1"&gt;-- time, that is, the time interval between successive wake ups.&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Create&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Alarm_Handler&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Alarm_Handlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Alarm_Handler_Access&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                    &lt;span class="n"&gt;Sampling&lt;/span&gt;      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Duration&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Watcher_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;-- Other stuff ...&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;  &lt;span class="n"&gt;Watchdogs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Connections&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Watcher_Type&lt;/code&gt; represents the object that does the "dirty work." The task to be controlled will make a connection to it and use it to communicate with the watcher.&lt;/p&gt;

&lt;p&gt;A watcher must be created with the function &lt;code&gt;Create&lt;/code&gt; that expects as first parameter an access (a &lt;em&gt;pointer&lt;/em&gt; in C jargon) to an &lt;em&gt;alarm handler&lt;/em&gt;.  &lt;/p&gt;

&lt;h2&gt;
  
  
  The alarm handler
&lt;/h2&gt;

&lt;p&gt;The definition of alarm handler is the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;Watchdogs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Alarm_Handlers&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- Interface for an alarm handler.  Every handler you want to implement&lt;/span&gt;
   &lt;span class="c1"&gt;-- must descend from this and implement Task_Exited and Task_Unresponsive.&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Alarm_Handler_Interface&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;limited&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Alarm_Handler_Access&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;
     &lt;span class="k"&gt;access&lt;/span&gt; &lt;span class="k"&gt;all&lt;/span&gt; &lt;span class="n"&gt;Alarm_Handler_Interface&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Class&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;For the non experienced in Ada: &lt;code&gt;interface&lt;/code&gt; means that &lt;code&gt;Alarm_Handler_Interface&lt;/code&gt; is an abstract type and you cannot create variables of this type, it works like a &lt;em&gt;interface template&lt;/em&gt;. You need to derive new concrete classes from it.  &lt;code&gt;limited&lt;/code&gt; means that you can derive limited types, that is, types that cannot be assigned (see in the following). Finally, &lt;code&gt;Alarm_Handler_Interface'Class&lt;/code&gt; is a &lt;em&gt;catch-all&lt;/em&gt; type that includes &lt;code&gt;Alarm_Handler_Interface&lt;/code&gt; and every type derived from it. In other words, &lt;code&gt;Alarm_Handler_Access&lt;/code&gt; can point to values of any type derived by &lt;code&gt;Alarm_Handler_Interface&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The interface &lt;code&gt;Alarm_Handler_Interface&lt;/code&gt; requires that any non-abstract descendant implements two procedure: &lt;code&gt;Task_Unresponsive&lt;/code&gt; (called when a task does not respond anymore, maybe because is stuck somewhere) and &lt;code&gt;Task_Exited&lt;/code&gt;, called when a task exists&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- Called when a task is unresponsive.  It receives the task identification&lt;/span&gt;
   &lt;span class="c1"&gt;-- (name and/or ID) and the latest registered checkpoint&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;Task_Unresponsive&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Handler&lt;/span&gt;    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;Alarm_Handler_Interface&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                                &lt;span class="n"&gt;ID&lt;/span&gt;         &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Task_Identification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Task_Id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                                &lt;span class="n"&gt;Name&lt;/span&gt;       &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                                &lt;span class="n"&gt;Checkpoint&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Checkpoint_Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt;
     &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;Pre&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Class&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
       &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;ID&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="n"&gt;Task_Identification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Null_Task_Id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- Called when a task exits.  It receives the task identification&lt;/span&gt;
   &lt;span class="c1"&gt;-- (name and/or ID)&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;Task_Exited&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Handler&lt;/span&gt;    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;Alarm_Handler_Interface&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                          &lt;span class="n"&gt;ID&lt;/span&gt;         &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Task_Identification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Task_Id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                          &lt;span class="n"&gt;Name&lt;/span&gt;       &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt;
     &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;Pre&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Class&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
       &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;ID&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="n"&gt;Task_Identification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Null_Task_Id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Watchdogs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Alarm_Handlers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Both procedures expects as parameter an identification of the task, namely its &lt;code&gt;Task_ID&lt;/code&gt; and/or a name. The ID or the name (but not both) can be empty.  The procedure &lt;code&gt;Task_Unresponsive&lt;/code&gt;  also expect a &lt;code&gt;Checkpoint&lt;/code&gt; value that can be used to know where the task got stuck, more about this later. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This approach allows the user to implement its own alarm handler that can do anything.  For convenience, package &lt;code&gt;Watchdogs.Alarm_Handlers.To_Stderr&lt;/code&gt; defines an alarm handler &lt;em&gt;prêt à porter&lt;/em&gt; that just prints a message to the standard error.  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is an example (from &lt;code&gt;main.adb&lt;/code&gt;) of how to create a &lt;em&gt;watcher&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- Get a watcher&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="n"&gt;Watcher&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;constant&lt;/span&gt; &lt;span class="n"&gt;Connections&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Watcher_Type&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt;
               &lt;span class="n"&gt;Connections&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Alarm_Handler&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;To_Stderr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Handler_Type&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  The connection
&lt;/h2&gt;

&lt;p&gt;The type for a connection to the watcher is &lt;code&gt;Watchdog_Connection&lt;/code&gt;. Its definition is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- A watchdog connection allows a task to communicate with the&lt;/span&gt;
   &lt;span class="c1"&gt;-- watcher&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Watchdog_Connection&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;limited&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If you have no experience with Ada you can find the syntax above a bit obscure. The &lt;code&gt;(&amp;lt;&amp;gt;)&lt;/code&gt; means that &lt;code&gt;Watchdog_Connection&lt;/code&gt; can have some unknown discriminant.  Without entering in technical details, this prevents the user to declare a variable of this type without initialization.  The &lt;code&gt;limited&lt;/code&gt; part means that you cannot copy a value of type &lt;code&gt;Watchdog_Connection&lt;/code&gt;, a value is born, lives and dies in the same variable. This is useful for values that carry an "external connection" and it makes no sense to copy.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A connection is created with the function &lt;code&gt;Open&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- Open a connection with the watcher.  The task needs to introduce itself&lt;/span&gt;
   &lt;span class="c1"&gt;-- with a name or a Task_ID, possibly both.  Those values will be passed&lt;/span&gt;
   &lt;span class="c1"&gt;-- to the Alarm_Handler if the task becomes unresponsive.&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Open&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Watchdog&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Watcher_Type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                  &lt;span class="n"&gt;Task_Name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                  &lt;span class="n"&gt;ID&lt;/span&gt;        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Task_Identification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Task_Id&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Task_Identification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Null_Task_Id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Watchdog_Connection&lt;/span&gt;
     &lt;span class="k"&gt;with&lt;/span&gt;
       &lt;span class="n"&gt;Pre&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Task_Name&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;ID&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="n"&gt;Task_Identification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Null_Task_Id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The function expects the watcher to connect to and a way to identify the task, it can be a name, the task ID or both, but at least one value must be present, as specified by the pre-condition.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Again, if you have no experience with Ada you could wonder what the part &lt;code&gt;with Pre =&amp;gt; ...&lt;/code&gt; is.  It is a &lt;em&gt;pre-condition&lt;/em&gt;, a condition that must be satisfied when you call the function.  It can be considered part of the documentation, but it has the advantage that the compiler (if instructed to do so) can add code that checks the pre-condition at run-time and raises an exception if not satisfied.  A powerful bug trap...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When the task ends the connection is automatically closed (and &lt;code&gt;Task_Exited&lt;/code&gt; called) by the destroyer of the connection. &lt;/p&gt;

&lt;p&gt;After opening a connection the task must declare its being alive; it does it by calling &lt;code&gt;I_Am_Alive&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Checkpoint_Type&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;mod&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- Let the watcher know that we are still alive.  If this function is&lt;/span&gt;
   &lt;span class="c1"&gt;-- called in different points of the task it is possible to distinguish&lt;/span&gt;
   &lt;span class="c1"&gt;-- different calls via the Checkpoint parameter.  The reason for having&lt;/span&gt;
   &lt;span class="c1"&gt;-- it is just to know what is the latest instance of I_Am_Alive&lt;/span&gt;
   &lt;span class="c1"&gt;-- called before the task crash.  Its value will be given to the&lt;/span&gt;
   &lt;span class="c1"&gt;-- alarm handler.&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;I_Am_Alive&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Connection&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;Watchdog_Connection&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                         &lt;span class="n"&gt;Checkpoint&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Checkpoint_Type&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The procedure &lt;code&gt;I_Am_Alive&lt;/code&gt; can accept a &lt;code&gt;Checkpoint&lt;/code&gt; parameter (a 16 bit unsigned integer, actually) that the task can use to distinguish between different calls of &lt;code&gt;I_Am_Alive&lt;/code&gt;.  If the task gets stuck the latest &lt;code&gt;Checkpoint&lt;/code&gt; is given to the alarm handler together with the task identification (ID or name), allowing to identify where the task got stuck.&lt;/p&gt;

&lt;h3&gt;
  
  
  An example
&lt;/h3&gt;

&lt;p&gt;This is a very simple example of how a connection is used.  This is a simplified version of what you find in &lt;a href="https://gitlab.com/mockturtle/ada-watchdog/-/blob/master/src/main.adb"&gt;main.adb&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;  &lt;span class="k"&gt;task&lt;/span&gt; &lt;span class="k"&gt;body&lt;/span&gt; &lt;span class="n"&gt;Foo&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;

      &lt;span class="n"&gt;Connection&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Connections&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Watchdog_Connection&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt;
                     &lt;span class="n"&gt;Connections&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Watchdog&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Watcher&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                       &lt;span class="n"&gt;Task_Name&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"my name is foo, task foo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                       &lt;span class="n"&gt;ID&lt;/span&gt;        &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Task_Identification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current_Task&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="n"&gt;Sleep_Time&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Duration&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;begin&lt;/span&gt;
      &lt;span class="c1"&gt;--&lt;/span&gt;
      &lt;span class="c1"&gt;-- Now we are connected with the watcher that will check that we&lt;/span&gt;
      &lt;span class="c1"&gt;-- call I_Am_Alive regularly&lt;/span&gt;
      &lt;span class="c1"&gt;--&lt;/span&gt;
      &lt;span class="k"&gt;loop&lt;/span&gt;
         &lt;span class="c1"&gt;--&lt;/span&gt;
         &lt;span class="c1"&gt;-- At every iteration we increase the Sleep_Time so that sooner&lt;/span&gt;
         &lt;span class="c1"&gt;-- or later it will exceed the wake up time of the watcher&lt;/span&gt;
         &lt;span class="c1"&gt;--&lt;/span&gt;
         &lt;span class="k"&gt;delay&lt;/span&gt; &lt;span class="n"&gt;Sleep_Time&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="n"&gt;Sleep_Time&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Sleep_Time&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

         &lt;span class="c1"&gt;--&lt;/span&gt;
         &lt;span class="c1"&gt;-- Tell the watcher we are alive&lt;/span&gt;
         &lt;span class="c1"&gt;--&lt;/span&gt;
         &lt;span class="n"&gt;Connections&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;I_Am_Alive&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Connection&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Digging in the internals
&lt;/h1&gt;

&lt;p&gt;The user API is nice and cool, but you want a bit of gory details about the implementation, right?  OK, so let's checkout the private definition of the watcher from package &lt;code&gt;Watchdogs.Connections&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt;
    &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Watcher_Type&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;access&lt;/span&gt; &lt;span class="n"&gt;Watchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Watchdog_Core&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Uh?!? That's it?  Just an access to a "core type"?  That's cheating...&lt;/p&gt;

&lt;p&gt;Well, let's check the definition of  &lt;code&gt;Watchdog_Core&lt;/code&gt; in &lt;code&gt;Watchdogs.Connections&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;Watchdogs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Watchers&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- Object doing all the work.  This exports an interface similar&lt;/span&gt;
   &lt;span class="c1"&gt;-- to the user visible Watcher_Type.  This object is multitask safe&lt;/span&gt;
   &lt;span class="c1"&gt;-- (with Ada it is just too easy...)&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Watchdog_Core&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;limited&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- Other stuff... &lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;

   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Watchdog_Core&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;limited&lt;/span&gt;
      &lt;span class="k"&gt;record&lt;/span&gt;
         &lt;span class="n"&gt;Doa_Table&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Task_Table_Access&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="n"&gt;Watcher&lt;/span&gt;   &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Watchdog_Task_Access&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="n"&gt;Handler&lt;/span&gt;   &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Alarm_Handlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Alarm_Handler_Access&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Watchdogs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Watchers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Several comments are in order.  &lt;/p&gt;

&lt;p&gt;&lt;em&gt;First&lt;/em&gt;, do you see the keyword &lt;code&gt;private&lt;/code&gt; before &lt;code&gt;package&lt;/code&gt;?  This means that &lt;code&gt;Watchdogs.Watchers&lt;/code&gt; is a &lt;em&gt;private package&lt;/em&gt; and it cannot be made visible outside the hierarchy of &lt;code&gt;Watchdogs&lt;/code&gt;.  In particular, the library user (i.e., the programmer that uses the library) will not be able to access directly the resources provided by &lt;code&gt;Watchdogs.Watchers&lt;/code&gt;, but only through &lt;code&gt;Watchdogs.Connections&lt;/code&gt; that &lt;code&gt;with&lt;/code&gt;s  &lt;code&gt;Watchdogs.Watchers&lt;/code&gt; with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;   &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;Watchdogs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Watchers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The keyword &lt;code&gt;private&lt;/code&gt; before &lt;code&gt;with&lt;/code&gt; says "Listen, I need the resources in &lt;code&gt;Watchdogs.Watchers&lt;/code&gt;, but I promise, cross my heart, that never ever I'll let the user see it". Indeed, if you check &lt;a href="https://gitlab.com/mockturtle/ada-watchdog/-/blob/master/src/watchdogs-connections.ads"&gt;watchdogs-connections.ads&lt;/a&gt; you'll see that &lt;code&gt;Watchdogs.Watchers&lt;/code&gt; is referred only in the private part of the package, out of reach of the prying hands of the user...&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Second&lt;/em&gt;, the definition of &lt;code&gt;Watcher_Type&lt;/code&gt; looks simple, just three fields.  The last one is the access to the alarm handler (this is easy), what are the other two fields? Here the hard stuff lies... ;-)&lt;br&gt;
Let's begin with the easy stuff: the field &lt;code&gt;Watcher&lt;/code&gt; is a &lt;code&gt;Watchdog_Task_Access&lt;/code&gt; that we guess being an access to &lt;code&gt;Watchdog_Task&lt;/code&gt;, but what is the latter? Well, a task&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;   &lt;span class="k"&gt;task&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Watchdog_Task&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;
      &lt;span class="c1"&gt;--&lt;/span&gt;
      &lt;span class="c1"&gt;-- This task is the real watchdog: it wakes up every now and then,&lt;/span&gt;
      &lt;span class="c1"&gt;-- check the task table for dead tasks and, if necessary, call&lt;/span&gt;
      &lt;span class="c1"&gt;-- the alarm handler&lt;/span&gt;
      &lt;span class="c1"&gt;--&lt;/span&gt;
      &lt;span class="kd"&gt;entry&lt;/span&gt; &lt;span class="nf"&gt;Init&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Sampling&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                  &lt;span class="n"&gt;Table&lt;/span&gt;    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Task_Table_Access&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                  &lt;span class="n"&gt;Handler&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Alarm_Handlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Alarm_Handler_Access&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Watchdog_Task&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Watchdog_Task_Access&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;access&lt;/span&gt; &lt;span class="n"&gt;Watchdog_Task&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since we declared it as &lt;code&gt;task type&lt;/code&gt; &lt;code&gt;Watchdog_Task&lt;/code&gt; behaves as type and we can, for example, declare variables of this type.  Declaring a variable of type &lt;code&gt;Watchdog_Task&lt;/code&gt; would start a new task that proceeds in parallel.  In Ada synchronization is done traditionally by message passing via the call to task &lt;code&gt;entry&lt;/code&gt;.  In this case the entry is just used to give the task few parameters.  The task will wake up every Sampling seconds, check the unresponsive tasks and call, if necessary, the handler.&lt;/p&gt;

&lt;p&gt;OK, cool, and what about &lt;code&gt;Table&lt;/code&gt; in the parameter list &lt;em&gt;and&lt;/em&gt; in the definition of &lt;code&gt;Watcher_Type&lt;/code&gt;?  Well, here is where most of the complexity is hidden.  A &lt;code&gt;Task_Table_Access&lt;/code&gt; is an &lt;code&gt;access&lt;/code&gt; to a &lt;code&gt;Task_Table&lt;/code&gt; that in turn has the following definition&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- The protected object Task_Table is the core data structure.&lt;/span&gt;
   &lt;span class="c1"&gt;-- It keeps which tasks are still alive and which ones did not&lt;/span&gt;
   &lt;span class="c1"&gt;-- confirm that they are alive.&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Task_Table&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;
      &lt;span class="c1"&gt;-- Register that the task associated with the connection&lt;/span&gt;
      &lt;span class="c1"&gt;-- just went by th checkpoint&lt;/span&gt;
      &lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;Mark_Alive&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Connection&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Connection_ID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                            &lt;span class="n"&gt;Checkpoint&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Checkpoint_Type&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="c1"&gt;-- Get the set of tasks that did not declared themselves alive&lt;/span&gt;
      &lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;Get_Dead_Tasks&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;Connection_To_Checkpoint_Tables&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="c1"&gt;-- Reset the state, setting all task as "to be confirmed alive"&lt;/span&gt;
      &lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;Reset&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="c1"&gt;-- Delete a task&lt;/span&gt;
      &lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;Delete&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Connection&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Connection_ID&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="c1"&gt;-- Allocate a new connection ID to a task&lt;/span&gt;
      &lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;Get_New_Id&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Connection&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;Connection_ID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                            &lt;span class="n"&gt;Name&lt;/span&gt;       &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                            &lt;span class="n"&gt;ID&lt;/span&gt;         &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Task_Identification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Task_Id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ID_Of&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Connection&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Connection_ID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Task_Identification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Task_Id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Name_Of&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Connection&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Connection_ID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;private&lt;/span&gt;
      &lt;span class="c1"&gt;--&lt;/span&gt;
      &lt;span class="c1"&gt;-- It works in this way: we keep two sets of "tasks:" Alive (the&lt;/span&gt;
      &lt;span class="c1"&gt;-- tasks that declared to be alive) and Dead (the task that still&lt;/span&gt;
      &lt;span class="c1"&gt;-- have to declare to be alive).  At timeout we read the Dead list&lt;/span&gt;
      &lt;span class="c1"&gt;-- and raise a warning for the tasks in list; successively we copy&lt;/span&gt;
      &lt;span class="c1"&gt;-- (with a Reset) Alive to Dead, restarting the iteration&lt;/span&gt;
      &lt;span class="c1"&gt;--&lt;/span&gt;
      &lt;span class="n"&gt;Alive&lt;/span&gt;   &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Connection_To_Checkpoint_Tables&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;Dead&lt;/span&gt;    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Connection_To_Checkpoint_Tables&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;Next_Id&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Connection_ID&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Connection_ID&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;First&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="c1"&gt;-- Keep name and ID of the tasks associated with a connection&lt;/span&gt;
      &lt;span class="n"&gt;Connection_Table&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Connection_To_Task_Tables&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Task_Table&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;Task_Table&lt;/code&gt; is the object that stores the state of the monitored tasks: if dead or alive and their identifications.  It is a &lt;code&gt;protected type&lt;/code&gt; which means that it is accessed according to a &lt;em&gt;reader/writer&lt;/em&gt; model (many tasks can read it at the same time, but writers have exclusive access).  The compiler will take care of inserting the required synchronization code.&lt;/p&gt;

&lt;p&gt;This object is manipulated mainly by the task watcher whose body is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;   &lt;span class="k"&gt;task&lt;/span&gt; &lt;span class="k"&gt;body&lt;/span&gt; &lt;span class="n"&gt;Watchdog_Task&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt;
      &lt;span class="n"&gt;Task_Table&lt;/span&gt;      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Task_Table_Access&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;Alarm_Handler&lt;/span&gt;   &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Alarm_Handlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Alarm_Handler_Access&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;Sampling_Period&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="n"&gt;Dead_Tasks&lt;/span&gt;      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Connection_To_Checkpoint_Tables&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;Connection_To_Checkpoint_Tables&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;begin&lt;/span&gt;
      &lt;span class="c1"&gt;-- Accept calls to the Init entry&lt;/span&gt;
      &lt;span class="k"&gt;accept&lt;/span&gt; &lt;span class="n"&gt;Init&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Sampling&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                   &lt;span class="n"&gt;Table&lt;/span&gt;    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Task_Table_Access&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                   &lt;span class="n"&gt;Handler&lt;/span&gt;  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Alarm_Handlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Alarm_Handler_Access&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;do&lt;/span&gt;
         &lt;span class="n"&gt;Sampling_Period&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Sampling&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="n"&gt;Task_Table&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="n"&gt;Alarm_Handler&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Handler&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Init&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="k"&gt;loop&lt;/span&gt;           
         &lt;span class="k"&gt;delay&lt;/span&gt; &lt;span class="n"&gt;Sampling_Period&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- get some sleep&lt;/span&gt;

         &lt;span class="c1"&gt;--&lt;/span&gt;
         &lt;span class="c1"&gt;-- Extract from the task table the task that did not&lt;/span&gt;
         &lt;span class="c1"&gt;-- claimed to be alive&lt;/span&gt;
         &lt;span class="c1"&gt;--&lt;/span&gt;
         &lt;span class="n"&gt;Task_Table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Get_Dead_Tasks&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dead_Tasks&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

         &lt;span class="c1"&gt;--&lt;/span&gt;
         &lt;span class="c1"&gt;-- Iterate over the list of dead tasks&lt;/span&gt;
         &lt;span class="c1"&gt;--&lt;/span&gt;
         &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;Pos&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;Dead_Tasks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Iterate&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;
            &lt;span class="k"&gt;declare&lt;/span&gt;
               &lt;span class="n"&gt;Connection&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;constant&lt;/span&gt; &lt;span class="n"&gt;Connection_ID&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Key&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Pos&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
               &lt;span class="n"&gt;Checkpoint&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;constant&lt;/span&gt; &lt;span class="n"&gt;Checkpoint_Type&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Element&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Pos&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;begin&lt;/span&gt;
               &lt;span class="c1"&gt;--&lt;/span&gt;
               &lt;span class="c1"&gt;-- Call the alarm handler with the task data&lt;/span&gt;
               &lt;span class="c1"&gt;--&lt;/span&gt;
               &lt;span class="n"&gt;Alarm_Handler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Task_Unresponsive&lt;/span&gt;
                 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ID&lt;/span&gt;         &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Task_Table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id_Of&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Connection&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                  &lt;span class="n"&gt;Name&lt;/span&gt;       &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Task_Table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name_Of&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Connection&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                  &lt;span class="n"&gt;Checkpoint&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Checkpoint&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

               &lt;span class="c1"&gt;--&lt;/span&gt;
               &lt;span class="c1"&gt;--  Remove the task from the table&lt;/span&gt;
               &lt;span class="c1"&gt;--&lt;/span&gt;
               &lt;span class="n"&gt;Task_Table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Delete&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Connection&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;end&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

         &lt;span class="n"&gt;Dead_Tasks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Clear&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

         &lt;span class="c1"&gt;--&lt;/span&gt;
         &lt;span class="c1"&gt;-- All the tasks that were declared alive get marked as&lt;/span&gt;
         &lt;span class="c1"&gt;-- dead.  Let them prove that they are alive! :-)&lt;/span&gt;
         &lt;span class="c1"&gt;--&lt;/span&gt;
         &lt;span class="n"&gt;Task_Table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Reset&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Watchdog_Task&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;As I said, I wrote this for the fun of it and, indeed, fun it was (Yoda-style).  I hope you found this interesting. &lt;/p&gt;

</description>
      <category>ada</category>
      <category>watchdog</category>
      <category>multitask</category>
      <category>threads</category>
    </item>
    <item>
      <title>Safer set-uid programs in Ada with the suid-helper library </title>
      <dc:creator>Riccardo Bernardini</dc:creator>
      <pubDate>Sat, 22 Feb 2020 19:03:35 +0000</pubDate>
      <link>https://dev.to/pinotattari/safer-set-uid-programs-in-ada-with-the-suid-helper-library-340j</link>
      <guid>https://dev.to/pinotattari/safer-set-uid-programs-in-ada-with-the-suid-helper-library-340j</guid>
      <description>&lt;h1&gt;
  
  
  Set-uid programs: the good, the bad and the risky
&lt;/h1&gt;

&lt;p&gt;In *nix systems access control is traditionally often done by checking if the user requesting a given operation has read/write/execution privilege with respect to a given file/directory. &lt;/p&gt;

&lt;p&gt;In some cases this policy is not fine-grained enough.  An example is the common &lt;code&gt;passwd&lt;/code&gt; that allows the user to change its password. Changing the password requires modifying the file &lt;code&gt;/etc/passwd&lt;/code&gt; that stores the passwords (to be honest, this was true on older systems, nowadays things are a bit different, but we stick to this for the sake of the example).  This causes a problem with the permission accesses of &lt;code&gt;/etc/passwd&lt;/code&gt;: the file must be clearly not writable by the ordinary user, but... how could the user change its password?&lt;/p&gt;

&lt;p&gt;The solution is allow the user to modify &lt;code&gt;/etc/passwd&lt;/code&gt; in a controlled way, via an executable that will change only the user's password.  In other words, the user must gain temporally limited root privileges (&lt;em&gt;administrator privileges&lt;/em&gt; for you Window people 😄). This is done with the idea of &lt;strong&gt;set-user-ID executable&lt;/strong&gt;. If an executable is marked as &lt;em&gt;setuid&lt;/em&gt;, the user running the program gets the privileges of the &lt;em&gt;owner&lt;/em&gt; of the executable (usually root).  In this way the program can carry out privileged actions on behalf of the user.&lt;/p&gt;

&lt;h2&gt;
  
  
  The three identities
&lt;/h2&gt;

&lt;p&gt;We need a bit of more detail.  In current *nix systems a running executable has 3 identities&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;real&lt;/strong&gt; user ID.  This is the user that is &lt;em&gt;running&lt;/em&gt; the code.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;effective&lt;/strong&gt; user ID.  This is the identity that is used to check the accesses.  In a normal program this is equal to the real user ID, but in a &lt;em&gt;setuid&lt;/em&gt; program it is initialized to the identity of the &lt;em&gt;owner&lt;/em&gt; of the executable.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;saved&lt;/strong&gt; user ID.  This is initialized with the effective user ID; therefore, in a normal program it is equal to the real UID, while in a &lt;em&gt;setuid&lt;/em&gt; program is equal to the ID of the &lt;em&gt;owner&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How do these IDs interact?  It is really simple: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The program can change its &lt;em&gt;effective&lt;/em&gt; ID to its &lt;em&gt;real&lt;/em&gt; or &lt;em&gt;saved&lt;/em&gt; ID.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means that a normal program starts with the effective ID equal to the identity of the user and it cannot be changed; while a &lt;em&gt;setuid&lt;/em&gt; program can set it to the &lt;em&gt;real&lt;/em&gt; identity (we will say that it &lt;em&gt;drops&lt;/em&gt; the privileges) or to the identity of the owner (we will say that it &lt;em&gt;restores&lt;/em&gt; the privileges).  It is possible for the program to &lt;em&gt;definitively drop&lt;/em&gt; the privileges by setting the saved ID to the real ID.&lt;/p&gt;

&lt;h2&gt;
  
  
  Good practices in &lt;code&gt;setuid&lt;/code&gt; codes
&lt;/h2&gt;

&lt;p&gt;Of course a &lt;em&gt;setuid&lt;/em&gt; program is a potential security risk because of this &lt;em&gt;privilege escalation&lt;/em&gt; provided by the &lt;em&gt;setuid&lt;/em&gt; mechanism. Sure, if the program was correctly written, without weakness nor bugs, it would be safe to have &lt;em&gt;setuid&lt;/em&gt; code, but... You know... We are all human beings and a defensive approach to limit the potential damage it is not a bad idea.  We will consider two simple good practices&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Drop the privileges&lt;/strong&gt; as soon as possible, restoring them only when necessary.&lt;/li&gt;
&lt;li&gt;Consider the &lt;strong&gt;environment variables&lt;/strong&gt; as &lt;em&gt;tainted&lt;/em&gt; since their value is under the control of the user.  For example, the user could set the &lt;code&gt;PATH&lt;/code&gt; variable to force the code to execute programs under the control of the user. It would be better not using environment variables at all, but if you really need &lt;em&gt;replace&lt;/em&gt; the variable values with trusted values (e.g. write in &lt;code&gt;PATH&lt;/code&gt; a list of trusted directory) or, at least, &lt;em&gt;sanitize&lt;/em&gt; the value given by the user.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  What is &lt;code&gt;setuid-helper&lt;/code&gt; and how can it help me?
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://gitlab.com/mockturtle/suid-helper"&gt;&lt;code&gt;setuid-helper&lt;/code&gt;&lt;/a&gt; is a small Ada library that provides a package &lt;code&gt;Setuid.Helper&lt;/code&gt; designed to help following the above guidelines for &lt;em&gt;setuid&lt;/em&gt; programs.&lt;/p&gt;

&lt;p&gt;During the package initialization (&lt;em&gt;elaboration&lt;/em&gt; in Ada jargon) the package does three things&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It saves the environment variables received from the user&lt;/li&gt;
&lt;li&gt;It deletes every environment variable &lt;/li&gt;
&lt;li&gt;It drops the privileges&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This means that when the first instruction of the main is executed, the program runs with the privileges dropped and the environment empty.  The only way to restore the privileges is by using the procedure &lt;code&gt;With_Privileges_Do&lt;/code&gt;.  This procedure exists in different &lt;em&gt;flavors&lt;/em&gt; that differ in how the action to be done is specified.&lt;/p&gt;

&lt;p&gt;In the simplest case&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;procedure With_Privileges_Do (Callback         : access procedure;
                              Drop_Permanently : Boolean := True);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;the procedure &lt;code&gt;With_Privileges_Do&lt;/code&gt; expects a &lt;code&gt;Callback&lt;/code&gt; parameter represented by an access (a &lt;em&gt;pointer&lt;/em&gt; for you &lt;code&gt;C&lt;/code&gt; people 😄) to a parameter-less procedure. The semantic is really simple: the privileges are restored, &lt;code&gt;Callback&lt;/code&gt; called and the privileged are dropped again.  Unless the second parameter is &lt;code&gt;False&lt;/code&gt;, the privileges are permanently dropped and it will not be possible to restore them again. Note that permanent dropping is the default.&lt;/p&gt;

&lt;p&gt;This is a typical usage example where the callback is defined &lt;em&gt;locally&lt;/em&gt; inside a &lt;code&gt;declare&lt;/code&gt; block&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   declare
      procedure Unmount_Callback is
      begin
         Unmount (Mountpoint_Name);
      end Unmount_Callback;
   begin
      With_Privileges_Do (Callback         =&amp;gt; Unmount_Callback'Access,
                          Drop_Permanently =&amp;gt; True);
   end;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;There are two other versions of &lt;code&gt;With_Privileges_Do&lt;/code&gt;: one expects a &lt;em&gt;handler&lt;/em&gt; object (that can store a status), the other expects a function and it can return a value. See &lt;a href="https://gitlab.com/mockturtle/suid-helper/-/blob/master/src/suid-helper.ads"&gt;the &lt;code&gt;.ads&lt;/code&gt; file&lt;/a&gt; and the &lt;a href="https://gitlab.com/mockturtle/suid-helper/-/blob/master/README.md"&gt;README file&lt;/a&gt; for more details.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Managing the environment
&lt;/h2&gt;

&lt;p&gt;As said before, it is maybe better not using environment variables at all.  This is not always possible and &lt;code&gt;setuid.helper&lt;/code&gt; provides a way to manage it as safely as possible, keeping two different environments: one normal and one privileged.&lt;/p&gt;

&lt;p&gt;More precisely, the package mantains three "enviroments"&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;em&gt;tainted&lt;/em&gt; environment.  This is a copy of the original environment. It cannot be changed, but it can be read with the functions &lt;code&gt;Tainted_Variable&lt;/code&gt;. This allows to import in a controlled way the values provided by the user in the environment.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;user&lt;/em&gt; environment.  This is used when the privileges are dropped. It is initially empty and it can be written with &lt;code&gt;Set_User_Environment&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;safe&lt;/em&gt; environment used when privileges are on. It is initially empty and it can be written with &lt;code&gt;Set_Safe_Environment&lt;/code&gt;.
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Procedure &lt;code&gt;Set_Safe_Environment&lt;/code&gt;  expects the value to be of type &lt;code&gt;Safe_Value&lt;/code&gt;, not &lt;code&gt;String&lt;/code&gt;.  A string can be converted to a &lt;code&gt;Safe_Value&lt;/code&gt; by calling the function &lt;code&gt;Bless&lt;/code&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Just to be clear: it is expected that the value given to &lt;code&gt;Bless&lt;/code&gt; is checked or sanitized, but nothing prevents the programmer to call &lt;code&gt;Bless&lt;/code&gt; with any unsafe value. The usage of a different type is to avoid involuntary short-circuits where a value of the original environment (to be read with &lt;code&gt;Tainted_Variable&lt;/code&gt;, &lt;em&gt;nomen omen&lt;/em&gt;...) is given to  &lt;code&gt;Set_Safe_Environment&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Portability, installation, ...
&lt;/h1&gt;

&lt;p&gt;Just check the beginning of the &lt;a href="https://gitlab.com/mockturtle/suid-helper/-/blob/master/README.md"&gt;README file&lt;/a&gt;. I work with Linux and in Linux it works; I guess it should work with any modern *nix (if it works for you under another *nix please let me know).&lt;/p&gt;

&lt;p&gt;What about Windows?  Honestly, I do not know much Windows, but I am afraid that the idea of the &lt;em&gt;setuid bit&lt;/em&gt; is very *nix-ish.  Is maybe possible to bring the same idea to Windows?  Well, let me know, never say never...&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;setuid-helper&lt;/code&gt; is a pretty young library (currently is 0.1.0 since I am not sure about the stability of its API).  Any feedback is welcome.&lt;/p&gt;

</description>
      <category>setuidprogram</category>
      <category>safety</category>
      <category>ada</category>
    </item>
    <item>
      <title>Ada for Webassembly</title>
      <dc:creator>Riccardo Bernardini</dc:creator>
      <pubDate>Tue, 04 Feb 2020 20:40:20 +0000</pubDate>
      <link>https://dev.to/pinotattari/ada-for-webassembly-1nnm</link>
      <guid>https://dev.to/pinotattari/ada-for-webassembly-1nnm</guid>
      <description>&lt;p&gt;A very, very fast post that I guess can be of interest to many (or at least raise some curiosity): &lt;a href="https://github.com/godunko/adawebpack"&gt;The GNAT-LLVM project&lt;/a&gt; is porting the Ada standard library and runtime  to WebAssembly, allowing to do web development with the robustness of Ada.&lt;/p&gt;

&lt;p&gt;Personally, I am quite excited by this piece of news, although I guess that it will not be easy to overcome the predominance of Javascript and its environment in this field.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>My first experience with SPARK-Ada</title>
      <dc:creator>Riccardo Bernardini</dc:creator>
      <pubDate>Sat, 25 May 2019 20:11:03 +0000</pubDate>
      <link>https://dev.to/pinotattari/my-first-experience-with-spark-ada-2onp</link>
      <guid>https://dev.to/pinotattari/my-first-experience-with-spark-ada-2onp</guid>
      <description>&lt;p&gt;I program in Ada and I like it. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Yes, I know, it sounds like a "coming out"... 😉&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the world of Ada there is a thing that I always wanted to try: it is  a reduced subset of Ada called &lt;em&gt;SPARK&lt;/em&gt; (nothing to do with Apache) that it allows for &lt;em&gt;formal checking&lt;/em&gt;, that is, &lt;em&gt;proving&lt;/em&gt; some properties of your code such as: absence of buffer overflow and other runtime errors, no uninitialized variable read, and so on. I like the idea to have my code (or, at least, part of it) &lt;em&gt;armored&lt;/em&gt; by a mathematical proof 😊.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Curious about SPARK? Check out this &lt;a href="https://learn.adacore.com/courses/intro-to-spark/index.html"&gt;Introduction to SPARK&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Few days ago I decided to give it a try using an old function of mine: a Ruby-like &lt;code&gt;split&lt;/code&gt; to split strings at a separator.  It is small piece of code that I wrote some time ago and used safely in many programs of mine: the right "&lt;em&gt;guinea pig&lt;/em&gt;" for the experiment. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you are curious, the final SPARK code is on &lt;a href="https://github.com/fintatarta/ada-tokenizer"&gt;github&lt;/a&gt;, be my guest! 😊&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The first thing to do was to replace the use of &lt;code&gt;Vectors&lt;/code&gt; (a kind of dynamic array, similar to its &lt;code&gt;C++&lt;/code&gt; counterpart) with normal arrays.  You see, as you can imagine, &lt;code&gt;Vectors&lt;/code&gt; use pointers (&lt;em&gt;access types&lt;/em&gt; in Ada jargon) and they are &lt;em&gt;prohibited&lt;/em&gt; in SPARK.  The fact is that pointers would allow for &lt;em&gt;aliasing&lt;/em&gt; of objects and this would make the formal check impossible.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is not as bad as it sounds: in Ada the necessity of using pointers is much reduced with respect to &lt;code&gt;C&lt;/code&gt; or &lt;code&gt;C++&lt;/code&gt;.  The only real need is when you want to dynamically allocate structures.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Therefore, I wrote my own pseudo-&lt;code&gt;Vector&lt;/code&gt; type with the minimal set of needed functionality. The idea was to keep the extracted pieces in a fixed array (allocated to the stack, so no explicit pointers) large enough to keep the pieces (if you &lt;code&gt;split&lt;/code&gt; a string with length &lt;code&gt;N&lt;/code&gt; you get at most &lt;code&gt;N+1&lt;/code&gt; pieces).  Maybe not very memory efficient, but fine for my needs.&lt;/p&gt;

&lt;p&gt;The resulting code (slightly pruned with respect to the original, for "didactic" purposes...) follows. It seems long, but it is just that I added few comments to explain the more Ada-esque syntax.  &lt;/p&gt;

&lt;p&gt;First the &lt;em&gt;spec&lt;/em&gt; part (very roughly the equivalent of a &lt;code&gt;*.h&lt;/code&gt;  file)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kn"&gt;with&lt;/span&gt; &lt;span class="nn"&gt;Ada&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Strings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Unbounded&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;Ada&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Strings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unbounded&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;package&lt;/span&gt; &lt;span class="nf"&gt;Token_Lists&lt;/span&gt; 
   &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;SPARK_Mode&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;On&lt;/span&gt; &lt;span class="c1"&gt;-- This specs is in SPARK&lt;/span&gt;
&lt;span class="k"&gt;is&lt;/span&gt;

   &lt;span class="kd"&gt;subtype&lt;/span&gt; &lt;span class="nf"&gt;List_Length&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nb"&gt;Integer&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="nb"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="c1"&gt;-- "Tagged types" correspond roughly to what other &lt;/span&gt;
   &lt;span class="c1"&gt;-- languages call "class".  Token_List is the desired&lt;/span&gt;
   &lt;span class="c1"&gt;-- pseudo-vector for holding the pieces.  &lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- The funny (&amp;lt;&amp;gt;) means that the type is parameterized, but the&lt;/span&gt;
   &lt;span class="c1"&gt;-- parameter is not public.  This forces you to initialize &lt;/span&gt;
   &lt;span class="c1"&gt;-- any variable of type Token_List with some kind of constructor.&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- Finally, "private" means that the internal details are not&lt;/span&gt;
   &lt;span class="c1"&gt;-- visible and are described later, in the private part. &lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Token_List&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;tagged&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Create&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;List_Length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Token_List&lt;/span&gt;
     &lt;span class="k"&gt;with&lt;/span&gt;
       &lt;span class="n"&gt;Post&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
         &lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Capacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;N&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;--&lt;/span&gt;
    &lt;span class="c1"&gt;-- Post is an "attribute" and it specifies a Boolean expression&lt;/span&gt;
    &lt;span class="c1"&gt;-- that will be true when the function returns (contract). &lt;/span&gt;
    &lt;span class="c1"&gt;--&lt;/span&gt;
    &lt;span class="c1"&gt;-- The post-condition says that the created list will have &lt;/span&gt;
    &lt;span class="c1"&gt;-- room for N entries and it will be empty&lt;/span&gt;
    &lt;span class="c1"&gt;--&lt;/span&gt;

   &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Capacity&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Item&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Token_List&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Positive&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Item&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Token_List&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Natural&lt;/span&gt;
     &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;Post&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Result&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Capacity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- Number of the pieces currently in the list.  Of course &lt;/span&gt;
   &lt;span class="c1"&gt;-- it cannot be larger than the capacity, if it happens &lt;/span&gt;
   &lt;span class="c1"&gt;-- there is something wrong somewhere...&lt;/span&gt;
   &lt;span class="c1"&gt;---&lt;/span&gt;

   &lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;Append&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;List&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;Token_List&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                     &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="k"&gt;with&lt;/span&gt;
       &lt;span class="n"&gt;Pre&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Class&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
         &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Capacity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

       &lt;span class="n"&gt;Post&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
         &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Old&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
     &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Capacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Capacity&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Old&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;-- &lt;/span&gt;
    &lt;span class="c1"&gt;-- The precondition (attribute "Pre") says that before calling&lt;/span&gt;
    &lt;span class="c1"&gt;-- the procedure there must be some room in the list; the&lt;/span&gt;
    &lt;span class="c1"&gt;-- post condition says that after the call there is a new entry,&lt;/span&gt;
    &lt;span class="c1"&gt;-- but the capacity is unchanged (it is obvious to you, not to&lt;/span&gt;
    &lt;span class="c1"&gt;-- SPARK).  SPARK will analyze the body of Append and it &lt;/span&gt;
    &lt;span class="c1"&gt;-- will try to prove that the contract is respected, that is, &lt;/span&gt;
    &lt;span class="c1"&gt;-- that the postcondition follows from the precondition.&lt;/span&gt;
    &lt;span class="c1"&gt;--&lt;/span&gt;
    &lt;span class="c1"&gt;-- If you arm correctly the code that calls Append, SPARK will&lt;/span&gt;
    &lt;span class="c1"&gt;-- try to prove that the precondition is always verified. If it  &lt;/span&gt;
    &lt;span class="c1"&gt;-- succeeds you know you'll never have an overflow!&lt;/span&gt;
    &lt;span class="c1"&gt;--&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- Some privacy needed... :-)&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;


   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Token_Array&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Positive&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;Unbounded_String&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;
   &lt;span class="c1"&gt;-- The funny "(Positive range &amp;lt;&amp;gt;)" says that the index of &lt;/span&gt;
   &lt;span class="c1"&gt;-- indexes of a variable of type Token_Array is a range of&lt;/span&gt;
   &lt;span class="c1"&gt;-- positive integers.  &lt;/span&gt;
   &lt;span class="c1"&gt;-- &lt;/span&gt;
   &lt;span class="c1"&gt;-- Yes, in Ada the array indexes do not *necessarily* start&lt;/span&gt;
   &lt;span class="c1"&gt;-- from zero... They can start from whatever you want... ;-D&lt;/span&gt;
   &lt;span class="c1"&gt;--&lt;/span&gt;

   &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Token_List&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;List_Length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;tagged&lt;/span&gt;
      &lt;span class="k"&gt;record&lt;/span&gt;
         &lt;span class="n"&gt;Tokens&lt;/span&gt;     &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Token_Array&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;others&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Null_Unbounded_String&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
         &lt;span class="n"&gt;First_Free&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Positive&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;record&lt;/span&gt;
     &lt;span class="k"&gt;with&lt;/span&gt; 
       &lt;span class="n"&gt;Predicate&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
         &lt;span class="n"&gt;Token_List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;First_Free&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nb"&gt;Integer&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Token_List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
     &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;Token_List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tokens&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Token_List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;-- &lt;/span&gt;
    &lt;span class="c1"&gt;-- This is the full definition of Token_List anticipated &lt;/span&gt;
    &lt;span class="c1"&gt;-- in the public part above.  In Ada it is not possible &lt;/span&gt;
    &lt;span class="c1"&gt;-- to have some field public and some private (like in C++&lt;/span&gt;
    &lt;span class="c1"&gt;-- or Ruby).  If you put the full definition in the public&lt;/span&gt;
    &lt;span class="c1"&gt;-- part, everything is public (typically considered bad practice),&lt;/span&gt;
    &lt;span class="c1"&gt;-- otherwise everything is private.  Honestly, I never felt the&lt;/span&gt;
    &lt;span class="c1"&gt;-- need of the hybrid solution.&lt;/span&gt;
    &lt;span class="c1"&gt;-- &lt;/span&gt;
    &lt;span class="c1"&gt;-- "Predicate" is another attribute.  It specifies a condition&lt;/span&gt;
    &lt;span class="c1"&gt;-- that a variable of type Token_List must always satisfy.&lt;/span&gt;
    &lt;span class="c1"&gt;--&lt;/span&gt;
    &lt;span class="c1"&gt;-- If you ask to the compiler to check assertions, the &lt;/span&gt;
    &lt;span class="c1"&gt;-- compiler will produce code that checks the predicate&lt;/span&gt;
    &lt;span class="c1"&gt;-- at the end of methods ("primitive procedures" in Ada &lt;/span&gt;
    &lt;span class="c1"&gt;-- jargon) that modify the variable and if the condition &lt;/span&gt;
    &lt;span class="c1"&gt;-- is not satisfied, an exception is raised, pointing an&lt;/span&gt;
    &lt;span class="c1"&gt;-- accusing finger against the culprit.  &lt;/span&gt;
    &lt;span class="c1"&gt;--&lt;/span&gt;
    &lt;span class="c1"&gt;-- A powerful bug trap!&lt;/span&gt;
    &lt;span class="c1"&gt;--&lt;/span&gt;



   &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Create&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;List_Length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Token_List&lt;/span&gt;
   &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Token_List&lt;/span&gt;&lt;span class="p"&gt;'(&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;     &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                   &lt;span class="n"&gt;Tokens&lt;/span&gt;     &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;others&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Null_Unbounded_String&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                   &lt;span class="n"&gt;First_Free&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

   &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Capacity&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Item&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Token_List&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Positive&lt;/span&gt;
   &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tokens&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Item&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Token_List&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Natural&lt;/span&gt;
   &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;First_Free&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Token_Lists&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now the &lt;em&gt;body&lt;/em&gt; (more or less the equivalent of a &lt;code&gt;*.c&lt;/code&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;Ada_2012&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;package&lt;/span&gt; &lt;span class="kd"&gt;body&lt;/span&gt; &lt;span class="nf"&gt;Token_Lists&lt;/span&gt; 
   &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;SPARK_Mode&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;On&lt;/span&gt; &lt;span class="c1"&gt;-- This body is in SPARK&lt;/span&gt;
&lt;span class="k"&gt;is&lt;/span&gt;

   &lt;span class="c1"&gt;------------&lt;/span&gt;
   &lt;span class="c1"&gt;-- Append --&lt;/span&gt;
   &lt;span class="c1"&gt;------------&lt;/span&gt;

   &lt;span class="kd"&gt;procedure&lt;/span&gt; &lt;span class="nf"&gt;Append&lt;/span&gt;
     &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;List&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;Token_List&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;What&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;is&lt;/span&gt;
   &lt;span class="k"&gt;begin&lt;/span&gt;
      &lt;span class="c1"&gt;--&lt;/span&gt;
      &lt;span class="c1"&gt;-- If you look at the definition of Token_List &lt;/span&gt;
      &lt;span class="c1"&gt;-- you'll see that List.Tokens is an array. &lt;/span&gt;
      &lt;span class="c1"&gt;-- List.Tokens'Last is the last index of Lists.Tokens&lt;/span&gt;
      &lt;span class="c1"&gt;-- &lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;First_Free&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tokens&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
         &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nb"&gt;Constraint_Error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tokens&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;First_Free&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;To_Unbounded_String&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;What&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;First_Free&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;First_Free&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Append&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Token_Lists&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Even if you do not know Ada, I think you should be able to understand the code.  &lt;/p&gt;

&lt;p&gt;As you can see, &lt;code&gt;Append&lt;/code&gt; is very simple and obviously correct, right?  I mean, the &lt;code&gt;if&lt;/code&gt; at the beginning stops every tentative of writing beyond the array &lt;code&gt;List.Tokens&lt;/code&gt;, right?&lt;/p&gt;

&lt;p&gt;Well, SPARK complained saying that it was not able to prove that an overflow would not happen.  My first reaction was something like &lt;em&gt;"Are you crazy?  Don't you see the check 4 lines above?  What is wrong with you?"&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;Anyway, next to the message error there was a "magnifying glass" icon. I clicked on it and I got, SPARK's courtesy, a &lt;em&gt;counterexample&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; [Counterexample] List = (Length =&amp;gt; ?, First_Free =&amp;gt; 2147483647) 
 and List.Tokens'First = 1 and List.Tokens'Last = 2147483647

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;What the... ? Ah! ... Yeah, OK, you are right...&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;In case you did not recognize it at once, &lt;code&gt;2147483647&lt;/code&gt; is 2^31 -1.  &lt;strong&gt;If&lt;/strong&gt; I created a &lt;code&gt;Token_List&lt;/code&gt; with 2 Giga entries (doubtful if it would fit on the stack... anyway...) &lt;strong&gt;and&lt;/strong&gt; I filled it completely, &lt;strong&gt;then&lt;/strong&gt; I &lt;em&gt;would&lt;/em&gt; actually have an overflow. &lt;/p&gt;

&lt;p&gt;Now I had two choices: the first one was to say that I will never use 2 Giga entries and ignore the case; the second one was to make it official that you can have at most 2^31 -2 entries.  This was really easy to do: it sufficed to change in the &lt;em&gt;specs&lt;/em&gt; file the line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;subtype&lt;/span&gt; &lt;span class="nf"&gt;List_Length&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nb"&gt;Integer&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="nb"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;that defines the type used for the list length to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;subtype&lt;/span&gt; &lt;span class="nf"&gt;List_Length&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nb"&gt;Integer&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="nb"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Last&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice the &lt;em&gt;-1&lt;/em&gt; at the end.  Since this type is used in the constructor&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Create&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;List_Length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Token_List&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;this guarantees that you'll never call &lt;code&gt;Create&lt;/code&gt; with &lt;code&gt;N&lt;/code&gt; larger than 2^31 -2 (if you try to do it, an exception is raised).  This placated SPARK. &lt;/p&gt;

&lt;p&gt;After catching this &lt;em&gt;border-line-condition&lt;/em&gt; bug I moved to SPARK-ing the rest of the code, catching another couple of border-line cases.  &lt;/p&gt;

&lt;p&gt;Overall, I can say that original code was fine since the bugs were triggered by some very extreme conditions; nevertheless it is nice to know that everything is clean now...&lt;/p&gt;

</description>
      <category>ada</category>
      <category>spark</category>
      <category>formalchecking</category>
      <category>bufferoverflow</category>
    </item>
    <item>
      <title>Reasons for loving Ada. #1: strong typing</title>
      <dc:creator>Riccardo Bernardini</dc:creator>
      <pubDate>Sun, 10 Dec 2017 16:41:32 +0000</pubDate>
      <link>https://dev.to/pinotattari/reasons-for-loving-ada-720</link>
      <guid>https://dev.to/pinotattari/reasons-for-loving-ada-720</guid>
      <description>&lt;p&gt;Yes, yes, I know... I am kind of an oddball for programming in Ada.  When the matter comes out in conversations I am usually asked to defend this choice of mine.  For future reference (and maybe to help others knowing this nice language) I decided to write few articles about what I like in Ada. &lt;/p&gt;

&lt;h1&gt;
  
  
  History and other stuff
&lt;/h1&gt;

&lt;p&gt;Before diving in the details, let me spend a couple of words about the language. Many never heard about Ada and many of those who did have some &lt;a href="http://www.electronicdesign.com/dev-tools/11-myths-about-ada"&gt;prejudices about it&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;The language Ada is named after &lt;a href="https://en.wikipedia.org/wiki/Ada_Lovelace"&gt;Ada Lovelace&lt;/a&gt; daughter of &lt;a href="https://en.wikipedia.org/wiki/Lord_Byron"&gt;Lord Byron&lt;/a&gt; (yes, &lt;em&gt;that&lt;/em&gt; Byron) and considered the &lt;a href="https://en.wikipedia.org/wiki/Ada_Lovelace#First_computer_program"&gt;first programmer in history&lt;/a&gt;.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Incidentally, did you notice that I write "Ada" and not "ADA"?  This is because it is a name of a person, not an acronym. Use "ADA" on an forum/newsgroup and you will be corrected in no time...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ada was developed in the '80s following an idea of the Department of Defence. (I am not replicating here the interesting history that is available &lt;a href="http://www.adahome.com/History/"&gt;elsewhere&lt;/a&gt;.) Since then it was updated every approximately ten years giving rise to several releases, informally known with the year number (Ada 83, Ada 95, Ada 2005 and Ada 2012, next release will probably be Ada 2020). Ada is a language very alive and modern, with several feature not easily found elsewhere: contracts, type invariants, formal checking, private packages, distributed programming, native multitasking and so on...&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Ada is quite flexible: it is used to write million-line code in avionics, but also to control small devices like the  &lt;a href="http://blog.adacore.com/tag/STM32"&gt;STM32&lt;/a&gt;.  See, for example, this &lt;a href="https://www.youtube.com/watch?v=MWYHBxHTW54"&gt;segway implemented with a Lego mindstorm&lt;/a&gt; controlled by a multitasking code (with a 16 bit processor and 64K of RAM!) &lt;a href="https://people.cs.kuleuven.be/~dirk.craeynest/ada-belgium/events/12/120204-fosdem/07-ada-segway-robot.pdf"&gt;presented at FOSDEM&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Why do you love Ada?
&lt;/h1&gt;

&lt;p&gt;A simple answer?  Reduced debugging time: my personal experience is that an Ada code will require maybe 1/10 of debugging time if compared with a C code of similar complexity. The reason is that  Ada compiler is much "stricter" than C, so that when the program compiles, it has an internal coherence that prevents the existence of many silly bugs (and most bugs are just silly!) that would have survived in a C code (functions with no return, a missing &lt;code&gt;break&lt;/code&gt;/&lt;code&gt;case&lt;/code&gt; in a &lt;code&gt;switch&lt;/code&gt;, dangling pointers, buffer overflows...). The remaining bugs are easily caught by the many &lt;em&gt;bug traps&lt;/em&gt; (I love this term) that the compiler adds to your code.  Programming in Ada is like doing &lt;a href="https://en.wikipedia.org/wiki/Pair_programming"&gt;&lt;em&gt;pair programming&lt;/em&gt;&lt;/a&gt;, but with &lt;em&gt;the compiler&lt;/em&gt; playing the role of the &lt;em&gt;observer&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Please note that &lt;em&gt;I am not&lt;/em&gt; claiming that by using Ada your code will be &lt;em&gt;automagically&lt;/em&gt; readable, maintainable and bug free.  You can write bad code in Ada, you just need to make an effort...  :-) &lt;br&gt;
Seriously, it is often said that the quality of the code depends mainly from the skill of the programmer, rather than from the type of tools. Nevertheless, using the right tool will help you to achieve the same result with less effort. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  OK, OK, I understand... But can you make me an example?
&lt;/h2&gt;

&lt;p&gt;Sure, I am here for this.  Maybe the simplest characteristic of Ada that helps you in writing robust software is the &lt;em&gt;type system&lt;/em&gt;, especially its &lt;em&gt;strong typing&lt;/em&gt; nature. Let me explain with an example: if in C you write&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;serial_number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can assign with no problem a variable of type &lt;code&gt;port&lt;/code&gt; to a variable of type &lt;code&gt;serial_port&lt;/code&gt; since both are integers; but if you do in Ada&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Serial_Number&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Port&lt;/span&gt;          &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you &lt;em&gt;cannot&lt;/em&gt; assign a variable of type &lt;code&gt;Port&lt;/code&gt; to a variable of type &lt;code&gt;Serial_Number&lt;/code&gt; although both "under the hood" are implemented as integers.  This is because they actually &lt;em&gt;represents two different things&lt;/em&gt; despite being implemented in the same way at the lowest level. Well, it makes sense, doesn't it? Why should you want to use a TCP port as a serial number?  Most probably, if you try to do such a thing, there is an error somewhere. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What if you actually need to use a port as a serial number?  No problem, you can convert it with &lt;code&gt;Serial := Serial_Number(Client_Port);&lt;/code&gt;. Note that this conversion has no cost since both are integers; it is just a way to tell to the compiler "Listen, this is not an error, I know what I want, please bear with me and assign this value."&lt;/p&gt;

&lt;p&gt;Note the use of "camel case with underscores" for names that are not keywords.  This style is quite common in modern Ada code.  It is not mandatory, of course, but personally, I find it quite readable.  Please also note that Ada is case-insensitive, so that you could write, e.g., &lt;code&gt;serial_number&lt;/code&gt;.  Oh, yes, and the use of ":=" for assignment.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Actually, the code above is not the best choice.  How do you know that an &lt;code&gt;Integer&lt;/code&gt; will be large enough to keep a serial number or a port?  Well, on current Intel processor (whit 32-bit integers), that is quite reasonable, but what if you have a small micro-controller with 16 bit integers?  Well, maybe the best thing is to let the compiler to decide by writing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Serial_Number&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="mi"&gt;999_999&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Port&lt;/span&gt;          &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note the use of '_' as separator in numbers.  A simple thing, but really convenient...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Note how  we do &lt;em&gt;not&lt;/em&gt; say to the compiler which low-level implementation to use, but which &lt;em&gt;characteristics&lt;/em&gt; we need and let the compiler to decide how to handle them.  On a microcontroller with 16-bit &lt;code&gt;int&lt;/code&gt;s maybe &lt;code&gt;Serial_Number&lt;/code&gt; will be implemented as a &lt;code&gt;long int&lt;/code&gt;, while &lt;code&gt;Port&lt;/code&gt; will be an &lt;code&gt;unsigned int&lt;/code&gt;. But who really cares? Let the compiler take care of this boring stuff...&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What if you need &lt;code&gt;Serial_Number&lt;/code&gt; to have a specific size, say  24 bits (because, for example, you need to write it in a packet)?  Just write&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Serial_Number&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="mi"&gt;999_999&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;Size&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;I do not want to dig deeper in the Ada type system, but I cannot resist telling you about two types that are not commonly found elsewhere.  If you write &lt;code&gt;type Volt is delta 0.125 range 0.0 .. 255.0;&lt;/code&gt; the variables of type &lt;code&gt;Volt&lt;/code&gt; will hold a &lt;a href="https://en.wikipedia.org/wiki/Fixed-point_arithmetic"&gt;&lt;em&gt;fixed point real&lt;/em&gt;&lt;/a&gt; ranging in the interval 0 V..255 V with step 0.125 V. Fixed point numbers are usually implemented as integers and are used, for example, in some DSP applications running on small processors without floating point maths.  Another uncommon type is the &lt;em&gt;decimal fixed point&lt;/em&gt; type defined with something like &lt;code&gt;type Money is delta 0.01 digits 15;&lt;/code&gt;.  I'll let you &lt;a href="https://en.wikipedia.org/wiki/Fixed-point_arithmetic#Binary_vs._decimal"&gt;discover about them&lt;/a&gt;.  See also the corresponding &lt;a href="http://www.adaic.org/resources/add_content/standards/12rm/html/RM-3-5-9.html"&gt;Reference Manual page&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  An example: cleaning user input
&lt;/h1&gt;

&lt;p&gt;Let us make a toy (but not so-toy) example of exploitation of the strict typing of Ada. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Someone could object that there are other ways to handle the problem considered here.  Yes, I know, but I just need a &lt;em&gt;simple&lt;/em&gt; example to show what you can do with strict typing.  I am not claiming that is the &lt;em&gt;only&lt;/em&gt; (or &lt;em&gt;best&lt;/em&gt;) solution (although, I think it is quite good).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is well known that in any application using user input care must be taken in using the user input since it could open security holes.  The following &lt;a href="https://xkcd.com/327"&gt;xkcd comic&lt;/a&gt; is a classic&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KT-c06nV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgs.xkcd.com/comics/exploits_of_a_mom.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KT-c06nV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgs.xkcd.com/comics/exploits_of_a_mom.png" alt="alt text" title="Exploit of a Mom"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Of course, we can pay all the attention we want to not use user input before sanitizing it, but if the application is very large and everything is a &lt;code&gt;String&lt;/code&gt; (or &lt;code&gt;char*&lt;/code&gt;), something can slip in the cracks...  Type strictness can help us here.  We just need to define a new type &lt;code&gt;Dirty_String&lt;/code&gt; and have all the user input function return a &lt;code&gt;Dirty_String&lt;/code&gt; rather than a string (this is easier to check).  The only way to transform a &lt;code&gt;Dirty_String&lt;/code&gt; in a normal &lt;code&gt;String&lt;/code&gt;  will be via a special &lt;code&gt;Sanitize&lt;/code&gt; function.  &lt;/p&gt;

&lt;p&gt;Let's dig into details.  We will define the following package &lt;em&gt;specs&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;package&lt;/span&gt; &lt;span class="nf"&gt;Dirty_Strings&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; 
  &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Dirty_String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Sanitize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Dirty_String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Taint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Dirty_String&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt;
  &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Dirty_String&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Taint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Dirty_String&lt;/span&gt;
  &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Dirty_String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;Dirty_Strings&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In Ada a package is divided into two parts: its &lt;em&gt;specs&lt;/em&gt; that specifies the "resources" exported by the package and its &lt;em&gt;body&lt;/em&gt; with the actual implementation.  The &lt;em&gt;specs&lt;/em&gt; are further divided into a &lt;em&gt;public&lt;/em&gt; part (visible to the rest of the code) and an optional &lt;em&gt;private&lt;/em&gt; part.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This package define a type &lt;code&gt;Dirty_String&lt;/code&gt;.  In the &lt;em&gt;public&lt;/em&gt; part (before the &lt;code&gt;private&lt;/code&gt;) the type is defined as &lt;code&gt;private&lt;/code&gt;, that is, &lt;em&gt;none of your business&lt;/em&gt;.  Moreover, the package exports two function that can be used to convert from &lt;code&gt;Dirty_String&lt;/code&gt; to normal &lt;code&gt;String&lt;/code&gt; and vice-versa.&lt;/p&gt;

&lt;p&gt;However, in the &lt;em&gt;private part&lt;/em&gt; we see that a &lt;code&gt;Dirty_String&lt;/code&gt; is just... a &lt;code&gt;String&lt;/code&gt;. Putting its definition in the private part prevents &lt;em&gt;short-cut conversions&lt;/em&gt; from &lt;code&gt;Dirty_String&lt;/code&gt; to &lt;code&gt;String&lt;/code&gt; and forces the programmer to go through the function &lt;code&gt;Sanitize&lt;/code&gt; that, we guess, will do stuff like quoting special characters.  Instead, the conversion of a normal &lt;code&gt;String&lt;/code&gt; to a &lt;code&gt;Dirty_String&lt;/code&gt; is just a type conversion since there is no need to change it.  This allows us to define it as a &lt;em&gt;expression function&lt;/em&gt; (see also the &lt;a href="http://www.adaic.org/resources/add_content/standards/12rm/html/RM-6-8.html"&gt;RM&lt;/a&gt;) that, most probably, will be "inlined" by the compiler.&lt;/p&gt;

&lt;h1&gt;
  
  
  Run-time constraints
&lt;/h1&gt;

&lt;p&gt;Let me conclude with a feature of Ada 2012 that I find quit cute (and useful). Few years ago, I wrote a small package to write Matlab files from Ada.  One day I discovered that the package was writing files that could not be read from Matlab. The reason was that the names of the variables in the Matlab file were not valid Matlab names.  After correcting the bug, I decided to add a new &lt;em&gt;bug trap&lt;/em&gt; to the code. I defined the type to be used for variable names as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nf"&gt;Matlab_Name&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;
     &lt;span class="nb"&gt;String&lt;/span&gt;
   &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;Dynamic_Predicate&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
     &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;all&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;Matlab_Name&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Range&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;Is_Alphanumeric&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Matlab_Name&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;I&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;Matlab_Name&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;I&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'_'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="ow"&gt;and&lt;/span&gt;
       &lt;span class="n"&gt;Is_Letter&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Matlab_Name&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Matlab_Name&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;First&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have a bit of programming experience, you should be able to understand the code above, even if you do not know Ada.  As you can see &lt;code&gt;Matlab_Name&lt;/code&gt; is a &lt;code&gt;String&lt;/code&gt; (but you cannot mix it with other strings, e.g., a filename!), but it also must satisfy a &lt;code&gt;Dynamic_Predicate&lt;/code&gt; (a condition that is checked at run-time, if you ask the compiler to do so).  The condition can be split in two, the first part&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;all&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;Matlab_Name&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;Range&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;Is_Alphanumeric&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Matlab_Name&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;I&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;Matlab_Name&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;I&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'_'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;requires that every character in the name must be a letter, a digit or an underscore, while the second part&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ada"&gt;&lt;code&gt;&lt;span class="n"&gt;Is_Letter&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Matlab_Name&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Matlab_Name&lt;/span&gt;&lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="na"&gt;First&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;requires that the first character must be a letter.  If checks are enabled and at some time your code generates a bad name, an exception will be raised precisely in the point were the bug happened. (This pinpointing of bugs helps a lot in debugging...)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;What about efficiency&lt;/em&gt;?  Yes, I can hear you, efficiency lovers.  The idea of having checks done at run-time seems to go against the idea of efficiency.  Well, it is true, run-time checks costs in term of efficiency, but in my experience you do not even notice it.  Unless you are on a very strict time budget (or unless you have very complex checks) it is usually more convenient to keep them on to catch possible bugs. You should take the decision of turning checks off only after discovering that you cannot afford them and you should turn off only those that are in most computationally intensive parts (possibly after thoroughly testing).&lt;/p&gt;

&lt;p&gt;An alternative approach is to code in &lt;a href="https://en.wikipedia.org/wiki/SPARK_(programming_language)"&gt;SPARK&lt;/a&gt; (a subset of Ada, nothing to do with Apache) in order to check your code formally. I'll let you discover the joy of SPARK... :-)&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>ada</category>
      <category>strongtyping</category>
      <category>correctness</category>
      <category>debug</category>
    </item>
    <item>
      <title>Which programming language should you learn?</title>
      <dc:creator>Riccardo Bernardini</dc:creator>
      <pubDate>Thu, 29 Dec 2016 12:56:42 +0000</pubDate>
      <link>https://dev.to/pinotattari/which-programming-language-should-you-learn-9gb</link>
      <guid>https://dev.to/pinotattari/which-programming-language-should-you-learn-9gb</guid>
      <description>&lt;p&gt;This question turns out now and then on forums, Facebook, and so on. Since I got tired of writing always the same things, I decided to write my suggestions here once for all.&lt;/p&gt;

&lt;p&gt;As usually, with this kind of questions, everything depends on your starting point (are you a beginner? an amateur?) and your target (do you want to do web development? Contribute to open source projects? Program small devices like STM32?). Usually who asks this question is a beginner and does not say anything about specific goals, therefore usually I assume that the objective is to get some “culture” in this field. The answer that follows is written with this generic objective in mind.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1 : learn to program (simplicity is the key)
&lt;/h3&gt;

&lt;p&gt;If you have no experience in programming at all, I will suggest starting with something simple, in order to learn &lt;em&gt;how to program&lt;/em&gt; before actually &lt;em&gt;learning a language&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Yes, because “learning to program” is not the same of “learning a programming language.” Learning to program means learning how to decompose a problem into smaller problems and specify a procedure to solve them. Knowing how to do this is a skill that is independent on the specific language and somehow on a higher level than knowing programming languages. You can write programs without even having a PC, but just pen and paper.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Ada_Lovelace"&gt;Ada Lovelace&lt;/a&gt;, daughter of Lord Byron (yes, &lt;strong&gt;that&lt;/strong&gt; Byron!), is considered the first programmer in history because of her notes to the &lt;a href="http://www.fourmilab.ch/babbage/sketch.html"&gt;article&lt;/a&gt; on the &lt;a href="https://en.wikipedia.org/wiki/Analytical_Engine"&gt;Analytical Engine&lt;/a&gt; designed (but never built) by &lt;a href="https://en.wikipedia.org/wiki/Charles_Babbage"&gt;Charles Babbage,&lt;/a&gt; a kind of computer of ‘800, but with the register made with gears and powered with steam… (I would suggest reading the &lt;a href="http://www.fourmilab.ch/babbage/sketch.html"&gt;Ada Lovelace article&lt;/a&gt;, it is very interesting)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Of course, even for learning the skill of programming a language is necessary, but I would stay on something simple.&lt;/p&gt;

&lt;p&gt;A language that was suited for beginners, in my opinion, was BASIC. It was fairly popular in the ’80 when every home computer had some kind of BASIC interpreter built-in. Although simple, it had all the necessary features and one could write quite complex stuff with it, although the resulting code was not very maintainable. An advantage of BASIC was that it was interpreted: you could write a single command and having it executed immediately, without the need of an IDE.&lt;/p&gt;

&lt;p&gt;Since BASIC is much less popular nowadays, I think that C is another good choice as a starting point since it is fairly simple. C is also quite low-level, giving you visibility of some machine level stuff like pointers and stack and this is useful because it gives you a sensibility about what happens “under the hood” even with other languages. (I find that this kind intuition helps me a lot when I learn a new language). Be sure to learn even all the tiny, obscure details of C such as pointers, struct, unions, functions with variable argument list (such as printf).&lt;/p&gt;

&lt;p&gt;Also with C you will learn about macro expansion, a methodology used in several places and that requires a mindset slightly different from the one used in imperative languages like C. To be honest, I do not love macro expansion since it is quite error-prone and it can make the code less readable, but there are context where it is useful.&lt;/p&gt;

&lt;h3&gt;
  
  
  Second step: best practices &amp;amp; OOP with Ada
&lt;/h3&gt;

&lt;p&gt;Since object-oriented programming (OOP) is widely diffused and fashionable, as a second language many would suggest C++ or Java. However, I am going to be heretic and suggest &lt;a href="https://en.wikipedia.org/wiki/Ada_(programming_language)"&gt;Ada&lt;/a&gt;. Beyond allowing OOP, it is a modern language, very powerful with strong emphasis on correctness and maintainability. It can introduce you also to stuff that it is not common in other languages such as contracts, type invariants and formal checking. Studying it you will learn some “good practices” in programming that will be useful even with other languages.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Since Ada is not widely used, finding information in the network is not easy. Few useful links: you can get an open-source, gcc-based compiler here: &lt;a href="http://l.facebook.com/l.php?u=http%3A%2F%2Flibre.adacore.com%2F&amp;amp;h=zAQFgNuwR"&gt;http://libre.adacore.com/&lt;/a&gt;. For an introduction to the language see the &lt;a href="http://u.adacore.com/"&gt;Adacore University&lt;/a&gt;, for an easy reference see the &lt;a href="https://en.wikibooks.org/wiki/Ada_Programming"&gt;Wikibook&lt;/a&gt;, for a mix of resources see the &lt;a href="http://www.adaic.org/"&gt;Ada Information Clearinghouse&lt;/a&gt; (where you can find the reference manual) and the site of &lt;a href="https://www.adacore.com/"&gt;Adacore&lt;/a&gt;. Finally, you can meet other Ada-ists on the usenet group comp.lang.ada and on some LinkedIn groups.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Now you have the most important basics
&lt;/h4&gt;

&lt;p&gt;Once you have a good experience of C and Ada, learning other languages is just a matter of learning a new syntax and maybe one or two new concepts. I, for example, learned Ruby in few hours just using a tutorial. In the end, most of the current languages are very similar one another.&lt;/p&gt;

&lt;h3&gt;
  
  
  Polishing it : Ruby/Python and assembly
&lt;/h3&gt;

&lt;p&gt;Finally, to round everything up, I would suggest, as the cake icing, a scripting language like Ruby or Python and maybe some assembly. It is true that assembly is not used much nowadays (although it depends on your context), but some experience of assembly will give you some good understanding of what happens “under the hood.”  Moreover, it is so much fun to go so close to the processor that you can almost feel the silicon... :-)&lt;/p&gt;

&lt;h3&gt;
  
  
  Strange stuff: functional languages, PROLOG, ….
&lt;/h3&gt;

&lt;p&gt;If you really want the widest view, you can also try some fancy stuff like functional languages (Eiffel, Haskell, … even LISP) and logic programming stuff like PROLOG. Those languages have a “model” that is different from the usual “procedural” model. I did not see them used too much and I wonder if they are suited for very large scale and very long lived software, but it is useful to know that they exist and how they work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusions and final remarks
&lt;/h3&gt;

&lt;p&gt;I hope that you found this useful or that, at least, gave you some ideas about programming. A final suggestion: my experience is that the best way to learn a new language is to use it. After you got acquainted with the syntax, choose a project of intermediate difficulty (even a silly one, it does not necessarily need to be useful) and do it (and, possibly, have fun…)&lt;/p&gt;

</description>
      <category>programming</category>
      <category>learning</category>
      <category>programminglanguage</category>
      <category>languagelearning</category>
    </item>
  </channel>
</rss>
