Skip to content

Commit

Permalink
30, 31, 35, 40, 42 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex28sh authored Aug 12, 2024
1 parent eb3202d commit 2d4be46
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 5 deletions.
30 changes: 30 additions & 0 deletions 030-get-positive.dfy
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;
}
}
19 changes: 19 additions & 0 deletions 031-is-prime.dfy
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;
}
}
19 changes: 19 additions & 0 deletions 035-max-element.dfy
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;
}
}
37 changes: 37 additions & 0 deletions 040-triples-sum-to-zero.dfy
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;
}
}
16 changes: 16 additions & 0 deletions 042-incr-list.dfy
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;
}
}
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ Current status:
- [ ] 27. flip_case
- [ ] 28. concatenate
- [ ] 29. filter_by_prefix
- [ ] 30. get_positive
- [ ] 31. is_prime
- [x] 30. get_positive
- [x] 31. is_prime
- [ ] 32. poly
- [ ] 33. sort_third
- [ ] 34. unique
- [ ] 35. max_element
- [x] 35. max_element
- [ ] 36. fizz_buzz
- [ ] 37. sort_even
- [ ] 38. encode_cyclic
- [ ] 39. prime_fib
- [ ] 40. triples_sum_to_zero
- [x] 40. triples_sum_to_zero
- [ ] 41. car_race_collision
- [ ] 42. incr_list
- [x] 42. incr_list
- [ ] 43. pairs_sum_to_zero
- [ ] 44. change_base
- [ ] 45. triangle_area
Expand Down

0 comments on commit 2d4be46

Please sign in to comment.