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

pure helpers + text descriptions #24

Merged
merged 1 commit into from
Nov 29, 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
14 changes: 6 additions & 8 deletions Bench/000-has-close-elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
from nagini_contracts.contracts import *


@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
Expand All @@ -19,9 +17,9 @@ def abs_value(val: int) -> int:

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

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

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


def has_close_elements(numbers: List[int], threshold: int) -> bool:
Expand Down Expand Up @@ -85,4 +83,4 @@ def has_close_elements(numbers: List[int], threshold: int) -> bool:
i += 1

return flag
# impl-end
# impl-end
12 changes: 6 additions & 6 deletions Bench/001-separate-paren-groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def IsValidParentheses(s : List[int], i : int, depth : int) -> bool :
Requires(i >= 0 and i <= len(s))
# pre-conditions-end

# impl-start
# pure-start
if (i) == (len(s)):
return (depth) >= (0)
elif (depth) < (0):
Expand All @@ -19,7 +19,7 @@ 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-end

@Pure
def IsValidParentheses2(s : List[int], i : int, depth : int) -> bool :
Expand All @@ -28,7 +28,7 @@ def IsValidParentheses2(s : List[int], i : int, depth : int) -> bool :
Requires(i >= 0 and i <= len(s))
# pre-conditions-end

# impl-start
# pure-start
if (i) == (len(s)):
return (depth) >= (0)
elif (depth) < (0):
Expand All @@ -39,7 +39,7 @@ 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-end

@Pure
def IsValidParentheses1(s : List[int], i : int, depth : int) -> bool :
Expand All @@ -48,7 +48,7 @@ def IsValidParentheses1(s : List[int], i : int, depth : int) -> bool :
Requires(i >= 0 and i <= len(s))
# pre-conditions-end

# impl-start
# pure-start
if (i) == (len(s)):
return (depth) == (0)
elif ((depth) <= (0)) and ((i) != (0)):
Expand All @@ -59,7 +59,7 @@ 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
# pure-end

def separate__paren__groups(paren__string : List[int]) -> List[List[int]]:
# pre-conditions-start
Expand Down
6 changes: 3 additions & 3 deletions Bench/003-below-zero.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ def psum(i : int, j : int, s : List[int]) -> int :
Requires(0 <= i and i <= j and j <= len(s))
# pre-conditions-end

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

def below__zero(ops : List[int]) -> bool:
# pre-conditions-start
Expand Down Expand Up @@ -51,4 +51,4 @@ def below__zero(ops : List[int]) -> bool:
d_4_i_ = (d_4_i_) + (1)

return True
# impl-end
# impl-end
2 changes: 1 addition & 1 deletion Bench/005-intersperse.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ def intersperse(numbers: List[int], delimiter: int) -> List[int]:
res.append(numbers[i])

return res
# impl-end
# impl-end
10 changes: 5 additions & 5 deletions Bench/006-parse_nested_parens.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ def get_len(s : List[int]) -> bool:
Requires(Acc(list_pred(s), 1/2))
# pre-conditions-end

# impl-start
# pure-start
return len(s) > 0
# impl-end
# pure-end

@Pure
def contains12(s : List[int]) -> bool:
# pre-conditions-start
Requires(Acc(list_pred(s), 1/2))
# pre-conditions-end

# impl-start
# pure-start
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))
# impl-end
# pure-end

def split(s : List[int]) -> List[List[int]]:
# pre-conditions-start
Expand Down Expand Up @@ -147,4 +147,4 @@ def parse__nested__parens(paren__string : List[int]) -> List[int]:
res = (res) + [d_17_cur_]
d_15_i_ = (d_15_i_) + (1)
return res
# impl-end
# impl-end
8 changes: 4 additions & 4 deletions Bench/007-filter_by_substring.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def EqArrays(a : List[int], x : List[int]) -> bool :
Requires(Acc(list_pred(x)))
# pre-conditions-end

# impl-start
# pure-start
return len(a) == len(x) and Forall(int, lambda d_0_i_: Implies(0 <= d_0_i_ and d_0_i_ < len(a), (a)[d_0_i_] == x[d_0_i_]))
# impl-end
# pure-end

@Pure
def InArray(a : List[List[int]], x : List[int]) -> bool :
Expand All @@ -63,11 +63,11 @@ def InArray(a : List[List[int]], x : List[int]) -> bool :
Requires(Forall(a, lambda d_0_s_: Acc(list_pred(d_0_s_))))
# pre-conditions-end

# impl-start
# pure-start
return Exists(int, lambda d_0_s_:
(Implies(((0) <= (d_0_s_)) and ((d_0_s_) < (len((a)))),
EqArrays(a[d_0_s_], x))))
# impl-end
# pure-end


def filter__by__substring(strings : List[List[int]], substring : List[int]) -> List[List[int]]:
Expand Down
10 changes: 5 additions & 5 deletions Bench/008-sum-product.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ def psum(i : int, j : int, s : List[int]) -> int :
Requires(0 <= i and i <= j and j <= len(s))
# pre-conditions-end

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

@Pure
def prod(i : int, j : int, s : List[int]) -> int :
Expand All @@ -22,12 +22,12 @@ def prod(i : int, j : int, s : List[int]) -> int :
Requires(0 <= i and i <= j and j <= len(s))
# pre-conditions-end

# impl-start
# pure-start
if i == j:
return 1
else:
return (s)[j - 1] * (prod(i, j - 1, s))
# impl-end
# pure-end

