2020-02-19 13:54:17 -08:00
|
|
|
#!/usr/bin/env python
|
2021-04-12 12:31:43 -07:00
|
|
|
###############################################################################
|
|
|
|
|
# Top contributors (to current version):
|
2022-04-05 13:38:57 -07:00
|
|
|
# Makai Mann, Aina Niemetz, Andrew Reynolds
|
2021-04-12 12:31:43 -07:00
|
|
|
#
|
|
|
|
|
# This file is part of the cvc5 project.
|
|
|
|
|
#
|
2025-01-23 09:54:20 -08:00
|
|
|
# Copyright (c) 2009-2025 by the authors listed in the file AUTHORS
|
2021-04-12 12:31:43 -07:00
|
|
|
# in the top-level source directory and their institutional affiliations.
|
|
|
|
|
# All rights reserved. See the file COPYING in the top-level source
|
|
|
|
|
# directory for licensing information.
|
|
|
|
|
# #############################################################################
|
|
|
|
|
#
|
|
|
|
|
# A simple demonstration of the solving capabilities of the cvc5 bit-vector
|
|
|
|
|
# solver through the Python API. This is a direct translation of
|
|
|
|
|
# bitvectors-new.cpp.
|
2020-09-22 09:51:56 -07:00
|
|
|
##
|
|
|
|
|
|
2022-02-02 15:45:42 -08:00
|
|
|
import cvc5
|
|
|
|
|
from cvc5 import Kind
|
2020-02-19 13:54:17 -08:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-03-14 14:34:58 -07:00
|
|
|
tm = cvc5.TermManager()
|
|
|
|
|
slv = cvc5.Solver(tm)
|
2020-02-19 13:54:17 -08:00
|
|
|
slv.setLogic("QF_BV") # Set the logic
|
|
|
|
|
# The following example has been adapted from the book A Hacker's Delight by
|
|
|
|
|
# Henry S. Warren.
|
|
|
|
|
#
|
|
|
|
|
# Given a variable x that can only have two values, a or b. We want to
|
|
|
|
|
# assign to x a value other than the current one. The straightforward code
|
|
|
|
|
# to do that is:
|
|
|
|
|
#
|
|
|
|
|
#(0) if (x == a ) x = b;
|
|
|
|
|
# else x = a;
|
|
|
|
|
#
|
|
|
|
|
# Two more efficient yet equivalent methods are:
|
|
|
|
|
#
|
|
|
|
|
#(1) x = a xor b xor x;
|
|
|
|
|
#
|
|
|
|
|
#(2) x = a + b - x;
|
|
|
|
|
#
|
2021-04-21 10:21:34 -07:00
|
|
|
# We will use cvc5 to prove that the three pieces of code above are all
|
2020-02-19 13:54:17 -08:00
|
|
|
# equivalent by encoding the problem in the bit-vector theory.
|
|
|
|
|
|
|
|
|
|
# Creating a bit-vector type of width 32
|
2024-03-14 14:34:58 -07:00
|
|
|
bitvector32 = tm.mkBitVectorSort(32)
|
2020-02-19 13:54:17 -08:00
|
|
|
|
|
|
|
|
# Variables
|
2024-03-14 14:34:58 -07:00
|
|
|
x = tm.mkConst(bitvector32, "x")
|
|
|
|
|
a = tm.mkConst(bitvector32, "a")
|
|
|
|
|
b = tm.mkConst(bitvector32, "b")
|
2020-02-19 13:54:17 -08:00
|
|
|
|
|
|
|
|
# First encode the assumption that x must be equal to a or b
|
2024-03-14 14:34:58 -07:00
|
|
|
x_eq_a = tm.mkTerm(Kind.EQUAL, x, a)
|
|
|
|
|
x_eq_b = tm.mkTerm(Kind.EQUAL, x, b)
|
|
|
|
|
assumption = tm.mkTerm(Kind.OR, x_eq_a, x_eq_b)
|
2020-02-19 13:54:17 -08:00
|
|
|
|
|
|
|
|
# Assert the assumption
|
|
|
|
|
slv.assertFormula(assumption)
|
|
|
|
|
|
|
|
|
|
# Introduce a new variable for the new value of x after assignment.
|
|
|
|
|
# x after executing code (0)
|
2024-03-14 14:34:58 -07:00
|
|
|
new_x = tm.mkConst(bitvector32, "new_x")
|
2020-02-19 13:54:17 -08:00
|
|
|
# x after executing code (1) or (2)
|
2024-03-14 14:34:58 -07:00
|
|
|
new_x_ = tm.mkConst(bitvector32, "new_x_")
|
2020-02-19 13:54:17 -08:00
|
|
|
|
|
|
|
|
# Encoding code (0)
|
|
|
|
|
# new_x = x == a ? b : a
|
2024-03-14 14:34:58 -07:00
|
|
|
ite = tm.mkTerm(Kind.ITE, x_eq_a, b, a)
|
|
|
|
|
assignment0 = tm.mkTerm(Kind.EQUAL, new_x, ite)
|
2020-02-19 13:54:17 -08:00
|
|
|
|
|
|
|
|
# Assert the encoding of code (0)
|
2021-04-21 10:21:34 -07:00
|
|
|
print("Asserting {} to cvc5".format(assignment0))
|
2020-02-19 13:54:17 -08:00
|
|
|
slv.assertFormula(assignment0)
|
|
|
|
|
print("Pushing a new context.")
|
|
|
|
|
slv.push()
|
|
|
|
|
|
|
|
|
|
# Encoding code (1)
|
|
|
|
|
# new_x_ = a xor b xor x
|
2024-03-14 14:34:58 -07:00
|
|
|
a_xor_b_xor_x = tm.mkTerm(Kind.BITVECTOR_XOR, a, b, x)
|
|
|
|
|
assignment1 = tm.mkTerm(Kind.EQUAL, new_x_, a_xor_b_xor_x)
|
2020-02-19 13:54:17 -08:00
|
|
|
|
2021-04-21 10:21:34 -07:00
|
|
|
# Assert encoding to cvc5 in current context
|
|
|
|
|
print("Asserting {} to cvc5".format(assignment1))
|
2020-02-19 13:54:17 -08:00
|
|
|
slv.assertFormula(assignment1)
|
2024-03-14 14:34:58 -07:00
|
|
|
new_x_eq_new_x_ = tm.mkTerm(Kind.EQUAL, new_x, new_x_)
|
2020-02-19 13:54:17 -08:00
|
|
|
|
2022-03-14 13:29:30 -05:00
|
|
|
print("Checking sat assuming:", new_x_eq_new_x_.notTerm())
|
|
|
|
|
print("Expect UNSAT.")
|
|
|
|
|
print("cvc5:", slv.checkSatAssuming(new_x_eq_new_x_.notTerm()))
|
2020-02-19 13:54:17 -08:00
|
|
|
print("Popping context.")
|
|
|
|
|
slv.pop()
|
|
|
|
|
|
|
|
|
|
# Encoding code (2)
|
|
|
|
|
# new_x_ = a + b - x
|
2024-03-14 14:34:58 -07:00
|
|
|
a_plus_b = tm.mkTerm(Kind.BITVECTOR_ADD, a, b)
|
|
|
|
|
a_plus_b_minus_x = tm.mkTerm(Kind.BITVECTOR_SUB, a_plus_b, x)
|
|
|
|
|
assignment2 = tm.mkTerm(Kind.EQUAL, new_x_, a_plus_b_minus_x)
|
2020-02-19 13:54:17 -08:00
|
|
|
|
2021-04-21 10:21:34 -07:00
|
|
|
# Assert encoding to cvc5 in current context
|
|
|
|
|
print("Asserting {} to cvc5".format(assignment2))
|
2020-02-19 13:54:17 -08:00
|
|
|
slv.assertFormula(assignment2)
|
|
|
|
|
|
2022-03-14 13:29:30 -05:00
|
|
|
print("Checking sat assuming:", new_x_eq_new_x_.notTerm())
|
|
|
|
|
print("Expect UNSAT.")
|
|
|
|
|
print("cvc5:", slv.checkSatAssuming(new_x_eq_new_x_.notTerm()))
|
2020-02-19 13:54:17 -08:00
|
|
|
|
|
|
|
|
|
2024-03-14 14:34:58 -07:00
|
|
|
x_neq_x = tm.mkTerm(Kind.EQUAL, x, x).notTerm()
|
|
|
|
|
query = tm.mkTerm(Kind.AND, new_x_eq_new_x_, x_neq_x)
|
2022-03-14 13:29:30 -05:00
|
|
|
print("Check sat assuming: ", query.notTerm())
|
|
|
|
|
print("Expect SAT.")
|
|
|
|
|
print("cvc5:", slv.checkSatAssuming(query.notTerm()))
|
2020-02-19 13:54:17 -08:00
|
|
|
|
|
|
|
|
# Assert that a is odd
|
2024-03-14 14:34:58 -07:00
|
|
|
extract_op = tm.mkOp(Kind.BITVECTOR_EXTRACT, 0, 0)
|
|
|
|
|
lsb_of_a = tm.mkTerm(extract_op, a)
|
2020-02-19 13:54:17 -08:00
|
|
|
print("Sort of {} is {}".format(lsb_of_a, lsb_of_a.getSort()))
|
2024-03-14 14:34:58 -07:00
|
|
|
a_odd = tm.mkTerm(Kind.EQUAL, lsb_of_a, tm.mkBitVector(1, 1))
|
2020-02-19 13:54:17 -08:00
|
|
|
print("Assert", a_odd)
|
|
|
|
|
print("Check satisifiability")
|
|
|
|
|
slv.assertFormula(a_odd)
|
|
|
|
|
print("Expect sat")
|
2021-04-21 10:21:34 -07:00
|
|
|
print("cvc5:", slv.checkSat())
|