Giter Club home page Giter Club logo

stablesort's Introduction

Stable sort algorithms in Coq

Docker CI

This library provides a generic and modular way to prove the correctness, including stability, of several mergesort functions. The correctness lemmas in this library are overloaded using a canonical structure (StableSort.function). This structure characterizes stable mergesort functions, say sort, by its abstract version asort parameterized over the type of sorted lists and its operators such as merge, the relational parametricity of asort, and two equational properties that asort instantiated with merge and concatenation are sort and the identity function, respectively, which intuitively mean that replacing merge with concatenation turns sort into the identity function. From this characterization, we derive an induction principle over traces—binary trees reflecting the underlying divide-and-conquer structure of mergesort—to reason about the relation between the input and output of sort, and the naturality of sort. These two properties are sufficient to deduce several correctness results of mergesort, including stability. Thus, one may prove the stability of a new sorting function by defining its abstract version, proving the relational parametricity of the latter using the parametricity translation (the Paramcoq plugin), and manually providing the equational properties.

As an application of the above proof methodology, this library provides two kinds of optimized mergesorts. The first kind is non-tail-recursive mergesort. In call-by-need evaluation, they allow us to compute the first k smallest elements of a list of length n in the optimal O(n + k log k) time complexity of the partial and incremental sorting problems. However, the non-tail-recursive merge function linearly consumes the call stack and triggers a stack overflow in call-by-value evaluation. The second kind is tail-recursive mergesorts and thus solves the above issue in call-by-value evaluation. However, it does not allow us to compute the output incrementally regardless of the evaluation strategy. Therefore, there is a performance trade-off between non-tail-recursive and tail-recursive mergesorts, and the best mergesort function for lists depends on the situation, in particular, the evaluation strategy and whether it should be incremental or not. In addition, each of the above two kinds of mergesort functions has a smooth (also called natural) variant of mergesort, which takes advantage of sorted slices in the input.

Meta

Building and installation instructions

The easiest way to install the development version of Stable sort algorithms in Coq is via OPAM:

git clone https://github.com/pi8027/stablesort.git
cd stablesort
opam repo add coq-released https://coq.inria.fr/opam/released
opam install ./coq-stablesort.opam

Credits

The mergesort functions and the stability proofs provided in this library are mostly based on ones in the path library of Mathematical Components.

stablesort's People

Contributors

cohencyril avatar pi8027 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

cohencyril

stablesort's Issues

Benchmark regarding `native_compute` is not reliable

because calling native_compute eventually makes native_compute itself slower: coq/coq#15528. To improve the situation, benchmark.v (currently written in Elpi) has to be reimplemented as an external tool (e.g., written as a shell script) that invokes coqc for each measurement.

Missing lemmas

(* TODO: *)
(* Lemma mem2_sort s x y : leT x y -> mem2 s x y -> mem2 (sort _ leT s) x y. *)
(* TODO: *)
(* Lemma mem2_sort_in s : *)
(* {in s &, total leT} -> {in s & &, transitive leT} -> *)
(* forall x y, leT x y -> mem2 s x y -> mem2 (sort _ leT s) x y. *)

  • sort leT (s1 ++ s2) = merge leT (sort leT s1) (sort leT s2)
  • sort _ (sort _ _) = sort _ _

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.