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