Skip to content

Commit

Permalink
26, 49, 64, 69, 73, 89, 108, 114, 128
Browse files Browse the repository at this point in the history
  • Loading branch information
alex28sh committed Aug 27, 2024
1 parent 67d0d84 commit fae95e9
Show file tree
Hide file tree
Showing 14 changed files with 462 additions and 85 deletions.
44 changes: 44 additions & 0 deletions Bench/026-remove_duplicates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import cast, List, Dict, Set, Optional, Union
from nagini_contracts.contracts import *

def remove__duplicates(a : List[int]) -> List[int]:
Requires(Acc(list_pred(a), 1/2))
Ensures(Acc(list_pred(a), 1/2))
Ensures(Acc(list_pred(Result())))
Ensures(len(a) == len(Old(a)))
Ensures(len(a) >= len(Result()))
Ensures(Forall(int, lambda d_2_i_:
Implies(((0) <= (d_2_i_)) and ((d_2_i_) < (len(a))) and (count__rec(a, a[d_2_i_], len(a)) == 1), Exists(int, lambda d_3_i_: d_3_i_ >= 0 and d_3_i_ < len(Result()) and Result()[d_3_i_] == a[d_2_i_]))))
result = list([int(0)] * 0) # type : List[int]
result = []
d_4_i_ = int(0) # type : int
d_4_i_ = 0
a_old = list(a)

while (d_4_i_) < (len(a)):
Invariant(Acc(list_pred(result)))
Invariant(Acc(list_pred(a), 1/2))
Invariant(Acc(list_pred(a_old), 1/2))
Invariant(len(a) == len(a_old))
Invariant(Forall(int, lambda d_3_i_: (Implies(d_3_i_ >= 0 and d_3_i_ < len(a), a_old[d_3_i_] == a[d_3_i_]))))
Invariant(((0) <= (d_4_i_)) and ((d_4_i_) <= (len(a))))
Invariant(len(result) <= d_4_i_)
Invariant(Forall(int, lambda d_2_i_:
(Implies(((0) <= (d_2_i_)) and ((d_2_i_) < (d_4_i_)) and (count__rec(a, a[d_2_i_], len(a)) == 1), Exists(int, lambda d_3_i_: d_3_i_ >= 0 and d_3_i_ < len(result) and result[d_3_i_] == a[d_2_i_])),
[[a[d_2_i_]]])))
d_8_cnt_ = int(0) # type : int
d_8_cnt_ = count__rec(a, (a)[d_4_i_], len(a))
if (d_8_cnt_) == (1):
result = (result) + [(a)[d_4_i_]]
Assert(count__rec(a, result[len(result) - 1], len(a)) == 1)
d_4_i_ = (d_4_i_) + (1)
return result

@Pure
def count__rec(a : List[int], x : int, i : int) -> int :
Requires(Acc(list_pred(a), 1/2))
Requires(((0) <= (i)) and ((i) <= (len(a))))
if (i) == 0:
return 0
else:
return (((a)[i - 1]) == (x)) + (count__rec(a, x, (i) - (1)))
28 changes: 28 additions & 0 deletions Bench/049-modp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import cast, List, Dict, Set, Optional, Union
from nagini_contracts.contracts import *

@Pure
def modp__rec(n : int, p : int) -> int :
Requires((p) > (0))
Requires((n) >= (0))
if (n) == (0):
return (1 % p)
elif True:
return ((modp__rec((n) - (1), p)) * (2)) % p

def modp(n : int, p : int) -> int:
Requires((p) > (0))
Requires((n) >= (0))
Ensures((Result()) == (modp__rec(n, p)))
r = int(0) # type : int
r = (1 % p)
d_0_i_ = int(0) # type : int
d_0_i_ = 0
while (d_0_i_) < (n):
Invariant(((0) <= (d_0_i_)) and ((d_0_i_) <= (n)))
Invariant(Forall(int, lambda d_0_i_: (Implies(d_0_i_ >= 0 and d_0_i_ < n, modp__rec(d_0_i_ + 1, p) == (modp__rec(d_0_i_, p) * 2) % p))))
Invariant((r) == (modp__rec(d_0_i_, p)))
Assert(modp__rec(d_0_i_ + 1, p) == (modp__rec(d_0_i_, p) * 2) % p)
r = (((r) * (2)) % p)
d_0_i_ = (d_0_i_) + (1)
return r
42 changes: 42 additions & 0 deletions Bench/064-vowel_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import cast, List, Dict, Set, Optional, Union
from nagini_contracts.contracts import *

