Quickstart Guide

First, create a cvc5 term manager instance:

tm = cvc5.TermManager()

Then, create a cvc5 solver instance:

solver = cvc5.Solver(tm)

We will ask the solver to produce models and unsat cores in the following, and for this we have to enable the following options.

solver.setOption("produce-models", "true")
solver.setOption("produce-unsat-cores", "true")

Next we set the logic. The simplest way to set a logic for the solver is to choose "ALL". This enables all logics in the solver. Alternatively, "QF_ALL" enables all logics without quantifiers. To optimize the solver’s behavior for a more specific logic, use the logic name, e.g. "QF_BV" or "QF_AUFBV".

solver.setLogic("ALL")

In the following, we will define constraints of reals and integers. For this, we first query the solver for the corresponding sorts.

realSort = tm.getRealSort()
intSort = tm.getIntegerSort()

Now, we create two constants x and y of sort Real, and two constants a and b of sort Integer. Notice that these are symbolic constants, but not actual values.

x = tm.mkConst(realSort, "x")
y = tm.mkConst(realSort, "y")
a = tm.mkConst(intSort, "a")
b = tm.mkConst(intSort, "b")

We define the following constraints regarding x and y:

\[(0 < x) \wedge (0 < y) \wedge (x + y < 1) \wedge (x \leq y)\]

We construct the required terms and assert them as follows:

# Formally, constraints are also terms. Their sort is Boolean.
# We will construct these constraints gradually,
# by defining each of their components.
# We start with the constant numerals 0 and 1:
zero = tm.mkReal(0)
one = tm.mkReal(1)

# Next, we construct the term x + y
xPlusY = tm.mkTerm(Kind.ADD, x, y)

# Now we can define the constraints.
# They use the operators +, <=, and <.
# In the API, these are denoted by Plus, Leq, and Lt.
constraint1 = tm.mkTerm(Kind.LT, zero, x)
constraint2 = tm.mkTerm(Kind.LT, zero, y)
constraint3 = tm.mkTerm(Kind.LT, xPlusY, one)
constraint4 = tm.mkTerm(Kind.LEQ, x, y)

# Now we assert the constraints to the solver.
solver.assertFormula(constraint1)
solver.assertFormula(constraint2)
solver.assertFormula(constraint3)
solver.assertFormula(constraint4)

Now we check if the asserted formula is satisfiable, that is, we check if there exist values of sort Real for x and y that satisfy all the constraints.

r1 = solver.checkSat()

The result we get from this satisfiability check is either sat, unsat or unknown. It’s status can be queried via isSat, isUnsat and isSatUnknown functions. Alternatively, it can also be printed.

print("expected: sat")
print("result: ", r1)

This will print:

expected: sat
result: sat

Now, we query the solver for the values for x and y that satisfy the constraints.

xVal = solver.getValue(x)
yVal = solver.getValue(y)

It is also possible to get values for terms that do not appear in the original formula.

xMinusY = tm.mkTerm(Kind.SUB, x, y)
xMinusYVal = solver.getValue(xMinusY)

We can retrieve the Python representation of these values as follows.

xPy = xVal.getRealValue()
yPy = yVal.getRealValue()
xMinusYPy = xMinusYVal.getRealValue()

print("value for x: ", xPy)
print("value for y: ", yPy)
print("value for x - y: ", xMinusYPy)

This will print the following:

value for x: 1/6
value for y: 1/6
value for x - y: 0

Another way to independently compute the value of x - y would be to use the Python minus operator instead of asking the solver. However, for more complex terms, it is easier to let the solver do the evaluation.

xMinusYComputed = xPy - yPy
if xMinusYComputed == xMinusYPy:
  print("computed correctly")
else:
  print("computed incorrectly")

This will print:

computed correctly

Further, we can convert these values to strings:

xStr = str(xPy)
yStr = str(yPy)
xMinusYStr = str(xMinusYPy)

Next, we will check satisfiability of the same formula, only this time over integer variables a and b. For this, we first reset the assertions added to the solver.

solver.resetAssertions()

Next, we assert the same assertions as above, but with integers. This time, we inline the construction of terms to the assertion command.

solver.assertFormula(tm.mkTerm(Kind.LT, tm.mkInteger(0), a))
solver.assertFormula(tm.mkTerm(Kind.LT, tm.mkInteger(0), b))
solver.assertFormula(
    tm.mkTerm(
        Kind.LT, tm.mkTerm(Kind.ADD, a, b), tm.mkInteger(1)))
