Skip to content

Commit

Permalink
Remove predicates, ensure each file has at least one method
Browse files Browse the repository at this point in the history
  • Loading branch information
gt22 committed Dec 16, 2024
1 parent 0887bd1 commit 9f59960
Show file tree
Hide file tree
Showing 30 changed files with 62 additions and 48 deletions.
4 changes: 2 additions & 2 deletions 001-separate-paren-groups.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ function ParenthesesDepth(s: string, i: int, j: int): int
ParenthesesDepth(s, i+1, j)
}

predicate InnerDepthsPositive(s: string)
function InnerDepthsPositive(s: string) : bool
{
forall i :: 0 < i < |s| ==> ParenthesesDepth(s, 0, i) > 0
}

predicate InnerDepthsNonnegative(s: string)
function InnerDepthsNonnegative(s: string) : bool
{
forall i :: 0 < i < |s| ==> ParenthesesDepth(s, 0, i) >= 0
}
Expand Down
4 changes: 2 additions & 2 deletions 002-truncate.dfy
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


function truncate(x : real) : (i : real)
method truncate(x : real) returns (i : real)
// pre-conditions-start
requires x >= 0.0
// pre-conditions-end
Expand All @@ -9,6 +9,6 @@ function truncate(x : real) : (i : real)
// post-conditions-end
{
// impl-start
x.Floor as real
return x.Floor as real;
// impl-end
}
2 changes: 1 addition & 1 deletion 011-string_xor.dfy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
predicate represents_byte(a: char)
function represents_byte(a: char) : bool
{
a in "01"
}
Expand Down
2 changes: 1 addition & 1 deletion 021-rescale_to_unit.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ lemma affine_unit(x: real, shift: real, scale: real)
ensures affine(x, shift, scale) == 1.0 {}


predicate affine_seq(s: seq<real>, r: seq<real>, shift: real, scale: real)
function affine_seq(s: seq<real>, r: seq<real>, shift: real, scale: real) : bool
requires scale > 0.0
requires |r| == |s|
{
Expand Down
4 changes: 2 additions & 2 deletions 023-strlen.dfy
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function strlen(s: string) : (len: int)
method strlen(s: string) returns (len: int)
// post-conditions-start
ensures len == |s|
// post-conditions-end
{
// impl-start
|s|
return |s|;
// impl-end
}
24 changes: 12 additions & 12 deletions 027-flip_case.dfy
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
predicate lower(c: char)
function lower(c: char) : bool
{
'a' <= c <= 'z'
}

predicate upper(c: char)
function upper(c: char) : bool
{
'A' <= c <= 'Z'
}
predicate alpha(c: char)
function alpha(c: char) : bool
{
lower(c) || upper(c)
}
Expand All @@ -20,14 +20,14 @@ function flip_char(c: char) : (C: char)
if upper(c) then c + 'a' - 'A' else c
}

function flip_case(s: string) : (S: string)
// post-conditions-start
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]))
// post-conditions-end
{
method flip_case(s: string) returns (S: string)
// post-conditions-start
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]))
// post-conditions-end
{
// impl-start
seq(|s|, i requires 0 <= i < |s| => flip_char(s[i]))
return seq(|s|, i requires 0 <= i < |s| => flip_char(s[i]));
// impl-end
}
}
2 changes: 1 addition & 1 deletion 059-largest-prime-factor.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ method is_prime(k: int) returns (result: bool)
// impl-end
}

predicate is_prime_pred(k: int)
function is_prime_pred(k: int) : bool
{
forall i :: 2 <= i < k ==> k % i != 0
}
Expand Down
2 changes: 1 addition & 1 deletion 072-will_it_fly.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ method will_it_fly(s: seq<int>, w: int) returns (result: bool)
// impl-end
}