def vowel__count(s : List[int]) -> int:
Requires(Acc(list_pred(s)))
Ensures(Acc(list_pred(s)))
Ensures((Result()) >= (0))
Ensures(Result() == ((count_fun(0, len(s), s)) + ((1 if ((len(s)) > (0)) and (((s)[(len(s)) - (1)]) == (121)) else 0))))
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 >= 0)
Invariant(Forall(int, lambda d_0_i_:
(Implies(d_0_i_ >= 0 and d_0_i_ < len(s), count_fun(0, d_0_i_ + 1, s) == count_fun(0, d_0_i_, s) + (1 if is__vowel(s[d_0_i_]) else 0)), [[count_fun(0, d_0_i_ + 1, s)]])))
Invariant((count) == (count_fun(0, d_1_i_, s)))
Assert(count_fun(0, d_1_i_ + 1, s) == count_fun(0, d_1_i_, s) + (1 if is__vowel(s[d_1_i_]) else 0))
if is__vowel((s)[d_1_i_]):
count = (count) + (1)
d_1_i_ = (d_1_i_) + (1)
count = (count) + ((1 if ((len(s)) > (0)) and (((s)[(len(s)) - (1)]) == (121)) else 0))
return count

@Pure
def count_fun(i : int, j : int, s : List[int]) -> int:
Requires(Acc(list_pred(s)))
Requires(((0) <= (i)) and ((i) <= (j)) and ((j) <= (len(s))))
Ensures((Result()) >= (0))
if i == j:
return 0
else:
if is__vowel((s)[j - 1]):
return (1) + (count_fun(i, j - 1, s))
else:
return count_fun(i, j - 1, s)

@Pure
def is__vowel(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))
30 changes: 30 additions & 0 deletions Bench/069-search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import cast, List, Dict, Set, Optional, Union
from nagini_contracts.contracts import *

@Pure
def freq_req(i : int, j : int, s : List[int], x : 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] == x) + (freq_req(i, j - 1, s, x))

def freq(s : List[int], x : int) -> int:
Requires(Acc(list_pred(s)))
Ensures(Acc(list_pred(s)))
Ensures(Result() == freq_req(0, len(s), s, x))
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(Forall(int, lambda y : (Implies(y >= 0 and y < len(s), freq_req(0, y + 1, s, x) == freq_req(0, y, s, x) + (s[y] == x)), [[freq_req(0, y + 1, s, x)]])))
Invariant(count == freq_req(0, d_1_i_, s, x))
Assert(freq_req(0, d_1_i_ + 1, s, x) == freq_req(0, d_1_i_, s, x) + (s[d_1_i_] == x))
if ((s)[d_1_i_]) == (x):
count = (count) + (1)
d_1_i_ = (d_1_i_) + (1)
return count
34 changes: 34 additions & 0 deletions Bench/073-smallest_change.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import cast, List, Dict, Set, Optional, Union
from nagini_contracts.contracts import *

@Pure
def smallest__change__fun(s : List[int], i : int, j : int) -> int:
Requires(Acc(list_pred(s)))
Requires(((0) <= (i)) and ((i) <= (j)) and ((j) <= (len(s) // 2)))
Ensures((Result()) >= (0))
if i == j:
return 0
else:
if (s)[j - 1] != (s)[len(s) - j]:
return 1 + (smallest__change__fun(s, i, j - 1))
else:
return smallest__change__fun(s, i, j - 1)

def smallest__change(s : List[int]) -> int:
Requires(Acc(list_pred(s)))
Ensures(Acc(list_pred(s)))
Ensures((Result()) == (smallest__change__fun(s, 0, len(s) // 2)))
c = int(0) # type : int
c = 0
d_1_i_ = int(0) # type : int
d_1_i_ = 0
while (d_1_i_) < ((len(s) // 2)):
Invariant(Acc(list_pred(s)))
Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= ((len(s) // 2))))
Invariant(Forall(int, lambda d_0_i_: (Implies(d_0_i_ >= 0 and d_0_i_ < len(s) // 2, smallest__change__fun(s, 0, d_0_i_ + 1) == (smallest__change__fun(s, 0, d_0_i_) + 1 if (s)[d_0_i_] != (s)[len(s) - d_0_i_ - 1] else smallest__change__fun(s, 0, d_0_i_))), [[smallest__change__fun(s, 0, d_0_i_ + 1)]])))
Invariant(c == smallest__change__fun(s, 0, d_1_i_))
Assert(smallest__change__fun(s, 0, d_1_i_ + 1) == (smallest__change__fun(s, 0, d_1_i_) + 1 if (s)[d_1_i_] != (s)[len(s) - d_1_i_ - 1] else smallest__change__fun(s, 0, d_1_i_)))
if ((s)[d_1_i_]) != ((s)[((len(s)) - (1)) - (d_1_i_)]):
c = (c) + (1)
d_1_i_ = (d_1_i_) + (1)
return c
40 changes: 40 additions & 0 deletions Bench/089-encrypt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from typing import cast, List, Dict, Set, Optional, Union, Tuple
from nagini_contracts.contracts import *

@Pure
def rot__sym(c : int) -> int :
Requires(c >= 0 and c <= 25)
Ensures(Result() >= 0 and Result() <= 25)
d_0_alph_ = c - 0 # type : int
return ((d_0_alph_) + ((2) * (2))) % 26

def encrypt(s : List[int]) -> List[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 (((0) <= ((s)[d_1_i_])) and (((s)[d_1_i_]) <= (25)))))
Ensures(Acc(list_pred(s)))
Ensures(Acc(list_pred(Result())))
Ensures((len(Result())) == (len(s)))
Ensures(Forall(int, lambda d_1_i_:
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((0) <= ((s)[d_1_i_])) and (((s)[d_1_i_]) <= (25)))))
Ensures(Forall(int, lambda d_1_i_:
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(Result())))) or (((0) <= ((Result())[d_1_i_])) and (((Result())[d_1_i_]) <= (25)))))
Ensures(Forall(int, lambda d_2_i_:
not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(s)))) or (((Result())[d_2_i_]) == (rot__sym((s)[d_2_i_])))))
r : List[int] = [] # type : List[int]
d_3_i_ = int(0) # type : int
d_3_i_ = 0
while (d_3_i_) < (len(s)):
Invariant(Acc(list_pred(s)))
Invariant(Acc(list_pred(r)))
Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(s))))
Invariant((len(r)) == (d_3_i_))
Invariant(Forall(int, lambda d_1_i_:
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) or (((0) <= ((s)[d_1_i_])) and (((s)[d_1_i_]) <= (25)))))
Invariant(Forall(int, lambda d_1_i_:
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(r)))) or (((0) <= ((r)[d_1_i_])) and (((r)[d_1_i_]) <= (25)))))
Invariant(Forall(int, lambda d_4_j_:
not (((0) <= (d_4_j_)) and ((d_4_j_) < (d_3_i_))) or (((r)[d_4_j_]) == (rot__sym((s)[d_4_j_])))))
r = (r) + [rot__sym((s)[d_3_i_])]
d_3_i_ = (d_3_i_) + (1)
return r
48 changes: 48 additions & 0 deletions Bench/108-count_nums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from typing import cast, List, Dict, Set, Optional, Union
from nagini_contracts.contracts import *

def count__nums(s : List[int]) -> int:
Requires(Acc(list_pred(s)))
Ensures(Acc(list_pred(s)))
Ensures(Result() == get_positive(0, len(s), s))
cnt = int(0) # type : int
cnt = 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 x :
(Implies(0 <= x and x < len(s), get_positive(0, x + 1, s) == ((digits__sum(s[x]) > 0) + get_positive(0, x, s))),
[[get_positive(0, x + 1, s)]])))
Invariant((cnt) == (get_positive(0, d_1_i_, s)))
Assert(get_positive(0, d_1_i_ + 1, s) == (digits__sum(s[d_1_i_]) > 0) + get_positive(0, d_1_i_, s))
if (digits__sum((s)[d_1_i_])) > (0):
cnt = (cnt) + (1)
d_1_i_ = (d_1_i_) + (1)
return cnt

@Pure
def get_positive(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 (digits__sum(s[j - 1]) > 0) + get_positive(i, j - 1, s)

@Pure
def digits__sum(x : int) -> int :
if abs(x) < 10:
return x % 10
else:
return (10 - x % 10) + digits__sum(x // 10)

@Pure
def abs(x : int) -> int :
Ensures((Result()) >= (0))
Ensures((Result()) == (x) or (Result()) == (0) - (x))
if (x) >= (0):
return x
elif True:
return (0) - (x)
56 changes: 56 additions & 0 deletions Bench/114-minSubArraySum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from typing import cast, List, Dict, Set, Optional, Union
from nagini_contracts.contracts import *

@Pure
def Sum(a : List[int], s : int, t : int) -> int :
Requires(Acc(list_pred(a)))
Requires(((0) <= (s)) and ((s) <= (t)) and ((t) <= (len(a))))
if s == t:
return 0
else:
return (a)[t - 1] + (Sum(a, s, t - 1))

def minSubArraySum(a : List[int]) -> int:
Requires(Acc(list_pred(a)))
Ensures(Acc(list_pred(a)))
Ensures(Forall(int, lambda d_1_p_:
Forall(int, lambda d_2_q_:
not ((((0) <= (d_1_p_)) and ((d_1_p_) <= (d_2_q_))) and ((d_2_q_) <= (len(a)))) or ((Sum(a, d_1_p_, d_2_q_)) >= (Result())))))
Ensures(Exists(int, lambda d_3_k_:
Exists(int, lambda d_4_m_:
((((0) <= (d_3_k_)) and ((d_3_k_) <= (d_4_m_))) and ((d_4_m_) <= (len(a)))) and ((Result()) == (Sum(a, d_3_k_, d_4_m_))))))
s = int(0) # type : int
d_5_k_ = int(0) # type : int
d_6_m_ = int(0) # type : int
d_7_n_ = int(0) # type : int
d_8_c_ = int(0) # type : int
d_9_t_ = int(0) # type : int
while (d_7_n_) < (len(a)):
Invariant(Acc(list_pred(a)))
Invariant(((0) <= (d_7_n_)) and ((d_7_n_) <= (len(a))))
Invariant(Forall(int, lambda d_1_p_: (Implies(0 <= d_1_p_ and d_1_p_ < len(a), Sum(a, 0, d_1_p_ + 1) == Sum(a, 0, d_1_p_) + a[d_1_p_]))))
Invariant(Forall(int, lambda st:
Implies(st >= 0 and st < len(a),
Forall(int, lambda d_1_p_: (Implies(st <= d_1_p_ and d_1_p_ < len(a), Sum(a, st, d_1_p_ + 1) == Sum(a, st, d_1_p_) + a[d_1_p_]))))))
Invariant(((((0) <= (d_8_c_)) and ((d_8_c_) <= (d_7_n_))) and ((d_7_n_) <= (len(a)))))
Invariant(((d_9_t_) == (Sum(a, d_8_c_, d_7_n_))))
Invariant(((((0) <= (d_5_k_)) and ((d_5_k_) <= (d_6_m_))) and ((d_6_m_) <= (d_7_n_))))
Invariant(((s) == (Sum(a, d_5_k_, d_6_m_))))
Invariant(Forall(int, lambda d_10_b_:
not (((0) <= (d_10_b_)) and ((d_10_b_) <= (d_7_n_))) or ((Sum(a, d_10_b_, d_7_n_)) >= (Sum(a, d_8_c_, d_7_n_)))))
Invariant(Sum(a, d_8_c_, d_7_n_) >= Sum(a, d_5_k_, d_6_m_))
Invariant(Forall(int, lambda d_11_p_:
Forall(int, lambda d_12_q_:
(not ((((0) <= (d_11_p_)) and ((d_11_p_) <= (d_12_q_))) and ((d_12_q_) <= (d_7_n_))) or ((Sum(a, d_11_p_, d_12_q_)) >= (Sum(a, d_5_k_, d_6_m_))), [[Sum(a, d_11_p_, d_12_q_)]]))))
Assert(Sum(a, d_8_c_, d_7_n_ + 1) == Sum(a, d_8_c_, d_7_n_) + a[d_7_n_])
d_9_t_ = (d_9_t_) + ((a)[d_7_n_])
d_7_n_ = (d_7_n_) + (1)
if (d_9_t_) > (0):
d_8_c_ = d_7_n_
d_9_t_ = 0
elif (s) > (d_9_t_):
d_5_k_ = d_8_c_
d_6_m_ = d_7_n_
s = d_9_t_
return s

65 changes: 65 additions & 0 deletions Bench/128-prod_signs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from typing import cast, List, Dict, Set, Optional, Union, Tuple
from nagini_contracts.contracts import *

@Pure
def abs(x : int) -> int :
if (x) >= (0):
return x
elif True:
return (0) - (x)

@Pure
def sum__abs(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 abs(s[j - 1]) + sum__abs(i, j - 1, s)

@Pure
def count_negatives(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 (1 if s[j - 1] < 0 else 0) + count_negatives(i, j - 1, s)

def prod__signs(numbers : List[int]) -> int:
Requires(Acc(list_pred(numbers)))
Ensures(Acc(list_pred(numbers)))
Ensures((abs(Result())) == (sum__abs(0, len(numbers), numbers)))
Ensures(Implies(count_negatives(0, len(numbers), numbers) % 2 == 1, Result() <= 0))
Ensures(Implies(count_negatives(0, len(numbers), numbers) % 2 == 0, Result() >= 0))
s = int(0) # type : int
s = 0
d_3_i_ = int(0) # type : int
d_3_i_ = 0
while (d_3_i_) < (len(numbers)):
Invariant(Acc(list_pred(numbers)))
Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(numbers))))
Invariant(Forall(int, lambda x :
(Implies(0 <= x and x < len(numbers), sum__abs(0, x + 1, numbers) == sum__abs(0, x, numbers) + abs(numbers[x])),
[[sum__abs(0, x + 1, numbers)]])))
Invariant((s) == (sum__abs(0, d_3_i_, numbers)))
Invariant(s >= 0)
Assert(sum__abs(0, d_3_i_ + 1, numbers) == sum__abs(0, d_3_i_, numbers) + abs(numbers[d_3_i_]))
s = (s) + (abs((numbers)[d_3_i_]))
d_3_i_ = (d_3_i_) + (1)
d_3_i_ = 0
d_4_cnt_ = int(0) # type : int
d_4_cnt_ = 0
while (d_3_i_) < (len(numbers)):
Invariant(Acc(list_pred(numbers)))
Invariant(s == sum__abs(0, len(numbers), numbers))
Invariant(s >= 0)
Invariant(((0) <= (d_3_i_)) and ((d_3_i_) <= (len(numbers))))
Invariant(Forall(int, lambda x : Implies(0 <= x and x < len(numbers), count_negatives(0, x + 1, numbers) == count_negatives(0, x, numbers) + (1 if numbers[x] < 0 else 0))))
Invariant((d_4_cnt_) == (count_negatives(0, d_3_i_, numbers)))
if ((numbers)[d_3_i_]) < (0):
d_4_cnt_ = (d_4_cnt_) + (1)
d_3_i_ = (d_3_i_) + (1)
if ((d_4_cnt_ % 2)) == (1):
s = (0) - (s)
return s
Loading

0 comments on commit fae95e9

Please sign in to comment.