comp.lang.ada
 help / color / mirror / Atom feed
From: John G. Volan <John_Volan@ccmail.dayton.saic.com>
Subject: Re: Side-effect arithmetic again [was: Ada ... in embedded systems]
Date: 1996/03/20
Date: 1996-03-20T00:00:00+00:00	[thread overview]
Message-ID: <4iq2ia$lk7@dayuc.dayton.saic.com> (raw)
In-Reply-To: EACHUS.96Mar19195517@spectre.mitre.org

In article <EACHUS.96Mar19195517@spectre.mitre.org> 
Robert I. Eachus, eachus@spectre.mitre.org writes:

>I may accept John Volan's challenge and post a proposed package tomorrow. 

I wasn't throwing down a gauntlet or anything, just throwing out an idea 
for consideration ... :-)

>(The issue I want to think about overnight
>is whether the generic formal should be "is range <>" or "is (<>)".

No need to think about it overnight, _both_ can be done fairly trivially: 

    ------------------------------------------------------------------------
	generic
	    type Item is (<>);
	package Discrete_Arithmetic is
	
	    procedure Increment (This : in out Item);
	    pragma Inline (Increment);
	    
	    procedure Decrement (This : in out Item);
	    pragma Inline (Decrement);
	    
	end Discrete_Arithmetic;
    ------------------------------------------------------------------------
    package body Discrete_Arithmetic is
	
	    procedure Increment (This : in out Item) is
	    begin
	        This := Item'Succ (This);
	    end Increment;
	    
	    procedure Decrement (This : in out Item) is
	    begin
	        This := Item'Pred (This);
	    end Decrement;
	    
	end Discrete_Arithmetic;
    ------------------------------------------------------------------------
	generic
	    type Item is range <>;
	package Integer_Arithmetic is
	
	    procedure Increment (This : in out Item);
	    pragma Inline (Increment);
	    
	    procedure Decrement (This : in out Item);
	    pragma Inline (Decrement);
	    
	    procedure Increment (This : in out Item; By : in Item);
	    pragma Inline (Increment);
	    
	    procedure Decrement (This : in out Item; By : in Item);
	    pragma Inline (Decrement);
	    
	    procedure Multiply (This : in out Item; By : in Item);
	    pragma Inline (Increment);
	    
	    procedure Divide (This : in out Item; By : in Item);
	    pragma Inline (Decrement);
	    
	    procedure Modulo (This : in out Item; By : in Item);
	    pragma Inline (Increment);
	    
	    procedure Remainder (This : in out Item; By : in Item);
	    pragma Inline (Decrement);
	    
	end Integer_Arithmetic;
    ------------------------------------------------------------------------
	package body Integer_Arithmetic is
	
	    procedure Increment (This : in out Item) is
	    begin
	        This := Item'Succ (This);
	    end Increment;
	    
	    procedure Decrement (This : in out Item) is
	    begin
	        This := Item'Pred (This);
	    end Decrement;
	    
	    procedure Increment (This : in out Item; By : in Item) is
	    begin
	        This := This + By;
	    end Increment;
	    
	    procedure Decrement (This : in out Item; By : in Item) is
	    begin
	        This := This - By;
	    end Decrement;
	    
	    procedure Multiply (This : in out Item; By : in Item) is
	    begin
	        This := This * By;
	    end Multiply;
	    
	    procedure Divide (This : in out Item; By : in Item) is
	    begin
	        This := This / By;
	    end Divide;
	    
	    procedure Modulo (This : in out Item; By : in Item) is
	    begin
	        This := This mod By;
	    end Modulo;
	    
	    procedure Remainder (This : in out Item; By : in Item) is
	    begin
	        This := This rem By;
	    end Remainder;
	    
	end Integer_Arithmetic;
    ------------------------------------------------------------------------
	generic
	    type Item is mod <>;
	package Modular_Arithmetic is
	
	    procedure Increment (This : in out Item);
	    pragma Inline (Increment);
	    
	    procedure Decrement (This : in out Item);
	    pragma Inline (Decrement);
	    
	    procedure Increment (This : in out Item; By : in Item);
	    pragma Inline (Increment);
	    
	    procedure Decrement (This : in out Item; By : in Item);
	    pragma Inline (Decrement);
	    
	    procedure Multiply (This : in out Item; By : in Item);
	    pragma Inline (Increment);
	    
	    procedure Divide (This : in out Item; By : in Item);
	    pragma Inline (Decrement);
	    
	    procedure Modulo (This : in out Item; By : in Item);
	    pragma Inline (Increment);
	    
	    procedure Remainder (This : in out Item; By : in Item);
	    pragma Inline (Decrement);
	    
	end Modular_Arithmetic;
    ------------------------------------------------------------------------
	package body Modular_Arithmetic is
	
	    procedure Increment (This : in out Item) is
	    begin
	        This := Item'Succ (This);
	    end Increment;
	    
	    procedure Decrement (This : in out Item) is
	    begin
	        This := Item'Pred (This);
	    end Decrement;
	    
	    procedure Increment (This : in out Item; By : in Item) is
	    begin
	        This := This + By;
	    end Increment;
	    
	    procedure Decrement (This : in out Item; By : in Item) is
	    begin
	        This := This - By;
	    end Decrement;
	    
	    procedure Multiply (This : in out Item; By : in Item) is
	    begin
	        This := This * By;
	    end Multiply;
	    
	    procedure Divide (This : in out Item; By : in Item) is
	    begin
	        This := This / By;
	    end Divide;
	    
	    procedure Modulo (This : in out Item; By : in Item) is
	    begin
	        This := This mod By;
	    end Modulo;
	    
	    procedure Remainder (This : in out Item; By : in Item) is
	    begin
	        This := This rem By;
	    end Remainder;
	    
	end Modular_Arithmetic;
    ------------------------------------------------------------------------
	generic
	    type Item is digits <>;
	package Floating_Arithmetic is
	
	    procedure Increment (This : in out Item);
	    pragma Inline (Increment);
	    
	    procedure Decrement (This : in out Item);
	    pragma Inline (Decrement);
	    
	    procedure Increment (This : in out Item; By : in Item);
	    pragma Inline (Increment);
	    
	    procedure Decrement (This : in out Item; By : in Item);
	    pragma Inline (Decrement);
	    
	    procedure Multiply (This : in out Item; By : in Item);
	    pragma Inline (Increment);
	    
	    procedure Divide (This : in out Item; By : in Item);
	    pragma Inline (Decrement);
	    
	end Floating_Arithmetic;
    ------------------------------------------------------------------------
	package body Floating_Arithmetic is
	
	    procedure Increment (This : in out Item) is
	    begin
	        This := Item'Succ (This);
	    end Increment;
	    
	    procedure Decrement (This : in out Item) is
	    begin
	        This := Item'Pred (This);
	    end Decrement;
	    
	    procedure Increment (This : in out Item; By : in Item) is
	    begin
	        This := This + By;
	    end Increment;
	    
	    procedure Decrement (This : in out Item; By : in Item) is
	    begin
	        This := This - By;
	    end Decrement;
	    
	    procedure Multiply (This : in out Item; By : in Item) is
	    begin
	        This := This * By;
	    end Multiply;
	    
	    procedure Divide (This : in out Item; By : in Item) is
	    begin
	        This := This / By;
	    end Divide;
	    
	end Floating_Arithmetic;
    ------------------------------------------------------------------------
	generic
	    type Item is delta <>;
	package Fixed_Arithmetic is
	
	    procedure Increment (This : in out Item);
	    pragma Inline (Increment);
	    
	    procedure Decrement (This : in out Item);
	    pragma Inline (Decrement);
	    
	    procedure Increment (This : in out Item; By : in Item);
	    pragma Inline (Increment);
	    
	    procedure Decrement (This : in out Item; By : in Item);
	    pragma Inline (Decrement);
	    
	    procedure Multiply (This : in out Item; By : in Item);
	    pragma Inline (Increment);
	    
	    procedure Divide (This : in out Item; By : in Item);
	    pragma Inline (Decrement);
	    
	end Fixed_Arithmetic;
    ------------------------------------------------------------------------
	package body Fixed_Arithmetic is
	
	    procedure Increment (This : in out Item) is
	    begin
	        This := Item'Succ (This);
	    end Increment;
	    
	    procedure Decrement (This : in out Item) is
	    begin
	        This := Item'Pred (This);
	    end Decrement;
	    
	    procedure Increment (This : in out Item; By : in Item) is
	    begin
	        This := This + By;
	    end Increment;
	    
	    procedure Decrement (This : in out Item; By : in Item) is
	    begin
	        This := This - By;
	    end Decrement;
	    
	    procedure Multiply (This : in out Item; By : in Item) is
	    begin
	        This := This * By;
	    end Multiply;
	    
	    procedure Divide (This : in out Item; By : in Item) is
	    begin
	        This := This / By;
	    end Divide;
	    
	end Fixed_Arithmetic;
    ------------------------------------------------------------------------

That only took me about 10 minutes of my lunch break to write.  Brought to
you by the miracle of cut and paste! ;-)

