From 80be200c84494a4f82ab30cb743b7758c67db5b5 Mon Sep 17 00:00:00 2001 From: Haniel Barbosa Date: Thu, 16 Aug 2018 11:18:11 -0500 Subject: Refactor apply2const (#2316) --- src/Makefile.am | 2 + src/preprocessing/passes/apply_to_const.cpp | 108 ++++++++++++++++++++++++++++ src/preprocessing/passes/apply_to_const.h | 51 +++++++++++++ src/smt/smt_engine.cpp | 69 ++---------------- 4 files changed, 167 insertions(+), 63 deletions(-) create mode 100644 src/preprocessing/passes/apply_to_const.cpp create mode 100644 src/preprocessing/passes/apply_to_const.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 43aa70174..c7dc311b6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -63,6 +63,8 @@ libcvc4_la_SOURCES = \ decision/justification_heuristic.h \ preprocessing/passes/apply_substs.cpp \ preprocessing/passes/apply_substs.h \ + preprocessing/passes/apply_to_const.cpp \ + preprocessing/passes/apply_to_const.h \ preprocessing/passes/bv_abstraction.cpp \ preprocessing/passes/bv_abstraction.h \ preprocessing/passes/bv_ackermann.cpp \ diff --git a/src/preprocessing/passes/apply_to_const.cpp b/src/preprocessing/passes/apply_to_const.cpp new file mode 100644 index 000000000..bbe4439ec --- /dev/null +++ b/src/preprocessing/passes/apply_to_const.cpp @@ -0,0 +1,108 @@ +/********************* */ +/*! \file apply_to_const.cpp + ** \verbatim + ** Top contributors (to current version): + ** Haniel Barbosa + ** This file is part of the CVC4 project. + ** Copyright (c) 2009-2018 by the authors listed in the file AUTHORS + ** in the top-level source directory) and their institutional affiliations. + ** All rights reserved. See the file COPYING in the top-level source + ** directory for licensing information.\endverbatim + ** + ** \brief The ApplyToConst preprocessing pass + ** + ** Rewrites applies to constants + **/ + +#include "preprocessing/passes/apply_to_const.h" + +#include "theory/rewriter.h" + +namespace CVC4 { +namespace preprocessing { +namespace passes { + +using namespace CVC4::theory; + +ApplyToConst::ApplyToConst(PreprocessingPassContext* preprocContext) + : PreprocessingPass(preprocContext, "apply-to-const"){}; + +Node ApplyToConst::rewriteApplyToConst(TNode n, NodeMap& cache) +{ + Trace("rewriteApplyToConst") << "rewriteApplyToConst :: " << n << std::endl; + + if (n.getMetaKind() == kind::metakind::CONSTANT + || n.getMetaKind() == kind::metakind::VARIABLE + || n.getMetaKind() == kind::metakind::NULLARY_OPERATOR) + { + return n; + } + + if (cache.find(n) != cache.end()) + { + Trace("rewriteApplyToConst") << "in cache :: " << cache[n] << std::endl; + return cache[n]; + } + + if (n.getKind() == kind::APPLY_UF) + { + if (n.getNumChildren() == 1 && n[0].isConst() && n[0].getType().isInteger()) + { + stringstream ss; + ss << n.getOperator() << "_"; + if (n[0].getConst() < 0) + { + ss << "m" << -n[0].getConst(); + } + else + { + ss << n[0]; + } + Node newvar = + NodeManager::currentNM()->mkSkolem(ss.str(), + n.getType(), + "rewriteApplyToConst skolem", + NodeManager::SKOLEM_EXACT_NAME); + cache[n] = newvar; + Trace("rewriteApplyToConst") << "made :: " << newvar << std::endl; + return newvar; + } + stringstream ss; + ss << "The rewrite-apply-to-const preprocessor is currently limited;\n" + << "it only works if all function symbols are unary and with Integer\n" + << "domain, and all applications are to integer values.\n" + << "Found application: " << n; + Unhandled(ss.str()); + } + + NodeBuilder<> builder(n.getKind()); + if (n.getMetaKind() == kind::metakind::PARAMETERIZED) + { + builder << n.getOperator(); + } + for (unsigned i = 0; i < n.getNumChildren(); ++i) + { + builder << rewriteApplyToConst(n[i], cache); + } + Node rewr = builder; + cache[n] = rewr; + Trace("rewriteApplyToConst") << "built :: " << rewr << std::endl; + return rewr; +} + +PreprocessingPassResult ApplyToConst::applyInternal( + AssertionPipeline* assertionsToPreprocess) +{ + NodeMap cache; + for (unsigned i = 0, size = assertionsToPreprocess->size(); i < size; ++i) + { + assertionsToPreprocess->replace(i, + Rewriter::rewrite(rewriteApplyToConst( + (*assertionsToPreprocess)[i], cache))); + } + return PreprocessingPassResult::NO_CONFLICT; +} + +} // namespace passes +} // namespace preprocessing +} // namespace CVC4 diff --git a/src/preprocessing/passes/apply_to_const.h b/src/preprocessing/passes/apply_to_const.h new file mode 100644 index 000000000..9d5072023 --- /dev/null +++ b/src/preprocessing/passes/apply_to_const.h @@ -0,0 +1,51 @@ +/********************* */ +/*! \file apply_to_const.h + ** \verbatim + ** Top contributors (to current version): + ** Haniel Barbosa + ** This file is part of the CVC4 project. + ** Copyright (c) 2009-2018 by the authors listed in the file AUTHORS + ** in the top-level source directory) and their institutional affiliations. + ** All rights reserved. See the file COPYING in the top-level source + ** directory for licensing information.\endverbatim + ** + ** \brief The ApplyToConst preprocessing pass + ** + ** Rewrites applies to constants + **/ + +#include "cvc4_private.h" + +#ifndef __CVC4__PREPROCESSING__PASSES__APPLY_TO_CONST_H +#define __CVC4__PREPROCESSING__PASSES__APPLY_TO_CONST_H + +#include + +#include "expr/node.h" +#include "preprocessing/preprocessing_pass.h" +#include "preprocessing/preprocessing_pass_context.h" + +namespace CVC4 { +namespace preprocessing { +namespace passes { + +using NodeMap = std::unordered_map; + +class ApplyToConst : public PreprocessingPass +{ + public: + ApplyToConst(PreprocessingPassContext* preprocContext); + + protected: + PreprocessingPassResult applyInternal( + AssertionPipeline* assertionsToPreprocess) override; + + private: + Node rewriteApplyToConst(TNode n, NodeMap& cache); +}; + +} // namespace passes +} // namespace preprocessing +} // namespace CVC4 + +#endif /* __CVC4__PREPROCESSING__PASSES__APPLY_TO_CONST_H */ diff --git a/src/smt/smt_engine.cpp b/src/smt/smt_engine.cpp index db4efe89f..41b113b94 100644 --- a/src/smt/smt_engine.cpp +++ b/src/smt/smt_engine.cpp @@ -70,6 +70,7 @@ #include "options/theory_options.h" #include "options/uf_options.h" #include "preprocessing/passes/apply_substs.h" +#include "preprocessing/passes/apply_to_const.h" #include "preprocessing/passes/bool_to_bv.h" #include "preprocessing/passes/bv_abstraction.h" #include "preprocessing/passes/bv_ackermann.h" @@ -897,66 +898,6 @@ public: return retval; } - NodeToNodeHashMap d_rewriteApplyToConstCache; - Node rewriteApplyToConst(TNode n) { - Trace("rewriteApplyToConst") << "rewriteApplyToConst :: " << n << std::endl; - - if(n.getMetaKind() == kind::metakind::CONSTANT || - n.getMetaKind() == kind::metakind::VARIABLE || - n.getMetaKind() == kind::metakind::NULLARY_OPERATOR) - { - return n; - } - - if(d_rewriteApplyToConstCache.find(n) != d_rewriteApplyToConstCache.end()) { - Trace("rewriteApplyToConst") << "in cache :: " - << d_rewriteApplyToConstCache[n] - << std::endl; - return d_rewriteApplyToConstCache[n]; - } - - if(n.getKind() == kind::APPLY_UF) { - if(n.getNumChildren() == 1 && n[0].isConst() && - n[0].getType().isInteger()) - { - stringstream ss; - ss << n.getOperator() << "_"; - if(n[0].getConst() < 0) { - ss << "m" << -n[0].getConst(); - } else { - ss << n[0]; - } - Node newvar = NodeManager::currentNM()->mkSkolem( - ss.str(), n.getType(), "rewriteApplyToConst skolem", - NodeManager::SKOLEM_EXACT_NAME); - d_rewriteApplyToConstCache[n] = newvar; - Trace("rewriteApplyToConst") << "made :: " << newvar << std::endl; - return newvar; - } else { - stringstream ss; - ss << "The rewrite-apply-to-const preprocessor is currently limited;" - << std::endl - << "it only works if all function symbols are unary and with Integer" - << std::endl - << "domain, and all applications are to integer values." << std::endl - << "Found application: " << n; - Unhandled(ss.str()); - } - } - - NodeBuilder<> builder(n.getKind()); - if(n.getMetaKind() == kind::metakind::PARAMETERIZED) { - builder << n.getOperator(); - } - for(unsigned i = 0; i < n.getNumChildren(); ++i) { - builder << rewriteApplyToConst(n[i]); - } - Node rewr = builder; - d_rewriteApplyToConstCache[n] = rewr; - Trace("rewriteApplyToConst") << "built :: " << rewr << std::endl; - return rewr; - } - void addUseTheoryListListener(TheoryEngine* theoryEngine){ Options& nodeManagerOptions = NodeManager::currentNM()->getOptions(); d_listenerRegistrations->add( @@ -2717,6 +2658,8 @@ void SmtEnginePrivate::finishInit() // actually assembling preprocessing pipelines). std::unique_ptr applySubsts( new ApplySubsts(d_preprocessingPassContext.get())); + std::unique_ptr applyToConst( + new ApplyToConst(d_preprocessingPassContext.get())); std::unique_ptr boolToBv( new BoolToBV(d_preprocessingPassContext.get())); std::unique_ptr bvAbstract( @@ -2750,6 +2693,8 @@ void SmtEnginePrivate::finishInit() new SepSkolemEmp(d_preprocessingPassContext.get())); d_preprocessingPassRegistry.registerPass("apply-substs", std::move(applySubsts)); + d_preprocessingPassRegistry.registerPass("apply-to-const", + std::move(applyToConst)); d_preprocessingPassRegistry.registerPass("bool-to-bv", std::move(boolToBv)); d_preprocessingPassRegistry.registerPass("bv-abstraction", std::move(bvAbstract)); @@ -4476,9 +4421,7 @@ void SmtEnginePrivate::processAssertions() { if(options::rewriteApplyToConst()) { Chat() << "Rewriting applies to constants..." << endl; TimerStat::CodeTimer codeTimer(d_smt.d_stats->d_rewriteApplyToConstTime); - for (unsigned i = 0; i < d_assertions.size(); ++ i) { - d_assertions[i] = Rewriter::rewrite(rewriteApplyToConst(d_assertions[i])); - } + d_preprocessingPassRegistry.getPass("apply-to-const")->apply(&d_assertions); } dumpAssertions("post-rewrite-apply-to-const", d_assertions); -- cgit v1.2.3