-
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.
Merge pull request #5 from alex28sh/096
096
- Loading branch information
Showing
8 changed files
with
230 additions
and
26 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
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,38 @@ | ||
from typing import cast, List, Dict, Set, Optional, Union | ||
from nagini_contracts.contracts import * | ||
|
||
@Pure | ||
def IsPrime(n : int) -> bool : | ||
return ((n) > (1)) and (Forall(int, lambda d_0_k_: | ||
Implies(((2) <= (d_0_k_)) and ((d_0_k_) < (n)), ((n % d_0_k_)) != (0)))) | ||
|
||
def CountUpTo(n : int) -> List[int]: | ||
Requires((n) >= (0)) | ||
Ensures(Acc(list_pred(Result()))) | ||
Ensures(Forall(int, lambda d_2_i_: | ||
not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(Result())))) or (((Result())[d_2_i_]) < (n)))) | ||
Ensures(Forall(int, lambda d_1_i_: | ||
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(Result())))) or (IsPrime((Result())[d_1_i_])))) | ||
Ensures(Forall(int, lambda d_3_p_: | ||
Implies((((2) <= (d_3_p_)) and ((d_3_p_) < (n))) and IsPrime(d_3_p_), | ||
Exists(int, lambda x: x >= 0 and x < len(Result()) and Result()[x] == d_3_p_)))) | ||
primes = list([int(0)] * 0) # type : List[int] | ||
primes = list([]) | ||
if (n) <= (2): | ||
return primes | ||
d_4_i_ = int(0) # type : int | ||
d_4_i_ = 2 | ||
while (d_4_i_) < (n): | ||
Invariant(Acc(list_pred(primes))) | ||
Invariant(((2) <= (d_4_i_)) and ((d_4_i_) <= (n))) | ||
Invariant(Forall(int, lambda x: | ||
Implies(x >= 0 and x < len(primes), 2 <= primes[x] and primes[x] < n))) | ||
Invariant(Forall(int, lambda d_5_j_: | ||
(Implies(((0) <= (d_5_j_)) and ((d_5_j_) < (len(primes))), IsPrime((primes)[d_5_j_])), [[IsPrime((primes)[d_5_j_])]]))) | ||
Invariant(Forall(int, lambda d_3_p_: | ||
Implies((((2) <= (d_3_p_)) and ((d_3_p_) < (d_4_i_))) and IsPrime(d_3_p_), | ||
Exists(int, lambda x: x >= 0 and x < len(primes) and primes[x] == d_3_p_)))) | ||
if IsPrime(d_4_i_): | ||
primes = primes + [(d_4_i_)] | ||
d_4_i_ = (d_4_i_) + (1) | ||
return primes |
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
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,84 @@ | ||
from typing import cast, List, Dict, Set, Optional, Union | ||
from nagini_contracts.contracts import * | ||
|
||
def remove__duplicates(a : List[int]) -> List[int]: | ||
Requires(Acc(list_pred(a), 1/2)) | ||
# Requires(Forall(int, lambda d_0_i_: | ||
# not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(a)))) or ((count__rec(a, (a)[d_0_i_], len(a))) >= (1)))) | ||
Ensures(Acc(list_pred(a), 1/2)) | ||
Ensures(Acc(list_pred(Result()))) | ||
Ensures(len(a) == len(Old(a))) | ||
Ensures(len(a) >= len(Result())) | ||
Ensures(Forall(int, lambda d_1_i_: | ||
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(Result())))) or ((count__rec(a, (Result())[d_1_i_], len((Result())))) == (1)))) | ||
# Ensures(Forall(int, lambda d_2_i_: | ||
# not (((0) <= (d_2_i_)) and ((d_2_i_) < (len(a)))) or ((((a)[d_2_i_]) in (Result())) == ((count__rec(a, (a)[d_2_i_], len(a))) == (1))))) | ||
result = list([int(0)] * 0) # type : List[int] | ||
result = [] | ||
d_4_i_ = int(0) # type : int | ||
d_4_i_ = 0 | ||
a_old = list(a) | ||
|
||
# if (len(a) > 0): | ||
# d_8_cnt_ = int(0) # type : int | ||
# var = (a)[d_4_i_] # type : int | ||
# Assert(len((a)) == l1) | ||
# b = list(a) | ||
# d_8_cnt_ = count_my(a, var) | ||
# Assert(len((a)) == l1) | ||
# Assert(len(a) == len(b)) | ||
# Assert(count__rec(a, var, len(a)) == d_8_cnt_) | ||
# Assert(len(Old(a)) == l1) | ||
while (d_4_i_) < (len(a)): | ||
Invariant(Acc(list_pred(result))) | ||
Invariant(Acc(list_pred(a), 1/2)) | ||
Invariant(Acc(list_pred(a_old), 1/2)) | ||
Invariant(len(a) == len(a_old)) | ||
Invariant(Forall(int, lambda d_3_i_: (Implies(d_3_i_ >= 0 and d_3_i_ < len(a), a_old[d_3_i_] == a[d_3_i_])))) | ||
Invariant(((0) <= (d_4_i_)) and ((d_4_i_) <= (len(a)))) | ||
Invariant(len(result) <= d_4_i_) | ||
Invariant(Forall(int, lambda d_5_j_: | ||
(Implies(((0) <= (d_5_j_)) and ((d_5_j_) < (len(result))), (count__rec(a, (result)[d_5_j_], len(a))) == (1)), [[count__rec(a, (result)[d_5_j_], len(a))]]))) | ||
# Invariant(Forall(int, lambda d_6_j_: | ||
# (Implies(((0) <= (d_6_j_)) and ((d_6_j_) < (d_4_i_)), (((a)[d_6_j_]) in (d_3_res_)) == ((count__rec(a, (a)[d_6_j_], len(a))) == (1))), [[count__rec(a, (a)[d_6_j_], len(a))]]))) | ||
# Invariant(Forall(int, lambda d_7_j_: | ||
# not (((0) <= (d_7_j_)) and ((d_7_j_) < (len(d_3_res_)))) or (((d_3_res_)[d_7_j_]) in (list((a)[:d_4_i_:]))))) | ||
d_8_cnt_ = int(0) # type : int | ||
d_8_cnt_ = count_my(a, (a)[d_4_i_]) | ||
if (d_8_cnt_) == (1): | ||
Assert(d_4_i_ < len(a_old)) | ||
Assert(len(a) == len(a_old)) | ||
Assert(count__rec(a, (a)[d_4_i_], len(a)) == 1) | ||
result = (result) + [(a)[d_4_i_]] | ||
d_4_i_ = (d_4_i_) + (1) | ||
return result | ||
|
||
@Pure | ||
def count__rec(a : List[int], x : int, i : int) -> int : | ||
Requires(Acc(list_pred(a), 1/2)) | ||
Requires(((0) <= (i)) and ((i) <= (len(a)))) | ||
if (i) == 0: | ||
return 0 | ||
else: | ||
return (((a)[i - 1]) == (x)) + (count__rec(a, x, (i) - (1))) | ||
|
||
def count_my(a : List[int], x : int) -> int: | ||
Requires(Acc(list_pred(a), 1/2)) | ||
Ensures(Acc(list_pred(a), 1/2)) | ||
Ensures(a == Old(a)) | ||
Ensures((Result()) == (count__rec(a, x, len(a)))) | ||
cnt = int(0) # type : int | ||
cnt = 0 | ||
d_11_i_ = int(0) # type : int | ||
d_11_i_ = 0 | ||
while (d_11_i_) < (len(a)): | ||
Invariant(Acc(list_pred(a), 1/2)) | ||
Invariant(((0) <= (d_11_i_)) and ((d_11_i_) <= (len(a)))) | ||
Invariant(Forall(int, lambda y: (Implies(y >= 0 and y < len(a), count__rec(a, x, y + 1) == (count__rec(a, x, y) + ((a)[y] == x))), [[count__rec(a, x, y + 1)]]))) | ||
Invariant((cnt) == (count__rec(a, x, d_11_i_))) | ||
|
||
Assert(count__rec(a, x, d_11_i_ + 1) == (count__rec(a, x, d_11_i_) + ((a)[d_11_i_] == x))) | ||
if ((a)[d_11_i_]) == (x): | ||
cnt = (cnt) + (1) | ||
d_11_i_ = (d_11_i_) + (1) | ||
return cnt |
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,85 @@ | ||
from typing import cast, List, Dict, Set, Optional, Union | ||
from nagini_contracts.contracts import * | ||
|
||
|
||
@Pure | ||
def starts__with(s : List[int], p : List[int], i : int) -> bool : | ||
Requires(Acc(list_pred(s), 1/2)) | ||
Requires(Acc(list_pred(p), 1/2)) | ||
Requires(i >= 0 and i <= len(p) and i <= len(s)) | ||
Ensures(Implies(len(p) == i and len(s) >= len(p), Result())) | ||
Ensures(Implies(len(s) < len(p), not Result())) | ||
return len(s) >= len(p) and Forall(int, lambda x: Implies(x >= i and x < len(p), s[x] == p[x])) | ||
|
||
# @Pure | ||
# def starts__with__fun(s : List[int], p : List[int], i : int) -> bool : | ||
# Requires(Acc(list_pred(s), 1/2)) | ||
# Requires(Acc(list_pred(p), 1/2)) | ||
# Requires(0 <= i and i <= len(p) and i <= len(s)) | ||
# # Ensures(Implies(len(p) == i, len(s) >= len(p) and Forall(int, lambda x: x >= i and x < len(p) and s[x] == p[x]) and Result())) | ||
# # Ensures(Implies(len(p) == i, Result() == starts__with(s,p, i))) | ||
# # Ensures(Result() == starts__with(s, p, i)) | ||
# if (len(p) == i): | ||
# return True | ||
# if (len(s) > i and len(s) >= len(p) and s[i] == p[i]): | ||
# return starts__with(s, p, i + 1) | ||
# return False | ||
|
||
def filter__by__prefix(xs : List[List[int]], p : List[int]) -> List[int]: | ||
Requires(Acc(list_pred(xs))) | ||
Requires(Acc(list_pred(p))) | ||
Requires(Forall(xs, lambda x : Acc(list_pred(x)))) | ||
# Requires(Forall(int, lambda x : (Implies(x >= 0 and x < len(xs), Acc(list_pred(xs[x])))))) | ||
Ensures(Acc(list_pred(p))) | ||
Ensures(Acc(list_pred(xs))) | ||
# Ensures(Forall(int, lambda x : (Implies(x >= 0 and x < len(xs), Acc(list_pred(xs[x])))))) | ||
Ensures(Acc(list_pred(Result()))) | ||
# Ensures(Forall(int, lambda x : Implies(x >= 0 and x < len(Result()), Acc(list_pred(Result()[x]))))) | ||
# Ensures(Forall(int, lambda d_0_i_: | ||
# not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(Result())))) or (starts__with(Result()[d_0_i_], p, 0)))) | ||
filtered = list([int(0)] * 0) # type : List[int] | ||
d_1_i_ = int(0) # type : int | ||
d_1_i_ = 0 | ||
while (d_1_i_) < (len(xs)): | ||
Invariant(Acc(list_pred(filtered))) | ||
Invariant(Acc(list_pred(xs), 1/2)) | ||
Invariant(Acc(list_pred(p), 1/2)) | ||
Invariant(((0) <= (d_1_i_)) and ((d_1_i_) <= (len(xs)))) | ||
Invariant(Forall(xs, lambda x : Acc(list_pred(x)))) | ||
Invariant(Forall(int, lambda d_2_j_: Implies(d_2_j_ >= 0 and d_2_j_ < len(filtered), filtered[d_2_j_] >= 0 and filtered[d_2_j_] < d_1_i_))) | ||
# Invariant(Forall(filtered, lambda x : Acc(list_pred(x)))) | ||
# Invariant(Forall(int, lambda x : (Implies(x >= 0 and x < len(filtered), Acc(list_pred(filtered[x]))), [[filtered[x]]]))) | ||
# Invariant(Forall(int, lambda x : (Implies(x >= 0 and x < len(xs), Acc(list_pred(xs[x])))))) | ||
# Invariant(Forall(filtered, lambda x: | ||
# (starts__with(x, p, 0), [[starts__with(x, p, 0)]]))) | ||
# Invariant(Forall(int, lambda d_2_j_: | ||
# (Implies(((0) <= (d_2_j_)) and ((d_2_j_) < (len(filtered))), starts__with(xs[(filtered)[d_2_j_]], p, 0)), [[starts__with(xs[(filtered)[d_2_j_]], p, 0)]]))) | ||
# Invariant(Forall(int, lambda d_2_j_: | ||
# (Implies(((0) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_)) and starts__with(xs[d_2_j_], p, 0), | ||
# Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_2_j_)), | ||
# [[xs[d_2_j_]]]))) | ||
Assume(Forall(int, lambda d_2_j_: | ||
(Implies(((0) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_)) and starts__with(xs[d_2_j_], p, 0), | ||
Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_2_j_)), | ||
[[xs[d_2_j_]]]))) | ||
if starts__with((xs)[d_1_i_], p, 0): | ||
filtered = (filtered) + [d_1_i_] | ||
Assert(starts__with(xs[(filtered)[len(filtered) - 1]], p, 0)) | ||
Assert(d_1_i_ == filtered[len(filtered) - 1]) | ||
Assert(Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_1_i_)) | ||
Assert(Forall(int, lambda d_2_j_: | ||
(Implies(((0) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_)) and starts__with(xs[d_2_j_], p, 0), | ||
Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_2_j_)), | ||
[[xs[d_2_j_]]]))) | ||
Assert(Forall(int, lambda d_2_j_: | ||
(Implies(((0) <= (d_2_j_)) and ((d_2_j_) <= (d_1_i_)) and starts__with(xs[d_2_j_], p, 0), | ||
Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_2_j_)), | ||
[[xs[d_2_j_]]]))) | ||
d_1_i_ = (d_1_i_) + (1) | ||
Assert(Forall(int, lambda d_2_j_: | ||
(Implies(((0) <= (d_2_j_)) and ((d_2_j_) < (d_1_i_)) and starts__with(xs[d_2_j_], p, 0), | ||
Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_2_j_)), | ||
[[xs[d_2_j_]]]))) | ||
# Assert(Implies(((0) <= (d_1_i_)) and ((d_1_i_) < (d_1_i_)) and starts__with(xs[d_1_i_], p, 0), | ||
# Exists(int, lambda x: x >= 0 and x < len(filtered) and filtered[x] == d_1_i_))) | ||
return filtered |
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
File renamed without changes.
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