#include "Forwards.hpp"
#include "Lib/DHSet.hpp"
#include "Inferences/InductionHelper.hpp"
#include "Kernel/Clause.hpp"
#include "Kernel/EqHelper.hpp"
#include "Kernel/Term.hpp"
#include "Kernel/TermIterators.hpp"
#include "TermSubstitutionTree.hpp"
#include "CodeTreeInterfaces.hpp"
#include "Saturation/SaturationAlgorithm.hpp"
#include "TermIndex.hpp"
namespace Indexing {
using namespace Lib;
using namespace Kernel;
using namespace Inferences;
SuperpositionSubtermIndex::SuperpositionSubtermIndex(SaturationAlgorithm& salg)
: TermIndex(new TermSubstitutionTree<TermLiteralClause>), _ord(salg.getOrdering()) {}
void SuperpositionSubtermIndex::handleClause(Clause* c, bool adding)
{
TIME_TRACE("backward superposition index maintenance");
unsigned selCnt=c->numSelected();
for (unsigned i=0; i<selCnt; i++) {
Literal* lit=(*c)[i];
auto rsti = EqHelper::getSubtermIterator(lit, _ord);
while (rsti.hasNext()) {
auto tt = TypedTermList(rsti.next());
((TermSubstitutionTree<TermLiteralClause>*)&*_is)->handle(TermLiteralClause{ tt, lit, c }, adding);
}
}
}
SuperpositionLHSIndex::SuperpositionLHSIndex(SaturationAlgorithm& salg)
: TermIndex(new TermSubstitutionTree<TermLiteralClause>),
_ord(salg.getOrdering()), _opt(salg.getOptions()) {}
void SuperpositionLHSIndex::handleClause(Clause* c, bool adding)
{
TIME_TRACE("forward superposition index maintenance");
unsigned selCnt=c->numSelected();
for (unsigned i=0; i<selCnt; i++) {
Literal* lit=(*c)[i];
auto lhsi = EqHelper::getSuperpositionLHSIterator(lit, _ord, _opt);
while (lhsi.hasNext()) {
_is->handle(TermLiteralClause{ lhsi.next(), lit, c }, adding);
}
}
}
DemodulationSubtermIndex::DemodulationSubtermIndex(SaturationAlgorithm& salg)
: TermIndex(new TermSubstitutionTree<TermLiteralClause>()),
_skipNonequationalLiterals(salg.getOptions().demodulationOnlyEquational()) {};
void DemodulationSubtermIndex::handleClause(Clause* c, bool adding)
{
TIME_TRACE("backward demodulation index maintenance");
static DHSet<Term*> inserted;
unsigned cLen=c->length();
for (unsigned i=0; i<cLen; i++) {
inserted.reset();
Literal* lit=(*c)[i];
if (lit->isAnswerLiteral()) {
continue;
}
if (_skipNonequationalLiterals && !lit->isEquality()) {
continue;
}
NonVariableNonTypeIterator it(lit);
while (it.hasNext()) {
Term* t= it.next();
if (!inserted.insert(t)) { it.right();
continue;
}
if (adding) {
_is->insert(TermLiteralClause{ t, lit, c });
} else {
_is->remove(TermLiteralClause{ t, lit, c });
}
}
}
}
DemodulationLHSIndex::DemodulationLHSIndex(SaturationAlgorithm& salg)
: TermIndex(new CodeTreeTIS<DemodulatorData>()), _ord(salg.getOrdering()),
_preordered(salg.getOptions().forwardDemodulation()==Options::Demodulation::PREORDERED) {};
void DemodulationLHSIndex::handleClause(Clause* c, bool adding)
{
if (c->length()!=1) {
return;
}
TIME_TRACE("forward demodulation index maintenance");
Literal* lit=(*c)[0];
auto [lhsi, preordered] = EqHelper::getDemodulationLHSIterator(lit, _preordered, _ord);
while (lhsi.hasNext()) {
auto lhs = lhsi.next();
Renaming r;
r.normalizeVariables(lhs);
DemodulatorData dd(
TypedTermList(r.apply(lhs),r.apply(lhs.sort())),
r.apply(EqHelper::getOtherEqualitySide(lit, lhs)),
c, preordered, _ord
);
_is->handle(std::move(dd), adding);
}
}
InductionTermIndex::InductionTermIndex(SaturationAlgorithm& salg)
: TermIndex(new TermSubstitutionTree<TermLiteralClause>()), _inductionGroundOnly(salg.getOptions().inductionGroundOnly()) {}
void InductionTermIndex::handleClause(Clause* c, bool adding)
{
TIME_TRACE("induction term index maintenance");
if (!InductionHelper::isInductionClause(c)) {
return;
}
for (const auto& lit : *c) {
if (_inductionGroundOnly && !lit->ground()) {
continue;
}
if (!InductionHelper::isInductionLiteral(lit)) {
continue;
}
DHSet<Term*> done;
NonVariableNonTypeIterator it(lit);
while (it.hasNext()) {
Term* t = it.next();
if (!done.insert(t)) {
it.right();
continue;
}
if (InductionHelper::isInductionTerm(t) &&
InductionHelper::isIntInductionTermListInLiteral(t, lit)) {
if (adding) {
_is->insert(TermLiteralClause{ t, lit, c });
} else {
_is->remove(TermLiteralClause{ t, lit, c });
}
}
}
}
}
StructInductionTermIndex::StructInductionTermIndex(SaturationAlgorithm& salg)
: TermIndex(new TermSubstitutionTree<TermLiteralClause>()), _inductionGroundOnly(salg.getOptions().inductionGroundOnly()) {}
void StructInductionTermIndex::handleClause(Clause* c, bool adding)
{
if (!InductionHelper::isInductionClause(c)) {
return;
}
for (const auto& lit : *c) {
if (_inductionGroundOnly && !lit->ground()) {
continue;
}
DHSet<Term*> done;
NonVariableNonTypeIterator it(lit);
while (it.hasNext()) {
Term* t = it.next();
if (!done.insert(t)) {
it.right();
continue;
}
if (InductionHelper::isInductionTerm(t) &&
InductionHelper::isStructInductionTerm(t)) {
if (adding) {
_is->insert(TermLiteralClause{ t, lit, c });
} else {
_is->remove(TermLiteralClause{ t, lit, c });
}
}
}
}
}
SkolemisingFormulaIndex::SkolemisingFormulaIndex(SaturationAlgorithm&)
: TermIndex(new TermSubstitutionTree<TermWithValue<TermList>>()) {}
void SkolemisingFormulaIndex::insertFormula(TermList formula, TermList skolem)
{
_is->insert(TermWithValue<TermList>(TypedTermList(formula.term()), skolem));
}
}