int main() { ExprManager em; SmtEngine smt(&em); smt.setLogic("QF_BV"); // Set the logic Type bitvector32 = em.mkBitVectorType(32); Expr x = em.mkVar("a", bitvector32); Expr ext_31_1 = em.mkConst(CVC4::BitVectorExtract(31,1)); Expr x_31_1 = em.mkExpr(ext_31_1, x); Expr ext_30_0 = em.mkConst(CVC4::BitVectorExtract(30,0)); Expr x_30_0 = em.mkExpr(ext_30_0, x); Expr ext_31_31 = em.mkConst(CVC4::BitVectorExtract(31,31)); Expr x_31_31 = em.mkExpr(ext_31_31, x); Expr ext_0_0 = em.mkConst(CVC4::BitVectorExtract(0,0)); Expr x_0_0 = em.mkExpr(ext_0_0, x); Expr eq = em.mkExpr(kind::EQUAL, x_31_1, x_30_0); cout << " Asserting: " << eq << endl; smt.assertFormula(eq); Expr eq2 = em.mkExpr(kind::EQUAL, x_31_31, x_0_0); cout << " Querying: " << eq2 << endl; cout << " Expect valid. " << endl; cout << " CVC4: " << smt.query(eq2) << endl; return 0; }
int main() { ExprManager em; SmtEngine smt(&em); smt.setLogic("QF_BV"); // Set the logic // The following example has been adapted from the book A Hacker's Delight by // Henry S. Warren. // // Given a variable x that can only have two values, a or b. We want to // assign to x a value other than the current one. The straightforward code // to do that is: // //(0) if (x == a ) x = b; // else x = a; // // Two more efficient yet equivalent methods are: // //(1) x = a ⊕ b ⊕ x; // //(2) x = a + b - x; // // We will use CVC4 to prove that the three pieces of code above are all // equivalent by encoding the problem in the bit-vector theory. // Creating a bit-vector type of width 32 Type bitvector32 = em.mkBitVectorType(32); // Variables Expr x = em.mkVar("x", bitvector32); Expr a = em.mkVar("a", bitvector32); Expr b = em.mkVar("b", bitvector32); // First encode the assumption that x must be equal to a or b Expr x_eq_a = em.mkExpr(kind::EQUAL, x, a); Expr x_eq_b = em.mkExpr(kind::EQUAL, x, b); Expr assumption = em.mkExpr(kind::OR, x_eq_a, x_eq_b); // Assert the assumption smt.assertFormula(assumption); // Introduce a new variable for the new value of x after assignment. Expr new_x = em.mkVar("new_x", bitvector32); // x after executing code (0) Expr new_x_ = em.mkVar("new_x_", bitvector32); // x after executing code (1) or (2) // Encoding code (0) // new_x = x == a ? b : a; Expr ite = em.mkExpr(kind::ITE, x_eq_a, b, a); Expr assignment0 = em.mkExpr(kind::EQUAL, new_x, ite); // Assert the encoding of code (0) cout << "Asserting " << assignment0 << " to CVC4 " << endl; smt.assertFormula(assignment0); cout << "Pushing a new context." << endl; smt.push(); // Encoding code (1) // new_x_ = a xor b xor x Expr a_xor_b_xor_x = em.mkExpr(kind::BITVECTOR_XOR, a, b, x); Expr assignment1 = em.mkExpr(kind::EQUAL, new_x_, a_xor_b_xor_x); // Assert encoding to CVC4 in current context; cout << "Asserting " << assignment1 << " to CVC4 " << endl; smt.assertFormula(assignment1); Expr new_x_eq_new_x_ = em.mkExpr(kind::EQUAL, new_x, new_x_); cout << " Querying: " << new_x_eq_new_x_ << endl; cout << " Expect valid. " << endl; cout << " CVC4: " << smt.query(new_x_eq_new_x_) << endl; cout << " Popping context. " << endl; smt.pop(); // Encoding code (2) // new_x_ = a + b - x Expr a_plus_b = em.mkExpr(kind::BITVECTOR_PLUS, a, b); Expr a_plus_b_minus_x = em.mkExpr(kind::BITVECTOR_SUB, a_plus_b, x); Expr assignment2 = em.mkExpr(kind::EQUAL, new_x_, a_plus_b_minus_x); // Assert encoding to CVC4 in current context; cout << "Asserting " << assignment2 << " to CVC4 " << endl; smt.assertFormula(assignment2); cout << " Querying: " << new_x_eq_new_x_ << endl; cout << " Expect valid. " << endl; cout << " CVC4: " << smt.query(new_x_eq_new_x_) << endl; return 0; }
int main() { ExprManager em; SmtEngine smt(&em); smt.setOption("produce-models", true); // Produce Models smt.setOption("output-language", "smtlib"); // output-language smt.setLogic("QF_AUFBV"); // Set the logic // Consider the following code (where size is some previously defined constant): // // // Assert (current_array[0] > 0); // for (unsigned i = 1; i < k; ++i) { // current_array[i] = 2 * current_array[i - 1]; // Assert (current_array[i-1] < current_array[i]); // } // // We want to check whether the assertion in the body of the for loop holds // throughout the loop. // Setting up the problem parameters unsigned k = 4; // number of unrollings (should be a power of 2) unsigned index_size = log2(k); // size of the index // Types Type elementType = em.mkBitVectorType(32); Type indexType = em.mkBitVectorType(index_size); Type arrayType = em.mkArrayType(indexType, elementType); // Variables Expr current_array = em.mkVar("current_array", arrayType); // Making a bit-vector constant Expr zero = em.mkConst(BitVector(index_size, 0u)); // Asserting that current_array[0] > 0 Expr current_array0 = em.mkExpr(kind::SELECT, current_array, zero); Expr current_array0_gt_0 = em.mkExpr(kind::BITVECTOR_SGT, current_array0, em.mkConst(BitVector(32, 0u))); smt.assertFormula(current_array0_gt_0); // Building the assertions in the loop unrolling Expr index = em.mkConst(BitVector(index_size, 0u)); Expr old_current = em.mkExpr(kind::SELECT, current_array, index); Expr two = em.mkConst(BitVector(32, 2u)); std::vector<Expr> assertions; for (unsigned i = 1; i < k; ++i) { index = em.mkConst(BitVector(index_size, Integer(i))); Expr new_current = em.mkExpr(kind::BITVECTOR_MULT, two, old_current); // current[i] = 2 * current[i-1] current_array = em.mkExpr(kind::STORE, current_array, index, new_current); // current[i-1] < current [i] Expr current_slt_new_current = em.mkExpr(kind::BITVECTOR_SLT, old_current, new_current); assertions.push_back(current_slt_new_current); old_current = em.mkExpr(kind::SELECT, current_array, index); } Expr query = em.mkExpr(kind::NOT, em.mkExpr(kind::AND, assertions)); cout << "Asserting " << query << " to CVC4 " << endl; smt.assertFormula(query); cout << "Expect sat. " << endl; cout << "CVC4: " << smt.checkSat(em.mkConst(true)) << endl; // Getting the model cout << "The satisfying model is: " << endl; cout << " current_array = " << smt.getValue(current_array) << endl; cout << " current_array[0] = " << smt.getValue(current_array0) << endl; return 0; }