[Grace-core] Typing of Number

Michael Homer mwh at ecs.vuw.ac.nz
Mon Jun 20 02:30:57 PDT 2011


On Mon, Jun 20, 2011 at 6:43 PM, Andrew P. Black <black at cs.pdx.edu> wrote:
>> It seems that they will all need to be coercible to Rational in order
>> for that to work, and consequently the return type of Number
>> operations is the dynamic type of the receiver
>
> We can coerce exact numbers to Rational, but not inexact numbers.  The (static) return type of an operation can depend only on the (static) types of the arguments, not on their values, or on their dynamic type, because doing otherwise means that we don't have static typing.  That is exactly why I think that Number must be the only type.

Right, that was how my first draft worked, and it's the other way to
be consistent. The rationale for making it otherwise would be that if
Rational and Binary64 aren't types then they don't achieve much -
you'd never be able to tell what you had, and with the "once inexact,
always inexact" behaviour you'd end up with "everything inexact",
since you could never tell if you'd introduced an inexact type
somewhere along the line (perhaps from an argument). When everything
is Number you have static typing, but it's a pretty degenerate case
along the lines of "everything is Object", or dynamic typing in
sheep's clothing. It might be the right thing to do if the idea is
that people shouldn't care what the implementation is, but otherwise
it gives strange properties.

> Note that correct arithmetic comes first.  If the type system won't permit correct arithmetic, then the type system needs to be fixed, not the other way around.  So no type system can ever make 2*(2+i) = 4.  Indeed, recall that the types can never affect the results of a computation; all that they can do is give advance warning that a given computation might go wrong.

For clarity, the implementation of Rational I was thinking of with
that would be like this:
  class Rational {
    method +(other : Number) -> Rational {
      self.__perform_addition(other.asRational)
    }
  }
It isn't the static typing that determines the return type, but the
dynamic type of the receiver, just like any other method. It would
work the same way without the types (and it does make 2*(2+i) == 4,
assuming an implementation of Complex.asRational). The type system is
only involved insofar as that's the implementation that typechecks without
turning everything into Number.
-Michael


More information about the Grace-core mailing list