|
| 1 | +/****************************************************************************** |
| 2 | + * Top contributors (to current version): |
| 3 | + * Andrew Reynolds |
| 4 | + * |
| 5 | + * This file is part of the cvc5 project. |
| 6 | + * |
| 7 | + * Copyright (c) 2009-2023 by the authors listed in the file AUTHORS |
| 8 | + * in the top-level source directory and their institutional affiliations. |
| 9 | + * All rights reserved. See the file COPYING in the top-level source |
| 10 | + * directory for licensing information. |
| 11 | + * **************************************************************************** |
| 12 | + * |
| 13 | + * Learner for literals asserted at level zero. |
| 14 | + */ |
| 15 | + |
| 16 | +#include "prop/lemma_inprocess.h" |
| 17 | + |
| 18 | +#include "expr/node_algorithm.h" |
| 19 | +#include "options/theory_options.h" |
| 20 | +#include "prop/zero_level_learner.h" |
| 21 | +#include "smt/env.h" |
| 22 | + |
| 23 | +namespace cvc5::internal { |
| 24 | +namespace prop { |
| 25 | + |
| 26 | +LemmaInprocess::LemmaInprocess(Env& env, CnfStream* cs, ZeroLevelLearner& zll) |
| 27 | + : EnvObj(env), |
| 28 | + d_cs(cs), |
| 29 | + d_tsmap(zll.getSimplifications()), |
| 30 | + d_subsLitMap(userContext()), |
| 31 | + d_eqLitLemmas(userContext()) |
| 32 | +{ |
| 33 | +} |
| 34 | +TrustNode LemmaInprocess::inprocessLemma(TrustNode& trn) |
| 35 | +{ |
| 36 | + if (trn.getKind() == TrustNodeKind::CONFLICT) |
| 37 | + { |
| 38 | + return trn; |
| 39 | + } |
| 40 | + const Node& proven = trn.getProven(); |
| 41 | + Node provenp = processInternal(proven); |
| 42 | + if (provenp != proven) |
| 43 | + { |
| 44 | + Trace("lemma-inprocess-lemma") |
| 45 | + << "Inprocess " << proven << " returns " << provenp << std::endl; |
| 46 | + // proofs not supported |
| 47 | + return TrustNode::mkTrustNode(trn.getKind(), provenp); |
| 48 | + } |
| 49 | + return trn; |
| 50 | +} |
| 51 | + |
| 52 | +Node LemmaInprocess::processInternal(const Node& lem) |
| 53 | +{ |
| 54 | + std::vector<Node> eqLitLemmas; |
| 55 | + NodeManager* nm = NodeManager::currentNM(); |
| 56 | + std::unordered_map<TNode, Node> visited; |
| 57 | + std::unordered_map<TNode, Node>::iterator it; |
| 58 | + std::vector<TNode> visit; |
| 59 | + context::CDHashMap<Node, Node>::iterator its; |
| 60 | + TNode cur; |
| 61 | + visit.emplace_back(lem); |
| 62 | + do |
| 63 | + { |
| 64 | + cur = visit.back(); |
| 65 | + Trace("lemma-inprocess-visit") << "visit " << cur << std::endl; |
| 66 | + Assert(cur.getType().isBoolean()); |
| 67 | + it = visited.find(cur); |
| 68 | + if (it == visited.end()) |
| 69 | + { |
| 70 | + if (expr::isBooleanConnective(cur)) |
| 71 | + { |
| 72 | + visited[cur] = Node::null(); |
| 73 | + visit.insert(visit.end(), cur.begin(), cur.end()); |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + visit.pop_back(); |
| 78 | + // literal case |
| 79 | + bool prevLit = d_cs->hasLiteral(cur); |
| 80 | + Node scur = d_tsmap.get().apply(cur, d_env.getRewriter()); |
| 81 | + its = d_subsLitMap.find(scur); |
| 82 | + if (its != d_subsLitMap.end()) |
| 83 | + { |
| 84 | + if (cur != its->second) |
| 85 | + { |
| 86 | + Trace("lemma-inprocess") |
| 87 | + << "Replace (indirect): " << cur << " -> " << its->second |
| 88 | + << ", prevLit = " << prevLit << std::endl; |
| 89 | + visited[cur] = its->second; |
| 90 | + continue; |
| 91 | + } |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + bool currLit = prevLit; |
| 96 | + if (scur != cur) |
| 97 | + { |
| 98 | + currLit = d_cs->hasLiteral(scur); |
| 99 | + scur = rewrite(scur); |
| 100 | + Trace("lemma-inprocess-debug") |
| 101 | + << "Inprocess " << cur << " -> " << scur << std::endl; |
| 102 | + bool doReplace = false; |
| 103 | + switch (options().theory.lemmaInprocess) |
| 104 | + { |
| 105 | + case options::LemmaInprocessMode::FULL: |
| 106 | + doReplace = (scur.isConst() || currLit || !prevLit); |
| 107 | + break; |
| 108 | + case options::LemmaInprocessMode::LIGHT: |
| 109 | + doReplace = (scur.isConst() || (currLit && !prevLit)); |
| 110 | + break; |
| 111 | + default: break; |
| 112 | + } |
| 113 | + if (doReplace) |
| 114 | + { |
| 115 | + if (options().theory.lemmaInprocessInferEqLit |
| 116 | + && ((scur.isConst() || currLit) && prevLit)) |
| 117 | + { |
| 118 | + // inferred they are equivalent? maybe should send clause here? |
| 119 | + Node eql = rewrite(scur.eqNode(cur)); |
| 120 | + if (d_eqLitLemmas.find(eql) == d_eqLitLemmas.end()) |
| 121 | + { |
| 122 | + d_eqLitLemmas.insert(eql); |
| 123 | + eqLitLemmas.emplace_back(eql); |
| 124 | + } |
| 125 | + } |
| 126 | + Trace("lemma-inprocess") |
| 127 | + << "Replace: " << cur << " -> " << scur |
| 128 | + << ", currLit = " << currLit << ", prevLit = " << prevLit |
| 129 | + << std::endl; |
| 130 | + visited[cur] = scur; |
| 131 | + d_subsLitMap[scur] = scur; |
| 132 | + continue; |
| 133 | + } |
| 134 | + } |
| 135 | + d_subsLitMap[scur] = cur; |
| 136 | + } |
| 137 | + visited[cur] = cur; |
| 138 | + } |
| 139 | + continue; |
| 140 | + } |
| 141 | + visit.pop_back(); |
| 142 | + if (it->second.isNull()) |
| 143 | + { |
| 144 | + Node ret = cur; |
| 145 | + bool childChanged = false; |
| 146 | + std::vector<Node> children; |
| 147 | + for (const Node& cn : cur) |
| 148 | + { |
| 149 | + it = visited.find(cn); |
| 150 | + Assert(it != visited.end()); |
| 151 | + Assert(!it->second.isNull()); |
| 152 | + childChanged = childChanged || cn != it->second; |
| 153 | + children.push_back(it->second); |
| 154 | + } |
| 155 | + if (childChanged) |
| 156 | + { |
| 157 | + ret = nm->mkNode(cur.getKind(), children); |
| 158 | + ret = rewrite(ret); |
| 159 | + } |
| 160 | + visited[cur] = ret; |
| 161 | + } |
| 162 | + } while (!visit.empty()); |
| 163 | + Assert(visited.find(lem) != visited.end()); |
| 164 | + Assert(!visited.find(lem)->second.isNull()); |
| 165 | + Node ret = visited[lem]; |
| 166 | + if (!eqLitLemmas.empty()) |
| 167 | + { |
| 168 | + eqLitLemmas.emplace_back(ret); |
| 169 | + return nm->mkAnd(eqLitLemmas); |
| 170 | + } |
| 171 | + return ret; |
| 172 | +} |
| 173 | + |
| 174 | +} // namespace prop |
| 175 | +} // namespace cvc5::internal |
0 commit comments