Skip to content

Commit 5e01002

Browse files
Refactor matching logic in solver to improve component handling and simplify equality checks.
1 parent 371c5c3 commit 5e01002

File tree

2 files changed

+11
-37
lines changed

2 files changed

+11
-37
lines changed

src/st/stsolver.cpp

+10-36
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,9 @@ namespace ratio
431431
else if (auto lhs_xpr = dynamic_cast<riddle::atom_term *>(&lhs))
432432
{ // we are dealing with atoms..
433433
auto rhs_xpr = static_cast<riddle::atom_term *>(&rhs);
434+
if (&lhs_xpr->get_type().get_scope() != &rhs_xpr->get_type().get_scope().get_core() && !match(*lhs_xpr->get(riddle::this_kw), *rhs_xpr->get(riddle::this_kw)))
435+
return false; // the atoms are not in the same scope, so they cannot match..
436+
// we check if the atoms' fields match..
434437
std::queue<riddle::predicate *> q;
435438
q.push(static_cast<riddle::predicate *>(&lhs_xpr->get_type()));
436439
while (!q.empty())
@@ -444,29 +447,15 @@ namespace ratio
444447
}
445448
return true;
446449
}
447-
else if (auto lhs_xpr = dynamic_cast<riddle::component *>(&lhs))
448-
{ // we are dealing with components..
449-
auto rhs_xpr = static_cast<riddle::component *>(&rhs);
450-
std::queue<riddle::component_type *> q;
451-
q.push(static_cast<riddle::component_type *>(&lhs_xpr->get_type()));
452-
while (!q.empty())
453-
{
454-
for (const auto &[f_name, f] : q.front()->get_fields())
455-
if (!f->is_synthetic() && !match(*lhs_xpr->get(f_name), *rhs_xpr->get(f_name)))
456-
return false;
457-
for (const auto &pp : q.front()->get_parents())
458-
q.push(&*pp);
459-
q.pop();
460-
}
461-
return true;
462-
}
463-
else // should not happen..
464-
throw std::runtime_error("Invalid type");
450+
else // we are dealing with components (and we have already checked their are not the same)..
451+
return false;
465452
}
466453

467454
void solver::make_eq(riddle::term &lhs, riddle::term &rhs, const utils::lit &p)
468455
{
469-
if (&lhs.get_type() != &rhs.get_type()) // the types are different, so the constraint is always false..
456+
if (&lhs == &rhs) // the terms are the same, so they are equal..
457+
return;
458+
else if (&lhs.get_type() != &rhs.get_type()) // the types are different, so the constraint is always false..
470459
add_clause({!p});
471460
else if (auto lhs_xpr = dynamic_cast<riddle::arith_item *>(&lhs)) // we are dealing with an arithmetic constraint..
472461
add_eq(lhs_xpr->get_lin(), static_cast<riddle::arith_item *>(&rhs)->get_lin(), p);
@@ -536,23 +525,8 @@ namespace ratio
536525
q.pop();
537526
}
538527
}
539-
else if (auto lhs_xpr = dynamic_cast<riddle::component *>(&lhs))
540-
{ // we are dealing with components..
541-
auto rhs_xpr = static_cast<riddle::component *>(&rhs);
542-
std::queue<riddle::component_type *> q;
543-
q.push(static_cast<riddle::component_type *>(&lhs_xpr->get_type()));
544-
while (!q.empty())
545-
{
546-
for (const auto &[f_name, f] : q.front()->get_fields())
547-
if (!f->is_synthetic())
548-
make_eq(*lhs_xpr->get(f_name), *rhs_xpr->get(f_name), p);
549-
for (const auto &pp : q.front()->get_parents())
550-
q.push(&*pp);
551-
q.pop();
552-
}
553-
}
554-
else
555-
throw std::runtime_error("Invalid type");
528+
else // we are dealing with components (and we have already checked their are not the same)..
529+
add_clause({!p});
556530
}
557531

558532
void solver::make_neq(riddle::term &lhs, riddle::term &rhs, const utils::lit &p)

0 commit comments

Comments
 (0)