solver.assertFormula(tm.mkTerm(Kind.LEQ, a, b))

Now, we check whether the revised assertion is satisfiable.

r2 = solver.checkSat()
print("expected: unsat")
print("result:", r2)

This time the asserted formula is unsatisfiable:

expected: unsat
result: unsat

We can query the solver for an unsatisfiable core, that is, a subset of the assertions that is already unsatisfiable.

unsatCore = solver.getUnsatCore()
print("unsat core size:", len(unsatCore))
print("unsat core:", unsatCore)

This will print:

unsat core size: 3
unsat core: [(< 0 a), (< 0 b), (< (+ a b) 1)]

Example

examples/api/python/quickstart.py

  1#!/usr/bin/env python
  2###############################################################################
  3# This file is part of the cvc5 project.
  4#
  5# Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
  6# in the top-level source directory and their institutional affiliations.
  7# All rights reserved.  See the file COPYING in the top-level source
  8# directory for licensing information.
  9# #############################################################################
 10#
 11# A simple demonstration of the api capabilities of cvc5, adapted from quickstart.cpp
 12##
 13
 14import cvc5
 15from cvc5 import Kind
 16
 17if __name__ == "__main__":
 18  # Create a term manager
 19  #! [docs-python-quickstart-0 start]
 20  tm = cvc5.TermManager()
 21  #! [docs-python-quickstart-0 end]
 22  # Create a solver
 23  #! [docs-python-quickstart-1 start]
 24  solver = cvc5.Solver(tm)
 25  #! [docs-python-quickstart-1 end]
 26
 27  # We will ask the solver to produce models and unsat cores,
 28  # hence these options should be turned on.
 29  #! [docs-python-quickstart-2 start]
 30  solver.setOption("produce-models", "true")
 31  solver.setOption("produce-unsat-cores", "true")
 32  #! [docs-python-quickstart-2 end]
 33
 34  # The simplest way to set a logic for the solver is to choose "ALL".
 35  # This enables all logics in the solver.
 36  # Alternatively, "QF_ALL" enables all logics without quantifiers.
 37  # To optimize the solver's behavior for a more specific logic,
 38  # use the logic name, e.g. "QF_BV" or "QF_AUFBV".
 39
 40  # Set the logic
 41  #! [docs-python-quickstart-3 start]
 42  solver.setLogic("ALL")
 43  #! [docs-python-quickstart-3 end]
 44
 45  # In this example, we will define constraints over reals and integers.
 46  # Hence, we first obtain the corresponding sorts.
 47  #! [docs-python-quickstart-4 start]
 48  realSort = tm.getRealSort()
 49  intSort = tm.getIntegerSort()
 50  #! [docs-python-quickstart-4 end]
 51
 52  # x and y will be real variables, while a and b will be integer variables.
 53  # Formally, their python type is Term,
 54  # and they are called "constants" in SMT jargon:
 55  #! [docs-python-quickstart-5 start]
 56  x = tm.mkConst(realSort, "x")
 57  y = tm.mkConst(realSort, "y")
 58  a = tm.mkConst(intSort, "a")
 59  b = tm.mkConst(intSort, "b")
 60  #! [docs-python-quickstart-5 end]
 61
 62  # Our constraints regarding x and y will be:
 63  #
 64  #   (1)  0 < x
 65  #   (2)  0 < y
 66  #   (3)  x + y < 1
 67  #   (4)  x <= y
 68  #
 69
 70  #! [docs-python-quickstart-6 start]
 71  # Formally, constraints are also terms. Their sort is Boolean.
 72  # We will construct these constraints gradually,
 73  # by defining each of their components.
 74  # We start with the constant numerals 0 and 1:
 75  zero = tm.mkReal(0)
 76  one = tm.mkReal(1)
 77
 78  # Next, we construct the term x + y
 79  xPlusY = tm.mkTerm(Kind.ADD, x, y)
 80
 81  # Now we can define the constraints.
 82  # They use the operators +, <=, and <.
 83  # In the API, these are denoted by Plus, Leq, and Lt.
 84  constraint1 = tm.mkTerm(Kind.LT, zero, x)
 85  constraint2 = tm.mkTerm(Kind.LT, zero, y)
 86  constraint3 = tm.mkTerm(Kind.LT, xPlusY, one)
 87  constraint4 = tm.mkTerm(Kind.LEQ, x, y)
 88
 89  # Now we assert the constraints to the solver.
 90  solver.assertFormula(constraint1)
 91  solver.assertFormula(constraint2)
 92  solver.assertFormula(constraint3)
 93  solver.assertFormula(constraint4)
 94  #! [docs-python-quickstart-6 end]
 95
 96  # Check if the formula is satisfiable, that is,
 97  # are there real values for x and y that satisfy all the constraints?
 98  #! [docs-python-quickstart-7 start]
 99  r1 = solver.checkSat()
