Handling Physical Dimensions in Ada

Christoph Karl Walter Grein

Contents

There has always been a demand to be able to compute with physical items where dimensional correctness is checked. So I searched the web for such methods and presented a page where four such methods were compared. This finally gave rise to a paper by Dmitry A. Kazakov, Fraser Wilson and me presented at the Ada Europe Conference 2003 in Toulouse:

Ada Europe Conference 2003

Download here as a zipped file holding the paper in PDF and the MS PowerPoint presentation:

Christoph Grein, Dmitry A. Kazakov, Fraser Wilson
A Survey of Physical Unit Handling Techniques in Ada

14.05.2004  Erratum added

The conference proceedings have appeared at Springer-Verlag:

© Springer-Verlag
Jean-Pierre Rosen, Alfred Strohmeier (Eds)
Reliable Software Technologies - Ada-Europe 2003
8th Ada-Europe International Conference on Reliable Software Technologies
Toulouse, France, June 16-20, 2003
Proceedings
Lecture Notes in Computer Science, Vol. 2655, Springer-Verlag, 2003

The proceedings are also available at Springer-Verlag's web site, but you need to be a subscriber in order to read the full texts:
     http://link.springer.de/link/service/series/0558/tocs/t2655.htm
or
     http://link.springer-ny.com/link/service/series/0558/tocs/t2655.htm

Ada 95 Issue 324

There exists also the Ada Issue 324, an Ada0Y proposal by the Ada Rapporteur Group for handling physical dimensions. Unfortunately I could not include my view on it in the conference proceedings because the final submission date for papers had just expired when the ARG proposal was published. So a very short outline thereof is included only in the PowerPoint presentation. The proposal exhibits a very clever use of generics and generic package parameters, so it is worth reading just for the expressive power of Ada.

However because of the tremendous number of instantiations needed, it is yet another demonstration that Ada is not suited to doing unit checking during compile time. It is therefore no longer pursued by the ARG.

You can find details directly in the Ada Issues Database of the Ada Conformity Assessment Authority.


Two of the methods presented in Toulouse can also be found here.

From the Big Bang to the Universe

This paper presents a method which allows dealing with physical quantities without operator overloading as far as possible while keeping the advantages of strong typing in critical cases. Basic mathematical functions like the square root and the sine in degrees and radians are included as predefined operations of the numeric base types.

The German version (last editorial update 23rd November 2004, a ready to use version was added) of this paper has originally been published in Ada Aktuell 1.1 (March 1993), the deceased periodical of Ada Germany.

The English version (last editorial update 23rd November 2004) is the translation of a still older German version and hence not as elaborate.

The method presented has successfully been in use for many years in several avionics projects under hard real-time conditions.

SI Units Checked and Unchecked

This paper (brand new design of the software 5th August 2020) presents a method which handles physical dimensions completely checked in full generality by allowing arbitrary rational powers of items. It is based on the seven SI units and includes any standard units like Newton (N) or Joule (J) derived from them as well as all prefixes like kilo and milli. Other non-coherent units like foot or nautical miles can easily be added. Basic mathematical functions like exp and sine are also available.

A further feature is dimensional Text_IO. In facct, numeric values are formatted like with Ada.Text_IO.Float_IO; the unit will be added in a default format, but is user selectable with the string parameter Dim.

   declare
     D: Length := 15.0E+3*"m";  -- 15.0E+3*"M"  will raise exception (wrong casing)
     T: Time   := 60.0*"min";   -- 60.0*"S"     will raise exception (wrong unit Siemens)
   begin
     Put (D/T);                 -- output as 4.17*m*s**(-1) (default format)
     Put (D/T, Dim => "km/h");  --          15.0*km/h
   end;

By changing only a few lines of code, dimension checking can be switched off in the released code (however big it may be), thus reducing the overhead of dimension checking to null.

This work has originally been published in Softwaretechnik-Trends Vol. 22.4 (November 2002), the periodical of Ada-Deutschland, special interest group 2.1.5 Ada within Gesellschaft für Informatik, the German Informatics Society.


Ada 2012: Physical units with GNAT (GPL 2013)

The new Ada 2012 aspect clauses have been appropriated by AdaCore to enhance their GNAT compiler with the ability to deal with physical units during compile-time. The notation is very natural and much resembles the one of the paper above, SI Units Checked and Unchecked.

See here a paper titled Physical units with GNAT (in PDF format), which presents a critical review of the achievements and shortcomings of this method as of GNAT GPL 2013, which has appeared in the Ada User Journal 35.1, March 2014.

This ingenious method could evolve to finally be included in the next Ada standard, whenever this might be.