C API Documentation
The C API of Bitwuzla is implemented as a thin wrapper around its C++ API, Bitwuzla’s primary API. This section provides a quickstart guide to give an introduction on how to use the C API and a comprehensive set of examples to cover basic and common use cases. A comprehensive description of the interface is given here.
Quickstart
First, create a BitwuzlaTermManager
instance that allows us to
create sorts and terms later:
BitwuzlaTermManager *tm = bitwuzla_term_manager_new();
Then, create a BitwuzlaOptions
instance:
BitwuzlaOptions *options = bitwuzla_options_new();
This instance can be configured via bitwuzla_set_option()
.
For example, to enable model generation
(SMT-LIB: (set-option :produce-models true)
):
bitwuzla_set_option(options, BITWUZLA_OPT_PRODUCE_MODELS, 1);
Some options have modes, which can be configured via the string representation of their modes. For example, to enable CaDiCaL as back end SAT solver (this is for illustration purposes only, CaDiCaL is configured by default):
bitwuzla_set_option_mode(options, BITWUZLA_OPT_SAT_SOLVER, "cadical");
For more details on available options, see Options.
Then, create a Bitwuzla
solver instance with a term manager
and configured options (configuration options are now frozen and cannot be
changed for this instance):
Bitwuzla *bitwuzla = bitwuzla_new(tm, options);
Next, you will want to create some expressions via the term manager tm and assert formulas.
Note
Sorts and terms can be shared between multiple solver instances as long as these solvers use the same term manager.
For example, consider the following SMT-LIB input:
(set-logic QF_ABV)
(set-option :produce-models true)
(declare-const x (_ BitVec 8))
(declare-const y (_ BitVec 8))
(declare-fun f ((_ BitVec 8) (_ BitVec 4)) (_ BitVec 8))
(declare-const a (Array (_ BitVec 8) (_ BitVec 8)))
(assert
(distinct
((_ extract 3 0) (bvsdiv x (_ bv2 8)))
((_ extract 3 0) (bvashr y (_ bv1 8)))))
(assert (= (f x ((_ extract 6 3) x)) y))
(assert (= (select a x) y))
(check-sat)
(get-model)
(get-value (x y f a (bvmul x x)))
(exit)
This input is created and asserted as follows:
// Create bit-vector sorts of size 4 and 8.
BitwuzlaSort sortbv4 = bitwuzla_mk_bv_sort(tm, 4);
BitwuzlaSort sortbv8 = bitwuzla_mk_bv_sort(tm, 8);
// Create function sort.
BitwuzlaSort domain[2] = {sortbv8, sortbv4};
BitwuzlaSort sortfun = bitwuzla_mk_fun_sort(tm, 2, domain, sortbv8);
// Create array sort.
BitwuzlaSort sortarr = bitwuzla_mk_array_sort(tm, sortbv8, sortbv8);
// Create two bit-vector constants of that sort.
BitwuzlaTerm x = bitwuzla_mk_const(tm, sortbv8, "x");
BitwuzlaTerm y = bitwuzla_mk_const(tm, sortbv8, "y");
// Create fun const.
BitwuzlaTerm f = bitwuzla_mk_const(tm, sortfun, "f");
// Create array const.
BitwuzlaTerm a = bitwuzla_mk_const(tm, sortarr, "a");
// Create bit-vector values one and two of the same sort.
BitwuzlaTerm one = bitwuzla_mk_bv_one(tm, sortbv8);
// Alternatively, you can create bit-vector value one with:
// BitwuzlaTerm one = bitwuzla_mk_bv_value(tm, sortbv8, "1", 2);
// BitwuzlaTerm one = bitwuzla_mk_bv_value_uint64(tm, sortbv8, 1);
BitwuzlaTerm two = bitwuzla_mk_bv_value_uint64(tm, sortbv8, 2);
// (bvsdiv x (_ bv2 8))
BitwuzlaTerm sdiv = bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_SDIV, x, two);
// (bvashr y (_ bv1 8))
BitwuzlaTerm ashr = bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_ASHR, y, one);
// ((_ extract 3 0) (bvsdiv x (_ bv2 8)))
BitwuzlaTerm sdive =
bitwuzla_mk_term1_indexed2(tm, BITWUZLA_KIND_BV_EXTRACT, sdiv, 3, 0);
// ((_ extract 3 0) (bvashr x (_ bv1 8)))
BitwuzlaTerm ashre =
bitwuzla_mk_term1_indexed2(tm, BITWUZLA_KIND_BV_EXTRACT, ashr, 3, 0);
// (assert
// (distinct
// ((_ extract 3 0) (bvsdiv x (_ bv2 8)))
// ((_ extract 3 0) (bvashr y (_ bv1 8)))))
bitwuzla_assert(bitwuzla,
bitwuzla_mk_term2(tm, BITWUZLA_KIND_DISTINCT, sdive, ashre));
// (assert (= (f x ((_ extract 6 3) x)) y))
bitwuzla_assert(
bitwuzla,
bitwuzla_mk_term2(
tm,
BITWUZLA_KIND_EQUAL,
bitwuzla_mk_term3(tm,
BITWUZLA_KIND_APPLY,
f,
x,
bitwuzla_mk_term1_indexed2(
tm, BITWUZLA_KIND_BV_EXTRACT, x, 6, 3)),
y));
// (assert (= (select a x) y))
bitwuzla_assert(
bitwuzla,
bitwuzla_mk_term2(tm,
BITWUZLA_KIND_EQUAL,
bitwuzla_mk_term2(tm, BITWUZLA_KIND_ARRAY_SELECT, a, x),
y));
Alternatively, you can parse an input file in BTOR2 format
[NPWB18] or SMT-LIB v2 format [BFT17] by creating a parser via
bitwuzla_parser_new()
and then parsing the input file via
bitwuzla_parser_parse()
.
Note
The input parser creates a Bitwuzla
instance, which can be
configured via the BitwuzlaOptions
instance passed into the
parser. This Bitwuzla instance can be retrieved via
bitwuzla_parser_get_bitwuzla()
.
For example, to parse an example file examples/smt2/quickstart.smt2 in SMT-LIB format:
// First, create a term manager instance.
BitwuzlaTermManager* tm = bitwuzla_term_manager_new();
// Create a Bitwuzla options instance.
BitwuzlaOptions* options = bitwuzla_options_new();
// We will parse example file `smt2/quickstart.smt2`.
// Create parser instance.
const char* infile_name = "../smt2/quickstart.smt2";
BitwuzlaParser* parser =
bitwuzla_parser_new(tm, options, "smt2", 2, "<stdout>");
const char* error_msg;
printf("Expect: sat\n");
printf("Bitwuzla: ");
bitwuzla_parser_parse(parser, infile_name, false, true, &error_msg);
// We expect no error to occur.
assert(!error_msg);
Note
If the input is given in SMT-LIB format, commands like check-sat
or get-value
will be executed while parsing if argument parse_only
is passed into bitwuzla_parser_parse()
as true
.
After parsing from an input file, the parsed assertions can be retrieved
via bitwuzla_get_assertions()
:
// Now we retrieve the set of asserted formulas and print them.
size_t size;
const BitwuzlaTerm* assertions =
bitwuzla_get_assertions(bitwuzla_parser_get_bitwuzla(parser), &size);
printf("Assertions:\n");
printf("{\n");
for (size_t i = 0; i < size; ++i)
{
printf(" %s\n", bitwuzla_term_to_string(assertions[i]));
}
printf("}\n");
Alternatively, Bitwuzla also supports parsing from strings via
bitwuzla_parser_parse()
. The quickstart input above can be parsed
as one huge input string, or any its subsets of commands.
Bitwuzla also allows to add onto input parsed from a file.
For example, after parsing in examples/smt2/quickstart.smt2
, which is
satisfiable, we add an assertion (which now makes the input formula
unsatisfiable) via parsing from string as follows:
bitwuzla_parser_parse(
parser, "(assert (distinct (select a x) y))", true, false, &error_msg);
// We expect no error to occur.
assert(!error_msg);
Bitwuzla also supports parsing terms and sorts from strings via
bitwuzla_parser_parse_term()
and
bitwuzla_parser_parse_sort()
.
Note
Declarations like declare-const
are commands (not terms) in the
SMT-LIB language. Commands must be parsed in via
bitwuzla_parser_parse()
.
Function bitwuzla_parser_parse_term()
and
bitwuzla_parser_parse_sort()
only support parsing
SMT-LIB terms and sorts, respectively.
For example, to parse a bit-vector sort of size 16 from string and show
that it corresponds to the bit-vector sort of size 16 created via
bitwuzla_mk_bv_sort()
:
// Declare bit-vector sort of size 16.
BitwuzlaSort bv16 =
bitwuzla_parser_parse_sort(parser, "(_ BitVec 16)", &error_msg);
// We expect no error to occur.
assert(!error_msg);
// Create bit-vector sort of size 16 and show that it corresponds to
// its string representation '(_ BitVec16)'.
assert(bv16 == bitwuzla_mk_bv_sort(tm, 16));
Then, to declare Boolean constants c
and d
and a bit-vector
constant b
:
bitwuzla_parser_parse(parser,
"(declare-const c Bool)(declare-const d Bool)",
true,
false,
&error_msg);
// Declare bit-vector constant 'b'.
bitwuzla_parser_parse(
parser, "(declare-const b (_ BitVec 16))", true, false, &error_msg);
// We expect no error to occur.
assert(!error_msg);
These terms can be retrieved via bitwuzla_parser_parse_term()
:
// Retrieve term representing 'b'.
BitwuzlaTerm b = bitwuzla_parser_parse_term(parser, "b", &error_msg);
// We expect no error to occur.
assert(!error_msg);
// Retrieve term representing 'c'.
BitwuzlaTerm c = bitwuzla_parser_parse_term(parser, "c", &error_msg);
// We expect no error to occur.
assert(!error_msg);
// Retrieve term representing 'd'.
BitwuzlaTerm d = bitwuzla_parser_parse_term(parser, "d", &error_msg);
// We expect no error to occur.
assert(!error_msg);
Now, to parse in terms using these constants via
bitwuzla_parser_parse_term()
:
// Create xor over 'a' and 'c' and show that it corresponds to term
// parsed in from its string representation '(xor c d)'.
assert(bitwuzla_parser_parse_term(parser, "(xor c d)", &error_msg)
== bitwuzla_mk_term2(tm, BITWUZLA_KIND_XOR, c, d));
// Create bit-vector addition over 'b' and bit-vector value
// '1011111010001010' and show that it corresponds to the term parsed in
// from its string representation '(bvadd b #b1011111010001010)'.
assert(bitwuzla_parser_parse_term(
parser, "(bvadd b #b1011111010001010)", &error_msg)
== bitwuzla_mk_term2(
tm,
BITWUZLA_KIND_BV_ADD,
b,
After parsing input and asserting formulas, satisfiability can be
determined via bitwuzla_check_sat()
.
BitwuzlaResult result = bitwuzla_check_sat(bitwuzla);
Formulas can also be assumed via bitwuzla_check_sat_assuming()
.
If the formula is satisfiable and model generation has been enabled, the
resulting model can be printed via bitwuzla_get_value()
and
bitwuzla_term_to_string()
. An example implementation illustrating
how to print the current model via declared symbols (in this case x
,
y
, f
and a
) is below:
printf("Model:\n");
BitwuzlaTerm decls[4] = {x, y, f, a};
printf("(\n");
for (uint32_t i = 0; i < 4; ++i)
{
BitwuzlaSort sort = bitwuzla_term_get_sort(decls[i]);
printf(" (define-fun %s (", bitwuzla_term_get_symbol(decls[i]));
if (bitwuzla_sort_is_fun(sort))
{
BitwuzlaTerm value = bitwuzla_get_value(bitwuzla, decls[i]);
size_t size;
BitwuzlaTerm *children = bitwuzla_term_get_children(value, &size);
assert(size == 2);
while (bitwuzla_term_get_kind(children[1]) == BITWUZLA_KIND_LAMBDA)
{
assert(bitwuzla_term_is_var(children[0]));
printf("(%s %s) ",
bitwuzla_term_to_string(children[0]),
bitwuzla_sort_to_string(bitwuzla_term_get_sort(children[0])));
value = children[1];
children = bitwuzla_term_get_children(value, &size);
}
assert(bitwuzla_term_is_var(children[0]));
// Note: The returned string of bitwuzla_term_to_string and
// bitwuzla_sort_to_string does not have to be freed, but is only
// valid until the next call to the respective function. Thus we
// split printing into separate printf calls so that none of these
// functions is called more than once in one printf call.
// Alternatively, we could also first get and copy the strings, use
// a single printf call, and then free the copied strings.
printf("(%s %s))",
bitwuzla_term_to_string(children[0]),
bitwuzla_sort_to_string(bitwuzla_term_get_sort(children[0])));
printf(" %s",
bitwuzla_sort_to_string(bitwuzla_sort_fun_get_codomain(sort)));
printf(" %s)\n", bitwuzla_term_to_string(children[1]));
}
else
{
printf(") %s %s)\n",
bitwuzla_sort_to_string(sort),
bitwuzla_term_to_string(bitwuzla_get_value(bitwuzla, decls[i])));
}
}
printf(")\n");
printf("\n");
This will output a possible model, in this case:
(
(define-fun x () (_ BitVec 8) #b10011111)
(define-fun y () (_ BitVec 8) #b11111111)
(define-fun f ((@bzla.var_74 (_ BitVec 8)) (@bzla.var_75 (_ BitVec 4))) (_ BitVec 8) (ite (and (= @bzla.var_74 #b10011111) (= @bzla.var_75 #b0011)) #b11111111 #b00000000))
(define-fun a () (Array (_ BitVec 8) (_ BitVec 8)) (store ((as const (Array (_ BitVec 8) (_ BitVec 8))) #b00000000) #b10011111 #b11111111))
)
Alternatively, it is possible to query the value of terms as assignment
string via bitwuzla_term_value_get_str()
, or as a term via
bitwuzla_get_value()
.
Additionally, for floating-point values,
bitwuzla_term_value_get_fp_ieee()
allows to retrieve the assignment
split into assignment strings for the sign bit, the exponent and the
significand.
For Boolean and RoundingMode
values,
bitwuzla_term_value_get_bool()
and
bitwuzla_term_value_get_rm()
allow the values as bool
and
BitwuzlaRoundingMode
, respectively.
In our case, we can query the assignments of x
and y
, both
bit-vector terms, as binary strings:
// Both x and y are bit-vector terms and their value is a bit-vector
// value that can be printed via bitwuzla_term_value_get_str().
printf("value of x: %s\n",
bitwuzla_term_value_get_str(bitwuzla_get_value(bitwuzla, x)));
printf("value of y: %s\n",
bitwuzla_term_value_get_str(bitwuzla_get_value(bitwuzla, y)));
printf("\n");
This will print:
value of x: 10011111
value of y: 11111111
The value of f
(a function term) and a
(an array term), on the
other hand, cannot be represented with a simple type. Thus, function values are
given as BITWUZLA_KIND_LAMBDA
, and array
values are given as
BITWUZLA_KIND_ARRAY_STORE
.
We can retrieve an SMT-LIB2 string representation of the values via
bitwuzla_term_to_string()
:
// f and a, on the other hand, are a function and array term, respectively.
// The value of these terms is not a value term: for f, it is a lambda term,
// and the value of a is represented as a store term. Thus we cannot use
// bitwuzla_term_value_get_str(), but we can print the value of the terms
// via bitwuzla_term_to_string().
printf("to_string representation of value of f:\n%s\n\n",
bitwuzla_term_to_string(bitwuzla_get_value(bitwuzla, f)));
printf("to_string representation of value of a:\n%s\n",
bitwuzla_term_to_string(bitwuzla_get_value(bitwuzla, a)));
printf("\n");
This will print:
to_string representation of value of f:
(lambda ((@bzla.var_74 (_ BitVec 8))) (lambda ((@bzla.var_75 (_ BitVec 4))) (ite (and (= @bzla.var_74 #b10011111) (= @bzla.var_75 #b0011)) #b11111111 #b00000000)))
to_string representation of value of a:
(store ((as const (Array (_ BitVec 8) (_ BitVec 8))) #b00000000) #b10011111 #b11111111)
Note that the string representation of values representable as simple type
(bit-vectors, boolean, floating-point, rounding mode) are given as pure
value string (in the given number format) via
bitwuzla_term_value_get_str()
.
Their string representation retrieved via bitwuzla_term_to_string()
,
however, is given in SMT-LIB2 format. For example,
printf("to_string representation of value of x: %s\n",
bitwuzla_term_to_string(bitwuzla_get_value(bitwuzla, x)));
printf("to_string representation of value of y: %s\n",
bitwuzla_term_to_string(bitwuzla_get_value(bitwuzla, y)));
printf("\n");
This will print:
to_string representation of value of x: #b10011111
to_string representation of value of y: #b11111111
It is also possible to query the model value of expressions that do not occur in the input formula:
BitwuzlaTerm v = bitwuzla_get_value(
bitwuzla, bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_MUL, x, x));
printf("value of v = x * x: %s\n",
bitwuzla_term_value_get_str(bitwuzla_get_value(bitwuzla, v)));
This will print:
value of v = x * x: 11000001
Finally, we delete the Bitwuzla, BitwuzlaOptions, and BitwuzlaTermManager instances.
bitwuzla_delete(bitwuzla);
bitwuzla_options_delete(options);
bitwuzla_term_manager_delete(tm);
Note
Make sure to delete the term manager last since the solver instance relies on
it.
Further, when the term manager is deleted, it will automatically release all
created sorts and terms. For a more fine-grained control on when sorts and
terms are released you can use
bitwuzla_sort_copy()
,
bitwuzla_sort_release()
,
bitwuzla_term_copy()
,
bitwuzla_term_release()
, and
bitwuzla_term_manager_release()
.
Examples
Quickstart Example
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2021 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <assert.h>
12#include <bitwuzla/c/bitwuzla.h>
13#include <stdio.h>
14
15int
16main()
17{
18 // First, create a term manager instance.
19 //! [docs-c-quickstart-0 start]
20 BitwuzlaTermManager *tm = bitwuzla_term_manager_new();
21 //! [docs-c-quickstart-0 end]
22 // Create a Bitwuzla options instance.
23 //! [docs-c-quickstart-1 start]
24 BitwuzlaOptions *options = bitwuzla_options_new();
25 //! [docs-c-quickstart-1 end]
26 // Then, enable model generation.
27 //! [docs-c-quickstart-2 start]
28 bitwuzla_set_option(options, BITWUZLA_OPT_PRODUCE_MODELS, 1);
29 //! [docs-c-quickstart-2 end]
30 // Now, for illustration purposes, we enable CaDiCaL as SAT solver
31 // (CaDiCaL is already configured by default).
32 // Note: This will silently fall back to one of the compiled in SAT solvers
33 // if the selected solver is not compiled in.
34 //! [docs-c-quickstart-3 start]
35 bitwuzla_set_option_mode(options, BITWUZLA_OPT_SAT_SOLVER, "cadical");
36 //! [docs-c-quickstart-3 end]
37 // Then, create a Bitwuzla instance.
38 //! [docs-c-quickstart-4 start]
39 Bitwuzla *bitwuzla = bitwuzla_new(tm, options);
40 //! [docs-c-quickstart-4 end]
41
42 //! [docs-c-quickstart-5 start]
43 // Create bit-vector sorts of size 4 and 8.
44 BitwuzlaSort sortbv4 = bitwuzla_mk_bv_sort(tm, 4);
45 BitwuzlaSort sortbv8 = bitwuzla_mk_bv_sort(tm, 8);
46 // Create function sort.
47 BitwuzlaSort domain[2] = {sortbv8, sortbv4};
48 BitwuzlaSort sortfun = bitwuzla_mk_fun_sort(tm, 2, domain, sortbv8);
49 // Create array sort.
50 BitwuzlaSort sortarr = bitwuzla_mk_array_sort(tm, sortbv8, sortbv8);
51
52 // Create two bit-vector constants of that sort.
53 BitwuzlaTerm x = bitwuzla_mk_const(tm, sortbv8, "x");
54 BitwuzlaTerm y = bitwuzla_mk_const(tm, sortbv8, "y");
55 // Create fun const.
56 BitwuzlaTerm f = bitwuzla_mk_const(tm, sortfun, "f");
57 // Create array const.
58 BitwuzlaTerm a = bitwuzla_mk_const(tm, sortarr, "a");
59 // Create bit-vector values one and two of the same sort.
60 BitwuzlaTerm one = bitwuzla_mk_bv_one(tm, sortbv8);
61 // Alternatively, you can create bit-vector value one with:
62 // BitwuzlaTerm one = bitwuzla_mk_bv_value(tm, sortbv8, "1", 2);
63 // BitwuzlaTerm one = bitwuzla_mk_bv_value_uint64(tm, sortbv8, 1);
64 BitwuzlaTerm two = bitwuzla_mk_bv_value_uint64(tm, sortbv8, 2);
65
66 // (bvsdiv x (_ bv2 8))
67 BitwuzlaTerm sdiv = bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_SDIV, x, two);
68 // (bvashr y (_ bv1 8))
69 BitwuzlaTerm ashr = bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_ASHR, y, one);
70 // ((_ extract 3 0) (bvsdiv x (_ bv2 8)))
71 BitwuzlaTerm sdive =
72 bitwuzla_mk_term1_indexed2(tm, BITWUZLA_KIND_BV_EXTRACT, sdiv, 3, 0);
73 // ((_ extract 3 0) (bvashr x (_ bv1 8)))
74 BitwuzlaTerm ashre =
75 bitwuzla_mk_term1_indexed2(tm, BITWUZLA_KIND_BV_EXTRACT, ashr, 3, 0);
76
77 // (assert
78 // (distinct
79 // ((_ extract 3 0) (bvsdiv x (_ bv2 8)))
80 // ((_ extract 3 0) (bvashr y (_ bv1 8)))))
81 bitwuzla_assert(bitwuzla,
82 bitwuzla_mk_term2(tm, BITWUZLA_KIND_DISTINCT, sdive, ashre));
83 // (assert (= (f x ((_ extract 6 3) x)) y))
84 bitwuzla_assert(
85 bitwuzla,
86 bitwuzla_mk_term2(
87 tm,
88 BITWUZLA_KIND_EQUAL,
89 bitwuzla_mk_term3(tm,
90 BITWUZLA_KIND_APPLY,
91 f,
92 x,
93 bitwuzla_mk_term1_indexed2(
94 tm, BITWUZLA_KIND_BV_EXTRACT, x, 6, 3)),
95 y));
96 // (assert (= (select a x) y))
97 bitwuzla_assert(
98 bitwuzla,
99 bitwuzla_mk_term2(tm,
100 BITWUZLA_KIND_EQUAL,
101 bitwuzla_mk_term2(tm, BITWUZLA_KIND_ARRAY_SELECT, a, x),
102 y));
103 //! [docs-c-quickstart-5 end]
104
105 // (check-sat)
106 //! [docs-c-quickstart-6 start]
107 BitwuzlaResult result = bitwuzla_check_sat(bitwuzla);
108 //! [docs-c-quickstart-6 end]
109
110 printf("Expect: sat\n");
111 printf("Bitwuzla: %s\n\n", bitwuzla_result_to_string(result));
112
113 // Print model in SMT-LIBv2 format.
114 //! [docs-c-quickstart-7 start]
115 printf("Model:\n");
116 BitwuzlaTerm decls[4] = {x, y, f, a};
117 printf("(\n");
118 for (uint32_t i = 0; i < 4; ++i)
119 {
120 BitwuzlaSort sort = bitwuzla_term_get_sort(decls[i]);
121 printf(" (define-fun %s (", bitwuzla_term_get_symbol(decls[i]));
122 if (bitwuzla_sort_is_fun(sort))
123 {
124 BitwuzlaTerm value = bitwuzla_get_value(bitwuzla, decls[i]);
125 size_t size;
126 BitwuzlaTerm *children = bitwuzla_term_get_children(value, &size);
127 assert(size == 2);
128 while (bitwuzla_term_get_kind(children[1]) == BITWUZLA_KIND_LAMBDA)
129 {
130 assert(bitwuzla_term_is_var(children[0]));
131 printf("(%s %s) ",
132 bitwuzla_term_to_string(children[0]),
133 bitwuzla_sort_to_string(bitwuzla_term_get_sort(children[0])));
134 value = children[1];
135 children = bitwuzla_term_get_children(value, &size);
136 }
137 assert(bitwuzla_term_is_var(children[0]));
138 // Note: The returned string of bitwuzla_term_to_string and
139 // bitwuzla_sort_to_string does not have to be freed, but is only
140 // valid until the next call to the respective function. Thus we
141 // split printing into separate printf calls so that none of these
142 // functions is called more than once in one printf call.
143 // Alternatively, we could also first get and copy the strings, use
144 // a single printf call, and then free the copied strings.
145 printf("(%s %s))",
146 bitwuzla_term_to_string(children[0]),
147 bitwuzla_sort_to_string(bitwuzla_term_get_sort(children[0])));
148 printf(" %s",
149 bitwuzla_sort_to_string(bitwuzla_sort_fun_get_codomain(sort)));
150 printf(" %s)\n", bitwuzla_term_to_string(children[1]));
151 }
152 else
153 {
154 printf(") %s %s)\n",
155 bitwuzla_sort_to_string(sort),
156 bitwuzla_term_to_string(bitwuzla_get_value(bitwuzla, decls[i])));
157 }
158 }
159 printf(")\n");
160 printf("\n");
161 //! [docs-c-quickstart-7 end]
162
163 // Print value for x, y, f and a.
164 // Note: The returned string of bitwuzla_term_value_get_str is only valid
165 // until the next call to bitwuzla_term_value_get_str
166 //! [docs-c-quickstart-8 start]
167 // Both x and y are bit-vector terms and their value is a bit-vector
168 // value that can be printed via bitwuzla_term_value_get_str().
169 printf("value of x: %s\n",
170 bitwuzla_term_value_get_str(bitwuzla_get_value(bitwuzla, x)));
171 printf("value of y: %s\n",
172 bitwuzla_term_value_get_str(bitwuzla_get_value(bitwuzla, y)));
173 printf("\n");
174 //! [docs-c-quickstart-8 end]
175 //! [docs-c-quickstart-9 start]
176 // f and a, on the other hand, are a function and array term, respectively.
177 // The value of these terms is not a value term: for f, it is a lambda term,
178 // and the value of a is represented as a store term. Thus we cannot use
179 // bitwuzla_term_value_get_str(), but we can print the value of the terms
180 // via bitwuzla_term_to_string().
181 printf("to_string representation of value of f:\n%s\n\n",
182 bitwuzla_term_to_string(bitwuzla_get_value(bitwuzla, f)));
183 printf("to_string representation of value of a:\n%s\n",
184 bitwuzla_term_to_string(bitwuzla_get_value(bitwuzla, a)));
185 printf("\n");
186 //! [docs-c-quickstart-9 end]
187 // Note that the assignment string of bit-vector terms is given as the
188 // pure assignment string, either in binary, hexadecimal or decimal format,
189 // whereas bitwuzla_term_to_string() prints the value in SMT-LIB2 format
190 // (in binary number format).
191 //! [docs-c-quickstart-10 start]
192 printf("to_string representation of value of x: %s\n",
193 bitwuzla_term_to_string(bitwuzla_get_value(bitwuzla, x)));
194 printf("to_string representation of value of y: %s\n",
195 bitwuzla_term_to_string(bitwuzla_get_value(bitwuzla, y)));
196 printf("\n");
197 //! [docs-c-quickstart-10 end]
198
199 // Query value of bit-vector term that does not occur in the input formula
200 //! [docs-c-quickstart-11 start]
201 BitwuzlaTerm v = bitwuzla_get_value(
202 bitwuzla, bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_MUL, x, x));
203 printf("value of v = x * x: %s\n",
204 bitwuzla_term_value_get_str(bitwuzla_get_value(bitwuzla, v)));
205 //! [docs-c-quickstart-11 end]
206
207 // Finally, delete the Bitwuzla solver, options, and term manager instances.
208 //! [docs-c-quickstart-12 start]
209 bitwuzla_delete(bitwuzla);
210 bitwuzla_options_delete(options);
211 bitwuzla_term_manager_delete(tm);
212 //! [docs-c-quickstart-12 end]
213
214 return 0;
215}
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <assert.h>
12#include <bitwuzla/cpp/bitwuzla.h>
13
14#include <iostream>
15
16using namespace bitwuzla;
17
18int
19main()
20{
21 // First, create a term manager instance.
22 //! [docs-cpp-quickstart-0 start]
23 TermManager tm;
24 //! [docs-cpp-quickstart-0 end]
25 // Create a Bitwuzla options instance.
26 //! [docs-cpp-quickstart-1 start]
27 Options options;
28 //! [docs-cpp-quickstart-1 end]
29 // Then, enable model generation.
30 //! [docs-cpp-quickstart-2 start]
31 options.set(Option::PRODUCE_MODELS, true);
32 //! [docs-cpp-quickstart-2 end]
33 // Now, for illustration purposes, we enable CaDiCaL as SAT solver
34 // (CaDiCaL is already configured by default).
35 // Note: This will silently fall back to one of the compiled in SAT solvers
36 // if the selected solver is not compiled in.
37 //! [docs-cpp-quickstart-3 start]
38 options.set(Option::SAT_SOLVER, "cadical");
39 //! [docs-cpp-quickstart-3 end]
40 // Then, create a Bitwuzla instance.
41 //! [docs-cpp-quickstart-4 start]
42 Bitwuzla bitwuzla(tm, options);
43 //! [docs-cpp-quickstart-4 end]
44
45 //! [docs-cpp-quickstart-5 start]
46 // Create bit-vector sorts of size 4 and 8.
47 Sort sortbv4 = tm.mk_bv_sort(4);
48 Sort sortbv8 = tm.mk_bv_sort(8);
49 // Create function sort.
50 Sort sortfun = tm.mk_fun_sort({sortbv8, sortbv4}, sortbv8);
51 // Create array sort.
52 Sort sortarr = tm.mk_array_sort(sortbv8, sortbv8);
53
54 // Create two bit-vector constants of that sort.
55 Term x = tm.mk_const(sortbv8, "x");
56 Term y = tm.mk_const(sortbv8, "y");
57 // Create fun const.
58 Term f = tm.mk_const(sortfun, "f");
59 // Create array const.
60 Term a = tm.mk_const(sortarr, "a");
61 // Create bit-vector values one and two of the same sort.
62 Term one = tm.mk_bv_one(sortbv8);
63 // Alternatively, you can create bit-vector value one with:
64 // Term one = tm.mk_bv_value(sortbv8, "1", 2);
65 // Term one = tm.mk_bv_value_uint64(sortbv8, 1);
66 Term two = tm.mk_bv_value_uint64(sortbv8, 2);
67
68 // (bvsdiv x (_ bv2 8))
69 Term sdiv = tm.mk_term(Kind::BV_SDIV, {x, two});
70 // (bvashr y (_ bv1 8))
71 Term ashr = tm.mk_term(Kind::BV_ASHR, {y, one});
72 // ((_ extract 3 0) (bvsdiv x (_ bv2 8)))
73 Term sdive = tm.mk_term(Kind::BV_EXTRACT, {sdiv}, {3, 0});
74 // ((_ extract 3 0) (bvashr x (_ bv1 8)))
75 Term ashre = tm.mk_term(Kind::BV_EXTRACT, {ashr}, {3, 0});
76
77 // (assert
78 // (distinct
79 // ((_ extract 3 0) (bvsdiv x (_ bv2 8)))
80 // ((_ extract 3 0) (bvashr y (_ bv1 8)))))
81 bitwuzla.assert_formula(tm.mk_term(Kind::DISTINCT, {sdive, ashre}));
82 // (assert (= (f x ((_ extract 6 3) x)) y))
83 bitwuzla.assert_formula(
84 tm.mk_term(Kind::EQUAL,
85 {tm.mk_term(Kind::APPLY,
86 {f, x, tm.mk_term(Kind::BV_EXTRACT, {x}, {6, 3})}),
87 y}));
88 // (assert (= (select a x) y))
89 bitwuzla.assert_formula(
90 tm.mk_term(Kind::EQUAL, {tm.mk_term(Kind::ARRAY_SELECT, {a, x}), y}));
91 //! [docs-cpp-quickstart-5 end]
92
93 // (check-sat)
94 //! [docs-cpp-quickstart-6 start]
95 Result result = bitwuzla.check_sat();
96 //! [docs-cpp-quickstart-6 end]
97
98 std::cout << "Expect: sat" << std::endl;
99 std::cout << "Bitwuzla: " << result << std::endl;
100
101 // Print model in SMT-LIBv2 format.
102 //! [docs-cpp-quickstart-7 start]
103 std::cout << "Model:" << std::endl << "(" << std::endl;
104 for (const auto& term : {x, y, f, a})
105 {
106 Sort sort = term.sort();
107 std::cout << " (define-fun "
108 << (term.symbol() ? *term.symbol()
109 : "@t" + std::to_string(term.id()))
110 << " (";
111 if (sort.is_fun())
112 {
113 bitwuzla::Term value = bitwuzla.get_value(term);
114 assert(value.kind() == bitwuzla::Kind::LAMBDA);
115 assert(value.num_children() == 2);
116 while (value[1].kind() == bitwuzla::Kind::LAMBDA)
117 {
118 assert(value[0].is_variable());
119 std::cout << "(" << value[0] << " " << value[0].sort() << ") ";
120 value = value[1];
121 }
122 assert(value[0].is_variable());
123 std::cout << "(" << value[0] << " " << value[0].sort() << ")) "
124 << sort.fun_codomain() << " ";
125 std::cout << value[1] << ")" << std::endl;
126 }
127 else
128 {
129 std::cout << ") " << sort << " " << bitwuzla.get_value(term) << ")"
130 << std::endl;
131 }
132 }
133 std::cout << ")" << std::endl << std::endl;
134 //! [docs-cpp-quickstart-7 end]
135
136 // Print value for x, y, f and a.
137 // Note: The returned string of Term::value() is only valid until the next
138 // call to Term::value().
139 //! [docs-cpp-quickstart-8 start]
140 // Both x and y are bit-vector terms and their value is a bit-vector
141 // value that can be printed via Term::value().
142 std::cout << "value of x: " << bitwuzla.get_value(x).value<std::string>(2)
143 << std::endl;
144 std::cout << "value of y: " << bitwuzla.get_value(y).value<std::string>(2)
145 << std::endl;
146 std::cout << std::endl;
147 //! [docs-cpp-quickstart-8 end]
148 //! [docs-cpp-quickstart-9 start]
149 // f and a, on the other hand, are a function and array term, respectively.
150 // The value of these terms is not a value term: for f, it is a lambda term,
151 // and the value of a is represented as a store term. Thus we cannot use
152 // Term::value(), but we can print the value of the terms via Term::str()
153 // or operator<<.
154 std::cout << "str() representation of value of f:" << std::endl
155 << bitwuzla.get_value(f) << std::endl
156 << std::endl;
157 std::cout << "str() representation of value of a:" << std::endl
158 << bitwuzla.get_value(a) << std::endl
159 << std::endl;
160 std::cout << std::endl;
161 //! [docs-cpp-quickstart-9 end]
162 // Note that the assignment string of bit-vector terms is given as the
163 // pure assignment string, either in binary, hexadecimal or decimal format,
164 // whereas Term::str() and operator<< print the value in SMT-LIB2 format
165 // ((in the configured bit-vector output number format, binary by default).
166 //! [docs-cpp-quickstart-10 start]
167 std::cout << "str() representation of value of x: " << bitwuzla.get_value(x)
168 << std::endl;
169 std::cout << "str() representation of value of y: " << bitwuzla.get_value(y)
170 << std::endl;
171 //! [docs-cpp-quickstart-10 end]
172 std::cout << std::endl;
173
174 // Query value of bit-vector term that does not occur in the input formula
175 //! [docs-cpp-quickstart-11 start]
176 Term v = bitwuzla.get_value(tm.mk_term(Kind::BV_MUL, {x, x}));
177 std::cout << "value of v = x * x: " << v.value<std::string>(2) << std::endl;
178 //! [docs-cpp-quickstart-11 end]
179
180 return 0;
181}
1#
2# Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3#
4# Copyright (C) 2023 by the authors listed in the AUTHORS file at
5# https:#github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6#
7# This file is part of Bitwuzla under the MIT license. See COPYING for more
8# information at https:#github.com/bitwuzla/bitwuzla/blob/main/COPYING
9##
10
11from bitwuzla import *
12
13if __name__ == "__main__":
14
15 # First, create a term manager instance.
16 tm = TermManager()
17 # Create a Bitwuzla options instance.
18 options = Options()
19 # Then, enable model generation.
20 options.set(Option.PRODUCE_MODELS, True)
21 # Now, for illustration purposes, we enable CaDiCaL as SAT solver
22 # (CaDiCaL is already configured by default).
23 # Note: This will silently fall back to one of the compiled in SAT solvers
24 # if the selected solver is not compiled in.
25 options.set(Option.SAT_SOLVER, 'cadical')
26 # Then, create a Bitwuzla instance.
27 bitwuzla = Bitwuzla(tm, options)
28
29 # Create bit-vector sorts of size 4 and 8.
30 sortbv4 = tm.mk_bv_sort(4)
31 sortbv8 = tm.mk_bv_sort(8)
32 # Create function sort.
33 sortfun = tm.mk_fun_sort([sortbv8, sortbv4], sortbv8)
34 # Create array sort.
35 sortarr = tm.mk_array_sort(sortbv8, sortbv8)
36
37 # Create two bit-vector constants of that sort.
38 x = tm.mk_const(sortbv8, "x")
39 y = tm.mk_const(sortbv8, "y")
40 # Create fun const.
41 f = tm.mk_const(sortfun, "f")
42 # Create array const.
43 a = tm.mk_const(sortarr, "a")
44 # Create bit-vector values one and two of the same sort.
45 one = tm.mk_bv_one(sortbv8)
46 # Alternatively, you can create bit-vector value one with:
47 # one = tm.mk_bv_value(sortbv8, "1", 2)
48 # one = tm.mk_bv_value(sortbv8, 1)
49 two = tm.mk_bv_value(sortbv8, 2)
50
51 # (bvsdiv x (_ bv2 8))
52 sdiv = tm.mk_term(Kind.BV_SDIV, [x, two])
53 # (bvashr y (_ bv1 8))
54 ashr = tm.mk_term(Kind.BV_ASHR, [y, one])
55 # ((_ extract 3 0) (bvsdiv x (_ bv2 8)))
56 sdive = tm.mk_term(Kind.BV_EXTRACT, [sdiv], [3, 0])
57 # ((_ extract 3 0) (bvashr x (_ bv1 8)))
58 ashre = tm.mk_term(Kind.BV_EXTRACT, [ashr], [3, 0])
59
60 # (assert
61 # (distinct
62 # ((_ extract 3 0) (bvsdiv x (_ bv2 8)))
63 # ((_ extract 3 0) (bvashr y (_ bv1 8)))))
64 bitwuzla.assert_formula(tm.mk_term(Kind.DISTINCT, [sdive, ashre]))
65 # (assert (= (f x ((_ extract 6 3) x)) y))
66 bitwuzla.assert_formula(tm.mk_term(
67 Kind.EQUAL,
68 [tm.mk_term(Kind.APPLY, [f, x, tm.mk_term(Kind.BV_EXTRACT, [x], [6, 3])]),
69 y]))
70 # (assert (= (select a x) y))
71 bitwuzla.assert_formula(
72 tm.mk_term(Kind.EQUAL, [tm.mk_term(Kind.ARRAY_SELECT, [a, x]), y]))
73
74 # (check-sat)
75 result = bitwuzla.check_sat()
76 print('Expect: sat')
77 print(f'Bitwuzla: {result}')
78
79 # Print model in SMT-LIBv2 format.
80 print('Model:\n(')
81 for term in [x, y, f, a]:
82 sort = term.sort()
83 symbol = term.symbol()
84 print(f' (define-fun {symbol if symbol else "@t" + term.id()} (',
85 end = '')
86 if sort.is_fun():
87 value = bitwuzla.get_value(term)
88 assert value.kind() == Kind.LAMBDA
89 assert value.num_children() == 2
90 while value[1].kind() == Kind.LAMBDA:
91 assert value[0].is_variable()
92 print(f'({value[0]} {value[0].sort()}) ',
93 end = '')
94 value = value[1]
95 assert value[0].is_variable()
96 print(f'({value[0]} {value[0].sort()})) ' \
97 + f'{sort.fun_codomain()} {value[1]})')
98 else:
99 print(f') {sort} {bitwuzla.get_value(term)})')
100 print(')')
101 print()
102
103 # Print value for x, y, f and a.
104 # Both x and y are bit-vector terms and their value is a bit-vector
105 # value that can be printed via Term.value().
106 print(f'value of x: {bitwuzla.get_value(x).value(2)}')
107 print(f'value of y: {bitwuzla.get_value(y).value(2)}')
108 print()
109 # f and a, on the other hand, are a function and array term, respectively.
110 # The value of these terms is not a value term: for f, it is a lambda term,
111 # and the value of a is represented as a store term. Thus we cannot use
112 # Term.value(), but we can print the value of the terms via Term.str().
113 print('str() representation of value of f:')
114 print(bitwuzla.get_value(f))
115 print()
116 print('str() representation of value of a:')
117 print(bitwuzla.get_value(a))
118 print()
119 # Note that the assignment string of bit-vector terms is given as the
120 # pure assignment string, either in binary, hexadecimal or decimal format,
121 # whereas Term.str() prints the value in SMT-LIB2 format (in the given
122 # bit-vector output number format, binary by default).
123 print(f'str() representation of value of x: {bitwuzla.get_value(x)}')
124 print(f'str() representation of value of y: {bitwuzla.get_value(y)}')
125 print()
126
127 # Query value of bit-vector term that does not occur in the input formula
128 v = bitwuzla.get_value(tm.mk_term(Kind.BV_MUL, [x, x]))
129 print(f'value of v = x * x: {v.value(2)}')
1(set-logic QF_ABV)
2(set-option :produce-models true)
3(declare-const x (_ BitVec 8))
4(declare-const y (_ BitVec 8))
5(declare-fun f ((_ BitVec 8) (_ BitVec 4)) (_ BitVec 8))
6(declare-const a (Array (_ BitVec 8) (_ BitVec 8)))
7(assert
8 (distinct
9 ((_ extract 3 0) (bvsdiv x (_ bv2 8)))
10 ((_ extract 3 0) (bvashr y (_ bv1 8)))))
11(assert (= (f x ((_ extract 6 3) x)) y))
12(assert (= (select a x) y))
13(check-sat)
14(get-model)
15(get-value (x y f a (bvmul x x)))
16(exit)
Options Example
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2021 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/c/bitwuzla.h>
12#include <inttypes.h>
13#include <stdio.h>
14
15int
16main()
17{
18 // First, create a term manager instance.
19 BitwuzlaTermManager* tm = bitwuzla_term_manager_new();
20 // Create a Bitwuzla options instance.
21 BitwuzlaOptions* options = bitwuzla_options_new();
22
23 // Enable model generation, which expects a boolean configuration value.
24 bitwuzla_set_option(options, BITWUZLA_OPT_PRODUCE_MODELS, 1);
25
26 // Increase the verbosity level, which expects an integer value.
27 printf("Previous verbosity level: %" PRIu64 "\n",
28 bitwuzla_get_option(options, BITWUZLA_OPT_VERBOSITY));
29 bitwuzla_set_option(options, BITWUZLA_OPT_VERBOSITY, 2);
30 printf("Current verbosity level: %" PRIu64 "\n",
31 bitwuzla_get_option(options, BITWUZLA_OPT_VERBOSITY));
32
33 // Now configure an option that has modes (a choice of configuration values).
34 // First, query which bit-vector solver engine is set.
35 printf("Default bv solver: %s\n",
36 bitwuzla_get_option_mode(options, BITWUZLA_OPT_BV_SOLVER));
37 // Then, select the propagation-based local search engine as solver engine.
38 bitwuzla_set_option_mode(options, BITWUZLA_OPT_BV_SOLVER, "prop");
39 printf("Current engine: %s\n",
40 bitwuzla_get_option_mode(options, BITWUZLA_OPT_BV_SOLVER));
41
42 // Now, create a Bitwuzla instance.
43 Bitwuzla* bitwuzla = bitwuzla_new(tm, options);
44 // Check sat (nothing to solve, input formula is empty).
45 BitwuzlaResult result = bitwuzla_check_sat(bitwuzla);
46 printf("Expect: sat\n");
47 printf("Bitwuzla: %s\n", bitwuzla_result_to_string(result));
48
49 // Finally, delete the Bitwuzla solver, options, and term manager instances.
50 bitwuzla_delete(bitwuzla);
51 bitwuzla_options_delete(options);
52 bitwuzla_term_manager_delete(tm);
53
54 return 0;
55}
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/cpp/bitwuzla.h>
12
13#include <iostream>
14
15using namespace bitwuzla;
16
17int
18main()
19{
20 // First, create a Bitwuzla options instance.
21 Options options;
22
23 // Enable model generation, which expects a boolean configuration value.
24 options.set(Option::PRODUCE_MODELS, true);
25
26 // Increase the verbosity level, which expects an integer value.
27 std::cout << "Previous verbosity level: " << options.get(Option::VERBOSITY)
28 << std::endl;
29 options.set(Option::VERBOSITY, 2);
30 std::cout << "Current verbosity level: " << options.get(Option::VERBOSITY)
31 << std::endl;
32
33 // Now configure an option that has modes (a choice of configuration values).
34 // First, query which bit-vector solver engine is set.
35 std::cout << "Default bv solver: " << options.get_mode(Option::BV_SOLVER)
36 << std::endl;
37 // Then, select the propagation-based local search engine as solver engine.
38 options.set(Option::BV_SOLVER, "prop");
39 std::cout << "Current engine: " << options.get_mode(Option::BV_SOLVER)
40 << std::endl;
41
42 // Now, create a Bitwuzla instance with a new term manager.
43 TermManager tm;
44 Bitwuzla bitwuzla(tm, options);
45 // Check sat (nothing to solve, input formula is empty).
46 Result result = bitwuzla.check_sat();
47 std::cout << "Expect: sat" << std::endl;
48 std::cout << "Bitwuzla: " << result << std::endl;
49
50 return 0;
51}
1###
2# Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3#
4# Copyright (C) 2023 by the authors listed in the AUTHORS file at
5# https:#github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6#
7# This file is part of Bitwuzla under the MIT license. See COPYING for more
8# information at https:#github.com/bitwuzla/bitwuzla/blob/main/COPYING
9##
10
11from bitwuzla import *
12
13if __name__ == '__main__':
14
15 # First, create a Bitwuzla options instance.
16 options = Options()
17
18 # Enable model generation, which expects a boolean configuration value.
19 options.set(Option.PRODUCE_MODELS, True)
20
21 # Increase the verbosity level, which expects an integer value.
22 print(f'Previous verbosity level: {options.get(Option.VERBOSITY)}')
23 options.set(Option.VERBOSITY, 2)
24 print(f'Current verbosity level: {options.get(Option.VERBOSITY)}')
25
26 # Now configure an option that has modes (a choice of configuration values).
27 # First, query which bit-vector solver engine is set.
28 print(f'Default bv solver: {options.get(Option.BV_SOLVER)}')
29 # Then, select the propagation-based local search engine as solver engine.
30 options.set(Option.BV_SOLVER, 'prop')
31 print(f'Current engine: {options.get(Option.BV_SOLVER)}')
32
33 # Now, create a Bitwuzla instance.
34 tm = TermManager()
35 bitwuzla = Bitwuzla(tm, options)
36 # Check sat (nothing to solve, input formula is empty).
37 result = bitwuzla.check_sat()
38 print('Expect: sat')
39 print(f'Bitwuzla: {result}')
1(set-option :verbosity 2)
2(set-option :bv-solver "prop")
3(set-option :produce-models true)
4(set-logic ALL)
5(check-sat)
Option Info Example
BitwuzlaOptionInfo
. 1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/c/bitwuzla.h>
12#include <inttypes.h>
13#include <stdio.h>
14
15int
16main()
17{
18 // First, create a Bitwuzla options instance.
19 BitwuzlaOptions* options = bitwuzla_options_new();
20 // Set some options to illustrate current vs default value.
21 bitwuzla_set_option(options, BITWUZLA_OPT_PRODUCE_MODELS, 1);
22 bitwuzla_set_option(options, BITWUZLA_OPT_VERBOSITY, 2);
23 bitwuzla_set_option_mode(options, BITWUZLA_OPT_BV_SOLVER, "prop");
24
25 // Then iterate over all available configuration options and extract info.
26 BitwuzlaOptionInfo info;
27 for (uint32_t i = 0; i < BITWUZLA_OPT_NUM_OPTS; ++i)
28 {
29 BitwuzlaOption opt = (BitwuzlaOption) i;
30 bitwuzla_get_option_info(options, opt, &info);
31 printf("- long name: %s\n", info.lng);
32 printf(" short name: %s\n", (info.shrt ? info.shrt : "-"));
33 printf(" kind: ");
34 if (info.is_numeric)
35 {
36 printf("numeric\n");
37 printf(" values:\n");
38 printf(" + current: %" PRIu64 "\n", info.numeric.cur);
39 printf(" + default: %" PRIu64 "\n", info.numeric.dflt);
40 printf(" + min: %" PRIu64 "\n", info.numeric.min);
41 printf(" + max: %" PRIu64 "\n", info.numeric.max);
42 }
43 else
44 {
45 printf("modes\n");
46 printf(" values:\n");
47 printf(" + current: %s\n", info.mode.cur);
48 printf(" + default: %s\n", info.mode.dflt);
49 printf(" + modes: {");
50 for (size_t i = 0, n = info.mode.num_modes; i < n; ++i)
51 {
52 printf("%s %s", (i > 0 ? "," : ""), info.mode.modes[i]);
53 }
54 printf(" }\n");
55 }
56 printf(" description: %s\n\n", info.description);
57 }
58
59 // Finally, delete options instance.
60 bitwuzla_options_delete(options);
61}
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/cpp/bitwuzla.h>
12
13#include <iostream>
14
15using namespace bitwuzla;
16
17int
18main()
19{
20 // First, create a Bitwuzla options instance.
21 Options options;
22 // Set some options to illustrate current vs default value.
23 options.set(Option::PRODUCE_MODELS, true);
24 options.set(Option::VERBOSITY, 2);
25 options.set(Option::BV_SOLVER, "prop");
26
27 // Then iterate over all available configuration options and extract info.
28 for (int32_t i = 0; i < static_cast<int32_t>(Option::NUM_OPTS); ++i)
29 {
30 Option opt = static_cast<Option>(i);
31 OptionInfo info(options, opt);
32 std::cout << "- long name: " << info.lng << std::endl;
33 std::cout << " short name: " << (info.shrt ? info.shrt : "-")
34 << std::endl;
35 std::cout << " kind: ";
36 if (info.kind == OptionInfo::Kind::BOOL)
37 {
38 std::cout << "bool" << std::endl;
39 std::cout << " values:" << std::endl;
40 const auto& values = std::get<OptionInfo::Bool>(info.values);
41 std::cout << " + current: " << (values.cur ? "true" : "false")
42 << std::endl;
43 std::cout << " + default: " << (values.dflt ? "true" : "false")
44 << std::endl;
45 }
46 else if (info.kind == OptionInfo::Kind::NUMERIC)
47 {
48 std::cout << "numeric" << std::endl;
49 std::cout << " values:" << std::endl;
50 const auto& values = std::get<OptionInfo::Numeric>(info.values);
51 std::cout << " + current: " << values.dflt << std::endl;
52 std::cout << " + default: " << values.cur << std::endl;
53 std::cout << " + min: " << values.min << std::endl;
54 std::cout << " + max: " << values.max << std::endl;
55 }
56 else
57 {
58 std::cout << "modes" << std::endl;
59 std::cout << " values:" << std::endl;
60 const auto& values = std::get<OptionInfo::Mode>(info.values);
61 std::cout << " + current: " << values.dflt << std::endl;
62 std::cout << " + default: " << values.cur << std::endl;
63 std::cout << " + modes: {";
64 for (size_t i = 0, n = values.modes.size(); i < n; ++i)
65 {
66 std::cout << (i > 0 ? "," : "") << " " << values.modes[i];
67 }
68 std::cout << " }" << std::endl;
69 }
70 std::cout << " description: " << info.description << std::endl;
71 std::cout << std::endl;
72 }
73}
1###
2# Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3#
4# Copyright (C) 2023 by the authors listed in the AUTHORS file at
5# https:#github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6#
7# This file is part of Bitwuzla under the MIT license. See COPYING for more
8# information at https:#github.com/bitwuzla/bitwuzla/blob/main/COPYING
9##
10
11from bitwuzla import *
12
13if __name__ == '__main__':
14
15 # First, create a Bitwuzla options instance.
16 options = Options()
17
18 # Set some options to illustrate current vs default value.
19 options.set(Option.PRODUCE_MODELS, True)
20 options.set(Option.VERBOSITY, 2)
21 options.set(Option.BV_SOLVER, 'prop')
22
23 # Then iterate over all available configuration options and extract info.
24 for opt in Option:
25 info = OptionInfo(options, opt)
26 print(f' long name: {info.lng()}')
27 print(f' short name: {info.shrt() if info.shrt() else "-"}')
28 print(' kind: ', end = '')
29 if info.kind() == OptionInfoKind.BOOL:
30 print('bool')
31 print(' values:')
32 print(f' + current: {info.cur()}')
33 print(f' + default: {info.dflt()}')
34 elif info.kind() == OptionInfoKind.NUMERIC:
35 print('numeric')
36 print(' values:')
37 print(f' + current: {info.cur()}')
38 print(f' + default: {info.dflt()}')
39 print(f' + min: {info.min()}')
40 print(f' + max: {info.max()}')
41 else:
42 print('modes')
43 print(' values:')
44 print(f' + current: {info.cur()}')
45 print(f' + default: {info.dflt()}')
46 print(f' + modes: {info.modes()}')
47 print(f' description: {info.description()}\n')
Incremental Example with push and pop
push
and pop
. 1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2021 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/c/bitwuzla.h>
12#include <stdio.h>
13
14int
15main()
16{
17 // First, create a term manager instance.
18 BitwuzlaTermManager* tm = bitwuzla_term_manager_new();
19 // Create a Bitwuzla options instance.
20 BitwuzlaOptions* options = bitwuzla_options_new();
21 // Then, create a Bitwuzla instance.
22 Bitwuzla* bitwuzla = bitwuzla_new(tm, options);
23
24 // Create a bit-vector sort of size 1.
25 BitwuzlaSort sortbv1 = bitwuzla_mk_bv_sort(tm, 1);
26 // Create a bit-vector sort of size 2.
27 BitwuzlaSort sortbv2 = bitwuzla_mk_bv_sort(tm, 2);
28
29 // Create bit-vector variables.
30 // (declare-const o0 (_ BitVec 1))
31 BitwuzlaTerm o0 = bitwuzla_mk_const(tm, sortbv1, "o0");
32 // (declare-const o1 (_ BitVec 1))
33 BitwuzlaTerm o1 = bitwuzla_mk_const(tm, sortbv1, "o1");
34 // (declare-const s0 (_ BitVec 2))
35 BitwuzlaTerm s0 = bitwuzla_mk_const(tm, sortbv2, "s0");
36 // (declare-const s1 (_ BitVec 2))
37 BitwuzlaTerm s1 = bitwuzla_mk_const(tm, sortbv2, "s1");
38 // (declare-const s2 (_ BitVec 2))
39 BitwuzlaTerm s2 = bitwuzla_mk_const(tm, sortbv2, "s2");
40 // (declare-const goal (_ BitVec 2))
41 BitwuzlaTerm goal = bitwuzla_mk_const(tm, sortbv2, "goal");
42
43 // Create bit-vector values zero, one, three.
44 BitwuzlaTerm zero = bitwuzla_mk_bv_zero(tm, sortbv2);
45 BitwuzlaTerm one1 = bitwuzla_mk_bv_one(tm, sortbv1);
46 BitwuzlaTerm one2 = bitwuzla_mk_bv_one(tm, sortbv2);
47 BitwuzlaTerm three = bitwuzla_mk_bv_value_uint64(tm, sortbv2, 3);
48
49 // Add some assertions.
50 bitwuzla_assert(bitwuzla,
51 bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, s0, zero));
52 bitwuzla_assert(bitwuzla,
53 bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, goal, three));
54
55 // Push, assert, check sat and pop.
56 bitwuzla_push(bitwuzla, 1);
57 bitwuzla_assert(bitwuzla,
58 bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, s0, goal));
59 BitwuzlaResult result = bitwuzla_check_sat(bitwuzla);
60 printf("Expect: unsat\n");
61 printf("Bitwuzla: %s\n", bitwuzla_result_to_string(result));
62 bitwuzla_pop(bitwuzla, 1);
63
64 // (assert (= s1 (ite (= o0 (_ sortbv1 1)) (bvadd s0 one) s0)))
65 bitwuzla_assert(bitwuzla,
66 bitwuzla_mk_term2(
67 tm,
68 BITWUZLA_KIND_EQUAL,
69 s1,
70 bitwuzla_mk_term3(
71 tm,
72 BITWUZLA_KIND_ITE,
73 bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, o0, one1),
74 bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_ADD, s0, one2),
75 s0)));
76
77 // Push, assert, check sat and pop.
78 bitwuzla_push(bitwuzla, 1);
79 bitwuzla_assert(bitwuzla,
80 bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, s1, goal));
81 result = bitwuzla_check_sat(bitwuzla);
82 printf("Expect: unsat\n");
83 printf("Bitwuzla: %s\n", bitwuzla_result_to_string(result));
84 bitwuzla_pop(bitwuzla, 1);
85
86 // (assert (= s2 (ite (= o1 (_ sortbv1 1)) (bvadd s1 one) s1)))
87 bitwuzla_assert(bitwuzla,
88 bitwuzla_mk_term2(
89 tm,
90 BITWUZLA_KIND_EQUAL,
91 s2,
92 bitwuzla_mk_term3(
93 tm,
94 BITWUZLA_KIND_ITE,
95 bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, o1, one1),
96 bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_ADD, s1, one2),
97 s1)));
98
99 // Push, assert, check sat and pop.
100 bitwuzla_push(bitwuzla, 1);
101 bitwuzla_assert(bitwuzla,
102 bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, s2, goal));
103 result = bitwuzla_check_sat(bitwuzla);
104 printf("Expect: unsat\n");
105 printf("Bitwuzla: %s\n", bitwuzla_result_to_string(result));
106 bitwuzla_pop(bitwuzla, 1);
107
108 // Finally, delete the Bitwuzla solver, options, and term manager instances.
109 bitwuzla_delete(bitwuzla);
110 bitwuzla_options_delete(options);
111 bitwuzla_term_manager_delete(tm);
112
113 return 0;
114}
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/cpp/bitwuzla.h>
12
13#include <iostream>
14
15using namespace bitwuzla;
16
17int
18main()
19{
20 // First, create a term manager instance.
21 TermManager tm;
22 // Create a Bitwuzla options instance.
23 Options options;
24 // Then, create a Bitwuzla instance.
25 Bitwuzla bitwuzla(tm, options);
26
27 // Create a bit-vector sort of size 1.
28 Sort sortbv1 = tm.mk_bv_sort(1);
29 // Create a bit-vector sort of size 2.
30 Sort sortbv2 = tm.mk_bv_sort(2);
31
32 // Create bit-vector variables.
33 // (declare-const o0 (_ BitVec 1))
34 Term o0 = tm.mk_const(sortbv1, "o0");
35 // (declare-const o1 (_ BitVec 1))
36 Term o1 = tm.mk_const(sortbv1, "o1");
37 // (declare-const s0 (_ BitVec 2))
38 Term s0 = tm.mk_const(sortbv2, "s0");
39 // (declare-const s1 (_ BitVec 2))
40 Term s1 = tm.mk_const(sortbv2, "s1");
41 // (declare-const s2 (_ BitVec 2))
42 Term s2 = tm.mk_const(sortbv2, "s2");
43 // (declare-const goal (_ BitVec 2))
44 Term goal = tm.mk_const(sortbv2, "goal");
45
46 // Create bit-vector values zero, one, three.
47 Term zero = tm.mk_bv_zero(sortbv2);
48 Term one1 = tm.mk_bv_one(sortbv1);
49 Term one2 = tm.mk_bv_one(sortbv2);
50 Term three = tm.mk_bv_value_uint64(sortbv2, 3);
51
52 // Add some assertions.
53 bitwuzla.assert_formula(tm.mk_term(Kind::EQUAL, {s0, zero}));
54 bitwuzla.assert_formula(tm.mk_term(Kind::EQUAL, {goal, three}));
55
56 // Push, assert, check sat and pop.
57 bitwuzla.push(1);
58 bitwuzla.assert_formula(tm.mk_term(Kind::EQUAL, {s0, goal}));
59 Result result = bitwuzla.check_sat();
60 std::cout << "Expect: unsat" << std::endl;
61 std::cout << "Bitwuzla: " << result << std::endl;
62 bitwuzla.pop(1);
63
64 // (assert (= s1 (ite (= o0 (_ sortbv1 1)) (bvadd s0 one) s0)))
65 bitwuzla.assert_formula(
66 tm.mk_term(Kind::EQUAL,
67 {s1,
68 tm.mk_term(Kind::ITE,
69 {tm.mk_term(Kind::EQUAL, {o0, one1}),
70 tm.mk_term(Kind::BV_ADD, {s0, one2}),
71 s0})}));
72
73 // Push, assert, check sat and pop.
74 bitwuzla.push(1);
75 bitwuzla.assert_formula(tm.mk_term(Kind::EQUAL, {s1, goal}));
76 result = bitwuzla.check_sat();
77 std::cout << "Expect: unsat" << std::endl;
78 std::cout << "Bitwuzla: " << result << std::endl;
79 bitwuzla.pop(1);
80
81 // (assert (= s2 (ite (= o1 (_ sortbv1 1)) (bvadd s1 one) s1)))
82 bitwuzla.assert_formula(
83 tm.mk_term(Kind::EQUAL,
84 {s2,
85 tm.mk_term(Kind::ITE,
86 {tm.mk_term(Kind::EQUAL, {o1, one1}),
87 tm.mk_term(Kind::BV_ADD, {s1, one2}),
88 s1})}));
89
90 // Push, assert, check sat and pop.
91 bitwuzla.push(1);
92 bitwuzla.assert_formula(tm.mk_term(Kind::EQUAL, {s2, goal}));
93 result = bitwuzla.check_sat();
94 std::cout << "Expect: unsat" << std::endl;
95 std::cout << "Bitwuzla: " << result << std::endl;
96 bitwuzla.pop(1);
97
98 return 0;
99}
1###
2# Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3#
4# Copyright (C) 2023 by the authors listed in the AUTHORS file at
5# https:#github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6#
7# This file is part of Bitwuzla under the MIT license. See COPYING for more
8# information at https:#github.com/bitwuzla/bitwuzla/blob/main/COPYING
9##
10
11from bitwuzla import *
12
13if __name__ == "__main__":
14
15 # No options configured, create Bitwuzla instance with default options.
16 tm = TermManager()
17 bitwuzla = Bitwuzla(tm)
18
19 # Create a bit-vector sort of size 1.
20 sortbv1 = tm.mk_bv_sort(1)
21 # Create a bit-vector sort of size 2.
22 sortbv2 = tm.mk_bv_sort(2)
23
24 # Create bit-vector variables.
25 # (declare-const o0 (_ BitVec 1))
26 o0 = tm.mk_const(sortbv1, 'o0')
27 # (declare-const o1 (_ BitVec 1))
28 o1 = tm.mk_const(sortbv1, 'o1')
29 # (declare-const s0 (_ BitVec 2))
30 s0 = tm.mk_const(sortbv2, 's0')
31 # (declare-const s1 (_ BitVec 2))
32 s1 = tm.mk_const(sortbv2, 's1')
33 # (declare-const s2 (_ BitVec 2))
34 s2 = tm.mk_const(sortbv2, 's2')
35 # (declare-const goal (_ BitVec 2))
36 goal = tm.mk_const(sortbv2, 'goal')
37
38 # Create bit-vector values zero, one, three.
39 zero = tm.mk_bv_zero(sortbv2)
40 one1 = tm.mk_bv_one(sortbv1)
41 one2 = tm.mk_bv_one(sortbv2)
42 three = tm.mk_bv_value(sortbv2, 3)
43
44 # Add some assertions.
45 bitwuzla.assert_formula(tm.mk_term(Kind.EQUAL, [s0, zero]))
46 bitwuzla.assert_formula(tm.mk_term(Kind.EQUAL, [goal, three]))
47
48 # Push, assert, check sat and pop.
49 bitwuzla.push(1)
50 bitwuzla.assert_formula(tm.mk_term(Kind.EQUAL, [s0, goal]))
51 result = bitwuzla.check_sat()
52 print('Expect: unsat')
53 print(f'Bitwuzla: {result}')
54 bitwuzla.pop(1)
55
56 # (assert (= s1 (ite (= o0 (_ sortbv1 1)) (bvadd s0 one) s0)))
57 bitwuzla.assert_formula(tm.mk_term(Kind.EQUAL,
58 [s1,
59 tm.mk_term(Kind.ITE,
60 [tm.mk_term(Kind.EQUAL, [o0, one1]),
61 tm.mk_term(Kind.BV_ADD, [s0, one2]),
62 s0])]))
63
64 # Push, assert, check sat and pop.
65 bitwuzla.push(1)
66 bitwuzla.assert_formula(tm.mk_term(Kind.EQUAL, [s1, goal]))
67 result = bitwuzla.check_sat()
68 print('Expect: unsat')
69 print(f'Bitwuzla: {result}')
70 bitwuzla.pop(1)
71
72 # (assert (= s2 (ite (= o1 (_ sortbv1 1)) (bvadd s1 one) s1)))
73 bitwuzla.assert_formula(tm.mk_term(Kind.EQUAL,
74 [s2,
75 tm.mk_term(Kind.ITE,
76 [tm.mk_term(Kind.EQUAL, [o1, one1]),
77 tm.mk_term(Kind.BV_ADD, [s1, one2]),
78 s1])]))
79
80 # Push, assert, check sat and pop.
81 bitwuzla.push(1)
82 bitwuzla.assert_formula(tm.mk_term(Kind.EQUAL, [s2, goal]))
83 result = bitwuzla.check_sat()
84 print('Expect: unsat')
85 print(f'Bitwuzla: {result}')
86 bitwuzla.pop(1)
1(set-logic QF_BV)
2
3(declare-fun o0 () (_ BitVec 1))
4(declare-fun o1 () (_ BitVec 1))
5(declare-fun s0 () (_ BitVec 2))
6(declare-fun s1 () (_ BitVec 2))
7(declare-fun s2 () (_ BitVec 2))
8(declare-fun goal () (_ BitVec 2))
9
10(assert (= s0 (_ bv0 2)))
11(assert (= goal (_ bv3 2)))
12(push 1)
13(assert (= s0 goal))
14(check-sat)
15(pop 1)
16
17(assert (= s1 (ite (= o0 (_ bv1 1)) (bvadd s0 (_ bv1 2)) s0)))
18(push 1)
19(assert (= s1 goal))
20(check-sat)
21(pop 1)
22
23(assert (= s2 (ite (= o1 (_ bv1 1)) (bvadd s1 (_ bv1 2)) s1)))
24(push 1)
25(assert (= s2 goal))
26(check-sat)
27(pop 1)
Incremental Example with check-sat-assuming
check-sat-assuming
. 1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2021 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/c/bitwuzla.h>
12#include <stdio.h>
13
14int
15main()
16{
17 BitwuzlaResult result;
18
19 // First, create a term manager instance.
20 BitwuzlaTermManager* tm = bitwuzla_term_manager_new();
21 // Create a Bitwuzla options instance.
22 BitwuzlaOptions* options = bitwuzla_options_new();
23 // Then, create a Bitwuzla instance.
24 Bitwuzla* bitwuzla = bitwuzla_new(tm, options);
25
26 // Create a bit-vector sort of size 1.
27 BitwuzlaSort sortbv1 = bitwuzla_mk_bv_sort(tm, 1);
28 // Create a bit-vector sort of size 2
29 BitwuzlaSort sortbv2 = bitwuzla_mk_bv_sort(tm, 2);
30
31 // Create bit-vector variables.
32 // (declare-const o0 (_ BitVec 1))
33 BitwuzlaTerm o0 = bitwuzla_mk_const(tm, sortbv1, "o0");
34 // (declare-const o1 (_ BitVec 1))
35 BitwuzlaTerm o1 = bitwuzla_mk_const(tm, sortbv1, "o1");
36 // (declare-const s0 (_ BitVec 2))
37 BitwuzlaTerm s0 = bitwuzla_mk_const(tm, sortbv2, "s0");
38 // (declare-const s1 (_ BitVec 2))
39 BitwuzlaTerm s1 = bitwuzla_mk_const(tm, sortbv2, "s1");
40 // (declare-const s2 (_ BitVec 2))
41 BitwuzlaTerm s2 = bitwuzla_mk_const(tm, sortbv2, "s2");
42 // (declare-const goal (_ BitVec 2))
43 BitwuzlaTerm goal = bitwuzla_mk_const(tm, sortbv2, "goal");
44
45 // Create bit-vector values zero, one, three.
46 BitwuzlaTerm zero = bitwuzla_mk_bv_zero(tm, sortbv2);
47 BitwuzlaTerm one1 = bitwuzla_mk_bv_one(tm, sortbv1);
48 BitwuzlaTerm one2 = bitwuzla_mk_bv_one(tm, sortbv2);
49 BitwuzlaTerm three = bitwuzla_mk_bv_value_uint64(tm, sortbv2, 3);
50
51 // Add some assertions.
52 bitwuzla_assert(bitwuzla,
53 bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, s0, zero));
54 bitwuzla_assert(bitwuzla,
55 bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, goal, three));
56
57 // (check-sat-assuming ((= s0 goal)))
58 BitwuzlaTerm assumptions[1] = {
59 bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, s0, goal)};
60 result = bitwuzla_check_sat_assuming(bitwuzla, 1, assumptions);
61 printf("Expect: unsat\n");
62 printf("Bitwuzla: %s\n", bitwuzla_result_to_string(result));
63
64 // (assert (= s1 (ite (= o0 (_ sortbv1 1)) (bvadd s0 one) s0)))
65 bitwuzla_assert(bitwuzla,
66 bitwuzla_mk_term2(
67 tm,
68 BITWUZLA_KIND_EQUAL,
69 s1,
70 bitwuzla_mk_term3(
71 tm,
72 BITWUZLA_KIND_ITE,
73 bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, o0, one1),
74 bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_ADD, s0, one2),
75 s0)));
76
77 // (check-sat-assuming ((= s1 goal)))
78 assumptions[0] = bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, s1, goal);
79 result = bitwuzla_check_sat_assuming(bitwuzla, 1, assumptions);
80 printf("Expect: unsat\n");
81 printf("Bitwuzla: %s\n", bitwuzla_result_to_string(result));
82
83 // (assert (= s2 (ite (= o1 (_ sortbv1 1)) (bvadd s1 one) s1)))
84 bitwuzla_assert(bitwuzla,
85 bitwuzla_mk_term2(
86 tm,
87 BITWUZLA_KIND_EQUAL,
88 s2,
89 bitwuzla_mk_term3(
90 tm,
91 BITWUZLA_KIND_ITE,
92 bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, o1, one1),
93 bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_ADD, s1, one2),
94 s1)));
95
96 // (check-sat-assuming ((= s2 goal)))
97 assumptions[0] = bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, s2, goal);
98 result = bitwuzla_check_sat_assuming(bitwuzla, 1, assumptions);
99 printf("Expect: unsat\n");
100 printf("Bitwuzla: %s\n", bitwuzla_result_to_string(result));
101
102 // Finally, delete the Bitwuzla solver, options, and term manager instances.
103 bitwuzla_delete(bitwuzla);
104 bitwuzla_options_delete(options);
105 bitwuzla_term_manager_delete(tm);
106
107 return 0;
108}
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/cpp/bitwuzla.h>
12
13#include <iostream>
14
15using namespace bitwuzla;
16
17int
18main()
19{
20 Result result;
21
22 // First, create a term manager instance.
23 TermManager tm;
24 // Create a Bitwuzla options instance.
25 Options options;
26 // Then, create a Bitwuzla instance.
27 Bitwuzla bitwuzla(tm, options);
28
29 // Create a bit-vector sort of size 1.
30 Sort sortbv1 = tm.mk_bv_sort(1);
31 // Create a bit-vector sort of size 2
32 Sort sortbv2 = tm.mk_bv_sort(2);
33
34 // Create bit-vector variables.
35 // (declare-const o0 (_ BitVec 1))
36 Term o0 = tm.mk_const(sortbv1, "o0");
37 // (declare-const o1 (_ BitVec 1))
38 Term o1 = tm.mk_const(sortbv1, "o1");
39 // (declare-const s0 (_ BitVec 2))
40 Term s0 = tm.mk_const(sortbv2, "s0");
41 // (declare-const s1 (_ BitVec 2))
42 Term s1 = tm.mk_const(sortbv2, "s1");
43 // (declare-const s2 (_ BitVec 2))
44 Term s2 = tm.mk_const(sortbv2, "s2");
45 // (declare-const goal (_ BitVec 2))
46 Term goal = tm.mk_const(sortbv2, "goal");
47
48 // Create bit-vector values zero, one, three.
49 Term zero = tm.mk_bv_zero(sortbv2);
50 Term one1 = tm.mk_bv_one(sortbv1);
51 Term one2 = tm.mk_bv_one(sortbv2);
52 Term three = tm.mk_bv_value_uint64(sortbv2, 3);
53
54 // Add some assertions.
55 bitwuzla.assert_formula(tm.mk_term(Kind::EQUAL, {s0, zero}));
56 bitwuzla.assert_formula(tm.mk_term(Kind::EQUAL, {goal, three}));
57
58 // (check-sat-assuming ((= s0 goal)))
59 result = bitwuzla.check_sat({tm.mk_term(Kind::EQUAL, {s0, goal})});
60 std::cout << "Expect: unsat" << std::endl;
61 std::cout << "Bitwuzla: " << result << std::endl;
62
63 // (assert (= s1 (ite (= o0 (_ sortbv1 1)) (bvadd s0 one) s0)))
64 bitwuzla.assert_formula(
65 tm.mk_term(Kind::EQUAL,
66 {s1,
67 tm.mk_term(Kind::ITE,
68 {tm.mk_term(Kind::EQUAL, {o0, one1}),
69 tm.mk_term(Kind::BV_ADD, {s0, one2}),
70 s0})}));
71
72 // (check-sat-assuming ((= s1 goal)))
73 result = bitwuzla.check_sat({tm.mk_term(Kind::EQUAL, {s1, goal})});
74 std::cout << "Expect: unsat" << std::endl;
75 std::cout << "Bitwuzla: " << result << std::endl;
76
77 // (assert (= s2 (ite (= o1 (_ sortbv1 1)) (bvadd s1 one) s1)))
78 bitwuzla.assert_formula(
79 tm.mk_term(Kind::EQUAL,
80 {s2,
81 tm.mk_term(Kind::ITE,
82 {tm.mk_term(Kind::EQUAL, {o1, one1}),
83 tm.mk_term(Kind::BV_ADD, {s1, one2}),
84 s1})}));
85
86 // (check-sat-assuming ((= s2 goal)))
87 result = bitwuzla.check_sat({tm.mk_term(Kind::EQUAL, {s2, goal})});
88 std::cout << "Expect: unsat" << std::endl;
89 std::cout << "Bitwuzla: " << result << std::endl;
90
91 return 0;
92}
1###
2# Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3#
4# Copyright (C) 2023 by the authors listed in the AUTHORS file at
5# https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6#
7# This file is part of Bitwuzla under the MIT license. See COPYING for more
8# information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9##
10
11from bitwuzla import *
12
13if __name__ == "__main__":
14
15 # No options configured, create Bitwuzla instance with default options.
16 tm = TermManager()
17 bitwuzla = Bitwuzla(tm)
18
19 # Create a bit-vector sort of size 1.
20 sortbv1 = tm.mk_bv_sort(1)
21 # Create a bit-vector sort of size 2
22 sortbv2 = tm.mk_bv_sort(2)
23
24 # Create bit-vector variables.
25 # (declare-const o0 (_ BitVec 1))
26 o0 = tm.mk_const(sortbv1, 'o0')
27 # (declare-const o1 (_ BitVec 1))
28 o1 = tm.mk_const(sortbv1, 'o1')
29 # (declare-const s0 (_ BitVec 2))
30 s0 = tm.mk_const(sortbv2, 's0')
31 # (declare-const s1 (_ BitVec 2))
32 s1 = tm.mk_const(sortbv2, 's1')
33 # (declare-const s2 (_ BitVec 2))
34 s2 = tm.mk_const(sortbv2, 's2')
35 # (declare-const goal (_ BitVec 2))
36 goal = tm.mk_const(sortbv2, 'goal')
37
38 # Create bit-vector values zero, one, three.
39 zero = tm.mk_bv_zero(sortbv2)
40 one1 = tm.mk_bv_one(sortbv1)
41 one2 = tm.mk_bv_one(sortbv2)
42 three = tm.mk_bv_value(sortbv2, 3)
43
44 # Add some assertions.
45 bitwuzla.assert_formula(tm.mk_term(Kind.EQUAL, [s0, zero]))
46 bitwuzla.assert_formula(tm.mk_term(Kind.EQUAL, [goal, three]))
47
48 # (check-sat-assuming ((= s0 goal)))
49 result = bitwuzla.check_sat(tm.mk_term(Kind.EQUAL, [s0, goal]))
50 print('Expect: unsat')
51 print(f'Bitwuzla: {result}')
52
53 # (assert (= s1 (ite (= o0 (_ sortbv1 1)) (bvadd s0 one) s0)))
54 bitwuzla.assert_formula(tm.mk_term(Kind.EQUAL,
55 [s1,
56 tm.mk_term(Kind.ITE,
57 [tm.mk_term(Kind.EQUAL, [o0, one1]),
58 tm.mk_term(Kind.BV_ADD, [s0, one2]),
59 s0])]))
60
61 # (check-sat-assuming ((= s1 goal)))
62 result = bitwuzla.check_sat(tm.mk_term(Kind.EQUAL, [s1, goal]))
63 print('Expect: unsat')
64 print(f'Bitwuzla: {result}')
65
66 # (assert (= s2 (ite (= o1 (_ sortbv1 1)) (bvadd s1 one) s1)))
67 bitwuzla.assert_formula(tm.mk_term(Kind.EQUAL,
68 [s2,
69 tm.mk_term(Kind.ITE,
70 [tm.mk_term(Kind.EQUAL, [o1, one1]),
71 tm.mk_term(Kind.BV_ADD, [s1, one2]),
72 s1])]))
73
74 # (check-sat-assuming ((= s2 goal)))
75 result = bitwuzla.check_sat(tm.mk_term(Kind.EQUAL, [s2, goal]))
76 print('Expect: unsat')
77 print(f'Bitwuzla: {result}')
1(set-logic QF_BV)
2
3(declare-fun o0 () (_ BitVec 1))
4(declare-fun o1 () (_ BitVec 1))
5(declare-fun s0 () (_ BitVec 2))
6(declare-fun s1 () (_ BitVec 2))
7(declare-fun s2 () (_ BitVec 2))
8(declare-fun goal () (_ BitVec 2))
9
10(assert (= s0 (_ bv0 2)))
11(assert (= goal (_ bv3 2)))
12(check-sat-assuming ((= s0 goal)))
13
14(assert (= s1 (ite (= o0 (_ bv1 1)) (bvadd s0 (_ bv1 2)) s0)))
15(check-sat-assuming ((= s1 goal)))
16
17(assert (= s2 (ite (= o1 (_ bv1 1)) (bvadd s1 (_ bv1 2)) s1)))
18(check-sat-assuming ((= s2 goal)))
Unsat Core Example
This example shows how to extract an unsat core.
It creates bit-vector and floating-point terms and further illustrates how to
create lambda terms (define-fun
).
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2021 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/c/bitwuzla.h>
12#include <stdio.h>
13
14int
15main()
16{
17 // First, create a term manager instance.
18 BitwuzlaTermManager *tm = bitwuzla_term_manager_new();
19 // Create a Bitwuzla options instance.
20 BitwuzlaOptions *options = bitwuzla_options_new();
21 // Then, enable unsat core extraction.
22 // Note: Unsat core extraction is disabled by default.
23 bitwuzla_set_option(options, BITWUZLA_OPT_PRODUCE_UNSAT_CORES, 1);
24 // Then, create a Bitwuzla instance.
25 Bitwuzla *bitwuzla = bitwuzla_new(tm, options);
26
27 // Create a bit-vector sort of size 2.
28 BitwuzlaSort sortbv2 = bitwuzla_mk_bv_sort(tm, 2);
29 // Create a bit-vector sort of size 4.
30 BitwuzlaSort sortbv4 = bitwuzla_mk_bv_sort(tm, 4);
31 // Create Float16 floatinf-point sort.
32 BitwuzlaSort sortfp16 = bitwuzla_mk_fp_sort(tm, 5, 11);
33
34 // Create bit-vector variables.
35 // (declare-const x0 (_ BitVec 4))
36 BitwuzlaTerm x0 = bitwuzla_mk_const(tm, sortbv4, "x0");
37 // (declare-const x1 (_ BitVec 2))
38 BitwuzlaTerm x1 = bitwuzla_mk_const(tm, sortbv2, "x1");
39 // (declare-const x2 (_ BitVec 2))
40 BitwuzlaTerm x2 = bitwuzla_mk_const(tm, sortbv2, "x2");
41 // (declare-const x3 (_ BitVec 2))
42 BitwuzlaTerm x3 = bitwuzla_mk_const(tm, sortbv2, "x3");
43 // (declare-const x4 Float16)
44 BitwuzlaTerm x4 = bitwuzla_mk_const(tm, sortfp16, "x4");
45
46 // Create FP positive zero.
47 BitwuzlaTerm fpzero = bitwuzla_mk_fp_pos_zero(tm, sortfp16);
48 // Create BV zero of size 4.
49 BitwuzlaTerm bvzero = bitwuzla_mk_bv_zero(tm, sortbv4);
50
51 // (define-fun f0 ((a Float16)) Bool (fp.gt a (_ +zero 5 11)))
52 BitwuzlaTerm a_f0 = bitwuzla_mk_var(tm, sortfp16, "a_f0");
53 BitwuzlaTerm f0 = bitwuzla_mk_term2(
54 tm,
55 BITWUZLA_KIND_LAMBDA,
56 a_f0,
57 bitwuzla_mk_term2(tm, BITWUZLA_KIND_FP_GT, a_f0, fpzero));
58
59 // (define-fun f1 ((a Float16)) (_ BitVec 4) (ite (f0 a) x0 #b0000))
60 BitwuzlaTerm a_f1 = bitwuzla_mk_var(tm, sortfp16, "a_f1");
61 BitwuzlaTerm f1 = bitwuzla_mk_term2(
62 tm,
63 BITWUZLA_KIND_LAMBDA,
64 a_f1,
65 bitwuzla_mk_term3(tm,
66 BITWUZLA_KIND_ITE,
67 bitwuzla_mk_term2(tm, BITWUZLA_KIND_APPLY, f0, a_f1),
68 x0,
69 bvzero));
70
71 // (define-fun f2 ((a Float16)) (_ BitVec 2) ((_ extract 1 0) (f1 a)))
72 BitwuzlaTerm a_f2 = bitwuzla_mk_var(tm, sortfp16, "a_f2");
73 BitwuzlaTerm f2 = bitwuzla_mk_term2(
74 tm,
75 BITWUZLA_KIND_LAMBDA,
76 a_f2,
77 bitwuzla_mk_term1_indexed2(
78 tm,
79 BITWUZLA_KIND_BV_EXTRACT,
80 bitwuzla_mk_term2(tm, BITWUZLA_KIND_APPLY, f1, a_f2),
81 1,
82 0));
83
84 // (assert (! (bvult x2 (f2 (_ +zero 5 11))) :named a0))
85 BitwuzlaTerm a0 =
86 bitwuzla_mk_term2(tm,
87 BITWUZLA_KIND_BV_ULT,
88 x2,
89 bitwuzla_mk_term2(tm, BITWUZLA_KIND_APPLY, f2, fpzero));
90 bitwuzla_assert(bitwuzla, a0);
91
92 // (assert (! (= x1 x2 x3) :named a1))
93 BitwuzlaTerm a1 = bitwuzla_mk_term3(tm, BITWUZLA_KIND_EQUAL, x1, x2, x3);
94 bitwuzla_assert(bitwuzla, a1);
95
96 // (assert (!(= x4 ((_ to_fp_unsigned 5 11) RNE x3)) :named a2))
97 BitwuzlaTerm a2 = bitwuzla_mk_term2(
98 tm,
99 BITWUZLA_KIND_EQUAL,
100 x4,
101 bitwuzla_mk_term2_indexed2(tm,
102 BITWUZLA_KIND_FP_TO_FP_FROM_UBV,
103 bitwuzla_mk_rm_value(tm, BITWUZLA_RM_RNE),
104 x3,
105 5,
106 11));
107 bitwuzla_assert(bitwuzla, a2);
108
109 // (check-sat)
110 BitwuzlaResult result = bitwuzla_check_sat(bitwuzla);
111 printf("Expect: unsat\n");
112 printf("Bitwuzla: %s\n", bitwuzla_result_to_string(result));
113
114 // (get-unsat-core)
115 size_t unsat_core_size;
116 const BitwuzlaTerm *unsat_core =
117 bitwuzla_get_unsat_core(bitwuzla, &unsat_core_size);
118 printf("Unsat Core:\n{\n");
119 for (uint32_t i = 0; i < unsat_core_size; ++i)
120 {
121 printf(" %s\n", bitwuzla_term_to_string(unsat_core[i]));
122 }
123 printf("}\n");
124
125 // Finally, delete the Bitwuzla solver, options, and term manager instances.
126 bitwuzla_delete(bitwuzla);
127 bitwuzla_options_delete(options);
128 bitwuzla_term_manager_delete(tm);
129
130 return 0;
131}
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/cpp/bitwuzla.h>
12
13using namespace bitwuzla;
14
15int
16main()
17{
18 // First, create a term manager instance.
19 TermManager tm;
20 // Create a Bitwuzla options instance.
21 Options options;
22 // Then, enable unsat core extraction.
23 // Note: Unsat core extraction is disabled by default.
24 options.set(Option::PRODUCE_UNSAT_CORES, true);
25 // Then, create a Bitwuzla instance.
26 Bitwuzla bitwuzla(tm, options);
27
28 // Create a bit-vector sort of size 2.
29 Sort sortbv2 = tm.mk_bv_sort(2);
30 // Create a bit-vector sort of size 4.
31 Sort sortbv4 = tm.mk_bv_sort(4);
32 // Create Float16 floatinf-point sort.
33 Sort sortfp16 = tm.mk_fp_sort(5, 11);
34
35 // Create bit-vector variables.
36 // (declare-const x0 (_ BitVec 4))
37 Term x0 = tm.mk_const(sortbv4, "x0");
38 // (declare-const x1 (_ BitVec 2))
39 Term x1 = tm.mk_const(sortbv2, "x1");
40 // (declare-const x2 (_ BitVec 2))
41 Term x2 = tm.mk_const(sortbv2, "x2");
42 // (declare-const x3 (_ BitVec 2))
43 Term x3 = tm.mk_const(sortbv2, "x3");
44 // (declare-const x4 Float16)
45 Term x4 = tm.mk_const(sortfp16, "x4");
46
47 // Create FP positive zero.
48 Term fpzero = tm.mk_fp_pos_zero(sortfp16);
49 // Create BV zero of size 4.
50 Term bvzero = tm.mk_bv_zero(sortbv4);
51
52 // (define-fun f0 ((a Float16)) Bool (fp.gt a (_ +zero 5 11)))
53 Term a_f0 = tm.mk_var(sortfp16, "a_f0");
54 Term f0 =
55 tm.mk_term(Kind::LAMBDA, {a_f0, tm.mk_term(Kind::FP_GT, {a_f0, fpzero})});
56
57 // (define-fun f1 ((a Float16)) (_ BitVec 4) (ite (f0 a) x0 #b0000))
58 Term a_f1 = tm.mk_var(sortfp16, "a_f1");
59 Term f1 = tm.mk_term(
60 Kind::LAMBDA,
61 {a_f1,
62 tm.mk_term(Kind::ITE,
63 {tm.mk_term(Kind::APPLY, {f0, a_f1}), x0, bvzero})});
64
65 // (define-fun f2 ((a Float16)) (_ BitVec 2) ((_ extract 1 0) (f1 a)))
66 Term a_f2 = tm.mk_var(sortfp16, "a_f2");
67 Term f2 = tm.mk_term(
68 Kind::LAMBDA,
69 {a_f2,
70 tm.mk_term(
71 Kind::BV_EXTRACT, {tm.mk_term(Kind::APPLY, {f1, a_f2})}, {1, 0})});
72
73 // (assert (! (bvult x2 (f2 (_ +zero 5 11))) :named a0))
74 Term a0 =
75 tm.mk_term(Kind::BV_ULT, {x2, tm.mk_term(Kind::APPLY, {f2, fpzero})});
76 bitwuzla.assert_formula(a0);
77
78 // (assert (! (= x1 x2 x3) :named a1))
79 Term a1 = tm.mk_term(Kind::EQUAL, {x1, x2, x3});
80 bitwuzla.assert_formula(a1);
81
82 // (assert (!(= x4 ((_ to_fp_unsigned 5 11) RNE x3)) :named a2))
83 Term a2 = tm.mk_term(Kind::EQUAL,
84 {x4,
85 tm.mk_term(Kind::FP_TO_FP_FROM_UBV,
86 {tm.mk_rm_value(RoundingMode::RNE), x3},
87 {5, 11})});
88 bitwuzla.assert_formula(a2);
89
90 // (check-sat)
91 Result result = bitwuzla.check_sat();
92 std::cout << "Expect: unsat" << std::endl;
93 std::cout << "Bitwuzla: " << result << std::endl;
94
95 // (get-unsat-core)
96 auto unsat_core = bitwuzla.get_unsat_core();
97 std::cout << "Unsat Core:" << std::endl << "{" << std::endl;
98 for (const auto& t : unsat_core)
99 {
100 std::cout << " " << t << std::endl;
101 }
102 std::cout << "}" << std::endl;
103
104 return 0;
105}
1###
2# Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3#
4# Copyright (C) 2023 by the authors listed in the AUTHORS file at
5# https:#github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6#
7# This file is part of Bitwuzla under the MIT license. See COPYING for more
8# information at https:#github.com/bitwuzla/bitwuzla/blob/main/COPYING
9##
10
11from bitwuzla import *
12
13if __name__ == '__main__':
14
15 # First, create a term manager instance.
16 tm = TermManager()
17 # Create a Bitwuzla options instance.
18 options = Options()
19 # Then, enable unsat core extraction.
20 # Note: Unsat core extraction is disabled by default.
21 options.set(Option.PRODUCE_UNSAT_CORES, True)
22
23 # Then, create a Bitwuzla instance.
24 bitwuzla = Bitwuzla(tm, options)
25
26 # Create a bit-vector sort of size 2.
27 sortbv2 = tm.mk_bv_sort(2)
28 # Create a bit-vector sort of size 4.
29 sortbv4 = tm.mk_bv_sort(4)
30 # Create Float16 floatinf-point sort.
31 sortfp16 = tm.mk_fp_sort(5, 11)
32
33 # Create bit-vector variables.
34 # (declare-const x0 (_ BitVec 4))
35 x0 = tm.mk_const(sortbv4, 'x0')
36 # (declare-const x1 (_ BitVec 2))
37 x1 = tm.mk_const(sortbv2, 'x1')
38 # (declare-const x2 (_ BitVec 2))
39 x2 = tm.mk_const(sortbv2, 'x2')
40 # (declare-const x3 (_ BitVec 2))
41 x3 = tm.mk_const(sortbv2, 'x3')
42 # (declare-const x4 Float16)
43 x4 = tm.mk_const(sortfp16, 'x4')
44
45 # Create FP positive zero.
46 fpzero = tm.mk_fp_pos_zero(sortfp16)
47 # Create BV zero of size 4.
48 bvzero = tm.mk_bv_zero(sortbv4)
49
50 # (define-fun f0 ((a Float16)) Bool (fp.gt a (_ +zero 5 11)))
51 a_f0 = tm.mk_var(sortfp16, 'a_f0')
52 f0 = tm.mk_term(Kind.LAMBDA, [a_f0, tm.mk_term(Kind.FP_GT, [a_f0, fpzero])])
53
54 # (define-fun f1 ((a Float16)) (_ BitVec 4) (ite (f0 a) x0 #b0000))
55 a_f1 = tm.mk_var(sortfp16, 'a_f1')
56 f1 = tm.mk_term(
57 Kind.LAMBDA,
58 [a_f1,
59 tm.mk_term(Kind.ITE, [tm.mk_term(Kind.APPLY, [f0, a_f1]), x0, bvzero])])
60
61 # (define-fun f2 ((a Float16)) (_ BitVec 2) ((_ extract 1 0) (f1 a)))
62 a_f2 = tm.mk_var(sortfp16, 'a_f2')
63 f2 = tm.mk_term(
64 Kind.LAMBDA,
65 [a_f2,
66 tm.mk_term(Kind.BV_EXTRACT, [tm.mk_term(Kind.APPLY, [f1, a_f2])], [1, 0])])
67
68 # (assert (! (bvult x2 (f2 (_ +zero 5 11))) :named a0))
69 a0 = tm.mk_term(Kind.BV_ULT, [x2, tm.mk_term(Kind.APPLY, [f2, fpzero])])
70 bitwuzla.assert_formula(a0)
71
72 # (assert (! (= x1 x2 x3) :named a1))
73 a1 = tm.mk_term(Kind.EQUAL, [x1, x2, x3])
74 bitwuzla.assert_formula(a1)
75
76 # (assert (!(= x4 ((_ to_fp_unsigned 5 11) RNE x3)) :named a2))
77 a2 = tm.mk_term(Kind.EQUAL,
78 [x4,
79 tm.mk_term(Kind.FP_TO_FP_FROM_UBV,
80 [tm.mk_rm_value(RoundingMode.RNE), x3],
81 [5, 11])])
82 bitwuzla.assert_formula(a2)
83
84 # (check-sat)
85 result = bitwuzla.check_sat()
86 print('Expect: unsat')
87 print(f'Bitwuzla: {result}')
88
89 # (get-unsat-core)
90 unsat_core = bitwuzla.get_unsat_core()
91 print('Unsat Core:')
92 print('{')
93 for t in unsat_core:
94 print(f' {t}')
95 print('}')
1(set-logic ALL)
2(set-option :produce-unsat-cores true)
3(declare-const x0 (_ BitVec 4))
4(declare-const x1 (_ BitVec 2))
5(declare-const x2 (_ BitVec 2))
6(declare-const x3 (_ BitVec 2))
7(declare-const x4 Float16)
8(define-fun f0 ((a Float16)) Bool (fp.gt a (_ +zero 5 11)))
9(define-fun f1 ((a Float16)) (_ BitVec 4) (ite (f0 a) x0 #b0000))
10(define-fun f2 ((a Float16)) (_ BitVec 2) ((_ extract 1 0) (f1 a)))
11(assert (! (bvult x2 (f2 (_ +zero 5 11))) :named a0))
12(assert (! (= x1 x2 x3) :named a1))
13(assert (!(= x4 ((_ to_fp_unsigned 5 11) RNE x3)) :named a2))
14(check-sat)
15(get-unsat-core)
Unsat Assumptions Example
get-unsat-assumptions
. 1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2021 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/c/bitwuzla.h>
12#include <stdio.h>
13
14int
15main()
16{
17 // First, create a term manager instance.
18 BitwuzlaTermManager *tm = bitwuzla_term_manager_new();
19 // Create a Bitwuzla options instance.
20 BitwuzlaOptions *options = bitwuzla_options_new();
21 // (set-option :produce-unsat-assumptions true)
22 bitwuzla_set_option(options, BITWUZLA_OPT_PRODUCE_UNSAT_ASSUMPTIONS, 1);
23 // Then, create a Bitwuzla instance.
24 Bitwuzla *bitwuzla = bitwuzla_new(tm, options);
25
26 // Create Boolean sort.
27 BitwuzlaSort sortbool = bitwuzla_mk_bool_sort(tm);
28 // Create a bit-vector sort of size 2.
29 BitwuzlaSort sortbv2 = bitwuzla_mk_bv_sort(tm, 2);
30 // Create a bit-vector sort of size 4.
31 BitwuzlaSort sortbv4 = bitwuzla_mk_bv_sort(tm, 4);
32 // Create Float16 floatinf-point sort.
33 BitwuzlaSort sortfp16 = bitwuzla_mk_fp_sort(tm, 5, 11);
34 // Create bit-vector variables.
35 // (declare-const x0 (_ BitVec 4))
36 BitwuzlaTerm x0 = bitwuzla_mk_const(tm, sortbv4, "x0");
37 // (declare-const x1 (_ BitVec 2))
38 BitwuzlaTerm x1 = bitwuzla_mk_const(tm, sortbv2, "x1");
39 // (declare-const x2 (_ BitVec 2))
40 BitwuzlaTerm x2 = bitwuzla_mk_const(tm, sortbv2, "x2");
41 // (declare-const x3 (_ BitVec 2))
42 BitwuzlaTerm x3 = bitwuzla_mk_const(tm, sortbv2, "x3");
43 // (declare-const x4 Float16)
44 BitwuzlaTerm x4 = bitwuzla_mk_const(tm, sortfp16, "x4");
45
46 // Create FP positive zero.
47 BitwuzlaTerm fpzero = bitwuzla_mk_fp_pos_zero(tm, sortfp16);
48 // Create BV zero of size 4.
49 BitwuzlaTerm bvzero = bitwuzla_mk_bv_zero(tm, sortbv4);
50
51 // (define-fun f0 ((a Float16)) Bool (fp.gt a (_ +zero 5 11)))
52 BitwuzlaTerm a_f0 = bitwuzla_mk_var(tm, sortfp16, "a_f0");
53 BitwuzlaTerm f0 = bitwuzla_mk_term2(
54 tm,
55 BITWUZLA_KIND_LAMBDA,
56 a_f0,
57 bitwuzla_mk_term2(tm, BITWUZLA_KIND_FP_GT, a_f0, fpzero));
58
59 // (define-fun f1 ((a Float16)) (_ BitVec 4) (ite (f0 a) x0 #b0000))
60 BitwuzlaTerm a_f1 = bitwuzla_mk_var(tm, sortfp16, "a_f1");
61 BitwuzlaTerm f1 = bitwuzla_mk_term2(
62 tm,
63 BITWUZLA_KIND_LAMBDA,
64 a_f1,
65 bitwuzla_mk_term3(tm,
66 BITWUZLA_KIND_ITE,
67 bitwuzla_mk_term2(tm, BITWUZLA_KIND_APPLY, f0, a_f1),
68 x0,
69 bvzero));
70
71 // (define-fun f2 ((a Float16)) (_ BitVec 2) ((_ extract 1 0) (f1 a)))
72 BitwuzlaTerm a_f2 = bitwuzla_mk_var(tm, sortfp16, "a_f2");
73 BitwuzlaTerm f2 = bitwuzla_mk_term2(
74 tm,
75 BITWUZLA_KIND_LAMBDA,
76 a_f2,
77 bitwuzla_mk_term1_indexed2(
78 tm,
79 BITWUZLA_KIND_BV_EXTRACT,
80 bitwuzla_mk_term2(tm, BITWUZLA_KIND_APPLY, f1, a_f2),
81 1,
82 0));
83
84 // (assert (! (bvult x2 (f2 (_ +zero 5 11))) :named a0))
85 BitwuzlaTerm a0 = bitwuzla_mk_const(tm, sortbool, "a0");
86 BitwuzlaTerm assumption0 =
87 bitwuzla_mk_term2(tm,
88 BITWUZLA_KIND_BV_ULT,
89 x2,
90 bitwuzla_mk_term2(tm, BITWUZLA_KIND_APPLY, f2, fpzero));
91 bitwuzla_assert(bitwuzla,
92 bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, a0, assumption0));
93 // (assert (! (= x1 x2 x3) :named a1))
94 BitwuzlaTerm a1 = bitwuzla_mk_const(tm, sortbool, "a1");
95 BitwuzlaTerm assumption1 =
96 bitwuzla_mk_term3(tm, BITWUZLA_KIND_EQUAL, x1, x2, x3);
97 bitwuzla_assert(bitwuzla,
98 bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, a1, assumption1));
99 // (assert (!(= x4 ((_ to_fp_unsigned 5 11) RNE x3)) :named a2))
100 BitwuzlaTerm a2 = bitwuzla_mk_const(tm, sortbool, "a2");
101 BitwuzlaTerm assumption2 = bitwuzla_mk_term2(
102 tm,
103 BITWUZLA_KIND_EQUAL,
104 x4,
105 bitwuzla_mk_term2_indexed2(tm,
106 BITWUZLA_KIND_FP_TO_FP_FROM_UBV,
107 bitwuzla_mk_rm_value(tm, BITWUZLA_RM_RNE),
108 x3,
109 5,
110 11));
111 bitwuzla_assert(bitwuzla,
112 bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, a2, assumption2));
113
114 BitwuzlaTerm assumptions[3] = {a0, a1, a2};
115 // (check-sat-assuming (assumption0 assumption1 assumption2))
116 BitwuzlaResult result = bitwuzla_check_sat_assuming(bitwuzla, 3, assumptions);
117 printf("Expect: unsat\n");
118 printf("Bitwuzla: %s\n", bitwuzla_result_to_string(result));
119
120 // (get-unsat-assumptions)
121 size_t unsat_assumptions_size;
122 const BitwuzlaTerm *unsat_assumptions =
123 bitwuzla_get_unsat_assumptions(bitwuzla, &unsat_assumptions_size);
124 printf("Unsat Assumptions: {");
125 for (uint32_t i = 0; i < unsat_assumptions_size; ++i)
126 {
127 printf(" %s", bitwuzla_term_to_string(unsat_assumptions[i]));
128 }
129 printf(" }\n");
130
131 // Finally, delete the Bitwuzla solver, options, and term manager instances.
132 bitwuzla_delete(bitwuzla);
133 bitwuzla_options_delete(options);
134 bitwuzla_term_manager_delete(tm);
135
136 return 0;
137}
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/cpp/bitwuzla.h>
12
13#include <iostream>
14
15using namespace bitwuzla;
16
17int
18main()
19{
20 // First, create a term manager instance.
21 TermManager tm;
22 // Create a Bitwuzla options instance.
23 Options options;
24 // (set-option :produce-unsat-assumptions true)
25 options.set(Option::PRODUCE_UNSAT_ASSUMPTIONS, true);
26 // Then, create a Bitwuzla instance.
27 Bitwuzla bitwuzla(tm, options);
28
29 // Create Boolean sort.
30 Sort sortbool = tm.mk_bool_sort();
31 // Create a bit-vector sort of size 2.
32 Sort sortbv2 = tm.mk_bv_sort(2);
33 // Create a bit-vector sort of size 4.
34 Sort sortbv4 = tm.mk_bv_sort(4);
35 // Create Float16 floatinf-point sort.
36 Sort sortfp16 = tm.mk_fp_sort(5, 11);
37 // Create bit-vector variables.
38 // (declare-const x0 (_ BitVec 4))
39 Term x0 = tm.mk_const(sortbv4, "x0");
40 // (declare-const x1 (_ BitVec 2))
41 Term x1 = tm.mk_const(sortbv2, "x1");
42 // (declare-const x2 (_ BitVec 2))
43 Term x2 = tm.mk_const(sortbv2, "x2");
44 // (declare-const x3 (_ BitVec 2))
45 Term x3 = tm.mk_const(sortbv2, "x3");
46 // (declare-const x4 Float16)
47 Term x4 = tm.mk_const(sortfp16, "x4");
48
49 // Create FP positive zero.
50 Term fpzero = tm.mk_fp_pos_zero(sortfp16);
51 // Create BV zero of size 4.
52 Term bvzero = tm.mk_bv_zero(sortbv4);
53
54 // (define-fun f0 ((a Float16)) Bool (fp.gt a (_ +zero 5 11)))
55 Term a_f0 = tm.mk_var(sortfp16, "a_f0");
56 Term f0 =
57 tm.mk_term(Kind::LAMBDA, {a_f0, tm.mk_term(Kind::FP_GT, {a_f0, fpzero})});
58
59 // (define-fun f1 ((a Float16)) (_ BitVec 4) (ite (f0 a) x0 #b0000))
60 Term a_f1 = tm.mk_var(sortfp16, "a_f1");
61 Term f1 = tm.mk_term(
62 Kind::LAMBDA,
63 {a_f1,
64 tm.mk_term(Kind::ITE,
65 {tm.mk_term(Kind::APPLY, {f0, a_f1}), x0, bvzero})});
66
67 // (define-fun f2 ((a Float16)) (_ BitVec 2) ((_ extract 1 0) (f1 a)))
68 Term a_f2 = tm.mk_var(sortfp16, "a_f2");
69 Term f2 = tm.mk_term(
70 Kind::LAMBDA,
71 {a_f2,
72 tm.mk_term(
73 Kind::BV_EXTRACT, {tm.mk_term(Kind::APPLY, {f1, a_f2})}, {1, 0})});
74
75 // (assert (! (bvult x2 (f2 (_ +zero 5 11))) :named a0))
76 Term a0 = tm.mk_const(sortbool, "a0");
77 Term assumption0 =
78 tm.mk_term(Kind::BV_ULT, {x2, tm.mk_term(Kind::APPLY, {f2, fpzero})});
79 bitwuzla.assert_formula(tm.mk_term(Kind::EQUAL, {a0, assumption0}));
80 // (assert (! (= x1 x2 x3) :named a1))
81 Term a1 = tm.mk_const(sortbool, "a1");
82 Term assumption1 = tm.mk_term(Kind::EQUAL, {x1, x2, x3});
83 bitwuzla.assert_formula(tm.mk_term(Kind::EQUAL, {a1, assumption1}));
84 // (assert (!(= x4 ((_ to_fp_unsigned 5 11) RNE x3)) :named a2))
85 Term a2 = tm.mk_const(sortbool, "a2");
86 Term assumption2 =
87 tm.mk_term(Kind::EQUAL,
88 {x4,
89 tm.mk_term(Kind::FP_TO_FP_FROM_UBV,
90 {tm.mk_rm_value(RoundingMode::RNE), x3},
91 {5, 11})});
92 bitwuzla.assert_formula(tm.mk_term(Kind::EQUAL, {a2, assumption2}));
93
94 // (check-sat-assuming (assumption0 assumption1 assumption2))
95 Result result = bitwuzla.check_sat({a0, a1, a2});
96 std::cout << "Expect: unsat" << std::endl;
97 std::cout << "Bitwuzla: " << result << std::endl;
98
99 // (get-unsat-assumptions)
100 auto unsat_assumptions = bitwuzla.get_unsat_assumptions();
101 std::cout << "Unsat Assumptions: {";
102 for (const auto& a : unsat_assumptions)
103 {
104 std::cout << " " << a;
105 }
106 std::cout << " }" << std::endl;
107
108 return 0;
109}
1###
2# Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3#
4# Copyright (C) 2023 by the authors listed in the AUTHORS file at
5# https:#github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6#
7# This file is part of Bitwuzla under the MIT license. See COPYING for more
8# information at https:#github.com/bitwuzla/bitwuzla/blob/main/COPYING
9##
10
11from bitwuzla import *
12
13if __name__ == '__main__':
14
15 # First, create a term manager instance.
16 tm = TermManager()
17 # Create a Bitwuzla options instance.
18 options = Options()
19 # (set-option :produce-unsat-assumptions true)
20 options.set(Option.PRODUCE_UNSAT_ASSUMPTIONS, True)
21
22 # Then, create a Bitwuzla instance.
23 bitwuzla = Bitwuzla(tm, options)
24
25 # Create Boolean sort.
26 sortbool = tm.mk_bool_sort()
27 # Create a bit-vector sort of size 2.
28 sortbv2 = tm.mk_bv_sort(2)
29 # Create a bit-vector sort of size 4.
30 sortbv4 = tm.mk_bv_sort(4)
31 # Create Float16 floatinf-point sort.
32 sortfp16 = tm.mk_fp_sort(5, 11)
33 # Create bit-vector variables.
34 # (declare-const x0 (_ BitVec 4))
35 x0 = tm.mk_const(sortbv4, 'x0')
36 # (declare-const x1 (_ BitVec 2))
37 x1 = tm.mk_const(sortbv2, 'x1')
38 # (declare-const x2 (_ BitVec 2))
39 x2 = tm.mk_const(sortbv2, 'x2')
40 # (declare-const x3 (_ BitVec 2))
41 x3 = tm.mk_const(sortbv2, 'x3')
42 # (declare-const x4 Float16)
43 x4 = tm.mk_const(sortfp16, 'x4')
44
45 # Create FP positive zero.
46 fpzero = tm.mk_fp_pos_zero(sortfp16)
47 # Create BV zero of size 4.
48 bvzero = tm.mk_bv_zero(sortbv4)
49
50 # (define-fun f0 ((a Float16)) Bool (fp.gt a (_ +zero 5 11)))
51 a_f0 = tm.mk_var(sortfp16, 'a_f0')
52 f0 = tm.mk_term(Kind.LAMBDA, [a_f0, tm.mk_term(Kind.FP_GT, [a_f0, fpzero])])
53
54 # (define-fun f1 ((a Float16)) (_ BitVec 4) (ite (f0 a) x0 #b0000))
55 a_f1 = tm.mk_var(sortfp16, 'a_f1')
56 f1 = tm.mk_term(
57 Kind.LAMBDA,
58 [a_f1,
59 tm.mk_term(Kind.ITE, [tm.mk_term(Kind.APPLY, [f0, a_f1]), x0, bvzero])])
60
61 # (define-fun f2 ((a Float16)) (_ BitVec 2) ((_ extract 1 0) (f1 a)))
62 a_f2 = tm.mk_var(sortfp16, 'a_f2')
63 f2 = tm.mk_term(
64 Kind.LAMBDA,
65 [a_f2,
66 tm.mk_term(Kind.BV_EXTRACT, [tm.mk_term(Kind.APPLY, [f1, a_f2])], [1, 0])])
67
68 # (assert (! (bvult x2 (f2 (_ +zero 5 11))) :named a0))
69 a0 = tm.mk_const(sortbool, 'a0')
70 assumption0 = tm.mk_term(Kind.BV_ULT, [x2, tm.mk_term(Kind.APPLY, [f2, fpzero])])
71 bitwuzla.assert_formula(tm.mk_term(Kind.EQUAL, [a0, assumption0]))
72 # (assert (! (= x1 x2 x3) :named a1))
73 a1 = tm.mk_const(sortbool, 'a1')
74 assumption1 = tm.mk_term(Kind.EQUAL, [x1, x2, x3])
75 bitwuzla.assert_formula(tm.mk_term(Kind.EQUAL, [a1, assumption1]))
76 # (assert (!(= x4 ((_ to_fp_unsigned 5 11) RNE x3)) :named a2))
77 a2 = tm.mk_const(sortbool, 'a2')
78 assumption2 = tm.mk_term(Kind.EQUAL,
79 [x4,
80 tm.mk_term(Kind.FP_TO_FP_FROM_UBV,
81 [tm.mk_rm_value(RoundingMode.RNE), x3],
82 [5, 11])])
83 bitwuzla.assert_formula(tm.mk_term(Kind.EQUAL, [a2, assumption2]))
84
85 # (check-sat-assuming (assumption0 assumption1 assumption2))
86 result = bitwuzla.check_sat(a0, a1, a2)
87 print('Expect: unsat')
88 print(f'Bitwuzla: {result}')
89
90 # (get-unsat-assumptions)
91 unsat_assumptions = bitwuzla.get_unsat_assumptions()
92 print('Unsat Assumptions: {', end = '')
93 for a in unsat_assumptions:
94 print(f' {a}', end = '')
95 print(' }')
1(set-logic ALL)
2(set-option :produce-unsat-assumptions true)
3(declare-const x0 (_ BitVec 4))
4(declare-const x1 (_ BitVec 2))
5(declare-const x2 (_ BitVec 2))
6(declare-const x3 (_ BitVec 2))
7(declare-const x4 Float16)
8(define-fun f0 ((a Float16)) Bool (fp.gt a (_ +zero 5 11)))
9(define-fun f1 ((a Float16)) (_ BitVec 4) (ite (f0 a) x0 #b0000))
10(define-fun f2 ((a Float16)) (_ BitVec 2) ((_ extract 1 0) (f1 a)))
11(check-sat-assuming
12 (
13 (! (bvult x2 (f2 (_ +zero 5 11))) :named a0)
14 (! (= x1 x2 x3) :named a1)
15 (! (= x4 ((_ to_fp_unsigned 5 11) RNE x3)) :named a2)
16 )
17)
18(get-unsat-assumptions)
Reset Example
reset
). 1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/c/bitwuzla.h>
12#include <stdio.h>
13
14int
15main()
16{
17 BitwuzlaResult result;
18
19 // First, create a term manager instance.
20 BitwuzlaTermManager* tm = bitwuzla_term_manager_new();
21 // Create a Bitwuzla options instance.
22 BitwuzlaOptions* options = bitwuzla_options_new();
23 // (set-option :produce-models false)
24 bitwuzla_set_option(options, BITWUZLA_OPT_PRODUCE_MODELS, 0);
25
26 // Then, create a Bitwuzla instance.
27 Bitwuzla* bitwuzla = bitwuzla_new(tm, options);
28
29 // Create a bit-vector sort of size 3.
30 BitwuzlaSort sortbv3 = bitwuzla_mk_bv_sort(tm, 3);
31
32 // (declare-const x (_ BitVec 3))
33 BitwuzlaTerm x = bitwuzla_mk_const(tm, sortbv3, "x");
34
35 // (assert (= x #b010))
36 bitwuzla_assert(
37 bitwuzla,
38 bitwuzla_mk_term2(tm,
39 BITWUZLA_KIND_EQUAL,
40 x,
41 bitwuzla_mk_bv_value_uint64(tm, sortbv3, 2)));
42 // (check-sat)
43 result = bitwuzla_check_sat(bitwuzla);
44 printf("Expect: sat\n");
45 printf("Bitwuzla: %s\n", bitwuzla_result_to_string(result));
46
47 bitwuzla_delete(bitwuzla);
48
49 // (set-option :produce-models true)
50 bitwuzla_set_option(options, BITWUZLA_OPT_PRODUCE_MODELS, true);
51
52 // (reset)
53 // Note: Bitwuzla does not provide an explicit API function for reset since
54 // this is achieved by simply discarding the current Bitwuzla instance
55 // and creating a new one.
56 bitwuzla = bitwuzla_new(tm, options);
57
58 // (declare-const a ( Array (_ BitVec 2) (_ BitVec 3)))
59 BitwuzlaSort sortbv2 = bitwuzla_mk_bv_sort(tm, 2);
60 BitwuzlaTerm a =
61 bitwuzla_mk_const(tm, bitwuzla_mk_array_sort(tm, sortbv2, sortbv3), "a");
62
63 // (assert (= x #b011))
64 bitwuzla_assert(
65 bitwuzla,
66 bitwuzla_mk_term2(tm,
67 BITWUZLA_KIND_EQUAL,
68 x,
69 bitwuzla_mk_bv_value_uint64(tm, sortbv3, 3)));
70 // (assert (= x (select a #b01)))
71 bitwuzla_assert(
72 bitwuzla,
73 bitwuzla_mk_term2(
74 tm,
75 BITWUZLA_KIND_EQUAL,
76 x,
77 bitwuzla_mk_term2(tm,
78 BITWUZLA_KIND_ARRAY_SELECT,
79 a,
80 bitwuzla_mk_bv_value_uint64(tm, sortbv2, 1))));
81 // (check-sat)
82 result = bitwuzla_check_sat(bitwuzla);
83 printf("Expect: sat\n");
84 printf("Bitwuzla: %s\n", bitwuzla_result_to_string(result));
85 // (get-model)
86 printf("(\n");
87 printf(" (define-fun %s () ", bitwuzla_term_get_symbol(x));
88 printf("%s ", bitwuzla_sort_to_string(bitwuzla_term_get_sort(x)));
89 printf("%s)\n", bitwuzla_term_to_string(bitwuzla_get_value(bitwuzla, x)));
90 printf(" (define-fun %s () ", bitwuzla_term_get_symbol(a));
91 printf("%s ", bitwuzla_sort_to_string(bitwuzla_term_get_sort(a)));
92 printf("%s)\n", bitwuzla_term_to_string(bitwuzla_get_value(bitwuzla, a)));
93 printf(")\n");
94
95 // Finally, delete the Bitwuzla solver, options, and term manager instances.
96 bitwuzla_delete(bitwuzla);
97 bitwuzla_options_delete(options);
98 bitwuzla_term_manager_delete(tm);
99
100 return 0;
101}
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/cpp/bitwuzla.h>
12
13#include <iostream>
14
15using namespace bitwuzla;
16
17int
18main()
19{
20 Result result;
21
22 // First, create a term manager instance.
23 TermManager tm;
24 // Create a Bitwuzla options instance.
25 Options options;
26 // (set-option :produce-models false)
27 options.set(Option::PRODUCE_MODELS, false);
28
29 // Then, create a Bitwuzla instance.
30 std::unique_ptr<Bitwuzla> bitwuzla(new Bitwuzla(tm, options));
31
32 // Create a bit-vector sort of size 3.
33 Sort sortbv3 = tm.mk_bv_sort(3);
34
35 // (declare-const x (_ BitVec 3))
36 Term x = tm.mk_const(sortbv3, "x");
37
38 // (assert (= x #b010))
39 bitwuzla->assert_formula(
40 tm.mk_term(Kind::EQUAL, {x, tm.mk_bv_value_uint64(sortbv3, 2)}));
41 // (check-sat)
42 result = bitwuzla->check_sat();
43 std::cout << "Expect: sat" << std::endl;
44 std::cout << "Bitwuzla: " << result << std::endl;
45
46 // (set-option :produce-models true)
47 options.set(Option::PRODUCE_MODELS, true);
48
49 // (reset)
50 // Note: Bitwuzla does not provide an explicit API function for reset since
51 // this is achieved by simply discarding the current Bitwuzla instance
52 // and creating a new one.
53 bitwuzla.reset(new Bitwuzla(tm, options));
54
55 // (declare-const a ( Array (_ BitVec 2) (_ BitVec 3)))
56 Sort sortbv2 = tm.mk_bv_sort(2);
57 Term a = tm.mk_const(tm.mk_array_sort(sortbv2, sortbv3), "a");
58
59 // (assert (= x #b011))
60 bitwuzla->assert_formula(
61 tm.mk_term(Kind::EQUAL, {x, tm.mk_bv_value_uint64(sortbv3, 3)}));
62 // (assert (= x (select a #b01)))
63 bitwuzla->assert_formula(
64 tm.mk_term(Kind::EQUAL,
65 {x,
66 tm.mk_term(Kind::ARRAY_SELECT,
67 {a, tm.mk_bv_value_uint64(sortbv2, 1)})}));
68 // (check-sat)
69 result = bitwuzla->check_sat();
70 std::cout << "Expect: sat" << std::endl;
71 std::cout << "Bitwuzla: " << result << std::endl;
72 // (get-model)
73 std::cout << "(" << std::endl
74 << " (define-fun " << x.symbol()->get() << " () " << x.sort()
75 << " " << bitwuzla->get_value(x) << ")" << std::endl
76 << " (define-fun " << a.symbol()->get() << " () " << a.sort()
77 << " " << bitwuzla->get_value(a) << ")" << std::endl
78 << ")" << std::endl;
79
80 return 0;
81}
1###
2# Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3#
4# Copyright (C) 2023 by the authors listed in the AUTHORS file at
5# https:#github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6#
7# This file is part of Bitwuzla under the MIT license. See COPYING for more
8# information at https:#github.com/bitwuzla/bitwuzla/blob/main/COPYING
9##
10
11from bitwuzla import *
12
13if __name__ == '__main__':
14
15 # First, create a term manager instance.
16 tm = TermManager()
17 # Create a Bitwuzla options instance.
18 options = Options()
19 # (set-option :produce-models true)
20 options.set(Option.PRODUCE_MODELS, False)
21
22 # Then, create a Bitwuzla instance.
23 bitwuzla = Bitwuzla(tm, options)
24
25 # Create a bit-vector sort of size 3.
26 sortbv3 = tm.mk_bv_sort(3)
27
28 # (declare-const x (_ BitVec 3))
29 x = tm.mk_const(sortbv3, 'x')
30
31 # (assert (= x #b010))
32 bitwuzla.assert_formula(
33 tm.mk_term(Kind.EQUAL, [x, tm.mk_bv_value(sortbv3, 2)]))
34 # (check-sat)
35 result = bitwuzla.check_sat()
36 print('Expect: sat')
37 print(f'Bitwuzla: {result}')
38
39 # (set-option :produce-models true)
40 options.set(Option.PRODUCE_MODELS, True)
41
42 # (reset)
43 # Note: Bitwuzla does not provide an explicit API function for reset since
44 # this is achieved by simply discarding the current Bitwuzla instance
45 # and creating a new one.
46 bitwuzla = Bitwuzla(tm, options)
47
48 # (declare-const a ( Array (_ BitVec 2) (_ BitVec 3)))
49 sortbv2 = tm.mk_bv_sort(2)
50 a = tm.mk_const(tm.mk_array_sort(sortbv2, sortbv3), 'a')
51
52 # (assert (= x #b011))
53 bitwuzla.assert_formula(
54 tm.mk_term(Kind.EQUAL, [x, tm.mk_bv_value(sortbv3, 3)]))
55 # (assert (= x (select a #b01)))
56 bitwuzla.assert_formula(tm.mk_term(
57 Kind.EQUAL,
58 [x, tm.mk_term(Kind.ARRAY_SELECT, [a, tm.mk_bv_value(sortbv2, 1)])]))
59 # (check-sat)
60 result = bitwuzla.check_sat()
61 print('Expect: sat')
62 print(f'Bitwuzla: {result}')
63 # (get-model)
64 print('(')
65 print(f' (define-fun {x.symbol()} () {x.sort()} {bitwuzla.get_value(x)} )')
66 print(f' (define-fun {a.symbol()} () {a.sort()} {bitwuzla.get_value(a)} )')
67 print(')')
1(set-logic QF_BV)
2(set-option :produce-models false)
3(declare-const x (_ BitVec 3))
4(assert (= x #b010))
5(check-sat) ; expect sat
6
7(reset)
8
9(set-logic QF_ABV)
10(set-option :produce-models true)
11(declare-const x (_ BitVec 3))
12(declare-const a ( Array (_ BitVec 2) (_ BitVec 3)))
13(assert (= x #b011))
14(assert (= x (select a #b01)))
15(check-sat) ; expect sat
16(get-model)
Reset Assertions Example
reset-assertions
). 1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/c/bitwuzla.h>
12#include <stdio.h>
13
14int
15main()
16{
17 BitwuzlaResult result;
18
19 // First, create a term manager instance.
20 BitwuzlaTermManager* tm = bitwuzla_term_manager_new();
21 // Create a Bitwuzla options instance.
22 BitwuzlaOptions* options = bitwuzla_options_new();
23 // (set-option :produce-models true)
24 bitwuzla_set_option(options, BITWUZLA_OPT_PRODUCE_MODELS, 1);
25
26 // Then, create a Bitwuzla instance.
27 Bitwuzla* bitwuzla = bitwuzla_new(tm, options);
28
29 // Create a bit-vector sort of size 3.
30 BitwuzlaSort sortbv3 = bitwuzla_mk_bv_sort(tm, 3);
31
32 // (declare-const x (_ BitVec 3))
33 BitwuzlaTerm x = bitwuzla_mk_const(tm, sortbv3, "x");
34
35 // (assert (= x #b010))
36 bitwuzla_assert(
37 bitwuzla,
38 bitwuzla_mk_term2(tm,
39 BITWUZLA_KIND_EQUAL,
40 x,
41 bitwuzla_mk_bv_value_uint64(tm, sortbv3, 2)));
42 // (check-sat)
43 result = bitwuzla_check_sat(bitwuzla);
44 printf("Expect: sat\n");
45 printf("Bitwuzla: %s\n", bitwuzla_result_to_string(result));
46 // (assert (= x #b001))
47 bitwuzla_assert(
48 bitwuzla,
49 bitwuzla_mk_term2(tm,
50 BITWUZLA_KIND_EQUAL,
51 x,
52 bitwuzla_mk_bv_value_uint64(tm, sortbv3, 1)));
53 // (check-sat)
54 result = bitwuzla_check_sat(bitwuzla);
55 printf("Expect: unsat\n");
56 printf("Bitwuzla: %s\n", bitwuzla_result_to_string(result));
57
58 // (reset-assertions)
59 // Note: Bitwuzla does not provide an explicit API function for
60 // reset-assertions since this is achieved by simply discarding
61 // the current Bitwuzla instance and creating a new one.
62 bitwuzla_delete(bitwuzla);
63 bitwuzla = bitwuzla_new(tm, options);
64
65 // (assert (= x #b011))
66 bitwuzla_assert(
67 bitwuzla,
68 bitwuzla_mk_term2(tm,
69 BITWUZLA_KIND_EQUAL,
70 x,
71 bitwuzla_mk_bv_value_uint64(tm, sortbv3, 3)));
72 // (check-sat)
73 result = bitwuzla_check_sat(bitwuzla);
74 printf("Expect: sat\n");
75 printf("Bitwuzla: %s\n", bitwuzla_result_to_string(result));
76 // (get-model)
77 printf("(\n");
78 printf(" (define-fun %s", bitwuzla_term_get_symbol(x));
79 printf(" () %s", bitwuzla_sort_to_string(bitwuzla_term_get_sort(x)));
80 printf(" %s)\n", bitwuzla_term_to_string(bitwuzla_get_value(bitwuzla, x)));
81 printf(")\n");
82
83 // Finally, delete the Bitwuzla solver, options, and term manager instances.
84 bitwuzla_delete(bitwuzla);
85 bitwuzla_options_delete(options);
86 bitwuzla_term_manager_delete(tm);
87
88 return 0;
89}
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/cpp/bitwuzla.h>
12
13#include <iostream>
14
15using namespace bitwuzla;
16
17int
18main()
19{
20 Result result;
21
22 // First, create a term manager instance.
23 TermManager tm;
24 // Create a Bitwuzla options instance.
25 Options options;
26 // (set-option :produce-models true)
27 options.set(Option::PRODUCE_MODELS, true);
28 // Then, create a Bitwuzla instance.
29 std::unique_ptr<Bitwuzla> bitwuzla(new Bitwuzla(tm, options));
30
31 // Create a bit-vector sort of size 3.
32 Sort sortbv3 = tm.mk_bv_sort(3);
33
34 // (declare-const x (_ BitVec 3))
35 Term x = tm.mk_const(sortbv3, "x");
36
37 // (assert (= x #b010))
38 bitwuzla->assert_formula(
39 tm.mk_term(Kind::EQUAL, {x, tm.mk_bv_value_uint64(sortbv3, 2)}));
40 // (check-sat)
41 result = bitwuzla->check_sat();
42 std::cout << "Expect: sat" << std::endl;
43 std::cout << "Bitwuzla: " << result << std::endl;
44
45 // (assert (= x #b001))
46 bitwuzla->assert_formula(
47 tm.mk_term(Kind::EQUAL, {x, tm.mk_bv_value_uint64(sortbv3, 1)}));
48 // (check-sat)
49 result = bitwuzla->check_sat();
50 std::cout << "Expect: unsat" << std::endl;
51 std::cout << "Bitwuzla: " << result << std::endl;
52
53 // (reset-assertions)
54 // Note: Bitwuzla does not provide an explicit API function for
55 // reset-assertions since this is achieved by simply discarding
56 // the current Bitwuzla instance and creating a new one.
57 bitwuzla.reset(new Bitwuzla(tm, options));
58
59 // (assert (= x #b011))
60 bitwuzla->assert_formula(
61 tm.mk_term(Kind::EQUAL, {x, tm.mk_bv_value_uint64(sortbv3, 3)}));
62 // (check-sat)
63 result = bitwuzla->check_sat();
64 std::cout << "Expect: sat" << std::endl;
65 std::cout << "Bitwuzla: " << result << std::endl;
66
67 // (get-model)
68 std::cout << "(" << std::endl
69 << " (define-fun " << x.symbol()->get() << " () " << x.sort()
70 << " " << bitwuzla->get_value(x) << ")" << std::endl
71 << ")" << std::endl;
72
73 return 0;
74}
1###
2# Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3#
4# Copyright (C) 2023 by the authors listed in the AUTHORS file at
5# https:#github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6#
7# This file is part of Bitwuzla under the MIT license. See COPYING for more
8# information at https:#github.com/bitwuzla/bitwuzla/blob/main/COPYING
9##
10
11from bitwuzla import *
12
13if __name__ == '__main__':
14
15 # First, create a term manager instance.
16 tm = TermManager()
17 # Create a Bitwuzla options instance.
18 options = Options()
19 # (set-option :produce-models true)
20 options.set(Option.PRODUCE_MODELS, True)
21
22 # Then, create a Bitwuzla instance.
23 bitwuzla = Bitwuzla(tm, options)
24
25 # Create a bit-vector sort of size 3.
26 sortbv3 = tm.mk_bv_sort(3)
27
28 # (declare-const x (_ BitVec 3))
29 x = tm.mk_const(sortbv3, 'x')
30
31 # (assert (= x #b010))
32 bitwuzla.assert_formula(
33 tm.mk_term(Kind.EQUAL, [x, tm.mk_bv_value(sortbv3, 2)]))
34 # (check-sat)
35 result = bitwuzla.check_sat()
36 print('Expect: sat')
37 print(f'Bitwuzla: {result}')
38
39 # (assert (= x #b001))
40 bitwuzla.assert_formula(
41 tm.mk_term(Kind.EQUAL, [x, tm.mk_bv_value(sortbv3, 1)]))
42 # (check-sat)
43 result = bitwuzla.check_sat()
44 print('Expect: unsat')
45 print(f'Bitwuzla: {result}')
46
47 # (reset-assertions)
48 # Note: Bitwuzla does not provide an explicit API function for
49 # reset-assertions since this is achieved by simply discarding
50 # the current Bitwuzla instance and creating a new one.
51 bitwuzla = Bitwuzla(tm, options)
52
53 # (assert (= x #b011))
54 bitwuzla.assert_formula(
55 tm.mk_term(Kind.EQUAL, [x, tm.mk_bv_value(sortbv3, 3)]))
56 # (check-sat)
57 result = bitwuzla.check_sat()
58 print('Expect: sat')
59 print(f'Bitwuzla: {result}')
60
61 # (get-model)
62 print(f'(\n (define-fun {x.symbol()} () {x.sort()} {bitwuzla.get_value(x)} )\n)')
63
1(set-logic QF_BV)
2(set-option :global-declarations true)
3(set-option :produce-models true)
4(declare-const x (_ BitVec 3))
5(assert (= x #b010))
6(check-sat) ; expect sat
7(assert (= x #b001))
8(check-sat) ; expect unsat
9
10(reset-assertions)
11
12(assert (= x #b011))
13(check-sat) ; expect sat
14(get-model)
Parsing Example
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <assert.h>
12#include <bitwuzla/c/bitwuzla.h>
13#include <bitwuzla/c/parser.h>
14#include <stdlib.h>
15
16int
17main()
18{
19 // First, create a term manager instance.
20 BitwuzlaTermManager* tm = bitwuzla_term_manager_new();
21 // Create a Bitwuzla options instance.
22 BitwuzlaOptions* options = bitwuzla_options_new();
23
24 // We will parse example file `smt2/quickstart.smt2`.
25 // Create parser instance.
26 const char* infile_name = "../smt2/quickstart.smt2";
27 BitwuzlaParser* parser =
28 bitwuzla_parser_new(tm, options, "smt2", 2, "<stdout>");
29
30 // Now parse the input file.
31 const char* error_msg;
32 printf("Expect: sat\n");
33 printf("Bitwuzla: ");
34 bitwuzla_parser_parse(parser, infile_name, false, true, &error_msg);
35 // We expect no error to occur.
36 assert(!error_msg);
37
38 // Now we retrieve the set of asserted formulas and print them.
39 size_t size;
40 const BitwuzlaTerm* assertions =
41 bitwuzla_get_assertions(bitwuzla_parser_get_bitwuzla(parser), &size);
42 printf("Assertions:\n");
43 printf("{\n");
44 for (size_t i = 0; i < size; ++i)
45 {
46 printf(" %s\n", bitwuzla_term_to_string(assertions[i]));
47 }
48 printf("}\n");
49
50 // Now we add an assertion via parsing from string.
51 bitwuzla_parser_parse(
52 parser, "(assert (distinct (select a x) y))", true, false, &error_msg);
53 // We expect no error to occur.
54 assert(!error_msg);
55 // Now the formula is unsat.
56 BitwuzlaResult result =
57 bitwuzla_check_sat(bitwuzla_parser_get_bitwuzla(parser));
58
59 printf("Expect: unsat\n");
60 printf("Bitwuzla: %s\n\n", bitwuzla_result_to_string(result));
61
62 // For illustration purposes, we now parse in some declarations and terms
63 // and sorts from string.
64
65 // Declare bit-vector sort of size 16.
66 BitwuzlaSort bv16 =
67 bitwuzla_parser_parse_sort(parser, "(_ BitVec 16)", &error_msg);
68 // We expect no error to occur.
69 assert(!error_msg);
70 // Create bit-vector sort of size 16 and show that it corresponds to
71 // its string representation '(_ BitVec16)'.
72 assert(bv16 == bitwuzla_mk_bv_sort(tm, 16));
73
74 // Declare Boolean constants 'c' and 'd'.
75 // Note: Declarations are commands (not terms) in the SMT-LIB language.
76 // Commands must be parsed in via bitwuzla_parser_parse(),
77 // bitwuzla_parser_parse_term() only supports parsing SMT-LIB terms.
78 bitwuzla_parser_parse(parser,
79 "(declare-const c Bool)(declare-const d Bool)",
80 true,
81 false,
82 &error_msg);
83 // Declare bit-vector constant 'b'.
84 bitwuzla_parser_parse(
85 parser, "(declare-const b (_ BitVec 16))", true, false, &error_msg);
86 // We expect no error to occur.
87 assert(!error_msg);
88 // Retrieve term representing 'b'.
89 BitwuzlaTerm b = bitwuzla_parser_parse_term(parser, "b", &error_msg);
90 // We expect no error to occur.
91 assert(!error_msg);
92 // Retrieve term representing 'c'.
93 BitwuzlaTerm c = bitwuzla_parser_parse_term(parser, "c", &error_msg);
94 // We expect no error to occur.
95 assert(!error_msg);
96 // Retrieve term representing 'd'.
97 BitwuzlaTerm d = bitwuzla_parser_parse_term(parser, "d", &error_msg);
98 // We expect no error to occur.
99 assert(!error_msg);
100 // Create xor over 'a' and 'c' and show that it corresponds to term
101 // parsed in from its string representation '(xor c d)'.
102 assert(bitwuzla_parser_parse_term(parser, "(xor c d)", &error_msg)
103 == bitwuzla_mk_term2(tm, BITWUZLA_KIND_XOR, c, d));
104 // Create bit-vector addition over 'b' and bit-vector value
105 // '1011111010001010' and show that it corresponds to the term parsed in
106 // from its string representation '(bvadd b #b1011111010001010)'.
107 assert(bitwuzla_parser_parse_term(
108 parser, "(bvadd b #b1011111010001010)", &error_msg)
109 == bitwuzla_mk_term2(
110 tm,
111 BITWUZLA_KIND_BV_ADD,
112 b,
113 bitwuzla_mk_bv_value(tm, bv16, "1011111010001010", 2)));
114 // Finally, delete Bitwuzla parser, options, and term manager instance.
115 bitwuzla_parser_delete(parser);
116 bitwuzla_options_delete(options);
117 bitwuzla_term_manager_delete(tm);
118
119 return 0;
120}
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/cpp/bitwuzla.h>
12#include <bitwuzla/cpp/parser.h>
13
14#include <cassert>
15#include <iostream>
16
17using namespace bitwuzla;
18
19int
20main()
21{
22 // First, create a term manager instance.
23 TermManager tm;
24 // Create a Bitwuzla options instance.
25 Options options;
26
27 // We will parse example file `smt2/quickstart.smt2`.
28 // Create parser instance.
29 parser::Parser parser(tm, options);
30
31 try
32 {
33 // Now parse the input file.
34 std::cout << "Expect: sat" << std::endl;
35 std::cout << "Bitwuzla: ";
36 parser.parse("../smt2/quickstart.smt2");
37
38 // Now we retrieve the set of asserted formulas and print them.
39 auto assertions = parser.bitwuzla()->get_assertions();
40 std::cout << "Assertions:" << std::endl << "{" << std::endl;
41 for (const auto& a : assertions)
42 {
43 std::cout << " " << a << std::endl;
44 }
45 std::cout << "}" << std::endl;
46
47 // Now we add an assertion via parsing from string.
48 parser.parse("(assert (distinct (select a x) y))", true, false);
49 // Now the formula is unsat.
50 Result result = parser.bitwuzla()->check_sat();
51
52 std::cout << "Expect: unsat" << std::endl;
53 std::cout << "Bitwuzla: " << result << std::endl;
54
55 // For illustration purposes, we now parse in some declarations and terms
56 // and sorts from string.
57
58 // Declare bit-vector sort of size 16.
59 Sort bv16 = parser.parse_sort("(_ BitVec 16)");
60 // Create bit-vector sort of size 16 and show that it corresponds to
61 // its string representation '(_ BitVec16)'.
62 assert(bv16 == tm.mk_bv_sort(16));
63
64 // Declare Boolean constants 'c' and 'd'.
65 // Note: Declarations are commands (not terms) in the SMT-LIB language.
66 // Commands must be parsed in via Parser::parse(),
67 // Parser::parse_term() only supports parsing SMT-LIB terms.
68 parser.parse("(declare-const c Bool)(declare-const d Bool)", true, false);
69 // Declare bit-vector constant 'b'.
70 parser.parse("(declare-const b (_ BitVec 16))", true, false);
71 // Retrieve term representing 'b'.
72 Term b = parser.parse_term("b");
73 // Retrieve term representing 'c'.
74 Term c = parser.parse_term("c");
75 // Retrieve term representing 'd'.
76 Term d = parser.parse_term("d");
77 // Create xor over 'c' and 'd' and show that it corresponds to term
78 // parsed in from its string representation '(xor c d)'.
79 assert(parser.parse_term("(xor c d)") == tm.mk_term(Kind::XOR, {c, d}));
80 // Create bit-vector addition over 'b' and bit-vector value
81 // '1011111010001010' and show that it corresponds to the term parsed in
82 // from its string representation '(bvadd b #b1011111010001010)'.
83 assert(parser.parse_term("(bvadd b #b1011111010001010)")
84 == tm.mk_term(Kind::BV_ADD,
85 {b, tm.mk_bv_value(bv16, "1011111010001010", 2)}));
86 }
87 catch (bitwuzla::parser::Exception& e)
88 {
89 // We expect no error to occur.
90 std::cout << "unexpected parser exception" << std::endl;
91 }
92 return 0;
93}
1###
2# Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3#
4# Copyright (C) 2023 by the authors listed in the AUTHORS file at
5# https:#github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6#
7# This file is part of Bitwuzla under the MIT license. See COPYING for more
8# information at https:#github.com/bitwuzla/bitwuzla/blob/main/COPYING
9##
10
11import sys
12
13from bitwuzla import *
14
15if __name__ == '__main__':
16
17 # First, create a term manager instance.
18 tm = TermManager()
19 # Create a Bitwuzla options instance.
20 options = Options()
21
22 # We will parse example file `smt2/quickstart.smt2`.
23 # Create parser instance.
24 parser = Parser(tm, options)
25
26 # Now parse the input file.
27 print('Expect: sat')
28 print('Bitwuzla: ', end='')
29 sys.stdout.flush()
30 res = parser.parse("../smt2/quickstart.smt2")
31 # We expect no error to occur.
32 assert not res
33
34 # Now we retrieve the set of asserted formulas and print them.
35 assertions = parser.bitwuzla().get_assertions()
36 print('Assertions:')
37 print('{')
38 for a in assertions:
39 print(f' {a}')
40 print('}')
41
42 # Now we add an assertion via parsing from string.
43 parser.parse('(assert (distinct (select a x) y))', True, False)
44 # Now the formula is unsat.
45 print('Expect: unsat')
46 print(f'Bitwuzla: {parser.bitwuzla().check_sat()}')
47
48 # For illustration purposes, we now parse in some declarations and terms
49 # and sorts from string.
50
51 # Declare bit-vector sort of size 16.
52 bv16 = parser.parse_sort('(_ BitVec 16)')
53 # Create bit-vector sort of size 16 and show that it corresponds to
54 # its string representation '(_ BitVec16)'.
55 assert bv16 == tm.mk_bv_sort(16)
56
57 # Declare Boolean constants 'c' and 'd'.
58 # Note: Declarations are commands (not terms) in the SMT-LIB language.
59 # Commands must be parsed in via Parser.parse(),
60 # Parser::parse_term() only supports parsing SMT-LIB terms.
61 parser.parse("(declare-const c Bool)(declare-const d Bool)", True, False)
62 # Declare bit-vector constant 'b'.
63 parser.parse('(declare-const b (_ BitVec 16))', True, False)
64 # Retrieve term representing 'b'.
65 b = parser.parse_term('b')
66 # Retrieve term representing 'c'.
67 c = parser.parse_term('c')
68 # Retrieve term representing 'd'.
69 d = parser.parse_term('d')
70 # Create xor over 'c' and 'd' and show that it corresponds to term
71 # parsed in from its string representation '(xor c d)'.
72 assert parser.parse_term('(xor c d)') == tm.mk_term(Kind.XOR, [c, d])
73 # Create bit-vector addition over 'b' and bit-vector value
74 # '1011111010001010' and show that it corresponds to the term parsed in
75 # from its string representation '(bvadd b #b1011111010001010)'.
76 assert parser.parse_term('(bvadd b #b1011111010001010)') \
77 == tm.mk_term(Kind.BV_ADD,
78 [b, tm.mk_bv_value(bv16, '1011111010001010', 2)])
Printing Example
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <assert.h>
12#include <bitwuzla/c/bitwuzla.h>
13#include <stdio.h>
14
15int
16main()
17{
18 // First, create a term manager instance.
19 BitwuzlaTermManager* tm = bitwuzla_term_manager_new();
20 // Create a Bitwuzla options instance.
21 BitwuzlaOptions* options = bitwuzla_options_new();
22 bitwuzla_set_option(options, BITWUZLA_OPT_PRODUCE_MODELS, 1);
23 // Then, create a Bitwuzla instance.
24 Bitwuzla* bitwuzla = bitwuzla_new(tm, options);
25 // Create some sorts.
26 BitwuzlaSort bv8 = bitwuzla_mk_bv_sort(tm, 8);
27 BitwuzlaSort bv32 = bitwuzla_mk_bv_sort(tm, 32);
28 BitwuzlaSort fp16 = bitwuzla_mk_fp_sort(tm, 5, 11);
29 BitwuzlaSort fun_domain[3] = {bv8, fp16, bv32};
30 BitwuzlaSort fun_sort = bitwuzla_mk_fun_sort(tm, 3, fun_domain, fp16);
31 // Create terms.
32 BitwuzlaTerm b = bitwuzla_mk_const(tm, bitwuzla_mk_bool_sort(tm), "b");
33 BitwuzlaTerm bv = bitwuzla_mk_const(tm, bv8, "bv");
34 BitwuzlaTerm fp = bitwuzla_mk_const(tm, fp16, "fp");
35 BitwuzlaTerm rm = bitwuzla_mk_const(tm, bitwuzla_mk_rm_sort(tm), "rm");
36 BitwuzlaTerm fun = bitwuzla_mk_const(tm, fun_sort, "fun");
37 BitwuzlaTerm zero = bitwuzla_mk_bv_zero(tm, bv8);
38 BitwuzlaTerm ones = bitwuzla_mk_bv_ones(tm, bitwuzla_mk_bv_sort(tm, 23));
39 BitwuzlaTerm z = bitwuzla_mk_var(tm, bv8, "z");
40 BitwuzlaTerm q = bitwuzla_mk_var(tm, bv8, "q");
41 BitwuzlaTerm lambda =
42 bitwuzla_mk_term2(tm,
43 BITWUZLA_KIND_LAMBDA,
44 z,
45 bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_ADD, z, bv));
46 BitwuzlaTerm args[4] = {
47 fun,
48 bv,
49 fp,
50 bitwuzla_mk_term1_indexed1(tm, BITWUZLA_KIND_BV_ZERO_EXTEND, ones, 9)};
51 BitwuzlaTerm fpleq =
52 bitwuzla_mk_term2(tm,
53 BITWUZLA_KIND_FP_LEQ,
54 bitwuzla_mk_term(tm, BITWUZLA_KIND_APPLY, 4, args),
55 fp);
56 BitwuzlaTerm exists = bitwuzla_mk_term2(
57 tm,
58 BITWUZLA_KIND_EXISTS,
59 q,
60 bitwuzla_mk_term2(tm,
61 BITWUZLA_KIND_EQUAL,
62 zero,
63 bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_MUL, bv, q)));
64 // Assert formulas.
65 bitwuzla_assert(bitwuzla, b);
66 bitwuzla_assert(
67 bitwuzla,
68 bitwuzla_mk_term2(tm,
69 BITWUZLA_KIND_EQUAL,
70 bitwuzla_mk_term2(tm, BITWUZLA_KIND_APPLY, lambda, bv),
71 zero));
72 bitwuzla_assert(bitwuzla, exists);
73 bitwuzla_assert(bitwuzla, fpleq);
74
75 // Print sort.
76 printf("Print bit-vector sort of size 32:\n");
77 printf("---------------------------------\n");
78 printf("bitwuzla_sort_print(): ");
79 bitwuzla_sort_print(bv32, stdout);
80 printf("\n");
81 printf("bitwuzla_sort_to_string(): %s\n\n", bitwuzla_sort_to_string(bv32));
82
83 // Print terms.
84 // Note: Hexadecimal bv output format is ignored if the value is not of size
85 // divisible by 4.
86 printf("Print term:\n");
87 printf("-----------\n");
88 printf("bitwuzla_term_print(): ");
89 bitwuzla_term_print(rm, stdout);
90 printf("\n");
91 printf("bitwuzla_term_print_fmt() [dec (ignored)]: ");
92 bitwuzla_term_print_fmt(rm, stdout, 10);
93 printf("\n");
94 printf("bitwuzla_term_to_string(): %s\n",
95 bitwuzla_term_to_string(rm));
96 printf("bitwuzla_term_to_string_fmt() [hex (ignored)]: %s\n",
97 bitwuzla_term_to_string_fmt(rm, 10));
98 printf("\n");
99 printf("bitwuzla_term_print() [bin]: ");
100 bitwuzla_term_print(zero, stdout);
101 printf("\n");
102 printf("bitwuzla_term_print_fmt() [dec]: ");
103 bitwuzla_term_print_fmt(zero, stdout, 10);
104 printf("\n");
105 printf("bitwuzla_term_print_fmt() [hex]: ");
106 bitwuzla_term_print_fmt(zero, stdout, 16);
107 printf("\n");
108 printf("bitwuzla_term_to_string() [bin]: %s\n",
109 bitwuzla_term_to_string(zero));
110 printf("bitwuzla_term_to_string_fmt() [dec]: %s\n",
111 bitwuzla_term_to_string_fmt(zero, 10));
112 printf("bitwuzla_term_to_string_fmt() [hex]: %s\n",
113 bitwuzla_term_to_string_fmt(zero, 16));
114 printf("\n");
115 printf("bitwuzla_term_print_fmt() [bin]: ");
116 bitwuzla_term_print_fmt(fpleq, stdout, 2);
117 printf("\n");
118 printf("bitwuzla_term_print_fmt() [dec]: ");
119 bitwuzla_term_print_fmt(fpleq, stdout, 10);
120 printf("\n");
121 printf("bitwuzla_term_print_fmt() [hex (ignored)]: ");
122 bitwuzla_term_print_fmt(fpleq, stdout, 16);
123 printf("\n");
124 printf("bitwuzla_term_to_string_fmt() [bin]: %s\n",
125 bitwuzla_term_to_string_fmt(fpleq, 2));
126 printf("bitwuzla_term_to_string_fmt() [dec]: %s\n",
127 bitwuzla_term_to_string_fmt(fpleq, 10));
128 printf("bitwuzla_term_to_string_fmt() [hex (ignored)]: %s\n",
129 bitwuzla_term_to_string_fmt(fpleq, 16));
130 printf("\n");
131
132 // Print asserted formulas using binary bit-vector output format.
133 {
134 printf("Print formula [binary bv output format]:\n");
135 printf("----------------------------------------\n");
136 bitwuzla_print_formula(bitwuzla, "smt2", stdout, 2);
137 printf("\n");
138 }
139
140 // Print asserted formulas using hexadecimal bit-vector output format.
141 {
142 printf("Print formula [hexadecimal bv output format]:\n");
143 printf("----------------------------------------\n");
144 bitwuzla_print_formula(bitwuzla, "smt2", stdout, 16);
145 printf("\n");
146 }
147
148 // Print asserted formulas using decimal bit-vector output format.
149 {
150 printf("Print formula [decimal bv output format]:\n");
151 printf("----------------------------------------\n");
152 bitwuzla_print_formula(bitwuzla, "smt2", stdout, 10);
153 printf("\n");
154 }
155
156 bitwuzla_check_sat(bitwuzla);
157
158 // Print values.
159 printf("Print value of Boolean predicate:\n");
160 printf("---------------------------------\n");
161 BitwuzlaTerm fpleq_val = bitwuzla_get_value(bitwuzla, fpleq);
162 bool fpleq_val_bool = bitwuzla_term_value_get_bool(fpleq_val);
163 const char* fpleq_val_str = bitwuzla_term_value_get_str(fpleq_val);
164 printf("%s: %*u [bool]\n", bitwuzla_term_to_string(fpleq), 4, fpleq_val_bool);
165 printf("%s: %*s [const char*]\n\n",
166 bitwuzla_term_to_string(fpleq),
167 4,
168 fpleq_val_str);
169
170 printf("Print value of bv const:\n");
171 printf("------------------------\n");
172 BitwuzlaTerm bv_val = bitwuzla_get_value(bitwuzla, bv);
173 printf("%s: %*s [const char*] (bin)\n",
174 bitwuzla_term_to_string(bv),
175 8,
176 bitwuzla_term_value_get_str(bv_val));
177 printf("%s: %*s [const char*] (dec)\n",
178 bitwuzla_term_to_string(bv),
179 8,
180 bitwuzla_term_value_get_str_fmt(bv_val, 10));
181 printf("%s: %*s [const char*] (hex)\n\n",
182 bitwuzla_term_to_string(bv),
183 8,
184 bitwuzla_term_value_get_str_fmt(bv_val, 16));
185
186 printf("Print value of RoundingMode const:\n");
187 printf("----------------------------------\n");
188 BitwuzlaTerm rm_val = bitwuzla_get_value(bitwuzla, rm);
189 BitwuzlaRoundingMode rm_val_rm = bitwuzla_term_value_get_rm(rm_val);
190 const char* rm_val_str = bitwuzla_term_value_get_str(rm_val);
191 printf("%s: %s [BitwuzlaRoundingMode]\n",
192 bitwuzla_term_to_string(rm),
193 bitwuzla_rm_to_string(rm_val_rm));
194 printf("%s: %s [const char*]\n\n", bitwuzla_term_to_string(rm), rm_val_str);
195
196 BitwuzlaTerm fp_val = bitwuzla_get_value(bitwuzla, fp);
197
198 printf("Print value of fp const via bitwuzla_term_value_get_str(_fmt):\n");
199 printf("--------------------------------------------------------------\n");
200 printf("%s: %*s [const char*] (bin) \n",
201 bitwuzla_term_to_string(fp),
202 16,
203 bitwuzla_term_value_get_str(fp_val));
204 printf("%s: %*s [const char*] (dec [ignored]) \n",
205 bitwuzla_term_to_string(fp),
206 16,
207 bitwuzla_term_value_get_str_fmt(fp_val, 10));
208 printf("%s: %*s [const char*] (hex [ignored]) \n\n",
209 bitwuzla_term_to_string(fp),
210 16,
211 bitwuzla_term_value_get_str_fmt(fp_val, 16));
212
213 printf("Print value of fp const via bitwuzla_term_value_get_fp_ieee():\n");
214 printf("--------------------------------------------------------------\n");
215 const char* sign;
216 const char* exponent;
217 const char* significand;
218 bitwuzla_term_value_get_fp_ieee(fp_val, &sign, &exponent, &significand, 2);
219 printf("%s: (%s %*s %*s) (bin)\n",
220 bitwuzla_term_to_string(fp),
221 sign,
222 5,
223 exponent,
224 11,
225 significand);
226 bitwuzla_term_value_get_fp_ieee(fp_val, &sign, &exponent, &significand, 10);
227 printf("%s: (%s %*s %*s) (dec)\n",
228 bitwuzla_term_to_string(fp),
229 sign,
230 5,
231 exponent,
232 11,
233 significand);
234 bitwuzla_term_value_get_fp_ieee(fp_val, &sign, &exponent, &significand, 16);
235 printf("%s: (%s %*s %*s) (hex)\n",
236 bitwuzla_term_to_string(fp),
237 sign,
238 5,
239 exponent,
240 11,
241 significand);
242
243 // Finally, delete the Bitwuzla solver, options, and term manager instances.
244 bitwuzla_delete(bitwuzla);
245 bitwuzla_options_delete(options);
246 bitwuzla_term_manager_delete(tm);
247 return 0;
248}
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/cpp/bitwuzla.h>
12
13#include <cassert>
14#include <iomanip>
15#include <iostream>
16#include <sstream>
17
18using namespace bitwuzla;
19
20int
21main()
22{
23 // First, create a term manager instance.
24 TermManager tm;
25 // Create a Bitwuzla options instance.
26 Options options;
27 options.set(Option::PRODUCE_MODELS, true);
28 // Then, create a Bitwuzla instance.
29 Bitwuzla bitwuzla(tm, options);
30 // Create some sorts.
31 Sort bv8 = tm.mk_bv_sort(8);
32 Sort bv32 = tm.mk_bv_sort(32);
33 Sort fp16 = tm.mk_fp_sort(5, 11);
34 // Create terms.
35 Term b = tm.mk_const(tm.mk_bool_sort(), "b");
36 Term bv = tm.mk_const(bv8, "bv");
37 Term fp = tm.mk_const(fp16, "fp");
38 Term rm = tm.mk_const(tm.mk_rm_sort(), "rm");
39 Term fun = tm.mk_const(tm.mk_fun_sort({bv8, fp16, bv32}, fp16), "fun");
40 Term zero = tm.mk_bv_zero(bv8);
41 Term ones = tm.mk_bv_ones(tm.mk_bv_sort(23));
42 Term z = tm.mk_var(bv8, "z");
43 Term q = tm.mk_var(bv8, "q");
44 Term lambda =
45 tm.mk_term(Kind::LAMBDA, {z, tm.mk_term(Kind::BV_ADD, {z, bv})});
46 Term fpleq = tm.mk_term(
47 Kind::FP_LEQ,
48 {tm.mk_term(Kind::APPLY,
49 {fun, bv, fp, tm.mk_term(Kind::BV_ZERO_EXTEND, {ones}, {9})}),
50 fp});
51 Term exists = tm.mk_term(
52 Kind::EXISTS,
53 {q, tm.mk_term(Kind::EQUAL, {zero, tm.mk_term(Kind::BV_MUL, {bv, q})})});
54 // Assert formulas.
55 bitwuzla.assert_formula(b);
56 bitwuzla.assert_formula(
57 tm.mk_term(Kind::EQUAL, {tm.mk_term(Kind::APPLY, {lambda, bv}), zero}));
58 bitwuzla.assert_formula(exists);
59 bitwuzla.assert_formula(fpleq);
60
61 // Print sort.
62 std::cout << "Print bit-vector sort of size 32:" << std::endl;
63 std::cout << "---------------------------------" << std::endl;
64 std::cout << "operator<<: " << bv32 << std::endl;
65 std::cout << "str(): " << bv32.str() << std::endl << std::endl;
66
67 // Print terms.
68 // Note: Hexadecimal bv output format is ignored if the value is not of size
69 // divisible by 4.
70 std::cout << "Print term:" << std::endl;
71 std::cout << "-----------" << std::endl;
72 std::cout << "operator<<: " << rm << std::endl;
73 std::cout << "operator<< [dec (ignored)]: " << set_bv_format(10) << rm
74 << std::endl;
75 std::cout << "str() [bin]: " << rm.str() << std::endl;
76 std::cout << "str(16) [hex (ignored)]: " << rm.str(16) << std::endl
77 << std::endl;
78 std::cout << "operator<< [bin]: " << set_bv_format(2) << zero << std::endl;
79 std::cout << "operator<< [dec]: " << set_bv_format(10) << zero << std::endl;
80 std::cout << "operator<< [hex]: " << set_bv_format(16) << zero << std::endl;
81 std::cout << "str() [bin]: " << zero.str() << std::endl;
82 std::cout << "str(10) [dec]: " << zero.str(10) << std::endl;
83 std::cout << "str(16) [hex]: " << zero.str(16) << std::endl << std::endl;
84 std::cout << "operator<< [bin]: " << set_bv_format(2) << fpleq
85 << std::endl;
86 std::cout << "operator<< [dec]: " << set_bv_format(10) << fpleq
87 << std::endl;
88 std::cout << "operator<< [hex (ignored)]: " << set_bv_format(16) << fpleq
89 << std::endl;
90 std::cout << "str() [bin]: " << fpleq.str() << std::endl;
91 std::cout << "str(10) [dec]: " << fpleq.str(10) << std::endl;
92 std::cout << "str(16) [hex (ignored)]: " << fpleq.str(16) << std::endl
93 << std::endl;
94
95 // Print asserted formulas.
96 // Note: This uses the default bit-vector output format (binary).
97 {
98 std::stringstream expected_smt2;
99 expected_smt2
100 << "(set-logic UFBVFP)" << std::endl
101 << "(declare-const b Bool)" << std::endl
102 << "(declare-const bv (_ BitVec 8))" << std::endl
103 << "(declare-const fp (_ FloatingPoint 5 11))" << std::endl
104 << "(declare-fun fun ((_ BitVec 8) (_ FloatingPoint 5 11) (_ BitVec "
105 "32)) (_ FloatingPoint 5 11))"
106 << std::endl
107 << "(assert b)" << std::endl
108 << "(assert (= ((lambda ((z (_ BitVec 8))) (bvadd z bv)) bv) "
109 "#b00000000))"
110 << std::endl
111 << "(assert (exists ((q (_ BitVec 8))) (= #b00000000 (bvmul bv q))))"
112 << std::endl
113 << "(assert (fp.leq (fun bv fp ((_ zero_extend 9) "
114 "#b11111111111111111111111)) fp))"
115 << std::endl
116 << "(check-sat)" << std::endl
117 << "(exit)" << std::endl;
118 std::stringstream ss;
119 bitwuzla.print_formula(ss, "smt2");
120 assert(ss.str() == expected_smt2.str());
121 std::cout << "Print formula [default (binary) bv output format]:"
122 << std::endl;
123 std::cout << "--------------------------------------------------"
124 << std::endl;
125 std::cout << ss.str() << std::endl;
126 }
127
128 // Print asserted formulas using hexadecimal bit-vector output format.
129 {
130 std::stringstream expected_smt2;
131 expected_smt2
132 << "(set-logic UFBVFP)" << std::endl
133 << "(declare-const b Bool)" << std::endl
134 << "(declare-const bv (_ BitVec 8))" << std::endl
135 << "(declare-const fp (_ FloatingPoint 5 11))" << std::endl
136 << "(declare-fun fun ((_ BitVec 8) (_ FloatingPoint 5 11) (_ BitVec "
137 "32)) (_ FloatingPoint 5 11))"
138 << std::endl
139 << "(assert b)" << std::endl
140 << "(assert (= ((lambda ((z (_ BitVec 8))) (bvadd z bv)) bv) "
141 "#x00))"
142 << std::endl
143 << "(assert (exists ((q (_ BitVec 8))) (= #x00 (bvmul bv q))))"
144 << std::endl
145 << "(assert (fp.leq (fun bv fp ((_ zero_extend 9) "
146 "#b11111111111111111111111)) fp))"
147 << std::endl
148 << "(check-sat)" << std::endl
149 << "(exit)" << std::endl;
150 std::stringstream ss;
151 // configure output stream with hexadecimal bv output format
152 ss << set_bv_format(16);
153 bitwuzla.print_formula(ss, "smt2");
154 assert(ss.str() == expected_smt2.str());
155 std::cout << "Print formula [hexadecimal bv output format]:" << std::endl;
156 std::cout << "---------------------------------------------" << std::endl;
157 std::cout << ss.str() << std::endl;
158 }
159
160 // Print asserted formulas using decimal bit-vector output format.
161 {
162 std::stringstream expected_smt2;
163 expected_smt2
164 << "(set-logic UFBVFP)" << std::endl
165 << "(declare-const b Bool)" << std::endl
166 << "(declare-const bv (_ BitVec 8))" << std::endl
167 << "(declare-const fp (_ FloatingPoint 5 11))" << std::endl
168 << "(declare-fun fun ((_ BitVec 8) (_ FloatingPoint 5 11) (_ BitVec "
169 "32)) (_ FloatingPoint 5 11))"
170 << std::endl
171 << "(assert b)" << std::endl
172 << "(assert (= ((lambda ((z (_ BitVec 8))) (bvadd z bv)) bv) "
173 "(_ bv0 8)))"
174 << std::endl
175 << "(assert (exists ((q (_ BitVec 8))) (= (_ bv0 8) (bvmul bv q))))"
176 << std::endl
177 << "(assert (fp.leq (fun bv fp ((_ zero_extend 9) "
178 "(_ bv8388607 23))) fp))"
179 << std::endl
180 << "(check-sat)" << std::endl
181 << "(exit)" << std::endl;
182 std::stringstream ss;
183 // configure output stream with decimal bv output format
184 ss << set_bv_format(10);
185 bitwuzla.print_formula(ss, "smt2");
186 assert(ss.str() == expected_smt2.str());
187 std::cout << "Print formula [decimal bv output format]:" << std::endl;
188 std::cout << "---------------------------------------------" << std::endl;
189 std::cout << ss.str() << std::endl;
190 }
191
192 bitwuzla.check_sat();
193
194 // Print values.
195 std::cout << "Print value of Boolean predicate:" << std::endl
196 << "---------------------------------" << std::endl;
197 bool fpleq_val = bitwuzla.get_value(fpleq).value<bool>();
198 std::string fpleq_val_str = bitwuzla.get_value(fpleq).value<std::string>();
199 std::cout << fpleq << ": " << std::setw(4) << fpleq_val << " [bool]"
200 << std::endl
201 << fpleq << ": " << std::setw(4) << fpleq_val_str
202 << " [std::string]" << std::endl
203 << std::endl;
204
205 std::cout << "Print value of bv const:" << std::endl
206 << "------------------------" << std::endl;
207 std::cout << bv << ": " << std::setw(8)
208 << bitwuzla.get_value(bv).value<std::string>()
209 << " [std::string] (bin)" << std::endl;
210 std::cout << bv << ": " << std::setw(8)
211 << bitwuzla.get_value(bv).value<std::string>(10)
212 << " [std::string] (dec)" << std::endl;
213 std::cout << bv << ": " << std::setw(8)
214 << bitwuzla.get_value(bv).value<std::string>(16)
215 << " [std::string] (dec)" << std::endl
216 << std::endl;
217
218 std::cout << "Print value of RoundingMode const:" << std::endl
219 << "----------------------------------" << std::endl;
220 RoundingMode rm_val = bitwuzla.get_value(rm).value<RoundingMode>();
221 std::string rm_val_str = bitwuzla.get_value(rm).value<std::string>();
222 std::cout << rm << ": " << rm_val << " [RoundingMode]" << std::endl
223 << rm << ": " << rm_val_str << " [std::string]" << std::endl
224 << std::endl;
225
226 Term fp_val = bitwuzla.get_value(fp);
227
228 std::cout << "Print value of fp const as std::string (base ignored):"
229 << std::endl
230 << "------------------------------------------------------"
231 << std::endl;
232 assert(fp_val.value<std::string>() == fp_val.value<std::string>(10));
233 assert(fp_val.value<std::string>() == fp_val.value<std::string>(16));
234 std::cout << fp << ": " << std::setw(16) << fp_val.value<std::string>()
235 << " [std::string] (bin)" << std::endl;
236 std::cout << fp << ": " << std::setw(16) << fp_val.value<std::string>(10)
237 << " [std::string] (dec [ignored])" << std::endl;
238 std::cout << fp << ": " << std::setw(16) << fp_val.value<std::string>(16)
239 << " [std::string] (hex [ignored])" << std::endl
240 << std::endl;
241
242 std::cout << "Print value of fp const as tuple of std::string:" << std::endl
243 << "------------------------------------------------" << std::endl;
244 auto fp_val_tup =
245 fp_val.value<std::tuple<std::string, std::string, std::string>>();
246 std::cout << fp << ": (" << std::get<0>(fp_val_tup) << ", " << std::setw(5)
247 << std::get<1>(fp_val_tup) << ", " << std::setw(11)
248 << std::get<2>(fp_val_tup) << ")"
249 << " [std::tuple<std::string, std::string, std::string>] (bin)"
250 << std::endl;
251 fp_val_tup =
252 fp_val.value<std::tuple<std::string, std::string, std::string>>(10);
253 std::cout << fp << ": (" << std::get<0>(fp_val_tup) << ", " << std::setw(5)
254 << std::get<1>(fp_val_tup) << ", " << std::setw(11)
255 << std::get<2>(fp_val_tup) << ")"
256 << " [std::tuple<std::string, std::string, std::string>] (dec)"
257 << std::endl;
258 fp_val_tup =
259 fp_val.value<std::tuple<std::string, std::string, std::string>>(16);
260 std::cout << fp << ": (" << std::get<0>(fp_val_tup) << ", " << std::setw(5)
261 << std::get<1>(fp_val_tup) << ", " << std::setw(11)
262 << std::get<2>(fp_val_tup) << ")"
263 << " [std::tuple<std::string, std::string, std::string>] (hex)"
264 << std::endl;
265
266 return 0;
267}
1###
2# Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3#
4# Copyright (C) 2023 by the authors listed in the AUTHORS file at
5# https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6#
7# This file is part of Bitwuzla under the MIT license. See COPYING for more
8# information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9##
10
11from bitwuzla import *
12
13if __name__ == "__main__":
14
15 # First, create a term manager instance.
16 tm = TermManager()
17 # Create a Bitwuzla options instance.
18 options = Options()
19 options.set(Option.PRODUCE_MODELS, True)
20 # Then, create a Bitwuzla instance.
21 bitwuzla = Bitwuzla(tm, options)
22 # Create some sorts.
23 bv8 = tm.mk_bv_sort(8)
24 bv32 = tm.mk_bv_sort(32)
25 fp16 = tm.mk_fp_sort(5, 11)
26 # Create terms.
27 b = tm.mk_const(tm.mk_bool_sort(), "b")
28 bv = tm.mk_const(bv8, "bv")
29 fp = tm.mk_const(fp16, "fp")
30 rm = tm.mk_const(tm.mk_rm_sort(), "rm")
31 fun = tm.mk_const(tm.mk_fun_sort([bv8, fp16, bv32], fp16), "fun")
32 zero = tm.mk_bv_zero(bv8)
33 ones = tm.mk_bv_ones(tm.mk_bv_sort(23))
34 z = tm.mk_var(bv8, "z")
35 q = tm.mk_var(bv8, "q")
36 lambd = tm.mk_term(Kind.LAMBDA, [z, tm.mk_term(Kind.BV_ADD, [z, bv])])
37 fpleq = tm.mk_term(
38 Kind.FP_LEQ,
39 [tm.mk_term(Kind.APPLY,
40 [fun, bv, fp, tm.mk_term(Kind.BV_ZERO_EXTEND, [ones], [9])]),
41 fp])
42 exists = tm.mk_term(
43 Kind.EXISTS,
44 [q, tm.mk_term(Kind.EQUAL, [zero, tm.mk_term(Kind.BV_MUL, [bv, q])])])
45 # Assert formulas.
46 bitwuzla.assert_formula(b)
47 bitwuzla.assert_formula(
48 tm.mk_term(Kind.EQUAL, [tm.mk_term(Kind.APPLY, [lambd, bv]), zero]))
49 bitwuzla.assert_formula(exists)
50 bitwuzla.assert_formula(fpleq)
51
52 # Print sort.
53 print('Print bit-vector sort of size 32:')
54 print('---------------------------------')
55 print(f'str(): {bv32}')
56 print()
57
58 # Print terms.
59 # Note: Hexadecimal bv output format is ignored if the value is not of size
60 # divisible by 4.
61 print('Print term:')
62 print('-----------')
63 print(f'str() [default]: {rm}')
64 print(f'str() [bin (ignored)]: {rm.str(2)}')
65 print(f'str() [dec (ignored)]: {rm.str(10)}')
66 print(f'str(16) [hex (ignored)]: {rm.str(16)}')
67 print()
68 print(f'str() [default]: {zero}')
69 print(f'str() [bin]: {zero.str(2)}')
70 print(f'str(10) [dec]: {zero.str(10)}')
71 print(f'str(16) [hex]: {zero.str(16)}')
72 print()
73 print(f'str() [default]: {fpleq}')
74 print(f'str() [bin]: {fpleq.str()}')
75 print(f'str(10) [dec]: {fpleq.str(10)}')
76 print(f'str(16) [hex (ignored)]: {fpleq.str(16)}')
77 print()
78
79 # Print asserted formulas.
80 # Note: This uses the default bit-vector output format (binary).
81 expected_smt2 = \
82 '(set-logic UFBVFP)\n' \
83 + '(declare-const b Bool)\n' \
84 + '(declare-const bv (_ BitVec 8))\n' \
85 + '(declare-const fp (_ FloatingPoint 5 11))\n' \
86 + '(declare-fun fun ((_ BitVec 8) (_ FloatingPoint 5 11) ' \
87 + '(_ BitVec 32)) (_ FloatingPoint 5 11))\n' \
88 + '(assert b)\n' \
89 + '(assert (= ((lambda ((z (_ BitVec 8))) (bvadd z bv)) bv) ' \
90 + '#b00000000))\n' \
91 + '(assert (exists ((q (_ BitVec 8))) (= #b00000000 ' \
92 + '(bvmul bv q))))\n' \
93 + '(assert (fp.leq (fun bv fp ((_ zero_extend 9) ' \
94 + '#b11111111111111111111111)) fp))\n' \
95 + '(check-sat)\n' \
96 + '(exit)\n'
97 res = bitwuzla.print_formula()
98 assert res == expected_smt2
99 print('Print formula [default (binary) bv output format]:')
100 print('--------------------------------------------------')
101 print(res)
102
103 # Print asserted formulas using hexadecimal bit-vector output format.
104 expected_smt2 = \
105 '(set-logic UFBVFP)\n' \
106 + '(declare-const b Bool)\n' \
107 + '(declare-const bv (_ BitVec 8))\n' \
108 + '(declare-const fp (_ FloatingPoint 5 11))\n' \
109 + '(declare-fun fun ((_ BitVec 8) (_ FloatingPoint 5 11) ' \
110 + '(_ BitVec 32)) (_ FloatingPoint 5 11))\n' \
111 + '(assert b)\n' \
112 + '(assert (= ((lambda ((z (_ BitVec 8))) (bvadd z bv)) bv) ' \
113 + '#x00))\n' \
114 + '(assert (exists ((q (_ BitVec 8))) (= #x00 (bvmul bv q))))\n' \
115 + '(assert (fp.leq (fun bv fp ((_ zero_extend 9) ' \
116 + '#b11111111111111111111111)) fp))\n' \
117 + '(check-sat)\n' \
118 + '(exit)\n'
119 res = bitwuzla.print_formula("smt2", 16)
120 assert res == expected_smt2
121 print('Print formula [hexadecimal bv output format]:')
122 print('--------------------------------------------------')
123 print(res)
124
125 # Print asserted formulas using decimal bit-vector output format.
126 expected_smt2 = \
127 '(set-logic UFBVFP)\n' \
128 + '(declare-const b Bool)\n' \
129 + '(declare-const bv (_ BitVec 8))\n' \
130 + '(declare-const fp (_ FloatingPoint 5 11))\n' \
131 + '(declare-fun fun ((_ BitVec 8) (_ FloatingPoint 5 11) ' \
132 + '(_ BitVec 32)) (_ FloatingPoint 5 11))\n' \
133 + '(assert b)\n' \
134 + '(assert (= ((lambda ((z (_ BitVec 8))) (bvadd z bv)) bv) ' \
135 + '(_ bv0 8)))\n' \
136 + '(assert (exists ((q (_ BitVec 8))) (= (_ bv0 8) ' \
137 + '(bvmul bv q))))\n' \
138 + '(assert (fp.leq (fun bv fp ((_ zero_extend 9) ' \
139 + '(_ bv8388607 23))) fp))\n' \
140 + '(check-sat)\n' \
141 + '(exit)\n'
142 res = bitwuzla.print_formula("smt2", 10)
143 assert res == expected_smt2
144 print('Print formula [decimal bv output format]:')
145 print('---------------------------------------------')
146 print(res)
147
148 bitwuzla.check_sat()
149
150 # Print values.
151 print('Print value of Boolean predicate:')
152 print('---------------------------------')
153 fpleq_val = bitwuzla.get_value(fpleq).value()
154 print(f'{fpleq}: {fpleq_val} [bool]')
155 print()
156
157 print('Print value of bv const:')
158 print('------------------------')
159 print(f'{bv}: {bitwuzla.get_value(bv).value():>8} [str] (bin)')
160 print(f'{bv}: {bitwuzla.get_value(bv).value(10):>8} [str] (dec)')
161 print(f'{bv}: {bitwuzla.get_value(bv).value(16):>8} [str] (hex)')
162 print()
163
164 print('Print value of RoundingMode const:')
165 print('----------------------------------')
166 print(f'{rm}: {bitwuzla.get_value(rm).value()} [RoundingMode]')
167 print()
168
169 fp_val = bitwuzla.get_value(fp)
170
171 print('Print value of fp const as single bit-vector (base ignored):')
172 print('------------------------------------------------------------')
173 assert fp_val.value(2, False) == fp_val.value(10, False)
174 assert fp_val.value(2, False) == fp_val.value(16, False)
175 print(f'{fp}: {fp_val.value(2, False):>16} [str] (bin)')
176 print(f'{fp}: {fp_val.value(10, False):>16} [str] (dec [ignored])')
177 print(f'{fp}: {fp_val.value(16, False):>16} [str] (hex [ignored])')
178 print()
179
180 print('Print value of fp const as list of component bit-vectors:')
181 print('---------------------------------------------------------')
182 val = fp_val.value(2)
183 print(f'{fp}: [{val[0]}, {val[1]:>5}, {val[2]:>11}] [str] (bin)')
184 val = fp_val.value(10)
185 print(f'{fp}: [{val[0]}, {val[1]:>5}, {val[2]:>11}] [str] (dec)')
186 val = fp_val.value(16)
187 print(f'{fp}: [{val[0]}, {val[1]:>5}, {val[2]:>11}] [str] (hex)')
188 print()
Termination Callback Example
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/c/bitwuzla.h>
12#include <stdint.h>
13#include <stdio.h>
14#include <sys/time.h>
15
16struct terminator_state
17{
18 struct timeval start;
19 uint32_t time_limit_ms;
20};
21
22static int32_t
23test_terminate(void* state)
24{
25 struct terminator_state* tstate = (struct terminator_state*) state;
26 struct timeval now;
27 gettimeofday(&now, NULL);
28 if (((now.tv_sec - tstate->start.tv_sec) * 1000
29 + (now.tv_usec - tstate->start.tv_usec) / 1000)
30 >= tstate->time_limit_ms)
31 {
32 return 1;
33 }
34 return 0;
35}
36
37int
38main()
39{
40 // First, create a term manager instance.
41 BitwuzlaTermManager* tm = bitwuzla_term_manager_new();
42 // Create a Bitwuzla options instance.
43 BitwuzlaOptions* options = bitwuzla_options_new();
44 // Then, create a Bitwuzla instance.
45 Bitwuzla* bitwuzla = bitwuzla_new(tm, options);
46
47 BitwuzlaSort bv = bitwuzla_mk_bv_sort(tm, 32);
48
49 BitwuzlaTerm x = bitwuzla_mk_const(tm, bv, "x");
50 BitwuzlaTerm s = bitwuzla_mk_const(tm, bv, "s");
51 BitwuzlaTerm t = bitwuzla_mk_const(tm, bv, "t");
52
53 BitwuzlaTerm a = bitwuzla_mk_term2(
54 tm,
55 BITWUZLA_KIND_DISTINCT,
56 bitwuzla_mk_term2(tm,
57 BITWUZLA_KIND_BV_MUL,
58 s,
59 bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_MUL, x, t)),
60 bitwuzla_mk_term2(tm,
61 BITWUZLA_KIND_BV_MUL,
62 bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_MUL, s, x),
63 t));
64
65 // Now, we check that the following formula is unsat.
66 // (assert (distinct (bvmul s (bvmul x t)) (bvmul (bvmul s x) t)))
67 BitwuzlaTerm assumptions[1] = {a};
68 printf("> Without terminator (with preprocessing):\n");
69 printf("Expect: unsat\n");
70 printf("Result: %s\n",
71 bitwuzla_result_to_string(
72 bitwuzla_check_sat_assuming(bitwuzla, 1, assumptions)));
73
74 // Now, for illustration purposes, we disable preprocessing, which will
75 // significantly increase solving time, and connect a terminator instance
76 // that terminates after a certain time limit.
77 bitwuzla_set_option(options, BITWUZLA_OPT_PREPROCESS, 0);
78 // Create new Bitwuzla instance with reconfigured options.
79 Bitwuzla* bitwuzla2 = bitwuzla_new(tm, options);
80 // Configure termination callback.
81 struct terminator_state state;
82 gettimeofday(&state.start, NULL);
83 state.time_limit_ms = 1000;
84 bitwuzla_set_termination_callback(bitwuzla2, test_terminate, &state);
85
86 // Now, we expect Bitwuzla to be terminated during the check-sat call.
87 printf("> With terminator (no preprocessing):\n");
88 printf("Expect: unknown\n");
89 printf("Result: %s\n",
90 bitwuzla_result_to_string(
91 bitwuzla_check_sat_assuming(bitwuzla2, 1, assumptions)));
92
93 // Finally, delete the Bitwuzla solver, options, and term manager instances.
94 bitwuzla_delete(bitwuzla);
95 bitwuzla_delete(bitwuzla2);
96 bitwuzla_options_delete(options);
97 bitwuzla_term_manager_delete(tm);
98
99 return 0;
100}
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2023 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/cpp/bitwuzla.h>
12
13#include <chrono>
14#include <iostream>
15
16using namespace bitwuzla;
17using namespace std::chrono;
18
19class TestTerminator : public Terminator
20{
21 public:
22 TestTerminator(uint32_t time_limit_ms)
23 : Terminator(),
24 time_limit_ms(time_limit_ms),
25 start(high_resolution_clock::now())
26 {
27 }
28 bool terminate() override
29 {
30 if (duration_cast<milliseconds>(high_resolution_clock::now() - start)
31 .count()
32 >= time_limit_ms)
33 {
34 return true;
35 }
36 return false;
37 }
38 uint32_t time_limit_ms = 0;
39 high_resolution_clock::time_point start;
40};
41
42int
43main()
44{
45 // First, create a term manager instance.
46 TermManager tm;
47 // Create a Bitwuzla options instance.
48 Options options;
49 // Then, create a Bitwuzla instance.
50 Bitwuzla bitwuzla(tm, options);
51
52 Sort bv = tm.mk_bv_sort(32);
53
54 Term x = tm.mk_const(bv);
55 Term s = tm.mk_const(bv);
56 Term t = tm.mk_const(bv);
57
58 Term a = tm.mk_term(
59 Kind::DISTINCT,
60 {tm.mk_term(Kind::BV_MUL, {s, tm.mk_term(Kind::BV_MUL, {x, t})}),
61 tm.mk_term(Kind::BV_MUL, {tm.mk_term(Kind::BV_MUL, {s, x}), t})});
62
63 // Now, we check that the following formula is unsat.
64 // (assert (distinct (bvmul s (bvmul x t)) (bvmul (bvmul s x) t)))
65 std::cout << "> Without terminator (with preprocessing):" << std::endl;
66 std::cout << "Expect: unsat" << std::endl;
67 std::cout << "Result: " << bitwuzla.check_sat({a}) << std::endl;
68
69 // Now, for illustration purposes, we disable preprocessing, which will
70 // significantly increase solving time, and connect a terminator instance
71 // that terminates after a certain time limit.
72 options.set(Option::PREPROCESS, false);
73 // Create new Bitwuzla instance with reconfigured options.
74 Bitwuzla bitwuzla2(tm, options);
75 // Configure and connect terminator.
76 TestTerminator tt(1000);
77 bitwuzla2.configure_terminator(&tt);
78
79 // Now, we expect Bitwuzla to be terminated during the check-sat call.
80 std::cout << "> With terminator (no preprocessing):" << std::endl;
81 std::cout << "Expect: unknown" << std::endl;
82 std::cout << "Result: " << bitwuzla2.check_sat({a}) << std::endl;
83
84 return 0;
85}
1###
2# Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3#
4# Copyright (C) 2023 by the authors listed in the AUTHORS file at
5# https:#github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6#
7# This file is part of Bitwuzla under the MIT license. See COPYING for more
8# information at https:#github.com/bitwuzla/bitwuzla/blob/main/COPYING
9##
10
11import time
12
13from bitwuzla import *
14
15class TestTerminator:
16 def __init__(self, time_limit):
17 self.start_time = time.time()
18 self.time_limit = time_limit
19
20 def __call__(self):
21 # Terminate after self.time_limit ms passed
22 return (time.time() - self.start_time) * 1000 > self.time_limit
23
24if __name__ == '__main__':
25
26 # First, create a term manager instance.
27 tm = TermManager()
28 # No options configured, create Bitwuzla instance with default options.
29 bitwuzla = Bitwuzla(tm)
30
31 bv = tm.mk_bv_sort(32)
32
33 x = tm.mk_const(bv)
34 s = tm.mk_const(bv)
35 t = tm.mk_const(bv)
36
37 a = tm.mk_term(Kind.DISTINCT,
38 [tm.mk_term(Kind.BV_MUL, [s, tm.mk_term(Kind.BV_MUL, [x, t])]),
39 tm.mk_term(Kind.BV_MUL, [tm.mk_term(Kind.BV_MUL, [s, x]), t])])
40
41 # Now, we check that the following formula is unsat.
42 # (assert (distinct (bvmul s (bvmul x t)) (bvmul (bvmul s x) t)))
43 print('> Without terminator (with preprocessing):')
44 print('Expect: unsat')
45 print(f'Bitwuzla: {bitwuzla.check_sat(a)}')
46
47 # Now, for illustration purposes, we disable preprocessing, which will
48 # significantly increase solving time, and connect a terminator instance
49 # that terminates after a certain time limit.
50 options = Options()
51 options.set(Option.PREPROCESS, False)
52 # Create new Bitwuzla instance with reconfigured options.
53 bitwuzla2 = Bitwuzla(tm, options)
54 # Configure and connect terminator.
55 tt = TestTerminator(1)
56 bitwuzla2.configure_terminator(tt)
57
58 # Now, we expect Bitwuzla to be terminated during the check-sat call.
59 print('> With terminator (no preprocessing):')
60 print('Expect: unsat')
61 print(f'Bitwuzla: {bitwuzla2.check_sat(a)}')
Manual Reference Counting of Sort and Terms Example
1/***
2 * Bitwuzla: Satisfiability Modulo Theories (SMT) solver.
3 *
4 * Copyright (C) 2024 by the authors listed in the AUTHORS file at
5 * https://github.com/bitwuzla/bitwuzla/blob/main/AUTHORS
6 *
7 * This file is part of Bitwuzla under the MIT license. See COPYING for more
8 * information at https://github.com/bitwuzla/bitwuzla/blob/main/COPYING
9 */
10
11#include <bitwuzla/c/bitwuzla.h>
12#include <stdio.h>
13#include <assert.h>
14
15int
16main()
17{
18 // First, create a term manager instance.
19 BitwuzlaTermManager *tm = bitwuzla_term_manager_new();
20 // Create a Bitwuzla options instance.
21 BitwuzlaOptions *options = bitwuzla_options_new();
22 bitwuzla_set_option(options, BITWUZLA_OPT_PRODUCE_MODELS, 1);
23 // Then, create a Bitwuzla instance.
24 Bitwuzla *bitwuzla = bitwuzla_new(tm, options);
25
26 BitwuzlaSort sortb = bitwuzla_mk_bool_sort(tm);
27 BitwuzlaTerm a = bitwuzla_mk_const(tm, sortb, "a");
28 BitwuzlaTerm b = bitwuzla_mk_const(tm, sortb, "b");
29
30 // Release sort reference.
31 bitwuzla_sort_release(sortb);
32
33 BitwuzlaTerm dis = bitwuzla_mk_term2(tm, BITWUZLA_KIND_DISTINCT, a, b);
34 bitwuzla_assert(bitwuzla, dis);
35 bitwuzla_check_sat(bitwuzla);
36
37 // Release term reference.
38 bitwuzla_term_release(dis);
39
40 BitwuzlaTerm val_a = bitwuzla_get_value(bitwuzla, a);
41 BitwuzlaTerm val_b = bitwuzla_get_value(bitwuzla, b);
42
43 assert(val_a != val_b);
44 printf("value a: %s\n", bitwuzla_term_to_string(val_a));
45 printf("value b: %s\n", bitwuzla_term_to_string(val_b));
46
47 // Release remaining term references.
48 bitwuzla_term_release(val_a);
49 bitwuzla_term_release(val_b);
50 bitwuzla_term_release(a);
51 bitwuzla_term_release(b);
52
53 // At this point all sort and term references have been released. If all sort
54 // and term references should be released at once we can also use the
55 // following function.
56 bitwuzla_term_manager_release(tm);
57
58 // Finally, delete the Bitwuzla solver, options, and term manager instances.
59 bitwuzla_delete(bitwuzla);
60 bitwuzla_options_delete(options);
61 // Note: All sorts and terms not released until now will be released when
62 // deleting the term manager.
63 bitwuzla_term_manager_delete(tm);
64
65 return 0;
66}
67