Theory of Bit-Vectors and Arrays
examples/api/cpp/bitvectors_and_arrays.cpp
1/******************************************************************************
2 * This file is part of the cvc5 project.
3 *
4 * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
5 * in the top-level source directory and their institutional affiliations.
6 * All rights reserved. See the file COPYING in the top-level source
7 * directory for licensing information.
8 * ****************************************************************************
9 *
10 * A simple demonstration of the solving capabilities of the cvc5
11 * bit-vector and array solvers.
12 *
13 */
14
15#include <cvc5/cvc5.h>
16
17#include <iostream>
18
19using namespace std;
20using namespace cvc5;
21
22int main()
23{
24 TermManager tm;
25 Solver slv(tm);
26 slv.setOption("produce-models", "true"); // Produce Models
27 slv.setLogic("QF_ABV"); // Set the logic
28
29 // Consider the following code (where size is some previously defined constant):
30 //
31 //
32 // Assert (current_array[0] > 0);
33 // for (unsigned i = 1; i < k; ++i) {
34 // current_array[i] = 2 * current_array[i - 1];
35 // Assert (current_array[i-1] < current_array[i]);
36 // }
37 //
38 // We want to check whether the assertion in the body of the for loop holds
39 // throughout the loop.
40
41 // Setting up the problem parameters
42 uint32_t k = 4; // number of unrollings (should be a power of 2)
43 uint32_t index_size = 2; // size of the index, must be log2(k)
44
45 // Sorts
46 Sort elementSort = tm.mkBitVectorSort(32);
47 Sort indexSort = tm.mkBitVectorSort(index_size);
48 Sort arraySort = tm.mkArraySort(indexSort, elementSort);
49
50 // Variables
51 Term current_array = tm.mkConst(arraySort, "current_array");
52
53 // Making a bit-vector constant
54 Term zero = tm.mkBitVector(index_size, 0u);
55
56 // Asserting that current_array[0] > 0
57 Term current_array0 = tm.mkTerm(Kind::SELECT, {current_array, zero});
58 Term current_array0_gt_0 = tm.mkTerm(
59 Kind::BITVECTOR_SGT, {current_array0, tm.mkBitVector(32, 0u)});
60 slv.assertFormula(current_array0_gt_0);
61
62 // Building the assertions in the loop unrolling
63 Term index = tm.mkBitVector(index_size, 0u);
64 Term old_current = tm.mkTerm(Kind::SELECT, {current_array, index});
65 Term two = tm.mkBitVector(32, 2u);
66
67 std::vector<Term> assertions;
68 for (uint32_t i = 1; i < k; ++i)
69 {
70 index = tm.mkBitVector(index_size, i);
71 Term new_current = tm.mkTerm(Kind::BITVECTOR_MULT, {two, old_current});
72 // current[i] = 2 * current[i-1]
73 current_array =
74 tm.mkTerm(Kind::STORE, {current_array, index, new_current});
75 // current[i-1] < current [i]
76 Term current_slt_new_current =
77 tm.mkTerm(Kind::BITVECTOR_SLT, {old_current, new_current});
78 assertions.push_back(current_slt_new_current);
79
80 old_current = tm.mkTerm(Kind::SELECT, {current_array, index});
81 }
82
83 Term query = tm.mkTerm(Kind::NOT, {tm.mkTerm(Kind::AND, assertions)});
84
85 cout << "Asserting " << query << " to cvc5" << endl;
86 slv.assertFormula(query);
87 cout << "Expect sat." << endl;
88 cout << "cvc5: " << slv.checkSat() << endl;
89
90 // Getting the model
91 cout << "The satisfying model is:" << endl;
92 cout << " current_array = " << slv.getValue(current_array) << endl;
93 cout << " current_array[0] = " << slv.getValue(current_array0) << endl;
94 return 0;
95}
examples/api/c/bitvectors_and_arrays.c
1/******************************************************************************
2 * This file is part of the cvc5 project.
3 *
4 * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
5 * in the top-level source directory and their institutional affiliations.
6 * All rights reserved. See the file COPYING in the top-level source
7 * directory for licensing information.
8 * ****************************************************************************
9 *
10 * A simple demonstration of the solving capabilities of the cvc5
11 * bit-vector and array solvers.
12 *
13 */
14
15#include <cvc5/c/cvc5.h>
16#include <stdio.h>
17
18int main()
19{
20 Cvc5TermManager* tm = cvc5_term_manager_new();
21 Cvc5* slv = cvc5_new(tm);
22 cvc5_set_option(slv, "produce-models", "true");
23 cvc5_set_logic(slv, "QF_ABV");
24
25 // Consider the following code (where size is some previously defined
26 // constant):
27 //
28 //
29 // Assert (current_array[0] > 0);
30 // for (unsigned i = 1; i < k; ++i) {
31 // current_array[i] = 2 * current_array[i - 1];
32 // Assert (current_array[i-1] < current_array[i]);
33 // }
34 //
35 // We want to check whether the assertion in the body of the for loop holds
36 // throughout the loop.
37
38 // Setting up the problem parameters
39 uint32_t k = 4; // number of unrollings (should be a power of 2)
40 uint32_t index_size = 2; // size of the index, must be log2(k)
41
42 // Sorts
43 Cvc5Sort elem_sort = cvc5_mk_bv_sort(tm, 32);
44 Cvc5Sort index_sort = cvc5_mk_bv_sort(tm, index_size);
45 Cvc5Sort arr_sort = cvc5_mk_array_sort(tm, index_sort, elem_sort);
46
47 // Variables
48 Cvc5Term cur_arr = cvc5_mk_const(tm, arr_sort, "current_array");
49
50 // Making a bit-vector constant
51 Cvc5Term zero = cvc5_mk_bv_uint64(tm, index_size, 0);
52
53 // Asserting that current_array[0] > 0
54 Cvc5Term args2[2] = {cur_arr, zero};
55 Cvc5Term cur_arr0 = cvc5_mk_term(tm, CVC5_KIND_SELECT, 2, args2);
56 args2[0] = cur_arr0;
57 args2[1] = cvc5_mk_bv_uint64(tm, 32, 0);
58 Cvc5Term cur_arr0_gt_0 = cvc5_mk_term(tm, CVC5_KIND_BITVECTOR_SGT, 2, args2);
59 cvc5_assert_formula(slv, cur_arr0_gt_0);
60
61 // Building the assertions in the loop unrolling
62 Cvc5Term index = cvc5_mk_bv_uint64(tm, index_size, 0);
63 args2[0] = cur_arr;
64 args2[1] = index;
65 Cvc5Term old_cur = cvc5_mk_term(tm, CVC5_KIND_SELECT, 2, args2);
66 Cvc5Term two = cvc5_mk_bv_uint64(tm, 32, 2);
67
68 uint32_t size = k - 1;
69 Cvc5Term assertions[size];
70 for (uint32_t i = 1; i < k; ++i)
71 {
72 index = cvc5_mk_bv_uint64(tm, index_size, i);
73 args2[0] = two;
74 args2[1] = old_cur;
75 Cvc5Term new_cur = cvc5_mk_term(tm, CVC5_KIND_BITVECTOR_MULT, 2, args2);
76 // current[i] = 2 * current[i-1]
77 Cvc5Term args3[3] = {cur_arr, index, new_cur};
78 cur_arr = cvc5_mk_term(tm, CVC5_KIND_STORE, 3, args3);
79 // current[i-1] < current [i]
80 args2[0] = old_cur;
81 args2[1] = new_cur;
82 Cvc5Term cur_slt_new_cur =
83 cvc5_mk_term(tm, CVC5_KIND_BITVECTOR_SLT, 2, args2);
84 assertions[i - 1] = cur_slt_new_cur;
85 args2[0] = cur_arr;
86 args2[1] = index;
87 old_cur = cvc5_mk_term(tm, CVC5_KIND_SELECT, 2, args2);
88 }
89
90 Cvc5Term args1[1] = {cvc5_mk_term(tm, CVC5_KIND_AND, size, assertions)};
91 Cvc5Term query = cvc5_mk_term(tm, CVC5_KIND_NOT, 1, args1);
92
93 printf("Asserting %s to cvc5\n", cvc5_term_to_string(query));
94 cvc5_assert_formula(slv, query);
95 printf("Expect sat.\n");
96 printf("cvc5: %s\n", cvc5_result_to_string(cvc5_check_sat(slv)));
97
98 // Getting the model
99 printf("The satisfying model is:\n");
100 printf(" current_array = %s\n",
101 cvc5_term_to_string(cvc5_get_value(slv, cur_arr)));
102 printf(" current_array[0] = %s\n",
103 cvc5_term_to_string(cvc5_get_value(slv, cur_arr0)));
104
105 cvc5_delete(slv);
106 cvc5_term_manager_delete(tm);
107 return 0;
108}
examples/api/java/BitVectorsAndArrays.java
1/******************************************************************************
2 * This file is part of the cvc5 project.
3 *
4 * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
5 * in the top-level source directory and their institutional affiliations.
6 * All rights reserved. See the file COPYING in the top-level source
7 * directory for licensing information.
8 * ****************************************************************************
9 *
10 * A simple demonstration of the solving capabilities of the cvc5
11 * bit-vector solver.
12 *
13 */
14
15import io.github.cvc5.*;
16import java.util.*;
17
18public class BitVectorsAndArrays
19{
20 private static int log2(int n)
21 {
22 return (int) Math.round(Math.log(n) / Math.log(2));
23 }
24
25 public static void main(String[] args) throws CVC5ApiException
26 {
27 TermManager tm = new TermManager();
28 Solver slv = new Solver(tm);
29 {
30 slv.setOption("produce-models", "true"); // Produce Models
31 slv.setOption("output-language", "smtlib"); // output-language
32 slv.setLogic("QF_ABV"); // 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 int k = 4; // number of unrollings (should be a power of 2)
48 int index_size = log2(k); // size of the index
49
50 // Sorts
51 Sort elementSort = tm.mkBitVectorSort(32);
52 Sort indexSort = tm.mkBitVectorSort(index_size);
53 Sort arraySort = tm.mkArraySort(indexSort, elementSort);
54
55 // Variables
56 Term current_array = tm.mkConst(arraySort, "current_array");
57
58 // Making a bit-vector constant
59 Term zero = tm.mkBitVector(index_size, 0);
60
61 // Asserting that current_array[0] > 0
62 Term current_array0 = tm.mkTerm(Kind.SELECT, current_array, zero);
63 Term current_array0_gt_0 =
64 tm.mkTerm(Kind.BITVECTOR_SGT, current_array0, tm.mkBitVector(32, 0));
65 slv.assertFormula(current_array0_gt_0);
66
67 // Building the assertions in the loop unrolling
68 Term index = tm.mkBitVector(index_size, 0);
69 Term old_current = tm.mkTerm(Kind.SELECT, current_array, index);
70 Term two = tm.mkBitVector(32, 2);
71
72 List<Term> assertions = new ArrayList<Term>();
73 for (int i = 1; i < k; ++i)
74 {
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 = tm.mkTerm(Kind.STORE, current_array, index, new_current);
79 // current[i-1] < current [i]
80 Term current_slt_new_current = tm.mkTerm(Kind.BITVECTOR_SLT, old_current, new_current);
81 assertions.add(current_slt_new_current);
82
83 old_current = tm.mkTerm(Kind.SELECT, current_array, index);
84 }
85
86 Term query = tm.mkTerm(Kind.NOT, tm.mkTerm(Kind.AND, assertions.toArray(new Term[0])));
87
88 System.out.println("Asserting " + query + " to cvc5 ");
89 slv.assertFormula(query);
90 System.out.println("Expect sat. ");
91 System.out.println("cvc5: " + slv.checkSatAssuming(tm.mkTrue()));
92
93 // Getting the model
94 System.out.println("The satisfying model is: ");
95 System.out.println(" current_array = " + slv.getValue(current_array));
96 System.out.println(" current_array[0] = " + slv.getValue(current_array0));
97 }
98 Context.deletePointers();
99 }
100}
examples/api/python/pythonic/bitvectors_and_arrays.py
1###############################################################################
2# This file is part of the cvc5 project.
3#
4# Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
5# in the top-level source directory and their institutional affiliations.
6# All rights reserved. See the file COPYING in the top-level source
7# directory for licensing information.
8# #############################################################################
9#
10# A simple demonstration of the solving capabilities of the cvc5
11# bit-vector and array solvers.
12##
13from cvc5.pythonic import *
14
15if __name__ == '__main__':
16 # Consider the following (where k is some previously defined constant):
17 #
18 #
19 # Assert (current_array[0] > 0);
20 # for (unsigned i = 1; i < k; ++i) {
21 # current_array[i] = 2 * current_array[i - 1];
22 # Assert (current_array[i-1] < current_array[i]);
23 # }
24 #
25 # We want to check whether the assertion in the body of the for loop holds
26 # throughout the loop.
27 k = 4
28 idx_bits = int(math.ceil(math.log(k, 2)))
29
30 init_array = Array('init_arr', BitVecSort(idx_bits), BitVecSort(32))
31 array = init_array
32 assertions = []
33 for i in range(1, k):
34 array = Store(array, i, 2 * Select(array, i - 1))
35 assertions.append(Select(array, i - 1) < Select(array, i))
36 # Does *not* hold.
37 # If the first element is large enough, the multiplication overflows.
38 prove(Implies(Select(init_array, 0) > 0, And(*assertions)))
examples/api/python/bitvectors_and_arrays.py
1#!/usr/bin/env python
2###############################################################################
3# This file is part of the cvc5 project.
4#
5# Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
6# in the top-level source directory and their institutional affiliations.
7# All rights reserved. See the file COPYING in the top-level source
8# directory for licensing information.
9# #############################################################################
10#
11# A simple demonstration of the solving capabilities of the cvc5
12# bit-vector and array solvers through the Python API. This is a direct
13# translation of bitvectors_and_arrays-new.cpp.
14##
15
16import cvc5
17from cvc5 import Kind
18
19import math
20
21if __name__ == "__main__":
22 tm = cvc5.TermManager()
23 slv = cvc5.Solver(tm)
24 slv.setOption("produce-models", "true")
25 slv.setOption("output-language", "smtlib")
26 slv.setLogic("QF_ABV")
27
28 # Consider the following code (where size is some previously defined constant):
29 #
30 #
31 # Assert (current_array[0] > 0);
32 # for (unsigned i = 1; i < k; ++i) {
33 # current_array[i] = 2 * current_array[i - 1];
34 # Assert (current_array[i-1] < current_array[i]);
35 # }
36 #
37 # We want to check whether the assertion in the body of the for loop holds
38 # throughout the loop.
39
40 # Setting up the problem parameters
41 k = 4
42 index_size = int(math.ceil(math.log(k, 2)))
43
44 # Sorts
45 elementSort = tm.mkBitVectorSort(32)
46 indexSort = tm.mkBitVectorSort(index_size)
47 arraySort = tm.mkArraySort(indexSort, elementSort)
48
49 # Variables
50 current_array = tm.mkConst(arraySort, "current_array")
51
52 # Making a bit-vector constant
53 zero = tm.mkBitVector(index_size, 0)
54
55 # Test making a constant array
56 constarr0 = tm.mkConstArray(arraySort, tm.mkBitVector(32, 0))
57
58 # Asserting that current_array[0] > 0
59 current_array0 = tm.mkTerm(Kind.SELECT, current_array, zero)
60 current_array0_gt_0 = tm.mkTerm(Kind.BITVECTOR_SGT,
61 current_array0,
62 tm.mkBitVector(32, 0))
63 slv.assertFormula(current_array0_gt_0)
64
65 # Building the assertions in the loop unrolling
66 index = tm.mkBitVector(index_size, 0)
67 old_current = tm.mkTerm(Kind.SELECT, current_array, index)
68 two = tm.mkBitVector(32, 2)
69
70 assertions = []
71 for i in range(1, k):
72 index = tm.mkBitVector(index_size, i)
73 new_current = tm.mkTerm(Kind.BITVECTOR_MULT, two, old_current)
74 # current[i] = 2*current[i-1]
75 current_array = \
76 tm.mkTerm(Kind.STORE, current_array, index, new_current)
77 # current[i-1] < current[i]
78 current_slt_new_current = \
79 tm.mkTerm(Kind.BITVECTOR_SLT, old_current, new_current)
80 assertions.append(current_slt_new_current)
81 old_current = tm.mkTerm(Kind.SELECT, current_array, index)
82
83 query = tm.mkTerm(Kind.NOT, tm.mkTerm(Kind.AND, *assertions))
84
85 print("Asserting {} to cvc5".format(query))
86 slv.assertFormula(query)
87 print("Expect sat.")
88 print("cvc5:", slv.checkSatAssuming(tm.mkTrue()))
89
90 # Getting the model
91 print("The satisfying model is: ")
92 print(" current_array =", slv.getValue(current_array))
93 print(" current_array[0]", slv.getValue(current_array0))
examples/api/smtlib/bitvectors_and_arrays.smt2
1(set-logic QF_ABV)
2(set-option :produce-models true)
3
4; Consider the following code (where size is some previously defined constant):
5
6
7; Assert (current_array[0] > 0);
8; for (unsigned i = 1; i < k; ++i) {
9; current_array[i] = 2 * current_array[i - 1];
10; Assert (current_array[i-1] < current_array[i]);
11; }
12
13; We want to check whether the assertion in the body of the for loop holds
14; throughout the loop. We will do so for k = 4.
15
16
17(define-sort Index () (_ BitVec 2))
18(define-sort Element () (_ BitVec 32))
19(define-sort ArraySort () (Array Index Element))
20
21; Declaring the array
22(declare-const current_array ArraySort)
23
24; Making utility bit-vector constants
25(define-const zeroI Index (_ bv0 2))
26(define-const oneI Index (_ bv1 2))
27(define-const twoI Index (_ bv2 2))
28(define-const threeI Index (_ bv3 2))
29
30(define-const zeroE Element (_ bv0 32))
31(define-const twoE Element (_ bv2 32))
32
33; Asserting that current_array[0] > 0
34(assert (bvsgt (select current_array zeroI) zeroE))
35
36; Building the formulas representing loop unrolling
37
38; current_array[0] < array [1]
39(define-const unroll1 Bool (bvslt (select current_array zeroI) (bvmul twoE (select current_array zeroI))))
40; current_array[1] = 2 * current_array[0]
41(define-const current_array_ ArraySort (store current_array oneI (bvmul twoE (select current_array zeroI))))
42
43; current_array[1] < array [2]
44(define-const unroll2 Bool (bvslt (select current_array_ oneI) (bvmul twoE (select current_array_ oneI))))
45; current_array[2] = 2 * current_array[1]
46(define-const current_array__ ArraySort (store current_array_ twoI (bvmul twoE (select current_array_ oneI))))
47
48; current_array[2] < array [3]
49(define-const unroll3 Bool (bvslt (select current_array_ twoI) (bvmul twoE (select current_array_ twoI))))
50; current_array[3] = 2 * current_array[2]
51(define-const current_array___ ArraySort (store current_array_ threeI (bvmul twoE (select current_array_ twoI))))
52
53(assert (not (and unroll1 unroll2 unroll3)))
54
55(check-sat)
56(get-value (current_array___ (select current_array___ zeroI)))