comp.lang.ada
 help / color / mirror / Atom feed
* Types with physical dimension
@ 1994-09-27 22:18 Paul Graham
  1994-09-28 13:59 ` Robert Dewar
  1994-10-03 17:31 ` Stephen A. Leake
  0 siblings, 2 replies; 26+ messages in thread
From: Paul Graham @ 1994-09-27 22:18 UTC (permalink / raw)



While Ada's strong typing mechanism can catch errors like this:

    d : dollars;
    f : francs;
...
    d := d + f;		-- error

it does allow such nonsensical constructs as

    d := d * d;		-- what does it mean to multiply dollars by dollars?
    d := d ** 10;	-- ??

VHDL provides physical types to model values with physical dimensions.  A
VHDL physical type lacks certain predefined multiplication operations, lacks
exponentiation, and its division operation returns a universal_integer (or
root_integer in Ada 9x terminolgy).  An example of physical types:

    type length is range integer'low to integer'high
        units inches;
            feet = 12 inches;
            yards = 3 feet;
        end units;

    type area is range integer'low to integer'high
        units inches_2;
            feet_2 = 144 inches_2;
            yards_2 = 9 feet_2;
        end units;

    function "*"(x, y : length) return area is
    begin
        return length'pos(x) * length'pos(y) * inches_2;
    end;

    function "/"(x : area; y : length) return length is
    begin
        return area'pos(x) / length'pos(y) * inches;
    end;

...
    variable L : length;
    variable A : area;
...
    L := L * 2;			-- legal
    L := L * L;			-- illegal
    A := L * L;			-- legal
    
    A := 2 inches * 2 inches;	-- legal (= 4 inches_2)

    L := 4 feet_2 / 2 inches;	-- legal (= 288 inches)

(Please excuse any typos; I haven't simulated this.)

One can model physical types in Ada by using private types, but this
involves writing a lot of operators.  What do Ada programmers do to guard
against dimensional errors?

Paul
--
Paul Graham 	graham@compass-da.com	Compass Design Automation, Inc.
(speaking only for myself)	"Cekoslovakyalilastiramadiklarimizdanmissiniz."



^ permalink raw reply	[flat|nested] 26+ messages in thread
* Re: Types with physical dimension
@ 1994-09-28 10:55 Simtel20 Transfer
  1994-09-28 18:56 ` Mark A Biggar
  1994-10-04  2:06 ` lmiller
  0 siblings, 2 replies; 26+ messages in thread
From: Simtel20 Transfer @ 1994-09-28 10:55 UTC (permalink / raw)


Paul Grham writes:
One can model physical types in Ada by using private types, but this
involves writing a lot of operators.  What do Ada programmers do to guard
against dimensional errors?

Paul
--
Paul Graham     graham@compass-da.com   Compass Design Automation, Inc.

The now renamed ABET (Ada Based Environment for Test) IEEE
standard has an Ada package that defines _many_ physical
types.  If you need pointers let me know.
p.s. It just occurred to e that maybe the SEI sghould
consider putting references to it on their home
page under programming languages.
p.p.s. Its a shame they can't put the entire standard
on-line but it is copyrighted by the IEEE (one of my
favorite gripes is that such standards are not on-line).
Now the IEEE may have changedd  their policy.  If so
will someone please correct me.
hope this helps.
sam harbaugh HARBAUGH@ROO.FIT.EDU



^ permalink raw reply	[flat|nested] 26+ messages in thread
[parent not found: <GRAHAM.94Sep27181841@canopus.clsi.com>]
* Re: Types with physical dimension
@ 1994-09-28 19:28 Paul Pukite
  0 siblings, 0 replies; 26+ messages in thread
From: Paul Pukite @ 1994-09-28 19:28 UTC (permalink / raw)
  To: graham

Paul Graham  <graham@clsi.COM> wrote:
>One can model physical types in Ada by using private types, but this
>involves writing a lot of operators.  What do Ada programmers do to guard
>against dimensional errors?

You don't necessarily need to invoke private types in Ada. By creating a 
derived type like:

   type Dollars is new INTEGER;  -- Also can put in range constraints

you cannot multiply two dollar objects in another package unless you
specifically state that you want the "*" operator used. (Ada 9X reduces 
the extra code required to "use" the "*".)

This guards against some of the dimensional errors as do the range 
constraints, but I agree that VHDL physical types are rather neat.

--
Paul Pukite (pukite@daina.com)       
DAINA Engineering 612/781-7600       
4111 Central AVE NE, Suite 212       
Minneapolis, MN  55421-2953          



^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~1994-10-13 22:15 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1994-09-27 22:18 Types with physical dimension Paul Graham
1994-09-28 13:59 ` Robert Dewar
1994-09-30  2:06   ` R_Tim_Coslet
1994-10-03 17:31 ` Stephen A. Leake
1994-10-04 11:51   ` Robert I. Eachus
1994-10-04 19:45     ` Mark A Biggar
     [not found]       ` <CxBBx8.7L@irvine.com>
1994-10-13 22:15         ` gamache
1994-10-12  3:43     ` Matt Kennel
1994-10-04 14:56   ` Robert Dewar
1994-10-05 14:53     ` Bob Gilbert
1994-10-05  8:38   ` Jean-Pierre Rosen
1994-10-05 10:35     ` Stephen J Bevan
1994-10-05 13:17       ` Jean-Pierre Rosen
1994-10-05 15:48     ` Norman H. Cohen
  -- strict thread matches above, loose matches on Subject: below --
1994-09-28 10:55 Simtel20 Transfer
1994-09-28 18:56 ` Mark A Biggar
1994-10-04  2:06 ` lmiller
     [not found] <GRAHAM.94Sep27181841@canopus.clsi.com>
1994-09-28 17:36 ` William Brennan
1994-09-28 21:41 ` Tucker Taft
1994-09-29  4:11   ` Robert Dewar
1994-09-29 11:19     ` Peter Hermann
1994-09-30 10:17       ` Dr John Stockton
1994-10-03  4:37       ` Robert Dewar
1994-09-29 13:37     ` Tucker Taft
1994-10-03  4:40       ` Robert Dewar
1994-09-28 19:28 Paul Pukite

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