Su(per)btypes in Ada 2012 – Part 1

If you want to learn a bit about types and the Static_Predicate aspect in Ada 2012, then this AdaCore gem has the good stuff:

Ada 2012 is full of features for specifying a rich set of type properties. In this series of three Gems, we describe three aspects that can be used to state invariant properties of types and subtypes. This first Gem is concerned with the Static_Predicate aspect.

With the Static_Predicate aspect you can do cool stuff like this:

type Day is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
type T_Day is new Day with Static_Predicate => T_Day in Tuesday | Thursday;

Nice eh’? The compiler will now protect you against assigning Friday to a T_Day. That is pretty neat. Stay tuned for part 2 and 3, where more of the Ada 2012 goodness will be explained.

Dr. Dobbs Got The Lowdown on Ada 2012 Contracts

In an article aptly named Ada 2012: Ada With Contracts we get a very nice presentation of the new Ada 2012 facilities for contract-based programming.

The most recent version of the Ada standard, known as Ada 2012, brings contract-based programming to a mainstream language. Preconditions, postconditions, type invariants, and subtype predicates allow software developers to specify their programs’ intent more clearly, in effect embedding requirements into the source code. This feature makes it easier to review and understand the program, and it facilitates verification by the compiler, static analysis tools, and/or runtime checks.

I’ve used these new-fangled shinys a bit, and they are nice. Very nice actually. There’s just something truly awesome about being able to add pre/post-conditions to your method specification. The extra level of information and protection against errors is pretty amazing, and it is so darn easy to work with.

If you haven’t already tried your hand at these new features, be sure to do so. As the article says at the very end:

…the techniques of contract-based programming represent a significant advance.

I wholeheartedly agree.

New Ada 2012 Rationale

The Ada 2012 Rationale has been released to the joy and benefit of all Ada programmers.

The Rationale for Ada 2012 provides an overview of new Ada 2012 features, examples of their use, compatibility with Ada 95 and 2005, and more. It was written by John Barnes, and was sponsored in part by the Ada Resource Association. This is an unofficial description of the language; refer to the proposed Ada 2012 standard for detailed language rules.

Written by John Barnes this is not to be missed. The Ada 2012 Rationale is available as both HTML and PDF.

Iterators in Ada 2012

Emmanuel Briot from AdaCore has written a couple of Ada gems about the new Ada 2012 iterators:

Ada 2012 iterators provide syntactic sugar for iterating over data structures. This Gem describes the new syntax and what it brings to the language. Part 2 will explain how you can define your own iterators when formulating new data structures.

This is very good stuff. Be sure not to miss these two articles.

Some Fast Ada Programming News

First lets start with the fact that it just got easier to build Ada packages for Debian. YAY!

Second it’s great to know that the Ada networking library, Anet, just hit version 0.1 – Get it while it’s hot.

Third you’ll be thrilled to know that the What would you like in Ada 202X? thread on comp.lang.ada has gone berserk! Fun read, if you have the time.

Fourth we get ZanyBlue, which is a project implementing a finite element based solution of the Maxwell field equations. En route to that goal, supporting libraries and applications are being implemented. Initial functionality covers localization support for Ada. ZanyBlue is now at version 1.0.0 BETA. Here’s the release announcement.

Finally Stephe Leake did the entire Ada 2005, 2012 (draft 17) reference manual in info format. Pretty awesome, if you ask me.

Trimming cost and size: The software side of the story

In his Trimming cost and size: The software side of the story article Benjamin Brosgol from AdaCore delves into the subject matter with his usual gusto. If you’ve done any kind of software development, you’ll know that the cheapest bugs to fix are those found before the product is launched and this even more so for embedded software, where it can be a real pain to update the devices. Brosgol does a good job of describing these issues:

For several reasons, buggy software is especially acute in embedded applications. First, an embedded system typically monitors or controls some external device, so in critical applications an error or vulnerability could compromise safety, security or both; lives could be lost, and substantial assets could be at risk.

Second, debugging embedded software is harder than debugging native software. For example, embedded systems generally involve concurrency and real-time constraints, which introduce opportunities for errors such as deadlock, missed deadlines and corrupted data. Because some of these bugs are timing dependent and not easily reproduced, they could go undetected until the software is fielded. With perhaps thousands or even millions of systems in use, even a bug with low probability of being triggered will occur eventually.

Third, correcting a defect in an embedded product already in service is complex and expensive. Solutions that involve wireless uploads raise serious security issues that are just beginning to be addressed in equipment ranging from medical devices to automotive systems.

One solution to help programmers produce fewer bugs is design by contract or contract-based programming, and halfway down Brosgol introduce us to the contract features of Ada 2012, specifically the pre- and postconditions for subprograms:

