Theory of Bit-Vectors

examples/api/cpp/bitvectors.cpp

  1/******************************************************************************
  2 * Top contributors (to current version):
  3 *   Liana Hadarean, Aina Niemetz, Mathias Preiner
  4 *
  5 * This file is part of the cvc5 project.
  6 *
  7 * Copyright (c) 2009-2022 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 * A simple demonstration of the solving capabilities of the cvc5
 14 * bit-vector solver.
 15 *
 16 */
 17
 18#include <cvc5/cvc5.h>
 19
 20#include <iostream>
 21
 22using namespace std;
 23using namespace cvc5;
 24
 25int main()
 26{
 27  Solver slv;
 28  slv.setLogic("QF_BV");  // Set the logic
 29
 30  // The following example has been adapted from the book A Hacker's Delight by
 31  // Henry S. Warren.
 32  //
 33  // Given a variable x that can only have two values, a or b. We want to
 34  // assign to x a value other than the current one. The straightforward code
 35  // to do that is:
 36  //
 37  //(0) if (x == a ) x = b;
 38  //    else x = a;
 39  //
 40  // Two more efficient yet equivalent methods are:
 41  //
 42  //(1) x = a ⊕ b ⊕ x;
 43  //
 44  //(2) x = a + b - x;
 45  //
 46  // We will use cvc5 to prove that the three pieces of code above are all
 47  // equivalent by encoding the problem in the bit-vector theory.
 48
 49  // Creating a bit-vector type of width 32
 50  Sort bitvector32 = slv.mkBitVectorSort(32);
 51
 52  // Variables
 53  Term x = slv.mkConst(bitvector32, "x");
 54  Term a = slv.mkConst(bitvector32, "a");
 55  Term b = slv.mkConst(bitvector32, "b");
 56
 57  // First encode the assumption that x must be equal to a or b
 58  Term x_eq_a = slv.mkTerm(Kind::EQUAL, {x, a});
 59  Term x_eq_b = slv.mkTerm(Kind::EQUAL, {x, b});
 60  Term assumption = slv.mkTerm(Kind::OR, {x_eq_a, x_eq_b});
 61
 62  // Assert the assumption
 63  slv.assertFormula(assumption);
 64
 65  // Introduce a new variable for the new value of x after assignment.
 66  Term new_x = slv.mkConst(bitvector32, "new_x");  // x after executing code (0)
 67  Term new_x_ =
 68      slv.mkConst(bitvector32, "new_x_");  // x after executing code (1) or (2)
 69
 70  // Encoding code (0)
 71  // new_x = x == a ? b : a;
 72  Term ite = slv.mkTerm(Kind::ITE, {x_eq_a, b, a});
 73  Term assignment0 = slv.mkTerm(Kind::EQUAL, {new_x, ite});
 74
 75  // Assert the encoding of code (0)
 76  cout << "Asserting " << assignment0 << " to cvc5 " << endl;
 77  slv.assertFormula(assignment0);
 78  cout << "Pushing a new context." << endl;
 79  slv.push();
 80
 81  // Encoding code (1)
 82  // new_x_ = a xor b xor x
 83  Term a_xor_b_xor_x = slv.mkTerm(Kind::BITVECTOR_XOR, {a, b, x});
 84  Term assignment1 = slv.mkTerm(Kind::EQUAL, {new_x_, a_xor_b_xor_x});
 85
 86  // Assert encoding to cvc5 in current context;
 87  cout << "Asserting " << assignment1 << " to cvc5 " << endl;
 88  slv.assertFormula(assignment1);
 89  Term new_x_eq_new_x_ = slv.mkTerm(Kind::EQUAL, {new_x, new_x_});
 90
 91  cout << " Check sat assuming: " << new_x_eq_new_x_.notTerm() << endl;
 92  cout << " Expect UNSAT. " << endl;
 93  cout << " cvc5: " << slv.checkSatAssuming(new_x_eq_new_x_.notTerm()) << endl;
 94  cout << " Popping context. " << endl;
 95  slv.pop();
 96
 97  // Encoding code (2)
 98  // new_x_ = a + b - x
 99  Term a_plus_b = slv.mkTerm(Kind::BITVECTOR_ADD, {a, b});
100  Term a_plus_b_minus_x = slv.mkTerm(Kind::BITVECTOR_SUB, {a_plus_b, x});
101  Term assignment2 = slv.mkTerm(Kind::EQUAL, {new_x_, a_plus_b_minus_x});
102
103  // Assert encoding to cvc5 in current context;
104  cout << "Asserting " << assignment2 << " to cvc5 " << endl;
105  slv.assertFormula(assignment2);
106
107  cout << " Check sat assuming: " << new_x_eq_new_x_.notTerm() << endl;
108  cout << " Expect UNSAT. " << endl;
109  cout << " cvc5: " << slv.checkSatAssuming(new_x_eq_new_x_.notTerm()) << endl;
110
111  Term x_neq_x = slv.mkTerm(Kind::EQUAL, {x, x}).notTerm();
112  std::vector<Term> v{new_x_eq_new_x_, x_neq_x};
113  Term query = slv.mkTerm(Kind::AND, {v});
114  cout << " Check sat assuming: " << query.notTerm() << endl;
115  cout << " Expect SAT. " << endl;
116  cout << " cvc5: " << slv.checkSatAssuming(query.notTerm()) << endl;
117
118  // Assert that a is odd
119  Op extract_op = slv.mkOp(Kind::BITVECTOR_EXTRACT, {0, 0});
120  Term lsb_of_a = slv.mkTerm(extract_op, {a});
121  cout << "Sort of " << lsb_of_a << " is " << lsb_of_a.getSort() << endl;
122  Term a_odd = slv.mkTerm(Kind::EQUAL, {lsb_of_a, slv.mkBitVector(1u, 1u)});
123  cout << "Assert " << a_odd << endl;
124  cout << "Check satisfiability." << endl;
125  slv.assertFormula(a_odd);
126  cout << " Expect sat. " << endl;
127  cout << " cvc5: " << slv.checkSat() << endl;
128  return 0;
129}