def test_stack_env(self): env1 = get_env() push_env() push_env(env1) self.assertEqual(env1, pop_env(), "Pushed environment was ignored.") env2 = get_env() self.assertIsNotNone(env2) self.assertNotEqual(env1, pop_env(), "New environment was not created.")
def test_simplify_q(self): simp = get_env().simplifier Iff = get_env().formula_manager.Iff for (f, _, _, logic) in get_example_formulae(): if logic.quantifier_free: continue simp.validate_simplifications = True sf = f.simplify() simp.validate_simplifications = False self.assertValid(Iff(f, sf), solver_name='z3', msg="Simplification did not provide equivalent "+ "result:\n f= %s\n sf = %s" % (f, sf))
def test_with_env(self): env1 = get_env() a1 = Symbol("A", REAL) with Environment(): env2 = get_env() self.assertIsNotNone(env2, "Context should create an environment") self.assertNotEqual(env1, env2, "Context should create a new environment") a2 = Symbol("A", REAL) self.assertNotEqual(a1, a2, "Symbols in different context should differ") a3 = Symbol("A", REAL) self.assertEqual(a1, a3, "Exiting a context should restore the previous environment")
def setUp(self): super(TestSimpleTypeChecker, self).setUp() self.tc = get_env().stc self.x = Symbol("x", BOOL) self.y = Symbol("y", BOOL) self.p = Symbol("p", INT) self.q = Symbol("q", INT) self.r = Symbol("r", REAL) self.s = Symbol("s", REAL) self.qfo = get_env().qfo
def test_simplify_q(self): simp = get_env().simplifier Iff = get_env().formula_manager.Iff for (f, _, _, logic) in get_example_formulae(): if logic.quantifier_free: continue simp.validate_simplifications = True sf = f.simplify() simp.validate_simplifications = False self.assertValid(Iff(f, sf), solver_name='z3', msg="Simplification did not provide equivalent " + "result:\n f= %s\n sf = %s" % (f, sf))
def get_last_formula(self, mgr=None): """Returns the last formula of the execution of the Script. This coincides with the conjunction of the assertions that are left on the assertion stack at the end of the SMTLibScript. """ stack = [] backtrack = [] _And = mgr.And if mgr else get_env().formula_manager.And for cmd in self.commands: if cmd.name == smtcmd.ASSERT: stack.append(cmd.args[0]) if cmd.name == smtcmd.RESET_ASSERTIONS: stack = [] backtrack = [] elif cmd.name == smtcmd.PUSH: for _ in xrange(cmd.args[0]): backtrack.append(len(stack)) elif cmd.name == smtcmd.POP: for _ in xrange(cmd.args[0]): l = backtrack.pop() stack = stack[:l] return _And(stack)
def Solver(name: str, logic: tp.Optional[logics.Logic] = None) -> SolverT: ''' Convience function for building a pysmt solver object with a switch backend. Similar to `pysmt.shortcuts.Solver`. ''' try: cls = SWITCH_SOLVERS[name] except KeyError: raise NoSolverAvailableError( f"Solver '{name}' is not available") from None if isinstance(logic, str): logic = logics.convert_logic_from_string(logic) elif logic is None: # Try to use the most general logic supported try: logic = logics.most_generic_logic(cls.LOGICS) except NoLogicAvailableError: # supported logics do not have a single "upper bound" # Check for some reasonable ones if logics.QF_UFLIRA in cls.LOGICS: logic = logics.QF_UFLIRA elif logics.QF_AUFBV in cls.LOGICS: logic = logics.QF_AUFBV else: # use the first one logic = cls.LOGICS[0] return cls(environment=get_env(), logic=logic)
def test_solver_factory_preferences(self): env = get_env() factory = env.factory self.assertEqual(factory.solver_preference_list, pysmt.factory.DEFAULT_SOLVER_PREFERENCE_LIST) for solver_name in factory.all_solvers(logic=logics.QF_UFLIRA): factory.set_solver_preference_list([solver_name]) self.assertEqual(factory.solver_preference_list, [solver_name]) solver = factory.get_solver(logic=logics.QF_UFLIRA) self.assertTrue(isinstance(solver, factory.all_solvers()[solver_name])) factory.set_solver_preference_list(['nosolver']) with self.assertRaises(NoSolverAvailableError): factory.get_solver() for qelim_name in factory.all_quantifier_eliminators(): factory.set_qelim_preference_list([qelim_name]) self.assertEqual(factory.qelim_preference_list, [qelim_name]) qelim = factory.get_quantifier_eliminator(logic=logics.BOOL) self.assertTrue(isinstance(qelim, factory.all_quantifier_eliminators()[qelim_name])) factory.set_qelim_preference_list(['nosolver']) with self.assertRaises(NoSolverAvailableError): factory.get_quantifier_eliminator()
def test_cannot_replace_global_walkers(self): env = get_env() # Check that environment contains standard walkers self.assertIsNotNone(env.formula_manager) self.assertIsNotNone(env.substituter) self.assertIsNotNone(env.simplifier) self.assertIsNotNone(env.serializer) self.assertIsNotNone(env.stc) # Cannot modify these elements with self.assertRaises(AttributeError): env.formula_manager = None with self.assertRaises(AttributeError): env.substituter = None with self.assertRaises(AttributeError): env.simplifier = None with self.assertRaises(AttributeError): env.serializer = None with self.assertRaises(AttributeError): env.stc = None
def from_graph(cls, graph, linear_energy_ranges, quadratic_energy_ranges): """Create Theta from a graph and energy ranges. Args: graph (:obj:`networkx.Graph`): Provides the structure for Theta. linear_energy_ranges (dict): A dict of the form {v: (min, max), ...} where min and max are the range of values allowed to v. quadratic_energy_ranges (dict): A dict of the form {(u, v): (min, max), ...} where min and max are the range of values allowed to (u, v). Returns: :obj:`.Theta` """ get_env( ).enable_infix_notation = True # not sure why we need this here theta = cls.empty(dimod.SPIN) theta.add_offset(Symbol('offset', REAL)) def Linear(v): """Create a Symbol for the linear bias including the energy range constraints.""" bias = Symbol('h_{}'.format(v), REAL) min_, max_ = linear_energy_ranges[v] theta.assertions.add(LE(bias, limitReal(max_))) theta.assertions.add(GE(bias, limitReal(min_))) return bias def Quadratic(u, v): """Create a Symbol for the quadratic bias including the energy range constraints.""" bias = Symbol('J_{},{}'.format(u, v), REAL) if (v, u) in quadratic_energy_ranges: min_, max_ = quadratic_energy_ranges[(v, u)] else: min_, max_ = quadratic_energy_ranges[(u, v)] theta.assertions.add(LE(bias, limitReal(max_))) theta.assertions.add(GE(bias, limitReal(min_))) return bias for v in graph.nodes: theta.add_variable(v, Linear(v)) for u, v in graph.edges: theta.add_interaction(u, v, Quadratic(u, v)) return theta
def __init__(self, assignment, environment=None): if environment is None: environment = get_env() Model.__init__(self, environment) self.environment = environment self.assignment = assignment # Create a copy of the assignments to memoize completions self.completed_assignment = dict(self.assignment)
def __call__(self, test_fun): msg = "Quantifier Eliminator for %s not available" % self.logic cond = len(get_env().factory.all_quantifier_eliminators(logic=self.logic)) == 0 @unittest.skipIf(cond, msg) @wraps(test_fun) def wrapper(*args, **kwargs): return test_fun(*args, **kwargs) return wrapper
def __init__(self, assignment, environment=None): if environment is None: environment = get_env() Model.__init__(self, environment) self.environment = environment self.assignment = dict(assignment) # Create a copy of the assignments to memoize completions self.completed_assignment = dict(self.assignment)
def __call__(self, test_fun): msg = "%s not available" % self.solver cond = self.solver not in get_env().factory.all_solvers() @unittest.skipIf(cond, msg) @wraps(test_fun) def wrapper(*args, **kwargs): return test_fun(*args, **kwargs) return wrapper
def __call__(self, test_fun): msg = "Quantifier Eliminator %s not available" % self.qe cond = self.qe not in get_env().factory.all_quantifier_eliminators() @unittest.skipIf(cond, msg) @wraps(test_fun) def wrapper(*args, **kwargs): return test_fun(*args, **kwargs) return wrapper
def __call__(self, test_fun): msg = "Solver for %s not available" % self.logic cond = not get_env().factory.has_solvers(logic=self.logic) @unittest.skipIf(cond, msg) @wraps(test_fun) def wrapper(*args, **kwargs): return test_fun(*args, **kwargs) return wrapper
def __call__(self, test_fun): msg = "Unsat Core Solver for %s not available" % self.logic cond = len(get_env().factory.all_unsat_core_solvers(logic=self.logic)) == 0 @unittest.skipIf(cond, msg) @wraps(test_fun) def wrapper(*args, **kwargs): return test_fun(*args, **kwargs) return wrapper
def check_installed(required_solvers, install_dir, bindings_dir, mirror_link): """Checks which solvers are visible to pySMT.""" # Check which solvers are accessible from the Factory pypath_solvers = get_env().factory.all_solvers() global_solvers_status = [] print("Installed Solvers:") for i in INSTALLERS: installer_ = i.InstallerClass(install_dir=install_dir, bindings_dir=bindings_dir, solver_version=i.version, mirror_link=mirror_link, **i.extra_params) solver = installer_.SOLVER version = installer_.get_installed_version() is_installed = (version is not None) global_solvers_status.append((solver, is_installed, version)) del installer_ for solver in required_solvers: if solver not in pypath_solvers: raise PysmtException("Was expecting to find %s installed" % solver) # # Output information # for (solver, is_installed, version) in global_solvers_status: msg = " %s%s " % (solver.ljust(10), is_installed) msg += ("(%s)" % version).ljust(20) if solver not in pypath_solvers: msg += "Not in Python's path!" print(msg) print("") print("Solvers: %s" % ", ".join(name for name in pypath_solvers)) qes = get_env().factory.all_quantifier_eliminators() print("Quantifier Eliminators: %s" % ", ".join(name for name in qes)) ucs = get_env().factory.all_unsat_core_solvers() print("UNSAT-Cores: %s" % ", ".join(name for name in ucs)) interps = get_env().factory.all_interpolators() print("Interpolators: %s" % ", ".join(name for name in interps))
def __init__(self, stream, annotations=None): TreeWalker.__init__(self) self.stream = stream self.write = self.stream.write self.mgr = get_env().formula_manager if not annotations: self.annotations = Annotations() else: self.annotations = annotations
def __init__(self, stream, template=".def_%d"): DagWalker.__init__(self, invalidate_memoization=True) self.stream = stream self.write = self.stream.write self.openings = 0 self.name_seed = 0 self.template = template self.names = None self.mgr = get_env().formula_manager
def __init__(self, env=None): if env is None: env = get_env() self.env = env self.mgr = env.formula_manager self.get_type = env.stc.get_type self.rules = [] self.scanner = None self.eoi = EndOfInput()
def get_strict_formula(self, mgr=None): if self.contains_command(smtcmd.PUSH) or \ self.contains_command(smtcmd.POP): raise Exception("Was not expecting push-pop commands") assert self.count_command_occurrences(smtcmd.CHECK_SAT) == 1 _And = mgr.And if mgr else get_env().formula_manager.And assertions = [cmd.args[0] for cmd in self.filter_by_command_name([smtcmd.ASSERT])] return _And(assertions)
def skipIfNoSolverAvailable(test_fun): """Skip the test if no solver is available.""" msg = "No solver available" cond = len(get_env().factory.all_solvers()) == 0 @unittest.skipIf(cond, msg) @wraps(test_fun) def wrapper(self, *args, **kwargs): return test_fun(self, *args, **kwargs) return wrapper
def __init__(self, stream, template=".def_%d"): DagWalker.__init__(self, invalidate_memoization=True) self.stream = stream self.write = self.stream.write self.openings = 0 self.name_seed = 0 self.template = template self.names = None self.mgr = get_env().formula_manager self.set_function(partial(self._walk_nary, "and"), op.AND) self.set_function(partial(self._walk_nary, "or"), op.OR) self.set_function(partial(self._walk_nary, "not"), op.NOT) self.set_function(partial(self._walk_nary, "=>"), op.IMPLIES) self.set_function(partial(self._walk_nary, "="), op.IFF) self.set_function(partial(self._walk_nary, "+"), op.PLUS) self.set_function(partial(self._walk_nary, "-"), op.MINUS) self.set_function(partial(self._walk_nary, "*"), op.TIMES) self.set_function(partial(self._walk_nary, "pow"), op.POW) self.set_function(partial(self._walk_nary, "="), op.EQUALS) self.set_function(partial(self._walk_nary, "<="), op.LE) self.set_function(partial(self._walk_nary, "<"), op.LT) self.set_function(partial(self._walk_nary, "ite"), op.ITE) self.set_function(partial(self._walk_nary, "to_real"), op.TOREAL) self.set_function(partial(self._walk_nary, "/"), op.DIV) self.set_function(partial(self._walk_nary, "bvand"), op.BV_AND) self.set_function(partial(self._walk_nary, "bvor"), op.BV_OR) self.set_function(partial(self._walk_nary, "bvnot"), op.BV_NOT) self.set_function(partial(self._walk_nary, "bvxor"), op.BV_XOR) self.set_function(partial(self._walk_nary, "bvadd"), op.BV_ADD) self.set_function(partial(self._walk_nary, "bvsub"), op.BV_SUB) self.set_function(partial(self._walk_nary, "bvneg"), op.BV_NEG) self.set_function(partial(self._walk_nary, "bvmul"), op.BV_MUL) self.set_function(partial(self._walk_nary, "bvudiv"), op.BV_UDIV) self.set_function(partial(self._walk_nary, "bvurem"), op.BV_UREM) self.set_function(partial(self._walk_nary, "bvshl"), op.BV_LSHL) self.set_function(partial(self._walk_nary, "bvlshr"), op.BV_LSHR) self.set_function(partial(self._walk_nary, "bvult"), op.BV_ULT) self.set_function(partial(self._walk_nary, "bvule"), op.BV_ULE) self.set_function(partial(self._walk_nary, "bvslt"), op.BV_SLT) self.set_function(partial(self._walk_nary, "bvsle"), op.BV_SLE) self.set_function(partial(self._walk_nary, "concat"), op.BV_CONCAT) self.set_function(partial(self._walk_nary, "bvcomp"), op.BV_COMP) self.set_function(partial(self._walk_nary, "bvashr"), op.BV_ASHR) self.set_function(partial(self._walk_nary, "bvsdiv"), op.BV_SDIV) self.set_function(partial(self._walk_nary, "bvsrem"), op.BV_SREM) self.set_function(self.walk_bv_extract, op.BV_EXTRACT) self.set_function(self.walk_bv_rotate, op.BV_ROR) self.set_function(self.walk_bv_rotate, op.BV_ROL) self.set_function(self.walk_bv_extend, op.BV_SEXT) self.set_function(self.walk_bv_extend, op.BV_ZEXT) self.set_function(partial(self._walk_nary, "select"), op.ARRAY_SELECT) self.set_function(partial(self._walk_nary, "store"), op.ARRAY_STORE)
def _run_solver(idx, solver, logic, options, formula, signaling_queue, ctrl_pipe): """Function used by the child Process to handle Portfolio requests. solver : name of the solver options : options for the solver formula : formula to assert signaling_queue: queue in which to write to indicate completion of solve ctrl_pipe: Pipe to communicate with parent process *after* solve """ from pysmt.environment import get_env Solver = get_env().factory.Solver with Solver(name=solver, logic=logic, **options) as s: s.add_assertion(formula) try: local_res = s.solve() except Exception as ex: signaling_queue.put((solver, ex)) return signaling_queue.put((idx, local_res)) _exit = False while not _exit: try: cmd = ctrl_pipe.recv() except EOFError: break if type(cmd) == tuple: cmd, args = cmd if cmd == "exit": _exit = True elif cmd == "get_model": # MG: Can we pickle the EagerModel directly? # Note: contextualization happens on the receiver side model = list(s.get_model()) ctrl_pipe.send(model) elif cmd == "get_value": args = get_env().formula_manager.normalize(args) ctrl_pipe.send(s.get_value(args)) else: raise ValueError("Unknown command '%s'" % cmd)
def __init__(self, LexerClass, env=None): if env is None: env = get_env() self.env = env self.mgr = env.formula_manager self.get_type = env.stc.get_type self.lexer = LexerClass(env) self.token = None self.tokenizer = None
def test_boolean(self): varA = Symbol("At", INT) varB = Symbol("Bt", INT) f = And(LT(varA, Plus(varB, Int(1))), GT(varA, Minus(varB, Int(1)))) g = Equals(varA, varB) h = Iff(f, g) tc = get_env().stc res = tc.walk(h) self.assertEqual(res, BOOL)
def get_strict_formula(self, mgr=None): if self.contains_command(smtcmd.PUSH) or \ self.contains_command(smtcmd.POP): raise PysmtValueError("Was not expecting push-pop commands") if self.count_command_occurrences(smtcmd.CHECK_SAT) != 1: raise PysmtValueError("Was expecting exactly one check-sat command") _And = mgr.And if mgr else get_env().formula_manager.And assertions = [cmd.args[0] for cmd in self.filter_by_command_name([smtcmd.ASSERT])] return _And(assertions)
def __call__(self, test_fun): msg = "Quantifier Eliminator for %s not available" % self.logic cond = len(get_env().factory.all_quantifier_eliminators( logic=self.logic)) == 0 @unittest.skipIf(cond, msg) @wraps(test_fun) def wrapper(*args, **kwargs): return test_fun(*args, **kwargs) return wrapper
def test_simplify_qf(self): simp = get_env().simplifier for (f, _, _, logic) in get_example_formulae(): if logic.is_quantified(): continue sname = "z3" if not logic.theory.strings else "cvc4" simp.validate_simplifications = sname sf = f.simplify() simp.validate_simplifications = None self.assertValid(Iff(f, sf), solver_name=sname, msg="Simplification did not provide equivalent "+ "result:\n f= %s\n sf = %s" % (f, sf))
def __call__(self, test_fun): msg = "Unsat Core Solver for %s not available" % self.logic cond = len( get_env().factory.all_unsat_core_solvers(logic=self.logic)) == 0 @unittest.skipIf(cond, msg) @wraps(test_fun) def wrapper(*args, **kwargs): return test_fun(*args, **kwargs) return wrapper
def _run_solver(idx, solver, options, formula, signaling_queue, ctrl_pipe): """Function used by the child Process to handle Portfolio requests. solver : name of the solver options : options for the solver formula : formula to assert signaling_queue: queue in which to write to indicate completion of solve ctrl_pipe: Pipe to communicate with parent process *after* solve """ from pysmt.environment import get_env Solver = get_env().factory.Solver with Solver(name=solver, **options) as s: s.add_assertion(formula) try: local_res = s.solve() except Exception as ex: signaling_queue.put((solver, ex)) return signaling_queue.put((idx, local_res)) _exit = False while not _exit: try: cmd = ctrl_pipe.recv() except EOFError: break if type(cmd) == tuple: cmd, args = cmd if cmd == "exit": _exit = True elif cmd == "get_model": # MG: Can we pickle the EagerModel directly? # Note: contextualization happens on the receiver side model = list(s.get_model()) ctrl_pipe.send(model) elif cmd == "get_value": args = get_env().formula_manager.normalize(args) ctrl_pipe.send(s.get_value(args)) else: raise ValueError("Unknown command '%s'" % cmd)
def __init__(self, environment=None): self.pysmt_env = get_env() if environment is None else environment # Placeholders for fields filled by self._reset self._current_env = None self.cache = None self.logic = None self._reset() # Special tokens appearing in expressions self.parentheses = set(["(", ")"]) self.specials = set(["let", "!", "exists", "forall"])
def test_simplify_qf(self): simp = get_env().simplifier for (f, _, _, logic) in get_example_formulae(): if logic.is_quantified(): continue sname = "z3" if not logic.theory.strings else "cvc4" simp.validate_simplifications = sname sf = f.simplify() simp.validate_simplifications = None self.assertValid(Iff(f, sf), solver_name=sname, msg="Simplification did not provide equivalent " + "result:\n f= %s\n sf = %s" % (f, sf))
def get_strict_formula(self, mgr=None): if self.contains_command(smtcmd.PUSH) or \ self.contains_command(smtcmd.POP): raise PysmtValueError("Was not expecting push-pop commands") if self.count_command_occurrences(smtcmd.CHECK_SAT) != 1: raise PysmtValueError( "Was expecting exactly one check-sat command") _And = mgr.And if mgr else get_env().formula_manager.And assertions = [ cmd.args[0] for cmd in self.filter_by_command_name([smtcmd.ASSERT]) ] return _And(assertions)
def test_plus_algebraic(self): from pysmt.constants import Numeral env = get_env() mgr = env.formula_manager r0 = Symbol("r0", REAL) p_2 = Real(2) m_5 = mgr._Algebraic(Numeral(-5)) m_3 = mgr._Algebraic(Numeral(-3)) # r0 + 2 - 5 expr = Plus(r0, p_2, m_5) res = expr.simplify() self.assertValid(Equals(expr, res)) self.assertIn(m_3, res.args())
def test_times_algebraic(self): from pysmt.constants import Numeral env = get_env() mgr = env.formula_manager r0 = Symbol("r0", REAL) p_2 = Real(2) m_5 = mgr._Algebraic(Numeral(-5)) m_10 = mgr._Algebraic(Numeral(-10)) # -5 * r0 * 2 expr = Times(m_5, r0, p_2) res = expr.simplify() self.assertValid(Equals(expr, res)) self.assertIn(m_10, res.args())
def smtlibscript_from_formula(formula, logic=None): script = SmtLibScript() if logic is None: # Get the simplest SmtLib logic that contains the formula f_logic = get_logic(formula) smt_logic = None try: smt_logic = get_closer_smtlib_logic(f_logic) except NoLogicAvailableError: warnings.warn("The logic %s is not reducible to any SMTLib2 " \ "standard logic. Proceeding with non-standard " \ "logic '%s'" % (f_logic, f_logic), stacklevel=3) smt_logic = f_logic elif not (isinstance(logic, Logic) or isinstance(logic, str)): raise UndefinedLogicError(str(logic)) else: if logic not in SMTLIB2_LOGICS: warnings.warn("The logic %s is not reducible to any SMTLib2 " \ "standard logic. Proceeding with non-standard " \ "logic '%s'" % (logic, logic), stacklevel=3) smt_logic = logic script.add(name=smtcmd.SET_LOGIC, args=[smt_logic]) # Declare all types types = get_env().typeso.get_types(formula, custom_only=True) for type_ in types: script.add(name=smtcmd.DECLARE_SORT, args=[type_.decl]) deps = formula.get_free_variables() # Declare all variables for symbol in deps: assert symbol.is_symbol() script.add(name=smtcmd.DECLARE_FUN, args=[symbol]) # Assert formula script.add_command(SmtLibCommand(name=smtcmd.ASSERT, args=[formula])) # check-sat script.add_command(SmtLibCommand(name=smtcmd.CHECK_SAT, args=[])) return script
def __init__(self, stream): TreeWalker.__init__(self) self.stream = stream self.write = self.stream.write self.mgr = get_env().formula_manager self.set_function(partial(self._walk_nary, "and"), op.AND) self.set_function(partial(self._walk_nary, "or"), op.OR) self.set_function(partial(self._walk_nary, "not"), op.NOT) self.set_function(partial(self._walk_nary, "=>"), op.IMPLIES) self.set_function(partial(self._walk_nary, "="), op.IFF) self.set_function(partial(self._walk_nary, "+"), op.PLUS) self.set_function(partial(self._walk_nary, "-"), op.MINUS) self.set_function(partial(self._walk_nary, "*"), op.TIMES) self.set_function(partial(self._walk_nary, "="), op.EQUALS) self.set_function(partial(self._walk_nary, "<="), op.LE) self.set_function(partial(self._walk_nary, "<"), op.LT) self.set_function(partial(self._walk_nary, "ite"), op.ITE) self.set_function(partial(self._walk_nary, "to_real"), op.TOREAL) self.set_function(partial(self._walk_nary, "bvand"), op.BV_AND) self.set_function(partial(self._walk_nary, "bvor"), op.BV_OR) self.set_function(partial(self._walk_nary, "bvnot"), op.BV_NOT) self.set_function(partial(self._walk_nary, "bvxor"), op.BV_XOR) self.set_function(partial(self._walk_nary, "bvadd"), op.BV_ADD) self.set_function(partial(self._walk_nary, "bvsub"), op.BV_SUB) self.set_function(partial(self._walk_nary, "bvneg"), op.BV_NEG) self.set_function(partial(self._walk_nary, "bvmul"), op.BV_MUL) self.set_function(partial(self._walk_nary, "bvudiv"), op.BV_UDIV) self.set_function(partial(self._walk_nary, "bvurem"), op.BV_UREM) self.set_function(partial(self._walk_nary, "bvshl"), op.BV_LSHL) self.set_function(partial(self._walk_nary, "bvlshr"), op.BV_LSHR) self.set_function(partial(self._walk_nary, "bvult"), op.BV_ULT) self.set_function(partial(self._walk_nary, "bvule"), op.BV_ULE) self.set_function(partial(self._walk_nary, "bvslt"), op.BV_SLT) self.set_function(partial(self._walk_nary, "bvsle"), op.BV_SLE) self.set_function(partial(self._walk_nary, "concat"), op.BV_CONCAT) self.set_function(partial(self._walk_nary, "bvcomp"), op.BV_COMP) self.set_function(partial(self._walk_nary, "bvashr"), op.BV_ASHR) self.set_function(partial(self._walk_nary, "bvsdiv"), op.BV_SDIV) self.set_function(partial(self._walk_nary, "bvsrem"), op.BV_SREM) self.set_function(self.walk_bv_extract, op.BV_EXTRACT) self.set_function(self.walk_bv_rotate, op.BV_ROR) self.set_function(self.walk_bv_rotate, op.BV_ROL) self.set_function(self.walk_bv_extend, op.BV_ZEXT) self.set_function(self.walk_bv_extend, op.BV_SEXT)
def test_functions(self): vi = Symbol("At", INT) vr = Symbol("Bt", REAL) f = Symbol("f", FunctionType(INT, [REAL])) g = Symbol("g", FunctionType(REAL, [INT])) tc = get_env().stc self.assertEqual(tc.walk(Function(f, [vr])), INT) self.assertEqual(tc.walk(Function(g, [vi])), REAL) self.assertEqual(tc.walk(Function(f, [Function(g, [vi])])), INT) self.assertEqual(tc.walk(LE(Plus(vi, Function(f, [Real(4)])), Int(8))), BOOL) self.assertEqual(tc.walk(LE(Plus(vr, Function(g, [Int(4)])), Real(8))), BOOL) with self.assertRaises(TypeError): LE(Plus(vr, Function(g, [Real(4)])), Real(8)) with self.assertRaises(TypeError): LE(Plus(vi, Function(f, [Int(4)])), Int(8))
def get_full_example_formulae(environment=None): """Return a list of Examples using the given environment.""" if environment is None: environment = get_env() with environment: x = Symbol("x", BOOL) y = Symbol("y", BOOL) p = Symbol("p", INT) q = Symbol("q", INT) r = Symbol("r", REAL) s = Symbol("s", REAL) aii = Symbol("aii", ARRAY_INT_INT) ari = Symbol("ari", ArrayType(REAL, INT)) arb = Symbol("arb", ArrayType(REAL, BV8)) abb = Symbol("abb", ArrayType(BV8, BV8)) nested_a = Symbol("a_arb_aii", ArrayType(ArrayType(REAL, BV8), ARRAY_INT_INT)) rf = Symbol("rf", FunctionType(REAL, [REAL, REAL])) rg = Symbol("rg", FunctionType(REAL, [REAL])) ih = Symbol("ih", FunctionType(INT, [REAL, INT])) ig = Symbol("ig", FunctionType(INT, [INT])) bf = Symbol("bf", FunctionType(BOOL, [BOOL])) bg = Symbol("bg", FunctionType(BOOL, [BOOL])) bv3 = Symbol("bv3", BVType(3)) bv8 = Symbol("bv8", BV8) bv16 = Symbol("bv16", BV16) result = [ # Formula, is_valid, is_sat, is_qf Example(hr="(x & y)", expr=And(x, y), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BOOL ), Example(hr="(x <-> y)", expr=Iff(x, y), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BOOL ), Example(hr="((x | y) & (! (x | y)))", expr=And(Or(x, y), Not(Or(x, y))), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BOOL ), Example(hr="(x & (! y))", expr=And(x, Not(y)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BOOL ), Example(hr="(False -> True)", expr=Implies(FALSE(),TRUE()), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BOOL ), Example(hr="((x | y) & (! (x | y)))", expr=And(Or(x, y), Not(Or(x, y))), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BOOL ), # # LIA # Example(hr="((q < p) & (x -> y))", expr=And(GT(p, q), Implies(x, y)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_IDL ), Example(hr="(((p + q) = 5) & (q < p))", expr=And(Equals(Plus(p,q),Int(5)) , GT(p, q)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LIA ), Example(hr="((q <= p) | (p <= q))", expr=Or(GE(p, q), LE(p, q)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_IDL ), Example(hr="(! (p < (q * 2)))", expr=Not(LT(p, Times(q, Int(2)))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LIA ), Example(hr="(p < (p - (5 - 2)))", expr=GT(Minus(p, Minus(Int(5), Int(2))), p), is_valid=False, is_sat=False, logic=pysmt.logics.QF_IDL ), Example(hr="((x ? 7 : ((p + -1) * 3)) = q)", expr=Equals(Ite(x, Int(7), Times(Plus(p, Int(-1)), Int(3))), q), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LIA ), Example(hr="(p < (q + 1))", expr=LT(p, Plus(q, Int(1))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LIA ), # # LRA # Example(hr="((s < r) & (x -> y))", expr=And(GT(r, s), Implies(x, y)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_RDL ), Example(hr="(((r + s) = 28/5) & (s < r))", expr=And(Equals(Plus(r,s), Real(Fraction("5.6"))) , GT(r, s)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LRA ), Example(hr="((s <= r) | (r <= s))", expr=Or(GE(r, s), LE(r, s)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_RDL ), Example(hr="(! ((r * 2.0) < (s * 2.0)))", expr=Not(LT(Div(r, Real((1,2))), Times(s, Real(2)))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LRA ), Example(hr="(! (r < (r - (5.0 - 2.0))))", expr=Not(GT(Minus(r, Minus(Real(5), Real(2))), r)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_RDL ), Example(hr="((x ? 7.0 : ((s + -1.0) * 3.0)) = r)", expr=Equals( Ite(x, Real(7), Times(Plus(s, Real(-1)), Real(3))), r), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LRA ), # # EUF # Example(hr="(bf(x) <-> bg(x))", expr=Iff(Function(bf, (x,)), Function(bg, (x,))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_UF ), Example(hr="(rf(5.0, rg(r)) = 0.0)", expr=Equals(Function(rf, (Real(5), Function(rg, (r,)))), Real(0)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_UFLRA ), Example(hr="((rg(r) = (5.0 + 2.0)) <-> (rg(r) = 7.0))", expr=Iff(Equals(Function(rg, [r]), Plus(Real(5), Real(2))), Equals(Function(rg, [r]), Real(7))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_UFLRA ), Example(hr="((r = (s + 1.0)) & (rg(s) = 5.0) & (rg((r - 1.0)) = 7.0))", expr=And([Equals(r, Plus(s, Real(1))), Equals(Function(rg, [s]), Real(5)), Equals( Function(rg, [Minus(r, Real(1))]), Real(7))]), is_valid=False, is_sat=False, logic=pysmt.logics.QF_UFLRA ), # # BV # Example(hr="((1_32 & 0_32) = 0_32)", expr=Equals(BVAnd(BVOne(32), BVZero(32)), BVZero(32)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((! 2_3) = 5_3)", expr=Equals(BVNot(BV("010")), BV("101")), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((! bv3) = 5_3)", expr=Equals(BVNot(bv3), BV("101")), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((7_3 xor 0_3) = 0_3)", expr=Equals(BVXor(BV("111"), BV("000")), BV("000")), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV ), Example(hr="((7_3 xor bv3) = (6_3 xor bv3))", expr=Equals(BVXor(BV("111"), bv3), BVXor(BV("110"), bv3)), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV ), Example(hr="((bv8::bv8) u< 0_16)", expr=BVULT(BVConcat(bv8, bv8), BVZero(16)), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV ), Example(hr="((bv8::bv8) u< (bv8::9_8))", expr=BVULT(BVConcat(bv8, bv8), BVConcat(bv8, BV(9, 8))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="(1_32[0:7] = 1_8)", expr=Equals(BVExtract(BVOne(32), end=7), BVOne(8)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="(0_8 u< (((bv8 + 1_8) * 5_8) u/ 5_8))", expr=BVUGT(BVUDiv(BVMul(BVAdd(bv8, BVOne(8)), BV(5, width=8)), BV(5, width=8)), BVZero(8)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="(0_16 u<= bv16)", expr=BVUGE(bv16, BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="(0_16 s<= bv16)", expr=BVSGE(bv16, BVZero(16)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((0_32 u< (5_32 u% 2_32)) & ((5_32 u% 2_32) u<= 1_32))", expr=And(BVUGT(BVURem(BV(5, width=32), BV(2, width=32)), BVZero(32)), BVULE(BVURem(BV(5, width=32), BV(2, width=32)), BVOne(32))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((((1_32 + (- 1_32)) << 1_32) >> 1_32) = 1_32)", expr=Equals(BVLShr(BVLShl(BVAdd(BVOne(32), BVNeg(BVOne(32))), 1), 1), BVOne(32)), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV ), Example(hr="((1_32 - 1_32) = 0_32)", expr=Equals(BVSub(BVOne(32), BVOne(32)), BVZero(32)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), # Rotations Example(hr="(((1_32 ROL 1) ROR 1) = 1_32)", expr=Equals(BVRor(BVRol(BVOne(32), 1),1), BVOne(32)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((bv16 ROL 1) = (bv16 ROR 2))", expr=Equals(BVRol(bv16, 1), BVRor(bv16, 2)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV ), # Extensions Example(hr="((0_5 ZEXT 11) = (0_1 SEXT 15))", expr=Equals(BVZExt(BVZero(5), 11), BVSExt(BVZero(1), 15)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((bv8 ZEXT 19) = (bv16 SEXT 11))", expr=Equals(BVZExt(bv8, 19), BVSExt(bv16, 11)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((bv16 - bv16) = 0_16)", expr=Equals(BVSub(bv16, bv16), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((bv16 - bv16)[0:7] = bv8)", expr=Equals(BVExtract(BVSub(bv16, bv16), 0, 7), bv8), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((bv16[0:7] bvcomp bv8) = 1_1)", expr=Equals(BVComp(BVExtract(bv16, 0, 7), bv8), BVOne(1)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((bv16 bvcomp bv16) = 0_1)", expr=Equals(BVComp(bv16, bv16), BVZero(1)), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV ), Example(hr="(bv16 s< bv16)", expr=BVSLT(bv16, bv16), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV ), Example(hr="(bv16 s< 0_16)", expr=BVSLT(bv16, BVZero(16)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((bv16 s< 0_16) | (0_16 s<= bv16))", expr=Or(BVSGT(BVZero(16), bv16), BVSGE(bv16, BVZero(16))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="(bv16 u< bv16)", expr=BVULT(bv16, bv16), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV ), Example(hr="(bv16 u< 0_16)", expr=BVULT(bv16, BVZero(16)), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV ), Example(hr="((bv16 | 0_16) = bv16)", expr=Equals(BVOr(bv16, BVZero(16)), bv16), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((bv16 | 5_16) = bv16)", expr=Equals(BVOr(bv16, BV(5, 16)), bv16), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((bv16 & 0_16) = 0_16)", expr=Equals(BVAnd(bv16, BVZero(16)), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((bv16 & 7_16) = 0_16)", expr=Equals(BVAnd(bv16, BV(7, 16)), BVZero(16)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((0_16 s< bv16) & ((bv16 s/ 65535_16) s< 0_16))", expr=And(BVSLT(BVZero(16), bv16), BVSLT(BVSDiv(bv16, SBV(-1, 16)), BVZero(16))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((0_16 s< bv16) & ((bv16 s% 1_16) s< 0_16))", expr=And(BVSLT(BVZero(16), bv16), BVSLT(BVSRem(bv16, BVOne(16)), BVZero(16))), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV ), Example(hr="((bv16 u% 1_16) = 0_16)", expr=Equals(BVURem(bv16, BVOne(16)), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((bv16 u% bv16) = 0_16)", expr=Equals(BVURem(bv16, bv16), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((bv16 s% 1_16) = 0_16)", expr=Equals(BVSRem(bv16, BVOne(16)), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((bv16 s% bv16) = 0_16)", expr=Equals(BVSRem(bv16, bv16), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((bv16 s% (- 1_16)) = 0_16)", expr=Equals(BVSRem(bv16, BVNeg(BVOne(16))), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="(bv16 s< (- bv16))", expr=BVSGT(BVNeg(bv16), bv16), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((bv16 a>> 0_16) = bv16)", expr=Equals(BVAShr(bv16, BVZero(16)), bv16), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV ), Example(hr="((0_16 s<= bv16) & ((bv16 a>> 1_16) = (bv16 >> 1_16)))", expr=And(BVSLE(BVZero(16), bv16), Equals(BVAShr(bv16, BVOne(16)), BVLShr(bv16, BVOne(16)))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV ), # # Quantification # Example(hr="(forall y . (x -> y))", expr=ForAll([y], Implies(x,y)), is_valid=False, is_sat=True, logic=pysmt.logics.BOOL ), Example(hr="(forall p, q . ((p + q) = 0))", expr=ForAll([p,q], Equals(Plus(p,q), Int(0))), is_valid=False, is_sat=False, logic=pysmt.logics.LIA ), Example(hr="(forall r, s . (((0.0 < r) & (0.0 < s)) -> ((r - s) < r)))", expr=ForAll([r,s], Implies(And(GT(r, Real(0)), GT(s, Real(0))), (LT(Minus(r,s), r)))), is_valid=True, is_sat=True, logic=pysmt.logics.LRA ), Example(hr="(exists x, y . (x -> y))", expr=Exists([x,y], Implies(x,y)), is_valid=True, is_sat=True, logic=pysmt.logics.BOOL ), Example(hr="(exists p, q . ((p + q) = 0))", expr=Exists([p,q], Equals(Plus(p,q), Int(0))), is_valid=True, is_sat=True, logic=pysmt.logics.LIA ), Example(hr="(exists r . (forall s . (r < (r - s))))", expr=Exists([r], ForAll([s], GT(Minus(r,s), r))), is_valid=False, is_sat=False, logic=pysmt.logics.LRA ), Example(hr="(forall r . (exists s . (r < (r - s))))", expr=ForAll([r], Exists([s], GT(Minus(r,s), r))), is_valid=True, is_sat=True, logic=pysmt.logics.LRA ), Example(hr="(x & (forall r . ((r + s) = 5.0)))", expr=And(x, ForAll([r], Equals(Plus(r,s), Real(5)))), is_valid=False, is_sat=False, logic=pysmt.logics.LRA ), Example(hr="(exists x . ((x <-> (5.0 < s)) & (s < 3.0)))", expr=Exists([x], (And(Iff(x, GT(s, Real(5))), LT(s, Real(3))))), is_valid=False, is_sat=True, logic=pysmt.logics.LRA ), # # UFLIRA # Example(hr="((p < ih(r, q)) & (x -> y))", expr=And(GT(Function(ih, (r, q)), p), Implies(x, y)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_UFLIRA ), Example(hr="(((p - 3) = q) -> ((p < ih(r, (q + 3))) | (ih(r, p) <= p)))", expr=Implies(Equals(Minus(p, Int(3)), q), Or(GT(Function(ih, (r, Plus(q, Int(3)))), p), LE(Function(ih, (r, p)), p))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_UFLIRA ), Example(hr="(((ToReal((p - 3)) = r) & (ToReal(q) = r)) -> ((p < ih(ToReal((p - 3)), (q + 3))) | (ih(r, p) <= p)))", expr=Implies(And(Equals(ToReal(Minus(p, Int(3))), r), Equals(ToReal(q), r)), Or(GT(Function(ih, (ToReal(Minus(p, Int(3))), Plus(q, Int(3)))), p), LE(Function(ih, (r, p)), p))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_UFLIRA ), Example(hr="(! (((ToReal((p - 3)) = r) & (ToReal(q) = r)) -> ((p < ih(ToReal((p - 3)), (q + 3))) | (ih(r, p) <= p))))", expr=Not(Implies(And(Equals(ToReal(Minus(p, Int(3))), r), Equals(ToReal(q), r)), Or(GT(Function(ih, (ToReal(Minus(p, Int(3))), Plus(q, Int(3)))), p), LE(Function(ih, (r, p)), p)))), is_valid=False, is_sat=False, logic=pysmt.logics.QF_UFLIRA ), Example(hr="""("Did you know that any string works? #yolo" & "10" & "|#somesolverskeepthe||" & " ")""", expr=And(Symbol("Did you know that any string works? #yolo"), Symbol("10"), Symbol("|#somesolverskeepthe||"), Symbol(" ")), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BOOL ), # # Arrays # Example(hr="((q = 0) -> (aii[0 := 0] = aii[0 := q]))", expr=Implies(Equals(q, Int(0)), Equals(Store(aii, Int(0), Int(0)), Store(aii, Int(0), q))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_ALIA ), Example(hr="(aii[0 := 0][0] = 0)", expr=Equals(Select(Store(aii, Int(0), Int(0)), Int(0)), Int(0)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_ALIA ), Example(hr="((Array{Int, Int}(0)[1 := 1] = aii) & (aii[1] = 0))", expr=And(Equals(Array(INT, Int(0), {Int(1) : Int(1)}), aii), Equals(Select(aii, Int(1)), Int(0))), is_valid=False, is_sat=False, logic=pysmt.logics.get_logic_by_name("QF_ALIA*") ), Example(hr="((Array{Int, Int}(0)[1 := 3] = aii) & (aii[1] = 3))", expr=And(Equals(Array(INT, Int(0), {Int(1) : Int(3)}), aii), Equals(Select(aii, Int(1)), Int(3))), is_valid=False, is_sat=True, logic=pysmt.logics.get_logic_by_name("QF_ALIA*") ), Example(hr="((Array{Real, Int}(10) = ari) & (ari[6/5] = 0))", expr=And(Equals(Array(REAL, Int(10)), ari), Equals(Select(ari, Real((6, 5))), Int(0))), is_valid=False, is_sat=False, logic=pysmt.logics.get_logic_by_name("QF_AUFBVLIRA*") ), Example(hr="((Array{Real, Int}(0)[1.0 := 10][2.0 := 20][3.0 := 30][4.0 := 40] = ari) & (! ((ari[0.0] = 0) & (ari[1.0] = 10) & (ari[2.0] = 20) & (ari[3.0] = 30) & (ari[4.0] = 40))))", expr=And(Equals(Array(REAL, Int(0), {Real(1) : Int(10), Real(2) : Int(20), Real(3) : Int(30), Real(4) : Int(40)}), ari), Not(And(Equals(Select(ari, Real(0)), Int(0)), Equals(Select(ari, Real(1)), Int(10)), Equals(Select(ari, Real(2)), Int(20)), Equals(Select(ari, Real(3)), Int(30)), Equals(Select(ari, Real(4)), Int(40))))), is_valid=False, is_sat=False, logic=pysmt.logics.get_logic_by_name("QF_AUFBVLIRA*") ), Example(hr="((Array{Real, Int}(0)[1.0 := 10][2.0 := 20][3.0 := 30][4.0 := 40][5.0 := 50] = ari) & (! ((ari[0.0] = 0) & (ari[1.0] = 10) & (ari[2.0] = 20) & (ari[3.0] = 30) & (ari[4.0] = 40) & (ari[5.0] = 50))))", expr=And(Equals(Array(REAL, Int(0), {Real(1) : Int(10), Real(2) : Int(20), Real(3) : Int(30), Real(4) : Int(40), Real(5) : Int(50)}), ari), Not(And(Equals(Select(ari, Real(0)), Int(0)), Equals(Select(ari, Real(1)), Int(10)), Equals(Select(ari, Real(2)), Int(20)), Equals(Select(ari, Real(3)), Int(30)), Equals(Select(ari, Real(4)), Int(40)), Equals(Select(ari, Real(5)), Int(50))))), is_valid=False, is_sat=False, logic=pysmt.logics.get_logic_by_name("QF_AUFBVLIRA*") ), Example(hr="((a_arb_aii = Array{Array{Real, BV{8}}, Array{Int, Int}}(Array{Int, Int}(7))) -> (a_arb_aii[arb][42] = 7))", expr=Implies(Equals(nested_a, Array(ArrayType(REAL, BV8), Array(INT, Int(7)))), Equals(Select(Select(nested_a, arb), Int(42)), Int(7))), is_valid=True, is_sat=True, logic=pysmt.logics.get_logic_by_name("QF_AUFBVLIRA*") ), Example(hr="(abb[bv8 := y_][bv8 := z_] = abb[bv8 := z_])", expr=Equals(Store(Store(abb, bv8, Symbol("y_", BV8)), bv8, Symbol("z_", BV8)), Store(abb, bv8, Symbol("z_", BV8))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_ABV ), Example(hr="((r / s) = (r * s))", expr=Equals(Div(r, s), Times(r,s)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_NRA ), Example(hr="(2.0 = (r * r))", expr=Equals(Real(2), Times(r,r)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_NRA ), Example(hr="((p ^ 2) = 0)", expr=Equals(Pow(p, Int(2)), Int(0)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_NIA ), Example(hr="((r ^ 2.0) = 0.0)", expr=Equals(Pow(r, Real(2)), Real(0)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_NRA ), Example(hr="((r * r * r) = 25.0)", expr=Equals(Times(r, r, r), Real(25)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_NRA ), Example(hr="((5.0 * r * 5.0) = 25.0)", expr=Equals(Times(Real(5), r, Real(5)), Real(25)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LRA ), Example(hr="((p * p * p) = 25)", expr=Equals(Times(p, p, p), Int(25)), is_valid=False, is_sat=False, logic=pysmt.logics.QF_NIA ), Example(hr="((5 * p * 5) = 25)", expr=Equals(Times(Int(5), p, Int(5)), Int(25)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LIA ), Example(hr="(((1 - 1) * p * 1) = 0)", expr=Equals(Times(Minus(Int(1), Int(1)), p, Int(1)), Int(0)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_LIA ), # Huge Fractions: Example(hr="((r * 1606938044258990275541962092341162602522202993782792835301376/7) = -20480000000000000000000000.0)", expr=Equals(Times(r, Real(Fraction(2**200,7))), Real(-200**11)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LRA ), Example(hr="(((r + 5.0 + s) * (s + 2.0 + r)) = 0.0)", expr=Equals(Times(Plus(r, Real(5), s), Plus(s, Real(2), r)), Real(0)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_NRA), Example(hr="(((p + 5 + q) * (p - (q - 5))) = ((p * p) + (10 * p) + 25 + (-1 * q * q)))", expr=Equals(Times(Plus(p, Int(5), q), Minus(p, Minus(q, Int(5)))), Plus(Times(p, p), Times(Int(10), p), Int(25), Times(Int(-1), q, q))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_NIA ), ] return result
def get_example_formulae(environment=None): if environment is None: environment = get_env() with environment: x = Symbol("x", BOOL) y = Symbol("y", BOOL) p = Symbol("p", INT) q = Symbol("q", INT) r = Symbol("r", REAL) s = Symbol("s", REAL) rf = Symbol("rf", FunctionType(REAL, [REAL, REAL])) rg = Symbol("rg", FunctionType(REAL, [REAL])) ih = Symbol("ih", FunctionType(INT, [REAL, INT])) ig = Symbol("ig", FunctionType(INT, [INT])) bv8 = Symbol("bv1", BV8) bv16 =Symbol("bv2", BV16) result = [ # Formula, is_valid, is_sat, is_qf # x /\ y Example(expr=And(x, y), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BOOL ), # x <-> y Example(expr=Iff(x, y), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BOOL ), # (x \/ y ) /\ ! ( x \/ y ) Example(expr=And(Or(x, y), Not(Or(x, y))), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BOOL ), # (x /\ !y) Example(expr=And(x, Not(y)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BOOL), # False -> True Example(expr=Implies(FALSE(),TRUE()), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BOOL ), # # LIA # # (p > q) /\ x -> y Example(expr=And(GT(p, q), Implies(x, y)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_IDL ), # (p + q) = 5 /\ (p > q) Example(expr=And(Equals(Plus(p,q),Int(5)) , GT(p, q)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LIA ), # (p >= q) \/ ( p <= q) Example(expr=Or(GE(p, q), LE(p, q)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_IDL ), # !( p < q * 2 ) Example(expr=Not(LT(p, Times(q, Int(2)))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LIA ), # p - (5 - 2) > p Example(expr=GT(Minus(p, Minus(Int(5), Int(2))), p), is_valid=False, is_sat=False, logic=pysmt.logics.QF_IDL ), # x ? 7: (p + -1) * 3 = q Example(expr=Equals(Ite(x, Int(7), Times(Plus(p, Int(-1)), Int(3))), q), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LIA ), Example(expr=LT(p, Plus(q, Int(1))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LIA ), # # LRA # # (r > s) /\ x -> y Example(expr=And(GT(r, s), Implies(x, y)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_RDL ), # (r + s) = 5.6 /\ (r > s) Example(expr=And(Equals(Plus(r,s), Real(Fraction("5.6"))) , GT(r, s)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LRA ), # (r >= s) \/ ( r <= s) Example(expr=Or(GE(r, s), LE(r, s)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_RDL ), # !( (r / (1/2)) < s * 2 ) Example(expr=Not(LT(Div(r, Real((1,2))), Times(s, Real(2)))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LRA ), # ! ( r - (5 - 2) > r ) Example(expr=Not(GT(Minus(r, Minus(Real(5), Real(2))), r)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_RDL ), # x ? 7: (s + -1) * 3 = r Example(expr=Equals( Ite(x, Real(7), Times(Plus(s, Real(-1)), Real(3))), r), is_valid=False, is_sat=True, logic=pysmt.logics.QF_LRA ), # # EUF # # rf(5, rg(2)) = 0 Example(expr=Equals(Function(rf, (Real(5), Function(rg, (r,)))), Real(0)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_UFLRA ), # (rg(r) = 5 + 2) <-> (rg(r) = 7) Example(expr=Iff(Equals(Function(rg, [r]), Plus(Real(5), Real(2))), Equals(Function(rg, [r]), Real(7))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_UFLRA ), # (r = s + 1) & (rg(s) = 5) & (rg(r - 1) = 7) Example(expr=And([Equals(r, Plus(s, Real(1))), Equals(Function(rg, [s]), Real(5)), Equals( Function(rg, [Minus(r, Real(1))]), Real(7))]), is_valid=False, is_sat=False, logic=pysmt.logics.QF_UFLRA), # # BV # # bv_one & bv_zero == bv_zero Example(expr=Equals(BVAnd(BVOne(32), BVZero(32)), BVZero(32)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), # ~(010) == 101 Example(expr=Equals(BVNot(BV("010")), BV("101")), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), # "111" xor "000" == "000" Example(expr=Equals(BVXor(BV("111"), BV("000")), BV("000")), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), # bv8 :: bv8 < bv_zero Example(expr=BVULT(BVConcat(bv8, bv8), BVZero(16)), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), # bv_one[:7] == bv_one Example(expr=Equals(BVExtract(BVOne(32), end=7), BVOne(8)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), # (((bv8 + bv_one) * bv(5)) / bv(5)) > bv(0) Example(expr=BVUGT(BVUDiv(BVMul(BVAdd(bv8, BVOne(8)), BV(5, width=8)), BV(5, width=8)), BVZero(8)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), # bv16 >=u bv(0) Example(expr=BVUGE(bv16, BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), # bv16 >=s bv(0) Example(expr=BVSGE(bv16, BVZero(16)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), # (BV(5) rem BV(2) > bv_zero) /\ (BV(5) rem BV(2) < bv_one) Example(expr=And(BVUGT(BVURem(BV(5, width=32), BV(2, width=32)), BVZero(32)), BVULE(BVURem(BV(5, width=32), BV(2, width=32)), BVOne(32))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), # ((bv_one + (- bv_one)) << 1) >> 1 == bv_one Example(expr=Equals(BVLShr(BVLShl(BVAdd(BVOne(32), BVNeg(BVOne(32))), 1), 1), BVOne(32)), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), # bv_one - bv_one == bv_zero Example(expr=Equals(BVSub(BVOne(32), BVOne(32)), BVZero(32)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), # Rotations Example(expr=Equals(BVRor(BVRol(BVOne(32), 1),1), BVOne(32)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), # Extensions Example(expr=Equals(BVZExt(BVZero(5), 11), BVSExt(BVZero(1), 15)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), # bv16 - bv16 = 0_16 Example(expr=Equals(BVSub(bv16, bv16), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), # (bv16 - bv16)[0:7] = bv8 Example(expr=Equals(BVExtract(BVSub(bv16, bv16), 0, 7), bv8), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), # (bv16[0,7] comp bv8) = bv1 Example(expr=Equals(BVComp(BVExtract(bv16, 0, 7), bv8), BVOne(1)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), # (bv16 comp bv16) = bv0 Example(expr=Equals(BVComp(bv16, bv16), BVZero(1)), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), # (bv16 s< bv16) Example(expr=BVSLT(bv16, bv16), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), # (bv16 s< 0_16) Example(expr=BVSLT(bv16, BVZero(16)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), # (bv16 u< bv16) Example(expr=BVULT(bv16, bv16), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), # (bv16 s< 0_16) Example(expr=BVULT(bv16, BVZero(16)), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), # (bv16 | 0_16) = bv16 Example(expr=Equals(BVOr(bv16, BVZero(16)), bv16), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), # (bv16 & 0_16) = 0_16 Example(expr=Equals(BVAnd(bv16, BVZero(16)), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), # 0_16 s< bv16 & ((bv16 s/ -1) s< 0) Example(expr=And(BVSLT(BVZero(16), bv16), BVSLT(BVSDiv(bv16, SBV(-1, 16)), BVZero(16))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), # 0_16 s< bv16 & ((bv16 s% -1) s< 0) Example(expr=And(BVSLT(BVZero(16), bv16), BVSLT(BVSRem(bv16, BVOne(16)), BVZero(16))), is_valid=False, is_sat=False, logic=pysmt.logics.QF_BV), # bv16 u% 1 = 0_16 Example(expr=Equals(BVURem(bv16, BVOne(16)), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), # bv16 s% 1 = 0_16 Example(expr=Equals(BVSRem(bv16, BVOne(16)), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), # bv16 s% -1 = 0_16 Example(expr=Equals(BVSRem(bv16, BVNeg(BVOne(16))), BVZero(16)), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), # bv16 a>> 0 = bv16 Example(expr=Equals(BVAShr(bv16, BVZero(16)), bv16), is_valid=True, is_sat=True, logic=pysmt.logics.QF_BV), # 0 s<= bv16 & bv16 a>> 1 = bv16 >> 1 Example(expr=And(BVSLE(BVZero(16), bv16), Equals(BVAShr(bv16, BVOne(16)), BVLShr(bv16, BVOne(16)))), is_valid=False, is_sat=True, logic=pysmt.logics.QF_BV), # # Quantification # # forall y . x -> y Example(expr=ForAll([y], Implies(x,y)), is_valid=False, is_sat=True, logic=pysmt.logics.BOOL ), # forall p,q . p + q = 0 Example(expr=ForAll([p,q], Equals(Plus(p,q), Int(0))), is_valid=False, is_sat=False, logic=pysmt.logics.LIA ), # forall r,s . ((r > 0) & (s > 0)) -> (r - s < r) Example(expr=ForAll([r,s], Implies(And(GT(r, Real(0)), GT(s, Real(0))), (LT(Minus(r,s), r)))), is_valid=True, is_sat=True, logic=pysmt.logics.LRA ), # exists x,y . x -> y Example(expr=Exists([x,y], Implies(x,y)), is_valid=True, is_sat=True, logic=pysmt.logics.BOOL ), # exists p,q . p + q = 0 Example(expr=Exists([p,q], Equals(Plus(p,q), Int(0))), is_valid=True, is_sat=True, logic=pysmt.logics.LIA ), # exists r . forall s . (r - s > r) Example(expr=Exists([r], ForAll([s], GT(Minus(r,s), r))), is_valid=False, is_sat=False, logic=pysmt.logics.LRA ), # forall r . exists s . (r - s > r) Example(expr=ForAll([r], Exists([s], GT(Minus(r,s), r))), is_valid=True, is_sat=True, logic=pysmt.logics.LRA ), # x /\ forall r. (r + s = 5) Example(expr=And(x, ForAll([r], Equals(Plus(r,s), Real(5)))), is_valid=False, is_sat=False, logic=pysmt.logics.LRA ), # # UFLIRA # # ih(r,q) > p /\ (x -> y) Example(expr=And(GT(Function(ih, (r, q)), p), Implies(x, y)), is_valid=False, is_sat=True, logic=pysmt.logics.QF_UFLIRA ), # ( (p - 3) = q ) -> ( ih(r, q + 3) > p \/ ih(r, p) <= p ) Example(expr=Implies(Equals(Minus(p, Int(3)), q), Or(GT(Function(ih, (r, Plus(q, Int(3)))), p), LE(Function(ih, (r, p)), p))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_UFLIRA ), # ( (ToReal(p - 3) = r) /\ (ToReal(q) = r) ) -> # ( ( ih(ToReal(p - 3), q + 3) > p ) \/ (ih(r, p) <= p) ) Example(expr=Implies(And(Equals(ToReal(Minus(p, Int(3))), r), Equals(ToReal(q), r)), Or(GT(Function(ih, (ToReal(Minus(p, Int(3))), Plus(q, Int(3)))), p), LE(Function(ih, (r, p)), p))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_UFLIRA ), # ! ( (ToReal(p - 3) = r /\ ToReal(q) = r) -> # ( ih(ToReal(p - 3), q + 3) > p \/ # ih(r,p) <= p ) ) Example(expr=Not(Implies(And(Equals(ToReal(Minus(p, Int(3))), r), Equals(ToReal(q), r)), Or(GT(Function(ih, (ToReal(Minus(p, Int(3))), Plus(q, Int(3)))), p), LE(Function(ih, (r, p)), p)))), is_valid=False, is_sat=False, logic=pysmt.logics.QF_UFLIRA ), ] return result
def __init__(self, environment=None, interactive=False): self.env = get_env() if environment is None else environment self.interactive = interactive # Placeholders for fields filled by self._reset self.cache = None self.logic = None self._reset() # Tokens representing interpreted functions appearing in expressions # Each token is handled by a dedicated function that takes the # recursion stack, the token stream and the parsed token # Common tokens are handled in the _reset function mgr = self.env.formula_manager self.interpreted = { "let": self._enter_let, "!": self._enter_annotation, "exists": self._enter_quantifier, "forall": self._enter_quantifier, "+": self._operator_adapter(mgr.Plus), "-": self._operator_adapter(self._minus_or_uminus), "*": self._operator_adapter(mgr.Times), "/": self._operator_adapter(self._division), ">": self._operator_adapter(mgr.GT), "<": self._operator_adapter(mgr.LT), ">=": self._operator_adapter(mgr.GE), "<=": self._operator_adapter(mgr.LE), "=": self._operator_adapter(self._equals_or_iff), "not": self._operator_adapter(mgr.Not), "and": self._operator_adapter(mgr.And), "or": self._operator_adapter(mgr.Or), "xor": self._operator_adapter(mgr.Xor), "=>": self._operator_adapter(mgr.Implies), "<->": self._operator_adapter(mgr.Iff), "ite": self._operator_adapter(mgr.Ite), "to_real": self._operator_adapter(mgr.ToReal), "concat": self._operator_adapter(mgr.BVConcat), "bvnot": self._operator_adapter(mgr.BVNot), "bvand": self._operator_adapter(mgr.BVAnd), "bvor": self._operator_adapter(mgr.BVOr), "bvneg": self._operator_adapter(mgr.BVNeg), "bvadd": self._operator_adapter(mgr.BVAdd), "bvmul": self._operator_adapter(mgr.BVMul), "bvudiv": self._operator_adapter(mgr.BVUDiv), "bvurem": self._operator_adapter(mgr.BVURem), "bvshl": self._operator_adapter(mgr.BVLShl), "bvlshr": self._operator_adapter(mgr.BVLShr), "bvsub": self._operator_adapter(mgr.BVSub), "bvult": self._operator_adapter(mgr.BVULT), "bvxor": self._operator_adapter(mgr.BVXor), "_": self._operator_adapter(self._smtlib_underscore), # Extended Functions "bvnand": self._operator_adapter(mgr.BVNand), "bvnor": self._operator_adapter(mgr.BVNor), "bvxnor": self._operator_adapter(mgr.BVXnor), "bvcomp": self._operator_adapter(mgr.BVComp), "bvsdiv": self._operator_adapter(mgr.BVSDiv), "bvsrem": self._operator_adapter(mgr.BVSRem), "bvsmod": self._operator_adapter(mgr.BVSMod), "bvashr": self._operator_adapter(mgr.BVAShr), "bvule": self._operator_adapter(mgr.BVULE), "bvugt": self._operator_adapter(mgr.BVUGT), "bvuge": self._operator_adapter(mgr.BVUGE), "bvslt": self._operator_adapter(mgr.BVSLT), "bvsle": self._operator_adapter(mgr.BVSLE), "bvsgt": self._operator_adapter(mgr.BVSGT), "bvsge": self._operator_adapter(mgr.BVSGE), } # Command tokens self.commands = { smtcmd.SET_INFO: self._cmd_set_info, smtcmd.SET_OPTION: self._cmd_set_option, smtcmd.ASSERT: self._cmd_assert, smtcmd.CHECK_SAT: self._cmd_check_sat, smtcmd.PUSH: self._cmd_push, smtcmd.POP: self._cmd_pop, smtcmd.EXIT: self._cmd_exit, smtcmd.SET_LOGIC: self._cmd_set_logic, smtcmd.DECLARE_CONST: self._cmd_declare_const, smtcmd.GET_VALUE: self._cmd_get_value, smtcmd.DECLARE_FUN: self._cmd_declare_fun, smtcmd.DEFINE_FUN: self._cmd_define_fun, }
Example(expr=Implies(And(Equals(ToReal(Minus(p, Int(3))), r), Equals(ToReal(q), r)), Or(GT(Function(ih, (ToReal(Minus(p, Int(3))), Plus(q, Int(3)))), p), LE(Function(ih, (r, p)), p))), is_valid=True, is_sat=True, logic=pysmt.logics.QF_UFLIRA ), # ! ( (ToReal(p - 3) = r /\ ToReal(q) = r) -> # ( ih(ToReal(p - 3), q + 3) > p \/ # ih(r,p) <= p ) ) Example(expr=Not(Implies(And(Equals(ToReal(Minus(p, Int(3))), r), Equals(ToReal(q), r)), Or(GT(Function(ih, (ToReal(Minus(p, Int(3))), Plus(q, Int(3)))), p), LE(Function(ih, (r, p)), p)))), is_valid=False, is_sat=False, logic=pysmt.logics.QF_UFLIRA ), ] return result EXAMPLE_FORMULAS = get_example_formulae(get_env()) EXAMPLE_FORMULAE = EXAMPLE_FORMULAS