From 738fc967fbc649640b8263f5a12e9e3349cc5b1e Mon Sep 17 00:00:00 2001 From: alex28sh Date: Thu, 22 Aug 2024 17:25:32 +0200 Subject: [PATCH 1/3] 41, 46, 126, 135, 138, 146 --- Bench/041-car_race_collision.py | 9 ++ Bench/046-fib4.py | 52 ++++++++ Bench/126-is_sorted.py | 68 ++++++++++ Bench/135_can_arrange.py | 32 +++++ Bench/138_is_equal_to_sum_even.py | 8 ++ Bench/146_specialFilter.py | 46 +++++++ {WIP => Bench}/159-eat.py | 7 +- WIP/002-truncate.py | 6 + WIP/029-filter_by_prefix.py | 40 +++--- WIP/045-triangle_area.py | 8 ++ ... 074-total_match.py => 074-total_match.py} | 24 ++-- WIP/096-count_up_to.py | 4 +- WIP/104-unique_digits.py | 116 ++++++++++-------- WIP/136-largest_smallest_integers.py | 48 ++++++++ WIP/WIP: 002-truncate.py | 6 - WIP: 154-cycpattern_check.py | 99 +++++++++++++++ public/scripts/test-all.sh | 2 +- public/scripts/test-new.sh | 2 +- 18 files changed, 481 insertions(+), 96 deletions(-) create mode 100644 Bench/041-car_race_collision.py create mode 100644 Bench/046-fib4.py create mode 100644 Bench/126-is_sorted.py create mode 100644 Bench/135_can_arrange.py create mode 100644 Bench/138_is_equal_to_sum_even.py create mode 100644 Bench/146_specialFilter.py rename {WIP => Bench}/159-eat.py (78%) create mode 100644 WIP/002-truncate.py create mode 100644 WIP/045-triangle_area.py rename WIP/{WIP: 074-total_match.py => 074-total_match.py} (67%) create mode 100644 WIP/136-largest_smallest_integers.py delete mode 100644 WIP/WIP: 002-truncate.py create mode 100644 WIP: 154-cycpattern_check.py diff --git a/Bench/041-car_race_collision.py b/Bench/041-car_race_collision.py new file mode 100644 index 0000000..25a50ad --- /dev/null +++ b/Bench/041-car_race_collision.py @@ -0,0 +1,9 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def car__race__collision(n : int) -> int: + Requires((n) >= (0)) + Ensures((Result()) == ((n) * (n))) + cnt = int(0) # type : int + cnt = (n) * (n) + return cnt diff --git a/Bench/046-fib4.py b/Bench/046-fib4.py new file mode 100644 index 0000000..50a78f5 --- /dev/null +++ b/Bench/046-fib4.py @@ -0,0 +1,52 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def fib4__rec(n : int) -> int : + Requires(n >= 0) + 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))) + +def fib4(n : int) -> int: + Requires(n >= 0) + Ensures((Result()) == (fib4__rec(n))) + result = int(0) # type : int + if (((n) == (0)) or ((n) == (1))) or ((n) == (2)): + result = 0 + return result + if (n) == (3): + result = 1 + return result + d_0_a_ = int(0) # type : int + d_1_b_ = int(0) # type : int + d_2_c_ = int(0) # type : int + d_3_d_ = int(0) # type : int + rhs0_ = 0 # type : int + rhs1_ = 0 # type : int + rhs2_ = 0 # type : int + rhs3_ = 1 # type : int + d_0_a_ = rhs0_ + d_1_b_ = rhs1_ + d_2_c_ = rhs2_ + d_3_d_ = rhs3_ + d_4_i_ = int(0) # type : int + d_4_i_ = 4 + while (d_4_i_) <= (n): + 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)))) + 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_ + d_1_b_ = d_2_c_ + d_2_c_ = d_3_d_ + d_3_d_ = d_5_temp_ + d_4_i_ = (d_4_i_) + (1) + result = d_3_d_ + return result diff --git a/Bench/126-is_sorted.py b/Bench/126-is_sorted.py new file mode 100644 index 0000000..f75cd17 --- /dev/null +++ b/Bench/126-is_sorted.py @@ -0,0 +1,68 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + + +def is__sorted(a : List[int]) -> bool: + Requires(Acc(list_pred(a))) + Ensures(Acc(list_pred(a))) + 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(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)))))) + f = False # type : bool + if (len(a)) == (0): + f = True + 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_])))))) + 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)): + 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_])))))) + if ((a)[(d_4_i_) - (1)]) > ((a)[d_4_i_]): + d_3_is__asc_ = False + d_4_i_ = (d_4_i_) + (1) + if not(d_3_is__asc_): + f = False + return f + d_4_i_ = 0 + d_7_has__no__more__that__2_ = False # type : bool + d_7_has__no__more__that__2_ = True + while (d_4_i_) < (len(a)): + Invariant(Acc(list_pred(a))) + Invariant(((0) <= (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_) < (len(a)))) or (((a)[d_5_j_]) <= ((a)[d_6_k_])))))) + Invariant((d_7_has__no__more__that__2_) == (Forall(int, lambda d_8_j_: + not (((0) <= (d_8_j_)) and ((d_8_j_) < (d_4_i_))) or ((count_set(a, (a)[d_8_j_], 0)) <= (2))) and + (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_]))))))) + d_9_count_ = int(0) # type : int + d_9_count_ = count_set(a, (a)[d_4_i_], 0) + if (d_9_count_) > (2): + d_7_has__no__more__that__2_ = False + d_4_i_ = (d_4_i_) + (1) + f = d_7_has__no__more__that__2_ + return f + +@Pure +def count_set(a : List[int], x : int, i : int) -> int: + Requires(Acc(list_pred(a), 1/2)) + Requires(((0) <= (i)) and ((i) <= (len(a)))) + if (i) == 0: + return 0 + else: + return (count_set(a, x, (i) - (1))) + (((a)[(i) - (1)]) == (x)) + diff --git a/Bench/135_can_arrange.py b/Bench/135_can_arrange.py new file mode 100644 index 0000000..e9ddffe --- /dev/null +++ b/Bench/135_can_arrange.py @@ -0,0 +1,32 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def can__arrange(arr : List[int]) -> int: + 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_]))))) + 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)]))))) + pos = int(0) # type : int + d_4_i_ = int(0) # type : int + d_4_i_ = 1 + pos = -1 + while (d_4_i_) < (len(arr)): + 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_: + not (((1) <= (d_5_j_)) and ((d_5_j_) < (d_4_i_))) or (((arr)[d_5_j_]) >= ((arr)[(d_5_j_) - (1)]))))) + 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)]))))) + if ((arr)[d_4_i_]) < ((arr)[(d_4_i_) - (1)]): + pos = d_4_i_ + d_4_i_ = (d_4_i_) + (1) + return pos + diff --git a/Bench/138_is_equal_to_sum_even.py b/Bench/138_is_equal_to_sum_even.py new file mode 100644 index 0000000..8924995 --- /dev/null +++ b/Bench/138_is_equal_to_sum_even.py @@ -0,0 +1,8 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def is__equal__to__sum__even(n : int) -> bool: + Ensures((Result()) == ((((n % 2)) == (0)) and ((n) >= (8)))) + b = False # type : bool + b = (((n % 2)) == (0)) and ((n) >= (8)) + return b diff --git a/Bench/146_specialFilter.py b/Bench/146_specialFilter.py new file mode 100644 index 0000000..e61a141 --- /dev/null +++ b/Bench/146_specialFilter.py @@ -0,0 +1,46 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def first__digit(n : int) -> int : + Requires(((0) <= (n))) + Ensures(0 <= Result() and (Result()) < (10)) + if n < 10: + return n + else: + return first__digit(n // 10) + +@Pure +def last__digit(n : int) -> int : + return (n % 10) + +def specialFilter(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_0_i_: + not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(Result())))) or (((Result())[d_0_i_]) > (10)))) + Ensures(Forall(int, lambda d_2_i_: + not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(Result())))) or ((((first__digit((Result())[d_2_i_]) % 2)) == (1)) and (((last__digit((Result())[d_2_i_]) % 2)) == (1))))) + 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_])))) + 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)): + Invariant(Acc(list_pred(r))) + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(s)))) + Invariant(Forall(int, lambda d_5_i_: + not (((0) <= (d_5_i_)) and ((d_5_i_) < (len(r)))) or (((r)[d_5_i_]) > (10)))) + Invariant(Forall(int, lambda d_7_i_: + (not (((0) <= (d_7_i_)) and ((d_7_i_) < (len(r)))) or ((((first__digit((r)[d_7_i_]) % 2)) == (1)) and (((last__digit((r)[d_7_i_]) % 2)) == (1))), [[first__digit((r)[d_7_i_])]]))) + 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_])))) + 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 diff --git a/WIP/159-eat.py b/Bench/159-eat.py similarity index 78% rename from WIP/159-eat.py rename to Bench/159-eat.py index 100333b..a990e32 100644 --- a/WIP/159-eat.py +++ b/Bench/159-eat.py @@ -7,9 +7,10 @@ def eat(number : int, need : int, remaining : int) -> List[int]: 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)))) - result = list([int(0)] * 0) # type : List[int] + result = list([int(0)] * 2) # type : List[int] if (remaining) < (need): - result = ([(number) + (remaining), 0]) + result[0] = (number) + (remaining) else: - result = ([(number) + (need), (remaining) - (need)]) + result[0] = (number) + (need) + result[1] = (remaining) - (need) return result \ No newline at end of file diff --git a/WIP/002-truncate.py b/WIP/002-truncate.py new file mode 100644 index 0000000..61431ca --- /dev/null +++ b/WIP/002-truncate.py @@ -0,0 +1,6 @@ +from nagini_contracts.contracts import * + +def truncate_number(number: float) -> float: + Requires(float("0.0") <= number) + Ensures(float("0.0") <= number - Result() and number - Result() < float("1.0")) + return number % float("1.0") \ No newline at end of file diff --git a/WIP/029-filter_by_prefix.py b/WIP/029-filter_by_prefix.py index 8d5bd68..a5ac11b 100644 --- a/WIP/029-filter_by_prefix.py +++ b/WIP/029-filter_by_prefix.py @@ -54,32 +54,32 @@ def filter__by__prefix(xs : List[List[int]], p : List[int]) -> List[int]: # (starts__with(x, p, 0), [[starts__with(x, p, 0)]]))) # Invariant(Forall(int, lambda d_2_j_: # (Implies(((0) <= (d_2_j_)) and ((d_2_j_) < (len(filtered))), starts__with(xs[(filtered)[d_2_j_]], p, 0)), [[starts__with(xs[(filtered)[d_2_j_]], p, 0)]]))) - # Invariant(Forall(int, lambda d_2_j_: - # (Implies(((0) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_)) and starts__with(xs[d_2_j_], p, 0), - # Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_2_j_)), - # [[xs[d_2_j_]]]))) - Assume(Forall(int, lambda d_2_j_: + Invariant(Forall(int, lambda d_2_j_: (Implies(((0) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_)) and starts__with(xs[d_2_j_], p, 0), Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_2_j_)), [[xs[d_2_j_]]]))) + # Assume(Forall(int, lambda d_2_j_: + # (Implies(((0) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_)) and starts__with(xs[d_2_j_], p, 0), + # Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_2_j_)), + # [[xs[d_2_j_]]]))) if starts__with((xs)[d_1_i_], p, 0): filtered = (filtered) + [d_1_i_] - Assert(starts__with(xs[(filtered)[len(filtered) - 1]], p, 0)) - Assert(d_1_i_ == filtered[len(filtered) - 1]) - Assert(Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_1_i_)) - Assert(Forall(int, lambda d_2_j_: - (Implies(((0) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_)) and starts__with(xs[d_2_j_], p, 0), - Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_2_j_)), - [[xs[d_2_j_]]]))) - Assert(Forall(int, lambda d_2_j_: - (Implies(((0) <= (d_2_j_)) and ((d_2_j_) <= (d_1_i_)) and starts__with(xs[d_2_j_], p, 0), - Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_2_j_)), - [[xs[d_2_j_]]]))) + # Assert(starts__with(xs[(filtered)[len(filtered) - 1]], p, 0)) + # Assert(d_1_i_ == filtered[len(filtered) - 1]) + # Assert(Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_1_i_)) + # Assert(Forall(int, lambda d_2_j_: + # (Implies(((0) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_)) and starts__with(xs[d_2_j_], p, 0), + # Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_2_j_)), + # [[xs[d_2_j_]]]))) + # Assert(Forall(int, lambda d_2_j_: + # (Implies(((0) <= (d_2_j_)) and ((d_2_j_) <= (d_1_i_)) and starts__with(xs[d_2_j_], p, 0), + # Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_2_j_)), + # [[xs[d_2_j_]]]))) d_1_i_ = (d_1_i_) + (1) - Assert(Forall(int, lambda d_2_j_: - (Implies(((0) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_)) and starts__with(xs[d_2_j_], p, 0), - Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_2_j_)), - [[xs[d_2_j_]]]))) + # Assert(Forall(int, lambda d_2_j_: + # (Implies(((0) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_)) and starts__with(xs[d_2_j_], p, 0), + # Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_2_j_)), + # [[xs[d_2_j_]]]))) # Assert(Implies(((0) <= (d_1_i_)) and ((d_1_i_) < (d_1_i_)) and starts__with(xs[d_1_i_], p, 0), # Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_1_i_))) return filtered \ No newline at end of file diff --git a/WIP/045-triangle_area.py b/WIP/045-triangle_area.py new file mode 100644 index 0000000..f45b66e --- /dev/null +++ b/WIP/045-triangle_area.py @@ -0,0 +1,8 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def CalculateTriangleArea(a : float, h : float) -> float: + Requires(((h) >= float("0.0")) and ((a) >= float("0.0"))) + Ensures((Result()) == (((h) * (a)) / (float("2.0")))) + area = ((h) * (a)) / float("2.0") # type : float + return area diff --git a/WIP/WIP: 074-total_match.py b/WIP/074-total_match.py similarity index 67% rename from WIP/WIP: 074-total_match.py rename to WIP/074-total_match.py index 9a1b770..430b7f0 100644 --- a/WIP/WIP: 074-total_match.py +++ b/WIP/074-total_match.py @@ -33,11 +33,9 @@ @Pure def sum__chars__rec(i : int, j : int, lst : List[List[int]]) -> int : - Requires(Acc(list_pred(lst))) - Requires(Forall(lst, lambda x: Acc(list_pred(x)))) - # Requires(Forall(int, lambda x: (Implies(x >= 0 and x < len(lst), Acc(list_pred(lst[x]))), [[lst[x]]]))) + Requires(Acc(list_pred(lst), 1/2)) + Requires(Forall(lst, lambda x: Acc(list_pred(x), 1/2))) Requires(((0) <= (i)) and ((i) <= (j)) and ((j) <= (len(lst)))) - Requires(Implies(j > 0, Acc(list_pred(lst[j - 1])))) if i == j: return 0 else: @@ -46,25 +44,21 @@ def sum__chars__rec(i : int, j : int, lst : List[List[int]]) -> int : def SumChars(lst : List[List[int]]) -> int: Requires(Acc(list_pred(lst))) Requires(Forall(lst, lambda x: Acc(list_pred(x)))) - # Requires(Forall(int, lambda x: (Implies(x >= 0 and x < len(lst), Acc(list_pred(lst[x]))), [[lst[x]]]))) Ensures(Acc(list_pred(lst))) Ensures(Forall(lst, lambda x: Acc(list_pred(x)))) - # Ensures(Forall(int, lambda x: (Implies(x >= 0 and x < len(lst), Acc(list_pred(lst[x]))), [[lst[x]]]))) - Ensures(Implies(len(lst) > 0, Acc(list_pred(lst[len(lst) - 1])))) - Ensures((Result()) == (sum__chars__rec(0, len(lst), lst))) + # Ensures((Result()) == (sum__chars__rec(0, len(lst), lst))) sum = int(0) # type : int sum = 0 d_3_i_ = int(0) # type : int d_3_i_ = 0 while (d_3_i_) < (len(lst)): - Invariant(Acc(list_pred(lst))) - # Invariant(Forall(int, lambda x: (Implies(x >= 0 and x < len(lst), Acc(list_pred(lst[x]))), [[lst[x]]]))) - Invariant(Forall(lst, lambda x: Acc(list_pred(x)))) + 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(Implies(d_3_i_ > 0, Acc(list_pred(lst[d_3_i_ - 1])))) - Invariant((sum) == (sum__chars__rec(0, d_3_i_, lst))) - Invariant(Forall(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len(lst))), Acc(list_pred(lst[d_0_i_])) and (Implies(d_0_i_ > 0, Acc(list_pred(lst[d_0_i_ - 1])))) and sum__chars__rec(0, d_0_i_ + 1, lst) == sum__chars__rec(0, d_0_i_, lst) + len(lst[d_0_i_])), [[sum__chars__rec(0, d_0_i_ + 1, lst)]]))) - Assert(sum__chars__rec(0, d_3_i_ + 1, lst) == sum__chars__rec(0, d_3_i_, lst) + len(lst[d_3_i_])) + 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) + len(lst[d_0_i_]))), + [[sum__chars__rec(0, d_0_i_ + 1, lst) == (sum__chars__rec(0, d_0_i_, lst) + len(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_])) sum = (sum) + (len((lst)[d_3_i_])) d_3_i_ = (d_3_i_) + (1) # Assert((list) == (list((list)[:len(list):]))) diff --git a/WIP/096-count_up_to.py b/WIP/096-count_up_to.py index 14623f5..e011694 100644 --- a/WIP/096-count_up_to.py +++ b/WIP/096-count_up_to.py @@ -34,8 +34,8 @@ def CountUpTo(n : int) -> List[int]: Invariant(Forall(int, lambda d_3_p_: Implies((((2) <= (d_3_p_)) and ((d_3_p_) < (d_4_i_))) and IsPrime(d_3_p_), Exists(int, lambda x: x >= 0 and x < len(primes) and primes[x] == d_3_p_)))) - # Invariant(Forall(int, lambda d_6_j_: - # (Implies(((0) <= (d_6_j_)) and ((d_6_j_) < (len(primes))), ((2) <= ((primes)[d_6_j_])) and (((primes)[d_6_j_]) < (d_4_i_)))))) + Invariant(Forall(int, lambda d_6_j_: + (Implies(((0) <= (d_6_j_)) and ((d_6_j_) < (len(primes))), ((2) <= ((primes)[d_6_j_])) and (((primes)[d_6_j_]) < (d_4_i_))), [[primes[d_6_j_]]]))) # Invariant(Forall(int, lambda d_7_p_: # (((((2) <= (d_7_p_)) and ((d_7_p_) < (d_4_i_))) and (IsPrime(d_7_p_))) == ((d_7_p_) in (primes)), [[IsPrime(d_7_p_)]]))) # Invariant(Forall(int, lambda d_8_j_: diff --git a/WIP/104-unique_digits.py b/WIP/104-unique_digits.py index fd03258..ee80290 100644 --- a/WIP/104-unique_digits.py +++ b/WIP/104-unique_digits.py @@ -11,6 +11,7 @@ def UniqueDigits(x : List[int]) -> List[int]: Requires(Forall(int, lambda d_0_i_: Implies(d_0_i_ >= 0 and d_0_i_ < len(x), (x[d_0_i_] >= 0)))) Ensures(Acc(list_pred(x), 1/2)) Ensures(Acc(list_pred(Result()))) + Ensures(len(Result()) <= len(x)) Ensures(Forall(int, lambda d_0_i_: Implies(d_0_i_ >= 0 and d_0_i_ < len(x), (x[d_0_i_] >= 0)))) Ensures(Forall(int, lambda d_0_i_: Implies(d_0_i_ >= 0 and d_0_i_ < len(Result()), (Result()[d_0_i_] >= 0)))) Ensures(Forall(int, lambda d_0_i_: @@ -19,11 +20,11 @@ def UniqueDigits(x : List[int]) -> List[int]: not (((d_8_e_) >= 0 and d_8_e_ < len(x)) and (HasNoEvenDigit(x[d_8_e_]))) or (Exists(int, lambda d_9_j_: (d_9_j_ >= 0 and d_9_j_ < len(Result())) and Result()[d_9_j_] == x[d_8_e_])))) - Ensures(Forall(int, lambda d_7_e_: - not ((d_7_e_) >= 0 and d_7_e_ < len(Result())) or (Exists(int, lambda d_8_j_: (d_8_j_ >= 0 and d_8_j_ < len(x)) and x[d_8_j_] == Result()[d_7_e_])))) - 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_]))))) + # Ensures(Forall(int, lambda d_7_e_: + # not ((d_7_e_) >= 0 and d_7_e_ < len(Result())) or (Exists(int, lambda d_8_j_: (d_8_j_ >= 0 and d_8_j_ < len(x)) and x[d_8_j_] == Result()[d_7_e_])))) + # 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_]))))) result = list([int(0)] * 0) # type : List[int] result = list([]) d_5_i_ = 0 @@ -32,6 +33,7 @@ def UniqueDigits(x : List[int]) -> List[int]: Invariant(Acc(list_pred(result))) Invariant(Acc(list_pred(x), 1/2)) Invariant(0 <= d_5_i_ and d_5_i_ <= len(x)) + Invariant(len(result) <= d_5_i_) Invariant(Forall(int, lambda d_0_i_: Implies(d_0_i_ >= 0 and d_0_i_ < len(x), (x[d_0_i_] >= 0)))) Invariant(Forall(int, lambda d_0_i_: Implies(d_0_i_ >= 0 and d_0_i_ < len(result), (result[d_0_i_] >= 0)))) Invariant(Forall(int, lambda d_6_j_: @@ -40,50 +42,68 @@ def UniqueDigits(x : List[int]) -> List[int]: (Implies(((d_8_e_) >= 0 and d_8_e_ < d_5_i_) and (HasNoEvenDigit(x[d_8_e_])), Exists(int, lambda d_9_j_: (d_9_j_ >= 0 and d_9_j_ < len(result)) and result[d_9_j_] == x[d_8_e_])), - [[(HasNoEvenDigit(x[d_8_e_])),]]))) - Invariant(Forall(int, lambda d_7_e_: - (Implies((d_7_e_) >= 0 and d_7_e_ < len(result), - Exists(int, lambda d_8_j_: (d_8_j_ >= 0 and d_8_j_ < d_5_i_) and x[d_8_j_] == result[d_7_e_]))))) + [[(HasNoEvenDigit(x[d_8_e_]))]]))) + # Invariant(Forall(int, lambda d_7_e_: + # (Implies((d_7_e_) >= 0 and d_7_e_ < len(result), + # Exists(int, lambda d_8_j_: (d_8_j_ >= 0 and d_8_j_ < d_5_i_) and x[d_8_j_] == result[d_7_e_]))))) 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)): - # Invariant(Acc(list_pred(result))) - # Invariant(((0) <= (d_9_i_)) and ((d_9_i_) <= (len(result)))) - # Invariant(Forall(int, lambda d_0_i_: Implies(d_0_i_ >= 0 and d_0_i_ < len(result), (result[d_0_i_] >= 0)))) - # # Invariant(Forall(int, lambda d_10_j_: - # # Forall(int, lambda d_11_k_: - # # not ((((0) <= (d_10_j_)) and ((d_10_j_) < (d_11_k_))) and ((d_11_k_) < (d_9_i_))) or (((result)[d_10_j_]) <= ((result)[d_11_k_]))))) - # # Invariant(Forall(int, lambda d_13_j_: - # # not (((0) <= (d_13_j_)) and ((d_13_j_) < (d_9_i_))) or (Forall(int, lambda d_14_k_: - # # not (((d_9_i_) <= (d_14_k_)) and ((d_14_k_) < (len(result)))) or (((result)[d_13_j_]) <= ((result)[d_14_k_])))))) - # # Invariant(Forall(int, lambda d_6_j_: - # # (Implies(((0) <= (d_6_j_)) and ((d_6_j_) < (len(result))), HasNoEvenDigit((result)[d_6_j_])), [[HasNoEvenDigit((result)[d_6_j_])]]))) - # # Invariant(Forall(int, lambda d_8_e_: - # # Implies(((d_8_e_) >= 0 and d_8_e_ < d_5_i_) and (HasNoEvenDigit(x[d_8_e_])), Exists(int, lambda d_9_j_: (d_9_j_ >= 0 and d_9_j_ < len(result)) and result[d_9_j_] == x[d_8_e_])))) - # # Invariant(Forall(int, lambda d_7_e_: - # # (Implies((d_7_e_) >= 0 and d_7_e_ < len(result), - # # Exists(int, lambda d_8_j_: (d_8_j_ >= 0 and d_8_j_ < d_5_i_) and x[d_8_j_] == result[d_7_e_])), [[Exists(int, lambda d_8_j_: (d_8_j_ >= 0 and d_8_j_ < d_5_i_) and x[d_8_j_] == result[d_7_e_])]]))) - # 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)): - # Invariant(Acc(list_pred(result))) - # Invariant(((0) <= (d_9_i_)) and ((d_9_i_) < (len(result)))) - # Invariant(Forall(int, lambda d_0_i_: Implies(d_0_i_ >= 0 and d_0_i_ < len(result), (result[d_0_i_] >= 0)))) - # Invariant((((d_9_i_) <= (d_17_minIndex_)) and ((d_17_minIndex_) < (d_18_j_))) and ((d_18_j_) <= (len(result)))) - # Invariant(Forall(int, lambda d_19_k_: - # not (((d_9_i_) <= (d_19_k_)) and ((d_19_k_) < (d_18_j_))) or (((result)[d_17_minIndex_]) <= ((result)[d_19_k_])))) - # 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_] - # result[d_9_i_] = (result)[d_17_minIndex_] - # result[d_17_minIndex_] = d_20_temp_ - # d_9_i_ = (d_9_i_) + (1) + d_9_i_ = int(0) # type : int + d_9_i_ = 0 + while (d_9_i_) < (len(result)): + Invariant(Acc(list_pred(result))) + Invariant(Acc(list_pred(x), 1/2)) + Invariant(len(result) <= len(x)) + Invariant(((0) <= (d_9_i_)) and ((d_9_i_) <= (len(result)))) + Invariant(Forall(int, lambda d_0_i_: Implies(d_0_i_ >= 0 and d_0_i_ < len(x), (x[d_0_i_] >= 0)))) + Invariant(Forall(int, lambda d_0_i_: Implies(d_0_i_ >= 0 and d_0_i_ < len(result), (result[d_0_i_] >= 0)))) + Invariant(Forall(int, lambda d_6_j_: + (Implies(((0) <= (d_6_j_)) and ((d_6_j_) < (len(result))), HasNoEvenDigit((result)[d_6_j_])), [[HasNoEvenDigit((result)[d_6_j_])]]))) + # Invariant(Forall(int, lambda d_8_e_: + # (Implies(((d_8_e_) >= 0 and d_8_e_ < len(x)) and (HasNoEvenDigit(x[d_8_e_])), + # Exists(int, lambda d_9_j_: + # (d_9_j_ >= 0 and d_9_j_ < len(result)) and result[d_9_j_] == x[d_8_e_])), + # [[(HasNoEvenDigit(x[d_8_e_]))]]))) + # Invariant(Forall(int, lambda d_10_j_: + # Forall(int, lambda d_11_k_: + # not ((((0) <= (d_10_j_)) and ((d_10_j_) < (d_11_k_))) and ((d_11_k_) < (d_9_i_))) or (((result)[d_10_j_]) <= ((result)[d_11_k_]))))) + # Invariant(Forall(int, lambda d_13_j_: + # not (((0) <= (d_13_j_)) and ((d_13_j_) < (d_9_i_))) or (Forall(int, lambda d_14_k_: + # not (((d_9_i_) <= (d_14_k_)) and ((d_14_k_) < (len(result)))) or (((result)[d_13_j_]) <= ((result)[d_14_k_])))))) + # Invariant(Forall(int, lambda d_8_e_: + # Implies(((d_8_e_) >= 0 and d_8_e_ < d_5_i_) and (HasNoEvenDigit(x[d_8_e_])), Exists(int, lambda d_9_j_: (d_9_j_ >= 0 and d_9_j_ < len(result)) and result[d_9_j_] == x[d_8_e_])))) + # Invariant(Forall(int, lambda d_7_e_: + # (Implies((d_7_e_) >= 0 and d_7_e_ < len(result), + # Exists(int, lambda d_8_j_: (d_8_j_ >= 0 and d_8_j_ < d_5_i_) and x[d_8_j_] == result[d_7_e_])), [[Exists(int, lambda d_8_j_: (d_8_j_ >= 0 and d_8_j_ < d_5_i_) and x[d_8_j_] == result[d_7_e_])]]))) + 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)): + Invariant(Acc(list_pred(result))) + Invariant(Acc(list_pred(x), 1/2)) + Invariant(len(result) <= len(x)) + Invariant(((0) <= (d_9_i_)) and ((d_9_i_) < (len(result)))) + Invariant(Forall(int, lambda d_0_i_: Implies(d_0_i_ >= 0 and d_0_i_ < len(x), (x[d_0_i_] >= 0)))) + Invariant(Forall(int, lambda d_0_i_: Implies(d_0_i_ >= 0 and d_0_i_ < len(result), (result[d_0_i_] >= 0)))) + Invariant((((d_9_i_) <= (d_17_minIndex_)) and ((d_17_minIndex_) < (d_18_j_))) and ((d_18_j_) <= (len(result)))) + Invariant(Forall(int, lambda d_19_k_: + not (((d_9_i_) <= (d_19_k_)) and ((d_19_k_) < (d_18_j_))) or (((result)[d_17_minIndex_]) <= ((result)[d_19_k_])))) + Invariant(Forall(int, lambda d_6_j_: + (Implies(((0) <= (d_6_j_)) and ((d_6_j_) < (len(result))), HasNoEvenDigit((result)[d_6_j_])), [[HasNoEvenDigit((result)[d_6_j_])]]))) + # Invariant(Forall(int, lambda d_8_e_: + # (Implies(((d_8_e_) >= 0 and d_8_e_ < len(x)) and (HasNoEvenDigit(x[d_8_e_])), + # Exists(int, lambda d_9_j_: + # (d_9_j_ >= 0 and d_9_j_ < len(result)) and result[d_9_j_] == x[d_8_e_])), + # [[(HasNoEvenDigit(x[d_8_e_]))]]))) + 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_] + result[d_9_i_] = (result)[d_17_minIndex_] + result[d_17_minIndex_] = d_20_temp_ + d_9_i_ = (d_9_i_) + (1) return result diff --git a/WIP/136-largest_smallest_integers.py b/WIP/136-largest_smallest_integers.py new file mode 100644 index 0000000..c4705a9 --- /dev/null +++ b/WIP/136-largest_smallest_integers.py @@ -0,0 +1,48 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def getVal(mx: Optional[int]) -> int: + Requires(mx is not None) + return mx + +def largest__smallest__integers(arr : List[int]) -> Tuple[Optional[int], Optional[int]]: + Requires(Acc(list_pred(arr))) + 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))))) + Ensures(not ((Result()[0]) is not None) or ((((Result()[0])) in (arr)) and (((Result()[0])) < (0)))) + # Ensures(not ((Result()[0]) is not None) or (Forall(int, lambda d_1_i_: + # not ((((0) <= (d_1_i_)) and ((d_1_i_) < (len(arr)))) and (((arr)[d_1_i_]) < (0))) or (((arr)[d_1_i_]) <= (getVal(Result()[0])))))) + # Ensures(not ((Result()[1]) is None) or (Forall(int, lambda d_2_i_: + # not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(arr)))) or (((arr)[d_2_i_]) <= (0))))) + # 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])))))) + # a = int(0) # type : Optional[int] + # b = int(0) # type : Optional[int] + a = None # type : Optional[int] + # b = None # type : Optional[int] + d_4_i_ = int(0) # type : int + d_4_i_ = 0 + while (d_4_i_) < (len(arr)): + Invariant(Acc(list_pred(arr))) + # Invariant(a is None or isinstance(a, int)) + Invariant(((0) <= (d_4_i_)) and ((d_4_i_) <= (len(arr)))) + Invariant(not ((a) is None) or (Forall(int, lambda d_5_j_: + not (((0) <= (d_5_j_)) and ((d_5_j_) < (d_4_i_))) or (((arr)[d_5_j_]) >= (0))))) + Invariant(Old(a) is None or (Old(a) is not None and Old(a) <= a)) + Invariant(not ((a) is not None) or ((Exists(int, lambda x: x >= 0 and x < d_4_i_ and arr[x] == a)) and ((a) < (0)))) + # Invariant(not ((a) is not None) or (Forall(int, lambda d_6_j_: + # not ((((0) <= (d_6_j_)) and ((d_6_j_) < (d_4_i_))) and (((arr)[d_6_j_]) < (0))) or (((arr)[d_6_j_]) <= (getVal(a)))))) + # Invariant(not ((b) is None) or (Forall(int, lambda d_7_j_: + # not (((0) <= (d_7_j_)) and ((d_7_j_) < (d_4_i_))) or (((arr)[d_7_j_]) <= (0))))) + # 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)))))) + 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, a) diff --git a/WIP/WIP: 002-truncate.py b/WIP/WIP: 002-truncate.py deleted file mode 100644 index 4cf61d5..0000000 --- a/WIP/WIP: 002-truncate.py +++ /dev/null @@ -1,6 +0,0 @@ -from nagini_contracts.contracts import * - -def truncate_number(number: float) -> float: - Requires(0 <= number) - Ensures(0 <= number - Result() and number - Result() < 1.0) - return number % 1.0 \ No newline at end of file diff --git a/WIP: 154-cycpattern_check.py b/WIP: 154-cycpattern_check.py new file mode 100644 index 0000000..8387dc6 --- /dev/null +++ b/WIP: 154-cycpattern_check.py @@ -0,0 +1,99 @@ +from typing import List +from nagini_contracts.contracts import * + +@Pure +def IsSubstring(s : List[int], sub : List[int], n : int) -> bool : + Requires(Acc(list_pred(s))) + Requires(Acc(list_pred(sub))) + 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]]]))))) + +# def IsSubstringFunc(s : List[int], sub : List[int], n : int) -> bool : +# Requires(Acc(list_pred(s))) +# Requires(Acc(list_pred(sub))) +# Ensures(Acc(list_pred(s))) +# Ensures(Acc(list_pred(sub))) +# Ensures(Implies(Result(), len(s) >= len(sub))) +# # Ensures(Result() == (((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]]]))))))) + +# if (len(s) < len(sub)): +# return False + +# i = 0 # type : int +# while i <= len(s) - len(sub): +# Invariant(Acc(list_pred(s), 1/2)) +# Invariant(Acc(list_pred(sub), 1/2)) +# Invariant(((0) <= (i)) and ((i) <= 1 + ((len(s)) - (len(sub))))) +# Invariant(len(s) >= len(sub)) +# # Invariant(Forall(int, lambda d_0_i: +# # (Implies(d_0_i >= 0 and d_0_i < i, +# # Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])), +# # [[Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])]]))) + +# Assume(Forall(int, lambda d_0_i: +# (Implies(d_0_i >= 0 and d_0_i < i, +# Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])), +# [[Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])]]))) +# j = 0 # type : int +# fl = True # type : bool +# Assert(Forall(int, lambda d_0_i: +# (Implies(d_0_i >= 0 and d_0_i < i, +# Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])), +# [[Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])]]))) +# while j < len(sub): +# Invariant(Acc(list_pred(s), 1/2)) +# Invariant(Acc(list_pred(sub), 1/2)) +# Invariant(((0) <= (j)) and ((j) <= (len(sub)))) +# Invariant(((0) <= (i)) and ((i) <= ((len(s)) - (len(sub))))) +# Invariant(len(s) >= len(sub)) +# Invariant(fl) +# Invariant(Forall(int, lambda d_0_i: +# (Implies(d_0_i >= 0 and d_0_i < i, +# Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])), +# [[Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])]]))) +# Invariant(Implies(fl, Forall(int, lambda y: (Implies(y >= 0 and y < j, sub[(n + y) % len(sub)] == s[i + y]), [[sub[(n + y) % len(sub)] == s[i + y]]])))) +# if sub[(n + j) % len(sub)] != s[i + j]: +# fl = False +# Assert(fl == (j == len(sub))) +# break +# j = j + 1 + +# Assert(fl == (j == len(sub))) +# if j == len(sub): +# Assert(IsSubstring(s, sub, n)) +# return True +# Assert(Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[i + y])) +# Assert(Forall(int, lambda d_0_i: +# (Implies(d_0_i >= 0 and d_0_i <= i, +# Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])), +# [[Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])]]))) +# i = i + 1 +# return False + +# def CycpatternCheck(word : List[int], pattern : List[int]) -> bool: +# Requires(Acc(list_pred(word))) +# Requires(Acc(list_pred(pattern))) +# 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_)))))) +# result = False # type : bool +# d_3_i_ = int(0) # type : int +# d_3_i_ = 0 +# while (d_3_i_) <= (len(pattern)): +# 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_)]]))) +# 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 diff --git a/public/scripts/test-all.sh b/public/scripts/test-all.sh index ef0b05b..bc8a877 100755 --- a/public/scripts/test-all.sh +++ b/public/scripts/test-all.sh @@ -5,7 +5,7 @@ set -eou pipefail DIRECTORY="./Bench" # You can change this to your specific directory # Timeout duration in seconds -TIMEOUT_DURATION=600 +TIMEOUT_DURATION=1200 echo "Running Nagini on Python files in $DIRECTORY" diff --git a/public/scripts/test-new.sh b/public/scripts/test-new.sh index d20c8c2..50a00b6 100755 --- a/public/scripts/test-new.sh +++ b/public/scripts/test-new.sh @@ -4,7 +4,7 @@ set -eou pipefail DIRECTORY="Bench" # You can change this to your specific directory # Timeout duration in seconds -TIMEOUT_DURATION=600 +TIMEOUT_DURATION=1200 file_count=0 file_no=0 From 6c2a3e095de0a98798a0dea3bc84875b753820e6 Mon Sep 17 00:00:00 2001 From: alex28sh Date: Thu, 22 Aug 2024 17:38:50 +0200 Subject: [PATCH 2/3] fix: enumerating all files? --- WIP/WIP: 154-cycpattern_check.py | 99 ++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/WIP/WIP: 154-cycpattern_check.py b/WIP/WIP: 154-cycpattern_check.py index e69de29..8387dc6 100644 --- a/WIP/WIP: 154-cycpattern_check.py +++ b/WIP/WIP: 154-cycpattern_check.py @@ -0,0 +1,99 @@ +from typing import List +from nagini_contracts.contracts import * + +@Pure +def IsSubstring(s : List[int], sub : List[int], n : int) -> bool : + Requires(Acc(list_pred(s))) + Requires(Acc(list_pred(sub))) + 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]]]))))) + +# def IsSubstringFunc(s : List[int], sub : List[int], n : int) -> bool : +# Requires(Acc(list_pred(s))) +# Requires(Acc(list_pred(sub))) +# Ensures(Acc(list_pred(s))) +# Ensures(Acc(list_pred(sub))) +# Ensures(Implies(Result(), len(s) >= len(sub))) +# # Ensures(Result() == (((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]]]))))))) + +# if (len(s) < len(sub)): +# return False + +# i = 0 # type : int +# while i <= len(s) - len(sub): +# Invariant(Acc(list_pred(s), 1/2)) +# Invariant(Acc(list_pred(sub), 1/2)) +# Invariant(((0) <= (i)) and ((i) <= 1 + ((len(s)) - (len(sub))))) +# Invariant(len(s) >= len(sub)) +# # Invariant(Forall(int, lambda d_0_i: +# # (Implies(d_0_i >= 0 and d_0_i < i, +# # Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])), +# # [[Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])]]))) + +# Assume(Forall(int, lambda d_0_i: +# (Implies(d_0_i >= 0 and d_0_i < i, +# Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])), +# [[Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])]]))) +# j = 0 # type : int +# fl = True # type : bool +# Assert(Forall(int, lambda d_0_i: +# (Implies(d_0_i >= 0 and d_0_i < i, +# Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])), +# [[Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])]]))) +# while j < len(sub): +# Invariant(Acc(list_pred(s), 1/2)) +# Invariant(Acc(list_pred(sub), 1/2)) +# Invariant(((0) <= (j)) and ((j) <= (len(sub)))) +# Invariant(((0) <= (i)) and ((i) <= ((len(s)) - (len(sub))))) +# Invariant(len(s) >= len(sub)) +# Invariant(fl) +# Invariant(Forall(int, lambda d_0_i: +# (Implies(d_0_i >= 0 and d_0_i < i, +# Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])), +# [[Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])]]))) +# Invariant(Implies(fl, Forall(int, lambda y: (Implies(y >= 0 and y < j, sub[(n + y) % len(sub)] == s[i + y]), [[sub[(n + y) % len(sub)] == s[i + y]]])))) +# if sub[(n + j) % len(sub)] != s[i + j]: +# fl = False +# Assert(fl == (j == len(sub))) +# break +# j = j + 1 + +# Assert(fl == (j == len(sub))) +# if j == len(sub): +# Assert(IsSubstring(s, sub, n)) +# return True +# Assert(Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[i + y])) +# Assert(Forall(int, lambda d_0_i: +# (Implies(d_0_i >= 0 and d_0_i <= i, +# Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])), +# [[Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])]]))) +# i = i + 1 +# return False + +# def CycpatternCheck(word : List[int], pattern : List[int]) -> bool: +# Requires(Acc(list_pred(word))) +# Requires(Acc(list_pred(pattern))) +# 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_)))))) +# result = False # type : bool +# d_3_i_ = int(0) # type : int +# d_3_i_ = 0 +# while (d_3_i_) <= (len(pattern)): +# 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_)]]))) +# 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 From 0559cf68016f624cfe38f40cb4a7b213943d81ad Mon Sep 17 00:00:00 2001 From: alex28sh Date: Thu, 22 Aug 2024 17:42:06 +0200 Subject: [PATCH 3/3] fix: enumerating all files? --- .github/workflows/test-new.yml | 3 +- WIP: 154-cycpattern_check.py | 99 ---------------------------------- 2 files changed, 2 insertions(+), 100 deletions(-) delete mode 100644 WIP: 154-cycpattern_check.py diff --git a/.github/workflows/test-new.yml b/.github/workflows/test-new.yml index 6e126bb..35cc851 100644 --- a/.github/workflows/test-new.yml +++ b/.github/workflows/test-new.yml @@ -37,6 +37,7 @@ jobs: ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} run: | chmod +x ./public/scripts/test-new.sh - ./public/scripts/test-new.sh $ALL_CHANGED_FILES + echo "${ALL_CHANGED_FILES[@]}" + ./public/scripts/test-new.sh "${ALL_CHANGED_FILES[@]}" shell: bash # nagini 000-has-close-elements.py \ No newline at end of file diff --git a/WIP: 154-cycpattern_check.py b/WIP: 154-cycpattern_check.py deleted file mode 100644 index 8387dc6..0000000 --- a/WIP: 154-cycpattern_check.py +++ /dev/null @@ -1,99 +0,0 @@ -from typing import List -from nagini_contracts.contracts import * - -@Pure -def IsSubstring(s : List[int], sub : List[int], n : int) -> bool : - Requires(Acc(list_pred(s))) - Requires(Acc(list_pred(sub))) - 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]]]))))) - -# def IsSubstringFunc(s : List[int], sub : List[int], n : int) -> bool : -# Requires(Acc(list_pred(s))) -# Requires(Acc(list_pred(sub))) -# Ensures(Acc(list_pred(s))) -# Ensures(Acc(list_pred(sub))) -# Ensures(Implies(Result(), len(s) >= len(sub))) -# # Ensures(Result() == (((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]]]))))))) - -# if (len(s) < len(sub)): -# return False - -# i = 0 # type : int -# while i <= len(s) - len(sub): -# Invariant(Acc(list_pred(s), 1/2)) -# Invariant(Acc(list_pred(sub), 1/2)) -# Invariant(((0) <= (i)) and ((i) <= 1 + ((len(s)) - (len(sub))))) -# Invariant(len(s) >= len(sub)) -# # Invariant(Forall(int, lambda d_0_i: -# # (Implies(d_0_i >= 0 and d_0_i < i, -# # Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])), -# # [[Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])]]))) - -# Assume(Forall(int, lambda d_0_i: -# (Implies(d_0_i >= 0 and d_0_i < i, -# Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])), -# [[Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])]]))) -# j = 0 # type : int -# fl = True # type : bool -# Assert(Forall(int, lambda d_0_i: -# (Implies(d_0_i >= 0 and d_0_i < i, -# Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])), -# [[Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])]]))) -# while j < len(sub): -# Invariant(Acc(list_pred(s), 1/2)) -# Invariant(Acc(list_pred(sub), 1/2)) -# Invariant(((0) <= (j)) and ((j) <= (len(sub)))) -# Invariant(((0) <= (i)) and ((i) <= ((len(s)) - (len(sub))))) -# Invariant(len(s) >= len(sub)) -# Invariant(fl) -# Invariant(Forall(int, lambda d_0_i: -# (Implies(d_0_i >= 0 and d_0_i < i, -# Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])), -# [[Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])]]))) -# Invariant(Implies(fl, Forall(int, lambda y: (Implies(y >= 0 and y < j, sub[(n + y) % len(sub)] == s[i + y]), [[sub[(n + y) % len(sub)] == s[i + y]]])))) -# if sub[(n + j) % len(sub)] != s[i + j]: -# fl = False -# Assert(fl == (j == len(sub))) -# break -# j = j + 1 - -# Assert(fl == (j == len(sub))) -# if j == len(sub): -# Assert(IsSubstring(s, sub, n)) -# return True -# Assert(Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[i + y])) -# Assert(Forall(int, lambda d_0_i: -# (Implies(d_0_i >= 0 and d_0_i <= i, -# Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])), -# [[Exists(int, lambda y: y >= 0 and y < len(sub) and sub[(n + y) % len(sub)] != s[d_0_i + y])]]))) -# i = i + 1 -# return False - -# def CycpatternCheck(word : List[int], pattern : List[int]) -> bool: -# Requires(Acc(list_pred(word))) -# Requires(Acc(list_pred(pattern))) -# 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_)))))) -# result = False # type : bool -# d_3_i_ = int(0) # type : int -# d_3_i_ = 0 -# while (d_3_i_) <= (len(pattern)): -# 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_)]]))) -# 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