Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

112 #13

Merged
merged 2 commits into from
Sep 2, 2024
Merged

112 #13

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions Bench/001-separate-paren-groups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from typing import cast, List, Dict, Set, Optional, Union
from nagini_contracts.contracts import *

@Pure
def IsValidParentheses(s : List[int], i : int, depth : int) -> bool :
Requires(Acc(list_pred(s), 1/2))
Requires(i >= 0 and i <= len(s))
if (i) == (len(s)):
return (depth) >= (0)
elif (depth) < (0):
return False
elif ((s)[i]) == (0):
return IsValidParentheses(s, (i) + (1), (depth) + (1))
elif ((s)[i]) == (1):
return ((depth) > (0)) and (IsValidParentheses(s, (i) + (1), (depth) - (1)))
elif True:
return IsValidParentheses(s, (i) + (1), depth)

@Pure
def IsValidParentheses2(s : List[int], i : int, depth : int) -> bool :
Requires(Acc(list_pred(s), 1/2))
Requires(i >= 0 and i <= len(s))
if (i) == (len(s)):
return (depth) >= (0)
elif (depth) < (0):
return False
elif ((s)[i]) == (0):
return IsValidParentheses(s, (i) + (1), (depth) + (1))
elif ((s)[i]) == (1):
return ((depth) > (0)) and (IsValidParentheses(s, (i) + (1), (depth) - (1)))
elif True:
return IsValidParentheses(s, (i) + (1), depth)

@Pure
def IsValidParentheses1(s : List[int], i : int, depth : int) -> bool :
Requires(Acc(list_pred(s), 1/2))
Requires(i >= 0 and i <= len(s))
if (i) == (len(s)):
return (depth) == (0)
elif ((depth) <= (0)) and ((i) != (0)):
return False
elif ((s)[i]) == (0):
return IsValidParentheses1(s, (i) + (1), (depth) + (1))
elif ((s)[i]) == (1):
return ((depth) > (0)) and (IsValidParentheses1(s, (i) + (1), (depth) - (1)))
elif True:
return IsValidParentheses1(s, (i) + (1), depth)

def separate__paren__groups(paren__string : List[int]) -> List[List[int]]:
Requires(Acc(list_pred(paren__string)))
Requires(Forall(int, lambda d_0_i_:
not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(paren__string)))) or ((((paren__string)[d_0_i_]) == (0)) or (((paren__string)[d_0_i_]) == (1)))))
Requires(Forall(int, lambda d_1_i_:
not (((0) <= (d_1_i_)) and ((d_1_i_) <= (len(paren__string)))) or (IsValidParentheses(paren__string, d_1_i_, 0))))
Requires(IsValidParentheses2(paren__string, 0, 0))
Ensures(Acc(list_pred(ResultT(List[List[int]]))))
Ensures(Forall(ResultT(List[List[int]]), lambda x: Acc(list_pred(x), 1/2)))
Ensures((Forall(int, lambda d_2_i_:
not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(Result())))) or (IsValidParentheses1((Result())[d_2_i_], 0, 0)))))
res : List[List[int]] = []
d_3_current__string_ : List[int] = []
d_4_current__depth_ = int(0) # type : int
d_5_i_ = int(0) # type : int
while (d_5_i_) < (len(paren__string)):
Invariant(Acc(list_pred(res)))
Invariant(Acc(list_pred(d_3_current__string_)))
Invariant(Acc(list_pred(paren__string)))
Invariant(Forall(res, lambda d_4_i_: Acc(list_pred(d_4_i_), 1/2)))
Invariant(((0) <= (d_5_i_)) and ((d_5_i_) <= (len(paren__string))))
Invariant(Forall(int, lambda d_0_i_:
not (((d_0_i_) >= (0)) and ((d_0_i_) < (len(paren__string)))) or ((((paren__string)[d_0_i_]) == (0)) or (((paren__string)[d_0_i_]) == (1)))))
Invariant(Forall(int, lambda d_1_i_:
not (((0) <= (d_1_i_)) and ((d_1_i_) <= (len(paren__string)))) or (IsValidParentheses(paren__string, d_1_i_, 0))))
Invariant((Forall(int, lambda d_6_i1_:
not (((0) <= (d_6_i1_)) and ((d_6_i1_) < (len(res)))) or (IsValidParentheses1((res)[d_6_i1_], 0, 0)))))
Invariant(IsValidParentheses(paren__string, d_5_i_, 0))
d_7_c_ = (paren__string)[d_5_i_]
if (d_7_c_) == (0):
d_4_current__depth_ = (d_4_current__depth_) + (1)
d_3_current__string_ = (d_3_current__string_) + [d_7_c_]
elif (d_7_c_) == (1):
d_4_current__depth_ = (d_4_current__depth_) - (1)
d_3_current__string_ = (d_3_current__string_) + [d_7_c_]
if (d_4_current__depth_) == (0):
res = (res) + [d_3_current__string_]
d_3_current__string_ = []
d_5_i_ = (d_5_i_) + (1)
return res

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

def parseparengroup(s : List[int]) -> int:
Requires(Acc(list_pred(s), 1/2))
Requires(contains12(s))
Requires(get_len(s))
Ensures(Acc(list_pred(s), 1/2))
Ensures(contains12(s))
Ensures(get_len(s))
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)):
Invariant(Acc(list_pred(s), 1/2))
Invariant(((d_2_i_) >= (0)) and ((d_2_i_) <= (len(s))))
Invariant(max__depth >= 0)
Invariant(contains12(s))
Invariant(get_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

@Pure
def get_len(s : List[int]) -> bool:
Requires(Acc(list_pred(s), 1/2))
return len(s) > 0

@Pure
def contains12(s : List[int]) -> bool:
Requires(Acc(list_pred(s), 1/2))
return Forall(int, lambda d_0_i_:
Implies(d_0_i_ >= 0 and d_0_i_ < len(s), s[d_0_i_] == 1 or s[d_0_i_] == 2))

def split(s : List[int]) -> List[List[int]]:
Requires(Acc(list_pred(s)))
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(ResultT(List[List[int]]))))
Ensures(Forall(ResultT(List[List[int]]), 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(ResultT(List[List[int]])), (get_len(ResultT(List[List[int]])[d_10_j_])))))
Ensures(Forall(int, lambda d_10_j_:
(Implies(d_10_j_ >= 0 and d_10_j_ < len(Result()),
contains12(Result()[d_10_j_])), [[contains12(Result()[d_10_j_])]])))
res : List[List[int]] = [] # type : List[List[int]]
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(res, lambda x: Acc(list_pred(x), 1/2)))
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(int, lambda d_10_j_:
(Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((get_len(res[d_10_j_])))), [[]])))
Invariant(Forall(int, lambda d_10_j_:
(Implies(d_10_j_ >= 0 and d_10_j_ < len(res),
contains12(res[d_10_j_])), [[contains12(res[d_10_j_])]])))
if ((s)[d_8_i_]) == (3):
if len(d_7_current__string_) > 0:
d_7_copy = list(d_7_current__string_)
res = (res) + [d_7_copy]
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 len(d_7_current__string_) > 0:
d_7_copy = list(d_7_current__string_)
Assert(get_len(d_7_copy))
res = (res) + [d_7_copy]
# Assert(Forall(int, lambda d_10_j_:
# (Implies(d_10_j_ >= 0 and d_10_j_ < len(res), ((get_len(res[d_10_j_])))), [[]])))
d_7_current__string_ = []
return res

def parse__nested__parens(paren__string : List[int]) -> List[int]:
Requires(Acc(list_pred(paren__string)))
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_]) == (3)) or (((paren__string)[d_12_i_]) == (1))) or (((paren__string)[d_12_i_]) == (2)))))
Ensures(Acc(list_pred(Result())))
Ensures(Forall(ResultT(List[int]), lambda d_13_x_: ((d_13_x_) >= (0))))
res : List[int] = []
d_14_strings_ : List[List[int]] = split(paren__string)
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(d_14_strings_, lambda x: Acc(list_pred(x), 1/2)))
Invariant(0 <= d_15_i_ and d_15_i_ <= len(d_14_strings_))
Invariant(Forall(res, lambda d_16_x_: ((d_16_x_) >= (0))))
Invariant(Forall(int, lambda d_10_j_:
Implies(d_10_j_ >= 0 and d_10_j_ < len(d_14_strings_), (get_len(d_14_strings_[d_10_j_])))))
Invariant(Forall(int, lambda d_10_j_:
(Implies(d_10_j_ >= 0 and d_10_j_ < len(d_14_strings_),
contains12(d_14_strings_[d_10_j_])), [[contains12(d_14_strings_[d_10_j_])]])))
d_17_cur_ = int(0) # type : int
d_17_cur_ = parseparengroup((d_14_strings_)[d_15_i_])
res = (res) + [d_17_cur_]
d_15_i_ = (d_15_i_) + (1)
return res
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ def InArray(a : List[List[int]], x : List[int]) -> bool :
(Implies(((0) <= (d_0_s_)) and ((d_0_s_) < (len((a)))),
EqArrays(a[d_0_s_], x))))

# @Pure
# def AccBiDim(a : List[List[int]]) -> bool :
# Requires(Acc(list_pred(a)))
# return Forall(a, lambda d_0_s_: Acc(list_pred(d_0_s_)))

def filter__by__substring(strings : List[List[int]], substring : List[int]) -> List[List[int]]:
Requires(Acc(list_pred(strings)))
Expand All @@ -62,13 +58,10 @@ def filter__by__substring(strings : List[List[int]], substring : List[int]) -> L
Ensures(Acc(list_pred(strings)))
Ensures(Forall(strings, lambda d_0_s_: Acc(list_pred(d_0_s_))))
Ensures(Acc(list_pred(Result())))
# Ensures(AccBiDim(Result()))
Ensures(Forall(ResultT(List[List[int]]), lambda d_0_s_: Acc(list_pred(d_0_s_))))
Ensures((len(Result())) <= (len(strings)))
# Ensures(Forall(List[int], lambda d_3_s_: Implies(Exists(int, lambda x : Implies(x >= 0 and x < len(Result()), Result()[x] == d_3_s_)), Acc(list_pred(d_3_s_)))))
# Ensures(Forall(int, lambda d_3_i_:
# (Implies(0 <= d_3_i_ and d_3_i_ < len(Result()), Acc(list_pred(Result()[d_3_i_])) and InArray(strings, Result()[d_3_i_])))))
# Ensures(Forall(List[int], lambda d_1_s_:
# not ((d_1_s_) in (Result())) or ((d_1_s_) in (strings))))
Ensures(Forall(int, lambda d_3_i_:
(Implies(0 <= d_3_i_ and d_3_i_ < len(Result()), InArray(strings, Result()[d_3_i_])))))
res : List[List[int]] = []
d_2_i_ = int(0) # type : int
d_2_i_ = 0
Expand All @@ -82,8 +75,6 @@ def filter__by__substring(strings : List[List[int]], substring : List[int]) -> L
Invariant((len(res)) <= (d_2_i_))
Invariant(Forall(int, lambda d_3_i_:
(Implies(0 <= d_3_i_ and d_3_i_ < len(res), InArray(strings, res[d_3_i_])), [[InArray(strings, res[d_3_i_])]])))
# Invariant(Forall(List[int], lambda d_3_s_:
# not ((d_3_s_) in (res)) or ((d_3_s_) in (strings))))
d_4_check_ = False # type : bool
d_4_check_ = checkSubstring((strings)[d_2_i_], substring)
if d_4_check_:
Expand Down
2 changes: 1 addition & 1 deletion Bench/023-strlen.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from nagini_contracts.contracts import *

def strlen(s : str) -> int:
Ensures((Result()) == (len(s)))
Ensures((ResultT(int)) == (len(s)))
return len(s)
File renamed without changes.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ examples from HumanEval translated to Nagini
Current status:

- [x] 0. has_close_elements
- [ ] 1. separate_paren_groups
- [x] 1. separate_paren_groups
- [x] 2. truncate
- [x] 3. below_zero
- [ ] 4. mean_absolute_derivation
- [x] 5. intersperse
- [ ] 6. parse_nested_parens
- [ ] 7. filter_by_substring
- [x] 6. parse_nested_parens
- [x] 7. filter_by_substring
- [x] 8. sum_product
- [x] 9. rolling_max
- [x] 10. is_palindrome
Expand Down
103 changes: 0 additions & 103 deletions WIP/006-parse_nested_parens.py

This file was deleted.

6 changes: 3 additions & 3 deletions WIP/087-get_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def get_row(lst : List[List[int]], x : int) -> List[Tuple[int, int]]:
# Forall(int, lambda j1:
# Implies(j1 >= 0 and j1 < len(lst[i]) and lst[i1][j1] == x,
# InArray(pos, i1, j1))))))
# Invariant(Forall(int, lambda j1:
# (Implies(j1 >= 0 and j1 < j and lst[i][j1] == x,
# InArray(pos, i, j1)), [[InArray(pos, i, j1)]])))
Invariant(Forall(int, lambda j1:
(Implies(j1 >= 0 and j1 < j and lst[i][j1] == x,
InArray(pos, i, j1)), [[InArray(pos, i, j1)]])))
if lst[i][j] == x:
pos = pos + [(i, j)]
j += 1
Expand Down
Loading
Loading