Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-13984] Numeric protocols have almost no laws #56379

Open
dabrahams opened this issue Dec 22, 2020 · 1 comment
Open

[SR-13984] Numeric protocols have almost no laws #56379

dabrahams opened this issue Dec 22, 2020 · 1 comment
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

Comments

@dabrahams
Copy link
Collaborator

Previous ID SR-13984
Radar rdar://problem/72592920
Original Reporter @dabrahams
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug, Documentation, ProtocolConformance
Assignee None
Priority Medium

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 from AdditiveArithmetic, e.g., subject to representational limitations (such as floating point precision or the inability to represent negative numbers):

  • .zero - x is the additive inverse of x.

  • + is commutative.

  • + is associative.

  • a + b == c implies c - b == a

  • for a type that conforms to both AdditiveArithmetic and Comparable, a > b, c >= 0 implies a + 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.

@typesanitizer
Copy link

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

2 participants