-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
77 changed files
with
3,483 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from typing import cast, List, Dict, Set, Optional, Union | ||
from nagini_contracts.contracts import * | ||
|
||
|
||
@Pure | ||
def abs_value(val: int) -> int: | ||
Ensures(Implies(val < 0, Result() == -val)) | ||
Ensures(Implies(val >= 0, Result() == val)) | ||
|
||
if val < 0: | ||
return -val | ||
else: | ||
return val | ||
|
||
|
||
@Pure | ||
def abs1(x: int, threshold: int) -> bool: | ||
return x >= threshold or x <= -threshold | ||
|
||
@Pure | ||
def fn(x: int, numbers: List[int], threshold: int) -> bool: | ||
Requires(threshold > 0) | ||
Requires(Acc(list_pred(numbers))) | ||
Requires(x >= 0 and x < len(numbers)) | ||
return Forall(range(len(numbers)), lambda y : | ||
x == y or | ||
abs1(numbers[x] - numbers[y], threshold) | ||
) | ||
|
||
|
||
def has_close_elements(numbers: List[int], threshold: int) -> bool: | ||
Requires(threshold > 0) | ||
Requires(Acc(list_pred(numbers))) | ||
Ensures(Implies(Result() != True, Forall(range(len(numbers)), lambda x : | ||
fn(x, numbers, threshold) | ||
))) | ||
|
||
|
||
flag = False | ||
i = 0 | ||
while i < len(numbers): | ||
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)))) | ||
|
||
j = 0 | ||
while j < len(numbers): | ||
Invariant(Acc(list_pred(numbers))) | ||
Invariant(0 <= i and i < len(numbers)) | ||
Invariant(0 <= j and j <= len(numbers)) | ||
|
||
Invariant( | ||
Implies( | ||
flag != True, | ||
Forall(range(i), lambda x : fn(x, numbers, threshold)) and | ||
Forall(range(j), lambda y : i == y or abs1(numbers[i] - numbers[y], threshold)) | ||
) | ||
) | ||
|
||
|
||
if i != j: | ||
distance = abs_value(numbers[i] - numbers[j]) | ||
if distance < threshold: | ||
flag = True | ||
|
||
j += 1 | ||
i += 1 | ||
|
||
return flag |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import cast, List, Dict, Set, Optional, Union | ||
from nagini_contracts.contracts import * | ||
|
||
@Pure | ||
def psum(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 (s)[j - 1] + (psum(i, j - 1, s)) | ||
|
||
def below__zero(ops : List[int]) -> bool: | ||
Requires(Acc(list_pred(ops))) | ||
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))))) | ||
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)): | ||
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)]]))) | ||
Assert((psum(0, (d_4_i_) + (1), ops)) == ((psum(0, d_4_i_, ops)) + ((ops)[d_4_i_]))) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 * | ||
|
||
def intersperse(numbers: List[int], delimiter: int) -> List[int]: | ||
Requires(Acc(list_pred(numbers))) | ||
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)) | ||
|
||
res = [] # type: List[int] | ||
if len(numbers) != 0: | ||
i = 0 | ||
while i + 1 < len(numbers): | ||
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)) | ||
|
||
res.append(numbers[i]) | ||
res.append(delimiter) | ||
i += 1 | ||
|
||
res.append(numbers[i]) | ||
|
||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from typing import cast, List, Dict, Set, Optional, Union, Tuple | ||
from nagini_contracts.contracts import * | ||
|
||
@Pure | ||
def psum(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 (s)[j - 1] + (psum(i, j - 1, s)) | ||
|
||
@Pure | ||
def prod(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 1 | ||
else: | ||
return (s)[j - 1] * (prod(i, j - 1, s)) | ||
|
||
def sum__product(numbers : List[int]) -> Tuple[int, int]: | ||
Requires(Acc(list_pred(numbers))) | ||
Ensures(Acc(list_pred(numbers))) | ||
Ensures((Result()[0]) == (psum(0, len(numbers), numbers))) | ||
Ensures((Result()[1]) == (prod(0, len(numbers), numbers))) | ||
s = int(0) # type : int | ||
p = int(0) # type : int | ||
s = 0 | ||
p = 1 | ||
d_2_i_ = int(0) # type : int | ||
d_2_i_ = 0 | ||
while (d_2_i_) < (len(numbers)): | ||
Invariant(Acc(list_pred(numbers))) | ||
Invariant(((0) <= (d_2_i_)) and ((d_2_i_) <= (len(numbers)))) | ||
Invariant((s) == (psum(0, d_2_i_, numbers))) | ||
Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(numbers)))) or ((psum(0, d_2_i_ + 1, numbers)) == (psum(0, d_2_i_, numbers) + numbers[d_2_i_])), [[psum(0, d_2_i_ + 1, numbers)]]))) | ||
Invariant(Forall(int, lambda d_2_i_: (not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(numbers)))) or ((prod(0, d_2_i_ + 1, numbers)) == (prod(0, d_2_i_, numbers) * numbers[d_2_i_])), [[prod(0, d_2_i_ + 1, numbers)]]))) | ||
Invariant((p) == (prod(0, d_2_i_, numbers))) | ||
Assert((psum(0, (d_2_i_) + (1), numbers)) == ((psum(0, d_2_i_, numbers) + (numbers)[d_2_i_]))) | ||
s = (s) + ((numbers)[d_2_i_]) | ||
Assert((prod(0, d_2_i_ + 1, numbers)) == ((prod(0, d_2_i_, numbers)) * ((numbers)[d_2_i_]))) | ||
p = (p) * ((numbers)[d_2_i_]) | ||
d_2_i_ = (d_2_i_) + (1) | ||
return s, p |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import cast, List, Dict, Set, Optional, Union | ||
from nagini_contracts.contracts import * | ||
|
||
@Pure | ||
def getVal(mx: Optional[int]) -> int: | ||
Requires(mx is not None) | ||
return mx | ||
|
||
def rolling_max(numbers: List[int]) -> List[int]: | ||
Requires(Acc(list_pred(numbers))) | ||
Ensures(Acc(list_pred(numbers))) | ||
Ensures(Acc(list_pred(Result()))) | ||
Ensures(len(Result()) == len(numbers)) | ||
Ensures(Forall(range(len(numbers)), lambda i: numbers[i] <= Result()[i])) | ||
Ensures(Forall(range(len(numbers) - 1), lambda i: Result()[i] <= Result()[i + 1])) | ||
|
||
running_max = None # type: Optional[int] | ||
result = [] # type: List[int] | ||
|
||
i = 0 | ||
while i < len(numbers): | ||
Invariant(Acc(list_pred(numbers))) | ||
Invariant(Acc(list_pred(result))) | ||
Invariant(0 <= i and i <= len(numbers)) | ||
Invariant(len(result) == i) | ||
Invariant(Forall(range(i), lambda i1: numbers[i1] <= result[i1])) | ||
Invariant(Old(running_max) is None or ((Old(running_max) is not None) and (getVal(Old(running_max)) <= getVal(running_max)))) | ||
Invariant(Implies(len(result) > 0, running_max is not None)) | ||
Invariant(Implies(len(result) > 0, result[-1] == getVal(running_max))) | ||
Invariant(Forall(range(i - 1), lambda i1: result[i1] <= result[i1 + 1])) | ||
|
||
n = numbers[i] | ||
if running_max is None or running_max < n: | ||
running_max = n | ||
|
||
result.append(running_max) | ||
i += 1 | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from typing import cast, List, Dict, Set, Optional, Union, Tuple | ||
from nagini_contracts.contracts import * | ||
|
||
def is__palindrome(start : int, s : List[int]) -> bool: | ||
Requires(Acc(list_pred(s), 1/2)) | ||
Requires((len(s)) > (0)) | ||
Requires(((0) <= (start)) and ((start) < (len(s)))) | ||
Ensures(Acc(list_pred(s), 1/2)) | ||
Ensures(((0) <= (start)) and ((start) < (len(s)))) | ||
Ensures((len(s)) > (0)) | ||
Ensures((Result()) == (Forall(int, lambda d_0_k_: | ||
not (((start) <= (d_0_k_)) and ((d_0_k_) < (len(s)))) or (((s)[d_0_k_]) == ((s)[((len(s)) - (1)) - (d_0_k_ - start)]))))) | ||
Ensures(Result() == is__palindrome__fun(start, s)) | ||
d_1_i_ = int(0) # type : int | ||
d_1_i_ = start | ||
d_2_j_ = int(0) # type : int | ||
d_2_j_ = (len(s)) - (1) | ||
while (d_1_i_) < (d_2_j_): | ||
Invariant(Acc(list_pred(s), 1/2)) | ||
Invariant(((0) <= (start)) and ((start) < (len(s)))) | ||
Invariant(d_1_i_ <= d_2_j_ + 1) | ||
Invariant(((start) <= (d_1_i_)) and ((d_1_i_) < (len(s)))) | ||
Invariant(((start) <= (d_2_j_)) and ((d_2_j_) < (len(s)))) | ||
Invariant((d_2_j_ - start) == (((len(s)) - (d_1_i_)) - (1))) | ||
Invariant(Forall(int, lambda d_3_k_: | ||
not (((start) <= (d_3_k_)) and ((d_3_k_) < (d_1_i_))) or (((s)[d_3_k_]) == ((s)[((len(s)) - (1)) - (d_3_k_ - start)])))) | ||
if ((s)[d_1_i_]) != ((s)[d_2_j_]): | ||
return False | ||
d_1_i_ = (d_1_i_) + (1) | ||
d_2_j_ = (d_2_j_) - (1) | ||
return True | ||
|
||
@Pure | ||
def is__palindrome__fun(start : int, s : List[int]) -> bool : | ||
Requires(Acc(list_pred(s), 1/2)) | ||
Requires(0 <= start and start < len(s)) | ||
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)]))) | ||
|
||
@Pure | ||
def starts__with(result : List[int], s : List[int]) -> bool : | ||
Requires(Acc(list_pred(s), 1/2)) | ||
Requires(Acc(list_pred(result), 1/2)) | ||
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_])))) | ||
|
||
def make__palindrome(s : List[int]) -> List[int]: | ||
Requires(Acc(list_pred(s))) | ||
Ensures(Acc(list_pred(s))) | ||
Ensures(Acc(list_pred(Result()))) | ||
Ensures((len(Result())) <= ((2) * (len(s)))) | ||
Ensures(len(Result()) == 0 or is__palindrome__fun(0, Result())) | ||
Ensures(starts__with(Result(), s)) | ||
result = list([int(0)] * 0) # type : List[int] | ||
if (len(s)) == (0): | ||
result = [] | ||
return result | ||
d_6_beginning__of__suffix_ = int(0) # type : int | ||
d_8_flag_ = is__palindrome(d_6_beginning__of__suffix_, s) # type : bool | ||
while not(d_8_flag_): | ||
Invariant(Acc(list_pred(s))) | ||
Invariant(len(s) > 0) | ||
Invariant((((d_6_beginning__of__suffix_) >= (0)) and (((d_6_beginning__of__suffix_) + (1)) < (len(s)))) or ((d_8_flag_) and (((d_6_beginning__of__suffix_) >= (0)) and ((d_6_beginning__of__suffix_) < (len(s)))))) | ||
Invariant(Implies(d_8_flag_, is__palindrome__fun(d_6_beginning__of__suffix_, s))) | ||
d_6_beginning__of__suffix_ = (d_6_beginning__of__suffix_) + (1) | ||
d_8_flag_ = is__palindrome(d_6_beginning__of__suffix_, s) | ||
d_10_reversed_ = reverse(d_6_beginning__of__suffix_, s) # type : List[int] | ||
result = (s) + (d_10_reversed_) | ||
return result | ||
|
||
def reverse(end : int, str : List[int]) -> List[int]: | ||
Requires(Acc(list_pred(str), 1/2)) | ||
Requires(0 <= end and end < len(str)) | ||
Ensures(Acc(list_pred(str), 1/2)) | ||
Ensures(0 <= end and end < len(str)) | ||
Ensures(Acc(list_pred(Result()))) | ||
Ensures(str == Old(str)) | ||
Ensures((len(Result())) == (end)) | ||
Ensures(Forall(int, lambda d_11_k_: | ||
not (((0) <= (d_11_k_)) and ((d_11_k_) < (end))) or (((Result())[d_11_k_]) == ((str)[((end) - (1)) - (d_11_k_)])))) | ||
rev = list([int(0)] * 0) # type : List[int] | ||
rev = [] | ||
d_12_i_ = int(0) # type : int | ||
d_12_i_ = 0 | ||
while (d_12_i_) < (end): | ||
Invariant(Acc(list_pred(str), 1/2)) | ||
Invariant(Acc(list_pred(rev))) | ||
Invariant(0 <= end and end < len(str)) | ||
Invariant(((d_12_i_) >= (0)) and ((d_12_i_) <= (end))) | ||
Invariant((len(rev)) == (d_12_i_)) | ||
Invariant(Forall(int, lambda d_13_k_: | ||
not (((0) <= (d_13_k_)) and ((d_13_k_) < (d_12_i_))) or (((rev)[d_13_k_]) == ((str)[(end - (1)) - (d_13_k_)])))) | ||
rev = (rev) + [(str)[(end - (d_12_i_)) - (1)]] | ||
d_12_i_ = (d_12_i_) + (1) | ||
return rev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 xor(a : int, b : int) -> int: | ||
Ensures((Result()) == ((0 if (a) == (b) else 1))) | ||
result = int(0) # type : int | ||
if (a) == (b): | ||
result = 0 | ||
else: | ||
result = 1 | ||
return result | ||
|
||
def string__xor(a : List[int], b : List[int]) -> List[int]: | ||
Requires(Acc(list_pred(b))) | ||
Requires(Acc(list_pred(a))) | ||
Requires((len(a)) == (len(b))) | ||
Requires(Forall(int, lambda d_0_i_: | ||
not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(a)))) or ((((a)[d_0_i_]) == (0)) or (((a)[d_0_i_]) == (1))))) | ||
Requires(Forall(int, lambda d_1_i_: | ||
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(b)))) or ((((b)[d_1_i_]) == (0)) or (((b)[d_1_i_]) == (1))))) | ||
Ensures(Acc(list_pred(b))) | ||
Ensures(Acc(list_pred(a))) | ||
Ensures(Acc(list_pred(Result()))) | ||
Ensures((len(a)) == (len(b))) | ||
Ensures((len(Result())) == (len(a))) | ||
Ensures(Forall(int, lambda d_2_i_: | ||
not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(Result())))) or ((((Result())[d_2_i_]) == (0)) or (((Result())[d_2_i_]) == (1))))) | ||
Ensures(Forall(int, lambda d_3_i_: | ||
not (((0) <= (d_3_i_)) and ((d_3_i_) < (len(Result())))) or (((Result())[d_3_i_]) == ((0 if ((a)[d_3_i_]) == ((b)[d_3_i_]) else 1))))) | ||
result = list([int(0)] * 0) # type : List[int] | ||
d_4_i_ = int(0) # type : int | ||
while (d_4_i_) < (len(a)): | ||
Invariant(Acc(list_pred(b))) | ||
Invariant(Acc(list_pred(a))) | ||
Invariant(Acc(list_pred(result))) | ||
Invariant((len(a)) == (len(b))) | ||
Invariant(((d_4_i_) >= (0)) and ((d_4_i_) <= (len(a)))) | ||
Invariant((len(result)) == (d_4_i_)) | ||
Invariant(Forall(int, lambda d_0_i_: | ||
not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(a)))) or ((((a)[d_0_i_]) == (0)) or (((a)[d_0_i_]) == (1))))) | ||
Invariant(Forall(int, lambda d_1_i_: | ||
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(b)))) or ((((b)[d_1_i_]) == (0)) or (((b)[d_1_i_]) == (1))))) | ||
Invariant(Forall(int, lambda d_5_j_: | ||
not (((0) <= (d_5_j_)) and ((d_5_j_) < (d_4_i_))) or (((result)[d_5_j_]) == ((0 if ((a)[d_5_j_]) == ((b)[d_5_j_]) else 1))))) | ||
d_6_bitResult_ = (0 if ((a)[d_4_i_]) == ((b)[d_4_i_]) else 1) | ||
result = (result) + [d_6_bitResult_] | ||
d_4_i_ = (d_4_i_) + (1) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from typing import cast, List, Dict, Set, Optional, Union | ||
from nagini_contracts.contracts import * | ||
|
||
def greatest_common_divisor(a: int, b: int) -> int: | ||
Requires(a != 0 or b != 0) | ||
Ensures(Result() != 0) | ||
|
||
x = a | ||
y = b | ||
|
||
while y != 0: | ||
Invariant(x != 0 or y != 0) | ||
temp = y | ||
y = x % y | ||
x = temp | ||
|
||
return x |
Oops, something went wrong.