def bmc_summarize(smtin_filename="UNNAMED_in.smt2", smtout_filename="UNNAMED_out.smt2"): parser = SmtLibParser() with open(smtin_filename, 'r+') as f: smtlib = parser.get_script(f) asserts = list(smtlib.filter_by_command_name([smtcmd.ASSERT])) defs = list(smtlib.filter_by_command_name([smtcmd.DEFINE_FUN])) decls = list(smtlib.filter_by_command_name([smtcmd.DECLARE_FUN])) #print(smtlib.get_last_formula()) func_summary = None for stmt in asserts: #print(stmt) if stmt.args[0].is_iff() or stmt.args[0].is_equals(): assert stmt.args[0].arg(0).is_symbol() and stmt.args[0].arg(1) #print("Assertion on a symbolic equation.") symbol_table[stmt.args[0].arg(0).symbol_name()] = stmt.args[0].arg( 1) #pattern match for assertion on summary (PROPERTY: !(... = 0) ) if stmt.args[0].is_not(): safety_prop = stmt.args[0].arg(0) if (safety_prop.is_equals() or safety_prop.is_iff() ) and safety_prop.arg(1).is_bv_constant(0): func_summary = safety_prop.arg(0) summarizer = IdentityDagWalker() try: summary = summarizer.walk(func_summary) except: print("Could not summarize the summary.") import pdb pdb.set_trace() #Exception raised. #import pdb; pdb.set_trace() #PLAY WITH REPRESENTATION in pdb if desired #Print to stdout in SMTLib format: print(";Summary looks like:\n") print(summary.to_smtlib(False) + "\n") #Rewrite back into SMTLibScript, then print simplification back to file newscript = SmtLibScript() newscript.add(smtcmd.SET_OPTION, [':produce-models', 'true']) newscript.add(smtcmd.SET_LOGIC, ["QF_AUFBV"]) for decl in decls: newscript.add_command(decl) newscript.add( smtcmd.ASSERT, [Not(Equals(summary, BVZero(width=32))) ]) #NOTE: need the !(...=0) structure again, for the assertion newscript.add(smtcmd.CHECK_SAT, []) newscript.add(smtcmd.EXIT, []) with open(smtout_filename, 'w+') as f: newscript.serialize(f, daggify=False)
def remove_quant_and_define_funs(path): parser = SmtLibParser() script = parser.get_script_fname(path) formula = script.get_last_formula() prenex_normalizer = PrenexNormalizer() quantifications, matrix = prenex_normalizer.walk(formula) quantifier, variables = quantifications[0] assert (len(quantifications) == 1) env = get_env() mgr = env.formula_manager subs = dict((x, mgr.FreshSymbol(x.symbol_type())) for x in variables) substitued_matrix = matrix.substitute(subs) declarations = [] declarations.extend(script.filter_by_command_name("declare-sort")) declarations.extend(script.filter_by_command_name("declare-fun")) declarations.extend(script.filter_by_command_name("declare-const")) new_script = SmtLibScript() new_script.add(SET_LOGIC, [QF_NRA]) for key in subs.keys(): new = subs[key] new_script.add("declare-fun", [new]) for declaration in declarations: new_script.add_command(declaration) new_script.add("assert", [substitued_matrix]) new_script.add("check-sat", []) buf = cStringIO() new_script.serialize(buf, False) smtlib = buf.getvalue() print(smtlib)
def test_basic_operations(self): script = SmtLibScript() script.add(name=smtcmd.SET_LOGIC, args=[None]) self.assertIsNotNone(SmtLibScript()) self.assertTrue(len(script) > 0) res = script.contains_command(smtcmd.SET_LOGIC) self.assertTrue(res) res = script.contains_command(smtcmd.CHECK_SAT) self.assertFalse(res) res = script.count_command_occurrences(smtcmd.CHECK_SAT) self.assertEqual(res, 0, "Was expecting 0 occurrences of check-sat") res = script.count_command_occurrences(smtcmd.SET_LOGIC) self.assertEqual(res, 1, "Was expecting 1 occurrences of set-logic") res = script.filter_by_command_name([smtcmd.SET_LOGIC]) self.assertEqual(len(list(res)), 1)
def _get_script(self, stream, optimize): #get script parser = ExtendedSmtLibParser(environment=self._env) script = parser.get_script(stream) #the internal representation of the formula inlines define-fun formula = script.get_last_formula() declarations = [] declarations.extend(script.filter_by_command_name("declare-sort")) declarations.extend(script.filter_by_command_name("declare-fun")) declarations.extend(script.filter_by_command_name("declare-const")) new_script = SmtLibScript() for declaration in declarations: new_script.add_command(declaration) if (optimize): #remove quantifiers prenex_normalizer = PrenexNormalizer() quantifications, matrix = prenex_normalizer.walk(formula) quantifier, variables = quantifications[0] assert (quantifier == self._env.formula_manager.Exists) assert (len(quantifications) == 1) env = get_env() mgr = env.formula_manager subs = dict( (x, mgr.FreshSymbol(x.symbol_type())) for x in variables) for key in subs.keys(): new = subs[key] new_script.add("declare-fun", [new]) substitued_matrix = matrix.substitute(subs) new_script.add("assert", [substitued_matrix]) else: new_script.add("assert", [formula]) new_script.add("check-sat", []) buf2 = cStringIO() new_script.serialize(buf2, False) smtlib_data = buf2.getvalue() return new_script