/********************* */ /*! \file cnf_stream.cpp ** \verbatim ** Original author: taking ** Major contributors: dejan ** Minor contributors (to current version): mdeters, cconway ** This file is part of the CVC4 prototype. ** Copyright (c) 2009, 2010 The Analysis of Computer Systems Group (ACSys) ** Courant Institute of Mathematical Sciences ** New York University ** See the file COPYING in the top-level source directory for licensing ** information.\endverbatim ** ** \brief A CNF converter that takes in asserts and has the side effect ** of given an equisatisfiable stream of assertions to PropEngine. ** ** A CNF converter that takes in asserts and has the side effect ** of given an equisatisfiable stream of assertions to PropEngine. **/ #include "sat.h" #include "prop/cnf_stream.h" #include "prop/prop_engine.h" #include "expr/node.h" #include "util/Assert.h" #include "util/output.h" #include using namespace std; using namespace CVC4::kind; namespace CVC4 { namespace prop { CnfStream::CnfStream(SatInputInterface *satSolver) : d_satSolver(satSolver) { } TseitinCnfStream::TseitinCnfStream(SatInputInterface* satSolver) : CnfStream(satSolver) { } void CnfStream::assertClause(TNode node, SatClause& c) { Debug("cnf") << "Inserting into stream " << c << endl; d_satSolver->addClause(c, d_assertingLemma); } void CnfStream::assertClause(TNode node, SatLiteral a) { SatClause clause(1); clause[0] = a; assertClause(node, clause); } void CnfStream::assertClause(TNode node, SatLiteral a, SatLiteral b) { SatClause clause(2); clause[0] = a; clause[1] = b; assertClause(node, clause); } void CnfStream::assertClause(TNode node, SatLiteral a, SatLiteral b, SatLiteral c) { SatClause clause(3); clause[0] = a; clause[1] = b; clause[2] = c; assertClause(node, clause); } bool CnfStream::isCached(TNode n) const { return d_translationCache.find(n) != d_translationCache.end(); } SatLiteral CnfStream::lookupInCache(TNode n) const { Assert(isCached(n), "Node is not in CNF translation cache"); return d_translationCache.find(n)->second; } void CnfStream::cacheTranslation(TNode node, SatLiteral lit) { Debug("cnf") << "caching translation " << node << " to " << lit << endl; // We always cash both the node and the negation at the same time d_translationCache[node] = lit; d_translationCache[node.notNode()] = ~lit; } SatLiteral CnfStream::newLiteral(TNode node, bool theoryLiteral) { SatLiteral lit = SatLiteral(d_satSolver->newVar(theoryLiteral)); cacheTranslation(node, lit); if (theoryLiteral) { d_nodeCache[lit] = node; d_nodeCache[~lit] = node.notNode(); } return lit; } Node CnfStream::getNode(const SatLiteral& literal) { Node node; NodeCache::iterator find = d_nodeCache.find(literal); if(find != d_nodeCache.end()) { node = find->second; } return node; } SatLiteral CnfStream::convertAtom(TNode node) { Assert(!isCached(node), "atom already mapped!"); Debug("cnf") << "convertAtom(" << node << ")" << endl; bool theoryLiteral = node.getKind() != kind::VARIABLE; SatLiteral lit = newLiteral(node, theoryLiteral); if(node.getKind() == kind::CONST_BOOLEAN) { if(node.getConst()) { assertClause(node, lit); } else { assertClause(node, ~lit); } } return lit; } SatLiteral CnfStream::getLiteral(TNode node, bool create /* = false */) { TranslationCache::iterator find = d_translationCache.find(node); SatLiteral literal; if(create) { if(find == d_translationCache.end()) { literal = convertAtom(node); } else { literal = find->second; } } else { Assert(find != d_translationCache.end(), "Literal not in the CNF Cache"); literal = find->second; } Debug("cnf") << "CnfStream::getLiteral(" << node << ", create = " << create << ") => " << literal << std::endl; return literal; } SatLiteral TseitinCnfStream::handleXor(TNode xorNode) { Assert(!isCached(xorNode), "Atom already mapped!"); Assert(xorNode.getKind() == XOR, "Expecting an XOR expression!"); Assert(xorNode.getNumChildren() == 2, "Expecting exactly 2 children!"); SatLiteral a = toCNF(xorNode[0]); SatLiteral b = toCNF(xorNode[1]); SatLiteral xorLit = newLiteral(xorNode); assertClause(xorNode, a, b, ~xorLit); assertClause(xorNode, ~a, ~b, ~xorLit); assertClause(xorNode, a, ~b, xorLit); assertClause(xorNode, ~a, b, xorLit); return xorLit; } SatLiteral TseitinCnfStream::handleOr(TNode orNode) { Assert(!isCached(orNode), "Atom already mapped!"); Assert(orNode.getKind() == OR, "Expecting an OR expression!"); Assert(orNode.getNumChildren() > 1, "Expecting more then 1 child!"); // Number of children unsigned n_children = orNode.getNumChildren(); // Transform all the children first TNode::const_iterator node_it = orNode.begin(); TNode::const_iterator node_it_end = orNode.end(); SatClause clause(n_children + 1); for(int i = 0; node_it != node_it_end; ++node_it, ++i) { clause[i] = toCNF(*node_it); } // Get the literal for this node SatLiteral orLit = newLiteral(orNode); // lit <- (a_1 | a_2 | a_3 | ... | a_n) // lit | ~(a_1 | a_2 | a_3 | ... | a_n) // (lit | ~a_1) & (lit | ~a_2) & (lit & ~a_3) & ... & (lit & ~a_n) for(unsigned i = 0; i < n_children; ++i) { assertClause(orNode, orLit, ~clause[i]); } // lit -> (a_1 | a_2 | a_3 | ... | a_n) // ~lit | a_1 | a_2 | a_3 | ... | a_n clause[n_children] = ~orLit; // This needs to go last, as the clause might get modified by the SAT solver assertClause(orNode, clause); // Return the literal return orLit; } SatLiteral TseitinCnfStream::handleAnd(TNode andNode) { Assert(!isCached(andNode), "Atom already mapped!"); Assert(andNode.getKind() == AND, "Expecting an AND expression!"); Assert(andNode.getNumChildren() > 1, "Expecting more than 1 child!"); // Number of children unsigned n_children = andNode.getNumChildren(); // Transform all the children first (remembering the negation) TNode::const_iterator node_it = andNode.begin(); TNode::const_iterator node_it_end = andNode.end(); SatClause clause(n_children + 1); for(int i = 0; node_it != node_it_end; ++node_it, ++i) { clause[i] = ~toCNF(*node_it); } // Get the literal for this node SatLiteral andLit = newLiteral(andNode); // lit -> (a_1 & a_2 & a_3 & ... & a_n) // ~lit | (a_1 & a_2 & a_3 & ... & a_n) // (~lit | a_1) & (~lit | a_2) & ... & (~lit | a_n) for(unsigned i = 0; i < n_children; ++i) { assertClause(andNode, ~andLit, ~clause[i]); } // lit <- (a_1 & a_2 & a_3 & ... a_n) // lit | ~(a_1 & a_2 & a_3 & ... & a_n) // lit | ~a_1 | ~a_2 | ~a_3 | ... | ~a_n clause[n_children] = andLit; // This needs to go last, as the clause might get modified by the SAT solver assertClause(andNode, clause); return andLit; } SatLiteral TseitinCnfStream::handleImplies(TNode impliesNode) { Assert(!isCached(impliesNode), "Atom already mapped!"); Assert(impliesNode.getKind() == IMPLIES, "Expecting an IMPLIES expression!"); Assert(impliesNode.getNumChildren() == 2, "Expecting exactly 2 children!"); // Convert the children to cnf SatLiteral a = toCNF(impliesNode[0]); SatLiteral b = toCNF(impliesNode[1]); SatLiteral impliesLit = newLiteral(impliesNode); // lit -> (a->b) // ~lit | ~ a | b assertClause(impliesNode, ~impliesLit, ~a, b); // (a->b) -> lit // ~(~a | b) | lit // (a | l) & (~b | l) assertClause(impliesNode, a, impliesLit); assertClause(impliesNode, ~b, impliesLit); return impliesLit; } SatLiteral TseitinCnfStream::handleIff(TNode iffNode) { Assert(!isCached(iffNode), "Atom already mapped!"); Assert(iffNode.getKind() == IFF, "Expecting an IFF expression!"); Assert(iffNode.getNumChildren() == 2, "Expecting exactly 2 children!"); // Convert the children to CNF SatLiteral a = toCNF(iffNode[0]); SatLiteral b = toCNF(iffNode[1]); // Get the now literal SatLiteral iffLit = newLiteral(iffNode); // lit -> ((a-> b) & (b->a)) // ~lit | ((~a | b) & (~b | a)) // (~a | b | ~lit) & (~b | a | ~lit) assertClause(iffNode, ~a, b, ~iffLit); assertClause(iffNode, a, ~b, ~iffLit); // (a<->b) -> lit // ~((a & b) | (~a & ~b)) | lit // (~(a & b)) & (~(~a & ~b)) | lit // ((~a | ~b) & (a | b)) | lit // (~a | ~b | lit) & (a | b | lit) assertClause(iffNode, ~a, ~b, iffLit); assertClause(iffNode, a, b, iffLit); return iffLit; } SatLiteral TseitinCnfStream::handleNot(TNode notNode) { Assert(!isCached(notNode), "Atom already mapped!"); Assert(notNode.getKind() == NOT, "Expecting a NOT expression!"); Assert(notNode.getNumChildren() == 1, "Expecting exactly 1 child!"); SatLiteral notLit = ~toCNF(notNode[0]); // Since we don't introduce new variables, we need to cache the translation cacheTranslation(notNode, notLit); return notLit; } SatLiteral TseitinCnfStream::handleIte(TNode iteNode) { Assert(iteNode.getKind() == ITE); Assert(iteNode.getNumChildren() == 3); Debug("cnf") << "handlIte(" << iteNode[0] << " " << iteNode[1] << " " << iteNode[2] << ")" << endl; SatLiteral condLit = toCNF(iteNode[0]); SatLiteral thenLit = toCNF(iteNode[1]); SatLiteral elseLit = toCNF(iteNode[2]); SatLiteral iteLit = newLiteral(iteNode); // If ITE is true then one of the branches is true and the condition // implies which one // lit -> (ite b t e) // lit -> (t | e) & (b -> t) & (!b -> e) // lit -> (t | e) & (!b | t) & (b | e) // (!lit | t | e) & (!lit | !b | t) & (!lit | b | e) assertClause(iteNode, ~iteLit, thenLit, elseLit); assertClause(iteNode, ~iteLit, ~condLit, thenLit); assertClause(iteNode, ~iteLit, condLit, elseLit); // If ITE is false then one of the branches is false and the condition // implies which one // !lit -> !(ite b t e) // !lit -> (!t | !e) & (b -> !t) & (!b -> !e) // !lit -> (!t | !e) & (!b | !t) & (b | !e) // (lit | !t | !e) & (lit | !b | !t) & (lit | b | !e) assertClause(iteNode, iteLit, ~thenLit, ~elseLit); assertClause(iteNode, iteLit, ~condLit, ~thenLit); assertClause(iteNode, iteLit, condLit, ~elseLit); return iteLit; } SatLiteral TseitinCnfStream::toCNF(TNode node, bool negated) { Debug("cnf") << "toCNF(" << node << ", negated = " << (negated ? "true" : "false") << ")" << endl; SatLiteral nodeLit; Node negatedNode = node.notNode(); // If the non-negated node has already been translated, get the translation if(isCached(node)) { nodeLit = lookupInCache(node); } else { // Handle each Boolean operator case switch(node.getKind()) { case NOT: nodeLit = handleNot(node); break; case XOR: nodeLit = handleXor(node); break; case ITE: nodeLit = handleIte(node); break; case IFF: nodeLit = handleIff(node); break; case IMPLIES: nodeLit = handleImplies(node); break; case OR: nodeLit = handleOr(node); break; case AND: nodeLit = handleAnd(node); break; default: { //TODO make sure this does not contain any boolean substructure nodeLit = convertAtom(node); //Unreachable(); //Node atomic = handleNonAtomicNode(node); //return isCached(atomic) ? lookupInCache(atomic) : convertAtom(atomic); } } } // Return the appropriate (negated) literal if (!negated) return nodeLit; else return ~nodeLit; } void TseitinCnfStream::convertAndAssertAnd(TNode node, bool lemma, bool negated) { Assert(node.getKind() == AND); if (!negated) { // If the node is a conjunction, we handle each conjunct separately for(TNode::const_iterator conjunct = node.begin(), node_end = node.end(); conjunct != node_end; ++conjunct ) { convertAndAssert(*conjunct, lemma, false); } } else { // If the node is a disjunction, we construct a clause and assert it int nChildren = node.getNumChildren(); SatClause clause(nChildren); TNode::const_iterator disjunct = node.begin(); for(int i = 0; i < nChildren; ++ disjunct, ++ i) { Assert( disjunct != node.end() ); clause[i] = toCNF(*disjunct, true); } Assert(disjunct == node.end()); assertClause(node, clause); } } void TseitinCnfStream::convertAndAssertOr(TNode node, bool lemma, bool negated) { Assert(node.getKind() == OR); if (!negated) { // If the node is a disjunction, we construct a clause and assert it int nChildren = node.getNumChildren(); SatClause clause(nChildren); TNode::const_iterator disjunct = node.begin(); for(int i = 0; i < nChildren; ++ disjunct, ++ i) { Assert( disjunct != node.end() ); clause[i] = toCNF(*disjunct, false); } Assert(disjunct == node.end()); assertClause(node, clause); } else { // If the node is a conjunction, we handle each conjunct separately for(TNode::const_iterator conjunct = node.begin(), node_end = node.end(); conjunct != node_end; ++conjunct ) { convertAndAssert(*conjunct, lemma, true); } } } void TseitinCnfStream::convertAndAssertXor(TNode node, bool lemma, bool negated) { if (!negated) { // p XOR q SatLiteral p = toCNF(node[0], false); SatLiteral q = toCNF(node[1], false); // Construct the clauses (p => !q) and (!q => p) SatClause clause1(2); clause1[0] = ~p; clause1[1] = ~q; assertClause(node, clause1); SatClause clause2(2); clause2[0] = p; clause2[1] = q; assertClause(node, clause2); } else { // !(p XOR q) is the same as p <=> q SatLiteral p = toCNF(node[0], false); SatLiteral q = toCNF(node[1], false); // Construct the clauses (p => q) and (q => p) SatClause clause1(2); clause1[0] = ~p; clause1[1] = q; assertClause(node, clause1); SatClause clause2(2); clause2[0] = p; clause2[1] = ~q; assertClause(node, clause2); } } void TseitinCnfStream::convertAndAssertIff(TNode node, bool lemma, bool negated) { if (!negated) { // p <=> q SatLiteral p = toCNF(node[0], false); SatLiteral q = toCNF(node[1], false); // Construct the clauses (p => q) and (q => p) SatClause clause1(2); clause1[0] = ~p; clause1[1] = q; assertClause(node, clause1); SatClause clause2(2); clause2[0] = p; clause2[1] = ~q; assertClause(node, clause2); } else { // !(p <=> q) is the same as p XOR q SatLiteral p = toCNF(node[0], false); SatLiteral q = toCNF(node[1], false); // Construct the clauses (p => !q) and (!q => p) SatClause clause1(2); clause1[0] = ~p; clause1[1] = ~q; assertClause(node, clause1); SatClause clause2(2); clause2[0] = p; clause2[1] = q; assertClause(node, clause2); } } void TseitinCnfStream::convertAndAssertImplies(TNode node, bool lemma, bool negated) { if (!negated) { // p => q SatLiteral p = toCNF(node[0], false); SatLiteral q = toCNF(node[1], false); // Construct the clause ~p || q SatClause clause(2); clause[0] = ~p; clause[1] = q; assertClause(node, clause); } else {// Construct the // !(p => q) is the same as (p && ~q) convertAndAssert(node[0], lemma, false); convertAndAssert(node[1], lemma, true); } } void TseitinCnfStream::convertAndAssertIte(TNode node, bool lemma, bool negated) { // ITE(p, q, r) SatLiteral p = toCNF(node[0], false); SatLiteral q = toCNF(node[1], negated); SatLiteral r = toCNF(node[2], negated); // Construct the clauses: // (p => q) and (!p => r) and (!q => !p) and (!r => p) SatClause clause1(2); clause1[0] = ~p; clause1[1] = q; assertClause(node, clause1); SatClause clause2(2); clause2[0] = p; clause2[1] = r; assertClause(node, clause2); SatClause clause3(2); clause3[0] = q; clause3[1] = ~p; assertClause(node, clause3); SatClause clause4(2); clause4[0] = r; clause4[1] = p; assertClause(node, clause4); } // At the top level we must ensure that all clauses that are asserted are // not unit, except for the direct assertions. This allows us to remove the // clauses later when they are not needed anymore (lemmas for example). void TseitinCnfStream::convertAndAssert(TNode node, bool lemma, bool negated) { Debug("cnf") << "convertAndAssert(" << node << ", negated = " << (negated ? "true" : "false") << ")" << endl; d_assertingLemma = lemma; switch(node.getKind()) { case AND: convertAndAssertAnd(node, lemma, negated); break; case OR: convertAndAssertOr(node, lemma, negated); break; case IFF: convertAndAssertIff(node, lemma, negated); break; case XOR: convertAndAssertXor(node, lemma, negated); break; case IMPLIES: convertAndAssertImplies(node, lemma, negated); break; case ITE: convertAndAssertIte(node, lemma, negated); break; case NOT: convertAndAssert(node[0], lemma, !negated); break; default: // Atoms assertClause(node, toCNF(node, negated)); break; } } }/* CVC4::prop namespace */ }/* CVC4 namespace */