From 4fb211e2a3894db91986c3a73ff132cf31991f8b Mon Sep 17 00:00:00 2001 From: alex28sh Date: Wed, 4 Sep 2024 23:58:26 +0200 Subject: [PATCH 1/3] 38, 56, 75, 90 --- Bench/038-encode_cyclic.py | 51 +++++++++++++ Bench/056-correct_bracketing.py | 43 +++++++++++ Bench/075-is_multiply_prime.py | 106 ++++++++++++++++++++++---- Bench/090-next_smallest.py | 45 +++++++++++ README.md | 8 +- WIP/025-factorize.py | 85 +++++++++++++++++++++ WIP/026-remove_duplicates.py | 10 +-- WIP/074-total_match.py | 6 +- WIP/075-is_multiply_prime.py | 131 -------------------------------- WIP/077-is_cube.py | 23 +++++- WIP/130-tri.py | 4 +- WIP/131-digits.py | 40 ++++++++++ WIP/149-sorted_list_sum.py | 24 ++++-- WIP/158-find_max.py | 38 +++++---- 14 files changed, 432 insertions(+), 182 deletions(-) create mode 100644 Bench/038-encode_cyclic.py create mode 100644 Bench/056-correct_bracketing.py create mode 100644 Bench/090-next_smallest.py create mode 100644 WIP/025-factorize.py delete mode 100644 WIP/075-is_multiply_prime.py create mode 100644 WIP/131-digits.py diff --git a/Bench/038-encode_cyclic.py b/Bench/038-encode_cyclic.py new file mode 100644 index 0000000..d73868a --- /dev/null +++ b/Bench/038-encode_cyclic.py @@ -0,0 +1,51 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def encode_cyclic(s: List[int]) -> List[int]: + Requires(Acc(list_pred(s), 1/2)) + Ensures(Acc(list_pred(Result()))) + Ensures(Acc(list_pred(s), 1/2)) + Ensures(len(s) == len(Result())) + Ensures(Forall(int, lambda x: + (Implies(0 <= x and x < len(s) - len(s) % 3, Implies(x % 3 == 0, Result()[x] == s[x + 1]))))) + Ensures(Forall(int, lambda x: + (Implies(0 <= x and x < len(s) - len(s) % 3, Implies(x % 3 == 1, Result()[x] == s[x + 1]))))) + Ensures(Forall(int, lambda x: + (Implies(0 <= x and x < len(s) - len(s) % 3, Implies(x % 3 == 2, Result()[x] == s[x - 2]))))) + Ensures(Forall(int, lambda x: + (Implies(len(s) - len(s) % 3 <= x and x < len(s), Result()[x] == s[x])))) + + res : List[int] = list(s) + i = 0 + while i + 2 < len(s): + Invariant(Acc(list_pred(res))) + Invariant(Acc(list_pred(s),1/2)) + Invariant(0 <= i and i <= len(s)) + Invariant(len(s) == len(res)) + Invariant(i % 3 == 0) + Invariant(Forall(int, lambda x: Implies(i <= x and x < len(s), res[x] == s[x]))) + Invariant(Forall(int, lambda x: + (Implies(0 <= x and x < i, Implies(x % 3 == 0, res[x] == s[x + 1])), [[res[x]]]))) + Invariant(Forall(int, lambda x: + (Implies(0 <= x and x < i, Implies(x % 3 == 1, res[x] == s[x + 1])), [[res[x]]]))) + Invariant(Forall(int, lambda x: + (Implies(0 <= x and x < i, Implies(x % 3 == 2, res[x] == s[x - 2])), [[res[x]]]))) + res[i] = s[i + 1] + res[i + 1] = s[i + 2] + res[i + 2] = s[i] + i = i + 3 + Assert(i == len(s) - len(s) % 3) + return res + +def decode_cyclic(s: List[int]) -> List[int]: + Requires(Acc(list_pred(s))) + Ensures(Acc(list_pred(Result()))) + Ensures(Acc(list_pred(s))) + Ensures(len(s) == len(Result())) + Ensures(Forall(int, lambda x: + (Implies(len(s) - len(s) % 3 <= x and x < len(s), Result()[x] == s[x])))) + Ensures(Forall(int, lambda x: + (Implies(0 <= x and x < len(s) - len(s) % 3, Implies(x % 3 == 0, Result()[x] == s[x + 2]))))) + Ensures(Forall(int, lambda x: + (Implies(0 <= x and x < len(s) - len(s) % 3, Implies(x % 3 == 1, Result()[x] == s[x - 1]))))) + return encode_cyclic(encode_cyclic(s)) \ No newline at end of file diff --git a/Bench/056-correct_bracketing.py b/Bench/056-correct_bracketing.py new file mode 100644 index 0000000..8162dd0 --- /dev/null +++ b/Bench/056-correct_bracketing.py @@ -0,0 +1,43 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def CalBal(s : List[int], i : int, j : int) -> int: + Requires(Acc(list_pred(s), 1/2)) + Requires(0 <= i and i <= j and j <= len(s)) + if i == j: + return 0 + else: + return (1 if s[j - 1] == 0 else -1) + CalBal(s, i, j - 1) + +def correct_bracketing(s : List[int]) -> bool: + Requires(Acc(list_pred(s))) + Requires(Forall(int, lambda d_0_i_: + not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(s)))) or ((((s)[d_0_i_]) == (0)) or (((s)[d_0_i_]) == (1))))) + Ensures(Acc(list_pred(s))) + Ensures(Forall(int, lambda d_0_i_: + not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(s)))) or ((((s)[d_0_i_]) == (0)) or (((s)[d_0_i_]) == (1))))) + Ensures(Implies(Result(), Forall(int, lambda x: (Implies(x >= 0 and x <= len(s), CalBal(s, 0, x) >= 0))))) + Ensures(Implies(Forall(int, lambda x: (Implies(x >= 0 and x <= len(s), CalBal(s, 0, x) >= 0))), Result())) + i = 0 # type : int + depth = 0 # type : int + result = True # type : bool + while i < len(s): + Invariant(Acc(list_pred(s), 1/2)) + Invariant(0 <= i and i <= len(s)) + Invariant(Forall(int, lambda d_0_i_: + not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(s)))) or ((((s)[d_0_i_]) == (0)) or (((s)[d_0_i_]) == (1))))) + Invariant(Forall(int, lambda x : (Implies(x >= 0 and x < len(s), CalBal(s, 0, x + 1) == CalBal(s, 0, x) + (1 if s[x] == 0 else -1)), [[CalBal(s, 0, x + 1)]]))) + Invariant(depth >= 0) + Invariant(depth == CalBal(s, 0, i)) + Invariant(CalBal(s, 0, i) >= 0) + Invariant(Forall(int, lambda x: (Implies(x >= 0 and x <= i, CalBal(s, 0, x) >= 0), [[CalBal(s, 0, x) >= 0]]))) + Assert(CalBal(s, 0, i + 1) == CalBal(s, 0, i) + (1 if s[i] == 0 else -1)) + if s[i] == 0: + depth = depth + 1 + else: + depth = depth - 1 + if depth < 0: + return False + i = i + 1 + return result \ No newline at end of file diff --git a/Bench/075-is_multiply_prime.py b/Bench/075-is_multiply_prime.py index 70d2ca7..75db8c9 100644 --- a/Bench/075-is_multiply_prime.py +++ b/Bench/075-is_multiply_prime.py @@ -31,22 +31,43 @@ def is__prime(k : int) -> bool: def is__multiply__prime(x : int) -> bool: Requires((x) > (1)) Ensures(Implies(Result(), Exists(int, lambda d_1_a_: + d_1_a_< x and + ((Prime(d_1_a_)) and Exists(int, lambda d_2_b_: - (((Prime(d_1_a_)) and (Prime(d_2_b_)))) and ((x) == (((d_1_a_) * (d_2_b_)))))))) + d_2_b_ < x and + (Prime(d_2_b_)) and + Exists(int, lambda d_3_c_: + (d_3_c_ < x) and (Prime(d_3_c_)) and ((x) == (((d_1_a_) * (d_2_b_)) * (d_3_c_))))))))) Ensures(Implies(not(Result()), Forall(int, lambda d_10_i_: - (Forall(int, lambda d_11_j_: - (Implies((1 < d_10_i_ and 1 < d_11_j_ and (d_10_i_) < (x) and d_11_j_ < x), Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), ((x) != (((d_10_i_) * (d_11_j_))))))))))))) + (Implies(1 < d_10_i_ and d_10_i_ < x, + Forall(int, lambda d_11_j_: + (Implies((1 < d_11_j_ and d_11_j_ < x), + (Forall(int, lambda d_12_k_: + (Implies(1 < d_12_k_ and d_12_k_ < x, + Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), Implies(Prime(d_12_k_), ((x) != (((d_10_i_) * (d_11_j_) * (d_12_k_))))))))))))))))))) d_4_a_ = int(2) # type : int result = False # type : bool while d_4_a_ < x: Invariant(x >= 2) Invariant(d_4_a_ >= 2 and d_4_a_ <= x) Invariant(Implies(result, Exists(int, lambda d_1_a_: + d_1_a_< x and + ((Prime(d_1_a_)) and Exists(int, lambda d_2_b_: - (d_1_a_ < x and d_2_b_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_)))) and ((x) == (((d_1_a_) * (d_2_b_)))))))) + d_2_b_ < x and + (Prime(d_2_b_)) and + Exists(int, lambda d_3_c_: + (d_3_c_ < x) and (Prime(d_3_c_)) and ((x) == (((d_1_a_) * (d_2_b_)) * (d_3_c_))))))))) Invariant(Implies(not(result), Forall(int, lambda d_10_i_: - (Forall(int, lambda d_11_j_: - (Implies((1 < d_10_i_ and 1 < d_11_j_ and (d_10_i_) < (d_4_a_) and d_11_j_ < x), Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), ((x) != (((d_10_i_) * (d_11_j_))))))), [[Prime(d_11_j_)]])), [[Prime(d_10_i_)]])))) + (Implies(1 < d_10_i_ and d_10_i_ < d_4_a_, + Forall(int, lambda d_11_j_: + (Implies((1 < d_11_j_ and d_11_j_ < x), + (Forall(int, lambda d_12_k_: + (Implies(1 < d_12_k_ and d_12_k_ < x, + Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), Implies(Prime(d_12_k_), ((x) != (((d_10_i_) * (d_11_j_) * (d_12_k_)))))))), + [[Prime(d_12_k_)]])))), + [[Prime(d_11_j_)]]))), + [[Prime(d_10_i_)]])))) if is__prime(d_4_a_): d_5_b_ = int(2) # type : int while d_5_b_ < x: @@ -55,15 +76,74 @@ def is__multiply__prime(x : int) -> bool: Invariant(d_4_a_ >= 2 and d_4_a_ < x) Invariant(d_5_b_ >= 2 and d_5_b_ <= x) Invariant(Implies(result, Exists(int, lambda d_1_a_: + d_1_a_< x and + ((Prime(d_1_a_)) and Exists(int, lambda d_2_b_: - (d_1_a_ < x and d_2_b_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_)))) and ((x) == (((d_1_a_) * (d_2_b_)))))))) - Invariant(Implies(not(result), Forall(int, lambda d_8_j_: - (Implies(1 < d_8_j_ and d_8_j_ < d_5_b_, Implies(Prime(d_8_j_), ((x) != (((d_4_a_) * (d_8_j_)))))), [[Prime(d_8_j_)]])))) + d_2_b_ < x and + (Prime(d_2_b_)) and + Exists(int, lambda d_3_c_: + (d_3_c_ < x) and (Prime(d_3_c_)) and ((x) == (((d_1_a_) * (d_2_b_)) * (d_3_c_))))))))) Invariant(Implies(not(result), Forall(int, lambda d_10_i_: - (Forall(int, lambda d_11_j_: - (Implies((1 < d_10_i_ and 1 < d_11_j_ and (d_10_i_) < (d_4_a_) and d_11_j_ < x), Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), ((x) != (((d_10_i_) * (d_11_j_))))))), [[Prime(d_11_j_)]])), [[Prime(d_10_i_)]])))) - if is__prime(d_5_b_) and x == d_4_a_ * d_5_b_: - result = True + (Implies(1 < d_10_i_ and d_10_i_ < d_4_a_, + Forall(int, lambda d_11_j_: + (Implies((1 < d_11_j_ and d_11_j_ < x), + (Forall(int, lambda d_12_k_: + (Implies(1 < d_12_k_ and d_12_k_ < x, + Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), Implies(Prime(d_12_k_), ((x) != (((d_10_i_) * (d_11_j_) * (d_12_k_)))))))), + [[Prime(d_12_k_)]])))), + [[Prime(d_11_j_)]]))), + [[Prime(d_10_i_)]])))) + Invariant(Implies(not(result), + Forall(int, lambda d_11_j_: + (Implies((1 < d_11_j_ and d_11_j_ < d_5_b_), + (Forall(int, lambda d_12_k_: + (Implies(1 < d_12_k_ and d_12_k_ < x, + Implies((Prime(d_11_j_)), Implies(Prime(d_12_k_), (x) != (((d_4_a_) * (d_11_j_) * (d_12_k_)))))), + [[Prime(d_12_k_)]])))), + [[Prime(d_11_j_)]])))) + if is__prime(d_5_b_): + d_6_c_ = int(2) # type : int + while d_6_c_ < x: + Invariant(x >= 2) + Invariant(Prime(d_4_a_)) + Invariant(Prime(d_5_b_)) + Invariant(d_4_a_ >= 2 and d_4_a_ < x) + Invariant(d_5_b_ >= 2 and d_5_b_ < x) + Invariant(d_6_c_ >= 2 and d_6_c_ <= x) + Invariant(Implies(result, Exists(int, lambda d_1_a_: + d_1_a_< x and + ((Prime(d_1_a_)) and + Exists(int, lambda d_2_b_: + d_2_b_ < x and + (Prime(d_2_b_)) and + Exists(int, lambda d_3_c_: + (d_3_c_ < x) and (Prime(d_3_c_)) and ((x) == (((d_1_a_) * (d_2_b_)) * (d_3_c_))))))))) + Invariant(Implies(not(result), Forall(int, lambda d_10_i_: + (Implies(1 < d_10_i_ and d_10_i_ < d_4_a_, + Forall(int, lambda d_11_j_: + (Implies((1 < d_11_j_ and d_11_j_ < x), + (Forall(int, lambda d_12_k_: + (Implies(1 < d_12_k_ and d_12_k_ < x, + Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), Implies(Prime(d_12_k_), ((x) != (((d_10_i_) * (d_11_j_) * (d_12_k_)))))))), + [[Prime(d_12_k_)]])))), + [[Prime(d_11_j_)]]))), + [[Prime(d_10_i_)]])))) + Invariant(Implies(not(result), + Forall(int, lambda d_11_j_: + (Implies((1 < d_11_j_ and d_11_j_ < d_5_b_), + (Forall(int, lambda d_12_k_: + (Implies(1 < d_12_k_ and d_12_k_ < x, + Implies((Prime(d_11_j_)), Implies(Prime(d_12_k_), (x) != (((d_4_a_) * (d_11_j_) * (d_12_k_)))))), + [[Prime(d_12_k_)]])))), + [[Prime(d_11_j_)]])))) + Invariant(Implies(not(result), + Forall(int, lambda d_12_k_: + (Implies(1 < d_12_k_ and d_12_k_ < d_6_c_, + Implies((Prime(d_12_k_)), ((x) != (((d_4_a_) * (d_5_b_) * (d_12_k_)))))), + [[Prime(d_12_k_)]])))) + if (is__prime(d_6_c_)) and ((x) == (((d_4_a_) * (d_5_b_)) * (d_6_c_))): + result = True + d_6_c_ = d_6_c_ + 1 d_5_b_ = d_5_b_ + 1 d_4_a_ = d_4_a_ + 1 return result diff --git a/Bench/090-next_smallest.py b/Bench/090-next_smallest.py new file mode 100644 index 0000000..975e95c --- /dev/null +++ b/Bench/090-next_smallest.py @@ -0,0 +1,45 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +def next_smallest(s : List[int]) -> Optional[int]: + Requires(Acc(list_pred(s))) + Ensures(Acc(list_pred(s))) + Ensures((Result() is None) == (len(s) < 2)) + Ensures(Implies(Result() is not None, Exists(int, lambda x: x >= 0 and x < len(s) and s[x] == Result()))) + Ensures(Forall(int, lambda x: + Implies(x >= 0 and x < len(s), + Forall(int, lambda y: + Implies(y > x and y < len(s), + s[x] <= Result() or s[y] <= Result()))))) + + if len(s) < 2: + return None + + res : Optional[int] = None + mx : int = s[0] + i = 1 + while i < len(s): + Invariant(Acc(list_pred(s))) + Invariant(len(s) >= 2) + Invariant(0 <= i and i <= len(s)) + Invariant(Implies(i == 1, res is None)) + Invariant(Implies(res is not None, res <= mx)) + Invariant(Forall(int, lambda x: Implies(x >= 0 and x < i, s[x] <= mx))) + Invariant(Implies(i > 1, res is not None)) + Invariant(Implies(i > 1, Forall(int, lambda x: + Implies(x >= 0 and x < i, s[x] == mx or s[x] <= res)))) + Invariant(Exists(int, lambda x: x >= 0 and x < i and s[x] == mx)) + Invariant(Implies(i > 1, Exists(int, lambda x: x >= 0 and x < i and s[x] == res))) + Invariant(Forall(int, lambda x: + Implies(x >= 0 and x < i, + Forall(int, lambda y: + Implies(y > x and y < i, + s[x] <= res or s[y] <= res))))) + + if s[i] > mx: + res = mx + mx = s[i] + elif res is None or s[i] > res: + res = s[i] + i += 1 + return res diff --git a/README.md b/README.md index b6b775b..1a4e690 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Current status: - [x] 0. has_close_elements - [x] 1. separate_paren_groups -- [x] 2. truncate +- [ ] 2. truncate - [x] 3. below_zero - [ ] 4. mean_absolute_derivation - [x] 5. intersperse @@ -41,7 +41,7 @@ Current status: - [x] 35. max_element - [x] 36. fizz_buzz - [x] 37. sort_even -- [ ] 38. encode_cyclic +- [x] 38. encode_cyclic - [ ] 39. prime_fib - [x] 40. triples_sum_to_zero - [x] 41. car_race_collision @@ -59,7 +59,7 @@ Current status: - [x] 53. add - [x] 54. same_chars - [x] 55. fib -- [ ] 56. correct_bracketing +- [x] 56. correct_bracketing - [x] 57. monotonic - [x] 58. common - [x] 59. largest_prime_factor @@ -93,7 +93,7 @@ Current status: - [ ] 87. get_row - [x] 88. sort_array - [x] 89. encrypt -- [ ] 90. next_smallest +- [x] 90. next_smallest - [ ] 91. is_bored - [x] 92. any_int - [x] 93. encode diff --git a/WIP/025-factorize.py b/WIP/025-factorize.py new file mode 100644 index 0000000..384aa09 --- /dev/null +++ b/WIP/025-factorize.py @@ -0,0 +1,85 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def prod(s : List[int], i : int, j : int) -> int : + Requires(Acc(list_pred(s), 1/2)) + Requires(i >= 0 and i <= len(s)) + Requires(j >= 0 and j <= len(s)) + Requires(i <= j) + if i == j: + return 1 + else: + return s[j - 1] * prod(s, i, j - 1) + +def try_divide(n : int, taken : int, temp : int, pre : int, d_1_cur_ : int, d_2_i_ : int, factors : List[int]) -> Tuple[int, int]: + Requires(d_1_cur_ >= 1 and d_1_cur_ <= n) + Requires(d_2_i_ >= 2) + Requires(temp * d_1_cur_ == pre) + Requires(Acc(list_pred(factors))) + Requires(prod(factors, 0, len(factors)) == taken) + Requires(Forall(int, lambda x : + (Implies(x >= 0 and x < len(factors), prod(factors, 0, x + 1) == factors[x] * prod(factors, 0, x))))) + # Ensures(Acc(list_pred(factors))) + # Ensures(Acc(list_pred(Old(factors)))) + Ensures(Result()[0] >= 1 and Result()[0] <= n) + Ensures(d_2_i_ >= 2) + Ensures(Result()[1] * Result()[0] == pre) + while ((d_1_cur_ % d_2_i_)) == (0): + Invariant(Acc(list_pred(factors))) + Invariant(d_1_cur_ >= 1 and d_1_cur_ <= n) + Invariant(d_2_i_ >= 2) + Invariant(temp * d_1_cur_ == pre) + Invariant(Forall(int, lambda x : + (Implies(x >= 0 and x < len(Old(factors)), factors[x] == Old(factors)[x]), [[factors[x]]]))) + # Invariant(Forall(int, lambda x : + # (Implies(x >= 0 and x < len(factors), prod(factors, 0, x + 1) == factors[x] * prod(factors, 0, x)), [[prod(factors, 0, x + 1)]]))) + # Invariant(prod(Old(factors), 0, len(Old(factors))) == taken) + factors = (factors) + [d_2_i_] + Assert(prod(factors, 0, len(factors)) == factors[len(factors) - 1] * prod(factors, 0, len(factors) - 1)) + d_1_cur_ = (d_1_cur_ // d_2_i_) + temp = (temp) * (d_2_i_) + return (d_1_cur_, temp) + +# def factorize(n : int) -> List[int]: +# Requires((n) > (0)) +# Ensures(Acc(list_pred(Result()))) +# # Ensures((prod(Result(), 0, len(Result()))) == (n)) +# factors : List[int] = [] +# d_1_cur_ = n +# d_2_i_ = 2 +# taken = 1 +# while ((d_2_i_) * (d_2_i_)) <= (d_1_cur_): +# Invariant(Acc(list_pred(factors))) +# Invariant(d_2_i_ >= 2) +# Invariant(d_1_cur_ >= 1 and d_1_cur_ <= n) +# Invariant(taken * d_1_cur_ == n) +# # Invariant(prod(factors, 0, len(factors)) == taken) +# # Invariant(prod(factors, 0, len(factors)) * d_1_cur_ == n) +# # Invariant((prod(factors)) == (d_3_taken_)) +# # Invariant(((d_3_taken_) * (d_1_cur_)) == (n)) +# temp = 1 +# (d_1_cur_, temp) = try_divide(n, temp, n, d_1_cur_, d_2_i_, factors) +# # while ((d_1_cur_ % d_2_i_)) == (0): +# # Invariant(Acc(list_pred(factors))) +# # Invariant(d_1_cur_ >= 1 and d_1_cur_ <= n) +# # Invariant(d_2_i_ >= 2) +# # Invariant(temp * d_1_cur_ == pre) +# # Invariant(prev == Old(prev)) +# # # Invariant(Forall(int, lambda x : Implies(x >= 0 and x < prev, prev_cur[x] == factors[x]))) +# # # Invariant(prod(factors, 0, len(factors)) == taken * temp) +# # # Invariant(prod(factors, 0, len(factors)) * d_1_cur_ == n) +# # # Invariant(((d_4_temp_) * (d_1_cur_)) == (d_5_pre_)) +# # # Invariant((prod(factors)) == ((d_3_taken_) * (d_4_temp_))) +# # # Assume(prod(factors, 0, prev) == taken) +# # factors = (factors) + [d_2_i_] +# # # Assert(prod(factors, 0, prev) == taken) +# # d_1_cur_ = (d_1_cur_ // d_2_i_) +# # temp = (temp) * (d_2_i_) +# # # Assert(((2) <= (d_2_i_)) and (((2) * (d_1_cur_)) <= ((d_2_i_) * (d_1_cur_)))) +# d_2_i_ = (d_2_i_) + (1) +# taken = (taken) * (temp) +# if (d_1_cur_) > (1): +# factors = (factors) + [d_1_cur_] +# # Assert((d_3_taken_) == (n)) +# return factors \ No newline at end of file diff --git a/WIP/026-remove_duplicates.py b/WIP/026-remove_duplicates.py index a210e19..27b240f 100644 --- a/WIP/026-remove_duplicates.py +++ b/WIP/026-remove_duplicates.py @@ -13,8 +13,8 @@ def remove__duplicates(a : List[int]) -> List[int]: Implies(((0) <= (d_2_i_)) and ((d_2_i_) < (len(a))) and (count__rec(a, a[d_2_i_], len(a)) == 1), exists_check(Result(), a[d_2_i_])))) Ensures(Forall(int, lambda d_1_i_: not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(Result())))) or (count_check(a, (Result())[d_1_i_])))) - Ensures(Forall(int, lambda d_2_i_: - not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(a)))) or check_eq(a, Result(), (a)[d_2_i_]))) + # Ensures(Forall(int, lambda d_2_i_: + # not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(a)))) or check_eq(a, Result(), (a)[d_2_i_]))) # Ensures(Forall(int, lambda d_2_i_: # not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(a)))) or ((((a)[d_2_i_]) in (Result())) == ((count__rec(a, (a)[d_2_i_], len(a))) == (1))))) result = list([int(0)] * 0) # type : List[int] @@ -37,9 +37,9 @@ def remove__duplicates(a : List[int]) -> List[int]: Invariant(Forall(int, lambda d_1_i_: (not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(result)))) or (count_check(a, (result)[d_1_i_])), [[]]))) - Invariant(Forall(int, lambda d_2_i_: - (not (((0) <= (d_2_i_)) and ((d_2_i_) < d_4_i_)) or check_eq(a, result, (a)[d_2_i_]), - [[]]))) + # Invariant(Forall(int, lambda d_2_i_: + # (not (((0) <= (d_2_i_)) and ((d_2_i_) < d_4_i_)) or check_eq(a, result, (a)[d_2_i_]), + # [[]]))) # Invariant(Forall(int, lambda d_6_j_: # (Implies(((0) <= (d_6_j_)) and ((d_6_j_) < (d_4_i_)), (((a)[d_6_j_]) in (d_3_res_)) == ((count__rec(a, (a)[d_6_j_], len(a))) == (1))), [[count__rec(a, (a)[d_6_j_], len(a))]]))) # Invariant(Forall(int, lambda d_7_j_: diff --git a/WIP/074-total_match.py b/WIP/074-total_match.py index 4b34501..3d63e91 100644 --- a/WIP/074-total_match.py +++ b/WIP/074-total_match.py @@ -66,13 +66,17 @@ def SumChars(lst : List[List[int]]) -> int: Invariant(Acc(list_pred(lst), 1/2)) Invariant(Forall(lst, lambda x: Acc(list_pred(x), 1/2))) Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(lst)))) + Invariant(Forall(int, lambda d_0_i_: + (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len(lst))), + sum__chars__rec(0, d_0_i_ + 1, lst) == (sum__chars__rec(0, d_0_i_, lst) + get_len(lst[d_0_i_]))), + [[]]))) # Invariant(Forall(int, lambda d_0_i_: # (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len(lst))), # progress(lst, d_0_i_)), # [[]]))) # Invariant((sum) == (sum__chars__rec(0, d_3_i_, lst))) # Assert(sum__chars__rec(0, d_3_i_ + 1, lst) == sum__chars__rec(0, d_3_i_, lst) + len(lst[d_3_i_])) - Assert(progress(lst, d_3_i_)) + # Assert(progress(lst, d_3_i_)) sum = (sum) + (get_len((lst)[d_3_i_])) d_3_i_ = (d_3_i_) + (1) return sum diff --git a/WIP/075-is_multiply_prime.py b/WIP/075-is_multiply_prime.py deleted file mode 100644 index 816b959..0000000 --- a/WIP/075-is_multiply_prime.py +++ /dev/null @@ -1,131 +0,0 @@ -from typing import cast, List, Dict, Set, Optional, Union, Tuple -from nagini_contracts.contracts import * - -@Pure -def Prime(p : int) -> bool : - return ((p) > (1)) and (Forall(int, lambda d_0_k_: - not (((1) < (d_0_k_)) and ((d_0_k_) < (p))) or (((p % d_0_k_)) != (0)))) - -def is__prime(k : int) -> bool: - Requires((k) >= (2)) - Ensures(not (Result()) or (Forall(int, lambda d_0_i_: - not (((2) <= (d_0_i_)) and ((d_0_i_) < (k))) or ((k % d_0_i_) != (0))))) - Ensures(not (not(Result())) or (Exists(int, lambda d_1_j_: - (((2) <= (d_1_j_)) and ((d_1_j_) < (k))) and (((k % d_1_j_)) == (0))))) - Ensures(Result() == Prime(k)) - result = False # type : bool - d_2_i_ = int(0) # type : int - d_2_i_ = 2 - result = True - while (d_2_i_) < (k): - Invariant(((2) <= (d_2_i_)) and ((d_2_i_) <= (k))) - Invariant(not (not(result)) or (Exists(int, lambda d_3_j_: - (((2) <= (d_3_j_)) and ((d_3_j_) < (d_2_i_))) and (((k % d_3_j_)) == (0))))) - Invariant(not (result) or (Forall(int, lambda d_4_j_: - not (((2) <= (d_4_j_)) and ((d_4_j_) < (d_2_i_))) or (((k % d_4_j_)) != (0))))) - if ((k % d_2_i_)) == (0): - result = False - d_2_i_ = (d_2_i_) + (1) - return result - -# def is__multiply__prime(x : int) -> bool: -# Requires((x) > (1)) -# # Ensures(Implies(Result(), Exists(int, lambda d_1_a_: -# # Exists(int, lambda d_2_b_: -# # Exists(int, lambda d_3_c_: -# # (d_1_a_< x and d_2_b_ < x and d_3_c_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_))) and (Prime(d_3_c_))) and ((x) == (((d_1_a_) * (d_2_b_)) * (d_3_c_)))))))) -# d_4_a_ = int(2) # type : int -# result = False # type : bool -# while d_4_a_ < x: -# Invariant(x >= 2) -# Invariant(d_4_a_ >= 2 and d_4_a_ <= x) -# # Invariant(Implies(result, Exists(int, lambda d_1_a_: -# # Exists(int, lambda d_2_b_: -# # Exists(int, lambda d_3_c_: -# # (d_1_a_< x and d_2_b_ < x and d_3_c_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_))) and (Prime(d_3_c_))) and ((x) == (((d_1_a_) * (d_2_b_)) * (d_3_c_)))))))) -# if is__prime(d_4_a_): -# d_5_b_ = int(2) # type : int -# while d_5_b_ < x: -# Invariant(x >= 2) -# Invariant(Prime(d_4_a_)) -# Invariant(d_4_a_ >= 2 and d_4_a_ < x) -# Invariant(d_5_b_ >= 2 and d_5_b_ <= x) -# # Invariant(Implies(result, Exists(int, lambda d_1_a_: -# # Exists(int, lambda d_2_b_: -# # Exists(int, lambda d_3_c_: -# # (d_1_a_< x and d_2_b_ < x and d_3_c_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_))) and (Prime(d_3_c_))) and ((x) == (((d_1_a_) * (d_2_b_)) * (d_3_c_)))))))) -# # Assume(Implies(result, Exists(int, lambda d_1_a_: -# # Exists(int, lambda d_2_b_: -# # Exists(int, lambda d_3_c_: -# # (d_1_a_< x and d_2_b_ < x and d_3_c_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_))) and (Prime(d_3_c_))) and ((x) == (((d_1_a_) * (d_2_b_)) * (d_3_c_)))))))) -# if is__prime(d_5_b_): -# d_6_c_ = int(2) # type : int -# while d_6_c_ < x: -# Invariant(x >= 2) -# Invariant(Prime(d_4_a_)) -# Invariant(Prime(d_5_b_)) -# Invariant(d_4_a_ >= 2 and d_4_a_ < x) -# Invariant(d_5_b_ >= 2 and d_5_b_ < x) -# Invariant(d_6_c_ >= 2 and d_6_c_ <= x) -# # Invariant(Implies(result, Exists(int, lambda d_1_a_: -# # Exists(int, lambda d_2_b_: -# # Exists(int, lambda d_3_c_: -# # (d_1_a_< x and d_2_b_ < x and d_3_c_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_))) and (Prime(d_3_c_))) and ((x) == (((d_1_a_) * (d_2_b_)) * (d_3_c_)))))))) -# if (is__prime(d_6_c_)) and ((x) == (((d_4_a_) * (d_5_b_)) * (d_6_c_))): -# result = True -# Assert(d_4_a_ < x and d_5_b_ < x and d_6_c_ < x and Prime(d_4_a_) and Prime(d_5_b_) and Prime(d_6_c_) and x == d_4_a_ * d_5_b_ * d_6_c_) -# Assert(Exists(int, lambda d_3_c_: -# d_4_a_ < x and d_5_b_ < x and d_3_c_ < x and Prime(d_4_a_) and Prime(d_5_b_) and Prime(d_3_c_) and x == d_4_a_ * d_5_b_ * d_3_c_)) -# Assert(Exists(int, lambda d_2_b_: -# Exists(int, lambda d_3_c_: -# (d_4_a_< x and d_2_b_ < x and d_3_c_ < x and ((Prime(d_4_a_)) and (Prime(d_2_b_))) and (Prime(d_3_c_))) and ((x) == (((d_4_a_) * (d_2_b_)) * (d_3_c_)))))) -# # Assert(d_4_a_ < x and Prime(d_4_a_)) -# Assert(d_4_a_ < x and Prime(d_4_a_) and Exists(int, lambda d_1_a_: -# Exists(int, lambda d_2_b_: -# Exists(int, lambda d_3_c_: -# (d_1_a_< x and d_2_b_ < x and d_3_c_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_))) and (Prime(d_3_c_))) and ((x) == (((d_1_a_) * (d_2_b_)) * (d_3_c_))))))) -# d_6_c_ = d_6_c_ + 1 -# d_5_b_ = d_5_b_ + 1 -# d_4_a_ = d_4_a_ + 1 -# return result - - -def is__multiply__prime(x : int) -> bool: - Requires((x) > (1)) - Ensures(Implies(Result(), Exists(int, lambda d_1_a_: - Exists(int, lambda d_2_b_: - (((Prime(d_1_a_)) and (Prime(d_2_b_)))) and ((x) == (((d_1_a_) * (d_2_b_)))))))) - Ensures(Implies(not(Result()), Forall(int, lambda d_10_i_: - (Forall(int, lambda d_11_j_: - (Implies((1 < d_10_i_ and 1 < d_11_j_ and (d_10_i_) < (x) and d_11_j_ < x), Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), ((x) != (((d_10_i_) * (d_11_j_))))))))))))) - d_4_a_ = int(2) # type : int - result = False # type : bool - while d_4_a_ < x: - Invariant(x >= 2) - Invariant(d_4_a_ >= 2 and d_4_a_ <= x) - Invariant(Implies(result, Exists(int, lambda d_1_a_: - Exists(int, lambda d_2_b_: - (d_1_a_ < x and d_2_b_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_)))) and ((x) == (((d_1_a_) * (d_2_b_)))))))) - Invariant(Implies(not(result), Forall(int, lambda d_10_i_: - (Forall(int, lambda d_11_j_: - (Implies((1 < d_10_i_ and 1 < d_11_j_ and (d_10_i_) < (d_4_a_) and d_11_j_ < x), Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), ((x) != (((d_10_i_) * (d_11_j_))))))), [[Prime(d_11_j_)]])), [[Prime(d_10_i_)]])))) - if is__prime(d_4_a_): - d_5_b_ = int(2) # type : int - while d_5_b_ < x: - Invariant(x >= 2) - Invariant(Prime(d_4_a_)) - Invariant(d_4_a_ >= 2 and d_4_a_ < x) - Invariant(d_5_b_ >= 2 and d_5_b_ <= x) - Invariant(Implies(result, Exists(int, lambda d_1_a_: - Exists(int, lambda d_2_b_: - (d_1_a_ < x and d_2_b_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_)))) and ((x) == (((d_1_a_) * (d_2_b_)))))))) - Invariant(Implies(not(result), Forall(int, lambda d_8_j_: - (Implies(1 < d_8_j_ and d_8_j_ < d_5_b_, Implies(Prime(d_8_j_), ((x) != (((d_4_a_) * (d_8_j_)))))), [[Prime(d_8_j_)]])))) - Invariant(Implies(not(result), Forall(int, lambda d_10_i_: - (Forall(int, lambda d_11_j_: - (Implies((1 < d_10_i_ and 1 < d_11_j_ and (d_10_i_) < (d_4_a_) and d_11_j_ < x), Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), ((x) != (((d_10_i_) * (d_11_j_))))))), [[Prime(d_11_j_)]])), [[Prime(d_10_i_)]])))) - if is__prime(d_5_b_) and x == d_4_a_ * d_5_b_: - result = True - d_5_b_ = d_5_b_ + 1 - d_4_a_ = d_4_a_ + 1 - return result diff --git a/WIP/077-is_cube.py b/WIP/077-is_cube.py index 7ea670a..39447ab 100644 --- a/WIP/077-is_cube.py +++ b/WIP/077-is_cube.py @@ -20,13 +20,30 @@ def cube__root(N : int) -> int: Invariant((cube(r)) <= (N)) Invariant(Forall(int, lambda x: (Implies(0 <= x and x <= N, cube_less(x, x + 1)), [[cube_less(x, x + 1)]]))) Invariant(Forall(int, lambda x: (Implies(0 <= x and x < r, cube_less(x, r)), [[cube_less(x, r)]]))) - Invariant(cube_less(r, r + 1)) - # Invariant(Forall(int, lambda x: (Implies(r < x and x <= N, cube_less(r, x)), [[]]))) - # Invariant(Forall(int, lambda x: Forall(int, lambda y : (Implies(0 <= x and x < y, cube(x) < cube(y)), [[cube(x) < cube(y)]])))) + # Invariant(Forall(int, lambda x: + # (Implies(0 <= x and x < r, cube(x) < N)), [[cube(x)]])) + # Invariant(Forall(int, lambda x: (Implies(0 <= r and r < x and x <= N, cube_less(r, x)), [[cube_less(r, x)]]))) + # Invariant(cube_less(r, r + 1)) + # Invariant(Forall(int, lambda x: + # (Implies(0 <= x and x <= N, + # Forall(int, lambda y : + # (Implies(x < y and y <= N, + # cube_less(x, y)), [[cube(y)]]))), + # [[cube(x)]]))) + Invariant(Forall(int, lambda x: + (Implies(0 <= x and x <= N, + cubes_less(x, N)), + [[cubes_less(x, N)]]))) # Invariant(Forall(int, lambda d_0_r_: (Implies(0 <= d_0_r_ and d_0_r_ < r, cube(d_0_r_) < N), [[cube(d_0_r_)]]))) + # Assert(cube_less(r, r + 1)) r = (r) + (1) return r +@Pure +def cubes_less(x : int, N : int) -> bool: + Requires(0 <= x and x <= N) + return Forall(int, lambda y : (Implies(x < y and y <= N, cube_less(x, y)), [[cube_less(x, y)]])) + @Pure def cube_less(a : int, b : int) -> bool: Requires(a >= 0) diff --git a/WIP/130-tri.py b/WIP/130-tri.py index 625b5fe..2b77195 100644 --- a/WIP/130-tri.py +++ b/WIP/130-tri.py @@ -39,8 +39,8 @@ def Tribonacci(n : int) -> List[int]: Invariant(Forall(int, lambda x: (Implies(2 <= x and x <= n, tri(x) == ((1 + (x // 2)) if x % 2 == 0 else tri(x - 1) + tri(x - 2) + (x + 3) // 2)), [[tri(x)]]))) Invariant(Forall(int, lambda x: (Implies(2 <= x and x <= n and x % 2 == 0, tri(x) == (1 + (x // 2))), [[tri(x)]]))) Invariant(Forall(int, lambda x: (Implies(2 <= x and x <= n and x % 2 == 1, tri(x) == (tri(x - 2) + tri(x - 1) + ((x + 3) // 2))), [[tri(x)]]))) - # Invariant(result[(d_1_i_) - (2)] == tri(d_1_i_ - 2)) - # Invariant(result[(d_1_i_) - (1)] == tri(d_1_i_ - 1)) + Invariant(result[(d_1_i_) - (2)] == tri(d_1_i_ - 2)) + Invariant(result[(d_1_i_) - (1)] == tri(d_1_i_ - 1)) # Invariant(Forall(int, lambda d_2_j_: # (Implies(((0) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_)), ((result)[d_2_j_]) == (tri(d_2_j_))), [[tri(d_2_j_)]]))) # , [[(result)[d_2_j_]]] # Assert(Forall(int, lambda d_2_j_: diff --git a/WIP/131-digits.py b/WIP/131-digits.py new file mode 100644 index 0000000..0936df5 --- /dev/null +++ b/WIP/131-digits.py @@ -0,0 +1,40 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + + +@Pure +def count_odd(n : int) -> int: + Requires((n) >= (0)) + if n == 0: + return 0 + else: + return (1 if (n % 10) % 2 == 1 else 0) + count_odd(n // 10) + +def digits(n : int) -> int: + Requires((n) >= (0)) + Ensures(Result() >= 0) + # Ensures((Result() == 0) == (count_odd(n) == 0)) + odd_count = 0 + product = 1 + n_copy = n + s1 = 0 + pw1 = 1 + while n > 0: + Invariant(n >= 0) + Invariant(odd_count >= 0) + Invariant(product >= 1) + Invariant(pw1 >= 1) + Invariant(s1 >= 0) + Invariant(odd_count == count_odd(s1)) + if n % 2 == 1: + odd_count += 1 + product = product * (n % 10) + Assert(count_odd(s1 + (n % 10) * pw1) == (1 if (n % 10) % 2 == 1 else 0) + count_odd(s1)) + s1 = s1 + (n % 10) * pw1 + pw1 = pw1 * 10 + # Assert(count_odd(n_copy, n // 10) == (1 if n % 2 == 1 else 0) + count_odd(n_copy, n)) + n //= 10 + # Assert(odd_count == count_odd(n_copy)) + if odd_count == 0: + return 0 + return product \ No newline at end of file diff --git a/WIP/149-sorted_list_sum.py b/WIP/149-sorted_list_sum.py index 37780cd..41d258d 100644 --- a/WIP/149-sorted_list_sum.py +++ b/WIP/149-sorted_list_sum.py @@ -70,6 +70,11 @@ def comparison(a : List[int], b : List[int], i : int) -> bool : # d_0_i_ = (d_0_i_) + (1) # return sorted +@Pure +def check_len(lst : List[int]) -> bool: + Requires(Acc(list_pred(lst), 1/2)) + return len(lst) % 2 == 0 + def sort__lengths(lst : List[List[int]]) -> List[List[int]]: Requires(Acc(list_pred(lst))) Requires(Forall(lst, lambda x: Acc(list_pred(x)))) @@ -78,7 +83,7 @@ def sort__lengths(lst : List[List[int]]) -> List[List[int]]: Ensures(Acc(list_pred(lst))) Ensures(Acc(list_pred(Result()))) Ensures(Forall(lst, lambda x: Acc(list_pred(x)))) - Ensures(Forall(ResultT(List[List[int]]), lambda x: Acc(list_pred(x)))) + Ensures(Forall(ResultT(List[List[int]]), lambda x: Acc(list_pred(x),1/2))) # Ensures(Forall(int, lambda d_5_i_: # not (((0) <= (d_5_i_)) and ((d_5_i_) < (len(Result())))) or (((len((Result())[d_5_i_]) % 2)) == (0)))) Ensures((len(Result())) == (len(lst))) @@ -97,9 +102,9 @@ def sort__lengths(lst : List[List[int]]) -> List[List[int]]: Invariant(((0) <= (d_0_i_)) and ((d_0_i_) <= (len(lst)))) Invariant((len(sorted)) == (len(lst))) Invariant(Forall(int, lambda d_4_i_: - not (((0) <= (d_4_i_)) and ((d_4_i_) < (len(lst)))) or (((len((lst)[d_4_i_]) % 2)) == (0)))) + not (((0) <= (d_4_i_)) and ((d_4_i_) < (len(lst)))) or (((check_len((lst)[d_4_i_])))))) Invariant(Forall(int, lambda d_4_i_: - not (((0) <= (d_4_i_)) and ((d_4_i_) < (d_0_i_))) or (((len((sorted)[d_4_i_]) % 2)) == (0)))) + not (((0) <= (d_4_i_)) and ((d_4_i_) < (d_0_i_))) or (((check_len((sorted)[d_4_i_])))))) sorted[d_0_i_] = list(lst[d_0_i_]) d_8_i_ = int(0) # type : int d_8_i_ = 0 @@ -108,9 +113,9 @@ def sort__lengths(lst : List[List[int]]) -> List[List[int]]: Invariant(Acc(list_pred(lst))) Invariant(len(sorted) == len(lst)) Invariant(Forall(lst, lambda x: Acc(list_pred(x)))) - Invariant(Forall(sorted, lambda x: Acc(list_pred(x)))) + Invariant(Forall(sorted, lambda x: Acc(list_pred(x),1/2))) Invariant(Forall(int, lambda d_4_i_: - (not (((0) <= (d_4_i_)) and ((d_4_i_) < (len(sorted)))) or (((len((sorted)[d_4_i_]) % 2)) == (0))))) + (not (((0) <= (d_4_i_)) and ((d_4_i_) < (len(sorted)))) or (((check_len((sorted)[d_4_i_])))), [[]]))) Invariant(((0) <= (d_8_i_)) and ((d_8_i_) <= (len(lst)))) Invariant((len(sorted)) == (len(lst))) # Invariant(Forall(int, lambda d_9_x_: @@ -123,14 +128,16 @@ def sort__lengths(lst : List[List[int]]) -> List[List[int]]: d_14_j_ = (d_8_i_) + (1) d_15_min_ = int(0) # type : int d_15_min_ = d_8_i_ + Assert(Forall(int, lambda d_4_i_: + (not (((0) <= (d_4_i_)) and ((d_4_i_) < (len(sorted)))) or (((check_len((sorted)[d_4_i_])))), [[]]))) while (d_14_j_) < (len(lst)): Invariant(Acc(list_pred(sorted))) Invariant(Acc(list_pred(lst))) Invariant(len(sorted) == len(lst)) Invariant(Forall(lst, lambda x: Acc(list_pred(x)))) - Invariant(Forall(sorted, lambda x: Acc(list_pred(x)))) + Invariant(Forall(sorted, lambda x: Acc(list_pred(x),1/2))) Invariant(Forall(int, lambda d_4_i_: - (not (((0) <= (d_4_i_)) and ((d_4_i_) < (len(sorted)))) or (((len((sorted)[d_4_i_]) % 2)) == (0))))) + (not (((0) <= (d_4_i_)) and ((d_4_i_) < (len(sorted)))) or (((check_len((sorted)[d_4_i_])))), [[]]))) Invariant((len(sorted)) == (len(lst))) Invariant(((0) <= (d_0_i_)) and ((d_8_i_) < (len(lst)))) Invariant((((0) <= (d_8_i_)) and ((d_8_i_) < (d_14_j_))) and ((d_14_j_) <= (len(lst)))) @@ -141,8 +148,11 @@ def sort__lengths(lst : List[List[int]]) -> List[List[int]]: d_15_min_ = d_14_j_ d_14_j_ = (d_14_j_) + (1) d_17_temp_ : List[int] = list((sorted)[d_8_i_]) + Assert(check_len(d_17_temp_)) sorted[d_8_i_] = list(sorted[d_15_min_]) + Assert(check_len(sorted[d_8_i_])) sorted[d_15_min_] = list(d_17_temp_) + Assert(check_len(sorted[d_15_min_])) d_8_i_ = (d_8_i_) + (1) return sorted diff --git a/WIP/158-find_max.py b/WIP/158-find_max.py index 1a26da4..501a71e 100644 --- a/WIP/158-find_max.py +++ b/WIP/158-find_max.py @@ -7,19 +7,19 @@ def withinRange(x : int) -> bool: @Pure def withinRangeString(s : List[int]) -> bool: - Requires(Acc(list_pred(s), 1/2)) + Requires(Acc(list_pred(s), 1/8)) return Forall(int, lambda x: Implies(x >= 0 and x < len(s), withinRange(s[x]))) @Pure def EqArrays(a : List[int], x : List[int]) -> bool : - Requires(Acc(list_pred(a), 1/2)) - Requires(Acc(list_pred(x), 1/2)) + Requires(Acc(list_pred(a), 1/16)) + Requires(Acc(list_pred(x), 1/16)) return len(a) == len(x) and Forall(int, lambda d_0_i_: (Implies(0 <= d_0_i_ and d_0_i_ < len(a), (a)[d_0_i_] == x[d_0_i_]), [[a[d_0_i_] == x[d_0_i_]]])) @Pure def contains_char(s : List[int], c : int, i : int, j : int) -> bool: - Requires(Acc(list_pred(s), 1/2)) + Requires(Acc(list_pred(s), 1/8)) Requires(withinRangeString(s)) Requires(0 <= i and i <= j and j <= len(s)) Requires(withinRange(c)) @@ -30,7 +30,7 @@ def contains_char(s : List[int], c : int, i : int, j : int) -> bool: @Pure def count_chars_inter(s : List[int], c : int) -> int: - Requires(Acc(list_pred(s), 1/2)) + Requires(Acc(list_pred(s), 1/8)) Requires(withinRangeString(s)) Requires(((97) <= (c)) and ((c) <= (123))) if c == 97: @@ -40,16 +40,16 @@ def count_chars_inter(s : List[int], c : int) -> int: @Pure def compare_strings(s1 : List[int], s2 : List[int]) -> bool: - Requires(Acc(list_pred(s1), 1/2)) - Requires(Acc(list_pred(s2), 1/2)) + Requires(Acc(list_pred(s1), 1/4)) + Requires(Acc(list_pred(s2), 1/4)) Requires(withinRangeString(s2)) Requires(withinRangeString(s1)) return count_chars_inter(s1, 123) <= count_chars_inter(s2, 123) @Pure def contains_char_compare(s1 : List[int], s2 : List[int], x : int) -> bool: - Requires(Acc(list_pred(s1), 1/2)) - Requires(Acc(list_pred(s2), 1/2)) + Requires(Acc(list_pred(s1), 1/4)) + Requires(Acc(list_pred(s2), 1/4)) Requires(withinRangeString(s1)) Requires(withinRangeString(s2)) Requires(withinRange(x)) @@ -58,25 +58,25 @@ def contains_char_compare(s1 : List[int], s2 : List[int], x : int) -> bool: return contains_char(s1, x, 0, len(s1)) == contains_char(s2, x, 0, len(s2)) def find__max(strings : List[List[int]]) -> List[int]: - Requires(Acc(list_pred(strings), 1/2)) - Requires(Forall(strings, lambda x : Acc(list_pred(x), 1/2))) + Requires(Acc(list_pred(strings))) + Requires(Forall(strings, lambda x : Acc(list_pred(x)))) Requires((len(strings)) > (0)) # Requires(Forall(int, lambda y: # Implies(y >= 0 and y < len(strings), Forall(int, lambda x: # Implies(x >= 0 and x < len(strings[y]), strings[y][x] >= 97 and strings[y][x] <= 122))))) Requires(Forall(strings, lambda y: withinRangeString(y))) - Ensures(Acc(list_pred(strings), 1/2)) - Ensures(Forall(strings, lambda x : Acc(list_pred(x), 1/2))) + Ensures(Acc(list_pred(strings))) + Ensures(Forall(strings, lambda x : Acc(list_pred(x)))) Ensures(Forall(strings, lambda y: withinRangeString(y))) # Ensures(Forall(int, lambda y: # Implies(y >= 0 and y < len(strings), Forall(int, lambda x: # Implies(x >= 0 and x < len(strings[y]), strings[y][x] >= 97 and strings[y][x] <= 122))))) - Ensures(Acc(list_pred(Result()), 1/2)) + Ensures(Acc(list_pred(Result()))) # Ensures(Exists(strings, lambda x: EqArrays(x, Result()))) Ensures(withinRangeString(Result())) # Ensures(Forall(int, lambda x: # Implies(x >= 0 and x < len(strings), count_chars_inter(Result(), 123) >= count_chars_inter(strings[x], 123)))) - s : List[int] = list((strings)[0]) + s : List[int] = (strings)[0] d_3_i_ = int(0) # type : int d_3_i_ = 1 # Assert(EqArrays(strings[0], s)) @@ -84,7 +84,13 @@ def find__max(strings : List[List[int]]) -> List[int]: # Assert(Exists(strings, lambda x: EqArrays(x, s))) Assert(withinRangeString(s)) Assert(Forall(int, lambda x: Implies(x >= 0 and x < len(s), s[x] == strings[0][x])) and len(s) == len(strings[0])) - Assert(contains_char_compare(s, s, 100)) + # Assert(Forall(int, lambda x: + # Implies(x >= 0 and x < len(s), + # Implies(s[x] == strings[0][x] and contains_char(s, 100, x + 1, len(s)) == contains_char(strings[0], 100, x + 1, len(strings[0])), + # contains_char(s, 100, x, len(s)) == contains_char(strings[0], 100, x, len(strings[0])))))) + # Assert(contains_char(strings[0], 100, 0, len(strings[0])) == contains_char(s, 100, 0, len(s))) + Assert(Acc(list_pred(s))) + Assert(contains_char_compare(strings[0], s, 100)) # Assert(Forall(int, lambda x: (Implies(withinRange(x), contains_char_compare(strings[0], s, x)), [[contains_char_compare(strings[0], s, x)]]))) # Assert(Forall(int, lambda x: (Implies(withinRange(x), contains_char_compare(strings[0], s, x)), [[contains_char_compare(strings[0], s, x)]]))) # Assert(count_chars_inter(strings[0], 123) == count_chars_inter(s, 123)) From 0862d525d3bdd9984737ae361e27ce06cc0d1534 Mon Sep 17 00:00:00 2001 From: alex28sh Date: Thu, 5 Sep 2024 18:12:40 +0200 Subject: [PATCH 2/3] 119, 132 --- Bench/119-match_parens.py | 83 +++++++++++++++++++++++++++ Bench/132-is_nested.py | 117 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 200 insertions(+) create mode 100644 Bench/119-match_parens.py create mode 100644 Bench/132-is_nested.py diff --git a/Bench/119-match_parens.py b/Bench/119-match_parens.py new file mode 100644 index 0000000..7c7c60a --- /dev/null +++ b/Bench/119-match_parens.py @@ -0,0 +1,83 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def CalBal(s : List[int], i : int, j : int, acc : int) -> int: + Requires(Acc(list_pred(s), 1/2)) + Requires(0 <= i and i <= j and j <= len(s)) + if i == j: + return acc + else: + return (1 if s[j - 1] == 0 else -1) + CalBal(s, i, j - 1, acc) + +def checkFixed(s1 : List[int], s2 : List[int]) -> bool: + Requires(Acc(list_pred(s1))) + Requires(Forall(int, lambda d_0_i_: + not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(s1)))) or ((((s1)[d_0_i_]) == (0)) or (((s1)[d_0_i_]) == (1))))) + Requires(Acc(list_pred(s2))) + Requires(Forall(int, lambda d_0_i_: + not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(s2)))) or ((((s2)[d_0_i_]) == (0)) or (((s2)[d_0_i_]) == (1))))) + + Ensures(Acc(list_pred(s1))) + Ensures(Forall(int, lambda d_0_i_: + not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(s1)))) or ((((s1)[d_0_i_]) == (0)) or (((s1)[d_0_i_]) == (1))))) + Ensures(Acc(list_pred(s2))) + Ensures(Forall(int, lambda d_0_i_: + not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(s2)))) or ((((s2)[d_0_i_]) == (0)) or (((s2)[d_0_i_]) == (1))))) + + Ensures(Implies(Result(), Forall(int, lambda x: (Implies(x >= 0 and x <= len(s1), CalBal(s1, 0, x, 0) >= 0))))) + Ensures(Implies(Result(), Forall(int, lambda x: (Implies(x >= 0 and x <= len(s2), CalBal(s1, 0, len(s1), 0) + CalBal(s2, 0, x, 0) >= 0))))) + Ensures(Implies(not(Result()), + Exists(int, lambda x: x >= 0 and x <= len(s1) and CalBal(s1, 0, x, 0) < 0) or + Exists(int, lambda x: x >= 0 and x <= len(s2) and CalBal(s1, 0, len(s1), 0) + CalBal(s2, 0, x, 0) < 0))) + + bal = 0 # type : int + i = 0 # type : int + + while i < len(s1): + Invariant(Acc(list_pred(s1), 1/2)) + Invariant(0 <= i and i <= len(s1)) + Invariant(Forall(int, lambda d_0_i_: + not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(s1)))) or ((((s1)[d_0_i_]) == (0)) or (((s1)[d_0_i_]) == (1))))) + Invariant(Forall(int, lambda x : (Implies(x >= 0 and x < len(s1), CalBal(s1, 0, x + 1, 0) == CalBal(s1, 0, x, 0) + (1 if s1[x] == 0 else -1)), [[CalBal(s1, 0, x + 1, 0)]]))) + Invariant(bal == CalBal(s1, 0, i, 0)) + Invariant(bal >= 0) + Invariant(Forall(int, lambda x: (Implies(x >= 0 and x <= i, CalBal(s1, 0, x, 0) >= 0), [[CalBal(s1, 0, x, 0) >= 0]]))) + + Assert(CalBal(s1, 0, i + 1, 0) == CalBal(s1, 0, i, 0) + (1 if s1[i] == 0 else -1)) + if s1[i] == 0: + bal = bal + 1 + else: + bal = bal - 1 + + if bal < 0: + Assert(Exists(int, lambda x: x >= 0 and x <= len(s1) and CalBal(s1, 0, x, 0) < 0)) + return False + i = i + 1 + + i = 0 + while i < len(s2): + Invariant(Acc(list_pred(s1), 1/2)) + Invariant(Acc(list_pred(s2), 1/2)) + Invariant(0 <= i and i <= len(s2)) + Invariant(Forall(int, lambda d_0_i_: + not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(s2)))) or ((((s2)[d_0_i_]) == (0)) or (((s2)[d_0_i_]) == (1))))) + Invariant(Forall(int, lambda x: (Implies(x >= 0 and x < len(s1), CalBal(s1, 0, x, 0) >= 0), [[CalBal(s1, 0, x, 0) >= 0]]))) + Invariant(Forall(int, lambda x : (Implies(x >= 0 and x < len(s2), CalBal(s2, 0, x + 1, 0) == CalBal(s2, 0, x, 0) + (1 if s2[x] == 0 else -1)), [[CalBal(s2, 0, x + 1, 0)]]))) + Invariant(bal == CalBal(s1, 0, len(s1), 0) + CalBal(s2, 0, i, 0)) + Invariant(bal >= 0) + Invariant(Forall(int, lambda x: (Implies(x >= 0 and x <= len(s1), CalBal(s1, 0, x, 0) >= 0), [[CalBal(s1, 0, x, 0) >= 0]]))) + Invariant(Forall(int, lambda x: (Implies(x >= 0 and x <= i, CalBal(s1, 0, len(s1), 0) + CalBal(s2, 0, x, 0) >= 0), [[CalBal(s1, 0, len(s1), 0) + CalBal(s2, 0, x, 0) >= 0]]))) + + if s2[i] == 0: + bal = bal + 1 + else: + bal = bal - 1 + + if bal < 0: + Assert(CalBal(s1, 0, len(s1), 0) + CalBal(s2, 0, i + 1, 0) < 0) + Assert(Exists(int, lambda x: x >= 0 and x <= len(s2) and CalBal(s1, 0, len(s1), 0) + CalBal(s2, 0, x, 0) < 0)) + return False + i = i + 1 + + return True \ No newline at end of file diff --git a/Bench/132-is_nested.py b/Bench/132-is_nested.py new file mode 100644 index 0000000..6e87c93 --- /dev/null +++ b/Bench/132-is_nested.py @@ -0,0 +1,117 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def is_nested(s : List[int]) -> bool: + Requires(Acc(list_pred(s))) + Ensures(Acc(list_pred(s))) + Ensures(Implies(Result(), Exists(int, lambda x: + x >= 0 and x < len(s) and s[x] == 0 and + Exists(int, lambda y: + y > x and y < len(s) and s[y] == 0 and + Exists(int, lambda z: + z > y and z < len(s) and s[z] == 1 and + Exists(int, lambda w: + w > z and w < len(s) and s[w] == 1)))))) + Ensures(Implies(Exists(int, lambda x: + x >= 0 and x < len(s) and s[x] == 0 and + Exists(int, lambda y: + y > x and y < len(s) and s[y] == 0 and + Exists(int, lambda z: + z > y and z < len(s) and s[z] == 1 and + Exists(int, lambda w: + w > z and w < len(s) and s[w] == 1)))), Result())) + + bal = 0 + i = 0 + while i < len(s) and bal < 2: + Invariant(Acc(list_pred(s))) + Invariant(0 <= i and i <= len(s)) + Invariant(0 <= bal and bal <= 2) + Invariant(Implies(bal == 1, Exists(int, lambda x: x >= 0 and x < i and s[x] == 0))) + Invariant(Implies(Exists(int, lambda x: x >= 0 and x < i and s[x] == 0), bal >= 1)) + Invariant(Implies(bal == 2, Exists(int, lambda x: + x >= 0 and x < i and s[x] == 0 and + Exists(int, lambda y: + y > x and y < i and s[y] == 0)))) + Invariant(Implies(Exists(int, lambda x: + x >= 0 and x < i and s[x] == 0 and + Exists(int, lambda y: + y > x and y < i and s[y] == 0)), bal == 2)) + Invariant(not(Exists(int, lambda x: + x >= 0 and x < i and s[x] == 0 and + Exists(int, lambda y: + y > x and y < i and s[y] == 0 and + Exists(int, lambda z: + z > y and z < i and s[z] == 1))))) + if s[i] == 0: + bal = bal + 1 + i = i + 1 + + if bal < 2: + Assert(not(Exists(int, lambda x: + x >= 0 and x < i and s[x] == 0 and + Exists(int, lambda y: + y > x and y < i and s[y] == 0)))) + return False + + + while i < len(s) and bal > 0: + Invariant(Acc(list_pred(s))) + Invariant(0 <= i and i <= len(s)) + Invariant(0 <= bal and bal <= 2) + Invariant(Exists(int, lambda x: + x >= 0 and x < i and s[x] == 0 and + Exists(int, lambda y: + y > x and y < i and s[y] == 0))) + Invariant(Implies(bal <= 1, Exists(int, lambda x: + x >= 0 and x < i and s[x] == 0 and + Exists(int, lambda y: + y > x and y < i and s[y] == 0 and + Exists(int, lambda z: + z > y and z < i and s[z] == 1))))) + Invariant(Implies(bal == 0, Exists(int, lambda x: + x >= 0 and x < i and s[x] == 0 and + Exists(int, lambda y: + y > x and y < i and s[y] == 0 and + Exists(int, lambda z: + z > y and z < i and s[z] == 1 and + Exists(int, lambda w: + w > z and w < i and s[w] == 1)))))) + Invariant(Implies(Exists(int, lambda x: + x >= 0 and x < i and s[x] == 0 and + Exists(int, lambda y: + y > x and y < i and s[y] == 0 and + Exists(int, lambda z: + z > y and z < i and s[z] == 1))), bal <= 1)) + Invariant(Implies(Exists(int, lambda x: + x >= 0 and x < i and s[x] == 0 and + Exists(int, lambda y: + y > x and y < i and s[y] == 0 and + Exists(int, lambda z: + z > y and z < i and s[z] == 1 and + Exists(int, lambda w: + w > z and w < i and s[w] == 1)))), bal == 0)) + if s[i] == 1: + bal = bal - 1 + Assert(Exists(int, lambda x: + x >= 0 and x < i and s[x] == 0 and + Exists(int, lambda y: + y > x and y < i and s[y] == 0 and + Exists(int, lambda z: + z > y and z <= i and s[z] == 1)))) + Assert(bal <= 1) + i = i + 1 + + if bal > 0: + Assert(not(Exists(int, lambda x: + x >= 0 and x < i and s[x] == 0 and + Exists(int, lambda y: + y > x and y < i and s[y] == 0 and + Exists(int, lambda z: + z > y and z < i and s[z] == 1 and + Exists(int, lambda w: + w > z and w < i and s[w] == 1)))))) + return False + + return True + From 1e2ad08ed8fbd9fa59f28b7007bc7c47707729cc Mon Sep 17 00:00:00 2001 From: alex28sh Date: Mon, 9 Sep 2024 11:17:37 +0200 Subject: [PATCH 3/3] markup --- Bench/000-has-close-elements.py | 25 +++++++++-- Bench/001-separate-paren-groups.py | 24 +++++++++++ Bench/003-below-zero.py | 20 +++++++++ Bench/005-intersperse.py | 12 +++++- Bench/006-parse_nested_parens.py | 43 +++++++++++++++++-- Bench/007-filter_by_substring.py | 34 ++++++++++++++- Bench/008-sum-product.py | 26 +++++++++++- Bench/009-rolling-max.py | 10 ++++- Bench/010-is_palindrome.py | 37 +++++++++++++++++ Bench/011-string_xor.py | 16 ++++++- Bench/012-longest.py | 18 +++++++- Bench/013-greatest-common-divisor.py | 10 ++++- Bench/016-count_distinct_characters.py | 21 +++++++++- Bench/020-find-closest-elements.py | 22 ++++++++++ Bench/023-strlen.py | 7 +++- Bench/024-largest-divisor.py | 11 ++++- Bench/026-remove_duplicates.py | 26 ++++++++++++ Bench/027-flip_case.py | 20 +++++++++ Bench/029-filter_by_prefix.py | 25 ++++++++++- Bench/030-get-positive.py | 13 ++++++ Bench/031-is-prime.py | 11 ++++- Bench/033-sort_third.py | 24 ++++++++++- Bench/034-unique.py | 29 ++++--------- Bench/035-max-element.py | 9 ++++ Bench/036-fizz_buzz.py | 32 ++++++++++++++ Bench/037-sort_even.py | 24 ++++++++++- Bench/038-encode_cyclic.py | 19 ++++++++- Bench/040-triples-sum-to-zero.py | 15 ++++++- Bench/041-car_race_collision.py | 7 ++++ Bench/042-incr-list.py | 9 ++++ Bench/043-pairs-sum-to-zero.py | 13 +++++- Bench/046-fib4.py | 14 +++++++ Bench/048-is-palindrome.py | 9 ++++ Bench/049-modp.py | 16 +++++++ Bench/050-encode_shift.py | 22 ++++++++++ Bench/051-remove-vowels.py | 9 ++++ Bench/052-below-threshold.py | 9 ++++ Bench/053-add.py | 7 +++- Bench/054-same-chars.py | 9 +++- Bench/055-fib.py | 18 +++++++- Bench/056-correct_bracketing.py | 18 +++++++- Bench/057-monotonic.py | 11 ++++- Bench/058-common.py | 24 ++++++++++- Bench/059-largest-prime-factor.py | 22 +++++++++- Bench/060-sum-to-n.py | 16 ++++++- Bench/062-derivative.py | 9 ++++ Bench/063-fibfib.py | 16 +++++++ Bench/064-vowel_count.py | 20 +++++++++ Bench/066-digitSum.py | 14 +++++++ Bench/068-pluck.py | 9 ++++ Bench/069-search.py | 16 +++++++ Bench/070-strange_sort_list.py | 29 ++++++++++++- Bench/072-will_it_fly.py | 24 +++++++++++ Bench/073-smallest_change.py | 21 +++++++++- Bench/075-is_multiply_prime.py | 24 +++++++++++ Bench/078-hex_key.py | 19 +++++++++ Bench/080-is_happy.py | 19 +++++++++ Bench/082-prime-length.py | 11 ++++- Bench/083-starts_one_ends.py | 14 ++++++- Bench/084-solve.py | 14 +++++++ Bench/085-add.py | 14 +++++++ Bench/088-sort_array.py | 38 ++++++++++++++++- Bench/089-encrypt.py | 16 +++++++ Bench/090-next_smallest.py | 8 ++++ Bench/092-any_int.py | 7 +++- Bench/093-encode.py | 17 +++++++- Bench/096-count_up_to.py | 11 +++++ Bench/097-multiply.py | 9 ++++ Bench/098-count_upper.py | 21 ++++++++++ Bench/100-make_a_pile.py | 9 ++++ Bench/102-choose_num.py | 9 ++++ Bench/104-unique_digits.py | 27 ++++++++++++ Bench/105-by_length.py | 46 ++++++++++++++++++++- Bench/106-f.py | 27 ++++++++++++ Bench/108-count_nums.py | 25 ++++++++++- Bench/109-move_one_ball.py | 18 ++++++++ Bench/110-exchange.py | 19 +++++++++ Bench/112-reverse_delete.py | 38 +++++++++++++++++ Bench/114-minSubArraySum.py | 17 ++++++++ Bench/116-sort_array.py | 19 ++++++++- Bench/118-get_closest_vowel.py | 13 ++++++ Bench/119-match_parens.py | 27 ++++++++++-- Bench/121-solution.py | 14 +++++++ Bench/122-add_elements.py | 18 +++++++- Bench/123-get_odd_collatz.py | 44 ++++++++++++++++++++ Bench/126-is_sorted.py | 15 +++++++ Bench/127-intersection.py | 13 ++++++ Bench/128-prod_signs.py | 26 ++++++++++++ Bench/132-is_nested.py | 16 +++++++ Bench/133-sum-squares.py | 14 +++++++ Bench/134-check_if_last_char_is_a_letter.py | 9 ++++ Bench/135_can_arrange.py | 9 ++++ Bench/136-largest_smallest_integers.py | 14 +++++++ Bench/138_is_equal_to_sum_even.py | 5 +++ Bench/139-special_factorial.py | 23 +++++++++++ Bench/142-sum_squares.py | 16 +++++++ Bench/146_specialFilter.py | 18 ++++++++ Bench/150-x_or_y.py | 10 +++++ Bench/151-double_the_difference.py | 18 +++++++- Bench/152-compare.py | 13 +++++- Bench/154-cycpattern_check.py | 16 ++++++- Bench/155_even_odd_count.py | 19 +++++++++ Bench/157-right_angle_triangle.py | 5 +++ Bench/159-eat.py | 9 +++- Bench/161-solve.py | 22 ++++++++++ Bench/163-generate_integers.py | 19 ++++++++- README.md | 6 +-- 107 files changed, 1824 insertions(+), 77 deletions(-) diff --git a/Bench/000-has-close-elements.py b/Bench/000-has-close-elements.py index 741af5e..c7e459b 100644 --- a/Bench/000-has-close-elements.py +++ b/Bench/000-has-close-elements.py @@ -4,48 +4,65 @@ @Pure def abs_value(val: int) -> int: + # post-conditions-start Ensures(Implies(val < 0, Result() == -val)) Ensures(Implies(val >= 0, Result() == val)) + # post-conditions-end + # impl-start if val < 0: return -val else: return val + # impl-end @Pure def abs1(x: int, threshold: int) -> bool: + # impl-start return x >= threshold or x <= -threshold + # impl-end @Pure def fn(x: int, numbers: List[int], threshold: int) -> bool: + # pre-conditions-start Requires(threshold > 0) Requires(Acc(list_pred(numbers))) Requires(x >= 0 and x < len(numbers)) + # pre-conditions-end + + # impl-start return Forall(range(len(numbers)), lambda y : x == y or abs1(numbers[x] - numbers[y], threshold) ) + # impl-end def has_close_elements(numbers: List[int], threshold: int) -> bool: + # pre-conditions-start Requires(threshold > 0) Requires(Acc(list_pred(numbers))) + # pre-conditions-end + # post-conditions-start Ensures(Implies(Result() != True, Forall(range(len(numbers)), lambda x : fn(x, numbers, threshold) ))) + # post-conditions-end - + # impl-start flag = False i = 0 while i < len(numbers): + # invariants-start Invariant(Acc(list_pred(numbers))) Invariant(0 <= i and i <= len(numbers)) Invariant(Implies(flag != True, Forall(range(i), lambda x : fn(x, numbers, threshold)))) - + # invariants-end j = 0 while j < len(numbers): + # invariants-start Invariant(Acc(list_pred(numbers))) Invariant(0 <= i and i < len(numbers)) Invariant(0 <= j and j <= len(numbers)) @@ -57,6 +74,7 @@ def has_close_elements(numbers: List[int], threshold: int) -> bool: Forall(range(j), lambda y : i == y or abs1(numbers[i] - numbers[y], threshold)) ) ) + # invariants-end if i != j: distance = abs_value(numbers[i] - numbers[j]) @@ -66,4 +84,5 @@ def has_close_elements(numbers: List[int], threshold: int) -> bool: j += 1 i += 1 - return flag \ No newline at end of file + return flag + # impl-end \ No newline at end of file diff --git a/Bench/001-separate-paren-groups.py b/Bench/001-separate-paren-groups.py index 7d2c9d4..02f53fb 100644 --- a/Bench/001-separate-paren-groups.py +++ b/Bench/001-separate-paren-groups.py @@ -3,8 +3,12 @@ @Pure def IsValidParentheses(s : List[int], i : int, depth : int) -> bool : + # pre-conditions-start Requires(Acc(list_pred(s), 1/2)) Requires(i >= 0 and i <= len(s)) + # pre-conditions-end + + # impl-start if (i) == (len(s)): return (depth) >= (0) elif (depth) < (0): @@ -15,11 +19,16 @@ def IsValidParentheses(s : List[int], i : int, depth : int) -> bool : return ((depth) > (0)) and (IsValidParentheses(s, (i) + (1), (depth) - (1))) elif True: return IsValidParentheses(s, (i) + (1), depth) + # impl-end @Pure def IsValidParentheses2(s : List[int], i : int, depth : int) -> bool : + # pre-conditions-start Requires(Acc(list_pred(s), 1/2)) Requires(i >= 0 and i <= len(s)) + # pre-conditions-end + + # impl-start if (i) == (len(s)): return (depth) >= (0) elif (depth) < (0): @@ -30,11 +39,16 @@ def IsValidParentheses2(s : List[int], i : int, depth : int) -> bool : return ((depth) > (0)) and (IsValidParentheses(s, (i) + (1), (depth) - (1))) elif True: return IsValidParentheses(s, (i) + (1), depth) + # impl-end @Pure def IsValidParentheses1(s : List[int], i : int, depth : int) -> bool : + # pre-conditions-start Requires(Acc(list_pred(s), 1/2)) Requires(i >= 0 and i <= len(s)) + # pre-conditions-end + + # impl-start if (i) == (len(s)): return (depth) == (0) elif ((depth) <= (0)) and ((i) != (0)): @@ -45,23 +59,31 @@ def IsValidParentheses1(s : List[int], i : int, depth : int) -> bool : return ((depth) > (0)) and (IsValidParentheses1(s, (i) + (1), (depth) - (1))) elif True: return IsValidParentheses1(s, (i) + (1), depth) + # impl-end def separate__paren__groups(paren__string : List[int]) -> List[List[int]]: + # pre-conditions-start Requires(Acc(list_pred(paren__string))) Requires(Forall(int, lambda d_0_i_: not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(paren__string)))) or ((((paren__string)[d_0_i_]) == (0)) or (((paren__string)[d_0_i_]) == (1))))) Requires(Forall(int, lambda d_1_i_: not (((0) <= (d_1_i_)) and ((d_1_i_) <= (len(paren__string)))) or (IsValidParentheses(paren__string, d_1_i_, 0)))) Requires(IsValidParentheses2(paren__string, 0, 0)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(ResultT(List[List[int]])))) Ensures(Forall(ResultT(List[List[int]]), lambda x: Acc(list_pred(x), 1/2))) Ensures((Forall(int, lambda d_2_i_: not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(Result())))) or (IsValidParentheses1((Result())[d_2_i_], 0, 0))))) + # post-conditions-end + + # impl-start res : List[List[int]] = [] d_3_current__string_ : List[int] = [] d_4_current__depth_ = int(0) # type : int d_5_i_ = int(0) # type : int while (d_5_i_) < (len(paren__string)): + # invariants-start Invariant(Acc(list_pred(res))) Invariant(Acc(list_pred(d_3_current__string_))) Invariant(Acc(list_pred(paren__string))) @@ -74,6 +96,7 @@ def separate__paren__groups(paren__string : List[int]) -> List[List[int]]: Invariant((Forall(int, lambda d_6_i1_: not (((0) <= (d_6_i1_)) and ((d_6_i1_) < (len(res)))) or (IsValidParentheses1((res)[d_6_i1_], 0, 0))))) Invariant(IsValidParentheses(paren__string, d_5_i_, 0)) + # invariants-end d_7_c_ = (paren__string)[d_5_i_] if (d_7_c_) == (0): d_4_current__depth_ = (d_4_current__depth_) + (1) @@ -86,4 +109,5 @@ def separate__paren__groups(paren__string : List[int]) -> List[List[int]]: d_3_current__string_ = [] d_5_i_ = (d_5_i_) + (1) return res + # impl-end diff --git a/Bench/003-below-zero.py b/Bench/003-below-zero.py index 4df47be..86c3c40 100644 --- a/Bench/003-below-zero.py +++ b/Bench/003-below-zero.py @@ -3,37 +3,57 @@ @Pure def psum(i : int, j : int, s : List[int]) -> int : + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(0 <= i and i <= j and j <= len(s)) + # pre-conditions-end + + # impl-start if i == j: return 0 else: return (s)[j - 1] + (psum(i, j - 1, s)) + # impl-end def below__zero(ops : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(ops))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(ops))) Ensures(not (Result()) or (Forall(int, lambda d_1_i_: not (((0) <= (d_1_i_)) and ((d_1_i_) <= (len(ops)))) or ((psum(0, d_1_i_, ops)) >= (0))))) Ensures(not (not(Result())) or (Exists(int, lambda d_2_i_: (((0) <= (d_2_i_)) and ((d_2_i_) <= (len(ops)))) and ((psum(0, d_2_i_, ops)) < (0))))) + # post-conditions-end + + # impl-start res = False # type : bool d_3_balance_ = int(0) # type : int d_3_balance_ = 0 d_4_i_ = int(0) # type : int d_4_i_ = 0 + while (d_4_i_) < (len(ops)): + # invariants-start Invariant(Acc(list_pred(ops))) Invariant(((0) <= (d_4_i_)) and ((d_4_i_) <= (len(ops)))) Invariant((d_3_balance_) == (psum(0, d_4_i_, ops))) Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(ops)))) or ((psum(0, d_2_i_ + 1, ops)) == (psum(0, d_2_i_, ops) + ops[d_2_i_])), [[psum(0, d_2_i_ + 1, ops)]]))) Invariant(Forall(int, lambda d_5_j_: (not (((0) <= (d_5_j_)) and ((d_5_j_) <= (d_4_i_))) or ((psum(0, d_5_j_, ops)) >= (0)), [[psum(0, d_5_j_, ops)]]))) + # invariants-end + + # assert-start Assert((psum(0, (d_4_i_) + (1), ops)) == ((psum(0, d_4_i_, ops)) + ((ops)[d_4_i_]))) + # assert-end + d_3_balance_ = (d_3_balance_) + ((ops)[d_4_i_]) if (d_3_balance_) < (0): res = False return res d_4_i_ = (d_4_i_) + (1) + res = True return res + # impl-end \ No newline at end of file diff --git a/Bench/005-intersperse.py b/Bench/005-intersperse.py index 8e08e4b..5245a10 100644 --- a/Bench/005-intersperse.py +++ b/Bench/005-intersperse.py @@ -2,24 +2,31 @@ from nagini_contracts.contracts import * def intersperse(numbers: List[int], delimiter: int) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(numbers))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(numbers))) Ensures(Acc(list_pred(Result()))) Ensures(Implies(len(numbers) != 0, len(Result()) == len(numbers) * 2 - 1)) Ensures(Implies(len(numbers) == 0, len(Result()) == 0)) Ensures(Forall(range(len(Result())), lambda i: i % 2 == 1 or Result()[i] == numbers[i // 2])) Ensures(Forall(range(len(Result())), lambda i: i % 2 == 0 or Result()[i] == delimiter)) - + # post-conditions-end + + # impl-start res = [] # type: List[int] if len(numbers) != 0: i = 0 while i + 1 < len(numbers): + # invariants-start Invariant(Acc(list_pred(numbers))) Invariant(Acc(list_pred(res))) Invariant(0 <= i and i < len(numbers)) Invariant(len(res) == 2 * i) Invariant(Forall(range(len(res)), lambda i: i % 2 == 1 or res[i] == numbers[i // 2])) Invariant(Forall(range(len(res)), lambda i: i % 2 == 0 or res[i] == delimiter)) + # invariants-end res.append(numbers[i]) res.append(delimiter) @@ -27,4 +34,5 @@ def intersperse(numbers: List[int], delimiter: int) -> List[int]: res.append(numbers[i]) - return res \ No newline at end of file + return res + # impl-end \ No newline at end of file diff --git a/Bench/006-parse_nested_parens.py b/Bench/006-parse_nested_parens.py index 678ba4b..19de28f 100644 --- a/Bench/006-parse_nested_parens.py +++ b/Bench/006-parse_nested_parens.py @@ -2,13 +2,19 @@ from nagini_contracts.contracts import * def parseparengroup(s : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(s), 1/2)) Requires(contains12(s)) Requires(get_len(s)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s), 1/2)) Ensures(contains12(s)) Ensures(get_len(s)) Ensures((Result()) >= (0)) + # post-conditions-end + + # impl-start max__depth = int(0) # type : int d_1_depth_ = int(0) # type : int d_1_depth_ = 0 @@ -16,11 +22,13 @@ def parseparengroup(s : List[int]) -> int: d_2_i_ = int(0) # type : int d_2_i_ = 0 while (d_2_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(s), 1/2)) Invariant(((d_2_i_) >= (0)) and ((d_2_i_) <= (len(s)))) Invariant(max__depth >= 0) Invariant(contains12(s)) Invariant(get_len(s)) + # invariants-end d_3_c_ = (s)[d_2_i_] # type : int if (d_3_c_) == (1): d_1_depth_ = (d_1_depth_) + (1) @@ -30,22 +38,36 @@ def parseparengroup(s : List[int]) -> int: d_1_depth_ = (d_1_depth_) - (1) d_2_i_ = (d_2_i_) + (1) return max__depth + # impl-end @Pure def get_len(s : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(s), 1/2)) + # pre-conditions-end + + # impl-start return len(s) > 0 + # impl-end @Pure def contains12(s : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(s), 1/2)) + # pre-conditions-end + + # impl-start return Forall(int, lambda d_0_i_: Implies(d_0_i_ >= 0 and d_0_i_ < len(s), s[d_0_i_] == 1 or s[d_0_i_] == 2)) + # impl-end def split(s : List[int]) -> List[List[int]]: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(Forall(int, lambda d_4_i_: not (((d_4_i_) >= (0)) and ((d_4_i_) < (len(s)))) or (((((s)[d_4_i_]) == (1)) or (((s)[d_4_i_]) == (2))) or (((s)[d_4_i_]) == (3))))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(ResultT(List[List[int]])))) Ensures(Forall(ResultT(List[List[int]]), lambda x: Acc(list_pred(x), 1/2))) Ensures(Forall(int, lambda d_10_j_: @@ -53,11 +75,15 @@ def split(s : List[int]) -> List[List[int]]: Ensures(Forall(int, lambda d_10_j_: (Implies(d_10_j_ >= 0 and d_10_j_ < len(Result()), contains12(Result()[d_10_j_])), [[contains12(Result()[d_10_j_])]]))) + # post-conditions-end + + # impl-start res : List[List[int]] = [] # type : List[List[int]] d_7_current__string_ : List[int] = [] # type : List[int] d_8_i_ = int(0) # type : int d_8_i_ = 0 while (d_8_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(res))) Invariant(Forall(res, lambda x: Acc(list_pred(x), 1/2))) Invariant(Acc(list_pred(d_7_current__string_))) @@ -74,6 +100,7 @@ def split(s : List[int]) -> List[List[int]]: Invariant(Forall(int, lambda d_10_j_: (Implies(d_10_j_ >= 0 and d_10_j_ < len(res), contains12(res[d_10_j_])), [[contains12(res[d_10_j_])]]))) + # invariants-end if ((s)[d_8_i_]) == (3): if len(d_7_current__string_) > 0: d_7_copy = list(d_7_current__string_) @@ -84,23 +111,31 @@ def split(s : List[int]) -> List[List[int]]: d_8_i_ = (d_8_i_) + (1) if len(d_7_current__string_) > 0: d_7_copy = list(d_7_current__string_) + # assert-start Assert(get_len(d_7_copy)) + # assert-end res = (res) + [d_7_copy] - # Assert(Forall(int, lambda d_10_j_: - # (Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((get_len(res[d_10_j_])))), [[]]))) d_7_current__string_ = [] return res + # impl-end def parse__nested__parens(paren__string : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(paren__string))) Requires(Forall(int, lambda d_12_i_: not (((d_12_i_) >= (0)) and ((d_12_i_) < (len(paren__string)))) or (((((paren__string)[d_12_i_]) == (3)) or (((paren__string)[d_12_i_]) == (1))) or (((paren__string)[d_12_i_]) == (2))))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(Result()))) Ensures(Forall(ResultT(List[int]), lambda d_13_x_: ((d_13_x_) >= (0)))) + # post-conditions-end + + # impl-start res : List[int] = [] d_14_strings_ : List[List[int]] = split(paren__string) d_15_i_ = int(0) # type : int while (d_15_i_) < (len(d_14_strings_)): + # invariants-start Invariant(Acc(list_pred(d_14_strings_))) Invariant(Acc(list_pred(res))) Invariant(Forall(d_14_strings_, lambda x: Acc(list_pred(x), 1/2))) @@ -111,8 +146,10 @@ def parse__nested__parens(paren__string : List[int]) -> List[int]: Invariant(Forall(int, lambda d_10_j_: (Implies(d_10_j_ >= 0 and d_10_j_ < len(d_14_strings_), contains12(d_14_strings_[d_10_j_])), [[contains12(d_14_strings_[d_10_j_])]]))) + # invariants-end d_17_cur_ = int(0) # type : int d_17_cur_ = parseparengroup((d_14_strings_)[d_15_i_]) res = (res) + [d_17_cur_] d_15_i_ = (d_15_i_) + (1) - return res \ No newline at end of file + return res + # impl-end \ No newline at end of file diff --git a/Bench/007-filter_by_substring.py b/Bench/007-filter_by_substring.py index 12611c9..7d4b447 100644 --- a/Bench/007-filter_by_substring.py +++ b/Bench/007-filter_by_substring.py @@ -2,10 +2,16 @@ from nagini_contracts.contracts import * def checkSubstring(s : List[int], sub : List[int]) -> bool: - Requires(Acc(list_pred(s),1/2)) + # pre-conditions-start + Requires(Acc(list_pred(s), 1/2)) Requires(Acc(list_pred(sub), 1/2)) - Ensures(Acc(list_pred(s),1/2)) + # pre-conditions-end + # post-conditions-start + Ensures(Acc(list_pred(s), 1/2)) Ensures(Acc(list_pred(sub), 1/2)) + # post-conditions-end + + # impl-start result = False # type : bool result = False if (len(sub)) == (0): @@ -14,18 +20,22 @@ def checkSubstring(s : List[int], sub : List[int]) -> bool: d_0_i_ = int(0) # type : int d_0_i_ = 0 while (d_0_i_) <= ((len(s)) - (len(sub))): + # invariants-start Invariant(Acc(list_pred(s), 1/2)) Invariant(Acc(list_pred(sub), 1/2)) Invariant(len(s) - len(sub) >= 0) Invariant(((0) <= (d_0_i_)) and ((d_0_i_) <= 1 + ((len(s)) - (len(sub))))) + # invariants-end x = 0 fl = True while x < len(sub): + # invariants-start Invariant(Acc(list_pred(s), 1/2)) Invariant(Acc(list_pred(sub), 1/2)) Invariant(len(s) - len(sub) >= 0) Invariant(((0) <= (d_0_i_)) and ((d_0_i_) <= ((len(s)) - (len(sub))))) Invariant(x >= 0 and x <= len(sub)) + # invariants-end if sub[x] != s[d_0_i_ + x]: fl = False break @@ -34,27 +44,41 @@ def checkSubstring(s : List[int], sub : List[int]) -> bool: result = True d_0_i_ = (d_0_i_) + (1) return result + # impl-end @Pure def EqArrays(a : List[int], x : List[int]) -> bool : + # pre-conditions-start Requires(Acc(list_pred(a))) Requires(Acc(list_pred(x))) + # pre-conditions-end + + # impl-start return len(a) == len(x) and Forall(int, lambda d_0_i_: Implies(0 <= d_0_i_ and d_0_i_ < len(a), (a)[d_0_i_] == x[d_0_i_])) + # impl-end @Pure def InArray(a : List[List[int]], x : List[int]) -> bool : + # pre-conditions-start Requires(Acc(list_pred(a))) Requires(Acc(list_pred(x))) Requires(Forall(a, lambda d_0_s_: Acc(list_pred(d_0_s_)))) + # pre-conditions-end + + # impl-start return Exists(int, lambda d_0_s_: (Implies(((0) <= (d_0_s_)) and ((d_0_s_) < (len((a)))), EqArrays(a[d_0_s_], x)))) + # impl-end def filter__by__substring(strings : List[List[int]], substring : List[int]) -> List[List[int]]: + # pre-conditions-start Requires(Acc(list_pred(strings))) Requires(Forall(strings, lambda d_0_s_: Acc(list_pred(d_0_s_)))) Requires(Acc(list_pred(substring))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(strings))) Ensures(Forall(strings, lambda d_0_s_: Acc(list_pred(d_0_s_)))) Ensures(Acc(list_pred(Result()))) @@ -62,10 +86,14 @@ def filter__by__substring(strings : List[List[int]], substring : List[int]) -> L Ensures((len(Result())) <= (len(strings))) Ensures(Forall(int, lambda d_3_i_: (Implies(0 <= d_3_i_ and d_3_i_ < len(Result()), InArray(strings, Result()[d_3_i_]))))) + # post-conditions-end + + # impl-start res : List[List[int]] = [] d_2_i_ = int(0) # type : int d_2_i_ = 0 while (d_2_i_) < (len(strings)): + # invariants-start Invariant(Acc(list_pred(res))) Invariant(Acc(list_pred(strings))) Invariant(Acc(list_pred(substring))) @@ -75,6 +103,7 @@ def filter__by__substring(strings : List[List[int]], substring : List[int]) -> L Invariant((len(res)) <= (d_2_i_)) Invariant(Forall(int, lambda d_3_i_: (Implies(0 <= d_3_i_ and d_3_i_ < len(res), InArray(strings, res[d_3_i_])), [[InArray(strings, res[d_3_i_])]]))) + # invariants-end d_4_check_ = False # type : bool d_4_check_ = checkSubstring((strings)[d_2_i_], substring) if d_4_check_: @@ -82,3 +111,4 @@ def filter__by__substring(strings : List[List[int]], substring : List[int]) -> L res = (res) + [cpy] d_2_i_ = (d_2_i_) + (1) return res + # impl-end diff --git a/Bench/008-sum-product.py b/Bench/008-sum-product.py index 9fb33a0..2674487 100644 --- a/Bench/008-sum-product.py +++ b/Bench/008-sum-product.py @@ -3,27 +3,43 @@ @Pure def psum(i : int, j : int, s : List[int]) -> int : + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(0 <= i and i <= j and j <= len(s)) + # pre-conditions-end + + # impl-start if i == j: return 0 else: return (s)[j - 1] + (psum(i, j - 1, s)) + # impl-end @Pure def prod(i : int, j : int, s : List[int]) -> int : + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(0 <= i and i <= j and j <= len(s)) + # pre-conditions-end + + # impl-start if i == j: return 1 else: return (s)[j - 1] * (prod(i, j - 1, s)) + # impl-end def sum__product(numbers : List[int]) -> Tuple[int, int]: + # pre-conditions-start Requires(Acc(list_pred(numbers))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(numbers))) Ensures((Result()[0]) == (psum(0, len(numbers), numbers))) Ensures((Result()[1]) == (prod(0, len(numbers), numbers))) + # post-conditions-end + + # impl-start s = int(0) # type : int p = int(0) # type : int s = 0 @@ -31,15 +47,23 @@ def sum__product(numbers : List[int]) -> Tuple[int, int]: d_2_i_ = int(0) # type : int d_2_i_ = 0 while (d_2_i_) < (len(numbers)): + # invariants-start Invariant(Acc(list_pred(numbers))) Invariant(((0) <= (d_2_i_)) and ((d_2_i_) <= (len(numbers)))) Invariant((s) == (psum(0, d_2_i_, numbers))) Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(numbers)))) or ((psum(0, d_2_i_ + 1, numbers)) == (psum(0, d_2_i_, numbers) + numbers[d_2_i_])), [[psum(0, d_2_i_ + 1, numbers)]]))) Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(numbers)))) or ((prod(0, d_2_i_ + 1, numbers)) == (prod(0, d_2_i_, numbers) * numbers[d_2_i_])), [[prod(0, d_2_i_ + 1, numbers)]]))) Invariant((p) == (prod(0, d_2_i_, numbers))) + # invariants-end + + # assert-start Assert((psum(0, (d_2_i_) + (1), numbers)) == ((psum(0, d_2_i_, numbers) + (numbers)[d_2_i_]))) + # assert-end s = (s) + ((numbers)[d_2_i_]) + # assert-start Assert((prod(0, d_2_i_ + 1, numbers)) == ((prod(0, d_2_i_, numbers)) * ((numbers)[d_2_i_]))) + # assert-end p = (p) * ((numbers)[d_2_i_]) d_2_i_ = (d_2_i_) + (1) - return s, p \ No newline at end of file + return s, p + # impl-end \ No newline at end of file diff --git a/Bench/009-rolling-max.py b/Bench/009-rolling-max.py index 3ba7808..4787d56 100644 --- a/Bench/009-rolling-max.py +++ b/Bench/009-rolling-max.py @@ -7,18 +7,24 @@ def getVal(mx: Optional[int]) -> int: return mx def rolling_max(numbers: List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(numbers))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(numbers))) Ensures(Acc(list_pred(Result()))) Ensures(len(Result()) == len(numbers)) Ensures(Forall(range(len(numbers)), lambda i: numbers[i] <= Result()[i])) Ensures(Forall(range(len(numbers) - 1), lambda i: Result()[i] <= Result()[i + 1])) + # post-conditions-end + # impl-start running_max = None # type: Optional[int] result = [] # type: List[int] i = 0 while i < len(numbers): + # invariants-start Invariant(Acc(list_pred(numbers))) Invariant(Acc(list_pred(result))) Invariant(0 <= i and i <= len(numbers)) @@ -28,6 +34,7 @@ def rolling_max(numbers: List[int]) -> List[int]: Invariant(Implies(len(result) > 0, running_max is not None)) Invariant(Implies(len(result) > 0, result[-1] == getVal(running_max))) Invariant(Forall(range(i - 1), lambda i1: result[i1] <= result[i1 + 1])) + # invariants-end n = numbers[i] if running_max is None or running_max < n: @@ -36,4 +43,5 @@ def rolling_max(numbers: List[int]) -> List[int]: result.append(running_max) i += 1 - return result \ No newline at end of file + return result + # impl-end \ No newline at end of file diff --git a/Bench/010-is_palindrome.py b/Bench/010-is_palindrome.py index 55c15d8..7762e56 100644 --- a/Bench/010-is_palindrome.py +++ b/Bench/010-is_palindrome.py @@ -2,20 +2,27 @@ from nagini_contracts.contracts import * def is__palindrome(start : int, s : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(s), 1/2)) Requires((len(s)) > (0)) Requires(((0) <= (start)) and ((start) < (len(s)))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s), 1/2)) Ensures(((0) <= (start)) and ((start) < (len(s)))) Ensures((len(s)) > (0)) Ensures((Result()) == (Forall(int, lambda d_0_k_: not (((start) <= (d_0_k_)) and ((d_0_k_) < (len(s)))) or (((s)[d_0_k_]) == ((s)[((len(s)) - (1)) - (d_0_k_ - start)]))))) Ensures(Result() == is__palindrome__fun(start, s)) + # post-conditions-end + + # impl-start d_1_i_ = int(0) # type : int d_1_i_ = start d_2_j_ = int(0) # type : int d_2_j_ = (len(s)) - (1) while (d_1_i_) < (d_2_j_): + # invariants-start Invariant(Acc(list_pred(s), 1/2)) Invariant(((0) <= (start)) and ((start) < (len(s)))) Invariant(d_1_i_ <= d_2_j_ + 1) @@ -24,33 +31,51 @@ def is__palindrome(start : int, s : List[int]) -> bool: Invariant((d_2_j_ - start) == (((len(s)) - (d_1_i_)) - (1))) Invariant(Forall(int, lambda d_3_k_: not (((start) <= (d_3_k_)) and ((d_3_k_) < (d_1_i_))) or (((s)[d_3_k_]) == ((s)[((len(s)) - (1)) - (d_3_k_ - start)])))) + # invariants-end if ((s)[d_1_i_]) != ((s)[d_2_j_]): return False d_1_i_ = (d_1_i_) + (1) d_2_j_ = (d_2_j_) - (1) return True + # impl-end @Pure def is__palindrome__fun(start : int, s : List[int]) -> bool : + # pre-conditions-start Requires(Acc(list_pred(s), 1/2)) Requires(0 <= start and start < len(s)) + # pre-conditions-end + + # impl-start return Forall(int, lambda d_4_k_: not (((start) <= (d_4_k_)) and ((d_4_k_) < (len(s)))) or (((s)[d_4_k_]) == ((s)[((len(s)) - (1)) - (d_4_k_ - start)]))) + # impl-end @Pure def starts__with(result : List[int], s : List[int]) -> bool : + # pre-conditions-start Requires(Acc(list_pred(s), 1/2)) Requires(Acc(list_pred(result), 1/2)) + # pre-conditions-end + + # impl-start return ((len(result)) >= (len(s))) and (Forall(int, lambda d_5_k_: not (((0) <= (d_5_k_)) and ((d_5_k_) < (len(s)))) or (((result)[d_5_k_]) == ((s)[d_5_k_])))) + # impl-end def make__palindrome(s : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures(Acc(list_pred(Result()))) Ensures((len(Result())) <= ((2) * (len(s)))) Ensures(len(Result()) == 0 or is__palindrome__fun(0, Result())) Ensures(starts__with(Result(), s)) + # post-conditions-end + + # impl-start result = list([int(0)] * 0) # type : List[int] if (len(s)) == (0): result = [] @@ -58,19 +83,25 @@ def make__palindrome(s : List[int]) -> List[int]: d_6_beginning__of__suffix_ = int(0) # type : int d_8_flag_ = is__palindrome(d_6_beginning__of__suffix_, s) # type : bool while not(d_8_flag_): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(len(s) > 0) Invariant((((d_6_beginning__of__suffix_) >= (0)) and (((d_6_beginning__of__suffix_) + (1)) < (len(s)))) or ((d_8_flag_) and (((d_6_beginning__of__suffix_) >= (0)) and ((d_6_beginning__of__suffix_) < (len(s)))))) Invariant(Implies(d_8_flag_, is__palindrome__fun(d_6_beginning__of__suffix_, s))) + # invariants-end d_6_beginning__of__suffix_ = (d_6_beginning__of__suffix_) + (1) d_8_flag_ = is__palindrome(d_6_beginning__of__suffix_, s) d_10_reversed_ = reverse(d_6_beginning__of__suffix_, s) # type : List[int] result = (s) + (d_10_reversed_) return result + # impl-end def reverse(end : int, str : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(str), 1/2)) Requires(0 <= end and end < len(str)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(str), 1/2)) Ensures(0 <= end and end < len(str)) Ensures(Acc(list_pred(Result()))) @@ -78,11 +109,15 @@ def reverse(end : int, str : List[int]) -> List[int]: Ensures((len(Result())) == (end)) Ensures(Forall(int, lambda d_11_k_: not (((0) <= (d_11_k_)) and ((d_11_k_) < (end))) or (((Result())[d_11_k_]) == ((str)[((end) - (1)) - (d_11_k_)])))) + # post-conditions-end + + # impl-start rev = list([int(0)] * 0) # type : List[int] rev = [] d_12_i_ = int(0) # type : int d_12_i_ = 0 while (d_12_i_) < (end): + # invariants-start Invariant(Acc(list_pred(str), 1/2)) Invariant(Acc(list_pred(rev))) Invariant(0 <= end and end < len(str)) @@ -90,6 +125,8 @@ def reverse(end : int, str : List[int]) -> List[int]: Invariant((len(rev)) == (d_12_i_)) Invariant(Forall(int, lambda d_13_k_: not (((0) <= (d_13_k_)) and ((d_13_k_) < (d_12_i_))) or (((rev)[d_13_k_]) == ((str)[(end - (1)) - (d_13_k_)])))) + # invariants-end rev = (rev) + [(str)[(end - (d_12_i_)) - (1)]] d_12_i_ = (d_12_i_) + (1) return rev + # impl-end diff --git a/Bench/011-string_xor.py b/Bench/011-string_xor.py index 82256f5..1fa10e3 100644 --- a/Bench/011-string_xor.py +++ b/Bench/011-string_xor.py @@ -3,15 +3,21 @@ @Pure def xor(a : int, b : int) -> int: + # pre-conditions-start Ensures((Result()) == ((0 if (a) == (b) else 1))) + # pre-conditions-end + + # impl-start result = int(0) # type : int if (a) == (b): result = 0 else: result = 1 return result + # impl-end def string__xor(a : List[int], b : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(b))) Requires(Acc(list_pred(a))) Requires((len(a)) == (len(b))) @@ -19,6 +25,8 @@ def string__xor(a : List[int], b : List[int]) -> List[int]: not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(a)))) or ((((a)[d_0_i_]) == (0)) or (((a)[d_0_i_]) == (1))))) Requires(Forall(int, lambda d_1_i_: not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(b)))) or ((((b)[d_1_i_]) == (0)) or (((b)[d_1_i_]) == (1))))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(b))) Ensures(Acc(list_pred(a))) Ensures(Acc(list_pred(Result()))) @@ -28,9 +36,13 @@ def string__xor(a : List[int], b : List[int]) -> List[int]: not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(Result())))) or ((((Result())[d_2_i_]) == (0)) or (((Result())[d_2_i_]) == (1))))) Ensures(Forall(int, lambda d_3_i_: not (((0) <= (d_3_i_)) and ((d_3_i_) < (len(Result())))) or (((Result())[d_3_i_]) == ((0 if ((a)[d_3_i_]) == ((b)[d_3_i_]) else 1))))) + # post-conditions-end + + # impl-start result = list([int(0)] * 0) # type : List[int] d_4_i_ = int(0) # type : int while (d_4_i_) < (len(a)): + # invariants-start Invariant(Acc(list_pred(b))) Invariant(Acc(list_pred(a))) Invariant(Acc(list_pred(result))) @@ -43,7 +55,9 @@ def string__xor(a : List[int], b : List[int]) -> List[int]: not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(b)))) or ((((b)[d_1_i_]) == (0)) or (((b)[d_1_i_]) == (1))))) Invariant(Forall(int, lambda d_5_j_: not (((0) <= (d_5_j_)) and ((d_5_j_) < (d_4_i_))) or (((result)[d_5_j_]) == ((0 if ((a)[d_5_j_]) == ((b)[d_5_j_]) else 1))))) + # invariants-end d_6_bitResult_ = (0 if ((a)[d_4_i_]) == ((b)[d_4_i_]) else 1) result = (result) + [d_6_bitResult_] d_4_i_ = (d_4_i_) + (1) - return result \ No newline at end of file + return result + # impl-end \ No newline at end of file diff --git a/Bench/012-longest.py b/Bench/012-longest.py index 66eba3d..b222a3a 100644 --- a/Bench/012-longest.py +++ b/Bench/012-longest.py @@ -3,12 +3,20 @@ @Pure def getVal(mx: Optional[int]) -> int: + # pre-conditions-start Requires(mx is not None) + # pre-conditions-end + + # impl-start return mx + # impl-end def longest(strings : List[List[int]]) -> Optional[int]: + # pre-conditions-start Requires(Acc(list_pred(strings))) Requires(Forall(strings, lambda d_0_s_: Acc(list_pred(d_0_s_)))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(strings))) Ensures(Forall(strings, lambda d_0_s_: Acc(list_pred(d_0_s_)))) Ensures(((Result()) is (None)) == ((len(strings)) == (0))) @@ -19,6 +27,9 @@ def longest(strings : List[List[int]]) -> Optional[int]: ((d_1_s_) >= 0 and d_1_s_ < len(strings)) and ((len(strings[getVal(Result())])) == (len(strings[d_1_s_])))))) Ensures(not ((Result()) is not (None)) or (Forall(int, lambda d_4_j_: (not (((0) <= (d_4_j_)) and ((d_4_j_) < (Result()))) or ((len((strings)[d_4_j_])) < (len(strings[getVal(Result())]))))))) + # post-conditions-end + + # impl-start result : Optional[int] = None if (len(strings)) != (0): d_5_i_ = int(0) # type : int @@ -26,6 +37,7 @@ def longest(strings : List[List[int]]) -> Optional[int]: d_6_mx_ = int(0) # type : int d_6_mx_ = -1 while (d_5_i_) < (len(strings)): + # invariants-start Invariant(Acc(list_pred(strings))) Invariant(Forall(strings, lambda d_0_s_: Acc(list_pred(d_0_s_)))) Invariant(((d_5_i_) >= (0)) and ((d_5_i_) <= (len(strings)))) @@ -41,9 +53,13 @@ def longest(strings : List[List[int]]) -> Optional[int]: ((d_1_s_) >= 0 and d_1_s_ < d_5_i_) and ((len(strings[getVal(result)])) == (len(strings[d_1_s_])))))) Invariant(not ((result) is not (None)) or (Forall(int, lambda d_4_j_: (not (((0) <= (d_4_j_)) and ((d_4_j_) < (result))) or ((len((strings)[d_4_j_])) < (len(strings[getVal(result)]))), [[((strings)[d_4_j_])]])))) + # invariants-end if result is None or (len((strings)[d_5_i_])) > (len(strings[getVal(result)])): d_6_mx_ = len((strings)[d_5_i_]) result = d_5_i_ + # assert-start Assert(Forall(int, lambda x: Implies(x >= 0 and x < result, len(strings[result]) > len(strings[x])))) + # assert-end d_5_i_ = (d_5_i_) + (1) - return result \ No newline at end of file + return result + # impl-end \ No newline at end of file diff --git a/Bench/013-greatest-common-divisor.py b/Bench/013-greatest-common-divisor.py index 25b3309..153a1e4 100644 --- a/Bench/013-greatest-common-divisor.py +++ b/Bench/013-greatest-common-divisor.py @@ -2,16 +2,24 @@ from nagini_contracts.contracts import * def greatest_common_divisor(a: int, b: int) -> int: + # pre-conditions-start Requires(a != 0 or b != 0) + # pre-conditions-end + # post-conditions-start Ensures(Result() != 0) + # post-conditions-end + # impl-start x = a y = b while y != 0: + # invariants-start Invariant(x != 0 or y != 0) + # invariants-end temp = y y = x % y x = temp - return x \ No newline at end of file + return x + # impl-end \ No newline at end of file diff --git a/Bench/016-count_distinct_characters.py b/Bench/016-count_distinct_characters.py index 5200daf..144badb 100644 --- a/Bench/016-count_distinct_characters.py +++ b/Bench/016-count_distinct_characters.py @@ -3,44 +3,63 @@ @Pure def contains_char(s : List[int], c : int, i : int, j : int) -> bool: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(Forall(int, lambda d_0_i_: not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) or (((97) <= ((s)[d_0_i_])) and (((s)[d_0_i_]) <= (122))))) Requires(0 <= i and i <= j and j <= len(s)) Requires(((97) <= (c)) and ((c) <= (122))) + # pre-conditions-end + + # impl-start if i == j: return False else: return s[j - 1] == c or contains_char(s, c, i, j - 1) + # impl-end @Pure def count_chars_inter(s : List[int], c : int) -> int: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(Forall(int, lambda d_0_i_: not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) or (((97) <= ((s)[d_0_i_])) and (((s)[d_0_i_]) <= (122))))) Requires(((97) <= (c)) and ((c) <= (123))) + # pre-conditions-end + + # impl-start if c == 97: return 0 else: return count_chars_inter(s, c - 1) + (1 if contains_char(s, c - 1, 0, len(s)) else 0) + # impl-end def count_distinct_characters(s : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(Forall(int, lambda d_1_i_: not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((97) <= ((s)[d_1_i_])) and (((s)[d_1_i_]) <= (122))))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures(Forall(int, lambda d_1_i_: not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((97) <= ((s)[d_1_i_])) and (((s)[d_1_i_]) <= (122))))) Ensures((Result()) == count_chars_inter(s, 123)) + # post-conditions-end + + # impl-start c = int(0) # type : int d_2_i_ = int(97) # type : int while (d_2_i_) <= (122): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(((97) <= (d_2_i_)) and ((d_2_i_) <= (123))) Invariant(Forall(int, lambda d_1_i_: not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((97) <= ((s)[d_1_i_])) and (((s)[d_1_i_]) <= (122))))) Invariant(c == count_chars_inter(s, d_2_i_)) + # invariants-end if contains_char(s, d_2_i_, 0, len(s)): c = c + 1 d_2_i_ = d_2_i_ + 1 - return c \ No newline at end of file + return c + # impl-end \ No newline at end of file diff --git a/Bench/020-find-closest-elements.py b/Bench/020-find-closest-elements.py index e1adcae..90f0295 100644 --- a/Bench/020-find-closest-elements.py +++ b/Bench/020-find-closest-elements.py @@ -3,15 +3,23 @@ @Pure def dist(a : int, b : int) -> int : + # pre-conditions-start Ensures(Result() >= 0) + # pre-conditions-end + + # impl-start if (a) < (b): return (b) - (a) else: return (a) - (b) + # impl-end def find__closest__elements(s : List[int]) -> Tuple[int, int]: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires((len(s)) >= (2)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures(len(s) >= 2) Ensures(Exists(int, lambda d_0_a_: @@ -20,15 +28,21 @@ def find__closest__elements(s : List[int]) -> Tuple[int, int]: Ensures(Forall(int, lambda d_2_a_: Forall(int, lambda d_3_b_: Implies((0 <= d_2_a_ and (d_2_a_) < (len(s)) and 0 <= d_3_b_ and d_3_b_ < len(s)) and (d_2_a_ != d_3_b_), (dist(Result()[0], Result()[1])) <= (dist((s)[d_2_a_], (s)[d_3_b_])))))) + # post-conditions-end + + # impl-start l = (s)[0] # type : int h = (s)[1] # type : int d_4_d_ = dist(l, h) # type : int d_5_i_ = int(0) # type : int d_5_i_ = 0 + # assert-start Assert(Exists(int, lambda d_6_a_: Exists(int, lambda d_7_b_: ((0 <= d_6_a_ and (d_6_a_) < (d_7_b_) and d_7_b_ < len(s))) and ((l) == ((s)[d_6_a_])) and ((h) == ((s)[d_7_b_]))))) + # assert-end while (d_5_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(((0) <= (d_5_i_)) and ((d_5_i_) <= (len(s)))) Invariant((d_4_d_) == (dist(l, h))) @@ -42,13 +56,17 @@ def find__closest__elements(s : List[int]) -> Tuple[int, int]: Invariant(Forall(int, lambda d_8_a_: Forall(int, lambda d_9_b_: (Implies((0 <= d_8_a_ and (d_8_a_) < (d_5_i_) and 0 <= d_9_b_ and d_9_b_ < len(s)) and (d_8_a_ != d_9_b_), (dist(l, h)) <= (dist((s)[d_8_a_], (s)[d_9_b_]))), [[dist((s)[d_8_a_], (s)[d_9_b_])]])))) + # invariants-end d_10_j_ = int(0) # type : int d_10_j_ = (d_5_i_) + (1) + # assert-start Assert(Forall(int, lambda d_8_a_: Forall(int, lambda d_9_b_: (Implies((0 <= d_8_a_ and (d_8_a_) < (d_5_i_) and 0 <= d_9_b_ and d_9_b_ < len(s)) and (d_8_a_ != d_9_b_), (dist(l, h)) <= (dist((s)[d_8_a_], (s)[d_9_b_]))), [[dist((s)[d_8_a_], (s)[d_9_b_])]])))) Assert(Forall(int, lambda x: (Implies(x >= 0 and x < d_5_i_, dist(l, h) <= dist(s[x], s[d_5_i_])), [[dist(s[x], s[d_5_i_])]]))) + # assert-end while (d_10_j_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(((0) <= (d_5_i_)) and ((d_5_i_) < (len(s)))) Invariant(((d_5_i_) < (d_10_j_)) and ((d_10_j_) <= (len(s)))) @@ -73,14 +91,18 @@ def find__closest__elements(s : List[int]) -> Tuple[int, int]: Invariant(Forall(int, lambda d_13_a_: Forall(int, lambda d_14_b_: (Implies(((0 <= d_13_a_ and (d_13_a_) < (d_5_i_) and 0 <= d_14_b_ and d_14_b_ < len(s))) and (d_13_a_ != d_14_b_), (dist(l, h)) <= (dist((s)[d_13_a_], (s)[d_14_b_]))), [[dist((s)[d_13_a_], (s)[d_14_b_])]])))) + # invariants-end if d_5_i_ != d_10_j_ and (dist((s)[d_5_i_], (s)[d_10_j_])) <= (d_4_d_): l = (s)[d_5_i_] h = (s)[d_10_j_] d_4_d_ = dist(l, h) d_10_j_ = (d_10_j_) + (1) + # assert-start Assert(Forall(int, lambda d_8_a_: Forall(int, lambda d_9_b_: (Implies((0 <= d_8_a_ and (d_8_a_) <= (d_5_i_) and 0 <= d_9_b_ and d_9_b_ < len(s)) and (d_8_a_ != d_9_b_), (dist(l, h)) <= (dist((s)[d_8_a_], (s)[d_9_b_]))), [[dist((s)[d_8_a_], (s)[d_9_b_])]])))) + # assert-end d_5_i_ = (d_5_i_) + (1) return (l, h) + # impl-end diff --git a/Bench/023-strlen.py b/Bench/023-strlen.py index ba4e54e..e81ddec 100644 --- a/Bench/023-strlen.py +++ b/Bench/023-strlen.py @@ -1,5 +1,10 @@ from nagini_contracts.contracts import * def strlen(s : str) -> int: + # pre-conditions-start Ensures((ResultT(int)) == (len(s))) - return len(s) \ No newline at end of file + # pre-conditions-end + + # impl-start + return len(s) + # impl-end \ No newline at end of file diff --git a/Bench/024-largest-divisor.py b/Bench/024-largest-divisor.py index 184255c..f236a39 100644 --- a/Bench/024-largest-divisor.py +++ b/Bench/024-largest-divisor.py @@ -2,20 +2,29 @@ from nagini_contracts.contracts import * def largest__divisor(n : int) -> int: + # pre-conditions-start Requires((n) > (1)) + # pre-conditions-end + # post-conditions-start Ensures(((1) <= (Result())) and ((Result()) < (n))) Ensures(((n % Result())) == (0)) Ensures(Forall(int, lambda d_0_k_: not (((Result()) < (d_0_k_)) and ((d_0_k_) < (n))) or (((n % d_0_k_)) != (0)))) + # post-conditions-end + + # impl-start d = int(0) # type : int d = (n) - (1) while (d) >= (1): + # invariants-start Invariant(((1) <= (d)) and ((d) < (n))) Invariant(Forall(int, lambda d_1_k_: not (((d) < (d_1_k_)) and ((d_1_k_) < (n))) or (((n % d_1_k_)) != (0)))) + # invariants-end if ((n % d)) == (0): d = d return d d = (d) - (1) d = 1 - return d \ No newline at end of file + return d + # impl-end \ No newline at end of file diff --git a/Bench/026-remove_duplicates.py b/Bench/026-remove_duplicates.py index 24971e2..154dc29 100644 --- a/Bench/026-remove_duplicates.py +++ b/Bench/026-remove_duplicates.py @@ -2,7 +2,10 @@ from nagini_contracts.contracts import * def remove__duplicates(a : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(a), 1/2)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(a), 1/2)) Ensures(Acc(list_pred(Result()))) Ensures(len(a) == len(Old(a))) @@ -11,6 +14,9 @@ def remove__duplicates(a : List[int]) -> List[int]: Implies(((0) <= (d_2_i_)) and ((d_2_i_) < (len(a))) and (count__rec(a, a[d_2_i_], len(a)) == 1), exists_check(Result(), a[d_2_i_])))) Ensures(Forall(int, lambda d_1_i_: not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(Result())))) or (count_check(a, (Result())[d_1_i_])))) + # post-conditions-end + + # impl-start result = list([int(0)] * 0) # type : List[int] result = [] d_4_i_ = int(0) # type : int @@ -18,6 +24,7 @@ def remove__duplicates(a : List[int]) -> List[int]: a_old = list(a) while (d_4_i_) < (len(a)): + # invariants-start Invariant(Acc(list_pred(result))) Invariant(Acc(list_pred(a), 1/2)) Invariant(Acc(list_pred(a_old), 1/2)) @@ -31,30 +38,49 @@ def remove__duplicates(a : List[int]) -> List[int]: Invariant(Forall(int, lambda d_1_i_: (not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(result)))) or (count_check(a, (result)[d_1_i_])), [[]]))) + # invariants-end d_8_cnt_ = int(0) # type : int d_8_cnt_ = count__rec(a, (a)[d_4_i_], len(a)) if (d_8_cnt_) == (1): result = (result) + [(a)[d_4_i_]] + # assert-start Assert(count__rec(a, result[len(result) - 1], len(a)) == 1) + # assert-end d_4_i_ = (d_4_i_) + (1) return result + # impl-end @Pure def exists_check(a : List[int], x : int) -> bool: + # pre-conditions-start Requires(Acc(list_pred(a), 1/2)) + # pre-conditions-end + + # impl-start return Exists(int, lambda d_0_i_: ((((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))) and ((a)[d_0_i_]) == (x)))) + # impl-end @Pure def count_check(a : List[int], x : int) -> bool: + # pre-conditions-start Requires(Acc(list_pred(a), 1/2)) + # pre-conditions-end + + # impl-start return (count__rec(a, x, len(a))) == (1) + # impl-end @Pure def count__rec(a : List[int], x : int, i : int) -> int : + # pre-conditions-start Requires(Acc(list_pred(a), 1/2)) Requires(((0) <= (i)) and ((i) <= (len(a)))) + # pre-conditions-end + + # impl-start if (i) == 0: return 0 else: return (((a)[i - 1]) == (x)) + (count__rec(a, x, (i) - (1))) + # impl-end diff --git a/Bench/027-flip_case.py b/Bench/027-flip_case.py index d4e6d88..403bf7c 100644 --- a/Bench/027-flip_case.py +++ b/Bench/027-flip_case.py @@ -3,43 +3,63 @@ @Pure def lower(c : int) -> bool : + # impl-start return ((0) <= (c)) and ((c) <= (25)) + # impl-end @Pure def upper(c : int) -> bool : + # impl-start return ((26) <= (c)) and ((c) <= (51)) + # impl-end @Pure def alpha(c : int) -> bool : + # impl-start return (lower(c)) or (upper(c)) + # impl-end @Pure def flip__char(c : int) -> int : + # pre-conditions-start Ensures(lower(c) == upper(Result())) Ensures(upper(c) == lower(Result())) + # pre-conditions-end + + # impl-start if lower(c): return ((c) - (0)) + (26) elif upper(c): return ((c) + (0)) - (26) elif True: return c + # impl-end def flip__case(s : List[int]) -> List[int] : + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures(Acc(list_pred(Result()))) Ensures((len(Result())) == (len(s))) Ensures(Forall(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len(s))), lower((s)[d_0_i_]) == upper((Result())[d_0_i_]))))) Ensures(Forall(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len(s))), upper((s)[d_0_i_]) == lower((Result())[d_0_i_]))))) + # post-conditions-end + + # impl-start res = list([int(0)] * len(s)) # type : List[int] i = int(0) # type : int while i < len(s): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(Acc(list_pred(res))) Invariant(((0) <= (i)) and ((i) <= (len(s)))) Invariant((len(res)) == (len(s))) Invariant(Forall(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (i)), lower((s)[d_0_i_]) == upper((res)[d_0_i_]))))) Invariant(Forall(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (i)), upper((s)[d_0_i_]) == lower((res)[d_0_i_]))))) + # invariants-end res[i] = flip__char(s[i]) i = i + 1 return res + # impl-end diff --git a/Bench/029-filter_by_prefix.py b/Bench/029-filter_by_prefix.py index 37d4b99..45a61d2 100644 --- a/Bench/029-filter_by_prefix.py +++ b/Bench/029-filter_by_prefix.py @@ -4,29 +4,46 @@ @Pure def starts__with(s : List[int], p : List[int], i : int) -> bool : + # pre-conditions-start Requires(Acc(list_pred(s), 1/2)) Requires(Acc(list_pred(p), 1/2)) Requires(i >= 0 and i <= len(p) and i <= len(s)) + # pre-conditions-end + # post-conditions-start Ensures(Implies(len(p) == i and len(s) >= len(p), Result())) Ensures(Implies(len(s) < len(p), not Result())) + # post-conditions-end + + # impl-start return len(s) >= len(p) and Forall(int, lambda x: Implies(x >= i and x < len(p), s[x] == p[x])) + # impl-end @Pure def starts__with__fun(s : List[int], p : List[int], i : int) -> bool : + # pre-conditions-start Requires(Acc(list_pred(s), 1/2)) Requires(Acc(list_pred(p), 1/2)) Requires(0 <= i and i <= len(p) and i <= len(s)) + # pre-conditions-end + # post-conditions-start Ensures(Result() == starts__with(s, p, i)) + # post-conditions-end + + # impl-start if (len(p) == i): return True if (len(s) > i and len(s) >= len(p) and s[i] == p[i]): return starts__with(s, p, i + 1) return False + # impl-end def filter__by__prefix(xs : List[List[int]], p : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(xs))) Requires(Acc(list_pred(p))) Requires(Forall(xs, lambda x : Acc(list_pred(x)))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(p))) Ensures(Acc(list_pred(xs))) Ensures(Forall(xs, lambda x : Acc(list_pred(x)))) @@ -35,10 +52,14 @@ def filter__by__prefix(xs : List[List[int]], p : List[int]) -> List[int]: Implies(d_2_j_ >= 0 and d_2_j_ < len(Result()), Result()[d_2_j_] >= 0 and Result()[d_2_j_] < len(xs)))) Ensures(Forall(int, lambda d_0_i_: not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(Result())))) or (starts__with(xs[Result()[d_0_i_]], p, 0)))) + # post-conditions-end + + # impl-start filtered = list([int(0)] * 0) # type : List[int] d_1_i_ = int(0) # type : int d_1_i_ = 0 while (d_1_i_) < (len(xs)): + # invariants-start Invariant(Acc(list_pred(filtered))) Invariant(Acc(list_pred(xs), 1/2)) Invariant(Acc(list_pred(p), 1/2)) @@ -48,7 +69,9 @@ def filter__by__prefix(xs : List[List[int]], p : List[int]) -> List[int]: (i >= 0 and i < d_1_i_))) Invariant(Forall(filtered, lambda i: (starts__with(xs[i], p, 0), [[starts__with(xs[i], p, 0)]]))) + # invariants-end if starts__with__fun((xs)[d_1_i_], p, 0): filtered = (filtered) + [d_1_i_] d_1_i_ = (d_1_i_) + (1) - return filtered \ No newline at end of file + return filtered + # impl-end \ No newline at end of file diff --git a/Bench/030-get-positive.py b/Bench/030-get-positive.py index 16d1936..135b1e5 100644 --- a/Bench/030-get-positive.py +++ b/Bench/030-get-positive.py @@ -2,7 +2,10 @@ from nagini_contracts.contracts import * def get__positive(l : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(l))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(l))) Ensures(Acc(list_pred(Result()))) Ensures(Forall(int, lambda d_0_i_: @@ -14,10 +17,14 @@ def get__positive(l : List[int]) -> List[int]: Ensures(((len(Result())) == (0)) or (Forall(int, lambda d_3_i1_: not (((d_3_i1_) >= (0)) and ((d_3_i1_) < (len(Result())))) or (Exists(int, lambda d_4_i2_: (((d_4_i2_) >= (0)) and ((d_4_i2_) < (len(l)))) and (((l)[d_4_i2_]) == ((Result())[d_3_i1_]))))))) + # post-conditions-end + + # impl-start result = list([0] * 0) # type : List[int] d_5_i_ = int(0) # type : int d_5_i_ = 0 while (d_5_i_) < (len(l)): + # invariants-start Invariant(Acc(list_pred(result))) Invariant(Acc(list_pred(l))) Invariant(((d_5_i_) >= (0)) and ((d_5_i_) <= (len(l)))) @@ -32,19 +39,25 @@ def get__positive(l : List[int]) -> List[int]: Invariant(((len(result)) == (0)) or (Forall(int, lambda d_11_i1_: not (((d_11_i1_) >= (0)) and ((d_11_i1_) < (len(result)))) or (Exists(int, lambda d_12_i2_: (((d_12_i2_) >= (0)) and ((d_12_i2_) < (len(l)))) and (((l)[d_12_i2_]) == ((result)[d_11_i1_]))))))) + # invariants-end d_13_n_ = int(0) # type : int d_13_n_ = (l)[d_5_i_] if (d_13_n_) > (0): d_17_res__prev_ = result + # assert-start Assert(Forall(int, lambda d_14_i1_: not (((d_14_i1_) >= (0)) and ((d_14_i1_) < (d_5_i_))) or (not (((l)[d_14_i1_]) > (0)) or (Exists(int, lambda d_15_i2_: (((d_15_i2_) >= (0)) and ((d_15_i2_) < (len(result)))) and (((result)[d_15_i2_]) == ((l)[d_14_i1_]))))))) + # assert-end result = (result) + ([d_13_n_]) + # assert-start Assert(((result)[(len(result)) - (1)]) == (d_13_n_)) Assert(Forall(int, lambda d_16_i1_: not (((d_16_i1_) >= (0)) and ((d_16_i1_) < (len(d_17_res__prev_)))) or (((d_17_res__prev_)[d_16_i1_]) == ((result)[d_16_i1_])))) Assert(Forall(int, lambda d_18_i1_: not (((d_18_i1_) >= (0)) and ((d_18_i1_) < (d_5_i_))) or (not (((l)[d_18_i1_]) > (0)) or (Exists(int, lambda d_19_i2_: (((d_19_i2_) >= (0)) and ((d_19_i2_) < (len(d_17_res__prev_)))) and (((d_17_res__prev_)[d_19_i2_]) == ((l)[d_18_i1_]))))))) + # assert-end d_5_i_ = (d_5_i_) + (1) return result + # impl-end \ No newline at end of file diff --git a/Bench/031-is-prime.py b/Bench/031-is-prime.py index f8af3e0..7bbb5b4 100644 --- a/Bench/031-is-prime.py +++ b/Bench/031-is-prime.py @@ -2,22 +2,31 @@ from nagini_contracts.contracts import * def is__prime(k : int) -> bool: + # pre-conditions-start Requires((k) >= (2)) + # pre-conditions-end + # post-conditions-start Ensures(not (Result()) or (Forall(int, lambda d_0_i_: not (((2) <= (d_0_i_)) and ((d_0_i_) < (k))) or ((k % d_0_i_) != (0))))) Ensures(not (not(Result())) or (Exists(int, lambda d_1_j_: (((2) <= (d_1_j_)) and ((d_1_j_) < (k))) and (((k % d_1_j_)) == (0))))) + # post-conditions-end + + # impl-start result = False # type : bool d_2_i_ = int(0) # type : int d_2_i_ = 2 result = True while (d_2_i_) < (k): + # invariants-start Invariant(((2) <= (d_2_i_)) and ((d_2_i_) <= (k))) Invariant(not (not(result)) or (Exists(int, lambda d_3_j_: (((2) <= (d_3_j_)) and ((d_3_j_) < (d_2_i_))) and (((k % d_3_j_)) == (0))))) Invariant(not (result) or (Forall(int, lambda d_4_j_: not (((2) <= (d_4_j_)) and ((d_4_j_) < (d_2_i_))) or (((k % d_4_j_)) != (0))))) + # invariants-end if ((k % d_2_i_)) == (0): result = False d_2_i_ = (d_2_i_) + (1) - return result \ No newline at end of file + return result + # impl-end \ No newline at end of file diff --git a/Bench/033-sort_third.py b/Bench/033-sort_third.py index cd6c090..1afc3ca 100644 --- a/Bench/033-sort_third.py +++ b/Bench/033-sort_third.py @@ -2,8 +2,11 @@ from nagini_contracts.contracts import * def sort__third(a : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(a))) Requires((len(a)) > (0)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(a))) Ensures(Acc(list_pred(Result()))) Ensures((len(Result())) == (len(a))) @@ -12,12 +15,16 @@ def sort__third(a : List[int]) -> List[int]: not ((((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len(Result())))) and (((d_0_i_ % 3)) == (0))) and (((d_1_j_ % 3)) == (0))) or (((Result())[d_0_i_]) <= ((Result())[d_1_j_]))))) Ensures(Forall(int, lambda d_2_i_: not ((((0) <= (d_2_i_)) and ((d_2_i_) < (len(a)))) and (((d_2_i_ % 3)) != (0))) or (((Result())[d_2_i_]) == ((a)[d_2_i_])))) + # post-conditions-end + + # impl-start sorted__even = list([int(0)] * 0) # type : List[int] d_3_p_ = list([False] * 0) # type : List[bool] d_3_p_ = list([]) d_4_i_ = int(0) # type : int d_4_i_ = 0 while (d_4_i_) < (len(a)): + # invariants-start Invariant(Acc(list_pred(d_3_p_))) Invariant(Acc(list_pred(sorted__even))) Invariant(Acc(list_pred(a))) @@ -25,15 +32,20 @@ def sort__third(a : List[int]) -> List[int]: Invariant((len(d_3_p_)) == (d_4_i_)) Invariant(Forall(int, lambda d_5_j_: not (((0) <= (d_5_j_)) and ((d_5_j_) < (d_4_i_))) or (((d_3_p_)[d_5_j_]) == (((d_5_j_ % 3)) == (0))))) + # invariants-end d_3_p_ = (d_3_p_) + [((d_4_i_ % 3)) == (0)] d_4_i_ = (d_4_i_) + (1) sorted__even = SortSeqPred(a, d_3_p_) return sorted__even + # impl-end def SortSeqPred(s : List[int], p : List[bool]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(p), 1/2)) Requires(Acc(list_pred(s), 1/2)) Requires((len(s)) == (len(p))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(p), 1/2)) Ensures(Acc(list_pred(s), 1/2)) Ensures(Acc(list_pred(Result()))) @@ -44,11 +56,15 @@ def SortSeqPred(s : List[int], p : List[bool]) -> List[int]: not ((((((0) <= (d_6_i_)) and ((d_6_i_) < (d_7_j_))) and ((d_7_j_) < (len(Result())))) and ((p)[d_6_i_])) and ((p)[d_7_j_])) or (((Result())[d_6_i_]) <= ((Result())[d_7_j_]))))) Ensures(Forall(int, lambda d_8_i_: not ((((0) <= (d_8_i_)) and ((d_8_i_) < (len(s)))) and (not((p)[d_8_i_]))) or (((Result())[d_8_i_]) == ((s)[d_8_i_])))) + # post-conditions-end + + # impl-start sorted = list([int(0)] * 0) # type : List[int] sorted = list(s) d_9_i_ = int(0) # type : int d_9_i_ = 0 while (d_9_i_) < (len(sorted)): + # invariants-start Invariant(Acc(list_pred(sorted))) Invariant(Acc(list_pred(p), 1/2)) Invariant(Acc(list_pred(s), 1/2)) @@ -66,12 +82,14 @@ def SortSeqPred(s : List[int], p : List[bool]) -> List[int]: (Forall(int, lambda d_13_k_: (not ((((d_9_i_) <= (d_13_k_)) and ((d_13_k_) < (len(sorted)))) and ((p)[d_13_k_])) or (((sorted)[d_12_j_]) <= ((sorted)[d_13_k_])), [[sorted[d_13_k_]]]))), [[(sorted)[d_12_j_]]]))) + # invariants-end if (p)[d_9_i_]: d_15_minIndex_ = int(0) # type : int d_15_minIndex_ = d_9_i_ d_16_j_ = int(0) # type : int d_16_j_ = (d_9_i_) + (1) while (d_16_j_) < (len(sorted)): + # invariants-start Invariant(Acc(list_pred(sorted))) Invariant(Acc(list_pred(p), 1/2)) Invariant(Acc(list_pred(s), 1/2)) @@ -94,14 +112,18 @@ def SortSeqPred(s : List[int], p : List[bool]) -> List[int]: (Forall(int, lambda d_13_k_: (not ((((d_9_i_) <= (d_13_k_)) and ((d_13_k_) < (len(sorted)))) and ((p)[d_13_k_])) or (((sorted)[d_12_j_]) <= ((sorted)[d_13_k_])), [[sorted[d_13_k_]]]))), [[(sorted)[d_12_j_]]]))) + # invariants-end if ((p)[d_16_j_]) and (((sorted)[d_16_j_]) < ((sorted)[d_15_minIndex_])): d_15_minIndex_ = d_16_j_ d_16_j_ = (d_16_j_) + (1) if (d_15_minIndex_) != (d_9_i_): + # assert-start Assert((p)[d_15_minIndex_]) Assert(p[d_9_i_]) + # assert-end rhs0_ = (sorted)[d_9_i_] # type : int (sorted)[d_9_i_] = (sorted)[d_15_minIndex_] (sorted)[d_15_minIndex_] = rhs0_ d_9_i_ = (d_9_i_) + (1) - return sorted \ No newline at end of file + return sorted + # impl-end \ No newline at end of file diff --git a/Bench/034-unique.py b/Bench/034-unique.py index b708318..85d04a7 100644 --- a/Bench/034-unique.py +++ b/Bench/034-unique.py @@ -61,33 +61,20 @@ def uniqueSorted(s : List[int]) -> List[int]: # Requires(Acc(list_pred(s))) # Ensures(Acc(list_pred(s))) # Ensures(Acc(list_pred(Result()))) -# Ensures(Forall(int, lambda d_12_i_: -# Forall(int, lambda d_13_j_: -# not ((((0) <= (d_12_i_)) and ((d_12_i_) < (d_13_j_))) and ((d_13_j_) < (len(Result())))) or (((Result())[d_12_i_]) < ((Result())[d_13_j_]))))) -# Ensures(Forall(int, lambda d_14_x_: -# not ((d_14_x_) in (Result())) or ((d_14_x_) in (s)))) -# Ensures(Forall(int, lambda d_15_x_: -# not ((d_15_x_) in (s)) or ((d_15_x_) in (Result())))) -# result = list([int(0)] * 0) # type : List[int] -# d_16_sorted_ = list([int(0)] * 0) # type : List[int] -# out0_ # type : List[int] -# out0_ = BubbleSort(s) -# d_16_sorted_ = out0_ -# out1_ # type : List[int] -# out1_ = uniqueSorted(d_16_sorted_) -# result = out1_ -# Assert(Forall(int, lambda d_17_x_: -# not ((d_17_x_) in (d_16_sorted_)) or ((d_17_x_) in (s)))) -# Assert(Forall(int, lambda d_18_x_: -# not ((d_18_x_) in (s)) or ((d_18_x_) in (d_16_sorted_)))) -# return result +# Ensures(Forall(int, lambda d_9_k_: +# (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(Result()))), InArray(s, Result()[d_9_k_]))))) +# Ensures(Forall(int, lambda d_2_i_: +# Forall(int, lambda d_3_j_: +# not ((((0) <= (d_2_i_)) and ((d_2_i_) < (d_3_j_))) and ((d_3_j_) < (len(Result())))) or (((Result())[d_2_i_]) < ((Result())[d_3_j_]))))) +# # Ensures(Forall(int, lambda d_11_j_: +# # (Implies(((0) <= (d_11_j_)) and ((d_11_j_) < (len(s))), InArray(Result(), s[d_11_j_]))))) +# return uniqueSorted(BubbleSort(s)) def BubbleSort(a1 : List[int]) -> List[int]: Requires(Acc(list_pred(a1), 1/2)) Ensures(Acc(list_pred(a1), 1/2)) Ensures(Acc(list_pred(Result()))) Ensures((len(a1)) == (len(Result()))) - # Ensures(ToMS(ToSeq(a1)) == ToMS(ToSeq(Result()))) Ensures(Forall(int, lambda d_0_i_: Forall(int, lambda d_1_j_: Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((Result())))), ((Result())[d_0_i_]) <= ((Result())[d_1_j_]))))) diff --git a/Bench/035-max-element.py b/Bench/035-max-element.py index 13bb39a..c97b82a 100644 --- a/Bench/035-max-element.py +++ b/Bench/035-max-element.py @@ -2,25 +2,34 @@ from nagini_contracts.contracts import * def max__element(l : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(l))) Requires((len(l)) > (0)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(l))) Ensures(Forall(int, lambda d_0_i_: not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(l)))) or (((l)[d_0_i_]) <= (Result())))) Ensures(Exists(int, lambda d_1_i_: (((d_1_i_) >= (0)) and ((d_1_i_) < (len(l)))) and (((l)[d_1_i_]) == (Result())))) + # post-conditions-end + + # impl-start result = int(0) # type : int result = (l)[0] d_2_i_ = int(0) # type : int d_2_i_ = 1 while (d_2_i_) < (len(l)): + # invariants-start Invariant(Acc(list_pred(l))) Invariant(((d_2_i_) >= (1)) and ((d_2_i_) <= (len(l)))) Invariant(Forall(int, lambda d_3_i1_: not (((d_3_i1_) >= (0)) and ((d_3_i1_) < (d_2_i_))) or (((l)[d_3_i1_]) <= (result)))) Invariant(Exists(int, lambda d_4_i1_: (((d_4_i1_) >= (0)) and ((d_4_i1_) < (d_2_i_))) and (((l)[d_4_i1_]) == (result)))) + # invariants-end if ((l)[d_2_i_]) > (result): result = (l)[d_2_i_] d_2_i_ = (d_2_i_) + (1) return result + # impl-end diff --git a/Bench/036-fizz_buzz.py b/Bench/036-fizz_buzz.py index 8a7b6e5..9003f24 100644 --- a/Bench/036-fizz_buzz.py +++ b/Bench/036-fizz_buzz.py @@ -2,56 +2,88 @@ from nagini_contracts.contracts import * def fizz__buzz(n : int) -> int: + # pre-conditions-start Requires(n >= 0) + # pre-conditions-end + # post-conditions-start Ensures(Result() >= 0) Ensures((Result()) == fizz_buzz_fun(n)) + # post-conditions-end + + # impl-start result = int(0) # type : int result = 0 d_1_i_ = int(0) # type : int d_1_i_ = 0 while (d_1_i_) < (n): + # invariants-start Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (n))) Invariant(Forall(int, lambda x : (Implies(x >= 0 and x < n, fizz_buzz_fun(x + 1) == (count7__r(x) if ((x % 11 == 0) or (x % 13 == 0)) else 0) + fizz_buzz_fun(x)), [[fizz_buzz_fun(x + 1)]]))) Invariant(result == fizz_buzz_fun(d_1_i_)) + # invariants-end if (((d_1_i_ % 11)) == (0)) or (((d_1_i_ % 13)) == (0)): d_4_cnt_ = int(0) # type : int d_4_cnt_ = count7(d_1_i_) result = (result) + (d_4_cnt_) d_1_i_ = (d_1_i_) + (1) return result + # impl-end @Pure def fizz_buzz_fun(n : int) -> int: + # pre-conditions-start Requires(n >= 0) + # pre-conditions-end + # post-conditions-start Ensures(Result() >= 0) + # post-conditions-end + + # impl-start if n == 0: return 0 else: return (count7__r(n - 1) if ((n - 1) % 11 == 0 or (n - 1) % 13 == 0) else 0) + fizz_buzz_fun(n - 1) + # impl-end def count7(x : int) -> int: + # pre-conditions-start Requires(x >= 0) + # pre-conditions-end + # post-conditions-start Ensures(Result() >= 0) Ensures((Result()) == (count7__r(x))) + # post-conditions-end + + # impl-start count = int(0) # type : int count = 0 d_6_y_ = int(0) # type : int d_6_y_ = x while (d_6_y_) > (0): + # invariants-start Invariant(((0) <= (d_6_y_)) and ((d_6_y_) <= (x))) Invariant(((count) + (count7__r(d_6_y_))) == (count7__r(x))) + # invariants-end if ((d_6_y_ % 10)) == (7): count = (count) + (1) d_6_y_ = d_6_y_ // 10 return count + # impl-end @Pure def count7__r(x : int) -> int : + # pre-conditions-start Requires(x >= 0) + # pre-conditions-end + # post-conditions-start Ensures(Result() >= 0) + # post-conditions-end + + # impl-start if x == 0: return 0 else: return (x % 10 == 7) + count7__r(x // 10) + # impl-end diff --git a/Bench/037-sort_even.py b/Bench/037-sort_even.py index 3b472a2..912d1e6 100644 --- a/Bench/037-sort_even.py +++ b/Bench/037-sort_even.py @@ -2,8 +2,11 @@ from nagini_contracts.contracts import * def sorted__even(a : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(a))) Requires((len(a)) > (0)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(a))) Ensures(Acc(list_pred(Result()))) Ensures((len(Result())) == (len(a))) @@ -12,12 +15,16 @@ def sorted__even(a : List[int]) -> List[int]: not ((((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len(Result())))) and (((d_0_i_ % 2)) == (0))) and (((d_1_j_ % 2)) == (0))) or (((Result())[d_0_i_]) <= ((Result())[d_1_j_]))))) Ensures(Forall(int, lambda d_2_i_: not ((((0) <= (d_2_i_)) and ((d_2_i_) < (len(a)))) and (((d_2_i_ % 2)) == (1))) or (((Result())[d_2_i_]) == ((a)[d_2_i_])))) + # post-conditions-end + + # impl-start sorted__even = list([int(0)] * 0) # type : List[int] d_3_p_ = list([False] * 0) # type : List[bool] d_3_p_ = list([]) d_4_i_ = int(0) # type : int d_4_i_ = 0 while (d_4_i_) < (len(a)): + # invariants-start Invariant(Acc(list_pred(d_3_p_))) Invariant(Acc(list_pred(sorted__even))) Invariant(Acc(list_pred(a))) @@ -25,15 +32,20 @@ def sorted__even(a : List[int]) -> List[int]: Invariant((len(d_3_p_)) == (d_4_i_)) Invariant(Forall(int, lambda d_5_j_: not (((0) <= (d_5_j_)) and ((d_5_j_) < (d_4_i_))) or (((d_3_p_)[d_5_j_]) == (((d_5_j_ % 2)) == (0))))) + # invariants-end d_3_p_ = (d_3_p_) + [((d_4_i_ % 2)) == (0)] d_4_i_ = (d_4_i_) + (1) sorted__even = SortSeqPred(a, d_3_p_) return sorted__even + # impl-end def SortSeqPred(s : List[int], p : List[bool]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(p), 1/2)) Requires(Acc(list_pred(s), 1/2)) Requires((len(s)) == (len(p))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(p), 1/2)) Ensures(Acc(list_pred(s), 1/2)) Ensures(Acc(list_pred(Result()))) @@ -44,11 +56,15 @@ def SortSeqPred(s : List[int], p : List[bool]) -> List[int]: not ((((((0) <= (d_6_i_)) and ((d_6_i_) < (d_7_j_))) and ((d_7_j_) < (len(Result())))) and ((p)[d_6_i_])) and ((p)[d_7_j_])) or (((Result())[d_6_i_]) <= ((Result())[d_7_j_]))))) Ensures(Forall(int, lambda d_8_i_: not ((((0) <= (d_8_i_)) and ((d_8_i_) < (len(s)))) and (not((p)[d_8_i_]))) or (((Result())[d_8_i_]) == ((s)[d_8_i_])))) + # post-conditions-end + + # impl-start sorted = list([int(0)] * 0) # type : List[int] sorted = list(s) d_9_i_ = int(0) # type : int d_9_i_ = 0 while (d_9_i_) < (len(sorted)): + # invariants-start Invariant(Acc(list_pred(sorted))) Invariant(Acc(list_pred(p), 1/2)) Invariant(Acc(list_pred(s), 1/2)) @@ -66,12 +82,14 @@ def SortSeqPred(s : List[int], p : List[bool]) -> List[int]: (Forall(int, lambda d_13_k_: (not ((((d_9_i_) <= (d_13_k_)) and ((d_13_k_) < (len(sorted)))) and ((p)[d_13_k_])) or (((sorted)[d_12_j_]) <= ((sorted)[d_13_k_])), [[sorted[d_13_k_]]]))), [[(sorted)[d_12_j_]]]))) + # invariants-end if (p)[d_9_i_]: d_15_minIndex_ = int(0) # type : int d_15_minIndex_ = d_9_i_ d_16_j_ = int(0) # type : int d_16_j_ = (d_9_i_) + (1) while (d_16_j_) < (len(sorted)): + # invariants-start Invariant(Acc(list_pred(sorted))) Invariant(Acc(list_pred(p), 1/2)) Invariant(Acc(list_pred(s), 1/2)) @@ -94,14 +112,18 @@ def SortSeqPred(s : List[int], p : List[bool]) -> List[int]: (Forall(int, lambda d_13_k_: (not ((((d_9_i_) <= (d_13_k_)) and ((d_13_k_) < (len(sorted)))) and ((p)[d_13_k_])) or (((sorted)[d_12_j_]) <= ((sorted)[d_13_k_])), [[sorted[d_13_k_]]]))), [[(sorted)[d_12_j_]]]))) + # invariants-end if ((p)[d_16_j_]) and (((sorted)[d_16_j_]) < ((sorted)[d_15_minIndex_])): d_15_minIndex_ = d_16_j_ d_16_j_ = (d_16_j_) + (1) if (d_15_minIndex_) != (d_9_i_): + # assert-start Assert((p)[d_15_minIndex_]) Assert(p[d_9_i_]) + # assert-end rhs0_ = (sorted)[d_9_i_] # type : int (sorted)[d_9_i_] = (sorted)[d_15_minIndex_] (sorted)[d_15_minIndex_] = rhs0_ d_9_i_ = (d_9_i_) + (1) - return sorted \ No newline at end of file + return sorted + # impl-end \ No newline at end of file diff --git a/Bench/038-encode_cyclic.py b/Bench/038-encode_cyclic.py index d73868a..6a20b96 100644 --- a/Bench/038-encode_cyclic.py +++ b/Bench/038-encode_cyclic.py @@ -2,7 +2,10 @@ from nagini_contracts.contracts import * def encode_cyclic(s: List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(s), 1/2)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(Result()))) Ensures(Acc(list_pred(s), 1/2)) Ensures(len(s) == len(Result())) @@ -14,10 +17,13 @@ def encode_cyclic(s: List[int]) -> List[int]: (Implies(0 <= x and x < len(s) - len(s) % 3, Implies(x % 3 == 2, Result()[x] == s[x - 2]))))) Ensures(Forall(int, lambda x: (Implies(len(s) - len(s) % 3 <= x and x < len(s), Result()[x] == s[x])))) + # post-conditions-end + # impl-start res : List[int] = list(s) i = 0 while i + 2 < len(s): + # invariants-start Invariant(Acc(list_pred(res))) Invariant(Acc(list_pred(s),1/2)) Invariant(0 <= i and i <= len(s)) @@ -30,15 +36,22 @@ def encode_cyclic(s: List[int]) -> List[int]: (Implies(0 <= x and x < i, Implies(x % 3 == 1, res[x] == s[x + 1])), [[res[x]]]))) Invariant(Forall(int, lambda x: (Implies(0 <= x and x < i, Implies(x % 3 == 2, res[x] == s[x - 2])), [[res[x]]]))) + # invariants-end res[i] = s[i + 1] res[i + 1] = s[i + 2] res[i + 2] = s[i] i = i + 3 + # assert-start Assert(i == len(s) - len(s) % 3) + # assert-end return res + # impl-end def decode_cyclic(s: List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(Result()))) Ensures(Acc(list_pred(s))) Ensures(len(s) == len(Result())) @@ -48,4 +61,8 @@ def decode_cyclic(s: List[int]) -> List[int]: (Implies(0 <= x and x < len(s) - len(s) % 3, Implies(x % 3 == 0, Result()[x] == s[x + 2]))))) Ensures(Forall(int, lambda x: (Implies(0 <= x and x < len(s) - len(s) % 3, Implies(x % 3 == 1, Result()[x] == s[x - 1]))))) - return encode_cyclic(encode_cyclic(s)) \ No newline at end of file + # post-conditions-end + + # impl-start + return encode_cyclic(encode_cyclic(s)) + # impl-end \ No newline at end of file diff --git a/Bench/040-triples-sum-to-zero.py b/Bench/040-triples-sum-to-zero.py index d8575f7..c8c5af3 100644 --- a/Bench/040-triples-sum-to-zero.py +++ b/Bench/040-triples-sum-to-zero.py @@ -2,7 +2,10 @@ from nagini_contracts.contracts import * def triples__sum__to__zero(l : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(l))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(l))) Ensures(Implies(not(Result()), Forall(int, lambda d_3_i_: Forall(int, lambda d_4_j_: @@ -12,20 +15,26 @@ def triples__sum__to__zero(l : List[int]) -> bool: Exists(int, lambda d_1_j_: Exists(int, lambda d_2_k_: ((((((((0) <= (d_0_i_)) and ((d_0_i_) < (len(l)))) and (((0) <= (d_1_j_)) and ((d_1_j_) < (len(l))))) and (((0) <= (d_2_k_)) and ((d_2_k_) < (len(l))))) and ((d_0_i_) != (d_1_j_))) and ((d_1_j_) != (d_2_k_))) and ((d_0_i_) != (d_2_k_))) and (((((l)[d_0_i_]) + ((l)[d_1_j_])) + ((l)[d_2_k_])) == (0))))))) + # post-conditions-end + + # impl-start result = False # type : bool result = False d_6_i_ = int(0) # type : int d_6_i_ = 0 while (d_6_i_) < (len(l)): + # invariants-start Invariant(Acc(list_pred(l), 1/2)) Invariant(((d_6_i_) >= (0)) and ((d_6_i_) <= (len(l)))) Invariant(Forall(int, lambda d_7_i1_: Forall(int, lambda d_8_j_: Forall(int, lambda d_9_k_: (Implies((((((((0) <= (d_7_i1_)) and ((d_7_i1_) < (d_6_i_))) and (((0) <= (d_8_j_)) and ((d_8_j_) < (len(l))))) and (((0) <= (d_9_k_)) and ((d_9_k_) < (len(l))))) and ((d_7_i1_) != (d_8_j_))) and ((d_8_j_) != (d_9_k_))) and ((d_7_i1_) != (d_9_k_)), ((((l)[d_7_i1_]) + ((l)[d_8_j_])) + ((l)[d_9_k_])) != (0)), [[(((l)[d_7_i1_]) + ((l)[d_8_j_])) + ((l)[d_9_k_])]]))))) + # invariants-end d_13_j_ = int(0) # type : int d_13_j_ = 0 while (d_13_j_) < (len(l)): + # invariants-start Invariant(Acc(list_pred(l), 1/2)) Invariant(((d_6_i_) >= (0)) and ((d_6_i_) < (len(l)))) Invariant(((d_13_j_) >= (0)) and ((d_13_j_) <= (len(l)))) @@ -35,9 +44,11 @@ def triples__sum__to__zero(l : List[int]) -> bool: (Implies(((((((((0) <= (d_14_i1_)) and ((d_14_i1_) < (d_6_i_))) and (((0) <= (d_15_j_)) and ((d_15_j_) < (len(l))))) and (((0) <= (d_16_k_)) and ((d_16_k_) < (len(l)))))) or (d_14_i1_ == d_6_i_ and (d_15_j_) >= 0 and (d_15_j_) < d_13_j_ and (d_16_k_) >= 0 and (d_16_k_) < len(l))) and ((d_14_i1_) != (d_15_j_)) and ((d_15_j_) != (d_16_k_))) and ((d_14_i1_) != (d_16_k_)), ((((l)[d_14_i1_]) + ((l)[d_15_j_])) + ((l)[d_16_k_])) != (0)), [[(((l)[d_14_i1_]) + ((l)[d_15_j_])) + ((l)[d_16_k_])]]))))) + # invariants-end d_24_k_ = int(0) # type : int d_24_k_ = 0 while (d_24_k_) < (len(l)): + # invariants-start Invariant(Acc(list_pred(l), 1/2)) Invariant(((d_6_i_) >= (0)) and ((d_6_i_) < (len(l)))) Invariant(((d_13_j_) >= (0)) and ((d_13_j_) < (len(l)))) @@ -49,9 +60,11 @@ def triples__sum__to__zero(l : List[int]) -> bool: (d_14_i1_ == d_6_i_ and (d_15_j_) >= 0 and (d_15_j_) < d_13_j_ and (d_16_k_) >= 0 and (d_16_k_) < len(l)) or (d_14_i1_ == d_6_i_ and (d_15_j_) == d_13_j_ and (d_16_k_) >= 0 and (d_16_k_) < d_24_k_)) and ((d_14_i1_) != (d_15_j_)) and ((d_15_j_) != (d_16_k_))) and ((d_14_i1_) != (d_16_k_)), ((((l)[d_14_i1_]) + ((l)[d_15_j_])) + ((l)[d_16_k_])) != (0)), [[(((l)[d_14_i1_]) + ((l)[d_15_j_])) + ((l)[d_16_k_])]]))))) + # invariants-end if ((((d_6_i_) != (d_13_j_)) and ((d_13_j_) != (d_24_k_))) and ((d_6_i_) != (d_24_k_))) and (((((l)[d_6_i_]) + ((l)[d_13_j_])) + ((l)[d_24_k_])) == (0)): return True d_24_k_ = (d_24_k_) + (1) d_13_j_ = (d_13_j_) + (1) d_6_i_ = (d_6_i_) + (1) - return False \ No newline at end of file + return False + # impl-end \ No newline at end of file diff --git a/Bench/041-car_race_collision.py b/Bench/041-car_race_collision.py index 25a50ad..9da1a32 100644 --- a/Bench/041-car_race_collision.py +++ b/Bench/041-car_race_collision.py @@ -2,8 +2,15 @@ from nagini_contracts.contracts import * def car__race__collision(n : int) -> int: + # pre-conditions-start Requires((n) >= (0)) + # pre-conditions-end + # post-conditions-start Ensures((Result()) == ((n) * (n))) + # post-conditions-end + + # impl-start cnt = int(0) # type : int cnt = (n) * (n) return cnt + # impl-end diff --git a/Bench/042-incr-list.py b/Bench/042-incr-list.py index 6b7d120..cc55e68 100644 --- a/Bench/042-incr-list.py +++ b/Bench/042-incr-list.py @@ -2,23 +2,32 @@ from nagini_contracts.contracts import * def incr__list(l : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(l))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(l))) Ensures(Acc(list_pred(Result()))) Ensures((len(Result())) == (len(l))) Ensures(Forall(int, lambda d_0_i_: not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(l)))) or (((Result())[d_0_i_]) == (((l)[d_0_i_]) + (1))))) + # post-conditions-end + + # impl-start result = list([int(0)] * 0) # type : List[int] result = list([]) d_1_i_ = int(0) # type : int d_1_i_ = 0 while (d_1_i_) < (len(l)): + # invariants-start Invariant(Acc(list_pred(result))) Invariant(Acc(list_pred(l))) Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(l)))) Invariant((len(result)) == (d_1_i_)) Invariant(Forall(int, lambda d_2_i1_: not (((0) <= (d_2_i1_)) and ((d_2_i1_) < (d_1_i_))) or (((result)[d_2_i1_]) == (((l)[d_2_i1_]) + (1))))) + # invariants-end result = (result) + ([((l)[d_1_i_]) + (1)]) d_1_i_ = (d_1_i_) + (1) return result + # impl-end diff --git a/Bench/043-pairs-sum-to-zero.py b/Bench/043-pairs-sum-to-zero.py index 352ddf4..83e0620 100644 --- a/Bench/043-pairs-sum-to-zero.py +++ b/Bench/043-pairs-sum-to-zero.py @@ -2,7 +2,10 @@ from nagini_contracts.contracts import * def pairs__sum__to__zero(l : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(l))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(l))) Ensures(not (Result()) or (Exists(int, lambda d_0_i_: Exists(int, lambda d_1_j_: @@ -10,11 +13,15 @@ def pairs__sum__to__zero(l : List[int]) -> bool: Ensures(not (not(Result())) or (Forall(int, lambda d_2_i_: Forall(int, lambda d_3_j_: (not (((((0) <= (d_2_i_)) and ((d_2_i_) < (len(l)))) and (((0) <= (d_3_j_)) and ((d_3_j_) < (len(l))))) and ((d_2_i_) != (d_3_j_))) or ((((l)[d_2_i_]) + ((l)[d_3_j_])) != (0)), [[((l)[d_2_i_]) + ((l)[d_3_j_])]]))))) + # post-conditions-end + + # impl-start result = False # type : bool result = False d_4_i_ = int(0) # type : int d_4_i_ = 0 while (d_4_i_) < (len(l)): + # invariants-start Invariant(Acc(list_pred(l))) Invariant(((d_4_i_) >= (0)) and ((d_4_i_) <= (len(l)))) Invariant(Implies(not(result), Forall(int, lambda d_5_i1_: @@ -23,9 +30,11 @@ def pairs__sum__to__zero(l : List[int]) -> bool: Invariant(not (result) or (Exists(int, lambda d_7_i1_: Exists(int, lambda d_8_j_: (((((0) <= (d_7_i1_)) and ((d_7_i1_) < (d_4_i_))) and (((0) <= (d_8_j_)) and ((d_8_j_) < (len(l))))) and ((d_7_i1_) != (d_8_j_))) and ((((l)[d_7_i1_]) + ((l)[d_8_j_])) == (0)))))) + # invariants-end d_9_j_ = int(0) # type : int d_9_j_ = 0 while (d_9_j_) < (len(l)): + # invariants-start Invariant(Acc(list_pred(l))) Invariant(((d_4_i_) >= (0)) and ((d_4_i_) < (len(l)))) Invariant(((d_9_j_) >= (0)) and ((d_9_j_) <= (len(l)))) @@ -37,8 +46,10 @@ def pairs__sum__to__zero(l : List[int]) -> bool: (((((0) <= (d_13_i1_)) and ((d_13_i1_) < (d_4_i_))) and (((0) <= (d_14_j1_)) and ((d_14_j1_) < (len(l))))) and ((d_13_i1_) != (d_14_j1_))) and ((((l)[d_13_i1_]) + ((l)[d_14_j1_])) == (0))))) or (Exists(int, lambda d_15_j1_: ((((0) <= (d_15_j1_)) and ((d_15_j1_) < (d_9_j_))) and ((d_4_i_) != (d_15_j1_))) and ((((l)[d_4_i_]) + ((l)[d_15_j1_])) == (0)))))) + # invariants-end if ((d_4_i_) != (d_9_j_)) and ((((l)[d_4_i_]) + ((l)[d_9_j_])) == (0)): result = True d_9_j_ = (d_9_j_) + (1) d_4_i_ = (d_4_i_) + (1) - return result \ No newline at end of file + return result + # impl-end \ No newline at end of file diff --git a/Bench/046-fib4.py b/Bench/046-fib4.py index 50a78f5..b4d6002 100644 --- a/Bench/046-fib4.py +++ b/Bench/046-fib4.py @@ -3,17 +3,28 @@ @Pure def fib4__rec(n : int) -> int : + # pre-conditions-start Requires(n >= 0) + # pre-conditions-end + + # impl-start if (((n) == (0)) or ((n) == (1))) or ((n) == (2)): return 0 elif (n) == (3): return 1 elif True: return (((fib4__rec((n) - (1))) + (fib4__rec((n) - (2)))) + (fib4__rec((n) - (3)))) + (fib4__rec((n) - (4))) + # impl-end def fib4(n : int) -> int: + # pre-conditions-start Requires(n >= 0) + # pre-conditions-end + # post-conditions-start Ensures((Result()) == (fib4__rec(n))) + # post-conditions-end + + # impl-start result = int(0) # type : int if (((n) == (0)) or ((n) == (1))) or ((n) == (2)): result = 0 @@ -36,11 +47,13 @@ def fib4(n : int) -> int: d_4_i_ = int(0) # type : int d_4_i_ = 4 while (d_4_i_) <= (n): + # invariants-start Invariant(((4) <= (d_4_i_)) and ((d_4_i_) <= ((n) + (1)))) Invariant((d_0_a_) == (fib4__rec((d_4_i_) - (4)))) Invariant((d_1_b_) == (fib4__rec((d_4_i_) - (3)))) Invariant((d_2_c_) == (fib4__rec((d_4_i_) - (2)))) Invariant((d_3_d_) == (fib4__rec((d_4_i_) - (1)))) + # invariants-end d_5_temp_ = int(0) # type : int d_5_temp_ = (((d_3_d_) + (d_2_c_)) + (d_1_b_)) + (d_0_a_) d_0_a_ = d_1_b_ @@ -50,3 +63,4 @@ def fib4(n : int) -> int: d_4_i_ = (d_4_i_) + (1) result = d_3_d_ return result + # impl-end diff --git a/Bench/048-is-palindrome.py b/Bench/048-is-palindrome.py index bffb32f..f8bd4f8 100644 --- a/Bench/048-is-palindrome.py +++ b/Bench/048-is-palindrome.py @@ -2,20 +2,29 @@ from nagini_contracts.contracts import * def is__palindrome(text : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(text))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(text))) Ensures((Result()) == (Forall(int, lambda d_0_i_: not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(text)))) or (((text)[d_0_i_]) == ((text)[((len(text)) - (d_0_i_)) - (1)]))))) + # post-conditions-end + + # impl-start result = False # type : bool result = True d_1_i_ = int(0) # type : int d_1_i_ = 0 while (d_1_i_) < ((len(text) // 2)): + # invariants-start Invariant(Acc(list_pred(text))) Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= ((len(text) // 2)))) Invariant((result) == (Forall(int, lambda d_2_i1_: not (((d_2_i1_) >= (0)) and ((d_2_i1_) < (d_1_i_))) or (((text)[d_2_i1_]) == ((text)[((len(text)) - (d_2_i1_)) - (1)]))))) + # invariants-end if ((text)[d_1_i_]) != ((text)[((len(text)) - (d_1_i_)) - (1)]): result = False d_1_i_ = (d_1_i_) + (1) return result + # impl-end diff --git a/Bench/049-modp.py b/Bench/049-modp.py index ffdc44a..db1dd29 100644 --- a/Bench/049-modp.py +++ b/Bench/049-modp.py @@ -3,26 +3,42 @@ @Pure def modp__rec(n : int, p : int) -> int : + # pre-conditions-start Requires((p) > (0)) Requires((n) >= (0)) + # pre-conditions-end + + # impl-start if (n) == (0): return (1 % p) elif True: return ((modp__rec((n) - (1), p)) * (2)) % p + # impl-end def modp(n : int, p : int) -> int: + # pre-conditions-start Requires((p) > (0)) Requires((n) >= (0)) + # pre-conditions-end + # post-conditions-start Ensures((Result()) == (modp__rec(n, p))) + # post-conditions-end + + # impl-start r = int(0) # type : int r = (1 % p) d_0_i_ = int(0) # type : int d_0_i_ = 0 while (d_0_i_) < (n): + # invariants-start Invariant(((0) <= (d_0_i_)) and ((d_0_i_) <= (n))) Invariant(Forall(int, lambda d_0_i_: (Implies(d_0_i_ >= 0 and d_0_i_ < n, modp__rec(d_0_i_ + 1, p) == (modp__rec(d_0_i_, p) * 2) % p)))) Invariant((r) == (modp__rec(d_0_i_, p))) + # invariants-end + # assert-start Assert(modp__rec(d_0_i_ + 1, p) == (modp__rec(d_0_i_, p) * 2) % p) + # assert-end r = (((r) * (2)) % p) d_0_i_ = (d_0_i_) + (1) return r + # impl-end diff --git a/Bench/050-encode_shift.py b/Bench/050-encode_shift.py index 98299b9..467b78d 100644 --- a/Bench/050-encode_shift.py +++ b/Bench/050-encode_shift.py @@ -3,54 +3,76 @@ @Pure def encode__char(c : int) -> int : + # impl-start return (c - 97 + 5) % 26 + 97 + # impl-end @Pure def decode__char(c : int) -> int : + # impl-start return ((c) - (97) - (5)) % 26 + 97 + # impl-end def encode__shift(s : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(Forall(int, lambda d_0_i_: not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) or (((97) <= ((s)[d_0_i_])) and (((s)[d_0_i_]) <= (122))))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(Result()))) Ensures(Acc(list_pred(s))) Ensures((len(s)) == (len(Result()))) Ensures(Forall(int, lambda d_1_i_: not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((Result())[d_1_i_]) == (encode__char((s)[d_1_i_]))))) + # post-conditions-end + + # impl-start t : List[int] = [] # type : List[int] d_2_i_ = int(0) # type : int d_2_i_ = 0 while (d_2_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(t))) Invariant(Acc(list_pred(s))) Invariant(((0) <= (d_2_i_)) and ((d_2_i_) <= (len(s)))) Invariant((len(t)) == (d_2_i_)) Invariant(Forall(int, lambda d_3_j_: (not (((0) <= (d_3_j_)) and ((d_3_j_) < (d_2_i_))) or (((t)[d_3_j_]) == (encode__char((s)[d_3_j_]))), [[encode__char((s)[d_3_j_])]]))) + # invariants-end t = (t) + [encode__char((s)[d_2_i_])] d_2_i_ = (d_2_i_) + (1) return t + # impl-end def decode__shift(s : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(Forall(int, lambda d_4_i_: not (((0) <= (d_4_i_)) and ((d_4_i_) < (len(s)))) or (((97) <= ((s)[d_4_i_])) and (((s)[d_4_i_]) <= (122))))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(Result()))) Ensures(Acc(list_pred(s))) Ensures((len(s)) == (len(Result()))) Ensures(Forall(int, lambda d_5_i_: not (((0) <= (d_5_i_)) and ((d_5_i_) < (len(s)))) or (((Result())[d_5_i_]) == (decode__char((s)[d_5_i_]))))) + # post-conditions-end + + # impl-start t : List[int] = [] d_6_i_ = int(0) # type : int d_6_i_ = 0 while (d_6_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(t))) Invariant(Acc(list_pred(s))) Invariant(((0) <= (d_6_i_)) and ((d_6_i_) <= (len(s)))) Invariant((len(t)) == (d_6_i_)) Invariant(Forall(int, lambda d_7_j_: (not (((0) <= (d_7_j_)) and ((d_7_j_) < (d_6_i_))) or (((t)[d_7_j_]) == (decode__char((s)[d_7_j_]))), [[decode__char((s)[d_7_j_])]]))) + # invariants-end t = (t) + [decode__char((s)[d_6_i_])] d_6_i_ = (d_6_i_) + (1) return t + # impl-end diff --git a/Bench/051-remove-vowels.py b/Bench/051-remove-vowels.py index 71bedbd..49c7bf0 100644 --- a/Bench/051-remove-vowels.py +++ b/Bench/051-remove-vowels.py @@ -2,7 +2,10 @@ from nagini_contracts.contracts import * def remove__vowels(text : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(text))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(text))) Ensures(Acc(list_pred(Result()))) Ensures(Forall(int, lambda d_0_i_: @@ -11,11 +14,15 @@ def remove__vowels(text : List[int]) -> List[int]: not (((d_1_i_) >= (0)) and ((d_1_i_) < (len(Result())))) or (((Result())[d_1_i_]) in (text)))) Ensures(Forall(int, lambda d_2_j_: not ((((((((d_2_j_) >= (0)) and ((d_2_j_) < (len(text)))) and (((text)[d_2_j_]) != (0))) and (((text)[d_2_j_]) != (1))) and (((text)[d_2_j_]) != (2))) and (((text)[d_2_j_]) != (3))) and (((text)[d_2_j_]) != (4))) or (((text)[d_2_j_]) in (Result())))) + # post-conditions-end + + # impl-start s = list([int(0)] * 0) # type : List[int] s = [] d_3_i_ = int(0) # type : int d_3_i_ = 0 while (d_3_i_) < (len(text)): + # invariants-start Invariant(Acc(list_pred(text))) Invariant(Acc(list_pred(s))) Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(text)))) @@ -25,8 +32,10 @@ def remove__vowels(text : List[int]) -> List[int]: not (((d_5_i_) >= (0)) and ((d_5_i_) < (len(s)))) or (((s)[d_5_i_]) in (text)))) Invariant(Forall(int, lambda d_6_j_: not ((((((((d_6_j_) >= (0)) and ((d_6_j_) < (d_3_i_))) and (((text)[d_6_j_]) != (0))) and (((text)[d_6_j_]) != (1))) and (((text)[d_6_j_]) != (2))) and (((text)[d_6_j_]) != (3))) and (((text)[d_6_j_]) != (4))) or (((text)[d_6_j_]) in (s)))) + # invariants-end d_7_c_ = (text)[d_3_i_] # type : int if (((((d_7_c_) != (0)) and ((d_7_c_) != (1))) and ((d_7_c_) != (2))) and ((d_7_c_) != (3))) and ((d_7_c_) != (4)): s = (s) + [d_7_c_] d_3_i_ = (d_3_i_) + (1) return s + # impl-end diff --git a/Bench/052-below-threshold.py b/Bench/052-below-threshold.py index 3e78bf0..d5df927 100644 --- a/Bench/052-below-threshold.py +++ b/Bench/052-below-threshold.py @@ -2,20 +2,29 @@ from nagini_contracts.contracts import * def below__threshold(l : List[int], t : int) -> bool: + # pre-conditions-start Requires(Acc(list_pred(l))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(l))) Ensures((Result()) == (Forall(int, lambda d_0_i_: not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(l)))) or (((l)[d_0_i_]) < (t))))) + # post-conditions-end + + # impl-start b = False # type : bool b = True d_1_i_ = int(0) # type : int d_1_i_ = 0 while (d_1_i_) < (len(l)): + # invariants-start Invariant(Acc(list_pred(l))) Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(l)))) Invariant((b) == (Forall(int, lambda d_2_i1_: not (((d_2_i1_) >= (0)) and ((d_2_i1_) < (d_1_i_))) or (((l)[d_2_i1_]) < (t))))) + # invariants-end if ((l)[d_1_i_]) >= (t): b = False d_1_i_ = (d_1_i_) + (1) return b + # impl-end diff --git a/Bench/053-add.py b/Bench/053-add.py index 926889b..5c2a168 100644 --- a/Bench/053-add.py +++ b/Bench/053-add.py @@ -1,7 +1,12 @@ from nagini_contracts.contracts import * def add(x : int, y : int) -> int: + # pre-conditions-start Ensures((Result()) == ((x) + (y))) + # pre-conditions-end + + # impl-start z = int(0) # type : int z = (x) + (y) - return z \ No newline at end of file + return z + # impl-end \ No newline at end of file diff --git a/Bench/054-same-chars.py b/Bench/054-same-chars.py index 5d4f6f4..848190a 100644 --- a/Bench/054-same-chars.py +++ b/Bench/054-same-chars.py @@ -2,11 +2,18 @@ from nagini_contracts.contracts import * def same_chars(s0: List[int], s1: List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(s0))) Requires(Acc(list_pred(s1))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s0))) Ensures(Acc(list_pred(s1))) Ensures(Result() == (Forall(int, lambda d_0_i_ : Implies(((0 <= d_0_i_) and (d_0_i_ < len(s0))), s0[d_0_i_] in s1)) and Forall(int, lambda d_1_i_ : Implies(((0 <= d_1_i_) and (d_1_i_ < len(s1))), s1[d_1_i_] in s0)))) + # post-conditions-end + + # impl-start return (Forall(int, lambda d_0_i_ : Implies(((0 <= d_0_i_) and (d_0_i_ < len(s0))), s0[d_0_i_] in s1)) and - Forall(int, lambda d_1_i_ : Implies(((0 <= d_1_i_) and (d_1_i_ < len(s1))), s1[d_1_i_] in s0))) \ No newline at end of file + Forall(int, lambda d_1_i_ : Implies(((0 <= d_1_i_) and (d_1_i_ < len(s1))), s1[d_1_i_] in s0))) + # impl-end \ No newline at end of file diff --git a/Bench/055-fib.py b/Bench/055-fib.py index 4384ed8..9da57ec 100644 --- a/Bench/055-fib.py +++ b/Bench/055-fib.py @@ -3,19 +3,32 @@ @Pure def fib(n : int) -> int : + # pre-conditions-start Requires((n) >= (0)) + # pre-conditions-end + # post-conditions-start Ensures(Result() >= 0) + # post-conditions-end + + # impl-start if (n) == (0): return 0 elif (n) == (1): return 1 elif True: return (fib((n) - (1))) + (fib((n) - (2))) + # impl-end def ComputeFib(n : int) -> int: + # pre-conditions-start Requires((n) >= (0)) + # pre-conditions-end + # post-conditions-start Ensures((Result()) == (fib(n))) Ensures(Result() >= 0) + # post-conditions-end + + # impl-start result = int(0) # type : int if (n) == (0): result = 0 @@ -32,13 +45,16 @@ def ComputeFib(n : int) -> int: d_2_i_ = int(0) # type : int d_2_i_ = 2 while (d_2_i_) <= (n): + # invariants-start Invariant(((2) <= (d_2_i_)) and ((d_2_i_) <= ((n) + (1)))) Invariant((d_0_a_) == (fib((d_2_i_) - (2)))) Invariant((d_1_b_) == (fib((d_2_i_) - (1)))) + # invariants-end d_3_temp_ = int(0) # type : int d_3_temp_ = (d_0_a_) + (d_1_b_) d_0_a_ = d_1_b_ d_1_b_ = d_3_temp_ d_2_i_ = (d_2_i_) + (1) result = d_1_b_ - return result \ No newline at end of file + return result + # impl-end \ No newline at end of file diff --git a/Bench/056-correct_bracketing.py b/Bench/056-correct_bracketing.py index 8162dd0..3bf6938 100644 --- a/Bench/056-correct_bracketing.py +++ b/Bench/056-correct_bracketing.py @@ -3,26 +3,38 @@ @Pure def CalBal(s : List[int], i : int, j : int) -> int: + # pre-conditions-start Requires(Acc(list_pred(s), 1/2)) Requires(0 <= i and i <= j and j <= len(s)) + # pre-conditions-end + + # impl-start if i == j: return 0 else: return (1 if s[j - 1] == 0 else -1) + CalBal(s, i, j - 1) + # impl-end def correct_bracketing(s : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(Forall(int, lambda d_0_i_: not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(s)))) or ((((s)[d_0_i_]) == (0)) or (((s)[d_0_i_]) == (1))))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures(Forall(int, lambda d_0_i_: not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(s)))) or ((((s)[d_0_i_]) == (0)) or (((s)[d_0_i_]) == (1))))) Ensures(Implies(Result(), Forall(int, lambda x: (Implies(x >= 0 and x <= len(s), CalBal(s, 0, x) >= 0))))) Ensures(Implies(Forall(int, lambda x: (Implies(x >= 0 and x <= len(s), CalBal(s, 0, x) >= 0))), Result())) + # post-conditions-end + + # impl-start i = 0 # type : int depth = 0 # type : int result = True # type : bool while i < len(s): + # invariants-start Invariant(Acc(list_pred(s), 1/2)) Invariant(0 <= i and i <= len(s)) Invariant(Forall(int, lambda d_0_i_: @@ -32,7 +44,10 @@ def correct_bracketing(s : List[int]) -> bool: Invariant(depth == CalBal(s, 0, i)) Invariant(CalBal(s, 0, i) >= 0) Invariant(Forall(int, lambda x: (Implies(x >= 0 and x <= i, CalBal(s, 0, x) >= 0), [[CalBal(s, 0, x) >= 0]]))) + # invariants-end + # assert-start Assert(CalBal(s, 0, i + 1) == CalBal(s, 0, i) + (1 if s[i] == 0 else -1)) + # assert-end if s[i] == 0: depth = depth + 1 else: @@ -40,4 +55,5 @@ def correct_bracketing(s : List[int]) -> bool: if depth < 0: return False i = i + 1 - return result \ No newline at end of file + return result + # impl-end \ No newline at end of file diff --git a/Bench/057-monotonic.py b/Bench/057-monotonic.py index d1cffeb..dd5ca34 100644 --- a/Bench/057-monotonic.py +++ b/Bench/057-monotonic.py @@ -2,14 +2,20 @@ from nagini_contracts.contracts import * def monotonic(xs : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(xs))) Requires((len(xs)) > (0)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(xs))) Ensures((Result()) == ((Forall(int, lambda d_0_i_: Forall(int, lambda d_1_j_: not ((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len(xs)))) or (((xs)[d_0_i_]) < ((xs)[d_1_j_]))))) or (Forall(int, lambda d_2_i_: Forall(int, lambda d_3_j_: not ((((0) <= (d_2_i_)) and ((d_2_i_) < (d_3_j_))) and ((d_3_j_) < (len(xs)))) or (((xs)[d_2_i_]) > ((xs)[d_3_j_]))))))) + # post-conditions-end + + # impl-start result = False # type : bool if (len(xs)) == (1): result = True @@ -21,6 +27,7 @@ def monotonic(xs : List[int]) -> bool: d_6_i_ = int(0) # type : int d_6_i_ = 1 while (d_6_i_) < (len(xs)): + # invariants-start Invariant(Acc(list_pred(xs))) Invariant(((1) <= (d_6_i_)) and ((d_6_i_) <= (len(xs)))) Invariant((d_4_increasing_) == (Forall(int, lambda d_7_j_: @@ -29,10 +36,12 @@ def monotonic(xs : List[int]) -> bool: Invariant((d_5_decreasing_) == (Forall(int, lambda d_9_j_: Forall(int, lambda d_10_k_: not ((((0) <= (d_9_j_)) and ((d_9_j_) < (d_10_k_))) and ((d_10_k_) < (d_6_i_))) or (((xs)[d_9_j_]) > ((xs)[d_10_k_])))))) + # invariants-end if ((xs)[(d_6_i_) - (1)]) >= ((xs)[d_6_i_]): d_4_increasing_ = False if ((xs)[(d_6_i_) - (1)]) <= ((xs)[d_6_i_]): d_5_decreasing_ = False d_6_i_ = (d_6_i_) + (1) result = (d_4_increasing_) or (d_5_decreasing_) - return result \ No newline at end of file + return result + # impl-end \ No newline at end of file diff --git a/Bench/058-common.py b/Bench/058-common.py index 7b22382..7068bbf 100644 --- a/Bench/058-common.py +++ b/Bench/058-common.py @@ -3,30 +3,47 @@ @Pure def NotInArray(a : List[int], x : int) -> bool : + # pre-conditions-start Requires(Acc(list_pred(a))) + # pre-conditions-end + + # impl-start return Forall(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))), ((a)[d_0_i_]) != (x)), [[(a)[d_0_i_]]])) + # impl-end @Pure def ExistsBoth(a : List[int], b : List[int], x : int) -> bool: + # pre-conditions-start Requires(Acc(list_pred(a), 1/2)) Requires(Acc(list_pred(b), 1/2)) + # pre-conditions-end + + # impl-start return Exists(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))), ((a)[d_0_i_]) == (x)))) and Exists(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len((b)))), ((b)[d_0_i_]) == (x)))) + # impl-end def common(l1 : List[int], l2 : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(l2), 1/2)) Requires(Acc(list_pred(l1), 1/2)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(l2), 1/2)) Ensures(Acc(list_pred(l1), 1/2)) Ensures(Acc(list_pred(Result()))) Ensures(Forall(int, lambda d_4_j_: (Implies(((d_4_j_) >= 0 and d_4_j_ < len(l1)), Implies((Exists(int, lambda x: x >= 0 and x< len(l2) and l2[x] == l1[d_4_j_])), Exists(int, lambda x: x >= 0 and x< len(Result()) and Result()[x] == l1[d_4_j_])))))) + # post-conditions-end + + # impl-start c = list([int(0)] * 0) # type : List[int] d_2_i_ = int(0) # type : int d_2_i_ = 0 while (d_2_i_) < (len(l1)): + # invariants-start Invariant(Acc(list_pred(c))) Invariant(Acc(list_pred(l2), 1/2)) Invariant(Acc(list_pred(l1), 1/2)) @@ -36,10 +53,11 @@ def common(l1 : List[int], l2 : List[int]) -> List[int]: [[l1[d_4_j_]]]))) Invariant(Forall(int, lambda d_0_i_: (Implies((d_0_i_) >= 0 and d_0_i_ < len(c), ExistsBoth(l1, l2, c[d_0_i_])), [[ExistsBoth(l1, l2, c[d_0_i_])]]))) - + # invariants-end d_5_j_ = int(0) # type : int d_5_j_ = 0 while (d_5_j_) < (len(l2)): + # invariants-start Invariant(Acc(list_pred(c))) Invariant(Acc(list_pred(l2), 1/2)) Invariant(Acc(list_pred(l1), 1/2)) @@ -51,11 +69,15 @@ def common(l1 : List[int], l2 : List[int]) -> List[int]: Invariant(Implies(Exists(int, lambda x: x >= 0 and x < d_5_j_ and l2[x] == l1[d_2_i_]), Exists(int, lambda x: x >= 0 and x < len(c) and c[x] == l1[d_2_i_]))) Invariant(Forall(int, lambda d_0_i_: (Implies((d_0_i_) >= 0 and d_0_i_ < len(c), ExistsBoth(l1, l2, c[d_0_i_])), [[ExistsBoth(l1, l2, c[d_0_i_])]]))) + # invariants-end if ((l1)[d_2_i_]) == ((l2)[d_5_j_]) and NotInArray(c, (l1)[d_2_i_]): c = c + [((l1)[d_2_i_])] + # assert-start Assert((Exists(int, lambda x : x >= 0 and x < len(l1) and (c[len(c) - 1]) == (l1[x])))) Assert((Exists(int, lambda x : x >= 0 and x < len(l2) and (c[len(c) - 1]) == (l2[x])))) Assert(ExistsBoth(l1, l2, c[len(c) - 1])) + # assert-end d_5_j_ = (d_5_j_) + (1) d_2_i_ = (d_2_i_) + (1) return c + # impl-end diff --git a/Bench/059-largest-prime-factor.py b/Bench/059-largest-prime-factor.py index 05da467..8316acf 100644 --- a/Bench/059-largest-prime-factor.py +++ b/Bench/059-largest-prime-factor.py @@ -1,34 +1,54 @@ from nagini_contracts.contracts import * def is_prime(k : int) -> bool: + # pre-conditions-start Requires(k >= 2) + # pre-conditions-end + # post-conditions-start Ensures(Implies(Result(), Forall(int, lambda i : Implies((i >= 2) and (i < k), (k % i != 0))))) Ensures(Implies(Result() == False, Exists(int, lambda j : ((j >= 2) and (j < k) and (k % j == 0))))) + # post-conditions-end + + # impl-start i = 2 # type : int result = True # type : bool while i < k: + # invariants-start Invariant(i >= 2 and i <= k) Invariant(Implies(result == False, Exists(int, lambda j : ((j >= 2) and (j < i) and (k % j == 0))))) Invariant(Implies(result, Forall(int, lambda j : Implies((j >= 2) and (j < i), (k % j != 0))))) + # invariants-end if k % i == 0: result = False i = i + 1 return result + # impl-end @Pure def is_prime_pred(k : int) -> bool: + # impl-start return Forall(int, lambda i : Implies((i >= 2) and (i < k), (k % i != 0))) + # impl-end def largest_prime_factor(n: int) -> int: + # pre-conditions-start Requires(n >= 2) + # pre-conditions-end + # post-conditions-start Ensures(Result() >= 1 and Result() <= n and (Result() == 1 or Result() > 1 and is_prime_pred(Result()))) + # post-conditions-end + + # impl-start largest = 1 # type : int j = 2 # type : int while j <= n: + # invariants-start Invariant(j >= 2 and j <= n + 1) Invariant(largest >= 1 and largest < j) Invariant(largest == 1 or largest > 1 and is_prime_pred(largest)) + # invariants-end if n % j == 0 and is_prime(j): largest = max(largest, j) j = j + 1 - return largest \ No newline at end of file + return largest + # impl-end \ No newline at end of file diff --git a/Bench/060-sum-to-n.py b/Bench/060-sum-to-n.py index ae5c604..b91ff59 100644 --- a/Bench/060-sum-to-n.py +++ b/Bench/060-sum-to-n.py @@ -3,22 +3,36 @@ @Pure def psum(i : int, j : int) -> int : + # pre-conditions-start Requires(0 <= i and i <= j + 1) + # pre-conditions-end + + # impl-start if i > j: return 0 else: return j + 1 + (psum(i, j - 1)) + # impl-end def sum__squares(n : int) -> int: + # pre-conditions-start Requires((n) >= (1)) + # pre-conditions-end + # post-conditions-start Ensures((Result()) == (psum(0, n - 1))) + # post-conditions-end + + # impl-start r = int(0) # type : int r = 0 d_2_k_ = int(0) # type : int d_2_k_ = 0 while (d_2_k_) < (n): + # invariants-start Invariant(((0) <= (d_2_k_)) and ((d_2_k_) <= (n))) Invariant((r) == (psum(0, d_2_k_ - 1))) + # invariants-end r = (r) + ((d_2_k_) + (1)) d_2_k_ = (d_2_k_) + (1) - return r \ No newline at end of file + return r + # impl-end \ No newline at end of file diff --git a/Bench/062-derivative.py b/Bench/062-derivative.py index e8c7f6b..2b023f7 100644 --- a/Bench/062-derivative.py +++ b/Bench/062-derivative.py @@ -2,24 +2,33 @@ from nagini_contracts.contracts import * def derivative(xs : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(xs))) Requires((len(xs)) > (0)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(xs))) Ensures(Acc(list_pred(Result()))) Ensures((len(Result())) == ((len(xs)) - (1))) Ensures(Forall(int, lambda d_0_i_: not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(Result())))) or (((Result())[d_0_i_]) == (((xs)[(d_0_i_) + (1)]) * ((d_0_i_) + (1)))))) + # post-conditions-end + + # impl-start result = list([int(0)] * 0) # type : List[int] result = list([]) d_1_i_ = int(0) # type : int d_1_i_ = 1 while (d_1_i_) < (len(xs)): + # invariants-start Invariant(Acc(list_pred(result))) Invariant(Acc(list_pred(xs))) Invariant(((1) <= (d_1_i_)) and ((d_1_i_) <= (len(xs)))) Invariant((len(result)) == ((d_1_i_) - (1))) Invariant(Forall(int, lambda d_2_j_: not (((0) <= (d_2_j_)) and ((d_2_j_) < (len(result)))) or (((result)[d_2_j_]) == (((xs)[(d_2_j_) + (1)]) * ((d_2_j_) + (1)))))) + # invariants-end result = (result) + [((xs)[d_1_i_]) * (d_1_i_)] d_1_i_ = (d_1_i_) + (1) return result + # impl-end diff --git a/Bench/063-fibfib.py b/Bench/063-fibfib.py index 98e4f1b..9f9350b 100644 --- a/Bench/063-fibfib.py +++ b/Bench/063-fibfib.py @@ -3,19 +3,32 @@ @Pure def fibfib(n : int) -> int : + # pre-conditions-start Requires((n) >= (0)) + # pre-conditions-end + # post-conditions-start Ensures((Result()) >= (0)) + # post-conditions-end + + # impl-start if ((n) == (0)) or ((n) == (1)): return 0 elif (n) == (2): return 1 elif True: return ((fibfib((n) - (1))) + (fibfib((n) - (2)))) + (fibfib((n) - (3))) + # impl-end def ComputeFibFib(n : int) -> int: + # pre-conditions-start Requires((n) >= (0)) + # pre-conditions-end + # post-conditions-start Ensures((Result()) >= (0)) Ensures((Result()) == (fibfib(n))) + # post-conditions-end + + # impl-start result = int(0) # type : int if ((n) == (0)) or ((n) == (1)): result = 0 @@ -35,10 +48,12 @@ def ComputeFibFib(n : int) -> int: d_3_i_ = int(0) # type : int d_3_i_ = 3 while (d_3_i_) <= (n): + # invariants-start Invariant(((3) <= (d_3_i_)) and ((d_3_i_) <= ((n) + (1)))) Invariant((d_0_a_) == (fibfib((d_3_i_) - (3)))) Invariant((d_1_b_) == (fibfib((d_3_i_) - (2)))) Invariant((d_2_c_) == (fibfib((d_3_i_) - (1)))) + # invariants-end d_4_temp_ = int(0) # type : int d_4_temp_ = ((d_2_c_) + (d_1_b_)) + (d_0_a_) d_0_a_ = d_1_b_ @@ -47,3 +62,4 @@ def ComputeFibFib(n : int) -> int: d_3_i_ = (d_3_i_) + (1) result = d_2_c_ return result + # impl-end diff --git a/Bench/064-vowel_count.py b/Bench/064-vowel_count.py index 2e72391..81d1399 100644 --- a/Bench/064-vowel_count.py +++ b/Bench/064-vowel_count.py @@ -2,33 +2,50 @@ from nagini_contracts.contracts import * def vowel__count(s : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures((Result()) >= (0)) Ensures(Result() == ((count_fun(0, len(s), s)) + ((1 if ((len(s)) > (0)) and (((s)[(len(s)) - (1)]) == (121)) else 0)))) + # post-conditions-end + + # impl-start count = int(0) # type : int count = 0 d_1_i_ = int(0) # type : int d_1_i_ = 0 while (d_1_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(s)))) Invariant(count >= 0) Invariant(Forall(int, lambda d_0_i_: (Implies(d_0_i_ >= 0 and d_0_i_ < len(s), count_fun(0, d_0_i_ + 1, s) == count_fun(0, d_0_i_, s) + (1 if is__vowel(s[d_0_i_]) else 0)), [[count_fun(0, d_0_i_ + 1, s)]]))) Invariant((count) == (count_fun(0, d_1_i_, s))) + # invariants-end + # assert-start Assert(count_fun(0, d_1_i_ + 1, s) == count_fun(0, d_1_i_, s) + (1 if is__vowel(s[d_1_i_]) else 0)) + # assert-end if is__vowel((s)[d_1_i_]): count = (count) + (1) d_1_i_ = (d_1_i_) + (1) count = (count) + ((1 if ((len(s)) > (0)) and (((s)[(len(s)) - (1)]) == (121)) else 0)) return count + # impl-end @Pure def count_fun(i : int, j : int, s : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(((0) <= (i)) and ((i) <= (j)) and ((j) <= (len(s)))) + # pre-conditions-end + # post-conditions-start Ensures((Result()) >= (0)) + # post-conditions-end + + # impl-start if i == j: return 0 else: @@ -36,7 +53,10 @@ def count_fun(i : int, j : int, s : List[int]) -> int: return (1) + (count_fun(i, j - 1, s)) else: return count_fun(i, j - 1, s) + # impl-end @Pure def is__vowel(c : int) -> bool : + # impl-start return ((((((((((c) == (97)) or ((c) == (101))) or ((c) == (105))) or ((c) == (111))) or ((c) == (117))) or ((c) == (65))) or ((c) == (69))) or ((c) == (73))) or ((c) == (79))) or ((c) == (85)) + # impl-end \ No newline at end of file diff --git a/Bench/066-digitSum.py b/Bench/066-digitSum.py index f5632a8..9638576 100644 --- a/Bench/066-digitSum.py +++ b/Bench/066-digitSum.py @@ -3,27 +3,41 @@ @Pure def upper__sum__rec(i : int, j : int, s : List[int]) -> int : + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(((0) <= (i)) and ((i) <= (j)) and ((j) <= (len(s)))) + # pre-conditions-end + + # impl-start if i == j: return 0 else: return s[j - 1] + upper__sum__rec(i, j - 1, s) + # impl-end def upper__sum(s : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures((Result()) == (upper__sum__rec(0, len(s), s))) + # post-conditions-end + + # impl-start res = int(0) # type : int res = 0 d_1_i_ = int(0) # type : int d_1_i_ = 0 while (d_1_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(s)))) Invariant(Forall(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len(s))), upper__sum__rec(0, d_0_i_ + 1, s) == upper__sum__rec(0, d_0_i_, s) + s[d_0_i_]), [[upper__sum__rec(0, d_1_i_ + 1, s)]]))) Invariant((res) == (upper__sum__rec(0, d_1_i_, s))) + # invariants-end res = (res) + (((s)[d_1_i_])) d_1_i_ = (d_1_i_) + (1) return res + # impl-end diff --git a/Bench/068-pluck.py b/Bench/068-pluck.py index d135e9a..61018af 100644 --- a/Bench/068-pluck.py +++ b/Bench/068-pluck.py @@ -2,10 +2,13 @@ from nagini_contracts.contracts import * def PluckSmallestEven(nodes : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(nodes))) Requires((len(nodes)) <= (10000)) Requires(Forall(int, lambda d_0_i_: not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(nodes)))) or (((nodes)[d_0_i_]) >= (0)))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(nodes))) Ensures(Acc(list_pred(Result()))) Ensures(((len(Result())) == (0)) or ((len(Result())) == (2))) @@ -17,6 +20,9 @@ def PluckSmallestEven(nodes : List[int]) -> List[int]: not (((0) <= (d_2_i_)) and ((d_2_i_) < ((Result())[1]))) or (((((nodes)[d_2_i_] % 2)) != (0)) or (((nodes)[d_2_i_]) > ((Result())[0])))))) Ensures(not ((len(Result())) == (0)) or (Forall(int, lambda d_3_i_: not (((0) <= (d_3_i_)) and ((d_3_i_) < (len(nodes)))) or ((((nodes)[d_3_i_] % 2)) != (0))))) + # post-conditions-end + + # impl-start result = list([int(0)] * 0) # type : List[int] d_4_smallestEven_ = int(0) # type : int d_4_smallestEven_ = -1 @@ -24,6 +30,7 @@ def PluckSmallestEven(nodes : List[int]) -> List[int]: d_5_smallestIndex_ = -1 d_6_i_ = int(0) # type : int while d_6_i_ < len(nodes): + # invariants-start Invariant(Acc(list_pred(result))) Invariant(Acc(list_pred(nodes))) Invariant(((0) <= (d_6_i_)) and ((d_6_i_) <= (len(nodes)))) @@ -37,6 +44,7 @@ def PluckSmallestEven(nodes : List[int]) -> List[int]: (not (((0) <= (d_8_j_)) and ((d_8_j_) < (d_5_smallestIndex_))) or ((((nodes)[d_8_j_] % 2)) != (0)) or (((nodes)[d_8_j_]) > (d_4_smallestEven_)), [[(nodes)[d_8_j_]]])))) Invariant(not ((d_5_smallestIndex_) == (-1)) or (Forall(int, lambda d_9_j_: (not (((0) <= (d_9_j_)) and ((d_9_j_) < (d_6_i_))) or ((((nodes)[d_9_j_] % 2)) != (0)), [[(nodes)[d_9_j_]]])))) + # invariants-end if ((((nodes)[d_6_i_] % 2)) == (0)) and (((d_4_smallestEven_) == (-1)) or (((nodes)[d_6_i_]) < (d_4_smallestEven_))): d_4_smallestEven_ = (nodes)[d_6_i_] d_5_smallestIndex_ = d_6_i_ @@ -47,3 +55,4 @@ def PluckSmallestEven(nodes : List[int]) -> List[int]: else: result = list([d_4_smallestEven_, d_5_smallestIndex_]) return result + # impl-end diff --git a/Bench/069-search.py b/Bench/069-search.py index fae2dcb..8d1aed4 100644 --- a/Bench/069-search.py +++ b/Bench/069-search.py @@ -3,28 +3,44 @@ @Pure def freq_req(i : int, j : int, s : List[int], x : int) -> int: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(0 <= i and i <= j and j <= len(s)) + # pre-conditions-end + + # impl-start if i == j: return 0 else: return ((s)[j - 1] == x) + (freq_req(i, j - 1, s, x)) + # impl-end def freq(s : List[int], x : int) -> int: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures(Result() == freq_req(0, len(s), s, x)) + # post-conditions-end + + # impl-start count = int(0) # type : int count = 0 d_1_i_ = int(0) # type : int d_1_i_ = 0 while (d_1_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(s)))) Invariant(Forall(int, lambda y : (Implies(y >= 0 and y < len(s), freq_req(0, y + 1, s, x) == freq_req(0, y, s, x) + (s[y] == x)), [[freq_req(0, y + 1, s, x)]]))) Invariant(count == freq_req(0, d_1_i_, s, x)) + # invariants-end + # assert-start Assert(freq_req(0, d_1_i_ + 1, s, x) == freq_req(0, d_1_i_, s, x) + (s[d_1_i_] == x)) + # assert-end if ((s)[d_1_i_]) == (x): count = (count) + (1) d_1_i_ = (d_1_i_) + (1) return count + # impl-end diff --git a/Bench/070-strange_sort_list.py b/Bench/070-strange_sort_list.py index f382d8c..b6b2719 100644 --- a/Bench/070-strange_sort_list.py +++ b/Bench/070-strange_sort_list.py @@ -2,11 +2,13 @@ from nagini_contracts.contracts import * def strange__sort__list__helper(s : List[int]) -> Tuple[List[int], List[int]]: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures(Acc(list_pred(Result()[0]))) Ensures(Acc(list_pred(Result()[1]))) - # Ensures(ToMS(ToSeq(s)) == ToMS(ToSeq(Result()[0]))) Ensures(Forall(int, lambda d_0_i_: Forall(int, lambda d_1_j_: Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((Result()[0])))), ((Result()[0])[d_0_i_]) <= ((Result()[0])[d_1_j_]))))) @@ -15,12 +17,16 @@ def strange__sort__list__helper(s : List[int]) -> Tuple[List[int], List[int]]: not ((((0) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) and (((d_0_i_ % 2)) == (0))) or (((Result()[1])[d_0_i_]) == ((Result()[0])[(d_0_i_ // 2)])))) Ensures(Forall(int, lambda d_1_i_: not ((((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) and (((d_1_i_ % 2)) == (1))) or (((Result()[1])[d_1_i_]) == ((Result()[0])[((len(s)) - ((((d_1_i_) - (1)) // 2))) - (1)])))) + # post-conditions-end + + # impl-start sorted = list([int(0)] * 0) # type : List[int] sorted = BubbleSort(s) # type : List[int] strange = list(s) # type : List[int] d_2_i_ = int(0) # type : int d_2_i_ = 0 while (d_2_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(strange))) Invariant(Acc(list_pred(sorted))) Invariant(Acc(list_pred(s))) @@ -34,6 +40,7 @@ def strange__sort__list__helper(s : List[int]) -> Tuple[List[int], List[int]]: (Implies((((0) <= (d_3_j_)) and ((d_3_j_) < (d_2_i_))) and (((d_3_j_ % 2)) == (0)), ((strange)[d_3_j_]) == ((sorted)[(d_3_j_ // 2)])), [[(strange)[d_3_j_]]]))) Invariant(Forall(int, lambda d_4_j_: (Implies((((0) <= (d_4_j_)) and ((d_4_j_) < (d_2_i_))) and (((d_4_j_ % 2)) == (1)), ((strange)[d_4_j_]) == ((sorted)[((len(s)) - ((((d_4_j_) - (1)) // 2))) - (1)])), [[(strange)[d_4_j_]]]))) + # invariants-end if ((d_2_i_ % 2)) == (0): strange[d_2_i_] = (sorted)[(d_2_i_ // 2)] else: @@ -42,28 +49,42 @@ def strange__sort__list__helper(s : List[int]) -> Tuple[List[int], List[int]]: strange[d_2_i_] = (sorted)[((len(s)) - (d_5_r_)) - (1)] d_2_i_ = (d_2_i_) + (1) return (sorted, strange) + # impl-end def strange__sort__list(s : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures(Acc(list_pred(Result()))) Ensures((len(s)) == (len(Result()))) + # post-conditions-end + + # impl-start p = strange__sort__list__helper(s) # type : Tuple[List[int], List[int]] return p[1] + # impl-end def BubbleSort(a1 : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(a1))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(a1))) Ensures(Acc(list_pred(Result()))) Ensures((len(a1)) == (len(Result()))) - # Ensures(ToMS(ToSeq(a1)) == ToMS(ToSeq(Result()))) Ensures(Forall(int, lambda d_0_i_: Forall(int, lambda d_1_j_: Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((Result())))), ((Result())[d_0_i_]) <= ((Result())[d_1_j_]))))) + # post-conditions-end + + # impl-start a = list(a1) # type : List[int] d_2_i_ = int(0) # type : int d_2_i_ = (len((a))) - (1) while (d_2_i_) > (0): + # invariants-start Invariant(Acc(list_pred(a))) Invariant(Acc(list_pred(a1))) Invariant((len(a1)) == (len(a))) @@ -79,9 +100,11 @@ def BubbleSort(a1 : List[int]) -> List[int]: (Implies(((((0) <= (d_5_k_)) and ((d_5_k_) <= (d_2_i_))) and ((d_2_i_) < (d_6_k_k_)) and (d_6_k_k_) < (len((a)))), ((a)[d_5_k_]) <= ((a)[d_6_k_k_])), [[(a)[d_6_k_k_]]])), [[(a)[d_5_k_]]]))) + # invariants-end d_7_j_ = int(0) # type : int d_7_j_ = 0 while (d_7_j_) < (d_2_i_): + # invariants-start Invariant(Acc(list_pred(a))) Invariant(Acc(list_pred(a1))) Invariant((len(a1)) == (len(a))) @@ -99,6 +122,7 @@ def BubbleSort(a1 : List[int]) -> List[int]: Invariant(Forall(int, lambda d_12_k_: (Implies(((0) <= (d_12_k_)) and ((d_12_k_) <= (d_7_j_)), ((a)[d_12_k_]) <= ((a)[d_7_j_])), [[(a)[d_12_k_]]]))) + # invariants-end if ((a)[d_7_j_]) > ((a)[(d_7_j_) + (1)]): rhs0_ = (a)[(d_7_j_) + (1)] # type : int (a)[(d_7_j_) + (1)] = (a)[d_7_j_] @@ -106,3 +130,4 @@ def BubbleSort(a1 : List[int]) -> List[int]: d_7_j_ = (d_7_j_) + (1) d_2_i_ = (d_2_i_) - (1) return a + # impl-end diff --git a/Bench/072-will_it_fly.py b/Bench/072-will_it_fly.py index 8793e95..8e05bdd 100644 --- a/Bench/072-will_it_fly.py +++ b/Bench/072-will_it_fly.py @@ -2,10 +2,16 @@ from nagini_contracts.contracts import * def will__it__fly(s : List[int], w : int) -> bool: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires((len(s)) > (0)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures((Result()) == ((is__palindrome__pred(s)) and ((psum(0, len(s), s)) <= (w)))) + # post-conditions-end + + # impl-start result = False # type : bool result = True d_0_i_ = int(0) # type : int @@ -13,12 +19,14 @@ def will__it__fly(s : List[int], w : int) -> bool: d_1_j_ = int(0) # type : int d_1_j_ = (len(s)) - (1) while (d_0_i_) < (d_1_j_): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(((0) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) Invariant(((0) <= (d_1_j_)) and ((d_1_j_) < (len(s)))) Invariant((d_1_j_) == (((len(s)) - (d_0_i_)) - (1))) Invariant(Forall(int, lambda d_2_k_: not (((0) <= (d_2_k_)) and ((d_2_k_) < (d_0_i_))) or (((s)[d_2_k_]) == ((s)[((len(s)) - (1)) - (d_2_k_)])))) + # invariants-end if ((s)[d_0_i_]) != ((s)[d_1_j_]): result = False return result @@ -28,28 +36,44 @@ def will__it__fly(s : List[int], w : int) -> bool: d_3_total_ = 0 d_0_i_ = 0 while (d_0_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(((0) <= (d_0_i_)) and ((d_0_i_) <= (len(s)))) Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(s)))) or ((psum(0, d_2_i_ + 1, s)) == (psum(0, d_2_i_, s) + s[d_2_i_])), [[psum(0, d_2_i_ + 1, s)]]))) Invariant((d_3_total_) == (psum(0, d_0_i_, s))) Invariant(Forall(int, lambda d_2_k_: not (((0) <= (d_2_k_)) and ((d_2_k_) < (len(s)))) or (((s)[d_2_k_]) == ((s)[((len(s)) - (1)) - (d_2_k_)])))) + # invariants-end + + # assert-start Assert((psum(0, (d_0_i_) + (1), s)) == ((psum(0, d_0_i_, s) + (s)[d_0_i_]))) + # assert-end d_3_total_ = (d_3_total_) + ((s)[d_0_i_]) d_0_i_ = (d_0_i_) + (1) return (d_3_total_) <= (w) + # impl-end @Pure def is__palindrome__pred(s : List[int]) -> bool : + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + + # impl-start return Forall(int, lambda d_4_k_: not (((0) <= (d_4_k_)) and ((d_4_k_) < (len(s)))) or (((s)[d_4_k_]) == ((s)[((len(s)) - (1)) - (d_4_k_)]))) + # impl-end @Pure def psum(i : int, j : int, s : List[int]) -> int : + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(0 <= i and i <= j and j <= len(s)) + # pre-conditions-end + + # impl-start if i == j: return 0 else: return (s)[j - 1] + (psum(i, j - 1, s)) + # impl-end diff --git a/Bench/073-smallest_change.py b/Bench/073-smallest_change.py index 84923c4..6927c5f 100644 --- a/Bench/073-smallest_change.py +++ b/Bench/073-smallest_change.py @@ -3,9 +3,15 @@ @Pure def smallest__change__fun(s : List[int], i : int, j : int) -> int: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(((0) <= (i)) and ((i) <= (j)) and ((j) <= (len(s) // 2))) + # pre-conditions-end + # post-conditions-start Ensures((Result()) >= (0)) + # post-conditions-end + + # impl-start if i == j: return 0 else: @@ -13,22 +19,35 @@ def smallest__change__fun(s : List[int], i : int, j : int) -> int: return 1 + (smallest__change__fun(s, i, j - 1)) else: return smallest__change__fun(s, i, j - 1) + # impl-end def smallest__change(s : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures((Result()) == (smallest__change__fun(s, 0, len(s) // 2))) + # post-conditions-end + + # impl-start c = int(0) # type : int c = 0 d_1_i_ = int(0) # type : int d_1_i_ = 0 while (d_1_i_) < ((len(s) // 2)): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= ((len(s) // 2)))) Invariant(Forall(int, lambda d_0_i_: (Implies(d_0_i_ >= 0 and d_0_i_ < len(s) // 2, smallest__change__fun(s, 0, d_0_i_ + 1) == (smallest__change__fun(s, 0, d_0_i_) + 1 if (s)[d_0_i_] != (s)[len(s) - d_0_i_ - 1] else smallest__change__fun(s, 0, d_0_i_))), [[smallest__change__fun(s, 0, d_0_i_ + 1)]]))) Invariant(c == smallest__change__fun(s, 0, d_1_i_)) + # invariants-end + + # assert-start Assert(smallest__change__fun(s, 0, d_1_i_ + 1) == (smallest__change__fun(s, 0, d_1_i_) + 1 if (s)[d_1_i_] != (s)[len(s) - d_1_i_ - 1] else smallest__change__fun(s, 0, d_1_i_))) + # assert-end if ((s)[d_1_i_]) != ((s)[((len(s)) - (1)) - (d_1_i_)]): c = (c) + (1) d_1_i_ = (d_1_i_) + (1) - return c \ No newline at end of file + return c + # impl-end \ No newline at end of file diff --git a/Bench/075-is_multiply_prime.py b/Bench/075-is_multiply_prime.py index 75db8c9..13f205a 100644 --- a/Bench/075-is_multiply_prime.py +++ b/Bench/075-is_multiply_prime.py @@ -3,33 +3,47 @@ @Pure def Prime(p : int) -> bool : + # impl-start return ((p) > (1)) and (Forall(int, lambda d_0_k_: not (((1) < (d_0_k_)) and ((d_0_k_) < (p))) or (((p % d_0_k_)) != (0)))) + # impl-end def is__prime(k : int) -> bool: + # pre-conditions-start Requires((k) >= (2)) + # pre-conditions-end + # post-conditions-start Ensures(not (Result()) or (Forall(int, lambda d_0_i_: not (((2) <= (d_0_i_)) and ((d_0_i_) < (k))) or ((k % d_0_i_) != (0))))) Ensures(not (not(Result())) or (Exists(int, lambda d_1_j_: (((2) <= (d_1_j_)) and ((d_1_j_) < (k))) and (((k % d_1_j_)) == (0))))) Ensures(Result() == Prime(k)) + # post-conditions-end + + # impl-start result = False # type : bool d_2_i_ = int(0) # type : int d_2_i_ = 2 result = True while (d_2_i_) < (k): + # invariants-start Invariant(((2) <= (d_2_i_)) and ((d_2_i_) <= (k))) Invariant(not (not(result)) or (Exists(int, lambda d_3_j_: (((2) <= (d_3_j_)) and ((d_3_j_) < (d_2_i_))) and (((k % d_3_j_)) == (0))))) Invariant(not (result) or (Forall(int, lambda d_4_j_: not (((2) <= (d_4_j_)) and ((d_4_j_) < (d_2_i_))) or (((k % d_4_j_)) != (0))))) + # invariants-end if ((k % d_2_i_)) == (0): result = False d_2_i_ = (d_2_i_) + (1) return result + # impl-end def is__multiply__prime(x : int) -> bool: + # pre-conditions-start Requires((x) > (1)) + # pre-conditions-end + # post-conditions-start Ensures(Implies(Result(), Exists(int, lambda d_1_a_: d_1_a_< x and ((Prime(d_1_a_)) and @@ -45,9 +59,13 @@ def is__multiply__prime(x : int) -> bool: (Forall(int, lambda d_12_k_: (Implies(1 < d_12_k_ and d_12_k_ < x, Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), Implies(Prime(d_12_k_), ((x) != (((d_10_i_) * (d_11_j_) * (d_12_k_))))))))))))))))))) + # post-conditions-end + + # impl-start d_4_a_ = int(2) # type : int result = False # type : bool while d_4_a_ < x: + # invariants-start Invariant(x >= 2) Invariant(d_4_a_ >= 2 and d_4_a_ <= x) Invariant(Implies(result, Exists(int, lambda d_1_a_: @@ -68,9 +86,11 @@ def is__multiply__prime(x : int) -> bool: [[Prime(d_12_k_)]])))), [[Prime(d_11_j_)]]))), [[Prime(d_10_i_)]])))) + # invariants-end if is__prime(d_4_a_): d_5_b_ = int(2) # type : int while d_5_b_ < x: + # invariants-start Invariant(x >= 2) Invariant(Prime(d_4_a_)) Invariant(d_4_a_ >= 2 and d_4_a_ < x) @@ -101,9 +121,11 @@ def is__multiply__prime(x : int) -> bool: Implies((Prime(d_11_j_)), Implies(Prime(d_12_k_), (x) != (((d_4_a_) * (d_11_j_) * (d_12_k_)))))), [[Prime(d_12_k_)]])))), [[Prime(d_11_j_)]])))) + # invariants-end if is__prime(d_5_b_): d_6_c_ = int(2) # type : int while d_6_c_ < x: + # invariants-start Invariant(x >= 2) Invariant(Prime(d_4_a_)) Invariant(Prime(d_5_b_)) @@ -141,9 +163,11 @@ def is__multiply__prime(x : int) -> bool: (Implies(1 < d_12_k_ and d_12_k_ < d_6_c_, Implies((Prime(d_12_k_)), ((x) != (((d_4_a_) * (d_5_b_) * (d_12_k_)))))), [[Prime(d_12_k_)]])))) + # invariants-end if (is__prime(d_6_c_)) and ((x) == (((d_4_a_) * (d_5_b_)) * (d_6_c_))): result = True d_6_c_ = d_6_c_ + 1 d_5_b_ = d_5_b_ + 1 d_4_a_ = d_4_a_ + 1 return result + # impl-end diff --git a/Bench/078-hex_key.py b/Bench/078-hex_key.py index 8ce767b..12cd19c 100644 --- a/Bench/078-hex_key.py +++ b/Bench/078-hex_key.py @@ -3,34 +3,53 @@ @Pure def IsPrimeHexDigit(c : int) -> bool : + # impl-start return ((((((c) == (2)) or ((c) == (3))) or ((c) == (5))) or ((c) == (7))) or ((c) == (11))) or ((c) == (13)) + # impl-end @Pure def count__prime__hex__digits__rec(i : int, j : int, num : List[int]) -> int : + # pre-conditions-start Requires(Acc(list_pred(num))) Requires(((0) <= (i)) and ((i) <= (j)) and ((j) <= (len(num)))) + # pre-conditions-end + + # impl-start if i == j: return 0 else: return (1 if IsPrimeHexDigit(num[j - 1]) else 0) + count__prime__hex__digits__rec(i, j - 1, num) + # impl-end def count__prime__hex__digits(s : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures((Result()) == (count__prime__hex__digits__rec(0, len(s), s))) Ensures(((0) <= (Result())) and ((Result()) <= (len(s)))) + # post-conditions-end + + # impl-start count = int(0) # type : int count = 0 d_1_i_ = int(0) # type : int d_1_i_ = 0 while (d_1_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(s)))) Invariant((count) == (count__prime__hex__digits__rec(0, d_1_i_, s))) Invariant(count >= 0 and count <= d_1_i_) Invariant(Forall(int, lambda x : (Implies(x >= 0 and x < len(s), (count__prime__hex__digits__rec(0, x + 1, s)) == ((count__prime__hex__digits__rec(0, x, s) + ((1 if IsPrimeHexDigit((s)[x]) else 0))))), [[count__prime__hex__digits__rec(0, x + 1, s)]]))) + # invariants-end + + # assert-start Assert((count__prime__hex__digits__rec(0, d_1_i_ + 1, s)) == ((count__prime__hex__digits__rec(0, d_1_i_, s) + ((1 if IsPrimeHexDigit((s)[d_1_i_]) else 0))))) + # assert-end count = (count) + ((1 if IsPrimeHexDigit((s)[d_1_i_]) else 0)) d_1_i_ = (d_1_i_) + (1) return count + # impl-end diff --git a/Bench/080-is_happy.py b/Bench/080-is_happy.py index 07ca060..ad6b458 100644 --- a/Bench/080-is_happy.py +++ b/Bench/080-is_happy.py @@ -3,20 +3,36 @@ @Pure def ThreeDistinct(s : List[int], i : int) -> bool : + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(((0) < (i)) and ((i) < ((len(s)) - (1)))) + # pre-conditions-end + + # impl-start return ((((s)[(i) - (1)]) != ((s)[i])) and (((s)[i]) != ((s)[(i) + (1)]))) and (((s)[(i) - (1)]) != ((s)[(i) + (1)])) + # impl-end @Pure def Happy(s : List[int]) -> bool : + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + + # impl-start return ((len(s)) >= (3)) and (Forall(int, lambda d_0_i_: Implies(((0) < (d_0_i_)) and ((d_0_i_) < ((len(s)) - (1))), ThreeDistinct(s, d_0_i_)))) + # impl-end def IsHappy(s : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures((Result()) == (Happy(s))) + # post-conditions-end + + # impl-start happy = False # type : bool if (len(s)) < (3): happy = False @@ -24,14 +40,17 @@ def IsHappy(s : List[int]) -> bool: d_1_i_ = int(0) # type : int d_1_i_ = 1 while (d_1_i_) < ((len(s)) - (1)): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(((0) < (d_1_i_)) and ((d_1_i_) <= ((len(s)) - (1)))) Invariant(len(s) >= 3) Invariant(Forall(int, lambda d_2_j_: Implies(((0) < (d_2_j_)) and ((d_2_j_) < (d_1_i_)), ThreeDistinct(s, d_2_j_)))) + # invariants-end if not(ThreeDistinct(s, d_1_i_)): happy = False return happy d_1_i_ = (d_1_i_) + (1) happy = True return happy + # impl-end diff --git a/Bench/082-prime-length.py b/Bench/082-prime-length.py index 103a6c1..b29ffab 100644 --- a/Bench/082-prime-length.py +++ b/Bench/082-prime-length.py @@ -2,22 +2,31 @@ from nagini_contracts.contracts import * def is__prime(s : str) -> bool: + # pre-conditions-start Requires((len(s)) >= (2)) + # pre-conditions-end + # post-conditions-start Ensures(not (Result()) or (Forall(int, lambda d_0_i_: not (((2) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) or ((len(s) % d_0_i_) != (0))))) Ensures(not (not(Result())) or (Exists(int, lambda d_1_j_: (((2) <= (d_1_j_)) and ((d_1_j_) < (len(s)))) and (((len(s) % d_1_j_)) == (0))))) + # post-conditions-end + + # impl-start result = False # type : bool d_2_i_ = int(0) # type : int d_2_i_ = 2 result = True while (d_2_i_) < (len(s)): + # invariants-start Invariant(((2) <= (d_2_i_)) and ((d_2_i_) <= (len(s)))) Invariant(not (not(result)) or (Exists(int, lambda d_3_j_: (((2) <= (d_3_j_)) and ((d_3_j_) < (d_2_i_))) and (((len(s) % d_3_j_)) == (0))))) Invariant(not (result) or (Forall(int, lambda d_4_j_: not (((2) <= (d_4_j_)) and ((d_4_j_) < (d_2_i_))) or (((len(s) % d_4_j_)) != (0))))) + # invariants-end if ((len(s) % d_2_i_)) == (0): result = False d_2_i_ = (d_2_i_) + (1) - return result \ No newline at end of file + return result + # impl-end \ No newline at end of file diff --git a/Bench/083-starts_one_ends.py b/Bench/083-starts_one_ends.py index 912e181..2df5685 100644 --- a/Bench/083-starts_one_ends.py +++ b/Bench/083-starts_one_ends.py @@ -2,20 +2,32 @@ from nagini_contracts.contracts import * def CountNumbersStartingOrEndingWithOne(n : int) -> int: + # pre-conditions-start Requires((n) > (0)) + # pre-conditions-end + # post-conditions-start Ensures(not ((n) == (1)) or ((Result()) == (1))) Ensures(not ((n) > (1)) or ((Result()) == ((18) * (Pow(10, (n) - (2)))))) + # post-conditions-end + + # impl-start count = int(0) # type : int if (n) == (1): count = 1 elif True: count = (18) * (Pow(10, (n) - (2))) return count + # impl-end @Pure def Pow(base : int, exponent : int) -> int : + # pre-conditions-start Requires((exponent) >= (0)) + # pre-conditions-end + + # impl-start if (exponent) == (0): return 1 else: - return (base) * (Pow(base, (exponent) - (1))) \ No newline at end of file + return (base) * (Pow(base, (exponent) - (1))) + # impl-end \ No newline at end of file diff --git a/Bench/084-solve.py b/Bench/084-solve.py index 50dc852..65bf6e2 100644 --- a/Bench/084-solve.py +++ b/Bench/084-solve.py @@ -2,26 +2,40 @@ from nagini_contracts.contracts import * def solve(n : int) -> int: + # pre-conditions-start Requires((n) >= (0)) + # pre-conditions-end + # post-conditions-start Ensures(Result() >= 0) Ensures((Result()) == (popcount(n))) + # post-conditions-end + + # impl-start r = int(0) # type : int d_0_m_ = int(0) # type : int d_0_m_ = n r = 0 while (d_0_m_) > (0): + # invariants-start Invariant(((0) <= (d_0_m_)) and ((d_0_m_) <= (n))) Invariant(r >= 0) Invariant(((r) + (popcount(d_0_m_))) == (popcount(n))) + # invariants-end r = (r) + ((d_0_m_ % 2)) d_0_m_ = (d_0_m_ // 2) return r + # impl-end @Pure def popcount(n : int) -> int : + # pre-conditions-start Requires(n >= 0) + # pre-conditions-end + + # impl-start if n == 0: return 0 else: return (n % 2) + popcount(n // 2) + # impl-end diff --git a/Bench/085-add.py b/Bench/085-add.py index a1d1f0f..ca4281c 100644 --- a/Bench/085-add.py +++ b/Bench/085-add.py @@ -3,25 +3,39 @@ @Pure def psum(i : int, j : int, s : List[int]) -> int : + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(0 <= i and i <= j + 1 and j < len(s)) + # pre-conditions-end + + # impl-start if i > j: return 0 else: return ((s)[j] if ((((j) % 2) == 1) and ((s)[j] % 2 == 0)) else 0) + (psum(i, j - 1, s)) + # impl-end def add(v : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(v))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(v))) Ensures((Result()) == (psum(0, len(v) - 1, v))) + # post-conditions-end + + # impl-start r = int(0) # type : int r = 0 d_2_k_ = int(0) # type : int d_2_k_ = 0 while (d_2_k_) < (len(v)): + # invariants-start Invariant(Acc(list_pred(v))) Invariant(((0) <= (d_2_k_)) and ((d_2_k_) <= (len(v)))) Invariant((r) == (psum(0, d_2_k_ - 1, v))) + # invariants-end r = (r) + (((v)[d_2_k_] if ((((d_2_k_) % 2) == 1) and ((v)[d_2_k_] % 2 == 0)) else 0)) d_2_k_ = (d_2_k_) + (1) return r + # impl-end diff --git a/Bench/088-sort_array.py b/Bench/088-sort_array.py index 982d66b..3da865f 100644 --- a/Bench/088-sort_array.py +++ b/Bench/088-sort_array.py @@ -2,7 +2,10 @@ from nagini_contracts.contracts import * def sort__array(s : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures(Acc(list_pred(Result()))) Ensures((len(Result())) == (len(s))) @@ -12,35 +15,52 @@ def sort__array(s : List[int]) -> List[int]: Ensures(not (((len(s)) > (0)) and ((((((s)[0]) + ((s)[(len(s)) - (1)])) % 2)) != (0))) or (Forall(int, lambda d_2_i_: Forall(int, lambda d_3_j_: not ((((0) <= (d_2_i_)) and ((d_2_i_) < (d_3_j_))) and ((d_3_j_) < (len(Result())))) or (((Result())[d_2_i_]) <= ((Result())[d_3_j_])))))) + # post-conditions-end + + # impl-start sorted = list([int(0)] * 0) # type : List[int] if (len(s)) == (0): sorted = list([]) return sorted elif (((((s)[0]) + ((s)[(len(s)) - (1)])) % 2)) == (0): + # assert-start Assert(len(s) > 0) + # assert-end d_4_t_ = list([int(0)] * 0) # type : List[int] d_4_t_ = BubbleSort(s) + # assert-start Assert(Forall(int, lambda d_0_i_: Forall(int, lambda d_1_j_: Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((d_4_t_)))), ((d_4_t_)[d_0_i_]) <= ((d_4_t_)[d_1_j_]))))) + # assert-end sorted = reverse(d_4_t_) + # assert-start Assert(Forall(int, lambda d_0_i_: Forall(int, lambda d_1_j_: Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((sorted)))), ((sorted)[d_0_i_]) >= ((sorted)[d_1_j_]))))) Assert(((len(s)) > (0)) and ((((((s)[0]) + ((s)[(len(s)) - (1)])) % 2)) == (0))) + # assert-end return sorted else: + # assert-start Assert(len(s) > 0) + # assert-end sorted = BubbleSort(s) + # assert-start Assert(Forall(int, lambda d_0_i_: Forall(int, lambda d_1_j_: Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((sorted)))), ((sorted)[d_0_i_]) <= ((sorted)[d_1_j_]))))) Assert(((len(s)) > (0)) and ((((((s)[0]) + ((s)[(len(s)) - (1)])) % 2)) != (0))) + # assert-end return sorted + # impl-end def reverse(str : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(str), 1/2)) Requires(Forall(int, lambda x: Forall(int, lambda y: not (((0) <= (x)) and ((x) < (y)) and ((y) < (len(str)))) or ((str)[x] <= (str)[y])))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(str), 1/2)) Ensures(Acc(list_pred(Result()))) Ensures(Forall(int, lambda x: Forall(int, lambda y: not (((0) <= (x)) and ((x) < (y)) and ((y) < (len(Result())))) or ((Result())[x] >= (Result())[y])))) @@ -48,11 +68,15 @@ def reverse(str : List[int]) -> List[int]: Ensures((len(Result())) == (len(str))) Ensures(Forall(int, lambda d_11_k_: not (((0) <= (d_11_k_)) and ((d_11_k_) < (len(str)))) or (((Result())[d_11_k_]) == ((str)[((len(str)) - (1)) - (d_11_k_)])))) + # post-conditions-end + + # impl-start rev = list([int(0)] * 0) # type : List[int] rev = [] d_12_i_ = int(0) # type : int d_12_i_ = 0 while (d_12_i_) < (len(str)): + # invariants-start Invariant(Acc(list_pred(str), 1/2)) Invariant(Acc(list_pred(rev))) Invariant(Forall(int, lambda x: Forall(int, lambda y: not (((0) <= (x)) and ((x) < (y)) and ((y) < (len(str)))) or ((str)[x] <= (str)[y])))) @@ -62,23 +86,31 @@ def reverse(str : List[int]) -> List[int]: not (((0) <= (d_13_k_)) and ((d_13_k_) < (d_12_i_))) or (((rev)[d_13_k_]) == ((str)[(len(str) - (1)) - (d_13_k_)])))) Invariant(Forall(int, lambda x: Forall(int, lambda y: (not (((0) <= (x)) and ((x) < (len(rev))) and (0 <= y and (y) < (len(str) - d_12_i_))) or ((str)[y] <= (rev)[x]), [[str[y] <= rev[x]]])))) Invariant(Forall(int, lambda x: Forall(int, lambda y: (not (((0) <= (x)) and ((x) < (y)) and ((y) < (len(rev)))) or ((rev)[x] >= (rev)[y]), [[rev[x] >= rev[y]]])))) + # invariants-end rev = (rev) + [(str)[(len(str) - (d_12_i_)) - (1)]] d_12_i_ = (d_12_i_) + (1) return rev + # impl-end def BubbleSort(a1 : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(a1), 1/2)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(a1), 1/2)) Ensures(Acc(list_pred(Result()))) Ensures((len(a1)) == (len(Result()))) - # Ensures(ToMS(ToSeq(a1)) == ToMS(ToSeq(Result()))) Ensures(Forall(int, lambda d_0_i_: Forall(int, lambda d_1_j_: Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((Result())))), ((Result())[d_0_i_]) <= ((Result())[d_1_j_]))))) + # post-conditions-end + + # impl-start a = list(a1) # type : List[int] d_2_i_ = int(0) # type : int d_2_i_ = (len((a))) - (1) while (d_2_i_) > (0): + # invariants-start Invariant(Acc(list_pred(a))) Invariant(Acc(list_pred(a1), 1/2)) Invariant((len(a1)) == (len(a))) @@ -94,9 +126,11 @@ def BubbleSort(a1 : List[int]) -> List[int]: (Implies(((((0) <= (d_5_k_)) and ((d_5_k_) <= (d_2_i_))) and ((d_2_i_) < (d_6_k_k_)) and (d_6_k_k_) < (len((a)))), ((a)[d_5_k_]) <= ((a)[d_6_k_k_])), [[(a)[d_6_k_k_]]])), [[(a)[d_5_k_]]]))) + # invariants-end d_7_j_ = int(0) # type : int d_7_j_ = 0 while (d_7_j_) < (d_2_i_): + # invariants-start Invariant(Acc(list_pred(a))) Invariant(Acc(list_pred(a1), 1/2)) Invariant((len(a1)) == (len(a))) @@ -114,6 +148,7 @@ def BubbleSort(a1 : List[int]) -> List[int]: Invariant(Forall(int, lambda d_12_k_: (Implies(((0) <= (d_12_k_)) and ((d_12_k_) <= (d_7_j_)), ((a)[d_12_k_]) <= ((a)[d_7_j_])), [[(a)[d_12_k_]]]))) + # invariants-end if ((a)[d_7_j_]) > ((a)[(d_7_j_) + (1)]): rhs0_ = (a)[(d_7_j_) + (1)] # type : int (a)[(d_7_j_) + (1)] = (a)[d_7_j_] @@ -121,3 +156,4 @@ def BubbleSort(a1 : List[int]) -> List[int]: d_7_j_ = (d_7_j_) + (1) d_2_i_ = (d_2_i_) - (1) return a + # impl-end diff --git a/Bench/089-encrypt.py b/Bench/089-encrypt.py index f4fe5ca..5de4c97 100644 --- a/Bench/089-encrypt.py +++ b/Bench/089-encrypt.py @@ -3,15 +3,25 @@ @Pure def rot__sym(c : int) -> int : + # pre-conditions-start Requires(c >= 0 and c <= 25) + # pre-conditions-end + # post-conditions-start Ensures(Result() >= 0 and Result() <= 25) + # post-conditions-end + + # impl-start d_0_alph_ = c - 0 # type : int return ((d_0_alph_) + ((2) * (2))) % 26 + # impl-end def encrypt(s : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(Forall(int, lambda d_1_i_: not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((0) <= ((s)[d_1_i_])) and (((s)[d_1_i_]) <= (25))))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures(Acc(list_pred(Result()))) Ensures((len(Result())) == (len(s))) @@ -21,10 +31,14 @@ def encrypt(s : List[int]) -> List[int]: not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(Result())))) or (((0) <= ((Result())[d_1_i_])) and (((Result())[d_1_i_]) <= (25))))) Ensures(Forall(int, lambda d_2_i_: not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(s)))) or (((Result())[d_2_i_]) == (rot__sym((s)[d_2_i_]))))) + # post-conditions-end + + # impl-start r : List[int] = [] # type : List[int] d_3_i_ = int(0) # type : int d_3_i_ = 0 while (d_3_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(Acc(list_pred(r))) Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(s)))) @@ -35,6 +49,8 @@ def encrypt(s : List[int]) -> List[int]: not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(r)))) or (((0) <= ((r)[d_1_i_])) and (((r)[d_1_i_]) <= (25))))) Invariant(Forall(int, lambda d_4_j_: not (((0) <= (d_4_j_)) and ((d_4_j_) < (d_3_i_))) or (((r)[d_4_j_]) == (rot__sym((s)[d_4_j_]))))) + # invariants-end r = (r) + [rot__sym((s)[d_3_i_])] d_3_i_ = (d_3_i_) + (1) return r + # impl-end diff --git a/Bench/090-next_smallest.py b/Bench/090-next_smallest.py index 975e95c..8b753f4 100644 --- a/Bench/090-next_smallest.py +++ b/Bench/090-next_smallest.py @@ -2,7 +2,10 @@ from nagini_contracts.contracts import * def next_smallest(s : List[int]) -> Optional[int]: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures((Result() is None) == (len(s) < 2)) Ensures(Implies(Result() is not None, Exists(int, lambda x: x >= 0 and x < len(s) and s[x] == Result()))) @@ -11,7 +14,9 @@ def next_smallest(s : List[int]) -> Optional[int]: Forall(int, lambda y: Implies(y > x and y < len(s), s[x] <= Result() or s[y] <= Result()))))) + # post-conditions-end + # impl-start if len(s) < 2: return None @@ -19,6 +24,7 @@ def next_smallest(s : List[int]) -> Optional[int]: mx : int = s[0] i = 1 while i < len(s): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(len(s) >= 2) Invariant(0 <= i and i <= len(s)) @@ -35,6 +41,7 @@ def next_smallest(s : List[int]) -> Optional[int]: Forall(int, lambda y: Implies(y > x and y < i, s[x] <= res or s[y] <= res))))) + # invariants-end if s[i] > mx: res = mx @@ -43,3 +50,4 @@ def next_smallest(s : List[int]) -> Optional[int]: res = s[i] i += 1 return res + # impl-end diff --git a/Bench/092-any_int.py b/Bench/092-any_int.py index d2ff5d6..2f04970 100644 --- a/Bench/092-any_int.py +++ b/Bench/092-any_int.py @@ -1,7 +1,12 @@ from nagini_contracts.contracts import * def any__int(a : int, b : int, c : int) -> bool: + # post-conditions-start Ensures((Result()) == ((((a) == ((b) + (c))) or ((b) == ((a) + (c)))) or ((c) == ((a) + (b))))) + # post-conditions-end + + # impl-start r = False # type : bool r = (((a) == ((b) + (c))) or ((b) == ((a) + (c)))) or ((c) == ((a) + (b))) - return r \ No newline at end of file + return r + # impl-end \ No newline at end of file diff --git a/Bench/093-encode.py b/Bench/093-encode.py index 61f1fb8..a05e1fe 100644 --- a/Bench/093-encode.py +++ b/Bench/093-encode.py @@ -2,9 +2,12 @@ from nagini_contracts.contracts import * def encode(s : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(Forall(int, lambda d_0_i_: not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) or ((((97) <= ((s)[d_0_i_])) and (((s)[d_0_i_]) <= (122))) or (((65) <= ((s)[d_0_i_])) and (((s)[d_0_i_]) <= (90)))))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(Result()))) Ensures(Acc(list_pred(s))) Ensures((len(s)) == (len(Result()))) @@ -12,10 +15,14 @@ def encode(s : List[int]) -> List[int]: not ((((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) and (is__vowel((s)[d_1_i_]))) or (((Result())[d_1_i_]) == (rot2(swap__case((s)[d_1_i_])))))) Ensures(Forall(int, lambda d_2_i_: not ((((0) <= (d_2_i_)) and ((d_2_i_) < (len(s)))) and (not(is__vowel((s)[d_2_i_])))) or (((Result())[d_2_i_]) == (swap__case((s)[d_2_i_]))))) + # post-conditions-end + + # impl-start t : List[int] = [] d_3_i_ = int(0) # type : int d_3_i_ = 0 while (d_3_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(t))) Invariant(Acc(list_pred(s))) Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(s)))) @@ -24,24 +31,32 @@ def encode(s : List[int]) -> List[int]: (not ((((0) <= (d_4_j_)) and ((d_4_j_) < (d_3_i_))) and (is__vowel((s)[d_4_j_]))) or (((t)[d_4_j_]) == (rot2(swap__case((s)[d_4_j_])))), [[rot2(swap__case((s)[d_4_j_]))]]))) Invariant(Forall(int, lambda d_5_j_: (not ((((0) <= (d_5_j_)) and ((d_5_j_) < (d_3_i_))) and (not(is__vowel((s)[d_5_j_])))) or (((t)[d_5_j_]) == (swap__case((s)[d_5_j_]))), [[swap__case((s)[d_5_j_])]]))) + # invariants-end if is__vowel((s)[d_3_i_]): t = (t) + [rot2(swap__case((s)[d_3_i_]))] else: t = (t) + [swap__case((s)[d_3_i_])] d_3_i_ = (d_3_i_) + (1) return t + # impl-end @Pure def swap__case(c : int) -> int : + # impl-start if ((97) <= (c)) and ((c) <= (122)): return (65) + ((c) - (97)) elif True: return (97) + ((c) - (65)) + # impl-end @Pure def rot2(c : int) -> int : - return (c + 2) + # impl-start + return (c + 2) + # impl-end @Pure def is__vowel(c : int) -> bool : + # impl-start return ((((((c) == (97)) or ((c) == (101))) or ((c) == (105))) or ((c) == (111))) or ((c) == (117))) or ((((((c) == (65)) or ((c) == (69))) or ((c) == (73))) or ((c) == (79))) or ((c) == (85))) + # impl-end diff --git a/Bench/096-count_up_to.py b/Bench/096-count_up_to.py index 837cfc3..abe68a2 100644 --- a/Bench/096-count_up_to.py +++ b/Bench/096-count_up_to.py @@ -3,16 +3,24 @@ @Pure def IsPrime(n : int) -> bool : + # impl-start return ((n) > (1)) and (Forall(int, lambda d_0_k_: Implies(((2) <= (d_0_k_)) and ((d_0_k_) < (n)), ((n % d_0_k_)) != (0)))) + # impl-end def CountUpTo(n : int) -> List[int]: + # pre-conditions-start Requires((n) >= (0)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(Result()))) Ensures(Forall(int, lambda d_2_i_: not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(Result())))) or (((Result())[d_2_i_]) < (n)))) Ensures(Forall(int, lambda d_1_i_: not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(Result())))) or (IsPrime((Result())[d_1_i_])))) + # post-conditions-end + + # impl-start primes = list([int(0)] * 0) # type : List[int] primes = list([]) if (n) <= (2): @@ -20,13 +28,16 @@ def CountUpTo(n : int) -> List[int]: d_4_i_ = int(0) # type : int d_4_i_ = 2 while (d_4_i_) < (n): + # invariants-start Invariant(Acc(list_pred(primes))) Invariant(((2) <= (d_4_i_)) and ((d_4_i_) <= (n))) Invariant(Forall(int, lambda x: Implies(x >= 0 and x < len(primes), 2 <= primes[x] and primes[x] < n))) Invariant(Forall(int, lambda d_5_j_: (Implies(((0) <= (d_5_j_)) and ((d_5_j_) < (len(primes))), IsPrime((primes)[d_5_j_])), [[IsPrime((primes)[d_5_j_])]]))) + # invariants-end if IsPrime(d_4_i_): primes = primes + [(d_4_i_)] d_4_i_ = (d_4_i_) + (1) return primes + # impl-end diff --git a/Bench/097-multiply.py b/Bench/097-multiply.py index f2ff39f..1a680e8 100644 --- a/Bench/097-multiply.py +++ b/Bench/097-multiply.py @@ -2,15 +2,24 @@ @Pure def last__digit(n : int) -> int : + # impl-start if (n) < (0): return (((0) - (n)) % 10) elif True: return (n % 10) + # impl-end def multiply(a : int, b : int) -> int: + # pre-conditions-start Requires((a) >= (0)) Requires((b) >= (0)) + # pre-conditions-end + # post-conditions-start Ensures((Result()) == ((last__digit(a)) * (last__digit(b)))) + # post-conditions-end + + # impl-start c = int(0) # type : int c = (last__digit(a)) * (last__digit(b)) return c + # impl-end diff --git a/Bench/098-count_upper.py b/Bench/098-count_upper.py index 5ef3afa..695736c 100644 --- a/Bench/098-count_upper.py +++ b/Bench/098-count_upper.py @@ -3,9 +3,15 @@ @Pure def count__upper__fun(s : List[int], i : int, j : int) -> int: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(((0) <= (i)) and ((i) <= (j)) and ((j) <= (len(s)))) + # pre-conditions-end + # post-conditions-start Ensures((Result()) >= (0)) + # post-conditions-end + + # impl-start if i == j: return 0 else: @@ -13,31 +19,46 @@ def count__upper__fun(s : List[int], i : int, j : int) -> int: return (1) + (count__upper__fun(s, i, j - 1)) else: return count__upper__fun(s, i, j - 1) + # impl-end def count__upper(s : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures((Result()) >= (0)) Ensures(Acc(list_pred(s))) Ensures((Result()) == (count__upper__fun(s, 0, len(s)))) + # post-conditions-end + + # impl-start cnt = int(0) # type : int cnt = 0 d_1_i_ = int(0) # type : int d_1_i_ = 0 while (d_1_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(s)))) Invariant(Forall(int, lambda d_0_i_: (Implies(d_0_i_ >= 0 and d_0_i_ < len(s), count__upper__fun(s, 0, d_0_i_ + 1) == (count__upper__fun(s, 0, d_0_i_) + (1) if is__upper__vowel(s[d_0_i_]) and d_0_i_ % 2 == 0 else count__upper__fun(s, 0, d_0_i_))), [[count__upper__fun(s, 0, d_0_i_ + 1)]]))) Invariant((cnt) == (count__upper__fun(s, 0, d_1_i_))) + # invariants-end + + # assert-start Assert(count__upper__fun(s, 0, d_1_i_ + 1) == (count__upper__fun(s, 0, d_1_i_) + (1) if is__upper__vowel(s[d_1_i_]) and d_1_i_ % 2 == 0 else count__upper__fun(s, 0, d_1_i_))) + # assert-end if (is__upper__vowel((s)[d_1_i_])) and (((d_1_i_ % 2)) == (0)): cnt = (cnt) + (1) d_1_i_ = (d_1_i_) + (1) return cnt + # impl-end @Pure def is__upper__vowel(c : int) -> bool : + # impl-start return (((((c) == (0)) or ((c) == (1))) or ((c) == (2))) or ((c) == (3))) or ((c) == (4)) + # impl-end diff --git a/Bench/100-make_a_pile.py b/Bench/100-make_a_pile.py index 088d31f..a21b419 100644 --- a/Bench/100-make_a_pile.py +++ b/Bench/100-make_a_pile.py @@ -2,12 +2,18 @@ from nagini_contracts.contracts import * def make__a__pile(n : int) -> List[int]: + # pre-conditions-start Requires((n) >= (0)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(Result()))) Ensures((len(Result())) == (n)) Ensures(Forall(int, lambda d_0_i_: Implies(((1) <= (d_0_i_)) and ((d_0_i_) < (n)), ((Result())[d_0_i_]) == (((Result())[(d_0_i_) - (1)]) + (2))))) Ensures(Implies((n) > (0), ((Result())[0]) == (n))) + # post-conditions-end + + # impl-start pile = list([int(0)] * 0) # type : List[int] pile = list([]) if (n) == (0): @@ -16,6 +22,7 @@ def make__a__pile(n : int) -> List[int]: d_1_i_ = int(0) # type : int d_1_i_ = 1 while (d_1_i_) < (n): + # invariants-start Invariant(Acc(list_pred(pile))) Invariant(len(pile) == d_1_i_) Invariant(len(pile) > 0) @@ -24,6 +31,8 @@ def make__a__pile(n : int) -> List[int]: Invariant(Forall(int, lambda d_2_j_: Implies(((1) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_)), ((pile)[d_2_j_]) == (((pile)[(d_2_j_) - (1)]) + (2))))) Invariant(Implies((n) > (0), ((pile)[0]) == (n))) + # invariants-end pile = (pile) + [((pile)[(d_1_i_) - (1)]) + (2)] d_1_i_ = (d_1_i_) + (1) return pile + # impl-end diff --git a/Bench/102-choose_num.py b/Bench/102-choose_num.py index 89afcaf..c8534f3 100644 --- a/Bench/102-choose_num.py +++ b/Bench/102-choose_num.py @@ -2,12 +2,18 @@ from nagini_contracts.contracts import * def choose__num(x : int, y : int) -> int: + # pre-conditions-start Requires(((0) <= (x)) and ((0) <= (y))) + # pre-conditions-end + # post-conditions-start Ensures(((Result()) == (-1)) or (((Result()) >= (x)) and ((Result()) <= (y)))) Ensures(((Result()) == (-1)) or (((Result() % 2)) == (0))) Ensures(((Result()) == (-1)) or (Forall(int, lambda d_0_i_: not ((((x) <= (d_0_i_)) and ((d_0_i_) <= (y))) and (((d_0_i_ % 2)) == (0))) or ((Result()) >= (d_0_i_))))) Ensures(((Result()) == (-1)) == ((x) >= (y))) + # post-conditions-end + + # impl-start num = int(0) # type : int num = -1 if (x) >= (y): @@ -19,12 +25,15 @@ def choose__num(x : int, y : int) -> int: d_1_i_ = int(0) # type : int d_1_i_ = (x) + (2) while (d_1_i_) <= (y): + # invariants-start Invariant(((d_1_i_) >= (x)) and ((d_1_i_) <= ((y) + (1)))) Invariant(((num) == (-1)) or (((num % 2)) == (0))) Invariant(Forall(int, lambda d_2_j_: not ((((x) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_))) and (((d_2_j_ % 2)) == (0))) or ((num) >= (d_2_j_)))) Invariant(((num) == (-1)) or (((num) >= (x)) and ((num) < (d_1_i_)))) + # invariants-end if ((d_1_i_ % 2)) == (0): num = d_1_i_ d_1_i_ = (d_1_i_) + (1) return num + # impl-end diff --git a/Bench/104-unique_digits.py b/Bench/104-unique_digits.py index ef9d2ec..016b73f 100644 --- a/Bench/104-unique_digits.py +++ b/Bench/104-unique_digits.py @@ -4,18 +4,31 @@ @Pure def InArray(a : List[int], x : int) -> bool: + # pre-conditions-start Requires(Acc(list_pred(a), 1/2)) + # pre-conditions-end + + # impl-start return Exists(int, lambda d_0_i_: ((((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))) and ((a)[d_0_i_]) == (x)))) + # impl-end @Pure def HasNoEvenDigit(n : int) -> bool : + # pre-conditions-start Requires(((0) <= (n))) + # pre-conditions-end + + # impl-start return (n == 0 or (((((n % 10) % 2)) != (0)) and (HasNoEvenDigit((n // 10))))) + # impl-end def UniqueDigits(x : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(x), 1/2)) Requires(Forall(int, lambda d_0_i_: Implies(d_0_i_ >= 0 and d_0_i_ < len(x), (x[d_0_i_] >= 0)))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(x), 1/2)) Ensures(Acc(list_pred(Result()))) Ensures(len(Result()) <= len(x)) @@ -32,11 +45,15 @@ def UniqueDigits(x : List[int]) -> List[int]: Ensures(Forall(int, lambda d_1_i_: Forall(int, lambda d_2_j_: not ((((0) <= (d_1_i_)) and ((d_1_i_) < (d_2_j_))) and ((d_2_j_) < (len(Result())))) or (((Result())[d_1_i_]) <= ((Result())[d_2_j_]))))) + # post-conditions-end + + # impl-start result = list([int(0)] * 0) # type : List[int] result = list([]) d_5_i_ = 0 while d_5_i_ < len(x): + # invariants-start Invariant(Acc(list_pred(result))) Invariant(Acc(list_pred(x), 1/2)) Invariant(0 <= d_5_i_ and d_5_i_ <= len(x)) @@ -54,12 +71,14 @@ def UniqueDigits(x : List[int]) -> List[int]: (Implies((d_7_e_) >= 0 and d_7_e_ < len(result), InArray(x, result[d_7_e_])), [[InArray(x, result[d_7_e_])]]))) + # invariants-end if HasNoEvenDigit((x)[d_5_i_]): result = (result) + [(x)[d_5_i_]] d_5_i_ = (d_5_i_) + (1) d_9_i_ = int(0) # type : int d_9_i_ = 0 while (d_9_i_) < (len(result)): + # invariants-start Invariant(Acc(list_pred(result))) Invariant(Acc(list_pred(x), 1/2)) Invariant(len(result) <= len(x)) @@ -91,11 +110,13 @@ def UniqueDigits(x : List[int]) -> List[int]: (((result)[d_12_j_]) <= ((result)[d_13_k_])), [[result[d_13_k_]]]))), [[(result)[d_12_j_]]]))) + # invariants-end d_17_minIndex_ = int(0) # type : int d_17_minIndex_ = d_9_i_ d_18_j_ = int(0) # type : int d_18_j_ = (d_9_i_) + (1) while (d_18_j_) < (len(result)): + # invariants-start Invariant(Acc(list_pred(result))) Invariant(Acc(list_pred(x), 1/2)) Invariant(len(result) <= len(x)) @@ -131,15 +152,21 @@ def UniqueDigits(x : List[int]) -> List[int]: (((result)[d_12_j_]) <= ((result)[d_13_k_])), [[result[d_13_k_]]]))), [[(result)[d_12_j_]]]))) + # invariants-end if ((result)[d_18_j_]) < ((result)[d_17_minIndex_]): d_17_minIndex_ = d_18_j_ d_18_j_ = (d_18_j_) + (1) if (d_17_minIndex_) != (d_9_i_): d_20_temp_ = int(0) # type : int d_20_temp_ = (result)[d_9_i_] + # assert-start Assert(HasNoEvenDigit((result)[d_17_minIndex_])) + # assert-end result[d_9_i_] = (result)[d_17_minIndex_] + # assert-start Assert(HasNoEvenDigit(d_20_temp_)) + # assert-end result[d_17_minIndex_] = d_20_temp_ d_9_i_ = (d_9_i_) + (1) return result + # impl-end diff --git a/Bench/105-by_length.py b/Bench/105-by_length.py index 1e9d568..2c68a4a 100644 --- a/Bench/105-by_length.py +++ b/Bench/105-by_length.py @@ -3,23 +3,34 @@ @Pure def WithinRange(n : int) -> bool: + # impl-start return ((n) >= (1)) and ((n) <= (9)) + # impl-end @Pure def WithinRangeString(s : str) -> bool: + # impl-start return (s == "One" or s == "Two" or s == "Three" or s == "Four" or s == "Five" or s == "Six" or s == "Seven" or s == "Eight" or s == "Nine") + # impl-end def SortReverseAndName(arr : List[int]) -> List[str]: + # pre-conditions-start Requires(Acc(list_pred(arr))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(arr))) Ensures(Acc(list_pred(Result()))) Ensures((len(Result())) <= (len(arr))) Ensures(Forall(int, lambda d_0_i_: Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len(Result()))), WithinRangeString(Result()[d_0_i_])))) + # post-conditions-end + + # impl-start d_1_validNumbers_ : List[int] = [] # type : List[int] d_2_i_ = int(0) # type : int while (d_2_i_) < (len(arr)): + # invariants-start Invariant(Acc(list_pred(d_1_validNumbers_))) Invariant(Acc(list_pred(arr))) Invariant(((0) <= (d_2_i_)) and ((d_2_i_) <= (len(arr)))) @@ -27,16 +38,20 @@ def SortReverseAndName(arr : List[int]) -> List[str]: Invariant(Forall(int, lambda d_3_j_: (Implies(((0) <= (d_3_j_)) and ((d_3_j_) < (len(d_1_validNumbers_))), WithinRange(d_1_validNumbers_[d_3_j_])), [[]]))) + # invariants-end if WithinRange((arr)[d_2_i_]): d_1_validNumbers_ = (d_1_validNumbers_) + [(arr)[d_2_i_]] d_2_i_ = (d_2_i_) + (1) + # assert-start Assert(len(d_1_validNumbers_) <= len(arr)) + # assert-end d_1_validNumbers_ = BubbleSort(d_1_validNumbers_) d_1_validNumbers_ = reverse(d_1_validNumbers_) d_2_i_ = 0 result : List[str] = [] # type : List[str] while (d_2_i_) < (len(d_1_validNumbers_)): + # invariants-start Invariant(Acc(list_pred(d_1_validNumbers_))) Invariant(Acc(list_pred(result))) Invariant(Acc(list_pred(arr))) @@ -51,14 +66,19 @@ def SortReverseAndName(arr : List[int]) -> List[str]: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len(result))), WithinRangeString(result[d_0_i_])), [[]]))) + # invariants-end result = (result) + [NumberToName((d_1_validNumbers_)[d_2_i_])] d_2_i_ = (d_2_i_) + (1) return result + # impl-end def BubbleSort(a1 : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(a1), 1/2)) Requires(Forall(int, lambda d_4_j_: Implies(((0) <= (d_4_j_)) and ((d_4_j_) < (len(a1))), ((1) <= ((a1)[d_4_j_])) and (((a1)[d_4_j_]) <= (9))))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(a1), 1/2)) Ensures(Acc(list_pred(Result()))) Ensures(Forall(int, lambda d_4_j_: @@ -67,10 +87,14 @@ def BubbleSort(a1 : List[int]) -> List[int]: Ensures(Forall(int, lambda d_0_i_: Forall(int, lambda d_1_j_: Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((Result())))), ((Result())[d_0_i_]) <= ((Result())[d_1_j_]))))) + # post-conditions-end + + # impl-start a = list(a1) # type : List[int] d_2_i_ = int(0) # type : int d_2_i_ = (len((a))) - (1) while (d_2_i_) > (0): + # invariants-start Invariant(Acc(list_pred(a))) Invariant(Acc(list_pred(a1), 1/2)) Invariant(Forall(int, lambda d_4_j_: @@ -89,9 +113,11 @@ def BubbleSort(a1 : List[int]) -> List[int]: (Implies(((((0) <= (d_5_k_)) and ((d_5_k_) <= (d_2_i_))) and ((d_2_i_) < (d_6_k_k_)) and (d_6_k_k_) < (len((a)))), ((a)[d_5_k_]) <= ((a)[d_6_k_k_])), [[(a)[d_6_k_k_]]])), [[(a)[d_5_k_]]]))) + # invariants-end d_7_j_ = int(0) # type : int d_7_j_ = 0 while (d_7_j_) < (d_2_i_): + # invariants-start Invariant(Acc(list_pred(a))) Invariant(Acc(list_pred(a1), 1/2)) Invariant(Forall(int, lambda d_4_j_: @@ -112,6 +138,7 @@ def BubbleSort(a1 : List[int]) -> List[int]: Invariant(Forall(int, lambda d_12_k_: (Implies(((0) <= (d_12_k_)) and ((d_12_k_) <= (d_7_j_)), ((a)[d_12_k_]) <= ((a)[d_7_j_])), [[(a)[d_12_k_]]]))) + # invariants-end if ((a)[d_7_j_]) > ((a)[(d_7_j_) + (1)]): rhs0_ = (a)[(d_7_j_) + (1)] # type : int (a)[(d_7_j_) + (1)] = (a)[d_7_j_] @@ -119,12 +146,16 @@ def BubbleSort(a1 : List[int]) -> List[int]: d_7_j_ = (d_7_j_) + (1) d_2_i_ = (d_2_i_) - (1) return a + # impl-end def reverse(str : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(str), 1/2)) Requires(Forall(int, lambda x: Forall(int, lambda y: Implies(((0) <= (x)) and ((x) < (y)) and ((y) < (len(str))), (str)[x] <= (str)[y])))) Requires(Forall(int, lambda d_4_j_: Implies(((0) <= (d_4_j_)) and ((d_4_j_) < (len(str))), ((1) <= ((str)[d_4_j_])) and (((str)[d_4_j_]) <= (9))))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(str), 1/2)) Ensures(Acc(list_pred(Result()))) Ensures(Forall(int, lambda d_4_j_: @@ -134,11 +165,15 @@ def reverse(str : List[int]) -> List[int]: Ensures((len(Result())) == (len(str))) Ensures(Forall(int, lambda d_11_k_: Implies(((0) <= (d_11_k_)) and ((d_11_k_) < (len(str))), ((Result())[d_11_k_]) == ((str)[((len(str)) - (1)) - (d_11_k_)])))) + # post-conditions-end + + # impl-start rev = list([int(0)] * 0) # type : List[int] rev = [] d_12_i_ = int(0) # type : int d_12_i_ = 0 while (d_12_i_) < (len(str)): + # invariants-start Invariant(Acc(list_pred(str), 1/2)) Invariant(Acc(list_pred(rev))) Invariant(Forall(int, lambda d_4_j_: @@ -159,13 +194,21 @@ def reverse(str : List[int]) -> List[int]: Invariant(Forall(int, lambda x: Forall(int, lambda y: (Implies(((0) <= (x)) and ((x) < (y)) and ((y) < (len(rev))), (rev)[x] >= (rev)[y]), [[rev[x] >= rev[y]]])))) + # invariants-end rev = (rev) + [(str)[(len(str) - (d_12_i_)) - (1)]] d_12_i_ = (d_12_i_) + (1) return rev + # impl-end def NumberToName(n : int) -> str : + # pre-conditions-start Requires(n >= 1 and n <= 9) + # pre-conditions-end + # post-conditions-start Ensures(WithinRangeString(Result())) + # pos-conditions-end + + # impl-start if (n) == (1): return "One" @@ -191,4 +234,5 @@ def NumberToName(n : int) -> str : return "Eight" if (n) == (9): - return "Nine" \ No newline at end of file + return "Nine" + # impl-end \ No newline at end of file diff --git a/Bench/106-f.py b/Bench/106-f.py index b776caa..ae6500e 100644 --- a/Bench/106-f.py +++ b/Bench/106-f.py @@ -3,35 +3,56 @@ @Pure def factorial__spec(n : int) -> int : + # pre-conditions-start Requires(n >= -1) + # pre-conditions-end + # post-conditions-start Ensures(Result() >= 0) + # post-conditions-end + + # impl-start if n == -1: return 1 else: return (n + 1) * factorial__spec(n - 1) + # impl-end @Pure def sum__spec(n : int) -> int : + # pre-conditions-start Requires(n >= -1) + # pre-conditions-end + # post-conditions-start Ensures(Result() >= 0) + # post-conditions-end + + # impl-start if 0 > n: return 0 else: return n + 1 + sum__spec(n - 1) + # impl-end def f(n : int) -> List[int]: + # pre-conditions-start Requires((n) >= (1)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(Result()))) Ensures((len(Result())) == (n)) Ensures(Forall(int, lambda d_2_i_: not ((((d_2_i_) >= (0)) and ((d_2_i_) < (len(Result())))) and (((d_2_i_ % 2)) == (0))) or (((Result())[d_2_i_]) == (factorial__spec(d_2_i_ - 1))))) Ensures(Forall(int, lambda d_3_i_: not ((((d_3_i_) >= (0)) and ((d_3_i_) < (len(Result())))) and (((d_3_i_ % 2)) != (0))) or (((Result())[d_3_i_]) == (sum__spec(d_3_i_ - 1))))) + # post-conditions-end + + # impl-start result = list([int(0)] * 0) # type : List[int] result = list([]) d_4_i_ = int(0) # type : int d_4_i_ = 0 while (d_4_i_) < (n): + # invariants-start Invariant(Acc(list_pred(result))) Invariant(((d_4_i_) >= (0)) and ((d_4_i_) <= (n))) Invariant((len(result)) == (d_4_i_)) @@ -39,12 +60,14 @@ def f(n : int) -> List[int]: (Implies((((d_5_i_) >= (0)) and ((d_5_i_) < (len(result)))) and (((d_5_i_ % 2)) == (0)), ((result)[d_5_i_]) == (factorial__spec(d_5_i_ - 1))), [[factorial__spec(d_5_i_ - 1)]]))) Invariant(Forall(int, lambda d_6_i_: (Implies((((d_6_i_) >= (0)) and ((d_6_i_) < (len(result)))) and (((d_6_i_ % 2)) != (0)), ((result)[d_6_i_]) == (sum__spec(d_6_i_ - 1))), [[sum__spec(d_6_i_ - 1)]]))) + # invariants-end if ((d_4_i_ % 2)) == (0): d_7_x_ = int(0) # type : int d_7_x_ = 1 d_8_j_ = int(0) # type : int d_8_j_ = 0 while (d_8_j_) < (d_4_i_): + # invariants-start Invariant(Acc(list_pred(result))) Invariant(((d_4_i_) >= (0)) and ((d_4_i_) <= (n))) Invariant((len(result)) == (d_4_i_)) @@ -54,6 +77,7 @@ def f(n : int) -> List[int]: (Implies((((d_5_i_) >= (0)) and ((d_5_i_) < (len(result)))) and (((d_5_i_ % 2)) == (0)), ((result)[d_5_i_]) == (factorial__spec(d_5_i_ - 1))), [[factorial__spec(d_5_i_ - 1)]]))) Invariant(Forall(int, lambda d_6_i_: (Implies((((d_6_i_) >= (0)) and ((d_6_i_) < (len(result)))) and (((d_6_i_ % 2)) != (0)), ((result)[d_6_i_]) == (sum__spec(d_6_i_ - 1))), [[sum__spec(d_6_i_ - 1)]]))) + # invariants-end d_7_x_ = (d_7_x_) * (d_8_j_ + 1) d_8_j_ = (d_8_j_) + (1) result = (result) + [d_7_x_] @@ -63,6 +87,7 @@ def f(n : int) -> List[int]: d_10_j_ = int(0) # type : int d_10_j_ = 0 while (d_10_j_) < (d_4_i_): + # invariants-start Invariant(Acc(list_pred(result))) Invariant(((d_4_i_) >= (0)) and ((d_4_i_) <= (n))) Invariant((len(result)) == (d_4_i_)) @@ -72,8 +97,10 @@ def f(n : int) -> List[int]: (Implies((((d_5_i_) >= (0)) and ((d_5_i_) < (len(result)))) and (((d_5_i_ % 2)) == (0)), ((result)[d_5_i_]) == (factorial__spec(d_5_i_ - 1))), [[factorial__spec(d_5_i_ - 1)]]))) Invariant(Forall(int, lambda d_6_i_: (Implies((((d_6_i_) >= (0)) and ((d_6_i_) < (len(result)))) and (((d_6_i_ % 2)) != (0)), ((result)[d_6_i_]) == (sum__spec(d_6_i_ - 1))), [[sum__spec(d_6_i_ - 1)]]))) + # invariants-end d_9_x_ = (d_9_x_) + (d_10_j_ + 1) d_10_j_ = (d_10_j_) + (1) result = (result) + [d_9_x_] d_4_i_ = (d_4_i_) + (1) return result + # impl-end \ No newline at end of file diff --git a/Bench/108-count_nums.py b/Bench/108-count_nums.py index 5897484..1b78676 100644 --- a/Bench/108-count_nums.py +++ b/Bench/108-count_nums.py @@ -2,47 +2,70 @@ from nagini_contracts.contracts import * def count__nums(s : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures(Result() == get_positive(0, len(s), s)) + # post-conditions-end + + # impl-start cnt = int(0) # type : int cnt = 0 d_1_i_ = int(0) # type : int d_1_i_ = 0 while (d_1_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(s))) Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(s)))) Invariant(Forall(int, lambda x : (Implies(0 <= x and x < len(s), get_positive(0, x + 1, s) == ((digits__sum(s[x]) > 0) + get_positive(0, x, s))), [[get_positive(0, x + 1, s)]]))) Invariant((cnt) == (get_positive(0, d_1_i_, s))) + # invariants-end + # assert-start Assert(get_positive(0, d_1_i_ + 1, s) == (digits__sum(s[d_1_i_]) > 0) + get_positive(0, d_1_i_, s)) + # assert-end if (digits__sum((s)[d_1_i_])) > (0): cnt = (cnt) + (1) d_1_i_ = (d_1_i_) + (1) return cnt + # impl-end @Pure def get_positive(i : int, j : int, s : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(0 <= i and i <= j and j <= len(s)) + # pre-conditions-end + + # impl-start if i == j: return 0 else: return (digits__sum(s[j - 1]) > 0) + get_positive(i, j - 1, s) + # impl-end @Pure def digits__sum(x : int) -> int : + # impl-start if abs(x) < 10: return x % 10 else: return (10 - x % 10) + digits__sum(x // 10) + # impl-end @Pure def abs(x : int) -> int : + # pre-conditions-start Ensures((Result()) >= (0)) Ensures((Result()) == (x) or (Result()) == (0) - (x)) + # pre-conditions-end + + # impl-start if (x) >= (0): return x elif True: - return (0) - (x) \ No newline at end of file + return (0) - (x) + # impl-end \ No newline at end of file diff --git a/Bench/109-move_one_ball.py b/Bench/109-move_one_ball.py index 6a7335c..de2fca4 100644 --- a/Bench/109-move_one_ball.py +++ b/Bench/109-move_one_ball.py @@ -3,18 +3,26 @@ @Pure def is__sorted(a : List[int], l : int, r : int) -> bool : + # pre-conditions-start Requires(Acc(list_pred(a))) Requires(((0) <= (l)) and ((l) <= (r)) and ((r) <= (len(a)))) + # pre-conditions-end + + # impl-start return Forall(int, lambda d_0_i_: Forall(int, lambda d_1_j_: not ((((l) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (r))) or (((a)[d_0_i_]) <= ((a)[d_1_j_])))) + # impl-end def move__one__ball(a : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(a))) Requires((len(a)) > (0)) Requires(Forall(int, lambda d_2_i_: Forall(int, lambda d_3_j_: not (((((0) <= (d_2_i_)) and ((d_2_i_) < (len(a)))) and (((0) <= (d_3_j_)) and ((d_3_j_) < (len(a))))) and ((d_2_i_) != (d_3_j_))) or (((a)[d_2_i_]) != ((a)[d_3_j_]))))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(a))) Ensures(Forall(int, lambda d_2_i_: Forall(int, lambda d_3_j_: @@ -29,9 +37,14 @@ def move__one__ball(a : List[int]) -> bool: Implies(0 <= d_5_j_ and d_5_j_ < d_4_i_, Forall(int, lambda d_6_j_: Implies(d_4_i_ <= d_6_j_ and d_6_j_ < len(a), a[d_5_j_] > a[d_6_j_]))))))), Result())) + # post-conditions-end + + # impl-start can = False # type : bool if (len(a)) <= (1): + # assert-start Assert(is__sorted(a, 0, len(a))) + # assert-end can = True return can can = False @@ -40,6 +53,7 @@ def move__one__ball(a : List[int]) -> bool: d_6_min__index_ = int(0) # type : int d_6_min__index_ = 0 while (d_5_i_) < (len(a)): + # invariants-start Invariant(Acc(list_pred(a))) Invariant(((0) <= (d_5_i_)) and ((d_5_i_) <= (len(a)))) Invariant(((0) <= (d_6_min__index_)) and ((d_6_min__index_) < (len(a)))) @@ -50,10 +64,12 @@ def move__one__ball(a : List[int]) -> bool: , [[a[d_2_i_]]]))) Invariant(Forall(int, lambda d_7_j_: (not ((((0) <= (d_7_j_)) and ((d_7_j_) < (d_5_i_))) and ((d_6_min__index_) != (d_7_j_))) or (((a)[d_6_min__index_]) < ((a)[d_7_j_])), [[(a)[d_7_j_]]]))) + # invariants-end if ((a)[d_5_i_]) < ((a)[d_6_min__index_]): d_6_min__index_ = d_5_i_ d_5_i_ = (d_5_i_) + (1) + # assert-start Assert(Implies(is__sorted(a, 0, d_6_min__index_) and is__sorted(a, d_6_min__index_, len(a)) and d_6_min__index_ == 0, is__sorted(a, 0, len(a)))) Assert(Implies(is__sorted(a, 0, d_6_min__index_) and is__sorted(a, d_6_min__index_, len(a)) and d_6_min__index_ != 0 and a[len(a) - 1] < a[0], @@ -61,5 +77,7 @@ def move__one__ball(a : List[int]) -> bool: Implies(0 <= d_5_j_ and d_5_j_ < d_6_min__index_, Forall(int, lambda d_6_j_: Implies(d_6_min__index_ <= d_6_j_ and d_6_j_ < len(a), a[d_5_j_] > a[d_6_j_]))))))) + # assert-end can = is__sorted(a, 0, d_6_min__index_) and is__sorted(a, d_6_min__index_, len(a)) and (d_6_min__index_ == 0 or a[len(a) - 1] < a[0]) return can + # impl-end diff --git a/Bench/110-exchange.py b/Bench/110-exchange.py index 43482e1..d1b4971 100644 --- a/Bench/110-exchange.py +++ b/Bench/110-exchange.py @@ -3,28 +3,46 @@ @Pure def IsEven(n : int) -> bool: + # pre-conditions-start Ensures(((Result()) == (True)) or ((Result()) == (False))) + # pre-conditions-end + + # impl-start return (n % 2) == 0 + # impl-end @Pure def CountEvens(i : int, lst : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(lst))) Requires(((i) >= (0)) and ((i) <= (len(lst)))) + # pre-conditions-end + # post-conditions-start Ensures((Result()) >= (0)) Ensures((Result()) <= (len(lst) - i)) + # post-conditions-end + + # impl-start if len(lst) == i: return 0 return CountEvens(i + 1, lst) + IsEven(lst[i]) + # impl-end def Exchange(lst1 : List[int], lst2 : List[int]) -> str: + # pre-conditions-start Requires(Acc(list_pred(lst2))) Requires(Acc(list_pred(lst1))) Requires(((len(lst1)) > (0)) and ((len(lst2)) > (0))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(lst2))) Ensures(Acc(list_pred(lst1))) Ensures(((Result()) == "YES") or ((Result()) == "NO")) Ensures(not ((Result()) == "YES") or (((CountEvens(0, lst1)) + (CountEvens(0, lst2))) >= (len(lst1)))) Ensures(not ((Result()) == "NO") or (((CountEvens(0, lst1)) + (CountEvens(0, lst2))) < (len(lst1)))) + # post-conditions-end + + # impl-start result = "" # type : str d_1_totalEvens_ = int(0) # type : int d_1_totalEvens_ = CountEvens(0, lst1) + CountEvens(0, lst2) @@ -33,3 +51,4 @@ def Exchange(lst1 : List[int], lst2 : List[int]) -> str: elif True: result = "NO" return result + # impl-end diff --git a/Bench/112-reverse_delete.py b/Bench/112-reverse_delete.py index b546d7c..0994058 100644 --- a/Bench/112-reverse_delete.py +++ b/Bench/112-reverse_delete.py @@ -3,25 +3,43 @@ @Pure def InArray(a : List[int], x : int) -> bool : + # pre-conditions-start Requires(Acc(list_pred(a))) + # pre-conditions-end + + # impl-start return Exists(int, lambda d_0_i_: ((((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))) and ((a)[d_0_i_]) == (x)))) + # impl-end @Pure def NotInArray(a : List[int], x : int) -> bool : + # pre-conditions-start Requires(Acc(list_pred(a))) + # pre-conditions-end + + # impl-start return Forall(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))), ((a)[d_0_i_]) != (x)))) + # impl-end @Pure def implArrays(chars : List[int], res : List[int], x : int) -> bool: + # pre-conditions-start Requires(Acc(list_pred(chars))) Requires(Acc(list_pred(res))) + # pre-conditions-end + + # impl-start return Implies(NotInArray(chars, x), InArray(res, x)) + # impl-end def reverse__delete(s : List[int], chars : List[int]) -> Tuple[List[int], bool]: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(Acc(list_pred(chars))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(Result()[0]))) Ensures(Acc(list_pred(s))) Ensures(Acc(list_pred(chars))) @@ -32,11 +50,15 @@ def reverse__delete(s : List[int], chars : List[int]) -> Tuple[List[int], bool]: not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(Result()[0])))) or (InArray(s, (Result()[0])[d_1_i_])))) Ensures(Forall(int, lambda d_6_j_: not ((((0) <= (d_6_j_)) and ((d_6_j_) < (len(s))))) or (implArrays(chars, Result()[0], (s)[d_6_j_])))) + # post-conditions-end + + # impl-start res : List[int] = [] is__palindrome = False # type : bool d_3_i_ = int(0) # type : int d_3_i_ = 0 while (d_3_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(res))) Invariant(Acc(list_pred(s))) Invariant(Acc(list_pred(chars))) @@ -47,34 +69,50 @@ def reverse__delete(s : List[int], chars : List[int]) -> Tuple[List[int], bool]: (not (((0) <= (d_5_i_)) and ((d_5_i_) < (len(res)))) or (InArray(s, res[d_5_i_])), [[InArray(s, res[d_5_i_])]]))) Invariant(Forall(int, lambda d_6_j_: (not ((((0) <= (d_6_j_)) and ((d_6_j_) < (d_3_i_)))) or (implArrays(chars, res, (s)[d_6_j_])), [[InArray(res, (s)[d_6_j_])]]))) + # invariants-end if NotInArray(chars, (s)[d_3_i_]): res = (res) + [(s)[d_3_i_]] d_3_i_ = (d_3_i_) + (1) is__palindrome = is__palindrome__fun(res) return (res, is__palindrome) + # impl-end def is__palindrome__fun(text : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(text), 1/2)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(text), 1/2)) Ensures((Result()) == (Forall(int, lambda d_0_i_: not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(text)))) or (((text)[d_0_i_]) == ((text)[((len(text)) - (d_0_i_)) - (1)]))))) Ensures(Result() == is__palindrome__pred(text)) + # post-conditions-end + + # impl-start result = False # type : bool result = True d_1_i_ = int(0) # type : int d_1_i_ = 0 while (d_1_i_) < ((len(text) // 2)): + # invariant-start Invariant(Acc(list_pred(text), 1/2)) Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= ((len(text) // 2)))) Invariant((result) == (Forall(int, lambda d_2_i1_: (not (((d_2_i1_) >= (0)) and ((d_2_i1_) < (d_1_i_))) or (((text)[d_2_i1_]) == ((text)[((len(text)) - (d_2_i1_)) - (1)])), [[]])))) + # invariant-end if ((text)[d_1_i_]) != ((text)[((len(text)) - (d_1_i_)) - (1)]): result = False d_1_i_ = (d_1_i_) + (1) return result + # impl-end @Pure def is__palindrome__pred(s : List[int]) -> bool : + # pre-conditions-start Requires(Acc(list_pred(s), 1/2)) + # pre-conditions-end + + # impl-start return Forall(int, lambda d_10_k_: (not (((0) <= (d_10_k_)) and ((d_10_k_) < (len(s)))) or (((s)[d_10_k_]) == ((s)[((len(s)) - (1)) - (d_10_k_)])))) + # impl-end diff --git a/Bench/114-minSubArraySum.py b/Bench/114-minSubArraySum.py index da3898d..0f0e1f2 100644 --- a/Bench/114-minSubArraySum.py +++ b/Bench/114-minSubArraySum.py @@ -3,15 +3,23 @@ @Pure def Sum(a : List[int], s : int, t : int) -> int : + # pre-conditions-start Requires(Acc(list_pred(a))) Requires(((0) <= (s)) and ((s) <= (t)) and ((t) <= (len(a)))) + # pre-conditions-end + + # impl-start if s == t: return 0 else: return (a)[t - 1] + (Sum(a, s, t - 1)) + # impl-end def minSubArraySum(a : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(a))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(a))) Ensures(Forall(int, lambda d_1_p_: Forall(int, lambda d_2_q_: @@ -19,6 +27,9 @@ def minSubArraySum(a : List[int]) -> int: Ensures(Exists(int, lambda d_3_k_: Exists(int, lambda d_4_m_: ((((0) <= (d_3_k_)) and ((d_3_k_) <= (d_4_m_))) and ((d_4_m_) <= (len(a)))) and ((Result()) == (Sum(a, d_3_k_, d_4_m_)))))) + # post-conditions-end + + # impl-start s = int(0) # type : int d_5_k_ = int(0) # type : int d_6_m_ = int(0) # type : int @@ -26,6 +37,7 @@ def minSubArraySum(a : List[int]) -> int: d_8_c_ = int(0) # type : int d_9_t_ = int(0) # type : int while (d_7_n_) < (len(a)): + # invariants-start Invariant(Acc(list_pred(a))) Invariant(((0) <= (d_7_n_)) and ((d_7_n_) <= (len(a)))) Invariant(Forall(int, lambda d_1_p_: (Implies(0 <= d_1_p_ and d_1_p_ < len(a), Sum(a, 0, d_1_p_ + 1) == Sum(a, 0, d_1_p_) + a[d_1_p_])))) @@ -42,7 +54,11 @@ def minSubArraySum(a : List[int]) -> int: Invariant(Forall(int, lambda d_11_p_: Forall(int, lambda d_12_q_: (not ((((0) <= (d_11_p_)) and ((d_11_p_) <= (d_12_q_))) and ((d_12_q_) <= (d_7_n_))) or ((Sum(a, d_11_p_, d_12_q_)) >= (Sum(a, d_5_k_, d_6_m_))), [[Sum(a, d_11_p_, d_12_q_)]])))) + # invariants-end + + # assert-start Assert(Sum(a, d_8_c_, d_7_n_ + 1) == Sum(a, d_8_c_, d_7_n_) + a[d_7_n_]) + # assert-end d_9_t_ = (d_9_t_) + ((a)[d_7_n_]) d_7_n_ = (d_7_n_) + (1) if (d_9_t_) > (0): @@ -53,4 +69,5 @@ def minSubArraySum(a : List[int]) -> int: d_6_m_ = d_7_n_ s = d_9_t_ return s + # impl-end diff --git a/Bench/116-sort_array.py b/Bench/116-sort_array.py index 3094c2a..31aab3d 100644 --- a/Bench/116-sort_array.py +++ b/Bench/116-sort_array.py @@ -3,27 +3,38 @@ @Pure def popcount(n : int) -> int : + # pre-conditions-start Requires(n >= 0) + # pre-conditions-end + + # impl-start if n == 0: return 0 else: return ((n % 2)) + popcount(n // 10) + # impl-end def BubbleSort(a1 : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(a1), 1/2)) Requires(Forall(int, lambda i : Implies(i >= 0 and i < len(a1), a1[i] >= 0))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(a1), 1/2)) Ensures(Acc(list_pred(Result()))) Ensures((len(a1)) == (len(Result()))) - # Ensures(ToMS(ToSeq(a1)) == ToMS(ToSeq(Result()))) Ensures(Forall(int, lambda i : Implies(i >= 0 and i < len(Result()), Result()[i] >= 0))) Ensures(Forall(int, lambda d_0_i_: Forall(int, lambda d_1_j_: Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((Result())))), popcount((Result())[d_0_i_]) <= popcount((Result())[d_1_j_]))))) + # post-conditions-end + + # impl-start a = list(a1) # type : List[int] d_2_i_ = int(0) # type : int d_2_i_ = (len((a))) - (1) while (d_2_i_) > (0): + # invariants-start Invariant(Acc(list_pred(a))) Invariant(Forall(int, lambda i : Implies(i >= 0 and i < len(a), a[i] >= 0))) Invariant(Acc(list_pred(a1), 1/2)) @@ -40,9 +51,11 @@ def BubbleSort(a1 : List[int]) -> List[int]: (Implies(((((0) <= (d_5_k_)) and ((d_5_k_) <= (d_2_i_))) and ((d_2_i_) < (d_6_k_k_)) and (d_6_k_k_) < (len((a)))), popcount((a)[d_5_k_]) <= popcount((a)[d_6_k_k_])), [[popcount((a)[d_6_k_k_])]])), [[popcount((a)[d_5_k_])]]))) + # invariants-end d_7_j_ = int(0) # type : int d_7_j_ = 0 while (d_7_j_) < (d_2_i_): + # invariants-start Invariant(Acc(list_pred(a))) Invariant(Forall(int, lambda i : Implies(i >= 0 and i < len(a), a[i] >= 0))) Invariant(Acc(list_pred(a1), 1/2)) @@ -61,11 +74,15 @@ def BubbleSort(a1 : List[int]) -> List[int]: Invariant(Forall(int, lambda d_12_k_: (Implies(((0) <= (d_12_k_)) and ((d_12_k_) <= (d_7_j_)), popcount((a)[d_12_k_]) <= popcount((a)[d_7_j_])), [[popcount((a)[d_12_k_])]]))) + # invariants-end if popcount((a)[d_7_j_]) > popcount((a)[(d_7_j_) + (1)]): rhs0_ = (a)[(d_7_j_) + (1)] # type : int (a)[(d_7_j_) + (1)] = (a)[d_7_j_] (a)[d_7_j_] = rhs0_ + # assert-start Assert(popcount((a)[d_7_j_]) <= popcount((a)[(d_7_j_) + (1)])) + # assert-end d_7_j_ = (d_7_j_) + (1) d_2_i_ = (d_2_i_) - (1) return a + # impl-end diff --git a/Bench/118-get_closest_vowel.py b/Bench/118-get_closest_vowel.py index 446fb26..771efc0 100644 --- a/Bench/118-get_closest_vowel.py +++ b/Bench/118-get_closest_vowel.py @@ -3,16 +3,23 @@ @Pure def IsVowel(c : int) -> bool : + # impl-start return ((((((((((c) == (97)) or ((c) == (101))) or ((c) == (105))) or ((c) == (111))) or ((c) == (117))) or ((c) == (65))) or ((c) == (69))) or ((c) == (73))) or ((c) == (79))) or ((c) == (85)) + # impl-end @Pure def IsConsonant(c : int) -> bool : + # impl-start return ((((65) <= (c)) and ((c) <= (90))) or (((97) <= (c)) and ((c) <= (122)))) and (not(IsVowel(c))) + # impl-end def get__closest__vowel(word : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(word))) Requires(Forall(int, lambda d_0_i_: not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(word)))) or ((((65) <= ((word)[d_0_i_])) and (((word)[d_0_i_]) <= (90))) or (((97) <= ((word)[d_0_i_])) and (((word)[d_0_i_]) <= (122)))))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(word))) Ensures(Acc(list_pred(Result()))) Ensures((len(Result())) <= (1)) @@ -21,6 +28,9 @@ def get__closest__vowel(word : List[int]) -> List[int]: ((((((1) <= (d_1_i_)) and (((d_1_i_) + (1)) < (len(word)))) and (IsVowel((word)[d_1_i_]))) and (IsConsonant((word)[(d_1_i_) - (1)]))) and (IsConsonant((word)[(d_1_i_) + (1)]))) and ( Forall(int, lambda j: not (j > d_1_i_ and j <= len(word) - 2) or (((not(IsVowel((word)[j]))) or (not(IsConsonant((word)[j - 1])))) or (not(IsConsonant((word)[j + 1]))))))))) + # post-conditions-end + + # impl-start result = list([int(0)] * 0) # type : List[int] if (len(word)) < (3): result = [] @@ -28,6 +38,7 @@ def get__closest__vowel(word : List[int]) -> List[int]: d_5_i_ = int(0) # type : int d_5_i_ = (len(word)) - (2) while (d_5_i_) > (0): + # invariants-start Invariant(Acc(list_pred(word))) Invariant(Acc(list_pred(result))) Invariant(((0) <= (d_5_i_)) and ((d_5_i_) <= ((len(word)) - (2)))) @@ -35,9 +46,11 @@ def get__closest__vowel(word : List[int]) -> List[int]: not (((d_5_i_) < (d_6_j_)) and ((d_6_j_) < ((len(word)) - (1)))) or (((not(IsVowel((word)[d_6_j_]))) or (not(IsConsonant((word)[(d_6_j_) - (1)])))) or (not(IsConsonant((word)[(d_6_j_) + (1)])))))) Invariant(Forall(int, lambda j: not (j > d_5_i_ and j <= len(word) - 2) or (((not(IsVowel((word)[j]))) or (not(IsConsonant((word)[j - 1])))) or (not(IsConsonant((word)[j + 1])))))) + # invariants-end if ((IsVowel((word)[d_5_i_])) and (IsConsonant((word)[(d_5_i_) - (1)]))) and (IsConsonant((word)[(d_5_i_) + (1)])): result = [(word)[d_5_i_]] return result d_5_i_ = (d_5_i_) - (1) result = [] return result + # impl-end diff --git a/Bench/119-match_parens.py b/Bench/119-match_parens.py index 7c7c60a..8ea06d8 100644 --- a/Bench/119-match_parens.py +++ b/Bench/119-match_parens.py @@ -3,21 +3,29 @@ @Pure def CalBal(s : List[int], i : int, j : int, acc : int) -> int: + # pre-conditions-start Requires(Acc(list_pred(s), 1/2)) Requires(0 <= i and i <= j and j <= len(s)) + # pre-conditions-end + + # impl-start if i == j: return acc else: return (1 if s[j - 1] == 0 else -1) + CalBal(s, i, j - 1, acc) + # impl-end def checkFixed(s1 : List[int], s2 : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(s1))) Requires(Forall(int, lambda d_0_i_: not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(s1)))) or ((((s1)[d_0_i_]) == (0)) or (((s1)[d_0_i_]) == (1))))) Requires(Acc(list_pred(s2))) Requires(Forall(int, lambda d_0_i_: not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(s2)))) or ((((s2)[d_0_i_]) == (0)) or (((s2)[d_0_i_]) == (1))))) - + # pre-conditions-end + + # post-conditions-start Ensures(Acc(list_pred(s1))) Ensures(Forall(int, lambda d_0_i_: not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(s1)))) or ((((s1)[d_0_i_]) == (0)) or (((s1)[d_0_i_]) == (1))))) @@ -30,11 +38,14 @@ def checkFixed(s1 : List[int], s2 : List[int]) -> bool: Ensures(Implies(not(Result()), Exists(int, lambda x: x >= 0 and x <= len(s1) and CalBal(s1, 0, x, 0) < 0) or Exists(int, lambda x: x >= 0 and x <= len(s2) and CalBal(s1, 0, len(s1), 0) + CalBal(s2, 0, x, 0) < 0))) - + # post-conditions-end + + # impl-start bal = 0 # type : int i = 0 # type : int while i < len(s1): + # invariants-start Invariant(Acc(list_pred(s1), 1/2)) Invariant(0 <= i and i <= len(s1)) Invariant(Forall(int, lambda d_0_i_: @@ -43,20 +54,26 @@ def checkFixed(s1 : List[int], s2 : List[int]) -> bool: Invariant(bal == CalBal(s1, 0, i, 0)) Invariant(bal >= 0) Invariant(Forall(int, lambda x: (Implies(x >= 0 and x <= i, CalBal(s1, 0, x, 0) >= 0), [[CalBal(s1, 0, x, 0) >= 0]]))) + # invariants-end + # assert-start Assert(CalBal(s1, 0, i + 1, 0) == CalBal(s1, 0, i, 0) + (1 if s1[i] == 0 else -1)) + # assert-end if s1[i] == 0: bal = bal + 1 else: bal = bal - 1 if bal < 0: + # assert-start Assert(Exists(int, lambda x: x >= 0 and x <= len(s1) and CalBal(s1, 0, x, 0) < 0)) + # assert-end return False i = i + 1 i = 0 while i < len(s2): + # invariants-start Invariant(Acc(list_pred(s1), 1/2)) Invariant(Acc(list_pred(s2), 1/2)) Invariant(0 <= i and i <= len(s2)) @@ -68,6 +85,7 @@ def checkFixed(s1 : List[int], s2 : List[int]) -> bool: Invariant(bal >= 0) Invariant(Forall(int, lambda x: (Implies(x >= 0 and x <= len(s1), CalBal(s1, 0, x, 0) >= 0), [[CalBal(s1, 0, x, 0) >= 0]]))) Invariant(Forall(int, lambda x: (Implies(x >= 0 and x <= i, CalBal(s1, 0, len(s1), 0) + CalBal(s2, 0, x, 0) >= 0), [[CalBal(s1, 0, len(s1), 0) + CalBal(s2, 0, x, 0) >= 0]]))) + # invariants-end if s2[i] == 0: bal = bal + 1 @@ -75,9 +93,12 @@ def checkFixed(s1 : List[int], s2 : List[int]) -> bool: bal = bal - 1 if bal < 0: + # assert-start Assert(CalBal(s1, 0, len(s1), 0) + CalBal(s2, 0, i + 1, 0) < 0) Assert(Exists(int, lambda x: x >= 0 and x <= len(s2) and CalBal(s1, 0, len(s1), 0) + CalBal(s2, 0, x, 0) < 0)) + # assert-end return False i = i + 1 - return True \ No newline at end of file + return True + # impl-end \ No newline at end of file diff --git a/Bench/121-solution.py b/Bench/121-solution.py index e80cc8d..a8dbebc 100644 --- a/Bench/121-solution.py +++ b/Bench/121-solution.py @@ -3,25 +3,39 @@ @Pure def psum(i : int, j : int, s : List[int]) -> int : + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(0 <= i and i <= j + 1 and j < len(s)) + # pre-conditions-end + + # impl-start if i > j: return 0 else: return ((s)[j] if ((((j) % 2) == 0) and ((s)[j] % 2 == 1)) else 0) + (psum(i, j - 1, s)) + # impl-end def add(v : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(v))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(v))) Ensures((Result()) == (psum(0, len(v) - 1, v))) + # post-conditions-end + + # impl-start r = int(0) # type : int r = 0 d_2_k_ = int(0) # type : int d_2_k_ = 0 while (d_2_k_) < (len(v)): + # invariants-start Invariant(Acc(list_pred(v))) Invariant(((0) <= (d_2_k_)) and ((d_2_k_) <= (len(v)))) Invariant((r) == (psum(0, d_2_k_ - 1, v))) + # invariants-end r = (r) + (((v)[d_2_k_] if ((((d_2_k_) % 2) == 0) and ((v)[d_2_k_] % 2 == 1)) else 0)) d_2_k_ = (d_2_k_) + (1) return r + # impl-end diff --git a/Bench/122-add_elements.py b/Bench/122-add_elements.py index f491786..acded76 100644 --- a/Bench/122-add_elements.py +++ b/Bench/122-add_elements.py @@ -3,31 +3,47 @@ @Pure def sum__chars__rec(i : int, j : int, lst : List[int]) -> int : + # pre-conditions-start Requires(Acc(list_pred(lst))) Requires(((0) <= (i)) and ((i) <= (j)) and ((j) <= (len(lst)))) + # pre-conditions-end + + # impl-start if i == j: return 0 else: return ((lst)[j - 1] if (lst)[j - 1] < 100 else 0) + sum__chars__rec(i, j - 1, lst) + # impl-end def SumElementsWithAtMostTwoDigits(lst : List[int], k : int) -> int: + # pre-conditions-start Requires(Acc(list_pred(lst))) Requires(1 <= k and k <= len(lst)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(lst))) Ensures(1 <= k and k <= len(lst)) Ensures(Result() == sum__chars__rec(0, k, lst)) + # post-conditions-end + + # impl-start sum = int(0) # type : int sum = 0 d_3_i_ = int(0) # type : int d_3_i_ = 0 while d_3_i_ < k: + # invariants-start Invariant(Acc(list_pred(lst))) Invariant(1 <= k and k <= len(lst)) Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(lst))) and d_3_i_ <= k) Invariant(Forall(int, lambda d_3_i_ : (Implies(d_3_i_ >= 0 and d_3_i_ < k, sum__chars__rec(0, d_3_i_ + 1, lst) == sum__chars__rec(0, d_3_i_, lst) + ((lst)[d_3_i_] if (lst)[d_3_i_] < 100 else 0)), [[sum__chars__rec(0, d_3_i_ + 1, lst)]]))) Invariant((sum) == (sum__chars__rec(0, d_3_i_, lst))) + # invariants-end + # assert-start Assert(sum__chars__rec(0, d_3_i_ + 1, lst) == sum__chars__rec(0, d_3_i_, lst) + ((lst)[d_3_i_] if (lst)[d_3_i_] < 100 else 0)) + # assert-end if (lst)[d_3_i_] < 100: sum = (sum) + ((lst)[d_3_i_]) d_3_i_ = (d_3_i_) + (1) - return sum \ No newline at end of file + return sum + # impl-end \ No newline at end of file diff --git a/Bench/123-get_odd_collatz.py b/Bench/123-get_odd_collatz.py index 9d17901..2e8b554 100644 --- a/Bench/123-get_odd_collatz.py +++ b/Bench/123-get_odd_collatz.py @@ -3,47 +3,73 @@ @Pure def iterate__to__odd(n : int) -> int : + # pre-conditions-start Requires(n % 2 == 0) Requires(n >= 0) + # pre-conditions-end + # post-conditions-start Ensures(Result() % 2 == 1) Ensures(Result() > 0) + # post-conditions-end + + # impl-start if (n // 2) % 2 == 1: return n // 2 else: return iterate__to__odd(n // 2) + # impl-end @Pure def next__odd__collatz(n : int) -> int : + # pre-conditions-start Requires(n > 0) + # pre-conditions-end + # post-conditions-start Ensures(Result() > 0) Ensures(Result() % 2 == 1) + # post-conditions-end + + # impl-start if ((n % 2)) == (0): return iterate__to__odd(n) else: return iterate__to__odd(((3) * (n)) + (1)) + # impl-end def next__odd__collatz__iter(n : int) -> int: + # pre-conditions-start Requires((n) > (0)) + # pre-conditions-end + # post-conditions-start Ensures(Result() > 0) Ensures(((Result() % 2)) == (1)) Ensures((Result()) == (next__odd__collatz(n))) + # post-conditions-end + + # impl-start next = int(0) # type : int next = n if ((next % 2)) == (1): next = ((3) * (next)) + (1) d_0_start_ = next # type : int while ((next % 2)) == (0): + # invariants-start Invariant((next) > (0)) Invariant(not (((next % 2)) == (0)) or ((next__odd__collatz(next)) == (next__odd__collatz(n)))) Invariant(not (((next % 2)) == (0)) or ((iterate__to__odd(next)) == (iterate__to__odd(d_0_start_)))) Invariant(not (((next % 2)) == (1)) or ((next) == (iterate__to__odd(d_0_start_)))) + # invariants-end next = (next // 2) return next + # impl-end def get__odd__collatz__unsorted(n : int) -> List[int]: + # pre-conditions-start Requires((n) > (1)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(Result()))) Ensures(Forall(int, lambda d_1_i_: not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(Result())))) or ((((Result())[d_1_i_] > 0))))) @@ -51,18 +77,22 @@ def get__odd__collatz__unsorted(n : int) -> List[int]: not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(Result())))) or ((((Result())[d_1_i_] % 2)) == (1)))) Ensures(Forall(int, lambda d_2_i_: not (((1) <= (d_2_i_)) and ((d_2_i_) < (len(Result())))) or (((Result())[d_2_i_]) == (next__odd__collatz((Result())[(d_2_i_) - (1)]))))) + # post-conditions-end odd__collatz = list([int(0)] * 0) # type : List[int] d_3_cur_ = int(0) # type : int d_3_cur_ = n if ((d_3_cur_ % 2)) == (0): d_3_cur_ = next__odd__collatz__iter(d_3_cur_) odd__collatz = [d_3_cur_] + # assert-start Assert(len(odd__collatz) == 1) Assert(Forall(int, lambda d_5_i_: (not (((1) <= (d_5_i_)) and ((d_5_i_) < (len(odd__collatz))))))) Assert(Forall(int, lambda d_5_i_: (not (((1) <= (d_5_i_)) and ((d_5_i_) < (len(odd__collatz)))) or (((odd__collatz)[d_5_i_]) == (next__odd__collatz((odd__collatz)[(d_5_i_) - (1)]))), [[next__odd__collatz((odd__collatz)[(d_5_i_) - (1)])]]))) + # assert-end while ((odd__collatz)[(len(odd__collatz)) - (1)]) != (1): + # invariants-start Invariant(Acc(list_pred(odd__collatz))) Invariant((d_3_cur_) > (0)) Invariant((len(odd__collatz)) > (0)) @@ -72,26 +102,36 @@ def get__odd__collatz__unsorted(n : int) -> List[int]: (not (((0) <= (d_4_i_)) and ((d_4_i_) < (len(odd__collatz)))) or ((((odd__collatz)[d_4_i_] % 2)) == (1))))) Invariant(Forall(int, lambda d_5_i_: (not (((1) <= (d_5_i_)) and ((d_5_i_) < (len(odd__collatz)))) or (((odd__collatz)[d_5_i_]) == (next__odd__collatz((odd__collatz)[(d_5_i_) - (1)]))), [[next__odd__collatz((odd__collatz)[(d_5_i_) - (1)])]]))) + # invariants-end odd__collatz = (odd__collatz) + [next__odd__collatz((odd__collatz)[(len(odd__collatz)) - (1)])] + # assert-start Assert(Forall(int, lambda d_5_i_: (not (((1) <= (d_5_i_)) and ((d_5_i_) < (len(odd__collatz)))) or (((odd__collatz)[d_5_i_]) == (next__odd__collatz((odd__collatz)[(d_5_i_) - (1)]))), [[next__odd__collatz((odd__collatz)[(d_5_i_) - (1)])]]))) + # assert-end return odd__collatz def get__odd__collatz(n : int) -> List[int]: + # pre-conditions-start Requires((n) > (1)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(Result()))) Ensures(Forall(int, lambda d_6_i_: Forall(int, lambda d_7_j_: not ((((0) <= (d_6_i_)) and ((d_6_i_) < (d_7_j_))) and ((d_7_j_) < (len(Result())))) or (((Result())[d_6_i_]) <= ((Result())[d_7_j_]))))) Ensures(Forall(int, lambda d_8_i_: not (((0) <= (d_8_i_)) and ((d_8_i_) < (len(Result())))) or ((((Result())[d_8_i_] % 2)) == (1)))) + # post-conditions-end + + # impl-start sorted = list([int(0)] * 0) # type : List[int] sorted = get__odd__collatz__unsorted(n) d_12_unsorted_ = list(sorted) # type : List[int] d_9_i_ = int(0) # type : int d_9_i_ = 0 while (d_9_i_) < (len(sorted)): + # invariants-start Invariant(Acc(list_pred(sorted))) Invariant(Acc(list_pred(d_12_unsorted_))) Invariant(((0) <= (d_9_i_)) and ((d_9_i_) <= (len(sorted)))) @@ -108,11 +148,13 @@ def get__odd__collatz(n : int) -> List[int]: (Forall(int, lambda d_13_k_: (not ((((d_9_i_) <= (d_13_k_)) and ((d_13_k_) < (len(sorted))))) or (((sorted)[d_12_j_]) <= ((sorted)[d_13_k_])), [[sorted[d_13_k_]]]))), [[(sorted)[d_12_j_]]]))) + # invariants-end d_15_minIndex_ = int(0) # type : int d_15_minIndex_ = d_9_i_ d_16_j_ = int(0) # type : int d_16_j_ = (d_9_i_) + (1) while (d_16_j_) < (len(sorted)): + # invariants-start Invariant(Acc(list_pred(sorted))) Invariant(Acc(list_pred(d_12_unsorted_))) Invariant((len(sorted)) == (len(d_12_unsorted_))) @@ -132,6 +174,7 @@ def get__odd__collatz(n : int) -> List[int]: (((sorted)[d_12_j_]) <= ((sorted)[d_13_k_])), [[sorted[d_13_k_]]]))), [[(sorted)[d_12_j_]]]))) Invariant(Forall(int, lambda d_17_k_: (not (((d_9_i_) <= (d_17_k_)) and ((d_17_k_) < (d_16_j_))) or (((sorted)[d_15_minIndex_]) <= ((sorted)[d_17_k_])), [[(sorted)[d_17_k_]]]))) + # invariants-end if ((sorted)[d_16_j_]) < ((sorted)[d_15_minIndex_]): d_15_minIndex_ = d_16_j_ d_16_j_ = (d_16_j_) + (1) @@ -141,4 +184,5 @@ def get__odd__collatz(n : int) -> List[int]: (sorted)[d_15_minIndex_] = rhs0_ d_9_i_ = (d_9_i_) + (1) return sorted + # impl-end diff --git a/Bench/126-is_sorted.py b/Bench/126-is_sorted.py index f75cd17..638acaa 100644 --- a/Bench/126-is_sorted.py +++ b/Bench/126-is_sorted.py @@ -3,7 +3,10 @@ def is__sorted(a : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(a))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(a))) Ensures((Result()) == ( Forall(int, lambda d_0_i_: @@ -11,23 +14,28 @@ def is__sorted(a : List[int]) -> bool: not ((((0) <= (d_0_i_)) and ((d_0_i_) <= (d_1_j_))) and ((d_1_j_) < (len(a)))) or ((((a)[d_0_i_]) <= ((a)[d_1_j_]))))) and (Forall(int, lambda d_2_i_: not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(a)))) or ((count_set(a, (a)[d_2_i_], 0)) <= (2)))))) + # post-conditions-end f = False # type : bool if (len(a)) == (0): f = True + # assert-start Assert(Forall(int, lambda d_0_i_: Forall(int, lambda d_1_j_: not ((((0) <= (d_0_i_)) and ((d_0_i_) <= (d_1_j_))) and ((d_1_j_) < (len(a)))) or ((((a)[d_0_i_]) <= ((a)[d_1_j_])))))) + # assert-end return f d_3_is__asc_ = False # type : bool d_3_is__asc_ = True d_4_i_ = int(0) # type : int d_4_i_ = 1 while (d_4_i_) < (len(a)): + # invariants-start Invariant(Acc(list_pred(a))) Invariant(((1) <= (d_4_i_)) and ((d_4_i_) <= (len(a)))) Invariant((d_3_is__asc_) == (Forall(int, lambda d_5_j_: Forall(int, lambda d_6_k_: not ((((0) <= (d_5_j_)) and ((d_5_j_) < (d_6_k_))) and ((d_6_k_) < (d_4_i_))) or (((a)[d_5_j_]) <= ((a)[d_6_k_])))))) + # invariants-end if ((a)[(d_4_i_) - (1)]) > ((a)[d_4_i_]): d_3_is__asc_ = False d_4_i_ = (d_4_i_) + (1) @@ -38,6 +46,7 @@ def is__sorted(a : List[int]) -> bool: d_7_has__no__more__that__2_ = False # type : bool d_7_has__no__more__that__2_ = True while (d_4_i_) < (len(a)): + # invariants-start Invariant(Acc(list_pred(a))) Invariant(((0) <= (d_4_i_)) and ((d_4_i_) <= (len(a)))) Invariant((d_3_is__asc_) == @@ -49,6 +58,7 @@ def is__sorted(a : List[int]) -> bool: (Forall(int, lambda d_5_j_: Forall(int, lambda d_6_k_: not ((((0) <= (d_5_j_)) and ((d_5_j_) < (d_6_k_))) and ((d_6_k_) < (len(a)))) or (((a)[d_5_j_]) <= ((a)[d_6_k_]))))))) + # invariants-end d_9_count_ = int(0) # type : int d_9_count_ = count_set(a, (a)[d_4_i_], 0) if (d_9_count_) > (2): @@ -59,10 +69,15 @@ def is__sorted(a : List[int]) -> bool: @Pure def count_set(a : List[int], x : int, i : int) -> int: + # pre-conditions-start Requires(Acc(list_pred(a), 1/2)) Requires(((0) <= (i)) and ((i) <= (len(a)))) + # pre-conditions-end + + # impl-start if (i) == 0: return 0 else: return (count_set(a, x, (i) - (1))) + (((a)[(i) - (1)]) == (x)) + # impl-end diff --git a/Bench/127-intersection.py b/Bench/127-intersection.py index 44978eb..0625a46 100644 --- a/Bench/127-intersection.py +++ b/Bench/127-intersection.py @@ -3,27 +3,39 @@ @Pure def IsPrime(n : int) -> bool : + # impl-start return ((n) > (1)) and (Forall(int, lambda d_0_k_: not (((2) <= (d_0_k_)) and ((d_0_k_) < (n))) or (((n % d_0_k_)) != (0)))) + # impl-end @Pure def min(a : int, b : int) -> int : + # impl-start if (a) <= (b): return a elif True: return b + # impl-end @Pure def max(a : int, b : int) -> int : + # impl-start if (a) >= (b): return a elif True: return b + # impl-end def Intersection(start1 : int, end1 : int, start2 : int, end2 : int) -> str: + # pre-conditions-start Requires(((start1) <= (end1)) and ((start2) <= (end2))) + # pre-conditions-end + # post-conditions-start Ensures(((Result()) == ("YES")) or ((Result()) == ("NO"))) Ensures(((Result()) == ("YES")) == (((max(start1, start2)) <= (min(end1, end2))) and (IsPrime(((min(end1, end2)) - (max(start1, start2))) + (1))))) + # post-conditions-end + + # impl-start result = "" # type : str d_1_intersectionStart_ = int(0) # type : int d_1_intersectionStart_ = max(start1, start2) @@ -37,3 +49,4 @@ def Intersection(start1 : int, end1 : int, start2 : int, end2 : int) -> str: return result result = "NO" return result + # impl-end diff --git a/Bench/128-prod_signs.py b/Bench/128-prod_signs.py index 4bc72a3..8e5c0df 100644 --- a/Bench/128-prod_signs.py +++ b/Bench/128-prod_signs.py @@ -3,40 +3,59 @@ @Pure def abs(x : int) -> int : + # impl-start if (x) >= (0): return x elif True: return (0) - (x) + # impl-end @Pure def sum__abs(i : int, j : int, s : List[int]) -> int : + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(0 <= i and i <= j and j <= len(s)) + # pre-conditions-end + + # impl-start if i == j: return 0 else: return abs(s[j - 1]) + sum__abs(i, j - 1, s) + # impl-end @Pure def count_negatives(i : int, j : int, s : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(0 <= i and i <= j and j <= len(s)) + # pre-conditions-end + + # impl-start if i == j: return 0 else: return (1 if s[j - 1] < 0 else 0) + count_negatives(i, j - 1, s) + # impl-end def prod__signs(numbers : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(numbers))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(numbers))) Ensures((abs(Result())) == (sum__abs(0, len(numbers), numbers))) Ensures(Implies(count_negatives(0, len(numbers), numbers) % 2 == 1, Result() <= 0)) Ensures(Implies(count_negatives(0, len(numbers), numbers) % 2 == 0, Result() >= 0)) + # post-conditions-end + + # impl-start s = int(0) # type : int s = 0 d_3_i_ = int(0) # type : int d_3_i_ = 0 while (d_3_i_) < (len(numbers)): + # invariants-start Invariant(Acc(list_pred(numbers))) Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(numbers)))) Invariant(Forall(int, lambda x : @@ -44,22 +63,29 @@ def prod__signs(numbers : List[int]) -> int: [[sum__abs(0, x + 1, numbers)]]))) Invariant((s) == (sum__abs(0, d_3_i_, numbers))) Invariant(s >= 0) + # invariants-end + + # assert-start Assert(sum__abs(0, d_3_i_ + 1, numbers) == sum__abs(0, d_3_i_, numbers) + abs(numbers[d_3_i_])) + # assert-end s = (s) + (abs((numbers)[d_3_i_])) d_3_i_ = (d_3_i_) + (1) d_3_i_ = 0 d_4_cnt_ = int(0) # type : int d_4_cnt_ = 0 while (d_3_i_) < (len(numbers)): + # invariants-start Invariant(Acc(list_pred(numbers))) Invariant(s == sum__abs(0, len(numbers), numbers)) Invariant(s >= 0) Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(numbers)))) Invariant(Forall(int, lambda x : Implies(0 <= x and x < len(numbers), count_negatives(0, x + 1, numbers) == count_negatives(0, x, numbers) + (1 if numbers[x] < 0 else 0)))) Invariant((d_4_cnt_) == (count_negatives(0, d_3_i_, numbers))) + # invariants-end if ((numbers)[d_3_i_]) < (0): d_4_cnt_ = (d_4_cnt_) + (1) d_3_i_ = (d_3_i_) + (1) if ((d_4_cnt_ % 2)) == (1): s = (0) - (s) return s + # impl-end diff --git a/Bench/132-is_nested.py b/Bench/132-is_nested.py index 6e87c93..cc3f90b 100644 --- a/Bench/132-is_nested.py +++ b/Bench/132-is_nested.py @@ -2,7 +2,10 @@ from nagini_contracts.contracts import * def is_nested(s : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures(Implies(Result(), Exists(int, lambda x: x >= 0 and x < len(s) and s[x] == 0 and @@ -20,10 +23,13 @@ def is_nested(s : List[int]) -> bool: z > y and z < len(s) and s[z] == 1 and Exists(int, lambda w: w > z and w < len(s) and s[w] == 1)))), Result())) + # post-conditions-end + # impl-start bal = 0 i = 0 while i < len(s) and bal < 2: + # invariants-start Invariant(Acc(list_pred(s))) Invariant(0 <= i and i <= len(s)) Invariant(0 <= bal and bal <= 2) @@ -43,19 +49,23 @@ def is_nested(s : List[int]) -> bool: y > x and y < i and s[y] == 0 and Exists(int, lambda z: z > y and z < i and s[z] == 1))))) + # invariants-end if s[i] == 0: bal = bal + 1 i = i + 1 if bal < 2: + # assert-start Assert(not(Exists(int, lambda x: x >= 0 and x < i and s[x] == 0 and Exists(int, lambda y: y > x and y < i and s[y] == 0)))) + # assert-end return False while i < len(s) and bal > 0: + # invariants-start Invariant(Acc(list_pred(s))) Invariant(0 <= i and i <= len(s)) Invariant(0 <= bal and bal <= 2) @@ -91,8 +101,10 @@ def is_nested(s : List[int]) -> bool: z > y and z < i and s[z] == 1 and Exists(int, lambda w: w > z and w < i and s[w] == 1)))), bal == 0)) + # invariants-end if s[i] == 1: bal = bal - 1 + # assert-start Assert(Exists(int, lambda x: x >= 0 and x < i and s[x] == 0 and Exists(int, lambda y: @@ -100,9 +112,11 @@ def is_nested(s : List[int]) -> bool: Exists(int, lambda z: z > y and z <= i and s[z] == 1)))) Assert(bal <= 1) + # assert-end i = i + 1 if bal > 0: + # assert-start Assert(not(Exists(int, lambda x: x >= 0 and x < i and s[x] == 0 and Exists(int, lambda y: @@ -111,7 +125,9 @@ def is_nested(s : List[int]) -> bool: z > y and z < i and s[z] == 1 and Exists(int, lambda w: w > z and w < i and s[w] == 1)))))) + # assert-end return False return True + # impl-end diff --git a/Bench/133-sum-squares.py b/Bench/133-sum-squares.py index 9f9dbc4..991e9b7 100644 --- a/Bench/133-sum-squares.py +++ b/Bench/133-sum-squares.py @@ -3,26 +3,40 @@ @Pure def psum(i : int, j : int, s : List[int]) -> int : + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(0 <= i and i <= j and j <= len(s)) + # pre-conditions-end + + # impl-start if i == j: return 0 else: return (s)[j - 1] * (s)[j - 1] + (psum(i, j - 1, s)) + # impl-end def sum__squares(lst : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(lst))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(lst))) Ensures((Result()) == (psum(0, len(lst), lst))) + # post-conditions-end + + # impl-start r = int(0) # type : int r = 0 d_2_k_ = int(0) # type : int d_2_k_ = 0 while (d_2_k_) < (len(lst)): + # invariants-start Invariant(Acc(list_pred(lst))) Invariant(((0) <= (d_2_k_)) and ((d_2_k_) <= (len(lst)))) Invariant((r) == (psum(0, d_2_k_, lst))) Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(lst)))) or ((psum(0, d_2_i_ + 1, lst)) == (psum(0, d_2_i_, lst) + lst[d_2_i_] * lst[d_2_i_])), [[psum(0, d_2_i_ + 1, lst)]]))) + # invariants-end r = (r) + ((lst)[d_2_k_]) * ((lst)[d_2_k_]) d_2_k_ = (d_2_k_) + (1) return r + # impl-end diff --git a/Bench/134-check_if_last_char_is_a_letter.py b/Bench/134-check_if_last_char_is_a_letter.py index 031dfcd..32693f3 100644 --- a/Bench/134-check_if_last_char_is_a_letter.py +++ b/Bench/134-check_if_last_char_is_a_letter.py @@ -2,13 +2,22 @@ from nagini_contracts.contracts import * def check__if__last__char__is__a__letter(s : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures((Result()) == ((((len(s)) > (0)) and (is__alpha((s)[(len(s)) - (1)]))) and (((len(s)) == (1)) or (((s)[(len(s)) - (2)]) == (32))))) + # post-conditions-end + + # impl-start b = False # type : bool b = (((len(s)) > (0)) and (is__alpha((s)[(len(s)) - (1)]))) and (((len(s)) == (1)) or (((s)[(len(s)) - (2)]) == (32))) return b + # impl-end @Pure def is__alpha(c : int) -> bool : + # impl-start return (((97) <= (c)) and ((c) <= (122))) or (((65) <= (c)) and ((c) <= (90))) + # impl-end diff --git a/Bench/135_can_arrange.py b/Bench/135_can_arrange.py index e9ddffe..d9a2d38 100644 --- a/Bench/135_can_arrange.py +++ b/Bench/135_can_arrange.py @@ -2,22 +2,29 @@ from nagini_contracts.contracts import * def can__arrange(arr : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(arr))) Requires((len(arr)) > (0)) Requires(Forall(int, lambda d_0_i_: Forall(int, lambda d_1_j_: not ((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len(arr)))) or (((arr)[d_0_i_]) != ((arr)[d_1_j_]))))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(arr))) Ensures(not ((Result()) == (-1)) or (Forall(int, lambda d_2_i_: not (((1) <= (d_2_i_)) and ((d_2_i_) < (len(arr)))) or (((arr)[d_2_i_]) >= ((arr)[(d_2_i_) - (1)]))))) Ensures(not ((Result()) >= (0)) or ((((1) <= (Result())) and ((Result()) < (len(arr)))) and (((arr)[Result()]) < ((arr)[(Result()) - (1)])))) Ensures(not ((Result()) >= (0)) or (Forall(int, lambda d_3_i_: not (((Result()) < (d_3_i_)) and ((d_3_i_) < (len(arr)))) or (((arr)[d_3_i_]) >= ((arr)[(d_3_i_) - (1)]))))) + # post-conditions-end + + # impl-start pos = int(0) # type : int d_4_i_ = int(0) # type : int d_4_i_ = 1 pos = -1 while (d_4_i_) < (len(arr)): + # invariants-start Invariant(Acc(list_pred(arr))) Invariant(((1) <= (d_4_i_)) and ((d_4_i_) <= (len(arr)))) Invariant(not ((pos) == (-1)) or (Forall(int, lambda d_5_j_: @@ -25,8 +32,10 @@ def can__arrange(arr : List[int]) -> int: Invariant(not ((pos) >= (0)) or ((((1) <= (pos)) and ((pos) < (d_4_i_))) and (((arr)[pos]) < ((arr)[(pos) - (1)])))) Invariant(not ((pos) >= (0)) or (Forall(int, lambda d_6_j_: not (((pos) < (d_6_j_)) and ((d_6_j_) < (d_4_i_))) or (((arr)[d_6_j_]) >= ((arr)[(d_6_j_) - (1)]))))) + # invariants-end if ((arr)[d_4_i_]) < ((arr)[(d_4_i_) - (1)]): pos = d_4_i_ d_4_i_ = (d_4_i_) + (1) return pos + # impl-end diff --git a/Bench/136-largest_smallest_integers.py b/Bench/136-largest_smallest_integers.py index fbb8676..35d27f4 100644 --- a/Bench/136-largest_smallest_integers.py +++ b/Bench/136-largest_smallest_integers.py @@ -3,11 +3,19 @@ @Pure def getVal(mx: Optional[int]) -> int: + # pre-conditions-start Requires(mx is not None) + # pre-conditions-end + + # impl-start return mx + # impl-end def largest__smallest__integers(arr : List[int]) -> Tuple[Optional[int], Optional[int]]: + # pre-conditions-start Requires(Acc(list_pred(arr))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(arr))) Ensures(not ((Result()[0]) is None) or (Forall(int, lambda d_0_i_: not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(arr)))) or (((arr)[d_0_i_]) >= (0))))) @@ -19,10 +27,14 @@ def largest__smallest__integers(arr : List[int]) -> Tuple[Optional[int], Optiona Ensures(not ((Result()[1]) is not None) or (((getVal(Result()[1])) in (arr)) and ((getVal(Result()[1])) > (0)))) Ensures(not ((Result()[1]) is not None) or (Forall(int, lambda d_3_i_: not ((((0) <= (d_3_i_)) and ((d_3_i_) < (len(arr)))) and (((arr)[d_3_i_]) > (0))) or (((arr)[d_3_i_]) >= (getVal(Result()[1])))))) + # post-conditions-end + + # impl-start a : Optional[int] = None b : Optional[int] = None d_4_i_ = int(0) while (d_4_i_) < (len(arr)): + # invariants-start Invariant(Acc(list_pred(arr))) Invariant(((0) <= (d_4_i_)) and ((d_4_i_) <= (len(arr)))) Invariant(not ((a) is None) or (Forall(int, lambda d_5_j_: @@ -36,9 +48,11 @@ def largest__smallest__integers(arr : List[int]) -> Tuple[Optional[int], Optiona Invariant(not ((b) is not None) or (((getVal(b)) in (arr)) and ((getVal(b)) > (0)))) Invariant(not ((b) is not None) or (Forall(int, lambda d_8_j_: not ((((0) <= (d_8_j_)) and ((d_8_j_) < (d_4_i_))) and (((arr)[d_8_j_]) > (0))) or (((arr)[d_8_j_]) >= (getVal(b)))))) + # invariants-end if (((arr)[d_4_i_]) < (0)) and (((a) is None) or (((arr)[d_4_i_]) >= (a))): a = ((arr)[d_4_i_]) if (((arr)[d_4_i_]) > (0)) and (((b) is None) or (((arr)[d_4_i_]) <= (getVal(b)))): b = ((arr)[d_4_i_]) d_4_i_ = (d_4_i_) + (1) return (a, b) + # impl-end diff --git a/Bench/138_is_equal_to_sum_even.py b/Bench/138_is_equal_to_sum_even.py index 8924995..c496bf4 100644 --- a/Bench/138_is_equal_to_sum_even.py +++ b/Bench/138_is_equal_to_sum_even.py @@ -2,7 +2,12 @@ from nagini_contracts.contracts import * def is__equal__to__sum__even(n : int) -> bool: + # pre-conditions-start Ensures((Result()) == ((((n % 2)) == (0)) and ((n) >= (8)))) + # pre-conditions-end + + # impl-start b = False # type : bool b = (((n % 2)) == (0)) and ((n) >= (8)) return b + # impl-end diff --git a/Bench/139-special_factorial.py b/Bench/139-special_factorial.py index 14f8e50..7d5e9ba 100644 --- a/Bench/139-special_factorial.py +++ b/Bench/139-special_factorial.py @@ -3,25 +3,45 @@ @Pure def factorial(n : int) -> int : + # pre-conditions-start Requires((n) >= (0)) + # pre-conditions-end + # post-conditions-start Ensures((Result()) >= (0)) + # post-conditions-end + + # impl-start if (n) == (0): return 1 else: return (n) * (factorial((n) - (1))) + # impl-end @Pure def special__factorial__rec(n : int) -> int : + # pre-conditions-start Requires((n) >= (0)) + # pre-conditions-end + # post-conditions-start Ensures((Result()) >= (0)) + # post-conditions-end + + # impl-start if (n) == (0): return 1 else: return factorial(n) * (special__factorial__rec((n) - (1))) + # impl-end def special__factorial(n : int) -> int: + # pre-conditions-start Requires((n) > (0)) + # pre-conditions-end + # post-conditions-start Ensures((Result()) == (special__factorial__rec(n))) + # post-conditions-end + + # impl-start result = int(0) # type : int result = 1 d_2_fact_ = int(0) # type : int @@ -29,12 +49,15 @@ def special__factorial(n : int) -> int: d_3_i_ = int(0) # type : int d_3_i_ = 1 while (d_3_i_) <= (n): + # invariants-start Invariant(((1) <= (d_3_i_)) and ((d_3_i_) <= (n + 1))) Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) <= (n))) or ((factorial(d_2_i_ + 1)) == (factorial(d_2_i_) * (d_2_i_ + 1))), [[factorial(d_2_i_ + 1)]]))) Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) <= (n))) or ((special__factorial__rec(d_2_i_ + 1)) == (special__factorial__rec(d_2_i_) * factorial(d_2_i_ + 1))), [[special__factorial__rec(d_2_i_ + 1)]]))) Invariant((result) == (special__factorial__rec(d_3_i_ - 1))) Invariant((d_2_fact_) == (factorial(d_3_i_ - 1))) + # invariants-end d_2_fact_ = (d_2_fact_) * (d_3_i_) result = (result) * (d_2_fact_) d_3_i_ = (d_3_i_) + (1) return result + # impl-end diff --git a/Bench/142-sum_squares.py b/Bench/142-sum_squares.py index b7571de..a4f6878 100644 --- a/Bench/142-sum_squares.py +++ b/Bench/142-sum_squares.py @@ -3,36 +3,52 @@ @Pure def checkVal(x : int) -> int: + # impl-start if x % 3 == 0: return x * x elif x % 4 == 0 and x % 3 != 0: return x * x * x else: return x + # impl-end @Pure def psum(i : int, j : int, s : List[int]) -> int : + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(0 <= i and i <= j and j <= len(s)) + # pre-conditions-end + + # impl-start if i == j: return 0 else: return checkVal((s)[j - 1]) + (psum(i, j - 1, s)) + # impl-end def sum__squares(lst : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(lst))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(lst))) Ensures((Result()) == (psum(0, len(lst), lst))) + # post-conditions-end + + # impl-start r = int(0) # type : int r = 0 d_2_k_ = int(0) # type : int d_2_k_ = 0 while (d_2_k_) < (len(lst)): + # invariants-start Invariant(Acc(list_pred(lst))) Invariant(((0) <= (d_2_k_)) and ((d_2_k_) <= (len(lst)))) Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(lst)))) or (psum(0, d_2_i_ + 1, lst) == checkVal(lst[d_2_i_]) + psum(0, d_2_i_, lst)), [[psum(0, d_2_i_ + 1, lst)]]))) Invariant((r) == (psum(0, d_2_k_, lst))) + # invariants-end r = r + checkVal(lst[d_2_k_]) d_2_k_ = (d_2_k_) + (1) return r + # impl-end diff --git a/Bench/146_specialFilter.py b/Bench/146_specialFilter.py index e61a141..b7f9162 100644 --- a/Bench/146_specialFilter.py +++ b/Bench/146_specialFilter.py @@ -3,19 +3,31 @@ @Pure def first__digit(n : int) -> int : + # pre-conditions-start Requires(((0) <= (n))) + # pre-conditions-end + # post-conditions-start Ensures(0 <= Result() and (Result()) < (10)) + # post-conditions-end + + # impl-start if n < 10: return n else: return first__digit(n // 10) + # impl-end @Pure def last__digit(n : int) -> int : + # impl-start return (n % 10) + # impl-end def specialFilter(s : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(s))) Ensures(Acc(list_pred(Result()))) Ensures(Forall(int, lambda d_0_i_: @@ -25,11 +37,15 @@ def specialFilter(s : List[int]) -> List[int]: Ensures(Forall(int, lambda d_5_i_: not (((0) <= (d_5_i_)) and ((d_5_i_) < (len(Result())))) or (Exists(int, lambda x: x >= 0 and x < len(s) and s[x] == Result()[d_5_i_])))) + # post-conditions-end + + # impl-start r = list([int(0)] * 0) # type : List[int] d_3_i_ = int(0) # type : int d_3_i_ = 0 r = list([]) while (d_3_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(r))) Invariant(Acc(list_pred(s))) Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(s)))) @@ -40,7 +56,9 @@ def specialFilter(s : List[int]) -> List[int]: Invariant(Forall(int, lambda d_5_i_: not (((0) <= (d_5_i_)) and ((d_5_i_) < (len(r)))) or (Exists(int, lambda x: x >= 0 and x < len(s) and s[x] == r[d_5_i_])))) + # invariants-end if ((((s)[d_3_i_]) > (10)) and (((last__digit((s)[d_3_i_]) % 2)) == (1))) and (((first__digit((s)[d_3_i_]) % 2)) == (1)): r = (r) + [(s)[d_3_i_]] d_3_i_ = (d_3_i_) + (1) return r + # impl-end diff --git a/Bench/150-x_or_y.py b/Bench/150-x_or_y.py index 17e8d20..33447c6 100644 --- a/Bench/150-x_or_y.py +++ b/Bench/150-x_or_y.py @@ -3,20 +3,30 @@ @Pure def IsPrime(n : int) -> bool : + # impl-start return ((n) > (1)) and (Forall(int, lambda d_0_k_: not (((2) <= (d_0_k_)) and ((d_0_k_) < (n))) or (((n % d_0_k_)) != (0)))) + # impl-end def x__or__y(n : int, x : int, y : int) -> int: + # pre-conditions-start Requires(n > 1) + # pre-conditions-end + # post-conditions-start Ensures(not (IsPrime(n)) or ((Result()) == (x))) Ensures(not (not(IsPrime(n))) or ((Result()) == (y))) + # post-conditions-end + # impl-start i = 2 while i < n: + # invariants-start Invariant(2 <= i and i <= n) Invariant(Forall(int, lambda d_0_k_: not (2 <= d_0_k_ and d_0_k_ < i) or (n % d_0_k_ != 0))) + # invariants-end if n % i == 0: return y i += 1 return x + # impl-end diff --git a/Bench/151-double_the_difference.py b/Bench/151-double_the_difference.py index b4078f6..22b9698 100644 --- a/Bench/151-double_the_difference.py +++ b/Bench/151-double_the_difference.py @@ -3,35 +3,51 @@ @Pure def checkVal(x : int) -> int: + # impl-start if x > 0 and x % 2 != 0: return x * x else: return 0 + # impl-end @Pure def psum(i : int, j : int, s : List[int]) -> int : + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(0 <= i and i <= j and j <= len(s)) + # pre-conditions-end + + # impl-start if i == j: return 0 else: return checkVal((s)[j - 1]) + (psum(i, j - 1, s)) + # impl-end def double__the__difference(lst : List[int]) -> int: + # pre-conditions-start Requires(Acc(list_pred(lst))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(lst))) Ensures((Result()) == (psum(0, len(lst), lst))) + # post-conditions-end + + # impl-start r = int(0) # type : int r = 0 d_3_k_ = int(0) # type : int d_3_k_ = 0 while (d_3_k_) < (len(lst)): + # invariants-start Invariant(Acc(list_pred(lst))) Invariant(((0) <= (d_3_k_)) and ((d_3_k_) <= (len(lst)))) Invariant((r) == (psum(0, d_3_k_, lst))) Invariant(Forall(int, lambda d_3_i_: (not (((0) <= (d_3_i_)) and ((d_3_i_) < (len(lst)))) or (psum(0, d_3_i_ + 1, lst) == checkVal(lst[d_3_i_]) + psum(0, d_3_i_, lst)), [[psum(0, d_3_i_ + 1, lst)]]))) + # invariants-end r = (r) + checkVal(((lst)[d_3_k_])) d_3_k_ = (d_3_k_) + (1) - return r \ No newline at end of file + return r + # impl-end \ No newline at end of file diff --git a/Bench/152-compare.py b/Bench/152-compare.py index 2aa9219..8f675f2 100644 --- a/Bench/152-compare.py +++ b/Bench/152-compare.py @@ -2,9 +2,12 @@ from nagini_contracts.contracts import * def Compare(scores : List[int], guesses : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(guesses))) Requires(Acc(list_pred(scores))) Requires((len((scores))) == (len((guesses)))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(guesses))) Ensures(Acc(list_pred(scores))) Ensures(Acc(list_pred(Result()))) @@ -12,12 +15,16 @@ def Compare(scores : List[int], guesses : List[int]) -> List[int]: Ensures((len((scores))) == (len((guesses)))) Ensures(Forall(int, lambda d_0_i_: not (((0) <= (d_0_i_)) and ((d_0_i_) < (len((Result()))))) or (((Result())[d_0_i_]) == (abs(((scores)[d_0_i_]) - ((guesses)[d_0_i_])))))) + # post-conditions-end + + # impl-start result = [int(0)] * 0 # type : List[int] nw0_ = [int(0)] * len((scores)) # type : List[int] result = nw0_ d_1_i_ = int(0) # type : int d_1_i_ = 0 while (d_1_i_) < (len((scores))): + # invariants-start Invariant(Acc(list_pred(result))) Invariant(Acc(list_pred(guesses))) Invariant(Acc(list_pred(scores))) @@ -26,13 +33,17 @@ def Compare(scores : List[int], guesses : List[int]) -> List[int]: Invariant((len((scores))) == (len((guesses)))) Invariant(Forall(int, lambda d_2_k_: not (((0) <= (d_2_k_)) and ((d_2_k_) < (d_1_i_))) or (((result)[d_2_k_]) == (abs(((scores)[d_2_k_]) - ((guesses)[d_2_k_])))))) + # invariants-end (result)[(d_1_i_)] = abs(((scores)[d_1_i_]) - ((guesses)[d_1_i_])) d_1_i_ = (d_1_i_) + (1) return result + # impl-end @Pure def abs(x : int) -> int : + # impl-start if (x) < (0): return (0) - (x) elif True: - return x \ No newline at end of file + return x + # impl-end \ No newline at end of file diff --git a/Bench/154-cycpattern_check.py b/Bench/154-cycpattern_check.py index bdfd919..e5b9cdf 100644 --- a/Bench/154-cycpattern_check.py +++ b/Bench/154-cycpattern_check.py @@ -3,33 +3,47 @@ @Pure def IsSubstring(s : List[int], sub : List[int], n : int) -> bool : + # pre-conditions-start Requires(Acc(list_pred(s))) Requires(Acc(list_pred(sub))) + # pre-conditions-end + + # impl-start return ((len(s)) >= (len(sub))) and (Exists(int, lambda d_0_i_: (((0) <= (d_0_i_)) and ((d_0_i_) < 1 + ((len(s)) - (len(sub))))) and ( Forall(int, lambda y: (Implies(y >= 0 and y < len(sub), sub[(n + y) % len(sub)] == s[d_0_i_ + y]), [[sub[(n + y) % len(sub)] == s[d_0_i_ + y]]]))))) + # impl-end def CycpatternCheck(word : List[int], pattern : List[int]) -> bool: + # pre-conditions-start Requires(Acc(list_pred(word))) Requires(Acc(list_pred(pattern))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(word))) Ensures(Acc(list_pred(pattern))) Ensures(not (Result()) or (Exists(int, lambda d_1_i_: (((0) <= (d_1_i_)) and ((d_1_i_) <= (len(pattern)))) and (IsSubstring(word, pattern, d_1_i_))))) Ensures(not (not(Result())) or (Forall(int, lambda d_2_i_: not (((0) <= (d_2_i_)) and ((d_2_i_) <= (len(pattern)))) or (not(IsSubstring(word, pattern, d_2_i_)))))) + # post-conditions-end + + # impl-start result = False # type : bool d_3_i_ = int(0) # type : int d_3_i_ = 0 while (d_3_i_) <= (len(pattern)): + # invariants-start Invariant(Acc(list_pred(word))) Invariant(Acc(list_pred(pattern))) Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= ((len(pattern)) + (1)))) Invariant(Forall(int, lambda d_4_j_: (Implies(((0) <= (d_4_j_)) and ((d_4_j_) < (d_3_i_)), not(IsSubstring(word, pattern, d_4_j_))), [[IsSubstring(word, pattern, d_4_j_)]]))) + # invariants-end if IsSubstring(word, pattern, d_3_i_): result = True return result d_3_i_ = (d_3_i_) + (1) result = False - return result \ No newline at end of file + return result + # impl-end \ No newline at end of file diff --git a/Bench/155_even_odd_count.py b/Bench/155_even_odd_count.py index 7183f50..5f815ed 100644 --- a/Bench/155_even_odd_count.py +++ b/Bench/155_even_odd_count.py @@ -2,34 +2,53 @@ from nagini_contracts.contracts import * def even__odd__count(n : int) -> Tuple[int, int]: + # pre-conditions-start Requires((n) > (0)) + # pre-conditions-end + # post-conditions-start Ensures((Result()[0]) == (even__count(n))) Ensures((Result()[1]) == (odd__count(n))) + # post-conditions-end + + # impl-start even = int(0) # type : int odd = int(0) # type : int d_0_num_ = n # type : int while (d_0_num_) > (0): + # invariants-start Invariant(((0) <= (d_0_num_))) Invariant(n > 0) Invariant(((even) + (even__count(d_0_num_))) == (even__count(n))) Invariant(((odd) + (odd__count(d_0_num_))) == (odd__count(n))) + # invariants-end even = (even) + ((d_0_num_ % 2) == 0) odd = (odd) + (d_0_num_ % 2) d_0_num_ = (d_0_num_ // 10) return (even, odd) + # impl-end @Pure def odd__count(n : int) -> int : + # pre-conditions-start Requires(n >= 0) + # pre-conditions-end + + # impl-start if n == 0: return 0 else: return (n % 2) + odd__count(n // 10) + # impl-end @Pure def even__count(n : int) -> int : + # pre-conditions-start Requires(n >= 0) + # pre-conditions-end + + # impl-start if n == 0: return 0 else: return ((n % 2) == 0) + even__count(n // 10) + # impl-end diff --git a/Bench/157-right_angle_triangle.py b/Bench/157-right_angle_triangle.py index d0bed6f..49730b5 100644 --- a/Bench/157-right_angle_triangle.py +++ b/Bench/157-right_angle_triangle.py @@ -1,7 +1,12 @@ from nagini_contracts.contracts import * def right__angle__triangle(a : int, b : int, c : int) -> bool: + # pre-conditions-start Ensures((Result()) == ((((((a) * (a)) + ((b) * (b))) == ((c) * (c))) or ((((a) * (a)) + ((c) * (c))) == ((b) * (b)))) or ((((b) * (b)) + ((c) * (c))) == ((a) * (a))))) + # pre-conditions-end + + # impl-start result = False # type : bool result = (((((a) * (a)) + ((b) * (b))) == ((c) * (c))) or ((((a) * (a)) + ((c) * (c))) == ((b) * (b)))) or ((((b) * (b)) + ((c) * (c))) == ((a) * (a))) return result + # impl-end diff --git a/Bench/159-eat.py b/Bench/159-eat.py index a990e32..c12b8a6 100644 --- a/Bench/159-eat.py +++ b/Bench/159-eat.py @@ -2,15 +2,22 @@ from nagini_contracts.contracts import * def eat(number : int, need : int, remaining : int) -> List[int]: + # pre-conditions-start Requires((((number) >= (0)) and ((need) >= (0))) and ((remaining) >= (0))) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(Result()))) Ensures((len(Result())) == (2)) Ensures(not ((remaining) >= (need)) or ((((Result())[0]) == ((number) + (need))) and (((Result())[1]) == ((remaining) - (need))))) Ensures(not ((remaining) < (need)) or ((((Result())[0]) == ((number) + (remaining))) and (((Result())[1]) == (0)))) + # post-conditions-end + + # impl-start result = list([int(0)] * 2) # type : List[int] if (remaining) < (need): result[0] = (number) + (remaining) else: result[0] = (number) + (need) result[1] = (remaining) - (need) - return result \ No newline at end of file + return result + # impl-end \ No newline at end of file diff --git a/Bench/161-solve.py b/Bench/161-solve.py index 9fe013a..de7eea1 100644 --- a/Bench/161-solve.py +++ b/Bench/161-solve.py @@ -2,7 +2,10 @@ from nagini_contracts.contracts import * def solve(s : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(s), 1/2)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(Result()), 1/2)) Ensures(Acc(list_pred(s), 1/2)) Ensures((len(s)) == (len(Result()))) @@ -12,11 +15,15 @@ def solve(s : List[int]) -> List[int]: Ensures(Implies(Exists(int, lambda d_2_i_: (((0) <= (d_2_i_)) and ((d_2_i_) < (len(s)))) and (is__alpha((s)[d_2_i_]))), Forall(int, lambda d_3_i_: not (((0) <= (d_3_i_)) and ((d_3_i_) < (len(Result())))) or ((((Result())[d_3_i_]) == (flip__case((s)[d_3_i_])) if is__alpha((s)[d_3_i_]) else ((Result())[d_3_i_]) == ((s)[d_3_i_])))))) + # post-conditions-end + + # impl-start t : List[int] = [] d_4_flag_ = False # type : bool d_5_i_ = int(0) # type : int d_5_i_ = 0 while (d_5_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(t))) Invariant(Acc(list_pred(s), 1/2)) Invariant(((0) <= (d_5_i_)) and ((d_5_i_) <= (len(s)))) @@ -31,6 +38,7 @@ def solve(s : List[int]) -> List[int]: Implies(((0) <= (d_7_j_)) and (d_7_j_) < (d_5_i_), not(is__alpha((s)[d_7_j_])))), not(d_4_flag_))) Invariant(Forall(int, lambda d_7_j_: (Implies(((0) <= (d_7_j_)) and ((d_7_j_) < (d_5_i_)), ((t)[d_7_j_]) == ((flip__case((s)[d_7_j_]) if is__alpha((s)[d_7_j_]) else (s)[d_7_j_]))), [[]]))) + # invariants-end if is__alpha((s)[d_5_i_]): t = (t) + [flip__case((s)[d_5_i_])] d_4_flag_ = True @@ -40,37 +48,51 @@ def solve(s : List[int]) -> List[int]: if not(d_4_flag_): t = reverse(t) return t + # impl-end def reverse(str : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(str), 1/2)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(str), 1/2)) Ensures(Acc(list_pred(Result()))) Ensures(str == Old(str)) Ensures((len(Result())) == (len(str))) Ensures(Forall(int, lambda d_11_k_: not (((0) <= (d_11_k_)) and ((d_11_k_) < (len(str)))) or (((Result())[d_11_k_]) == ((str)[((len(str)) - (1)) - (d_11_k_)])))) + # post-conditions-end + + # impl-start rev = list([int(0)] * 0) # type : List[int] rev = [] d_12_i_ = int(0) # type : int d_12_i_ = 0 while (d_12_i_) < (len(str)): + # invariants-start Invariant(Acc(list_pred(str), 1/2)) Invariant(Acc(list_pred(rev))) Invariant(((d_12_i_) >= (0)) and ((d_12_i_) <= (len(str)))) Invariant((len(rev)) == (d_12_i_)) Invariant(Forall(int, lambda d_13_k_: not (((0) <= (d_13_k_)) and ((d_13_k_) < (d_12_i_))) or (((rev)[d_13_k_]) == ((str)[(len(str) - (1)) - (d_13_k_)])))) + # invariants-end rev = (rev) + [(str)[(len(str) - (d_12_i_)) - (1)]] d_12_i_ = (d_12_i_) + (1) return rev + # impl-end @Pure def is__alpha(c : int) -> bool : + # impl-start return (((97) <= (c)) and ((c) <= (122))) or (((65) <= (c)) and ((c) <= (90))) + # impl-end @Pure def flip__case(c : int) -> int : + # impl-start if ((97) <= (c)) and ((c) <= (122)): return ((c) - (97)) + (65) elif True: return ((c) - (65)) + (97) + # impl-end diff --git a/Bench/163-generate_integers.py b/Bench/163-generate_integers.py index 08f2236..a471c9d 100644 --- a/Bench/163-generate_integers.py +++ b/Bench/163-generate_integers.py @@ -3,32 +3,46 @@ @Pure def min(a : int, b : int) -> int: + # pre-conditions-start Ensures(((Result()) == (a)) or ((Result()) == (b))) Ensures(((Result()) <= (a)) and ((Result()) <= (b))) + # pre-conditions-end + + # impl-start m = int(0) # type : int if (a) < (b): m = a elif True: m = b return m + # impl-end @Pure def max(a : int, b : int) -> int: + # pre-conditions-start Ensures(((Result()) == (a)) or ((Result()) == (b))) Ensures(((Result()) >= (a)) and ((Result()) >= (b))) + # pre-conditions-end + + # impl-start m = int(0) # type : int if (a) > (b): m = a elif True: m = b return m + # impl-end def generate__integers(a : int, b : int) -> List[int]: + # post-conditions-start Ensures(Acc(list_pred(Result()))) Ensures(Forall(int, lambda d_0_i_: not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(Result())))) or ((((Result())[d_0_i_] % 2)) == (0)))) Ensures(Forall(int, lambda d_1_i_: not (((d_1_i_) >= (0)) and ((d_1_i_) < (len(Result())))) or (((Result())[d_1_i_]) > 0 and ((Result())[d_1_i_]) < 10))) + # post-conditions-end + + # impl-start result = list([int(0)] * 0) # type : List[int] d_2_left_ = int(0) # type : int d_2_left_ = min(a, b) @@ -43,6 +57,7 @@ def generate__integers(a : int, b : int) -> List[int]: d_6_i_ = d_4_lower_ while (d_6_i_) <= (d_5_upper_): + # invariants-start Invariant(Acc(list_pred(result))) Invariant(d_6_i_ >= 2) Invariant(Implies((d_6_i_) <= (d_5_upper_), d_6_i_ <= 8)) @@ -51,7 +66,9 @@ def generate__integers(a : int, b : int) -> List[int]: not (((d_7_i_) >= (0)) and ((d_7_i_) < (len(result)))) or ((((result)[d_7_i_] % 2)) == (0)))) Invariant(Forall(int, lambda d_8_j_: (not (((d_8_j_) >= (0)) and ((d_8_j_) < (len(result)))) or (((result)[d_8_j_]) > 0 and ((result)[d_8_j_]) < 10), [[result[d_8_j_]]]))) + # invariants-end if ((d_6_i_ % 2)) == (0): result = (result) + (([d_6_i_])) d_6_i_ = (d_6_i_) + (1) - return result \ No newline at end of file + return result + # impl-end \ No newline at end of file diff --git a/README.md b/README.md index 1a4e690..053a93a 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ Current status: - [x] 72. will_it_fly - [x] 73. smallest_change - [ ] 74. total_match -- [ ] 75. is_multiply_prime +- [x] 75. is_multiply_prime - [ ] 76. is_simple_power - [ ] 77. iscube - [x] 78. hex_key @@ -122,7 +122,7 @@ Current status: - [x] 116. sort_array - [ ] 117. select_words - [x] 118. get_closest_vowel -- [ ] 119. match_parens +- [x] 119. match_parens - [ ] 120. maximum - [x] 121. solution - [x] 122. add_elements @@ -135,7 +135,7 @@ Current status: - [ ] 129. minPath - [ ] 130. tri - [ ] 131. digits -- [ ] 132. is_nested +- [x] 132. is_nested - [x] 133. sum_squares - [x] 134. check_if_last_char_is_a_letter - [x] 135. can_arrange