Skip to content

Commit

Permalink
Generalisation of Fisher-Yates Correctness (#146)
Browse files Browse the repository at this point in the history
Matches the correctness specification we match against in
`TestFisherYates` in `Tests.dfy`.

By submitting this pull request, I confirm that my contribution is made
under the terms of the [MIT
license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).
  • Loading branch information
stefan-aws authored Feb 12, 2024
1 parent 72e4ae4 commit b8a91b2
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Util/FisherYates/Correctness.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ module FisherYates.Correctness {
Lemmas
*******/

lemma CorrectnessFisherYates<T(!new)>(xs: seq<T>, p: seq<T>)
requires
var xs' := seq(|xs|, i requires 0 <= i < |xs| => (xs[i], i));
var p' := seq(|p|, i requires 0 <= i < |p| => (p[i], i));
multiset(p') == multiset(xs')
ensures
var xs' := seq(|xs|, i requires 0 <= i < |xs| => (xs[i], i));
var p' := seq(|p|, i requires 0 <= i < |p| => (p[i], i));
var e := iset s | Model.Shuffle(xs')(s).Equals(p');
e in Rand.eventSpace
&& Rand.prob(e) == 1.0 / (NatArith.FactorialTraditional(|xs|) as real)
{
var xs' := seq(|xs|, i requires 0 <= i < |xs| => (xs[i], i));
var p' := seq(|p|, i requires 0 <= i < |p| => (p[i], i));
CorrectnessFisherYatesUniqueElements(xs', p');
}

lemma CorrectnessFisherYatesUniqueElements<T(!new)>(xs: seq<T>, p: seq<T>)
requires forall a, b | 0 <= a < b < |xs| :: xs[a] != xs[b]
requires multiset(p) == multiset(xs)
Expand Down

0 comments on commit b8a91b2

Please sign in to comment.