From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,751584f55705ddb7 X-Google-Attributes: gid103376,public From: John G. Volan Subject: Re: Side-effect arithmetic again [was: Ada ... in embedded systems] Date: 1996/03/20 Message-ID: <4iq2ia$lk7@dayuc.dayton.saic.com> X-Deja-AN: 143475112 distribution: world references: <4hv2fb$6ra@cville-srv.wam.umd.edu> <4il3ce$fqa@saba.info.ucla.edu> <4in8ko$klb@watnews1.watson.ibm.com> content-type: text/plain; charset=ISO-8859-1 x-xxmessage-id: organization: Science Applications International Corp. (SAIC) mime-version: 1.0 newsgroups: comp.lang.ada Date: 1996-03-20T00:00:00+00:00 List-Id: In article 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?" ); ------------------------------------------------------------------------