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.
21 py::enum_<SMT::SolverCall> py_smt_solver_call(py_smt, "SolverCall", R
"(
22 Identifier for how the SMT solver is invoked.
29 py::class_<SMT::QueryConfig> py_smt_query_config(py_smt, "QueryConfig", R
"(
30 Represents the data structure to configure an SMT query.
33 py_smt_query_config.def(py::init<>(), R"(
34 Constructs a new query configuration.
38 The SMT solver identifier.
40 :type: hal_py.SMT.SolverType
43 py_smt_query_config.def_readwrite("local", &SMT::QueryConfig::local, R
"(
44 Controls whether the SMT query is performed on a local or a remote machine.
50 Controls whether the SMT solver should generate a model in case formula is satisfiable.
56 The timeout after which the SMT solver is killed in seconds.
62 Sets the solver type to the desired SMT solver.
64 :param hal_py.SMT.SolverType solver: The solver type identifier.
65 :returns: The updated SMT query configuration.
66 :rtype: hal_py.SMT.QueryConfig
70 Sets the call type to the desired target.
72 :param hal_py.SMT.SolverCall call: The solver call.
73 :returns: The updated SMT query configuration.
74 :rtype: hal_py.SMT.QueryConfig
78 Activates local SMT solver execution.
80 :returns: The updated SMT query configuration.
81 :rtype: hal_py.SMT.QueryConfig
85 Indicates that the SMT solver runs on a remote machine.
87 :returns: The updated SMT query configuration.
88 :rtype: hal_py.SMT.QueryConfig
92 Indicates that the SMT solver should generate a model in case the formula is satisfiable.
94 :returns: The updated SMT query configuration.
95 :rtype: hal_py.SMT.QueryConfig
99 Indicates that the SMT solver should not generate a model.
101 :returns: The updated SMT query configuration.
102 :rtype: hal_py.SMT.QueryConfig
106 Sets a timeout in seconds that terminates an SMT query after the specified time has passed.
108 :param int seconds: The timeout in seconds.
109 :returns: The updated SMT query configuration.
110 :rtype: hal_py.SMT.QueryConfig
114 Translates the SMT query configuration into its string representation.
116 :returns: A string representing the SMT query configuration.
121 Translates the SMT query configuration into its string representation.
123 :returns: A string representing the SMT query configuration.
127 py::class_<SMT::Constraint> py_smt_constraint(py_smt, "Constraint", R
"(
128 Represents a constraint to the SMT query.
129 A constraint is either an assignment of two Boolean functions or a single Boolean function, e.g., an equality check or similar.
133 A constraint that is either an assignment of two Boolean functions or a single Boolean function, e.g., an equality check or similar.
135 :type: hal_py.BooleanFunction or tuple(hal_py.BooleanFunction, hal_py.BooleanFunction)
139 Constructs a new constraint from one Boolean function that evaluates to a single bit.
141 :param hal_py.BooleanFunction constraint: The constraint function.
145 Constructs a new equality constraint from two Boolean functions.
147 :param hal_py.BooleanFunction lhs: The left-hand side of the equality constraint.
148 :param hal_py.BooleanFunction rhs: The right-hand side of the equality constraint.
152 Checks whether the constraint is an assignment constraint.
154 :returns: ``True`` if the constraint is an assignment, ``False`` otherwise.
158 py_smt_constraint.def(
160 [](
const SMT::Constraint&
self) -> std::optional<std::pair<BooleanFunction, BooleanFunction>> {
161 auto res =
self.get_assignment();
168 log_error(
"python_context",
"{}", res.get_error().get());
173 Returns the assignment constraint as a pair of Boolean functions.
175 :returns: The assignment constraint on success, ``None`` otherwise.
176 :rtype: tuple(hal_py.BooleanFunction,hal_py.BooleanFunction) or None
179 py_smt_constraint.def(
181 [](
const SMT::Constraint&
self) -> std::optional<const BooleanFunction*> {
182 auto res =
self.get_function();
189 log_error(
"python_context",
"{}", res.get_error().get());
194 Returns the function constraint.
196 :returns: The function constraint on success, ``None`` otherwise.
197 :rtype: hal_py.BooleanFunction or None
201 Translates the SMT constraint into its string representation.
203 :returns: A string representing the SMT constraint.
208 Translates the SMT constraint into its string representation.
210 :returns: A string representing the SMT constraint.
214 py::enum_<SMT::SolverResultType> py_smt_result_type(py_smt, "SolverResultType", R
"(
215 Result type of an SMT solver query.
223 py::class_<SMT::Model> py_smt_model(py_smt, "Model", R
"(
224 Represents a list of assignments for variable nodes that yield a satisfiable assignment for a given list of constraints.
227 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
"(
228 Constructs a new model from a map of variable names to value and bit-size.
230 :param dict[str,tuple(int,int)] model: A dict from variable name to value and bit-size.
233 py_smt_model.def(py::self == py::self, R"(
234 Checks whether two SMT models are equal.
236 :returns: ``True`` if both models are equal, ``False`` otherwise.
240 py_smt_model.def(py::self != py::self, R"(
241 Checks whether two SMT models are unequal.
243 :returns: ``True`` if both models are unequal, ``False`` otherwise.
248 A dict from variable identifiers to a (1) value and (2) its bit-size.
250 :type: dict(str,tuple(int,int))
253 py_smt_model.def_static(
255 [](
const std::string& model_str,
const SMT::SolverType& solver) -> std::optional<SMT::Model> {
263 log_error(
"python_context",
"{}", res.get_error().get());
267 py::arg(
"model_str"),
270 Parses an SMT-Lib model from a string output by a solver of the given type.
272 :param str model_str: The SMT-Lib model string.
273 :param hal_py.SMT.SolverType solver: The solver that computed the model.
274 :returns: The model on success, ``None`` otherwise.
275 :rtype: hal_py.SMT.Model or None
281 auto res =
self.evaluate(bf);
288 log_error(
"python_context",
"{}", res.get_error().get());
294 Evaluates the given Boolean function by replacing all variables contained in the model with their corresponding value and simplifying the result.
296 :param hal_py.BooleanFunction bf: The Boolean function to evaluate.
297 :returns: The evaluated function on success, ``None`` otherwise.
298 :rtype: hal_py.BooleanFunction or None
302 Translates the SMT model into its string representation.
304 :returns: A string representing the SMT model.
309 Translates the SMT model into its string representation.
311 :returns: A string representing the SMT model.
315 py::class_<SMT::SolverResult> py_smt_result(py_smt, "SolverResult", R
"(
316 Represents the result of an SMT query.
320 Result type of the SMT query.
322 :type: hal_py.SMT.ResultType
326 The (optional) model that is only available if type == SMT.ResultType.Sat and model generation is enabled.
328 :type: hal_py.SMT.Model
331 py_smt_result.def_static("Sat", &
SMT::SolverResult::Sat, py::arg(
"model") = std::optional<SMT::Model>(), R
"(
332 Creates a satisfiable result with an optional model.
334 :param hal_py.SMT.Model model: Optional model for satisfiable formula.
335 :returns: The result.
336 :rtype: hal_py.SMT.SolverResult
340 Creates an unsatisfiable result.
342 :returns: The result.
343 :rtype: hal_py.SMT.SolverResult
347 Creates an unknown result.
349 :returns: The result.
350 :rtype: hal_py.SMT.SolverResult
354 Checks whether the result is of a specific type.
356 :param hal_py.SMT.ResultType type: The type to check.
357 :returns: ``True`` in case result matches the given type, ``False`` otherwise.
362 Checks whether the result is satisfiable.
364 :returns: ``True`` in case result is satisfiable, ``False`` otherwise.
369 Checks whether the result is unsatisfiable.
371 :returns: ``True`` in case result is unsatisfiable, ``False`` otherwise.
376 Checks whether the result is unknown.
378 :returns: ``True`` in case result is unknown, ``False`` otherwise.
383 Translates the SMT result into its string representation.
385 :returns: A string representing the SMT result.
390 Translates the SMT result into its string representation.
392 :returns: A string representing the SMT result.
396 py::class_<SMT::Solver> py_smt_solver(py_smt, "Solver", R
"(
397 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.
400 py_smt_solver.def(py::init<std::vector<SMT::Constraint>>(), py::arg("constraints") = std::vector<SMT::Constraint>(), R
"(
401 Constructs an solver with an optional list of constraints.
403 :param list[hal_py.SMT.Constraint] constraints: The (optional) list of constraints.
407 The list of constraints.
409 :type: list[hal_py.SMT.Constraint]
413 Returns the list of constraints.
415 :returns: The list of constraints.
416 :rtype: list[hal_py.SMT.Constraint]
420 Adds a constraint to the SMT solver.
422 :param hal_py.SMT.Constraint constraint: The constraint.
423 :returns: The updated SMT solver.
424 :rtype: hal_py.SMT.Solver
428 Adds a list of constraints to the SMT solver.
430 :param list[hal_py.SMT.Constraint] constraints: The constraints.
431 :returns: The updated SMT solver.
432 :rtype: hal_py.SMT.Solver
436 Checks whether a SMT solver of the given type is available on the local machine.
438 :param hal_py.SMT.SolverType type: The SMT solver type.
439 :param hal_py.SMT.SolverCall call: The solver call.
440 :returns: ``True`` if an SMT solver of the requested type is available, ``False`` otherwise.
447 auto res =
self.query(config);
454 log_error(
"python_context",
"{}", res.get_error().get());
460 Queries an SMT solver with the specified query configuration.
462 :param hal_py.SMT.QueryConfig config: The SMT solver query configuration.
463 :returns: The result on success, a string error message otherwise.
464 :rtype: hal_py.SMT.Result or str
470 auto res =
self.query_local(config);
477 log_error(
"python_context",
"{}", res.get_error().get());
483 Queries a local SMT solver with the specified query configuration.
485 :param hal_py.SMT.QueryConfig config: The SMT solver query configuration.
486 :returns: The result on success, a string error message otherwise.
487 :rtype: hal_py.SMT.Result or str
493 auto res =
self.to_smt2(config);
498 log_error(
"python_context",
"{}", res.get_error().get());
503 Translate the constraints of the solver into an smt2 representation of the query.
505 :param hal_py.SMT.QueryConfig config: The SMT solver query configuration.
506 :returns: The smt2 representation on success, ``None`` otherwise.
510 py_smt_solver.def_static(
511 "query_local_with_smt2",
512 [](
const SMT::QueryConfig& config,
const std::string& smt2) -> std::optional<SMT::SolverResult> {
520 log_error(
"python_context",
"{}", res.get_error().get());
527 Queries a local SMT solver with the specified query configuration and the provided smt2 representation of the query.
529 :param hal_py.SMT.QueryConfig config: The SMT solver query configuration.
530 :param str smt2: The SMT solver query as smt2 string.
531 :returns: The result on success, a string error message otherwise.
532 :rtype: hal_py.SMT.Result or str
536 Queries a remote SMT solver with the specified query configuration.
538 WARNING: This function is not yet implemented.
540 :param hal_py.SMT.QueryConfig config: The SMT solver query configuration.
541 :returns: The result on success, a string error message otherwise.
542 :rtype: hal_py.SMT.Result or str
545 py::class_<SMT::SymbolicState> py_smt_symbolic_state(py_smt, "SymbolicState", R
"(
546 Represents the data structure that keeps track of symbolic variable values (e.g., required for symbolic simplification).
549 py_smt_symbolic_state.def(py::init<const std::vector<BooleanFunction>&>(), py::arg(
"variables") = std::vector<BooleanFunction>(), R
"(
550 Constructs a symbolic state and (optionally) initializes the variables.
552 :param list[hal_py.BooleanFunction] variables: The (optional) list of variables.
556 Looks up a Boolean function in the symbolic state.
558 :param hal_py.BooleanFunction key: The Boolean function to look up.
559 :returns: The Boolean function from the symbolic state or the key itself if it is not contained in the symbolic state.
560 :rtype: hal_py.BooleanFunction
564 Sets a Boolean function equivalent in the symbolic state.
566 :param hal_py.BooleanFunction key: The Boolean function.
567 :param hal_py.BooleanFunction value: The equivalent Boolean function.
570 py::class_<SMT::SymbolicExecution> py_smt_symbolic_execution(py_smt, "SymbolicExecution", R
"(
571 Represents the symbolic execution engine that handles the evaluation and simplification of Boolean function abstract syntax trees.
575 The current symbolic state.
577 :type: hal_py.SMT.SymbolicState
580 py_smt_symbolic_execution.def(py::init<const std::vector<BooleanFunction>&>(), py::arg(
"variables") = std::vector<BooleanFunction>(), R
"(
581 Creates a symbolic execution engine and (optionally) initializes the variables.
583 :param list[hal_py.BooleanFunction] variables: The (optional) list of variables.
586 py_smt_symbolic_execution.def(
589 auto res =
self.evaluate(
function);
594 log_error(
"python_context",
"{}", res.get_error().get());
599 Evaluates a Boolean function within the symbolic state of the symbolic execution.
601 :param hal_py.BooleanFunction function: The Boolean function to evaluate.
602 :returns: The evaluated Boolean function on success, ``None`` otherwise.
603 :rtype: hal_py.BooleanFunction or None
606 py_smt_symbolic_execution.def(
609 auto res =
self.evaluate(constraint);
614 log_error(
"python_context",
"{}", res.get_error().get());
617 py::arg(
"constraint"),
619 Evaluates an equality constraint and applies it to the symbolic state of the symbolic execution.
621 :param hal_py.SMT.Constraint constraint: The equality constraint to evaluate.
622 :returns: ``True`` on success, ``False`` otherwise.
static std::string to_string(Value value)
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)
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)
std::string to_string() const
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.
std::string to_string() const
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()
std::string to_string() const
QueryConfig & with_local_solver()
QueryConfig & with_timeout(u64 seconds)
QueryConfig & with_solver(SolverType solver)
QueryConfig & with_call(SolverCall call)
static SolverResult Unknown()
std::string to_string() const
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.