def sum__product(numbers : List[int]) -> Tuple[int, int]:
# pre-conditions-start
Expand Down Expand Up @@ -63,4 +63,4 @@ def sum__product(numbers : List[int]) -> Tuple[int, int]:
p = (p) * ((numbers)[d_2_i_])
d_2_i_ = (d_2_i_) + (1)
return s, p
# impl-end
# impl-end
6 changes: 3 additions & 3 deletions Bench/009-rolling-max.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ def getVal(mx: Optional[int]) -> int:
# pre-conditions-start
Requires(mx is not None)
# pre-conditions-end
# impl-start
# pure-start
return mx
# impl-end
# pure-end

def rolling_max(numbers: List[int]) -> List[int]:
# pre-conditions-start
Expand Down Expand Up @@ -48,4 +48,4 @@ def rolling_max(numbers: List[int]) -> List[int]:
i += 1

return result
# impl-end
# impl-end
8 changes: 4 additions & 4 deletions Bench/010-is_palindrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def is__palindrome__fun(start : int, s : List[int]) -> bool :
Requires(0 <= start and start < len(s))
# pre-conditions-end

# impl-start
# pure-start
return Forall(int, lambda d_4_k_:
not (((start) <= (d_4_k_)) and ((d_4_k_) < (len(s)))) or (((s)[d_4_k_]) == ((s)[((len(s)) - (1)) - (d_4_k_ - start)])))
# impl-end
# pure-end

@Pure
def starts__with(result : List[int], s : List[int]) -> bool :
Expand All @@ -56,10 +56,10 @@ def starts__with(result : List[int], s : List[int]) -> bool :
Requires(Acc(list_pred(result), 1/2))
# pre-conditions-end

# impl-start
# pure-start
return ((len(result)) >= (len(s))) and (Forall(int, lambda d_5_k_:
not (((0) <= (d_5_k_)) and ((d_5_k_) < (len(s)))) or (((result)[d_5_k_]) == ((s)[d_5_k_]))))
# impl-end
# pure-end

def make__palindrome(s : List[int]) -> List[int]:
# pre-conditions-start
Expand Down
6 changes: 3 additions & 3 deletions Bench/011-string_xor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ def xor(a : int, b : int) -> int:
Ensures((Result()) == ((0 if (a) == (b) else 1)))
# pre-conditions-end

# impl-start
# pure-start
result : int = int(0)
if (a) == (b):
result = 0
else:
result = 1
return result
# impl-end
# pure-end

def string__xor(a : List[int], b : List[int]) -> List[int]:
# pre-conditions-start
Expand Down Expand Up @@ -60,4 +60,4 @@ def string__xor(a : List[int], b : List[int]) -> List[int]:
result = (result) + [d_6_bitResult_]
d_4_i_ = (d_4_i_) + (1)
return result
# impl-end
# impl-end
6 changes: 3 additions & 3 deletions Bench/012-longest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def getVal(mx: Optional[int]) -> int:
Requires(mx is not None)
# pre-conditions-end

# impl-start
# pure-start
return mx
# impl-end
# pure-end

def longest(strings : List[List[int]]) -> Optional[int]:
# pre-conditions-start
Expand Down Expand Up @@ -60,4 +60,4 @@ def longest(strings : List[List[int]]) -> Optional[int]:
# assert-end
d_5_i_ = (d_5_i_) + (1)
return result
# impl-end
# impl-end
2 changes: 1 addition & 1 deletion Bench/013-greatest-common-divisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ def greatest_common_divisor(a: int, b: int) -> int:
x = temp

return x
# impl-end
# impl-end
10 changes: 5 additions & 5 deletions Bench/016-count_distinct_characters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ def contains_char(s : List[int], c : int, i : int, j : int) -> bool:
Requires(((97) <= (c)) and ((c) <= (122)))
# pre-conditions-end

# impl-start
# pure-start
if i == j:
return False
else:
return s[j - 1] == c or contains_char(s, c, i, j - 1)
# impl-end
# pure-end

@Pure
def count_chars_inter(s : List[int], c : int) -> int:
Expand All @@ -27,12 +27,12 @@ def count_chars_inter(s : List[int], c : int) -> int:
Requires(((97) <= (c)) and ((c) <= (123)))
# pre-conditions-end

# impl-start
# pure-start
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)
# impl-end
# pure-end

def count_distinct_characters(s : List[int]) -> int:
# pre-conditions-start
Expand Down Expand Up @@ -62,4 +62,4 @@ def count_distinct_characters(s : List[int]) -> int:
c = c + 1
d_2_i_ = d_2_i_ + 1
return c
# impl-end
# impl-end
8 changes: 4 additions & 4 deletions Bench/020-find-closest-elements.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from typing import cast, List, Dict, Set, Optional, Union, Tuple
from typing import List, Tuple
from nagini_contracts.contracts import *

#use-as-unpure
@Pure
def dist(a : int, b : int) -> int :
# pre-conditions-start
Ensures(Result() >= 0)
# pre-conditions-end

# impl-start
# pure-start
if (a) < (b):
return (b) - (a)
else:
return (a) - (b)
# impl-end
# pure-end

def find__closest__elements(s : List[int]) -> Tuple[int, int]:
# pre-conditions-start
Expand Down Expand Up @@ -103,4 +104,3 @@ def find__closest__elements(s : List[int]) -> Tuple[int, int]:
d_5_i_ = (d_5_i_) + (1)
return (l, h)
# impl-end

2 changes: 1 addition & 1 deletion Bench/023-strlen.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ def strlen(s : str) -> int:

# impl-start
return len(s)
# impl-end
# impl-end
Loading
Loading