comp.lang.ada
 help / color / mirror / Atom feed
From: dmitry@elros.cbb-automation.de (Dmitry A. Kazakov)
Subject: Re: Dimensionality Checking (Ada 20XX)
Date: Mon, 17 Dec 2001 12:31:29 GMT
Date: 2001-12-17T12:31:29+00:00	[thread overview]
Message-ID: <3c1dd328.10829203@News.CIS.DFN.DE> (raw)
In-Reply-To: mailman.1008400262.31005.comp.lang.ada@ada.eu.org

On Sat, 15 Dec 2001 02:07:41 -0500, "Steven Deller"
<deller@smsail.com> wrote:

>What is wrong with using Pat Roger's dimensioning system.  It has units
>and scaling.  It allows making metric, English, or other dimensional
>units, with all "interconversions" straightforward and easy.  For
>example, the following user code works with dimension checking and
>appropriate scaling conversions:
>
>==================================================================
>-- (C) Copyright 1998 by Patrick Rogers of Software Arts & Sciences
>-- progers@classwide.com
>-- http://www.classwide.com
>--
>-- Rights to use, distribute or modify this package in any way is hereby
>-- granted, provided this header is kept unchanged in all versions.
>-- Additionl headers may be added. If you make a valuable addition,
>-- please keep us informed by sending a message to progers@classwide.com
>--
>
>-- THIS ADA LIBRARY IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
>IMPLIED
>-- WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
>-- MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
>
>with Float_Units;
>with Metric_Float_Units;
>with Ada.Text_IO;
>
>procedure Test_Metric is
>  use Float_Units, Metric_Float_Units;
>
>  M : Mass := 1.0 * Kilogram;
>  E : Erg := M * C**2;
>begin
>  Ada.Text_IO.Put( Float'Image( Value_of(E) ) );
>end Test_Metric;
>================================================================
>
>This seems very powerful, easy to use, and readable.  Mass is just a
>subtype of Unit with fixed discriminants.  Kilogram is just a Mass with
>a specific magnitude.  
>
>"Unit"s are discriminated records with a single float magnitude.  The
>"Dimensioned Units" is a generic package (generic with respect to the
>magnitude type) and defines all *operations* that units can participate
>in (add, multiply, exponent to an integer, and so on).  The "system" of
>units (e.g. MKS or CGS or English or ...) is independently defined as a
>generic that takes a package that is some instance of the units package.
>It is in such a "system" where units are defined.
>
>The whole thing looks like the PERFECT abstraction of the issues and has
>appropriate separation of concerns. 
>
>As many people have pointed out, the dimensions in the discriminants (of
>the implementation) are constants and the intermixings involve simple
>integer operations, so a smart compiler could collapse all discriminant
>operations and do all dimension validation during compilation (e.g.
>WARNING: This operation will always raise Dimension_Exception).  
>
>A compiler could even do away with any explicit storage of the
>discriminants because they are *always* "constant" (when constants are
>collapsed) so storing them is just redundant.  Not that any compilers do
>so yet ...  but it seems as easy to do as implementing some "units
>checking scheme yet to be invented" and the optimizations would have a
>more general applicability.
>
>Not sure where to pick up Pat Roger's stuff on the net. If you can't
>find it and want it, let me know.  
>
>Of course, for 7 dimensions, you will have 7 discriminants (all
>constants of some integer type), but it seems to me that such
>information would be needed in any case.  I'd guess that storing
>dimensions as 8-bit integers would be more than enough -- I can't think
>of any sensible unit's system that needed more than about plus or minus
>4 or so multiples of a given dimension.  A 16-byte record could hold up
>to 8 dimension discriminants, along with a high-precision float.  Of
>course all these should be generic.

I used a similar approach. The difference was that I used only one
discriminant - a modular number holding all 7 powers. With
Interfaces.Unsigned_32 it holds powers -8..7. Then I used an
additional field for the offset [to cope with damned Celsius degree
(:-))]

>The only difficulties I see are getting the RIGHT system of dimensions
>for such a package (but then again, not being "built in", it can be
>changed is so needed).

The approach is thinkable and it has a great advantage - an ability to
write "class-wide" subroutines for dimensioned types. But it still
faces several problems.

1. Optimization issues. I saw no compiler able to remove run-time
checks and storage for the dicriminants for constrained subtypes. [I
wrote a small test program which calculated performance penalty. It is
6..20 times when compared with regular float]. I agree with you that
such kind of optimizations could be even more useful than dimensioned
values itself: some sort of user-defined compile-time expressions,
removal of static discriminants as well as tags (the later one might
require revison of tagged types, because of redispatch).

2. Scaled units like Celsius require additional fields which cannot be
made discriminants because the offset is not of a discrete type and an
access type would make the whole type limited.

3. There are still difficulties with different unit systems. One
cannot calculate everything in SI because of precission losses by
conversions (ft vs. m) and range problems (eV vs. J). One could
relatively easily produce another unit system (as you said, with
generics), but then there would be no predefined cross conversions
between it and SI. [ In fact a necessity to use generics always
indicate a language problem (:-)) ] Here we go again.

