Theory of Bit-Vectors and Arrays

examples/api/cpp/bitvectors_and_arrays.cpp

  1/******************************************************************************
  2 * Top contributors (to current version):
  3 *   Aina Niemetz, Liana Hadarean, Mathias Preiner
  4 *
  5 * This file is part of the cvc5 project.
  6 *
  7 * Copyright (c) 2009-2024 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 and array solvers.
 15 *
 16 */
 17
 18#include <cvc5/cvc5.h>
 19
 20#include <cmath>
 21#include <iostream>
 22
 23using namespace std;
 24using namespace cvc5;
 25
 26int main()
 27{
 28  TermManager tm;
 29  Solver slv(tm);
 30  slv.setOption("produce-models", "true");      // Produce Models
 31  slv.setOption("output-language", "smtlib"); // output-language
 32  slv.setLogic("QF_AUFBV");                   // Set the logic
 33
 34  // Consider the following code (where size is some previously defined constant):
 35  //
 36  //
 37  //   Assert (current_array[0] > 0);
 38  //   for (unsigned i = 1; i < k; ++i) {
 39  //     current_array[i] = 2 * current_array[i - 1];
 40  //     Assert (current_array[i-1] < current_array[i]);
 41  //     }
 42  //
 43  // We want to check whether the assertion in the body of the for loop holds
 44  // throughout the loop.
 45
 46  // Setting up the problem parameters
 47  unsigned k = 4;                // number of unrollings (should be a power of 2)
 48  unsigned index_size = log2(k); // size of the index
 49
 50
 51  // Sorts
 52  Sort elementSort = tm.mkBitVectorSort(32);
 53  Sort indexSort = tm.mkBitVectorSort(index_size);
 54  Sort arraySort = tm.mkArraySort(indexSort, elementSort);
 55
 56  // Variables
 57  Term current_array = tm.mkConst(arraySort, "current_array");
 58
 59  // Making a bit-vector constant
 60  Term zero = tm.mkBitVector(index_size, 0u);
 61
 62  // Asserting that current_array[0] > 0
 63  Term current_array0 = tm.mkTerm(Kind::SELECT, {current_array, zero});
 64  Term current_array0_gt_0 = tm.mkTerm(
 65      Kind::BITVECTOR_SGT, {current_array0, tm.mkBitVector(32, 0u)});
 66  slv.assertFormula(current_array0_gt_0);
 67
 68  // Building the assertions in the loop unrolling
 69  Term index = tm.mkBitVector(index_size, 0u);
 70  Term old_current = tm.mkTerm(Kind::SELECT, {current_array, index});
 71  Term two = tm.mkBitVector(32, 2u);
 72
 73  std::vector<Term> assertions;
 74  for (unsigned i = 1; i < k; ++i) {
 75    index = tm.mkBitVector(index_size, i);
 76    Term new_current = tm.mkTerm(Kind::BITVECTOR_MULT, {two, old_current});
 77    // current[i] = 2 * current[i-1]
 78    current_array =
 79        tm.mkTerm(Kind::STORE, {current_array, index, new_current});
 80    // current[i-1] < current [i]
 81    Term current_slt_new_current =
 82        tm.mkTerm(Kind::BITVECTOR_SLT, {old_current, new_current});
 83    assertions.push_back(current_slt_new_current);
 84
 85    old_current = tm.mkTerm(Kind::SELECT, {current_array, index});
 86  }
 87
 88  Term query = tm.mkTerm(Kind::NOT, {tm.mkTerm(Kind::AND, assertions)});
 89
 90  cout << "Asserting " << query << " to cvc5 " << endl;
 91  slv.assertFormula(query);
 92  cout << "Expect sat. " << endl;
 93  cout << "cvc5: " << slv.checkSatAssuming(tm.mkTrue()) << endl;
 94
 95  // Getting the model
 96  cout << "The satisfying model is: " << endl;
 97  cout << "  current_array = " << slv.getValue(current_array) << endl;
 98  cout << "  current_array[0] = " << slv.getValue(current_array0) << endl;
 99  return 0;
100}