29

I tried googling and read some snippets online. Why is Ada a "safety critical" language? Some things I notice are

  • No pointers
  • Specify a range (this type is an integer but can only be 1-12)
  • Explicitly state if a function parameter is out or in/out
  • Range-based loops (To avoid bound errors or bound checking)

The rest of the syntax I either didn't understand or didn't see how it helps it to be 'safety critical'. These are some points but I don't see the big picture. Does it have design-by-contract that I am not seeing? Does it have rules that make it harder for code to compile (and if so what are some?) Why is it a 'safety critical' language?

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
  • 1
    I think it has to be looked at in comparative fashion to answer that question. Compare it to C (less restrictive all about) and then to Haskell (more restrictive in some cases, less in others [without becoming complicated ;-]), for instance. –  Oct 18 '11 at 23:17
  • 8
    Ada does have pointers; it calls them "access types". – Keith Thompson Oct 18 '11 at 23:57
  • 7
    For even more safety critical systems, use SPARK. It's a subset of Ada with annotations for additional checks: http://en.wikipedia.org/wiki/SPARK – Rommudoh Oct 19 '11 at 08:10
  • 1
    See also type safety : http://en.wikipedia.org/wiki/Type_safety – NWS Oct 19 '11 at 15:36
  • 3
    And access types in Ada are true pointers and nothing else; not the address/integer hybrids of C. – WGroleau Jun 02 '14 at 04:33

5 Answers5

17

Well, this is pretty simple. The reason there's a lot of Ada sytax that doesn't seem to have much of anything to do with making the language "safety-critical" (whatever that means for a language) is that this was not Ada's design goal. It was designed to be a general-purpose compiled system's programming language, sufficently capable that the U.S. Department of Defense could get rid of all its little one-use languages it had to support all over the place.

