Skip to content

Commit

Permalink
Merge pull request #12 from alex28sh/93,112
Browse files Browse the repository at this point in the history
93, 112
  • Loading branch information
alex28sh authored Sep 2, 2024
2 parents 2a26238 + db15db6 commit e2ee087
Show file tree
Hide file tree
Showing 25 changed files with 909 additions and 331 deletions.
46 changes: 46 additions & 0 deletions Bench/016-count_distinct_characters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from typing import cast, List, Dict, Set, Optional, Union, Tuple
from nagini_contracts.contracts import *

@Pure
def contains_char(s : List[int], c : int, i : int, j : int) -> bool:
Requires(Acc(list_pred(s)))
Requires(Forall(int, lambda d_0_i_:
not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) or (((97) <= ((s)[d_0_i_])) and (((s)[d_0_i_]) <= (122)))))
Requires(0 <= i and i <= j and j <= len(s))
Requires(((97) <= (c)) and ((c) <= (122)))
if i == j:
return False
else:
return s[j - 1] == c or contains_char(s, c, i, j - 1)

@Pure
def count_chars_inter(s : List[int], c : int) -> int:
Requires(Acc(list_pred(s)))
Requires(Forall(int, lambda d_0_i_:
not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(s)))) or (((97) <= ((s)[d_0_i_])) and (((s)[d_0_i_]) <= (122)))))
Requires(((97) <= (c)) and ((c) <= (123)))
if c == 97:
return 0
else:
return count_chars_inter(s, c - 1) + (1 if contains_char(s, c - 1, 0, len(s)) else 0)

def count_distinct_characters(s : List[int]) -> int:
Requires(Acc(list_pred(s)))
Requires(Forall(int, lambda d_1_i_:
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((97) <= ((s)[d_1_i_])) and (((s)[d_1_i_]) <= (122)))))
Ensures(Acc(list_pred(s)))
Ensures(Forall(int, lambda d_1_i_:
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((97) <= ((s)[d_1_i_])) and (((s)[d_1_i_]) <= (122)))))
Ensures((Result()) == count_chars_inter(s, 123))
c = int(0) # type : int
d_2_i_ = int(97) # type : int
while (d_2_i_) <= (122):
Invariant(Acc(list_pred(s)))
Invariant(((97) <= (d_2_i_)) and ((d_2_i_) <= (123)))
Invariant(Forall(int, lambda d_1_i_:
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((97) <= ((s)[d_1_i_])) and (((s)[d_1_i_]) <= (122)))))
Invariant(c == count_chars_inter(s, d_2_i_))
if contains_char(s, d_2_i_, 0, len(s)):
c = c + 1
d_2_i_ = d_2_i_ + 1
return c
2 changes: 1 addition & 1 deletion Bench/026-remove_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def remove__duplicates(a : List[int]) -> List[int]:
def exists_check(a : List[int], x : int) -> bool:
Requires(Acc(list_pred(a), 1/2))
return Exists(int, lambda d_0_i_:
(Implies(((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))), ((a)[d_0_i_]) == (x))))
((((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))) and ((a)[d_0_i_]) == (x))))

@Pure
def count_check(a : List[int], x : int) -> bool:
Expand Down
72 changes: 30 additions & 42 deletions WIP/034-unique.py → Bench/034-unique.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
from typing import cast, List, Dict, Set, Optional, Union
from nagini_contracts.contracts import *

@Pure
def InArray(a : List[int], x : int) -> bool:
Requires(Acc(list_pred(a), 1/2))
return Exists(int, lambda d_0_i_:
((((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))) and ((a)[d_0_i_]) == (x))))



def uniqueSorted(s : List[int]) -> List[int]:
Requires(Acc(list_pred(s)))
Requires(Forall(int, lambda d_0_i_:
Expand All @@ -9,13 +17,13 @@ def uniqueSorted(s : List[int]) -> List[int]:
((s)[d_0_i_]) <= ((s)[d_1_j_])))))
Ensures(Acc(list_pred(s)))
Ensures(Acc(list_pred(Result())))
# Ensures(Forall(int, lambda d_2_i_:
# Forall(int, lambda d_3_j_:
# not ((((0) <= (d_2_i_)) and ((d_2_i_) < (d_3_j_))) and ((d_3_j_) < (len(Result())))) or (((Result())[d_2_i_]) < ((Result())[d_3_j_])))))
# Ensures(Forall(int, lambda d_4_x_:
# not ((d_4_x_) in (Result())) or ((d_4_x_) in (s))))
# Ensures(Forall(int, lambda d_5_x_:
# not ((d_5_x_) in (s)) or ((d_5_x_) in (Result()))))
Ensures(Forall(int, lambda d_9_k_:
(Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(Result()))), InArray(s, Result()[d_9_k_])))))
Ensures(Forall(int, lambda d_2_i_:
Forall(int, lambda d_3_j_:
not ((((0) <= (d_2_i_)) and ((d_2_i_) < (d_3_j_))) and ((d_3_j_) < (len(Result())))) or (((Result())[d_2_i_]) < ((Result())[d_3_j_])))))
Ensures(Forall(int, lambda d_11_j_:
(Implies(((0) <= (d_11_j_)) and ((d_11_j_) < (len(s))), InArray(Result(), s[d_11_j_])))))
result = list([int(0)] * 0) # type : List[int]
result = list([])
d_6_i_ = int(0) # type : int
Expand All @@ -28,44 +36,24 @@ def uniqueSorted(s : List[int]) -> List[int]:
Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len(s))),
((s)[d_0_i_]) <= ((s)[d_1_j_])))))
Invariant(((0) <= (d_6_i_)) and ((d_6_i_) <= (len(s))))
# Invariant(Forall(int, lambda d_7_k_:
# Forall(int, lambda d_8_l_:
# not ((((0) <= (d_7_k_)) and ((d_7_k_) < (d_8_l_))) and ((d_8_l_) < (len(result)))) or (((result)[d_7_k_]) < ((result)[d_8_l_])))))
Invariant(Forall(int, lambda d_9_k_:
(Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(result))), Exists(int, lambda d_10_m_:
(((0) <= (d_10_m_)) and ((d_10_m_) < (d_6_i_))) and (((result)[d_9_k_]) == ((s)[d_10_m_])))), [[(result)[d_9_k_]]])))
# Invariant(Forall(int, lambda d_11_k_:
# (Forall(int, lambda d_12_l_:
# (Implies(0 <= d_11_k_ and d_11_k_ < len(result),
# Implies(d_6_i_ <= d_12_l_ and d_12_l_ < len(s), result[d_11_k_] <= s[d_12_l_])),
# [[s[d_12_l_]]])), [[result[d_11_k_]]])))
# Invariant(Forall(int, lambda d_11_j_:
# not (((0) <= (d_11_j_)) and ((d_11_j_) < (d_6_i_))) or (((s)[d_11_j_]) in (result))))
(Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(result))), InArray(s, result[d_9_k_])), [[InArray(s, result[d_9_k_])]])))
Invariant(Implies(d_6_i_ < len(s),
Forall(int, lambda d_2_i_:
(Implies(((0) <= (d_2_i_)) and ((d_2_i_) < (len(result))), result[d_2_i_] <= s[d_6_i_]), [[result[d_2_i_]]]))))
Invariant(Forall(int, lambda d_7_k_:
(Forall(int, lambda d_8_l_:
(not ((((0) <= (d_7_k_)) and ((d_7_k_) < (d_8_l_))) and ((d_8_l_) < (len(result)))) or (((result)[d_7_k_]) < ((result)[d_8_l_])),
[[(result)[d_8_l_]]])),
[[(result)[d_7_k_]]])))
Invariant(Forall(int, lambda d_11_j_:
(Implies(((0) <= (d_11_j_)) and ((d_11_j_) < (d_6_i_)), InArray(result, s[d_11_j_])), [[]])))
if ((len(result)) == (0)) or (((result)[(len(result)) - (1)]) != ((s)[d_6_i_])):
# Assert(((len(result)) == (0)) or (((result)[(len(result)) - (1)]) < ((s)[d_6_i_])))
# old_res = list(result)
# Assert(Forall(int, lambda d_9_k_:
# (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(result))), Exists(int, lambda d_10_m_:
# (((0) <= (d_10_m_)) and ((d_10_m_) < (d_6_i_))) and (((result)[d_9_k_]) == ((s)[d_10_m_])))), [[(result)[d_9_k_]]])))
# Assert(Forall(int, lambda d_9_k_: Implies(0 <= d_9_k_ and d_9_k_ < len(result), result[d_9_k_] == old_res[d_9_k_])))
# Assert(Forall(int, lambda d_9_k_:
# (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(old_res))), Exists(int, lambda d_10_m_:
# (((0) <= (d_10_m_)) and ((d_10_m_) < (d_6_i_))) and (((old_res)[d_9_k_]) == ((s)[d_10_m_])))), [[(old_res)[d_9_k_]]])))
Assert(Implies(len(result) > 0, result[len(result) - 1] < s[d_6_i_]))
Assert(Implies(len(result) > 0,
Forall(int, lambda d_11_j_:
Implies(0 <= d_11_j_ and d_11_j_ < len(result), result[d_11_j_] < s[d_6_i_]))))
result = (result) + [(s)[d_6_i_]]
# Assert(Exists(int, lambda d_10_m_:
# (((0) <= (d_10_m_)) and ((d_10_m_) <= (d_6_i_))) and (((result)[len(result) - 1]) == ((s)[d_10_m_]))))
# Assert(Forall(int, lambda d_9_k_:
# (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(old_res))), Exists(int, lambda d_10_m_:
# (((0) <= (d_10_m_)) and ((d_10_m_) < (d_6_i_))) and (((old_res)[d_9_k_]) == ((s)[d_10_m_])))), [[(old_res)[d_9_k_]]])))
# Assert(Forall(int, lambda d_9_k_:
# (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(old_res))), Exists(int, lambda d_10_m_:
# (((0) <= (d_10_m_)) and ((d_10_m_) <= (d_6_i_))) and (((old_res)[d_9_k_]) == ((s)[d_10_m_])))), [[(old_res)[d_9_k_]]])))
# Assert(Forall(int, lambda d_9_k_:
# (Implies(((0) <= (d_9_k_)) and ((d_9_k_) + 1 < (len(result))), Exists(int, lambda d_10_m_:
# (((0) <= (d_10_m_)) and ((d_10_m_) <= (d_6_i_))) and (((result)[d_9_k_]) == ((s)[d_10_m_])))), [[(result)[d_9_k_]]])))
# Assert(Forall(int, lambda d_9_k_:
# (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(result))), Exists(int, lambda d_10_m_:
# (((0) <= (d_10_m_)) and ((d_10_m_) <= (d_6_i_))) and (((result)[d_9_k_]) == ((s)[d_10_m_])))), [[(result)[d_9_k_]]])))
d_6_i_ = (d_6_i_) + (1)
return result

Expand Down
57 changes: 57 additions & 0 deletions Bench/036-fizz_buzz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from typing import cast, List, Dict, Set, Optional, Union
from nagini_contracts.contracts import *

def fizz__buzz(n : int) -> int:
Requires(n >= 0)
Ensures(Result() >= 0)
Ensures((Result()) == fizz_buzz_fun(n))
result = int(0) # type : int
result = 0
d_1_i_ = int(0) # type : int
d_1_i_ = 0
while (d_1_i_) < (n):
Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (n)))
Invariant(Forall(int, lambda x : (Implies(x >= 0 and x < n,
fizz_buzz_fun(x + 1) == (count7__r(x) if ((x % 11 == 0) or (x % 13 == 0)) else 0) + fizz_buzz_fun(x)), [[fizz_buzz_fun(x + 1)]])))
Invariant(result == fizz_buzz_fun(d_1_i_))
if (((d_1_i_ % 11)) == (0)) or (((d_1_i_ % 13)) == (0)):
d_4_cnt_ = int(0) # type : int
d_4_cnt_ = count7(d_1_i_)
result = (result) + (d_4_cnt_)
d_1_i_ = (d_1_i_) + (1)
return result

@Pure
def fizz_buzz_fun(n : int) -> int:
Requires(n >= 0)
Ensures(Result() >= 0)
if n == 0:
return 0
else:
return (count7__r(n - 1) if ((n - 1) % 11 == 0 or (n - 1) % 13 == 0) else 0) + fizz_buzz_fun(n - 1)


def count7(x : int) -> int:
Requires(x >= 0)
Ensures(Result() >= 0)
Ensures((Result()) == (count7__r(x)))
count = int(0) # type : int
count = 0
d_6_y_ = int(0) # type : int
d_6_y_ = x
while (d_6_y_) > (0):
Invariant(((0) <= (d_6_y_)) and ((d_6_y_) <= (x)))
Invariant(((count) + (count7__r(d_6_y_))) == (count7__r(x)))
if ((d_6_y_ % 10)) == (7):
count = (count) + (1)
d_6_y_ = d_6_y_ // 10
return count

@Pure
def count7__r(x : int) -> int :
Requires(x >= 0)
Ensures(Result() >= 0)
if x == 0:
return 0
else:
return (x % 10 == 7) + count7__r(x // 10)
27 changes: 27 additions & 0 deletions Bench/084-solve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import cast, List, Dict, Set, Optional, Union, Tuple
from nagini_contracts.contracts import *

def solve(n : int) -> int:
Requires((n) >= (0))
Ensures(Result() >= 0)
Ensures((Result()) == (popcount(n)))
r = int(0) # type : int
d_0_m_ = int(0) # type : int
d_0_m_ = n
r = 0
while (d_0_m_) > (0):
Invariant(((0) <= (d_0_m_)) and ((d_0_m_) <= (n)))
Invariant(r >= 0)
Invariant(((r) + (popcount(d_0_m_))) == (popcount(n)))
r = (r) + ((d_0_m_ % 2))
d_0_m_ = (d_0_m_ // 2)
return r

@Pure
def popcount(n : int) -> int :
Requires(n >= 0)
if n == 0:
return 0
else:
return (n % 2) + popcount(n // 2)

4 changes: 2 additions & 2 deletions Bench/093-encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def encode(s : List[int]) -> List[int]:
Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(s))))
Invariant((len(t)) == (d_3_i_))
Invariant(Forall(int, lambda d_4_j_:
not ((((0) <= (d_4_j_)) and ((d_4_j_) < (d_3_i_))) and (is__vowel((s)[d_4_j_]))) or (((t)[d_4_j_]) == (rot2(swap__case((s)[d_4_j_]))))))
(not ((((0) <= (d_4_j_)) and ((d_4_j_) < (d_3_i_))) and (is__vowel((s)[d_4_j_]))) or (((t)[d_4_j_]) == (rot2(swap__case((s)[d_4_j_])))), [[rot2(swap__case((s)[d_4_j_]))]])))
Invariant(Forall(int, lambda d_5_j_:
not ((((0) <= (d_5_j_)) and ((d_5_j_) < (d_3_i_))) and (not(is__vowel((s)[d_5_j_])))) or (((t)[d_5_j_]) == (swap__case((s)[d_5_j_])))))
(not ((((0) <= (d_5_j_)) and ((d_5_j_) < (d_3_i_))) and (not(is__vowel((s)[d_5_j_])))) or (((t)[d_5_j_]) == (swap__case((s)[d_5_j_]))), [[swap__case((s)[d_5_j_])]])))
if is__vowel((s)[d_3_i_]):
t = (t) + [rot2(swap__case((s)[d_3_i_]))]
else:
Expand Down
Loading

0 comments on commit e2ee087

Please sign in to comment.