diff --git a/Bench/000-has-close-elements.py b/Bench/000-has-close-elements.py new file mode 100644 index 0000000..7970ab7 --- /dev/null +++ b/Bench/000-has-close-elements.py @@ -0,0 +1,70 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + + +@Pure +def abs_value(val: int) -> int: + Ensures(Implies(val < 0, Result() == -val)) + Ensures(Implies(val >= 0, Result() == val)) + + if val < 0: + return -val + else: + return val + + +@Pure +def abs1(x: int, threshold: int) -> bool: + return x >= threshold or x <= -threshold + +@Pure +def fn(x: int, numbers: List[int], threshold: int) -> bool: + Requires(threshold > 0) + Requires(Acc(list_pred(numbers))) + Requires(x >= 0 and x < len(numbers)) + return Forall(range(len(numbers)), lambda y : + x == y or + abs1(numbers[x] - numbers[y], threshold) + ) + + +def has_close_elements(numbers: List[int], threshold: int) -> bool: + Requires(threshold > 0) + Requires(Acc(list_pred(numbers))) + Ensures(Implies(Result() != True, Forall(range(len(numbers)), lambda x : + fn(x, numbers, threshold) + ))) + + + flag = False + i = 0 + while i < len(numbers): + Invariant(Acc(list_pred(numbers))) + Invariant(0 <= i and i <= len(numbers)) + Invariant(Implies(flag != True, + Forall(range(i), lambda x : fn(x, numbers, threshold)))) + + j = 0 + while j < len(numbers): + Invariant(Acc(list_pred(numbers))) + Invariant(0 <= i and i < len(numbers)) + Invariant(0 <= j and j <= len(numbers)) + + Invariant( + Implies( + flag != True, + Forall(range(i), lambda x : fn(x, numbers, threshold)) and + Forall(range(j), lambda y : i == y or abs1(numbers[i] - numbers[y], threshold)) + ) + ) + + + if i != j: + distance = abs_value(numbers[i] - numbers[j]) + if distance < threshold: + flag = True + + j += 1 + i += 1 + + return flag \ No newline at end of file diff --git a/Bench/003-below-zero.py b/Bench/003-below-zero.py new file mode 100644 index 0000000..4df47be --- /dev/null +++ b/Bench/003-below-zero.py @@ -0,0 +1,39 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def psum(i : int, j : int, s : List[int]) -> int : + Requires(Acc(list_pred(s))) + Requires(0 <= i and i <= j and j <= len(s)) + if i == j: + return 0 + else: + return (s)[j - 1] + (psum(i, j - 1, s)) + +def below__zero(ops : List[int]) -> bool: + Requires(Acc(list_pred(ops))) + Ensures(Acc(list_pred(ops))) + Ensures(not (Result()) or (Forall(int, lambda d_1_i_: + not (((0) <= (d_1_i_)) and ((d_1_i_) <= (len(ops)))) or ((psum(0, d_1_i_, ops)) >= (0))))) + Ensures(not (not(Result())) or (Exists(int, lambda d_2_i_: + (((0) <= (d_2_i_)) and ((d_2_i_) <= (len(ops)))) and ((psum(0, d_2_i_, ops)) < (0))))) + res = False # type : bool + d_3_balance_ = int(0) # type : int + d_3_balance_ = 0 + d_4_i_ = int(0) # type : int + d_4_i_ = 0 + while (d_4_i_) < (len(ops)): + Invariant(Acc(list_pred(ops))) + Invariant(((0) <= (d_4_i_)) and ((d_4_i_) <= (len(ops)))) + Invariant((d_3_balance_) == (psum(0, d_4_i_, ops))) + Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(ops)))) or ((psum(0, d_2_i_ + 1, ops)) == (psum(0, d_2_i_, ops) + ops[d_2_i_])), [[psum(0, d_2_i_ + 1, ops)]]))) + Invariant(Forall(int, lambda d_5_j_: + (not (((0) <= (d_5_j_)) and ((d_5_j_) <= (d_4_i_))) or ((psum(0, d_5_j_, ops)) >= (0)), [[psum(0, d_5_j_, ops)]]))) + Assert((psum(0, (d_4_i_) + (1), ops)) == ((psum(0, d_4_i_, ops)) + ((ops)[d_4_i_]))) + d_3_balance_ = (d_3_balance_) + ((ops)[d_4_i_]) + if (d_3_balance_) < (0): + res = False + return res + d_4_i_ = (d_4_i_) + (1) + res = True + return res diff --git a/Bench/005-intersperse.py b/Bench/005-intersperse.py new file mode 100644 index 0000000..8e08e4b --- /dev/null +++ b/Bench/005-intersperse.py @@ -0,0 +1,30 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def intersperse(numbers: List[int], delimiter: int) -> List[int]: + Requires(Acc(list_pred(numbers))) + Ensures(Acc(list_pred(numbers))) + Ensures(Acc(list_pred(Result()))) + Ensures(Implies(len(numbers) != 0, len(Result()) == len(numbers) * 2 - 1)) + Ensures(Implies(len(numbers) == 0, len(Result()) == 0)) + Ensures(Forall(range(len(Result())), lambda i: i % 2 == 1 or Result()[i] == numbers[i // 2])) + Ensures(Forall(range(len(Result())), lambda i: i % 2 == 0 or Result()[i] == delimiter)) + + res = [] # type: List[int] + if len(numbers) != 0: + i = 0 + while i + 1 < len(numbers): + Invariant(Acc(list_pred(numbers))) + Invariant(Acc(list_pred(res))) + Invariant(0 <= i and i < len(numbers)) + Invariant(len(res) == 2 * i) + Invariant(Forall(range(len(res)), lambda i: i % 2 == 1 or res[i] == numbers[i // 2])) + Invariant(Forall(range(len(res)), lambda i: i % 2 == 0 or res[i] == delimiter)) + + res.append(numbers[i]) + res.append(delimiter) + i += 1 + + res.append(numbers[i]) + + return res \ No newline at end of file diff --git a/Bench/008-sum-product.py b/Bench/008-sum-product.py new file mode 100644 index 0000000..9fb33a0 --- /dev/null +++ b/Bench/008-sum-product.py @@ -0,0 +1,45 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def psum(i : int, j : int, s : List[int]) -> int : + Requires(Acc(list_pred(s))) + Requires(0 <= i and i <= j and j <= len(s)) + if i == j: + return 0 + else: + return (s)[j - 1] + (psum(i, j - 1, s)) + +@Pure +def prod(i : int, j : int, s : List[int]) -> int : + Requires(Acc(list_pred(s))) + Requires(0 <= i and i <= j and j <= len(s)) + if i == j: + return 1 + else: + return (s)[j - 1] * (prod(i, j - 1, s)) + +def sum__product(numbers : List[int]) -> Tuple[int, int]: + Requires(Acc(list_pred(numbers))) + Ensures(Acc(list_pred(numbers))) + Ensures((Result()[0]) == (psum(0, len(numbers), numbers))) + Ensures((Result()[1]) == (prod(0, len(numbers), numbers))) + s = int(0) # type : int + p = int(0) # type : int + s = 0 + p = 1 + d_2_i_ = int(0) # type : int + d_2_i_ = 0 + while (d_2_i_) < (len(numbers)): + Invariant(Acc(list_pred(numbers))) + Invariant(((0) <= (d_2_i_)) and ((d_2_i_) <= (len(numbers)))) + Invariant((s) == (psum(0, d_2_i_, numbers))) + Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(numbers)))) or ((psum(0, d_2_i_ + 1, numbers)) == (psum(0, d_2_i_, numbers) + numbers[d_2_i_])), [[psum(0, d_2_i_ + 1, numbers)]]))) + Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(numbers)))) or ((prod(0, d_2_i_ + 1, numbers)) == (prod(0, d_2_i_, numbers) * numbers[d_2_i_])), [[prod(0, d_2_i_ + 1, numbers)]]))) + Invariant((p) == (prod(0, d_2_i_, numbers))) + Assert((psum(0, (d_2_i_) + (1), numbers)) == ((psum(0, d_2_i_, numbers) + (numbers)[d_2_i_]))) + s = (s) + ((numbers)[d_2_i_]) + Assert((prod(0, d_2_i_ + 1, numbers)) == ((prod(0, d_2_i_, numbers)) * ((numbers)[d_2_i_]))) + p = (p) * ((numbers)[d_2_i_]) + d_2_i_ = (d_2_i_) + (1) + return s, p \ No newline at end of file diff --git a/Bench/009-rolling-max.py b/Bench/009-rolling-max.py new file mode 100644 index 0000000..3ba7808 --- /dev/null +++ b/Bench/009-rolling-max.py @@ -0,0 +1,39 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def getVal(mx: Optional[int]) -> int: + Requires(mx is not None) + return mx + +def rolling_max(numbers: List[int]) -> List[int]: + Requires(Acc(list_pred(numbers))) + Ensures(Acc(list_pred(numbers))) + Ensures(Acc(list_pred(Result()))) + Ensures(len(Result()) == len(numbers)) + Ensures(Forall(range(len(numbers)), lambda i: numbers[i] <= Result()[i])) + Ensures(Forall(range(len(numbers) - 1), lambda i: Result()[i] <= Result()[i + 1])) + + running_max = None # type: Optional[int] + result = [] # type: List[int] + + i = 0 + while i < len(numbers): + Invariant(Acc(list_pred(numbers))) + Invariant(Acc(list_pred(result))) + Invariant(0 <= i and i <= len(numbers)) + Invariant(len(result) == i) + Invariant(Forall(range(i), lambda i1: numbers[i1] <= result[i1])) + Invariant(Old(running_max) is None or ((Old(running_max) is not None) and (getVal(Old(running_max)) <= getVal(running_max)))) + Invariant(Implies(len(result) > 0, running_max is not None)) + Invariant(Implies(len(result) > 0, result[-1] == getVal(running_max))) + Invariant(Forall(range(i - 1), lambda i1: result[i1] <= result[i1 + 1])) + + n = numbers[i] + if running_max is None or running_max < n: + running_max = n + + result.append(running_max) + i += 1 + + return result \ No newline at end of file diff --git a/Bench/010-is_palindrome.py b/Bench/010-is_palindrome.py new file mode 100644 index 0000000..55c15d8 --- /dev/null +++ b/Bench/010-is_palindrome.py @@ -0,0 +1,95 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +def is__palindrome(start : int, s : List[int]) -> bool: + Requires(Acc(list_pred(s), 1/2)) + Requires((len(s)) > (0)) + Requires(((0) <= (start)) and ((start) < (len(s)))) + Ensures(Acc(list_pred(s), 1/2)) + Ensures(((0) <= (start)) and ((start) < (len(s)))) + Ensures((len(s)) > (0)) + Ensures((Result()) == (Forall(int, lambda d_0_k_: + not (((start) <= (d_0_k_)) and ((d_0_k_) < (len(s)))) or (((s)[d_0_k_]) == ((s)[((len(s)) - (1)) - (d_0_k_ - start)]))))) + Ensures(Result() == is__palindrome__fun(start, s)) + d_1_i_ = int(0) # type : int + d_1_i_ = start + d_2_j_ = int(0) # type : int + d_2_j_ = (len(s)) - (1) + while (d_1_i_) < (d_2_j_): + Invariant(Acc(list_pred(s), 1/2)) + Invariant(((0) <= (start)) and ((start) < (len(s)))) + Invariant(d_1_i_ <= d_2_j_ + 1) + Invariant(((start) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) + Invariant(((start) <= (d_2_j_)) and ((d_2_j_) < (len(s)))) + Invariant((d_2_j_ - start) == (((len(s)) - (d_1_i_)) - (1))) + Invariant(Forall(int, lambda d_3_k_: + not (((start) <= (d_3_k_)) and ((d_3_k_) < (d_1_i_))) or (((s)[d_3_k_]) == ((s)[((len(s)) - (1)) - (d_3_k_ - start)])))) + if ((s)[d_1_i_]) != ((s)[d_2_j_]): + return False + d_1_i_ = (d_1_i_) + (1) + d_2_j_ = (d_2_j_) - (1) + return True + +@Pure +def is__palindrome__fun(start : int, s : List[int]) -> bool : + Requires(Acc(list_pred(s), 1/2)) + Requires(0 <= start and start < len(s)) + return Forall(int, lambda d_4_k_: + not (((start) <= (d_4_k_)) and ((d_4_k_) < (len(s)))) or (((s)[d_4_k_]) == ((s)[((len(s)) - (1)) - (d_4_k_ - start)]))) + +@Pure +def starts__with(result : List[int], s : List[int]) -> bool : + Requires(Acc(list_pred(s), 1/2)) + Requires(Acc(list_pred(result), 1/2)) + return ((len(result)) >= (len(s))) and (Forall(int, lambda d_5_k_: + not (((0) <= (d_5_k_)) and ((d_5_k_) < (len(s)))) or (((result)[d_5_k_]) == ((s)[d_5_k_])))) + +def make__palindrome(s : List[int]) -> List[int]: + Requires(Acc(list_pred(s))) + Ensures(Acc(list_pred(s))) + Ensures(Acc(list_pred(Result()))) + Ensures((len(Result())) <= ((2) * (len(s)))) + Ensures(len(Result()) == 0 or is__palindrome__fun(0, Result())) + Ensures(starts__with(Result(), s)) + result = list([int(0)] * 0) # type : List[int] + if (len(s)) == (0): + result = [] + return result + d_6_beginning__of__suffix_ = int(0) # type : int + d_8_flag_ = is__palindrome(d_6_beginning__of__suffix_, s) # type : bool + while not(d_8_flag_): + Invariant(Acc(list_pred(s))) + Invariant(len(s) > 0) + Invariant((((d_6_beginning__of__suffix_) >= (0)) and (((d_6_beginning__of__suffix_) + (1)) < (len(s)))) or ((d_8_flag_) and (((d_6_beginning__of__suffix_) >= (0)) and ((d_6_beginning__of__suffix_) < (len(s)))))) + Invariant(Implies(d_8_flag_, is__palindrome__fun(d_6_beginning__of__suffix_, s))) + d_6_beginning__of__suffix_ = (d_6_beginning__of__suffix_) + (1) + d_8_flag_ = is__palindrome(d_6_beginning__of__suffix_, s) + d_10_reversed_ = reverse(d_6_beginning__of__suffix_, s) # type : List[int] + result = (s) + (d_10_reversed_) + return result + +def reverse(end : int, str : List[int]) -> List[int]: + Requires(Acc(list_pred(str), 1/2)) + Requires(0 <= end and end < len(str)) + Ensures(Acc(list_pred(str), 1/2)) + Ensures(0 <= end and end < len(str)) + Ensures(Acc(list_pred(Result()))) + Ensures(str == Old(str)) + Ensures((len(Result())) == (end)) + Ensures(Forall(int, lambda d_11_k_: + not (((0) <= (d_11_k_)) and ((d_11_k_) < (end))) or (((Result())[d_11_k_]) == ((str)[((end) - (1)) - (d_11_k_)])))) + rev = list([int(0)] * 0) # type : List[int] + rev = [] + d_12_i_ = int(0) # type : int + d_12_i_ = 0 + while (d_12_i_) < (end): + Invariant(Acc(list_pred(str), 1/2)) + Invariant(Acc(list_pred(rev))) + Invariant(0 <= end and end < len(str)) + Invariant(((d_12_i_) >= (0)) and ((d_12_i_) <= (end))) + Invariant((len(rev)) == (d_12_i_)) + Invariant(Forall(int, lambda d_13_k_: + not (((0) <= (d_13_k_)) and ((d_13_k_) < (d_12_i_))) or (((rev)[d_13_k_]) == ((str)[(end - (1)) - (d_13_k_)])))) + rev = (rev) + [(str)[(end - (d_12_i_)) - (1)]] + d_12_i_ = (d_12_i_) + (1) + return rev diff --git a/Bench/011-string_xor.py b/Bench/011-string_xor.py new file mode 100644 index 0000000..82256f5 --- /dev/null +++ b/Bench/011-string_xor.py @@ -0,0 +1,49 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def xor(a : int, b : int) -> int: + Ensures((Result()) == ((0 if (a) == (b) else 1))) + result = int(0) # type : int + if (a) == (b): + result = 0 + else: + result = 1 + return result + +def string__xor(a : List[int], b : List[int]) -> List[int]: + Requires(Acc(list_pred(b))) + Requires(Acc(list_pred(a))) + Requires((len(a)) == (len(b))) + Requires(Forall(int, lambda d_0_i_: + not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(a)))) or ((((a)[d_0_i_]) == (0)) or (((a)[d_0_i_]) == (1))))) + Requires(Forall(int, lambda d_1_i_: + not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(b)))) or ((((b)[d_1_i_]) == (0)) or (((b)[d_1_i_]) == (1))))) + Ensures(Acc(list_pred(b))) + Ensures(Acc(list_pred(a))) + Ensures(Acc(list_pred(Result()))) + Ensures((len(a)) == (len(b))) + Ensures((len(Result())) == (len(a))) + Ensures(Forall(int, lambda d_2_i_: + not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(Result())))) or ((((Result())[d_2_i_]) == (0)) or (((Result())[d_2_i_]) == (1))))) + Ensures(Forall(int, lambda d_3_i_: + not (((0) <= (d_3_i_)) and ((d_3_i_) < (len(Result())))) or (((Result())[d_3_i_]) == ((0 if ((a)[d_3_i_]) == ((b)[d_3_i_]) else 1))))) + result = list([int(0)] * 0) # type : List[int] + d_4_i_ = int(0) # type : int + while (d_4_i_) < (len(a)): + Invariant(Acc(list_pred(b))) + Invariant(Acc(list_pred(a))) + Invariant(Acc(list_pred(result))) + Invariant((len(a)) == (len(b))) + Invariant(((d_4_i_) >= (0)) and ((d_4_i_) <= (len(a)))) + Invariant((len(result)) == (d_4_i_)) + Invariant(Forall(int, lambda d_0_i_: + not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(a)))) or ((((a)[d_0_i_]) == (0)) or (((a)[d_0_i_]) == (1))))) + Invariant(Forall(int, lambda d_1_i_: + not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(b)))) or ((((b)[d_1_i_]) == (0)) or (((b)[d_1_i_]) == (1))))) + Invariant(Forall(int, lambda d_5_j_: + not (((0) <= (d_5_j_)) and ((d_5_j_) < (d_4_i_))) or (((result)[d_5_j_]) == ((0 if ((a)[d_5_j_]) == ((b)[d_5_j_]) else 1))))) + d_6_bitResult_ = (0 if ((a)[d_4_i_]) == ((b)[d_4_i_]) else 1) + result = (result) + [d_6_bitResult_] + d_4_i_ = (d_4_i_) + (1) + return result \ No newline at end of file diff --git a/Bench/013-greatest-common-divisor.py b/Bench/013-greatest-common-divisor.py new file mode 100644 index 0000000..25b3309 --- /dev/null +++ b/Bench/013-greatest-common-divisor.py @@ -0,0 +1,17 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def greatest_common_divisor(a: int, b: int) -> int: + Requires(a != 0 or b != 0) + Ensures(Result() != 0) + + x = a + y = b + + while y != 0: + Invariant(x != 0 or y != 0) + temp = y + y = x % y + x = temp + + return x \ No newline at end of file diff --git a/Bench/020-find-closest-elements.py b/Bench/020-find-closest-elements.py new file mode 100644 index 0000000..e1adcae --- /dev/null +++ b/Bench/020-find-closest-elements.py @@ -0,0 +1,86 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def dist(a : int, b : int) -> int : + Ensures(Result() >= 0) + if (a) < (b): + return (b) - (a) + else: + return (a) - (b) + +def find__closest__elements(s : List[int]) -> Tuple[int, int]: + Requires(Acc(list_pred(s))) + Requires((len(s)) >= (2)) + Ensures(Acc(list_pred(s))) + Ensures(len(s) >= 2) + Ensures(Exists(int, lambda d_0_a_: + Exists(int, lambda d_1_b_: + ((0 <= d_0_a_ and d_0_a_ < d_1_b_ and d_1_b_ < len(s)) and ((Result()[0]) == ((s)[d_0_a_])) and (Result()[1]) == ((s)[d_1_b_]))))) + Ensures(Forall(int, lambda d_2_a_: + Forall(int, lambda d_3_b_: + Implies((0 <= d_2_a_ and (d_2_a_) < (len(s)) and 0 <= d_3_b_ and d_3_b_ < len(s)) and (d_2_a_ != d_3_b_), (dist(Result()[0], Result()[1])) <= (dist((s)[d_2_a_], (s)[d_3_b_])))))) + l = (s)[0] # type : int + h = (s)[1] # type : int + d_4_d_ = dist(l, h) # type : int + d_5_i_ = int(0) # type : int + d_5_i_ = 0 + Assert(Exists(int, lambda d_6_a_: + Exists(int, lambda d_7_b_: + ((0 <= d_6_a_ and (d_6_a_) < (d_7_b_) and d_7_b_ < len(s))) and ((l) == ((s)[d_6_a_])) and ((h) == ((s)[d_7_b_]))))) + while (d_5_i_) < (len(s)): + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_5_i_)) and ((d_5_i_) <= (len(s)))) + Invariant((d_4_d_) == (dist(l, h))) + Invariant((len(s)) >= (2)) + Invariant(Exists(int, lambda d_6_a_: + Exists(int, lambda d_7_b_: + ((0 <= d_6_a_ and d_6_a_ < d_7_b_ and d_7_b_ < len(s) and ((l) == ((s)[d_6_a_]))) and ((h) == ((s)[d_7_b_])))))) + Invariant(Forall(int, lambda x: + Forall(int, lambda y: + (Implies((0 <= x and x < len(s) and 0 <= y and y < len(s)), dist(s[x], s[y]) == dist(s[y], s[x])), [[dist(s[x], s[y]) == dist(s[y], s[x])]])))) + Invariant(Forall(int, lambda d_8_a_: + Forall(int, lambda d_9_b_: + (Implies((0 <= d_8_a_ and (d_8_a_) < (d_5_i_) and 0 <= d_9_b_ and d_9_b_ < len(s)) and (d_8_a_ != d_9_b_), (dist(l, h)) <= (dist((s)[d_8_a_], (s)[d_9_b_]))), [[dist((s)[d_8_a_], (s)[d_9_b_])]])))) + d_10_j_ = int(0) # type : int + d_10_j_ = (d_5_i_) + (1) + Assert(Forall(int, lambda d_8_a_: + Forall(int, lambda d_9_b_: + (Implies((0 <= d_8_a_ and (d_8_a_) < (d_5_i_) and 0 <= d_9_b_ and d_9_b_ < len(s)) and (d_8_a_ != d_9_b_), (dist(l, h)) <= (dist((s)[d_8_a_], (s)[d_9_b_]))), [[dist((s)[d_8_a_], (s)[d_9_b_])]])))) + Assert(Forall(int, lambda x: (Implies(x >= 0 and x < d_5_i_, dist(l, h) <= dist(s[x], s[d_5_i_])), [[dist(s[x], s[d_5_i_])]]))) + while (d_10_j_) < (len(s)): + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_5_i_)) and ((d_5_i_) < (len(s)))) + Invariant(((d_5_i_) < (d_10_j_)) and ((d_10_j_) <= (len(s)))) + Invariant((d_4_d_) == (dist(l, h))) + Invariant((len(s)) >= (2)) + Invariant(Exists(int, lambda d_11_a_: + Exists(int, lambda d_12_b_: + ((((0 <= d_11_a_ and d_11_a_ < d_12_b_ and d_12_b_ < len(s)) ) and ((l) == ((s)[d_11_a_]))) and ((h) == ((s)[d_12_b_])))))) + Invariant(Forall(int, lambda x: + Forall(int, lambda y: + (Implies((0 <= x and x < len(s) and 0 <= y and y < len(s)), dist(s[x], s[y]) == dist(s[y], s[x])), [[dist(s[x], s[y]) == dist(s[y], s[x])]])))) + Invariant(Forall(int, lambda x: (Implies(x >= 0 and x < d_5_i_, dist(s[d_5_i_], s[x]) == dist(s[x], s[d_5_i_])), [[dist(s[d_5_i_], s[x])]]))) + Invariant(Forall(int, lambda x: (Implies(x >= 0 and x < d_5_i_, Implies(dist(l, h) <= dist(s[x], s[d_5_i_]), dist(l, h) <= dist(s[d_5_i_], s[x]))), [[dist(s[d_5_i_], s[x])]]))) + Invariant(Forall(int, lambda x: (Implies(x >= 0 and x < d_5_i_, dist(l, h) <= dist(s[x], s[d_5_i_])), [[dist(s[x], s[d_5_i_])]]))) + Invariant(Forall(int, lambda x: (Implies(x >= 0 and x < d_5_i_, dist(l, h) <= dist(s[d_5_i_], s[x])), [[dist(s[d_5_i_], s[x])]]))) + Invariant(Forall(int, lambda d_13_a_: + Forall(int, lambda d_14_b_: + (Implies((((d_13_a_ == (d_5_i_) and d_5_i_ < d_14_b_ and d_14_b_ < d_10_j_))) and (d_13_a_ != d_14_b_), (dist(l, h)) <= (dist((s)[d_13_a_], (s)[d_14_b_]))), [[dist((s)[d_13_a_], (s)[d_14_b_])]])))) + Invariant(Forall(int, lambda d_13_a_: + Forall(int, lambda d_14_b_: + (Implies((((d_13_a_ == (d_5_i_) and 0 <= d_14_b_ and d_14_b_ < d_10_j_))) and (d_13_a_ != d_14_b_), (dist(l, h)) <= (dist((s)[d_13_a_], (s)[d_14_b_]))), [[dist((s)[d_13_a_], (s)[d_14_b_])]])))) + Invariant(Forall(int, lambda d_13_a_: + Forall(int, lambda d_14_b_: + (Implies(((0 <= d_13_a_ and (d_13_a_) < (d_5_i_) and 0 <= d_14_b_ and d_14_b_ < len(s))) and (d_13_a_ != d_14_b_), (dist(l, h)) <= (dist((s)[d_13_a_], (s)[d_14_b_]))), [[dist((s)[d_13_a_], (s)[d_14_b_])]])))) + if d_5_i_ != d_10_j_ and (dist((s)[d_5_i_], (s)[d_10_j_])) <= (d_4_d_): + l = (s)[d_5_i_] + h = (s)[d_10_j_] + d_4_d_ = dist(l, h) + d_10_j_ = (d_10_j_) + (1) + Assert(Forall(int, lambda d_8_a_: + Forall(int, lambda d_9_b_: + (Implies((0 <= d_8_a_ and (d_8_a_) <= (d_5_i_) and 0 <= d_9_b_ and d_9_b_ < len(s)) and (d_8_a_ != d_9_b_), (dist(l, h)) <= (dist((s)[d_8_a_], (s)[d_9_b_]))), [[dist((s)[d_8_a_], (s)[d_9_b_])]])))) + d_5_i_ = (d_5_i_) + (1) + return (l, h) + diff --git a/Bench/023-strlen.py b/Bench/023-strlen.py new file mode 100644 index 0000000..6685322 --- /dev/null +++ b/Bench/023-strlen.py @@ -0,0 +1,5 @@ +from nagini_contracts.contracts import * + +def strlen(s : str) -> int: + Ensures((Result()) == (len(s))) + return len(s) \ No newline at end of file diff --git a/Bench/024-largest-divisor.py b/Bench/024-largest-divisor.py new file mode 100644 index 0000000..184255c --- /dev/null +++ b/Bench/024-largest-divisor.py @@ -0,0 +1,21 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def largest__divisor(n : int) -> int: + Requires((n) > (1)) + Ensures(((1) <= (Result())) and ((Result()) < (n))) + Ensures(((n % Result())) == (0)) + Ensures(Forall(int, lambda d_0_k_: + not (((Result()) < (d_0_k_)) and ((d_0_k_) < (n))) or (((n % d_0_k_)) != (0)))) + d = int(0) # type : int + d = (n) - (1) + while (d) >= (1): + Invariant(((1) <= (d)) and ((d) < (n))) + Invariant(Forall(int, lambda d_1_k_: + not (((d) < (d_1_k_)) and ((d_1_k_) < (n))) or (((n % d_1_k_)) != (0)))) + if ((n % d)) == (0): + d = d + return d + d = (d) - (1) + d = 1 + return d \ No newline at end of file diff --git a/Bench/027-flip_case.py b/Bench/027-flip_case.py new file mode 100644 index 0000000..d4e6d88 --- /dev/null +++ b/Bench/027-flip_case.py @@ -0,0 +1,45 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def lower(c : int) -> bool : + return ((0) <= (c)) and ((c) <= (25)) + +@Pure +def upper(c : int) -> bool : + return ((26) <= (c)) and ((c) <= (51)) + +@Pure +def alpha(c : int) -> bool : + return (lower(c)) or (upper(c)) + +@Pure +def flip__char(c : int) -> int : + Ensures(lower(c) == upper(Result())) + Ensures(upper(c) == lower(Result())) + if lower(c): + return ((c) - (0)) + (26) + elif upper(c): + return ((c) + (0)) - (26) + elif True: + return c + +def flip__case(s : List[int]) -> List[int] : + Requires(Acc(list_pred(s))) + Ensures(Acc(list_pred(s))) + Ensures(Acc(list_pred(Result()))) + Ensures((len(Result())) == (len(s))) + Ensures(Forall(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len(s))), lower((s)[d_0_i_]) == upper((Result())[d_0_i_]))))) + Ensures(Forall(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len(s))), upper((s)[d_0_i_]) == lower((Result())[d_0_i_]))))) + res = list([int(0)] * len(s)) # type : List[int] + i = int(0) # type : int + while i < len(s): + Invariant(Acc(list_pred(s))) + Invariant(Acc(list_pred(res))) + Invariant(((0) <= (i)) and ((i) <= (len(s)))) + Invariant((len(res)) == (len(s))) + Invariant(Forall(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (i)), lower((s)[d_0_i_]) == upper((res)[d_0_i_]))))) + Invariant(Forall(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (i)), upper((s)[d_0_i_]) == lower((res)[d_0_i_]))))) + res[i] = flip__char(s[i]) + i = i + 1 + return res diff --git a/Bench/030-get-positive.py b/Bench/030-get-positive.py new file mode 100644 index 0000000..16d1936 --- /dev/null +++ b/Bench/030-get-positive.py @@ -0,0 +1,50 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def get__positive(l : List[int]) -> List[int]: + Requires(Acc(list_pred(l))) + Ensures(Acc(list_pred(l))) + Ensures(Acc(list_pred(Result()))) + Ensures(Forall(int, lambda d_0_i_: + not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(Result())))) or (((Result())[d_0_i_]) > (0)))) + Ensures((len(Result())) <= (len(l))) + Ensures(Forall(int, lambda d_1_i1_: + not (((d_1_i1_) >= (0)) and ((d_1_i1_) < (len(l)))) or (not (((l)[d_1_i1_]) > (0)) or (Exists(int, lambda d_2_i2_: + (((d_2_i2_) >= (0)) and ((d_2_i2_) < (len(Result())))) and (((Result())[d_2_i2_]) == ((l)[d_1_i1_]))))))) + Ensures(((len(Result())) == (0)) or (Forall(int, lambda d_3_i1_: + not (((d_3_i1_) >= (0)) and ((d_3_i1_) < (len(Result())))) or (Exists(int, lambda d_4_i2_: + (((d_4_i2_) >= (0)) and ((d_4_i2_) < (len(l)))) and (((l)[d_4_i2_]) == ((Result())[d_3_i1_]))))))) + result = list([0] * 0) # type : List[int] + d_5_i_ = int(0) # type : int + d_5_i_ = 0 + while (d_5_i_) < (len(l)): + Invariant(Acc(list_pred(result))) + Invariant(Acc(list_pred(l))) + Invariant(((d_5_i_) >= (0)) and ((d_5_i_) <= (len(l)))) + Invariant((len(result)) <= (d_5_i_)) + Invariant(Forall(int, lambda d_6_i1_: + not (((d_6_i1_) >= (0)) and ((d_6_i1_) < (len(result)))) or (((result)[d_6_i1_]) > (0)))) + Invariant(not ((d_5_i_) > (0)) or (not (((l)[(d_5_i_) - (1)]) > (0)) or (Exists(int, lambda d_7_i2_: + (((d_7_i2_) >= (0)) and ((d_7_i2_) < (len(result)))) and (((result)[d_7_i2_]) == ((l)[(d_5_i_) - (1)])))))) + Invariant(Forall(int, lambda d_9_i1_: + not (((d_9_i1_) >= (0)) and ((d_9_i1_) < (d_5_i_))) or (not (((l)[d_9_i1_]) > (0)) or (Exists(int, lambda d_10_i2_: + (((d_10_i2_) >= (0)) and ((d_10_i2_) < (len(result)))) and (((result)[d_10_i2_]) == ((l)[d_9_i1_]))))))) + Invariant(((len(result)) == (0)) or (Forall(int, lambda d_11_i1_: + not (((d_11_i1_) >= (0)) and ((d_11_i1_) < (len(result)))) or (Exists(int, lambda d_12_i2_: + (((d_12_i2_) >= (0)) and ((d_12_i2_) < (len(l)))) and (((l)[d_12_i2_]) == ((result)[d_11_i1_]))))))) + d_13_n_ = int(0) # type : int + d_13_n_ = (l)[d_5_i_] + if (d_13_n_) > (0): + d_17_res__prev_ = result + Assert(Forall(int, lambda d_14_i1_: + not (((d_14_i1_) >= (0)) and ((d_14_i1_) < (d_5_i_))) or (not (((l)[d_14_i1_]) > (0)) or (Exists(int, lambda d_15_i2_: + (((d_15_i2_) >= (0)) and ((d_15_i2_) < (len(result)))) and (((result)[d_15_i2_]) == ((l)[d_14_i1_]))))))) + result = (result) + ([d_13_n_]) + Assert(((result)[(len(result)) - (1)]) == (d_13_n_)) + Assert(Forall(int, lambda d_16_i1_: + not (((d_16_i1_) >= (0)) and ((d_16_i1_) < (len(d_17_res__prev_)))) or (((d_17_res__prev_)[d_16_i1_]) == ((result)[d_16_i1_])))) + Assert(Forall(int, lambda d_18_i1_: + not (((d_18_i1_) >= (0)) and ((d_18_i1_) < (d_5_i_))) or (not (((l)[d_18_i1_]) > (0)) or (Exists(int, lambda d_19_i2_: + (((d_19_i2_) >= (0)) and ((d_19_i2_) < (len(d_17_res__prev_)))) and (((d_17_res__prev_)[d_19_i2_]) == ((l)[d_18_i1_]))))))) + d_5_i_ = (d_5_i_) + (1) + return result diff --git a/Bench/031-is-prime.py b/Bench/031-is-prime.py new file mode 100644 index 0000000..f8af3e0 --- /dev/null +++ b/Bench/031-is-prime.py @@ -0,0 +1,23 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def is__prime(k : int) -> bool: + Requires((k) >= (2)) + Ensures(not (Result()) or (Forall(int, lambda d_0_i_: + not (((2) <= (d_0_i_)) and ((d_0_i_) < (k))) or ((k % d_0_i_) != (0))))) + Ensures(not (not(Result())) or (Exists(int, lambda d_1_j_: + (((2) <= (d_1_j_)) and ((d_1_j_) < (k))) and (((k % d_1_j_)) == (0))))) + result = False # type : bool + d_2_i_ = int(0) # type : int + d_2_i_ = 2 + result = True + while (d_2_i_) < (k): + Invariant(((2) <= (d_2_i_)) and ((d_2_i_) <= (k))) + Invariant(not (not(result)) or (Exists(int, lambda d_3_j_: + (((2) <= (d_3_j_)) and ((d_3_j_) < (d_2_i_))) and (((k % d_3_j_)) == (0))))) + Invariant(not (result) or (Forall(int, lambda d_4_j_: + not (((2) <= (d_4_j_)) and ((d_4_j_) < (d_2_i_))) or (((k % d_4_j_)) != (0))))) + if ((k % d_2_i_)) == (0): + result = False + d_2_i_ = (d_2_i_) + (1) + return result \ No newline at end of file diff --git a/Bench/033-sort_third.py b/Bench/033-sort_third.py new file mode 100644 index 0000000..cd6c090 --- /dev/null +++ b/Bench/033-sort_third.py @@ -0,0 +1,107 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def sort__third(a : List[int]) -> List[int]: + Requires(Acc(list_pred(a))) + Requires((len(a)) > (0)) + Ensures(Acc(list_pred(a))) + Ensures(Acc(list_pred(Result()))) + Ensures((len(Result())) == (len(a))) + Ensures(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(Result())))) and (((d_0_i_ % 3)) == (0))) and (((d_1_j_ % 3)) == (0))) or (((Result())[d_0_i_]) <= ((Result())[d_1_j_]))))) + Ensures(Forall(int, lambda d_2_i_: + not ((((0) <= (d_2_i_)) and ((d_2_i_) < (len(a)))) and (((d_2_i_ % 3)) != (0))) or (((Result())[d_2_i_]) == ((a)[d_2_i_])))) + sorted__even = list([int(0)] * 0) # type : List[int] + d_3_p_ = list([False] * 0) # type : List[bool] + d_3_p_ = list([]) + d_4_i_ = int(0) # type : int + d_4_i_ = 0 + while (d_4_i_) < (len(a)): + Invariant(Acc(list_pred(d_3_p_))) + Invariant(Acc(list_pred(sorted__even))) + Invariant(Acc(list_pred(a))) + Invariant(((0) <= (d_4_i_)) and ((d_4_i_) <= (len(a)))) + Invariant((len(d_3_p_)) == (d_4_i_)) + Invariant(Forall(int, lambda d_5_j_: + not (((0) <= (d_5_j_)) and ((d_5_j_) < (d_4_i_))) or (((d_3_p_)[d_5_j_]) == (((d_5_j_ % 3)) == (0))))) + d_3_p_ = (d_3_p_) + [((d_4_i_ % 3)) == (0)] + d_4_i_ = (d_4_i_) + (1) + sorted__even = SortSeqPred(a, d_3_p_) + return sorted__even + +def SortSeqPred(s : List[int], p : List[bool]) -> List[int]: + Requires(Acc(list_pred(p), 1/2)) + Requires(Acc(list_pred(s), 1/2)) + Requires((len(s)) == (len(p))) + Ensures(Acc(list_pred(p), 1/2)) + Ensures(Acc(list_pred(s), 1/2)) + Ensures(Acc(list_pred(Result()))) + Ensures((len(s)) == (len(p))) + Ensures((len(Result())) == (len(s))) + Ensures(Forall(int, lambda d_6_i_: + Forall(int, lambda d_7_j_: + not ((((((0) <= (d_6_i_)) and ((d_6_i_) < (d_7_j_))) and ((d_7_j_) < (len(Result())))) and ((p)[d_6_i_])) and ((p)[d_7_j_])) or (((Result())[d_6_i_]) <= ((Result())[d_7_j_]))))) + Ensures(Forall(int, lambda d_8_i_: + not ((((0) <= (d_8_i_)) and ((d_8_i_) < (len(s)))) and (not((p)[d_8_i_]))) or (((Result())[d_8_i_]) == ((s)[d_8_i_])))) + sorted = list([int(0)] * 0) # type : List[int] + sorted = list(s) + d_9_i_ = int(0) # type : int + d_9_i_ = 0 + while (d_9_i_) < (len(sorted)): + Invariant(Acc(list_pred(sorted))) + Invariant(Acc(list_pred(p), 1/2)) + Invariant(Acc(list_pred(s), 1/2)) + Invariant((len(s)) == (len(p))) + Invariant(((0) <= (d_9_i_)) and ((d_9_i_) <= (len(sorted)))) + Invariant((len(sorted)) == (len(s))) + Invariant(Forall(int, lambda d_14_j_: + not ((((0) <= (d_14_j_)) and ((d_14_j_) < (len(s)))) and (not((p)[d_14_j_]))) or (((sorted)[d_14_j_]) == ((s)[d_14_j_])))) + 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_))) and ((p)[d_10_j_])) and ((p)[d_11_k_])) or + (((sorted)[d_10_j_]) <= ((sorted)[d_11_k_])), [[(sorted)[d_11_k_]]])), [[sorted[d_10_j_]]]))) + Invariant(Forall(int, lambda d_12_j_: + (not ((((0) <= (d_12_j_)) and ((d_12_j_) < (d_9_i_))) and ((p)[d_12_j_])) or + (Forall(int, lambda d_13_k_: + (not ((((d_9_i_) <= (d_13_k_)) and ((d_13_k_) < (len(sorted)))) and ((p)[d_13_k_])) or + (((sorted)[d_12_j_]) <= ((sorted)[d_13_k_])), [[sorted[d_13_k_]]]))), [[(sorted)[d_12_j_]]]))) + if (p)[d_9_i_]: + d_15_minIndex_ = int(0) # type : int + d_15_minIndex_ = d_9_i_ + d_16_j_ = int(0) # type : int + d_16_j_ = (d_9_i_) + (1) + while (d_16_j_) < (len(sorted)): + Invariant(Acc(list_pred(sorted))) + Invariant(Acc(list_pred(p), 1/2)) + Invariant(Acc(list_pred(s), 1/2)) + Invariant((len(s)) == (len(p))) + Invariant((len(sorted)) == (len(s))) + Invariant(0 <= d_9_i_ and d_9_i_ < len(sorted)) + Invariant((((d_9_i_) <= (d_15_minIndex_)) and ((d_15_minIndex_) < (d_16_j_))) and ((d_16_j_) <= (len(sorted)))) + Invariant(p[d_9_i_]) + Invariant((p)[d_15_minIndex_]) + Invariant(Forall(int, lambda d_17_k_: + not ((((d_9_i_) <= (d_17_k_)) and ((d_17_k_) < (d_16_j_))) and ((p)[d_17_k_])) or (((sorted)[d_15_minIndex_]) <= ((sorted)[d_17_k_])))) + Invariant(Forall(int, lambda d_14_j_: + not ((((0) <= (d_14_j_)) and ((d_14_j_) < (len(s)))) and (not((p)[d_14_j_]))) or (((sorted)[d_14_j_]) == ((s)[d_14_j_])))) + 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_))) and ((p)[d_10_j_])) and ((p)[d_11_k_])) or + (((sorted)[d_10_j_]) <= ((sorted)[d_11_k_])), [[(sorted)[d_11_k_]]])), [[sorted[d_10_j_]]]))) + Invariant(Forall(int, lambda d_12_j_: + (not ((((0) <= (d_12_j_)) and ((d_12_j_) < (d_9_i_))) and ((p)[d_12_j_])) or + (Forall(int, lambda d_13_k_: + (not ((((d_9_i_) <= (d_13_k_)) and ((d_13_k_) < (len(sorted)))) and ((p)[d_13_k_])) or + (((sorted)[d_12_j_]) <= ((sorted)[d_13_k_])), [[sorted[d_13_k_]]]))), [[(sorted)[d_12_j_]]]))) + if ((p)[d_16_j_]) and (((sorted)[d_16_j_]) < ((sorted)[d_15_minIndex_])): + d_15_minIndex_ = d_16_j_ + d_16_j_ = (d_16_j_) + (1) + if (d_15_minIndex_) != (d_9_i_): + Assert((p)[d_15_minIndex_]) + Assert(p[d_9_i_]) + rhs0_ = (sorted)[d_9_i_] # type : int + (sorted)[d_9_i_] = (sorted)[d_15_minIndex_] + (sorted)[d_15_minIndex_] = rhs0_ + d_9_i_ = (d_9_i_) + (1) + return sorted \ No newline at end of file diff --git a/Bench/035-max-element.py b/Bench/035-max-element.py new file mode 100644 index 0000000..13bb39a --- /dev/null +++ b/Bench/035-max-element.py @@ -0,0 +1,26 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def max__element(l : List[int]) -> int: + Requires(Acc(list_pred(l))) + Requires((len(l)) > (0)) + Ensures(Acc(list_pred(l))) + Ensures(Forall(int, lambda d_0_i_: + not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(l)))) or (((l)[d_0_i_]) <= (Result())))) + Ensures(Exists(int, lambda d_1_i_: + (((d_1_i_) >= (0)) and ((d_1_i_) < (len(l)))) and (((l)[d_1_i_]) == (Result())))) + result = int(0) # type : int + result = (l)[0] + d_2_i_ = int(0) # type : int + d_2_i_ = 1 + while (d_2_i_) < (len(l)): + Invariant(Acc(list_pred(l))) + Invariant(((d_2_i_) >= (1)) and ((d_2_i_) <= (len(l)))) + Invariant(Forall(int, lambda d_3_i1_: + not (((d_3_i1_) >= (0)) and ((d_3_i1_) < (d_2_i_))) or (((l)[d_3_i1_]) <= (result)))) + Invariant(Exists(int, lambda d_4_i1_: + (((d_4_i1_) >= (0)) and ((d_4_i1_) < (d_2_i_))) and (((l)[d_4_i1_]) == (result)))) + if ((l)[d_2_i_]) > (result): + result = (l)[d_2_i_] + d_2_i_ = (d_2_i_) + (1) + return result diff --git a/Bench/037-sort_even.py b/Bench/037-sort_even.py new file mode 100644 index 0000000..3b472a2 --- /dev/null +++ b/Bench/037-sort_even.py @@ -0,0 +1,107 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def sorted__even(a : List[int]) -> List[int]: + Requires(Acc(list_pred(a))) + Requires((len(a)) > (0)) + Ensures(Acc(list_pred(a))) + Ensures(Acc(list_pred(Result()))) + Ensures((len(Result())) == (len(a))) + Ensures(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(Result())))) and (((d_0_i_ % 2)) == (0))) and (((d_1_j_ % 2)) == (0))) or (((Result())[d_0_i_]) <= ((Result())[d_1_j_]))))) + Ensures(Forall(int, lambda d_2_i_: + not ((((0) <= (d_2_i_)) and ((d_2_i_) < (len(a)))) and (((d_2_i_ % 2)) == (1))) or (((Result())[d_2_i_]) == ((a)[d_2_i_])))) + sorted__even = list([int(0)] * 0) # type : List[int] + d_3_p_ = list([False] * 0) # type : List[bool] + d_3_p_ = list([]) + d_4_i_ = int(0) # type : int + d_4_i_ = 0 + while (d_4_i_) < (len(a)): + Invariant(Acc(list_pred(d_3_p_))) + Invariant(Acc(list_pred(sorted__even))) + Invariant(Acc(list_pred(a))) + Invariant(((0) <= (d_4_i_)) and ((d_4_i_) <= (len(a)))) + Invariant((len(d_3_p_)) == (d_4_i_)) + Invariant(Forall(int, lambda d_5_j_: + not (((0) <= (d_5_j_)) and ((d_5_j_) < (d_4_i_))) or (((d_3_p_)[d_5_j_]) == (((d_5_j_ % 2)) == (0))))) + d_3_p_ = (d_3_p_) + [((d_4_i_ % 2)) == (0)] + d_4_i_ = (d_4_i_) + (1) + sorted__even = SortSeqPred(a, d_3_p_) + return sorted__even + +def SortSeqPred(s : List[int], p : List[bool]) -> List[int]: + Requires(Acc(list_pred(p), 1/2)) + Requires(Acc(list_pred(s), 1/2)) + Requires((len(s)) == (len(p))) + Ensures(Acc(list_pred(p), 1/2)) + Ensures(Acc(list_pred(s), 1/2)) + Ensures(Acc(list_pred(Result()))) + Ensures((len(s)) == (len(p))) + Ensures((len(Result())) == (len(s))) + Ensures(Forall(int, lambda d_6_i_: + Forall(int, lambda d_7_j_: + not ((((((0) <= (d_6_i_)) and ((d_6_i_) < (d_7_j_))) and ((d_7_j_) < (len(Result())))) and ((p)[d_6_i_])) and ((p)[d_7_j_])) or (((Result())[d_6_i_]) <= ((Result())[d_7_j_]))))) + Ensures(Forall(int, lambda d_8_i_: + not ((((0) <= (d_8_i_)) and ((d_8_i_) < (len(s)))) and (not((p)[d_8_i_]))) or (((Result())[d_8_i_]) == ((s)[d_8_i_])))) + sorted = list([int(0)] * 0) # type : List[int] + sorted = list(s) + d_9_i_ = int(0) # type : int + d_9_i_ = 0 + while (d_9_i_) < (len(sorted)): + Invariant(Acc(list_pred(sorted))) + Invariant(Acc(list_pred(p), 1/2)) + Invariant(Acc(list_pred(s), 1/2)) + Invariant((len(s)) == (len(p))) + Invariant(((0) <= (d_9_i_)) and ((d_9_i_) <= (len(sorted)))) + Invariant((len(sorted)) == (len(s))) + Invariant(Forall(int, lambda d_14_j_: + not ((((0) <= (d_14_j_)) and ((d_14_j_) < (len(s)))) and (not((p)[d_14_j_]))) or (((sorted)[d_14_j_]) == ((s)[d_14_j_])))) + 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_))) and ((p)[d_10_j_])) and ((p)[d_11_k_])) or + (((sorted)[d_10_j_]) <= ((sorted)[d_11_k_])), [[(sorted)[d_11_k_]]])), [[sorted[d_10_j_]]]))) + Invariant(Forall(int, lambda d_12_j_: + (not ((((0) <= (d_12_j_)) and ((d_12_j_) < (d_9_i_))) and ((p)[d_12_j_])) or + (Forall(int, lambda d_13_k_: + (not ((((d_9_i_) <= (d_13_k_)) and ((d_13_k_) < (len(sorted)))) and ((p)[d_13_k_])) or + (((sorted)[d_12_j_]) <= ((sorted)[d_13_k_])), [[sorted[d_13_k_]]]))), [[(sorted)[d_12_j_]]]))) + if (p)[d_9_i_]: + d_15_minIndex_ = int(0) # type : int + d_15_minIndex_ = d_9_i_ + d_16_j_ = int(0) # type : int + d_16_j_ = (d_9_i_) + (1) + while (d_16_j_) < (len(sorted)): + Invariant(Acc(list_pred(sorted))) + Invariant(Acc(list_pred(p), 1/2)) + Invariant(Acc(list_pred(s), 1/2)) + Invariant((len(s)) == (len(p))) + Invariant((len(sorted)) == (len(s))) + Invariant(0 <= d_9_i_ and d_9_i_ < len(sorted)) + Invariant((((d_9_i_) <= (d_15_minIndex_)) and ((d_15_minIndex_) < (d_16_j_))) and ((d_16_j_) <= (len(sorted)))) + Invariant(p[d_9_i_]) + Invariant((p)[d_15_minIndex_]) + Invariant(Forall(int, lambda d_17_k_: + not ((((d_9_i_) <= (d_17_k_)) and ((d_17_k_) < (d_16_j_))) and ((p)[d_17_k_])) or (((sorted)[d_15_minIndex_]) <= ((sorted)[d_17_k_])))) + Invariant(Forall(int, lambda d_14_j_: + not ((((0) <= (d_14_j_)) and ((d_14_j_) < (len(s)))) and (not((p)[d_14_j_]))) or (((sorted)[d_14_j_]) == ((s)[d_14_j_])))) + 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_))) and ((p)[d_10_j_])) and ((p)[d_11_k_])) or + (((sorted)[d_10_j_]) <= ((sorted)[d_11_k_])), [[(sorted)[d_11_k_]]])), [[sorted[d_10_j_]]]))) + Invariant(Forall(int, lambda d_12_j_: + (not ((((0) <= (d_12_j_)) and ((d_12_j_) < (d_9_i_))) and ((p)[d_12_j_])) or + (Forall(int, lambda d_13_k_: + (not ((((d_9_i_) <= (d_13_k_)) and ((d_13_k_) < (len(sorted)))) and ((p)[d_13_k_])) or + (((sorted)[d_12_j_]) <= ((sorted)[d_13_k_])), [[sorted[d_13_k_]]]))), [[(sorted)[d_12_j_]]]))) + if ((p)[d_16_j_]) and (((sorted)[d_16_j_]) < ((sorted)[d_15_minIndex_])): + d_15_minIndex_ = d_16_j_ + d_16_j_ = (d_16_j_) + (1) + if (d_15_minIndex_) != (d_9_i_): + Assert((p)[d_15_minIndex_]) + Assert(p[d_9_i_]) + rhs0_ = (sorted)[d_9_i_] # type : int + (sorted)[d_9_i_] = (sorted)[d_15_minIndex_] + (sorted)[d_15_minIndex_] = rhs0_ + d_9_i_ = (d_9_i_) + (1) + return sorted \ No newline at end of file diff --git a/Bench/040-triples-sum-to-zero.py b/Bench/040-triples-sum-to-zero.py new file mode 100644 index 0000000..d8575f7 --- /dev/null +++ b/Bench/040-triples-sum-to-zero.py @@ -0,0 +1,57 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def triples__sum__to__zero(l : List[int]) -> bool: + Requires(Acc(list_pred(l))) + Ensures(Acc(list_pred(l))) + Ensures(Implies(not(Result()), Forall(int, lambda d_3_i_: + Forall(int, lambda d_4_j_: + Forall(int, lambda d_5_k_: + Implies((((((((0) <= (d_3_i_)) and ((d_3_i_) < (len(l)))) and (((0) <= (d_4_j_)) and ((d_4_j_) < (len(l))))) and (((0) <= (d_5_k_)) and ((d_5_k_) < (len(l))))) and ((d_3_i_) != (d_4_j_))) and ((d_4_j_) != (d_5_k_))) and ((d_3_i_) != (d_5_k_)), ((((l)[d_3_i_]) + ((l)[d_4_j_])) + ((l)[d_5_k_])) != (0))))))) + Ensures(Implies(Result(), Exists(int, lambda d_0_i_: + Exists(int, lambda d_1_j_: + Exists(int, lambda d_2_k_: + ((((((((0) <= (d_0_i_)) and ((d_0_i_) < (len(l)))) and (((0) <= (d_1_j_)) and ((d_1_j_) < (len(l))))) and (((0) <= (d_2_k_)) and ((d_2_k_) < (len(l))))) and ((d_0_i_) != (d_1_j_))) and ((d_1_j_) != (d_2_k_))) and ((d_0_i_) != (d_2_k_))) and (((((l)[d_0_i_]) + ((l)[d_1_j_])) + ((l)[d_2_k_])) == (0))))))) + result = False # type : bool + result = False + d_6_i_ = int(0) # type : int + d_6_i_ = 0 + while (d_6_i_) < (len(l)): + Invariant(Acc(list_pred(l), 1/2)) + Invariant(((d_6_i_) >= (0)) and ((d_6_i_) <= (len(l)))) + Invariant(Forall(int, lambda d_7_i1_: + Forall(int, lambda d_8_j_: + Forall(int, lambda d_9_k_: + (Implies((((((((0) <= (d_7_i1_)) and ((d_7_i1_) < (d_6_i_))) and (((0) <= (d_8_j_)) and ((d_8_j_) < (len(l))))) and (((0) <= (d_9_k_)) and ((d_9_k_) < (len(l))))) and ((d_7_i1_) != (d_8_j_))) and ((d_8_j_) != (d_9_k_))) and ((d_7_i1_) != (d_9_k_)), ((((l)[d_7_i1_]) + ((l)[d_8_j_])) + ((l)[d_9_k_])) != (0)), [[(((l)[d_7_i1_]) + ((l)[d_8_j_])) + ((l)[d_9_k_])]]))))) + d_13_j_ = int(0) # type : int + d_13_j_ = 0 + while (d_13_j_) < (len(l)): + Invariant(Acc(list_pred(l), 1/2)) + Invariant(((d_6_i_) >= (0)) and ((d_6_i_) < (len(l)))) + Invariant(((d_13_j_) >= (0)) and ((d_13_j_) <= (len(l)))) + Invariant(Forall(int, lambda d_14_i1_: + Forall(int, lambda d_15_j_: + Forall(int, lambda d_16_k_: + (Implies(((((((((0) <= (d_14_i1_)) and ((d_14_i1_) < (d_6_i_))) and (((0) <= (d_15_j_)) and ((d_15_j_) < (len(l))))) and (((0) <= (d_16_k_)) and ((d_16_k_) < (len(l)))))) or + (d_14_i1_ == d_6_i_ and (d_15_j_) >= 0 and (d_15_j_) < d_13_j_ and (d_16_k_) >= 0 and (d_16_k_) < len(l))) + and ((d_14_i1_) != (d_15_j_)) and ((d_15_j_) != (d_16_k_))) and ((d_14_i1_) != (d_16_k_)), ((((l)[d_14_i1_]) + ((l)[d_15_j_])) + ((l)[d_16_k_])) != (0)), [[(((l)[d_14_i1_]) + ((l)[d_15_j_])) + ((l)[d_16_k_])]]))))) + d_24_k_ = int(0) # type : int + d_24_k_ = 0 + while (d_24_k_) < (len(l)): + Invariant(Acc(list_pred(l), 1/2)) + Invariant(((d_6_i_) >= (0)) and ((d_6_i_) < (len(l)))) + Invariant(((d_13_j_) >= (0)) and ((d_13_j_) < (len(l)))) + Invariant(((d_24_k_) >= (0)) and ((d_24_k_) <= (len(l)))) + Invariant(Forall(int, lambda d_14_i1_: + Forall(int, lambda d_15_j_: + Forall(int, lambda d_16_k_: + (Implies(((((((((0) <= (d_14_i1_)) and ((d_14_i1_) < (d_6_i_))) and (((0) <= (d_15_j_)) and ((d_15_j_) < (len(l))))) and (((0) <= (d_16_k_)) and ((d_16_k_) < (len(l)))))) or + (d_14_i1_ == d_6_i_ and (d_15_j_) >= 0 and (d_15_j_) < d_13_j_ and (d_16_k_) >= 0 and (d_16_k_) < len(l)) or + (d_14_i1_ == d_6_i_ and (d_15_j_) == d_13_j_ and (d_16_k_) >= 0 and (d_16_k_) < d_24_k_)) + and ((d_14_i1_) != (d_15_j_)) and ((d_15_j_) != (d_16_k_))) and ((d_14_i1_) != (d_16_k_)), ((((l)[d_14_i1_]) + ((l)[d_15_j_])) + ((l)[d_16_k_])) != (0)), [[(((l)[d_14_i1_]) + ((l)[d_15_j_])) + ((l)[d_16_k_])]]))))) + if ((((d_6_i_) != (d_13_j_)) and ((d_13_j_) != (d_24_k_))) and ((d_6_i_) != (d_24_k_))) and (((((l)[d_6_i_]) + ((l)[d_13_j_])) + ((l)[d_24_k_])) == (0)): + return True + d_24_k_ = (d_24_k_) + (1) + d_13_j_ = (d_13_j_) + (1) + d_6_i_ = (d_6_i_) + (1) + return False \ No newline at end of file diff --git a/Bench/042-incr-list.py b/Bench/042-incr-list.py new file mode 100644 index 0000000..6b7d120 --- /dev/null +++ b/Bench/042-incr-list.py @@ -0,0 +1,24 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def incr__list(l : List[int]) -> List[int]: + Requires(Acc(list_pred(l))) + Ensures(Acc(list_pred(l))) + Ensures(Acc(list_pred(Result()))) + Ensures((len(Result())) == (len(l))) + Ensures(Forall(int, lambda d_0_i_: + not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(l)))) or (((Result())[d_0_i_]) == (((l)[d_0_i_]) + (1))))) + result = list([int(0)] * 0) # type : List[int] + result = list([]) + d_1_i_ = int(0) # type : int + d_1_i_ = 0 + while (d_1_i_) < (len(l)): + Invariant(Acc(list_pred(result))) + Invariant(Acc(list_pred(l))) + Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(l)))) + Invariant((len(result)) == (d_1_i_)) + Invariant(Forall(int, lambda d_2_i1_: + not (((0) <= (d_2_i1_)) and ((d_2_i1_) < (d_1_i_))) or (((result)[d_2_i1_]) == (((l)[d_2_i1_]) + (1))))) + result = (result) + ([((l)[d_1_i_]) + (1)]) + d_1_i_ = (d_1_i_) + (1) + return result diff --git a/Bench/043-pairs-sum-to-zero.py b/Bench/043-pairs-sum-to-zero.py new file mode 100644 index 0000000..352ddf4 --- /dev/null +++ b/Bench/043-pairs-sum-to-zero.py @@ -0,0 +1,44 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def pairs__sum__to__zero(l : List[int]) -> bool: + Requires(Acc(list_pred(l))) + Ensures(Acc(list_pred(l))) + Ensures(not (Result()) or (Exists(int, lambda d_0_i_: + Exists(int, lambda d_1_j_: + (((((0) <= (d_0_i_)) and ((d_0_i_) < (len(l)))) and (((0) <= (d_1_j_)) and ((d_1_j_) < (len(l))))) and ((d_0_i_) != (d_1_j_))) and ((((l)[d_0_i_]) + ((l)[d_1_j_])) == (0)))))) + Ensures(not (not(Result())) or (Forall(int, lambda d_2_i_: + Forall(int, lambda d_3_j_: + (not (((((0) <= (d_2_i_)) and ((d_2_i_) < (len(l)))) and (((0) <= (d_3_j_)) and ((d_3_j_) < (len(l))))) and ((d_2_i_) != (d_3_j_))) or ((((l)[d_2_i_]) + ((l)[d_3_j_])) != (0)), [[((l)[d_2_i_]) + ((l)[d_3_j_])]]))))) + result = False # type : bool + result = False + d_4_i_ = int(0) # type : int + d_4_i_ = 0 + while (d_4_i_) < (len(l)): + Invariant(Acc(list_pred(l))) + Invariant(((d_4_i_) >= (0)) and ((d_4_i_) <= (len(l)))) + Invariant(Implies(not(result), Forall(int, lambda d_5_i1_: + Forall(int, lambda d_6_j_: + (Implies(((((0) <= (d_5_i1_)) and ((d_5_i1_) < (d_4_i_))) and (((0) <= (d_6_j_)) and ((d_6_j_) < (len(l))))) and ((d_5_i1_) != (d_6_j_)), (((l)[d_5_i1_]) + ((l)[d_6_j_])) != (0)), [[((l)[d_5_i1_]) + ((l)[d_6_j_])]]))))) + Invariant(not (result) or (Exists(int, lambda d_7_i1_: + Exists(int, lambda d_8_j_: + (((((0) <= (d_7_i1_)) and ((d_7_i1_) < (d_4_i_))) and (((0) <= (d_8_j_)) and ((d_8_j_) < (len(l))))) and ((d_7_i1_) != (d_8_j_))) and ((((l)[d_7_i1_]) + ((l)[d_8_j_])) == (0)))))) + d_9_j_ = int(0) # type : int + d_9_j_ = 0 + while (d_9_j_) < (len(l)): + Invariant(Acc(list_pred(l))) + Invariant(((d_4_i_) >= (0)) and ((d_4_i_) < (len(l)))) + Invariant(((d_9_j_) >= (0)) and ((d_9_j_) <= (len(l)))) + Invariant(Implies(not(result), Forall(int, lambda d_5_i1_: + Forall(int, lambda d_6_j_: + (Implies((((((0) <= (d_5_i1_)) and ((d_5_i1_) < (d_4_i_))) and (((0) <= (d_6_j_)) and ((d_6_j_) < (len(l))))) or (d_5_i1_ == d_4_i_ and ((0) <= (d_6_j_)) and ((d_6_j_) < (d_9_j_)))) and ((d_5_i1_) != (d_6_j_)), (((l)[d_5_i1_]) + ((l)[d_6_j_])) != (0)), [[((l)[d_5_i1_]) + ((l)[d_6_j_])]]))))) + Invariant(not (result) or ((Exists(int, lambda d_13_i1_: + Exists(int, lambda d_14_j1_: + (((((0) <= (d_13_i1_)) and ((d_13_i1_) < (d_4_i_))) and (((0) <= (d_14_j1_)) and ((d_14_j1_) < (len(l))))) and ((d_13_i1_) != (d_14_j1_))) and ((((l)[d_13_i1_]) + ((l)[d_14_j1_])) == (0))))) + or (Exists(int, lambda d_15_j1_: + ((((0) <= (d_15_j1_)) and ((d_15_j1_) < (d_9_j_))) and ((d_4_i_) != (d_15_j1_))) and ((((l)[d_4_i_]) + ((l)[d_15_j1_])) == (0)))))) + if ((d_4_i_) != (d_9_j_)) and ((((l)[d_4_i_]) + ((l)[d_9_j_])) == (0)): + result = True + d_9_j_ = (d_9_j_) + (1) + d_4_i_ = (d_4_i_) + (1) + return result \ No newline at end of file diff --git a/Bench/048-is-palindrome.py b/Bench/048-is-palindrome.py new file mode 100644 index 0000000..bffb32f --- /dev/null +++ b/Bench/048-is-palindrome.py @@ -0,0 +1,21 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def is__palindrome(text : List[int]) -> bool: + Requires(Acc(list_pred(text))) + Ensures(Acc(list_pred(text))) + Ensures((Result()) == (Forall(int, lambda d_0_i_: + not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(text)))) or (((text)[d_0_i_]) == ((text)[((len(text)) - (d_0_i_)) - (1)]))))) + 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))) + 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 diff --git a/Bench/051-remove-vowels.py b/Bench/051-remove-vowels.py new file mode 100644 index 0000000..71bedbd --- /dev/null +++ b/Bench/051-remove-vowels.py @@ -0,0 +1,32 @@ +from typing import List +from nagini_contracts.contracts import * + +def remove__vowels(text : List[int]) -> List[int]: + Requires(Acc(list_pred(text))) + Ensures(Acc(list_pred(text))) + Ensures(Acc(list_pred(Result()))) + Ensures(Forall(int, lambda d_0_i_: + not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(Result())))) or (((((((Result())[d_0_i_]) != (0)) and (((Result())[d_0_i_]) != (1))) and (((Result())[d_0_i_]) != (2))) and (((Result())[d_0_i_]) != (3))) and (((Result())[d_0_i_]) != (4))))) + Ensures(Forall(int, lambda d_1_i_: + not (((d_1_i_) >= (0)) and ((d_1_i_) < (len(Result())))) or (((Result())[d_1_i_]) in (text)))) + Ensures(Forall(int, lambda d_2_j_: + not ((((((((d_2_j_) >= (0)) and ((d_2_j_) < (len(text)))) and (((text)[d_2_j_]) != (0))) and (((text)[d_2_j_]) != (1))) and (((text)[d_2_j_]) != (2))) and (((text)[d_2_j_]) != (3))) and (((text)[d_2_j_]) != (4))) or (((text)[d_2_j_]) in (Result())))) + s = list([int(0)] * 0) # type : List[int] + s = [] + d_3_i_ = int(0) # type : int + d_3_i_ = 0 + while (d_3_i_) < (len(text)): + Invariant(Acc(list_pred(text))) + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(text)))) + Invariant(Forall(int, lambda d_4_i_: + not (((d_4_i_) >= (0)) and ((d_4_i_) < (len(s)))) or (((((((s)[d_4_i_]) != (0)) and (((s)[d_4_i_]) != (1))) and (((s)[d_4_i_]) != (2))) and (((s)[d_4_i_]) != (3))) and (((s)[d_4_i_]) != (4))))) + Invariant(Forall(int, lambda d_5_i_: + not (((d_5_i_) >= (0)) and ((d_5_i_) < (len(s)))) or (((s)[d_5_i_]) in (text)))) + Invariant(Forall(int, lambda d_6_j_: + not ((((((((d_6_j_) >= (0)) and ((d_6_j_) < (d_3_i_))) and (((text)[d_6_j_]) != (0))) and (((text)[d_6_j_]) != (1))) and (((text)[d_6_j_]) != (2))) and (((text)[d_6_j_]) != (3))) and (((text)[d_6_j_]) != (4))) or (((text)[d_6_j_]) in (s)))) + d_7_c_ = (text)[d_3_i_] # type : int + if (((((d_7_c_) != (0)) and ((d_7_c_) != (1))) and ((d_7_c_) != (2))) and ((d_7_c_) != (3))) and ((d_7_c_) != (4)): + s = (s) + [d_7_c_] + d_3_i_ = (d_3_i_) + (1) + return s diff --git a/Bench/052-below-threshold.py b/Bench/052-below-threshold.py new file mode 100644 index 0000000..3e78bf0 --- /dev/null +++ b/Bench/052-below-threshold.py @@ -0,0 +1,21 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def below__threshold(l : List[int], t : int) -> bool: + Requires(Acc(list_pred(l))) + Ensures(Acc(list_pred(l))) + Ensures((Result()) == (Forall(int, lambda d_0_i_: + not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(l)))) or (((l)[d_0_i_]) < (t))))) + b = False # type : bool + b = True + d_1_i_ = int(0) # type : int + d_1_i_ = 0 + while (d_1_i_) < (len(l)): + Invariant(Acc(list_pred(l))) + Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(l)))) + Invariant((b) == (Forall(int, lambda d_2_i1_: + not (((d_2_i1_) >= (0)) and ((d_2_i1_) < (d_1_i_))) or (((l)[d_2_i1_]) < (t))))) + if ((l)[d_1_i_]) >= (t): + b = False + d_1_i_ = (d_1_i_) + (1) + return b diff --git a/Bench/053-add.py b/Bench/053-add.py new file mode 100644 index 0000000..926889b --- /dev/null +++ b/Bench/053-add.py @@ -0,0 +1,7 @@ +from nagini_contracts.contracts import * + +def add(x : int, y : int) -> int: + Ensures((Result()) == ((x) + (y))) + z = int(0) # type : int + z = (x) + (y) + return z \ No newline at end of file diff --git a/Bench/054-same-chars.py b/Bench/054-same-chars.py new file mode 100644 index 0000000..5d4f6f4 --- /dev/null +++ b/Bench/054-same-chars.py @@ -0,0 +1,12 @@ +from typing import cast, List, Dict, Set, Optional, Union, FrozenSet +from nagini_contracts.contracts import * + +def same_chars(s0: List[int], s1: List[int]) -> bool: + Requires(Acc(list_pred(s0))) + Requires(Acc(list_pred(s1))) + Ensures(Acc(list_pred(s0))) + Ensures(Acc(list_pred(s1))) + Ensures(Result() == (Forall(int, lambda d_0_i_ : Implies(((0 <= d_0_i_) and (d_0_i_ < len(s0))), s0[d_0_i_] in s1)) and + Forall(int, lambda d_1_i_ : Implies(((0 <= d_1_i_) and (d_1_i_ < len(s1))), s1[d_1_i_] in s0)))) + return (Forall(int, lambda d_0_i_ : Implies(((0 <= d_0_i_) and (d_0_i_ < len(s0))), s0[d_0_i_] in s1)) and + Forall(int, lambda d_1_i_ : Implies(((0 <= d_1_i_) and (d_1_i_ < len(s1))), s1[d_1_i_] in s0))) \ No newline at end of file diff --git a/Bench/055-fib.py b/Bench/055-fib.py new file mode 100644 index 0000000..4384ed8 --- /dev/null +++ b/Bench/055-fib.py @@ -0,0 +1,44 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def fib(n : int) -> int : + Requires((n) >= (0)) + Ensures(Result() >= 0) + if (n) == (0): + return 0 + elif (n) == (1): + return 1 + elif True: + return (fib((n) - (1))) + (fib((n) - (2))) + +def ComputeFib(n : int) -> int: + Requires((n) >= (0)) + Ensures((Result()) == (fib(n))) + Ensures(Result() >= 0) + result = int(0) # type : int + if (n) == (0): + result = 0 + return result + if (n) == (1): + result = 1 + return result + d_0_a_ = int(0) # type : int + d_1_b_ = int(0) # type : int + rhs0_ = 0 # type : int + rhs1_ = 1 # type : int + d_0_a_ = rhs0_ + d_1_b_ = rhs1_ + d_2_i_ = int(0) # type : int + d_2_i_ = 2 + while (d_2_i_) <= (n): + Invariant(((2) <= (d_2_i_)) and ((d_2_i_) <= ((n) + (1)))) + Invariant((d_0_a_) == (fib((d_2_i_) - (2)))) + Invariant((d_1_b_) == (fib((d_2_i_) - (1)))) + d_3_temp_ = int(0) # type : int + d_3_temp_ = (d_0_a_) + (d_1_b_) + d_0_a_ = d_1_b_ + d_1_b_ = d_3_temp_ + d_2_i_ = (d_2_i_) + (1) + result = d_1_b_ + return result \ No newline at end of file diff --git a/Bench/057-monotonic.py b/Bench/057-monotonic.py new file mode 100644 index 0000000..d1cffeb --- /dev/null +++ b/Bench/057-monotonic.py @@ -0,0 +1,38 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def monotonic(xs : List[int]) -> bool: + Requires(Acc(list_pred(xs))) + Requires((len(xs)) > (0)) + Ensures(Acc(list_pred(xs))) + Ensures((Result()) == ((Forall(int, lambda d_0_i_: + Forall(int, lambda d_1_j_: + not ((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len(xs)))) or (((xs)[d_0_i_]) < ((xs)[d_1_j_]))))) or (Forall(int, lambda d_2_i_: + Forall(int, lambda d_3_j_: + not ((((0) <= (d_2_i_)) and ((d_2_i_) < (d_3_j_))) and ((d_3_j_) < (len(xs)))) or (((xs)[d_2_i_]) > ((xs)[d_3_j_]))))))) + result = False # type : bool + if (len(xs)) == (1): + result = True + return result + d_4_increasing_ = False # type : bool + d_4_increasing_ = True + d_5_decreasing_ = False # type : bool + d_5_decreasing_ = True + d_6_i_ = int(0) # type : int + d_6_i_ = 1 + while (d_6_i_) < (len(xs)): + Invariant(Acc(list_pred(xs))) + Invariant(((1) <= (d_6_i_)) and ((d_6_i_) <= (len(xs)))) + Invariant((d_4_increasing_) == (Forall(int, lambda d_7_j_: + Forall(int, lambda d_8_k_: + not ((((0) <= (d_7_j_)) and ((d_7_j_) < (d_8_k_))) and ((d_8_k_) < (d_6_i_))) or (((xs)[d_7_j_]) < ((xs)[d_8_k_])))))) + Invariant((d_5_decreasing_) == (Forall(int, lambda d_9_j_: + Forall(int, lambda d_10_k_: + not ((((0) <= (d_9_j_)) and ((d_9_j_) < (d_10_k_))) and ((d_10_k_) < (d_6_i_))) or (((xs)[d_9_j_]) > ((xs)[d_10_k_])))))) + if ((xs)[(d_6_i_) - (1)]) >= ((xs)[d_6_i_]): + d_4_increasing_ = False + if ((xs)[(d_6_i_) - (1)]) <= ((xs)[d_6_i_]): + d_5_decreasing_ = False + d_6_i_ = (d_6_i_) + (1) + result = (d_4_increasing_) or (d_5_decreasing_) + return result \ No newline at end of file diff --git a/Bench/058-common.py b/Bench/058-common.py new file mode 100644 index 0000000..06e3ad1 --- /dev/null +++ b/Bench/058-common.py @@ -0,0 +1,49 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@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)), [[(a)[d_0_i_]]])) + + +def common(l1 : List[int], l2 : List[int]) -> List[int]: + Requires(Acc(list_pred(l2), 1/2)) + Requires(Acc(list_pred(l1), 1/2)) + Ensures(Acc(list_pred(l2), 1/2)) + Ensures(Acc(list_pred(l1), 1/2)) + Ensures(Acc(list_pred(Result()))) + Ensures(Forall(int, lambda d_4_j_: + (Implies(((d_4_j_) >= 0 and d_4_j_ < len(l1)), Implies((Exists(int, lambda x: x >= 0 and x< len(l2) and l2[x] == l1[d_4_j_])), Exists(int, lambda x: x >= 0 and x< len(Result()) and Result()[x] == l1[d_4_j_])))))) + c = list([int(0)] * 0) # type : List[int] + d_2_i_ = int(0) # type : int + d_2_i_ = 0 + while (d_2_i_) < (len(l1)): + Invariant(Acc(list_pred(c))) + Invariant(Acc(list_pred(l2), 1/2)) + Invariant(Acc(list_pred(l1), 1/2)) + Invariant(((d_2_i_) >= (0)) and ((d_2_i_) <= (len(l1)))) + Invariant(Forall(int, lambda d_4_j_: + (Implies(((d_4_j_) >= 0 and d_4_j_ < d_2_i_), Implies((Exists(int, lambda x: x >= 0 and x< len(l2) and l2[x] == l1[d_4_j_])), Exists(int, lambda x: x >= 0 and x< len(c) and c[x] == l1[d_4_j_]))), + [[l1[d_4_j_]]]))) + + d_5_j_ = int(0) # type : int + d_5_j_ = 0 + while (d_5_j_) < (len(l2)): + Invariant(Acc(list_pred(c))) + Invariant(Acc(list_pred(l2), 1/2)) + Invariant(Acc(list_pred(l1), 1/2)) + Invariant(((d_2_i_) >= (0)) and ((d_2_i_) < (len(l1)))) + Invariant(((d_5_j_) >= (0)) and ((d_5_j_) <= (len(l2)))) + Invariant(Forall(int, lambda d_4_j_: + (Implies(((d_4_j_) >= 0 and d_4_j_ < d_2_i_), Implies((Exists(int, lambda x: x >= 0 and x< len(l2) and l2[x] == l1[d_4_j_])), Exists(int, lambda x: x >= 0 and x< len(c) and c[x] == l1[d_4_j_]))), + [[l1[d_4_j_]]]))) + Invariant(Implies(Exists(int, lambda x: x >= 0 and x < d_5_j_ and l2[x] == l1[d_2_i_]), Exists(int, lambda x: x >= 0 and x < len(c) and c[x] == l1[d_2_i_]))) + if ((l1)[d_2_i_]) == ((l2)[d_5_j_]) and NotInArray(c, (l1)[d_2_i_]): + c = c + [((l1)[d_2_i_])] + Assert((Exists(int, lambda x : x >= 0 and x < len(l1) and (c[len(c) - 1]) == (l1[x])))) + Assert((Exists(int, lambda x : x >= 0 and x < len(l2) and (c[len(c) - 1]) == (l2[x])))) + d_5_j_ = (d_5_j_) + (1) + d_2_i_ = (d_2_i_) + (1) + return c diff --git a/Bench/059-largest-prime-factor.py b/Bench/059-largest-prime-factor.py new file mode 100644 index 0000000..05da467 --- /dev/null +++ b/Bench/059-largest-prime-factor.py @@ -0,0 +1,34 @@ +from nagini_contracts.contracts import * + +def is_prime(k : int) -> bool: + Requires(k >= 2) + Ensures(Implies(Result(), Forall(int, lambda i : Implies((i >= 2) and (i < k), (k % i != 0))))) + Ensures(Implies(Result() == False, Exists(int, lambda j : ((j >= 2) and (j < k) and (k % j == 0))))) + i = 2 # type : int + result = True # type : bool + while i < k: + Invariant(i >= 2 and i <= k) + Invariant(Implies(result == False, Exists(int, lambda j : ((j >= 2) and (j < i) and (k % j == 0))))) + Invariant(Implies(result, Forall(int, lambda j : Implies((j >= 2) and (j < i), (k % j != 0))))) + if k % i == 0: + result = False + i = i + 1 + return result + +@Pure +def is_prime_pred(k : int) -> bool: + return Forall(int, lambda i : Implies((i >= 2) and (i < k), (k % i != 0))) + +def largest_prime_factor(n: int) -> int: + Requires(n >= 2) + Ensures(Result() >= 1 and Result() <= n and (Result() == 1 or Result() > 1 and is_prime_pred(Result()))) + largest = 1 # type : int + j = 2 # type : int + while j <= n: + Invariant(j >= 2 and j <= n + 1) + Invariant(largest >= 1 and largest < j) + Invariant(largest == 1 or largest > 1 and is_prime_pred(largest)) + if n % j == 0 and is_prime(j): + largest = max(largest, j) + j = j + 1 + return largest \ No newline at end of file diff --git a/Bench/060-sum-to-n.py b/Bench/060-sum-to-n.py new file mode 100644 index 0000000..ae5c604 --- /dev/null +++ b/Bench/060-sum-to-n.py @@ -0,0 +1,24 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def psum(i : int, j : int) -> int : + Requires(0 <= i and i <= j + 1) + if i > j: + return 0 + else: + return j + 1 + (psum(i, j - 1)) + +def sum__squares(n : int) -> int: + Requires((n) >= (1)) + Ensures((Result()) == (psum(0, n - 1))) + r = int(0) # type : int + r = 0 + d_2_k_ = int(0) # type : int + d_2_k_ = 0 + while (d_2_k_) < (n): + Invariant(((0) <= (d_2_k_)) and ((d_2_k_) <= (n))) + Invariant((r) == (psum(0, d_2_k_ - 1))) + r = (r) + ((d_2_k_) + (1)) + d_2_k_ = (d_2_k_) + (1) + return r \ No newline at end of file diff --git a/Bench/062-derivative.py b/Bench/062-derivative.py new file mode 100644 index 0000000..e8c7f6b --- /dev/null +++ b/Bench/062-derivative.py @@ -0,0 +1,25 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def derivative(xs : List[int]) -> List[int]: + Requires(Acc(list_pred(xs))) + Requires((len(xs)) > (0)) + Ensures(Acc(list_pred(xs))) + Ensures(Acc(list_pred(Result()))) + Ensures((len(Result())) == ((len(xs)) - (1))) + Ensures(Forall(int, lambda d_0_i_: + not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(Result())))) or (((Result())[d_0_i_]) == (((xs)[(d_0_i_) + (1)]) * ((d_0_i_) + (1)))))) + result = list([int(0)] * 0) # type : List[int] + result = list([]) + d_1_i_ = int(0) # type : int + d_1_i_ = 1 + while (d_1_i_) < (len(xs)): + Invariant(Acc(list_pred(result))) + Invariant(Acc(list_pred(xs))) + Invariant(((1) <= (d_1_i_)) and ((d_1_i_) <= (len(xs)))) + Invariant((len(result)) == ((d_1_i_) - (1))) + Invariant(Forall(int, lambda d_2_j_: + not (((0) <= (d_2_j_)) and ((d_2_j_) < (len(result)))) or (((result)[d_2_j_]) == (((xs)[(d_2_j_) + (1)]) * ((d_2_j_) + (1)))))) + result = (result) + [((xs)[d_1_i_]) * (d_1_i_)] + d_1_i_ = (d_1_i_) + (1) + return result diff --git a/Bench/063-fibfib.py b/Bench/063-fibfib.py new file mode 100644 index 0000000..98e4f1b --- /dev/null +++ b/Bench/063-fibfib.py @@ -0,0 +1,49 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def fibfib(n : int) -> int : + Requires((n) >= (0)) + Ensures((Result()) >= (0)) + if ((n) == (0)) or ((n) == (1)): + return 0 + elif (n) == (2): + return 1 + elif True: + return ((fibfib((n) - (1))) + (fibfib((n) - (2)))) + (fibfib((n) - (3))) + +def ComputeFibFib(n : int) -> int: + Requires((n) >= (0)) + Ensures((Result()) >= (0)) + Ensures((Result()) == (fibfib(n))) + result = int(0) # type : int + if ((n) == (0)) or ((n) == (1)): + result = 0 + return result + if (n) == (2): + 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 + rhs0_ = 0 # type : int + rhs1_ = 0 # type : int + rhs2_ = 1 # type : int + d_0_a_ = rhs0_ + d_1_b_ = rhs1_ + d_2_c_ = rhs2_ + d_3_i_ = int(0) # type : int + d_3_i_ = 3 + while (d_3_i_) <= (n): + Invariant(((3) <= (d_3_i_)) and ((d_3_i_) <= ((n) + (1)))) + Invariant((d_0_a_) == (fibfib((d_3_i_) - (3)))) + Invariant((d_1_b_) == (fibfib((d_3_i_) - (2)))) + Invariant((d_2_c_) == (fibfib((d_3_i_) - (1)))) + d_4_temp_ = int(0) # type : int + d_4_temp_ = ((d_2_c_) + (d_1_b_)) + (d_0_a_) + d_0_a_ = d_1_b_ + d_1_b_ = d_2_c_ + d_2_c_ = d_4_temp_ + d_3_i_ = (d_3_i_) + (1) + result = d_2_c_ + return result diff --git a/Bench/066-digitSum.py b/Bench/066-digitSum.py new file mode 100644 index 0000000..f5632a8 --- /dev/null +++ b/Bench/066-digitSum.py @@ -0,0 +1,29 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def upper__sum__rec(i : int, j : int, s : List[int]) -> int : + Requires(Acc(list_pred(s))) + Requires(((0) <= (i)) and ((i) <= (j)) and ((j) <= (len(s)))) + if i == j: + return 0 + else: + return s[j - 1] + upper__sum__rec(i, j - 1, s) + +def upper__sum(s : List[int]) -> int: + Requires(Acc(list_pred(s))) + Ensures(Acc(list_pred(s))) + Ensures((Result()) == (upper__sum__rec(0, len(s), s))) + res = int(0) # type : int + res = 0 + d_1_i_ = int(0) # type : int + d_1_i_ = 0 + while (d_1_i_) < (len(s)): + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(s)))) + Invariant(Forall(int, lambda d_0_i_: (Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len(s))), upper__sum__rec(0, d_0_i_ + 1, s) == upper__sum__rec(0, d_0_i_, s) + s[d_0_i_]), [[upper__sum__rec(0, d_1_i_ + 1, s)]]))) + Invariant((res) == (upper__sum__rec(0, d_1_i_, s))) + res = (res) + (((s)[d_1_i_])) + d_1_i_ = (d_1_i_) + (1) + return res + diff --git a/Bench/068-pluck.py b/Bench/068-pluck.py new file mode 100644 index 0000000..d135e9a --- /dev/null +++ b/Bench/068-pluck.py @@ -0,0 +1,49 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def PluckSmallestEven(nodes : List[int]) -> List[int]: + Requires(Acc(list_pred(nodes))) + Requires((len(nodes)) <= (10000)) + Requires(Forall(int, lambda d_0_i_: + not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(nodes)))) or (((nodes)[d_0_i_]) >= (0)))) + Ensures(Acc(list_pred(nodes))) + Ensures(Acc(list_pred(Result()))) + Ensures(((len(Result())) == (0)) or ((len(Result())) == (2))) + Ensures(not ((len(Result())) == (2)) or ((((0) <= ((Result())[1])) and (((Result())[1]) < (len(nodes)))) and (((nodes)[(Result())[1]]) == ((Result())[0])))) + Ensures(not ((len(Result())) == (2)) or ((((Result())[0] % 2)) == (0))) + Ensures(not ((len(Result())) == (2)) or (Forall(int, lambda d_1_i_: + not ((((0) <= (d_1_i_)) and ((d_1_i_) < (len(nodes)))) and ((((nodes)[d_1_i_] % 2)) == (0))) or (((Result())[0]) <= ((nodes)[d_1_i_]))))) + Ensures(not ((len(Result())) == (2)) or (Forall(int, lambda d_2_i_: + not (((0) <= (d_2_i_)) and ((d_2_i_) < ((Result())[1]))) or (((((nodes)[d_2_i_] % 2)) != (0)) or (((nodes)[d_2_i_]) > ((Result())[0])))))) + Ensures(not ((len(Result())) == (0)) or (Forall(int, lambda d_3_i_: + not (((0) <= (d_3_i_)) and ((d_3_i_) < (len(nodes)))) or ((((nodes)[d_3_i_] % 2)) != (0))))) + result = list([int(0)] * 0) # type : List[int] + d_4_smallestEven_ = int(0) # type : int + d_4_smallestEven_ = -1 + d_5_smallestIndex_ = int(0) # type : int + d_5_smallestIndex_ = -1 + d_6_i_ = int(0) # type : int + while d_6_i_ < len(nodes): + Invariant(Acc(list_pred(result))) + Invariant(Acc(list_pred(nodes))) + Invariant(((0) <= (d_6_i_)) and ((d_6_i_) <= (len(nodes)))) + Invariant(((d_4_smallestEven_) == (-1)) == ((d_5_smallestIndex_) == (-1))) + Invariant(not ((d_5_smallestIndex_) != (-1)) or (((0) <= (d_5_smallestIndex_)) and ((d_5_smallestIndex_) < (d_6_i_)))) + Invariant(not ((d_5_smallestIndex_) != (-1)) or (((nodes)[d_5_smallestIndex_]) == (d_4_smallestEven_))) + Invariant(not ((d_4_smallestEven_) != (-1)) or (((d_4_smallestEven_ % 2)) == (0))) + Invariant(not ((d_4_smallestEven_) != (-1)) or (Forall(int, lambda d_7_j_: + (not ((((0) <= (d_7_j_)) and ((d_7_j_) < (d_6_i_))) and ((((nodes)[d_7_j_] % 2)) == (0))) or ((d_4_smallestEven_) <= ((nodes)[d_7_j_])), [[(nodes)[d_7_j_]]])))) + Invariant(not ((d_5_smallestIndex_) != (-1)) or (Forall(int, lambda d_8_j_: + (not (((0) <= (d_8_j_)) and ((d_8_j_) < (d_5_smallestIndex_))) or ((((nodes)[d_8_j_] % 2)) != (0)) or (((nodes)[d_8_j_]) > (d_4_smallestEven_)), [[(nodes)[d_8_j_]]])))) + Invariant(not ((d_5_smallestIndex_) == (-1)) or (Forall(int, lambda d_9_j_: + (not (((0) <= (d_9_j_)) and ((d_9_j_) < (d_6_i_))) or ((((nodes)[d_9_j_] % 2)) != (0)), [[(nodes)[d_9_j_]]])))) + if ((((nodes)[d_6_i_] % 2)) == (0)) and (((d_4_smallestEven_) == (-1)) or (((nodes)[d_6_i_]) < (d_4_smallestEven_))): + d_4_smallestEven_ = (nodes)[d_6_i_] + d_5_smallestIndex_ = d_6_i_ + d_6_i_ = d_6_i_ + 1 + if (d_5_smallestIndex_) == (-1): + result = list([]) + return result + else: + result = list([d_4_smallestEven_, d_5_smallestIndex_]) + return result diff --git a/Bench/070-strange_sort_list.py b/Bench/070-strange_sort_list.py new file mode 100644 index 0000000..f382d8c --- /dev/null +++ b/Bench/070-strange_sort_list.py @@ -0,0 +1,108 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +def strange__sort__list__helper(s : List[int]) -> Tuple[List[int], List[int]]: + Requires(Acc(list_pred(s))) + Ensures(Acc(list_pred(s))) + Ensures(Acc(list_pred(Result()[0]))) + Ensures(Acc(list_pred(Result()[1]))) + # Ensures(ToMS(ToSeq(s)) == ToMS(ToSeq(Result()[0]))) + Ensures(Forall(int, lambda d_0_i_: + Forall(int, lambda d_1_j_: + Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((Result()[0])))), ((Result()[0])[d_0_i_]) <= ((Result()[0])[d_1_j_]))))) + Ensures(((len(s)) == (len(Result()[0]))) and ((len(Result()[0])) == (len(Result()[1])))) + Ensures(Forall(int, lambda d_0_i_: + not ((((0) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) and (((d_0_i_ % 2)) == (0))) or (((Result()[1])[d_0_i_]) == ((Result()[0])[(d_0_i_ // 2)])))) + Ensures(Forall(int, lambda d_1_i_: + not ((((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) and (((d_1_i_ % 2)) == (1))) or (((Result()[1])[d_1_i_]) == ((Result()[0])[((len(s)) - ((((d_1_i_) - (1)) // 2))) - (1)])))) + sorted = list([int(0)] * 0) # type : List[int] + sorted = BubbleSort(s) # type : List[int] + strange = list(s) # type : List[int] + d_2_i_ = int(0) # type : int + d_2_i_ = 0 + while (d_2_i_) < (len(s)): + Invariant(Acc(list_pred(strange))) + Invariant(Acc(list_pred(sorted))) + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_2_i_)) and ((d_2_i_) <= (len(s)))) + Invariant(len(sorted) == len(s)) + Invariant((len(strange)) == len(s)) + Invariant(Forall(int, lambda d_0_i_: + Forall(int, lambda d_1_j_: + Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((sorted)))), ((sorted)[d_0_i_]) <= ((sorted)[d_1_j_]))))) + Invariant(Forall(int, lambda d_3_j_: + (Implies((((0) <= (d_3_j_)) and ((d_3_j_) < (d_2_i_))) and (((d_3_j_ % 2)) == (0)), ((strange)[d_3_j_]) == ((sorted)[(d_3_j_ // 2)])), [[(strange)[d_3_j_]]]))) + Invariant(Forall(int, lambda d_4_j_: + (Implies((((0) <= (d_4_j_)) and ((d_4_j_) < (d_2_i_))) and (((d_4_j_ % 2)) == (1)), ((strange)[d_4_j_]) == ((sorted)[((len(s)) - ((((d_4_j_) - (1)) // 2))) - (1)])), [[(strange)[d_4_j_]]]))) + if ((d_2_i_ % 2)) == (0): + strange[d_2_i_] = (sorted)[(d_2_i_ // 2)] + else: + d_5_r_ = int(0) # type : int + d_5_r_ = (((d_2_i_) - (1)) // 2) + strange[d_2_i_] = (sorted)[((len(s)) - (d_5_r_)) - (1)] + d_2_i_ = (d_2_i_) + (1) + return (sorted, strange) + +def strange__sort__list(s : List[int]) -> List[int]: + Requires(Acc(list_pred(s))) + Ensures(Acc(list_pred(s))) + Ensures(Acc(list_pred(Result()))) + Ensures((len(s)) == (len(Result()))) + p = strange__sort__list__helper(s) # type : Tuple[List[int], List[int]] + return p[1] + +def BubbleSort(a1 : List[int]) -> List[int]: + Requires(Acc(list_pred(a1))) + Ensures(Acc(list_pred(a1))) + Ensures(Acc(list_pred(Result()))) + Ensures((len(a1)) == (len(Result()))) + # Ensures(ToMS(ToSeq(a1)) == ToMS(ToSeq(Result()))) + Ensures(Forall(int, lambda d_0_i_: + Forall(int, lambda d_1_j_: + Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((Result())))), ((Result())[d_0_i_]) <= ((Result())[d_1_j_]))))) + a = list(a1) # type : List[int] + d_2_i_ = int(0) # type : int + d_2_i_ = (len((a))) - (1) + while (d_2_i_) > (0): + Invariant(Acc(list_pred(a))) + Invariant(Acc(list_pred(a1))) + Invariant((len(a1)) == (len(a))) + Invariant(not ((d_2_i_) < (0)) or ((len((a))) == (0))) + Invariant(((-1) <= (d_2_i_)) and ((d_2_i_) < (len((a))))) + Invariant(Forall(int, lambda d_3_ii_: + (Forall(int, lambda d_4_jj_: + (Implies((((d_2_i_) <= (d_3_ii_)) and ((d_3_ii_) < (d_4_jj_))) and ((d_4_jj_) < (len((a)))), ((a)[d_3_ii_]) <= ((a)[d_4_jj_])), + [[(a)[d_4_jj_]]])), + [[(a)[d_3_ii_]]]))) + Invariant(Forall(int, lambda d_5_k_: + (Forall(int, lambda d_6_k_k_: + (Implies(((((0) <= (d_5_k_)) and ((d_5_k_) <= (d_2_i_))) and ((d_2_i_) < (d_6_k_k_)) and (d_6_k_k_) < (len((a)))), ((a)[d_5_k_]) <= ((a)[d_6_k_k_])), + [[(a)[d_6_k_k_]]])), + [[(a)[d_5_k_]]]))) + d_7_j_ = int(0) # type : int + d_7_j_ = 0 + while (d_7_j_) < (d_2_i_): + Invariant(Acc(list_pred(a))) + Invariant(Acc(list_pred(a1))) + Invariant((len(a1)) == (len(a))) + Invariant((((0) < (d_2_i_)) and ((d_2_i_) < (len((a))))) and (((0) <= (d_7_j_)) and ((d_7_j_) <= (d_2_i_)))) + Invariant(Forall(int, lambda d_8_ii_: + (Forall(int, lambda d_9_jj_: + (Implies((((d_2_i_) <= (d_8_ii_)) and ((d_8_ii_) <= (d_9_jj_))) and ((d_9_jj_) < (len((a)))), ((a)[d_8_ii_]) <= ((a)[d_9_jj_])), + [[(a)[d_9_jj_]]])), + [[(a)[d_8_ii_]]]))) + Invariant(Forall(int, lambda d_10_k_: + (Forall(int, lambda d_11_k_k_: + (Implies(((((0) <= (d_10_k_)) and ((d_10_k_) <= (d_2_i_))) and ((d_2_i_) < (d_11_k_k_))) and ((d_11_k_k_) < (len((a)))), ((a)[d_10_k_]) <= ((a)[d_11_k_k_])), + [[(a)[d_11_k_k_]]])), + [[(a)[d_10_k_]]]))) + Invariant(Forall(int, lambda d_12_k_: + (Implies(((0) <= (d_12_k_)) and ((d_12_k_) <= (d_7_j_)), ((a)[d_12_k_]) <= ((a)[d_7_j_])), + [[(a)[d_12_k_]]]))) + if ((a)[d_7_j_]) > ((a)[(d_7_j_) + (1)]): + rhs0_ = (a)[(d_7_j_) + (1)] # type : int + (a)[(d_7_j_) + (1)] = (a)[d_7_j_] + (a)[d_7_j_] = rhs0_ + d_7_j_ = (d_7_j_) + (1) + d_2_i_ = (d_2_i_) - (1) + return a diff --git a/Bench/072-will_it_fly.py b/Bench/072-will_it_fly.py new file mode 100644 index 0000000..8793e95 --- /dev/null +++ b/Bench/072-will_it_fly.py @@ -0,0 +1,55 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +def will__it__fly(s : List[int], w : int) -> bool: + Requires(Acc(list_pred(s))) + Requires((len(s)) > (0)) + Ensures(Acc(list_pred(s))) + Ensures((Result()) == ((is__palindrome__pred(s)) and ((psum(0, len(s), s)) <= (w)))) + result = False # type : bool + result = True + d_0_i_ = int(0) # type : int + d_0_i_ = 0 + d_1_j_ = int(0) # type : int + d_1_j_ = (len(s)) - (1) + while (d_0_i_) < (d_1_j_): + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) + Invariant(((0) <= (d_1_j_)) and ((d_1_j_) < (len(s)))) + Invariant((d_1_j_) == (((len(s)) - (d_0_i_)) - (1))) + Invariant(Forall(int, lambda d_2_k_: + not (((0) <= (d_2_k_)) and ((d_2_k_) < (d_0_i_))) or (((s)[d_2_k_]) == ((s)[((len(s)) - (1)) - (d_2_k_)])))) + if ((s)[d_0_i_]) != ((s)[d_1_j_]): + result = False + return result + d_0_i_ = (d_0_i_) + (1) + d_1_j_ = (d_1_j_) - (1) + d_3_total_ = int(0) # type : int + d_3_total_ = 0 + d_0_i_ = 0 + while (d_0_i_) < (len(s)): + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_0_i_)) and ((d_0_i_) <= (len(s)))) + Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(s)))) or ((psum(0, d_2_i_ + 1, s)) == (psum(0, d_2_i_, s) + s[d_2_i_])), [[psum(0, d_2_i_ + 1, s)]]))) + Invariant((d_3_total_) == (psum(0, d_0_i_, s))) + Invariant(Forall(int, lambda d_2_k_: + not (((0) <= (d_2_k_)) and ((d_2_k_) < (len(s)))) or (((s)[d_2_k_]) == ((s)[((len(s)) - (1)) - (d_2_k_)])))) + Assert((psum(0, (d_0_i_) + (1), s)) == ((psum(0, d_0_i_, s) + (s)[d_0_i_]))) + d_3_total_ = (d_3_total_) + ((s)[d_0_i_]) + d_0_i_ = (d_0_i_) + (1) + return (d_3_total_) <= (w) + +@Pure +def is__palindrome__pred(s : List[int]) -> bool : + Requires(Acc(list_pred(s))) + return Forall(int, lambda d_4_k_: + not (((0) <= (d_4_k_)) and ((d_4_k_) < (len(s)))) or (((s)[d_4_k_]) == ((s)[((len(s)) - (1)) - (d_4_k_)]))) + +@Pure +def psum(i : int, j : int, s : List[int]) -> int : + Requires(Acc(list_pred(s))) + Requires(0 <= i and i <= j and j <= len(s)) + if i == j: + return 0 + else: + return (s)[j - 1] + (psum(i, j - 1, s)) diff --git a/Bench/075-is_multiply_prime.py b/Bench/075-is_multiply_prime.py new file mode 100644 index 0000000..70d2ca7 --- /dev/null +++ b/Bench/075-is_multiply_prime.py @@ -0,0 +1,69 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def Prime(p : int) -> bool : + return ((p) > (1)) and (Forall(int, lambda d_0_k_: + not (((1) < (d_0_k_)) and ((d_0_k_) < (p))) or (((p % d_0_k_)) != (0)))) + +def is__prime(k : int) -> bool: + Requires((k) >= (2)) + Ensures(not (Result()) or (Forall(int, lambda d_0_i_: + not (((2) <= (d_0_i_)) and ((d_0_i_) < (k))) or ((k % d_0_i_) != (0))))) + Ensures(not (not(Result())) or (Exists(int, lambda d_1_j_: + (((2) <= (d_1_j_)) and ((d_1_j_) < (k))) and (((k % d_1_j_)) == (0))))) + Ensures(Result() == Prime(k)) + result = False # type : bool + d_2_i_ = int(0) # type : int + d_2_i_ = 2 + result = True + while (d_2_i_) < (k): + Invariant(((2) <= (d_2_i_)) and ((d_2_i_) <= (k))) + Invariant(not (not(result)) or (Exists(int, lambda d_3_j_: + (((2) <= (d_3_j_)) and ((d_3_j_) < (d_2_i_))) and (((k % d_3_j_)) == (0))))) + Invariant(not (result) or (Forall(int, lambda d_4_j_: + not (((2) <= (d_4_j_)) and ((d_4_j_) < (d_2_i_))) or (((k % d_4_j_)) != (0))))) + if ((k % d_2_i_)) == (0): + result = False + d_2_i_ = (d_2_i_) + (1) + return result + +def is__multiply__prime(x : int) -> bool: + Requires((x) > (1)) + Ensures(Implies(Result(), Exists(int, lambda d_1_a_: + Exists(int, lambda d_2_b_: + (((Prime(d_1_a_)) and (Prime(d_2_b_)))) and ((x) == (((d_1_a_) * (d_2_b_)))))))) + Ensures(Implies(not(Result()), Forall(int, lambda d_10_i_: + (Forall(int, lambda d_11_j_: + (Implies((1 < d_10_i_ and 1 < d_11_j_ and (d_10_i_) < (x) and d_11_j_ < x), Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), ((x) != (((d_10_i_) * (d_11_j_))))))))))))) + d_4_a_ = int(2) # type : int + result = False # type : bool + while d_4_a_ < x: + Invariant(x >= 2) + Invariant(d_4_a_ >= 2 and d_4_a_ <= x) + Invariant(Implies(result, Exists(int, lambda d_1_a_: + Exists(int, lambda d_2_b_: + (d_1_a_ < x and d_2_b_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_)))) and ((x) == (((d_1_a_) * (d_2_b_)))))))) + Invariant(Implies(not(result), Forall(int, lambda d_10_i_: + (Forall(int, lambda d_11_j_: + (Implies((1 < d_10_i_ and 1 < d_11_j_ and (d_10_i_) < (d_4_a_) and d_11_j_ < x), Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), ((x) != (((d_10_i_) * (d_11_j_))))))), [[Prime(d_11_j_)]])), [[Prime(d_10_i_)]])))) + if is__prime(d_4_a_): + d_5_b_ = int(2) # type : int + while d_5_b_ < x: + Invariant(x >= 2) + Invariant(Prime(d_4_a_)) + Invariant(d_4_a_ >= 2 and d_4_a_ < x) + Invariant(d_5_b_ >= 2 and d_5_b_ <= x) + Invariant(Implies(result, Exists(int, lambda d_1_a_: + Exists(int, lambda d_2_b_: + (d_1_a_ < x and d_2_b_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_)))) and ((x) == (((d_1_a_) * (d_2_b_)))))))) + Invariant(Implies(not(result), Forall(int, lambda d_8_j_: + (Implies(1 < d_8_j_ and d_8_j_ < d_5_b_, Implies(Prime(d_8_j_), ((x) != (((d_4_a_) * (d_8_j_)))))), [[Prime(d_8_j_)]])))) + Invariant(Implies(not(result), Forall(int, lambda d_10_i_: + (Forall(int, lambda d_11_j_: + (Implies((1 < d_10_i_ and 1 < d_11_j_ and (d_10_i_) < (d_4_a_) and d_11_j_ < x), Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), ((x) != (((d_10_i_) * (d_11_j_))))))), [[Prime(d_11_j_)]])), [[Prime(d_10_i_)]])))) + if is__prime(d_5_b_) and x == d_4_a_ * d_5_b_: + result = True + d_5_b_ = d_5_b_ + 1 + d_4_a_ = d_4_a_ + 1 + return result diff --git a/Bench/078-hex_key.py b/Bench/078-hex_key.py new file mode 100644 index 0000000..8ce767b --- /dev/null +++ b/Bench/078-hex_key.py @@ -0,0 +1,36 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def IsPrimeHexDigit(c : int) -> bool : + return ((((((c) == (2)) or ((c) == (3))) or ((c) == (5))) or ((c) == (7))) or ((c) == (11))) or ((c) == (13)) + +@Pure +def count__prime__hex__digits__rec(i : int, j : int, num : List[int]) -> int : + Requires(Acc(list_pred(num))) + Requires(((0) <= (i)) and ((i) <= (j)) and ((j) <= (len(num)))) + if i == j: + return 0 + else: + return (1 if IsPrimeHexDigit(num[j - 1]) else 0) + count__prime__hex__digits__rec(i, j - 1, num) + + +def count__prime__hex__digits(s : List[int]) -> int: + Requires(Acc(list_pred(s))) + Ensures(Acc(list_pred(s))) + Ensures((Result()) == (count__prime__hex__digits__rec(0, len(s), s))) + Ensures(((0) <= (Result())) and ((Result()) <= (len(s)))) + count = int(0) # type : int + count = 0 + d_1_i_ = int(0) # type : int + d_1_i_ = 0 + while (d_1_i_) < (len(s)): + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(s)))) + Invariant((count) == (count__prime__hex__digits__rec(0, d_1_i_, s))) + Invariant(count >= 0 and count <= d_1_i_) + Invariant(Forall(int, lambda x : (Implies(x >= 0 and x < len(s), (count__prime__hex__digits__rec(0, x + 1, s)) == ((count__prime__hex__digits__rec(0, x, s) + ((1 if IsPrimeHexDigit((s)[x]) else 0))))), [[count__prime__hex__digits__rec(0, x + 1, s)]]))) + Assert((count__prime__hex__digits__rec(0, d_1_i_ + 1, s)) == ((count__prime__hex__digits__rec(0, d_1_i_, s) + ((1 if IsPrimeHexDigit((s)[d_1_i_]) else 0))))) + count = (count) + ((1 if IsPrimeHexDigit((s)[d_1_i_]) else 0)) + d_1_i_ = (d_1_i_) + (1) + return count diff --git a/Bench/080-is_happy.py b/Bench/080-is_happy.py new file mode 100644 index 0000000..07ca060 --- /dev/null +++ b/Bench/080-is_happy.py @@ -0,0 +1,37 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def ThreeDistinct(s : List[int], i : int) -> bool : + Requires(Acc(list_pred(s))) + Requires(((0) < (i)) and ((i) < ((len(s)) - (1)))) + return ((((s)[(i) - (1)]) != ((s)[i])) and (((s)[i]) != ((s)[(i) + (1)]))) and (((s)[(i) - (1)]) != ((s)[(i) + (1)])) + +@Pure +def Happy(s : List[int]) -> bool : + Requires(Acc(list_pred(s))) + return ((len(s)) >= (3)) and (Forall(int, lambda d_0_i_: + Implies(((0) < (d_0_i_)) and ((d_0_i_) < ((len(s)) - (1))), ThreeDistinct(s, d_0_i_)))) + +def IsHappy(s : List[int]) -> bool: + Requires(Acc(list_pred(s))) + Ensures(Acc(list_pred(s))) + Ensures((Result()) == (Happy(s))) + happy = False # type : bool + if (len(s)) < (3): + happy = False + return happy + d_1_i_ = int(0) # type : int + d_1_i_ = 1 + while (d_1_i_) < ((len(s)) - (1)): + Invariant(Acc(list_pred(s))) + Invariant(((0) < (d_1_i_)) and ((d_1_i_) <= ((len(s)) - (1)))) + Invariant(len(s) >= 3) + Invariant(Forall(int, lambda d_2_j_: + Implies(((0) < (d_2_j_)) and ((d_2_j_) < (d_1_i_)), ThreeDistinct(s, d_2_j_)))) + if not(ThreeDistinct(s, d_1_i_)): + happy = False + return happy + d_1_i_ = (d_1_i_) + (1) + happy = True + return happy diff --git a/Bench/082-prime-length.py b/Bench/082-prime-length.py new file mode 100644 index 0000000..103a6c1 --- /dev/null +++ b/Bench/082-prime-length.py @@ -0,0 +1,23 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def is__prime(s : str) -> bool: + Requires((len(s)) >= (2)) + Ensures(not (Result()) or (Forall(int, lambda d_0_i_: + not (((2) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) or ((len(s) % d_0_i_) != (0))))) + Ensures(not (not(Result())) or (Exists(int, lambda d_1_j_: + (((2) <= (d_1_j_)) and ((d_1_j_) < (len(s)))) and (((len(s) % d_1_j_)) == (0))))) + result = False # type : bool + d_2_i_ = int(0) # type : int + d_2_i_ = 2 + result = True + while (d_2_i_) < (len(s)): + Invariant(((2) <= (d_2_i_)) and ((d_2_i_) <= (len(s)))) + Invariant(not (not(result)) or (Exists(int, lambda d_3_j_: + (((2) <= (d_3_j_)) and ((d_3_j_) < (d_2_i_))) and (((len(s) % d_3_j_)) == (0))))) + Invariant(not (result) or (Forall(int, lambda d_4_j_: + not (((2) <= (d_4_j_)) and ((d_4_j_) < (d_2_i_))) or (((len(s) % d_4_j_)) != (0))))) + if ((len(s) % d_2_i_)) == (0): + result = False + d_2_i_ = (d_2_i_) + (1) + return result \ No newline at end of file diff --git a/Bench/083-starts_one_ends.py b/Bench/083-starts_one_ends.py new file mode 100644 index 0000000..912e181 --- /dev/null +++ b/Bench/083-starts_one_ends.py @@ -0,0 +1,21 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def CountNumbersStartingOrEndingWithOne(n : int) -> int: + Requires((n) > (0)) + Ensures(not ((n) == (1)) or ((Result()) == (1))) + Ensures(not ((n) > (1)) or ((Result()) == ((18) * (Pow(10, (n) - (2)))))) + count = int(0) # type : int + if (n) == (1): + count = 1 + elif True: + count = (18) * (Pow(10, (n) - (2))) + return count + +@Pure +def Pow(base : int, exponent : int) -> int : + Requires((exponent) >= (0)) + if (exponent) == (0): + return 1 + else: + return (base) * (Pow(base, (exponent) - (1))) \ No newline at end of file diff --git a/Bench/085-add.py b/Bench/085-add.py new file mode 100644 index 0000000..a1d1f0f --- /dev/null +++ b/Bench/085-add.py @@ -0,0 +1,27 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def psum(i : int, j : int, s : List[int]) -> int : + Requires(Acc(list_pred(s))) + Requires(0 <= i and i <= j + 1 and j < len(s)) + if i > j: + return 0 + else: + return ((s)[j] if ((((j) % 2) == 1) and ((s)[j] % 2 == 0)) else 0) + (psum(i, j - 1, s)) + +def add(v : List[int]) -> int: + Requires(Acc(list_pred(v))) + Ensures(Acc(list_pred(v))) + Ensures((Result()) == (psum(0, len(v) - 1, v))) + r = int(0) # type : int + r = 0 + d_2_k_ = int(0) # type : int + d_2_k_ = 0 + while (d_2_k_) < (len(v)): + Invariant(Acc(list_pred(v))) + Invariant(((0) <= (d_2_k_)) and ((d_2_k_) <= (len(v)))) + Invariant((r) == (psum(0, d_2_k_ - 1, v))) + r = (r) + (((v)[d_2_k_] if ((((d_2_k_) % 2) == 1) and ((v)[d_2_k_] % 2 == 0)) else 0)) + d_2_k_ = (d_2_k_) + (1) + return r diff --git a/Bench/088-sort_array.py b/Bench/088-sort_array.py new file mode 100644 index 0000000..982d66b --- /dev/null +++ b/Bench/088-sort_array.py @@ -0,0 +1,123 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +def sort__array(s : List[int]) -> List[int]: + Requires(Acc(list_pred(s))) + Ensures(Acc(list_pred(s))) + Ensures(Acc(list_pred(Result()))) + Ensures((len(Result())) == (len(s))) + Ensures(not (((len(s)) > (0)) and ((((((s)[0]) + ((s)[(len(s)) - (1)])) % 2)) == (0))) or (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(Result())))) or (((Result())[d_0_i_]) >= ((Result())[d_1_j_])))))) + Ensures(not (((len(s)) > (0)) and ((((((s)[0]) + ((s)[(len(s)) - (1)])) % 2)) != (0))) or (Forall(int, lambda d_2_i_: + Forall(int, lambda d_3_j_: + not ((((0) <= (d_2_i_)) and ((d_2_i_) < (d_3_j_))) and ((d_3_j_) < (len(Result())))) or (((Result())[d_2_i_]) <= ((Result())[d_3_j_])))))) + sorted = list([int(0)] * 0) # type : List[int] + if (len(s)) == (0): + sorted = list([]) + return sorted + elif (((((s)[0]) + ((s)[(len(s)) - (1)])) % 2)) == (0): + Assert(len(s) > 0) + d_4_t_ = list([int(0)] * 0) # type : List[int] + d_4_t_ = BubbleSort(s) + Assert(Forall(int, lambda d_0_i_: + Forall(int, lambda d_1_j_: + Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((d_4_t_)))), ((d_4_t_)[d_0_i_]) <= ((d_4_t_)[d_1_j_]))))) + sorted = reverse(d_4_t_) + Assert(Forall(int, lambda d_0_i_: + Forall(int, lambda d_1_j_: + Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((sorted)))), ((sorted)[d_0_i_]) >= ((sorted)[d_1_j_]))))) + Assert(((len(s)) > (0)) and ((((((s)[0]) + ((s)[(len(s)) - (1)])) % 2)) == (0))) + return sorted + else: + Assert(len(s) > 0) + sorted = BubbleSort(s) + Assert(Forall(int, lambda d_0_i_: + Forall(int, lambda d_1_j_: + Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((sorted)))), ((sorted)[d_0_i_]) <= ((sorted)[d_1_j_]))))) + Assert(((len(s)) > (0)) and ((((((s)[0]) + ((s)[(len(s)) - (1)])) % 2)) != (0))) + return sorted + +def reverse(str : List[int]) -> List[int]: + Requires(Acc(list_pred(str), 1/2)) + Requires(Forall(int, lambda x: Forall(int, lambda y: not (((0) <= (x)) and ((x) < (y)) and ((y) < (len(str)))) or ((str)[x] <= (str)[y])))) + Ensures(Acc(list_pred(str), 1/2)) + Ensures(Acc(list_pred(Result()))) + Ensures(Forall(int, lambda x: Forall(int, lambda y: not (((0) <= (x)) and ((x) < (y)) and ((y) < (len(Result())))) or ((Result())[x] >= (Result())[y])))) + 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(Forall(int, lambda x: Forall(int, lambda y: not (((0) <= (x)) and ((x) < (y)) and ((y) < (len(str)))) or ((str)[x] <= (str)[y])))) + 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_)])))) + Invariant(Forall(int, lambda x: Forall(int, lambda y: (not (((0) <= (x)) and ((x) < (len(rev))) and (0 <= y and (y) < (len(str) - d_12_i_))) or ((str)[y] <= (rev)[x]), [[str[y] <= rev[x]]])))) + Invariant(Forall(int, lambda x: Forall(int, lambda y: (not (((0) <= (x)) and ((x) < (y)) and ((y) < (len(rev)))) or ((rev)[x] >= (rev)[y]), [[rev[x] >= rev[y]]])))) + rev = (rev) + [(str)[(len(str) - (d_12_i_)) - (1)]] + d_12_i_ = (d_12_i_) + (1) + return rev + +def BubbleSort(a1 : List[int]) -> List[int]: + Requires(Acc(list_pred(a1), 1/2)) + Ensures(Acc(list_pred(a1), 1/2)) + Ensures(Acc(list_pred(Result()))) + Ensures((len(a1)) == (len(Result()))) + # Ensures(ToMS(ToSeq(a1)) == ToMS(ToSeq(Result()))) + Ensures(Forall(int, lambda d_0_i_: + Forall(int, lambda d_1_j_: + Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((Result())))), ((Result())[d_0_i_]) <= ((Result())[d_1_j_]))))) + a = list(a1) # type : List[int] + d_2_i_ = int(0) # type : int + d_2_i_ = (len((a))) - (1) + while (d_2_i_) > (0): + Invariant(Acc(list_pred(a))) + Invariant(Acc(list_pred(a1), 1/2)) + Invariant((len(a1)) == (len(a))) + Invariant(not ((d_2_i_) < (0)) or ((len((a))) == (0))) + Invariant(((-1) <= (d_2_i_)) and ((d_2_i_) < (len((a))))) + Invariant(Forall(int, lambda d_3_ii_: + (Forall(int, lambda d_4_jj_: + (Implies((((d_2_i_) <= (d_3_ii_)) and ((d_3_ii_) < (d_4_jj_))) and ((d_4_jj_) < (len((a)))), ((a)[d_3_ii_]) <= ((a)[d_4_jj_])), + [[(a)[d_4_jj_]]])), + [[(a)[d_3_ii_]]]))) + Invariant(Forall(int, lambda d_5_k_: + (Forall(int, lambda d_6_k_k_: + (Implies(((((0) <= (d_5_k_)) and ((d_5_k_) <= (d_2_i_))) and ((d_2_i_) < (d_6_k_k_)) and (d_6_k_k_) < (len((a)))), ((a)[d_5_k_]) <= ((a)[d_6_k_k_])), + [[(a)[d_6_k_k_]]])), + [[(a)[d_5_k_]]]))) + d_7_j_ = int(0) # type : int + d_7_j_ = 0 + while (d_7_j_) < (d_2_i_): + Invariant(Acc(list_pred(a))) + Invariant(Acc(list_pred(a1), 1/2)) + Invariant((len(a1)) == (len(a))) + Invariant((((0) < (d_2_i_)) and ((d_2_i_) < (len((a))))) and (((0) <= (d_7_j_)) and ((d_7_j_) <= (d_2_i_)))) + Invariant(Forall(int, lambda d_8_ii_: + (Forall(int, lambda d_9_jj_: + (Implies((((d_2_i_) <= (d_8_ii_)) and ((d_8_ii_) <= (d_9_jj_))) and ((d_9_jj_) < (len((a)))), ((a)[d_8_ii_]) <= ((a)[d_9_jj_])), + [[(a)[d_9_jj_]]])), + [[(a)[d_8_ii_]]]))) + Invariant(Forall(int, lambda d_10_k_: + (Forall(int, lambda d_11_k_k_: + (Implies(((((0) <= (d_10_k_)) and ((d_10_k_) <= (d_2_i_))) and ((d_2_i_) < (d_11_k_k_))) and ((d_11_k_k_) < (len((a)))), ((a)[d_10_k_]) <= ((a)[d_11_k_k_])), + [[(a)[d_11_k_k_]]])), + [[(a)[d_10_k_]]]))) + Invariant(Forall(int, lambda d_12_k_: + (Implies(((0) <= (d_12_k_)) and ((d_12_k_) <= (d_7_j_)), ((a)[d_12_k_]) <= ((a)[d_7_j_])), + [[(a)[d_12_k_]]]))) + if ((a)[d_7_j_]) > ((a)[(d_7_j_) + (1)]): + rhs0_ = (a)[(d_7_j_) + (1)] # type : int + (a)[(d_7_j_) + (1)] = (a)[d_7_j_] + (a)[d_7_j_] = rhs0_ + d_7_j_ = (d_7_j_) + (1) + d_2_i_ = (d_2_i_) - (1) + return a diff --git a/Bench/092-any_int.py b/Bench/092-any_int.py new file mode 100644 index 0000000..d2ff5d6 --- /dev/null +++ b/Bench/092-any_int.py @@ -0,0 +1,7 @@ +from nagini_contracts.contracts import * + +def any__int(a : int, b : int, c : int) -> bool: + Ensures((Result()) == ((((a) == ((b) + (c))) or ((b) == ((a) + (c)))) or ((c) == ((a) + (b))))) + r = False # type : bool + r = (((a) == ((b) + (c))) or ((b) == ((a) + (c)))) or ((c) == ((a) + (b))) + return r \ No newline at end of file diff --git a/Bench/097-multiply.py b/Bench/097-multiply.py new file mode 100644 index 0000000..f2ff39f --- /dev/null +++ b/Bench/097-multiply.py @@ -0,0 +1,16 @@ +from nagini_contracts.contracts import * + +@Pure +def last__digit(n : int) -> int : + if (n) < (0): + return (((0) - (n)) % 10) + elif True: + return (n % 10) + +def multiply(a : int, b : int) -> int: + Requires((a) >= (0)) + Requires((b) >= (0)) + Ensures((Result()) == ((last__digit(a)) * (last__digit(b)))) + c = int(0) # type : int + c = (last__digit(a)) * (last__digit(b)) + return c diff --git a/Bench/100-make_a_pile.py b/Bench/100-make_a_pile.py new file mode 100644 index 0000000..088d31f --- /dev/null +++ b/Bench/100-make_a_pile.py @@ -0,0 +1,29 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def make__a__pile(n : int) -> List[int]: + Requires((n) >= (0)) + Ensures(Acc(list_pred(Result()))) + Ensures((len(Result())) == (n)) + Ensures(Forall(int, lambda d_0_i_: + Implies(((1) <= (d_0_i_)) and ((d_0_i_) < (n)), ((Result())[d_0_i_]) == (((Result())[(d_0_i_) - (1)]) + (2))))) + Ensures(Implies((n) > (0), ((Result())[0]) == (n))) + pile = list([int(0)] * 0) # type : List[int] + pile = list([]) + if (n) == (0): + return pile + pile = [n] + d_1_i_ = int(0) # type : int + d_1_i_ = 1 + while (d_1_i_) < (n): + Invariant(Acc(list_pred(pile))) + Invariant(len(pile) == d_1_i_) + Invariant(len(pile) > 0) + Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (n))) + Invariant((len(pile)) == (d_1_i_)) + Invariant(Forall(int, lambda d_2_j_: + Implies(((1) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_)), ((pile)[d_2_j_]) == (((pile)[(d_2_j_) - (1)]) + (2))))) + Invariant(Implies((n) > (0), ((pile)[0]) == (n))) + pile = (pile) + [((pile)[(d_1_i_) - (1)]) + (2)] + d_1_i_ = (d_1_i_) + (1) + return pile diff --git a/Bench/102-choose_num.py b/Bench/102-choose_num.py new file mode 100644 index 0000000..89afcaf --- /dev/null +++ b/Bench/102-choose_num.py @@ -0,0 +1,30 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def choose__num(x : int, y : int) -> int: + Requires(((0) <= (x)) and ((0) <= (y))) + Ensures(((Result()) == (-1)) or (((Result()) >= (x)) and ((Result()) <= (y)))) + Ensures(((Result()) == (-1)) or (((Result() % 2)) == (0))) + Ensures(((Result()) == (-1)) or (Forall(int, lambda d_0_i_: + not ((((x) <= (d_0_i_)) and ((d_0_i_) <= (y))) and (((d_0_i_ % 2)) == (0))) or ((Result()) >= (d_0_i_))))) + Ensures(((Result()) == (-1)) == ((x) >= (y))) + num = int(0) # type : int + num = -1 + if (x) >= (y): + return num + if ((x % 2)) == (0): + num = x + elif True: + num = (x) + (1) + d_1_i_ = int(0) # type : int + d_1_i_ = (x) + (2) + while (d_1_i_) <= (y): + Invariant(((d_1_i_) >= (x)) and ((d_1_i_) <= ((y) + (1)))) + Invariant(((num) == (-1)) or (((num % 2)) == (0))) + Invariant(Forall(int, lambda d_2_j_: + not ((((x) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_))) and (((d_2_j_ % 2)) == (0))) or ((num) >= (d_2_j_)))) + Invariant(((num) == (-1)) or (((num) >= (x)) and ((num) < (d_1_i_)))) + if ((d_1_i_ % 2)) == (0): + num = d_1_i_ + d_1_i_ = (d_1_i_) + (1) + return num diff --git a/Bench/106-f.py b/Bench/106-f.py new file mode 100644 index 0000000..b776caa --- /dev/null +++ b/Bench/106-f.py @@ -0,0 +1,79 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def factorial__spec(n : int) -> int : + Requires(n >= -1) + Ensures(Result() >= 0) + if n == -1: + return 1 + else: + return (n + 1) * factorial__spec(n - 1) + +@Pure +def sum__spec(n : int) -> int : + Requires(n >= -1) + Ensures(Result() >= 0) + if 0 > n: + return 0 + else: + return n + 1 + sum__spec(n - 1) + +def f(n : int) -> List[int]: + Requires((n) >= (1)) + Ensures(Acc(list_pred(Result()))) + Ensures((len(Result())) == (n)) + Ensures(Forall(int, lambda d_2_i_: + not ((((d_2_i_) >= (0)) and ((d_2_i_) < (len(Result())))) and (((d_2_i_ % 2)) == (0))) or (((Result())[d_2_i_]) == (factorial__spec(d_2_i_ - 1))))) + Ensures(Forall(int, lambda d_3_i_: + not ((((d_3_i_) >= (0)) and ((d_3_i_) < (len(Result())))) and (((d_3_i_ % 2)) != (0))) or (((Result())[d_3_i_]) == (sum__spec(d_3_i_ - 1))))) + result = list([int(0)] * 0) # type : List[int] + result = list([]) + d_4_i_ = int(0) # type : int + d_4_i_ = 0 + while (d_4_i_) < (n): + Invariant(Acc(list_pred(result))) + Invariant(((d_4_i_) >= (0)) and ((d_4_i_) <= (n))) + Invariant((len(result)) == (d_4_i_)) + Invariant(Forall(int, lambda d_5_i_: + (Implies((((d_5_i_) >= (0)) and ((d_5_i_) < (len(result)))) and (((d_5_i_ % 2)) == (0)), ((result)[d_5_i_]) == (factorial__spec(d_5_i_ - 1))), [[factorial__spec(d_5_i_ - 1)]]))) + Invariant(Forall(int, lambda d_6_i_: + (Implies((((d_6_i_) >= (0)) and ((d_6_i_) < (len(result)))) and (((d_6_i_ % 2)) != (0)), ((result)[d_6_i_]) == (sum__spec(d_6_i_ - 1))), [[sum__spec(d_6_i_ - 1)]]))) + if ((d_4_i_ % 2)) == (0): + d_7_x_ = int(0) # type : int + d_7_x_ = 1 + d_8_j_ = int(0) # type : int + d_8_j_ = 0 + while (d_8_j_) < (d_4_i_): + Invariant(Acc(list_pred(result))) + Invariant(((d_4_i_) >= (0)) and ((d_4_i_) <= (n))) + Invariant((len(result)) == (d_4_i_)) + Invariant(((d_8_j_) >= (0)) and ((d_8_j_) <= (d_4_i_))) + Invariant((d_7_x_) == (factorial__spec(d_8_j_ - 1))) + Invariant(Forall(int, lambda d_5_i_: + (Implies((((d_5_i_) >= (0)) and ((d_5_i_) < (len(result)))) and (((d_5_i_ % 2)) == (0)), ((result)[d_5_i_]) == (factorial__spec(d_5_i_ - 1))), [[factorial__spec(d_5_i_ - 1)]]))) + Invariant(Forall(int, lambda d_6_i_: + (Implies((((d_6_i_) >= (0)) and ((d_6_i_) < (len(result)))) and (((d_6_i_ % 2)) != (0)), ((result)[d_6_i_]) == (sum__spec(d_6_i_ - 1))), [[sum__spec(d_6_i_ - 1)]]))) + d_7_x_ = (d_7_x_) * (d_8_j_ + 1) + d_8_j_ = (d_8_j_) + (1) + result = (result) + [d_7_x_] + else: + d_9_x_ = int(0) # type : int + d_9_x_ = 0 + d_10_j_ = int(0) # type : int + d_10_j_ = 0 + while (d_10_j_) < (d_4_i_): + Invariant(Acc(list_pred(result))) + Invariant(((d_4_i_) >= (0)) and ((d_4_i_) <= (n))) + Invariant((len(result)) == (d_4_i_)) + Invariant(((d_10_j_) >= (0)) and ((d_10_j_) <= (d_4_i_))) + Invariant((d_9_x_) == (sum__spec(d_10_j_ - 1))) + Invariant(Forall(int, lambda d_5_i_: + (Implies((((d_5_i_) >= (0)) and ((d_5_i_) < (len(result)))) and (((d_5_i_ % 2)) == (0)), ((result)[d_5_i_]) == (factorial__spec(d_5_i_ - 1))), [[factorial__spec(d_5_i_ - 1)]]))) + Invariant(Forall(int, lambda d_6_i_: + (Implies((((d_6_i_) >= (0)) and ((d_6_i_) < (len(result)))) and (((d_6_i_ % 2)) != (0)), ((result)[d_6_i_]) == (sum__spec(d_6_i_ - 1))), [[sum__spec(d_6_i_ - 1)]]))) + d_9_x_ = (d_9_x_) + (d_10_j_ + 1) + d_10_j_ = (d_10_j_) + (1) + result = (result) + [d_9_x_] + d_4_i_ = (d_4_i_) + (1) + return result diff --git a/Bench/110-exchange.py b/Bench/110-exchange.py new file mode 100644 index 0000000..43482e1 --- /dev/null +++ b/Bench/110-exchange.py @@ -0,0 +1,35 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def IsEven(n : int) -> bool: + Ensures(((Result()) == (True)) or ((Result()) == (False))) + return (n % 2) == 0 + +@Pure +def CountEvens(i : int, lst : List[int]) -> int: + Requires(Acc(list_pred(lst))) + Requires(((i) >= (0)) and ((i) <= (len(lst)))) + Ensures((Result()) >= (0)) + Ensures((Result()) <= (len(lst) - i)) + if len(lst) == i: + return 0 + return CountEvens(i + 1, lst) + IsEven(lst[i]) + +def Exchange(lst1 : List[int], lst2 : List[int]) -> str: + Requires(Acc(list_pred(lst2))) + Requires(Acc(list_pred(lst1))) + Requires(((len(lst1)) > (0)) and ((len(lst2)) > (0))) + Ensures(Acc(list_pred(lst2))) + Ensures(Acc(list_pred(lst1))) + Ensures(((Result()) == "YES") or ((Result()) == "NO")) + Ensures(not ((Result()) == "YES") or (((CountEvens(0, lst1)) + (CountEvens(0, lst2))) >= (len(lst1)))) + Ensures(not ((Result()) == "NO") or (((CountEvens(0, lst1)) + (CountEvens(0, lst2))) < (len(lst1)))) + result = "" # type : str + d_1_totalEvens_ = int(0) # type : int + d_1_totalEvens_ = CountEvens(0, lst1) + CountEvens(0, lst2) + if (d_1_totalEvens_) >= (len(lst1)): + result = "YES" + elif True: + result = "NO" + return result diff --git a/Bench/118-get_closest_vowel.py b/Bench/118-get_closest_vowel.py new file mode 100644 index 0000000..446fb26 --- /dev/null +++ b/Bench/118-get_closest_vowel.py @@ -0,0 +1,43 @@ +from typing import List +from nagini_contracts.contracts import * + +@Pure +def IsVowel(c : int) -> bool : + return ((((((((((c) == (97)) or ((c) == (101))) or ((c) == (105))) or ((c) == (111))) or ((c) == (117))) or ((c) == (65))) or ((c) == (69))) or ((c) == (73))) or ((c) == (79))) or ((c) == (85)) + +@Pure +def IsConsonant(c : int) -> bool : + return ((((65) <= (c)) and ((c) <= (90))) or (((97) <= (c)) and ((c) <= (122)))) and (not(IsVowel(c))) + +def get__closest__vowel(word : List[int]) -> List[int]: + Requires(Acc(list_pred(word))) + Requires(Forall(int, lambda d_0_i_: + not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(word)))) or ((((65) <= ((word)[d_0_i_])) and (((word)[d_0_i_]) <= (90))) or (((97) <= ((word)[d_0_i_])) and (((word)[d_0_i_]) <= (122)))))) + Ensures(Acc(list_pred(word))) + Ensures(Acc(list_pred(Result()))) + Ensures((len(Result())) <= (1)) + Ensures(not ((len(Result())) == (1)) or (IsVowel((Result())[0]))) + Ensures(not ((len(Result())) == (1)) or (Exists(int, lambda d_1_i_: + ((((((1) <= (d_1_i_)) and (((d_1_i_) + (1)) < (len(word)))) and (IsVowel((word)[d_1_i_]))) and (IsConsonant((word)[(d_1_i_) - (1)]))) and (IsConsonant((word)[(d_1_i_) + (1)]))) and ( + Forall(int, lambda j: + not (j > d_1_i_ and j <= len(word) - 2) or (((not(IsVowel((word)[j]))) or (not(IsConsonant((word)[j - 1])))) or (not(IsConsonant((word)[j + 1]))))))))) + result = list([int(0)] * 0) # type : List[int] + if (len(word)) < (3): + result = [] + return result + d_5_i_ = int(0) # type : int + d_5_i_ = (len(word)) - (2) + while (d_5_i_) > (0): + Invariant(Acc(list_pred(word))) + Invariant(Acc(list_pred(result))) + Invariant(((0) <= (d_5_i_)) and ((d_5_i_) <= ((len(word)) - (2)))) + Invariant(Forall(int, lambda d_6_j_: + not (((d_5_i_) < (d_6_j_)) and ((d_6_j_) < ((len(word)) - (1)))) or (((not(IsVowel((word)[d_6_j_]))) or (not(IsConsonant((word)[(d_6_j_) - (1)])))) or (not(IsConsonant((word)[(d_6_j_) + (1)])))))) + Invariant(Forall(int, lambda j: + not (j > d_5_i_ and j <= len(word) - 2) or (((not(IsVowel((word)[j]))) or (not(IsConsonant((word)[j - 1])))) or (not(IsConsonant((word)[j + 1])))))) + if ((IsVowel((word)[d_5_i_])) and (IsConsonant((word)[(d_5_i_) - (1)]))) and (IsConsonant((word)[(d_5_i_) + (1)])): + result = [(word)[d_5_i_]] + return result + d_5_i_ = (d_5_i_) - (1) + result = [] + return result diff --git a/Bench/122-add_elements.py b/Bench/122-add_elements.py new file mode 100644 index 0000000..f491786 --- /dev/null +++ b/Bench/122-add_elements.py @@ -0,0 +1,33 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def sum__chars__rec(i : int, j : int, lst : List[int]) -> int : + Requires(Acc(list_pred(lst))) + Requires(((0) <= (i)) and ((i) <= (j)) and ((j) <= (len(lst)))) + if i == j: + return 0 + else: + return ((lst)[j - 1] if (lst)[j - 1] < 100 else 0) + sum__chars__rec(i, j - 1, lst) + +def SumElementsWithAtMostTwoDigits(lst : List[int], k : int) -> int: + Requires(Acc(list_pred(lst))) + Requires(1 <= k and k <= len(lst)) + Ensures(Acc(list_pred(lst))) + Ensures(1 <= k and k <= len(lst)) + Ensures(Result() == sum__chars__rec(0, k, lst)) + sum = int(0) # type : int + sum = 0 + d_3_i_ = int(0) # type : int + d_3_i_ = 0 + while d_3_i_ < k: + Invariant(Acc(list_pred(lst))) + Invariant(1 <= k and k <= len(lst)) + Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(lst))) and d_3_i_ <= k) + Invariant(Forall(int, lambda d_3_i_ : (Implies(d_3_i_ >= 0 and d_3_i_ < k, sum__chars__rec(0, d_3_i_ + 1, lst) == sum__chars__rec(0, d_3_i_, lst) + ((lst)[d_3_i_] if (lst)[d_3_i_] < 100 else 0)), [[sum__chars__rec(0, d_3_i_ + 1, lst)]]))) + Invariant((sum) == (sum__chars__rec(0, d_3_i_, lst))) + Assert(sum__chars__rec(0, d_3_i_ + 1, lst) == sum__chars__rec(0, d_3_i_, lst) + ((lst)[d_3_i_] if (lst)[d_3_i_] < 100 else 0)) + if (lst)[d_3_i_] < 100: + sum = (sum) + ((lst)[d_3_i_]) + d_3_i_ = (d_3_i_) + (1) + return sum \ No newline at end of file diff --git a/Bench/127-intersection.py b/Bench/127-intersection.py new file mode 100644 index 0000000..44978eb --- /dev/null +++ b/Bench/127-intersection.py @@ -0,0 +1,39 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def IsPrime(n : int) -> bool : + return ((n) > (1)) and (Forall(int, lambda d_0_k_: + not (((2) <= (d_0_k_)) and ((d_0_k_) < (n))) or (((n % d_0_k_)) != (0)))) + +@Pure +def min(a : int, b : int) -> int : + if (a) <= (b): + return a + elif True: + return b + +@Pure +def max(a : int, b : int) -> int : + if (a) >= (b): + return a + elif True: + return b + +def Intersection(start1 : int, end1 : int, start2 : int, end2 : int) -> str: + Requires(((start1) <= (end1)) and ((start2) <= (end2))) + Ensures(((Result()) == ("YES")) or ((Result()) == ("NO"))) + Ensures(((Result()) == ("YES")) == (((max(start1, start2)) <= (min(end1, end2))) and (IsPrime(((min(end1, end2)) - (max(start1, start2))) + (1))))) + result = "" # type : str + d_1_intersectionStart_ = int(0) # type : int + d_1_intersectionStart_ = max(start1, start2) + d_2_intersectionEnd_ = int(0) # type : int + d_2_intersectionEnd_ = min(end1, end2) + if (d_1_intersectionStart_) <= (d_2_intersectionEnd_): + d_3_length_ = int(0) # type : int + d_3_length_ = ((d_2_intersectionEnd_) - (d_1_intersectionStart_)) + (1) + if IsPrime(d_3_length_): + result = "YES" + return result + result = "NO" + return result diff --git a/Bench/133-sum-squares.py b/Bench/133-sum-squares.py new file mode 100644 index 0000000..9f9dbc4 --- /dev/null +++ b/Bench/133-sum-squares.py @@ -0,0 +1,28 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def psum(i : int, j : int, s : List[int]) -> int : + Requires(Acc(list_pred(s))) + Requires(0 <= i and i <= j and j <= len(s)) + if i == j: + return 0 + else: + return (s)[j - 1] * (s)[j - 1] + (psum(i, j - 1, s)) + +def sum__squares(lst : List[int]) -> int: + Requires(Acc(list_pred(lst))) + Ensures(Acc(list_pred(lst))) + Ensures((Result()) == (psum(0, len(lst), lst))) + r = int(0) # type : int + r = 0 + d_2_k_ = int(0) # type : int + d_2_k_ = 0 + while (d_2_k_) < (len(lst)): + Invariant(Acc(list_pred(lst))) + Invariant(((0) <= (d_2_k_)) and ((d_2_k_) <= (len(lst)))) + Invariant((r) == (psum(0, d_2_k_, lst))) + Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(lst)))) or ((psum(0, d_2_i_ + 1, lst)) == (psum(0, d_2_i_, lst) + lst[d_2_i_] * lst[d_2_i_])), [[psum(0, d_2_i_ + 1, lst)]]))) + r = (r) + ((lst)[d_2_k_]) * ((lst)[d_2_k_]) + d_2_k_ = (d_2_k_) + (1) + return r diff --git a/Bench/139-special_factorial.py b/Bench/139-special_factorial.py new file mode 100644 index 0000000..14f8e50 --- /dev/null +++ b/Bench/139-special_factorial.py @@ -0,0 +1,40 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def factorial(n : int) -> int : + Requires((n) >= (0)) + Ensures((Result()) >= (0)) + if (n) == (0): + return 1 + else: + return (n) * (factorial((n) - (1))) + +@Pure +def special__factorial__rec(n : int) -> int : + Requires((n) >= (0)) + Ensures((Result()) >= (0)) + if (n) == (0): + return 1 + else: + return factorial(n) * (special__factorial__rec((n) - (1))) + +def special__factorial(n : int) -> int: + Requires((n) > (0)) + Ensures((Result()) == (special__factorial__rec(n))) + result = int(0) # type : int + result = 1 + d_2_fact_ = int(0) # type : int + d_2_fact_ = 1 + d_3_i_ = int(0) # type : int + d_3_i_ = 1 + while (d_3_i_) <= (n): + Invariant(((1) <= (d_3_i_)) and ((d_3_i_) <= (n + 1))) + Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) <= (n))) or ((factorial(d_2_i_ + 1)) == (factorial(d_2_i_) * (d_2_i_ + 1))), [[factorial(d_2_i_ + 1)]]))) + Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) <= (n))) or ((special__factorial__rec(d_2_i_ + 1)) == (special__factorial__rec(d_2_i_) * factorial(d_2_i_ + 1))), [[special__factorial__rec(d_2_i_ + 1)]]))) + Invariant((result) == (special__factorial__rec(d_3_i_ - 1))) + Invariant((d_2_fact_) == (factorial(d_3_i_ - 1))) + d_2_fact_ = (d_2_fact_) * (d_3_i_) + result = (result) * (d_2_fact_) + d_3_i_ = (d_3_i_) + (1) + return result diff --git a/Bench/142-sum_squares.py b/Bench/142-sum_squares.py new file mode 100644 index 0000000..b7571de --- /dev/null +++ b/Bench/142-sum_squares.py @@ -0,0 +1,38 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def checkVal(x : int) -> int: + if x % 3 == 0: + return x * x + elif x % 4 == 0 and x % 3 != 0: + return x * x * x + else: + return x + +@Pure +def psum(i : int, j : int, s : List[int]) -> int : + Requires(Acc(list_pred(s))) + Requires(0 <= i and i <= j and j <= len(s)) + if i == j: + return 0 + else: + return checkVal((s)[j - 1]) + (psum(i, j - 1, s)) + +def sum__squares(lst : List[int]) -> int: + Requires(Acc(list_pred(lst))) + Ensures(Acc(list_pred(lst))) + Ensures((Result()) == (psum(0, len(lst), lst))) + r = int(0) # type : int + r = 0 + d_2_k_ = int(0) # type : int + d_2_k_ = 0 + while (d_2_k_) < (len(lst)): + Invariant(Acc(list_pred(lst))) + Invariant(((0) <= (d_2_k_)) and ((d_2_k_) <= (len(lst)))) + Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(lst)))) or + (psum(0, d_2_i_ + 1, lst) == checkVal(lst[d_2_i_]) + psum(0, d_2_i_, lst)), [[psum(0, d_2_i_ + 1, lst)]]))) + Invariant((r) == (psum(0, d_2_k_, lst))) + r = r + checkVal(lst[d_2_k_]) + d_2_k_ = (d_2_k_) + (1) + return r diff --git a/Bench/150-x_or_y.py b/Bench/150-x_or_y.py new file mode 100644 index 0000000..17e8d20 --- /dev/null +++ b/Bench/150-x_or_y.py @@ -0,0 +1,22 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def IsPrime(n : int) -> bool : + return ((n) > (1)) and (Forall(int, lambda d_0_k_: + not (((2) <= (d_0_k_)) and ((d_0_k_) < (n))) or (((n % d_0_k_)) != (0)))) + + +def x__or__y(n : int, x : int, y : int) -> int: + Requires(n > 1) + Ensures(not (IsPrime(n)) or ((Result()) == (x))) + Ensures(not (not(IsPrime(n))) or ((Result()) == (y))) + + i = 2 + while i < n: + Invariant(2 <= i and i <= n) + Invariant(Forall(int, lambda d_0_k_: not (2 <= d_0_k_ and d_0_k_ < i) or (n % d_0_k_ != 0))) + if n % i == 0: + return y + i += 1 + return x diff --git a/Bench/151-double_the_difference.py b/Bench/151-double_the_difference.py new file mode 100644 index 0000000..b4078f6 --- /dev/null +++ b/Bench/151-double_the_difference.py @@ -0,0 +1,37 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def checkVal(x : int) -> int: + if x > 0 and x % 2 != 0: + return x * x + else: + return 0 + +@Pure +def psum(i : int, j : int, s : List[int]) -> int : + Requires(Acc(list_pred(s))) + Requires(0 <= i and i <= j and j <= len(s)) + if i == j: + return 0 + else: + return checkVal((s)[j - 1]) + (psum(i, j - 1, s)) + + +def double__the__difference(lst : List[int]) -> int: + Requires(Acc(list_pred(lst))) + Ensures(Acc(list_pred(lst))) + Ensures((Result()) == (psum(0, len(lst), lst))) + r = int(0) # type : int + r = 0 + d_3_k_ = int(0) # type : int + d_3_k_ = 0 + while (d_3_k_) < (len(lst)): + Invariant(Acc(list_pred(lst))) + Invariant(((0) <= (d_3_k_)) and ((d_3_k_) <= (len(lst)))) + Invariant((r) == (psum(0, d_3_k_, lst))) + Invariant(Forall(int, lambda d_3_i_: (not (((0) <= (d_3_i_)) and ((d_3_i_) < (len(lst)))) or + (psum(0, d_3_i_ + 1, lst) == checkVal(lst[d_3_i_]) + psum(0, d_3_i_, lst)), [[psum(0, d_3_i_ + 1, lst)]]))) + r = (r) + checkVal(((lst)[d_3_k_])) + d_3_k_ = (d_3_k_) + (1) + return r \ No newline at end of file diff --git a/Bench/152-compare.py b/Bench/152-compare.py new file mode 100644 index 0000000..2aa9219 --- /dev/null +++ b/Bench/152-compare.py @@ -0,0 +1,38 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def Compare(scores : List[int], guesses : List[int]) -> List[int]: + Requires(Acc(list_pred(guesses))) + Requires(Acc(list_pred(scores))) + Requires((len((scores))) == (len((guesses)))) + Ensures(Acc(list_pred(guesses))) + Ensures(Acc(list_pred(scores))) + Ensures(Acc(list_pred(Result()))) + Ensures((len((Result()))) == (len((scores)))) + Ensures((len((scores))) == (len((guesses)))) + Ensures(Forall(int, lambda d_0_i_: + not (((0) <= (d_0_i_)) and ((d_0_i_) < (len((Result()))))) or (((Result())[d_0_i_]) == (abs(((scores)[d_0_i_]) - ((guesses)[d_0_i_])))))) + result = [int(0)] * 0 # type : List[int] + nw0_ = [int(0)] * len((scores)) # type : List[int] + result = nw0_ + d_1_i_ = int(0) # type : int + d_1_i_ = 0 + while (d_1_i_) < (len((scores))): + Invariant(Acc(list_pred(result))) + Invariant(Acc(list_pred(guesses))) + Invariant(Acc(list_pred(scores))) + Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len((scores))))) + Invariant((len((result))) == (len((scores)))) + Invariant((len((scores))) == (len((guesses)))) + Invariant(Forall(int, lambda d_2_k_: + not (((0) <= (d_2_k_)) and ((d_2_k_) < (d_1_i_))) or (((result)[d_2_k_]) == (abs(((scores)[d_2_k_]) - ((guesses)[d_2_k_])))))) + (result)[(d_1_i_)] = abs(((scores)[d_1_i_]) - ((guesses)[d_1_i_])) + d_1_i_ = (d_1_i_) + (1) + return result + +@Pure +def abs(x : int) -> int : + if (x) < (0): + return (0) - (x) + elif True: + return x \ No newline at end of file diff --git a/Bench/154-cycpattern_check.py b/Bench/154-cycpattern_check.py new file mode 100644 index 0000000..bdfd919 --- /dev/null +++ b/Bench/154-cycpattern_check.py @@ -0,0 +1,35 @@ +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 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/Bench/157-right_angle_triangle.py b/Bench/157-right_angle_triangle.py new file mode 100644 index 0000000..d0bed6f --- /dev/null +++ b/Bench/157-right_angle_triangle.py @@ -0,0 +1,7 @@ +from nagini_contracts.contracts import * + +def right__angle__triangle(a : int, b : int, c : int) -> bool: + Ensures((Result()) == ((((((a) * (a)) + ((b) * (b))) == ((c) * (c))) or ((((a) * (a)) + ((c) * (c))) == ((b) * (b)))) or ((((b) * (b)) + ((c) * (c))) == ((a) * (a))))) + result = False # type : bool + result = (((((a) * (a)) + ((b) * (b))) == ((c) * (c))) or ((((a) * (a)) + ((c) * (c))) == ((b) * (b)))) or ((((b) * (b)) + ((c) * (c))) == ((a) * (a))) + return result diff --git a/Bench/159-eat.py b/Bench/159-eat.py new file mode 100644 index 0000000..100333b --- /dev/null +++ b/Bench/159-eat.py @@ -0,0 +1,15 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def eat(number : int, need : int, remaining : int) -> List[int]: + Requires((((number) >= (0)) and ((need) >= (0))) and ((remaining) >= (0))) + Ensures(Acc(list_pred(Result()))) + Ensures((len(Result())) == (2)) + Ensures(not ((remaining) >= (need)) or ((((Result())[0]) == ((number) + (need))) and (((Result())[1]) == ((remaining) - (need))))) + Ensures(not ((remaining) < (need)) or ((((Result())[0]) == ((number) + (remaining))) and (((Result())[1]) == (0)))) + result = list([int(0)] * 0) # type : List[int] + if (remaining) < (need): + result = ([(number) + (remaining), 0]) + else: + result = ([(number) + (need), (remaining) - (need)]) + return result \ No newline at end of file diff --git a/Bench/163-generate_integers.py b/Bench/163-generate_integers.py new file mode 100644 index 0000000..53b5470 --- /dev/null +++ b/Bench/163-generate_integers.py @@ -0,0 +1,56 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def min(a : int, b : int) -> int: + Ensures(((Result()) == (a)) or ((Result()) == (b))) + Ensures(((Result()) <= (a)) and ((Result()) <= (b))) + m = int(0) # type : int + if (a) < (b): + m = a + elif True: + m = b + return m + +@Pure +def max(a : int, b : int) -> int: + Ensures(((Result()) == (a)) or ((Result()) == (b))) + Ensures(((Result()) >= (a)) and ((Result()) >= (b))) + m = int(0) # type : int + if (a) > (b): + m = a + elif True: + m = b + return m + +def generate__integers(a : int, b : int) -> List[int]: + Ensures(Acc(list_pred(Result()))) + Ensures(Forall(int, lambda d_0_i_: + not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(Result())))) or ((((Result())[d_0_i_] % 2)) == (0)))) + Ensures(Forall(int, lambda d_1_i_: + not (((d_1_i_) >= (0)) and ((d_1_i_) < (len(Result())))) or (((Result())[d_1_i_]) > 0 and ((Result())[d_1_i_]) < 10))) + result = list([int(0)] * 0) # type : List[int] + d_2_left_ = int(0) # type : int + d_2_left_ = min(a, b) + d_3_right_ = int(0) # type : int + d_3_right_ = max(a, b) + d_4_lower_ = int(0) # type : int + d_4_lower_ = max(2, d_2_left_) + d_5_upper_ = int(0) # type : int + d_5_upper_ = min(8, d_3_right_) + result = list([]) + d_6_i_ = int(0) # type : int + d_6_i_ = d_4_lower_ + while (d_6_i_) <= (d_5_upper_): + Invariant(Acc(list_pred(result))) + Invariant(d_6_i_ >= 2) + Invariant(Implies((d_6_i_) <= (d_5_upper_), d_6_i_ <= 8)) + Invariant(d_5_upper_ <= 8) + Invariant(Forall(int, lambda d_7_i_: + not (((d_7_i_) >= (0)) and ((d_7_i_) < (len(result)))) or ((((result)[d_7_i_] % 2)) == (0)))) + Invariant(Forall(int, lambda d_8_j_: + (not (((d_8_j_) >= (0)) and ((d_8_j_) < (len(result)))) or (((result)[d_8_j_]) > 0 and ((result)[d_8_j_]) < 10), [[result[d_8_j_]]]))) + if ((d_6_i_ % 2)) == (0): + result = (result) + (([d_6_i_])) + d_6_i_ = (d_6_i_) + (1) + return result \ No newline at end of file diff --git a/WIP/096-count_up_to.py b/WIP/096-count_up_to.py new file mode 100644 index 0000000..25fe389 --- /dev/null +++ b/WIP/096-count_up_to.py @@ -0,0 +1,53 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def IsPrime(n : int) -> bool : + return ((n) > (1)) and (Forall(int, lambda d_0_k_: + Implies(((2) <= (d_0_k_)) and ((d_0_k_) < (n)), ((n % d_0_k_)) != (0)))) + +def CountUpTo(n : int) -> List[int]: + Requires((n) >= (0)) + Ensures(Acc(list_pred(Result()))) + Ensures(Forall(int, lambda d_2_i_: + not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(Result())))) or (((Result())[d_2_i_]) < (n)))) + # Ensures(Forall(int, lambda d_1_i_: + # not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(Result())))) or (IsPrime((Result())[d_1_i_])))) + # Ensures(Forall(int, lambda d_3_p_: + # ((((2) <= (d_3_p_)) and ((d_3_p_) < (n))) and (IsPrime(d_3_p_))) == ((d_3_p_) in (Result())))) + primes = list([int(0)] * 0) # type : List[int] + primes = list([]) + if (n) <= (2): + return primes + d_4_i_ = int(0) # type : int + d_4_i_ = 2 + while (d_4_i_) < (n): + Invariant(Acc(list_pred(primes))) + Invariant(((2) <= (d_4_i_)) and ((d_4_i_) <= (n))) + Invariant(Forall(int, lambda x: + Implies(x >= 0 and x < len(primes), 2 <= primes[x] and primes[x] < n))) + # Invariant(Forall(int, lambda d_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_: + (Implies(((0) <= (d_8_j_)) and ((d_8_j_) < (len(primes))), ((primes)[d_8_j_]) < (d_4_i_)), [[(primes)[d_8_j_]]]))) + # Invariant(Forall(int, lambda d_8_j_: + # Forall(int, lambda d_9_k_: + # (Implies((((0) <= (d_8_j_)) and ((d_8_j_) < (d_9_k_))) and ((d_9_k_) < (len(primes))), ((primes)[d_8_j_]) < ((primes)[d_9_k_])), [[(primes)[d_8_j_] < (primes)[d_9_k_]]])))) + # Invariant(Forall(int, lambda d_5_j_: + # (Implies(((0) <= (d_5_j_)) and ((d_5_j_) < (len(primes))), IsPrime((primes)[d_5_j_])), [[IsPrime((primes)[d_5_j_])]]))) + # 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_]]]))) + if IsPrime(d_4_i_): + # prime_prev = list(primes) + # Assert(Forall(int, lambda d_5_j_: + # (Implies(((0) <= (d_5_j_)) and ((d_5_j_) < (len(primes))), IsPrime((primes)[d_5_j_]))))) + # Assert(Forall(int, lambda d_8_j_: + # (Implies(((0) <= (d_8_j_)) and ((d_8_j_) < (len(primes))), ((primes)[d_8_j_]) < (d_4_i_ + 1)), [[(primes)[d_8_j_]]]))) + primes = primes + [(d_4_i_)] + # Assert(primes[len(primes) - 1] < d_4_i_ + 1) + # Assert(Forall(int, lambda d_8_j_: + # (Implies(((0) <= (d_8_j_)) and ((d_8_j_) < (len(primes) - 1)), ((primes)[d_8_j_]) < (d_4_i_ + 1)), [[(primes)[d_8_j_]]]))) + # Assert(Forall(int, lambda d_5_j_: + # (Implies(((0) <= (d_5_j_)) and ((d_5_j_) < (len(primes))), IsPrime((primes)[d_5_j_])), [[IsPrime((primes)[d_5_j_])]]))) + d_4_i_ = (d_4_i_) + (1) + return primes diff --git a/WIP/104-unique_digits.py b/WIP/104-unique_digits.py new file mode 100644 index 0000000..5af77b3 --- /dev/null +++ b/WIP/104-unique_digits.py @@ -0,0 +1,88 @@ +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(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(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(((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) + return result diff --git a/WIP/WIP: 002-truncate.py b/WIP/WIP: 002-truncate.py new file mode 100644 index 0000000..4cf61d5 --- /dev/null +++ b/WIP/WIP: 002-truncate.py @@ -0,0 +1,6 @@ +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/WIP: 006-parse_nested_parens.py b/WIP/WIP: 006-parse_nested_parens.py new file mode 100644 index 0000000..cb5e84b --- /dev/null +++ b/WIP/WIP: 006-parse_nested_parens.py @@ -0,0 +1,77 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +# def parse__paren__group(s : List[int]) -> int: +# Requires(Forall(int, lambda d_0_i_: +# not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(s)))) or ((((s)[d_0_i_]) == (1)) or (((s)[d_0_i_]) == (2))))) +# Ensures((Result()) >= (0)) +# max__depth = int(0) # type : int +# d_1_depth_ = int(0) # type : int +# d_1_depth_ = 0 +# max__depth = 0 +# d_2_i_ = int(0) # type : int +# d_2_i_ = 0 +# while (d_2_i_) < (len(s)): +# d_3_c_ = (s)[d_2_i_] # type : int +# if (d_3_c_) == (1): +# d_1_depth_ = (d_1_depth_) + (1) +# if (d_1_depth_) > (max__depth): +# max__depth = d_1_depth_ +# else: +# d_1_depth_ = (d_1_depth_) - (1) +# d_2_i_ = (d_2_i_) + (1) +# return max__depth + +def split(s : List[int]) -> List[List[int]]: + # Requires(Forall(int, lambda d_4_i_: + # not (((d_4_i_) >= (0)) and ((d_4_i_) < (len(s)))) or (((((s)[d_4_i_]) == (1)) or (((s)[d_4_i_]) == (2))) or (((s)[d_4_i_]) == (3))))) + # Ensures(Acc(list_pred(Result()))) + # Ensures(Forall(int, lambda d_5_i_: Implies(d_5_i_ >= 0 and d_5_i_ < len(Result()), Acc(list_pred(Result()[d_5_i_]))))) + # 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))))) + res = list([([int(0)] * 0)] * 0) # type : List[List[int]] + d_7_current__string_ = list([int(0)] * 0) # type : List[int] + d_8_i_ = int(0) # type : int + d_8_i_ = 0 + while (d_8_i_) < (len(s)): + # Invariant(Acc(list_pred(res))) + # Invariant(Forall(int, lambda d_9_j_: Implies(d_9_j_ >= 0 and d_9_j_ < len(res), Acc(list_pred(res[d_9_j_]))))) + # Invariant(((d_8_i_) >= (0)) and ((d_8_i_) <= (len(s)))) + # Invariant(Forall(int, lambda d_9_j_: + # not (((d_9_j_) >= (0)) and ((d_9_j_) < (len(d_7_current__string_)))) or ((((d_7_current__string_)[d_9_j_]) == (1)) or (((d_7_current__string_)[d_9_j_]) == (2))))) + # 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))))) and ((len(res[d_10_j_])) > (0))))) + if ((s)[d_8_i_]) == (3): + if (d_7_current__string_) != []: + res = (res) + [d_7_current__string_] + d_7_current__string_ = [] + else: + d_7_current__string_ = (d_7_current__string_) + [(s)[d_8_i_]] + d_8_i_ = (d_8_i_) + (1) + if (d_7_current__string_) != []: + res = (res) + [d_7_current__string_] + d_7_current__string_ = [] + return res + +# def parse__nested__parens(paren__string : List[int]) -> List[int]: +# Requires(Forall(int, lambda d_12_i_: +# not (((d_12_i_) >= (0)) and ((d_12_i_) < (len(paren__string)))) or (((((paren__string)[d_12_i_]) == (1)) or (((paren__string)[d_12_i_]) == (2))) or (((paren__string)[d_12_i_]) == (3))))) +# Ensures(Acc(list_pred(Result()))) +# Ensures(Forall(int, lambda d_13_x_: +# Implies(d_13_x_ >= 0 and d_13_x_ < len(Result()), Result()[d_13_x_] >= 0))) +# res = list([int(0)] * 0) # type : List[int] +# d_14_strings_ = split(paren__string) # type : List[List[int]] +# d_15_i_ = int(0) # type : int +# while (d_15_i_) < (len(d_14_strings_)): +# Invariant(Acc(list_pred(d_14_strings_))) +# Invariant(Acc(list_pred(res))) +# Invariant(Forall(int, lambda d_5_i_: Implies(d_5_i_ >= 0 and d_5_i_ < len(d_14_strings_), Acc(list_pred(d_14_strings_[d_5_i_]))))) +# Invariant(((d_15_i_) >= (0)) and ((d_15_i_) <= (len(d_14_strings_)))) +# Invariant(Forall(int, lambda d_13_x_: +# Implies(d_13_x_ >= 0 and d_13_x_ < len(res), res[d_13_x_] >= 0))) +# d_17_cur_ = parse__paren__group((d_14_strings_)[d_15_i_]) # type : int +# res = (res) + [d_17_cur_] +# d_15_i_ = (d_15_i_) + (1) +# return res \ No newline at end of file diff --git a/WIP/WIP: 012-longest.py b/WIP/WIP: 012-longest.py new file mode 100644 index 0000000..86f370e --- /dev/null +++ b/WIP/WIP: 012-longest.py @@ -0,0 +1,50 @@ +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 longest(strings : List[List[int]]) -> Optional[int]: + Requires(Acc(list_pred(strings))) + Requires(Forall(strings, lambda d_0_s_: Acc(list_pred(d_0_s_)))) + Ensures(Acc(list_pred(strings))) + Ensures(Forall(strings, lambda d_0_s_: Acc(list_pred(d_0_s_)))) + Ensures(((Result()) is (None)) == ((len(strings)) == (0))) + Ensures(Implies(Result() is not None, getVal(Result()) >= 0 and getVal(Result()) < len(strings))) + Ensures(not ((Result()) is not (None)) or (Forall(int, lambda d_1_s_: + not ((d_1_s_) >= 0 and d_1_s_ < len(strings)) or ((len(strings[getVal(Result())])) >= (len(strings[d_1_s_])))))) + # Ensures(not ((Result()) != (None)) or (Exists(List[int], lambda d_2_s_: + # ((d_2_s_) in (strings)) and ((len(d_2_s_)) == (len(getVal(Result()))))))) + # Ensures(not ((Result()) != (None)) or (Exists(int, lambda d_3_i_: + # ((((0) <= (d_3_i_)) and ((d_3_i_) < (len(strings)))) and (((strings)[d_3_i_]) == (getVal(Result())))) and (Forall(int, lambda d_4_j_: + # not (((0) <= (d_4_j_)) and ((d_4_j_) < (d_3_i_))) or ((len((strings)[d_4_j_])) < (len(getVal(Result()))))))))) + result = int(0) # type : Optional[int] + result = None + if (len(strings)) != (0): + d_5_i_ = int(0) # type : int + d_5_i_ = 0 + d_6_mx_ = int(0) # type : int + d_6_mx_ = -1 + while (d_5_i_) < (len(strings)): + Invariant(Acc(list_pred(strings))) + Invariant(Forall(strings, lambda d_0_s_: Acc(list_pred(d_0_s_)))) + Invariant(((d_5_i_) >= (0)) and ((d_5_i_) <= (len(strings)))) + Invariant(((d_6_mx_) == (-1)) == ((result) is (None))) + Invariant(not ((d_5_i_) == (0)) or ((d_6_mx_) == (-1))) + Invariant(Implies(result is not None, getVal(result) >= 0 and getVal(result) < d_5_i_)) + Invariant(Implies(result is not None, len(strings[getVal(result)]) == d_6_mx_)) + Invariant(not ((d_5_i_) > (0)) or ((d_6_mx_) == (len(strings[getVal(result)])))) + # Invariant(not ((d_5_i_) > (0)) or (Forall(List[char], lambda d_7_s_: + # not ((d_7_s_) in (list((strings)[0:d_5_i_:]))) or ((d_6_mx_) >= (len(d_7_s_)))))) + # Invariant(not ((d_5_i_) > (0)) or (Exists(List[int], lambda d_8_s_: + # ((d_8_s_) in (strings)) and ((d_6_mx_) == (len(d_8_s_)))))) + # Invariant(not ((result) != (None)) or (Exists(int, lambda d_9_i_: + # ((((0) <= (d_9_i_)) and ((d_9_i_) < (len(strings)))) and (((strings)[d_9_i_]) == (getVal(result)))) and (Forall(int, lambda d_10_j_: + # not (((0) <= (d_10_j_)) and ((d_10_j_) < (d_9_i_))) or ((len((strings)[d_10_j_])) < (len(getVal(result))))))))) + if result is None or (len((strings)[d_5_i_])) > (len(strings[getVal(result)])): + d_6_mx_ = len((strings)[d_5_i_]) + result = d_5_i_ + d_5_i_ = (d_5_i_) + (1) + return result \ No newline at end of file diff --git a/WIP/WIP: 034-unique.py b/WIP/WIP: 034-unique.py new file mode 100644 index 0000000..da126d6 --- /dev/null +++ b/WIP/WIP: 034-unique.py @@ -0,0 +1,151 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +def uniqueSorted(s : List[int]) -> List[int]: + Requires(Acc(list_pred(s))) + Requires(Forall(int, lambda d_0_i_: + Forall(int, lambda d_1_j_: + Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len(s))), + ((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())))) + result = list([int(0)] * 0) # type : List[int] + result = list([]) + d_6_i_ = int(0) # type : int + d_6_i_ = 0 + while (d_6_i_) < (len(s)): + Invariant(Acc(list_pred(result))) + Invariant(Acc(list_pred(s), 1/2)) + Invariant(Forall(int, lambda d_0_i_: + Forall(int, lambda d_1_j_: + Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len(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)))) + 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_]]]))) + 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 + +# def unique(s : List[int]) -> List[int]: +# Requires(Acc(list_pred(s))) +# Ensures(Acc(list_pred(s))) +# Ensures(Acc(list_pred(Result()))) +# Ensures(Forall(int, lambda d_12_i_: +# Forall(int, lambda d_13_j_: +# not ((((0) <= (d_12_i_)) and ((d_12_i_) < (d_13_j_))) and ((d_13_j_) < (len(Result())))) or (((Result())[d_12_i_]) < ((Result())[d_13_j_]))))) +# Ensures(Forall(int, lambda d_14_x_: +# not ((d_14_x_) in (Result())) or ((d_14_x_) in (s)))) +# Ensures(Forall(int, lambda d_15_x_: +# not ((d_15_x_) in (s)) or ((d_15_x_) in (Result())))) +# result = list([int(0)] * 0) # type : List[int] +# d_16_sorted_ = list([int(0)] * 0) # type : List[int] +# out0_ # type : List[int] +# out0_ = BubbleSort(s) +# d_16_sorted_ = out0_ +# out1_ # type : List[int] +# out1_ = uniqueSorted(d_16_sorted_) +# result = out1_ +# Assert(Forall(int, lambda d_17_x_: +# not ((d_17_x_) in (d_16_sorted_)) or ((d_17_x_) in (s)))) +# Assert(Forall(int, lambda d_18_x_: +# not ((d_18_x_) in (s)) or ((d_18_x_) in (d_16_sorted_)))) +# return result + +def BubbleSort(a1 : List[int]) -> List[int]: + Requires(Acc(list_pred(a1), 1/2)) + Ensures(Acc(list_pred(a1), 1/2)) + Ensures(Acc(list_pred(Result()))) + Ensures((len(a1)) == (len(Result()))) + # Ensures(ToMS(ToSeq(a1)) == ToMS(ToSeq(Result()))) + Ensures(Forall(int, lambda d_0_i_: + Forall(int, lambda d_1_j_: + Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((Result())))), ((Result())[d_0_i_]) <= ((Result())[d_1_j_]))))) + a = list(a1) # type : List[int] + d_2_i_ = int(0) # type : int + d_2_i_ = (len((a))) - (1) + while (d_2_i_) > (0): + Invariant(Acc(list_pred(a))) + Invariant(Acc(list_pred(a1), 1/2)) + Invariant((len(a1)) == (len(a))) + Invariant(not ((d_2_i_) < (0)) or ((len((a))) == (0))) + Invariant(((-1) <= (d_2_i_)) and ((d_2_i_) < (len((a))))) + Invariant(Forall(int, lambda d_3_ii_: + (Forall(int, lambda d_4_jj_: + (Implies((((d_2_i_) <= (d_3_ii_)) and ((d_3_ii_) < (d_4_jj_))) and ((d_4_jj_) < (len((a)))), ((a)[d_3_ii_]) <= ((a)[d_4_jj_])), + [[(a)[d_4_jj_]]])), + [[(a)[d_3_ii_]]]))) + Invariant(Forall(int, lambda d_5_k_: + (Forall(int, lambda d_6_k_k_: + (Implies(((((0) <= (d_5_k_)) and ((d_5_k_) <= (d_2_i_))) and ((d_2_i_) < (d_6_k_k_)) and (d_6_k_k_) < (len((a)))), ((a)[d_5_k_]) <= ((a)[d_6_k_k_])), + [[(a)[d_6_k_k_]]])), + [[(a)[d_5_k_]]]))) + d_7_j_ = int(0) # type : int + d_7_j_ = 0 + while (d_7_j_) < (d_2_i_): + Invariant(Acc(list_pred(a))) + Invariant(Acc(list_pred(a1), 1/2)) + Invariant((len(a1)) == (len(a))) + Invariant((((0) < (d_2_i_)) and ((d_2_i_) < (len((a))))) and (((0) <= (d_7_j_)) and ((d_7_j_) <= (d_2_i_)))) + Invariant(Forall(int, lambda d_8_ii_: + (Forall(int, lambda d_9_jj_: + (Implies((((d_2_i_) <= (d_8_ii_)) and ((d_8_ii_) <= (d_9_jj_))) and ((d_9_jj_) < (len((a)))), ((a)[d_8_ii_]) <= ((a)[d_9_jj_])), + [[(a)[d_9_jj_]]])), + [[(a)[d_8_ii_]]]))) + Invariant(Forall(int, lambda d_10_k_: + (Forall(int, lambda d_11_k_k_: + (Implies(((((0) <= (d_10_k_)) and ((d_10_k_) <= (d_2_i_))) and ((d_2_i_) < (d_11_k_k_))) and ((d_11_k_k_) < (len((a)))), ((a)[d_10_k_]) <= ((a)[d_11_k_k_])), + [[(a)[d_11_k_k_]]])), + [[(a)[d_10_k_]]]))) + Invariant(Forall(int, lambda d_12_k_: + (Implies(((0) <= (d_12_k_)) and ((d_12_k_) <= (d_7_j_)), ((a)[d_12_k_]) <= ((a)[d_7_j_])), + [[(a)[d_12_k_]]]))) + if ((a)[d_7_j_]) > ((a)[(d_7_j_) + (1)]): + rhs0_ = (a)[(d_7_j_) + (1)] # type : int + (a)[(d_7_j_) + (1)] = (a)[d_7_j_] + (a)[d_7_j_] = rhs0_ + d_7_j_ = (d_7_j_) + (1) + d_2_i_ = (d_2_i_) - (1) + return a diff --git a/WIP/WIP: 058-common.py b/WIP/WIP: 058-common.py new file mode 100644 index 0000000..b50c6ab --- /dev/null +++ b/WIP/WIP: 058-common.py @@ -0,0 +1,112 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@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)), [[(a)[d_0_i_]]])) + + +def common(l1 : List[int], l2 : List[int]) -> List[int]: + Requires(Acc(list_pred(l2), 1/2)) + Requires(Acc(list_pred(l1), 1/2)) + Ensures(Acc(list_pred(l2), 1/2)) + Ensures(Acc(list_pred(l1), 1/2)) + Ensures(Acc(list_pred(Result()))) + Ensures(Forall(int, lambda d_4_j_: + (Implies(((d_4_j_) >= 0 and d_4_j_ < len(l1)), Implies((Exists(int, lambda x: x >= 0 and x< len(l2) and l2[x] == l1[d_4_j_])), Exists(int, lambda x: x >= 0 and x< len(Result()) and Result()[x] == l1[d_4_j_])))))) + # Ensures(Forall(int, lambda d_0_i_: + # not ((d_0_i_) >= 0 and d_0_i_ < len(Result())) or ((Exists(int, lambda x : x >= 0 and x < len(l1) and (Result()[d_0_i_]) == (l1[x]))) and + # (Exists(int, lambda x : x >= 0 and x < len(l2) and (Result()[d_0_i_]) == (l2[x])))))) + c = list([int(0)] * 0) # type : List[int] + d_2_i_ = int(0) # type : int + d_2_i_ = 0 + while (d_2_i_) < (len(l1)): + Invariant(Acc(list_pred(c))) + Invariant(Acc(list_pred(l2), 1/2)) + Invariant(Acc(list_pred(l1), 1/2)) + Invariant(((d_2_i_) >= (0)) and ((d_2_i_) <= (len(l1)))) + Invariant(Forall(int, lambda d_4_j_: + (Implies(((d_4_j_) >= 0 and d_4_j_ < d_2_i_), Implies((Exists(int, lambda x: x >= 0 and x< len(l2) and l2[x] == l1[d_4_j_])), Exists(int, lambda x: x >= 0 and x< len(c) and c[x] == l1[d_4_j_]))), + [[l1[d_4_j_]]]))) + # Invariant(Forall(int, lambda d_0_i_: + # not ((d_0_i_) >= 0 and d_0_i_ < len(c)) or ((Exists(int, lambda x : x >= 0 and x < len(l1) and (c[d_0_i_]) == (l1[x]))) and + # (Exists(int, lambda x : x >= 0 and x < len(l2) and (c[d_0_i_]) == (l2[x])))))) + # Invariant(Forall(int, lambda d_0_i_: + # (Implies((d_0_i_) >= 0 and d_0_i_ < len(c), (Exists(int, lambda x : x >= 0 and x < d_2_i_ and (c[d_0_i_]) == (l1[x]))) and + # (Exists(int, lambda x : x >= 0 and x < len(l2) and (c[d_0_i_]) == (l2[x])))), [[c[d_0_i_]]]))) + + d_5_j_ = int(0) # type : int + d_5_j_ = 0 + # Assume(Forall(int, lambda d_0_i_: + # (Implies((d_0_i_) >= 0 and d_0_i_ < len(c), (Exists(int, lambda x : x >= 0 and x < d_2_i_ and (c[d_0_i_]) == (l1[x]))) and + # (Exists(int, lambda x : x >= 0 and x < len(l2) and (c[d_0_i_]) == (l2[x])))), [[c[d_0_i_]]]))) + while (d_5_j_) < (len(l2)): + Invariant(Acc(list_pred(c))) + Invariant(Acc(list_pred(l2), 1/2)) + Invariant(Acc(list_pred(l1), 1/2)) + Invariant(((d_2_i_) >= (0)) and ((d_2_i_) < (len(l1)))) + Invariant(((d_5_j_) >= (0)) and ((d_5_j_) <= (len(l2)))) + Invariant(Forall(int, lambda d_4_j_: + (Implies(((d_4_j_) >= 0 and d_4_j_ < d_2_i_), Implies((Exists(int, lambda x: x >= 0 and x< len(l2) and l2[x] == l1[d_4_j_])), Exists(int, lambda x: x >= 0 and x< len(c) and c[x] == l1[d_4_j_]))), + [[l1[d_4_j_]]]))) + Invariant(Implies(Exists(int, lambda x: x >= 0 and x < d_5_j_ and l2[x] == l1[d_2_i_]), Exists(int, lambda x: x >= 0 and x < len(c) and c[x] == l1[d_2_i_]))) + # Invariant(Forall(int, lambda d_0_i_: + # (Implies((d_0_i_) >= 0 and d_0_i_ < len(c), ((Exists(int, lambda x : x >= 0 and x < d_2_i_ and (c[d_0_i_]) == (l1[x]))) and + # (Exists(int, lambda x : x >= 0 and x < len(l2) and (c[d_0_i_]) == (l2[x])))) or + # (l1[d_2_i_] == c[d_0_i_] and (Exists(int, lambda x : x >= 0 and x < d_5_j_ and (c[d_0_i_]) == (l2[x]))))), [[c[d_0_i_]]]))) + + + # Assume(Forall(int, lambda d_0_i_: + # (Implies((d_0_i_) >= 0 and d_0_i_ < len(c), ((Exists(int, lambda x : x >= 0 and x < d_2_i_ and (c[d_0_i_]) == (l1[x]))) and + # (Exists(int, lambda x : x >= 0 and x < len(l2) and (c[d_0_i_]) == (l2[x])))) or + # (l1[d_2_i_] == c[d_0_i_] and (Exists(int, lambda x : x >= 0 and x < d_5_j_ and (c[d_0_i_]) == (l2[x]))))), [[c[d_0_i_]]]))) + # Assume(Forall(int, lambda d_0_i_: + # (Implies((d_0_i_) >= 0 and d_0_i_ < len(c), ((Exists(int, lambda x : x >= 0 and x < d_2_i_ and (c[d_0_i_]) == (l1[x]))) and + # (Exists(int, lambda x : x >= 0 and x < len(l2) and (c[d_0_i_]) == (l2[x])))) or + # (l1[d_2_i_] == c[d_0_i_] and (Exists(int, lambda x : x >= 0 and x < d_5_j_ and (c[d_0_i_]) == (l2[x]))))), [[c[d_0_i_]]]))) + if ((l1)[d_2_i_]) == ((l2)[d_5_j_]) and NotInArray(c, (l1)[d_2_i_]): + # Assert(Forall(int, lambda d_0_i_: + # (Implies((d_0_i_) >= 0 and d_0_i_ < len(c), (Exists(int, lambda x : x >= 0 and x < d_2_i_ and (c[d_0_i_]) == (l1[x]))) and + # (Exists(int, lambda x : x >= 0 and x < len(l2) and (c[d_0_i_]) == (l2[x])))), [[c[d_0_i_]]]))) + # c_old = list(c) + # Assert(Forall(int, lambda x: Implies(x >= 0 and x < len(c_old), c_old[x] == c[x]))) + # Assert(Forall(int, lambda d_0_i_: + # (Implies((d_0_i_) >= 0 and d_0_i_ < len(c_old), ((Exists(int, lambda x : x >= 0 and x < d_2_i_ and (c_old[d_0_i_]) == (l1[x]))) and + # (Exists(int, lambda x : x >= 0 and x < len(l2) and (c_old[d_0_i_]) == (l2[x])))) or + # (l1[d_2_i_] == c[d_0_i_] and (Exists(int, lambda x : x >= 0 and x < d_5_j_ and (c_old[d_0_i_]) == (l2[x]))))), [[c_old[d_0_i_]]]))) + c = c + [((l1)[d_2_i_])] + Assert((Exists(int, lambda x : x >= 0 and x < len(l1) and (c[len(c) - 1]) == (l1[x])))) + Assert((Exists(int, lambda x : x >= 0 and x < len(l2) and (c[len(c) - 1]) == (l2[x])))) + # Assert(l1[d_2_i_] == c[len(c) - 1] and (Exists(int, lambda x : x >= 0 and x <= d_5_j_ and (c[len(c) - 1]) == (l2[x])))) + # Assert(((Exists(int, lambda x : x >= 0 and x < d_2_i_ and (c[len(c) - 1]) == (l1[x]))) and + # (Exists(int, lambda x : x >= 0 and x < len(l2) and (c[len(c) - 1]) == (l2[x])))) or + # (l1[d_2_i_] == c[len(c) - 1] and (Exists(int, lambda x : x >= 0 and x <= d_5_j_ and (c[len(c) - 1]) == (l2[x]))))) + # Assert(Forall(int, lambda x: Implies(x >= 0 and x < len(c) - 1, c[x] == Old(c)[x]))) + # Assert(Forall(int, lambda d_0_i_: + # (Implies((d_0_i_) >= 0 and d_0_i_ < len(c_old), ((Exists(int, lambda x : x >= 0 and x < d_2_i_ and (c_old[d_0_i_]) == (l1[x]))) and + # (Exists(int, lambda x : x >= 0 and x < len(l2) and (c_old[d_0_i_]) == (l2[x])))) or + # (l1[d_2_i_] == c[d_0_i_] and (Exists(int, lambda x : x >= 0 and x < d_5_j_ and (c_old[d_0_i_]) == (l2[x]))))), [[c_old[d_0_i_]]]))) + # Assert(Forall(int, lambda d_0_i_: + # (Implies((d_0_i_) >= 0 and d_0_i_ < len(c) - 1, ((Exists(int, lambda x : x >= 0 and x < d_2_i_ and (c[d_0_i_]) == (l1[x]))) and + # (Exists(int, lambda x : x >= 0 and x < len(l2) and (c[d_0_i_]) == (l2[x])))) or + # (l1[d_2_i_] == c[d_0_i_] and (Exists(int, lambda x : x >= 0 and x < d_5_j_ and (c[d_0_i_]) == (l2[x]))))), [[c[d_0_i_]]]))) + # Assert(((Exists(int, lambda x : x >= 0 and x < d_2_i_ and (c[len(c) - 1]) == (l1[x]))) and + # (Exists(int, lambda x : x >= 0 and x < len(l2) and (c[len(c) - 1]) == (l2[x])))) or + # (l1[d_2_i_] == c[len(c) - 1] and (Exists(int, lambda x : x >= 0 and x <= d_5_j_ and (c[len(c) - 1]) == (l2[x]))))) + # Assert(Forall(int, lambda d_0_i_: + # (Implies(d_0_i_ == len(c) - 1, ((Exists(int, lambda x : x >= 0 and x < d_2_i_ and (c[d_0_i_]) == (l1[x]))) and + # (Exists(int, lambda x : x >= 0 and x < len(l2) and (c[d_0_i_]) == (l2[x])))) or + # (l1[d_2_i_] == c[d_0_i_] and (Exists(int, lambda x : x >= 0 and x < d_5_j_ and (c[d_0_i_]) == (l2[x])))))))) + # Assert(Forall(int, lambda d_0_i_: + # (Implies((d_0_i_) >= 0 and d_0_i_ < len(c), ((Exists(int, lambda x : x >= 0 and x < d_2_i_ and (c[d_0_i_]) == (l1[x]))) and + # (Exists(int, lambda x : x >= 0 and x < len(l2) and (c[d_0_i_]) == (l2[x])))) or + # (l1[d_2_i_] == c[d_0_i_] and (Exists(int, lambda x : x >= 0 and x <= d_5_j_ and (c[d_0_i_]) == (l2[x]))))), [[c[d_0_i_]]]))) + # Assert(Forall(int, lambda d_0_i_: + # (Implies((d_0_i_) >= 0 and d_0_i_ < len(c), ((Exists(int, lambda x : x >= 0 and x < d_2_i_ and (c[d_0_i_]) == (l1[x]))) and + # (Exists(int, lambda x : x >= 0 and x < len(l2) and (c[d_0_i_]) == (l2[x])))) or + # (l1[d_2_i_] == c[d_0_i_] and (Exists(int, lambda x : x >= 0 and x <= d_5_j_ and (c[d_0_i_]) == (l2[x]))))), [[c[d_0_i_]]]))) + d_5_j_ = (d_5_j_) + (1) + d_2_i_ = (d_2_i_) + (1) + return c diff --git a/WIP/WIP: 069-search.py b/WIP/WIP: 069-search.py new file mode 100644 index 0000000..9c6823c --- /dev/null +++ b/WIP/WIP: 069-search.py @@ -0,0 +1,36 @@ +@staticmethod +def freq(s : List[int], x : int) -> int: + Requires(Acc(list_pred(s))) + Ensures(Acc(list_pred(s))) + Ensures(def iife0_(): + coll0_ = set() # type : Set[int] + compr_0_ = int(0) # type : int + for compr_0_ in range(0, len(s)): + d_0_i_: int = compr_0_ + if (((0) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) and (((s)[d_0_i_]) == (x)): + coll0_ = coll0_.union(set([d_0_i_])) + return set(coll0_) + (Result()) == (len(iife0_() + ))) + count = int(0) # type : int + count = 0 + d_1_i_ = int(0) # type : int + d_1_i_ = 0 + def iife1_(): + coll1_ = set() # type : Set[int] + compr_1_ = int(0) # type : int + for compr_1_ in range(0, d_1_i_): + d_3_j_: int = compr_1_ + if (((0) <= (d_3_j_)) and ((d_3_j_) < (d_1_i_))) and (((s)[d_3_j_]) == (x)): + coll1_ = coll1_.union(set([d_3_j_])) + return set(coll1_) + while (d_1_i_) < (len(s)): + Invariant(Acc(list_pred(s))) + Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(s)))) + Invariant((count) == (len(d_2_good_))) + Invariant((d_2_good_) == (iife1_() + )) + if ((s)[d_1_i_]) == (x): + count = (count) + (1) + d_1_i_ = (d_1_i_) + (1) + return count diff --git a/WIP/WIP: 074-total_match.py b/WIP/WIP: 074-total_match.py new file mode 100644 index 0000000..9a1b770 --- /dev/null +++ b/WIP/WIP: 074-total_match.py @@ -0,0 +1,71 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + + +# def TotalMatch(list1 : List[List[int]], list2 : List[List[int]]) -> List[List[int]]: +# Requires(Acc(list_pred(list2))) +# Requires(Acc(list_pred(list1))) +# Requires(Forall(list1, lambda x: Acc(list_pred(x)))) +# Requires(Forall(list2, lambda x: Acc(list_pred(x)))) +# # Requires(Forall(int, lambda x: Implies(x >= 0 and x < len(list1), Acc(list_pred(list1[x]))))) +# # Requires(Forall(int, lambda x: Implies(x >= 0 and x < len(list2), Acc(list_pred(list2[x]))))) +# Ensures(Acc(list_pred(list2))) +# Ensures(Acc(list_pred(list1))) +# Ensures(Forall(list1, lambda x: Acc(list_pred(x)))) +# Ensures(Forall(list2, lambda x: Acc(list_pred(x)))) +# # Ensures(Forall(int, lambda x: Implies(x >= 0 and x < len(list1), Acc(list_pred(list1[x]))))) +# # Ensures(Forall(int, lambda x: Implies(x >= 0 and x < len(list2), Acc(list_pred(list2[x]))))) +# Ensures(Acc(list_pred(Result()))) +# Ensures(Forall(int, lambda x: Implies(x >= 0 and x < len(Result()), Acc(list_pred(Result()[x]))))) +# Ensures(((len(Result())) == (len(list1))) or ((len(Result())) == (len(list2)))) +# Ensures(((Result()) == (list1)) or ((Result()) == (list2))) +# Ensures((sum__chars__rec(0, len(Result()), Result())) <= (sum__chars__rec(0, len(list1), list1))) +# Ensures((sum__chars__rec(0, len(Result()), Result())) <= (sum__chars__rec(0, len(list2), list2))) +# Ensures(not ((sum__chars__rec(0, len(list1), list1)) == (sum__chars__rec(0, len(list2), list2))) or ((Result()) == (list1))) +# d_0_sum1_ = SumChars(list1) # type : int +# d_1_sum2_ = SumChars(list2) # type : int +# if (d_0_sum1_) <= (d_1_sum2_): +# # res = list(list1) # type : List[List[int]] +# return list1 +# else: +# # res = list(list2) # type : List[List[int]] +# return list2 + +@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(((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: + return len((lst)[j - 1]) + sum__chars__rec(i, j - 1, lst) + +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))) + 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(((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_])) + sum = (sum) + (len((lst)[d_3_i_])) + d_3_i_ = (d_3_i_) + (1) + # Assert((list) == (list((list)[:len(list):]))) + return sum diff --git a/WIP/WIP: 075-is_multiply_prime.py b/WIP/WIP: 075-is_multiply_prime.py new file mode 100644 index 0000000..816b959 --- /dev/null +++ b/WIP/WIP: 075-is_multiply_prime.py @@ -0,0 +1,131 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def Prime(p : int) -> bool : + return ((p) > (1)) and (Forall(int, lambda d_0_k_: + not (((1) < (d_0_k_)) and ((d_0_k_) < (p))) or (((p % d_0_k_)) != (0)))) + +def is__prime(k : int) -> bool: + Requires((k) >= (2)) + Ensures(not (Result()) or (Forall(int, lambda d_0_i_: + not (((2) <= (d_0_i_)) and ((d_0_i_) < (k))) or ((k % d_0_i_) != (0))))) + Ensures(not (not(Result())) or (Exists(int, lambda d_1_j_: + (((2) <= (d_1_j_)) and ((d_1_j_) < (k))) and (((k % d_1_j_)) == (0))))) + Ensures(Result() == Prime(k)) + result = False # type : bool + d_2_i_ = int(0) # type : int + d_2_i_ = 2 + result = True + while (d_2_i_) < (k): + Invariant(((2) <= (d_2_i_)) and ((d_2_i_) <= (k))) + Invariant(not (not(result)) or (Exists(int, lambda d_3_j_: + (((2) <= (d_3_j_)) and ((d_3_j_) < (d_2_i_))) and (((k % d_3_j_)) == (0))))) + Invariant(not (result) or (Forall(int, lambda d_4_j_: + not (((2) <= (d_4_j_)) and ((d_4_j_) < (d_2_i_))) or (((k % d_4_j_)) != (0))))) + if ((k % d_2_i_)) == (0): + result = False + d_2_i_ = (d_2_i_) + (1) + return result + +# def is__multiply__prime(x : int) -> bool: +# Requires((x) > (1)) +# # Ensures(Implies(Result(), Exists(int, lambda d_1_a_: +# # Exists(int, lambda d_2_b_: +# # Exists(int, lambda d_3_c_: +# # (d_1_a_< x and d_2_b_ < x and d_3_c_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_))) and (Prime(d_3_c_))) and ((x) == (((d_1_a_) * (d_2_b_)) * (d_3_c_)))))))) +# d_4_a_ = int(2) # type : int +# result = False # type : bool +# while d_4_a_ < x: +# Invariant(x >= 2) +# Invariant(d_4_a_ >= 2 and d_4_a_ <= x) +# # Invariant(Implies(result, Exists(int, lambda d_1_a_: +# # Exists(int, lambda d_2_b_: +# # Exists(int, lambda d_3_c_: +# # (d_1_a_< x and d_2_b_ < x and d_3_c_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_))) and (Prime(d_3_c_))) and ((x) == (((d_1_a_) * (d_2_b_)) * (d_3_c_)))))))) +# if is__prime(d_4_a_): +# d_5_b_ = int(2) # type : int +# while d_5_b_ < x: +# Invariant(x >= 2) +# Invariant(Prime(d_4_a_)) +# Invariant(d_4_a_ >= 2 and d_4_a_ < x) +# Invariant(d_5_b_ >= 2 and d_5_b_ <= x) +# # Invariant(Implies(result, Exists(int, lambda d_1_a_: +# # Exists(int, lambda d_2_b_: +# # Exists(int, lambda d_3_c_: +# # (d_1_a_< x and d_2_b_ < x and d_3_c_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_))) and (Prime(d_3_c_))) and ((x) == (((d_1_a_) * (d_2_b_)) * (d_3_c_)))))))) +# # Assume(Implies(result, Exists(int, lambda d_1_a_: +# # Exists(int, lambda d_2_b_: +# # Exists(int, lambda d_3_c_: +# # (d_1_a_< x and d_2_b_ < x and d_3_c_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_))) and (Prime(d_3_c_))) and ((x) == (((d_1_a_) * (d_2_b_)) * (d_3_c_)))))))) +# if is__prime(d_5_b_): +# d_6_c_ = int(2) # type : int +# while d_6_c_ < x: +# Invariant(x >= 2) +# Invariant(Prime(d_4_a_)) +# Invariant(Prime(d_5_b_)) +# Invariant(d_4_a_ >= 2 and d_4_a_ < x) +# Invariant(d_5_b_ >= 2 and d_5_b_ < x) +# Invariant(d_6_c_ >= 2 and d_6_c_ <= x) +# # Invariant(Implies(result, Exists(int, lambda d_1_a_: +# # Exists(int, lambda d_2_b_: +# # Exists(int, lambda d_3_c_: +# # (d_1_a_< x and d_2_b_ < x and d_3_c_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_))) and (Prime(d_3_c_))) and ((x) == (((d_1_a_) * (d_2_b_)) * (d_3_c_)))))))) +# if (is__prime(d_6_c_)) and ((x) == (((d_4_a_) * (d_5_b_)) * (d_6_c_))): +# result = True +# Assert(d_4_a_ < x and d_5_b_ < x and d_6_c_ < x and Prime(d_4_a_) and Prime(d_5_b_) and Prime(d_6_c_) and x == d_4_a_ * d_5_b_ * d_6_c_) +# Assert(Exists(int, lambda d_3_c_: +# d_4_a_ < x and d_5_b_ < x and d_3_c_ < x and Prime(d_4_a_) and Prime(d_5_b_) and Prime(d_3_c_) and x == d_4_a_ * d_5_b_ * d_3_c_)) +# Assert(Exists(int, lambda d_2_b_: +# Exists(int, lambda d_3_c_: +# (d_4_a_< x and d_2_b_ < x and d_3_c_ < x and ((Prime(d_4_a_)) and (Prime(d_2_b_))) and (Prime(d_3_c_))) and ((x) == (((d_4_a_) * (d_2_b_)) * (d_3_c_)))))) +# # Assert(d_4_a_ < x and Prime(d_4_a_)) +# Assert(d_4_a_ < x and Prime(d_4_a_) and Exists(int, lambda d_1_a_: +# Exists(int, lambda d_2_b_: +# Exists(int, lambda d_3_c_: +# (d_1_a_< x and d_2_b_ < x and d_3_c_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_))) and (Prime(d_3_c_))) and ((x) == (((d_1_a_) * (d_2_b_)) * (d_3_c_))))))) +# d_6_c_ = d_6_c_ + 1 +# d_5_b_ = d_5_b_ + 1 +# d_4_a_ = d_4_a_ + 1 +# return result + + +def is__multiply__prime(x : int) -> bool: + Requires((x) > (1)) + Ensures(Implies(Result(), Exists(int, lambda d_1_a_: + Exists(int, lambda d_2_b_: + (((Prime(d_1_a_)) and (Prime(d_2_b_)))) and ((x) == (((d_1_a_) * (d_2_b_)))))))) + Ensures(Implies(not(Result()), Forall(int, lambda d_10_i_: + (Forall(int, lambda d_11_j_: + (Implies((1 < d_10_i_ and 1 < d_11_j_ and (d_10_i_) < (x) and d_11_j_ < x), Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), ((x) != (((d_10_i_) * (d_11_j_))))))))))))) + d_4_a_ = int(2) # type : int + result = False # type : bool + while d_4_a_ < x: + Invariant(x >= 2) + Invariant(d_4_a_ >= 2 and d_4_a_ <= x) + Invariant(Implies(result, Exists(int, lambda d_1_a_: + Exists(int, lambda d_2_b_: + (d_1_a_ < x and d_2_b_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_)))) and ((x) == (((d_1_a_) * (d_2_b_)))))))) + Invariant(Implies(not(result), Forall(int, lambda d_10_i_: + (Forall(int, lambda d_11_j_: + (Implies((1 < d_10_i_ and 1 < d_11_j_ and (d_10_i_) < (d_4_a_) and d_11_j_ < x), Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), ((x) != (((d_10_i_) * (d_11_j_))))))), [[Prime(d_11_j_)]])), [[Prime(d_10_i_)]])))) + if is__prime(d_4_a_): + d_5_b_ = int(2) # type : int + while d_5_b_ < x: + Invariant(x >= 2) + Invariant(Prime(d_4_a_)) + Invariant(d_4_a_ >= 2 and d_4_a_ < x) + Invariant(d_5_b_ >= 2 and d_5_b_ <= x) + Invariant(Implies(result, Exists(int, lambda d_1_a_: + Exists(int, lambda d_2_b_: + (d_1_a_ < x and d_2_b_ < x and ((Prime(d_1_a_)) and (Prime(d_2_b_)))) and ((x) == (((d_1_a_) * (d_2_b_)))))))) + Invariant(Implies(not(result), Forall(int, lambda d_8_j_: + (Implies(1 < d_8_j_ and d_8_j_ < d_5_b_, Implies(Prime(d_8_j_), ((x) != (((d_4_a_) * (d_8_j_)))))), [[Prime(d_8_j_)]])))) + Invariant(Implies(not(result), Forall(int, lambda d_10_i_: + (Forall(int, lambda d_11_j_: + (Implies((1 < d_10_i_ and 1 < d_11_j_ and (d_10_i_) < (d_4_a_) and d_11_j_ < x), Implies((Prime(d_10_i_)), Implies((Prime(d_11_j_)), ((x) != (((d_10_i_) * (d_11_j_))))))), [[Prime(d_11_j_)]])), [[Prime(d_10_i_)]])))) + if is__prime(d_5_b_) and x == d_4_a_ * d_5_b_: + result = True + d_5_b_ = d_5_b_ + 1 + d_4_a_ = d_4_a_ + 1 + return result diff --git a/WIP/WIP: 076-is_simple_power.py b/WIP/WIP: 076-is_simple_power.py new file mode 100644 index 0000000..cf1b517 --- /dev/null +++ b/WIP/WIP: 076-is_simple_power.py @@ -0,0 +1,56 @@ +from typing import cast, List, Dict, Set, Optional, Union, Tuple +from nagini_contracts.contracts import * + +@Pure +def power(x : int, y : int) -> int : + Requires(x >= 1) + Requires(y >= 0) + Ensures(Result() >= 1) + Ensures(Implies(x == 1, Result() == 1)) + if y == 0: + return 1 + else: + return x * power(x, y - 1) + +def is__simple__power(x : int, n : int) -> bool: + Requires((x) > (0)) + Requires(n >= 0) + # Ensures((Result()) == (Exists(int, lambda d_1_y_: + # n >= d_1_y_ and d_1_y_ >= 0 and (n) == (power(x, d_1_y_))))) + if (x) == (1): + Assert(Forall(int, lambda d_2_y_: (Implies(d_2_y_ >= 0, (power(x, d_2_y_)) == (1)), [[power(x, d_2_y_)]]))) + Assert(not ((n) == (1)) or ((n) == (power(x, 1)))) + return n == 1 + d_3_acc_ = int(0) # type : int + d_3_acc_ = 1 + d_4_i_ = int(0) # type : int + d_4_i_ = 0 + while (d_3_acc_) < (n): + Invariant(x > 1) + Invariant(n >= 0) + Invariant(d_4_i_ >= 0) + Invariant(d_3_acc_ >= 1) + Invariant(Forall(int, lambda y: (Implies(y >= 0, power(x, y + 1) == x * power(x, y)), [[power(x, y + 1)]]))) + Invariant(Forall(int, lambda y: (Implies(y >= 0, power(x, y + 1) > power(x, y)), [[power(x, y + 1)]]))) + Invariant(Forall(int, lambda y: (Implies(d_4_i_ > y and y >= 0, power(x, y) < n), [[power(x, y) < n]]))) + Invariant(d_4_i_ <= d_3_acc_) + Invariant(d_4_i_ <= n) + Invariant((d_3_acc_) == (power(x, d_4_i_))) + Invariant(Forall(int, lambda y: (Implies(d_4_i_ > y and y >= 0 and power(x, y) < n, power(x, y) != n)))) + Invariant(Forall(int, lambda y: (Implies(d_4_i_ > y and y >= 0, power(x, y) < n)))) + Invariant(Forall(int, lambda y: (Implies(d_4_i_ > y and y >= 0, power(x, y) != n)))) + d_3_acc_ = (x) * (d_3_acc_) + d_4_i_ = (d_4_i_) + (1) + if (d_3_acc_) == (n): + Assert((Exists(int, lambda d_1_y_: + n >= d_1_y_ and d_1_y_ >= 0 and (n) == (power(x, d_1_y_))))) + return True + else: + Assert(power(x, d_4_i_) > n) + Assert(Forall(int, lambda y: (Implies(d_4_i_ > y and y >= 0, power(x, y) < n)))) + # Assert(Forall(int, lambda d_6_j_: + # (Implies(n >= d_6_j_ and (d_6_j_) >= (d_4_i_), (power(x, d_6_j_)) >= power(x, d_4_i_)), [[(power(x, d_6_j_))]]))) + # Assert(Forall(int, lambda y: (Implies(y >= 0 and y < d_4_i_, power(x, y) != n), [[power(x, y) != n]]))) + # Assert(Forall(int, lambda d_6_j_: + # Implies(n >= d_6_j_ and (d_6_j_) >= (d_4_i_), (power(x, d_6_j_)) > (n)))) + return False diff --git a/WIP/WIP: 077-is_cube.py b/WIP/WIP: 077-is_cube.py new file mode 100644 index 0000000..14be67b --- /dev/null +++ b/WIP/WIP: 077-is_cube.py @@ -0,0 +1,50 @@ +from nagini_contracts.contracts import * + +def cube__root(N : int) -> int: + Requires((N) >= (0)) + Ensures((N) >= (0)) + Ensures(Result() >= 0) + Ensures(((cube(Result())) <= (N)) and ((N) < (cube((Result()) + (1))))) + Ensures((Result()) <= (N)) + # Ensures(Forall(int, lambda d_0_r_: Implies(r < d_0_r_ and d_0_r_ <= N, cube(d_0_r_) > N))) + # 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: 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: (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: 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 + + +def is__cube(n : int) -> bool: + Requires(n >= (0)) + Ensures((n) >= (0)) + Ensures(Implies(Result(), Exists(int, lambda d_0_r_: + (((0) <= (d_0_r_)) and ((d_0_r_) <= (n))) and ((n) == (cube(d_0_r_)))))) + # Ensures(Implies(not(Result()), Forall(int, lambda d_1_r_: + # Implies(((0) <= (d_1_r_)) and ((d_1_r_) <= (n)), (n) != (cube(d_1_r_)))))) + r = False # type : bool + d_2_root_ = int(0) # type : int + out0_ = cube__root(n) # type : int + d_2_root_ = out0_ + if (cube(d_2_root_)) == (n): + r = True + elif True: + r = False + return r + +@Pure +def cube(n : int) -> int : + Requires(n >= 0) + Ensures(Result() >= 0) + return ((n) * (n)) * (n) \ No newline at end of file diff --git a/WIP/WIP: 130-tri.py b/WIP/WIP: 130-tri.py new file mode 100644 index 0000000..d1f4390 --- /dev/null +++ b/WIP/WIP: 130-tri.py @@ -0,0 +1,58 @@ +from typing import cast, List, Dict, Set, Optional, Union +from nagini_contracts.contracts import * + +@Pure +def tri(n : int) -> int : + Requires((n) >= (0)) + if (n) == (1): + return 3 + elif ((n % 2)) == (0): + return (1) + ((n // 2)) + elif True: + return ((tri((n) - (2)))) + (tri((n) - (1))) + (tri((n) + (1))) + +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] + result[0] = 1 + 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_)))))) + while (d_1_i_) <= (n): + Invariant(Acc(list_pred(result))) + Invariant(n >= 1) + Invariant(((2) <= (d_1_i_)) and ((d_1_i_) <= ((n) + (1)))) + Invariant((len(result)) == (n + 1)) + Invariant(Forall(int, lambda x: (Implies(0 <= x and x <= n, tri(x) == (3 if x == 1 else ((1 + (x // 2)) if x % 2 == 0 else tri(x - 1) + tri(x - 2) + tri(x + 1)))), [[tri(x)]]))) + Invariant(Forall(int, lambda x: (Implies(2 <= x and x <= n and x % 2 == 1, (x + 1) % 2 == 0 and tri(x + 1) == (x + 3) // 2), [[tri(x + 1)]]))) + 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_)]]))) + if ((d_1_i_ % 2)) == (0): + result[d_1_i_] = (1) + ((d_1_i_ // 2)) + Assert(((result)[d_1_i_]) == (tri(d_1_i_))) + else: + 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(((result)[d_1_i_]) == (tri(d_1_i_))) + d_1_i_ = (d_1_i_) + (1) + return result diff --git a/WIP/WIP: 154-cycpattern_check.py b/WIP/WIP: 154-cycpattern_check.py new file mode 100644 index 0000000..e69de29 diff --git a/public/scripts/test.sh b/public/scripts/test.sh index 0bb5f60..53921a9 100755 --- a/public/scripts/test.sh +++ b/public/scripts/test.sh @@ -1,7 +1,10 @@ #!/bin/bash # Directory containing Python files -DIRECTORY="./" # You can change this to your specific directory +DIRECTORY="./Bench" # You can change this to your specific directory + +# Timeout duration in seconds +TIMEOUT_DURATION=600 echo "Running Nagini on Python files in $DIRECTORY" @@ -9,7 +12,7 @@ echo "Running Nagini on Python files in $DIRECTORY" for file in "$DIRECTORY"/*.py; do if [[ -f "$file" ]]; then echo "Running Nagini on $file" - nagini "$file" + timeout "$TIMEOUT_DURATION" nagini "$file" else echo "No Python files found in $DIRECTORY" fi