Branch data Line data Source code
1 : : /****************************************************************************** 2 : : * Top contributors (to current version): 3 : : * Andrew Reynolds, Aina Niemetz, Mathias Preiner 4 : : * 5 : : * This file is part of the cvc5 project. 6 : : * 7 : : * Copyright (c) 2009-2025 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 : : * Implementation of term context utilities. 14 : : */ 15 : : 16 : : #include "expr/term_context.h" 17 : : 18 : : #include "expr/node_algorithm.h" 19 : : #include "theory/theory.h" 20 : : 21 : : namespace cvc5::internal { 22 : : 23 : 935 : uint32_t TermContext::computeValueOp(CVC5_UNUSED TNode t, uint32_t tval) const 24 : : { 25 : : // default is no change 26 : 935 : return tval; 27 : : } 28 : : 29 : 1853880 : uint32_t RtfTermContext::initialValue() const 30 : : { 31 : : // by default, not in a term context or a quantifier 32 : 1853880 : return 0; 33 : : } 34 : : 35 : 25504700 : uint32_t RtfTermContext::computeValue(TNode t, 36 : : uint32_t tval, 37 : : CVC5_UNUSED size_t child) const 38 : : { 39 [ + + ]: 25504700 : if (t.isClosure()) 40 : : { 41 [ + + ]: 5672 : if (tval % 2 == 0) 42 : : { 43 : 5172 : return tval + 1; 44 : : } 45 : : } 46 [ + + ]: 25499000 : else if (hasNestedTermChildren(t)) 47 : : { 48 [ + + ]: 6252340 : if (tval < 2) 49 : : { 50 : 3561330 : return tval + 2; 51 : : } 52 : : } 53 : 21938200 : return tval; 54 : : } 55 : : 56 : 0 : uint32_t RtfTermContext::getValue(bool inQuant, bool inTerm) 57 : : { 58 [ - - ][ - - ]: 0 : return (inQuant ? 1 : 0) + 2 * (inTerm ? 1 : 0); 59 : : } 60 : : 61 : 5405220 : void RtfTermContext::getFlags(uint32_t val, bool& inQuant, bool& inTerm) 62 : : { 63 : 5405220 : inQuant = val % 2 == 1; 64 : 5405220 : inTerm = val >= 2; 65 : 5405220 : } 66 : : 67 : 25499000 : bool RtfTermContext::hasNestedTermChildren(TNode t) 68 : : { 69 : 25499000 : Kind k = t.getKind(); 70 : : // dont' worry about FORALL or EXISTS, these are part of inQuant. 71 [ + + ]: 34935300 : return theory::kindToTheoryId(k) != theory::THEORY_BOOL && k != Kind::EQUAL 72 [ + + ][ + + ]: 6261660 : && k != Kind::SEP_STAR && k != Kind::SEP_WAND && k != Kind::SEP_LABEL [ + + ] 73 [ + + ][ + + ]: 34935300 : && k != Kind::BITVECTOR_EAGER_ATOM; 74 : : } 75 : : 76 : 0 : uint32_t InQuantTermContext::initialValue() const { return 0; } 77 : : 78 : 0 : uint32_t InQuantTermContext::computeValue(TNode t, 79 : : uint32_t tval, 80 : : CVC5_UNUSED size_t index) const 81 : : { 82 [ - - ]: 0 : return t.isClosure() ? 1 : tval; 83 : : } 84 : : 85 [ - - ]: 0 : uint32_t InQuantTermContext::getValue(bool inQuant) { return inQuant ? 1 : 0; } 86 : : 87 : 0 : bool InQuantTermContext::inQuant(uint32_t val) { return val == 1; } 88 : : 89 : 18008 : uint32_t PolarityTermContext::initialValue() const 90 : : { 91 : : // by default, we have true polarity 92 : 18008 : return 2; 93 : : } 94 : : 95 : 198948 : uint32_t PolarityTermContext::computeValue(TNode t, 96 : : uint32_t tval, 97 : : size_t index) const 98 : : { 99 [ + + ][ + + ]: 198948 : switch (t.getKind()) [ - + ] 100 : : { 101 : 19300 : case Kind::AND: 102 : : case Kind::OR: 103 : : case Kind::SEP_STAR: 104 : : // polarity preserved 105 : 19300 : return tval; 106 : 72 : case Kind::IMPLIES: 107 : : // first child reverses, otherwise we preserve 108 [ + + ][ + - ]: 72 : return index == 0 ? (tval == 0 ? 0 : (3 - tval)) : tval; 109 : 17881 : case Kind::NOT: 110 : : // polarity reversed 111 [ + + ]: 17881 : return tval == 0 ? 0 : (3 - tval); 112 : 1893 : case Kind::ITE: 113 : : // head has no polarity, branches preserve 114 [ + + ]: 1893 : return index == 0 ? 0 : tval; 115 : 0 : case Kind::FORALL: 116 : : // body preserves, others have no polarity. 117 [ - - ]: 0 : return index == 1 ? tval : 0; 118 : 159802 : default: 119 : : // no polarity 120 : 159802 : break; 121 : : } 122 : 159802 : return 0; 123 : : } 124 : : 125 : 0 : uint32_t PolarityTermContext::getValue(bool hasPol, bool pol) 126 : : { 127 [ - - ][ - - ]: 0 : return hasPol ? (pol ? 2 : 1) : 0; 128 : : } 129 : : 130 : 66712 : void PolarityTermContext::getFlags(uint32_t val, bool& hasPol, bool& pol) 131 : : { 132 : 66712 : hasPol = val != 0; 133 : 66712 : pol = val == 2; 134 : 66712 : } 135 : : 136 : 14590 : uint32_t TheoryLeafTermContext::initialValue() const { return 0; } 137 : : 138 : 110646 : uint32_t TheoryLeafTermContext::computeValue(TNode t, 139 : : uint32_t tval, 140 : : CVC5_UNUSED size_t index) const 141 : : { 142 [ + + ]: 110646 : return theory::Theory::isLeafOf(t, d_theoryId) ? 1 : tval; 143 : : } 144 : 64928 : uint32_t BoolSkeletonTermContext::initialValue() const { return 0; } 145 : : 146 : 954222 : uint32_t BoolSkeletonTermContext::computeValue(TNode t, 147 : : uint32_t tval, 148 : : CVC5_UNUSED size_t child) const 149 : : { 150 [ + + ]: 954222 : if (tval == 0) 151 : : { 152 [ + + ]: 473070 : if (!expr::isBooleanConnective(t)) 153 : : { 154 : 213866 : return 1; 155 : : } 156 : 259204 : return 0; 157 : : } 158 : 481152 : return 1; 159 : : } 160 : : 161 : 8614 : uint32_t WithinKindTermContext::initialValue() const { return 0; } 162 : : 163 : 445564 : uint32_t WithinKindTermContext::computeValue(TNode t, 164 : : uint32_t tval, 165 : : CVC5_UNUSED size_t index) const 166 : : { 167 [ + + ]: 445564 : if (tval == 0) 168 : : { 169 [ + + ]: 445452 : if (t.getKind() == d_kind) 170 : : { 171 : 46 : return 1; 172 : : } 173 : 445406 : return 0; 174 : : } 175 : 112 : return 1; 176 : : } 177 : : 178 : 1648 : uint32_t WithinPathTermContext::initialValue() const { return 1; } 179 : : 180 : 599868 : uint32_t WithinPathTermContext::computeValue(CVC5_UNUSED TNode t, 181 : : uint32_t tval, 182 : : size_t index) const 183 : : { 184 [ + + ]: 599868 : if (tval == 0) 185 : : { 186 : 568548 : return 0; 187 : : } 188 [ - + ][ - + ]: 31320 : Assert(!d_path.empty()); [ - - ] 189 : 31320 : size_t cc = (tval - 1) % d_path.size(); 190 [ + + ]: 31320 : if (index == d_path[cc]) 191 : : { 192 : 10684 : return tval + 1; 193 : : } 194 : 20636 : return 0; 195 : : } 196 : : } // namespace cvc5::internal