mirror of
				https://github.com/Z3Prover/z3
				synced 2025-10-31 03:32:28 +00:00 
			
		
		
		
	three smt2 examples added and one python example updated (#5690)
This commit is contained in:
		
							parent
							
								
									71cbb160d2
								
							
						
					
					
						commit
						cbdd7b0696
					
				
					 4 changed files with 445 additions and 23 deletions
				
			
		|  | @ -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() | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue