From d3a4d3881f63f7667731bb902e5b7c08be541afe Mon Sep 17 00:00:00 2001 From: alex28sh Date: Mon, 9 Sep 2024 15:14:08 +0200 Subject: [PATCH] 34 --- Bench/034-unique.py | 100 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 85 insertions(+), 15 deletions(-) diff --git a/Bench/034-unique.py b/Bench/034-unique.py index 85d04a7..6a258c1 100644 --- a/Bench/034-unique.py +++ b/Bench/034-unique.py @@ -3,19 +3,27 @@ @Pure def InArray(a : List[int], x : int) -> bool: + # pre-conditions-start Requires(Acc(list_pred(a), 1/2)) + # pre-conditions-end + + # impl-start return Exists(int, lambda d_0_i_: ((((0) <= (d_0_i_)) and ((d_0_i_) < (len((a)))) and ((a)[d_0_i_]) == (x)))) + # impl-end def uniqueSorted(s : List[int]) -> List[int]: - Requires(Acc(list_pred(s))) + # pre-conditions-start + Requires(Acc(list_pred(s), 1/2)) Requires(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(s))), ((s)[d_0_i_]) <= ((s)[d_1_j_]))))) - Ensures(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start + Ensures(Acc(list_pred(s), 1/2)) Ensures(Acc(list_pred(Result()))) Ensures(Forall(int, lambda d_9_k_: (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(Result()))), InArray(s, Result()[d_9_k_]))))) @@ -24,11 +32,15 @@ def uniqueSorted(s : List[int]) -> List[int]: not ((((0) <= (d_2_i_)) and ((d_2_i_) < (d_3_j_))) and ((d_3_j_) < (len(Result())))) or (((Result())[d_2_i_]) < ((Result())[d_3_j_]))))) Ensures(Forall(int, lambda d_11_j_: (Implies(((0) <= (d_11_j_)) and ((d_11_j_) < (len(s))), InArray(Result(), s[d_11_j_]))))) + # post-conditions-end + + # impl-start result = list([int(0)] * 0) # type : List[int] result = list([]) d_6_i_ = int(0) # type : int d_6_i_ = 0 while (d_6_i_) < (len(s)): + # invariants-start Invariant(Acc(list_pred(result))) Invariant(Acc(list_pred(s), 1/2)) Invariant(Forall(int, lambda d_0_i_: @@ -48,40 +60,90 @@ def uniqueSorted(s : List[int]) -> List[int]: [[(result)[d_7_k_]]]))) Invariant(Forall(int, lambda d_11_j_: (Implies(((0) <= (d_11_j_)) and ((d_11_j_) < (d_6_i_)), InArray(result, s[d_11_j_])), [[]]))) + # invariants-end if ((len(result)) == (0)) or (((result)[(len(result)) - (1)]) != ((s)[d_6_i_])): + # assert-start Assert(Implies(len(result) > 0, result[len(result) - 1] < s[d_6_i_])) Assert(Implies(len(result) > 0, Forall(int, lambda d_11_j_: Implies(0 <= d_11_j_ and d_11_j_ < len(result), result[d_11_j_] < s[d_6_i_])))) + # assert-end result = (result) + [(s)[d_6_i_]] d_6_i_ = (d_6_i_) + (1) return result + # impl-end -# def unique(s : List[int]) -> List[int]: -# Requires(Acc(list_pred(s))) -# Ensures(Acc(list_pred(s))) -# Ensures(Acc(list_pred(Result()))) -# Ensures(Forall(int, lambda d_9_k_: -# (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(Result()))), InArray(s, Result()[d_9_k_]))))) -# Ensures(Forall(int, lambda d_2_i_: -# Forall(int, lambda d_3_j_: -# not ((((0) <= (d_2_i_)) and ((d_2_i_) < (d_3_j_))) and ((d_3_j_) < (len(Result())))) or (((Result())[d_2_i_]) < ((Result())[d_3_j_]))))) -# # Ensures(Forall(int, lambda d_11_j_: -# # (Implies(((0) <= (d_11_j_)) and ((d_11_j_) < (len(s))), InArray(Result(), s[d_11_j_]))))) -# return uniqueSorted(BubbleSort(s)) +def unique(s : List[int]) -> List[int]: + # pre-conditions-start + Requires(Acc(list_pred(s))) + # pre-conditions-end + # post-conditions-start + Ensures(Acc(list_pred(s))) + Ensures(Acc(list_pred(Result()))) + Ensures(Forall(int, lambda d_9_k_: + (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(Result()))), InArray(s, Result()[d_9_k_]))))) + Ensures(Forall(int, lambda d_2_i_: + Forall(int, lambda d_3_j_: + not ((((0) <= (d_2_i_)) and ((d_2_i_) < (d_3_j_))) and ((d_3_j_) < (len(Result())))) or (((Result())[d_2_i_]) < ((Result())[d_3_j_]))))) + # post-conditions-end + + # impl-start + a = BubbleSort(s) + # assert-start + Assert(Forall(int, lambda d_9_k_: + (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(a))), InArray(s, a[d_9_k_]))))) + # assert-end + b = uniqueSorted(a) + # assert-start + Assert(Forall(int, lambda d_9_k_: + (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(a))), InArray(s, a[d_9_k_]))))) + Assert(Forall(int, lambda d_9_k_: + (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(b))), InArray(a, b[d_9_k_]))))) + Assert(Forall(int, lambda d_9_k_: + (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(b))), InArray(s, b[d_9_k_]))))) + # assert-end + return b + # impl-end def BubbleSort(a1 : List[int]) -> List[int]: + # pre-conditions-start Requires(Acc(list_pred(a1), 1/2)) + # pre-conditions-end + # post-conditions-start Ensures(Acc(list_pred(a1), 1/2)) Ensures(Acc(list_pred(Result()))) Ensures((len(a1)) == (len(Result()))) 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())))), ((Result())[d_0_i_]) <= ((Result())[d_1_j_]))))) - a = list(a1) # type : List[int] + Ensures(Forall(int, lambda d_9_k_: + (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(Result()))), InArray(a1, Result()[d_9_k_]))))) + # post-conditions-end + + # impl-start + a : List[int] = [] d_2_i_ = int(0) # type : int + while d_2_i_ < len(a1): + # invariants-start + Invariant(Acc(list_pred(a))) + Invariant(Acc(list_pred(a1), 1/2)) + Invariant(len(a) == d_2_i_) + Invariant(0 <= d_2_i_ and d_2_i_ <= len(a1)) + Invariant(Forall(int, lambda d_9_k_: + (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (d_2_i_)), a[d_9_k_] == a1[d_9_k_])))) + Invariant(Forall(int, lambda d_9_k_: + (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (d_2_i_)), InArray(a1, a[d_9_k_])), [[InArray(a1, a[d_9_k_])]]))) + Invariant(Forall(int, lambda d_9_k_: + (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (d_2_i_)), InArray(a, a1[d_9_k_])), [[InArray(a, a1[d_9_k_])]]))) + # invariants-end + a = a + [a1[d_2_i_]] + # assert-start + Assert(InArray(a1, a[d_2_i_])) + # assert-end + d_2_i_ = d_2_i_ + 1 d_2_i_ = (len((a))) - (1) while (d_2_i_) > (0): + # invariants-start Invariant(Acc(list_pred(a))) Invariant(Acc(list_pred(a1), 1/2)) Invariant((len(a1)) == (len(a))) @@ -97,9 +159,13 @@ def BubbleSort(a1 : List[int]) -> List[int]: (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)))), ((a)[d_5_k_]) <= ((a)[d_6_k_k_])), [[(a)[d_6_k_k_]]])), [[(a)[d_5_k_]]]))) + Invariant(Forall(int, lambda d_9_k_: + (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(a))), InArray(a1, a[d_9_k_])), [[InArray(a1, a[d_9_k_])]]))) + # invariants-end d_7_j_ = int(0) # type : int d_7_j_ = 0 while (d_7_j_) < (d_2_i_): + # invariants-start Invariant(Acc(list_pred(a))) Invariant(Acc(list_pred(a1), 1/2)) Invariant((len(a1)) == (len(a))) @@ -117,6 +183,9 @@ def BubbleSort(a1 : List[int]) -> List[int]: Invariant(Forall(int, lambda d_12_k_: (Implies(((0) <= (d_12_k_)) and ((d_12_k_) <= (d_7_j_)), ((a)[d_12_k_]) <= ((a)[d_7_j_])), [[(a)[d_12_k_]]]))) + Invariant(Forall(int, lambda d_9_k_: + (Implies(((0) <= (d_9_k_)) and ((d_9_k_) < (len(a))), InArray(a1, a[d_9_k_])), [[InArray(a1, a[d_9_k_])]]))) + # invariants-end if ((a)[d_7_j_]) > ((a)[(d_7_j_) + (1)]): rhs0_ = (a)[(d_7_j_) + (1)] # type : int (a)[(d_7_j_) + (1)] = (a)[d_7_j_] @@ -124,3 +193,4 @@ def BubbleSort(a1 : List[int]) -> List[int]: d_7_j_ = (d_7_j_) + (1) d_2_i_ = (d_2_i_) - (1) return a + # impl-end