[swift-evolution] [Review] SE-0104: Protocol-oriented integers

Dave Abrahams dabrahams at apple.com
Mon Jun 27 18:21:11 CDT 2016


on Fri Jun 24 2016, plx <swift-evolution at swift.org> wrote:

> +1 but with a few reservations.
>
> # Arithmetic
>
> What *are* the expected semantics of the operators? It seems like you
> can’t assume much about a generic `Arithmetic` type, e.g. in a generic
> context I can’t reliably assume even things like these:
>
> - `(x / y) * y == x` (or even “is close to" x) 
> - `(x + x + … + x) / n ==  x` (for `x` added together `n` times)
> - `((x + y) + z) == (x + (y + z))` (etc.)
> - `(x + y) - y == x`(? I think...)
>
> …and so on; am I missing something? 
>
> If `Arithmetic` is as lacking in semantics as it seems, then it feels
> like one of those “bag of syntax” protocols that keep getting
> mentioned as against the stdlib philosophy.

Well, this is a special case for two reasons:

1. Floating point is weird.  Technically, it doesn't obey any
   mathematical laws understandable by people whose name is not Steve
   Canon.  However, approximately nobody programs as though that's the
   case, because it would be impossible.  We program as though floating
   point types are idealized real numbers and then we deal with the fact
   that they're only a pretty good approximation as an afterthought at
   best.  Within the limits of that approximation, floating point fits
   into a set of laws governing Arithmetic... that we admittedly have
   not written down, but should.

2. The proper semantic breakdown of protocols in this area is too
   complex for most people to handle (you can find some figures in
   http://www.cs.indiana.edu/pub/techreports/TR638.pdf), and we're not
   sure yet which subset of these belongs in the standard library.


-- 
Dave



More information about the swift-evolution mailing list