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}
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_AUFBV"); // 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_AUFBV")
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_AUFBV)
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)))