Zulip Chat Archive
Stream: lean4
Topic: Array (and Vector) sorting API
Wrenna Robson (Sep 16 2025 at 09:28):
There are various sort functions on Array: Array.qsort, Array.insertionSort, Array.heapSort, plus some merging functions but no sorts as such.
What I can't find is any API for these - in particular, any assertion that these do actually sort an array or indeed all produce the same function. We also don't I think have any sorting capability for Vector, despite it really being a natural thing to define because sorting doesn't change the size of an Array.
Am I wrong about this? Could this be added? What actually needs to be added?
Henrik Böving (Sep 16 2025 at 09:45):
The verification of Array.qsort is being delayed on purpose as it needs to be refactored to a proper qsort implementation that is more robust. Array.insertionSort is more of a meta programming gadget though I guess it might be interesting to verify it. I can't speak on heapSort.
Wrenna Robson (Sep 16 2025 at 10:17):
Meta-programming in the sense that it is not efficient so it isn't good for actual implementations?
Aaron Liu (Sep 16 2025 at 10:25):
Metaprogramming as in there are times in the metaprogramming where you have to sort an almost sorted list
Aaron Liu (Sep 16 2025 at 10:25):
so it's used for that
Wrenna Robson (Sep 16 2025 at 10:25):
Right
Aaron Liu (Sep 16 2025 at 10:26):
I forgot where is used though
Ruben Van de Velde (Sep 16 2025 at 10:26):
I'm not sure that counts as "specific for metaprogramming" rather than "specific for programming"
Aaron Liu (Sep 16 2025 at 10:26):
Well I guess also in programming you have to sort lists
Ruben Van de Velde (Sep 16 2025 at 10:27):
That's what most of my algorithms class claimed, yes :)
Wrenna Robson (Sep 16 2025 at 10:36):
It would at least be good to have some kind of predicate for "this array is sorted" or "these two arrays are the same but permuted" - like List.Sorted or List.Perm.
Wrenna Robson (Sep 16 2025 at 10:37):
Ah in fact Array.Perm exists.
Wrenna Robson (Sep 16 2025 at 10:38):
But is somewhat undersupported because it's just a wrapper around List.Perm.
Wrenna Robson (Sep 16 2025 at 10:39):
There is no Array.isPermthough (which would be good).
Shreyas Srinivas (Sep 16 2025 at 11:22):
I started a repo for collecting efficient sorting algorithms in lean. Progress is slow since I only work on this repo twice a month. But PRs are always welcome : https://github.com/Shreyas4991/Sorting-in-Lean
Shreyas Srinivas (Sep 16 2025 at 11:23):
Henrik Böving said:
The verification of
Array.qsortis being delayed on purpose as it needs to be refactored to a proper qsort implementation that is more robust.Array.insertionSortis more of a meta programming gadget though I guess it might be interesting to verify it. I can't speak onheapSort.
insertion sort has some interesting uses. It's basically as good as it gets for streams, and it is a useful primitive for sorting small subarrays.
Henrik Böving (Sep 16 2025 at 11:25):
Shreyas Srinivas said:
I started a repo for collecting efficient sorting algorithms in lean. Progress is slow since I only work on this repo twice a month. But PRs are always welcome : https://github.com/Shreyas4991/Sorting-in-Lean
AFAICT there is no code in there at all?
Shreyas Srinivas (Sep 16 2025 at 11:26):
Takes time to lit search
Shreyas Srinivas (Sep 16 2025 at 11:26):
I only started this last month 3 weeks ago.
Wrenna Robson (Sep 16 2025 at 11:26):
I would quite like Batcher's odd-even sorting network.
Wrenna Robson (Sep 16 2025 at 11:28):
But in general I wanted this because if an array as represents a permutation p (i.e. as[i] = p i), then if you sort the array whose ith element is (as[i], i) with the lexographic order, the resulting array has ith element (i, p.symm i).
Wrenna Robson (Sep 16 2025 at 11:29):
Which is a useful way of applying the inverse of a permutation stored as an array while avoiding conditional memory lookups, if you use a sorting network.
Last updated: Dec 20 2025 at 21:32 UTC