-
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
7 changed files
with
124 additions
and
6 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,6 @@ | ||
method car_race_collision(n: int) returns (cnt: int) | ||
requires n >= 0 | ||
ensures cnt == n * n | ||
{ | ||
cnt := n * n; | ||
} |
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,6 @@ | ||
method CalculateTriangleArea(a: real, h: real) returns (area: real) | ||
requires h >= 0.0 && a >= 0.0 | ||
ensures area == (h * a) / 2.0 | ||
{ | ||
area := (h * a) / 2.0; | ||
} |
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,39 @@ | ||
function fib4_rec(n: nat): nat | ||
decreases n | ||
{ | ||
if n == 0 || n == 1 || n == 2 then 0 | ||
else if n == 3 then 1 | ||
else fib4_rec(n - 1) + fib4_rec(n - 2) + fib4_rec(n - 3) + fib4_rec(n - 4) | ||
} | ||
|
||
method fib4(n: nat) returns (result: nat) | ||
ensures result == fib4_rec(n) | ||
{ | ||
if n == 0 || n == 1 || n == 2 { | ||
return 0; | ||
} | ||
|
||
if n == 3 { | ||
return 1; | ||
} | ||
|
||
var a, b, c, d := 0, 0, 0, 1; | ||
var i := 4; | ||
|
||
while i <= n | ||
invariant 4 <= i <= n + 1 | ||
invariant a == fib4_rec(i-4) | ||
invariant b == fib4_rec(i-3) | ||
invariant c == fib4_rec(i-2) | ||
invariant d == fib4_rec(i-1) | ||
{ | ||
var temp := d + c + b + a; | ||
a := b; | ||
b := c; | ||
c := d; | ||
d := temp; | ||
i := i + 1; | ||
} | ||
|
||
return d; | ||
} |
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,21 @@ | ||
method can_arrange(arr: seq<int>) returns (pos: int) | ||
requires |arr| > 0 | ||
requires forall i, j :: 0 <= i < j < |arr| ==> arr[i] != arr[j] | ||
ensures pos == -1 ==> forall i :: 1 <= i < |arr| ==> arr[i] >= arr[i - 1] | ||
ensures pos >= 0 ==> 1 <= pos < |arr| && arr[pos] < arr[pos - 1] | ||
ensures pos >= 0 ==> forall i :: pos < i < |arr| ==> arr[i] >= arr[i - 1] | ||
{ | ||
var i := 1; | ||
pos := -1; | ||
while i < |arr| | ||
invariant 1 <= i <= |arr| | ||
invariant pos == -1 ==> forall j :: 1 <= j < i ==> arr[j] >= arr[j - 1] | ||
invariant pos >= 0 ==> 1 <= pos < i && arr[pos] < arr[pos - 1] | ||
invariant pos >= 0 ==> forall j :: pos < j < i ==> arr[j] >= arr[j - 1] | ||
{ | ||
if arr[i] < arr[i - 1] { | ||
pos := 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,41 @@ | ||
datatype Option<T> = None | Some(value: T) | ||
|
||
function get_value(o: Option<int>): int | ||
requires o.Some? | ||
ensures get_value(o) == o.value | ||
{ | ||
o.value | ||
} | ||
|
||
method largest_smallest_integers(arr: seq<int>) returns (a: Option<int>, b: Option<int>) | ||
ensures a.None? ==> forall i :: 0 <= i < |arr| ==> arr[i] >= 0 | ||
ensures a.Some? ==> get_value(a) in arr && get_value(a) < 0 | ||
ensures a.Some? ==> forall i :: 0 <= i < |arr| && arr[i] < 0 ==> arr[i] <= get_value(a) | ||
|
||
ensures b.None? ==> forall i :: 0 <= i < |arr| ==> arr[i] <= 0 | ||
ensures b.Some? ==> get_value(b) in arr && get_value(b) > 0 | ||
ensures b.Some? ==> forall i :: 0 <= i < |arr| && arr[i] > 0 ==> arr[i] >= get_value(b) | ||
{ | ||
var i := 0; | ||
a := None; | ||
b := None; | ||
while i < |arr| | ||
invariant 0 <= i <= |arr| | ||
invariant a.None? ==> forall j :: 0 <= j < i ==> arr[j] >= 0 | ||
invariant a.Some? ==> get_value(a) in arr && get_value(a) < 0 | ||
invariant a.Some? ==> forall j :: 0 <= j < i && arr[j] < 0 ==> arr[j] <= get_value(a) | ||
|
||
invariant b.None? ==> forall j :: 0 <= j < i ==> arr[j] <= 0 | ||
invariant b.Some? ==> get_value(b) in arr && get_value(b) > 0 | ||
invariant b.Some? ==> forall j :: 0 <= j < i && arr[j] > 0 ==> arr[j] >= get_value(b) | ||
{ | ||
if arr[i] < 0 && (a.None? || arr[i] >= get_value(a)) { | ||
a := Some(arr[i]); | ||
} | ||
|
||
if arr[i] > 0 && (b.None? || arr[i] <= get_value(b)) { | ||
b := Some(arr[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 is_equal_to_sum_even(n: int) returns (b : bool) | ||
ensures b <==> n % 2 == 0 && n >= 8 // 2 + 2 + 2 + (n - 6) | ||
{ | ||
b := n % 2 == 0 && n >= 8; | ||
} |
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