On Fri, Jul 22, 2016 at 9:38 PM, <jaden.gel...@gmail.com> wrote: > This seems reasonable to me. I don't see why `===` has to do a total order > comparison when we already have another operator, `<=>`, that does that. >
I take it back. It wouldn't solve the issue that generic `<` would still behave in surprising ways, where two equal floating point values represented differently might be less than each other or greater than each other. I think what we really want is all identity and comparison divorced from IEEE totalOrder. > On Jul 22, 2016, at 7:35 PM, Xiaodi Wu <xiaodi...@gmail.com> wrote: > > On Fri, Jul 22, 2016 at 9:28 PM, Dave Abrahams <dabrah...@apple.com> > wrote: > >> >> on Fri Jul 22 2016, Xiaodi Wu <xiaodi.wu-AT-gmail.com> wrote: >> >> > On Fri, Jul 22, 2016 at 9:13 PM, Matthew Johnson < >> matt...@anandabits.com> >> > wrote: >> > >> >> >> >> On Jul 22, 2016, at 9:10 PM, Xiaodi Wu <xiaodi...@gmail.com> wrote: >> >> >> >> On Fri, Jul 22, 2016 at 9:08 PM, Matthew Johnson < >> matt...@anandabits.com> >> >> wrote: >> >> >> >>> >> >>> On Jul 22, 2016, at 9:04 PM, Xiaodi Wu <xiaodi...@gmail.com> wrote: >> >>> >> >>> On Fri, Jul 22, 2016 at 8:57 PM, Matthew Johnson < >> matt...@anandabits.com> >> >>> wrote: >> >>> >> >>>> >> >>>> On Jul 22, 2016, at 8:54 PM, Xiaodi Wu via swift-evolution < >> >>>> swift-evolution@swift.org> wrote: >> >>>> >> >>>> On Fri, Jul 22, 2016 at 8:52 PM, Jaden Geller via swift-evolution < >> >>>> swift-evolution@swift.org> wrote: >> >>>> >> >>>>> "The totalOrder predicate will order these cases, and it also >> >>>>> distinguishes between different representations of NaNs and between >> the >> >>>>> same decimal floating point number encoded in different ways." >> >>>>> - [Wikipedia]( >> >>>>> >> https://en.wikipedia.org/wiki/IEEE_floating_point#Total-ordering_predicate >> >>>>> ) >> >>>>> >> >>>>> Sounds like `===` should not return `true` for zeros of different >> >>>>> signs, then. >> >>>>> >> >>>> >> >>>> Fair enough; the result of that will be, as Pyry noted above, that: >> >>>> >> >>>> ``` >> >>>> [-0.0, 1.0, .nan, 0.0].firstIndex(of: 0.0) //=> 3, not 0 >> >>>> ``` >> >>>> >> >>>> >> >>>> Maybe we need floating point specific implementations of some >> algorithms >> >>>> to resolve this problem? >> >>>> >> >>>> It doesn’t seem like there is a way to provide the semantics >> required by >> >>>> generic algorithms and still provide the expected behavior for >> floating >> >>>> point values. >> >>>> >> >>> >> >>> Well, what I'm trying to say is that generic algorithms such as >> >>> `index(of:)` require only an equivalence relation. For floating point >> >>> types, there are three ways to slice it: >> >>> >> >>> 1. NaN != NaN and +0 == -0 [what the traditional comparison operators >> are >> >>> constrained to do] >> >>> 2. NaN == NaN, +0 == -0, and the same number encoded different ways >> >>> compare equal >> >>> 3. NaN == NaN, +0 != -0, and the same number encoded different ways >> >>> compare not equal >> >>> >> >>> Both #2 and #3 can fall out of valid equivalence relations; if `===` >> >>> behaved like #2 for FloatingPoint types, then generic algorithms work >> just >> >>> fine. If we insist on using a total ordering defined by `<=>` all the >> time, >> >>> then we've got problems. >> >>> >> >>> >> >>> And if we don’t then we’re back to 3 different concepts of equality. >> >>> There is definitely a tradeoff no matter what we choose. >> >>> >> >> >> >> If some types have three concepts of equality, each with their >> particular >> >> use, why must we eliminate one of them? >> >> >> >> >> >> This isn’t about eliminating concepts of equality for a type. They can >> >> have 42 if they want. >> >> >> >> This is about the right way to define the semantics of specific >> >> protocols. It says nothing about additional notions of equality a >> type may >> >> have available. >> >> >> >> The difficulty is finding a design for the protocols that makes sense >> with >> >> floating point types because we want them to be able to conform to the >> >> protocols. >> >> >> > >> > Agreed. My argument is that if a Comparable can define its own `===`, >> still >> > supplying a valid equivalence relation but not being constrained by a >> > contract that `(a <=> b) == .same` iff `a === b`, then we are good to go >> > with floating point types. >> >> How would that work? Can you spell out the implications, show how <=> >> and === would be implemented, and describe what it would mean for >> algorithms? >> > > Right. I'm not married to this solution anymore, but I do think it could > work. There would still be a relationship required between `===` and `<=>`. > Namely: > > `a === b` if `(a <=> b) == .same` > > But for some values a and b, it is permitted that `a === b && (a <=> b) != > .same`. That is, two identical values may be ordered in a total ordering > based on *inessential* qualities. > Generic algorithms that need to produce a stable ordering of elements will > use `<=>`. Those such as `index(of:)` will use `===` to test for identity. > Wouldn't that work? > > > >> > On Jul 22, 2016, at 6:48 PM, Dave Abrahams via swift-evolution < >> >>>>> swift-evolution@swift.org> wrote: >> >>>>> >> >>>>> >> >>>>> on Fri Jul 22 2016, Jaden Geller <swift-evolution@swift.org> wrote: >> >>>>> >> >>>>> For floating point, I'd hope that `a === b` if `(a <=> b) == .same` >> >>>>> *but not iff*. This is to satisfy IEEE 754: "Comparisons shall >> >>>>> ignore the sign of zero (so +0 = −0)". >> >>>>> >> >>>>> >> >>>>> I don't see why both `(+0) === (-0)` and `(+0) <=> (-0)` can't >> return >> >>>>> `true` and `.same`, respectively. This doesn't break the total >> >>>>> ordering of values. `===` doesn't do raw memory comparison. They're >> >>>>> "identical", so it ought to return `true`. >> >>>>> >> >>>>> >> >>>>> It ought to do whatever IEEE-754 specifies that its total ordering >> test >> >>>>> does. That is, IEEE-754 gets to decide whether the difference >> between >> >>>>> +0 and -0 is “essential” to IEEE-754 floating point types, or not. >> >>>>> >> >>>>> >> >>>>> On Jul 22, 2016, at 6:37 PM, Xiaodi Wu via swift-evolution >> >>>>> <swift-evolution@swift.org> wrote: >> >>>>> >> >>>>> On Fri, Jul 22, 2016 at 8:20 PM, Dave Abrahams via swift-evolution >> >>>>> <swift-evolution@swift.org >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>>> >> >>>>> wrote: >> >>>>> >> >>>>> on Fri Jul 22 2016, Daniel Duan <daniel-AT-duan.org >> <http://daniel-at-duan.org> >> >>>>> <http://daniel-at-duan.org/>> wrote: >> >>>>> >> >>>>> On Jul 22, 2016, at 3:00 PM, Dave Abrahams via swift-evolution >> >>>>> <swift-evolution@swift.org >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>>> >> >>>>> wrote: >> >>>>> >> >>>>> >> >>>>> on Fri Jul 22 2016, Daniel Duan >> >>>>> <swift-evolution@swift.org >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>>>> >> >>>>> wrote: >> >>>>> >> >>>>> >> >>>>> On Jul 22, 2016, at 11:05 AM, Dave Abrahams via swift-evolution >> >>>>> <swift-evolution@swift.org >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>>>> >> >>>>> wrote: >> >>>>> >> >>>>> >> >>>>> on Thu Jul 21 2016, Duan >> >>>>> >> >>>>> >> >>>>> <swift-evolution@swift.org >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>>> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>>>>> >> >>>>> wrote: >> >>>>> >> >>>>> Great proposal. I want to second that areSame may mislead user to >> >>>>> think this is about identity. >> >>>>> >> >>>>> I like areEquivalent() but there may be better names. >> >>>>> >> >>>>> >> >>>>> It really *is* about identity as I posted in a previous message. >> But >> >>>>> that doesn't change the fact that areEquivalent might be a better >> name. >> >>>>> It's one of the things we considered; it just seemed long for no >> real >> >>>>> benefit. >> >>>>> >> >>>>> >> >>>>> If the addresses of the arguments aren’t being used, then we don’t >> >>>>> consider >> >>>>> them part of their *identity*. I can follow this logic. My fear is >> most >> >>>>> users >> >>>>> won’t make this leap on their own and get the same initial >> impression >> >>>>> as I did. >> >>>>> It's entirely possible this fear is unfounded. Some educated >> bikesheding >> >>>>> wouldn't hurt here IMO :) >> >>>>> >> >>>>> >> >>>>> Well, it's still a very real question whether we ought to have the >> >>>>> additional API surface implied by areSame, or wether we should >> collapse >> >>>>> it with ===. >> >>>>> >> >>>>> >> >>>>> To spell this out (because I had to think about it for a second): >> === >> >>>>> will be derived from >> >>>>> <=>, >> >>>>> but also becomes default implementation for ==, which remains open >> for >> >>>>> customization. >> >>>>> >> >>>>> >> >>>>> I was imagining roughly this (untested): >> >>>>> >> >>>>> /// Two references are identical if they refer to the same >> >>>>> /// instance. >> >>>>> /// >> >>>>> /// - Note: Classes with a more-refined notion of “identical” >> >>>>> /// should conform to `Identifiable` and implement `===`. >> >>>>> func ===(lhs: AnyObject, rhs: AnyObject) -> Bool { >> >>>>> ObjectIdentifier(lhs) == ObjectIdentifier(rhs) >> >>>>> } >> >>>>> >> >>>>> /// Supports testing that two values of `Self` are identical >> >>>>> /// >> >>>>> /// If `a` and `b` are of type `Self`, `a === b` means that >> >>>>> /// `a` and `b` are interchangeable in most code. A conforming >> >>>>> /// type can document that specific observable characteristics >> >>>>> /// (such as the `capacity` of an `Array`) are inessential and >> >>>>> /// thus not to be considered as part of the interchangeability >> >>>>> /// guarantee. >> >>>>> /// >> >>>>> /// - Requires: `===` induces an equivalence relation over >> >>>>> /// instances. >> >>>>> /// - Note: conforming types will gain an `==` operator that >> >>>>> /// forwards to `===`. >> >>>>> /// - Note: Types that require domain-specific `==` >> >>>>> /// implementations with different semantics (e.g. floating >> >>>>> /// point) should define a more-specific overload of `==`, >> >>>>> /// which will be used in contexts where the static type is >> >>>>> /// known to the compiler. >> >>>>> /// - Note: Generic code should usually use `==` to compare >> >>>>> /// conforming instances; that will always dispatch to `===` >> >>>>> /// and will be unaffected by more specific overloads of >> >>>>> /// `==`. >> >>>>> protocol Identifiable { // née Equatable name is negotiable >> >>>>> func ===(_: Self, _: aSelf) -> Bool >> >>>>> } >> >>>>> >> >>>>> /// Default definition of `==` for Identifiable types. >> >>>>> func ==<T: Identifiable>(lhs: T, rhs: T) -> Bool { >> >>>>> return lhs === rhs >> >>>>> } >> >>>>> >> >>>>> /// Conforming types have a default total ordering. >> >>>>> /// >> >>>>> /// If `a` and `b` are of type `Self`, `a <=> b` means that >> >>>>> /// `a` and `b` are interchangeable in most code. A conforming >> >>>>> /// type can document that specific observable characteristics >> >>>>> /// (such as the `capacity` of an `Array`) are inessential and >> >>>>> /// thus not to be considered as part of the interchangeability >> >>>>> /// guarantee. >> >>>>> /// >> >>>>> /// - Requires: `<=>` induces a total ordering over >> >>>>> /// instances. >> >>>>> /// - Requires: the semantics of `<=>` are consistent with >> >>>>> /// those of `===`. That is, `(a <=> b) == .equivalent` >> >>>>> /// iff `a === b`. >> >>>>> >> >>>>> For floating point, I'd hope that `a === b` if `(a <=> b) == .same` >> >>>>> *but not iff*. This is to satisfy IEEE 754: "Comparisons shall >> ignore the >> >>>>> sign of zero (so +0 = −0)". >> >>>>> >> >>>>> /// - Note: conforming types will gain `<`, `<=`, `>`, and `>=` >> >>>>> /// operators defined in terms of `<=>`. >> >>>>> /// - Note: Types that require domain-specific `<`, etc. >> >>>>> /// implementations with different semantics (e.g. floating >> >>>>> /// point) should define more-specific overloads of those >> >>>>> /// operators, which will be used in contexts where the >> >>>>> /// static type is known to the compiler. >> >>>>> /// - Note: Generic code can freely use `<=>` or the traditional >> >>>>> /// comparison operators to compare conforming instances; >> >>>>> /// the result will always be supplied by `<=>` >> >>>>> /// and will be unaffected by more specific overloads of >> >>>>> /// the other operators. >> >>>>> protocol Comparable : Identifiable { >> >>>>> func <=> (lhs: Self, rhs: Self) -> Ordering >> >>>>> } >> >>>>> >> >>>>> /// Default implementations of `<`, `<=`, `>`, and `>=`. >> >>>>> extension Comparable { >> >>>>> static func <(lhs: Self, rhs: Self) -> Bool { >> >>>>> return (lhs <=> rhs) == .ascending >> >>>>> } >> >>>>> static func <=(lhs: Self, rhs: Self) -> Bool { >> >>>>> return (rhs <=> lhs) != .ascending >> >>>>> } >> >>>>> static func >(lhs: Self, rhs: Self) -> Bool { >> >>>>> return (lhs <=> rhs) == .descending >> >>>>> } >> >>>>> static func >=(lhs: Self, rhs: Self) -> Bool { >> >>>>> return (rhs <=> lhs) != .descending >> >>>>> } >> >>>>> } >> >>>>> >> >>>>> I like this idea. If we keep === as a separate thing, now users >> have 3 >> >>>>> “opportunities” to define >> >>>>> equality. The must be few, if any, use cases for this. >> >>>>> >> >>>>> Would love to see if anyone on the list can give us an example. >> >>>>> Otherwise we should make >> >>>>> areSame === again™! >> >>>>> >> >>>>> >> >>>>> Daniel Duan >> >>>>> Sent from my iPhone >> >>>>> >> >>>>> On Jul 21, 2016, at 6:32 PM, Robert Widmann via swift-evolution >> >>>>> <swift-evolution@swift.org >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>>> >> >>>>> wrote: >> >>>>> >> >>>>> >> >>>>> On Jul 21, 2016, at 6:19 PM, Xiaodi Wu >> >>>>> <xiaodi...@gmail.com >> >>>>> <mailto:xiaodi...@gmail.com <xiaodi...@gmail.com>>> >> >>>>> wrote: >> >>>>> >> >>>>> This is nice. Is `areSame()` being proposed because static `==` is >> >>>>> the status quo and you're trying to make the point that `==` in the >> >>>>> future need not guarantee the same semantics? >> >>>>> >> >>>>> >> >>>>> Yep! Equivalence and equality are strictly very different things. >> >>>>> >> >>>>> >> >>>>> Nit: I think the more common term in stdlib would be >> >>>>> `areEquivalent()`. Do you think `same` in that context (independent >> >>>>> of the word "ordering") might erroneously suggest identity? >> >>>>> >> >>>>> >> >>>>> There is room for improvement here. Keep ‘em coming. >> >>>>> >> >>>>> >> >>>>> >> >>>>> On Thu, Jul 21, 2016 at 8:11 PM, Robert Widmann via >> >>>>> swift-evolution >> >>>>> <swift-evolution@swift.org >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>>> >> >>>>> wrote: >> >>>>> Hello Swift Community, >> >>>>> >> >>>>> Harlan Haskins, Jaden Geller, and I have been working on a >> >>>>> proposal to clean up the semantics of ordering relations in the >> >>>>> standard library. We have a draft that you can get as a gist. >> >>>>> Any feedback you might have about this proposal helps - though >> >>>>> please keeps your comments on Swift-Evolution and not on the gist. >> >>>>> >> >>>>> Cheers, >> >>>>> >> >>>>> ~Robert Widmann >> >>>>> >> >>>>> >> >>>>> >> >>>>> >> >>>>> >> >>>>> _______________________________________________ >> >>>>> swift-evolution mailing list >> >>>>> swift-evolution@swift.org >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>> >> >>>>> https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution> >> >>>>> >> >>>>> >> >>>>> >> >>>>> _______________________________________________ >> >>>>> swift-evolution mailing list >> >>>>> swift-evolution@swift.org >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>> >> >>>>> https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution> >> >>>>> >> >>>>> _______________________________________________ >> >>>>> swift-evolution mailing list >> >>>>> swift-evolution@swift.org >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>> >> >>>>> https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution> >> >>>>> >> >>>>> >> >>>>> -- >> >>>>> Dave >> >>>>> >> >>>>> _______________________________________________ >> >>>>> swift-evolution mailing list >> >>>>> swift-evolution@swift.org >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>>>> >> >>>>> https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution> >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution>> >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution> >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution>>> >> >>>>> >> >>>>> _______________________________________________ >> >>>>> swift-evolution mailing list >> >>>>> swift-evolution@swift.org >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>>> >> >>>>> https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution> >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution>> >> >>>>> >> >>>>> >> >>>>> -- >> >>>>> Dave >> >>>>> >> >>>>> _______________________________________________ >> >>>>> swift-evolution mailing list >> >>>>> swift-evolution@swift.org >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org> >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>>> >> >>>>> https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution> >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution>> >> >>>>> >> >>>>> >> >>>>> -- >> >>>>> Dave >> >>>>> _______________________________________________ >> >>>>> swift-evolution mailing list >> >>>>> swift-evolution@swift.org >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>> >> >>>>> https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution> >> >>>>> >> >>>>> _______________________________________________ >> >>>>> swift-evolution mailing list >> >>>>> swift-evolution@swift.org >> >>>>> <mailto:swift-evolution@swift.org <swift-evolution@swift.org>> >> >>>>> https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution> >> >>>>> >> >>>>> _______________________________________________ >> >>>>> swift-evolution mailing list >> >>>>> swift-evolution@swift.org >> >>>>> https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>>> >> >>>>> >> >>>>> -- >> >>>>> Dave >> >>>>> >> >>>>> _______________________________________________ >> >>>>> swift-evolution mailing list >> >>>>> swift-evolution@swift.org >> >>>>> https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>>> >> >>>>> >> >>>>> >> >>>>> _______________________________________________ >> >>>>> swift-evolution mailing list >> >>>>> swift-evolution@swift.org >> >>>>> https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>>> >> >>>>> >> >>>> _______________________________________________ >> >>>> swift-evolution mailing list >> >>>> swift-evolution@swift.org >> >>>> https://lists.swift.org/mailman/listinfo/swift-evolution >> >>>> >> >>>> >> >>> >> >> >> >> >> >> -- >> Dave >> > >
_______________________________________________ swift-evolution mailing list swift-evolution@swift.org https://lists.swift.org/mailman/listinfo/swift-evolution