Theory Combination

examples/api/cpp/combination.cpp

  1/******************************************************************************
  2 * Top contributors (to current version):
  3 *   Aina Niemetz, Tim King, Mathias Preiner
  4 *
  5 * This file is part of the cvc5 project.
  6 *
  7 * Copyright (c) 2009-2024 by the authors listed in the file AUTHORS
  8 * in the top-level source directory and their institutional affiliations.
  9 * All rights reserved.  See the file COPYING in the top-level source
 10 * directory for licensing information.
 11 * ****************************************************************************
 12 *
 13 * A simple demonstration of the capabilities of cvc5
 14 *
 15 * A simple demonstration of how to use uninterpreted functions, combining this
 16 * with arithmetic, and extracting a model at the end of a satisfiable query.
 17 * The model is displayed using getValue().
 18 */
 19
 20#include <cvc5/cvc5.h>
 21
 22#include <iostream>
 23
 24using namespace std;
 25using namespace cvc5;
 26
 27void prefixPrintGetValue(Solver& slv, Term t, int level = 0)
 28{
 29  cout << "slv.getValue(" << t << "): " << slv.getValue(t) << endl;
 30
 31  for (const Term& c : t)
 32  {
 33    prefixPrintGetValue(slv, c, level + 1);
 34  }
 35}
 36
 37int main()
 38{
 39  TermManager tm;
 40  Solver slv(tm);
 41  slv.setOption("produce-models", "true");  // Produce Models
 42  slv.setOption("dag-thresh", "0"); // Disable dagifying the output
 43  slv.setLogic("QF_UFLIRA");
 44
 45  // Sorts
 46  Sort u = tm.mkUninterpretedSort("u");
 47  Sort integer = tm.getIntegerSort();
 48  Sort boolean = tm.getBooleanSort();
 49  Sort uToInt = tm.mkFunctionSort({u}, integer);
 50  Sort intPred = tm.mkFunctionSort({integer}, boolean);
 51
 52  // Variables
 53  Term x = tm.mkConst(u, "x");
 54  Term y = tm.mkConst(u, "y");
 55
 56  // Functions
 57  Term f = tm.mkConst(uToInt, "f");
 58  Term p = tm.mkConst(intPred, "p");
 59
 60  // Constants
 61  Term zero = tm.mkInteger(0);
 62  Term one = tm.mkInteger(1);
 63
 64  // Terms
 65  Term f_x = tm.mkTerm(Kind::APPLY_UF, {f, x});
 66  Term f_y = tm.mkTerm(Kind::APPLY_UF, {f, y});
 67  Term sum = tm.mkTerm(Kind::ADD, {f_x, f_y});
 68  Term p_0 = tm.mkTerm(Kind::APPLY_UF, {p, zero});
 69  Term p_f_y = tm.mkTerm(Kind::APPLY_UF, {p, f_y});
 70
 71  // Construct the assertions
 72  Term assertions =
 73      tm.mkTerm(Kind::AND,
 74                {
 75                    tm.mkTerm(Kind::LEQ, {zero, f_x}),  // 0 <= f(x)
 76                    tm.mkTerm(Kind::LEQ, {zero, f_y}),  // 0 <= f(y)
 77                    tm.mkTerm(Kind::LEQ, {sum, one}),   // f(x) + f(y) <= 1
 78                    p_0.notTerm(),                      // not p(0)
 79                    p_f_y                               // p(f(y))
 80                });
 81  slv.assertFormula(assertions);
 82
 83  cout << "Given the following assertions:" << endl
 84       << assertions << endl << endl;
 85
 86  cout << "Prove x /= y is entailed." << endl
 87       << "cvc5: " << slv.checkSatAssuming(tm.mkTerm(Kind::EQUAL, {x, y}))
 88       << "." << endl
 89       << endl;
 90
 91  cout << "Call checkSat to show that the assertions are satisfiable." << endl
 92       << "cvc5: " << slv.checkSat() << "." << endl
 93       << endl;
 94
 95  cout << "Call slv.getValue(...) on terms of interest."
 96       << endl;
 97  cout << "slv.getValue(" << f_x << "): " << slv.getValue(f_x) << endl;
 98  cout << "slv.getValue(" << f_y << "): " << slv.getValue(f_y) << endl;
 99  cout << "slv.getValue(" << sum << "): " << slv.getValue(sum) << endl;
100  cout << "slv.getValue(" << p_0 << "): " << slv.getValue(p_0) << endl;
101  cout << "slv.getValue(" << p_f_y << "): " << slv.getValue(p_f_y)
102       << endl << endl;
103
104  cout << "Alternatively, iterate over assertions and call slv.getValue(...) "
105       << "on all terms."
106       << endl;
107  prefixPrintGetValue(slv, assertions);
108
109  cout << endl;
110  cout << "You can also use nested loops to iterate over terms." << endl;
111  for (Term::const_iterator it = assertions.begin();
112       it != assertions.end();
113       ++it)
114  {
115    cout << "term: " << *it << endl;
116    for (Term::const_iterator it2 = (*it).begin();
117         it2 != (*it).end();
118         ++it2)
119    {
120      cout << " + child: " << *it2 << std::endl;
121    }
122  }
123  cout << endl;
124  cout << "Alternatively, you can also use for-each loops." << endl;
125  for (const Term& t : assertions)
126  {
127    cout << "term: " << t << endl;
128    for (const Term& c : t)
129    {
130      cout << " + child: " << c << endl;
131    }
132  }
133
134  return 0;
135}