7 auto py_smt = m.def_submodule(
"SMT", R
"(
11 py::enum_<SMT::SolverType> py_smt_solver_type(py_smt, "SolverType", R
"(
12 Identifier for the SMT solver type.
20 py::class_<SMT::QueryConfig> py_smt_query_config(py_smt, "QueryConfig", R
"(
21 Represents the data structure to configure an SMT query.
24 py_smt_query_config.def(py::init<>(), R"(
25 Constructs a new query configuration.
29 The SMT solver identifier.
31 :type: hal_py.SMT.SolverType
34 py_smt_query_config.def_readwrite("local", &SMT::QueryConfig::local, R
"(
35 Controls whether the SMT query is performed on a local or a remote machine.
41 Controls whether the SMT solver should generate a model in case formula is satisfiable.
47 The timeout after which the SMT solver is killed in seconds.
53 Sets the solver type to the desired SMT solver.
55 :param hal_py.SMT.SolverType solver: The solver type identifier.
56 :returns: The updated SMT query configuration.
57 :rtype: hal_py.SMT.QueryConfig
61 Sets the call type to the desired target.
63 :param hal_py.SMT.CallTyepe call: The solver type identifier.
64 :returns: The updated SMT query configuration.
65 :rtype: hal_py.SMT.QueryConfig
69 Activates local SMT solver execution.
71 :returns: The updated SMT query configuration.
72 :rtype: hal_py.SMT.QueryConfig
76 Activates remote SMT solver execution.
78 :returns: The updated SMT query configuration.
79 :rtype: hal_py.SMT.QueryConfig
83 Indicates that the SMT solver should generate a model in case the formula is satisfiable.
85 :returns: The updated SMT query configuration.
86 :rtype: hal_py.SMT.QueryConfig
90 Indicates that the SMT solver should not generate a model.
92 :returns: The updated SMT query configuration.
93 :rtype: hal_py.SMT.QueryConfig
97 Sets a timeout in seconds that terminates an SMT query after the specified time has passed.
99 :param int seconds: The timeout in seconds.
100 :returns: The updated SMT query configuration.
101 :rtype: hal_py.SMT.QueryConfig
104 py::class_<SMT::Constraint> py_smt_constraint(py_smt, "Constraint", R
"(
105 Represents a constraint to the SMT query.
106 A constraint is either an assignment of two Boolean functions or a single Boolean function, e.g., an equality check or similar.
110 A constraint that is either an assignment of two Boolean functions or a single Boolean function, e.g., an equality check or similar.
112 :type: hal_py.BooleanFunction or tuple(hal_py.BooleanFunction, hal_py.BooleanFunction)
116 Constructs a new constraint from one Boolean function that evaluates to a single bit.
118 :param hal_py.BooleanFunction constraint: The constraint function.
122 Constructs a new equality constraint from two Boolean functions.
124 :param hal_py.BooleanFunction lhs: The left-hand side of the equality constraint.
125 :param hal_py.BooleanFunction rhs: The right-hand side of the equality constraint.
129 Checks whether the constraint is an assignment constraint.
131 :returns: True of the constraint is an assignment, False otherwise.
135 py_smt_constraint.def(
137 [](
const SMT::Constraint&
self) -> std::optional<std::pair<BooleanFunction, BooleanFunction>> {
138 auto res =
self.get_assignment();
145 log_error(
"python_context",
"{}", res.get_error().get());
150 Returns the assignment constraint as a pair of Boolean functions.
152 :returns: The assignment constraint on success, None otherwise.
153 :rtype: tuple(hal_py.BooleanFunction,hal_py.BooleanFunction) or None
156 py_smt_constraint.def(
158 [](
const SMT::Constraint&
self) -> std::optional<const BooleanFunction*> {
159 auto res =
self.get_function();
166 log_error(
"python_context",
"{}", res.get_error().get());
171 Returns the function constraint as a Boolean function.
173 :returns: The function constraint on success, None otherwise.
174 :rtype: hal_py.BooleanFunction or None
177 py::enum_<SMT::SolverResultType> py_smt_result_type(py_smt, "SolverResultType", R
"(
178 Result type of an SMT solver query.
186 py::class_<SMT::Model> py_smt_model(py_smt, "Model", R
"(
187 Represents a list of assignments for variable nodes that yield a satisfiable assignment for a given list of constraints.
190 py_smt_model.def(py::init<const std::map<std::string, std::tuple<u64, u16>>&>(), py::arg(
"model") = std::map<std::string, std::tuple<u64, u16>>(), R
"(
191 Constructs a new model from a map of variable names to value and bit-size.
193 :param dict[str,tuple(int,int)] model: A dict from variable name to value and bit-size.
196 py_smt_model.def(py::self == py::self, R"(
197 Checks whether two SMT models are equal.
199 :returns: True if both models are equal, False otherwise.
203 py_smt_model.def(py::self != py::self, R"(
204 Checks whether two SMT models are unequal.
206 :returns: True if both models are unequal, False otherwise.
211 A dict from variable identifiers to a (1) value and (2) its bit-size.
213 :type: dict(str,tuple(int,int))
216 py_smt_model.def_static(
218 [](
const std::string& model_str,
const SMT::SolverType& solver) -> std::optional<SMT::Model> {
226 log_error(
"python_context",
"{}", res.get_error().get());
230 py::arg(
"model_str"),
233 Parses an SMT-Lib model from a string output by a solver of the given type.
235 :param str model_str: The SMT-Lib model string.
236 :param hal_py.SMT.SolverType solver: The solver that computed the model.
237 :returns: The model on success, None otherwise.
238 :rtype: hal_py.SMT.Model or None
244 auto res =
self.evaluate(bf);
251 log_error(
"python_context",
"{}", res.get_error().get());
257 Evaluates the given Boolean function by replacing all variables contained in the model with their corresponding value and simplifying the result.
259 :param hal_py.BooleanFunction bf: The Boolean function to evaluate.
260 :returns: The evaluated function on success, None otherwise.
261 :rtype: hal_py.BooleanFunction or None
264 py::class_<SMT::SolverResult> py_smt_result(py_smt, "SolverResult", R
"(
265 Represents the result of an SMT query.
269 Result type of the SMT query.
271 :type: hal_py.SMT.ResultType
275 The (optional) model that is only available if type == SMT.ResultType.Sat and model generation is enabled.
277 :type: hal_py.SMT.Model
280 py_smt_result.def_static("Sat", &
SMT::SolverResult::Sat, py::arg(
"model") = std::optional<SMT::Model>(), R
"(
281 Creates a satisfiable result with an optional model.
283 :param hal_py.SMT.Model model: Optional model for satisfiable formula.
284 :returns: The result.
285 :rtype: hal_py.SMT.SolverResult
289 Creates an unsatisfiable result.
291 :returns: The result.
292 :rtype: hal_py.SMT.SolverResult
296 Creates an unknown result.
298 :returns: The result.
299 :rtype: hal_py.SMT.SolverResult
303 Checks whether the result is of a specific type.
305 :param hal_py.SMT.ResultType type: The type to check.
306 :returns: True in case result matches the given type, False otherwise.
311 Checks whether the result is satisfiable.
313 :returns: True in case result is satisfiable, False otherwise.
318 Checks whether the result is unsatisfiable.
320 :returns: True in case result is unsatisfiable, False otherwise.
325 Checks whether the result is unknown.
327 :returns: True in case result is unknown, False otherwise.
331 py::class_<SMT::Solver> py_smt_solver(py_smt, "Solver", R
"(
332 Provides an interface to query SMT solvers for a list of constraints, i.e. statements that have to be equal. To this end, we translate constraints to a SMT-LIB v2 string representation and query solvers with a defined configuration, i.e., chosen solver, model generation etc.
335 py_smt_solver.def(py::init<std::vector<SMT::Constraint>>(), py::arg("constraints") = std::vector<SMT::Constraint>(), R
"(
336 Constructs an solver with an optional list of constraints.
338 :param list[hal_py.SMT.Constraint] constraints: The (optional) list of constraints.
342 The list of constraints.
344 :type: list[hal_py.SMT.Constraint]
348 Returns the list of constraints.
350 :returns: The list of constraints.
351 :rtype: list[hal_py.SMT.Constraint]
355 Adds a constraint to the SMT solver.
357 :param hal_py.SMT.Constraint constraint: The constraint.
358 :returns: The updated SMT solver.
359 :rtype: hal_py.SMT.Solver
363 Adds a list of constraints to the SMT solver.
365 :param list[hal_py.SMT.Constraint] constraints: The constraints.
366 :returns: The updated SMT solver.
367 :rtype: hal_py.SMT.Solver
371 Checks whether a SMT solver of the given type is available on the local machine.
373 :param hal_py.SMT.SolverType type: The SMT solver type.
374 :param hal_py.SMT.SolverCall call: The call to the SMT solver.
375 :returns: True if an SMT solver of the requested type is available, False otherwise.
382 auto res =
self.query(config);
389 log_error(
"python_context",
"{}", res.get_error().get());
395 Queries an SMT solver with the specified query configuration.
397 :param hal_py.SMT.QueryConfig config: The SMT solver query configuration.
398 :returns: The result on success, a string error message otherwise.
399 :rtype: hal_py.SMT.Result or str
405 auto res =
self.query_local(config);
412 log_error(
"python_context",
"{}", res.get_error().get());
418 Queries a local SMT solver with the specified query configuration.
420 :param hal_py.SMT.QueryConfig config: The SMT solver query configuration.
421 :returns: The result on success, a string error message otherwise.
422 :rtype: hal_py.SMT.Result or str
425 py_smt_solver.def_static(
426 "query_local_with_smt2",
427 [](
const SMT::QueryConfig& config,
const std::string& smt2) -> std::optional<SMT::SolverResult> {
435 log_error(
"python_context",
"{}", res.get_error().get());
442 Queries a local SMT solver with the specified query configuration and the smt2 formatted query.
444 :param hal_py.SMT.QueryConfig config: The SMT solver query configuration.
445 :param string smt2: A solver query formatted in smt2 style.
446 :returns: The result on success, a string error message otherwise.
447 :rtype: hal_py.SMT.Result or str
451 Queries a remote SMT solver with the specified query configuration.
453 WARNING: This function is not yet implemented.
455 :param hal_py.SMT.QueryConfig config: The SMT solver query configuration.
456 :returns: The result on success, a string error message otherwise.
457 :rtype: hal_py.SMT.Result or str
460 py::class_<SMT::SymbolicState> py_smt_symbolic_state(py_smt, "SymbolicState", R
"(
461 Represents the data structure that keeps track of symbolic variable values (e.g., required for symbolic simplification).
464 py_smt_symbolic_state.def(py::init<const std::vector<BooleanFunction>&>(), py::arg(
"variables") = std::vector<BooleanFunction>(), R
"(
465 Constructs a symbolic state and (optionally) initializes the variables.
467 :param list[hal_py.BooleanFunction] variables: The (optional) list of variables.
471 Looks up the Boolean function equivalent in the symbolic state.
473 :param hal_py.BooleanFunction key: The Boolean function to look up.
474 :returns: The Boolean function equivalent from the symbolic state or the key itself if it is not contained in the symbolic state.
475 :rtype: hal_py.BooleanFunction
479 Sets a Boolean function equivalent in the symbolic state.
481 :param hal_py.BooleanFunction key: The Boolean function.
482 :param hal_py.BooleanFunction value: The equivalent Boolean function.
485 py::class_<SMT::SymbolicExecution> py_smt_symbolic_execution(py_smt, "SymbolicExecution", R
"(
486 Represents the symbolic execution engine that handles the evaluation and simplification of Boolean function abstract syntax trees.
490 The current symbolic state.
492 :type: hal_py.SMT.SymbolicState
495 py_smt_symbolic_execution.def(py::init<const std::vector<BooleanFunction>&>(), py::arg(
"variables") = std::vector<BooleanFunction>(), R
"(
496 Creates a symbolic execution engine and (optionally) initializes the variables.
498 :param list[hal_py.BooleanFunction] variables: The (optional) list of variables.
501 py_smt_symbolic_execution.def("evaluate", py::overload_cast<const BooleanFunction&>(&
SMT::SymbolicExecution::evaluate, py::const_), py::arg(
"function"), R
"(
502 Evaluates a Boolean function within the symbolic state of the symbolic execution.
504 :param hal_py.BooleanFunction function: The Boolean function to evaluate.
505 :returns: The evaluated Boolean function on success, a string error message otherwise.
506 :rtype: hal_py.BooleanFunction or str
510 Evaluates an equality constraint and applies it to the symbolic state of the symbolic execution.
512 :param hal_py.SMT.Constraint constraint: The equality constraint to evaluate.
513 :returns: None on success, a string error message otherwise.
static bool has_local_solver_for(SolverType type, SolverCall call)
Solver & with_constraints(const std::vector< Constraint > &constraints)
const std::vector< Constraint > & get_constraints() const
static Result< SolverResult > query_local_with_smt2(const QueryConfig &config, const std::string &smt2)
Result< SolverResult > query_remote(const QueryConfig &config) const
Solver & with_constraint(const Constraint &constraint)
Result< BooleanFunction > evaluate(const BooleanFunction &function) const
SymbolicState state
The current symbolic state.
const BooleanFunction & get(const BooleanFunction &key) const
void set(const BooleanFunction &key, const BooleanFunction &value)
void smt_init(py::module &m)
#define log_error(channel,...)
const Module * module(const Gate *g, const NodeBoxes &boxes)
bool is_assignment() const
std::variant< BooleanFunction, std::pair< BooleanFunction, BooleanFunction > > constraint
A constraint that is either an assignment of two Boolean functions or a single Boolean function,...
static Result< Model > parse(const std::string &model_str, const SolverType &solver)
std::map< std::string, std::tuple< u64, u16 > > model
maps variable identifiers to a (1) value and (2) its bit-size.
bool generate_model
Controls whether the SMT solver should generate a model in case formula is satisfiable.
QueryConfig & with_remote_solver()
SolverType solver
The SMT solver identifier.
u64 timeout_in_seconds
The timeout after which the SMT solver is killed in seconds.
QueryConfig & without_model_generation()
QueryConfig & with_model_generation()
QueryConfig & with_local_solver()
QueryConfig & with_timeout(u64 seconds)
QueryConfig & with_solver(SolverType solver)
QueryConfig & with_call(SolverCall call)
static SolverResult Unknown()
std::optional< Model > model
The (optional) model that is only available if type == SMT::ResultType::Sat and model generation is e...
static SolverResult Sat(const std::optional< Model > &model={})
bool is(const SolverResultType &type) const
static SolverResult UnSat()
SolverResultType type
Result type of the SMT query.