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 
23 using namespace std;
24 using namespace cvc5;
25 
26 int 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(SELECT, {current_array, zero});
63   Term current_array0_gt_0 =
64       slv.mkTerm(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(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(BITVECTOR_MULT, {two, old_current});
76     // current[i] = 2 * current[i-1]
77     current_array = slv.mkTerm(STORE, {current_array, index, new_current});
78     // current[i-1] < current [i]
79     Term current_slt_new_current =
80         slv.mkTerm(BITVECTOR_SLT, {old_current, new_current});
81     assertions.push_back(current_slt_new_current);
82 
83     old_current = slv.mkTerm(SELECT, {current_array, index});
84   }
85 
86   Term query = slv.mkTerm(NOT, {slv.mkTerm(AND, assertions)});
87 
88   cout << "Asserting " << query << " to cvc5 " << endl;
89   slv.assertFormula(query);
90   cout << "Expect sat. " << endl;
91   cout << "cvc5: " << slv.checkSatAssuming(slv.mkTrue()) << endl;
92 
93   // Getting the model
94   cout << "The satisfying model is: " << endl;
95   cout << "  current_array = " << slv.getValue(current_array) << endl;
96   cout << "  current_array[0] = " << slv.getValue(current_array0) << endl;
97   return 0;
98 }