diff --git a/.github/workflows/test-new.yml b/.github/workflows/test-new.yml index 7368a4c..6e126bb 100644 --- a/.github/workflows/test-new.yml +++ b/.github/workflows/test-new.yml @@ -5,7 +5,7 @@ name: Nagini Tests (Changed) Ubuntu on: pull_request: - types: [synchronize] + types: [synchronize, opened, reopened, edited] branches: - main diff --git a/Bench/096-count_up_to.py b/Bench/096-count_up_to.py new file mode 100644 index 0000000..76169eb --- /dev/null +++ b/Bench/096-count_up_to.py @@ -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 diff --git a/Bench/163-generate_integers.py b/Bench/163-generate_integers.py index 53b5470..08f2236 100644 --- a/Bench/163-generate_integers.py +++ b/Bench/163-generate_integers.py @@ -41,6 +41,7 @@ def generate__integers(a : int, b : int) -> List[int]: result = list([]) d_6_i_ = int(0) # type : int d_6_i_ = d_4_lower_ + while (d_6_i_) <= (d_5_upper_): Invariant(Acc(list_pred(result))) Invariant(d_6_i_ >= 2) diff --git a/WIP/026-remove_duplicates.py b/WIP/026-remove_duplicates.py new file mode 100644 index 0000000..b10a187 --- /dev/null +++ b/WIP/026-remove_duplicates.py @@ -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 diff --git a/WIP/029-filter_by_prefix.py b/WIP/029-filter_by_prefix.py new file mode 100644 index 0000000..8d5bd68 --- /dev/null +++ b/WIP/029-filter_by_prefix.py @@ -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 \ No newline at end of file diff --git a/WIP/096-count_up_to.py b/WIP/096-count_up_to.py index f79705d..14623f5 100644 --- a/WIP/096-count_up_to.py +++ b/WIP/096-count_up_to.py @@ -9,12 +9,13 @@ def IsPrime(n : int) -> bool : 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_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_)))) # Ensures(Forall(int, lambda d_3_p_: # ((((2) <= (d_3_p_)) and ((d_3_p_) < (n))) and (IsPrime(d_3_p_))) == ((d_3_p_) in (Result())))) primes = list([int(0)] * 0) # type : List[int] @@ -28,28 +29,21 @@ def CountUpTo(n : int) -> List[int]: 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_)))) + # Invariant(Forall(int, lambda d_6_j_: + # (Implies(((0) <= (d_6_j_)) and ((d_6_j_) < (len(primes))), ((2) <= ((primes)[d_6_j_])) and (((primes)[d_6_j_]) < (d_4_i_)))))) # Invariant(Forall(int, lambda d_7_p_: # (((((2) <= (d_7_p_)) and ((d_7_p_) < (d_4_i_))) and (IsPrime(d_7_p_))) == ((d_7_p_) in (primes)), [[IsPrime(d_7_p_)]]))) - Invariant(Forall(int, lambda d_8_j_: - (Implies(((0) <= (d_8_j_)) and ((d_8_j_) < (len(primes))), ((primes)[d_8_j_]) < (d_4_i_)), [[(primes)[d_8_j_]]]))) + # Invariant(Forall(int, lambda d_8_j_: + # (Implies(((0) <= (d_8_j_)) and ((d_8_j_) < (len(primes))), ((primes)[d_8_j_]) < (d_4_i_)), [[(primes)[d_8_j_]]]))) # Invariant(Forall(int, lambda d_8_j_: # Forall(int, lambda d_9_k_: # (Implies((((0) <= (d_8_j_)) and ((d_8_j_) < (d_9_k_))) and ((d_9_k_) < (len(primes))), ((primes)[d_8_j_]) < ((primes)[d_9_k_])), [[(primes)[d_8_j_] < (primes)[d_9_k_]]])))) - # 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_6_j_: - # (Implies(((0) <= (d_6_j_)) and ((d_6_j_) < (len(primes))), ((2) <= ((primes)[d_6_j_])) and (((primes)[d_6_j_]) < (d_4_i_))), [[(primes)[d_6_j_]]]))) if IsPrime(d_4_i_): - # prime_prev = list(primes) - # Assert(Forall(int, lambda d_5_j_: - # (Implies(((0) <= (d_5_j_)) and ((d_5_j_) < (len(primes))), IsPrime((primes)[d_5_j_]))))) - # Assert(Forall(int, lambda d_8_j_: - # (Implies(((0) <= (d_8_j_)) and ((d_8_j_) < (len(primes))), ((primes)[d_8_j_]) < (d_4_i_ + 1)), [[(primes)[d_8_j_]]]))) primes = primes + [(d_4_i_)] - # Assert(primes[len(primes) - 1] < d_4_i_ + 1) - # Assert(Forall(int, lambda d_8_j_: - # (Implies(((0) <= (d_8_j_)) and ((d_8_j_) < (len(primes) - 1)), ((primes)[d_8_j_]) < (d_4_i_ + 1)), [[(primes)[d_8_j_]]]))) - # Assert(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_])]]))) d_4_i_ = (d_4_i_) + (1) return primes diff --git a/Bench/159-eat.py b/WIP/159-eat.py similarity index 100% rename from Bench/159-eat.py rename to WIP/159-eat.py diff --git a/public/scripts/test-new.sh b/public/scripts/test-new.sh index b6d5f4f..d20c8c2 100755 --- a/public/scripts/test-new.sh +++ b/public/scripts/test-new.sh @@ -1,28 +1,30 @@ #!/bin/bash set -eou pipefail -DIRECTORY="./Bench" # You can change this to your specific directory +DIRECTORY="Bench" # You can change this to your specific directory # Timeout duration in seconds TIMEOUT_DURATION=600 file_count=0 +file_no=0 + echo "New files found:" for f in $1; do # Check if the file is in the known directory echo "check" echo $f - if [[ $f == $DIRECTORY/* ]]; then + if [[ $f == *.py ]]; then echo "check1" echo $f - if [[ $f == *.py ]]; then + if [[ $f == $DIRECTORY/* ]]; then echo $f file_count=$((file_count+1)) fi fi done -echo "Staring the check" +echo "Starting the check" for f in $1 do # Check if the file is in the known directory @@ -30,7 +32,7 @@ do if [[ $f == *.py ]]; then file_no=$((file_no+1)) echo "Running dafny on $(basename "$f") ($file_no/$file_count)" - timeout "$TIMEOUT_DURATION" nagini "$file" + timeout "$TIMEOUT_DURATION" nagini "$f" fi fi done