>... But I don't think it needs to be standardized.

The trouble is, anybody and his brother could do the same thing I just
did, but slightly differently one way, or slightly differently another
way ... eventually, the world is going to abound with multiple versions
of this idea (that is, _if_ folks really think this is worth doing, but
that's another issue).  Anybody and his brother could have implemented
dynamic unbounded strings and complex arithmetic and transcendental
functions and so on in Ada83 (and they did!), but there's value in
having a standardized way of doing those things now in Ada95.  So maybe
my packages above could have been Ada.Numerics.Discrete_Arithmetic,
Ada.Numerics.Integer_Arithmetic, and so on ...

>    function Inc(Value: in Formal_Type) return Formal_Type;
>    function Inc(Value: in Formal_Type; By: in Integer) return Formal_Type;
...
>    (Should the function form be named something other than Inc and
>Dec?  

Yes, they should be named 'Succ and 'Pred, and "+" and "-". :-)  Really,
all we're talking about is providing Ada95 procedure equivalents for
C's arithmetic-with-side-effect operators: ++ -- += -= *= /=.  No need
to come up with a substitute for operations Ada95 already has.  But if
you must, why not just do renamings?

    type Item is range <>;
    ...
    function Successor_Of
        (This : in Item) return Item renames Item'Succ;
    function Predecessor_Of
        (This : in Item) return Item renames Item'Pred;
    function Sum_Of
        (This, Plus_That  : in Item) return Item renames "+";
    function Difference_Of
        (This, Minus_That : in Item) return Item renames "-";
    function Product_Of
        (This, Times_That : in Item) return Item renames "*";
    function Quotient_Of
        (This, Over_That  : in Item) return Item renames "/";
    function Remainder_Of
        (This, Over_That  : in Item) return Item renames "rem";
    function Modulo_Of
        (This, Over_That  : in Item) return Item renames "mod";

But that seems a little extreme, don't you think? :-)
   
>Does the Ada domain specific knowledge that the parameters of
>a function are not changed dominate here, or should it be named Next?)

