# Why can't I use \`min\_by\_key\` and friends with \`PartialOrd\`?

**URL:** <https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312>\
**Category:** Uncategorized\
**Created:** [January 30, 2019, 1:49pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312 "2019-01-30T13:49:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![quadrupleslap](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quadrupleslap/32/2361_2.png) [@quadrupleslap](https://internals.rust-lang.org/u/quadrupleslap)\
**Post date:** [January 30, 2019, 1:49pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/1 "2019-01-30T13:49:05Z")

</div>

They’re still guaranteed to find a **min** imal or **max** imal element even if it isn’t a total order, right?

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [January 30, 2019, 1:51pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/2 "2019-01-30T13:51:02Z")

</div>

I don’t think so. The minimum or maximum of a poset are not uniquely defined.

---

<div class="post-metadata">

**Author:** ![DanielKeep](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danielkeep/32/1332_2.png) [@DanielKeep](https://internals.rust-lang.org/u/DanielKeep)\
**Post date:** [January 30, 2019, 1:53pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/3 "2019-01-30T13:53:33Z")

</div>

You can say which is smaller of “Banana” and “Grape”, and which is smaller (by wavelength) of “Red” and “Purple”… but which is smaller of “Banana” and “Purple”?

---

<div class="post-metadata">

**Author:** ![quadrupleslap](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quadrupleslap/32/2361_2.png) [@quadrupleslap](https://internals.rust-lang.org/u/quadrupleslap)\
**Post date:** [January 30, 2019, 1:56pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/4 "2019-01-30T13:56:25Z")

</div>

It can still be useful to get any one of those minima.

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [January 30, 2019, 4:39pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/5 "2019-01-30T16:39:08Z")

</div>

- `f64::NAN < 0.0` is `false`
- `0.0 < f64::NAN` is `false`

Which should `min_by_key` return?

---

<div class="post-metadata">

**Author:** ![quadrupleslap](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quadrupleslap/32/2361_2.png) [@quadrupleslap](https://internals.rust-lang.org/u/quadrupleslap)\
**Post date:** [January 30, 2019, 5:25pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/6 "2019-01-30T17:25:15Z")

</div>

I want `min` to be equivalent to the following.

```rust
fn min<I: Iterator>(mut iter: I) -> Option<I::Item> where I::Item: PartialOrd {
    iter.next().map(|first| iter.fold(first, |current, other| if other < current { other } else { current }))
}

```

That’s guaranteed to return _a_ minimal element. It’s not necessarily the first one, though. So I guess in your example the list `[NAN, 0]` would give `NAN`, but `[0, NAN]` would give `0`.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [January 30, 2019, 7:00pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/7 "2019-01-30T19:00:48Z")

</div>

> [@quadrupleslap](#):
>
> That’s guaranteed to return _a_ minimal element.

That depends on your definitions. If you say a minimal element is an `x` such that `x <= a[i]` for all valid `i` -- which I think is a perfectly intuitive definition -- then `NAN` is not a minimal element, and in fact there is no minimal element if the sequence contains `NAN`.

---

<div class="post-metadata">

**Author:** ![Tom-Phinney](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tom-phinney/32/3299_2.png) [@Tom-Phinney](https://internals.rust-lang.org/u/Tom-Phinney)\
**Post date:** [January 30, 2019, 8:38pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/8 "2019-01-30T20:38:26Z")

</div>

In a [partially-ordered set](https://en.wikipedia.org/wiki/Partially_ordered_set), all elements need not be comparable. In other words, for two elements `a` and `b`, it may be the case that none of `a < b`, `a = b` or `a > b` hold. In IEEE floating point, `NaN` is such an _incomparable_ value (i.e., comparison to anything, even another `NaN`, is meaningless).

---

<div class="post-metadata">

**Author:** ![quadrupleslap](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quadrupleslap/32/2361_2.png) [@quadrupleslap](https://internals.rust-lang.org/u/quadrupleslap)\
**Post date:** [January 31, 2019, 10:51am UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/9 "2019-01-31T10:51:19Z")

</div>

> That depends on your definitions.

Is there a reason not to use [this definition (the common one in maths)](https://en.wikipedia.org/wiki/Maximal_and_minimal_elements)?

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [January 31, 2019, 11:19am UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/10 "2019-01-31T11:19:03Z")

</div>

For example because not even Wikipedia gets to use it consistently. In the article you linked, “minimum” and “minimal element” are treated as different; meanwhile, on the [page about posets](https://en.wikipedia.org/wiki/Partially_ordered_set), these elements are referred to as “least element” and “minima”, and the elements called minima here aren’t unique (there can be 0, 1, or more of them).

I would say that if even the very same medium isn’t sure which concept bears which name, maybe totally different people using Rust wouldn’t agree either, so this has a decent amount of potential for causing confusion.

A different pair of methods with clearer names could help, though; for example something verbose like `element_not_greater_than_any_other` (this is just a strawman name, as it is obviously too long, but you get the idea.)

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [January 31, 2019, 2:03pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/11 "2019-01-31T14:03:17Z")

</div>

`partial_min_by_key` would fit `partial_cmp` & co.

---

<div class="post-metadata">

**Author:** ![quadrupleslap](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quadrupleslap/32/2361_2.png) [@quadrupleslap](https://internals.rust-lang.org/u/quadrupleslap)\
**Post date:** [January 31, 2019, 2:29pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/12 "2019-01-31T14:29:50Z")

</div>

Good point, “minimum” is vague, but “minimal element” always refers to “an element that is not more than any element” and “least element” always refers to “an element that is less than or equal to every element”.

---

<div class="post-metadata">

**Author:** ![Tom-Phinney](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tom-phinney/32/3299_2.png) [@Tom-Phinney](https://internals.rust-lang.org/u/Tom-Phinney)\
**Post date:** [January 31, 2019, 2:42pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/13 "2019-01-31T14:42:08Z")

</div>

But none of “minimum” or “minimal element” or “least element” apply to the `NaN`s of IEEE floating point, which by definition **cannot be compared**. If posets can contain such elements, per the Wikipedia definition, then some term like @kornel’s `partial_min_by_key` will necessarily be required.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [January 31, 2019, 2:50pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/14 "2019-01-31T14:50:59Z")

</div>

That’s actually a great idea.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [January 31, 2019, 7:21pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/15 "2019-01-31T19:21:56Z")

</div>

> [@quadrupleslap](#):
>
> Is there a reason not to use [this definition (the common one in maths)](https://en.wikipedia.org/wiki/Maximal_and_minimal_elements)?

You'll notice that's not "maximum". What I mentioned is [Greatest element and least element - Wikipedia](https://en.m.wikipedia.org/wiki/Greatest_and_least_elements), which explicitly mentions

> In a totally ordered set both terms coincide; it is also called maximum

So both definitions exist in math; I just prefer the one with what I consider the more useful postcondition.

---

<div class="post-metadata">

**Author:** ![nematsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nematsakis/32/4940_2.png) [@nematsakis](https://internals.rust-lang.org/u/nematsakis)\
**Post date:** [February 2, 2019, 7:10pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/16 "2019-02-02T19:10:31Z")

</div>

There are probably use cases for getting a minimal element from a collection of `PartialOrd` but you should have to opt-in to that behavior by using a different function. It seems likely to me that if the standard functions tried to “help” you in this situation it would more often lead to bugs discovered down the road than the desired behavior. It probably also means the implementation of `min` can be more efficient because it doesn’t need to consider the case of incomparable elements.

---

<div class="post-metadata">

**Author:** ![felix.s](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/felix.s/32/8073_2.png) [@felix.s](https://internals.rust-lang.org/u/felix.s)\
**Post date:** [February 2, 2019, 10:56pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/17 "2019-02-02T22:56:35Z")

</div>

The commonly accepted definition of a (mathematical) partial order requires the (non-strict) order relation to be reflexive, which explicitly doesn’t hold for NaNs. Thus, you may even argue that terms like ‘least element’ and ‘minimal element’ are meaningless for the IEEE 754 ‘partial order’, because they presuppose conditions which don’t hold for the ‘ordering’ relation. If we overlook that technicality though, a NaN could be straightforwardly considered a minimal element, because it is true that ¬∃ _x_: (_x_ ≤ NaN ∧ _x_ ≠ NaN). No floating-point value is the least, though.

That said, given that in a general partial order multiple minimal elements may exist in a given collection, a ‘minimal element’ function would have to make some arbitrary choice among them (e.g. always return the leftmost on the argument list/first returned by an iterator). I’d be wary of code that would subtly rely on a particular choice of minimal element, even though some applications may not mind this nondeterminism.

---

<div class="post-metadata">

**Author:** ![punkstarman](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/punkstarman/32/5065_2.png) [@punkstarman](https://internals.rust-lang.org/u/punkstarman)\
**Post date:** [February 5, 2019, 9:53pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/18 "2019-02-05T21:53:35Z")

</div>

I find that using IEEE floats as an example of a partially ordered set isn’t very compelling. In Haskell, the Ord typeclass is implemented by Float. The documentation mentions two caveats:

- Note that due to the presence of `NaN` , `Float` 's `Ord` instance does not satisfy reflexivity.
- Also note that, due to the same, `Ord` 's operator interactions are not respected by `Float` 's instance. See [http://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Ord.html](http://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Ord.html)

Ignoring `NaN` as an edge case seems plausible.

Semilattices make for much better examples of posets. Consider dictionary words ordered using the prefix relation. While the minimum of [“the”, “these”, “they”] is “the”, what is the maximum?

---

<div class="post-metadata">

**Author:** ![DanielKeep](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danielkeep/32/1332_2.png) [@DanielKeep](https://internals.rust-lang.org/u/DanielKeep)\
**Post date:** [February 5, 2019, 11:35pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/19 "2019-02-05T23:35:34Z")

</div>

> [@punkstarman](#):
>
> I find that using IEEE floats as an example of a partially ordered set isn’t very compelling.

Personally, I find it _extremely_ compelling. Porting a simulator from D to Rust caught several bugs _specifically_ because Rust doesn't implement `Ord` on floats.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [May 6, 2019, 11:35pm UTC](https://internals.rust-lang.org/t/why-cant-i-use-min-by-key-and-friends-with-partialord/9312/20 "2019-05-06T23:35:36Z")

</div>

Closed on May 6, 2019, 11:35pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
