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 : 1365 : uint32_t TermContext::computeValueOp(CVC5_UNUSED TNode t, uint32_t tval) const 21 : : { 22 : : // default is no change 23 : 1365 : return tval; 24 : : } 25 : : 26 : 1892183 : uint32_t RtfTermContext::initialValue() const 27 : : { 28 : : // by default, not in a term context or a quantifier 29 : 1892183 : return 0; 30 : : } 31 : : 32 : 25508698 : uint32_t RtfTermContext::computeValue(TNode t, 33 : : uint32_t tval, 34 : : CVC5_UNUSED size_t child) const 35 : : { 36 [ + + ]: 25508698 : if (t.isClosure()) 37 : : { 38 [ + + ]: 5676 : if (tval % 2 == 0) 39 : : { 40 : 5160 : return tval + 1; 41 : : } 42 : : } 43 [ + + ]: 25503022 : else if (hasNestedTermChildren(t)) 44 : : { 45 [ + + ]: 6005660 : if (tval < 2) 46 : : { 47 : 3678907 : return tval + 2; 48 : : } 49 : : } 50 : 21824631 : 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 : 5585019 : void RtfTermContext::getFlags(uint32_t val, bool& inQuant, bool& inTerm) 59 : : { 60 : 5585019 : inQuant = val % 2 == 1; 61 : 5585019 : inTerm = val >= 2; 62 : 5585019 : } 63 : : 64 : 25503022 : bool RtfTermContext::hasNestedTermChildren(TNode t) 65 : : { 66 : 25503022 : Kind k = t.getKind(); 67 : : // dont' worry about FORALL or EXISTS, these are part of inQuant. 68 [ + + ]: 34818376 : return theory::kindToTheoryId(k) != theory::THEORY_BOOL && k != Kind::EQUAL 69 [ + + ][ + + ]: 6014982 : && k != Kind::SEP_STAR && k != Kind::SEP_WAND && k != Kind::SEP_LABEL [ + + ] 70 [ + + ][ + + ]: 34818376 : && 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 : 17983 : uint32_t PolarityTermContext::initialValue() const 87 : : { 88 : : // by default, we have true polarity 89 : 17983 : return 2; 90 : : } 91 : : 92 : 195912 : uint32_t PolarityTermContext::computeValue(TNode t, 93 : : uint32_t tval, 94 : : size_t index) const 95 : : { 96 [ + + ][ + + ]: 195912 : switch (t.getKind()) [ - + ] 97 : : { 98 : 18870 : case Kind::AND: 99 : : case Kind::OR: 100 : : case Kind::SEP_STAR: 101 : : // polarity preserved 102 : 18870 : 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 : 17646 : case Kind::NOT: 107 : : // polarity reversed 108 [ + + ]: 17646 : return tval == 0 ? 0 : (3 - tval); 109 : 1899 : case Kind::ITE: 110 : : // head has no polarity, branches preserve 111 [ + + ]: 1899 : 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 : 157425 : default: 116 : : // no polarity 117 : 157425 : break; 118 : : } 119 : 157425 : 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 : 64812 : void PolarityTermContext::getFlags(uint32_t val, bool& hasPol, bool& pol) 128 : : { 129 : 64812 : hasPol = val != 0; 130 : 64812 : pol = val == 2; 131 : 64812 : } 132 : : 133 : 15452 : uint32_t TheoryLeafTermContext::initialValue() const { return 0; } 134 : : 135 : 122312 : uint32_t TheoryLeafTermContext::computeValue(TNode t, 136 : : uint32_t tval, 137 : : CVC5_UNUSED size_t index) const 138 : : { 139 [ + + ]: 122312 : return theory::Theory::isLeafOf(t, d_theoryId) ? 1 : tval; 140 : : } 141 : 93216 : uint32_t BoolSkeletonTermContext::initialValue() const { return 0; } 142 : : 143 : 1339404 : uint32_t BoolSkeletonTermContext::computeValue(TNode t, 144 : : uint32_t tval, 145 : : CVC5_UNUSED size_t child) const 146 : : { 147 [ + + ]: 1339404 : if (tval == 0) 148 : : { 149 [ + + ]: 668030 : if (!expr::isBooleanConnective(t)) 150 : : { 151 : 307246 : return 1; 152 : : } 153 : 360784 : return 0; 154 : : } 155 : 671374 : return 1; 156 : : } 157 : : 158 : 12860 : uint32_t WithinKindTermContext::initialValue() const { return 0; } 159 : : 160 : 668804 : uint32_t WithinKindTermContext::computeValue(TNode t, 161 : : uint32_t tval, 162 : : CVC5_UNUSED size_t index) const 163 : : { 164 [ + + ]: 668804 : if (tval == 0) 165 : : { 166 [ + + ]: 668646 : if (t.getKind() == d_kind) 167 : : { 168 : 64 : return 1; 169 : : } 170 : 668582 : return 0; 171 : : } 172 : 158 : return 1; 173 : : } 174 : : 175 : 2568 : uint32_t WithinPathTermContext::initialValue() const { return 1; } 176 : : 177 : 650614 : uint32_t WithinPathTermContext::computeValue(CVC5_UNUSED TNode t, 178 : : uint32_t tval, 179 : : size_t index) const 180 : : { 181 [ + + ]: 650614 : if (tval == 0) 182 : : { 183 : 607096 : return 0; 184 : : } 185 [ - + ][ - + ]: 43518 : Assert(!d_path.empty()); [ - - ] 186 : 43518 : size_t cc = (tval - 1) % d_path.size(); 187 [ + + ]: 43518 : if (index == d_path[cc]) 188 : : { 189 : 14840 : return tval + 1; 190 : : } 191 : 28678 : return 0; 192 : : } 193 : : } // namespace cvc5::internal