3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-27 02:45:51 +00:00

three smt2 examples added and one python example updated (#5690)

This commit is contained in:
Matteo Nicoli 2021-12-02 01:21:12 +01:00 committed by GitHub
parent 71cbb160d2
commit cbdd7b0696
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 445 additions and 23 deletions

View file

@ -1,4 +1,6 @@
# BUBBLESORT - Copyright (c) June, 2020 - Matteo Nicoli
# File name: bubble_sort.py
#
# BUBBLESORT - Copyright (c) June, 2020 - Matteo Nicoli
from z3 import Solver, Int, Array, IntSort, And, Not, If, Select, Store, sat
@ -36,34 +38,56 @@ def check(variables, Ar, dim) :
yield variables[e] == Select(Ar,e)
def mk_post_condition(values) :
condition = []
for v1,v2 in zip(values,values[1:]) :
condition.append(v1 <= v2)
condition = [v1 <= v2 for v1,v2 in zip(values,values[1:])]
return And(*condition)
dim = int(input("size of the array: "))
i = [Int(f"i_{x}") for x in range(dim + 1)]
j = [Int(f"j_{x}") for x in range(dim + 1)]
A = [Array(f"A_{x}",IntSort(),IntSort()) for x in range(dim + 1)]
tmp = [Int(f"tmp_{x}") for x in range(dim)]
def main() :
dim = int(input("size of the array: "))
s = Solver()
i = [Int(f"i_{x}") for x in range(dim + 1)]
j = [Int(f"j_{x}") for x in range(dim + 1)]
A = [Array(f"A_{x}",IntSort(),IntSort()) for x in range(dim + 1)]
tmp = [Int(f"tmp_{x}") for x in range(dim)]
init_condition = init(i[0],j[0])
s.add(init_condition)
s = Solver()
tran_condition= mk_tran_condition(A, i, j, tmp, dim)
s.add(And(*tran_condition))
init_condition = init(i[0],j[0])
s.add(init_condition)
values = [Int(f"n_{x}") for x in range(dim)]
init_check_condition = check(values,A[-1],dim)
s.add(And(*init_check_condition))
tran_condition = mk_tran_condition(A, i, j, tmp, dim)
s.add(And(*tran_condition))
post_condition = mk_post_condition(values)
s.add(Not(post_condition))
values = [Int(f"n_{x}") for x in range(dim)]
init_check_condition = check(values,A[-1],dim)
s.add(And(*init_check_condition))
if s.check() == sat :
print(f"counterexample:\n{s.model()}")
else :
print("valid")
post_condition = mk_post_condition(values)
print("Bubble sort")
print("---------------------")
s.push()
s.add(Not(post_condition))
print("Testing the validity of the algorithm; `valid expected`:")
if s.check() == sat :
print(f"counterexample:\n{s.model()}")
else :
print("valid")
print("---------------------")
s.pop()
s.add(post_condition)
print("Getting a model...")
print("Model:")
if s.check() == sat :
print(s.model())
if __name__ == "__main__" :
main()