So I believe, that some sort of compiler support is required.

Regards,
Dmitry Kazakov



  reply	other threads:[~2001-12-17 12:31 UTC|newest]

Thread overview: 78+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-12-07  0:09 Dimensionality Checking (Ada 20XX) Snodgrass, Britt (NM75)
2001-12-07 16:15 ` Ian
2001-12-09 17:58   ` Nick Roberts
2001-12-09 22:58     ` Nick Roberts
2001-12-10  0:17     ` Mark Lundquist
2001-12-10  1:51       ` James Rogers
2001-12-10  3:33         ` Nick Roberts
2001-12-10 19:09           ` Nick Roberts
2001-12-11  8:20             ` Thomas Koenig
2001-12-11 15:37               ` Nick Roberts
2001-12-11 20:18                 ` Thomas Koenig
2001-12-12  0:58                   ` Mark Lundquist
2001-12-12  8:19                     ` Wilhelm Spickermann
2001-12-12 14:21                     ` Stephen Leake
2001-12-12 19:10                       ` Nick Roberts
2001-12-13 19:04                         ` Stephen Leake
2001-12-13 22:56                           ` Nick Roberts
2001-12-14  0:11                             ` Nick Roberts
2001-12-14 22:14                       ` Mark Lundquist
2001-12-15  1:30                         ` Nick Roberts
2001-12-10 20:22         ` Thomas Koenig
2001-12-10 17:21       ` Wes Groleau
2001-12-10 19:51         ` Mark Lundquist
2001-12-10 19:56           ` Wes Groleau
2001-12-10 20:37             ` Mark Lundquist
2001-12-10 18:56       ` Nick Roberts
2001-12-11 15:05         ` Wes Groleau
2001-12-11 16:39         ` Stephen Leake
2001-12-11 19:05           ` Nick Roberts
2001-12-11 22:50             ` Mark Johnson
2001-12-12  1:59               ` Nick Roberts
2001-12-11 23:01             ` Stephen Leake
2001-12-12  2:21               ` Nick Roberts
2001-12-12 14:16                 ` Stephen Leake
2001-12-13 19:52                   ` Nick Roberts
2001-12-13 22:22                     ` Nick Roberts
2001-12-14  6:40                       ` Robert C. Leif, Ph.D.
2001-12-14 17:30                       ` Stephen Leake
2001-12-14 17:38                     ` Stephen Leake
2001-12-11 22:45           ` Mark Lundquist
2001-12-12  1:42             ` Nick Roberts
2001-12-12 15:17               ` Mark Lundquist
2001-12-12 14:03             ` Stephen Leake
2001-12-12  9:35           ` Dmitry A. Kazakov
2001-12-12 14:26             ` Stephen Leake
2001-12-13 17:02               ` daniele andreatta
2001-12-13 19:06                 ` Stephen Leake
2001-12-14 10:16                 ` Dmitry A. Kazakov
2001-12-14 22:01                   ` Nick Roberts
2001-12-17 11:10                     ` Dmitry A. Kazakov
2001-12-17 12:16                       ` Thomas Koenig
2001-12-17 14:30                         ` Dmitry A. Kazakov
2001-12-27 17:18                         ` Steven Deller
2001-12-15  7:07                   ` Steven Deller
2001-12-17 12:31                     ` Dmitry A. Kazakov [this message]
2001-12-17 13:46                       ` Thomas Koenig
2001-12-17 15:00                         ` Dmitry A. Kazakov
2001-12-17 16:38                         ` Thomas Koenig
2001-12-17 21:07                       ` Britt Snodgrass
2001-12-20 13:44                         ` Dmitry A. Kazakov
2001-12-13 19:33         ` Mark Lundquist
2001-12-13 22:15           ` Nick Roberts
2001-12-14 20:20             ` Mark Lundquist
2001-12-10 23:31       ` Mark Lundquist
2001-12-10 13:57     ` Ian
2001-12-10 17:24       ` Wes Groleau
2001-12-10 20:38       ` Britt Snodgrass
  -- strict thread matches above, loose matches on Subject: below --
2001-12-11 13:11 Mike Brenner
2001-12-11 17:03 ` Mark Lundquist
2001-12-02 16:01 Another Idea for Ada 20XX James Rogers
2001-12-03 14:56 ` Mark Lundquist
2001-12-03 15:12   ` Lutz Donnerhacke
2001-12-03 21:13     ` Dimensionality Checking (Ada 20XX) Nick Roberts
2001-12-04 14:00       ` Dmitry A. Kazakov
2001-12-06 19:52         ` Britt Snodgrass
2001-12-06 20:55           ` Mark Lundquist
2001-12-06 22:38           ` Wes Groleau
2001-12-06 23:12             ` Mark Lundquist
2001-12-07 14:36               ` Wes Groleau
2001-12-07  9:37           ` Dmitry A. Kazakov
2001-12-07 22:51           ` Mark Lundquist
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox