-
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
5 changed files
with
134 additions
and
4 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,29 @@ | ||
predicate lower(c: char) | ||
{ | ||
'a' <= c <= 'z' | ||
} | ||
|
||
predicate upper(c: char) | ||
{ | ||
'A' <= c <= 'Z' | ||
} | ||
predicate alpha(c: char) | ||
{ | ||
lower(c) || upper(c) | ||
} | ||
|
||
function flip_char(c: char) : (C: char) | ||
ensures lower(c) <==> upper(C) | ||
ensures upper(c) <==> lower(C) | ||
{ | ||
if lower(c) then c - 'a' + 'A' else | ||
if upper(c) then c + 'a' - 'A' else c | ||
} | ||
|
||
function flip_case(s: string) : (S: string) | ||
ensures |S| == |s| | ||
ensures forall i :: 0 <= i < |s| ==> (lower(s[i]) <==> upper(S[i])) | ||
ensures forall i :: 0 <= i < |s| ==> (upper(s[i]) <==> lower(S[i])) | ||
{ | ||
seq(|s|, i requires 0 <= i < |s| => flip_char(s[i])) | ||
} |
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,31 @@ | ||
predicate Prime(p: nat) | ||
{ | ||
p > 1 && | ||
forall k :: 1 < k < p ==> p % k != 0 | ||
} | ||
|
||
method is_multiply_prime(x: nat) returns (ans : bool) | ||
requires x > 1 | ||
ensures ans <==> exists a: nat, b: nat, c: nat :: Prime(a) && Prime(b) && Prime(c) && x == a * b * c | ||
{ | ||
for a := 2 to x | ||
invariant forall i: nat, j: nat, k: nat :: (Prime(i) && Prime(j) && Prime(k) && i < a) ==> x != i * j * k | ||
{ | ||
if Prime(a) { | ||
for b := 2 to x | ||
invariant forall j: nat, k: nat :: (Prime(j) && Prime(k) && j < b) ==> x != a * j * k | ||
{ | ||
if Prime(b) { | ||
for c := 2 to x | ||
invariant forall k: nat :: (Prime(k) && k < c) ==> x != a * b * k | ||
{ | ||
if Prime(c) && x == a * b * c { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} |
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 power(x: nat, y: nat): nat { | ||
if y == 0 then 1 else x * power(x, y-1) | ||
} | ||
|
||
lemma power_unit(y: nat) | ||
ensures power(1, y) == 1 | ||
{} | ||
|
||
lemma power_monotonic(x: nat, y: nat, j: nat) | ||
requires x > 0 | ||
requires j >= y | ||
ensures power(x, j) >= power(x, y) | ||
{} | ||
|
||
method is_simple_power(x: nat, n: int) returns (ans : bool) | ||
requires x > 0 | ||
ensures ans <==> exists y :: n == power(x, y) | ||
{ | ||
if(x == 1) { | ||
assert forall y :: power(x, y) == 1 by { forall y { power_unit(y); } } | ||
assert n == 1 ==> n == power(x, 1); | ||
return n == 1; | ||
} | ||
var acc := 1; | ||
var i := 0; | ||
while(acc < n) | ||
invariant acc == power(x, i) | ||
invariant forall j : nat :: j < i ==> power(x, j) < n | ||
{ | ||
acc := x * acc; | ||
i := i + 1; | ||
} | ||
if(acc == n) { | ||
return true; | ||
} else { | ||
assert forall j : nat :: j >= i ==> power(x, j) > n by { forall j | j > i { power_monotonic(x, i, j); } } | ||
return false; | ||
} | ||
} |
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,31 @@ | ||
function ThreeDistinct(s: string, i: int): bool | ||
requires 0 < i < |s| - 1 | ||
{ | ||
(s[i - 1] != s[i]) && (s[i] != s[i + 1]) && (s[i - 1] != s[i + 1]) | ||
} | ||
|
||
predicate Happy(s: string) | ||
{ | ||
|s| >= 3 && | ||
forall i :: 0 < i < |s| - 1 ==> ThreeDistinct(s, i) | ||
} | ||
|
||
method IsHappy(s: string) returns (happy : bool) | ||
ensures happy <==> Happy(s) | ||
{ | ||
if |s| < 3 { | ||
return false; | ||
} | ||
|
||
var i := 1; | ||
while(i < |s| - 1) | ||
invariant 0 < i <= |s| - 1 | ||
invariant forall j :: 0 < j < i ==> ThreeDistinct(s, j) | ||
{ | ||
if !ThreeDistinct(s, i) { | ||
return false; | ||
} | ||
i := i + 1; | ||
} | ||
return true; | ||
} |
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