A bargain for mergesorts (functional pearl) -- How to prove your mergesort correct and stable, almost for free
<|reference_start|>A bargain for mergesorts (functional pearl) -- How to prove your mergesort correct and stable, almost for free: We present a novel characterization of stable mergesort functions using relational parametricity, and show that it implies the correctness of mergesort. As a result, one can prove the correctness of several variations of mergesort (e.g., top-down, bottom-up, tail-recursive, non-tail-recursive, smooth, and non-smooth mergesorts) by proving the characterization property for each variation. To further motivate this work, we show a performance trade-off between tail-recursive and non-tail-recursive mergesorts that (1) the former in call-by-value evaluation avoids using up stack space and is efficient and (2) the latter in call-by-need evaluation is an optimal incremental sort, meaning that it performs only $\mathcal{O}(n + k \log k)$ comparisons to compute the least (or greatest) $k$ items of a list of length $n$. Thanks to our characterization and the parametricity translation, we deduced the correctness results, including stability, of various implementations of mergesort for lists, including highly optimized ones, in the Coq proof assistant.<|reference_end|>
@article{cohen2024a,
    title={A bargain for mergesorts (functional pearl) -- How to prove your
  mergesort correct and stable, almost for free},
    author={Cyril Cohen and Kazuhiko Sakaguchi},
    journal={arXiv preprint arXiv:2403.08173},
    year={2024},
    archivePrefix={arXiv},
    eprint={2403.08173},
    primaryClass={cs.LO cs.DS cs.PL}
}