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 : : * Implementation of term context utilities. 11 : : */ 12 : : 13 : : #include "expr/term_context.h" 14 : : 15 : : #include "expr/node_algorithm.h" 16 : : #include "theory/theory.h" 17 : : 18 : : namespace cvc5::internal { 19 : : 20 : 935 : uint32_t TermContext::computeValueOp(CVC5_UNUSED TNode t, uint32_t tval) const 21 : : { 22 : : // default is no change 23 : 935 : return tval; 24 : : } 25 : : 26 : 1884822 : uint32_t RtfTermContext::initialValue() const 27 : : { 28 : : // by default, not in a term context or a quantifier 29 : 1884822 : return 0; 30 : : } 31 : : 32 : 25618258 : uint32_t RtfTermContext::computeValue(TNode t, 33 : : uint32_t tval, 34 : : CVC5_UNUSED size_t child) const 35 : : { 36 [ + + ]: 25618258 : if (t.isClosure()) 37 : : { 38 [ + + ]: 5672 : if (tval % 2 == 0) 39 : : { 40 : 5172 : return tval + 1; 41 : : } 42 : : } 43 [ + + ]: 25612586 : else if (hasNestedTermChildren(t)) 44 : : { 45 [ + + ]: 5909672 : if (tval < 2) 46 : : { 47 : 3616685 : return tval + 2; 48 : : } 49 : : } 50 : 21996401 : return tval; 51 : : } 52 : : 53 : 0 : uint32_t RtfTermContext::getValue(bool inQuant, bool inTerm) 54 : : { 55 [ - - ][ - - ]: 0 : return (inQuant ? 1 : 0) + 2 * (inTerm ? 1 : 0); 56 : : } 57 : : 58 : 5530274 : void RtfTermContext::getFlags(uint32_t val, bool& inQuant, bool& inTerm) 59 : : { 60 : 5530274 : inQuant = val % 2 == 1; 61 : 5530274 : inTerm = val >= 2; 62 : 5530274 : } 63 : : 64 : 25612586 : bool RtfTermContext::hasNestedTermChildren(TNode t) 65 : : { 66 : 25612586 : Kind k = t.getKind(); 67 : : // dont' worry about FORALL or EXISTS, these are part of inQuant. 68 [ + + ]: 34784686 : return theory::kindToTheoryId(k) != theory::THEORY_BOOL && k != Kind::EQUAL 69 [ + + ][ + + ]: 5918994 : && k != Kind::SEP_STAR && k != Kind::SEP_WAND && k != Kind::SEP_LABEL [ + + ] 70 [ + + ][ + + ]: 34784686 : && k != Kind::BITVECTOR_EAGER_ATOM; 71 : : } 72 : : 73 : 0 : uint32_t InQuantTermContext::initialValue() const { return 0; } 74 : : 75 : 0 : uint32_t InQuantTermContext::computeValue(TNode t, 76 : : uint32_t tval, 77 : : CVC5_UNUSED size_t index) const 78 : : { 79 [ - - ]: 0 : return t.isClosure() ? 1 : tval; 80 : : } 81 : : 82 [ - - ]: 0 : uint32_t InQuantTermContext::getValue(bool inQuant) { return inQuant ? 1 : 0; } 83 : : 84 : 0 : bool InQuantTermContext::inQuant(uint32_t val) { return val == 1; } 85 : : 86 : 18092 : uint32_t PolarityTermContext::initialValue() const 87 : : { 88 : : // by default, we have true polarity 89 : 18092 : return 2; 90 : : } 91 : : 92 : 199396 : uint32_t PolarityTermContext::computeValue(TNode t, 93 : : uint32_t tval, 94 : : size_t index) const 95 : : { 96 [ + + ][ + + ]: 199396 : switch (t.getKind()) [ - + ] 97 : : { 98 : 19356 : case Kind::AND: 99 : : case Kind::OR: 100 : : case Kind::SEP_STAR: 101 : : // polarity preserved 102 : 19356 : return tval; 103 : 72 : case Kind::IMPLIES: 104 : : // first child reverses, otherwise we preserve 105 [ + + ][ + - ]: 72 : return index == 0 ? (tval == 0 ? 0 : (3 - tval)) : tval; 106 : 17937 : case Kind::NOT: 107 : : // polarity reversed 108 [ + + ]: 17937 : return tval == 0 ? 0 : (3 - tval); 109 : 1893 : case Kind::ITE: 110 : : // head has no polarity, branches preserve 111 [ + + ]: 1893 : return index == 0 ? 0 : tval; 112 : 0 : case Kind::FORALL: 113 : : // body preserves, others have no polarity. 114 [ - - ]: 0 : return index == 1 ? tval : 0; 115 : 160138 : default: 116 : : // no polarity 117 : 160138 : break; 118 : : } 119 : 160138 : return 0; 120 : : } 121 : : 122 : 0 : uint32_t PolarityTermContext::getValue(bool hasPol, bool pol) 123 : : { 124 [ - - ][ - - ]: 0 : return hasPol ? (pol ? 2 : 1) : 0; 125 : : } 126 : : 127 : 67188 : void PolarityTermContext::getFlags(uint32_t val, bool& hasPol, bool& pol) 128 : : { 129 : 67188 : hasPol = val != 0; 130 : 67188 : pol = val == 2; 131 : 67188 : } 132 : : 133 : 15428 : uint32_t TheoryLeafTermContext::initialValue() const { return 0; } 134 : : 135 : 119640 : uint32_t TheoryLeafTermContext::computeValue(TNode t, 136 : : uint32_t tval, 137 : : CVC5_UNUSED size_t index) const 138 : : { 139 [ + + ]: 119640 : return theory::Theory::isLeafOf(t, d_theoryId) ? 1 : tval; 140 : : } 141 : 65576 : uint32_t BoolSkeletonTermContext::initialValue() const { return 0; } 142 : : 143 : 940604 : uint32_t BoolSkeletonTermContext::computeValue(TNode t, 144 : : uint32_t tval, 145 : : CVC5_UNUSED size_t child) const 146 : : { 147 [ + + ]: 940604 : if (tval == 0) 148 : : { 149 [ + + ]: 456814 : if (!expr::isBooleanConnective(t)) 150 : : { 151 : 211654 : return 1; 152 : : } 153 : 245160 : return 0; 154 : : } 155 : 483790 : return 1; 156 : : } 157 : : 158 : 8606 : uint32_t WithinKindTermContext::initialValue() const { return 0; } 159 : : 160 : 447318 : uint32_t WithinKindTermContext::computeValue(TNode t, 161 : : uint32_t tval, 162 : : CVC5_UNUSED size_t index) const 163 : : { 164 [ + + ]: 447318 : if (tval == 0) 165 : : { 166 [ + + ]: 447206 : if (t.getKind() == d_kind) 167 : : { 168 : 46 : return 1; 169 : : } 170 : 447160 : return 0; 171 : : } 172 : 112 : return 1; 173 : : } 174 : : 175 : 1744 : uint32_t WithinPathTermContext::initialValue() const { return 1; } 176 : : 177 : 603676 : uint32_t WithinPathTermContext::computeValue(CVC5_UNUSED TNode t, 178 : : uint32_t tval, 179 : : size_t index) const 180 : : { 181 [ + + ]: 603676 : if (tval == 0) 182 : : { 183 : 571564 : return 0; 184 : : } 185 [ - + ][ - + ]: 32112 : Assert(!d_path.empty()); [ - - ] 186 : 32112 : size_t cc = (tval - 1) % d_path.size(); 187 [ + + ]: 32112 : if (index == d_path[cc]) 188 : : { 189 : 10916 : return tval + 1; 190 : : } 191 : 21196 : return 0; 192 : : } 193 : : } // namespace cvc5::internal