comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adambeneschan@gmail.com>
Subject: Re: Integers and Mathematical Correctness
Date: Sat, 27 Sep 2014 11:54:32 -0700 (PDT)
Date: 2014-09-27T11:54:32-07:00	[thread overview]
Message-ID: <e5ed51a5-7689-4638-a72f-d069773e40df@googlegroups.com> (raw)
In-Reply-To: <c8o75iFobkU1@mid.individual.net>

On Saturday, September 27, 2014 9:32:52 AM UTC-7, Niklas Holsti wrote:
> On 14-09-27 12:01 , AdaMagica wrote:
> 
> > Adam,
> > Na, you forget
> > A: Rational := 1/3+1/4;
> > This will be 0 for your function.
> 
> That seems not to be the case. With the definition
> 
>    type Rational is record
>       Numerator, Denominator : Integer;
>    end record;
> 
> and the obvious definitions of "+", "/", and Image for Rational, the
> program fragment
> 
>    R : Rational := 1/4 + 3/2;
> begin
>    Put_Line (Image (R));
> 
> results in the output
> 
>  7 / 2

Well, it should be 7/4 as Jeff pointed out, but yes, that's what I'd expect as long as there are no versions of "+" that take one Integer and one Rational parameter and return Rational.  If there is a visible "+" like that, the program would be rejected as ambiguous.  But 0 (or 0/1 or any Rational number representing 0) is not a possible result.

Christoph did point out that having those versions of "+" is important.  Defining another integer type to use as parameters to those functions is one way of eliminating ambiguity--I assume that this type would use "is abstract" to nullify all of the standard operator declarations that this type definition would create.  

    function "+" (Left, Right : Whole_Number) return Whole_Number is abstract;
    etc.

But it's not the only way; you can manage without defining a new integer type.  If 

    R : Rational := 1/4 + 3/2

is ambiguous, you can disambiguate like this:

    R : Rational := Rational'(1/4) + Rational'(3/2);

which forces the compiler to use the versions of "/" that have a Rational result, and ignore the declarations in Standard that return Integer.  I think there's a tradeoff.  Having to add this in some places could be inconvenient.  But the solution of defining another integer type is probably going to require some type conversions to and from that integer type (since you would not be able to use other arithmetic operations on that type), which could also be inconvenient.  I don't know which is the better solution.

Finally, I want to reiterate the point I made earlier: when it comes to overload resolution, the *location* where the operators are declared (in a USE'd package, in Standard, in the local scope or an enclosing scope or a parent package) is not relevant.  It's relevant when you have two or more declarations that have the same name, AND the same parameters AND the same result type.  When that happens, the location is used to determine which declaration takes precedence over all other declarations THAT HAVE THE SAME PARAMETER AND RESULT TYPES.  Once that's done, there is at most one of each operator with the same parameter/result types, but there can still be more than one that have different parameter and/or result types.  Deciding which of those is used is called overload resolution or name resolution, and the locations where the operators are declared has no effect on overload resolution.

                                -- Adam





  parent reply	other threads:[~2014-09-27 18:54 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-10-31 20:27 Integers and Mathematical Correctness chris.danx
2001-10-31 21:21 ` David C. Hoos
2001-10-31 22:16   ` chris.danx
2001-10-31 22:47     ` David C. Hoos
2001-10-31 22:55       ` chris.danx
2001-10-31 23:16         ` Matthew Heaney
2001-10-31 21:42 ` Mark Johnson
2001-11-01 18:57   ` Mark Johnson
2001-11-01 14:32 ` Wes Groleau
2001-11-01 16:18   ` wilhelm.spickermann
2001-11-01 16:48   ` chris.danx
2001-11-01 15:45 ` Charles Sampson
2001-11-01 16:20   ` Marin David Condic
2001-11-03 17:02     ` Richard Riehle
2001-11-05 14:47       ` Marin David Condic
2001-11-06  3:53         ` Eric G. Miller
2001-11-06  4:28           ` James Rogers
2001-11-06  6:06             ` peter
2001-11-06 14:48               ` James Rogers
2001-11-06 15:54                 ` Marin David Condic
2001-11-07  3:44             ` Eric G. Miller
2001-11-01 17:10   ` chris.danx
2001-11-01 17:52     ` Chad Robert Meiners
2001-11-01 19:02       ` chris.danx
2001-11-01 17:57     ` Wes Groleau
2001-11-03 14:57     ` Charles Sampson
2001-11-01 16:11 ` Charles Lindsey
2001-11-01 18:40   ` Wilhelm Spickermann
2001-11-01 19:18   ` chris.danx
2001-11-02  1:37     ` Steven Deller
2014-09-26  9:07       ` vincent.diemunsch
2014-09-26 16:38         ` Niklas Holsti
2014-09-26 16:58           ` AdaMagica
2014-09-26 17:51             ` Adam Beneschan
2014-09-27  9:01               ` AdaMagica
2014-09-27 10:15                 ` AdaMagica
2014-09-27 16:32                 ` Niklas Holsti
2014-09-27 16:49                   ` Jeffrey Carter
2014-09-27 18:52                     ` Niklas Holsti
2014-09-27 18:54                   ` Adam Beneschan [this message]
2014-09-27 19:07                     ` Adam Beneschan
     [not found]                 ` <3489504a-f82b-4fec-8a6c-7cb91854dd1e@googlegroups.com>
2014-09-27 19:21                   ` AdaMagica
2014-09-27 11:44           ` gautier_niouzes
2014-09-26 16:41         ` Adam Beneschan
2014-09-26 16:46         ` Adam Beneschan
2014-09-27 15:21           ` vincent.diemunsch
     [not found]             ` <34da5a39-9fa3-4e8e-a3f9-98f61a4ebcc7@googlegroups.com>
2014-09-28  7:47               ` Dmitry A. Kazakov
2014-09-29 14:58                 ` Adam Beneschan
2014-09-29 16:25                   ` Dmitry A. Kazakov
2014-10-01 19:48                   ` vincent.diemunsch
2014-10-02 11:10                     ` G.B.
2001-11-01 18:08 ` Tucker Taft
2001-11-01 18:54 ` David Starner
2001-11-01 21:44   ` Wilhelm Spickermann
2001-11-02 12:52 ` chris.danx
  -- strict thread matches above, loose matches on Subject: below --
2001-10-31 22:42 Beard, Frank
replies disabled

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