r/haskell • u/Javran • Mar 01 '23
[ANN] sorting-network initial release
https://hackage.haskell.org/package/sorting-network repo: https://github.com/Javran/sorting-network (PRs are welcome!)
I have this idea of using TH to turn sorting network into partial sort functions for small lists (2 - 16 elements) for a while. Not many resources turn up with I searched Haskell topics on this so I got my hands dirty. I'm happy with the performance (see some benchmark output below) with my proof-of-concept code, so I decided to turn this into a package (apologies that I didn't spend much work on documenting, in my defense I'm quite excited to share it - expect quality to improve in future releases).
To use this, import Data.SortingNetwork to bring into scope functions like sortList4By :: (a -> a -> Ordering) -> [a] -> [a] (partial function) and sortTup4By :: (a -> a -> Ordering) -> (a, a, a, a) -> (a, a, a, a) (supporting list / tuple of 2 - 16 elements, but TH module is exported so you can generate more).
To explain how it works, the implementation took some inspiration from https://doisinkidney.com/posts/2018-05-06-sorting-small.html. For a input size 3, a sorting network (more specifically, my package uses https://en.wikipedia.org/wiki/Batcher_odd%E2%80%93even_mergesort - I'll try to try to have optimal network and other varieties in future releases) would compare and conditionally swap a-b, a-c, b-c in that order (I'm kinda relying on optimization to figure out data dependencies and take advantage of parallelism). And the TH generates function like below (name shadowing is intentional for demostration, although in code I do ensure that all new variables are fresh):
sortTup3By = \cmp (a, b, c) ->
let sw = \u v f ->
if cmp u v == GT then f v u else f u v
in sw a b \a b ->
sw a c \a c ->
sw b c \b c ->
(a, b, c) Unit tests are complete coverage based on 0-1 principle.
Benchmark output is available: https://gist.github.com/Javran/95cf757866668e43fe188d70f25c1c05 (comparing with Data.List.sort)
2
u/Endicy Mar 03 '23
Sorry, can you explain why there's 15 different sort functions for lists? What if I use
sortList2Bywith a list that's longer than 2 elements?6
u/Syrak Mar 01 '23
I wonder how smart a compiler needs to be to compile this to a sequence of conditional swaps instead of blowing up the code into a fat binary tree, or whether it can be coerced into doing that.
It would also be interesting to compare sorting networks---which have a fixed geometry---with general sorting algorithms that require fewer comparison but may have less data parallelism.
🤩
3
u/[deleted] Mar 01 '23
[removed] — view removed comment