00001 /*****************************************************************************/ 00002 /*! 00003 * \file bitvector_proof_rules.h 00004 * \brief Arithmetic proof rules 00005 * 00006 * Author: Vijay Ganesh. 00007 * 00008 * Created:Wed May 5 15:47:28 PST 2004 00009 * 00010 * <hr> 00011 * 00012 * License to use, copy, modify, sell and/or distribute this software 00013 * and its documentation for any purpose is hereby granted without 00014 * royalty, subject to the terms and conditions defined in the \ref 00015 * LICENSE file provided with this distribution. 00016 * 00017 * <hr> 00018 * 00019 */ 00020 /*****************************************************************************/ 00021 00022 #ifndef _cvc3__bitvector_proof_rules_h_ 00023 #define _cvc3__bitvector_proof_rules_h_ 00024 00025 #include <string> 00026 #include <vector> 00027 00028 namespace CVC3 { 00029 00030 class Expr; 00031 class Theorem; 00032 00033 class BitvectorProofRules { 00034 public: 00035 // Destructor 00036 virtual ~BitvectorProofRules() { } 00037 00038 //////////////////////////////////////////////////////////////////// 00039 // Bitblasting rules for equations 00040 //////////////////////////////////////////////////////////////////// 00041 00042 /*! \param thm input theorem: (e1[i]<=>e2[i])<=>false 00043 * 00044 * \result (e1=e2)<=>false 00045 */ 00046 virtual Theorem bitvectorFalseRule(const Theorem& thm) = 0; 00047 00048 /*! \param thm input theorem: (~e1[i]<=>e2[i])<=>true 00049 * 00050 * \result (e1!=e2)<=>true 00051 */ 00052 virtual Theorem bitvectorTrueRule(const Theorem& thm) = 0; 00053 00054 00055 //! t1=t2 ==> AND_i(t1[i:i] = t2[i:i]) 00056 /*! 00057 * \param e is a Expr(t1=t2) 00058 * 00059 * \param f is the resulting expression AND_i(t1[i:i] = t2[i:i]) 00060 * is passed to the rule for efficiency. 00061 */ 00062 virtual Theorem bitBlastEqnRule(const Expr& e, const Expr& f) = 0; 00063 //! t1/=t2 ==> OR_i(NOT t1[i]<=>t2[i]) 00064 /*! 00065 * \param e is a Theorem(t1/=t2) 00066 * 00067 * \param f is the resulting expression OR_i(NOT t1[i]<=>t2[i]), 00068 * passed to the rule for efficiency. 00069 */ 00070 virtual Theorem bitBlastDisEqnRule(const Theorem& e, const Expr& f) = 0; 00071 00072 00073 //////////////////////////////////////////////////////////////////// 00074 // Bitblasting and rewrite rules for Inequations 00075 //////////////////////////////////////////////////////////////////// 00076 00077 //! sign extend the input SX(e) appropriately 00078 virtual Theorem signExtendRule(const Expr& e) = 0; 00079 00080 //! Pad the kids of BVLT/BVLE to make their length equal 00081 virtual Theorem padBVLTRule(const Expr& e, int len) = 0; 00082 00083 //! Sign Extend the kids of BVSLT/BVSLE to make their length equal 00084 virtual Theorem padBVSLTRule(const Expr& e, int len) = 0; 00085 00086 /*! input: e0 <=(s) e1. output depends on whether the topbits(MSB) of 00087 * e0 and e1 are constants. If they are constants then optimizations 00088 * are done, otherwise the following rule is implemented. 00089 * 00090 * e0 <=(s) e1 <==> (e0[n-1] AND NOT e1[n-1]) OR 00091 * (e0[n-1] AND e1[n-1] AND e1[n-2:0] <= e0[n-2:0]) OR 00092 * (NOT e0[n-1] AND NOT e1[n-1] AND e0[n-2:0] <= e1[n-2:0]) 00093 */ 00094 virtual Theorem signBVLTRule(const Expr& e, 00095 const Theorem& topBit0, 00096 const Theorem& topBit1) = 0; 00097 00098 /*! NOT(e[0][0] < e[0][1]) <==> (e[0][1] <= e[0][0]), 00099 * NOT(e[0][0] <= e[0][1]) <==> (e[0][1] < e[0][0]) 00100 */ 00101 virtual Theorem notBVLTRule(const Expr& e, int Kind) = 0; 00102 00103 //! if(lhs==rhs) then we have (lhs < rhs <==> false),(lhs <= rhs <==> true) 00104 virtual Theorem lhsEqRhsIneqn(const Expr& e, int kind) = 0; 00105 00106 00107 virtual Theorem zeroLeq(const Expr& e) = 0; 00108 virtual Theorem bvConstIneqn(const Expr& e, int kind) = 0; 00109 00110 virtual Theorem generalIneqn(const Expr& e, 00111 const Theorem& e0, 00112 const Theorem& e1, int kind) = 0; 00113 00114 00115 //////////////////////////////////////////////////////////////////// 00116 // Bitblast rules for terms 00117 //////////////////////////////////////////////////////////////////// 00118 00119 //! t[i] ==> t[i:i] = 0bin1 or NOT t[i] ==> t[i:i] = 0bin0 00120 /*! \param thm is a Theorem(t[i]) or Theorem(NOT t[i]), where t[i] 00121 * is BOOLEXTRACT(t, i). 00122 */ 00123 virtual Theorem bitExtractToExtract(const Theorem& thm) = 0; 00124 00125 //! t[i] <=> t[i:i][0] (to use rewriter for simplifying t[i:i]) 00126 virtual Theorem bitExtractRewrite(const Expr& x) = 0; 00127 00128 /*! \param x is bitvector constant 00129 * \param i is extracted bitposition 00130 * 00131 * \result \f[ \frac{}{\mathrm{BOOLEXTRACT(x,i)} \iff 00132 * \mathrm{TRUE}} \f], if bitposition has a 1; \f[ 00133 * \frac{}{\mathrm{BOOLEXTRACT(x,i)} \iff \mathrm{FALSE}} \f], if 00134 * the bitposition has a 0 00135 */ 00136 virtual Theorem bitExtractConstant(const Expr & x, int i)= 0; 00137 00138 /*! \param x is bitvector binary concatenation 00139 * \param i is extracted bitposition 00140 * 00141 * \result \f[ \frac{}{(t_{[m]}@q_{[n]})[i] \iff (q_{[n]})[i]} 00142 * \f], where \f[ 0 \geq i \geq n-1 \f], another case of 00143 * boolextract over concatenation is: 00144 * \f[\frac{}{(t_{[m]}@q_{[n]})[i] \iff (t_{[m]})[i-n]} \f], 00145 * where \f[ n \geq i \geq m+n-1 \f] 00146 */ 00147 virtual Theorem bitExtractConcatenation(const Expr & x, 00148 int i)= 0; 00149 00150 /*! \param t is bitvector binary BVMULT. x[0] must be BVCONST 00151 * \param i is extracted bitposition 00152 * 00153 * \result bitblast of BVMULT 00154 */ 00155 virtual Theorem bitExtractConstBVMult(const Expr& t, int i)= 0; 00156 00157 /*! \param t : input1 is bitvector binary BVMULT. t[0] must not be BVCONST 00158 * \param i : input2 is extracted bitposition 00159 * 00160 * \result bitblast of BVMULT 00161 */ 00162 virtual Theorem bitExtractBVMult(const Expr& t, int i) = 0; 00163 00164 /*! \param x is bitvector extraction e[k:j] 00165 * \param i is extracted bitposition 00166 * 00167 * \result \f[ \frac{}{(t_{[n]}[k:j])[i] \iff (t_{[n]})[i+j]} 00168 * \f], where \f[ 0 \geq j \geq k < n, 0 \geq i < k-j \f] 00169 */ 00170 virtual Theorem bitExtractExtraction(const Expr & x, int i)= 0; 00171 00172 /*! \param t1 is vector of bitblasts of t, from bit i-1 to 0 00173 * \param t2 is vector of bitblasts of q, from bit i-1 to 0 00174 * \param bvPlusTerm is BVPLUS term: BVPLUS(n,t,q) 00175 * \param i is extracted bitposition 00176 * 00177 * \result The base case is: \f[ 00178 * \frac{}{(\mathrm{BVPLUS}(n,t,q))[0] \iff t[0] \oplus q[0]} 00179 * \f], when \f[ 0 < i \leq n-1 \f], we have \f[ 00180 * \frac{}{(\mathrm{BVPLUS}(n,t,q))[i] \iff t[i] \oplus q[i] 00181 * \oplus c(t,q,i)} \f], where c(t,q,i) is the carry generated 00182 * by the addition of bits from 0 to i-1 00183 */ 00184 virtual Theorem bitExtractBVPlus(const std::vector<Theorem>& t1, 00185 const std::vector<Theorem>& t2, 00186 const Expr& bvPlusTerm, int i) = 0; 00187 00188 00189 virtual Theorem bitExtractBVPlusPreComputed(const Theorem& t1_i, 00190 const Theorem& t2_i, 00191 const Expr& bvPlusTerm, 00192 int bitPos, 00193 int precomputed) = 0; 00194 00195 00196 /*! \param bvPlusTerm : input1 is bvPlusTerm, a BVPLUS term with 00197 * arity > 2 00198 * 00199 * \result : output is iff-Theorem: bvPlusTerm <==> outputTerm, 00200 * where outputTerm is an equivalent BINARY bvplus. 00201 */ 00202 virtual Theorem bvPlusAssociativityRule(const Expr& bvPlusTerm)= 0; 00203 00204 /*! \param x : input1 is bitwise NEGATION 00205 * \param i : input2 is extracted bitposition 00206 * 00207 * \result \f[ \frac{}{(\sim t_{[n]})[i] \iff \neg (t_{[n]}[i])} 00208 * \f] 00209 */ 00210 virtual Theorem bitExtractNot(const Expr & x, int i)= 0; 00211 00212 /*! \param x : input1 is bitwise AND 00213 * \param i : input2 is extracted bitposition 00214 * 00215 * \result \f[ \frac{}{(t_{[n]} \& q_{[n]})[i] \iff (t_{[n]}[i] 00216 * \wedge q_{[n]}[i])} \f] 00217 */ 00218 virtual Theorem bitExtractAnd(const Expr & x, int i)= 0; 00219 00220 /*! \param x : input1 is bitwise OR 00221 * \param i : input2 is extracted bitposition 00222 * 00223 * \result \f[ \frac{}{(t_{[n]} \mid q_{[n]})[i] \iff (t_{[n]}[i] 00224 * \vee q_{[n]}[i])} \f] 00225 */ 00226 virtual Theorem bitExtractOr(const Expr & x, int i)= 0; 00227 00228 /*! \param x : input1 is bitvector FIXED SHIFT \f[ e_{[n]} \ll k \f] 00229 * \param i : input2 is extracted bitposition 00230 * 00231 * \result \f[\frac{}{(e_{[n]} \ll k)[i] \iff \mathrm{FALSE}} 00232 * \f], if 0 <= i < k. however, if k <= i < n then, result is 00233 * \f[\frac{}{(e_{[n]} \ll k)[i] \iff e_{[n]}[i]} \f] 00234 */ 00235 virtual Theorem bitExtractFixedLeftShift(const Expr & x, 00236 int i)= 0; 00237 00238 virtual Theorem bitExtractFixedRightShift(const Expr & x, 00239 int i)= 0; 00240 00241 /*! \param e : input1 is bitvector term 00242 * \param r : input2 is extracted bitposition 00243 * 00244 * \result we check if r > bvlength(e). if yes, then return 00245 * BOOLEXTRACT(e,r) <==> FALSE; else raise soundness 00246 * exception. (Note: this rule is used in BVPLUS bitblast 00247 * function) 00248 */ 00249 virtual Theorem zeroPaddingRule(const Expr& e, int r)= 0; 00250 00251 00252 virtual Theorem bitExtractSXRule(const Expr& e, int i) = 0; 00253 00254 /////////////////////////////////////////////////////////////////// 00255 ///// Special case rewrite rules 00256 /////////////////////////////////////////////////////////////////// 00257 00258 //! c1=c2 <=> TRUE/FALSE (equality of constant bitvectors) 00259 virtual Theorem eqConst(const Expr& e) = 0; 00260 //! |- c1=c2 ==> |- AND(c1[i:i] = c2[i:i]) - expanding equalities into bits 00261 virtual Theorem eqToBits(const Theorem& eq) = 0; 00262 //! t<<n = c @ 0bin00...00, takes e == (t<<n) 00263 virtual Theorem leftShiftToConcat(const Expr& e) = 0; 00264 //! t<<n = c @ 0bin00...00, takes e == (t<<n) 00265 virtual Theorem constWidthLeftShiftToConcat(const Expr& e) = 0; 00266 //! t>>m = 0bin00...00 @ t[bvlength-1:m], takes e == (t>>n) 00267 virtual Theorem rightShiftToConcat(const Expr& e) = 0; 00268 //! a XOR b <=> (a & ~b) | (~a & b) 00269 virtual Theorem rewriteXOR(const Expr& e) = 0; 00270 //! a XNOR b <=> (~a & ~b) | (a & b) 00271 virtual Theorem rewriteXNOR(const Expr& e) = 0; 00272 //! a NAND b <=> ~(a & b) 00273 virtual Theorem rewriteNAND(const Expr& e) = 0; 00274 //! a NOR b <=> ~(a | b) 00275 virtual Theorem rewriteNOR(const Expr& e) = 0; 00276 //! a - b <=> a + (-b) 00277 virtual Theorem rewriteBVSub(const Expr& e) = 0; 00278 //! k*t = BVPLUS(n, <sum of shifts of t>) -- translation of k*t to BVPLUS 00279 /*! If k = 2^m, return k*t = t\@0...0 */ 00280 virtual Theorem constMultToPlus(const Expr& e) = 0; 00281 //! 0bin0...0 @ BVPLUS(n, args) = BVPLUS(n+k, args) 00282 /*! provided that m+ceil(log2(l)) <= n, where k is the size of the 00283 * 0bin0...0, m is the max. length of each argument, and l is the 00284 * number of arguments. 00285 */ 00286 virtual Theorem bvplusZeroConcatRule(const Expr& e) = 0; 00287 00288 00289 /////////////////////////////////////////////////////////////////// 00290 ///// Bvplus Normal Form rules 00291 /////////////////////////////////////////////////////////////////// 00292 virtual Theorem zeroCoeffBVMult(const Expr& e)=0; 00293 virtual Theorem oneCoeffBVMult(const Expr& e)=0; 00294 virtual Theorem flipBVMult(const Expr& e) = 0; 00295 //! Make args the same length as the result (zero-extend) 00296 virtual Theorem padBVPlus(const Expr& e) = 0; 00297 //! Make args the same length as the result (zero-extend) 00298 virtual Theorem padBVMult(const Expr& e) = 0; 00299 virtual Theorem bvConstMultAssocRule(const Expr& e) = 0; 00300 virtual Theorem bvMultAssocRule(const Expr& e) = 0; 00301 virtual Theorem bvMultDistRule(const Expr& e) = 0; 00302 virtual Theorem flattenBVPlus(const Expr& e) = 0; 00303 virtual Theorem combineLikeTermsRule(const Expr& e) = 0; 00304 virtual Theorem lhsMinusRhsRule(const Expr& e) = 0; 00305 //! (x *[n] y)[m:k] = (x *[m+1] y)[m:k], where m < n 00306 virtual Theorem extractBVMult(const Expr& e) = 0; 00307 //! (x +[n] y)[m:k] = (x +[m+1] y)[m:k], where m < n 00308 virtual Theorem extractBVPlus(const Expr& e) = 0; 00309 //! ite(c,t1,t2)[i:j] <=> ite(c,t1[i:j],t2[i:j]) 00310 virtual Theorem iteExtractRule(const Expr& e) = 0; 00311 //! ~ite(c,t1,t2) <=> ite(c,~t1,~t2) 00312 virtual Theorem iteBVnegRule(const Expr& e) = 0; 00313 00314 virtual Theorem bvuminusBVConst(const Expr& e) = 0; 00315 virtual Theorem bvuminusBVMult(const Expr& e) = 0; 00316 virtual Theorem bvuminusBVUminus(const Expr& e) = 0; 00317 virtual Theorem bvuminusVar(const Expr& e) = 0; 00318 virtual Theorem bvmultBVUminus(const Expr& e) = 0; 00319 //! -t <==> ~t+1 00320 virtual Theorem bvuminusToBVPlus(const Expr& e) = 0; 00321 //! -(c1*t1+...+cn*tn) <==> (-(c1*t1)+...+-(cn*tn)) 00322 virtual Theorem bvuminusBVPlus(const Expr& e) = 0; 00323 00324 00325 00326 /////////////////////////////////////////////////////////////////// 00327 ///// Concatenation Normal Form rules 00328 /////////////////////////////////////////////////////////////////// 00329 00330 // Extraction rules 00331 00332 //! c1[i:j] = c (extraction from a constant bitvector) 00333 virtual Theorem extractConst(const Expr& e) = 0; 00334 //! t[n-1:0] = t for n-bit t 00335 virtual Theorem extractWhole(const Expr& e) = 0; 00336 //! t[i:j][k:l] = t[k+j:l+j] (eliminate double extraction) 00337 virtual Theorem extractExtract(const Expr& e) = 0; 00338 //! (t1 @ t2)[i:j] = t1[...] @ t2[...] (push extraction through concat) 00339 virtual Theorem extractConcat(const Expr& e) = 0; 00340 //! (t1 & t2)[i:j] = t1[i:j] & t2[i:j] (push extraction through OR) 00341 virtual Theorem extractAnd(const Expr& e) = 0; 00342 //! (t1 | t2)[i:j] = t1[i:j] | t2[i:j] (push extraction through AND) 00343 virtual Theorem extractOr(const Expr& e) = 0; 00344 //! (~t)[i:j] = ~(t[i:j]) (push extraction through NEG) 00345 virtual Theorem extractNeg(const Expr& e) = 0; 00346 //! Auxiliary function: (t1 op t2)[i:j] = t1[i:j] op t2[i:j] 00347 virtual Theorem extractBitwise(const Expr& e, 00348 int kind, const std::string& name) = 0; 00349 00350 // Negation rules 00351 00352 //! ~c1 = c (bit-wise negation of a constant bitvector) 00353 virtual Theorem negConst(const Expr& e) = 0; 00354 //! ~(t1\@...\@tn) = (~t1)\@...\@(~tn) -- push negation through concat 00355 virtual Theorem negConcat(const Expr& e) = 0; 00356 //! ~(~t) = t -- eliminate double negation 00357 virtual Theorem negNeg(const Expr& e) = 0; 00358 //! ~(t1 & t2) <=> ~t1 | ~t2 -- DeMorgan's Laws 00359 virtual Theorem negBVand(const Expr& e) = 0; 00360 //! ~(t1 | t2) <=> ~t1 & ~t2 -- DeMorgan's Laws 00361 virtual Theorem negBVor(const Expr& e) = 0; 00362 //! ~(t1 xor t2) = ~t1 xor t2 00363 virtual Theorem negBVxor(const Expr& e) = 0; 00364 //! ~(t1 xnor t2) = t1 xor t2 00365 virtual Theorem negBVxnor(const Expr& e) = 0; 00366 00367 // Bit-wise AND rules 00368 //! c1&c2&t = c&t -- compute bit-wise AND of constant bitvector arguments 00369 /*!\param e is the bit-wise AND expression; 00370 * 00371 * \param idxs are the indices of the constant bitvectors. There 00372 * must be at least constant expressions in this rule. 00373 * 00374 * \return Theorem(e==e'), where e' is either a constant 00375 * bitvector, or is a bit-wise AND with a single constant 00376 * bitvector in e'[0]. 00377 */ 00378 virtual Theorem andConst(const Expr& e, const std::vector<int>& idxs) = 0; 00379 //! 0bin0...0 & t = 0bin0...0 -- bit-wise AND with zero bitvector 00380 /*! \param e is the bit-wise AND expr 00381 * \param idx is the index of the zero bitvector 00382 */ 00383 virtual Theorem andZero(const Expr& e, int idx) = 0; 00384 //! 0bin1...1 & t = t -- bit-wise AND with bitvector of 1's 00385 /*! \param e is the bit-wise AND expr 00386 * \param idxs is a vector of indices of the bitvectors of 1's 00387 * which will be dropped 00388 */ 00389 virtual Theorem andOne(const Expr& e, const std::vector<int> idxs) = 0; 00390 //! ... & (t1\@...\@tk) & ... = (...& t1 &...)\@...\@(...& tk &...) 00391 /*! 00392 * Lifts concatenation to the top of bit-wise AND. Takes the 00393 * bit-wise AND expression 'e' and the index 'i' of the 00394 * concatenation. 00395 */ 00396 virtual Theorem andConcat(const Expr& e, int idx) = 0; 00397 //! (t1 & (t2 & t3) & t4) = t1 & t2 & t3 & t4 -- flatten bit-wise AND 00398 /*! Also reorders the terms according to a fixed total ordering */ 00399 virtual Theorem andFlatten(const Expr& e) = 0; 00400 00401 // Bit-wise OR rules 00402 00403 //! c1|c2|t = c|t -- compute bit-wise OR of constant bitvector arguments 00404 /*!\param e is the bit-wise OR expression; 00405 * 00406 * \param idxs are the indices of the constant bitvectors. There 00407 * must be at least constant expressions in this rule. 00408 * 00409 * \return Theorem(e==e'), where e' is either a constant 00410 * bitvector, or is a bit-wise OR with a single constant 00411 * bitvector in e'[0]. 00412 */ 00413 virtual Theorem orConst(const Expr& e, const std::vector<int>& idxs) = 0; 00414 //! 0bin1...1 | t = 0bin1...1 -- bit-wise OR with bitvector of 1's 00415 /*! \param e is the bit-wise OR expr 00416 * \param idx is the index of the bitvector of 1's 00417 */ 00418 virtual Theorem orOne(const Expr& e, int idx) = 0; 00419 //! 0bin0...0 | t = t -- bit-wise OR with bitvector of 0's 00420 /*! \param e is the bit-wise OR expr 00421 * \param idxs is a vector of indices of the bitvectors of 0's 00422 * which will be dropped 00423 */ 00424 virtual Theorem orZero(const Expr& e, const std::vector<int> idxs) = 0; 00425 //! ... | (t1\@...\@tk) | ... = (...| t1 |...)\@...\@(...| tk |...) 00426 /*! 00427 * Lifts concatenation to the top of bit-wise OR. Takes the 00428 * bit-wise OR expression 'e' and the index 'i' of the 00429 * concatenation. 00430 */ 00431 virtual Theorem orConcat(const Expr& e, int idx) = 0; 00432 //! (t1 | (t2 | t3) | t4) = t1 | t2 | t3 | t4 -- flatten bit-wise OR 00433 /*! Also reorders the terms according to a fixed total ordering */ 00434 virtual Theorem orFlatten(const Expr& e) = 0; 00435 00436 // Concatenation rules 00437 00438 //! c1\@c2\@...\@cn = c (concatenation of constant bitvectors) 00439 virtual Theorem concatConst(const Expr& e) = 0; 00440 //! Flatten one level of nested concatenation, e.g.: x\@(y\@z)\@w = x\@y\@z\@w 00441 virtual Theorem concatFlatten(const Expr& e) = 0; 00442 //! Merge n-ary concat. of adjacent extractions: x[15:8]\@x[7:0] = x[15:0] 00443 virtual Theorem concatMergeExtract(const Expr& e) = 0; 00444 00445 /////////////////////////////////////////////////////////////////// 00446 ///// Modulo arithmetic rules 00447 /////////////////////////////////////////////////////////////////// 00448 00449 //! BVPLUS(n, c1,c2,...,cn) = c (bit-vector plus of constant bitvectors) 00450 virtual Theorem bvplusConst(const Expr& e) = 0; 00451 /*! @brief n*c1 = c, where n >= 0 (multiplication of a constant 00452 * bitvector by a non-negative constant) */ 00453 virtual Theorem bvmultConst(const Expr& e) = 0; 00454 00455 /////////////////////////////////////////////////////////////////// 00456 ///// Type predicate rules 00457 /////////////////////////////////////////////////////////////////// 00458 00459 //! |- t=0 OR t=1 for any 1-bit bitvector t 00460 virtual Theorem typePredBit(const Expr& e) = 0; 00461 //! Expand the type predicate wrapper (compute the actual type predicate) 00462 virtual Theorem expandTypePred(const Theorem& tp) = 0; 00463 00464 00465 }; // end of class BitvectorProofRules 00466 } // end of namespace CVC3 00467 00468 #endif