[SR-13984] Numeric protocols have almost no laws #56379
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
conformances
Feature → protocol: protocol conformances
documentation
standard library
Area: Standard library umbrella
Additional Detail from JIRA
md5: e2f203ba095374993098f4b917691791
Issue Description:
I was trying to write tests for semantic conformance to the AdditiveArithmetic protocol along the lines of these when I realized we have almost no semantic constraints on conforming types. The docs for
zero
say:— Zero is the identity element for addition. For any value,
x + .zero == x
and.zero + x == x
.But really—aside from the implied relationship between
+
and+=
,-
and-=
based on their general descriptions—that's about it. One would expect a relationship between addition and subtraction, for example. @stephentyrone and I came up with a (probably incomplete) list of things that are missing fromAdditiveArithmetic
, e.g., subject to representational limitations (such as floating point precision or the inability to represent negative numbers):.zero - x
is the additive inverse ofx
.+
is commutative.+
is associative.a + b == c
impliesc - b == a
for a type that conforms to both
AdditiveArithmetic
andComparable
,a > b
,c >= 0
impliesa + c >= b + c
, with equality only if rounding occurs.Note: when these laws are documented they shouldn't be buried in the detailed descriptions of individual operations, like the quoted text about
.zero
above is.Note: the longer we wait to document these laws, the more existing conformances are potentially “broken” by the introduction of the new semantic constraints.
The text was updated successfully, but these errors were encountered: