3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-04-29 23:33:38 +00:00

Adopt std::optional for try_get_value and try_get_size functions (#8268)

* Initial plan

* Convert try_get_value and try_get_size to use std::optional

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>

* Add unit tests for std::optional conversions and fix compilation error

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>

* Address code review comments - improve readability and reduce code duplication

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>
This commit is contained in:
Copilot 2026-01-21 12:41:50 -08:00 committed by GitHub
parent 2e7b700769
commit 1bb471447e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 92 additions and 44 deletions

View file

@ -50,7 +50,10 @@ namespace datalog {
sort_domain(sort_kind k, context & ctx, sort * s)
: m_kind(k), m_sort(s, ctx.get_manager()) {
m_limited_size = ctx.get_decl_util().try_get_size(s, m_size);
auto opt_size = ctx.get_decl_util().try_get_size(s);
m_limited_size = opt_size.has_value();
if (m_limited_size)
m_size = *opt_size;
}
public:
virtual ~sort_domain() = default;

View file

@ -1125,13 +1125,12 @@ protected:
\brief Make a constant for DK_SYMBOL sort out of an integer
*/
app* mk_symbol_const(uint64_t el, sort* s) {
uint64_t sz = 0;
if (m_arith.is_int(s))
return m_arith.mk_numeral(rational(el, rational::ui64()), s);
else if (m_decl_util.try_get_size(s, sz)) {
if (el >= sz) {
else if (auto sz = m_decl_util.try_get_size(s)) {
if (el >= *sz) {
std::ostringstream ous;
ous << "numeric value " << el << " is out of bounds of domain size " << sz;
ous << "numeric value " << el << " is out of bounds of domain size " << *sz;
throw default_exception(ous.str());
}
return m_decl_util.mk_numeral(el, s);

View file

@ -400,7 +400,11 @@ namespace datalog {
}
bool relation_manager::relation_sort_to_table(const relation_sort & from, table_sort & to) {
return get_context().get_decl_util().try_get_size(from, to);
if (auto size = get_context().get_decl_util().try_get_size(from)) {
to = *size;
return true;
}
return false;
}
void relation_manager::from_predicate(func_decl * pred, unsigned arg_index, relation_sort & result) {

View file

@ -248,6 +248,17 @@ namespace datalog {
SASSERT(dl.is_finite_sort(s));
return dl.mk_numeral(r.get_uint64(), s);
}
// Helper function to count bits needed to represent a size
unsigned udoc_plugin::count_bits_for_size(uint64_t size) const {
unsigned num_bits = 0;
while (size > 0) {
++num_bits;
size /= 2;
}
return num_bits;
}
bool udoc_plugin::is_numeral(expr* e, rational& r, unsigned& num_bits) {
if (bv.is_numeral(e, r, num_bits)) return true;
if (m.is_true(e)) {
@ -260,12 +271,13 @@ namespace datalog {
num_bits = 1;
return true;
}
uint64_t n, sz;
if (dl.is_numeral(e, n) && dl.try_get_size(e->get_sort(), sz)) {
num_bits = 0;
while (sz > 0) ++num_bits, sz = sz/2;
r = rational(n, rational::ui64());
return true;
uint64_t n;
if (dl.is_numeral(e, n)) {
if (auto sz = dl.try_get_size(e->get_sort())) {
num_bits = count_bits_for_size(*sz);
r = rational(n, rational::ui64());
return true;
}
}
return false;
}
@ -275,10 +287,8 @@ namespace datalog {
return bv.get_bv_size(s);
if (m.is_bool(s))
return 1;
uint64_t sz;
if (dl.try_get_size(s, sz)) {
while (sz > 0) ++num_bits, sz /= 2;
return num_bits;
if (auto sz = dl.try_get_size(s)) {
return count_bits_for_size(*sz);
}
UNREACHABLE();
return 0;

View file

@ -105,6 +105,7 @@ namespace datalog {
static udoc_relation const & get(relation_base const& r);
void mk_union(doc_manager& dm, udoc& dst, udoc const& src, udoc* delta);
bool is_numeral(expr* e, rational& r, unsigned& num_bits);
unsigned count_bits_for_size(uint64_t size) const;
unsigned num_sort_bits(expr* e) const { return num_sort_bits(e->get_sort()); }
unsigned num_sort_bits(sort* s) const;
bool is_finite_sort(sort* s) const;