100  #! [docs-python-quickstart-7 end]
101
102  # The result is either SAT, UNSAT, or UNKNOWN.
103  # In this case, it is SAT.
104  #! [docs-python-quickstart-8 start]
105  print("expected: sat")
106  print("result: ", r1)
107  #! [docs-python-quickstart-8 end]
108
109  # We can get the values for x and y that satisfy the constraints.
110  #! [docs-python-quickstart-9 start]
111  xVal = solver.getValue(x)
112  yVal = solver.getValue(y)
113  #! [docs-python-quickstart-9 end]
114
115  # It is also possible to get values for compound terms,
116  # even if those did not appear in the original formula.
117  #! [docs-python-quickstart-10 start]
118  xMinusY = tm.mkTerm(Kind.SUB, x, y)
119  xMinusYVal = solver.getValue(xMinusY)
120  #! [docs-python-quickstart-10 end]
121
122  # We can now obtain the values as python values
123  #! [docs-python-quickstart-11 start]
124  xPy = xVal.getRealValue()
125  yPy = yVal.getRealValue()
126  xMinusYPy = xMinusYVal.getRealValue()
127
128  print("value for x: ", xPy)
129  print("value for y: ", yPy)
130  print("value for x - y: ", xMinusYPy)
131  #! [docs-python-quickstart-11 end]
132
133  # Another way to independently compute the value of x - y would be
134  # to use the python minus operator instead of asking the solver.
135  # However, for more complex terms,
136  # it is easier to let the solver do the evaluation.
137  #! [docs-python-quickstart-12 start]
138  xMinusYComputed = xPy - yPy
139  if xMinusYComputed == xMinusYPy:
140    print("computed correctly")
141  else:
142    print("computed incorrectly")
143  #! [docs-python-quickstart-12 end]
144
145  # Further, we can convert the values to strings
146  #! [docs-python-quickstart-13 start]
147  xStr = str(xPy)
148  yStr = str(yPy)
149  xMinusYStr = str(xMinusYPy)
150  #! [docs-python-quickstart-13 end]
151
152  # Next, we will check satisfiability of the same formula,
153  # only this time over integer variables a and b.
154
155  # We start by resetting assertions added to the solver.
156  #! [docs-python-quickstart-14 start]
157  solver.resetAssertions()
158  #! [docs-python-quickstart-14 end]
159
160  # Next, we assert the same assertions above with integers.
161  # This time, we inline the construction of terms
162  # to the assertion command.
163  #! [docs-python-quickstart-15 start]
164  solver.assertFormula(tm.mkTerm(Kind.LT, tm.mkInteger(0), a))
165  solver.assertFormula(tm.mkTerm(Kind.LT, tm.mkInteger(0), b))
166  solver.assertFormula(
167      tm.mkTerm(
168          Kind.LT, tm.mkTerm(Kind.ADD, a, b), tm.mkInteger(1)))
169  solver.assertFormula(tm.mkTerm(Kind.LEQ, a, b))
170  #! [docs-python-quickstart-15 end]
171
172  # We check whether the revised assertion is satisfiable.
173  #! [docs-python-quickstart-16 start]
174  r2 = solver.checkSat()
175  #! [docs-python-quickstart-16 end]
176
177  # This time the formula is unsatisfiable
178  #! [docs-python-quickstart-17 start]
179  print("expected: unsat")
180  print("result:", r2)
181  #! [docs-python-quickstart-17 end]
182
183  # We can query the solver for an unsatisfiable core, i.e., a subset
184  # of the assertions that is already unsatisfiable.
185  #! [docs-python-quickstart-18 start]
186  unsatCore = solver.getUnsatCore()
187  print("unsat core size:", len(unsatCore))
188  print("unsat core:", unsatCore)
189  #! [docs-python-quickstart-18 end]