Branch data Line data Source code
1 : : /****************************************************************************** 2 : : * This file is part of the cvc5 project. 3 : : * 4 : : * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS 5 : : * in the top-level source directory and their institutional affiliations. 6 : : * All rights reserved. See the file COPYING in the top-level source 7 : : * directory for licensing information. 8 : : * **************************************************************************** 9 : : * 10 : : * Print channels for LFSC proofs. 11 : : */ 12 : : 13 : : #include "proof/lfsc/lfsc_print_channel.h" 14 : : 15 : : #include <sstream> 16 : : 17 : : #include "proof/lfsc/lfsc_util.h" 18 : : #include "rewriter/rewrite_proof_rule.h" 19 : : 20 : : using namespace cvc5::internal::rewriter; 21 : : 22 : : namespace cvc5::internal { 23 : : namespace proof { 24 : : 25 : 1706 : LfscPrintChannelOut::LfscPrintChannelOut(std::ostream& out) : d_out(out) {} 26 : 3719724 : void LfscPrintChannelOut::printNode(TNode n) 27 : : { 28 : 3719724 : d_out << " "; 29 : 3719724 : printNodeInternal(d_out, n); 30 : 3719724 : } 31 : : 32 : 20633 : void LfscPrintChannelOut::printTypeNode(TypeNode tn) 33 : : { 34 : 20633 : d_out << " "; 35 : 20633 : printTypeNodeInternal(d_out, tn); 36 : 20633 : } 37 : : 38 : 20665358 : void LfscPrintChannelOut::printHole() { d_out << " _ "; } 39 : 608136 : void LfscPrintChannelOut::printTrust(TNode res, ProofRule src) 40 : : { 41 : 608136 : d_out << std::endl << "(trust "; 42 : 608136 : printNodeInternal(d_out, res); 43 : 608136 : d_out << ") ; from " << src << std::endl; 44 : 608136 : } 45 : : 46 : 7808910 : void LfscPrintChannelOut::printOpenRule(const ProofNode* pn) 47 : : { 48 : 7808910 : d_out << std::endl << "("; 49 : 7808910 : printRule(d_out, pn); 50 : 7808910 : } 51 : : 52 : 1595102 : void LfscPrintChannelOut::printOpenLfscRule(LfscRule lr) 53 : : { 54 : 1595102 : d_out << std::endl << "(" << lr; 55 : 1595102 : } 56 : : 57 : 8626702 : void LfscPrintChannelOut::printCloseRule(size_t nparen) 58 : : { 59 [ + + ]: 18030714 : for (size_t i = 0; i < nparen; i++) 60 : : { 61 : 9404012 : d_out << ")"; 62 : : } 63 : 8626702 : } 64 : : 65 : 3780327 : void LfscPrintChannelOut::printId(size_t id, const std::string& prefix) 66 : : { 67 : 3780327 : d_out << " "; 68 : 3780327 : printId(d_out, id, prefix); 69 : 3780327 : } 70 : : 71 : 1619130 : void LfscPrintChannelOut::printEndLine() { d_out << std::endl; } 72 : : 73 : 4790222 : void LfscPrintChannelOut::printNodeInternal(std::ostream& out, Node n) 74 : : { 75 : : // due to use of special names in the node converter, we must clean symbols 76 : 4790222 : std::stringstream ss; 77 : 4790222 : options::ioutils::applyOutputLanguage(ss, Language::LANG_SMTLIB_V2_6); 78 : 4790222 : n.toStream(ss); 79 : 4790222 : std::string s = ss.str(); 80 : 4790222 : cleanSymbols(s); 81 : 4790222 : out << s; 82 : 4790222 : } 83 : : 84 : 37819 : void LfscPrintChannelOut::printTypeNodeInternal(std::ostream& out, TypeNode tn) 85 : : { 86 : : // due to use of special names in the node converter, we must clean symbols 87 : 37819 : std::stringstream ss; 88 : 37819 : options::ioutils::applyOutputLanguage(ss, Language::LANG_SMTLIB_V2_6); 89 : 37819 : tn.toStream(ss); 90 : 37819 : std::string s = ss.str(); 91 : 37819 : cleanSymbols(s); 92 : 37819 : out << s; 93 : 37819 : } 94 : : 95 : 7808910 : void LfscPrintChannelOut::printRule(std::ostream& out, const ProofNode* pn) 96 : : { 97 [ + + ]: 7808910 : if (pn->getRule() == ProofRule::LFSC_RULE) 98 : : { 99 : 4202726 : const std::vector<Node>& args = pn->getArguments(); 100 : 4202726 : out << getLfscRule(args[0]); 101 : 4202730 : return; 102 : : } 103 [ + + ]: 3606184 : else if (pn->getRule() == ProofRule::DSL_REWRITE) 104 : : { 105 : 4 : const std::vector<Node>& args = pn->getArguments(); 106 : : ProofRewriteRule di; 107 [ + - ]: 4 : if (rewriter::getRewriteRule(args[0], di)) 108 : : { 109 : 4 : printProofRewriteRule(out, di); 110 : : } 111 : : else 112 : : { 113 : 0 : Unreachable(); 114 : : } 115 : 4 : return; 116 : : } 117 : : // Otherwise, convert to lower case 118 : 3606180 : std::stringstream ss; 119 : 3606180 : ss << pn->getRule(); 120 : 3606180 : std::string rname = ss.str(); 121 : 3606180 : std::transform( 122 : 21339423 : rname.begin(), rname.end(), rname.begin(), [](unsigned char c) { 123 : 21339423 : return std::tolower(c); 124 : : }); 125 : 3606180 : out << rname; 126 : 3606180 : } 127 : : 128 : 4658031 : void LfscPrintChannelOut::printId(std::ostream& out, 129 : : size_t id, 130 : : const std::string& prefix) 131 : : { 132 : 4658031 : out << prefix << id; 133 : 4658031 : } 134 : : 135 : 6 : void LfscPrintChannelOut::printProofRewriteRule(std::ostream& out, 136 : : ProofRewriteRule id) 137 : : { 138 : 6 : out << "dsl." << id; 139 : 6 : } 140 : : 141 : 4828041 : void LfscPrintChannelOut::cleanSymbols(std::string& s) 142 : : { 143 : 4828041 : size_t start_pos = 0; 144 [ + + ]: 4829549 : while ((start_pos = s.find("(_ ", start_pos)) != std::string::npos) 145 : : { 146 : 1508 : s.replace(start_pos, 3, "("); 147 : 1508 : start_pos += 1; 148 : : } 149 : 4828041 : start_pos = 0; 150 [ + + ]: 4832599 : while ((start_pos = s.find("__LFSC_TMP", start_pos)) != std::string::npos) 151 : : { 152 : 4558 : s.replace(start_pos, 10, ""); 153 : : } 154 : 4828041 : } 155 : : 156 : 1706 : LfscPrintChannelPre::LfscPrintChannelPre(LetBinding& lbind) : d_lbind(lbind) {} 157 : : 158 : 3719724 : void LfscPrintChannelPre::printNode(TNode n) { d_lbind.process(n); } 159 : 608136 : void LfscPrintChannelPre::printTrust(TNode res, CVC5_UNUSED ProofRule src) 160 : : { 161 : 608136 : d_lbind.process(res); 162 : 608136 : } 163 : : 164 : 7808910 : void LfscPrintChannelPre::printOpenRule(const ProofNode* pn) 165 : : { 166 : : // if its a DSL rule, remember it 167 [ + + ]: 7808910 : if (pn->getRule() == ProofRule::DSL_REWRITE) 168 : : { 169 : 4 : Node idn = pn->getArguments()[0]; 170 : : ProofRewriteRule di; 171 [ + - ]: 4 : if (rewriter::getRewriteRule(idn, di)) 172 : : { 173 : 4 : d_dprs.insert(di); 174 : : } 175 : : else 176 : : { 177 : 0 : Unhandled(); 178 : : } 179 : 4 : } 180 : 7808910 : } 181 : : 182 : : const std::unordered_set<ProofRewriteRule>& 183 : 1706 : LfscPrintChannelPre::getDslRewrites() const 184 : : { 185 : 1706 : return d_dprs; 186 : : } 187 : : 188 : : } // namespace proof 189 : : } // namespace cvc5::internal