Theory of Linear Arithmetic

This example asserts three constraints over an integer variable x and a real variable y. Firstly, it checks that these constraints entail an upper bound on the difference y - x <= 2/3. Secondly, it checks that this bound is tight by asserting y - x = 2/3 and checking for satisfiability. The two checks are separated by using push and pop.

examples/api/cpp/linear_arith.cpp

 1/******************************************************************************
 2 * This file is part of the cvc5 project.
 3 *
 4 * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
 5 * in the top-level source directory and their institutional affiliations.
 6 * All rights reserved.  See the file COPYING in the top-level source
 7 * directory for licensing information.
 8 * ****************************************************************************
 9 *
10 * A simple demonstration of the linear arithmetic solving capabilities and
11 * the push pop of cvc5. This also gives an example option.
12 */
13
14#include <iostream>
15
16#include <cvc5/cvc5.h>
17
18using namespace std;
19using namespace cvc5;
20
21int main()
22{
23  TermManager tm;
24  Solver slv(tm);
25  slv.setLogic("QF_LIRA"); // Set the logic
26
27  // Prove that if given x (Integer) and y (Real) then
28  // the maximum value of y - x is 2/3
29
30  // Sorts
31  Sort real = tm.getRealSort();
32  Sort integer = tm.getIntegerSort();
33
34  // Variables
35  Term x = tm.mkConst(integer, "x");
36  Term y = tm.mkConst(real, "y");
37
38  // Constants
39  Term three = tm.mkInteger(3);
40  Term neg2 = tm.mkInteger(-2);
41  Term two_thirds = tm.mkReal(2, 3);
42
43  // Terms
44  Term three_y = tm.mkTerm(Kind::MULT, {three, y});
45  Term diff = tm.mkTerm(Kind::SUB, {y, x});
46
47  // Formulas
48  Term x_geq_3y = tm.mkTerm(Kind::GEQ, {x, three_y});
49  Term x_leq_y = tm.mkTerm(Kind::LEQ, {x, y});
50  Term neg2_lt_x = tm.mkTerm(Kind::LT, {neg2, x});
51
52  Term assertions = tm.mkTerm(Kind::AND, {x_geq_3y, x_leq_y, neg2_lt_x});
53
54  cout << "Given the assertions " << assertions << endl;
55  slv.assertFormula(assertions);
56
57
58  slv.push();
59  Term diff_leq_two_thirds = tm.mkTerm(Kind::LEQ, {diff, two_thirds});
60  cout << "Prove that " << diff_leq_two_thirds << " with cvc5." << endl;
61  cout << "cvc5 should report UNSAT." << endl;
62  cout << "Result from cvc5 is: "
63       << slv.checkSatAssuming(diff_leq_two_thirds.notTerm()) << endl;
64  slv.pop();
65
66  cout << endl;
67
68  slv.push();
69  Term diff_is_two_thirds = tm.mkTerm(Kind::EQUAL, {diff, two_thirds});
70  slv.assertFormula(diff_is_two_thirds);
71  cout << "Show that the assertions are consistent with " << endl;
72  cout << diff_is_two_thirds << " with cvc5." << endl;
73  cout << "cvc5 should report SAT." << endl;
74  cout << "Result from cvc5 is: " << slv.checkSat() << endl;
75  slv.pop();
76
77  cout << "Thus the maximum value of (y - x) is 2/3."<< endl;
78
79  return 0;
80}