-
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
6 changed files
with
126 additions
and
5 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,30 @@ | ||
|
||
method get_positive(l : seq<int>) returns (result : seq<int>) | ||
ensures forall i : int :: i >= 0 && i < |result| ==> result[i] > 0 | ||
ensures |result| <= |l| | ||
ensures forall i1 :: i1 >= 0 && i1 < |l| ==> (l[i1] > 0 ==> exists i2 :: i2 >= 0 && i2 < |result| && result[i2] == l[i1]) | ||
ensures |result| == 0 || (forall i1 : int :: i1 >= 0 && i1 < |result| ==> (exists i2 : int :: i2 >= 0 && i2 < |l| && l[i2] == result[i1])) | ||
{ | ||
result := []; | ||
var i : int := 0; | ||
while i < |l| | ||
invariant i >= 0 && i <= |l| | ||
invariant |result| <= i | ||
invariant forall i1 : int :: i1 >= 0 && i1 < |result| ==> result[i1] > 0 | ||
invariant i > 0 ==> (l[i - 1] > 0 ==> exists i2 :: i2 >= 0 && i2 < |result| && result[i2] == l[i - 1]) | ||
invariant forall i1 : int :: i1 >= 0 && i1 < old(|result|) ==> old(result[i1]) == result[i1] | ||
invariant forall i1 :: i1 >= 0 && i1 < i ==> (l[i1] > 0 ==> exists i2 :: i2 >= 0 && i2 < |result| && result[i2] == l[i1]) | ||
invariant |result| == 0 || (forall i1 : int :: i1 >= 0 && i1 < |result| ==> (exists i2 : int :: i2 >= 0 && i2 < |l| && l[i2] == result[i1])); | ||
{ | ||
var n : int := l[i]; | ||
if n > 0 { | ||
ghost var res_prev := result; | ||
assert forall i1 :: i1 >= 0 && i1 < i ==> (l[i1] > 0 ==> exists i2 :: i2 >= 0 && i2 < |result| && result[i2] == l[i1]); | ||
result := result + [n]; | ||
assert result[|result| - 1] == n; | ||
assert forall i1 :: i1 >= 0 && i1 < |res_prev| ==> res_prev[i1] == result[i1]; | ||
assert forall i1 :: i1 >= 0 && i1 < i ==> (l[i1] > 0 ==> exists i2 :: i2 >= 0 && i2 < |res_prev| && res_prev[i2] == l[i1]); | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
method is_prime(k: int) returns (result: bool) | ||
requires k >= 2 | ||
ensures result ==> forall i :: 2 <= i < k ==> k % i != 0 | ||
ensures !result ==> exists j :: 2 <= j < k && k % j == 0 | ||
{ | ||
var i := 2; | ||
result := true; | ||
while i < k | ||
invariant 2 <= i <= k | ||
invariant !result ==> exists j :: 2 <= j < i && k % j == 0 | ||
invariant result ==> forall j :: 2 <= j < i ==> k % j != 0 | ||
{ | ||
if k % i == 0 { | ||
result := false; | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
method max_element(l : seq<int>) returns (result : int) | ||
requires |l| > 0 | ||
ensures forall i : int :: i >= 0 && i < |l| ==> l[i] <= result | ||
ensures exists i : int :: i >= 0 && i < |l| && l[i] == result | ||
{ | ||
result := l[0]; | ||
var i : int := 1; | ||
while i < |l| | ||
invariant i >= 1 && i <= |l| | ||
invariant forall i1 : int :: i1 >= 0 && i1 < i ==> l[i1] <= result | ||
invariant exists i1 : int :: i1 >= 0 && i1 < i && l[i1] == result | ||
{ | ||
if l[i] > result { | ||
result := l[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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
method triples_sum_to_zero(l : seq<int>) returns (result : bool) | ||
ensures result ==> exists i : int, j : int, k : int :: 0 <= i < |l| && 0 <= j < |l| && 0 <= k < |l| && i != j && j != k && i != k && l[i] + l[j] + l[k] == 0 | ||
ensures !result ==> forall i : int, j : int, k : int :: 0 <= i < |l| && 0 <= j < |l| && 0 <= k < |l| && i != j && j != k && i != k ==> l[i] + l[j] + l[k] != 0 | ||
{ | ||
result := false; | ||
var i : int := 0; | ||
while i < |l| | ||
invariant i >= 0 && i <= |l| | ||
invariant !result ==> forall i1 : int, j : int, k : int :: 0 <= i1 < i && 0 <= j < |l| && 0 <= k < |l| && i1 != j && j != k && i1 != k ==> l[i1] + l[j] + l[k] != 0 | ||
invariant result ==> exists i1 : int, j : int, k : int :: 0 <= i1 < i && 0 <= j < |l| && 0 <= k < |l| && i1 != j && j != k && i1 != k && l[i1] + l[j] + l[k] == 0 | ||
{ | ||
var j : int := 0; | ||
while j < |l| | ||
invariant j >= 0 && j <= |l| | ||
invariant !result ==> forall i1 : int, j : int, k : int :: 0 <= i1 < i && 0 <= j < |l| && 0 <= k < |l| && i1 != j && j != k && i1 != k ==> l[i1] + l[j] + l[k] != 0 | ||
invariant !result ==> forall j1 : int, k : int :: 0 <= j1 < j && 0 <= k < |l| && i != j1 && j1 != k && i != k ==> l[i] + l[j1] + l[k] != 0 | ||
invariant result ==> (exists i1 : int, j : int, k : int :: 0 <= i1 < i && 0 <= j < |l| && 0 <= k < |l| && i1 != j && j != k && i1 != k && l[i1] + l[j] + l[k] == 0) || (exists j1 : int, k : int :: 0 <= j1 < j && 0 <= k < |l| && i != j1 && j1 != k && i != k && l[i] + l[j1] + l[k] == 0) | ||
{ | ||
var k : int := 0; | ||
while k < |l| | ||
invariant k >= 0 && k <= |l| | ||
invariant !result ==> forall i1 : int, j : int, k : int :: 0 <= i1 < i && 0 <= j < |l| && 0 <= k < |l| && i1 != j && j != k && i1 != k ==> l[i1] + l[j] + l[k] != 0 | ||
invariant !result ==> forall j1 : int, k : int :: 0 <= j1 < j && 0 <= k < |l| && i != j1 && j1 != k && i != k ==> l[i] + l[j1] + l[k] != 0 | ||
invariant !result ==> forall k1 : int :: 0 <= k1 < k && i != j && j != k1 && i != k1 ==> l[i] + l[j] + l[k1] != 0 | ||
invariant result ==> (exists i1 : int, j : int, k : int :: 0 <= i1 < i && 0 <= j < |l| && 0 <= k < |l| && i1 != j && j != k && i1 != k && l[i1] + l[j] + l[k] == 0) || (exists j1 : int, k : int :: 0 <= j1 < j && 0 <= k < |l| && i != j1 && j1 != k && i != k && l[i] + l[j1] + l[k] == 0) || (exists k1 : int :: 0 <= k1 < k && i != j && j != k1 && i != k1 && l[i] + l[j] + l[k1] == 0) | ||
{ | ||
if i != j && j != k && i != k && l[i] + l[j] + l[k] == 0 { | ||
result := true; | ||
} | ||
k := k + 1; | ||
} | ||
j := j + 1; | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
method incr_list(l: seq<int>) returns (result: seq<int>) | ||
ensures |result| == |l| | ||
ensures forall i :: 0 <= i < |l| ==> result[i] == l[i] + 1 | ||
{ | ||
result := []; | ||
var i := 0; | ||
while i < |l| | ||
invariant 0 <= i <= |l| | ||
invariant |result| == i | ||
invariant forall i1 :: 0 <= i1 < i ==> result[i1] == l[i1] + 1 | ||
{ | ||
result := result + [l[i] + 1]; | ||
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