Other language technologies treat contracts as run-time constructs. A recent example is Ada 2012, in which contracts take the form of Boolean conditions that are supplied in contexts such as invariants for types and preconditions and/or postconditions for subprograms. The programmer can control whether the software generates code to check these conditions at run-time; a failed check raises an exception. In this way, the specified contracts can be used either as formal comments that document the program’s intent or as run-time checks that are part of a testing regimen.

Next Brosgol goes on to talk about the extensive restrictions programmers can utilize in Ada to tailor the run-time requirements to their exact needs, using the Restrictions pragma. The entire article is a very nice and informative read, so be sure not to miss it.

Overridable Class Attributes in Ada 2012

One of the nicest features of the AdaCore website is the Ada Gem of the Week. Currently sitting at 117 gems, this place is a regular treasure-trove for Ada programming.

Today’s gem is written by Emmanuel Briot and it deals with some of the new Ada 2012 features, specifically in relation to object oriented Ada programming. it’s called Gem #117: Design Pattern: Overridable Class Attributes in Ada 2012.

Most object-oriented programming languages provide a facility for declaring variables that are shared by all objects of a given class. In C++, these are called “static members” (and use the “static” keyword), and similarly Python has the notion of “class attributes”.

Let’s consider an example where this is useful. For instance, let’s say we want to define the notion of a block of text that is generated by expanding a template (perhaps after we replace some parameters in that template, as can be done with AWS’s templates parser, for instance). Once we have computed those parameters, we might want to generate multiple outputs (for instance HTML and CSV). Only the template needs to change, not the computation of the parameters.

I can’t say I’m a huge expert on object oriented Ada programming, but this little snippet actually made me hungry for playing around with some of the new Ada 2012 features, such as expression functions, where you can do stuff like this:

pragma Ada_2012;
type Text_Block is tagged null record;
function Template (Self : Text_Block) return String
   is ("filename.txt");
function Render (Self : Text_Block) return String;

Instead of the old more clunky method:

type Text_Block is tagged null record;
function Template (Self : Text_Block) return String;
function Render (Self : Text_Block) return String;
function Template (Self : Text_Block) return String is
   pragma Unreferenced (Self);
   return "file_name.txt";
end Template;

Very nice indeed! The new Ada 2012 syntax allows for a much more simple and readable setup of the Text_Block type. But don’t take my word for it, go read the Gem instead. It is definitely worth it.

Final version of the Ada 2012 standard released

Draft 14 of the Ada 2012 standard is going to be the final version of what will end up as Ada 2012, so if you have any issues with the standard, now is the time to speak up.

If you want to know more about all the shiny new stuff in Ada 2012, then I’d suggest taking a look at the Ada 2012 Language Reference Manual and/or the Annotated Ada 2012 Language Reference Manual.

Thanks to Marc C. for bringing this to my attention.

Ada 2012 explained by John Barnes

As you can see from this chart, Ada 2012 is going to bring a lot of new stuff to the table. Sure, it’s not as big a step as going from 83 to 95, but there are still some pretty significant things going on.

Luckily the ever industrious John Barnes has stepped up and written a Brief introduction to Ada 2012, courtesy of AdaCore.

The reader might feel that the changes are quite extensive but each has an important role to play in making Ada more useful. Indeed the solution of one problem often leads to auxiliary requirements. The desire to introduce stronger description of contracts led to the search for good syntax which led to aspect specifications. And these strengthened the need for more flexible forms of expressions and so on. Other changes were driven by outside considerations such as the multiprocessors and others stem from what now seem to be obvious but minor flaws in Ada 2005.

As with anything else by written by John Barnes, this brief introduction to Ada 2012 is a joy to read. If you’re interested in the new features of Ada 2012, then this is probably the best place to start.

GNAT GPL 2011 is out!

And so is the 2011 versions of all the software, such as AWS, XML/Ada and GNATcoll.

GNAT GPL 2011 is, as you probably know, the GPL edition of the AdaCore Ada compiler. The package comes complete with compiler and the excellent Ada/C/C++ IDE GNAT Programming Studio.

Obviously there are a lot of changes and new stuff in the 2011 GNAT GPL package, but some of the most notable are:

  • Improved support for Ada 2012
  • More flexible and more efficient project manager tool
  • Support for unloading Ada plug-ins
  • Improved support for Ada constructs on the .NET platform
  • More detailed exception messages (-gnateE switch)
  • complete support for Lego MINDSTORMS hardware, including audio and I2C sensors

I’m especially interested in the new Ada 2012 features and in the -gnateE switch. I could do with some more information in my exceptions.

I haven’t tried any of the new stuff yet, as download speeds are currently abysmal. GNAT GPL is downloading at 18.8kb/s so it’ll be a while yet before I can get my hands on the shiny new stuff.