-
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
9 changed files
with
459 additions
and
94 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,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 |
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,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)) |
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,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) |
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
Oops, something went wrong.