The fact that the end result is a language that is rather useful for safety-critical applications was just a happy side-effect of the fact that the language was very well-designed with military applications (where lives are often staked on the software's reliability) in mind.

Surprisingly few other modern languages had support for building reliable software as a design goal. Most seem to be cooked up by a lone "genius" hacker, with the chief goal being the ability to facilitate cranking out lots of code quickly, perhaps in some new way that hacker favors.

Community
  • 1
  • 1
T.E.D.
  • 44,016
  • 10
  • 73
  • 134
  • 6
    "_Surprisingly few other modern languages had support for building reliable software as a design goal._" which modern "compiled" language do _not_ have this goal? – curiousguy Dec 08 '11 at 21:06
  • 10
    @curiousguy - Note the past tense. I'm talking late 70's to early 80's here. The C designers in fact were invited to put C up for consideration at the time, and declined saying exactly this. – T.E.D. Apr 03 '12 at 16:30
  • 2
    "The fact that the end result is a language that is rather useful for safety-critical applications was just a happy side-effect" - i just had to downvote this, reliability was one of the initial language design goals: *"1B. Reliability. The language should aid the design and development of reliable programs. The language shall be designed to avoid error prone features and to maximize automatic detection of programming errors. The language shall require some redundant, but not duplicative, specifications in programs."* – user7860670 May 12 '20 at 15:17
  • 1
    @user7860670 - *Reliability* and *safety-critical* are two different (but somewhat related) issues. Ada was indeed designed for the former, but the latter is a specific discipline that has largely developed after 1983, and was not called out in the original language design goals. For an example of a language that *was* designed specificaly for safety-critical applications, check out [SPARK](https://en.wikipedia.org/wiki/SPARK_(programming_language)) (which is largely an Ada subset). – T.E.D. May 12 '20 at 15:51
14

All of those are good for safety-critical application; but consider also the ability to assign a layout (down to the bits) and the ability to [optionally] specify that such a record can ONLY be at a certain location (useful for things like video-memory mappings).

Consider that a lot of safety-critical applications are also without standard (in the senses both of "wide-spread" and of forward-comparability) interfaces; example: nuclear reactors, rocket engines (the engineering itself differs from generation to generation*), models-of-aircraft.

The upcoming Ada 2012 standard DOES have actual contracts, in the form of pre- and post-conditions; example (taken from http://www2.adacore.com/wp-content/uploads/2006/03/Ada2012_Rational_Introducion.pdf):

generic
   type Item is private;
package Stacks is

type Stack is private;

function Is_Empty(S: Stack) return Boolean;
function Is_Full(S: Stack) return Boolean;

procedure Push(S: in out Stack; X: in Item)
with
    Pre => not Is_Full(S),
    Post => not Is_Empty(S);

procedure Pop(S: in out Stack; X: out Item)
with
    Pre => not Is_Empty(S),
    Post => not Is_Full(S);

Stack_Error: exception;

private
 -- Private portion.
end Stacks;

Also, another thing that gets glossed over, is the ability to exclude Null from your Access/pointer types; this is useful in that you can a) specify that exclusion in your subprogram parameters, and b) to streamline your algorithm [since you don't have to check for null at every instance-of-use], and c) letting your exception handler handle the (I assume) exceptional circumstance of a Null.

* The Arianne 5 disaster occurred precisely because the management disregarded this fact and had the programmers use the incorrect specifications: that of the Arianne 4.

Shark8
  • 4,095
  • 1
  • 17
  • 31
  • 2
    Re Arianne 5 - The program worked correctly, for an arianne 4, and when the trajectory was outside the limits for an arianne 4, it correctly triggered the self destruct. – NWS Oct 19 '11 at 15:33
  • 1
    Exactly the point; if the Arinne 5 and 4 were compatible engineering-wise there wouldn't have been a problem any more than there would be a problem with the software on a 747 if you were to rip out all the passenger portions to use it as a cargo plane for your furniture. – Shark8 Oct 20 '11 at 22:58
  • 1
    "_it correctly triggered the self destruct_" the aerodynamic forces broke the launcher actually. – curiousguy Dec 08 '11 at 21:09
  • 4
    Consider also that Ada makes almost impossible many of the mistakes that some people falsely claim "real programmers" are too smart to make. – WGroleau Jun 02 '14 at 04:36
10

AdaCore has a nice presentation of various safety features of Ada 2005 here:

http://www.adacore.com/knowledge/technical-papers/safe-secure/

The US Government and industry also ran studies on program reliability a long time ago that compared languages. I couldn't find one very quickly as the sites are all old (!) but here's a quote from DDCI's website you: " In studies conducted during the eighties Ada consistently outperformed established programming languages like Pascal, Fortran, and C. In the nineties, Ada continues to surpass C++ in performance evaluations measuring capability, efficiency, maintenance, risk, and lifecycle cost."

Lists reasons they used it on Commanche project in link below. I'll add that the platform implementations have been around for a LONG time and stayed stable. Like a source in article said, maintenance is where majority of costs come in. We've seen the modern contenders .NET and Java change like crazy. Long-term stability of Ada is better for safety-critical apps which are often fielded for long periods (sometimes decades).

http://www.ddci.com/displayNews.php?fn=programs_rah66.php

Another benefit is Ada was designed for cross-language development. I keep seeing in the news people talking about how .NET and JVM are innovative b/c they let you mix the "right tools for the job" into one system. Ada's had that ability for a long time. It's common for apps to be a mix of Ada, C, C++, assembler, etc. (MULTOS CA comes to mind.) And they still function well.

It hasn't been static either. They keep updating the language, most recently in 2012. Its portability allowed it to run on both JVM and .NET too for people that want the libraries or have plenty existing code on those. There's also Ada development tools and robust runtimes for many OS's and RTOS's from IBM, Aonix, AdaCore, and Green Hills.

Last benefit: if it will compile, it will work. Usually.

Nick P
  • 1,477
  • 1
  • 11
  • 14
  • 1
    I was looking for that reference for you but found this gem instead: a NASA guide on many aspects of safety-critical software development. Page 207 focuses on picking the language for the job. It lists what the look for, evaluates major languages, & picks a winner. (Used archive.org cuz govt shutdown killed real link) – Nick P Oct 03 '13 at 00:49
  • http://web.archive.org/web/20130510145632/http://www.hq.nasa.gov/office/codeq/doctree/871913.pdf – Nick P Oct 03 '13 at 00:50
  • 6
    One point made far too briefly in that article : early detection of errors. Even if an error persists until runtime, chances are you'll get an exception with a message pointing spookily close to the error, rather than a silent corruption that manifests as something else, long after the failure... IMO this alone is a **huge** advantage –  Oct 03 '13 at 21:09
4

I know this is late, but it's an example I recently encountered. I wrote the following C++ function that worked fine in -O0:

size_t get_index(const Monomial & t, const Monomial & u) {
    get_index(t, u.log()); // forgot to type "return" first...
}

This actually compiles, and while it might emit a warning if you're lucky to have a decent compiler, you're not likely to see it when it's one of a lot of programs being compiled. Miraculously, it ran just fine when I compiled it with -O0. But when I compiled it in -O3 it crashed every time, and for the life of me I couldn't figure out why, because I didn't see the warning (if it even appeared). Imagine debugging that when you think you imagine a return there simply because you know your intent.

Likewise, back when I was learning C I frequently made this mistake:

int a;
scanf("%d", a); /* left out & before the a */

Using int's for pointers and vice versa is considered normal programming practice in C, so much so that compilers 25 years ago didn't even bother to emit a warning. Heck, that was a feature of C, not a bug. (See, for instance, Brian Kernaghan's "Why Pascal is not my favorite Language.") And of course back then home computer OS's didn't have memory protection; if you were lucky the computer wasn't writing to hard disk when it reset.

These kinds of mistakes won't even compile in Ada. Functions have to return a value, and you cannot accidentally use an Integer in place of an access Integer (i.e., pointer to integer).

And that's just the start!

John Perry
  • 2,497
  • 2
  • 19
  • 28
  • Uninitialized variables aren't zero initialized in Ada. – Molly Stewart-Gallus Jul 19 '19 at 20:47
  • 2
    I came across a program where the off-line test version (Windows) worked fine, but the real version (VxWorks on PowerPC) failed precisely because the Windows compiler zeroed uninitialized data & the PPC one didn’t. – Simon Wright Nov 08 '20 at 21:24
2

Ada has superior support for real-time http://www.adaic.org/resources/add_content/standards/05rat/html/Rat-1-3-4.html . This allows the programmer to implement a greater degree of determinism rather than worry about the technical details of the programming language itself. While many of the run-time features supported by Ada can be achieved in C with some deep understanding of things, Ada achieves most real-time features very well since it's standardized. Ada even has a Ravenscar profile http://en.wikipedia.org/wiki/Ravenscar_profile and SPARK a computer language where "highly reliable operation is essential" is based on a subset of Ada 83 and 95 http://en.wikipedia.org/wiki/SPARK_%28programming_language%29. My guess is that SPARK does not have a version for later Ada versions b/c it is too early to tell how safe the newer versions really are. It's also mentioned in the latter article that Ada can be optimized for speeds rivaling C which would be important for real-time applications that relied on precise control during rapidly changing events. There are many built in standard features for real-time control which are obviously important for a 'safety critical' language.

  • 4
    Actually Spark-2014 builds on Ada-2012 contracts to make formally provable code easier to write. http://www.spark-2014.org/ –  Oct 03 '13 at 20:38