[Grace-core] Numbers and structural typing

Andrew P. Black black at cs.pdx.edu
Thu Oct 16 23:16:25 PDT 2014


On 16 Oct 2014, at 21:16, Marco Servetto <marco.servetto at gmail.com> wrote:

> I realized the other days that numbers and structural typing does not
> necessarelly works well..
> In a structurally typed language I should be able to pass to a method
> every object that have the right methods with the right behaviour.
> 
> So, what is the right structural shape for an object to pass to 1+__
> in order to get 2?

If you want to do Peano Arithmetic, then the answer is: `pred` and `isZero`.
Theoretically, that’s all that numbers need to implement.

Of course, this is unrealistic.  In practice, we want numbers to have an operation `machineNumber`.
We implement arithmetic by requesting `machineNumber` from the argument and using 
some (non-language defined) way of getting to the machine instructions that do arithmetic.
This is more or less what Smalltalk implementations do; there is an efficient runtime check
on whether the argument can or cannot produce a machine integer, and if it can, it is
used in a machine instruction.  (If it can’t, then Smalltalk code handles it; this 
is used, for example, to implement  complex and bignums.)

In Emerald, we didn’t want to pay the cost of this runtime check.  So, while the rest of the type system was
structural, Integers were (essentially) branded:  it was impossible for the programmer to re-implement
integers.  This meant that the static type system could guarantee that both arguments to + were indeed
machine integers.  Branding was, I think, a reasonable trade-off to make in 1983, on 1 Mip machines.   
I don’t think that it’s reasonable any longer.

I didn’t see Kim’s answer; maybe he didn’t reply to all?

	Andrew







More information about the Grace-core mailing list