-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
method is_sorted(a: seq<int>) returns (f: bool) | ||
ensures f == forall i, j :: 0 <= i <= j < |a| ==> a[i] <= a[j] && forall i :: 0 <= i < |a| ==> count_set(a, a[i]) <= 2 | ||
{ | ||
if |a| == 0 { | ||
return true; | ||
} | ||
var is_asc := true; | ||
var i := 1; | ||
while i < |a| | ||
invariant 1 <= i <= |a| | ||
invariant is_asc == forall j, k :: 0 <= j < k < i ==> a[j] <= a[k] | ||
{ | ||
if a[i - 1] > a[i] { | ||
is_asc := false; | ||
} | ||
i := i + 1; | ||
} | ||
|
||
if !is_asc { | ||
return false; | ||
} | ||
|
||
i := 0; | ||
|
||
var has_no_more_that_2 := true; | ||
while i < |a| | ||
invariant 0 <= i <= |a| | ||
invariant has_no_more_that_2 == forall j :: 0 <= j < i ==> count_set(a, a[j]) <= 2 | ||
{ | ||
var count := count_set(a, a[i]); | ||
if count > 2 { | ||
has_no_more_that_2 := false; | ||
} | ||
i := i + 1; | ||
} | ||
return has_no_more_that_2; | ||
} | ||
|
||
|
||
method count_sorted(a: seq<int>, x: int, pos: int) returns (count: int) | ||
requires forall i, j :: 0 <= i <= j < |a| ==> a[i] <= a[j] | ||
requires 0 <= pos < |a| | ||
requires a[pos] == x | ||
requires pos == 0 || a[pos - 1] < x | ||
ensures count == count_set(a, x) | ||
{ | ||
count := 0; | ||
var i := pos; | ||
ghost var positions := {}; | ||
|
||
while i < |a| && a[i] == x | ||
invariant 0 <= i <= |a| | ||
invariant positions == set j | 0 <= j < i && a[j] == x | ||
invariant count == |positions| | ||
{ | ||
count := count + 1; | ||
positions := positions + {i}; | ||
i := i + 1; | ||
} | ||
assert positions == set j | 0 <= j < |a| && a[j] == x; | ||
} | ||
|
||
function count_set(a: seq<int>, x: int): int { | ||
|set i | 0 <= i < |a| && a[i] == x| | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
function first_digit(n: int): int | ||
requires n >= 0 | ||
ensures 0 <= first_digit(n) < 10 | ||
{ | ||
if n < 10 then n else first_digit(n / 10) | ||
} | ||
|
||
function last_digit(n: int): int | ||
requires n >= 0 | ||
ensures 0 <= last_digit(n) < 10 | ||
ensures last_digit(n) == n % 10 | ||
{ | ||
n % 10 | ||
} | ||
|
||
method specialFilter(s: seq<int>) returns (r: seq<int>) | ||
ensures forall i :: 0 <= i < |r| ==> r[i] > 10 | ||
ensures forall x :: x in r ==> x in s | ||
ensures forall i :: 0 <= i < |r| ==> first_digit(r[i]) % 2 == 1 && last_digit(r[i]) % 2 == 1 | ||
{ | ||
var i := 0; | ||
r := []; | ||
while i < |s| | ||
invariant 0 <= i <= |s| | ||
invariant forall x :: x in r ==> x in s | ||
invariant forall i :: 0 <= i < |r| ==> r[i] > 10 | ||
invariant forall x :: x in r ==> x in s | ||
invariant forall i :: 0 <= i < |r| ==> first_digit(r[i]) % 2 == 1 && last_digit(r[i]) % 2 == 1 | ||
{ | ||
if s[i] > 10 && last_digit(s[i]) % 2 == 1 && first_digit(s[i]) % 2 == 1 { | ||
r := r + [s[i]]; | ||
} | ||
i := i + 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters