Skip to content

Commit

Permalink
12, 98, 136
Browse files Browse the repository at this point in the history
  • Loading branch information
alex28sh committed Aug 26, 2024
1 parent 8924829 commit 67d0d84
Show file tree
Hide file tree
Showing 9 changed files with 459 additions and 94 deletions.
49 changes: 49 additions & 0 deletions Bench/012-longest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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() is not None) or (Exists(int, lambda d_1_s_:
((d_1_s_) >= 0 and d_1_s_ < len(strings)) and ((len(strings[getVal(Result())])) == (len(strings[d_1_s_]))))))
Ensures(not ((Result()) is not (None)) or (Forall(int, lambda d_4_j_:
(not (((0) <= (d_4_j_)) and ((d_4_j_) < (Result()))) or ((len((strings)[d_4_j_])) < (len(strings[getVal(Result())])))))))
result : Optional[int] = 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 (result is not None))
Invariant(not ((d_5_i_) > (0)) or ((d_6_mx_) == (len(strings[getVal(result)]))))
Invariant(not (result is not None) or (Forall(int, lambda d_1_s_:
not ((d_1_s_) >= 0 and d_1_s_ < d_5_i_) or ((len(strings[getVal(result)])) >= (len(strings[d_1_s_]))))))
Invariant(not (result is not None) or (Exists(int, lambda d_1_s_:
((d_1_s_) >= 0 and d_1_s_ < d_5_i_) and ((len(strings[getVal(result)])) == (len(strings[d_1_s_]))))))
Invariant(not ((result) is not (None)) or (Forall(int, lambda d_4_j_:
(not (((0) <= (d_4_j_)) and ((d_4_j_) < (result))) or ((len((strings)[d_4_j_])) < (len(strings[getVal(result)]))), [[((strings)[d_4_j_])]]))))
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_
Assert(Forall(int, lambda x: Implies(x >= 0 and x < result, len(strings[result]) > len(strings[x]))))
d_5_i_ = (d_5_i_) + (1)
return result
43 changes: 43 additions & 0 deletions Bench/098-count_upper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import cast, List, Dict, Set, Optional, Union, Tuple
from nagini_contracts.contracts import *

@Pure
def count__upper__fun(s : List[int], i : int, j : 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__upper__vowel((s)[j - 1]) and (j - 1) % 2 == 0:
return (1) + (count__upper__fun(s, i, j - 1))
else:
return count__upper__fun(s, i, j - 1)


def count__upper(s : List[int]) -> int:
Requires(Acc(list_pred(s)))
Ensures((Result()) >= (0))
Ensures(Acc(list_pred(s)))
Ensures((Result()) == (count__upper__fun(s, 0, len(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 d_0_i_: (Implies(d_0_i_ >= 0 and d_0_i_ < len(s),
count__upper__fun(s, 0, d_0_i_ + 1) == (count__upper__fun(s, 0, d_0_i_) + (1) if is__upper__vowel(s[d_0_i_]) and d_0_i_ % 2 == 0 else count__upper__fun(s, 0, d_0_i_))),
[[count__upper__fun(s, 0, d_0_i_ + 1)]])))
Invariant((cnt) == (count__upper__fun(s, 0, d_1_i_)))
Assert(count__upper__fun(s, 0, d_1_i_ + 1) == (count__upper__fun(s, 0, d_1_i_) + (1) if is__upper__vowel(s[d_1_i_]) and d_1_i_ % 2 == 0 else count__upper__fun(s, 0, d_1_i_)))
if (is__upper__vowel((s)[d_1_i_])) and (((d_1_i_ % 2)) == (0)):
cnt = (cnt) + (1)
d_1_i_ = (d_1_i_) + (1)
return cnt

@Pure
def is__upper__vowel(c : int) -> bool :
return (((((c) == (0)) or ((c) == (1))) or ((c) == (2))) or ((c) == (3))) or ((c) == (4))
44 changes: 44 additions & 0 deletions Bench/136-largest_smallest_integers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import cast, List, Dict, Set, Optional, Union, Tuple
from nagini_contracts.contracts import *

@Pure
def getVal(mx: Optional[int]) -> int:
Requires(mx is not None)
return mx

def largest__smallest__integers(arr : List[int]) -> Tuple[Optional[int], Optional[int]]:
Requires(Acc(list_pred(arr)))
Ensures(Acc(list_pred(arr)))
Ensures(not ((Result()[0]) is None) or (Forall(int, lambda d_0_i_:
not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(arr)))) or (((arr)[d_0_i_]) >= (0)))))
Ensures(not ((Result()[0]) is not None) or ((((Result()[0])) in (arr)) and (((Result()[0])) < (0))))
Ensures(not ((Result()[0]) is not None) or (Forall(int, lambda d_1_i_:
not ((((0) <= (d_1_i_)) and ((d_1_i_) < (len(arr)))) and (((arr)[d_1_i_]) < (0))) or (((arr)[d_1_i_]) <= (getVal(Result()[0]))))))
Ensures(not ((Result()[1]) is None) or (Forall(int, lambda d_2_i_:
not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(arr)))) or (((arr)[d_2_i_]) <= (0)))))
Ensures(not ((Result()[1]) is not None) or (((getVal(Result()[1])) in (arr)) and ((getVal(Result()[1])) > (0))))
Ensures(not ((Result()[1]) is not None) or (Forall(int, lambda d_3_i_:
not ((((0) <= (d_3_i_)) and ((d_3_i_) < (len(arr)))) and (((arr)[d_3_i_]) > (0))) or (((arr)[d_3_i_]) >= (getVal(Result()[1]))))))
a : Optional[int] = None
b : Optional[int] = None
d_4_i_ = int(0)
while (d_4_i_) < (len(arr)):
Invariant(Acc(list_pred(arr)))
Invariant(((0) <= (d_4_i_)) and ((d_4_i_) <= (len(arr))))
Invariant(not ((a) is None) or (Forall(int, lambda d_5_j_:
not (((0) <= (d_5_j_)) and ((d_5_j_) < (d_4_i_))) or (((arr)[d_5_j_]) >= (0)))))
Invariant(Old(a) is None or (Old(a) is not None and Old(a) <= a))
Invariant(not ((a) is not None) or ((Exists(int, lambda x: x >= 0 and x < d_4_i_ and arr[x] == a)) and ((a) < (0))))
Invariant(not ((a) is not None) or (Forall(int, lambda d_6_j_:
not ((((0) <= (d_6_j_)) and ((d_6_j_) < (d_4_i_))) and (((arr)[d_6_j_]) < (0))) or (((arr)[d_6_j_]) <= (getVal(a))))))
Invariant(not ((b) is None) or (Forall(int, lambda d_7_j_:
not (((0) <= (d_7_j_)) and ((d_7_j_) < (d_4_i_))) or (((arr)[d_7_j_]) <= (0)))))
Invariant(not ((b) is not None) or (((getVal(b)) in (arr)) and ((getVal(b)) > (0))))
Invariant(not ((b) is not None) or (Forall(int, lambda d_8_j_:
not ((((0) <= (d_8_j_)) and ((d_8_j_) < (d_4_i_))) and (((arr)[d_8_j_]) > (0))) or (((arr)[d_8_j_]) >= (getVal(b))))))
if (((arr)[d_4_i_]) < (0)) and (((a) is None) or (((arr)[d_4_i_]) >= (a))):
a = ((arr)[d_4_i_])
if (((arr)[d_4_i_]) > (0)) and (((b) is None) or (((arr)[d_4_i_]) <= (getVal(b)))):
b = ((arr)[d_4_i_])
d_4_i_ = (d_4_i_) + (1)
return (a, b)
86 changes: 68 additions & 18 deletions WIP/006-parse_nested_parens.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from nagini_contracts.contracts import *

# def parse__paren__group(s : List[int]) -> int:
# Requires(Acc(list_pred(s)))
# 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))
Expand All @@ -12,6 +13,9 @@
# d_2_i_ = int(0) # type : int
# d_2_i_ = 0
# while (d_2_i_) < (len(s)):
# Invariant(Acc(list_pred(s)))
# Invariant(((d_2_i_) >= (0)) and ((d_2_i_) <= (len(s))))
# Invariant(max__depth >= 0)
# d_3_c_ = (s)[d_2_i_] # type : int
# if (d_3_c_) == (1):
# d_1_depth_ = (d_1_depth_) + (1)
Expand All @@ -22,38 +26,84 @@
# 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_])))))
def split(s : List[int], res : List[List[int]]) -> None:
Requires(Acc(list_pred(s)))
Requires(Acc(list_pred(res)))
Requires(Forall(res, lambda x: Acc(list_pred(x), 1/2)))
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(res)))
Ensures(Forall(res, lambda x: Acc(list_pred(x), 1/2)))
Ensures(Forall(int, lambda d_10_j_:
Implies(d_10_j_ >= 0 and d_10_j_ < len(res), (len(res[d_10_j_])) > (0))))
# 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]
# res : List[List[int]] = [] # type : List[List[int]]
res = []
d_7_current__string_ : List[int] = [] # 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(Acc(list_pred(res)))
Invariant(Acc(list_pred(d_7_current__string_)))
Invariant(Acc(list_pred(s)))
Invariant(((d_8_i_) >= (0)) and ((d_8_i_) <= (len(s))))
Invariant(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))), [[]])))
Invariant(Forall(int, lambda d_4_i_:
(not (((d_4_i_) >= (0)) and ((d_4_i_) < (len(d_7_current__string_)))) or
(((((d_7_current__string_)[d_4_i_]) == (1)) or (((d_7_current__string_)[d_4_i_]) == (2)))), [[]])))
Invariant(Forall(res, lambda x: Acc(list_pred(x), 1/2)))
Invariant(Forall(int, lambda d_10_j_:
(Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((len(res[d_10_j_])) > (0))), [[]])))
# Invariant(Forall(int, lambda d_10_j_:
# not (d_10_j_ >= 0 and d_10_j_ < len(res)) or ((Forall(int, lambda d_11_j_:
# not (((d_11_j_) >= (0)) and ((d_11_j_) < (len(res[d_10_j_])))) or ((((res[d_10_j_])[d_11_j_]) == (1)) or (((res[d_10_j_])[d_11_j_]) == (2))))) and ((len(res[d_10_j_])) > (0)))))
# (not (d_10_j_ >= 0 and d_10_j_ < len(res)) or
# ((Forall(int, lambda d_11_j_:
# not (((d_11_j_) >= (0)) and ((d_11_j_) < (len(res[d_10_j_])))) or
# ((((res[d_10_j_])[d_11_j_]) == (1)) or (((res[d_10_j_])[d_11_j_]) == (2))))))))) # , [[res[d_10_j_]]]
# Invariant(Forall(res, lambda x:
# ((Forall(int, lambda d_11_j_:
# (not (((d_11_j_) >= (0)) and ((d_11_j_) < (len(x)))) or
# ((((x)[d_11_j_]) == (1)) or (((x)[d_11_j_]) == (2))), [[]]))), [[]]))) # , [[res[d_10_j_]]]
# Invariant(Forall(int, lambda d_10_j_:
# (not (d_10_j_ >= 0 and d_10_j_ < len(res)) or
# ((Forall(int, lambda d_11_j_:
# not (((d_11_j_) >= (0)) and ((d_11_j_) < (len(res[d_10_j_])))) or
# ((((res[d_10_j_])[d_11_j_]) == (1)) or (((res[d_10_j_])[d_11_j_]) == (2)))))), [[res[d_10_j_]]]))) # , [[res[d_10_j_]]]
if ((s)[d_8_i_]) == (3):
if (d_7_current__string_) != []:
res = (res) + [d_7_current__string_]
Assert(Forall(int, lambda d_10_j_:
(Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((len(res[d_10_j_])) > (0))), [[]])))
if len(d_7_current__string_) > 0:
d_7_copy = list(d_7_current__string_)
Assert(len(d_7_current__string_) > 0)
Assert(len(d_7_copy) > 0)
Assert(Forall(int, lambda d_4_i_:
(not (((d_4_i_) >= (0)) and ((d_4_i_) < (len(d_7_current__string_)))) or (((((d_7_current__string_)[d_4_i_]) == (1)) or (((d_7_current__string_)[d_4_i_]) == (2)))), [[]])))
Assert(Forall(int, lambda d_9_j_:
(not (d_9_j_ >= 0 and d_9_j_ < len(d_7_copy)) or
((d_7_copy[d_9_j_]) == (1) or (d_7_copy[d_9_j_]) == (2)), [[]])))
Assert(Forall(int, lambda d_10_j_:
(Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((len(res[d_10_j_])) > (0))), [[]])))
res = (res) + [d_7_copy]
Assert(Forall(int, lambda d_10_j_:
(Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((len(res[d_10_j_])) > (0))), [[]])))
d_7_current__string_ = []
else:
d_7_current__string_ = (d_7_current__string_) + [(s)[d_8_i_]]
d_8_i_ = (d_8_i_) + (1)
if (d_7_current__string_) != []:
Assert(Forall(int, lambda d_10_j_:
(Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((len(res[d_10_j_])) > (0))), [[]])))
if len(d_7_current__string_) > 0:
# Assert(Forall(int, lambda d_10_j_:
# (Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((len(res[d_10_j_])) > (0))))))
res = (res) + [d_7_current__string_]
# Assert(Forall(int, lambda d_10_j_:
# (Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((len(res[d_10_j_])) > (0))))))
d_7_current__string_ = []
return res
Assert(Forall(int, lambda d_10_j_:
(Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((len(res[d_10_j_])) > (0))), [[]])))

# def parse__nested__parens(paren__string : List[int]) -> List[int]:
# Requires(Forall(int, lambda d_12_i_:
Expand Down
Loading

0 comments on commit 67d0d84

Please sign in to comment.