predicate is_palindrome_pred(s : seq<int>) {
function is_palindrome_pred(s : seq<int>) : bool {
forall k :: 0 <= k < |s| ==> s[k] == s[|s| - 1 - k]
}

Expand Down
2 changes: 1 addition & 1 deletion 075-is_multiply_prime.dfy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
predicate Prime(p: nat)
function Prime(p: nat) : bool
{
p > 1 &&
forall k :: 1 < k < p ==> p % k != 0
Expand Down
4 changes: 2 additions & 2 deletions 079-decimal_to_binary.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ type stringBin = s: string |
forall i | 0 <= i < |s| :: s[i] in "01"
witness "1"

function decimal_to_binary(n: nat): (s: string)
method decimal_to_binary(n: nat) returns (s: string)
// post-conditions-start
ensures |s| == |decimal_to_binary_helper(n)| + 4
ensures s[..2] == "db"
Expand All @@ -12,7 +12,7 @@ function decimal_to_binary(n: nat): (s: string)
// post-conditions-end
{
// impl-start
"db" + decimal_to_binary_helper(n) + "db"
return "db" + decimal_to_binary_helper(n) + "db";
// impl-end
}

Expand Down
2 changes: 1 addition & 1 deletion 080-is_happy.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function ThreeDistinct(s: string, i: int): bool
(s[i - 1] != s[i]) && (s[i] != s[i + 1]) && (s[i - 1] != s[i + 1])
}

predicate Happy(s: string)
function Happy(s: string) : bool
{
|s| >= 3 &&
forall i :: 0 < i < |s| - 1 ==> ThreeDistinct(s, i)
Expand Down
8 changes: 4 additions & 4 deletions 082-prime_length.dfy
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
predicate IsPrime(n: int)
function IsPrime(n: int) : bool
{
n > 1 &&
forall k :: 2 <= k < n ==> n % k != 0
}

function PrimeLength(s: string): bool
method PrimeLength(s: string) returns (p : bool)
// post-conditions-start
ensures PrimeLength(s) <==> IsPrime(|s|)
ensures p <==> IsPrime(|s|)
// post-conditions-end
{
// impl-start
IsPrime(|s|)
return IsPrime(|s|);
// impl-end
}
2 changes: 1 addition & 1 deletion 093-encode.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function rot2(c: char): char
(c as int + 2) as char
}

predicate is_vowel(c: char) {
function is_vowel(c: char) : bool {
(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
|| (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
}
2 changes: 1 addition & 1 deletion 094-skjkasdkd.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,6 @@ function max(a: int, b: int): int
if a > b then a else b
}

predicate is_prime(k: int) {
function is_prime(k: int) : bool {
k != 1 && forall i :: 2 <= i < k ==> k % i != 0
}
4 changes: 2 additions & 2 deletions 095-check_dict_case.dfy
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
predicate IsLowerCase(s: string)
function IsLowerCase(s: string) : bool
{
forall i :: 0 <= i < |s| ==> 'a' <= s[i] <= 'z'
}

predicate IsUpperCase(s: string)
function IsUpperCase(s: string) : bool
{
forall i :: 0 <= i < |s| ==> 'A' <= s[i] <= 'Z'
}
Expand Down
2 changes: 1 addition & 1 deletion 096-count_up_to.dfy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
predicate IsPrime(n: int)
function IsPrime(n: int) : bool
{
n > 1 && forall k :: 2 <= k < n ==> n % k != 0
}
Expand Down
2 changes: 1 addition & 1 deletion 098-count_upper.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ method count_upper(s: string) returns (cnt: int)
// impl-end
}

predicate is_upper_vowel(c: char) {
function is_upper_vowel(c: char) : bool {
c == 'A' || c == 'E' || c == 'U' || c == 'I' || c == 'O'
}
2 changes: 1 addition & 1 deletion 104-unique_digits.dfy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
predicate HasNoEvenDigit(n: int)
function HasNoEvenDigit(n: int) : bool
decreases n
{
n >= 0 && ((n < 10 && n % 2 == 1) || (n % 2 == 1 && HasNoEvenDigit(n / 10)))
Expand Down
2 changes: 1 addition & 1 deletion 107-even_odd_palindrome.dfy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
predicate is_palindrome(n: nat) {
function is_palindrome(n: nat) : bool {
var s := natToString(n);
forall i | 0 <= i < |s| :: s[i] == s[|s|-1-i]
}
Expand Down
2 changes: 1 addition & 1 deletion 109-move_one_ball.dfy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
predicate is_sorted(a: seq<int>) {
function is_sorted(a: seq<int>) : bool {
forall i, j :: 0 <= i < j < |a| ==> a[i] <= a[j]
}

Expand Down
2 changes: 1 addition & 1 deletion 110-exchange.dfy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
predicate IsEven(n: int)
function IsEven(n: int) : bool
{
n % 2 == 0
}
Expand Down
2 changes: 1 addition & 1 deletion 112-reverse_delete.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ method check_palindrome(s: string) returns (result: bool)
// impl-end
}

predicate is_palindrome_pred(s : string) {
function is_palindrome_pred(s : string) : bool {
forall k :: 0 <= k < |s| ==> s[k] == s[|s| - 1 - k]
}
4 changes: 2 additions & 2 deletions 118-get_closest_vowel.dfy
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
predicate IsVowel(c: char)
function IsVowel(c: char) : bool
{
c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'
}

predicate IsConsonant(c: char)
function IsConsonant(c: char) : bool
{
('A' <= c <= 'Z' || 'a' <= c <= 'z') && !IsVowel(c)
}
Expand Down
2 changes: 1 addition & 1 deletion 127-intersection.dfy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
predicate IsPrime(n: nat)
function IsPrime(n: nat) : bool
{
n > 1 &&
forall k :: 2 <= k < n ==> n % k != 0
Expand Down
2 changes: 1 addition & 1 deletion 134-check_if_last_char_is_a_letter.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ method check_if_last_char_is_a_letter(s: string) returns (b: bool)
// impl-end
}

predicate is_alpha(c: char) {
function is_alpha(c: char) : bool {
'a' <= c <= 'z' || 'A' <= c <= 'Z'
}
13 changes: 13 additions & 0 deletions 148-bf.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,16 @@ function GetPlanetsBetweenIndices(start: int, end: int): seq<string>
case 7 => ["Neptune"]
// impl-end
}

method bf(start: int, end: int) returns (planets: seq<string>)
// pre-conditions-start
requires 0 <= start <= 7 && 0 <= end <= 7
// pre-conditions-end
// post-conditions-start
ensures |planets| <= (if start <= end then end - start + 1 else 0)
// post-conditions-end
{
// impl-start
return GetPlanetsBetweenIndices(start, end);
// impl-end
}
1 change: 1 addition & 0 deletions 149-sorted_list_sum.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ method sorted_list_sum(list: seq<string>) returns (sorted: seq<string>)
{
if |init[i]| % 2 == 0 {
sorted := sorted + [init[i]];
assert multiset(sorted) <= multiset(init[0..i]);
assert multiset(sorted) <= multiset(init[0..i + 1]); // assert-line
} else {
assert init[0..i + 1] == init[0..i] + [init[i]]; // assert-line
Expand Down
2 changes: 1 addition & 1 deletion 150-x_or_y.dfy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
predicate IsPrime(n: nat)
function IsPrime(n: nat) : bool
{
n > 1 &&
forall k :: 2 <= k < n ==> n % k != 0
Expand Down
2 changes: 1 addition & 1 deletion 154-cycpattern_check.dfy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
predicate IsSubstring(s: string, sub: string)
function IsSubstring(s: string, sub: string) : bool
{
|s| >= |sub| && exists i {:trigger s[i..i+|sub|]} :: 0 <= i <= |s| - |sub| && s[i..i+|sub|] == sub
}
Expand Down
2 changes: 1 addition & 1 deletion 161-solve.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ method reverse(s: string) returns (rev: string)
// impl-end
}

predicate is_alpha(c: char) {
function is_alpha(c: char) : bool {
'a' <= c <= 'z' || 'A' <= c <= 'Z'
}

Expand Down

0 comments on commit 9f59960

Please sign in to comment.