Theory of Bit-Vectors and Arrays

examples/api/cpp/bitvectors_and_arrays.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 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  Solver slv;
29  slv.setOption("produce-models", "true");      // Produce Models
30  slv.setOption("output-language", "smtlib"); // output-language
31  slv.setLogic("QF_AUFBV");                   // Set the logic
32
33  // Consider the following code (where size is some previously defined constant):
34  //
35  //
36  //   Assert (current_array[0] > 0);
37  //   for (unsigned i = 1; i < k; ++i) {
38  //     current_array[i] = 2 * current_array[i - 1];
39  //     Assert (current_array[i-1] < current_array[i]);
40  //     }
41  //
42  // We want to check whether the assertion in the body of the for loop holds
43  // throughout the loop.
44
45  // Setting up the problem parameters
46  unsigned k = 4;                // number of unrollings (should be a power of 2)
47  unsigned index_size = log2(k); // size of the index
48
49
50  // Sorts
51  Sort elementSort = slv.mkBitVectorSort(32);
52  Sort indexSort = slv.mkBitVectorSort(index_size);
53  Sort arraySort = slv.mkArraySort(indexSort, elementSort);
54
55  // Variables
56  Term current_array = slv.mkConst(arraySort, "current_array");
57
58  // Making a bit-vector constant
59  Term zero = slv.mkBitVector(index_size, 0u);
60
61  // Asserting that current_array[0] > 0
62  Term current_array0 = slv.mkTerm(Kind::SELECT, {current_array, zero});
63  Term current_array0_gt_0 = slv.mkTerm(
64      Kind::BITVECTOR_SGT, {current_array0, slv.mkBitVector(32, 0u)});
65  slv.assertFormula(current_array0_gt_0);
66
67  // Building the assertions in the loop unrolling
68  Term index = slv.mkBitVector(index_size, 0u);
69  Term old_current = slv.mkTerm(Kind::SELECT, {current_array, index});
70  Term two = slv.mkBitVector(32, 2u);
71
72  std::vector<Term> assertions;
73  for (unsigned i = 1; i < k; ++i) {
74    index = slv.mkBitVector(index_size, i);
75    Term new_current = slv.mkTerm(Kind::BITVECTOR_MULT, {two, old_current});
76    // current[i] = 2 * current[i-1]
77    current_array =
78        slv.mkTerm(Kind::STORE, {current_array, index, new_current});
79    // current[i-1] < current [i]
80    Term current_slt_new_current =
81        slv.mkTerm(Kind::BITVECTOR_SLT, {old_current, new_current});
82    assertions.push_back(current_slt_new_current);
83
84    old_current = slv.mkTerm(Kind::SELECT, {current_array, index});
85  }
86
87  Term query = slv.mkTerm(Kind::NOT, {slv.mkTerm(Kind::AND, assertions)});
88
89  cout << "Asserting " << query << " to cvc5 " << endl;
90  slv.assertFormula(query);
91  cout << "Expect sat. " << endl;
92  cout << "cvc5: " << slv.checkSatAssuming(slv.mkTrue()) << endl;
93
94  // Getting the model
95  cout << "The satisfying model is: " << endl;
96  cout << "  current_array = " << slv.getValue(current_array) << endl;
97  cout << "  current_array[0] = " << slv.getValue(current_array0) << endl;
98  return 0;
99}