-
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
8 changed files
with
167 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,3 @@ | ||
{ | ||
"tab_size": 2 | ||
} |
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,29 @@ | ||
method choose_num(x : int, y : int) returns (num : int) | ||
requires 0 <= x && 0 <= y | ||
ensures num == -1 || (num >= x && num <= y) | ||
ensures num == -1 || num % 2 == 0 | ||
ensures num == -1 || (forall i : int :: x <= i <= y && i % 2 == 0 ==> num >= i) | ||
ensures num == -1 <==> x >= y | ||
{ | ||
num := -1; | ||
if x >= y { | ||
return; | ||
} | ||
if x % 2 == 0 { | ||
num := x; | ||
} else { | ||
num := x + 1; | ||
} | ||
var i := x + 2; | ||
while i <= y | ||
invariant i >= x && i <= y + 1 | ||
invariant num == -1 || num % 2 == 0 | ||
invariant forall j : int :: x <= j < i && j % 2 == 0 ==> num >= j | ||
invariant num == -1 || (num >= x && num < i) | ||
{ | ||
if i % 2 == 0 { | ||
num := 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,54 @@ | ||
function factorial_spec(n : int) : int | ||
requires n >= 0 | ||
decreases n | ||
{ | ||
if n == 0 then 1 else n * factorial_spec(n - 1) | ||
} | ||
|
||
function sum_spec(n : int) : int | ||
requires n >= 0 | ||
decreases n | ||
{ | ||
if n == 0 then 1 else n + sum_spec(n - 1) | ||
} | ||
|
||
method f(n : int) returns (result : seq<int>) | ||
requires n >= 1 | ||
ensures |result| == n | ||
ensures forall i : int :: i >= 0 && i < |result| && i % 2 == 0 ==> result[i] == factorial_spec(i) | ||
ensures forall i : int :: i >= 0 && i < |result| && i % 2 != 0 ==> result[i] == sum_spec(i) | ||
{ | ||
result := []; | ||
var i := 0; | ||
while i < n | ||
invariant i >= 0 && i <= n | ||
invariant |result| == i | ||
invariant forall i : int :: i >= 0 && i < |result| && i % 2 == 0 ==> result[i] == factorial_spec(i) | ||
invariant forall i : int :: i >= 0 && i < |result| && i % 2 != 0 ==> result[i] == sum_spec(i) | ||
{ | ||
if i % 2 == 0 { | ||
var x := 1; | ||
var j := 0; | ||
while j < i | ||
invariant j >= 0 && j <= i | ||
invariant x == factorial_spec(j) | ||
{ | ||
j := j + 1; | ||
x := x * j; | ||
} | ||
result := result + [x]; | ||
} else { | ||
var x := 1; | ||
var j := 0; | ||
while j < i | ||
invariant j >= 0 && j <= i | ||
invariant x == sum_spec(j) | ||
{ | ||
j := j + 1; | ||
x := x + j; | ||
} | ||
result := result + [x]; | ||
} | ||
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,23 @@ | ||
method sum_squares(a : seq<int>) returns (result : seq<int>) | ||
ensures |result| == |a| | ||
ensures forall i : int :: i >= 0 && i < |result| && i % 3 == 0 ==> result[i] == a[i] * a[i] | ||
ensures forall i : int :: i >= 0 && i < |result| && i % 4 == 0 && i % 3 != 0 ==> result[i] == a[i] * a[i] * a[i] | ||
{ | ||
result := []; | ||
var i := 0; | ||
while i < |a| | ||
invariant i >= 0 && i <= |a| | ||
invariant |result| == i | ||
invariant forall j : int :: j >= 0 && j < |result| && j % 3 == 0 ==> result[j] == a[j] * a[j] | ||
invariant forall j : int :: j >= 0 && j < |result| && j % 4 == 0 && j % 3 != 0 ==> result[j] == a[j] * a[j] * a[j] | ||
{ | ||
if i % 3 == 0 { | ||
result := result + [a[i] * a[i]]; | ||
} else if i % 4 == 0 && i % 3 != 0 { | ||
result := result + [a[i] * a[i] * a[i]]; | ||
} else { | ||
result := result + [a[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,5 @@ | ||
method right_angle_triangle(a : int, b : int, c : int) returns (result : bool) | ||
ensures result == (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a) | ||
{ | ||
result := a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a; | ||
} |
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,12 @@ | ||
method eat(number : int, need : int, remaining : int) returns (result: seq<int>) | ||
requires number >= 0 && need >= 0 && remaining >= 0 | ||
ensures |result| == 2 | ||
ensures remaining >= need ==> result[0] == number + need && result[1] == remaining - need | ||
ensures remaining < need ==> result[0] == number + remaining && result[1] == 0 | ||
{ | ||
if remaining < need { | ||
result := [number + remaining, 0]; | ||
} else { | ||
result := [number + need, remaining - need]; | ||
} | ||
} |
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,36 @@ | ||
method min(a : int, b : int) returns (m : int) | ||
ensures m == a || m == b | ||
ensures m <= a && m <= b | ||
{ | ||
if a < b { m := a; } else { m := b; } | ||
} | ||
|
||
method max(a : int, b : int) returns (m : int) | ||
ensures m == a || m == b | ||
ensures m >= a && m >= b | ||
{ | ||
if a > b { m := a; } else { m := b; } | ||
} | ||
|
||
method generate_integers(a : int, b : int) returns (result: seq<int>) | ||
ensures forall i : int :: i >= 0 && i < |result| ==> result[i] % 2 == 0 | ||
ensures forall i : int :: i >= 0 && i < |result| ==> result[i] in [2, 4, 6, 8] | ||
{ | ||
var left := min(a, b); | ||
var right := max(a, b); | ||
|
||
var lower := max(2, left); | ||
var upper := min(8, right); | ||
|
||
result := []; | ||
var i := lower; | ||
while i <= upper | ||
invariant forall i : int :: i >= 0 && i < |result| ==> result[i] % 2 == 0 | ||
invariant forall j : int :: j >= 0 && j < |result| ==> result[j] in [2, 4, 6, 8] | ||
{ | ||
if i % 2 == 0 { | ||
result := result + [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