Skip to content

Commit

Permalink
Merge pull request #9 from alex28sh/29,96
Browse files Browse the repository at this point in the history
29,96
  • Loading branch information
alex28sh authored Aug 23, 2024
2 parents e482790 + d95b1c0 commit bf7eec7
Show file tree
Hide file tree
Showing 25 changed files with 505 additions and 93 deletions.
54 changes: 54 additions & 0 deletions Bench/029-filter_by_prefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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(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))))
Ensures(Acc(list_pred(p)))
Ensures(Acc(list_pred(xs)))
Ensures(Forall(xs, lambda x : Acc(list_pred(x))))
Ensures(Acc(list_pred(Result())))
Ensures(Forall(int, lambda d_2_j_:
Implies(d_2_j_ >= 0 and d_2_j_ < len(Result()), Result()[d_2_j_] >= 0 and Result()[d_2_j_] < len(xs))))
Ensures(Forall(int, lambda d_0_i_:
not (((0) <= (d_0_i_)) and ((d_0_i_) < (len(Result())))) or (starts__with(xs[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(filtered, lambda i:
(i >= 0 and i < d_1_i_)))
Invariant(Forall(filtered, lambda i:
(starts__with(xs[i], p, 0), [[starts__with(xs[i], p, 0)]])))
if starts__with__fun((xs)[d_1_i_], p, 0):
filtered = (filtered) + [d_1_i_]
d_1_i_ = (d_1_i_) + (1)
return filtered
9 changes: 9 additions & 0 deletions Bench/045-triangle_area.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import cast, List, Dict, Set, Optional, Union
from nagini_contracts.contracts import *

def CalculateTriangleArea(a : float, h : float) -> float:
Requires(h * a == h * a)
Requires(((h) >= float("0.0")) and ((a) >= float("0.0")))
Ensures((Result()) == (((h) * (a)) / (float("2.0"))))
area = ((h) * (a)) / float("2.0") # type : float
return area
32 changes: 32 additions & 0 deletions Bench/096-count_up_to.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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_]))))
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_])]])))
if IsPrime(d_4_i_):
primes = primes + [(d_4_i_)]
d_4_i_ = (d_4_i_) + (1)
return primes
71 changes: 71 additions & 0 deletions Bench/116-sort_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from typing import List, Tuple
from nagini_contracts.contracts import *

@Pure
def popcount(n : int) -> int :
Requires(n >= 0)
if n == 0:
return 0
else:
return ((n % 2)) + popcount(n // 10)

def BubbleSort(a1 : List[int]) -> List[int]:
Requires(Acc(list_pred(a1), 1/2))
Requires(Forall(int, lambda i : Implies(i >= 0 and i < len(a1), a1[i] >= 0)))
Ensures(Acc(list_pred(a1), 1/2))
Ensures(Acc(list_pred(Result())))
Ensures((len(a1)) == (len(Result())))
# Ensures(ToMS(ToSeq(a1)) == ToMS(ToSeq(Result())))
Ensures(Forall(int, lambda i : Implies(i >= 0 and i < len(Result()), Result()[i] >= 0)))
Ensures(Forall(int, lambda d_0_i_:
Forall(int, lambda d_1_j_:
Implies((((0) <= (d_0_i_)) and ((d_0_i_) < (d_1_j_))) and ((d_1_j_) < (len((Result())))), popcount((Result())[d_0_i_]) <= popcount((Result())[d_1_j_])))))
a = list(a1) # type : List[int]
d_2_i_ = int(0) # type : int
d_2_i_ = (len((a))) - (1)
while (d_2_i_) > (0):
Invariant(Acc(list_pred(a)))
Invariant(Forall(int, lambda i : Implies(i >= 0 and i < len(a), a[i] >= 0)))
Invariant(Acc(list_pred(a1), 1/2))
Invariant((len(a1)) == (len(a)))
Invariant(not ((d_2_i_) < (0)) or ((len((a))) == (0)))
Invariant(((-1) <= (d_2_i_)) and ((d_2_i_) < (len((a)))))
Invariant(Forall(int, lambda d_3_ii_:
(Forall(int, lambda d_4_jj_:
(Implies((((d_2_i_) <= (d_3_ii_)) and ((d_3_ii_) < (d_4_jj_))) and ((d_4_jj_) < (len((a)))), popcount((a)[d_3_ii_]) <= popcount((a)[d_4_jj_])),
[[popcount((a)[d_4_jj_])]])),
[[popcount((a)[d_3_ii_])]])))
Invariant(Forall(int, lambda d_5_k_:
(Forall(int, lambda d_6_k_k_:
(Implies(((((0) <= (d_5_k_)) and ((d_5_k_) <= (d_2_i_))) and ((d_2_i_) < (d_6_k_k_)) and (d_6_k_k_) < (len((a)))), popcount((a)[d_5_k_]) <= popcount((a)[d_6_k_k_])),
[[popcount((a)[d_6_k_k_])]])),
[[popcount((a)[d_5_k_])]])))
d_7_j_ = int(0) # type : int
d_7_j_ = 0
while (d_7_j_) < (d_2_i_):
Invariant(Acc(list_pred(a)))
Invariant(Forall(int, lambda i : Implies(i >= 0 and i < len(a), a[i] >= 0)))
Invariant(Acc(list_pred(a1), 1/2))
Invariant((len(a1)) == (len(a)))
Invariant((((0) < (d_2_i_)) and ((d_2_i_) < (len((a))))) and (((0) <= (d_7_j_)) and ((d_7_j_) <= (d_2_i_))))
Invariant(Forall(int, lambda d_8_ii_:
(Forall(int, lambda d_9_jj_:
(Implies((((d_2_i_) <= (d_8_ii_)) and ((d_8_ii_) <= (d_9_jj_))) and ((d_9_jj_) < (len((a)))), popcount((a)[d_8_ii_]) <= popcount((a)[d_9_jj_])),
[[popcount((a)[d_9_jj_])]])),
[[popcount((a)[d_8_ii_])]])))
Invariant(Forall(int, lambda d_10_k_:
(Forall(int, lambda d_11_k_k_:
(Implies(((((0) <= (d_10_k_)) and ((d_10_k_) <= (d_2_i_))) and ((d_2_i_) < (d_11_k_k_))) and ((d_11_k_k_) < (len((a)))), popcount((a)[d_10_k_]) <= popcount((a)[d_11_k_k_])),
[[popcount((a)[d_11_k_k_])]])),
[[popcount((a)[d_10_k_])]])))
Invariant(Forall(int, lambda d_12_k_:
(Implies(((0) <= (d_12_k_)) and ((d_12_k_) <= (d_7_j_)), popcount((a)[d_12_k_]) <= popcount((a)[d_7_j_])),
[[popcount((a)[d_12_k_])]])))
if popcount((a)[d_7_j_]) > popcount((a)[(d_7_j_) + (1)]):
rhs0_ = (a)[(d_7_j_) + (1)] # type : int
(a)[(d_7_j_) + (1)] = (a)[d_7_j_]
(a)[d_7_j_] = rhs0_
Assert(popcount((a)[d_7_j_]) <= popcount((a)[(d_7_j_) + (1)]))
d_7_j_ = (d_7_j_) + (1)
d_2_i_ = (d_2_i_) - (1)
return a
144 changes: 144 additions & 0 deletions Bench/123-get_odd_collatz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
from typing import cast, List, Dict, Set, Optional, Union
from nagini_contracts.contracts import *

@Pure
def iterate__to__odd(n : int) -> int :
Requires(n % 2 == 0)
Requires(n >= 0)
Ensures(Result() % 2 == 1)
Ensures(Result() > 0)
if (n // 2) % 2 == 1:
return n // 2
else:
return iterate__to__odd(n // 2)

@Pure
def next__odd__collatz(n : int) -> int :
Requires(n > 0)
Ensures(Result() > 0)
Ensures(Result() % 2 == 1)
if ((n % 2)) == (0):
return iterate__to__odd(n)
else:
return iterate__to__odd(((3) * (n)) + (1))


def next__odd__collatz__iter(n : int) -> int:
Requires((n) > (0))
Ensures(Result() > 0)
Ensures(((Result() % 2)) == (1))
Ensures((Result()) == (next__odd__collatz(n)))
next = int(0) # type : int
next = n
if ((next % 2)) == (1):
next = ((3) * (next)) + (1)
d_0_start_ = next # type : int
while ((next % 2)) == (0):
Invariant((next) > (0))
Invariant(not (((next % 2)) == (0)) or ((next__odd__collatz(next)) == (next__odd__collatz(n))))
Invariant(not (((next % 2)) == (0)) or ((iterate__to__odd(next)) == (iterate__to__odd(d_0_start_))))
Invariant(not (((next % 2)) == (1)) or ((next) == (iterate__to__odd(d_0_start_))))
next = (next // 2)
return next


def get__odd__collatz__unsorted(n : int) -> List[int]:
Requires((n) > (1))
Ensures(Acc(list_pred(Result())))
Ensures(Forall(int, lambda d_1_i_:
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(Result())))) or ((((Result())[d_1_i_] > 0)))))
Ensures(Forall(int, lambda d_1_i_:
not (((0) <= (d_1_i_)) and ((d_1_i_) < (len(Result())))) or ((((Result())[d_1_i_] % 2)) == (1))))
Ensures(Forall(int, lambda d_2_i_:
not (((1) <= (d_2_i_)) and ((d_2_i_) < (len(Result())))) or (((Result())[d_2_i_]) == (next__odd__collatz((Result())[(d_2_i_) - (1)])))))
odd__collatz = list([int(0)] * 0) # type : List[int]
d_3_cur_ = int(0) # type : int
d_3_cur_ = n
if ((d_3_cur_ % 2)) == (0):
d_3_cur_ = next__odd__collatz__iter(d_3_cur_)
odd__collatz = [d_3_cur_]
Assert(len(odd__collatz) == 1)
Assert(Forall(int, lambda d_5_i_:
(not (((1) <= (d_5_i_)) and ((d_5_i_) < (len(odd__collatz)))))))
Assert(Forall(int, lambda d_5_i_:
(not (((1) <= (d_5_i_)) and ((d_5_i_) < (len(odd__collatz)))) or (((odd__collatz)[d_5_i_]) == (next__odd__collatz((odd__collatz)[(d_5_i_) - (1)]))), [[next__odd__collatz((odd__collatz)[(d_5_i_) - (1)])]])))
while ((odd__collatz)[(len(odd__collatz)) - (1)]) != (1):
Invariant(Acc(list_pred(odd__collatz)))
Invariant((d_3_cur_) > (0))
Invariant((len(odd__collatz)) > (0))
Invariant(Forall(int, lambda d_4_i_:
(not (((0) <= (d_4_i_)) and ((d_4_i_) < (len(odd__collatz)))) or ((((odd__collatz)[d_4_i_] > 0))))))
Invariant(Forall(int, lambda d_4_i_:
(not (((0) <= (d_4_i_)) and ((d_4_i_) < (len(odd__collatz)))) or ((((odd__collatz)[d_4_i_] % 2)) == (1)))))
Invariant(Forall(int, lambda d_5_i_:
(not (((1) <= (d_5_i_)) and ((d_5_i_) < (len(odd__collatz)))) or (((odd__collatz)[d_5_i_]) == (next__odd__collatz((odd__collatz)[(d_5_i_) - (1)]))), [[next__odd__collatz((odd__collatz)[(d_5_i_) - (1)])]])))
odd__collatz = (odd__collatz) + [next__odd__collatz((odd__collatz)[(len(odd__collatz)) - (1)])]
Assert(Forall(int, lambda d_5_i_:
(not (((1) <= (d_5_i_)) and ((d_5_i_) < (len(odd__collatz)))) or (((odd__collatz)[d_5_i_]) == (next__odd__collatz((odd__collatz)[(d_5_i_) - (1)]))), [[next__odd__collatz((odd__collatz)[(d_5_i_) - (1)])]])))
return odd__collatz


def get__odd__collatz(n : int) -> List[int]:
Requires((n) > (1))
Ensures(Acc(list_pred(Result())))
Ensures(Forall(int, lambda d_6_i_:
Forall(int, lambda d_7_j_:
not ((((0) <= (d_6_i_)) and ((d_6_i_) < (d_7_j_))) and ((d_7_j_) < (len(Result())))) or (((Result())[d_6_i_]) <= ((Result())[d_7_j_])))))
Ensures(Forall(int, lambda d_8_i_:
not (((0) <= (d_8_i_)) and ((d_8_i_) < (len(Result())))) or ((((Result())[d_8_i_] % 2)) == (1))))
sorted = list([int(0)] * 0) # type : List[int]
sorted = get__odd__collatz__unsorted(n)
d_12_unsorted_ = list(sorted) # type : List[int]
d_9_i_ = int(0) # type : int
d_9_i_ = 0
while (d_9_i_) < (len(sorted)):
Invariant(Acc(list_pred(sorted)))
Invariant(Acc(list_pred(d_12_unsorted_)))
Invariant(((0) <= (d_9_i_)) and ((d_9_i_) <= (len(sorted))))
Invariant(Forall(int, lambda d_8_i_:
not (((0) <= (d_8_i_)) and ((d_8_i_) < (len(sorted)))) or ((((sorted)[d_8_i_] % 2)) == (1))))
Invariant((len(sorted)) == (len(d_12_unsorted_)))
Invariant(Forall(int, lambda d_10_j_:
(Forall(int, lambda d_11_k_:
(not ((((0) <= (d_10_j_)) and ((d_10_j_) < (d_11_k_))) and ((d_11_k_) < (d_9_i_))) or (((sorted)[d_10_j_]) <= ((sorted)[d_11_k_])),
[[(sorted)[d_11_k_]]])),
[[sorted[d_10_j_]]])))
Invariant(Forall(int, lambda d_12_j_:
(not ((((0) <= (d_12_j_)) and ((d_12_j_) < (d_9_i_)))) or
(Forall(int, lambda d_13_k_:
(not ((((d_9_i_) <= (d_13_k_)) and ((d_13_k_) < (len(sorted))))) or
(((sorted)[d_12_j_]) <= ((sorted)[d_13_k_])), [[sorted[d_13_k_]]]))), [[(sorted)[d_12_j_]]])))
d_15_minIndex_ = int(0) # type : int
d_15_minIndex_ = d_9_i_
d_16_j_ = int(0) # type : int
d_16_j_ = (d_9_i_) + (1)
while (d_16_j_) < (len(sorted)):
Invariant(Acc(list_pred(sorted)))
Invariant(Acc(list_pred(d_12_unsorted_)))
Invariant((len(sorted)) == (len(d_12_unsorted_)))
Invariant(((0) <= (d_9_i_)) and ((d_9_i_) < (len(sorted))))
Invariant((((d_9_i_) <= (d_15_minIndex_)) and ((d_15_minIndex_) < (d_16_j_))) and ((d_16_j_) <= (len(sorted))))
Invariant(Forall(int, lambda d_8_i_:
not (((0) <= (d_8_i_)) and ((d_8_i_) < (len(sorted)))) or ((((sorted)[d_8_i_] % 2)) == (1))))
Invariant(Forall(int, lambda d_10_j_:
(Forall(int, lambda d_11_k_:
(not ((((0) <= (d_10_j_)) and ((d_10_j_) < (d_11_k_))) and ((d_11_k_) < (d_9_i_))) or (((sorted)[d_10_j_]) <= ((sorted)[d_11_k_])),
[[(sorted)[d_11_k_]]])),
[[sorted[d_10_j_]]])))
Invariant(Forall(int, lambda d_12_j_:
(not ((((0) <= (d_12_j_)) and ((d_12_j_) < (d_9_i_)))) or
(Forall(int, lambda d_13_k_:
(not ((((d_9_i_) <= (d_13_k_)) and ((d_13_k_) < (len(sorted))))) or
(((sorted)[d_12_j_]) <= ((sorted)[d_13_k_])), [[sorted[d_13_k_]]]))), [[(sorted)[d_12_j_]]])))
Invariant(Forall(int, lambda d_17_k_:
(not (((d_9_i_) <= (d_17_k_)) and ((d_17_k_) < (d_16_j_))) or (((sorted)[d_15_minIndex_]) <= ((sorted)[d_17_k_])), [[(sorted)[d_17_k_]]])))
if ((sorted)[d_16_j_]) < ((sorted)[d_15_minIndex_]):
d_15_minIndex_ = d_16_j_
d_16_j_ = (d_16_j_) + (1)
if (d_15_minIndex_) != (d_9_i_):
rhs0_ = (sorted)[d_9_i_] # type : int
(sorted)[d_9_i_] = (sorted)[d_15_minIndex_]
(sorted)[d_15_minIndex_] = rhs0_
d_9_i_ = (d_9_i_) + (1)
return sorted

35 changes: 35 additions & 0 deletions Bench/155_even_odd_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import List, Tuple
from nagini_contracts.contracts import *

def even__odd__count(n : int) -> Tuple[int, int]:
Requires((n) > (0))
Ensures((Result()[0]) == (even__count(n)))
Ensures((Result()[1]) == (odd__count(n)))
even = int(0) # type : int
odd = int(0) # type : int
d_0_num_ = n # type : int
while (d_0_num_) > (0):
Invariant(((0) <= (d_0_num_)))
Invariant(n > 0)
Invariant(((even) + (even__count(d_0_num_))) == (even__count(n)))
Invariant(((odd) + (odd__count(d_0_num_))) == (odd__count(n)))
even = (even) + ((d_0_num_ % 2) == 0)
odd = (odd) + (d_0_num_ % 2)
d_0_num_ = (d_0_num_ // 10)
return (even, odd)

@Pure
def odd__count(n : int) -> int :
Requires(n >= 0)
if n == 0:
return 0
else:
return (n % 2) + odd__count(n // 10)

@Pure
def even__count(n : int) -> int :
Requires(n >= 0)
if n == 0:
return 0
else:
return ((n % 2) == 0) + even__count(n // 10)
Loading

0 comments on commit bf7eec7

Please sign in to comment.