Skip to content

Commit

Permalink
Remove SetToSeq
Browse files Browse the repository at this point in the history
The method makes it difficult to use the libraries in a project that
enables `--enforce-determinism`.
  • Loading branch information
atomb committed Aug 20, 2024
1 parent a063212 commit 095d07e
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 124 deletions.
62 changes: 0 additions & 62 deletions src/Collections/Sequences/Seq.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -846,65 +846,3 @@ module {:options "-functionSyntax:4"} Seq {
result := next + result;
}
}

/**********************************************************
*
* Sets to Ordered Sequences
*
***********************************************************/

/* Converts a set to a sequence (ghost). */
ghost function SetToSeqSpec<T>(s: set<T>): (xs: seq<T>)
ensures multiset(s) == multiset(xs)
{
if s == {} then [] else var x :| x in s; [x] + SetToSeqSpec(s - {x})
}

/* Converts a set to a sequence (compiled). */
method SetToSeq<T>(s: set<T>) returns (xs: seq<T>)
ensures multiset(s) == multiset(xs)
{
xs := [];
var left: set<T> := s;
while left != {}
invariant multiset(left) + multiset(xs) == multiset(s)
{
var x :| x in left;
left := left - {x};
xs := xs + [x];
}
}

/* Proves that any two sequences that are sorted by a total order and that have the same elements are equal. */
lemma SortedUnique<T(!new)>(xs: seq<T>, ys: seq<T>, R: (T, T) -> bool)
requires SortedBy(xs, R)
requires SortedBy(ys, R)
requires TotalOrdering(R)
requires multiset(xs) == multiset(ys)
ensures xs == ys
{
assert |xs| == |multiset(xs)| == |multiset(ys)| == |ys|;
if xs == [] || ys == [] {
} else {
assert xs == [xs[0]] + xs[1..];
assert ys == [ys[0]] + ys[1..];
assert multiset(xs[1..]) == multiset(xs) - multiset{xs[0]};
assert multiset(ys[1..]) == multiset(ys) - multiset{ys[0]};
assert multiset(xs[1..]) == multiset(ys[1..]);
SortedUnique(xs[1..], ys[1..], R);
}
}

/* Converts a set to a sequence that is ordered w.r.t. a given total order. */
function SetToSortedSeq<T(!new)>(s: set<T>, R: (T, T) -> bool): (xs: seq<T>)
requires TotalOrdering(R)
ensures multiset(s) == multiset(xs)
ensures SortedBy(xs, R)
{
MergeSortBy(SetToSeqSpec(s), R)
} by method {
xs := SetToSeq(s);
xs := MergeSortBy(xs, R);
SortedUnique(xs, SetToSortedSeq(s, R), R);
}
}
62 changes: 0 additions & 62 deletions src/dafny/Collections/Seqs.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -803,68 +803,6 @@ module {:options "-functionSyntax:4"} Dafny.Collections.Seq {
}


/**********************************************************
*
* Sets to Ordered Sequences
*
***********************************************************/

/* Converts a set to a sequence (ghost). */
ghost function SetToSeqSpec<T>(s: set<T>): (xs: seq<T>)
ensures multiset(s) == multiset(xs)
{
if s == {} then [] else var x :| x in s; [x] + SetToSeqSpec(s - {x})
}

/* Converts a set to a sequence (compiled). */
method SetToSeq<T>(s: set<T>) returns (xs: seq<T>)
ensures multiset(s) == multiset(xs)
{
xs := [];
var left: set<T> := s;
while left != {}
invariant multiset(left) + multiset(xs) == multiset(s)
{
var x :| x in left;
left := left - {x};
xs := xs + [x];
}
}

/* Proves that any two sequences that are sorted by a total order and that have the same elements are equal. */
lemma SortedUnique<T(!new)>(xs: seq<T>, ys: seq<T>, R: (T, T) -> bool)
requires SortedBy(xs, R)
requires SortedBy(ys, R)
requires TotalOrdering(R)
requires multiset(xs) == multiset(ys)
ensures xs == ys
{
assert |xs| == |multiset(xs)| == |multiset(ys)| == |ys|;
if xs == [] || ys == [] {
} else {
assert xs == [xs[0]] + xs[1..];
assert ys == [ys[0]] + ys[1..];
assert multiset(xs[1..]) == multiset(xs) - multiset{xs[0]};
assert multiset(ys[1..]) == multiset(ys) - multiset{ys[0]};
assert multiset(xs[1..]) == multiset(ys[1..]);
SortedUnique(xs[1..], ys[1..], R);
}
}

/* Converts a set to a sequence that is ordered w.r.t. a given total order. */
function SetToSortedSeq<T(!new)>(s: set<T>, R: (T, T) -> bool): (xs: seq<T>)
requires TotalOrdering(R)
ensures multiset(s) == multiset(xs)
ensures SortedBy(xs, R)
{
MergeSortBy(SetToSeqSpec(s), R)
} by method {
xs := SetToSeq(s);
xs := MergeSortBy(xs, R);
SortedUnique(xs, SetToSortedSeq(s, R), R);
}


/****************************
** Sorting sequences
***************************** */
Expand Down

0 comments on commit 095d07e

Please sign in to comment.