2021-04-12 12:31:43 -07:00
|
|
|
/******************************************************************************
|
|
|
|
|
* Top contributors (to current version):
|
2022-04-05 13:38:57 -07:00
|
|
|
* Aina Niemetz, Mathias Preiner, Tim King
|
2021-04-12 12:31:43 -07:00
|
|
|
*
|
|
|
|
|
* This file is part of the cvc5 project.
|
|
|
|
|
*
|
2022-04-05 13:38:57 -07:00
|
|
|
* Copyright (c) 2009-2022 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.
|
|
|
|
|
* ****************************************************************************
|
|
|
|
|
*
|
2021-04-20 13:25:10 -07:00
|
|
|
* A simple demonstration of the capabilities of cvc5
|
2021-04-12 12:31:43 -07:00
|
|
|
*
|
|
|
|
|
* A simple demonstration of how to use uninterpreted functions, combining this
|
|
|
|
|
* with arithmetic, and extracting a model at the end of a satisfiable query.
|
|
|
|
|
* The model is displayed using getValue().
|
|
|
|
|
*/
|
2012-11-30 18:30:37 +00:00
|
|
|
|
2021-04-05 19:31:28 -07:00
|
|
|
#include <cvc5/cvc5.h>
|
2012-11-30 18:30:37 +00:00
|
|
|
|
2021-04-05 19:31:28 -07:00
|
|
|
#include <iostream>
|
2012-11-30 18:30:37 +00:00
|
|
|
|
|
|
|
|
using namespace std;
|
2022-03-29 16:23:01 -07:00
|
|
|
using namespace cvc5;
|
2012-11-30 18:30:37 +00:00
|
|
|
|
2020-06-23 13:46:02 -07:00
|
|
|
void prefixPrintGetValue(Solver& slv, Term t, int level = 0)
|
|
|
|
|
{
|
|
|
|
|
cout << "slv.getValue(" << t << "): " << slv.getValue(t) << endl;
|
2012-11-30 22:33:28 +00:00
|
|
|
|
2020-06-23 13:46:02 -07:00
|
|
|
for (const Term& c : t)
|
2012-11-30 22:33:28 +00:00
|
|
|
{
|
2020-06-23 13:46:02 -07:00
|
|
|
prefixPrintGetValue(slv, c, level + 1);
|
2012-11-30 22:33:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 13:46:02 -07:00
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
Solver slv;
|
|
|
|
|
slv.setOption("produce-models", "true"); // Produce Models
|
|
|
|
|
slv.setOption("dag-thresh", "0"); // Disable dagifying the output
|
|
|
|
|
slv.setOption("output-language", "smt2"); // use smt-lib v2 as output language
|
|
|
|
|
slv.setLogic(string("QF_UFLIRA"));
|
2012-11-30 18:30:37 +00:00
|
|
|
|
|
|
|
|
// Sorts
|
2020-06-23 13:46:02 -07:00
|
|
|
Sort u = slv.mkUninterpretedSort("u");
|
|
|
|
|
Sort integer = slv.getIntegerSort();
|
|
|
|
|
Sort boolean = slv.getBooleanSort();
|
2022-03-31 21:50:25 -07:00
|
|
|
Sort uToInt = slv.mkFunctionSort({u}, integer);
|
|
|
|
|
Sort intPred = slv.mkFunctionSort({integer}, boolean);
|
2012-11-30 18:30:37 +00:00
|
|
|
|
|
|
|
|
// Variables
|
2020-06-23 13:46:02 -07:00
|
|
|
Term x = slv.mkConst(u, "x");
|
|
|
|
|
Term y = slv.mkConst(u, "y");
|
2012-11-30 18:30:37 +00:00
|
|
|
|
2012-11-30 22:33:28 +00:00
|
|
|
// Functions
|
2020-06-23 13:46:02 -07:00
|
|
|
Term f = slv.mkConst(uToInt, "f");
|
|
|
|
|
Term p = slv.mkConst(intPred, "p");
|
2012-11-30 18:30:37 +00:00
|
|
|
|
|
|
|
|
// Constants
|
2020-10-29 13:26:51 -05:00
|
|
|
Term zero = slv.mkInteger(0);
|
|
|
|
|
Term one = slv.mkInteger(1);
|
2012-11-30 18:30:37 +00:00
|
|
|
|
2012-11-30 22:33:28 +00:00
|
|
|
// Terms
|
2022-03-21 17:22:41 -07:00
|
|
|
Term f_x = slv.mkTerm(APPLY_UF, {f, x});
|
|
|
|
|
Term f_y = slv.mkTerm(APPLY_UF, {f, y});
|
|
|
|
|
Term sum = slv.mkTerm(ADD, {f_x, f_y});
|
|
|
|
|
Term p_0 = slv.mkTerm(APPLY_UF, {p, zero});
|
|
|
|
|
Term p_f_y = slv.mkTerm(APPLY_UF, {p, f_y});
|
2012-11-30 18:30:37 +00:00
|
|
|
|
2020-06-23 13:46:02 -07:00
|
|
|
// Construct the assertions
|
2022-03-21 17:22:41 -07:00
|
|
|
Term assertions =
|
|
|
|
|
slv.mkTerm(AND,
|
|
|
|
|
{
|
|
|
|
|
slv.mkTerm(LEQ, {zero, f_x}), // 0 <= f(x)
|
|
|
|
|
slv.mkTerm(LEQ, {zero, f_y}), // 0 <= f(y)
|
|
|
|
|
slv.mkTerm(LEQ, {sum, one}), // f(x) + f(y) <= 1
|
|
|
|
|
p_0.notTerm(), // not p(0)
|
|
|
|
|
p_f_y // p(f(y))
|
|
|
|
|
});
|
2020-06-23 13:46:02 -07:00
|
|
|
slv.assertFormula(assertions);
|
2012-11-30 22:33:28 +00:00
|
|
|
|
2020-06-23 13:46:02 -07:00
|
|
|
cout << "Given the following assertions:" << endl
|
|
|
|
|
<< assertions << endl << endl;
|
2012-11-30 18:30:37 +00:00
|
|
|
|
2020-06-23 13:46:02 -07:00
|
|
|
cout << "Prove x /= y is entailed. " << endl
|
2022-03-21 17:22:41 -07:00
|
|
|
<< "cvc5: " << slv.checkSatAssuming(slv.mkTerm(EQUAL, {x, y})) << "."
|
2020-06-23 13:46:02 -07:00
|
|
|
<< endl
|
2012-12-01 00:11:20 +00:00
|
|
|
<< endl;
|
2020-06-23 13:46:02 -07:00
|
|
|
|
|
|
|
|
cout << "Call checkSat to show that the assertions are satisfiable. "
|
|
|
|
|
<< endl
|
2021-04-20 13:25:10 -07:00
|
|
|
<< "cvc5: "
|
2020-06-23 13:46:02 -07:00
|
|
|
<< slv.checkSat() << "."<< endl << endl;
|
|
|
|
|
|
|
|
|
|
cout << "Call slv.getValue(...) on terms of interest."
|
|
|
|
|
<< endl;
|
|
|
|
|
cout << "slv.getValue(" << f_x << "): " << slv.getValue(f_x) << endl;
|
|
|
|
|
cout << "slv.getValue(" << f_y << "): " << slv.getValue(f_y) << endl;
|
|
|
|
|
cout << "slv.getValue(" << sum << "): " << slv.getValue(sum) << endl;
|
|
|
|
|
cout << "slv.getValue(" << p_0 << "): " << slv.getValue(p_0) << endl;
|
|
|
|
|
cout << "slv.getValue(" << p_f_y << "): " << slv.getValue(p_f_y)
|
|
|
|
|
<< endl << endl;
|
|
|
|
|
|
|
|
|
|
cout << "Alternatively, iterate over assertions and call slv.getValue(...) "
|
|
|
|
|
<< "on all terms."
|
|
|
|
|
<< endl;
|
|
|
|
|
prefixPrintGetValue(slv, assertions);
|
|
|
|
|
|
|
|
|
|
cout << endl;
|
|
|
|
|
cout << "You can also use nested loops to iterate over terms." << endl;
|
|
|
|
|
for (Term::const_iterator it = assertions.begin();
|
|
|
|
|
it != assertions.end();
|
|
|
|
|
++it)
|
|
|
|
|
{
|
|
|
|
|
cout << "term: " << *it << endl;
|
|
|
|
|
for (Term::const_iterator it2 = (*it).begin();
|
|
|
|
|
it2 != (*it).end();
|
|
|
|
|
++it2)
|
|
|
|
|
{
|
|
|
|
|
cout << " + child: " << *it2 << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cout << endl;
|
|
|
|
|
cout << "Alternatively, you can also use for-each loops." << endl;
|
|
|
|
|
for (const Term& t : assertions)
|
|
|
|
|
{
|
|
|
|
|
cout << "term: " << t << endl;
|
|
|
|
|
for (const Term& c : t)
|
|
|
|
|
{
|
|
|
|
|
cout << " + child: " << c << endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-11-30 18:30:37 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|