My instinct is to use nouns for function names rather than verbs, as you
can see above.  It emphasizes the fact that the point of a function is
the result it returns, and not some naughty side-effect that it shouldn't
be doing anyway. :-)  (But let's not start up _that_ thread again... :-)

------------------------------------------------------------------------
Internet.Usenet.Put_Signature
( Name => "John G. Volan", E_Mail => "John_Volan@dayton.saic.com",
  Favorite_Slogan => "Ada95: The *FIRST* International-Standard OOPL",
  Humorous_Disclaimer => "These opinions are undefined by SAIC, so" &
    "any use would be erroneous ... or is that a bounded error now?" );
------------------------------------------------------------------------




  reply	other threads:[~1996-03-20  0:00 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <823906039.22113@assen.demon.co.uk>
     [not found] ` <4fgrq3$mc4@qualcomm.com>
     [not found]   ` <dewar.823962356@schonberg>
1996-02-17  0:00     ` Ada is almost useless in embedded systems Tore Joergensen
1996-02-17  0:00       ` Robert Dewar
1996-02-19  0:00       ` Keith Thompson
1996-02-19  0:00         ` John McCabe
1996-02-21  0:00           ` Richard A. O'Keefe
1996-02-21  0:00             ` Norman H. Cohen
1996-02-19  0:00 ` R.A.L Williams
1996-02-21  0:00   ` Richard A. O'Keefe
1996-02-19  0:00 ` AdaWorks
1996-02-21  0:00   ` Ken Garlington
1996-02-23  0:00     ` AdaWorks
1996-02-19  0:00 ` Jon S Anthony
     [not found] ` <824056183.18993@assen.demon.co.uk>
     [not found]   ` <311E924E.74CE@escmail.orl.mmc.com>
1996-02-17  0:00     ` Ada is great for embedded systems (was Ada is almost useless in embedded systems) Ken & Virginia Garlington
     [not found]   ` <4fnqpm$3nh@news.sanders.lockheed.com>
1996-02-19  0:00     ` Ada is almost useless in embedded systems AdaWorks
1996-02-21  0:00       ` Hugh Dunne
1996-02-21  0:00       ` Ken Garlington
     [not found]   ` <4fnp37$nj1@theopolis.orl.mmc.com>
1996-02-22  0:00     ` Alan Brain
     [not found] ` <emery-0902962215150001@line316.nwm.mindlink.net>
     [not found]   ` <DMoA85.52I@eskimo.com>
     [not found]   ` <823965654.4500@assen.demon.co.uk>
     [not found]     ` <824165619.14894@assen.demon.co.uk>
     [not found]       ` <JSA.96Feb13133713@organon.com>
     [not found]         ` <824332550.2485@assen.demon.co.uk>
1996-02-17  0:00           ` Ken & Virginia Garlington
1996-02-17  0:00             ` Robert Dewar
1996-02-18  0:00               ` John McCabe
1996-02-18  0:00                 ` Robert Dewar
1996-02-19  0:00                   ` John McCabe
     [not found]         ` <824259217.26321@assen.demon.co.uk>
1996-02-17  0:00           ` Robert Dewar
1996-02-18  0:00             ` John McCabe
1996-02-18  0:00               ` Robert Dewar
1996-02-19  0:00                 ` John McCabe
1996-02-20  0:00                   ` Robert Dewar
1996-02-21  0:00                   ` Fergus Henderson
     [not found]       ` <4fs7ml$cf1@rational.rational.com>
1996-02-26  0:00         ` Ada 83 " Alan Brain
1996-02-26  0:00 ` Ada is almost useless " R.A.L Williams
     [not found]   ` <4h3q56$1vk@goanna.cs.rmit.EDU.AU>
     [not found]     ` <dewar.825635955@schonberg>
     [not found]       ` <826571250.140@assen.demon.co.uk>
     [not found]         ` <dewar.826634800@schonberg>
1996-03-21  0:00           ` John McCabe
1996-03-23  0:00             ` Side-effect arithmetic again [was: Ada ... in embedded systems] John G. Volan
1996-03-23  0:00               ` Robert Dewar
1996-03-25  0:00                 ` Tucker Taft
1996-03-25  0:00                   ` Norman H. Cohen
1996-03-25  0:00                   ` Robert A Duff
1996-03-26  0:00               ` John G. Volan
1996-03-26  0:00                 ` Robert A Duff
1996-03-26  0:00                   ` Tore Joergensen
1996-03-27  0:00                     ` John G. Volan
1996-03-28  0:00                       ` Tucker Taft
1996-03-28  0:00                         ` Robert Dewar
1996-03-29  0:00                           ` Tucker Taft
1996-03-29  0:00                             ` Tucker Taft
1996-03-27  0:00                     ` John G. Volan
1996-03-29  0:00                       ` Robert A Duff
1996-03-30  0:00                         ` John G. Volan
1996-03-31  0:00                           ` AdaWorks
1996-04-01  0:00                           ` Robert A Duff
1996-03-30  0:00                         ` John G. Volan
1996-03-27  0:00                     ` John G. Volan
1996-03-26  0:00                 ` Robert Dewar
1996-03-29  0:00                   ` Robert I. Eachus
     [not found] ` <RALW.96Feb28100925@vulcan.gmrc.gecm.com>
     [not found]   ` <dewar.825775334@schonberg>
     [not found]     ` <RALW.96Mar8113005@vulcan.gecm.com>
     [not found]       ` <4hv2fb$6ra@cville-srv.wam.umd.edu>
     [not found]         ` <4xybp895y6.fsf@leibniz.enst-bretagne.fr>
     [not found]           ` <3144CC40.33A0@escmail.orl.mmc.com>
     [not found]             ` <dewar.826604375@schonberg>
     [not found]               ` <3145FF2C.6139@escmail.orl.mmc.com>
     [not found]                 ` <dewar.826829407@schonberg>
     [not found]                   ` <31499D21.1DA6@escmail.orl.mmc.com>
1996-03-15  0:00                     ` Bug or Limitation? (was: Ada is almost useless in embedded systems) Robert Dewar
1996-03-16  0:00                       ` Ted Dennison
1996-03-20  0:00                         ` Side-effect arithmetic again [was: Ada ... in embedded systems] Robert I. Eachus
1996-03-20  0:00                           ` John G. Volan [this message]
1996-03-22  0:00                             ` Alan Brain
1996-03-20  0:00                           ` Robert A Duff
1996-03-21  0:00                             ` Peter Hermann
1996-03-21  0:00                               ` Robert Dewar
1996-03-25  0:00                                 ` Robert I. Eachus
1996-03-28  0:00                               ` Mats Weber
1996-03-29  0:00                                 ` John G. Volan
1996-03-21  0:00                           ` Geert Bosch
1996-03-26  0:00                           ` Mats Weber
1996-03-15  0:00   ` Ada is almost useless in embedded systems Robert I. Eachus
     [not found]     ` <dirk.827148504@demokrit>
1996-03-18  0:00       ` David Weller
1996-03-18  0:00     ` Alan Brain
     [not found]       ` <4ik5bm$ogg@dayuc.dayton.saic.com>
1996-03-18  0:00         ` Side-effect arithmetic again [was: Ada ... in embedded systems] Robert Dewar
1996-03-19  0:00           ` Norman H. Cohen
1996-03-19  0:00           ` Jay Martin
1996-03-21  0:00             ` Robert I. Eachus
1996-03-16  0:00 ` Ada is almost useless in embedded systems Kevin Dalley
replies disabled

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