Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: sorting #39

Closed
robin-aws opened this issue Mar 16, 2022 · 3 comments
Closed

Request: sorting #39

robin-aws opened this issue Mar 16, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@robin-aws
Copy link
Member

robin-aws commented Mar 16, 2022

I whipped up a quick mergesort implementation for the new dafny-reportgenerator tool, but more for fun than because I thought it was the absolute best default choice of algorithm for any Dafny user: https://github.com/dafny-lang/dafny-reportgenerator/blob/main/src/StandardLibrary.dfy#L38 There are lots of sorting algorithm implementations floating around in Dafny, including at least 4 in the regression suite. :) But there should be a sensible default implementation in the standard library.

I'd suggest implementing these signatures (with the appropriate pre- and post-conditions), for maximum flexibility (Edit: avoid requiring T<0> by using ToArray):

  method SortArray<T>(a: array<T>, compare: (T, T) -> bool) {
    ...
  }

  function Sort<T>(s: seq<T>, compare: (T, T) -> bool): seq<T> {
    ...
  } by method {
    var a := Seq.ToArray(s);  // From https://github.com/dafny-lang/libraries/blob/master/src/Collections/Sequences/Seq.dfy#L128
    SortArray(a, compare);
    return a[..];
  }

I'd recommend implementing Musser's Introspective Sort, which is becoming the standard: https://en.wikipedia.org/wiki/Introsort. I'd be happy with just a heapsort to start with, though, as it has the most predictable runtime and minimal space overhead if you're starting with a mutable array. We can always add the optimization of quicksort-until-it-tends-quadratic later.

Another wrinkle is how to parameterize by the comparison function. Expressing the total ordering requirements using forall expressions is easy IF we assume T is not a reference type, but I expect that will be limiting. Perhaps it's feasible to require the comparison function is a total ordering just over all values being sorted, rather than all possible T values?

@robin-aws robin-aws added the enhancement New feature or request label Mar 16, 2022
@RustanLeino
Copy link
Collaborator

A way to write the ordering requirements today is to quantify only over the elements of the array. For example,

forall x :: x in a[..] ==> compare(x, x) // reflexivity

@cpitclaudel
Copy link
Member

I'd recommend implementing Musser's Introspective Sort,

Or https://en.wikipedia.org/wiki/Timsort , which historically had a bug that was found using formal methods ^^

@robin-aws
Copy link
Member Author

We have https://github.com/dafny-lang/libraries/blob/master/src/Collections/Sequences/MergeSort.dfy now, and #54 as a request for something more efficient, so we can close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants