Theory of Bit-Vectors
examples/api/cpp/bitvectors.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 solver.
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.setLogic("QF_BV"); // Set the logic
27
28 // The following example has been adapted from the book A Hacker's Delight by
29 // Henry S. Warren.
30 //
31 // Given a variable x that can only have two values, a or b. We want to
32 // assign to x a value other than the current one. The straightforward code
33 // to do that is:
34 //
35 //(0) if (x == a ) x = b;
36 // else x = a;
37 //
38 // Two more efficient yet equivalent methods are:
39 //
40 //(1) x = a ⊕ b ⊕ x;
41 //
42 //(2) x = a + b - x;
43 //
44 // We will use cvc5 to prove that the three pieces of code above are all
45 // equivalent by encoding the problem in the bit-vector theory.
46
47 // Creating a bit-vector type of width 32
48 Sort bv32 = tm.mkBitVectorSort(32);
49
50 // Variables
51 Term x = tm.mkConst(bv32, "x");
52 Term a = tm.mkConst(bv32, "a");
53 Term b = tm.mkConst(bv32, "b");
54
55 // First encode the assumption that x must be equal to a or b
56 Term x_eq_a = tm.mkTerm(Kind::EQUAL, {x, a});
57 Term x_eq_b = tm.mkTerm(Kind::EQUAL, {x, b});
58 Term assumption = tm.mkTerm(Kind::OR, {x_eq_a, x_eq_b});
59
60 // Assert the assumption
61 slv.assertFormula(assumption);
62
63 // Introduce a new variable for the new value of x after assignment.
64 Term new_x = tm.mkConst(bv32, "new_x"); // x after executing code (0)
65 Term new_x_ =
66 tm.mkConst(bv32, "new_x_"); // x after executing code (1) or (2)
67
68 // Encoding code (0)
69 // new_x = x == a ? b : a;
70 Term ite = tm.mkTerm(Kind::ITE, {x_eq_a, b, a});
71 Term assignment0 = tm.mkTerm(Kind::EQUAL, {new_x, ite});
72
73 // Assert the encoding of code (0)
74 cout << "Asserting " << assignment0 << " to cvc5" << endl;
75 slv.assertFormula(assignment0);
76 cout << "Pushing a new context." << endl;
77 slv.push();
78
79 // Encoding code (1)
80 // new_x_ = a xor b xor x
81 Term a_xor_b_xor_x = tm.mkTerm(Kind::BITVECTOR_XOR, {a, b, x});
82 Term assignment1 = tm.mkTerm(Kind::EQUAL, {new_x_, a_xor_b_xor_x});
83
84 // Assert encoding to cvc5 in current context;
85 cout << "Asserting " << assignment1 << " to cvc5" << endl;
86 slv.assertFormula(assignment1);
87 Term new_x_eq_new_x_ = tm.mkTerm(Kind::EQUAL, {new_x, new_x_});
88
89 cout << " Check sat assuming: " << new_x_eq_new_x_.notTerm() << endl;
90 cout << " Expect UNSAT." << endl;
91 cout << " cvc5: " << slv.checkSatAssuming(new_x_eq_new_x_.notTerm()) << endl;
92 cout << " Popping context." << endl;
93 slv.pop();
94
95 // Encoding code (2)
96 // new_x_ = a + b - x
97 Term a_plus_b = tm.mkTerm(Kind::BITVECTOR_ADD, {a, b});
98 Term a_plus_b_minus_x = tm.mkTerm(Kind::BITVECTOR_SUB, {a_plus_b, x});
99 Term assignment2 = tm.mkTerm(Kind::EQUAL, {new_x_, a_plus_b_minus_x});
100
101 // Assert encoding to cvc5 in current context;
102 cout << "Asserting " << assignment2 << " to cvc5" << endl;
103 slv.assertFormula(assignment2);
104
105 cout << " Check sat assuming: " << new_x_eq_new_x_.notTerm() << endl;
106 cout << " Expect UNSAT." << endl;
107 cout << " cvc5: " << slv.checkSatAssuming(new_x_eq_new_x_.notTerm()) << endl;
108
109 Term x_neq_x = tm.mkTerm(Kind::DISTINCT, {x, x});
110 std::vector<Term> v{new_x_eq_new_x_, x_neq_x};
111 Term query = tm.mkTerm(Kind::AND, {v});
112 cout << " Check sat assuming: " << query.notTerm() << endl;
113 cout << " Expect SAT." << endl;
114 cout << " cvc5: " << slv.checkSatAssuming(query.notTerm()) << endl;
115
116 // Assert that a is odd
117 Op extract_op = tm.mkOp(Kind::BITVECTOR_EXTRACT, {0, 0});
118 Term lsb_of_a = tm.mkTerm(extract_op, {a});
119 cout << "Sort of " << lsb_of_a << " is " << lsb_of_a.getSort() << endl;
120 Term a_odd = tm.mkTerm(Kind::EQUAL, {lsb_of_a, tm.mkBitVector(1u, 1u)});
121 cout << "Assert " << a_odd << endl;
122 cout << "Check satisfiability." << endl;
123 slv.assertFormula(a_odd);
124 cout << " Expect sat." << endl;
125 cout << " cvc5: " << slv.checkSat() << endl;
126 return 0;
127}
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
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_logic(slv, "QF_BV");
23
24 // The following example has been adapted from the book A Hacker's Delight by
25 // Henry S. Warren.
26 //
27 // Given a variable x that can only have two values, a or b. We want to
28 // assign to x a value other than the current one. The straightforward code
29 // to do that is:
30 //
31 //(0) if (x == a ) x = b;
32 // else x = a;
33 //
34 // Two more efficient yet equivalent methods are:
35 //
36 //(1) x = a ⊕ b ⊕ x;
37 //
38 //(2) x = a + b - x;
39 //
40 // We will use cvc5 to prove that the three pieces of code above are all
41 // equivalent by encoding the problem in the bit-vector theory.
42
43 // Creating a bit-vector type of width 32
44 Cvc5Sort bv32 = cvc5_mk_bv_sort(tm, 32);
45
46 // Variables
47 Cvc5Term x = cvc5_mk_const(tm, bv32, "x");
48 Cvc5Term a = cvc5_mk_const(tm, bv32, "a");
49 Cvc5Term b = cvc5_mk_const(tm, bv32, "b");
50
51 Cvc5Term args2[2];
52
53 // First encode the assumption that x must be equal to a or b
54 args2[0] = x;
55 args2[1] = a;
56 Cvc5Term x_eq_a = cvc5_mk_term(tm, CVC5_KIND_EQUAL, 2, args2);
57 args2[0] = x;
58 args2[1] = b;
59 Cvc5Term x_eq_b = cvc5_mk_term(tm, CVC5_KIND_EQUAL, 2, args2);
60 args2[0] = x_eq_a;
61 args2[1] = x_eq_b;
62 Cvc5Term assumption = cvc5_mk_term(tm, CVC5_KIND_OR, 2, args2);
63
64 // Assert the assumption
65 cvc5_assert_formula(slv, assumption);
66
67 // Introduce a new variable for the new value of x after assignment.
68 // x after executing code (0)
69 Cvc5Term new_x = cvc5_mk_const(tm, bv32, "new_x");
70 // x after executing code (1) or (2)
71 Cvc5Term new_x_ = cvc5_mk_const(tm, bv32, "new_x_");
72
73 // Encoding code (0)
74 // new_x = x == a ? b : a;
75 Cvc5Term args3[3] = {x_eq_a, b, a};
76 Cvc5Term ite = cvc5_mk_term(tm, CVC5_KIND_ITE, 3, args3);
77 args2[0] = new_x;
78 args2[1] = ite;
79 Cvc5Term assignment0 = cvc5_mk_term(tm, CVC5_KIND_EQUAL, 2, args2);
80
81 // Assert the encoding of code (0)
82 printf("Asserting %s to cvc5\n", cvc5_term_to_string(assignment0));
83 cvc5_assert_formula(slv, assignment0);
84 printf("Pushing a new context.\n");
85 cvc5_push(slv, 1);
86
87 // Encoding code (1)
88 // new_x_ = a xor b xor x
89 args3[0] = a;
90 args3[1] = b;
91 args3[2] = x;
92 Cvc5Term a_xor_b_xor_x = cvc5_mk_term(tm, CVC5_KIND_BITVECTOR_XOR, 3, args3);
93 args2[0] = new_x_;
94 args2[1] = a_xor_b_xor_x;
95 Cvc5Term assignment1 = cvc5_mk_term(tm, CVC5_KIND_EQUAL, 2, args2);
96
97 // Assert encoding to cvc5 in current context;
98 printf("Asserting %s to cvc5\n", cvc5_term_to_string(assignment1));
99 cvc5_assert_formula(slv, assignment1);
100 args2[0] = new_x;
101 args2[1] = new_x_;
102 Cvc5Term new_x_eq_new_x_ = cvc5_mk_term(tm, CVC5_KIND_EQUAL, 2, args2);
103
104 Cvc5Term args1[1] = {new_x_eq_new_x_};
105 Cvc5Term not_new_x_eq_new_x_ = cvc5_mk_term(tm, CVC5_KIND_NOT, 1, args1);
106 printf(" Check sat assuming: %s\n", cvc5_term_to_string(not_new_x_eq_new_x_));
107 printf(" Expect UNSAT.\n");
108
109 Cvc5Term assumptions[1] = {not_new_x_eq_new_x_};
110 printf(" cvc5: %s\n",
111 cvc5_result_to_string(cvc5_check_sat_assuming(slv, 1, assumptions)));
112 printf(" Popping context.\n");
113 cvc5_pop(slv, 1);
114
115 // Encoding code (2)
116 // new_x_ = a + b - x
117 args2[0] = a;
118 args2[1] = b;
119 Cvc5Term a_plus_b = cvc5_mk_term(tm, CVC5_KIND_BITVECTOR_ADD, 2, args2);
120 args2[0] = a_plus_b;
121 args2[1] = x;
122 Cvc5Term a_plus_b_minus_x =
123 cvc5_mk_term(tm, CVC5_KIND_BITVECTOR_SUB, 2, args2);
124 args2[0] = new_x_;
125 args2[1] = a_plus_b_minus_x;
126 Cvc5Term assignment2 = cvc5_mk_term(tm, CVC5_KIND_EQUAL, 2, args2);
127
128 // Assert encoding to cvc5 in current context;
129 printf("Asserting %s to cvc5\n", cvc5_term_to_string(assignment2));
130 cvc5_assert_formula(slv, assignment2);
131
132 printf(" Check sat assuming: %s\n", cvc5_term_to_string(not_new_x_eq_new_x_));
133 printf(" Expect UNSAT.\n");
134 printf(" cvc5: %s\n",
135 cvc5_result_to_string(cvc5_check_sat_assuming(slv, 1, assumptions)));
136
137 args2[0] = x;
138 args2[1] = x;
139 Cvc5Term x_neq_x = cvc5_mk_term(tm, CVC5_KIND_DISTINCT, 2, args2);
140
141 args2[0] = new_x_eq_new_x_;
142 args2[1] = x_neq_x;
143 Cvc5Term query = cvc5_mk_term(tm, CVC5_KIND_AND, 2, args2);
144 args1[0] = query;
145 Cvc5Term not_query = cvc5_mk_term(tm, CVC5_KIND_NOT, 1, args1);
146 printf(" Check sat assuming: %s\n", cvc5_term_to_string(not_query));
147 printf(" Expect SAT.\n");
148 assumptions[0] = not_query;
149 printf(" cvc5: %s\n",
150 cvc5_result_to_string(cvc5_check_sat_assuming(slv, 1, assumptions)));
151
152 // Assert that a is odd
153 uint32_t idxs[2] = {0, 0};
154 Cvc5Op extract_op = cvc5_mk_op(tm, CVC5_KIND_BITVECTOR_EXTRACT, 2, idxs);
155 args1[0] = a;
156 Cvc5Term lsb_of_a = cvc5_mk_term_from_op(tm, extract_op, 1, args1);
157 printf("Sort of %s is %s\n",
158 cvc5_term_to_string(lsb_of_a),
159 cvc5_sort_to_string(cvc5_term_get_sort(lsb_of_a)));
160 args2[0] = lsb_of_a;
161 args2[1] = cvc5_mk_bv_uint64(tm, 1, 1);
162 Cvc5Term a_odd = cvc5_mk_term(tm, CVC5_KIND_EQUAL, 2, args2);
163 printf("Assert %s\n", cvc5_term_to_string(a_odd));
164 printf("Check satisfiability.\n");
165 cvc5_assert_formula(slv, a_odd);
166 printf(" Expect sat.\n");
167 printf(" cvc5: %s\n", cvc5_result_to_string(cvc5_check_sat(slv)));
168
169 cvc5_delete(slv);
170 cvc5_term_manager_delete(tm);
171 return 0;
172}
examples/api/java/BitVectors.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 BitVectors
19{
20 public static void main(String args[]) throws CVC5ApiException
21 {
22 TermManager tm = new TermManager();
23 Solver slv = new Solver(tm);
24 {
25 slv.setLogic("QF_BV"); // Set the logic
26
27 // The following example has been adapted from the book A Hacker's Delight by
28 // Henry S. Warren.
29 //
30 // Given a variable x that can only have two values, a or b. We want to
31 // assign to x a value other than the current one. The straightforward code
32 // to do that is:
33 //
34 //(0) if (x == a ) x = b;
35 // else x = a;
36 //
37 // Two more efficient yet equivalent methods are:
38 //
39 //(1) x = a ⊕ b ⊕ x;
40 //
41 //(2) x = a + b - x;
42 //
43 // We will use cvc5 to prove that the three pieces of code above are all
44 // equivalent by encoding the problem in the bit-vector theory.
45
46 // Creating a bit-vector type of width 32
47 Sort bitvector32 = tm.mkBitVectorSort(32);
48
49 // Variables
50 Term x = tm.mkConst(bitvector32, "x");
51 Term a = tm.mkConst(bitvector32, "a");
52 Term b = tm.mkConst(bitvector32, "b");
53
54 // First encode the assumption that x must be Kind.EQUAL to a or b
55 Term x_eq_a = tm.mkTerm(Kind.EQUAL, x, a);
56 Term x_eq_b = tm.mkTerm(Kind.EQUAL, x, b);
57 Term assumption = tm.mkTerm(Kind.OR, x_eq_a, x_eq_b);
58
59 // Assert the assumption
60 slv.assertFormula(assumption);
61
62 // Introduce a new variable for the new value of x after assignment.
63 Term new_x = tm.mkConst(bitvector32, "new_x"); // x after executing code (0)
64 Term new_x_ = tm.mkConst(bitvector32, "new_x_"); // x after executing code (1) or (2)
65
66 // Encoding code (0)
67 // new_x = x == a ? b : a;
68 Term ite = tm.mkTerm(Kind.ITE, x_eq_a, b, a);
69 Term assignment0 = tm.mkTerm(Kind.EQUAL, new_x, ite);
70
71 // Assert the encoding of code (0)
72 System.out.println("Asserting " + assignment0 + " to cvc5 ");
73 slv.assertFormula(assignment0);
74 System.out.println("Pushing a new context.");
75 slv.push();
76
77 // Encoding code (1)
78 // new_x_ = a xor b xor x
79 Term a_xor_b_xor_x = tm.mkTerm(Kind.BITVECTOR_XOR, a, b, x);
80 Term assignment1 = tm.mkTerm(Kind.EQUAL, new_x_, a_xor_b_xor_x);
81
82 // Assert encoding to cvc5 in current context;
83 System.out.println("Asserting " + assignment1 + " to cvc5 ");
84 slv.assertFormula(assignment1);
85 Term new_x_eq_new_x_ = tm.mkTerm(Kind.EQUAL, new_x, new_x_);
86
87 System.out.println(" Check sat assuming: " + new_x_eq_new_x_.notTerm());
88 System.out.println(" Expect UNSAT. ");
89 System.out.println(" cvc5: " + slv.checkSatAssuming(new_x_eq_new_x_.notTerm()));
90 System.out.println(" Popping context. ");
91 slv.pop();
92
93 // Encoding code (2)
94 // new_x_ = a + b - x
95 Term a_plus_b = tm.mkTerm(Kind.BITVECTOR_ADD, a, b);
96 Term a_plus_b_minus_x = tm.mkTerm(Kind.BITVECTOR_SUB, a_plus_b, x);
97 Term assignment2 = tm.mkTerm(Kind.EQUAL, new_x_, a_plus_b_minus_x);
98
99 // Assert encoding to cvc5 in current context;
100 System.out.println("Asserting " + assignment2 + " to cvc5 ");
101 slv.assertFormula(assignment2);
102
103 System.out.println(" Check sat assuming: " + new_x_eq_new_x_.notTerm());
104 System.out.println(" Expect UNSAT. ");
105 System.out.println(" cvc5: " + slv.checkSatAssuming(new_x_eq_new_x_.notTerm()));
106
107 Term x_neq_x = tm.mkTerm(Kind.EQUAL, x, x).notTerm();
108 Term[] v = new Term[] {new_x_eq_new_x_, x_neq_x};
109 Term query = tm.mkTerm(Kind.AND, v);
110 System.out.println(" Check sat assuming: " + query.notTerm());
111 System.out.println(" Expect SAT. ");
112 System.out.println(" cvc5: " + slv.checkSatAssuming(query.notTerm()));
113
114 // Assert that a is odd
115 Op extract_op = tm.mkOp(Kind.BITVECTOR_EXTRACT, 0, 0);
116 Term lsb_of_a = tm.mkTerm(extract_op, a);
117 System.out.println("Sort of " + lsb_of_a + " is " + lsb_of_a.getSort());
118 Term a_odd = tm.mkTerm(Kind.EQUAL, lsb_of_a, tm.mkBitVector(1, 1));
119 System.out.println("Assert " + a_odd);
120 System.out.println("Check satisfiability.");
121 slv.assertFormula(a_odd);
122 System.out.println(" Expect sat. ");
123 System.out.println(" cvc5: " + slv.checkSat());
124 }
125 Context.deletePointers();
126 }
127}
examples/api/python/pythonic/bitvectors.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 solver.
12##
13from cvc5.pythonic import *
14
15if __name__ == '__main__':
16 # The following example has been adapted from the book A Hacker's Delight
17 # by Henry S. Warren.
18 #
19 # Given a variable x that can only have two values, a or b. We want to
20 # assign to x a value other than the current one. The straightforward code
21 # to do that is:
22 #
23 # (0) if (x == a ) x = b;
24 # else x = a;
25 #
26 # Two more efficient yet equivalent methods are:
27 #
28 # (1) x = a xor b xor x;
29 #
30 # (2) x = a + b - x;
31 #
32 # We will use cvc5 to prove that the three pieces of code above are all
33 # equivalent by encoding the problem in the bit-vector theory.
34
35 x, a, b = BitVecs('x a b', 32)
36 x_is_a_or_b = Or(x == a, x == b)
37
38 # new_x0 set per (0)
39 new_x0 = If(x == a, b, a)
40 # new_x1 set per (1)
41 new_x1 = a ^ b ^ x
42 # new_x2 set per (2)
43 new_x2 = a + b - x
44
45 prove(Implies(x_is_a_or_b, new_x0 == new_x1))
46
47 prove(Implies(x_is_a_or_b, new_x0 == new_x2))
examples/api/python/bitvectors.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 bit-vector
12# solver through the Python API. This is a direct translation of
13# bitvectors-new.cpp.
14##
15
16import cvc5
17from cvc5 import Kind
18
19if __name__ == "__main__":
20 tm = cvc5.TermManager()
21 slv = cvc5.Solver(tm)
22 slv.setLogic("QF_BV") # Set the logic
23 # The following example has been adapted from the book A Hacker's Delight by
24 # Henry S. Warren.
25 #
26 # Given a variable x that can only have two values, a or b. We want to
27 # assign to x a value other than the current one. The straightforward code
28 # to do that is:
29 #
30 #(0) if (x == a ) x = b;
31 # else x = a;
32 #
33 # Two more efficient yet equivalent methods are:
34 #
35 #(1) x = a xor b xor x;
36 #
37 #(2) x = a + b - x;
38 #
39 # We will use cvc5 to prove that the three pieces of code above are all
40 # equivalent by encoding the problem in the bit-vector theory.
41
42 # Creating a bit-vector type of width 32
43 bitvector32 = tm.mkBitVectorSort(32)
44
45 # Variables
46 x = tm.mkConst(bitvector32, "x")
47 a = tm.mkConst(bitvector32, "a")
48 b = tm.mkConst(bitvector32, "b")
49
50 # First encode the assumption that x must be equal to a or b
51 x_eq_a = tm.mkTerm(Kind.EQUAL, x, a)
52 x_eq_b = tm.mkTerm(Kind.EQUAL, x, b)
53 assumption = tm.mkTerm(Kind.OR, x_eq_a, x_eq_b)
54
55 # Assert the assumption
56 slv.assertFormula(assumption)
57
58 # Introduce a new variable for the new value of x after assignment.
59 # x after executing code (0)
60 new_x = tm.mkConst(bitvector32, "new_x")
61 # x after executing code (1) or (2)
62 new_x_ = tm.mkConst(bitvector32, "new_x_")
63
64 # Encoding code (0)
65 # new_x = x == a ? b : a
66 ite = tm.mkTerm(Kind.ITE, x_eq_a, b, a)
67 assignment0 = tm.mkTerm(Kind.EQUAL, new_x, ite)
68
69 # Assert the encoding of code (0)
70 print("Asserting {} to cvc5".format(assignment0))
71 slv.assertFormula(assignment0)
72 print("Pushing a new context.")
73 slv.push()
74
75 # Encoding code (1)
76 # new_x_ = a xor b xor x
77 a_xor_b_xor_x = tm.mkTerm(Kind.BITVECTOR_XOR, a, b, x)
78 assignment1 = tm.mkTerm(Kind.EQUAL, new_x_, a_xor_b_xor_x)
79
80 # Assert encoding to cvc5 in current context
81 print("Asserting {} to cvc5".format(assignment1))
82 slv.assertFormula(assignment1)
83 new_x_eq_new_x_ = tm.mkTerm(Kind.EQUAL, new_x, new_x_)
84
85 print("Checking sat assuming:", new_x_eq_new_x_.notTerm())
86 print("Expect UNSAT.")
87 print("cvc5:", slv.checkSatAssuming(new_x_eq_new_x_.notTerm()))
88 print("Popping context.")
89 slv.pop()
90
91 # Encoding code (2)
92 # new_x_ = a + b - x
93 a_plus_b = tm.mkTerm(Kind.BITVECTOR_ADD, a, b)
94 a_plus_b_minus_x = tm.mkTerm(Kind.BITVECTOR_SUB, a_plus_b, x)
95 assignment2 = tm.mkTerm(Kind.EQUAL, new_x_, a_plus_b_minus_x)
96
97 # Assert encoding to cvc5 in current context
98 print("Asserting {} to cvc5".format(assignment2))
99 slv.assertFormula(assignment2)
100
101 print("Checking sat assuming:", new_x_eq_new_x_.notTerm())
102 print("Expect UNSAT.")
103 print("cvc5:", slv.checkSatAssuming(new_x_eq_new_x_.notTerm()))
104
105
106 x_neq_x = tm.mkTerm(Kind.EQUAL, x, x).notTerm()
107 query = tm.mkTerm(Kind.AND, new_x_eq_new_x_, x_neq_x)
108 print("Check sat assuming: ", query.notTerm())
109 print("Expect SAT.")
110 print("cvc5:", slv.checkSatAssuming(query.notTerm()))
111
112 # Assert that a is odd
113 extract_op = tm.mkOp(Kind.BITVECTOR_EXTRACT, 0, 0)
114 lsb_of_a = tm.mkTerm(extract_op, a)
115 print("Sort of {} is {}".format(lsb_of_a, lsb_of_a.getSort()))
116 a_odd = tm.mkTerm(Kind.EQUAL, lsb_of_a, tm.mkBitVector(1, 1))
117 print("Assert", a_odd)
118 print("Check satisifiability")
119 slv.assertFormula(a_odd)
120 print("Expect sat")
121 print("cvc5:", slv.checkSat())
examples/api/smtlib/bitvectors.smt2
1(set-logic QF_BV)
2(set-option :incremental true)
3
4(declare-const x (_ BitVec 32))
5(declare-const a (_ BitVec 32))
6(declare-const b (_ BitVec 32))
7(declare-const new_x (_ BitVec 32))
8(declare-const new_x_ (_ BitVec 32))
9
10(echo "Assert the assumption.")
11(assert (or (= x a) (= x b)))
12
13(echo "Asserting assignment to cvc5.")
14(assert (= new_x (ite (= x a) b a)))
15
16(echo "Pushing a new context.")
17(push 1)
18
19(echo "Asserting another assignment to cvc5.")
20(assert (= new_x_ (bvxor a b x)))
21
22(echo "Check entailment assuming new_x = new_x_.")
23(echo "UNSAT (of negation) == ENTAILED")
24(check-sat-assuming ((not (= new_x new_x_))))
25(echo "Popping context.")
26(pop 1)
27
28(echo "Asserting another assignment to cvc5.")
29(assert (= new_x_ (bvsub (bvadd a b) x)))
30
31(echo "Check entailment assuming new_x = new_x_.")
32(echo "UNSAT (of negation) == ENTAILED")
33(check-sat-assuming ((not (= new_x new_x_))))
34
35(echo "Check entailment assuming [new_x = new_x_, x != x.")
36(echo "UNSAT (of negation) == ENTAILED")
37(check-sat-assuming ((not (= new_x new_x_)) (not (= x x))))
38
39(echo "Assert that a is odd, i.e. lsb is one.")
40(assert (= ((_ extract 0 0) a) (_ bv1 1)))
41(echo "Check satisfiability, expecting sat.")
42(check-sat)