Skip to content

Instantly share code, notes, and snippets.

@hwayne
Last active April 11, 2018 20:31
Show Gist options
  • Save hwayne/92d65e81b4dbf5e08c334d695a7b41fd to your computer and use it in GitHub Desktop.
Save hwayne/92d65e81b4dbf5e08c334d695a7b41fd to your computer and use it in GitHub Desktop.
Dafny
method Unique(a: seq<int>) returns (b: seq<int>)
ensures forall k :: 0 <= k < |a| ==> a[k] in b
ensures forall i, j :: 0 <= i < j < |b| ==> b[i] != b[j]
{
var index := 0;
b := [];
while index < |a|
invariant index <= |a|
invariant forall i, j :: 0 <= i < j < |b| ==> b[i] != b[j]
invariant forall k :: 0 <= k < index ==> a[k] in b
{
if (a[index] !in b) {
b := b + [a[index]];
}
assert a[index] in b;
index := index + 1;
}
return b;
}
//https://rise4fun.com/Dafny/3K7p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment