From c94032625ea9825f4f91843bf21af405612da2b1 Mon Sep 17 00:00:00 2001 From: alex28sh Date: Thu, 29 Aug 2024 08:54:01 +0200 Subject: [PATCH 1/6] 93, 112 --- Bench/093-encode.py | 4 +- Bench/112-reverse_delete.py | 83 ++++++++++++++++++++++++++++++++++ WIP/006-parse_nested_parens.py | 50 ++++++-------------- 3 files changed, 98 insertions(+), 39 deletions(-) create mode 100644 Bench/112-reverse_delete.py diff --git a/Bench/093-encode.py b/Bench/093-encode.py index c4ba40f..61f1fb8 100644 --- a/Bench/093-encode.py +++ b/Bench/093-encode.py @@ -21,9 +21,9 @@ def encode(s : List[int]) -> List[int]: Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(s)))) Invariant((len(t)) == (d_3_i_)) Invariant(Forall(int, lambda d_4_j_: - 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_])))))) + (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_]))))) + (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_])]]))) if is__vowel((s)[d_3_i_]): t = (t) + [rot2(swap__case((s)[d_3_i_]))] else: diff --git a/Bench/112-reverse_delete.py b/Bench/112-reverse_delete.py new file mode 100644 index 0000000..f5add94 --- /dev/null +++ b/Bench/112-reverse_delete.py @@ -0,0 +1,83 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def InArray(a : List[int], x : int) -> bool : + Requires(Acc(list_pred(a))) + return Exists(int, lambda d_0_i_: + (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))), ((a)[d_0_i_]) == (x)))) + +@Pure +def NotInArray(a : List[int], x : int) -> bool : + Requires(Acc(list_pred(a))) + return Forall(int, lambda d_0_i_: + (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))), ((a)[d_0_i_]) != (x)))) + +@Pure +def implArrays(chars : List[int], res : List[int], x : int) -> bool: + Requires(Acc(list_pred(chars))) + Requires(Acc(list_pred(res))) + return Implies(NotInArray(chars, x), InArray(res, x)) + +def reverse__delete(s : List[int], chars : List[int]) -> Tuple[List[int], bool]: + Requires(Acc(list_pred(s))) + Requires(Acc(list_pred(chars))) + Ensures(Acc(list_pred(Result()[0]))) + Ensures(Acc(list_pred(s))) + Ensures(Acc(list_pred(chars))) + Ensures((Result()[1]) == (is__palindrome__pred(Result()[0]))) + Ensures(Forall(int, lambda d_0_i_: + not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(Result()[0])))) or (NotInArray(chars, (Result()[0])[d_0_i_])))) + Ensures(Forall(int, lambda d_1_i_: + 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_])))) + 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)): + Invariant(Acc(list_pred(res))) + Invariant(Acc(list_pred(s))) + Invariant(Acc(list_pred(chars))) + Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(s)))) + Invariant(Forall(int, lambda d_4_i_: + (not (((0) <= (d_4_i_)) and ((d_4_i_) < (len(res)))) or (NotInArray(chars, (res)[d_4_i_])), [[]]))) + Invariant(Forall(int, lambda d_5_i_: + (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_])), [[implArrays(chars, res, (s)[d_6_j_])]]))) + 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) + Assert(Forall(int, lambda d_4_i_: + (not (((0) <= (d_4_i_)) and ((d_4_i_) < (len(res)))) or (NotInArray(chars, (res)[d_4_i_])), [[NotInArray(chars, (res)[d_4_i_])]]))) + return (res, is__palindrome) + +def is__palindrome__fun(text : List[int]) -> bool: + Requires(Acc(list_pred(text), 1/2)) + 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)) + 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(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)])), [[]])))) + if ((text)[d_1_i_]) != ((text)[((len(text)) - (d_1_i_)) - (1)]): + result = False + d_1_i_ = (d_1_i_) + (1) + return result + +@Pure +def is__palindrome__pred(s : List[int]) -> bool : + Requires(Acc(list_pred(s), 1/2)) + 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_)])), [[]])) + diff --git a/WIP/006-parse_nested_parens.py b/WIP/006-parse_nested_parens.py index d488be3..76e60f4 100644 --- a/WIP/006-parse_nested_parens.py +++ b/WIP/006-parse_nested_parens.py @@ -26,6 +26,11 @@ # d_2_i_ = (d_2_i_) + (1) # return max__depth +@Pure +def get_len(s : List[int]) -> bool: + Requires(Acc(list_pred(s), 1/2)) + return len(s) > 0 + def split(s : List[int], res : List[List[int]]) -> None: Requires(Acc(list_pred(s))) Requires(Acc(list_pred(res))) @@ -35,7 +40,7 @@ def split(s : List[int], res : List[List[int]]) -> None: Ensures(Acc(list_pred(res))) Ensures(Forall(res, lambda x: Acc(list_pred(x), 1/2))) Ensures(Forall(int, lambda d_10_j_: - Implies(d_10_j_ >= 0 and d_10_j_ < len(res), (len(res[d_10_j_])) > (0)))) + Implies(d_10_j_ >= 0 and d_10_j_ < len(res), (get_len(res[d_10_j_]))))) # Ensures(Forall(int, lambda d_10_j_: # not (d_10_j_ >= 0 and d_10_j_ < len(Result())) or ((Forall(int, lambda d_11_j_: # not (((d_11_j_) >= (0)) and ((d_11_j_) < (len(Result()[d_10_j_])))) or ((((Result()[d_10_j_])[d_11_j_]) == (1)) or (((Result()[d_10_j_])[d_11_j_]) == (2))))) and ((len(Result()[d_10_j_])) > (0))))) @@ -57,53 +62,24 @@ def split(s : List[int], res : List[List[int]]) -> None: (((((d_7_current__string_)[d_4_i_]) == (1)) or (((d_7_current__string_)[d_4_i_]) == (2)))), [[]]))) Invariant(Forall(res, lambda x: Acc(list_pred(x), 1/2))) Invariant(Forall(int, lambda d_10_j_: - (Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((len(res[d_10_j_])) > (0))), [[]]))) - # Invariant(Forall(int, lambda d_10_j_: - # (not (d_10_j_ >= 0 and d_10_j_ < len(res)) or - # ((Forall(int, lambda d_11_j_: - # not (((d_11_j_) >= (0)) and ((d_11_j_) < (len(res[d_10_j_])))) or - # ((((res[d_10_j_])[d_11_j_]) == (1)) or (((res[d_10_j_])[d_11_j_]) == (2))))))))) # , [[res[d_10_j_]]] - # Invariant(Forall(res, lambda x: - # ((Forall(int, lambda d_11_j_: - # (not (((d_11_j_) >= (0)) and ((d_11_j_) < (len(x)))) or - # ((((x)[d_11_j_]) == (1)) or (((x)[d_11_j_]) == (2))), [[]]))), [[]]))) # , [[res[d_10_j_]]] - # Invariant(Forall(int, lambda d_10_j_: - # (not (d_10_j_ >= 0 and d_10_j_ < len(res)) or - # ((Forall(int, lambda d_11_j_: - # not (((d_11_j_) >= (0)) and ((d_11_j_) < (len(res[d_10_j_])))) or - # ((((res[d_10_j_])[d_11_j_]) == (1)) or (((res[d_10_j_])[d_11_j_]) == (2)))))), [[res[d_10_j_]]]))) # , [[res[d_10_j_]]] + (Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((get_len(res[d_10_j_])))), [[]]))) if ((s)[d_8_i_]) == (3): - Assert(Forall(int, lambda d_10_j_: - (Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((len(res[d_10_j_])) > (0))), [[]]))) if len(d_7_current__string_) > 0: d_7_copy = list(d_7_current__string_) - Assert(len(d_7_current__string_) > 0) - Assert(len(d_7_copy) > 0) - Assert(Forall(int, lambda d_4_i_: - (not (((d_4_i_) >= (0)) and ((d_4_i_) < (len(d_7_current__string_)))) or (((((d_7_current__string_)[d_4_i_]) == (1)) or (((d_7_current__string_)[d_4_i_]) == (2)))), [[]]))) - Assert(Forall(int, lambda d_9_j_: - (not (d_9_j_ >= 0 and d_9_j_ < len(d_7_copy)) or - ((d_7_copy[d_9_j_]) == (1) or (d_7_copy[d_9_j_]) == (2)), [[]]))) - Assert(Forall(int, lambda d_10_j_: - (Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((len(res[d_10_j_])) > (0))), [[]]))) res = (res) + [d_7_copy] - Assert(Forall(int, lambda d_10_j_: - (Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((len(res[d_10_j_])) > (0))), [[]]))) d_7_current__string_ = [] else: d_7_current__string_ = (d_7_current__string_) + [(s)[d_8_i_]] d_8_i_ = (d_8_i_) + (1) - Assert(Forall(int, lambda d_10_j_: - (Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((len(res[d_10_j_])) > (0))), [[]]))) if len(d_7_current__string_) > 0: - # Assert(Forall(int, lambda d_10_j_: - # (Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((len(res[d_10_j_])) > (0)))))) - res = (res) + [d_7_current__string_] - # Assert(Forall(int, lambda d_10_j_: - # (Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((len(res[d_10_j_])) > (0)))))) + d_7_copy = list(d_7_current__string_) + 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_ = [] Assert(Forall(int, lambda d_10_j_: - (Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((len(res[d_10_j_])) > (0))), [[]]))) + (Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((get_len(res[d_10_j_])))), [[]]))) + return None # def parse__nested__parens(paren__string : List[int]) -> List[int]: # Requires(Forall(int, lambda d_12_i_: From 63d5f1a2e9b6a7e70be66c0abd103fdec4f88bf2 Mon Sep 17 00:00:00 2001 From: alex28sh Date: Thu, 29 Aug 2024 09:46:39 +0200 Subject: [PATCH 2/6] 109 --- Bench/109-move_one_ball.py | 65 ++++++++++++++++++++++++++++++++++++++ README.md | 4 +-- 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 Bench/109-move_one_ball.py diff --git a/Bench/109-move_one_ball.py b/Bench/109-move_one_ball.py new file mode 100644 index 0000000..6a7335c --- /dev/null +++ b/Bench/109-move_one_ball.py @@ -0,0 +1,65 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def is__sorted(a : List[int], l : int, r : int) -> bool : + Requires(Acc(list_pred(a))) + Requires(((0) <= (l)) and ((l) <= (r)) and ((r) <= (len(a)))) + 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_])))) + +def move__one__ball(a : List[int]) -> bool: + 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_]))))) + Ensures(Acc(list_pred(a))) + Ensures(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_]))))) + Ensures(Implies(Result(), Exists(int, lambda d_4_i_: + (((0) <= (d_4_i_)) and ((d_4_i_) < (len(a)))) and (is__sorted(a, 0, d_4_i_) and is__sorted(a, d_4_i_, len(a)) and (Forall(int, lambda d_5_j_: + 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_]))))))))) + Ensures(Implies(Exists(int, lambda d_4_i_: + (((0) <= (d_4_i_)) and ((d_4_i_) < (len(a)))) and (is__sorted(a, 0, d_4_i_) and is__sorted(a, d_4_i_, len(a)) and (Forall(int, lambda d_5_j_: + 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())) + can = False # type : bool + if (len(a)) <= (1): + Assert(is__sorted(a, 0, len(a))) + can = True + return can + can = False + d_5_i_ = int(0) # type : int + d_5_i_ = 0 + d_6_min__index_ = int(0) # type : int + d_6_min__index_ = 0 + while (d_5_i_) < (len(a)): + 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)))) + Invariant(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_])) + , [[(a)[d_3_j_]]])) + , [[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_]]]))) + 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(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], + (Forall(int, lambda d_5_j_: + 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_]))))))) + 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 diff --git a/README.md b/README.md index 50db694..f0d2f1b 100644 --- a/README.md +++ b/README.md @@ -112,10 +112,10 @@ Current status: - [x] 106. f - [ ] 107. even_odd_palindrome - [x] 108. count_nums -- [ ] 109. move_one_ball +- [x] 109. move_one_ball - [x] 110. exchange - [ ] 111. histogram -- [ ] 112. reverse_delete +- [x] 112. reverse_delete - [ ] 113. odd_count - [x] 114. minSubArraySum - [ ] 115. max_fill From 5b3553e5400c216bd74946f34c6cb4d816f12e96 Mon Sep 17 00:00:00 2001 From: alex28sh Date: Thu, 29 Aug 2024 17:45:00 +0200 Subject: [PATCH 3/6] 34, 84, 104, 112, 134 --- Bench/026-remove_duplicates.py | 2 +- {WIP => Bench}/034-unique.py | 72 ++++------ Bench/084-solve.py | 27 ++++ Bench/104-unique_digits.py | 145 ++++++++++++++++++++ Bench/112-reverse_delete.py | 6 +- Bench/134-check_if_last_char_is_a_letter.py | 14 ++ README.md | 8 +- WIP/007-filter_by_substring.py | 93 +++++++++++++ WIP/026-remove_duplicates.py | 6 +- WIP/029-filter_by_prefix.py | 23 +++- WIP/074-total_match.py | 25 ++-- WIP/104-unique_digits.py | 109 --------------- WIP/115-max_fill.py | 15 +- WIP/130-tri.py | 60 ++++---- 14 files changed, 396 insertions(+), 209 deletions(-) rename {WIP => Bench}/034-unique.py (60%) create mode 100644 Bench/084-solve.py create mode 100644 Bench/104-unique_digits.py create mode 100644 Bench/134-check_if_last_char_is_a_letter.py create mode 100644 WIP/007-filter_by_substring.py delete mode 100644 WIP/104-unique_digits.py diff --git a/Bench/026-remove_duplicates.py b/Bench/026-remove_duplicates.py index a12f947..24971e2 100644 --- a/Bench/026-remove_duplicates.py +++ b/Bench/026-remove_duplicates.py @@ -43,7 +43,7 @@ def remove__duplicates(a : List[int]) -> List[int]: def exists_check(a : List[int], x : int) -> bool: Requires(Acc(list_pred(a), 1/2)) return Exists(int, lambda d_0_i_: - (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))), ((a)[d_0_i_]) == (x)))) + ((((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))) and ((a)[d_0_i_]) == (x)))) @Pure def count_check(a : List[int], x : int) -> bool: diff --git a/WIP/034-unique.py b/Bench/034-unique.py similarity index 60% rename from WIP/034-unique.py rename to Bench/034-unique.py index c53f899..b708318 100644 --- a/WIP/034-unique.py +++ b/Bench/034-unique.py @@ -1,6 +1,14 @@ from typing import cast, List, Dict, Set, Optional, Union from nagini_contracts.contracts import * +@Pure +def InArray(a : List[int], x : int) -> bool: + Requires(Acc(list_pred(a), 1/2)) + return Exists(int, lambda d_0_i_: + ((((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))) and ((a)[d_0_i_]) == (x)))) + + + def uniqueSorted(s : List[int]) -> List[int]: Requires(Acc(list_pred(s))) Requires(Forall(int, lambda d_0_i_: @@ -9,13 +17,13 @@ def uniqueSorted(s : List[int]) -> List[int]: ((s)[d_0_i_]) <= ((s)[d_1_j_]))))) Ensures(Acc(list_pred(s))) Ensures(Acc(list_pred(Result()))) - # 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_4_x_: - # not ((d_4_x_) in (Result())) or ((d_4_x_) in (s)))) - # Ensures(Forall(int, lambda d_5_x_: - # not ((d_5_x_) in (s)) or ((d_5_x_) in (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_]))))) result = list([int(0)] * 0) # type : List[int] result = list([]) d_6_i_ = int(0) # type : int @@ -28,44 +36,24 @@ def uniqueSorted(s : List[int]) -> List[int]: Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len(s))), ((s)[d_0_i_]) <= ((s)[d_1_j_]))))) Invariant(((0) <= (d_6_i_)) and ((d_6_i_) <= (len(s)))) - # Invariant(Forall(int, lambda d_7_k_: - # Forall(int, lambda d_8_l_: - # not ((((0) <= (d_7_k_)) and ((d_7_k_) < (d_8_l_))) and ((d_8_l_) < (len(result)))) or (((result)[d_7_k_]) < ((result)[d_8_l_]))))) Invariant(Forall(int, lambda d_9_k_: - (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(result))), Exists(int, lambda d_10_m_: - (((0) <= (d_10_m_)) and ((d_10_m_) < (d_6_i_))) and (((result)[d_9_k_]) == ((s)[d_10_m_])))), [[(result)[d_9_k_]]]))) - # Invariant(Forall(int, lambda d_11_k_: - # (Forall(int, lambda d_12_l_: - # (Implies(0 <= d_11_k_ and d_11_k_ < len(result), - # Implies(d_6_i_ <= d_12_l_ and d_12_l_ < len(s), result[d_11_k_] <= s[d_12_l_])), - # [[s[d_12_l_]]])), [[result[d_11_k_]]]))) - # Invariant(Forall(int, lambda d_11_j_: - # not (((0) <= (d_11_j_)) and ((d_11_j_) < (d_6_i_))) or (((s)[d_11_j_]) in (result)))) + (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(result))), InArray(s, result[d_9_k_])), [[InArray(s, result[d_9_k_])]]))) + Invariant(Implies(d_6_i_ < len(s), + Forall(int, lambda d_2_i_: + (Implies(((0) <= (d_2_i_)) and ((d_2_i_) < (len(result))), result[d_2_i_] <= s[d_6_i_]), [[result[d_2_i_]]])))) + Invariant(Forall(int, lambda d_7_k_: + (Forall(int, lambda d_8_l_: + (not ((((0) <= (d_7_k_)) and ((d_7_k_) < (d_8_l_))) and ((d_8_l_) < (len(result)))) or (((result)[d_7_k_]) < ((result)[d_8_l_])), + [[(result)[d_8_l_]]])), + [[(result)[d_7_k_]]]))) + Invariant(Forall(int, lambda d_11_j_: + (Implies(((0) <= (d_11_j_)) and ((d_11_j_) < (d_6_i_)), InArray(result, s[d_11_j_])), [[]]))) if ((len(result)) == (0)) or (((result)[(len(result)) - (1)]) != ((s)[d_6_i_])): - # Assert(((len(result)) == (0)) or (((result)[(len(result)) - (1)]) < ((s)[d_6_i_]))) - # old_res = list(result) - # Assert(Forall(int, lambda d_9_k_: - # (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(result))), Exists(int, lambda d_10_m_: - # (((0) <= (d_10_m_)) and ((d_10_m_) < (d_6_i_))) and (((result)[d_9_k_]) == ((s)[d_10_m_])))), [[(result)[d_9_k_]]]))) - # Assert(Forall(int, lambda d_9_k_: Implies(0 <= d_9_k_ and d_9_k_ < len(result), result[d_9_k_] == old_res[d_9_k_]))) - # Assert(Forall(int, lambda d_9_k_: - # (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(old_res))), Exists(int, lambda d_10_m_: - # (((0) <= (d_10_m_)) and ((d_10_m_) < (d_6_i_))) and (((old_res)[d_9_k_]) == ((s)[d_10_m_])))), [[(old_res)[d_9_k_]]]))) + Assert(Implies(len(result) > 0, result[len(result) - 1] < s[d_6_i_])) + Assert(Implies(len(result) > 0, + Forall(int, lambda d_11_j_: + Implies(0 <= d_11_j_ and d_11_j_ < len(result), result[d_11_j_] < s[d_6_i_])))) result = (result) + [(s)[d_6_i_]] - # Assert(Exists(int, lambda d_10_m_: - # (((0) <= (d_10_m_)) and ((d_10_m_) <= (d_6_i_))) and (((result)[len(result) - 1]) == ((s)[d_10_m_])))) - # Assert(Forall(int, lambda d_9_k_: - # (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(old_res))), Exists(int, lambda d_10_m_: - # (((0) <= (d_10_m_)) and ((d_10_m_) < (d_6_i_))) and (((old_res)[d_9_k_]) == ((s)[d_10_m_])))), [[(old_res)[d_9_k_]]]))) - # Assert(Forall(int, lambda d_9_k_: - # (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(old_res))), Exists(int, lambda d_10_m_: - # (((0) <= (d_10_m_)) and ((d_10_m_) <= (d_6_i_))) and (((old_res)[d_9_k_]) == ((s)[d_10_m_])))), [[(old_res)[d_9_k_]]]))) - # Assert(Forall(int, lambda d_9_k_: - # (Implies(((0) <= (d_9_k_)) and ((d_9_k_) + 1 < (len(result))), Exists(int, lambda d_10_m_: - # (((0) <= (d_10_m_)) and ((d_10_m_) <= (d_6_i_))) and (((result)[d_9_k_]) == ((s)[d_10_m_])))), [[(result)[d_9_k_]]]))) - # Assert(Forall(int, lambda d_9_k_: - # (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(result))), Exists(int, lambda d_10_m_: - # (((0) <= (d_10_m_)) and ((d_10_m_) <= (d_6_i_))) and (((result)[d_9_k_]) == ((s)[d_10_m_])))), [[(result)[d_9_k_]]]))) d_6_i_ = (d_6_i_) + (1) return result diff --git a/Bench/084-solve.py b/Bench/084-solve.py new file mode 100644 index 0000000..50dc852 --- /dev/null +++ b/Bench/084-solve.py @@ -0,0 +1,27 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +def solve(n : int) -> int: + Requires((n) >= (0)) + Ensures(Result() >= 0) + Ensures((Result()) == (popcount(n))) + r = int(0) # type : int + d_0_m_ = int(0) # type : int + d_0_m_ = n + r = 0 + while (d_0_m_) > (0): + Invariant(((0) <= (d_0_m_)) and ((d_0_m_) <= (n))) + Invariant(r >= 0) + Invariant(((r) + (popcount(d_0_m_))) == (popcount(n))) + r = (r) + ((d_0_m_ % 2)) + d_0_m_ = (d_0_m_ // 2) + return r + +@Pure +def popcount(n : int) -> int : + Requires(n >= 0) + if n == 0: + return 0 + else: + return (n % 2) + popcount(n // 2) + diff --git a/Bench/104-unique_digits.py b/Bench/104-unique_digits.py new file mode 100644 index 0000000..ef9d2ec --- /dev/null +++ b/Bench/104-unique_digits.py @@ -0,0 +1,145 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + + +@Pure +def InArray(a : List[int], x : int) -> bool: + Requires(Acc(list_pred(a), 1/2)) + return Exists(int, lambda d_0_i_: + ((((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))) and ((a)[d_0_i_]) == (x)))) + +@Pure +def HasNoEvenDigit(n : int) -> bool : + Requires(((0) <= (n))) + return (n == 0 or (((((n % 10) % 2)) != (0)) and (HasNoEvenDigit((n // 10))))) + +def UniqueDigits(x : List[int]) -> List[int]: + 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)))) + 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_: + not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(Result())))) or (HasNoEvenDigit((Result())[d_0_i_])))) + Ensures(Forall(int, lambda d_8_e_: + 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 (InArray(x, 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 + + while d_5_i_ < len(x): + 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_: + (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_])), + [[(HasNoEvenDigit(x[d_8_e_]))]]))) + Invariant(Forall(int, lambda d_7_e_: + (Implies((d_7_e_) >= 0 and d_7_e_ < len(result), + InArray(x, result[d_7_e_])), + [[InArray(x, 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(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)), [[x[d_0_i_]]]))) + Invariant(Forall(int, lambda d_0_i_: + (Implies(d_0_i_ >= 0 and d_0_i_ < len(result), (result[d_0_i_] >= 0)), [[result[d_0_i_]]]))) + 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_7_e_: + (Implies((d_7_e_) >= 0 and d_7_e_ < len(result), + InArray(x, result[d_7_e_])), + [[InArray(x, result[d_7_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_])), + [[(result)[d_11_k_]]])), + [[(result)[d_10_j_]]]))) + Invariant(Forall(int, lambda d_12_j_: + (not ((((0) <= (d_12_j_)) and ((d_12_j_) < (d_9_i_)))) or + (Forall(int, lambda d_13_k_: + (not ((((d_9_i_) <= (d_13_k_)) and ((d_13_k_) < (len(result))))) or + (((result)[d_12_j_]) <= ((result)[d_13_k_])), + [[result[d_13_k_]]]))), + [[(result)[d_12_j_]]]))) + 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)), [[x[d_0_i_]]]))) + Invariant(Forall(int, lambda d_0_i_: + (Implies(d_0_i_ >= 0 and d_0_i_ < len(result), (result[d_0_i_] >= 0)), [[result[d_0_i_]]]))) + 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_6_j_: + (Implies(((0) <= (d_6_j_)) and ((d_6_j_) < (len(result))), HasNoEvenDigit((result)[d_6_j_])), [[HasNoEvenDigit((result)[d_6_j_])]]))) + Invariant(HasNoEvenDigit((result)[d_17_minIndex_])) + 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_7_e_: + (Implies((d_7_e_) >= 0 and d_7_e_ < len(result), + InArray(x, result[d_7_e_])), + [[InArray(x, result[d_7_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_])), + [[(result)[d_11_k_]]])), + [[(result)[d_10_j_]]]))) + 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_])), [[((result)[d_19_k_])]]))) + Invariant(Forall(int, lambda d_12_j_: + (not ((((0) <= (d_12_j_)) and ((d_12_j_) < (d_9_i_)))) or + (Forall(int, lambda d_13_k_: + (not ((((d_9_i_) <= (d_13_k_)) and ((d_13_k_) < (len(result))))) or + (((result)[d_12_j_]) <= ((result)[d_13_k_])), + [[result[d_13_k_]]]))), + [[(result)[d_12_j_]]]))) + 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(HasNoEvenDigit((result)[d_17_minIndex_])) + result[d_9_i_] = (result)[d_17_minIndex_] + Assert(HasNoEvenDigit(d_20_temp_)) + result[d_17_minIndex_] = d_20_temp_ + d_9_i_ = (d_9_i_) + (1) + return result diff --git a/Bench/112-reverse_delete.py b/Bench/112-reverse_delete.py index f5add94..906aa00 100644 --- a/Bench/112-reverse_delete.py +++ b/Bench/112-reverse_delete.py @@ -5,7 +5,7 @@ def InArray(a : List[int], x : int) -> bool : Requires(Acc(list_pred(a))) return Exists(int, lambda d_0_i_: - (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))), ((a)[d_0_i_]) == (x)))) + ((((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))) and ((a)[d_0_i_]) == (x)))) @Pure def NotInArray(a : List[int], x : int) -> bool : @@ -46,13 +46,11 @@ def reverse__delete(s : List[int], chars : List[int]) -> Tuple[List[int], bool]: Invariant(Forall(int, lambda d_5_i_: (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_])), [[implArrays(chars, res, (s)[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_])]]))) 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) - Assert(Forall(int, lambda d_4_i_: - (not (((0) <= (d_4_i_)) and ((d_4_i_) < (len(res)))) or (NotInArray(chars, (res)[d_4_i_])), [[NotInArray(chars, (res)[d_4_i_])]]))) return (res, is__palindrome) def is__palindrome__fun(text : List[int]) -> bool: diff --git a/Bench/134-check_if_last_char_is_a_letter.py b/Bench/134-check_if_last_char_is_a_letter.py new file mode 100644 index 0000000..031dfcd --- /dev/null +++ b/Bench/134-check_if_last_char_is_a_letter.py @@ -0,0 +1,14 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def check__if__last__char__is__a__letter(s : List[int]) -> bool: + Requires(Acc(list_pred(s))) + 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))))) + 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 + +@Pure +def is__alpha(c : int) -> bool : + return (((97) <= (c)) and ((c) <= (122))) or (((65) <= (c)) and ((c) <= (90))) diff --git a/README.md b/README.md index f0d2f1b..e05fd79 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Current status: - [x] 31. is_prime - [ ] 32. poly - [x] 33. sort_third -- [ ] 34. unique +- [x] 34. unique - [x] 35. max_element - [ ] 36. fizz_buzz - [x] 37. sort_even @@ -87,7 +87,7 @@ Current status: - [ ] 81. numerical_letter_grade - [x] 82. prime_length - [x] 83. starts_one_ends -- [ ] 84. solve +- [x] 84. solve - [x] 85. add - [ ] 86. anti_shuffle - [ ] 87. get_row @@ -107,7 +107,7 @@ Current status: - [ ] 101. words_string - [x] 102. choose_num - [ ] 103. rounded_avg -- [ ] 104. unique_digits +- [x] 104. unique_digits - [x] 105. by_length - [x] 106. f - [ ] 107. even_odd_palindrome @@ -137,7 +137,7 @@ Current status: - [ ] 131. digits - [ ] 132. is_nested - [x] 133. sum_squares -- [ ] 134. check_if_last_char_is_a_letter +- [x] 134. check_if_last_char_is_a_letter - [x] 135. can_arrange - [x] 136. largest_smallest_integers - [ ] 137. compare_one diff --git a/WIP/007-filter_by_substring.py b/WIP/007-filter_by_substring.py new file mode 100644 index 0000000..9dbda39 --- /dev/null +++ b/WIP/007-filter_by_substring.py @@ -0,0 +1,93 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def checkSubstring(s : List[int], sub : List[int]) -> bool: + Requires(Acc(list_pred(s),1/2)) + Requires(Acc(list_pred(sub), 1/2)) + Ensures(Acc(list_pred(s),1/2)) + Ensures(Acc(list_pred(sub), 1/2)) + result = False # type : bool + result = False + if (len(sub)) == (0): + result = True + elif (len(s)) >= (len(sub)): + d_0_i_ = int(0) # type : int + d_0_i_ = 0 + while (d_0_i_) <= ((len(s)) - (len(sub))): + 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))))) + x = 0 + fl = True + while x < len(sub): + 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)) + if sub[x] != s[d_0_i_ + x]: + fl = False + break + x = x + 1 + if fl: + result = True + d_0_i_ = (d_0_i_) + (1) + return result + +@Pure +def EqArrays(a : List[int], x : List[int]) -> bool : + Requires(Acc(list_pred(a))) + Requires(Acc(list_pred(x))) + 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_])) + +@Pure +def InArray(a : List[List[int]], x : List[int]) -> bool : + Requires(Acc(list_pred(a))) + Requires(Acc(list_pred(x))) + Requires(Forall(a, lambda d_0_s_: Acc(list_pred(d_0_s_)))) + return Exists(int, lambda d_0_s_: + (Implies(((0) <= (d_0_s_)) and ((d_0_s_) < (len((a)))), + EqArrays(a[d_0_s_], x)))) + +# @Pure +# def AccBiDim(a : List[List[int]]) -> bool : +# Requires(Acc(list_pred(a))) +# return Forall(a, lambda d_0_s_: Acc(list_pred(d_0_s_))) + +def filter__by__substring(strings : List[List[int]], substring : List[int]) -> List[List[int]]: + Requires(Acc(list_pred(strings))) + Requires(Forall(strings, lambda d_0_s_: Acc(list_pred(d_0_s_)))) + Requires(Acc(list_pred(substring))) + Ensures(Acc(list_pred(strings))) + Ensures(Forall(strings, lambda d_0_s_: Acc(list_pred(d_0_s_)))) + Ensures(Acc(list_pred(Result()))) + # Ensures(AccBiDim(Result())) + Ensures((len(Result())) <= (len(strings))) + # Ensures(Forall(List[int], lambda d_3_s_: Implies(Exists(int, lambda x : Implies(x >= 0 and x < len(Result()), Result()[x] == d_3_s_)), Acc(list_pred(d_3_s_))))) + # Ensures(Forall(int, lambda d_3_i_: + # (Implies(0 <= d_3_i_ and d_3_i_ < len(Result()), Acc(list_pred(Result()[d_3_i_])) and InArray(strings, Result()[d_3_i_]))))) + # Ensures(Forall(List[int], lambda d_1_s_: + # not ((d_1_s_) in (Result())) or ((d_1_s_) in (strings)))) + res : List[List[int]] = [] + d_2_i_ = int(0) # type : int + d_2_i_ = 0 + while (d_2_i_) < (len(strings)): + Invariant(Acc(list_pred(res))) + Invariant(Acc(list_pred(strings))) + Invariant(Acc(list_pred(substring))) + Invariant(Forall(strings, lambda d_0_s_: Acc(list_pred(d_0_s_)))) + Invariant(Forall(res, lambda d_3_s_: Acc(list_pred(d_3_s_)))) + Invariant(((0) <= (d_2_i_)) and ((d_2_i_) <= (len(strings)))) + 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_])]]))) + # Invariant(Forall(List[int], lambda d_3_s_: + # not ((d_3_s_) in (res)) or ((d_3_s_) in (strings)))) + d_4_check_ = False # type : bool + d_4_check_ = checkSubstring((strings)[d_2_i_], substring) + if d_4_check_: + cpy = list((strings)[d_2_i_]) + res = (res) + [cpy] + d_2_i_ = (d_2_i_) + (1) + return res diff --git a/WIP/026-remove_duplicates.py b/WIP/026-remove_duplicates.py index e338355..a210e19 100644 --- a/WIP/026-remove_duplicates.py +++ b/WIP/026-remove_duplicates.py @@ -39,7 +39,7 @@ def remove__duplicates(a : List[int]) -> List[int]: [[]]))) 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_]), - [[(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_: @@ -56,13 +56,13 @@ def remove__duplicates(a : List[int]) -> List[int]: def check_eq(a : List[int], result : List[int], x : int) -> bool: Requires(Acc(list_pred(a), 1/2)) Requires(Acc(list_pred(result), 1/2)) - return (count__rec(a, x, len(a)) == 1) == (Exists(int, lambda d_3_i_: d_3_i_ >= 0 and d_3_i_ < len(result) and result[d_3_i_] == x)) + return (count__rec(a, x, len(a)) == 1) == (exists_check(result, x)) @Pure def exists_check(a : List[int], x : int) -> bool: Requires(Acc(list_pred(a), 1/2)) return Exists(int, lambda d_0_i_: - (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))), ((a)[d_0_i_]) == (x)))) + ((((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))) and ((a)[d_0_i_]) == (x)))) @Pure def count_check(a : List[int], x : int) -> bool: diff --git a/WIP/029-filter_by_prefix.py b/WIP/029-filter_by_prefix.py index 8d259fb..eda689d 100644 --- a/WIP/029-filter_by_prefix.py +++ b/WIP/029-filter_by_prefix.py @@ -2,6 +2,12 @@ from nagini_contracts.contracts import * +@Pure +def InArray(a : List[int], x : int) -> bool: + Requires(Acc(list_pred(a), 1/2)) + return Exists(int, lambda d_0_i_: + ((((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))) and ((a)[d_0_i_]) == (x)))) + @Pure def starts__with(s : List[int], p : List[int], i : int) -> bool : Requires(Acc(list_pred(s), 1/2)) @@ -25,6 +31,15 @@ def starts__with__fun(s : List[int], p : List[int], i : int) -> bool : return starts__with(s, p, i + 1) return False +@Pure +def implStarts__with(xs : List[List[int]], p : List[int], filtered : List[int], d_2_j : int) -> bool: + Requires(Acc(list_pred(xs), 1/2)) + Requires(Forall(xs, lambda x : Acc(list_pred(x), 1/2))) + Requires(Acc(list_pred(p), 1/2)) + Requires(Acc(list_pred(filtered), 1/2)) + Requires(0 <= d_2_j and d_2_j < len(xs)) + return Implies(starts__with(xs[d_2_j], p, 0), InArray(filtered, d_2_j)) + def filter__by__prefix(xs : List[List[int]], p : List[int]) -> List[int]: Requires(Acc(list_pred(xs))) Requires(Acc(list_pred(p))) @@ -50,10 +65,10 @@ 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)]]))) - # 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_)), - # [[starts__with(xs[d_2_j_], p, 0)]]))) + Invariant(Forall(int, lambda d_2_j_: + (Implies(((0) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_)), + implStarts__with(xs, p, filtered, 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_)), diff --git a/WIP/074-total_match.py b/WIP/074-total_match.py index 88f8423..4b34501 100644 --- a/WIP/074-total_match.py +++ b/WIP/074-total_match.py @@ -27,6 +27,11 @@ # # res = list(list2) # type : List[List[int]] # return list2 +@Pure +def get_len(lst : List[int]) -> int: + Requires(Acc(list_pred(lst), 1/2)) + return len(lst) + @Pure def sum__chars__rec(i : int, j : int, lst : List[List[int]]) -> int : Requires(Acc(list_pred(lst), 1/2)) @@ -35,14 +40,14 @@ def sum__chars__rec(i : int, j : int, lst : List[List[int]]) -> int : if i == j: return 0 else: - return len((lst)[j - 1]) + sum__chars__rec(i, j - 1, lst) + return get_len((lst)[j - 1]) + sum__chars__rec(i, j - 1, lst) @Pure def progress(lst : List[List[int]], i : int) -> bool: Requires(Acc(list_pred(lst), 1/2)) Requires(Forall(lst, lambda x: Acc(list_pred(x), 1/2))) Requires(((0) <= (i)) and ((i) < (len(lst)))) - return sum__chars__rec(0, i + 1, lst) == (sum__chars__rec(0, i, lst) + len(lst[i])) + return sum__chars__rec(0, i + 1, lst) == (sum__chars__rec(0, i, lst) + get_len(lst[i])) def SumChars(lst : List[List[int]]) -> int: Requires(Acc(list_pred(lst))) @@ -54,20 +59,20 @@ def SumChars(lst : List[List[int]]) -> int: sum = 0 d_3_i_ = int(0) # type : int d_3_i_ = 0 - # Assert(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)]]))) + Assert(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_]))), + [[]]))) while (d_3_i_) < (len(lst)): 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(lst == Old(lst)) - Invariant(Forall(int, lambda d_0_i_: - (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len(lst))), - progress(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_)) - sum = (sum) + (len((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/104-unique_digits.py b/WIP/104-unique_digits.py deleted file mode 100644 index ee80290..0000000 --- a/WIP/104-unique_digits.py +++ /dev/null @@ -1,109 +0,0 @@ -from typing import cast, List, Dict, Set, Optional, Union -from nagini_contracts.contracts import * - -@Pure -def HasNoEvenDigit(n : int) -> bool : - Requires(((0) <= (n))) - return (n == 0 or (((((n % 10) % 2)) != (0)) and (HasNoEvenDigit((n // 10))))) - -def UniqueDigits(x : List[int]) -> List[int]: - 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)))) - 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_: - not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(Result())))) or (HasNoEvenDigit((Result())[d_0_i_])))) - Ensures(Forall(int, lambda d_8_e_: - 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_]))))) - result = list([int(0)] * 0) # type : List[int] - result = list([]) - d_5_i_ = 0 - - while d_5_i_ < len(x): - 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_: - (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_])), - [[(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(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/115-max_fill.py b/WIP/115-max_fill.py index 1459524..0a3d8b1 100644 --- a/WIP/115-max_fill.py +++ b/WIP/115-max_fill.py @@ -68,9 +68,10 @@ def max__fill(grid : List[List[int]], capacity : int) -> int: , [[grid[d_0_i_]]]))) Invariant(Forall(int, lambda d_0_i_: (Implies(d_0_i_ >= 0 and d_0_i_ < len(grid), - psum2(0, d_0_i_ + 1, grid, capacity) == (((psum(0, len(grid[d_0_i_]), grid[d_0_i_]) + capacity - 1) // capacity) + psum2(0, d_0_i_, grid, capacity))), - [[psum2(0, d_0_i_ + 1, grid, capacity)]]))) + psumCheck(d_0_i_, grid, capacity, lst_h)), + [[psumCheck(d_0_i_, grid, capacity, lst_h)]]))) Assert(lst_h[d_2_i_] == psum(0, len(grid[d_2_i_]), grid[d_2_i_])) + Assert(psumCheck(d_2_i_, grid, capacity, lst_h)) # Assert(psum2(0, d_2_i_ + 1, grid, capacity) == psum2(0, d_2_i_, grid, capacity) + (psum(0, len(grid[d_2_i_]), grid[d_2_i_]) + capacity - 1) // capacity) cnt = cnt + ((lst_h[d_2_i_] + capacity - 1) // capacity) d_2_i_= d_2_i_ + 1 @@ -84,6 +85,16 @@ def max__fill(grid : List[List[int]], capacity : int) -> int: # Invariant((psum(0, d_4_j_ + 1, grid[d_2_i_])) == ((psum(0, d_4_j_, grid[d_2_i_]) + (grid[d_2_i_])[d_4_j_]))) # Invariant(cnt == (psum2(0, d_2_i_, grid, capacity))) +@Pure +def psumCheck(d_2_i_ : int, grid : List[List[int]], capacity : int, lst_h : List[int]) -> bool: + Requires(Acc(list_pred(lst_h), 1/2)) + Requires(Acc(list_pred(grid), 1/2)) + Requires(Forall(grid, lambda grid0: Acc(list_pred(grid0), 1/2))) + Requires(capacity > 0) + Requires(0 <= d_2_i_ and d_2_i_ < len(grid)) + Requires(len(grid) == len(lst_h)) + return psum2(0, d_2_i_ + 1, grid, capacity) == (psum2(0, d_2_i_, grid, capacity) + (psum(0, len(grid[d_2_i_]), grid[d_2_i_]) + capacity - 1) // capacity) + @Pure def psum2(i : int, j : int, s : List[List[int]], capacity : int) -> int : Requires(Acc(list_pred(s), 1/2)) diff --git a/WIP/130-tri.py b/WIP/130-tri.py index 529ca6f..625b5fe 100644 --- a/WIP/130-tri.py +++ b/WIP/130-tri.py @@ -15,20 +15,20 @@ def Tribonacci(n : int) -> List[int]: Requires((n) >= (0)) Ensures(Acc(list_pred(Result()))) Ensures((len(Result())) == ((n) + (1))) - Ensures(Forall(int, lambda d_0_i_: - Implies(((0) <= (d_0_i_)) and ((d_0_i_) <= (n)), ((Result())[d_0_i_]) == (tri(d_0_i_))))) - result = list([int(0)] * (n + 1)) # type : List[int] + # Ensures(Forall(int, lambda d_0_i_: + # Implies(((0) <= (d_0_i_)) and ((d_0_i_) <= (n)), ((Result())[d_0_i_]) == (tri(d_0_i_))))) + result : List[int] = list([int(0)] * (n + 1)) result[0] = 1 - Assert(result[0] == tri(0)) + # Assert(result[0] == tri(0)) if (n) > 0: result[1] = 3 d_1_i_ = int(0) # type : int d_1_i_ = 2 - Assert(result[1] == tri(1)) - Assert(result[(d_1_i_) - (2)] == tri(d_1_i_ - 2)) - Assert(result[(d_1_i_) - (1)] == tri(d_1_i_ - 1)) - Assert(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_)))))) + # Assert(result[1] == tri(1)) + # Assert(result[(d_1_i_) - (2)] == tri(d_1_i_ - 2)) + # Assert(result[(d_1_i_) - (1)] == tri(d_1_i_ - 1)) + # Assert(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_)))))) while (d_1_i_) <= (n): Invariant(Acc(list_pred(result))) Invariant(n >= 1) @@ -39,32 +39,32 @@ 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(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_: - (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_]]] + # 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_: + # (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_]]] if ((d_1_i_ % 2)) == (0): result[d_1_i_] = (1) + ((d_1_i_ // 2)) Assert(((result)[d_1_i_]) == (tri(d_1_i_))) - Assert(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_)]]))) - Assert(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_)]]))) + # Assert(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_)]]))) + # Assert(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_)]]))) else: - Assert(result[(d_1_i_) - (2)] == tri(d_1_i_ - 2)) - Assert(result[(d_1_i_) - (1)] == tri(d_1_i_ - 1)) + # Assert(result[(d_1_i_) - (2)] == tri(d_1_i_ - 2)) + # Assert(result[(d_1_i_) - (1)] == tri(d_1_i_ - 1)) Assert(tri(d_1_i_) == tri(d_1_i_ - 2) + tri(d_1_i_ - 1) + (d_1_i_ + 3) // 2) result[d_1_i_] = (((result)[(d_1_i_) - (2)]) + ((result)[(d_1_i_) - (1)])) + ((((d_1_i_) + (3)) // 2)) - Assert(Implies((result)[(d_1_i_) - (2)] == tri(d_1_i_ - 2) and (result)[(d_1_i_) - (1)] == tri(d_1_i_ - 1), result[d_1_i_] == tri(d_1_i_))) - Assert(((result)[d_1_i_]) == (tri(d_1_i_))) - Assert(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_)]]))) - Assert(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_)]]))) - Assert(((result)[d_1_i_]) == (tri(d_1_i_))) - Assert(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_)]]))) + # Assert(Implies((result)[(d_1_i_) - (2)] == tri(d_1_i_ - 2) and (result)[(d_1_i_) - (1)] == tri(d_1_i_ - 1), result[d_1_i_] == tri(d_1_i_))) + # Assert(((result)[d_1_i_]) == (tri(d_1_i_))) + # Assert(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_)]]))) + # Assert(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_)]]))) + # Assert(((result)[d_1_i_]) == (tri(d_1_i_))) + # Assert(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_)]]))) d_1_i_ = (d_1_i_) + (1) return result From 68b4c8ecdbc24070eac2165b742ff62bee7bc4cd Mon Sep 17 00:00:00 2001 From: alex28sh Date: Fri, 30 Aug 2024 17:52:23 +0200 Subject: [PATCH 4/6] 16, 36, 161 --- Bench/016-count_distinct_characters.py | 46 ++++++++++ Bench/036-fizz_buzz.py | 57 +++++++++++++ Bench/161-solve.py | 76 +++++++++++++++++ README.md | 8 +- WIP/029-filter_by_prefix.py | 96 --------------------- WIP/077-is_cube.py | 15 +++- WIP/087-get_row.py | 64 ++++++++++++++ WIP/158-find_max.py | 112 +++++++++++++++++++++++++ 8 files changed, 370 insertions(+), 104 deletions(-) create mode 100644 Bench/016-count_distinct_characters.py create mode 100644 Bench/036-fizz_buzz.py create mode 100644 Bench/161-solve.py delete mode 100644 WIP/029-filter_by_prefix.py create mode 100644 WIP/087-get_row.py create mode 100644 WIP/158-find_max.py diff --git a/Bench/016-count_distinct_characters.py b/Bench/016-count_distinct_characters.py new file mode 100644 index 0000000..5200daf --- /dev/null +++ b/Bench/016-count_distinct_characters.py @@ -0,0 +1,46 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def contains_char(s : List[int], c : int, i : int, j : int) -> bool: + 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))) + if i == j: + return False + else: + return s[j - 1] == c or contains_char(s, c, i, j - 1) + +@Pure +def count_chars_inter(s : List[int], c : int) -> int: + 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))) + 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) + +def count_distinct_characters(s : List[int]) -> int: + 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))))) + 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)) + c = int(0) # type : int + d_2_i_ = int(97) # type : int + while (d_2_i_) <= (122): + 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_)) + 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 diff --git a/Bench/036-fizz_buzz.py b/Bench/036-fizz_buzz.py new file mode 100644 index 0000000..8a7b6e5 --- /dev/null +++ b/Bench/036-fizz_buzz.py @@ -0,0 +1,57 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def fizz__buzz(n : int) -> int: + Requires(n >= 0) + Ensures(Result() >= 0) + Ensures((Result()) == fizz_buzz_fun(n)) + result = int(0) # type : int + result = 0 + d_1_i_ = int(0) # type : int + d_1_i_ = 0 + while (d_1_i_) < (n): + 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_)) + 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 + +@Pure +def fizz_buzz_fun(n : int) -> int: + Requires(n >= 0) + Ensures(Result() >= 0) + 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) + + +def count7(x : int) -> int: + Requires(x >= 0) + Ensures(Result() >= 0) + Ensures((Result()) == (count7__r(x))) + count = int(0) # type : int + count = 0 + d_6_y_ = int(0) # type : int + d_6_y_ = x + while (d_6_y_) > (0): + Invariant(((0) <= (d_6_y_)) and ((d_6_y_) <= (x))) + Invariant(((count) + (count7__r(d_6_y_))) == (count7__r(x))) + if ((d_6_y_ % 10)) == (7): + count = (count) + (1) + d_6_y_ = d_6_y_ // 10 + return count + +@Pure +def count7__r(x : int) -> int : + Requires(x >= 0) + Ensures(Result() >= 0) + if x == 0: + return 0 + else: + return (x % 10 == 7) + count7__r(x // 10) diff --git a/Bench/161-solve.py b/Bench/161-solve.py new file mode 100644 index 0000000..9fe013a --- /dev/null +++ b/Bench/161-solve.py @@ -0,0 +1,76 @@ +from typing import List +from nagini_contracts.contracts import * + +def solve(s : List[int]) -> List[int]: + Requires(Acc(list_pred(s), 1/2)) + Ensures(Acc(list_pred(Result()), 1/2)) + Ensures(Acc(list_pred(s), 1/2)) + Ensures((len(s)) == (len(Result()))) + Ensures(Implies(Forall(int, lambda d_0_i_: + not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) or (not(is__alpha((s)[d_0_i_])))), Forall(int, lambda d_1_i_: + not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((s)[d_1_i_]) == ((Result())[((len(Result())) - (1)) - (d_1_i_)]))))) + 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_])))))) + 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)): + Invariant(Acc(list_pred(t))) + Invariant(Acc(list_pred(s), 1/2)) + Invariant(((0) <= (d_5_i_)) and ((d_5_i_) <= (len(s)))) + Invariant((len(t)) == (d_5_i_)) + Invariant(Implies(d_4_flag_, Exists(int, lambda d_6_j_: + (((0) <= (d_6_j_)) and ((d_6_j_) < (d_5_i_))) and (is__alpha((s)[d_6_j_]))))) + Invariant(Implies(Exists(int, lambda d_6_j_: + (((0) <= (d_6_j_)) and ((d_6_j_) < (d_5_i_))) and (is__alpha((s)[d_6_j_]))), d_4_flag_)) + Invariant(Implies(not(d_4_flag_), Forall(int, lambda d_7_j_: + Implies(((0) <= (d_7_j_)) and (d_7_j_) < (d_5_i_), not(is__alpha((s)[d_7_j_])))))) + Invariant(Implies(Forall(int, lambda d_7_j_: + 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_]))), [[]]))) + if is__alpha((s)[d_5_i_]): + t = (t) + [flip__case((s)[d_5_i_])] + d_4_flag_ = True + else: + t = (t) + [(s)[d_5_i_]] + d_5_i_ = (d_5_i_) + (1) + if not(d_4_flag_): + t = reverse(t) + return t + +def reverse(str : List[int]) -> List[int]: + Requires(Acc(list_pred(str), 1/2)) + 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_)])))) + 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)): + 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_)])))) + rev = (rev) + [(str)[(len(str) - (d_12_i_)) - (1)]] + d_12_i_ = (d_12_i_) + (1) + return rev + +@Pure +def is__alpha(c : int) -> bool : + return (((97) <= (c)) and ((c) <= (122))) or (((65) <= (c)) and ((c) <= (90))) + +@Pure +def flip__case(c : int) -> int : + if ((97) <= (c)) and ((c) <= (122)): + return ((c) - (97)) + (65) + elif True: + return ((c) - (65)) + (97) diff --git a/README.md b/README.md index e05fd79..778e027 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Current status: - [x] 13. greatest_common_divisor - [ ] 14. all_prefixes - [ ] 15. string_sequence -- [ ] 16. count_distinct_characters +- [x] 16. count_distinct_characters - [ ] 17. parse_music - [ ] 18. how_many_times - [ ] 19. sort_numbers @@ -39,7 +39,7 @@ Current status: - [x] 33. sort_third - [x] 34. unique - [x] 35. max_element -- [ ] 36. fizz_buzz +- [x] 36. fizz_buzz - [x] 37. sort_even - [ ] 38. encode_cyclic - [ ] 39. prime_fib @@ -78,7 +78,7 @@ Current status: - [x] 72. will_it_fly - [x] 73. smallest_change - [ ] 74. total_match -- [x] 75. is_multiply_prime +- [ ] 75. is_multiply_prime - [ ] 76. is_simple_power - [ ] 77. iscube - [x] 78. hex_key @@ -164,6 +164,6 @@ Current status: - [ ] 158. find_max - [x] 159. eat - [ ] 160. do_algebra -- [ ] 161. solve +- [x] 161. solve - [ ] 162. string_to_md5 - [x] 163. generate_integers diff --git a/WIP/029-filter_by_prefix.py b/WIP/029-filter_by_prefix.py deleted file mode 100644 index eda689d..0000000 --- a/WIP/029-filter_by_prefix.py +++ /dev/null @@ -1,96 +0,0 @@ -from typing import cast, List, Dict, Set, Optional, Union -from nagini_contracts.contracts import * - - -@Pure -def InArray(a : List[int], x : int) -> bool: - Requires(Acc(list_pred(a), 1/2)) - return Exists(int, lambda d_0_i_: - ((((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))) and ((a)[d_0_i_]) == (x)))) - -@Pure -def starts__with(s : List[int], p : List[int], i : int) -> bool : - 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)) - Ensures(Implies(len(p) == i and len(s) >= len(p), Result())) - Ensures(Implies(len(s) < len(p), not Result())) - return len(s) >= len(p) and Forall(int, lambda x: Implies(x >= i and x < len(p), s[x] == p[x])) - -@Pure -def starts__with__fun(s : List[int], p : List[int], i : int) -> bool : - 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)) - # Ensures(Implies(len(p) == i, len(s) >= len(p) and Forall(int, lambda x: x >= i and x < len(p) and s[x] == p[x]) and Result())) - # Ensures(Implies(len(p) == i, Result() == starts__with(s,p, i))) - Ensures(Result() == starts__with(s, p, i)) - 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 - -@Pure -def implStarts__with(xs : List[List[int]], p : List[int], filtered : List[int], d_2_j : int) -> bool: - Requires(Acc(list_pred(xs), 1/2)) - Requires(Forall(xs, lambda x : Acc(list_pred(x), 1/2))) - Requires(Acc(list_pred(p), 1/2)) - Requires(Acc(list_pred(filtered), 1/2)) - Requires(0 <= d_2_j and d_2_j < len(xs)) - return Implies(starts__with(xs[d_2_j], p, 0), InArray(filtered, d_2_j)) - -def filter__by__prefix(xs : List[List[int]], p : List[int]) -> List[int]: - Requires(Acc(list_pred(xs))) - Requires(Acc(list_pred(p))) - Requires(Forall(xs, lambda x : Acc(list_pred(x)))) - Ensures(Acc(list_pred(p))) - Ensures(Acc(list_pred(xs))) - Ensures(Forall(xs, lambda x : Acc(list_pred(x)))) - Ensures(Acc(list_pred(Result()))) - Ensures(Forall(int, lambda d_2_j_: - 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)))) - 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)): - Invariant(Acc(list_pred(filtered))) - Invariant(Acc(list_pred(xs), 1/2)) - Invariant(Acc(list_pred(p), 1/2)) - Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(xs)))) - Invariant(Forall(xs, lambda x : Acc(list_pred(x)))) - Invariant(Forall(filtered, lambda i: - (i >= 0 and i < d_1_i_))) - Invariant(Forall(filtered, lambda i: - (starts__with(xs[i], p, 0), [[starts__with(xs[i], p, 0)]]))) - Invariant(Forall(int, lambda d_2_j_: - (Implies(((0) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_)), - implStarts__with(xs, p, filtered, 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__fun((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_)), - # [[starts__with(xs[d_2_j_], p, 0)]]))) - # 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(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/077-is_cube.py b/WIP/077-is_cube.py index 14be67b..7ea670a 100644 --- a/WIP/077-is_cube.py +++ b/WIP/077-is_cube.py @@ -10,21 +10,28 @@ def cube__root(N : int) -> int: # Ensures(Forall(int, lambda d_0_r_: Implies(0 <= d_0_r_ and d_0_r_ < r, cube(d_0_r_) < N))) r = int(0) # type : int r = 0 - Assert(Forall(int, lambda x: x < (x + 1))) + # Assert(Forall(int, lambda x: x < (x + 1))) # Assert(Forall(int, lambda x: Implies(0 <= x and x < N, x * x < (x + 1) * (x + 1)))) - Assert(Forall(int, lambda x: x * x * x < (x + 1) * (x + 1) * (x + 1))) + # Assert(Forall(int, lambda x: x * x * x < (x + 1) * (x + 1) * (x + 1))) # Assert(Forall(int, lambda x: (Implies(0 <= x and x < N, cube(x) < cube(x + 1)), [[cube(x)]]))) while (cube((r) + (1))) <= (N): Invariant(N >= 0) Invariant(r >= 0 and r <= N) Invariant((cube(r)) <= (N)) - # Invariant(Forall(int, lambda x: (Implies(0 <= x, cube(x) < cube(x + 1)), [[cube(x)]]))) - # Invariant(Forall(int, lambda x: (Implies(0 <= x and x < r, cube(x) < cube(r)), [[cube(x) < cube(r)]]))) + 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 d_0_r_: (Implies(0 <= d_0_r_ and d_0_r_ < r, cube(d_0_r_) < N), [[cube(d_0_r_)]]))) r = (r) + (1) return r +@Pure +def cube_less(a : int, b : int) -> bool: + Requires(a >= 0) + Requires(b >= 0) + return cube(a) < cube(b) def is__cube(n : int) -> bool: Requires(n >= (0)) diff --git a/WIP/087-get_row.py b/WIP/087-get_row.py new file mode 100644 index 0000000..5e929f0 --- /dev/null +++ b/WIP/087-get_row.py @@ -0,0 +1,64 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def InArray(lst : List[Tuple[int, int]], i1 : int, j1 : int) -> bool: + Requires(Acc(list_pred(lst))) + return Exists(int, lambda i: i >= 0 and i < len(lst) and lst[i] == (i1, j1)) + +def get_row(lst : List[List[int]], x : int) -> List[Tuple[int, int]]: + Requires(Acc(list_pred(lst), 1/2)) + Requires(Forall(lst, lambda x: Acc(list_pred(x), 1/2))) + Ensures(Acc(list_pred(lst), 1/2)) + Ensures(Forall(lst, lambda x: Acc(list_pred(x), 1/2))) + Ensures(Acc(list_pred(Result()), 1/2)) + Ensures(Forall(int, lambda i: + Implies(i >= 0 and i < len(Result()), 0 <= Result()[i][0] and Result()[i][0] < len(lst) and + 0 <= Result()[i][1] and Result()[i][1] < len(lst[Result()[i][0]]) and + lst[Result()[i][0]][Result()[i][1]] == x))) + Ensures(Forall(int, lambda i: + Implies(i >= 0 and i < len(lst), + Forall(int, lambda j: + Implies(j >= 0 and j < len(lst[i]) and lst[i][j] == x, + InArray(Result(), i, j)))))) + + pos : List[Tuple[int, int]] = [] + i = 0 + while i < len(lst): + Invariant(Acc(list_pred(lst), 1/2)) + Invariant(Forall(lst, lambda x: Acc(list_pred(x), 1/2))) + Invariant(Acc(list_pred(pos))) + Invariant(0 <= i and i <= len(lst)) + Invariant(Forall(int, lambda j: + (Implies(j >= 0 and j < len(pos), 0 <= pos[j][0] and pos[j][0] < len(lst) and + 0 <= pos[j][1] and pos[j][1] < len(lst[pos[j][0]]) and + lst[pos[j][0]][pos[j][1]] == x)))) + Invariant(Forall(int, lambda i1: + Implies(i1 >= 0 and i1 < i, + Forall(int, lambda j1: + Implies(j1 >= 0 and j1 < len(lst[i]) and lst[i1][j1] == x, + InArray(pos, i1, j1)))))) + j = 0 + while j < len(lst[i]): + Invariant(Acc(list_pred(lst), 1/2)) + Invariant(Forall(lst, lambda x: Acc(list_pred(x), 1/2))) + Invariant(Acc(list_pred(pos))) + Invariant(0 <= i and i < len(lst)) + Invariant(0 <= j and j <= len(lst[i])) + Invariant(Forall(int, lambda j: + (Implies(j >= 0 and j < len(pos), 0 <= pos[j][0] and pos[j][0] < len(lst) and + 0 <= pos[j][1] and pos[j][1] < len(lst[pos[j][0]]) and + lst[pos[j][0]][pos[j][1]] == x)))) + Invariant(Forall(int, lambda i1: + Implies(i1 >= 0 and i1 < i, + Forall(int, lambda j1: + Implies(j1 >= 0 and j1 < len(lst[i]) and lst[i1][j1] == x, + InArray(pos, i1, j1)))))) + Invariant(Forall(int, lambda j1: + Implies(j1 >= 0 and j1 < j and lst[i][j1] == x, + InArray(pos, i, j1)))) + if lst[i][j] == x: + pos = pos + [(i, j)] + j += 1 + i += 1 + return pos \ No newline at end of file diff --git a/WIP/158-find_max.py b/WIP/158-find_max.py new file mode 100644 index 0000000..1a26da4 --- /dev/null +++ b/WIP/158-find_max.py @@ -0,0 +1,112 @@ +from typing import List +from nagini_contracts.contracts import * + +@Pure +def withinRange(x : int) -> bool: + return 97 <= x and x <= 122 + +@Pure +def withinRangeString(s : List[int]) -> bool: + Requires(Acc(list_pred(s), 1/2)) + 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)) + 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(withinRangeString(s)) + Requires(0 <= i and i <= j and j <= len(s)) + Requires(withinRange(c)) + if i == j: + return False + else: + return s[j - 1] == c or contains_char(s, c, i, j - 1) + +@Pure +def count_chars_inter(s : List[int], c : int) -> int: + Requires(Acc(list_pred(s), 1/2)) + Requires(withinRangeString(s)) + Requires(((97) <= (c)) and ((c) <= (123))) + 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) + +@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(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(withinRangeString(s1)) + Requires(withinRangeString(s2)) + Requires(withinRange(x)) + Requires(len(s1) == len(s2)) + Requires(Forall(int, lambda x: Implies(0 <= x and x < len(s1), s1[x] == s2[x]))) + 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((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(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(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]) + d_3_i_ = int(0) # type : int + d_3_i_ = 1 + # Assert(EqArrays(strings[0], s)) + # Assert(Exists(int, lambda x: x >= 0 and x < d_3_i_ and (len(strings[x]) == len(s)))) + # 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(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)) + # Assert(compare_strings(strings[0], s)) + # while (d_3_i_) < (len(strings)): + # Invariant(Acc(list_pred(strings), 1/2)) + # Invariant(Forall(strings, lambda x : Acc(list_pred(x), 1/2))) + # Invariant(Forall(strings, lambda y: withinRangeString(y))) + # Invariant(Forall(int, lambda y: + # (Implies(y >= 0 and y < len(strings), withinRangeString(strings[y])), + # [[]]))) + # Invariant(((1) <= (d_3_i_)) and ((d_3_i_) <= (len(strings)))) + # Invariant(Acc(list_pred(s), 1/2)) + # Invariant(withinRangeString(s)) + # # Invariant(Exists(strings, lambda x: EqArrays(x, s))) + # # Invariant(Forall(int, lambda x: + # # (Implies(x >= 0 and x < d_3_i_, compare_strings(strings[x], s)), [[compare_strings(strings[x], s)]]))) + # if (count_chars_inter((strings)[d_3_i_], 123)) > (count_chars_inter(s, 123)): + # s = list((strings)[d_3_i_]) + # # Assert(Forall(int, lambda x: + # # (Implies(x >= 0 and x <= d_3_i_, compare_strings(strings[x], s)), [[compare_strings(strings[x], s)]]))) + # # Assert(Forall(int, lambda x: + # # (Implies(x >= 0 and x <= d_3_i_, compare_strings(strings[x], s)), [[compare_strings(strings[x], s)]]))) + # d_3_i_ = (d_3_i_) + (1) + return s From 5be9281f3dd714433200ac42d15f4b565c1bb6fc Mon Sep 17 00:00:00 2001 From: alex28sh Date: Mon, 2 Sep 2024 11:12:52 +0200 Subject: [PATCH 5/6] fixes? --- Bench/112-reverse_delete.py | 2 +- WIP/087-get_row.py | 40 ++++++++++++++++++------------------- public/scripts/test-all.sh | 1 + public/scripts/test-new.sh | 1 + 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/Bench/112-reverse_delete.py b/Bench/112-reverse_delete.py index 906aa00..20324af 100644 --- a/Bench/112-reverse_delete.py +++ b/Bench/112-reverse_delete.py @@ -77,5 +77,5 @@ def is__palindrome__fun(text : List[int]) -> bool: def is__palindrome__pred(s : List[int]) -> bool : Requires(Acc(list_pred(s), 1/2)) 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_)])), [[]])) + (not (((0) <= (d_10_k_)) and ((d_10_k_) < (len(s)))) or (((s)[d_10_k_]) == ((s)[((len(s)) - (1)) - (d_10_k_)])))) diff --git a/WIP/087-get_row.py b/WIP/087-get_row.py index 5e929f0..ef46b9b 100644 --- a/WIP/087-get_row.py +++ b/WIP/087-get_row.py @@ -16,11 +16,11 @@ def get_row(lst : List[List[int]], x : int) -> List[Tuple[int, int]]: Implies(i >= 0 and i < len(Result()), 0 <= Result()[i][0] and Result()[i][0] < len(lst) and 0 <= Result()[i][1] and Result()[i][1] < len(lst[Result()[i][0]]) and lst[Result()[i][0]][Result()[i][1]] == x))) - Ensures(Forall(int, lambda i: - Implies(i >= 0 and i < len(lst), - Forall(int, lambda j: - Implies(j >= 0 and j < len(lst[i]) and lst[i][j] == x, - InArray(Result(), i, j)))))) + # Ensures(Forall(int, lambda i: + # Implies(i >= 0 and i < len(lst), + # Forall(int, lambda j: + # Implies(j >= 0 and j < len(lst[i]) and lst[i][j] == x, + # InArray(Result(), i, j)))))) pos : List[Tuple[int, int]] = [] i = 0 @@ -32,12 +32,12 @@ def get_row(lst : List[List[int]], x : int) -> List[Tuple[int, int]]: Invariant(Forall(int, lambda j: (Implies(j >= 0 and j < len(pos), 0 <= pos[j][0] and pos[j][0] < len(lst) and 0 <= pos[j][1] and pos[j][1] < len(lst[pos[j][0]]) and - lst[pos[j][0]][pos[j][1]] == x)))) - Invariant(Forall(int, lambda i1: - Implies(i1 >= 0 and i1 < i, - Forall(int, lambda j1: - Implies(j1 >= 0 and j1 < len(lst[i]) and lst[i1][j1] == x, - InArray(pos, i1, j1)))))) + lst[pos[j][0]][pos[j][1]] == x), [[]]))) + # Invariant(Forall(int, lambda i1: + # Implies(i1 >= 0 and i1 < i, + # Forall(int, lambda j1: + # Implies(j1 >= 0 and j1 < len(lst[i]) and lst[i1][j1] == x, + # InArray(pos, i1, j1)))))) j = 0 while j < len(lst[i]): Invariant(Acc(list_pred(lst), 1/2)) @@ -48,15 +48,15 @@ def get_row(lst : List[List[int]], x : int) -> List[Tuple[int, int]]: Invariant(Forall(int, lambda j: (Implies(j >= 0 and j < len(pos), 0 <= pos[j][0] and pos[j][0] < len(lst) and 0 <= pos[j][1] and pos[j][1] < len(lst[pos[j][0]]) and - lst[pos[j][0]][pos[j][1]] == x)))) - Invariant(Forall(int, lambda i1: - Implies(i1 >= 0 and i1 < i, - Forall(int, lambda j1: - Implies(j1 >= 0 and j1 < len(lst[i]) and lst[i1][j1] == x, - InArray(pos, i1, j1)))))) - Invariant(Forall(int, lambda j1: - Implies(j1 >= 0 and j1 < j and lst[i][j1] == x, - InArray(pos, i, j1)))) + lst[pos[j][0]][pos[j][1]] == x), [[]]))) + # Invariant(Forall(int, lambda i1: + # Implies(i1 >= 0 and i1 < i, + # Forall(int, lambda j1: + # Implies(j1 >= 0 and j1 < len(lst[i]) and lst[i1][j1] == x, + # InArray(pos, i1, j1)))))) + # Invariant(Forall(int, lambda j1: + # (Implies(j1 >= 0 and j1 < j and lst[i][j1] == x, + # InArray(pos, i, j1)), [[InArray(pos, i, j1)]]))) if lst[i][j] == x: pos = pos + [(i, j)] j += 1 diff --git a/public/scripts/test-all.sh b/public/scripts/test-all.sh index bc8a877..09ef02b 100755 --- a/public/scripts/test-all.sh +++ b/public/scripts/test-all.sh @@ -13,6 +13,7 @@ echo "Running Nagini on Python files in $DIRECTORY" for file in "$DIRECTORY"/*.py; do if [[ -f "$file" ]]; then echo "Running Nagini on $file" + rm -rf .mypy_cache timeout "$TIMEOUT_DURATION" nagini "$file" else echo "No Python files found in $DIRECTORY" diff --git a/public/scripts/test-new.sh b/public/scripts/test-new.sh index 50a00b6..d5e8591 100755 --- a/public/scripts/test-new.sh +++ b/public/scripts/test-new.sh @@ -32,6 +32,7 @@ do if [[ $f == *.py ]]; then file_no=$((file_no+1)) echo "Running dafny on $(basename "$f") ($file_no/$file_count)" + rm -rf .mypy_cache timeout "$TIMEOUT_DURATION" nagini "$f" fi fi From db15db6775ec6d206fe0967b6deb773a57009f90 Mon Sep 17 00:00:00 2001 From: alex28sh Date: Mon, 2 Sep 2024 11:29:04 +0200 Subject: [PATCH 6/6] remove 112 --- WIP/087-get_row.py | 4 ++-- Bench/112-reverse_delete.py => WIP/122-reverse_delete.py | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) rename Bench/112-reverse_delete.py => WIP/122-reverse_delete.py (99%) diff --git a/WIP/087-get_row.py b/WIP/087-get_row.py index ef46b9b..f31d84f 100644 --- a/WIP/087-get_row.py +++ b/WIP/087-get_row.py @@ -32,7 +32,7 @@ def get_row(lst : List[List[int]], x : int) -> List[Tuple[int, int]]: Invariant(Forall(int, lambda j: (Implies(j >= 0 and j < len(pos), 0 <= pos[j][0] and pos[j][0] < len(lst) and 0 <= pos[j][1] and pos[j][1] < len(lst[pos[j][0]]) and - lst[pos[j][0]][pos[j][1]] == x), [[]]))) + lst[pos[j][0]][pos[j][1]] == x)))) # Invariant(Forall(int, lambda i1: # Implies(i1 >= 0 and i1 < i, # Forall(int, lambda j1: @@ -48,7 +48,7 @@ def get_row(lst : List[List[int]], x : int) -> List[Tuple[int, int]]: Invariant(Forall(int, lambda j: (Implies(j >= 0 and j < len(pos), 0 <= pos[j][0] and pos[j][0] < len(lst) and 0 <= pos[j][1] and pos[j][1] < len(lst[pos[j][0]]) and - lst[pos[j][0]][pos[j][1]] == x), [[]]))) + lst[pos[j][0]][pos[j][1]] == x)))) # Invariant(Forall(int, lambda i1: # Implies(i1 >= 0 and i1 < i, # Forall(int, lambda j1: diff --git a/Bench/112-reverse_delete.py b/WIP/122-reverse_delete.py similarity index 99% rename from Bench/112-reverse_delete.py rename to WIP/122-reverse_delete.py index 20324af..b546d7c 100644 --- a/Bench/112-reverse_delete.py +++ b/WIP/122-reverse_delete.py @@ -78,4 +78,3 @@ def is__palindrome__pred(s : List[int]) -> bool : Requires(Acc(list_pred(s), 1/2)) 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_)])))) -