def test_synthesis_simple(self): grammar = templates.load_gramar_from_SYGUS_spec("((Start Int (x y)))") h = smt_synthesis.HoleDecl('H0', grammar, None, True, 2) code = "(= res (* 2 (+ H0 y)))" env = utils.Options({ '--solver': 'z3', '--logic': 'NIA', '--silent': 1, '--lang': 'smt2', '--output_data': 'holes_content', '--solver_timeout': '2000' }) vars = ProgramVars({'x': 'Int', 'y': 'Int'}, {'res': 'Int'}) # Trivial case with the postcondition being always true. res = smt_synthesis.synthesize(code, 'true', 'true', vars, env, [h]) self.assertEquals('sat', res.decision) # Slightly less trivial case. res = smt_synthesis.synthesize(code, 'true', '(= res (+ (* 2 x) (* 2 y)))', vars, env, [h]) self.assertEquals('sat', res.decision) self.assertEquals('0', res.model['H0Start0_r0']) self.assertEquals('x', res.holes_content['H0']) self.assertEquals("{'H0': 'x'}", res.str_formatted()) # Case for which UNSAT is the expected answer. res = smt_synthesis.synthesize(code, 'true', '(= res 0)', vars, env, [h]) self.assertEquals('unsat', res.decision)
def test_synthesis_instruction_hole(self): code = "H0" grammar = templates.load_gramar_from_SYGUS_spec("((Start Int (x y)))") h = smt_synthesis.HoleDecl('H0', grammar, None, True, 2) env = utils.Options({ '--logic': 'NIA', '--silent': 1, '--lang': 'python', '--output_data': 'holes_content' }) vars = ProgramVars({'x': 'Int', 'y': 'Int'}, {'res': 'Int'}) with self.assertRaises(Exception) as context: smt_synthesis.synthesize(code, 'True', 'True', vars, env, [h]) self.assertEquals("Instruction holes are currently not supported!", str(context.exception))
def run_from_options(env): """Runs synthesis or verification depending on the provided options.""" program_vars = get_program_vars(env) if env.verify: res = smt_verifier.verify(env.program, env.pre, env.post, program_vars, env) # Printing the result. print(res.str_formatted()) if not env.silent: # Additional commentary if res.decision == 'unsat': print('Counterexample not found! Program is correct.') elif res.decision == 'sat': print('Counterexample found! Program is incorrect.') return res elif env.example: res = smt_verifier.find_example(env.program, env.pre, env.post, program_vars, env) # Printing the result. print(res.str_formatted()) if not env.silent: # Additional commentary if res.decision == 'unsat': print( 'Example not found! Program is incorrect for all inputs.') elif res.decision == 'sat': print( 'Example found! Program is correct for at least one input.' ) return res elif env.synthesize: holes = smt_synthesis.HoleDecl.many_from_str(env.synth_holes) if env.test_cases == "": res = smt_synthesis.synthesize(env.program, env.pre, env.post, program_vars, env, holes, free_vars=env.free_vars) else: test_cases = contract.TestCases.from_str(env.test_cases) res = smt_synthesis.synthesize_tc(test_cases, env.program, env.pre, env.post, program_vars, env, holes, free_vars=env.free_vars) # Printing the result. print(res.str_formatted()) return res else: print( "Task was not specified! Use either --verify, --example or --synthesize." ) exit()
def synthesize_max(): code = """ if H1: res = H2 else: res = H3 """ code_pre = 'True' code_post = 'res >= x and res >= y and (res == x or res == y)' # Specification of the hole's template in the form of the grammar in SYGUS format. sygus_grammar_hole1 = """ ( ( Start Bool ( (Constant Bool) (> TermInt TermInt) (>= TermInt TermInt) (= TermInt TermInt) (<= TermInt TermInt) (< TermInt TermInt) ) ) ( TermInt Int ( (Constant Int) x y ) ) ) """ sygus_grammar_hole23 = """ ( ( Start Int ( (Constant Int) x y (+ x y) (- x y) (- y x) (+ x ( Constant Int )) (+ y ( Constant Int )) ) ) ) """ grammar1 = templates.load_gramar_from_SYGUS_spec(sygus_grammar_hole1) grammar23 = templates.load_gramar_from_SYGUS_spec(sygus_grammar_hole23) pv = contract.ProgramVars({'x': 'Int', 'y': 'Int'}, {'res': 'Int'}) h1 = smt_synthesis.HoleDecl('H1', grammar1, pv, True, 2) h2 = smt_synthesis.HoleDecl('H2', grammar23, pv, True, 2) h3 = smt_synthesis.HoleDecl('H3', grammar23, pv, True, 2) hole_decls = [h1, h2, h3] # The result is currently only a raw output from the solver, but one can verify from the model # that synthesized program is correct. env = utils.Options(['--solver', 'z3', '--logic', 'NIA']) res = smt_synthesis.synthesize(code, code_pre, code_post, pv, env, hole_decls) return res
def test_synthesis_recursive_grammar(self): code = """ if x > y: res = HOLE2 else: res = HOLE3 """ vars = ProgramVars({'x': 'Int', 'y': 'Int'}, {'res': 'Int'}) code_pre = 'True' code_post = 'res >= x and res >= y and (res == x or res == y)' sygus_grammar_hole23 = """ ( ( Start Int ( ( Constant Int ) x y (+ Start Start) (- Start Start) (- y x) (+ x ( Constant Int )) (* Start Start) ) ) ) """ grammar23 = templates.load_gramar_from_SYGUS_spec(sygus_grammar_hole23) h2 = smt_synthesis.HoleDecl('HOLE2', grammar23, None, True, 2) h3 = smt_synthesis.HoleDecl('HOLE3', grammar23, None, True, 2) hole_decls = [h2, h3] assertions = [] #['(assert (= HOLE3_r0 3))'] env = utils.Options({ '--solver': 'z3', '--solver_interactive_mode': 0, '--logic': 'NIA', '--silent': 0, '--solver_timeout': '2000' }) res = smt_synthesis.synthesize(code, code_pre, code_post, vars, env, hole_decls, assertions) print('[test_synthesis_recursive_grammar] RES:') print(res.text) print('[test_synthesis_recursive_grammar] SYNTHESIZED CODE:') print(res.final_code) self.assertTrue(res.decision == 'sat' or res.decision == "unknown")