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

38, 56, 75, 90 #14

Merged
merged 3 commits into from
Sep 9, 2024
Merged
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
25 changes: 22 additions & 3 deletions Bench/000-has-close-elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,65 @@

@Pure
def abs_value(val: int) -> int:
# post-conditions-start
Ensures(Implies(val < 0, Result() == -val))
Ensures(Implies(val >= 0, Result() == val))
# post-conditions-end

# impl-start
if val < 0:
return -val
else:
return val
# impl-end


@Pure
def abs1(x: int, threshold: int) -> bool:
# impl-start
return x >= threshold or x <= -threshold
# impl-end

@Pure
def fn(x: int, numbers: List[int], threshold: int) -> bool:
# pre-conditions-start
Requires(threshold > 0)
Requires(Acc(list_pred(numbers)))
Requires(x >= 0 and x < len(numbers))
# pre-conditions-end

# impl-start
return Forall(range(len(numbers)), lambda y :
x == y or
abs1(numbers[x] - numbers[y], threshold)
)
# impl-end


def has_close_elements(numbers: List[int], threshold: int) -> bool:
# pre-conditions-start
Requires(threshold > 0)
Requires(Acc(list_pred(numbers)))
# pre-conditions-end
# post-conditions-start
Ensures(Implies(Result() != True, Forall(range(len(numbers)), lambda x :
fn(x, numbers, threshold)
)))
# post-conditions-end


# impl-start
flag = False
i = 0
while i < len(numbers):
# invariants-start
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))))

# invariants-end
j = 0
while j < len(numbers):
# invariants-start
Invariant(Acc(list_pred(numbers)))
Invariant(0 <= i and i < len(numbers))
Invariant(0 <= j and j <= len(numbers))
Expand All @@ -57,6 +74,7 @@ def has_close_elements(numbers: List[int], threshold: int) -> bool:
Forall(range(j), lambda y : i == y or abs1(numbers[i] - numbers[y], threshold))
)
)
# invariants-end

if i != j:
distance = abs_value(numbers[i] - numbers[j])
Expand All @@ -66,4 +84,5 @@ def has_close_elements(numbers: List[int], threshold: int) -> bool:
j += 1
i += 1

return flag
return flag
# impl-end
24 changes: 24 additions & 0 deletions Bench/001-separate-paren-groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

@Pure
def IsValidParentheses(s : List[int], i : int, depth : int) -> bool :
# pre-conditions-start
Requires(Acc(list_pred(s), 1/2))
Requires(i >= 0 and i <= len(s))
# pre-conditions-end

# impl-start
if (i) == (len(s)):
return (depth) >= (0)
elif (depth) < (0):
Expand All @@ -15,11 +19,16 @@ def IsValidParentheses(s : List[int], i : int, depth : int) -> bool :
return ((depth) > (0)) and (IsValidParentheses(s, (i) + (1), (depth) - (1)))
elif True:
return IsValidParentheses(s, (i) + (1), depth)
# impl-end

@Pure
def IsValidParentheses2(s : List[int], i : int, depth : int) -> bool :
# pre-conditions-start
Requires(Acc(list_pred(s), 1/2))
Requires(i >= 0 and i <= len(s))
# pre-conditions-end

# impl-start
if (i) == (len(s)):
return (depth) >= (0)
elif (depth) < (0):
Expand All @@ -30,11 +39,16 @@ def IsValidParentheses2(s : List[int], i : int, depth : int) -> bool :
return ((depth) > (0)) and (IsValidParentheses(s, (i) + (1), (depth) - (1)))
elif True:
return IsValidParentheses(s, (i) + (1), depth)
# impl-end

@Pure
def IsValidParentheses1(s : List[int], i : int, depth : int) -> bool :
# pre-conditions-start
Requires(Acc(list_pred(s), 1/2))
Requires(i >= 0 and i <= len(s))
# pre-conditions-end

# impl-start
if (i) == (len(s)):
return (depth) == (0)
elif ((depth) <= (0)) and ((i) != (0)):
Expand All @@ -45,23 +59,31 @@ def IsValidParentheses1(s : List[int], i : int, depth : int) -> bool :
return ((depth) > (0)) and (IsValidParentheses1(s, (i) + (1), (depth) - (1)))
elif True:
return IsValidParentheses1(s, (i) + (1), depth)
# impl-end

def separate__paren__groups(paren__string : List[int]) -> List[List[int]]:
# pre-conditions-start
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))
# pre-conditions-end
# post-conditions-start
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)))))
# post-conditions-end

# impl-start
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)):
# invariants-start
Invariant(Acc(list_pred(res)))
Invariant(Acc(list_pred(d_3_current__string_)))
Invariant(Acc(list_pred(paren__string)))
Expand All @@ -74,6 +96,7 @@ def separate__paren__groups(paren__string : List[int]) -> List[List[int]]:
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))
# invariants-end
d_7_c_ = (paren__string)[d_5_i_]
if (d_7_c_) == (0):
d_4_current__depth_ = (d_4_current__depth_) + (1)
Expand All @@ -86,4 +109,5 @@ def separate__paren__groups(paren__string : List[int]) -> List[List[int]]:
d_3_current__string_ = []
d_5_i_ = (d_5_i_) + (1)
return res
# impl-end

20 changes: 20 additions & 0 deletions Bench/003-below-zero.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,57 @@

@Pure
def psum(i : int, j : int, s : List[int]) -> int :
# pre-conditions-start
Requires(Acc(list_pred(s)))
Requires(0 <= i and i <= j and j <= len(s))
# pre-conditions-end

# impl-start
if i == j:
return 0
else:
return (s)[j - 1] + (psum(i, j - 1, s))
# impl-end

def below__zero(ops : List[int]) -> bool:
# pre-conditions-start
Requires(Acc(list_pred(ops)))
# pre-conditions-end
# post-conditions-start
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)))))
# post-conditions-end

# impl-start
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)):
# invariants-start
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)]])))
# invariants-end

# assert-start
Assert((psum(0, (d_4_i_) + (1), ops)) == ((psum(0, d_4_i_, ops)) + ((ops)[d_4_i_])))
# assert-end

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
# impl-end
12 changes: 10 additions & 2 deletions Bench/005-intersperse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,37 @@
from nagini_contracts.contracts import *

def intersperse(numbers: List[int], delimiter: int) -> List[int]:
# pre-conditions-start
Requires(Acc(list_pred(numbers)))
# pre-conditions-end
# post-conditions-start
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))

# post-conditions-end

# impl-start
res = [] # type: List[int]
if len(numbers) != 0:
i = 0
while i + 1 < len(numbers):
# invariants-start
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))
# invariants-end

res.append(numbers[i])
res.append(delimiter)
i += 1

res.append(numbers[i])

return res
return res
# impl-end
Loading
Loading