Aspect Dimenision_System with a single dimension

I am playing around with GNAT’s dimensionality checking for physical quantities, which are implemented with the aspects Dimension_System and Dimension, see https://blog.adacore.com/uploads/dc.pdf.

I would like a type which models geometric quantities, i.e. angle, length and volume. For that, we need only a length dimension. However, I can’t get the following example to compile:

package Geometry is

   type Quantity is new Float with
      Dimension_System => (
         (Unit_Name => Meter, Unit_Symbol => 'm', Dim_Symbol => 'L')
   );

end Geometry;

Trying to compile this yields

geometry.ads:5:10: error: expected positional aggregate

Can anyone tell me what’s going on here? I am using GCC GNAT 14.2.0 by the way.

You have to give the parameters in the correct order (contrary to other aggregates).

See a critical review of this method’s achievements and shortcomings in my paper Physical units with GNAT, which has appeared in Ada User Journal 35.1).
http://archive.adaic.com/tools/CKWG/Dimension/Physical_units_with_GNAT_GPL_2013-AUJ35.1.pdf

As alternatives, you can try Dmitry Kazakov’s Units of measurement for Ada or
GitHub - CKWG/SI_Units-Checked-and-Unchecked: Ada packages for handling SI units of measure.

2 Likes
subtype Length is MKS_Type with
  Dimension => (Symbol => 'm',
                Meter =>1,
                others =>0);

Isn’t Unit_Name, Unit_Symbol, Dim_Symbol the correct order? That’s how it appears in all the examples I’ve seen.

Interesting paper by the way! It addresses exactly some of my own concerns with the implementation, particularily the units-as-variables issue. I see that the paper is from 2014; has there been any significant progress on the shortcomings you listed in the last eleven years?

The aspect Dimension_System seems to want more than one dimension. If you add a second dimension to the list, it compiles for me. There may be a way to give it only one dimension, but nothing I tried quickly worked.

Seems like a compiler bug then.

I’ve never used the GNAT dimension system, so I do not know whether there are any changes.

I also do not see any ambitions to use any such method of unit checking. See the poll:

Not a single answer.

Isn’t Unit_Name, Unit_Symbol, Dim_Symbol the correct order? That’s how it appears in all the examples I’ve seen.

It is, but the attribute Dimension_System seems to want more than one unit in a positional aggregate. This seems to be another deficiency in this method: Is there a syntax defined for the attribute?

From the GNAT Reference Manual:

3.11 Aspect Dimension_System

The Dimension_System aspect is used to define a system of dimensions that will be used in subsequent subtype declarations with Dimension aspects that reference this system. The syntax is:

with Dimension_System => (DIMENSION {, DIMENSION});

DIMENSION ::= ([Unit_Name   =>] IDENTIFIER,
               [Unit_Symbol =>] SYMBOL,
               [Dim_Symbol  =>] SYMBOL)

SYMBOL ::= CHARACTER_LITERAL | STRING_LITERAL

You could check the parser to see if it actually does that.

The reason I didn’t reply to it is because I haven’t used any of them for anything serious, though I had plans to for a big project in my last place of [degree-relevant] employment, ad was going to use your SI Units, Checked if possible.

Even such a short note is valuable, so please do enter this in the poll.

From The Big Bang To The Universe presents a simple method successfully in use in several helicopter avionics projects under hard real-time conditions, ready to download. This is the only such method I ever have used. Our requirements specification document was pure numeric, no units given, so no chance to verify the equations. When I asked I got the answer “This is no schoolwork…” which left me speechless!

Someone I met on a conference once told me that they used a unit system reduced to only few unit (mechanics perhaps) with a separate type for each unit. They had dozens of overloaded operations, I do not remember the number, but it was tremendous.

So I think many ponder the idea but soon find out the effect is not worth the expenditure. My poll tries to prove or to refute this.